@leikeduntech/leiai-js 4.1.2 → 4.1.5

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 +12 -110
  2. package/package.json +1 -1
package/build/index.js CHANGED
@@ -1,10 +1,3 @@
1
- var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
- get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
- }) : x)(function(x) {
4
- if (typeof require !== "undefined") return require.apply(this, arguments);
5
- throw Error('Dynamic require of "' + x + '" is not supported');
6
- });
7
-
8
1
  // src/chatgpt-api.ts
9
2
  import { Spark } from "@leikeduntech/spark-nodejs";
10
3
  import Keyv from "keyv";
@@ -342,7 +335,7 @@ var ChatGPTAPI = class {
342
335
  };
343
336
  const responseP = new Promise(
344
337
  async (resolve, reject) => {
345
- var _a2, _b2, _c2, _d2, _e2, _f2, _g2;
338
+ var _a2, _b2, _c2, _d2, _e2, _f2;
346
339
  let url = `${this._apiBaseUrl}/chat/completions`;
347
340
  const headers = {
348
341
  "Content-Type": "application/json",
@@ -396,32 +389,6 @@ var ChatGPTAPI = class {
396
389
  typeof messages
397
390
  );
398
391
  }
399
- if (pluginParams) {
400
- const debugLog = {
401
- location: "chatgpt-api.ts:430",
402
- message: "Second sendMessage with pluginParams",
403
- data: {
404
- manufacturer: this._manufacturer,
405
- messagesLength: messages.length,
406
- messages: JSON.stringify(messages).substring(0, 2e3),
407
- hasPluginRes: !!(pluginParams == null ? void 0 : pluginParams.plugin_res),
408
- pluginResPreview: JSON.stringify(pluginParams == null ? void 0 : pluginParams.plugin_res).substring(0, 500),
409
- bodyMessagesLength: (_a2 = body.messages) == null ? void 0 : _a2.length,
410
- bodyMessages: JSON.stringify(body.messages).substring(0, 2e3),
411
- stream,
412
- url
413
- },
414
- timestamp: Date.now(),
415
- sessionId: "debug-session",
416
- runId: "run19",
417
- hypothesisId: "T"
418
- };
419
- try {
420
- const fs = __require("fs");
421
- fs.appendFileSync("/Volumes/softdev2/gpt-project/LeiAI-Web-H5/.cursor/debug.log", JSON.stringify(debugLog) + "\n");
422
- } catch (e) {
423
- }
424
- }
425
392
  if (this._manufacturer.toLowerCase() === "xunfei") {
426
393
  const self = this;
427
394
  const keyList = this._apiKey.split(".");
@@ -473,30 +440,8 @@ var ChatGPTAPI = class {
473
440
  body: JSON.stringify(body),
474
441
  signal: abortSignal,
475
442
  onMessage: (data) => {
476
- var _a3, _b3, _c3, _d3, _e3, _f3, _g3, _h2, _i;
443
+ var _a3, _b3;
477
444
  if (data === "[DONE]") {
478
- if (pluginParams) {
479
- const debugLog = {
480
- location: "chatgpt-api.ts:520",
481
- message: "Second sendMessage stream DONE",
482
- data: {
483
- manufacturer: this._manufacturer,
484
- finalTextLength: result.text.length,
485
- finalTextPreview: result.text.substring(0, 500),
486
- hasDetail: !!result.detail,
487
- resultKeys: Object.keys(result || {})
488
- },
489
- timestamp: Date.now(),
490
- sessionId: "debug-session",
491
- runId: "run19",
492
- hypothesisId: "T"
493
- };
494
- try {
495
- const fs = __require("fs");
496
- fs.appendFileSync("/Volumes/softdev2/gpt-project/LeiAI-Web-H5/.cursor/debug.log", JSON.stringify(debugLog) + "\n");
497
- } catch (e) {
498
- }
499
- }
500
445
  result.text = result.text.trim();
501
446
  return resolve(result);
502
447
  }
@@ -529,60 +474,11 @@ var ChatGPTAPI = class {
529
474
  if (response == null ? void 0 : response.id) {
530
475
  result.id = response.id;
531
476
  }
532
- if (pluginParams) {
533
- const debugLog = {
534
- location: "chatgpt-api.ts:570",
535
- message: "Second sendMessage onMessage (streaming)",
536
- data: {
537
- manufacturer: this._manufacturer,
538
- hasDelta: !!delta,
539
- deltaContent: delta == null ? void 0 : delta.content,
540
- deltaContentLength: (_c3 = delta == null ? void 0 : delta.content) == null ? void 0 : _c3.length,
541
- finishReason,
542
- resultTextLength: result.text.length,
543
- resultTextPreview: result.text.substring(0, 200),
544
- responseChoicesLength: (_d3 = response.choices) == null ? void 0 : _d3.length
545
- },
546
- timestamp: Date.now(),
547
- sessionId: "debug-session",
548
- runId: "run19",
549
- hypothesisId: "T"
550
- };
551
- try {
552
- const fs = __require("fs");
553
- fs.appendFileSync("/Volumes/softdev2/gpt-project/LeiAI-Web-H5/.cursor/debug.log", JSON.stringify(debugLog) + "\n");
554
- } catch (e) {
555
- }
556
- }
557
477
  onProgress == null ? void 0 : onProgress(result);
558
478
  if (finishReason === "stop" || finishReason === "length") {
559
479
  result.text = result.text.trim();
560
480
  return resolve(result);
561
481
  }
562
- } else {
563
- if (pluginParams) {
564
- const debugLog = {
565
- location: "chatgpt-api.ts:605",
566
- message: "Second sendMessage onMessage (unexpected format)",
567
- data: {
568
- manufacturer: this._manufacturer,
569
- responseStructure: JSON.stringify(response).substring(0, 1e3),
570
- hasChoices: !!response.choices,
571
- choicesLength: (_e3 = response.choices) == null ? void 0 : _e3.length,
572
- firstChoiceDelta: (_g3 = (_f3 = response.choices) == null ? void 0 : _f3[0]) == null ? void 0 : _g3.delta,
573
- firstChoiceFinishReason: (_i = (_h2 = response.choices) == null ? void 0 : _h2[0]) == null ? void 0 : _i.finish_reason
574
- },
575
- timestamp: Date.now(),
576
- sessionId: "debug-session",
577
- runId: "run19",
578
- hypothesisId: "T"
579
- };
580
- try {
581
- const fs = __require("fs");
582
- fs.appendFileSync("/Volumes/softdev2/gpt-project/LeiAI-Web-H5/.cursor/debug.log", JSON.stringify(debugLog) + "\n");
583
- } catch (e) {
584
- }
585
- }
586
482
  }
587
483
  } catch (err) {
588
484
  console.warn(
@@ -619,13 +515,13 @@ var ChatGPTAPI = class {
619
515
  console.log(
620
516
  `row data ${typeof response} : `,
621
517
  response,
622
- (response == null ? void 0 : response.choices) && ((_b2 = response == null ? void 0 : response.choices[0]) == null ? void 0 : _b2.message)
518
+ (response == null ? void 0 : response.choices) && ((_a2 = response == null ? void 0 : response.choices[0]) == null ? void 0 : _a2.message)
623
519
  );
624
520
  }
625
521
  if (response == null ? void 0 : response.id) {
626
522
  result.id = response.id;
627
523
  }
628
- if (((_c2 = response == null ? void 0 : response.choices) == null ? void 0 : _c2.length) && ((_d2 = response.choices[0]) == null ? void 0 : _d2.message)) {
524
+ if (((_b2 = response == null ? void 0 : response.choices) == null ? void 0 : _b2.length) && ((_c2 = response.choices[0]) == null ? void 0 : _c2.message)) {
629
525
  const message2 = response.choices[0].message;
630
526
  result.text = message2.content || "";
631
527
  result.role = message2.role || "assistant";
@@ -633,7 +529,7 @@ var ChatGPTAPI = class {
633
529
  const res2 = response;
634
530
  return reject(
635
531
  new Error(
636
- `API error: ${((_e2 = res2 == null ? void 0 : res2.detail) == null ? void 0 : _e2.message) || ((_f2 = res2 == null ? void 0 : res2.detail) == null ? void 0 : _f2.error_msg) || ((_g2 = res2 == null ? void 0 : res2.error) == null ? void 0 : _g2.message) || (res2 == null ? void 0 : res2.detail) || "unknown"}`
532
+ `API error: ${((_d2 = res2 == null ? void 0 : res2.detail) == null ? void 0 : _d2.message) || ((_e2 = res2 == null ? void 0 : res2.detail) == null ? void 0 : _e2.error_msg) || ((_f2 = res2 == null ? void 0 : res2.error) == null ? void 0 : _f2.message) || (res2 == null ? void 0 : res2.detail) || "unknown"}`
637
533
  )
638
534
  );
639
535
  }
@@ -840,7 +736,13 @@ ${content}`]);
840
736
  const parentMessageRole = parentMessage.role || "user";
841
737
  let parentMessageItem = null;
842
738
  if (parentMessage.content && ((_a = parentMessage.content) == null ? void 0 : _a.finish_reason) === "tool_calls") {
843
- parentMessageItem = parentMessage.content.message;
739
+ const messageFromContent = parentMessage.content.message || {};
740
+ parentMessageItem = {
741
+ role: "assistant",
742
+ // 强制设置为 assistant(包含 tool_calls 的消息必须是 assistant 角色)
743
+ ...messageFromContent
744
+ // 保留其他字段(tool_calls, content 等)
745
+ };
844
746
  } else {
845
747
  parentMessageItem = {
846
748
  role: parentMessageRole,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leikeduntech/leiai-js",
3
- "version": "4.1.2",
3
+ "version": "4.1.5",
4
4
  "author": "liuhean",
5
5
  "repository": {
6
6
  "type": "git",