gemikenwork / runtime-dist /runtime /runtimeClient.js
burtenshaw's picture
burtenshaw HF Staff
Serve WorkAdventure map from Hub CDN and reduce agent polling
a1a1b60 verified
Raw History Blame Contribute Delete
4.54 kB
export class HttpRuntimeClient {
baseUrl;
controlToken;
constructor(baseUrl = process.env.WA_AGENT_RUNTIME_URL ?? "http://127.0.0.1:8787") {
this.baseUrl = baseUrl.replace(/\/$/, "");
this.controlToken = nonEmptyEnv("WA_AGENT_CONTROL_TOKEN") ?? nonEmptyEnv("WA_AGENT_BRIDGE_SECRET");
}
async listSessions() {
const response = await this.request("/sessions");
return response.sessions;
}
getSession(sessionId) {
return this.request(`/sessions/${encodeURIComponent(sessionId)}`);
}
enqueueAction(sessionId, action) {
return this.request(`/sessions/${encodeURIComponent(sessionId)}/actions`, {
method: "POST",
body: { action },
});
}
say(sessionId, text, options = {}) {
return this.request(`/sessions/${encodeURIComponent(sessionId)}/say`, {
method: "POST",
body: {
text,
channel: options.channel ?? "bubble",
voice: options.voice ?? false,
},
});
}
speak(sessionId, text) {
return this.request(`/sessions/${encodeURIComponent(sessionId)}/speak`, {
method: "POST",
body: { text },
});
}
sendAgentMessage(targetSessionId, fromSessionId, message, data) {
return this.request(`/sessions/${encodeURIComponent(targetSessionId)}/agent-messages`, {
method: "POST",
body: {
fromSessionId,
message,
data,
},
});
}
async createBridgeUrl(options) {
const url = new URL("/workadventure/bridge-url", this.baseUrl);
url.searchParams.set("session", options.sessionId);
if (options.iframeApiUrl) {
url.searchParams.set("waApi", options.iframeApiUrl);
}
const response = await this.request(url.pathname + url.search);
return response.url;
}
getHutterState() {
return this.request("/hutter/state");
}
getHutterEntity(entityId) {
return this.request(`/hutter/entities/${encodeURIComponent(entityId)}`);
}
async submitHutterResult(input) {
const response = await this.request("/hutter/results", {
method: "POST",
body: input,
});
return response.result;
}
async postHutterMessage(input) {
const response = await this.request("/hutter/messages", {
method: "POST",
body: input,
});
return response.message;
}
getHutterLecture() {
return this.request("/hutter/lecture");
}
async startHutterLecture(input) {
const response = await this.request("/hutter/lecture/start", {
method: "POST",
body: input,
});
return response.lecture;
}
async nextHutterLectureSlide() {
const response = await this.request("/hutter/lecture/next", {
method: "POST",
body: {},
});
return response.lecture;
}
async stopHutterLecture() {
const response = await this.request("/hutter/lecture/stop", {
method: "POST",
body: {},
});
return response.lecture;
}
async addHutterLectureTranscript(input) {
const response = await this.request("/hutter/lecture/transcript", {
method: "POST",
body: input,
});
return response.lecture;
}
async request(path, options = {}) {
const headers = {};
if (options.body !== undefined) {
headers["Content-Type"] = "application/json";
}
if (this.controlToken) {
headers.Authorization = `Bearer ${this.controlToken}`;
}
const response = await fetch(`${this.baseUrl}${path}`, {
method: options.method ?? "GET",
headers: Object.keys(headers).length === 0 ? undefined : headers,
body: options.body === undefined ? undefined : JSON.stringify(options.body),
});
const text = await response.text();
const data = text ? JSON.parse(text) : undefined;
if (!response.ok) {
throw new Error(typeof data?.error === "string" ? data.error : `Runtime request failed with ${response.status}`);
}
return data;
}
}
function nonEmptyEnv(name) {
const value = process.env[name]?.trim();
return value ? value : undefined;
}
//# sourceMappingURL=runtimeClient.js.map