@chatluna/v1-shared-adapter 1.0.7 → 1.0.9

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
@@ -138,7 +138,7 @@ var import_sse = require("koishi-plugin-chatluna/utils/sse");
138
138
  var import_messages = require("@langchain/core/messages");
139
139
  var import_zod_to_json_schema = require("zod-to-json-schema");
140
140
  var import_string = require("koishi-plugin-chatluna/utils/string");
141
- var import_zod = require("zod");
141
+ var import_types = require("@langchain/core/utils/types");
142
142
  async function langchainMessageToOpenAIMessage(messages, plugin, model, supportImageInput2, removeSystemMessage) {
143
143
  const result = [];
144
144
  for (const rawMessage of messages) {
@@ -294,7 +294,7 @@ function formatToolsToOpenAITools(tools, includeGoogleSearch) {
294
294
  __name(formatToolsToOpenAITools, "formatToolsToOpenAITools");
295
295
  function formatToolToOpenAITool(tool) {
296
296
  const parameters = removeAdditionalProperties(
297
- tool.schema instanceof import_zod.ZodSchema ? (0, import_zod_to_json_schema.zodToJsonSchema)(tool.schema, {
297
+ (0, import_types.isZodSchemaV3)(tool.schema) ? (0, import_zod_to_json_schema.zodToJsonSchema)(tool.schema, {
298
298
  allowedAdditionalProperties: void 0
299
299
  }) : tool.schema
300
300
  );
@@ -513,16 +513,6 @@ async function* processStreamResponse(requestContext, iterator) {
513
513
  new Error("Error when calling completion, Result: " + chunk)
514
514
  );
515
515
  }
516
- if (data.usage) {
517
- yield new import_outputs.ChatGenerationChunk({
518
- message: new import_messages2.AIMessageChunk(""),
519
- text: "",
520
- generationInfo: {
521
- tokenUsage: data.usage
522
- }
523
- });
524
- continue;
525
- }
526
516
  const choice = data.choices?.[0];
527
517
  if (!choice) continue;
528
518
  const { delta } = choice;
@@ -536,6 +526,16 @@ async function* processStreamResponse(requestContext, iterator) {
536
526
  message: messageChunk,
537
527
  text: messageChunk.content
538
528
  });
529
+ if (data.usage) {
530
+ yield new import_outputs.ChatGenerationChunk({
531
+ message: new import_messages2.AIMessageChunk(""),
532
+ text: "",
533
+ generationInfo: {
534
+ tokenUsage: data.usage
535
+ }
536
+ });
537
+ continue;
538
+ }
539
539
  } catch (e) {
540
540
  if (errorCount > 5) {
541
541
  requestContext.modelRequester.logger.error(
@@ -673,21 +673,29 @@ async function createEmbeddings(requestContext, params, embeddingUrl = "embeddin
673
673
  }
674
674
  throw new Error(`Call Embedding Error: ${JSON.stringify(data)}`);
675
675
  } catch (e) {
676
- requestContext.modelRequester.logger.debug(e);
676
+ if (e instanceof import_error.ChatLunaError) {
677
+ throw e;
678
+ }
677
679
  throw new import_error.ChatLunaError(import_error.ChatLunaErrorCode.API_REQUEST_FAILED, e);
678
680
  }
679
681
  }
680
682
  __name(createEmbeddings, "createEmbeddings");
681
- async function getModels(requestContext) {
683
+ async function getModels(requestContext, config) {
682
684
  const { modelRequester } = requestContext;
683
685
  let data;
684
686
  try {
685
- const response = await modelRequester.get("models");
687
+ const response = await modelRequester.get(
688
+ "models",
689
+ {},
690
+ { signal: config?.signal }
691
+ );
686
692
  data = await response.text();
687
693
  data = JSON.parse(data);
688
694
  return data.data.map((model) => model.id);
689
695
  } catch (e) {
690
- requestContext.modelRequester.logger.error(e);
696
+ if (e instanceof import_error.ChatLunaError) {
697
+ throw e;
698
+ }
691
699
  throw new Error(
692
700
  "error when listing openai models, Result: " + JSON.stringify(data)
693
701
  );
package/lib/index.mjs CHANGED
@@ -108,7 +108,7 @@ import {
108
108
  getImageMimeType,
109
109
  isMessageContentImageUrl
110
110
  } from "koishi-plugin-chatluna/utils/string";
111
- import { ZodSchema } from "zod";
111
+ import { isZodSchemaV3 } from "@langchain/core/utils/types";
112
112
  async function langchainMessageToOpenAIMessage(messages, plugin, model, supportImageInput2, removeSystemMessage) {
113
113
  const result = [];
114
114
  for (const rawMessage of messages) {
@@ -264,7 +264,7 @@ function formatToolsToOpenAITools(tools, includeGoogleSearch) {
264
264
  __name(formatToolsToOpenAITools, "formatToolsToOpenAITools");
265
265
  function formatToolToOpenAITool(tool) {
266
266
  const parameters = removeAdditionalProperties(
267
- tool.schema instanceof ZodSchema ? zodToJsonSchema(tool.schema, {
267
+ isZodSchemaV3(tool.schema) ? zodToJsonSchema(tool.schema, {
268
268
  allowedAdditionalProperties: void 0
269
269
  }) : tool.schema
270
270
  );
@@ -483,16 +483,6 @@ async function* processStreamResponse(requestContext, iterator) {
483
483
  new Error("Error when calling completion, Result: " + chunk)
484
484
  );
485
485
  }
486
- if (data.usage) {
487
- yield new ChatGenerationChunk({
488
- message: new AIMessageChunk2(""),
489
- text: "",
490
- generationInfo: {
491
- tokenUsage: data.usage
492
- }
493
- });
494
- continue;
495
- }
496
486
  const choice = data.choices?.[0];
497
487
  if (!choice) continue;
498
488
  const { delta } = choice;
@@ -506,6 +496,16 @@ async function* processStreamResponse(requestContext, iterator) {
506
496
  message: messageChunk,
507
497
  text: messageChunk.content
508
498
  });
499
+ if (data.usage) {
500
+ yield new ChatGenerationChunk({
501
+ message: new AIMessageChunk2(""),
502
+ text: "",
503
+ generationInfo: {
504
+ tokenUsage: data.usage
505
+ }
506
+ });
507
+ continue;
508
+ }
509
509
  } catch (e) {
510
510
  if (errorCount > 5) {
511
511
  requestContext.modelRequester.logger.error(
@@ -643,21 +643,29 @@ async function createEmbeddings(requestContext, params, embeddingUrl = "embeddin
643
643
  }
644
644
  throw new Error(`Call Embedding Error: ${JSON.stringify(data)}`);
645
645
  } catch (e) {
646
- requestContext.modelRequester.logger.debug(e);
646
+ if (e instanceof ChatLunaError) {
647
+ throw e;
648
+ }
647
649
  throw new ChatLunaError(ChatLunaErrorCode.API_REQUEST_FAILED, e);
648
650
  }
649
651
  }
650
652
  __name(createEmbeddings, "createEmbeddings");
651
- async function getModels(requestContext) {
653
+ async function getModels(requestContext, config) {
652
654
  const { modelRequester } = requestContext;
653
655
  let data;
654
656
  try {
655
- const response = await modelRequester.get("models");
657
+ const response = await modelRequester.get(
658
+ "models",
659
+ {},
660
+ { signal: config?.signal }
661
+ );
656
662
  data = await response.text();
657
663
  data = JSON.parse(data);
658
664
  return data.data.map((model) => model.id);
659
665
  } catch (e) {
660
- requestContext.modelRequester.logger.error(e);
666
+ if (e instanceof ChatLunaError) {
667
+ throw e;
668
+ }
661
669
  throw new Error(
662
670
  "error when listing openai models, Result: " + JSON.stringify(data)
663
671
  );
@@ -5,6 +5,7 @@ import { SSEEvent } from 'koishi-plugin-chatluna/utils/sse';
5
5
  import { ChatLunaPlugin } from 'koishi-plugin-chatluna/services/chat';
6
6
  import { Context } from 'koishi';
7
7
  import { Response } from 'undici/types/fetch';
8
+ import { RunnableConfig } from '@langchain/core/runnables';
8
9
  interface RequestContext<T extends ClientConfig = ClientConfig, R extends ChatLunaPlugin.Config = ChatLunaPlugin.Config> {
9
10
  ctx: Context;
10
11
  config: T;
@@ -43,6 +44,6 @@ export declare function processResponse<T extends ClientConfig, R extends ChatLu
43
44
  export declare function completionStream<T extends ClientConfig, R extends ChatLunaPlugin.Config>(requestContext: RequestContext<T, R>, params: ModelRequestParams, completionUrl?: string, enableGoogleSearch?: boolean, supportImageInput?: boolean): AsyncGenerator<ChatGenerationChunk>;
44
45
  export declare function completion<T extends ClientConfig, R extends ChatLunaPlugin.Config>(requestContext: RequestContext<T, R>, params: ModelRequestParams, completionUrl?: string, enableGoogleSearch?: boolean, supportImageInput?: boolean): Promise<ChatGenerationChunk>;
45
46
  export declare function createEmbeddings<T extends ClientConfig, R extends ChatLunaPlugin.Config>(requestContext: RequestContext<T, R>, params: EmbeddingsRequestParams, embeddingUrl?: string): Promise<number[] | number[][]>;
46
- export declare function getModels<T extends ClientConfig, R extends ChatLunaPlugin.Config>(requestContext: RequestContext<T, R>): Promise<string[]>;
47
+ export declare function getModels<T extends ClientConfig, R extends ChatLunaPlugin.Config>(requestContext: RequestContext<T, R>, config?: RunnableConfig): Promise<string[]>;
47
48
  export declare function createRequestContext<T extends ClientConfig, R extends ChatLunaPlugin.Config>(ctx: Context, config: T, pluginConfig: R, plugin: ChatLunaPlugin, modelRequester: ModelRequester<T, R>): RequestContext<T, R>;
48
49
  export {};
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.7",
4
+ "version": "1.0.9",
5
5
  "main": "lib/index.cjs",
6
6
  "module": "lib/index.mjs",
7
7
  "typings": "lib/index.d.ts",
@@ -37,16 +37,16 @@
37
37
  },
38
38
  "resolutions": {
39
39
  "@langchain/core": "0.3.62",
40
- "js-tiktoken": "npm:@dingyi222666/js-tiktoken@^1.0.19"
40
+ "js-tiktoken": "npm:@dingyi222666/js-tiktoken@^1.0.21"
41
41
  },
42
42
  "overrides": {
43
43
  "@langchain/core": "0.3.62",
44
- "js-tiktoken": "npm:@dingyi222666/js-tiktoken@^1.0.19"
44
+ "js-tiktoken": "npm:@dingyi222666/js-tiktoken@^1.0.21"
45
45
  },
46
46
  "pnpm": {
47
47
  "overrides": {
48
48
  "@langchain/core": "0.3.62",
49
- "js-tiktoken": "npm:@dingyi222666/js-tiktoken@^1.0.19"
49
+ "js-tiktoken": "npm:@dingyi222666/js-tiktoken@^1.0.21"
50
50
  }
51
51
  },
52
52
  "keywords": [
@@ -70,6 +70,6 @@
70
70
  },
71
71
  "peerDependencies": {
72
72
  "koishi": "^4.18.7",
73
- "koishi-plugin-chatluna": "^1.3.0-alpha.28"
73
+ "koishi-plugin-chatluna": "^1.3.0-alpha.42"
74
74
  }
75
75
  }