@chatluna/v1-shared-adapter 1.0.8 → 1.0.10
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 +27 -22
- package/lib/index.mjs +27 -22
- package/lib/requester.d.ts +2 -1
- package/lib/utils.d.ts +2 -2
- package/package.json +2 -2
package/lib/index.cjs
CHANGED
|
@@ -111,8 +111,9 @@ var imageModelMatchers = [
|
|
|
111
111
|
"gemini",
|
|
112
112
|
"qwen-vl",
|
|
113
113
|
"omni",
|
|
114
|
-
"
|
|
114
|
+
"qwen*-omni",
|
|
115
115
|
"qwen-omni",
|
|
116
|
+
"qwen*-vl",
|
|
116
117
|
"qvq",
|
|
117
118
|
"o1",
|
|
118
119
|
"o3",
|
|
@@ -138,7 +139,7 @@ var import_sse = require("koishi-plugin-chatluna/utils/sse");
|
|
|
138
139
|
var import_messages = require("@langchain/core/messages");
|
|
139
140
|
var import_zod_to_json_schema = require("zod-to-json-schema");
|
|
140
141
|
var import_string = require("koishi-plugin-chatluna/utils/string");
|
|
141
|
-
var
|
|
142
|
+
var import_types = require("@langchain/core/utils/types");
|
|
142
143
|
async function langchainMessageToOpenAIMessage(messages, plugin, model, supportImageInput2, removeSystemMessage) {
|
|
143
144
|
const result = [];
|
|
144
145
|
for (const rawMessage of messages) {
|
|
@@ -148,7 +149,6 @@ async function langchainMessageToOpenAIMessage(messages, plugin, model, supportI
|
|
|
148
149
|
name: role === "assistant" || role === "tool" ? rawMessage.name : void 0,
|
|
149
150
|
role,
|
|
150
151
|
// function_call: rawMessage.additional_kwargs.function_call,
|
|
151
|
-
tool_calls: rawMessage.additional_kwargs.tool_calls,
|
|
152
152
|
tool_call_id: rawMessage.tool_call_id
|
|
153
153
|
};
|
|
154
154
|
if (msg.tool_calls == null) {
|
|
@@ -157,13 +157,17 @@ async function langchainMessageToOpenAIMessage(messages, plugin, model, supportI
|
|
|
157
157
|
if (msg.tool_call_id == null) {
|
|
158
158
|
delete msg.tool_call_id;
|
|
159
159
|
}
|
|
160
|
-
if (
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
160
|
+
if (rawMessage.getType() === "ai") {
|
|
161
|
+
const toolCalls = rawMessage.tool_calls;
|
|
162
|
+
if (Array.isArray(toolCalls) && toolCalls.length > 0) {
|
|
163
|
+
msg.tool_calls = toolCalls.map((toolCall) => ({
|
|
164
|
+
id: toolCall.id,
|
|
165
|
+
type: "function",
|
|
166
|
+
function: {
|
|
167
|
+
name: toolCall.name,
|
|
168
|
+
arguments: JSON.stringify(toolCall.args)
|
|
169
|
+
}
|
|
170
|
+
}));
|
|
167
171
|
}
|
|
168
172
|
}
|
|
169
173
|
const images = rawMessage.additional_kwargs.images;
|
|
@@ -294,7 +298,7 @@ function formatToolsToOpenAITools(tools, includeGoogleSearch) {
|
|
|
294
298
|
__name(formatToolsToOpenAITools, "formatToolsToOpenAITools");
|
|
295
299
|
function formatToolToOpenAITool(tool) {
|
|
296
300
|
const parameters = removeAdditionalProperties(
|
|
297
|
-
tool.schema
|
|
301
|
+
(0, import_types.isZodSchemaV3)(tool.schema) ? (0, import_zod_to_json_schema.zodToJsonSchema)(tool.schema, {
|
|
298
302
|
allowedAdditionalProperties: void 0
|
|
299
303
|
}) : tool.schema
|
|
300
304
|
);
|
|
@@ -337,13 +341,6 @@ function convertMessageToMessageChunk(message) {
|
|
|
337
341
|
const reasoningContent = message.reasoning_content ?? "";
|
|
338
342
|
const role = ((message.role?.length ?? 0) > 0 ? message.role : "assistant").toLowerCase();
|
|
339
343
|
let additionalKwargs;
|
|
340
|
-
if (message.tool_calls) {
|
|
341
|
-
additionalKwargs = {
|
|
342
|
-
tool_calls: message.tool_calls
|
|
343
|
-
};
|
|
344
|
-
} else {
|
|
345
|
-
additionalKwargs = {};
|
|
346
|
-
}
|
|
347
344
|
if (reasoningContent.length > 0) {
|
|
348
345
|
additionalKwargs.reasoning_content = reasoningContent;
|
|
349
346
|
}
|
|
@@ -673,21 +670,29 @@ async function createEmbeddings(requestContext, params, embeddingUrl = "embeddin
|
|
|
673
670
|
}
|
|
674
671
|
throw new Error(`Call Embedding Error: ${JSON.stringify(data)}`);
|
|
675
672
|
} catch (e) {
|
|
676
|
-
|
|
673
|
+
if (e instanceof import_error.ChatLunaError) {
|
|
674
|
+
throw e;
|
|
675
|
+
}
|
|
677
676
|
throw new import_error.ChatLunaError(import_error.ChatLunaErrorCode.API_REQUEST_FAILED, e);
|
|
678
677
|
}
|
|
679
678
|
}
|
|
680
679
|
__name(createEmbeddings, "createEmbeddings");
|
|
681
|
-
async function getModels(requestContext) {
|
|
680
|
+
async function getModels(requestContext, config) {
|
|
682
681
|
const { modelRequester } = requestContext;
|
|
683
682
|
let data;
|
|
684
683
|
try {
|
|
685
|
-
const response = await modelRequester.get(
|
|
684
|
+
const response = await modelRequester.get(
|
|
685
|
+
"models",
|
|
686
|
+
{},
|
|
687
|
+
{ signal: config?.signal }
|
|
688
|
+
);
|
|
686
689
|
data = await response.text();
|
|
687
690
|
data = JSON.parse(data);
|
|
688
691
|
return data.data.map((model) => model.id);
|
|
689
692
|
} catch (e) {
|
|
690
|
-
|
|
693
|
+
if (e instanceof import_error.ChatLunaError) {
|
|
694
|
+
throw e;
|
|
695
|
+
}
|
|
691
696
|
throw new Error(
|
|
692
697
|
"error when listing openai models, Result: " + JSON.stringify(data)
|
|
693
698
|
);
|
package/lib/index.mjs
CHANGED
|
@@ -68,8 +68,9 @@ var imageModelMatchers = [
|
|
|
68
68
|
"gemini",
|
|
69
69
|
"qwen-vl",
|
|
70
70
|
"omni",
|
|
71
|
-
"
|
|
71
|
+
"qwen*-omni",
|
|
72
72
|
"qwen-omni",
|
|
73
|
+
"qwen*-vl",
|
|
73
74
|
"qvq",
|
|
74
75
|
"o1",
|
|
75
76
|
"o3",
|
|
@@ -108,7 +109,7 @@ import {
|
|
|
108
109
|
getImageMimeType,
|
|
109
110
|
isMessageContentImageUrl
|
|
110
111
|
} from "koishi-plugin-chatluna/utils/string";
|
|
111
|
-
import {
|
|
112
|
+
import { isZodSchemaV3 } from "@langchain/core/utils/types";
|
|
112
113
|
async function langchainMessageToOpenAIMessage(messages, plugin, model, supportImageInput2, removeSystemMessage) {
|
|
113
114
|
const result = [];
|
|
114
115
|
for (const rawMessage of messages) {
|
|
@@ -118,7 +119,6 @@ async function langchainMessageToOpenAIMessage(messages, plugin, model, supportI
|
|
|
118
119
|
name: role === "assistant" || role === "tool" ? rawMessage.name : void 0,
|
|
119
120
|
role,
|
|
120
121
|
// function_call: rawMessage.additional_kwargs.function_call,
|
|
121
|
-
tool_calls: rawMessage.additional_kwargs.tool_calls,
|
|
122
122
|
tool_call_id: rawMessage.tool_call_id
|
|
123
123
|
};
|
|
124
124
|
if (msg.tool_calls == null) {
|
|
@@ -127,13 +127,17 @@ async function langchainMessageToOpenAIMessage(messages, plugin, model, supportI
|
|
|
127
127
|
if (msg.tool_call_id == null) {
|
|
128
128
|
delete msg.tool_call_id;
|
|
129
129
|
}
|
|
130
|
-
if (
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
130
|
+
if (rawMessage.getType() === "ai") {
|
|
131
|
+
const toolCalls = rawMessage.tool_calls;
|
|
132
|
+
if (Array.isArray(toolCalls) && toolCalls.length > 0) {
|
|
133
|
+
msg.tool_calls = toolCalls.map((toolCall) => ({
|
|
134
|
+
id: toolCall.id,
|
|
135
|
+
type: "function",
|
|
136
|
+
function: {
|
|
137
|
+
name: toolCall.name,
|
|
138
|
+
arguments: JSON.stringify(toolCall.args)
|
|
139
|
+
}
|
|
140
|
+
}));
|
|
137
141
|
}
|
|
138
142
|
}
|
|
139
143
|
const images = rawMessage.additional_kwargs.images;
|
|
@@ -264,7 +268,7 @@ function formatToolsToOpenAITools(tools, includeGoogleSearch) {
|
|
|
264
268
|
__name(formatToolsToOpenAITools, "formatToolsToOpenAITools");
|
|
265
269
|
function formatToolToOpenAITool(tool) {
|
|
266
270
|
const parameters = removeAdditionalProperties(
|
|
267
|
-
tool.schema
|
|
271
|
+
isZodSchemaV3(tool.schema) ? zodToJsonSchema(tool.schema, {
|
|
268
272
|
allowedAdditionalProperties: void 0
|
|
269
273
|
}) : tool.schema
|
|
270
274
|
);
|
|
@@ -307,13 +311,6 @@ function convertMessageToMessageChunk(message) {
|
|
|
307
311
|
const reasoningContent = message.reasoning_content ?? "";
|
|
308
312
|
const role = ((message.role?.length ?? 0) > 0 ? message.role : "assistant").toLowerCase();
|
|
309
313
|
let additionalKwargs;
|
|
310
|
-
if (message.tool_calls) {
|
|
311
|
-
additionalKwargs = {
|
|
312
|
-
tool_calls: message.tool_calls
|
|
313
|
-
};
|
|
314
|
-
} else {
|
|
315
|
-
additionalKwargs = {};
|
|
316
|
-
}
|
|
317
314
|
if (reasoningContent.length > 0) {
|
|
318
315
|
additionalKwargs.reasoning_content = reasoningContent;
|
|
319
316
|
}
|
|
@@ -643,21 +640,29 @@ async function createEmbeddings(requestContext, params, embeddingUrl = "embeddin
|
|
|
643
640
|
}
|
|
644
641
|
throw new Error(`Call Embedding Error: ${JSON.stringify(data)}`);
|
|
645
642
|
} catch (e) {
|
|
646
|
-
|
|
643
|
+
if (e instanceof ChatLunaError) {
|
|
644
|
+
throw e;
|
|
645
|
+
}
|
|
647
646
|
throw new ChatLunaError(ChatLunaErrorCode.API_REQUEST_FAILED, e);
|
|
648
647
|
}
|
|
649
648
|
}
|
|
650
649
|
__name(createEmbeddings, "createEmbeddings");
|
|
651
|
-
async function getModels(requestContext) {
|
|
650
|
+
async function getModels(requestContext, config) {
|
|
652
651
|
const { modelRequester } = requestContext;
|
|
653
652
|
let data;
|
|
654
653
|
try {
|
|
655
|
-
const response = await modelRequester.get(
|
|
654
|
+
const response = await modelRequester.get(
|
|
655
|
+
"models",
|
|
656
|
+
{},
|
|
657
|
+
{ signal: config?.signal }
|
|
658
|
+
);
|
|
656
659
|
data = await response.text();
|
|
657
660
|
data = JSON.parse(data);
|
|
658
661
|
return data.data.map((model) => model.id);
|
|
659
662
|
} catch (e) {
|
|
660
|
-
|
|
663
|
+
if (e instanceof ChatLunaError) {
|
|
664
|
+
throw e;
|
|
665
|
+
}
|
|
661
666
|
throw new Error(
|
|
662
667
|
"error when listing openai models, Result: " + JSON.stringify(data)
|
|
663
668
|
);
|
package/lib/requester.d.ts
CHANGED
|
@@ -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
|
|
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/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):
|
|
13
|
-
export declare function convertDeltaToMessageChunk(delta: Record<string, any>, defaultRole?: ChatCompletionResponseMessageRoleEnum):
|
|
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;
|
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.
|
|
4
|
+
"version": "1.0.10",
|
|
5
5
|
"main": "lib/index.cjs",
|
|
6
6
|
"module": "lib/index.mjs",
|
|
7
7
|
"typings": "lib/index.d.ts",
|
|
@@ -70,6 +70,6 @@
|
|
|
70
70
|
},
|
|
71
71
|
"peerDependencies": {
|
|
72
72
|
"koishi": "^4.18.7",
|
|
73
|
-
"koishi-plugin-chatluna": "^1.3.0-alpha.
|
|
73
|
+
"koishi-plugin-chatluna": "^1.3.0-alpha.44"
|
|
74
74
|
}
|
|
75
75
|
}
|