@chatluna/v1-shared-adapter 1.0.11 → 1.0.13

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/lib/index.cjs CHANGED
@@ -442,6 +442,7 @@ __name(convertDeltaToMessageChunk, "convertDeltaToMessageChunk");
442
442
  // src/requester.ts
443
443
  var import_messages2 = require("@langchain/core/messages");
444
444
  var import_string2 = require("koishi-plugin-chatluna/utils/string");
445
+ var import_logger = require("koishi-plugin-chatluna/utils/logger");
445
446
  async function buildChatCompletionParams(params, plugin, enableGoogleSearch, supportImageInput2) {
446
447
  const base = {
447
448
  model: params.model,
@@ -511,6 +512,20 @@ async function* processStreamResponse(requestContext, iterator) {
511
512
  );
512
513
  }
513
514
  const choice = data.choices?.[0];
515
+ if (data.usage) {
516
+ yield new import_outputs.ChatGenerationChunk({
517
+ message: new import_messages2.AIMessageChunk(""),
518
+ text: "",
519
+ generationInfo: {
520
+ tokenUsage: {
521
+ promptTokens: data.usage.prompt_tokens,
522
+ completionTokens: data.usage.completion_tokens,
523
+ totalTokens: data.usage.total_tokens
524
+ }
525
+ }
526
+ });
527
+ continue;
528
+ }
514
529
  if (!choice) continue;
515
530
  const { delta } = choice;
516
531
  const messageChunk = convertDeltaToMessageChunk(delta, defaultRole);
@@ -523,16 +538,6 @@ async function* processStreamResponse(requestContext, iterator) {
523
538
  message: messageChunk,
524
539
  text: messageChunk.content
525
540
  });
