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