@axiom-lattice/client-sdk 1.0.22 → 1.0.24
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/README.md +64 -0
- package/dist/index.d.ts +137 -8
- package/dist/index.js +542 -115
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +541 -105
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -3
package/dist/index.mjs
CHANGED
|
@@ -21,7 +21,6 @@ var AuthenticationError = class extends Error {
|
|
|
21
21
|
};
|
|
22
22
|
|
|
23
23
|
// src/client.ts
|
|
24
|
-
import axios from "axios";
|
|
25
24
|
var Client = class {
|
|
26
25
|
/**
|
|
27
26
|
* Creates a new Client instance
|
|
@@ -74,7 +73,8 @@ var Client = class {
|
|
|
74
73
|
threadId: options.threadId,
|
|
75
74
|
message: typeof message.content === "string" ? message.content : JSON.stringify(message.content),
|
|
76
75
|
streaming: true,
|
|
77
|
-
background: options.background
|
|
76
|
+
background: options.background,
|
|
77
|
+
enableReturnStateWhenSteamCompleted: options.enableReturnStateWhenSteamCompleted
|
|
78
78
|
},
|
|
79
79
|
onEvent,
|
|
80
80
|
onComplete,
|
|
@@ -107,48 +107,70 @@ var Client = class {
|
|
|
107
107
|
}
|
|
108
108
|
};
|
|
109
109
|
this.config = {
|
|
110
|
-
timeout:
|
|
110
|
+
timeout: 3e5,
|
|
111
111
|
...config
|
|
112
112
|
};
|
|
113
113
|
this.assistantId = config.assistantId;
|
|
114
|
-
this.
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
headers
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
114
|
+
this.headers = {
|
|
115
|
+
"Content-Type": "application/json",
|
|
116
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
117
|
+
...this.config.headers
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Helper method to handle fetch responses and errors
|
|
122
|
+
* @private
|
|
123
|
+
*/
|
|
124
|
+
async handleResponse(response) {
|
|
125
|
+
if (!response.ok) {
|
|
126
|
+
if (response.status === 401) {
|
|
127
|
+
throw new AuthenticationError("Authentication failed");
|
|
128
|
+
} else {
|
|
129
|
+
const errorData = await response.json().catch(() => ({}));
|
|
130
|
+
throw new ApiError(
|
|
131
|
+
errorData.message || "API Error",
|
|
132
|
+
response.status,
|
|
133
|
+
errorData
|
|
134
|
+
);
|
|
121
135
|
}
|
|
122
|
-
}
|
|
123
|
-
|
|
136
|
+
}
|
|
137
|
+
if (response.status === 204) {
|
|
138
|
+
return {};
|
|
139
|
+
}
|
|
140
|
+
try {
|
|
141
|
+
return await response.json();
|
|
142
|
+
} catch (error) {
|
|
143
|
+
throw new Error(`Failed to parse response: ${error}`);
|
|
144
|
+
}
|
|
124
145
|
}
|
|
125
146
|
/**
|
|
126
|
-
*
|
|
147
|
+
* Helper method to make fetch requests
|
|
127
148
|
* @private
|
|
128
149
|
*/
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
throw new Error(`Request error: ${error.message}`);
|
|
149
|
-
}
|
|
150
|
+
async fetchWithTimeout(url, options = {}) {
|
|
151
|
+
const controller = new AbortController();
|
|
152
|
+
const timeoutId = setTimeout(() => controller.abort(), this.config.timeout);
|
|
153
|
+
try {
|
|
154
|
+
const fullUrl = url.startsWith("http") ? url : `${this.config.baseURL}${url}`;
|
|
155
|
+
const response = await fetch(fullUrl, {
|
|
156
|
+
...options,
|
|
157
|
+
headers: {
|
|
158
|
+
...this.headers,
|
|
159
|
+
...options.headers || {}
|
|
160
|
+
},
|
|
161
|
+
signal: controller.signal
|
|
162
|
+
});
|
|
163
|
+
return await this.handleResponse(response);
|
|
164
|
+
} catch (error) {
|
|
165
|
+
if (error instanceof DOMException && error.name === "AbortError") {
|
|
166
|
+
throw new NetworkError("Request timed out");
|
|
167
|
+
} else if (error instanceof TypeError && error.message.includes("fetch")) {
|
|
168
|
+
throw new NetworkError("Network request failed");
|
|
150
169
|
}
|
|
151
|
-
|
|
170
|
+
throw error;
|
|
171
|
+
} finally {
|
|
172
|
+
clearTimeout(timeoutId);
|
|
173
|
+
}
|
|
152
174
|
}
|
|
153
175
|
/**
|
|
154
176
|
* Set tenant ID for multi-tenant environments
|
|
@@ -156,7 +178,7 @@ var Client = class {
|
|
|
156
178
|
*/
|
|
157
179
|
setTenantId(tenantId) {
|
|
158
180
|
this.tenantId = tenantId;
|
|
159
|
-
this.
|
|
181
|
+
this.headers["x-tenant-id"] = tenantId;
|
|
160
182
|
}
|
|
161
183
|
/**
|
|
162
184
|
* Creates a new thread
|
|
@@ -165,11 +187,14 @@ var Client = class {
|
|
|
165
187
|
*/
|
|
166
188
|
async createThread(options) {
|
|
167
189
|
try {
|
|
168
|
-
const
|
|
169
|
-
|
|
170
|
-
|
|
190
|
+
const data = await this.fetchWithTimeout("/threads", {
|
|
191
|
+
method: "POST",
|
|
192
|
+
body: JSON.stringify({
|
|
193
|
+
...options,
|
|
194
|
+
assistantId: this.assistantId
|
|
195
|
+
})
|
|
171
196
|
});
|
|
172
|
-
return
|
|
197
|
+
return data.id;
|
|
173
198
|
} catch (error) {
|
|
174
199
|
throw error;
|
|
175
200
|
}
|
|
@@ -181,12 +206,9 @@ var Client = class {
|
|
|
181
206
|
*/
|
|
182
207
|
async getThread(threadId) {
|
|
183
208
|
try {
|
|
184
|
-
const
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
}
|
|
188
|
-
});
|
|
189
|
-
return response.data;
|
|
209
|
+
const url = new URL(`${this.config.baseURL}/threads/${threadId}`);
|
|
210
|
+
url.searchParams.append("assistantId", this.assistantId);
|
|
211
|
+
return await this.fetchWithTimeout(url.toString());
|
|
190
212
|
} catch (error) {
|
|
191
213
|
throw error;
|
|
192
214
|
}
|
|
@@ -198,13 +220,18 @@ var Client = class {
|
|
|
198
220
|
*/
|
|
199
221
|
async listThreads(options) {
|
|
200
222
|
try {
|
|
201
|
-
const
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
223
|
+
const url = new URL(`${this.config.baseURL}/threads`);
|
|
224
|
+
url.searchParams.append("assistantId", this.assistantId);
|
|
225
|
+
if (options?.limit) {
|
|
226
|
+
url.searchParams.append("limit", options.limit.toString());
|
|
227
|
+
}
|
|
228
|
+
if (options?.offset !== void 0) {
|
|
229
|
+
url.searchParams.append("offset", options.offset.toString());
|
|
230
|
+
}
|
|
231
|
+
const data = await this.fetchWithTimeout(
|
|
232
|
+
url.toString()
|
|
233
|
+
);
|
|
234
|
+
return data.threads;
|
|
208
235
|
} catch (error) {
|
|
209
236
|
throw error;
|
|
210
237
|
}
|
|
@@ -216,10 +243,10 @@ var Client = class {
|
|
|
216
243
|
*/
|
|
217
244
|
async deleteThread(threadId) {
|
|
218
245
|
try {
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
246
|
+
const url = new URL(`${this.config.baseURL}/threads/${threadId}`);
|
|
247
|
+
url.searchParams.append("assistantId", this.assistantId);
|
|
248
|
+
await this.fetchWithTimeout(url.toString(), {
|
|
249
|
+
method: "DELETE"
|
|
223
250
|
});
|
|
224
251
|
} catch (error) {
|
|
225
252
|
throw error;
|
|
@@ -232,18 +259,20 @@ var Client = class {
|
|
|
232
259
|
*/
|
|
233
260
|
async getMessages(options) {
|
|
234
261
|
try {
|
|
235
|
-
const
|
|
236
|
-
|
|
237
|
-
{
|
|
238
|
-
params: {
|
|
239
|
-
limit: options.limit,
|
|
240
|
-
after: options.after,
|
|
241
|
-
reverse: options.reverse,
|
|
242
|
-
assistantId: this.assistantId
|
|
243
|
-
}
|
|
244
|
-
}
|
|
262
|
+
const url = new URL(
|
|
263
|
+
`${this.config.baseURL}/api/assistants/${this.assistantId}/${options.threadId}/memory`
|
|
245
264
|
);
|
|
246
|
-
|
|
265
|
+
if (options.limit) {
|
|
266
|
+
url.searchParams.append("limit", options.limit.toString());
|
|
267
|
+
}
|
|
268
|
+
if (options.after) {
|
|
269
|
+
url.searchParams.append("after", options.after);
|
|
270
|
+
}
|
|
271
|
+
if (options.reverse !== void 0) {
|
|
272
|
+
url.searchParams.append("reverse", options.reverse.toString());
|
|
273
|
+
}
|
|
274
|
+
url.searchParams.append("assistantId", this.assistantId);
|
|
275
|
+
return await this.fetchWithTimeout(url.toString());
|
|
247
276
|
} catch (error) {
|
|
248
277
|
throw error;
|
|
249
278
|
}
|
|
@@ -255,10 +284,9 @@ var Client = class {
|
|
|
255
284
|
*/
|
|
256
285
|
async getAgentState(threadId) {
|
|
257
286
|
try {
|
|
258
|
-
|
|
287
|
+
return await this.fetchWithTimeout(
|
|
259
288
|
`/api/assistants/${this.assistantId}/${threadId}/state`
|
|
260
289
|
);
|
|
261
|
-
return response.data;
|
|
262
290
|
} catch (error) {
|
|
263
291
|
throw error;
|
|
264
292
|
}
|
|
@@ -269,10 +297,10 @@ var Client = class {
|
|
|
269
297
|
*/
|
|
270
298
|
async getAgentGraph() {
|
|
271
299
|
try {
|
|
272
|
-
const
|
|
300
|
+
const data = await this.fetchWithTimeout(
|
|
273
301
|
`/api/assistants/${this.assistantId}/graph`
|
|
274
302
|
);
|
|
275
|
-
return
|
|
303
|
+
return data.image;
|
|
276
304
|
} catch (error) {
|
|
277
305
|
throw error;
|
|
278
306
|
}
|
|
@@ -280,7 +308,7 @@ var Client = class {
|
|
|
280
308
|
/**
|
|
281
309
|
* Run agent with options
|
|
282
310
|
* @param options - Options for running the agent
|
|
283
|
-
* @returns A promise that resolves to the run result
|
|
311
|
+
* @returns A promise that resolves to the run result
|
|
284
312
|
*/
|
|
285
313
|
async run(options) {
|
|
286
314
|
try {
|
|
@@ -293,9 +321,10 @@ var Client = class {
|
|
|
293
321
|
"Streaming without callbacks is not supported. Use chat.stream with callbacks instead."
|
|
294
322
|
);
|
|
295
323
|
} else {
|
|
296
|
-
|
|
297
|
-
"
|
|
298
|
-
|
|
324
|
+
return await this.fetchWithTimeout("/api/runs", {
|
|
325
|
+
method: "POST",
|
|
326
|
+
headers,
|
|
327
|
+
body: JSON.stringify({
|
|
299
328
|
assistant_id: this.assistantId,
|
|
300
329
|
thread_id: options.threadId,
|
|
301
330
|
message: options.message,
|
|
@@ -303,10 +332,8 @@ var Client = class {
|
|
|
303
332
|
command: options.command,
|
|
304
333
|
streaming: false,
|
|
305
334
|
background: options.background || false
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
);
|
|
309
|
-
return response.data;
|
|
335
|
+
})
|
|
336
|
+
});
|
|
310
337
|
}
|
|
311
338
|
} catch (error) {
|
|
312
339
|
throw error;
|
|
@@ -322,22 +349,20 @@ var Client = class {
|
|
|
322
349
|
*/
|
|
323
350
|
streamRun(options, onEvent, onComplete, onError) {
|
|
324
351
|
const headers = {
|
|
325
|
-
"Content-Type": "application/json"
|
|
352
|
+
"Content-Type": "application/json",
|
|
353
|
+
Accept: "text/event-stream",
|
|
354
|
+
...this.headers
|
|
326
355
|
};
|
|
327
356
|
if (this.tenantId) {
|
|
328
357
|
headers["x-tenant-id"] = this.tenantId;
|
|
329
358
|
}
|
|
330
359
|
const controller = new AbortController();
|
|
331
|
-
const signal = controller
|
|
360
|
+
const { signal } = controller;
|
|
332
361
|
(async () => {
|
|
333
362
|
try {
|
|
334
363
|
const response = await fetch(`${this.config.baseURL}/api/runs`, {
|
|
335
364
|
method: "POST",
|
|
336
|
-
headers
|
|
337
|
-
"Content-Type": "application/json",
|
|
338
|
-
Authorization: `Bearer ${this.config.apiKey}`,
|
|
339
|
-
...headers
|
|
340
|
-
},
|
|
365
|
+
headers,
|
|
341
366
|
body: JSON.stringify({
|
|
342
367
|
assistant_id: this.assistantId,
|
|
343
368
|
thread_id: options.threadId,
|
|
@@ -350,7 +375,7 @@ var Client = class {
|
|
|
350
375
|
signal
|
|
351
376
|
});
|
|
352
377
|
if (!response.ok) {
|
|
353
|
-
throw new Error(`HTTP error!
|
|
378
|
+
throw new Error(`HTTP error! Status: ${response.status}`);
|
|
354
379
|
}
|
|
355
380
|
if (!response.body) {
|
|
356
381
|
throw new Error("Response body is null");
|
|
@@ -359,17 +384,9 @@ var Client = class {
|
|
|
359
384
|
const decoder = new TextDecoder();
|
|
360
385
|
let buffer = "";
|
|
361
386
|
while (true) {
|
|
362
|
-
const {
|
|
363
|
-
if (done)
|
|
364
|
-
if (buffer.trim()) {
|
|
365
|
-
try {
|
|
366
|
-
const data = JSON.parse(buffer.trim());
|
|
367
|
-
onEvent(data);
|
|
368
|
-
} catch (error) {
|
|
369
|
-
}
|
|
370
|
-
}
|
|
387
|
+
const { done, value } = await reader.read();
|
|
388
|
+
if (done)
|
|
371
389
|
break;
|
|
372
|
-
}
|
|
373
390
|
const chunk = decoder.decode(value, { stream: true });
|
|
374
391
|
buffer += chunk;
|
|
375
392
|
const lines = buffer.split("\n");
|
|
@@ -377,9 +394,10 @@ var Client = class {
|
|
|
377
394
|
for (const line of lines) {
|
|
378
395
|
if (line.trim().startsWith("data: ")) {
|
|
379
396
|
try {
|
|
380
|
-
const
|
|
381
|
-
onEvent(
|
|
397
|
+
const eventData = JSON.parse(line.trim().slice(6));
|
|
398
|
+
onEvent(eventData);
|
|
382
399
|
} catch (error) {
|
|
400
|
+
console.error("Error parsing SSE data:", line, error);
|
|
383
401
|
if (onError) {
|
|
384
402
|
onError(
|
|
385
403
|
error instanceof Error ? error : new Error(String(error))
|
|
@@ -389,11 +407,28 @@ var Client = class {
|
|
|
389
407
|
}
|
|
390
408
|
}
|
|
391
409
|
}
|
|
410
|
+
if (buffer && buffer.trim().startsWith("data: ")) {
|
|
411
|
+
try {
|
|
412
|
+
const eventData = JSON.parse(buffer.trim().slice(6));
|
|
413
|
+
onEvent(eventData);
|
|
414
|
+
} catch (error) {
|
|
415
|
+
console.error("Error parsing SSE data:", buffer, error);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
392
418
|
if (onComplete) {
|
|
393
|
-
|
|
419
|
+
if (options.enableReturnStateWhenSteamCompleted) {
|
|
420
|
+
try {
|
|
421
|
+
const state = await this.getAgentState(options.threadId);
|
|
422
|
+
onComplete(state);
|
|
423
|
+
} catch (error) {
|
|
424
|
+
onComplete();
|
|
425
|
+
}
|
|
426
|
+
} else {
|
|
427
|
+
onComplete();
|
|
428
|
+
}
|
|
394
429
|
}
|
|
395
430
|
} catch (error) {
|
|
396
|
-
if (
|
|
431
|
+
if (error instanceof DOMException && error.name === "AbortError") {
|
|
397
432
|
return;
|
|
398
433
|
}
|
|
399
434
|
if (onError) {
|
|
@@ -407,6 +442,405 @@ var Client = class {
|
|
|
407
442
|
}
|
|
408
443
|
};
|
|
409
444
|
|
|
445
|
+
// src/wechat-client.ts
|
|
446
|
+
var WeChatClient = class {
|
|
447
|
+
/**
|
|
448
|
+
* Creates a new WeChatClient instance
|
|
449
|
+
* @param config - Configuration options for the client
|
|
450
|
+
*/
|
|
451
|
+
constructor(config) {
|
|
452
|
+
this.tenantId = "";
|
|
453
|
+
this.registeredTools = /* @__PURE__ */ new Map();
|
|
454
|
+
/**
|
|
455
|
+
* Chat namespace for sending messages and streaming responses
|
|
456
|
+
*/
|
|
457
|
+
this.chat = {
|
|
458
|
+
/**
|
|
459
|
+
* Sends a message to a thread and receives a response
|
|
460
|
+
* @param options - Options for sending a message
|
|
461
|
+
* @returns A promise that resolves to the chat response
|
|
462
|
+
*/
|
|
463
|
+
send: async (options) => {
|
|
464
|
+
try {
|
|
465
|
+
const message = options.messages[options.messages.length - 1];
|
|
466
|
+
const result = await this.run({
|
|
467
|
+
threadId: options.threadId,
|
|
468
|
+
message: typeof message.content === "string" ? message.content : JSON.stringify(message.content),
|
|
469
|
+
streaming: false
|
|
470
|
+
});
|
|
471
|
+
return {
|
|
472
|
+
message: {
|
|
473
|
+
role: "ai",
|
|
474
|
+
content: result.output || "",
|
|
475
|
+
id: result.id || ""
|
|
476
|
+
},
|
|
477
|
+
traceId: result.id || ""
|
|
478
|
+
};
|
|
479
|
+
} catch (error) {
|
|
480
|
+
throw error;
|
|
481
|
+
}
|
|
482
|
+
},
|
|
483
|
+
/**
|
|
484
|
+
* Sends a message to a thread and streams the response
|
|
485
|
+
* @param options - Options for streaming a message
|
|
486
|
+
* @param onEvent - Callback function that receives stream events
|
|
487
|
+
* @param onComplete - Optional callback function called when streaming completes
|
|
488
|
+
* @param onError - Optional callback function called when an error occurs
|
|
489
|
+
* @returns A function that can be called to stop the stream
|
|
490
|
+
*/
|
|
491
|
+
stream: (options, onEvent, onComplete, onError) => {
|
|
492
|
+
const message = options.messages[options.messages.length - 1];
|
|
493
|
+
return this.streamRun(
|
|
494
|
+
{
|
|
495
|
+
threadId: options.threadId,
|
|
496
|
+
message: typeof message.content === "string" ? message.content : JSON.stringify(message.content),
|
|
497
|
+
streaming: true,
|
|
498
|
+
background: options.background,
|
|
499
|
+
enableReturnStateWhenSteamCompleted: options.enableReturnStateWhenSteamCompleted
|
|
500
|
+
},
|
|
501
|
+
onEvent,
|
|
502
|
+
onComplete,
|
|
503
|
+
onError
|
|
504
|
+
);
|
|
505
|
+
}
|
|
506
|
+
};
|
|
507
|
+
/**
|
|
508
|
+
* Tools namespace for registering and unregistering client-side tools
|
|
509
|
+
*/
|
|
510
|
+
this.tools = {
|
|
511
|
+
/**
|
|
512
|
+
* Registers a client-side tool
|
|
513
|
+
* @param options - Options for registering a tool
|
|
514
|
+
*/
|
|
515
|
+
register: (options) => {
|
|
516
|
+
if (this.config.transport !== "ws") {
|
|
517
|
+
throw new Error(
|
|
518
|
+
"Client-side tools are only supported with WebSocket transport"
|
|
519
|
+
);
|
|
520
|
+
}
|
|
521
|
+
this.registeredTools.set(options.name, options);
|
|
522
|
+
},
|
|
523
|
+
/**
|
|
524
|
+
* Unregisters a client-side tool
|
|
525
|
+
* @param name - Tool name
|
|
526
|
+
*/
|
|
527
|
+
unregister: (name) => {
|
|
528
|
+
this.registeredTools.delete(name);
|
|
529
|
+
}
|
|
530
|
+
};
|
|
531
|
+
this.config = {
|
|
532
|
+
timeout: 3e4,
|
|
533
|
+
...config
|
|
534
|
+
};
|
|
535
|
+
this.assistantId = config.assistantId;
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* Set tenant ID for multi-tenant environments
|
|
539
|
+
* @param tenantId - Tenant identifier
|
|
540
|
+
*/
|
|
541
|
+
setTenantId(tenantId) {
|
|
542
|
+
this.tenantId = tenantId;
|
|
543
|
+
}
|
|
544
|
+
/**
|
|
545
|
+
* Helper method to make WeChat HTTP requests
|
|
546
|
+
* @private
|
|
547
|
+
*/
|
|
548
|
+
async request(options) {
|
|
549
|
+
const { url, method, data, params } = options;
|
|
550
|
+
let fullUrl = `${this.config.baseURL}${url}`;
|
|
551
|
+
if (params && Object.keys(params).length > 0) {
|
|
552
|
+
const queryParams = new URLSearchParams();
|
|
553
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
554
|
+
if (value !== void 0) {
|
|
555
|
+
queryParams.append(key, String(value));
|
|
556
|
+
}
|
|
557
|
+
});
|
|
558
|
+
fullUrl += `?${queryParams.toString()}`;
|
|
559
|
+
}
|
|
560
|
+
const headers = {
|
|
561
|
+
"Content-Type": "application/json",
|
|
562
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
563
|
+
...this.config.headers
|
|
564
|
+
};
|
|
565
|
+
if (this.tenantId) {
|
|
566
|
+
headers["x-tenant-id"] = this.tenantId;
|
|
567
|
+
}
|
|
568
|
+
return new Promise((resolve, reject) => {
|
|
569
|
+
wx.request({
|
|
570
|
+
url: fullUrl,
|
|
571
|
+
method,
|
|
572
|
+
data,
|
|
573
|
+
header: headers,
|
|
574
|
+
timeout: this.config.timeout,
|
|
575
|
+
success: (res) => {
|
|
576
|
+
const statusCode = res.statusCode;
|
|
577
|
+
if (statusCode >= 200 && statusCode < 300) {
|
|
578
|
+
resolve(res.data);
|
|
579
|
+
} else if (statusCode === 401) {
|
|
580
|
+
reject(
|
|
581
|
+
new AuthenticationError(
|
|
582
|
+
res.data?.message || "Authentication failed"
|
|
583
|
+
)
|
|
584
|
+
);
|
|
585
|
+
} else {
|
|
586
|
+
reject(
|
|
587
|
+
new ApiError(
|
|
588
|
+
res.data?.message || "API Error",
|
|
589
|
+
statusCode,
|
|
590
|
+
res.data
|
|
591
|
+
)
|
|
592
|
+
);
|
|
593
|
+
}
|
|
594
|
+
},
|
|
595
|
+
fail: (err) => {
|
|
596
|
+
reject(new NetworkError(`Request failed: ${err.errMsg}`));
|
|
597
|
+
}
|
|
598
|
+
});
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
/**
|
|
602
|
+
* Creates a new thread
|
|
603
|
+
* @param options - Options for creating a thread
|
|
604
|
+
* @returns A promise that resolves to the thread ID
|
|
605
|
+
*/
|
|
606
|
+
async createThread(options) {
|
|
607
|
+
try {
|
|
608
|
+
const response = await this.request({
|
|
609
|
+
url: "/threads",
|
|
610
|
+
method: "POST",
|
|
611
|
+
data: {
|
|
612
|
+
...options,
|
|
613
|
+
assistantId: this.assistantId
|
|
614
|
+
}
|
|
615
|
+
});
|
|
616
|
+
return response.id;
|
|
617
|
+
} catch (error) {
|
|
618
|
+
throw error;
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
/**
|
|
622
|
+
* Retrieves thread information
|
|
623
|
+
* @param threadId - Thread identifier
|
|
624
|
+
* @returns A promise that resolves to the thread information
|
|
625
|
+
*/
|
|
626
|
+
async getThread(threadId) {
|
|
627
|
+
try {
|
|
628
|
+
return await this.request({
|
|
629
|
+
url: `/threads/${threadId}`,
|
|
630
|
+
method: "GET",
|
|
631
|
+
params: {
|
|
632
|
+
assistantId: this.assistantId
|
|
633
|
+
}
|
|
634
|
+
});
|
|
635
|
+
} catch (error) {
|
|
636
|
+
throw error;
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
/**
|
|
640
|
+
* Lists all threads
|
|
641
|
+
* @param options - Options for listing threads
|
|
642
|
+
* @returns A promise that resolves to an array of threads
|
|
643
|
+
*/
|
|
644
|
+
async listThreads(options) {
|
|
645
|
+
try {
|
|
646
|
+
const response = await this.request({
|
|
647
|
+
url: "/threads",
|
|
648
|
+
method: "GET",
|
|
649
|
+
params: {
|
|
650
|
+
...options,
|
|
651
|
+
assistantId: this.assistantId
|
|
652
|
+
}
|
|
653
|
+
});
|
|
654
|
+
return response.threads;
|
|
655
|
+
} catch (error) {
|
|
656
|
+
throw error;
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
/**
|
|
660
|
+
* Deletes a thread
|
|
661
|
+
* @param threadId - Thread identifier
|
|
662
|
+
* @returns A promise that resolves when the thread is deleted
|
|
663
|
+
*/
|
|
664
|
+
async deleteThread(threadId) {
|
|
665
|
+
try {
|
|
666
|
+
await this.request({
|
|
667
|
+
url: `/threads/${threadId}`,
|
|
668
|
+
method: "DELETE",
|
|
669
|
+
params: {
|
|
670
|
+
assistantId: this.assistantId
|
|
671
|
+
}
|
|
672
|
+
});
|
|
673
|
+
} catch (error) {
|
|
674
|
+
throw error;
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
/**
|
|
678
|
+
* Retrieves messages from a thread
|
|
679
|
+
* @param options - Options for retrieving messages
|
|
680
|
+
* @returns A promise that resolves to an array of messages
|
|
681
|
+
*/
|
|
682
|
+
async getMessages(options) {
|
|
683
|
+
try {
|
|
684
|
+
return await this.request({
|
|
685
|
+
url: `/api/assistants/${this.assistantId}/${options.threadId}/memory`,
|
|
686
|
+
method: "GET",
|
|
687
|
+
params: {
|
|
688
|
+
limit: options.limit,
|
|
689
|
+
after: options.after,
|
|
690
|
+
reverse: options.reverse,
|
|
691
|
+
assistantId: this.assistantId
|
|
692
|
+
}
|
|
693
|
+
});
|
|
694
|
+
} catch (error) {
|
|
695
|
+
throw error;
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
/**
|
|
699
|
+
* Retrieves agent state
|
|
700
|
+
* @param threadId - Thread identifier
|
|
701
|
+
* @returns A promise that resolves to the agent state
|
|
702
|
+
*/
|
|
703
|
+
async getAgentState(threadId) {
|
|
704
|
+
try {
|
|
705
|
+
return await this.request({
|
|
706
|
+
url: `/api/assistants/${this.assistantId}/${threadId}/state`,
|
|
707
|
+
method: "GET"
|
|
708
|
+
});
|
|
709
|
+
} catch (error) {
|
|
710
|
+
throw error;
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
/**
|
|
714
|
+
* Gets agent graph visualization
|
|
715
|
+
* @returns A promise that resolves to the graph visualization data
|
|
716
|
+
*/
|
|
717
|
+
async getAgentGraph() {
|
|
718
|
+
try {
|
|
719
|
+
const response = await this.request({
|
|
720
|
+
url: `/api/assistants/${this.assistantId}/graph`,
|
|
721
|
+
method: "GET"
|
|
722
|
+
});
|
|
723
|
+
return response.image;
|
|
724
|
+
} catch (error) {
|
|
725
|
+
throw error;
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
/**
|
|
729
|
+
* Run agent with options
|
|
730
|
+
* @param options - Options for running the agent
|
|
731
|
+
* @returns A promise that resolves to the run result
|
|
732
|
+
*/
|
|
733
|
+
async run(options) {
|
|
734
|
+
try {
|
|
735
|
+
if (options.streaming) {
|
|
736
|
+
throw new Error(
|
|
737
|
+
"Streaming without callbacks is not supported. Use chat.stream with callbacks instead."
|
|
738
|
+
);
|
|
739
|
+
} else {
|
|
740
|
+
return await this.request({
|
|
741
|
+
url: "/api/runs",
|
|
742
|
+
method: "POST",
|
|
743
|
+
data: {
|
|
744
|
+
assistant_id: this.assistantId,
|
|
745
|
+
thread_id: options.threadId,
|
|
746
|
+
message: options.message,
|
|
747
|
+
files: options.files,
|
|
748
|
+
command: options.command,
|
|
749
|
+
streaming: false,
|
|
750
|
+
background: options.background || false
|
|
751
|
+
}
|
|
752
|
+
});
|
|
753
|
+
}
|
|
754
|
+
} catch (error) {
|
|
755
|
+
throw error;
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
/**
|
|
759
|
+
* Stream run results using WeChat's downloadFile API
|
|
760
|
+
* @param options - Options for streaming run results
|
|
761
|
+
* @param onEvent - Callback function that receives stream events
|
|
762
|
+
* @param onComplete - Optional callback function called when streaming completes
|
|
763
|
+
* @param onError - Optional callback function called when an error occurs
|
|
764
|
+
* @returns A function that can be called to stop the stream
|
|
765
|
+
*/
|
|
766
|
+
streamRun(options, onEvent, onComplete, onError) {
|
|
767
|
+
const headers = {
|
|
768
|
+
"Content-Type": "application/json",
|
|
769
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
770
|
+
...this.config.headers
|
|
771
|
+
};
|
|
772
|
+
if (this.tenantId) {
|
|
773
|
+
headers["x-tenant-id"] = this.tenantId;
|
|
774
|
+
}
|
|
775
|
+
const taskId = wx.request({
|
|
776
|
+
url: `${this.config.baseURL}/api/runs`,
|
|
777
|
+
method: "POST",
|
|
778
|
+
data: {
|
|
779
|
+
assistant_id: this.assistantId,
|
|
780
|
+
thread_id: options.threadId,
|
|
781
|
+
message: options.message,
|
|
782
|
+
files: options.files,
|
|
783
|
+
command: options.command,
|
|
784
|
+
streaming: true,
|
|
785
|
+
background: options.background || false
|
|
786
|
+
},
|
|
787
|
+
header: headers,
|
|
788
|
+
responseType: "text",
|
|
789
|
+
enableChunked: true,
|
|
790
|
+
success: async () => {
|
|
791
|
+
},
|
|
792
|
+
fail: (err) => {
|
|
793
|
+
if (onError) {
|
|
794
|
+
onError(new Error(`Stream request failed: ${err.errMsg}`));
|
|
795
|
+
}
|
|
796
|
+
},
|
|
797
|
+
complete: async () => {
|
|
798
|
+
if (onComplete) {
|
|
799
|
+
if (options.enableReturnStateWhenSteamCompleted) {
|
|
800
|
+
try {
|
|
801
|
+
const state = await this.getAgentState(options.threadId);
|
|
802
|
+
onComplete(state);
|
|
803
|
+
} catch (error) {
|
|
804
|
+
onComplete();
|
|
805
|
+
}
|
|
806
|
+
} else {
|
|
807
|
+
onComplete();
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
},
|
|
811
|
+
// Using WeChat's chunked data capability
|
|
812
|
+
dataReceived: (res) => {
|
|
813
|
+
if (!res.data)
|
|
814
|
+
return;
|
|
815
|
+
const textDecoder = new TextDecoder();
|
|
816
|
+
const text = textDecoder.decode(res.data);
|
|
817
|
+
const lines = text.split("\n");
|
|
818
|
+
for (const line of lines) {
|
|
819
|
+
if (line.trim().startsWith("data: ")) {
|
|
820
|
+
try {
|
|
821
|
+
const eventData = JSON.parse(line.trim().slice(6));
|
|
822
|
+
onEvent(eventData);
|
|
823
|
+
} catch (error) {
|
|
824
|
+
if (onError) {
|
|
825
|
+
onError(
|
|
826
|
+
error instanceof Error ? error : new Error(String(error))
|
|
827
|
+
);
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
});
|
|
834
|
+
return () => {
|
|
835
|
+
wx.abortRequest({
|
|
836
|
+
requestId: taskId,
|
|
837
|
+
success: () => console.log("Stream request aborted"),
|
|
838
|
+
fail: (err) => console.error("Failed to abort stream request:", err.errMsg)
|
|
839
|
+
});
|
|
840
|
+
};
|
|
841
|
+
}
|
|
842
|
+
};
|
|
843
|
+
|
|
410
844
|
// src/ChunkMessageMerger.ts
|
|
411
845
|
function createSimpleMessageMerger() {
|
|
412
846
|
let messages = [];
|
|
@@ -489,7 +923,6 @@ function createSimpleMessageMerger() {
|
|
|
489
923
|
messages = msgs;
|
|
490
924
|
}
|
|
491
925
|
function push(chunk) {
|
|
492
|
-
console.log(chunk);
|
|
493
926
|
const role = normalizeRole(chunk.type);
|
|
494
927
|
const message = ensureMessage(chunk.data.id, role);
|
|
495
928
|
if (chunk.data.content) {
|
|
@@ -525,7 +958,8 @@ function createSimpleMessageMerger() {
|
|
|
525
958
|
name: tc.name,
|
|
526
959
|
args: tc.args,
|
|
527
960
|
type: "tool_call",
|
|
528
|
-
response: tc.response
|
|
961
|
+
response: tc.response,
|
|
962
|
+
status: "success"
|
|
529
963
|
}));
|
|
530
964
|
if (toolCalls.length > 0) {
|
|
531
965
|
message.tool_calls = toolCalls;
|
|
@@ -568,7 +1002,8 @@ function createSimpleMessageMerger() {
|
|
|
568
1002
|
if (toolResponse) {
|
|
569
1003
|
return {
|
|
570
1004
|
...toolCall,
|
|
571
|
-
response: toolResponse.content
|
|
1005
|
+
response: toolResponse.content,
|
|
1006
|
+
status: "success"
|
|
572
1007
|
};
|
|
573
1008
|
}
|
|
574
1009
|
return toolCall;
|
|
@@ -603,6 +1038,7 @@ export {
|
|
|
603
1038
|
AuthenticationError,
|
|
604
1039
|
Client,
|
|
605
1040
|
NetworkError,
|
|
1041
|
+
WeChatClient,
|
|
606
1042
|
createSimpleMessageMerger
|
|
607
1043
|
};
|
|
608
1044
|
//# sourceMappingURL=index.mjs.map
|