@useago/sdk 0.3.3 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/AgoClient-B6PsfMeg.cjs +6 -0
- package/dist/{AgoClient-DF7M2WAz.js → AgoClient-BerJvssl.js} +235 -219
- package/dist/angular.cjs +1 -1
- package/dist/angular.d.cts +12 -0
- package/dist/angular.d.ts +12 -0
- package/dist/angular.js +1 -1
- package/dist/createConversationSession-CLZvxyuu.js +58 -0
- package/dist/createConversationSession-D3K5Gf1k.cjs +1 -0
- package/dist/createFormCollector-CD1rqRG4.js +208 -0
- package/dist/createFormCollector-D1FfiSgA.cjs +1 -0
- package/dist/createMockClient-DMFGIOtc.cjs +1 -0
- package/dist/createMockClient-tVsApEAh.js +72 -0
- package/dist/devtools.cjs +26 -26
- package/dist/devtools.d.cts +16 -26
- package/dist/devtools.d.ts +16 -26
- package/dist/devtools.js +55 -55
- package/dist/helpers.cjs +1 -1
- package/dist/helpers.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +102 -1
- package/dist/index.d.ts +102 -1
- package/dist/index.js +46 -43
- package/dist/react.cjs +19 -19
- package/dist/react.d.cts +67 -8
- package/dist/react.d.ts +67 -8
- package/dist/react.js +1066 -1051
- package/dist/vue.cjs +1 -1
- package/dist/vue.d.cts +12 -0
- package/dist/vue.d.ts +12 -0
- package/dist/vue.js +1 -1
- package/dist/widget.cjs +1 -0
- package/dist/widget.d.cts +635 -0
- package/dist/widget.d.ts +635 -0
- package/dist/widget.js +418 -0
- package/package.json +58 -15
- package/dist/AgoClient-bjN-z2ZQ.cjs +0 -6
- package/dist/createMockClient-D6uVh-B9.js +0 -254
- package/dist/createMockClient-DecMEUcK.cjs +0 -1
- /package/dist/{functions-DdROP6Ui.js → functions-C1-5vadY.js} +0 -0
- /package/dist/{functions-Bz-1Bs3I.cjs → functions-DsmzgHjC.cjs} +0 -0
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
var
|
|
2
|
-
constructor(e, t,
|
|
3
|
-
super(e), this.code = t, this.statusCode =
|
|
1
|
+
var d = class extends Error {
|
|
2
|
+
constructor(e, t, s) {
|
|
3
|
+
super(e), this.code = t, this.statusCode = s, this.name = "AgoError";
|
|
4
4
|
}
|
|
5
|
-
},
|
|
6
|
-
constructor(t,
|
|
7
|
-
super(t,
|
|
5
|
+
}, h = class p extends d {
|
|
6
|
+
constructor(t, s, r, i, o, a) {
|
|
7
|
+
super(t, s, r), this.type = i, this.param = o, this.docUrl = a, this.name = "AgoApiError";
|
|
8
8
|
}
|
|
9
|
-
static fromResponse(t,
|
|
10
|
-
const
|
|
11
|
-
return new
|
|
9
|
+
static fromResponse(t, s) {
|
|
10
|
+
const r = t.error;
|
|
11
|
+
return new p(r.message, r.code, s, r.type, r.param, r.doc_url);
|
|
12
12
|
}
|
|
13
|
-
},
|
|
13
|
+
}, u = class extends d {
|
|
14
14
|
constructor(e, t) {
|
|
15
15
|
super(e, "network_error"), this.originalError = t, this.name = "AgoNetworkError";
|
|
16
16
|
}
|
|
17
|
-
},
|
|
17
|
+
}, g = class extends d {
|
|
18
18
|
constructor(e) {
|
|
19
19
|
super(e, "stream_error"), this.name = "AgoStreamError";
|
|
20
20
|
}
|
|
21
|
-
},
|
|
22
|
-
constructor(e, t,
|
|
23
|
-
super(e, "function_error"), this.functionName = t, this.originalError =
|
|
21
|
+
}, m = class extends d {
|
|
22
|
+
constructor(e, t, s) {
|
|
23
|
+
super(e, "function_error"), this.functionName = t, this.originalError = s, this.name = "AgoFunctionError";
|
|
24
24
|
}
|
|
25
|
-
},
|
|
25
|
+
}, y = class {
|
|
26
26
|
constructor(e = !1) {
|
|
27
27
|
this.enabled = e;
|
|
28
28
|
}
|
|
@@ -44,8 +44,8 @@ var h = class extends Error {
|
|
|
44
44
|
debug(...e) {
|
|
45
45
|
this.enabled && console.debug("[AGO SDK]", ...e);
|
|
46
46
|
}
|
|
47
|
-
}, n = new
|
|
48
|
-
function
|
|
47
|
+
}, n = new y(), f = "ago_widget_id";
|
|
48
|
+
function C() {
|
|
49
49
|
if (typeof localStorage < "u") {
|
|
50
50
|
const t = localStorage.getItem(f);
|
|
51
51
|
if (t) return t;
|
|
@@ -57,9 +57,9 @@ function v() {
|
|
|
57
57
|
}
|
|
58
58
|
return e;
|
|
59
59
|
}
|
|
60
|
-
var
|
|
60
|
+
var E = class {
|
|
61
61
|
constructor(e) {
|
|
62
|
-
this.baseUrl = e.baseUrl.replace(/\/$/, ""), this.headers = { "X-Widget-Id": e.widgetId ||
|
|
62
|
+
this.baseUrl = e.baseUrl.replace(/\/$/, ""), this.headers = { "X-Widget-Id": e.widgetId || C() }, e.userEmail && (this.headers["X-User-Email"] = e.userEmail), e.userJwt && (this.headers.Authorization = `Bearer ${e.userJwt}`), e.permission && (this.headers["X-Widget-Permission"] = e.permission);
|
|
63
63
|
}
|
|
64
64
|
updateConfig(e) {
|
|
65
65
|
e.baseUrl && (this.baseUrl = e.baseUrl.replace(/\/$/, "")), e.widgetId && (this.headers["X-Widget-Id"] = e.widgetId), e.userEmail && (this.headers["X-User-Email"] = e.userEmail), e.userJwt && (this.headers.Authorization = `Bearer ${e.userJwt}`), e.permission !== void 0 && (e.permission ? this.headers["X-Widget-Permission"] = e.permission : delete this.headers["X-Widget-Permission"]);
|
|
@@ -71,10 +71,10 @@ var y = class {
|
|
|
71
71
|
return this.request("POST", e, t);
|
|
72
72
|
}
|
|
73
73
|
async postStream(e, t) {
|
|
74
|
-
const
|
|
75
|
-
n.debug("POST (stream)",
|
|
74
|
+
const s = `${this.baseUrl}${e}`;
|
|
75
|
+
n.debug("POST (stream)", s, t);
|
|
76
76
|
try {
|
|
77
|
-
const
|
|
77
|
+
const r = await fetch(s, {
|
|
78
78
|
method: "POST",
|
|
79
79
|
headers: {
|
|
80
80
|
...this.headers,
|
|
@@ -82,41 +82,41 @@ var y = class {
|
|
|
82
82
|
},
|
|
83
83
|
body: t ? JSON.stringify(t) : void 0
|
|
84
84
|
});
|
|
85
|
-
return
|
|
86
|
-
} catch (
|
|
87
|
-
throw
|
|
85
|
+
return r.ok || await this.handleErrorResponse(r), r;
|
|
86
|
+
} catch (r) {
|
|
87
|
+
throw r instanceof h ? r : new u(`Network error: ${r instanceof Error ? r.message : "Unknown error"}`, r instanceof Error ? r : void 0);
|
|
88
88
|
}
|
|
89
89
|
}
|
|
90
90
|
async postFormData(e, t) {
|
|
91
|
-
const
|
|
92
|
-
n.debug("POST (formData)",
|
|
93
|
-
const
|
|
91
|
+
const s = `${this.baseUrl}${e}`;
|
|
92
|
+
n.debug("POST (formData)", s);
|
|
93
|
+
const r = { ...this.headers };
|
|
94
94
|
try {
|
|
95
|
-
const i = await fetch(
|
|
95
|
+
const i = await fetch(s, {
|
|
96
96
|
method: "POST",
|
|
97
|
-
headers:
|
|
97
|
+
headers: r,
|
|
98
98
|
body: t
|
|
99
99
|
});
|
|
100
100
|
return i.ok || await this.handleErrorResponse(i), i;
|
|
101
101
|
} catch (i) {
|
|
102
|
-
throw i instanceof
|
|
102
|
+
throw i instanceof h ? i : new u(`Network error: ${i instanceof Error ? i.message : "Unknown error"}`, i instanceof Error ? i : void 0);
|
|
103
103
|
}
|
|
104
104
|
}
|
|
105
|
-
async request(e, t,
|
|
106
|
-
const
|
|
107
|
-
n.debug(e,
|
|
105
|
+
async request(e, t, s) {
|
|
106
|
+
const r = `${this.baseUrl}${t}`;
|
|
107
|
+
n.debug(e, r, s);
|
|
108
108
|
try {
|
|
109
|
-
const i = await fetch(
|
|
109
|
+
const i = await fetch(r, {
|
|
110
110
|
method: e,
|
|
111
111
|
headers: {
|
|
112
112
|
...this.headers,
|
|
113
113
|
"Content-Type": "application/json"
|
|
114
114
|
},
|
|
115
|
-
body:
|
|
115
|
+
body: s ? JSON.stringify(s) : void 0
|
|
116
116
|
});
|
|
117
117
|
return i.ok || await this.handleErrorResponse(i), i.status === 204 ? void 0 : i.json();
|
|
118
118
|
} catch (i) {
|
|
119
|
-
throw i instanceof
|
|
119
|
+
throw i instanceof h ? i : new u(`Network error: ${i instanceof Error ? i.message : "Unknown error"}`, i instanceof Error ? i : void 0);
|
|
120
120
|
}
|
|
121
121
|
}
|
|
122
122
|
async handleErrorResponse(e) {
|
|
@@ -125,13 +125,13 @@ var y = class {
|
|
|
125
125
|
t = await e.json();
|
|
126
126
|
} catch {
|
|
127
127
|
}
|
|
128
|
-
throw t?.error ?
|
|
128
|
+
throw t?.error ? h.fromResponse(t, e.status) : new h(`HTTP ${e.status}: ${e.statusText}`, "http_error", e.status, "api_error");
|
|
129
129
|
}
|
|
130
|
-
},
|
|
130
|
+
}, b = class {
|
|
131
131
|
constructor() {
|
|
132
132
|
this.functions = /* @__PURE__ */ new Map();
|
|
133
133
|
}
|
|
134
|
-
register(e, t,
|
|
134
|
+
register(e, t, s) {
|
|
135
135
|
if (typeof e == "object") {
|
|
136
136
|
const { name: i, handler: o, description: a, parameters: c } = e;
|
|
137
137
|
return this.register(i, o, {
|
|
@@ -139,15 +139,15 @@ var y = class {
|
|
|
139
139
|
parameters: c
|
|
140
140
|
});
|
|
141
141
|
}
|
|
142
|
-
const
|
|
143
|
-
if (!t || !
|
|
144
|
-
this.functions.has(
|
|
142
|
+
const r = e;
|
|
143
|
+
if (!t || !s) throw new Error("handler and schema are required");
|
|
144
|
+
this.functions.has(r) && n.warn(`Function "${r}" is being overwritten`), this.functions.set(r, {
|
|
145
145
|
schema: {
|
|
146
|
-
...
|
|
147
|
-
name:
|
|
146
|
+
...s,
|
|
147
|
+
name: r
|
|
148
148
|
},
|
|
149
149
|
handler: t
|
|
150
|
-
}), n.log(`Registered function: ${
|
|
150
|
+
}), n.log(`Registered function: ${r}`);
|
|
151
151
|
}
|
|
152
152
|
unregister(e) {
|
|
153
153
|
const t = this.functions.delete(e);
|
|
@@ -163,14 +163,14 @@ var y = class {
|
|
|
163
163
|
return Array.from(this.functions.values()).map((e) => e.schema);
|
|
164
164
|
}
|
|
165
165
|
async execute(e, t) {
|
|
166
|
-
const
|
|
167
|
-
if (!
|
|
166
|
+
const s = this.functions.get(e);
|
|
167
|
+
if (!s) throw new m(`Function "${e}" is not registered`, e);
|
|
168
168
|
n.log(`Executing function: ${e}`, t);
|
|
169
169
|
try {
|
|
170
|
-
const
|
|
171
|
-
return n.log(`Function ${e} completed:`,
|
|
172
|
-
} catch (
|
|
173
|
-
throw n.error(`Function ${e} failed:`,
|
|
170
|
+
const r = await s.handler(t);
|
|
171
|
+
return n.log(`Function ${e} completed:`, r), r;
|
|
172
|
+
} catch (r) {
|
|
173
|
+
throw n.error(`Function ${e} failed:`, r), new m(`Function "${e}" execution failed: ${r instanceof Error ? r.message : "Unknown error"}`, e, r instanceof Error ? r : void 0);
|
|
174
174
|
}
|
|
175
175
|
}
|
|
176
176
|
clear() {
|
|
@@ -179,7 +179,7 @@ var y = class {
|
|
|
179
179
|
get size() {
|
|
180
180
|
return this.functions.size;
|
|
181
181
|
}
|
|
182
|
-
},
|
|
182
|
+
}, _ = class {
|
|
183
183
|
constructor() {
|
|
184
184
|
this.entries = /* @__PURE__ */ new Map(), this.dynamicProviders = /* @__PURE__ */ new Map();
|
|
185
185
|
}
|
|
@@ -200,35 +200,35 @@ var y = class {
|
|
|
200
200
|
getSnapshot() {
|
|
201
201
|
if (this.entries.size === 0 && this.dynamicProviders.size === 0) return null;
|
|
202
202
|
const e = {};
|
|
203
|
-
for (const [t,
|
|
204
|
-
for (const [t,
|
|
205
|
-
const
|
|
206
|
-
|
|
207
|
-
} catch (
|
|
208
|
-
n.error(`DynamicContext provider "${t}" threw:`,
|
|
203
|
+
for (const [t, s] of this.entries) e[t] = s;
|
|
204
|
+
for (const [t, s] of this.dynamicProviders) try {
|
|
205
|
+
const r = s();
|
|
206
|
+
r && (e[t] = r);
|
|
207
|
+
} catch (r) {
|
|
208
|
+
n.error(`DynamicContext provider "${t}" threw:`, r);
|
|
209
209
|
}
|
|
210
210
|
return Object.keys(e).length === 0 ? null : { entries: e };
|
|
211
211
|
}
|
|
212
212
|
clear() {
|
|
213
213
|
this.entries.clear(), this.dynamicProviders.clear(), n.log("ClientContext cleared");
|
|
214
214
|
}
|
|
215
|
-
},
|
|
215
|
+
}, k = class {
|
|
216
216
|
constructor(e) {
|
|
217
217
|
this.callbacks = e, this.buffer = "", this.message = {}, this.toolCalls = [], this.sources = [], this.followUpReplies = [], this.isFirstChunk = !0;
|
|
218
218
|
}
|
|
219
219
|
async processStream(e) {
|
|
220
|
-
if (!e.body) throw new
|
|
221
|
-
const t = e.body.getReader(),
|
|
220
|
+
if (!e.body) throw new g("Response has no body");
|
|
221
|
+
const t = e.body.getReader(), s = new TextDecoder();
|
|
222
222
|
try {
|
|
223
223
|
for (; ; ) {
|
|
224
|
-
const { done:
|
|
225
|
-
if (
|
|
226
|
-
const o =
|
|
224
|
+
const { done: r, value: i } = await t.read();
|
|
225
|
+
if (r) break;
|
|
226
|
+
const o = s.decode(i, { stream: !0 });
|
|
227
227
|
this.buffer += o, this.processBuffer();
|
|
228
228
|
}
|
|
229
229
|
return this.buffer.trim() && this.processBuffer(), this.buildFinalMessage();
|
|
230
|
-
} catch (
|
|
231
|
-
const i =
|
|
230
|
+
} catch (r) {
|
|
231
|
+
const i = r instanceof Error ? r : new g("Stream processing failed");
|
|
232
232
|
throw this.callbacks.onError?.(i), i;
|
|
233
233
|
} finally {
|
|
234
234
|
t.releaseLock();
|
|
@@ -241,11 +241,11 @@ var y = class {
|
|
|
241
241
|
this.buffer = e.pop() || "";
|
|
242
242
|
for (const t of e) {
|
|
243
243
|
if (!t.trim()) continue;
|
|
244
|
-
const
|
|
244
|
+
const s = t.split(`
|
|
245
245
|
`);
|
|
246
|
-
for (const
|
|
247
|
-
if (!
|
|
248
|
-
const i =
|
|
246
|
+
for (const r of s)
|
|
247
|
+
if (!r.startsWith(": ") && r.startsWith("data: ")) {
|
|
248
|
+
const i = r.slice(6);
|
|
249
249
|
try {
|
|
250
250
|
const o = JSON.parse(i);
|
|
251
251
|
this.handleChunk(o);
|
|
@@ -268,11 +268,11 @@ var y = class {
|
|
|
268
268
|
name: e.agent.name,
|
|
269
269
|
displayName: e.agent.display_name
|
|
270
270
|
}), e.knowledge_sources && (this.sources = e.knowledge_sources.map((t) => {
|
|
271
|
-
const
|
|
271
|
+
const s = t.knowledge_document;
|
|
272
272
|
return {
|
|
273
|
-
id:
|
|
274
|
-
title:
|
|
275
|
-
url:
|
|
273
|
+
id: s.id,
|
|
274
|
+
title: s.title,
|
|
275
|
+
url: s.use_external_link ? s.external_link_url : s.internal_link_url
|
|
276
276
|
};
|
|
277
277
|
})), e.type === "client_function" && e.function_name && this.callbacks.onClientFunction?.({
|
|
278
278
|
invocationId: e.id || "",
|
|
@@ -282,8 +282,8 @@ var y = class {
|
|
|
282
282
|
}), e.tool_call_data && e.type && e.type !== "client_function") {
|
|
283
283
|
const t = this.parseToolCall(e);
|
|
284
284
|
this.callbacks.onToolCall?.(t);
|
|
285
|
-
const
|
|
286
|
-
|
|
285
|
+
const s = this.toolCalls.findIndex((r) => r.id === t.id);
|
|
286
|
+
s >= 0 ? this.toolCalls[s] = t : this.toolCalls.push(t);
|
|
287
287
|
}
|
|
288
288
|
e.follow_up_replies && (this.followUpReplies = e.follow_up_replies);
|
|
289
289
|
}
|
|
@@ -317,11 +317,11 @@ var y = class {
|
|
|
317
317
|
return this.callbacks.onComplete?.(e), e;
|
|
318
318
|
}
|
|
319
319
|
};
|
|
320
|
-
function
|
|
320
|
+
function x(e) {
|
|
321
321
|
const t = e.message.toLowerCase();
|
|
322
322
|
return t.includes("load failed") || t.includes("failed to fetch") || t.includes("network") || t.includes("abort");
|
|
323
323
|
}
|
|
324
|
-
var
|
|
324
|
+
var S = class {
|
|
325
325
|
constructor() {
|
|
326
326
|
this.handlers = /* @__PURE__ */ new Map();
|
|
327
327
|
}
|
|
@@ -329,175 +329,191 @@ var _ = class {
|
|
|
329
329
|
this.handlers.has(e) || this.handlers.set(e, /* @__PURE__ */ new Set()), this.handlers.get(e).add(t);
|
|
330
330
|
}
|
|
331
331
|
off(e, t) {
|
|
332
|
-
const
|
|
333
|
-
|
|
332
|
+
const s = this.handlers.get(e);
|
|
333
|
+
s && s.delete(t);
|
|
334
334
|
}
|
|
335
335
|
emit(e, t) {
|
|
336
|
-
const
|
|
337
|
-
|
|
336
|
+
const s = this.handlers.get(e);
|
|
337
|
+
s && s.forEach((r) => {
|
|
338
338
|
try {
|
|
339
|
-
|
|
339
|
+
r(t);
|
|
340
340
|
} catch (i) {
|
|
341
341
|
console.error(`Error in event handler for ${String(e)}:`, i);
|
|
342
342
|
}
|
|
343
343
|
});
|
|
344
344
|
}
|
|
345
345
|
once(e, t) {
|
|
346
|
-
const
|
|
347
|
-
this.off(e,
|
|
346
|
+
const s = ((r) => {
|
|
347
|
+
this.off(e, s), t(r);
|
|
348
348
|
});
|
|
349
|
-
this.on(e,
|
|
349
|
+
this.on(e, s);
|
|
350
350
|
}
|
|
351
351
|
waitFor(e, t) {
|
|
352
|
-
return new Promise((
|
|
352
|
+
return new Promise((s, r) => {
|
|
353
353
|
let i;
|
|
354
354
|
const o = ((a) => {
|
|
355
|
-
i && clearTimeout(i),
|
|
355
|
+
i && clearTimeout(i), s(a);
|
|
356
356
|
});
|
|
357
357
|
this.once(e, o), t?.timeout && (i = setTimeout(() => {
|
|
358
|
-
this.off(e, o),
|
|
358
|
+
this.off(e, o), r(/* @__PURE__ */ new Error(`waitFor("${String(e)}") timed out after ${t.timeout}ms`));
|
|
359
359
|
}, t.timeout));
|
|
360
360
|
});
|
|
361
361
|
}
|
|
362
362
|
removeAllListeners(e) {
|
|
363
363
|
e ? this.handlers.delete(e) : this.handlers.clear();
|
|
364
364
|
}
|
|
365
|
-
},
|
|
366
|
-
constructor(
|
|
367
|
-
this.config =
|
|
365
|
+
}, $ = class v {
|
|
366
|
+
constructor(t) {
|
|
367
|
+
this.config = t, this.httpClient = new E(t), this.functionRegistry = new b(), this.contextRegistry = new _(), this.eventEmitter = new S(), t.debug && n.enable(), n.log("AgoClient initialized");
|
|
368
368
|
}
|
|
369
|
-
async sendMessage(
|
|
370
|
-
const r = this.functionRegistry.getSchemas(),
|
|
371
|
-
content:
|
|
372
|
-
conversation_id:
|
|
373
|
-
agent_id:
|
|
369
|
+
async sendMessage(t, s) {
|
|
370
|
+
const r = this.functionRegistry.getSchemas(), i = this.contextRegistry.getSnapshot(), o = this.config.agent || this.config.defaultAgentId, a = {
|
|
371
|
+
content: t,
|
|
372
|
+
conversation_id: s?.conversationId,
|
|
373
|
+
agent_id: s?.agentId || o
|
|
374
374
|
};
|
|
375
|
-
r.length > 0 && (
|
|
376
|
-
let
|
|
377
|
-
if (
|
|
378
|
-
const
|
|
379
|
-
|
|
380
|
-
for (const
|
|
381
|
-
|
|
382
|
-
} else
|
|
383
|
-
return this.processSSEResponse(
|
|
384
|
-
}
|
|
385
|
-
async processSSEResponse(
|
|
386
|
-
return new
|
|
387
|
-
onStart: (
|
|
388
|
-
this.eventEmitter.emit("message:start",
|
|
375
|
+
r.length > 0 && (a.client_functions = r), i && (a.client_context = i);
|
|
376
|
+
let c;
|
|
377
|
+
if (s?.files && s.files.length > 0) {
|
|
378
|
+
const l = new FormData();
|
|
379
|
+
l.append("content", t), s.conversationId && l.append("conversation_id", s.conversationId), (s.agentId || o) && l.append("agent_id", s.agentId || o || ""), r.length > 0 && l.append("client_functions", JSON.stringify(r)), i && l.append("client_context", JSON.stringify(i));
|
|
380
|
+
for (const w of s.files) l.append("files", w);
|
|
381
|
+
c = await this.httpClient.postFormData("/api/sdk/v1/messages", l);
|
|
382
|
+
} else c = await this.httpClient.postStream("/api/sdk/v1/messages", a);
|
|
383
|
+
return this.processSSEResponse(c);
|
|
384
|
+
}
|
|
385
|
+
async processSSEResponse(t) {
|
|
386
|
+
return new k({
|
|
387
|
+
onStart: (s) => {
|
|
388
|
+
this.eventEmitter.emit("message:start", s);
|
|
389
389
|
},
|
|
390
|
-
onChunk: (
|
|
391
|
-
this.eventEmitter.emit("message:chunk",
|
|
390
|
+
onChunk: (s) => {
|
|
391
|
+
this.eventEmitter.emit("message:chunk", s);
|
|
392
392
|
},
|
|
393
|
-
onToolCall: (
|
|
394
|
-
this.eventEmitter.emit("toolCall:received",
|
|
393
|
+
onToolCall: (s) => {
|
|
394
|
+
this.eventEmitter.emit("toolCall:received", s), s.type === "form" && this.eventEmitter.emit("toolCall:form", s);
|
|
395
395
|
},
|
|
396
|
-
onClientFunction: async (
|
|
397
|
-
this.eventEmitter.emit("function:invoke",
|
|
396
|
+
onClientFunction: async (s) => {
|
|
397
|
+
this.eventEmitter.emit("function:invoke", s), await this.handleClientFunctionInvocation(s);
|
|
398
398
|
},
|
|
399
|
-
onComplete: (
|
|
400
|
-
this.eventEmitter.emit("message:complete",
|
|
399
|
+
onComplete: (s) => {
|
|
400
|
+
this.eventEmitter.emit("message:complete", s);
|
|
401
401
|
},
|
|
402
|
-
onError: (
|
|
403
|
-
this.eventEmitter.emit("message:error", { error:
|
|
402
|
+
onError: (s) => {
|
|
403
|
+
this.eventEmitter.emit("message:error", { error: s.message });
|
|
404
404
|
}
|
|
405
|
-
}).processStream(
|
|
405
|
+
}).processStream(t);
|
|
406
406
|
}
|
|
407
|
-
async handleClientFunctionInvocation(
|
|
408
|
-
let
|
|
407
|
+
async handleClientFunctionInvocation(t) {
|
|
408
|
+
let s, r;
|
|
409
409
|
try {
|
|
410
|
-
|
|
411
|
-
} catch (
|
|
412
|
-
r =
|
|
410
|
+
s = await this.functionRegistry.execute(t.functionName, t.arguments);
|
|
411
|
+
} catch (i) {
|
|
412
|
+
r = i instanceof Error ? i.message : "Unknown error", n.error("Client function execution failed:", i);
|
|
413
413
|
}
|
|
414
|
-
if (
|
|
415
|
-
await this.submitToolCallForm(
|
|
416
|
-
result:
|
|
414
|
+
if (t.invocationId) try {
|
|
415
|
+
await this.submitToolCallForm(t.invocationId, {
|
|
416
|
+
result: s,
|
|
417
417
|
error: r,
|
|
418
418
|
_type: "client_function_result"
|
|
419
419
|
});
|
|
420
|
-
} catch (
|
|
421
|
-
n.error("Failed to submit function result:",
|
|
420
|
+
} catch (i) {
|
|
421
|
+
n.error("Failed to submit function result:", i);
|
|
422
422
|
}
|
|
423
423
|
this.eventEmitter.emit("function:result", {
|
|
424
|
-
invocationId:
|
|
425
|
-
result:
|
|
424
|
+
invocationId: t.invocationId,
|
|
425
|
+
result: s,
|
|
426
426
|
error: r
|
|
427
427
|
});
|
|
428
428
|
}
|
|
429
429
|
async getConversations() {
|
|
430
|
-
return (await this.httpClient.get("/api/sdk/v1/conversations")).items.map((
|
|
431
|
-
id: e.id,
|
|
432
|
-
title: e.title,
|
|
433
|
-
lastMessageDate: new Date(e.last_message_date)
|
|
434
|
-
}));
|
|
435
|
-
}
|
|
436
|
-
async getConversation(e) {
|
|
437
|
-
const t = await this.httpClient.get(`/api/sdk/v1/conversations/${e}`);
|
|
438
|
-
return {
|
|
430
|
+
return (await this.httpClient.get("/api/sdk/v1/conversations")).items.map((t) => ({
|
|
439
431
|
id: t.id,
|
|
440
432
|
title: t.title,
|
|
441
|
-
lastMessageDate: new Date(t.last_message_date)
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
433
|
+
lastMessageDate: new Date(t.last_message_date)
|
|
434
|
+
}));
|
|
435
|
+
}
|
|
436
|
+
async getConversation(t) {
|
|
437
|
+
const s = await this.httpClient.get(`/api/sdk/v1/conversations/${t}`), r = {
|
|
438
|
+
id: s.id,
|
|
439
|
+
title: s.title,
|
|
440
|
+
lastMessageDate: new Date(s.last_message_date),
|
|
441
|
+
messages: s.messages.map((i) => ({
|
|
442
|
+
id: i.id,
|
|
443
|
+
conversationId: s.id,
|
|
444
|
+
content: i.content,
|
|
445
|
+
role: i.role,
|
|
446
|
+
status: i.status,
|
|
447
|
+
createdAt: new Date(i.created_at),
|
|
448
|
+
toolCalls: v.mapPersistedToolCalls(i.tool_call_data)
|
|
449
449
|
}))
|
|
450
450
|
};
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
451
|
+
return this.eventEmitter.emit("conversation:loaded", r), r;
|
|
452
|
+
}
|
|
453
|
+
static mapPersistedToolCalls(t) {
|
|
454
|
+
if (!(!t || t.length === 0))
|
|
455
|
+
return t.map((s) => ({
|
|
456
|
+
id: String(s.id ?? ""),
|
|
457
|
+
type: s.type ?? "status_message",
|
|
458
|
+
status: String(s.status ?? "unknown"),
|
|
459
|
+
toolName: s.tool_name ?? "",
|
|
460
|
+
toolDisplayName: s.tool_display_name,
|
|
461
|
+
message: s.message,
|
|
462
|
+
formSchema: s.form_schema,
|
|
463
|
+
data: s.data,
|
|
464
|
+
functionName: s.function_name,
|
|
465
|
+
arguments: s.arguments
|
|
466
|
+
}));
|
|
467
|
+
}
|
|
468
|
+
async getMessages(t) {
|
|
469
|
+
return (await this.getConversation(t)).messages || [];
|
|
470
|
+
}
|
|
471
|
+
async submitToolCallForm(t, s) {
|
|
472
|
+
await this.httpClient.post(`/api/sdk/v1/tool-calls/${t}/submit`, { formData: s });
|
|
473
|
+
}
|
|
474
|
+
async confirmToolCall(t) {
|
|
475
|
+
await this.httpClient.post(`/api/tool-calls/${t}/confirm/`);
|
|
476
|
+
}
|
|
477
|
+
async rejectToolCall(t) {
|
|
478
|
+
await this.httpClient.post(`/api/tool-calls/${t}/reject/`);
|
|
479
|
+
}
|
|
480
|
+
async submitFormCollector(t, s) {
|
|
465
481
|
return this.httpClient.post("/api/sdk/v1/forms/submit", {
|
|
466
|
-
destination:
|
|
467
|
-
values:
|
|
482
|
+
destination: t,
|
|
483
|
+
values: s
|
|
468
484
|
});
|
|
469
485
|
}
|
|
470
|
-
async submitFeedback(
|
|
471
|
-
await this.httpClient.post(`/api/sdk/v1/messages/${
|
|
486
|
+
async submitFeedback(t, s) {
|
|
487
|
+
await this.httpClient.post(`/api/sdk/v1/messages/${t}/feedback`, { rating: s });
|
|
472
488
|
}
|
|
473
|
-
registerFunction(
|
|
474
|
-
typeof
|
|
489
|
+
registerFunction(t, s, r) {
|
|
490
|
+
typeof t == "object" ? this.functionRegistry.register(t) : this.functionRegistry.register(t, s, r);
|
|
475
491
|
}
|
|
476
|
-
register(
|
|
477
|
-
if (Array.isArray(
|
|
478
|
-
else this.functionRegistry.register(
|
|
492
|
+
register(t) {
|
|
493
|
+
if (Array.isArray(t)) for (const s of t) this.functionRegistry.register(s);
|
|
494
|
+
else this.functionRegistry.register(t);
|
|
479
495
|
}
|
|
480
|
-
unregisterFunction(
|
|
481
|
-
return this.functionRegistry.unregister(
|
|
496
|
+
unregisterFunction(t) {
|
|
497
|
+
return this.functionRegistry.unregister(t);
|
|
482
498
|
}
|
|
483
499
|
getRegisteredFunctions() {
|
|
484
500
|
return this.functionRegistry.getSchemas();
|
|
485
501
|
}
|
|
486
|
-
registerNavigationFunction(
|
|
487
|
-
const r =
|
|
502
|
+
registerNavigationFunction(t, s) {
|
|
503
|
+
const r = s.map((o) => o.name), i = s.map((o) => `- "${o.name}": ${o.description}`).join(`
|
|
488
504
|
`);
|
|
489
|
-
this.registerFunction("navigateToPage", async (
|
|
490
|
-
const
|
|
491
|
-
return
|
|
505
|
+
this.registerFunction("navigateToPage", async (o) => {
|
|
506
|
+
const a = o.page, c = s.find((l) => l.name === a);
|
|
507
|
+
return c ? (t(c.path), {
|
|
492
508
|
success: !0,
|
|
493
|
-
navigatedTo:
|
|
509
|
+
navigatedTo: c.path
|
|
494
510
|
}) : {
|
|
495
511
|
success: !1,
|
|
496
|
-
error: `Unknown page: ${
|
|
512
|
+
error: `Unknown page: ${a}`
|
|
497
513
|
};
|
|
498
514
|
}, {
|
|
499
515
|
description: `Navigate the user to a page in the application. Available pages:
|
|
500
|
-
${
|
|
516
|
+
${i}`,
|
|
501
517
|
parameters: {
|
|
502
518
|
type: "object",
|
|
503
519
|
properties: { page: {
|
|
@@ -509,27 +525,27 @@ ${s}`,
|
|
|
509
525
|
}
|
|
510
526
|
});
|
|
511
527
|
}
|
|
512
|
-
setContext(
|
|
513
|
-
this.contextRegistry.set(
|
|
528
|
+
setContext(t, s) {
|
|
529
|
+
this.contextRegistry.set(t, s);
|
|
514
530
|
}
|
|
515
|
-
removeContext(
|
|
516
|
-
return this.contextRegistry.remove(
|
|
531
|
+
removeContext(t) {
|
|
532
|
+
return this.contextRegistry.remove(t);
|
|
517
533
|
}
|
|
518
|
-
addDynamicContext(
|
|
519
|
-
this.contextRegistry.addDynamicProvider(
|
|
534
|
+
addDynamicContext(t, s) {
|
|
535
|
+
this.contextRegistry.addDynamicProvider(t, s);
|
|
520
536
|
}
|
|
521
|
-
removeDynamicContext(
|
|
522
|
-
return this.contextRegistry.removeDynamicProvider(
|
|
537
|
+
removeDynamicContext(t) {
|
|
538
|
+
return this.contextRegistry.removeDynamicProvider(t);
|
|
523
539
|
}
|
|
524
540
|
enableAutoPageContext() {
|
|
525
541
|
this.contextRegistry.addDynamicProvider("browser-page", () => {
|
|
526
|
-
const
|
|
527
|
-
return !
|
|
542
|
+
const t = typeof window < "u" ? window.location.href : void 0, s = typeof document < "u" ? document.title : void 0;
|
|
543
|
+
return !t && !s ? null : {
|
|
528
544
|
name: "Browser page",
|
|
529
545
|
description: "Current page the user is viewing",
|
|
530
546
|
data: {
|
|
531
|
-
url:
|
|
532
|
-
title:
|
|
547
|
+
url: t,
|
|
548
|
+
title: s
|
|
533
549
|
}
|
|
534
550
|
};
|
|
535
551
|
});
|
|
@@ -537,39 +553,39 @@ ${s}`,
|
|
|
537
553
|
getContextSnapshot() {
|
|
538
554
|
return this.contextRegistry.getSnapshot();
|
|
539
555
|
}
|
|
540
|
-
on(
|
|
541
|
-
this.eventEmitter.on(
|
|
556
|
+
on(t, s) {
|
|
557
|
+
this.eventEmitter.on(t, s);
|
|
542
558
|
}
|
|
543
|
-
off(
|
|
544
|
-
this.eventEmitter.off(
|
|
559
|
+
off(t, s) {
|
|
560
|
+
this.eventEmitter.off(t, s);
|
|
545
561
|
}
|
|
546
|
-
once(
|
|
547
|
-
this.eventEmitter.once(
|
|
562
|
+
once(t, s) {
|
|
563
|
+
this.eventEmitter.once(t, s);
|
|
548
564
|
}
|
|
549
|
-
waitFor(
|
|
550
|
-
return this.eventEmitter.waitFor(
|
|
565
|
+
waitFor(t, s) {
|
|
566
|
+
return this.eventEmitter.waitFor(t, s);
|
|
551
567
|
}
|
|
552
|
-
updateConfig(
|
|
568
|
+
updateConfig(t) {
|
|
553
569
|
this.config = {
|
|
554
570
|
...this.config,
|
|
555
|
-
...
|
|
556
|
-
}, this.httpClient.updateConfig(
|
|
571
|
+
...t
|
|
572
|
+
}, this.httpClient.updateConfig(t), t.debug !== void 0 && (t.debug ? n.enable() : n.disable());
|
|
557
573
|
}
|
|
558
574
|
destroy() {
|
|
559
575
|
this.eventEmitter.removeAllListeners(), this.functionRegistry.clear(), this.contextRegistry.clear(), n.log("AgoClient destroyed");
|
|
560
576
|
}
|
|
561
577
|
};
|
|
562
578
|
export {
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
d,
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
579
|
+
_ as a,
|
|
580
|
+
h as c,
|
|
581
|
+
u as d,
|
|
582
|
+
g as f,
|
|
583
|
+
x as i,
|
|
584
|
+
d as l,
|
|
585
|
+
S as n,
|
|
586
|
+
b as o,
|
|
587
|
+
k as r,
|
|
572
588
|
n as s,
|
|
573
|
-
|
|
574
|
-
|
|
589
|
+
$ as t,
|
|
590
|
+
m as u
|
|
575
591
|
};
|