@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.
Files changed (40) hide show
  1. package/dist/AgoClient-B6PsfMeg.cjs +6 -0
  2. package/dist/{AgoClient-DF7M2WAz.js → AgoClient-BerJvssl.js} +235 -219
  3. package/dist/angular.cjs +1 -1
  4. package/dist/angular.d.cts +12 -0
  5. package/dist/angular.d.ts +12 -0
  6. package/dist/angular.js +1 -1
  7. package/dist/createConversationSession-CLZvxyuu.js +58 -0
  8. package/dist/createConversationSession-D3K5Gf1k.cjs +1 -0
  9. package/dist/createFormCollector-CD1rqRG4.js +208 -0
  10. package/dist/createFormCollector-D1FfiSgA.cjs +1 -0
  11. package/dist/createMockClient-DMFGIOtc.cjs +1 -0
  12. package/dist/createMockClient-tVsApEAh.js +72 -0
  13. package/dist/devtools.cjs +26 -26
  14. package/dist/devtools.d.cts +16 -26
  15. package/dist/devtools.d.ts +16 -26
  16. package/dist/devtools.js +55 -55
  17. package/dist/helpers.cjs +1 -1
  18. package/dist/helpers.js +1 -1
  19. package/dist/index.cjs +1 -1
  20. package/dist/index.d.cts +102 -1
  21. package/dist/index.d.ts +102 -1
  22. package/dist/index.js +46 -43
  23. package/dist/react.cjs +19 -19
  24. package/dist/react.d.cts +67 -8
  25. package/dist/react.d.ts +67 -8
  26. package/dist/react.js +1066 -1051
  27. package/dist/vue.cjs +1 -1
  28. package/dist/vue.d.cts +12 -0
  29. package/dist/vue.d.ts +12 -0
  30. package/dist/vue.js +1 -1
  31. package/dist/widget.cjs +1 -0
  32. package/dist/widget.d.cts +635 -0
  33. package/dist/widget.d.ts +635 -0
  34. package/dist/widget.js +418 -0
  35. package/package.json +58 -15
  36. package/dist/AgoClient-bjN-z2ZQ.cjs +0 -6
  37. package/dist/createMockClient-D6uVh-B9.js +0 -254
  38. package/dist/createMockClient-DecMEUcK.cjs +0 -1
  39. /package/dist/{functions-DdROP6Ui.js → functions-C1-5vadY.js} +0 -0
  40. /package/dist/{functions-Bz-1Bs3I.cjs → functions-DsmzgHjC.cjs} +0 -0
