File size: 4,537 Bytes
8eaf022
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1a1b60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8eaf022
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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