526
- if (data.usage) {
527
- yield new import_outputs.ChatGenerationChunk({
528
- message: new import_messages2.AIMessageChunk(""),
529
- text: "",
530
- generationInfo: {
531
- tokenUsage: data.usage
532
- }
533
- });
534
- continue;
535
- }
536
541
  } catch (e) {
537
542
  if (errorCount > 5) {
538
543
  requestContext.modelRequester.logger.error(
@@ -604,15 +609,16 @@ async function processResponse(requestContext, response) {
604
609
  __name(processResponse, "processResponse");
605
610
  async function* completionStream(requestContext, params, completionUrl = "chat/completions", enableGoogleSearch, supportImageInput2) {
606
611
  const { modelRequester } = requestContext;
612
+ const chatCompletionParams = await buildChatCompletionParams(
613
+ params,
614
+ requestContext.plugin,
615
+ enableGoogleSearch ?? false,
616
+ supportImageInput2 ?? true
617
+ );
607
618
  try {
608
619
  const response = await modelRequester.post(
609
620
  completionUrl,
610
- await buildChatCompletionParams(
611
- params,
612
- requestContext.plugin,
613
- enableGoogleSearch ?? false,
614
- supportImageInput2 ?? true
615
- ),
621
+ chatCompletionParams,
616
622
  {
617
623
  signal: params.signal
618
624
  }
@@ -620,6 +626,13 @@ async function* completionStream(requestContext, params, completionUrl = "chat/c
620
626
  const iterator = (0, import_sse.sseIterable)(response);
621
627
  yield* processStreamResponse(requestContext, iterator);
622
628
  } catch (e) {
629
+ if (requestContext.ctx.chatluna.config.isLog) {
630
+ await (0, import_logger.trackLogToLocal)(
631
+ "Request",
632
+ JSON.stringify(chatCompletionParams),
633
+ requestContext.ctx.logger("")
634
+ );
635
+ }
623
636
  if (e instanceof import_error.ChatLunaError) {
624
637
  throw e;
625
638
  } else {
@@ -647,6 +660,13 @@ async function completion(requestContext, params, completionUrl = "chat/completi
647
660
  );
648
661
  return await processResponse(requestContext, response);
649
662
  } catch (e) {
663
+ if (requestContext.ctx.chatluna.config.isLog) {
664
+ await (0, import_logger.trackLogToLocal)(
665
+ "Request",
666
+ JSON.stringify(chatCompletionParams),
667
+ requestContext.ctx.logger("")
668
+ );
669
+ }
650
670
  if (e instanceof import_error.ChatLunaError) {
651
671
  throw e;
652
672
  } else {
package/lib/index.mjs CHANGED
@@ -412,6 +412,7 @@ __name(convertDeltaToMessageChunk, "convertDeltaToMessageChunk");
412
412
  // src/requester.ts
413
413
  import { AIMessageChunk as AIMessageChunk2 } from "@langchain/core/messages";
414
414
  import { getMessageContent } from "koishi-plugin-chatluna/utils/string";
415
+ import { trackLogToLocal } from "koishi-plugin-chatluna/utils/logger";
415
416
  async function buildChatCompletionParams(params, plugin, enableGoogleSearch, supportImageInput2) {
416
417
  const base = {
417
418
  model: params.model,
@@ -481,6 +482,20 @@ async function* processStreamResponse(requestContext, iterator) {
481
482
  );
482
483
  }
483
484
  const choice = data.choices?.[0];
485
+ if (data.usage) {
486
+ yield new ChatGenerationChunk({
487
+ message: new AIMessageChunk2(""),
488
+ text: "",
489
+ generationInfo: {
490
+ tokenUsage: {
491
+ promptTokens: data.usage.prompt_tokens,
492
+ completionTokens: data.usage.completion_tokens,
493
+ totalTokens: data.usage.total_tokens
494
+ }
495
+ }
496
+ });
497
+ continue;
498
+ }
484
499
  if (!choice) continue;
485
500
  const { delta } = choice;
486
501
  const messageChunk = convertDeltaToMessageChunk(delta, defaultRole);
@@ -493,16 +508,6 @@ async function* processStreamResponse(requestContext, iterator) {
493
508
  message: messageChunk,
494
509
  text: messageChunk.content
495
510
  });
496
- if (data.usage) {
497
- yield new ChatGenerationChunk({
498
- message: new AIMessageChunk2(""),
499
- text: "",
500
- generationInfo: {
501
- tokenUsage: data.usage
502
- }
503
- });
504
- continue;
505
- }
506
511
  } catch (e) {
507
512
  if (errorCount > 5) {
508
513
  requestContext.modelRequester.logger.error(
@@ -574,15 +579,16 @@ async function processResponse(requestContext, response) {
574
579
  __name(processResponse, "processResponse");
575
580
  async function* completionStream(requestContext, params, completionUrl = "chat/completions", enableGoogleSearch, supportImageInput2) {
576
581
  const { modelRequester } = requestContext;
582
+ const chatCompletionParams = await buildChatCompletionParams(
583
+ params,
584
+ requestContext.plugin,
585
+ enableGoogleSearch ?? false,
586
+ supportImageInput2 ?? true
587
+ );
577
588
  try {
578
589
  const response = await modelRequester.post(
579
590
  completionUrl,
580
- await buildChatCompletionParams(
581
- params,
582
- requestContext.plugin,
583
- enableGoogleSearch ?? false,
584
- supportImageInput2 ?? true
585
- ),
591
+ chatCompletionParams,
586
592
  {
587
593
  signal: params.signal
588
594
  }
@@ -590,6 +596,13 @@ async function* completionStream(requestContext, params, completionUrl = "chat/c
590
596
  const iterator = sseIterable(response);
591
597
  yield* processStreamResponse(requestContext, iterator);
592
598
  } catch (e) {
599
+ if (requestContext.ctx.chatluna.config.isLog) {
600
+ await trackLogToLocal(
601
+ "Request",
602
+ JSON.stringify(chatCompletionParams),
603
+ requestContext.ctx.logger("")
604
+ );
605
+ }
593
606
  if (e instanceof ChatLunaError) {
594
607
  throw e;
595
608
  } else {
@@ -617,6 +630,13 @@ async function completion(requestContext, params, completionUrl = "chat/completi
617
630
  );
618
631
  return await processResponse(requestContext, response);
619
632
  } catch (e) {
633
+ if (requestContext.ctx.chatluna.config.isLog) {
634
+ await trackLogToLocal(
635
+ "Request",
636
+ JSON.stringify(chatCompletionParams),
637
+ requestContext.ctx.logger("")
638
+ );
639
+ }
620
640
  if (e instanceof ChatLunaError) {
621
641
  throw e;
622
642
  } else {
package/lib/utils.d.ts CHANGED
@@ -9,5 +9,5 @@ export declare function messageTypeToOpenAIRole(type: MessageType): ChatCompleti
9
9
  export declare function formatToolsToOpenAITools(tools: StructuredTool[], includeGoogleSearch: boolean): ChatCompletionTool[];
10
10
  export declare function formatToolToOpenAITool(tool: StructuredTool): ChatCompletionTool;
11
11
  export declare function removeAdditionalProperties(schema: JsonSchema7Type): JsonSchema7Type;
12
- export declare function convertMessageToMessageChunk(message: ChatCompletionResponseMessage): HumanMessageChunk | AIMessageChunk | SystemMessageChunk | FunctionMessageChunk | ToolMessageChunk | ChatMessageChunk;
13
- export declare function convertDeltaToMessageChunk(delta: Record<string, any>, defaultRole?: ChatCompletionResponseMessageRoleEnum): HumanMessageChunk | AIMessageChunk | SystemMessageChunk | FunctionMessageChunk | ToolMessageChunk | ChatMessageChunk;
12
+ export declare function convertMessageToMessageChunk(message: ChatCompletionResponseMessage): AIMessageChunk | HumanMessageChunk | SystemMessageChunk | FunctionMessageChunk | ToolMessageChunk | ChatMessageChunk;
13
+ export declare function convertDeltaToMessageChunk(delta: Record<string, any>, defaultRole?: ChatCompletionResponseMessageRoleEnum): AIMessageChunk | HumanMessageChunk | SystemMessageChunk | FunctionMessageChunk | ToolMessageChunk | ChatMessageChunk;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@chatluna/v1-shared-adapter",
3
3
  "description": "chatluna shared adapter",
4
- "version": "1.0.11",
4
+ "version": "1.0.13",
5
5
  "main": "lib/index.cjs",
6
6
  "module": "lib/index.mjs",
7
7
  "typings": "lib/index.d.ts",
@@ -22,13 +22,13 @@
22
22
  "repository": {
23
23
  "type": "git",
24
24
  "url": "https://github.com/ChatLunaLab/chatluna.git",
25
- "directory": "packages/openai-like-adapter"
25
+ "directory": "packages/shared-adapter"
26
26
  },
27
27
  "license": "AGPL-3.0",
28
28
  "bugs": {
29
29
  "url": "https://github.com/ChatLunaLab/chatluna/issues"
30
30
  },
31
- "homepage": "https://github.com/ChatLunaLab/chatluna/tree/v1-dev/packages/openai-like-adapter#readme",
31
+ "homepage": "https://github.com/ChatLunaLab/chatluna/tree/v1-dev/packages/shared-adapter#readme",
32
32
  "scripts": {
33
33
  "build": "atsc -b"
34
34
  },
@@ -62,14 +62,14 @@
62
62
  "dependencies": {
63
63
  "@langchain/core": "0.3.62",
64
64
  "zod": "3.25.76",
65
- "zod-to-json-schema": "^3.24.5"
65
+ "zod-to-json-schema": "^3.24.6"
66
66
  },
67
67
  "devDependencies": {
68
68
  "atsc": "^2.1.0",
69
- "koishi": "^4.18.7"
69
+ "koishi": "^4.18.9"
70
70
  },
71
71
  "peerDependencies": {
72
- "koishi": "^4.18.7",
73
- "koishi-plugin-chatluna": "^1.3.0-alpha.45"
72
+ "koishi": "^4.18.9",
73
+ "koishi-plugin-chatluna": "^1.3.0-alpha.56"
74
74
  }
75
75
  }