@@ -1,28 +1,28 @@
1
- var h = class extends Error {
2
- constructor(e, t, r) {
3
- super(e), this.code = t, this.statusCode = r, this.name = "AgoError";
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
- }, l = class m extends h {
6
- constructor(t, r, s, i, o, a) {
7
- super(t, r, s), this.type = i, this.param = o, this.docUrl = a, this.name = "AgoApiError";
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, r) {
10
- const s = t.error;
11
- return new m(s.message, s.code, r, s.type, s.param, s.doc_url);
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
- }, d = class extends h {
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
- }, u = class extends h {
17
+ }, g = class extends d {
18
18
  constructor(e) {
19
19
  super(e, "stream_error"), this.name = "AgoStreamError";
20
20
  }
21
- }, g = class extends h {
22
- constructor(e, t, r) {
23
- super(e, "function_error"), this.functionName = t, this.originalError = r, this.name = "AgoFunctionError";
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
- }, w = class {
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 w(), f = "ago_widget_id";
48
- function v() {
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 y = class {
60
+ var E = class {
61
61
  constructor(e) {
62
- this.baseUrl = e.baseUrl.replace(/\/$/, ""), this.headers = { "X-Widget-Id": e.widgetId || v() }, 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);
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 r = `${this.baseUrl}${e}`;
75
- n.debug("POST (stream)", r, t);
74
+ const s = `${this.baseUrl}${e}`;
75
+ n.debug("POST (stream)", s, t);
76
76
  try {
77
- const s = await fetch(r, {
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 s.ok || await this.handleErrorResponse(s), s;
86
- } catch (s) {
87
- throw s instanceof l ? s : new d(`Network error: ${s instanceof Error ? s.message : "Unknown error"}`, s instanceof Error ? s : void 0);
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 r = `${this.baseUrl}${e}`;
92
- n.debug("POST (formData)", r);
93
- const s = { ...this.headers };
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(r, {
95
+ const i = await fetch(s, {
96
96
  method: "POST",
97
- headers: s,
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 l ? i : new d(`Network error: ${i instanceof Error ? i.message : "Unknown error"}`, i instanceof Error ? i : void 0);
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, r) {
106
- const s = `${this.baseUrl}${t}`;
107
- n.debug(e, s, r);
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(s, {
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: r ? JSON.stringify(r) : void 0
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 l ? i : new d(`Network error: ${i instanceof Error ? i.message : "Unknown error"}`, i instanceof Error ? i : void 0);
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 ? l.fromResponse(t, e.status) : new l(`HTTP ${e.status}: ${e.statusText}`, "http_error", e.status, "api_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
- }, C = class {
130
+ }, b = class {
131
131
  constructor() {
132
132
  this.functions = /* @__PURE__ */ new Map();
133
133
  }
134
- register(e, t, r) {
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 s = e;
143
- if (!t || !r) throw new Error("handler and schema are required");
144
- this.functions.has(s) && n.warn(`Function "${s}" is being overwritten`), this.functions.set(s, {
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
- ...r,
147
- name: s
146
+ ...s,
147
+ name: r
148
148
  },
149
149
  handler: t
150
- }), n.log(`Registered function: ${s}`);
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 r = this.functions.get(e);
167
- if (!r) throw new g(`Function "${e}" is not registered`, e);
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 s = await r.handler(t);
171
- return n.log(`Function ${e} completed:`, s), s;
172
- } catch (s) {
173
- throw n.error(`Function ${e} failed:`, s), new g(`Function "${e}" execution failed: ${s instanceof Error ? s.message : "Unknown error"}`, e, s instanceof Error ? s : void 0);
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
- }, E = class {
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, r] of this.entries) e[t] = r;
204
- for (const [t, r] of this.dynamicProviders) try {
205
- const s = r();
206
- s && (e[t] = s);
207
- } catch (s) {
208
- n.error(`DynamicContext provider "${t}" threw:`, s);
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
- }, b = class {
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 u("Response has no body");
221
- const t = e.body.getReader(), r = new TextDecoder();
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: s, value: i } = await t.read();
225
- if (s) break;
226
- const o = r.decode(i, { stream: !0 });
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 (s) {
231
- const i = s instanceof Error ? s : new u("Stream processing failed");
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 r = t.split(`
244
+ const s = t.split(`
245
245
  `);
246
- for (const s of r)
247
- if (!s.startsWith(": ") && s.startsWith("data: ")) {
248
- const i = s.slice(6);
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 r = t.knowledge_document;
271
+ const s = t.knowledge_document;
272
272
  return {
273
- id: r.id,
274
- title: r.title,
275
- url: r.use_external_link ? r.external_link_url : r.internal_link_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 r = this.toolCalls.findIndex((s) => s.id === t.id);
286
- r >= 0 ? this.toolCalls[r] = t : this.toolCalls.push(t);
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 k(e) {
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 _ = class {
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 r = this.handlers.get(e);
333
- r && r.delete(t);
332
+ const s = this.handlers.get(e);
333
+ s && s.delete(t);
334
334
  }
335
335
  emit(e, t) {
336
- const r = this.handlers.get(e);
337
- r && r.forEach((s) => {
336
+ const s = this.handlers.get(e);
337
+ s && s.forEach((r) => {
338
338
  try {
339
- s(t);
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 r = ((s) => {
347
- this.off(e, r), t(s);
346
+ const s = ((r) => {
347
+ this.off(e, s), t(r);
348
348
  });
349
- this.on(e, r);
349
+ this.on(e, s);
350
350
  }
351
351
  waitFor(e, t) {
352
- return new Promise((r, s) => {
352
+ return new Promise((s, r) => {
353
353
  let i;
354
354
  const o = ((a) => {
355
- i && clearTimeout(i), r(a);
355
+ i && clearTimeout(i), s(a);
356
356
  });
357
357
  this.once(e, o), t?.timeout && (i = setTimeout(() => {
358
- this.off(e, o), s(/* @__PURE__ */ new Error(`waitFor("${String(e)}") timed out after ${t.timeout}ms`));
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
- }, S = class {
366
- constructor(e) {
367
- this.config = e, this.httpClient = new y(e), this.functionRegistry = new C(), this.contextRegistry = new E(), this.eventEmitter = new _(), e.debug && n.enable(), n.log("AgoClient initialized");
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(e, t) {
370
- const r = this.functionRegistry.getSchemas(), s = this.contextRegistry.getSnapshot(), i = this.config.agent || this.config.defaultAgentId, o = {
371
- content: e,
372
- conversation_id: t?.conversationId,
373
- agent_id: t?.agentId || i
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 && (o.client_functions = r), s && (o.client_context = s);
376
- let a;
377
- if (t?.files && t.files.length > 0) {
378
- const c = new FormData();
379
- c.append("content", e), t.conversationId && c.append("conversation_id", t.conversationId), (t.agentId || i) && c.append("agent_id", t.agentId || i || ""), r.length > 0 && c.append("client_functions", JSON.stringify(r)), s && c.append("client_context", JSON.stringify(s));
380
- for (const p of t.files) c.append("files", p);
381
- a = await this.httpClient.postFormData("/api/sdk/v1/messages", c);
382
- } else a = await this.httpClient.postStream("/api/sdk/v1/messages", o);
383
- return this.processSSEResponse(a);
384
- }
385
- async processSSEResponse(e) {
386
- return new b({
387
- onStart: (t) => {
388
- this.eventEmitter.emit("message:start", t);
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: (t) => {
391
- this.eventEmitter.emit("message:chunk", t);
390
+ onChunk: (s) => {
391
+ this.eventEmitter.emit("message:chunk", s);
392
392
  },
393
- onToolCall: (t) => {
394
- this.eventEmitter.emit("toolCall:received", t), t.type === "form" && this.eventEmitter.emit("toolCall:form", t);
393
+ onToolCall: (s) => {
394
+ this.eventEmitter.emit("toolCall:received", s), s.type === "form" && this.eventEmitter.emit("toolCall:form", s);
395
395
  },
396
- onClientFunction: async (t) => {
397
- this.eventEmitter.emit("function:invoke", t), await this.handleClientFunctionInvocation(t);
396
+ onClientFunction: async (s) => {
397
+ this.eventEmitter.emit("function:invoke", s), await this.handleClientFunctionInvocation(s);
398
398
  },
399
- onComplete: (t) => {
400
- this.eventEmitter.emit("message:complete", t);
399
+ onComplete: (s) => {
400
+ this.eventEmitter.emit("message:complete", s);
401
401
  },
402
- onError: (t) => {
403
- this.eventEmitter.emit("message:error", { error: t.message });
402
+ onError: (s) => {
403
+ this.eventEmitter.emit("message:error", { error: s.message });
404
404
  }
405
- }).processStream(e);
405
+ }).processStream(t);
406
406
  }
407
- async handleClientFunctionInvocation(e) {
408
- let t, r;
407
+ async handleClientFunctionInvocation(t) {
408
+ let s, r;
409
409
  try {
410
- t = await this.functionRegistry.execute(e.functionName, e.arguments);
411
- } catch (s) {
412
- r = s instanceof Error ? s.message : "Unknown error", n.error("Client function execution failed:", s);
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 (e.invocationId) try {
415
- await this.submitToolCallForm(e.invocationId, {
416
- result: t,
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 (s) {
421
- n.error("Failed to submit function result:", s);
420
+ } catch (i) {
421
+ n.error("Failed to submit function result:", i);
422
422
  }
423
423
  this.eventEmitter.emit("function:result", {
424
- invocationId: e.invocationId,
425
- result: t,
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((e) => ({
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
- messages: t.messages.map((r) => ({
443
- id: r.id,
444
- conversationId: t.id,
445
- content: r.content,
446
- role: r.role,
447
- status: r.status,
448
- createdAt: new Date(r.created_at)
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
- async getMessages(e) {
453
- return (await this.getConversation(e)).messages || [];
454
- }
455
- async submitToolCallForm(e, t) {
456
- await this.httpClient.post(`/api/sdk/v1/tool-calls/${e}/submit`, { formData: t });
457
- }
458
- async confirmToolCall(e) {
459
- await this.httpClient.post(`/api/tool-calls/${e}/confirm/`);
460
- }
461
- async rejectToolCall(e) {
462
- await this.httpClient.post(`/api/tool-calls/${e}/reject/`);
463
- }
464
- async submitFormCollector(e, t) {
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: e,
467
- values: t
482
+ destination: t,
483
+ values: s
468
484
  });
469
485
  }
470
- async submitFeedback(e, t) {
471
- await this.httpClient.post(`/api/sdk/v1/messages/${e}/feedback`, { rating: t });
486
+ async submitFeedback(t, s) {
487
+ await this.httpClient.post(`/api/sdk/v1/messages/${t}/feedback`, { rating: s });
472
488
  }
473
- registerFunction(e, t, r) {
474
- typeof e == "object" ? this.functionRegistry.register(e) : this.functionRegistry.register(e, t, r);
489
+ registerFunction(t, s, r) {
490
+ typeof t == "object" ? this.functionRegistry.register(t) : this.functionRegistry.register(t, s, r);
475
491
  }
476
- register(e) {
477
- if (Array.isArray(e)) for (const t of e) this.functionRegistry.register(t);
478
- else this.functionRegistry.register(e);
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(e) {
481
- return this.functionRegistry.unregister(e);
496
+ unregisterFunction(t) {
497
+ return this.functionRegistry.unregister(t);
482
498
  }
483
499
  getRegisteredFunctions() {
484
500
  return this.functionRegistry.getSchemas();
485
501
  }
486
- registerNavigationFunction(e, t) {
487
- const r = t.map((i) => i.name), s = t.map((i) => `- "${i.name}": ${i.description}`).join(`
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 (i) => {
490
- const o = i.page, a = t.find((c) => c.name === o);
491
- return a ? (e(a.path), {
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: a.path
509
+ navigatedTo: c.path
494
510
  }) : {
495
511
  success: !1,
496
- error: `Unknown page: ${o}`
512
+ error: `Unknown page: ${a}`
497
513
  };
498
514
  }, {
499
515
  description: `Navigate the user to a page in the application. Available pages:
500
- ${s}`,
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(e, t) {
513
- this.contextRegistry.set(e, t);
528
+ setContext(t, s) {
529
+ this.contextRegistry.set(t, s);
514
530
  }
515
- removeContext(e) {
516
- return this.contextRegistry.remove(e);
531
+ removeContext(t) {
532
+ return this.contextRegistry.remove(t);
517
533
  }
518
- addDynamicContext(e, t) {
519
- this.contextRegistry.addDynamicProvider(e, t);
534
+ addDynamicContext(t, s) {
535
+ this.contextRegistry.addDynamicProvider(t, s);
520
536
  }
521
- removeDynamicContext(e) {
522
- return this.contextRegistry.removeDynamicProvider(e);
537
+ removeDynamicContext(t) {
538
+ return this.contextRegistry.removeDynamicProvider(t);
523
539
  }
524
540
  enableAutoPageContext() {
525
541
  this.contextRegistry.addDynamicProvider("browser-page", () => {
526
- const e = typeof window < "u" ? window.location.href : void 0, t = typeof document < "u" ? document.title : void 0;
527
- return !e && !t ? null : {
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: e,
532
- title: t
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(e, t) {
541
- this.eventEmitter.on(e, t);
556
+ on(t, s) {
557
+ this.eventEmitter.on(t, s);
542
558
  }
543
- off(e, t) {
544
- this.eventEmitter.off(e, t);
559
+ off(t, s) {
560
+ this.eventEmitter.off(t, s);
545
561
  }
546
- once(e, t) {
547
- this.eventEmitter.once(e, t);
562
+ once(t, s) {
563
+ this.eventEmitter.once(t, s);
548
564
  }
549
- waitFor(e, t) {
550
- return this.eventEmitter.waitFor(e, t);
565
+ waitFor(t, s) {
566
+ return this.eventEmitter.waitFor(t, s);
551
567
  }
552
- updateConfig(e) {
568
+ updateConfig(t) {
553
569
  this.config = {
554
570
  ...this.config,
555
- ...e
556
- }, this.httpClient.updateConfig(e), e.debug !== void 0 && (e.debug ? n.enable() : n.disable());
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
- E as a,
564
- l as c,
565
- d,
566
- u as f,
567
- k as i,
568
- h as l,
569
- _ as n,
570
- C as o,
571
- b as r,
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
- S as t,
574
- g as u
589
+ $ as t,
590
+ m as u
575
591
  };