@leikeduntech/leiai-js 4.0.4 → 4.1.0

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 (2) hide show
  1. package/build/index.js +55 -20
  2. package/package.json +1 -1
package/build/index.js CHANGED
@@ -248,6 +248,7 @@ var ChatGPTAPI = class {
248
248
  * @returns The response from ChatGPT
249
249
  */
250
250
  async sendMessage(text, opts = {}, pluginParams) {
251
+ var _a, _b, _c, _d, _e, _f, _g;
251
252
  let parentMsg;
252
253
  if (pluginParams) {
253
254
  parentMsg = await this._getMessageById(pluginParams.assistant_res.id);
@@ -279,8 +280,42 @@ var ChatGPTAPI = class {
279
280
  let latestQuestion = message, last_assistant_res_message = null, last_assistant_res = null, last_plugin_res = null;
280
281
  if (pluginParams) {
281
282
  last_assistant_res_message = pluginParams.assistant_res.detail.choices[0];
283
+ let toolCalls = ((_a = last_assistant_res_message == null ? void 0 : last_assistant_res_message.message) == null ? void 0 : _a.tool_calls) || [];
284
+ if (!toolCalls || toolCalls.length === 0) {
285
+ toolCalls = ((_b = last_assistant_res_message == null ? void 0 : last_assistant_res_message.delta) == null ? void 0 : _b.tool_calls) || [];
286
+ }
287
+ if (!toolCalls || toolCalls.length === 0) {
288
+ toolCalls = ((_f = (_e = (_d = (_c = pluginParams.assistant_res.detail) == null ? void 0 : _c.choices) == null ? void 0 : _d[0]) == null ? void 0 : _e.delta) == null ? void 0 : _f.tool_calls) || [];
289
+ }
290
+ if ((!toolCalls || toolCalls.length === 0) && pluginParams.plugin_res && Array.isArray(pluginParams.plugin_res) && pluginParams.plugin_res.length > 0) {
291
+ const firstPluginRes = pluginParams.plugin_res[0];
292
+ if (firstPluginRes == null ? void 0 : firstPluginRes.skill) {
293
+ toolCalls = [{
294
+ id: `call_${Date.now()}`,
295
+ type: "function",
296
+ function: {
297
+ name: firstPluginRes.skill,
298
+ arguments: "{}"
299
+ },
300
+ index: 0
301
+ }];
302
+ }
303
+ }
304
+ if (!toolCalls || toolCalls.length === 0) {
305
+ throw new Error("\u65E0\u6CD5\u4ECE assistant_res \u4E2D\u83B7\u53D6 tool_calls\uFF0C\u8BF7\u786E\u4FDD assistant_res.detail.choices[0].message.tool_calls \u6216 delta.tool_calls \u5B58\u5728");
306
+ }
307
+ if (!last_assistant_res_message.message) {
308
+ last_assistant_res_message.message = {
309
+ role: ((_g = last_assistant_res_message.message) == null ? void 0 : _g.role) || "assistant",
310
+ tool_calls: toolCalls
311
+ };
312
+ } else {
313
+ if (!last_assistant_res_message.message.tool_calls || last_assistant_res_message.message.tool_calls.length === 0) {
314
+ last_assistant_res_message.message.tool_calls = toolCalls;
315
+ }
316
+ }
282
317
  last_assistant_res = {
283
- role: last_assistant_res_message.message.role,
318
+ role: last_assistant_res_message.message.role || "assistant",
284
319
  id: parentMsg.id,
285
320
  conversationId,
286
321
  parentMessageId: parentMsg.parentMessageId,
@@ -294,12 +329,12 @@ var ChatGPTAPI = class {
294
329
  last_plugin_res = {
295
330
  role: r,
296
331
  id: messageId,
297
- name: last_assistant_res_message.message.tool_calls[0].function.name,
332
+ name: toolCalls[0].function.name,
298
333
  conversationId,
299
334
  parentMessageId: pluginParams.assistant_res.id,
300
335
  text,
301
336
  content: pluginParams.plugin_res,
302
- tool_call_id: last_assistant_res_message.message.tool_calls[0].id
337
+ tool_call_id: toolCalls[0].id
303
338
  };
304
339
  await this._upsertMessage(last_assistant_res);
305
340
  latestQuestion = last_plugin_res;
@@ -332,7 +367,7 @@ var ChatGPTAPI = class {
332
367
  };
333
368
  const responseP = new Promise(
334
369
  async (resolve, reject) => {
335
- var _a, _b, _c, _d, _e, _f, _g, _h;
370
+ var _a2, _b2, _c2, _d2, _e2, _f2, _g2, _h;
336
371
  let url = `${this._apiBaseUrl}/chat/completions`;
337
372
  const headers = {
338
373
  "Content-Type": "application/json",
@@ -430,8 +465,8 @@ var ChatGPTAPI = class {
430
465
  } else if (this._manufacturer.toLowerCase() === "chatdoc") {
431
466
  let query = messages.splice(messages.length - 1, 1);
432
467
  body = Object.assign(body, {
433
- upload_id: (_a = completionParams == null ? void 0 : completionParams.extParams) == null ? void 0 : _a.upload_id,
434
- question: (_b = query[0]) == null ? void 0 : _b.content,
468
+ upload_id: (_a2 = completionParams == null ? void 0 : completionParams.extParams) == null ? void 0 : _a2.upload_id,
469
+ question: (_b2 = query[0]) == null ? void 0 : _b2.content,
435
470
  history: messages
436
471
  });
437
472
  if (body.extParams) delete body.extParams;
@@ -504,7 +539,7 @@ var ChatGPTAPI = class {
504
539
  body: JSON.stringify(body),
505
540
  signal: abortSignal,
506
541
  onMessage: (data) => {
507
- var _a2, _b2, _c2, _d2, _e2, _f2, _g2, _h2, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B;
542
+ var _a3, _b3, _c3, _d3, _e3, _f3, _g3, _h2, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B;
508
543
  if (data === "[DONE]") {
509
544
  result.text = result.text.trim();
510
545
  return resolve(result);
@@ -525,21 +560,21 @@ var ChatGPTAPI = class {
525
560
  return resolve(result);
526
561
  }
527
562
  } else if (this._manufacturer.toLowerCase() === "azure") {
528
- if (((_a2 = response.choices[0]) == null ? void 0 : _a2.finish_reason) === "stop") {
563
+ if (((_a3 = response.choices[0]) == null ? void 0 : _a3.finish_reason) === "stop") {
529
564
  result.text = result.text.trim();
530
565
  return resolve(result);
531
566
  }
532
567
  } else if (this._manufacturer.toLowerCase() === "aliyun") {
533
568
  if (["stop", "length"].indexOf(
534
- (_c2 = (_b2 = response == null ? void 0 : response.output) == null ? void 0 : _b2.choices[0]) == null ? void 0 : _c2.finish_reason
569
+ (_c3 = (_b3 = response == null ? void 0 : response.output) == null ? void 0 : _b3.choices[0]) == null ? void 0 : _c3.finish_reason
535
570
  ) > -1) {
536
- result.text = (_f2 = (_e2 = (_d2 = response == null ? void 0 : response.output) == null ? void 0 : _d2.choices[0]) == null ? void 0 : _e2.message) == null ? void 0 : _f2.content.trim();
571
+ result.text = (_f3 = (_e3 = (_d3 = response == null ? void 0 : response.output) == null ? void 0 : _d3.choices[0]) == null ? void 0 : _e3.message) == null ? void 0 : _f3.content.trim();
537
572
  return resolve(result);
538
573
  }
539
574
  } else if (this._manufacturer.toLowerCase() === "zhipu") {
540
575
  if (completionParams.model === "glm-4") {
541
576
  const gelResponse = JSON.parse(response.data);
542
- if (((_h2 = (_g2 = gelResponse == null ? void 0 : gelResponse.choices) == null ? void 0 : _g2[0]) == null ? void 0 : _h2.finish_reason) === "stop") {
577
+ if (((_h2 = (_g3 = gelResponse == null ? void 0 : gelResponse.choices) == null ? void 0 : _g3[0]) == null ? void 0 : _h2.finish_reason) === "stop") {
543
578
  result.text = result.text.trim();
544
579
  return resolve(result);
545
580
  }
@@ -682,7 +717,7 @@ var ChatGPTAPI = class {
682
717
  console.log(
683
718
  `row data ${typeof response} : `,
684
719
  response,
685
- (response == null ? void 0 : response.choices) && ((_c = response == null ? void 0 : response.choices[0]) == null ? void 0 : _c.message)
720
+ (response == null ? void 0 : response.choices) && ((_c2 = response == null ? void 0 : response.choices[0]) == null ? void 0 : _c2.message)
686
721
  );
687
722
  }
688
723
  if (this._manufacturer.toLowerCase() === "aliyun") {
@@ -694,7 +729,7 @@ var ChatGPTAPI = class {
694
729
  result.id = response.id;
695
730
  }
696
731
  }
697
- if (((_d = response == null ? void 0 : response.choices) == null ? void 0 : _d.length) && ["openai", "azure"].indexOf(this._manufacturer.toLowerCase()) > -1) {
732
+ if (((_d2 = response == null ? void 0 : response.choices) == null ? void 0 : _d2.length) && ["openai", "azure"].indexOf(this._manufacturer.toLowerCase()) > -1) {
698
733
  const message2 = response.choices[0].message;
699
734
  result.text = message2.content;
700
735
  if (message2.role) {
@@ -703,14 +738,14 @@ var ChatGPTAPI = class {
703
738
  } else if ((response == null ? void 0 : response.result) && this._manufacturer.toLowerCase() === "baidu") {
704
739
  result.text = response.result;
705
740
  result.role = "assistant";
706
- } else if (((_e = response == null ? void 0 : response.output) == null ? void 0 : _e.text) && this._manufacturer.toLowerCase() === "aliyun") {
707
- result.text = (_f = response == null ? void 0 : response.output) == null ? void 0 : _f.text;
741
+ } else if (((_e2 = response == null ? void 0 : response.output) == null ? void 0 : _e2.text) && this._manufacturer.toLowerCase() === "aliyun") {
742
+ result.text = (_f2 = response == null ? void 0 : response.output) == null ? void 0 : _f2.text;
708
743
  result.role = "assistant";
709
744
  } else {
710
745
  const res2 = response;
711
746
  return reject(
712
747
  new Error(
713
- `${this._manufacturer} error: ${((_g = res2 == null ? void 0 : res2.detail) == null ? void 0 : _g.message) || ((_h = res2 == null ? void 0 : res2.detail) == null ? void 0 : _h.error_msg) || (res2 == null ? void 0 : res2.detail) || "unknown"}`
748
+ `${this._manufacturer} error: ${((_g2 = res2 == null ? void 0 : res2.detail) == null ? void 0 : _g2.message) || ((_h = res2 == null ? void 0 : res2.detail) == null ? void 0 : _h.error_msg) || (res2 == null ? void 0 : res2.detail) || "unknown"}`
714
749
  )
715
750
  );
716
751
  }
@@ -725,7 +760,7 @@ var ChatGPTAPI = class {
725
760
  }
726
761
  }
727
762
  ).then(async (message2) => {
728
- var _a, _b, _c, _d, _e;
763
+ var _a2, _b2, _c2, _d2, _e2;
729
764
  if (this._debug) console.log("\u8DDF\u8E2A7", JSON.stringify(message2));
730
765
  if (message2.detail) {
731
766
  if (!message2.detail.usage) {
@@ -733,7 +768,7 @@ var ChatGPTAPI = class {
733
768
  const promptTokens = numTokens;
734
769
  let completionTokens = 0;
735
770
  if (["baidu", "aliyun"].indexOf(this._manufacturer.toLowerCase()) > -1) {
736
- completionTokens = (_b = (_a = message2.detail) == null ? void 0 : _a.usage) == null ? void 0 : _b.total_tokens;
771
+ completionTokens = (_b2 = (_a2 = message2.detail) == null ? void 0 : _a2.usage) == null ? void 0 : _b2.total_tokens;
737
772
  } else {
738
773
  completionTokens = await this._getTokenCount(message2.text);
739
774
  }
@@ -747,7 +782,7 @@ var ChatGPTAPI = class {
747
782
  }
748
783
  }
749
784
  if (this._manufacturer.toLowerCase() === "azure") {
750
- if (((_d = (_c = message2.detail) == null ? void 0 : _c.choices[0]) == null ? void 0 : _d.finish_reason) === "function_call") {
785
+ if (((_d2 = (_c2 = message2.detail) == null ? void 0 : _c2.choices[0]) == null ? void 0 : _d2.finish_reason) === "function_call") {
751
786
  message2.detail.choices[0].finish_reason = "tool_calls";
752
787
  message2.detail.choices[0].message = {
753
788
  role: message2.detail.choices[0].message.role,
@@ -755,7 +790,7 @@ var ChatGPTAPI = class {
755
790
  {
756
791
  id: `call_${uuidv4()}`,
757
792
  type: "function",
758
- function: (_e = message2.detail.choices[0].message) == null ? void 0 : _e.function_call
793
+ function: (_e2 = message2.detail.choices[0].message) == null ? void 0 : _e2.function_call
759
794
  }
760
795
  ]
761
796
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leikeduntech/leiai-js",
3
- "version": "4.0.4",
3
+ "version": "4.1.0",
4
4
  "author": "liuhean",
5
5
  "repository": {
6
6
  "type": "git",