@chatluna/v1-shared-adapter 1.0.31 → 1.0.33
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/client.d.ts +2 -1
- package/lib/index.cjs +451 -3
- package/lib/index.mjs +437 -3
- package/lib/requester.d.ts +31 -0
- package/lib/types.d.ts +169 -0
- package/lib/utils.d.ts +23 -1
- package/package.json +2 -2
package/lib/client.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ModelInfo } from 'koishi-plugin-chatluna/llm-core/platform/types';
|
|
2
|
-
export type OpenAIReasoningEffort = 'none' | 'minimal' | 'low' | 'medium' | 'high' | 'xhigh';
|
|
2
|
+
export type OpenAIReasoningEffort = 'none' | 'minimal' | 'low' | 'medium' | 'high' | 'max' | 'xhigh';
|
|
3
3
|
export declare const reasoningEffortModelSuffixes: readonly ["non-thinking", "minimal-thinking", "low-thinking", "medium-thinking", "high-thinking", "xhigh-thinking", "thinking"];
|
|
4
4
|
export declare function expandReasoningEffortModelVariants(model: string, suffixes?: readonly string[]): string[];
|
|
5
5
|
export declare function parseOpenAIModelNameWithReasoningEffort(modelName: string): {
|
|
@@ -9,5 +9,6 @@ export declare function parseOpenAIModelNameWithReasoningEffort(modelName: strin
|
|
|
9
9
|
export declare function normalizeOpenAIModelName(modelName: string): string;
|
|
10
10
|
export declare function isEmbeddingModel(modelName: string): boolean;
|
|
11
11
|
export declare function isNonLLMModel(modelName: string): boolean;
|
|
12
|
+
export declare function isImageGenerationModel(modelName: string): boolean;
|
|
12
13
|
export declare function getModelMaxContextSize(info: ModelInfo): number;
|
|
13
14
|
export declare function supportImageInput(modelName: string): boolean;
|
package/lib/index.cjs
CHANGED
|
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
23
|
buildChatCompletionParams: () => buildChatCompletionParams,
|
|
24
|
+
buildResponseParams: () => buildResponseParams,
|
|
24
25
|
completion: () => completion,
|
|
25
26
|
completionStream: () => completionStream,
|
|
26
27
|
convertDeltaToMessageChunk: () => convertDeltaToMessageChunk,
|
|
@@ -33,20 +34,33 @@ __export(index_exports, {
|
|
|
33
34
|
fetchImageUrl: () => fetchImageUrl,
|
|
34
35
|
formatToolToOpenAITool: () => formatToolToOpenAITool,
|
|
35
36
|
formatToolsToOpenAITools: () => formatToolsToOpenAITools,
|
|
37
|
+
formatToolsToResponseTools: () => formatToolsToResponseTools,
|
|
36
38
|
getModelMaxContextSize: () => getModelMaxContextSize,
|
|
37
39
|
getModels: () => getModels,
|
|
38
40
|
isEmbeddingModel: () => isEmbeddingModel,
|
|
41
|
+
isImageGenerationModel: () => isImageGenerationModel,
|
|
39
42
|
isNonLLMModel: () => isNonLLMModel,
|
|
40
43
|
langchainMessageToOpenAIMessage: () => langchainMessageToOpenAIMessage,
|
|
44
|
+
langchainMessageToResponseInput: () => langchainMessageToResponseInput,
|
|
41
45
|
messageTypeToOpenAIRole: () => messageTypeToOpenAIRole,
|
|
42
46
|
normalizeOpenAIModelName: () => normalizeOpenAIModelName,
|
|
47
|
+
openAIResponseUsageToUsageMetadata: () => openAIResponseUsageToUsageMetadata,
|
|
43
48
|
openAIUsageToUsageMetadata: () => openAIUsageToUsageMetadata,
|
|
44
49
|
parseOpenAIModelNameWithReasoningEffort: () => parseOpenAIModelNameWithReasoningEffort,
|
|
45
50
|
processInterleavedThinkMessages: () => processInterleavedThinkMessages,
|
|
46
51
|
processResponse: () => processResponse,
|
|
52
|
+
processResponseApiResponse: () => processResponseApiResponse,
|
|
53
|
+
processResponseApiStream: () => processResponseApiStream,
|
|
47
54
|
processStreamResponse: () => processStreamResponse,
|
|
48
55
|
reasoningEffortModelSuffixes: () => reasoningEffortModelSuffixes,
|
|
49
56
|
removeAdditionalProperties: () => removeAdditionalProperties,
|
|
57
|
+
responseApiCompletion: () => responseApiCompletion,
|
|
58
|
+
responseApiCompletionStream: () => responseApiCompletionStream,
|
|
59
|
+
responseInputContent: () => responseInputContent,
|
|
60
|
+
responseOutputImageItems: () => responseOutputImageItems,
|
|
61
|
+
responseOutputText: () => responseOutputText,
|
|
62
|
+
responseOutputToolCalls: () => responseOutputToolCalls,
|
|
63
|
+
responseToChatGeneration: () => responseToChatGeneration,
|
|
50
64
|
supportImageInput: () => supportImageInput,
|
|
51
65
|
transformSystemMessages: () => transformSystemMessages
|
|
52
66
|
});
|
|
@@ -71,7 +85,7 @@ function parseOpenAIModelNameWithReasoningEffort(modelName) {
|
|
|
71
85
|
let model = modelName;
|
|
72
86
|
let reasoningEffort;
|
|
73
87
|
const explicitMatch = model.match(
|
|
74
|
-
/-(none|minimal|low|medium|high|xhigh|tiny)-thinking$/
|
|
88
|
+
/-(none|minimal|low|medium|high|max|xhigh|tiny)-thinking$/
|
|
75
89
|
);
|
|
76
90
|
if (explicitMatch?.[1]) {
|
|
77
91
|
const level = explicitMatch[1];
|
|
@@ -109,6 +123,10 @@ function isNonLLMModel(modelName) {
|
|
|
109
123
|
);
|
|
110
124
|
}
|
|
111
125
|
__name(isNonLLMModel, "isNonLLMModel");
|
|
126
|
+
function isImageGenerationModel(modelName) {
|
|
127
|
+
return isNonLLMModel(modelName) && ["dall-e", "image"].some((keyword) => modelName.includes(keyword));
|
|
128
|
+
}
|
|
129
|
+
__name(isImageGenerationModel, "isImageGenerationModel");
|
|
112
130
|
function getModelMaxContextSize(info) {
|
|
113
131
|
const maxTokens = info.maxTokens;
|
|
114
132
|
if (maxTokens != null) {
|
|
@@ -127,6 +145,7 @@ function getModelMaxContextSize(info) {
|
|
|
127
145
|
"gemini-2.0-pro": 2097152,
|
|
128
146
|
"gemini-2.5": 2097152,
|
|
129
147
|
"gemini-3.0-pro": 1097152,
|
|
148
|
+
"gemini-3.1-pro": 1097152,
|
|
130
149
|
"gemini-2.0": 2097152,
|
|
131
150
|
deepseek: 128e3,
|
|
132
151
|
"llama3.1": 128e3,
|
|
@@ -143,7 +162,7 @@ function getModelMaxContextSize(info) {
|
|
|
143
162
|
return modelMaxContextSizeTable[key];
|
|
144
163
|
}
|
|
145
164
|
}
|
|
146
|
-
return
|
|
165
|
+
return 2e5;
|
|
147
166
|
}
|
|
148
167
|
__name(getModelMaxContextSize, "getModelMaxContextSize");
|
|
149
168
|
function createGlobMatcher(pattern) {
|
|
@@ -162,6 +181,7 @@ var imageModelMatchers = [
|
|
|
162
181
|
"gemini",
|
|
163
182
|
"qwen-vl",
|
|
164
183
|
"omni",
|
|
184
|
+
"gemma",
|
|
165
185
|
"qwen*-omni",
|
|
166
186
|
"qwen-omni",
|
|
167
187
|
"qwen*-vl",
|
|
@@ -176,7 +196,8 @@ var imageModelMatchers = [
|
|
|
176
196
|
"glm-*v",
|
|
177
197
|
"kimi-k2.5",
|
|
178
198
|
"step3",
|
|
179
|
-
"grok-4"
|
|
199
|
+
"grok-4",
|
|
200
|
+
"mimo-v2.5*"
|
|
180
201
|
].map((pattern) => createGlobMatcher(pattern));
|
|
181
202
|
function supportImageInput(modelName) {
|
|
182
203
|
const lowerModel = normalizeOpenAIModelName(modelName).toLowerCase();
|
|
@@ -197,11 +218,13 @@ var import_types = require("@langchain/core/utils/types");
|
|
|
197
218
|
function createUsageMetadata(data) {
|
|
198
219
|
const inputTokenDetails = {
|
|
199
220
|
...data.inputAudioTokens != null ? { audio: data.inputAudioTokens } : {},
|
|
221
|
+
...data.inputImageTokens != null ? { image: data.inputImageTokens } : {},
|
|
200
222
|
...data.cacheReadTokens != null ? { cache_read: data.cacheReadTokens } : {},
|
|
201
223
|
...data.cacheCreationTokens != null ? { cache_creation: data.cacheCreationTokens } : {}
|
|
202
224
|
};
|
|
203
225
|
const outputTokenDetails = {
|
|
204
226
|
...data.outputAudioTokens != null ? { audio: data.outputAudioTokens } : {},
|
|
227
|
+
...data.outputImageTokens != null ? { image: data.outputImageTokens } : {},
|
|
205
228
|
...data.reasoningTokens != null ? { reasoning: data.reasoningTokens } : {}
|
|
206
229
|
};
|
|
207
230
|
return {
|
|
@@ -225,6 +248,144 @@ function openAIUsageToUsageMetadata(usage) {
|
|
|
225
248
|
});
|
|
226
249
|
}
|
|
227
250
|
__name(openAIUsageToUsageMetadata, "openAIUsageToUsageMetadata");
|
|
251
|
+
function openAIResponseUsageToUsageMetadata(usage) {
|
|
252
|
+
return createUsageMetadata({
|
|
253
|
+
inputTokens: usage.input_tokens,
|
|
254
|
+
outputTokens: usage.output_tokens,
|
|
255
|
+
totalTokens: usage.total_tokens,
|
|
256
|
+
cacheReadTokens: usage.input_tokens_details?.cached_tokens,
|
|
257
|
+
reasoningTokens: usage.output_tokens_details?.reasoning_tokens
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
__name(openAIResponseUsageToUsageMetadata, "openAIResponseUsageToUsageMetadata");
|
|
261
|
+
async function langchainMessageToResponseInput(messages, plugin, model, supportImageInputType) {
|
|
262
|
+
const chatMessages = await langchainMessageToOpenAIMessage(
|
|
263
|
+
messages,
|
|
264
|
+
plugin,
|
|
265
|
+
model,
|
|
266
|
+
supportImageInputType
|
|
267
|
+
);
|
|
268
|
+
const result = [];
|
|
269
|
+
for (const msg of chatMessages) {
|
|
270
|
+
if (msg.role === "tool") {
|
|
271
|
+
result.push({
|
|
272
|
+
type: "function_call_output",
|
|
273
|
+
call_id: msg.tool_call_id,
|
|
274
|
+
output: responseInputContent(msg.content)
|
|
275
|
+
});
|
|
276
|
+
continue;
|
|
277
|
+
}
|
|
278
|
+
if (msg.role === "function") {
|
|
279
|
+
result.push({
|
|
280
|
+
type: "message",
|
|
281
|
+
role: "user",
|
|
282
|
+
content: responseInputContent(msg.content)
|
|
283
|
+
});
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
if (msg.content != null && msg.content !== "") {
|
|
287
|
+
result.push({
|
|
288
|
+
type: "message",
|
|
289
|
+
role: msg.role === "system" || msg.role === "assistant" || msg.role === "user" ? msg.role : "user",
|
|
290
|
+
content: responseInputContent(msg.content)
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
if (msg.role !== "assistant" || !Array.isArray(msg.tool_calls)) {
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
result.push(
|
|
297
|
+
...msg.tool_calls.map((toolCall) => ({
|
|
298
|
+
type: "function_call",
|
|
299
|
+
call_id: toolCall.id,
|
|
300
|
+
name: toolCall.function.name,
|
|
301
|
+
arguments: toolCall.function.arguments,
|
|
302
|
+
status: "completed"
|
|
303
|
+
}))
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
return result;
|
|
307
|
+
}
|
|
308
|
+
__name(langchainMessageToResponseInput, "langchainMessageToResponseInput");
|
|
309
|
+
function responseInputContent(content) {
|
|
310
|
+
if (typeof content === "string") return content;
|
|
311
|
+
if (!Array.isArray(content)) return "";
|
|
312
|
+
return content.map((part) => {
|
|
313
|
+
if (part.type === "text") {
|
|
314
|
+
const text = part.text;
|
|
315
|
+
return {
|
|
316
|
+
type: "input_text",
|
|
317
|
+
text
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
if (part.type === "image_url") {
|
|
321
|
+
const raw = part.image_url;
|
|
322
|
+
const imageUrl = typeof raw === "string" ? raw : raw.url;
|
|
323
|
+
const detail = typeof raw === "string" ? void 0 : raw.detail;
|
|
324
|
+
return {
|
|
325
|
+
type: "input_image",
|
|
326
|
+
image_url: imageUrl,
|
|
327
|
+
detail: detail ?? "auto"
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
if (part.type === "file_url") {
|
|
331
|
+
const raw = part["file_url"];
|
|
332
|
+
return {
|
|
333
|
+
type: "input_file",
|
|
334
|
+
file_url: typeof raw === "string" ? raw : raw.url,
|
|
335
|
+
filename: typeof raw === "string" ? void 0 : raw.filename
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
return void 0;
|
|
339
|
+
}).filter((part) => part != null);
|
|
340
|
+
}
|
|
341
|
+
__name(responseInputContent, "responseInputContent");
|
|
342
|
+
function formatToolsToResponseTools(tools, includeGoogleSearch, builtinTools = []) {
|
|
343
|
+
const result = (formatToolsToOpenAITools(tools, includeGoogleSearch) ?? []).map((tool) => {
|
|
344
|
+
if (tool.function.name === "googleSearch") {
|
|
345
|
+
return {
|
|
346
|
+
type: "web_search"
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
return {
|
|
350
|
+
type: "function",
|
|
351
|
+
name: tool.function.name,
|
|
352
|
+
description: tool.function.description,
|
|
353
|
+
parameters: tool.function.parameters
|
|
354
|
+
};
|
|
355
|
+
});
|
|
356
|
+
for (const tool of builtinTools) {
|
|
357
|
+
if (result.some((item) => item.type === tool.type)) continue;
|
|
358
|
+
result.push(tool);
|
|
359
|
+
}
|
|
360
|
+
return result.length ? result : void 0;
|
|
361
|
+
}
|
|
362
|
+
__name(formatToolsToResponseTools, "formatToolsToResponseTools");
|
|
363
|
+
function responseOutputText(response) {
|
|
364
|
+
if ((response.output_text?.length ?? 0) > 0) return response.output_text;
|
|
365
|
+
return (response.output ?? []).flatMap((item) => {
|
|
366
|
+
if (item.type !== "message") return [];
|
|
367
|
+
return (item.content ?? []).map(
|
|
368
|
+
(part) => {
|
|
369
|
+
if (part.type === "output_text") return part.text;
|
|
370
|
+
if (part.type === "refusal") return part.refusal;
|
|
371
|
+
return "";
|
|
372
|
+
}
|
|
373
|
+
);
|
|
374
|
+
}).join("");
|
|
375
|
+
}
|
|
376
|
+
__name(responseOutputText, "responseOutputText");
|
|
377
|
+
function responseOutputToolCalls(response) {
|
|
378
|
+
return (response.output ?? []).filter(
|
|
379
|
+
(item) => item.type === "function_call"
|
|
380
|
+
);
|
|
381
|
+
}
|
|
382
|
+
__name(responseOutputToolCalls, "responseOutputToolCalls");
|
|
383
|
+
function responseOutputImageItems(response) {
|
|
384
|
+
return (response.output ?? []).filter((item) => item.type === "image_generation_call" && item.result).map(
|
|
385
|
+
(item) => item
|
|
386
|
+
);
|
|
387
|
+
}
|
|
388
|
+
__name(responseOutputImageItems, "responseOutputImageItems");
|
|
228
389
|
async function langchainMessageToOpenAIMessage(messages, plugin, model, supportImageInputType, removeSystemMessage) {
|
|
229
390
|
const result = [];
|
|
230
391
|
const normalizedModel = model ? normalizeOpenAIModelName(model) : model;
|
|
@@ -712,6 +873,37 @@ async function buildChatCompletionParams(params, plugin, enableGoogleSearch, sup
|
|
|
712
873
|
return (0, import_object.deepAssign)({}, base, params.overrideRequestParams ?? {});
|
|
713
874
|
}
|
|
714
875
|
__name(buildChatCompletionParams, "buildChatCompletionParams");
|
|
876
|
+
async function buildResponseParams(params, plugin, opts = {}, supportImageInput2) {
|
|
877
|
+
const parsedModel = parseOpenAIModelNameWithReasoningEffort(params.model);
|
|
878
|
+
const normalizedModel = parsedModel.model;
|
|
879
|
+
const base = {
|
|
880
|
+
model: normalizedModel,
|
|
881
|
+
input: await langchainMessageToResponseInput(
|
|
882
|
+
params.input,
|
|
883
|
+
plugin,
|
|
884
|
+
normalizedModel,
|
|
885
|
+
supportImageInput2
|
|
886
|
+
),
|
|
887
|
+
tools: opts.googleSearch || (opts.builtinTools?.length ?? 0) > 0 || params.tools != null ? formatToolsToResponseTools(
|
|
888
|
+
params.tools ?? [],
|
|
889
|
+
opts.googleSearch ?? false,
|
|
890
|
+
opts.builtinTools
|
|
891
|
+
) : void 0,
|
|
892
|
+
max_output_tokens: normalizedModel.includes("vision") ? void 0 : params.maxTokens,
|
|
893
|
+
temperature: params.temperature === 0 ? void 0 : params.temperature,
|
|
894
|
+
top_p: params.topP,
|
|
895
|
+
prompt_cache_key: params.id,
|
|
896
|
+
reasoning: parsedModel.reasoningEffort == null || parsedModel.reasoningEffort === "none" ? void 0 : { effort: parsedModel.reasoningEffort },
|
|
897
|
+
stream: true,
|
|
898
|
+
stream_options: {
|
|
899
|
+
include_obfuscation: false
|
|
900
|
+
},
|
|
901
|
+
store: false,
|
|
902
|
+
parallel_tool_calls: true
|
|
903
|
+
};
|
|
904
|
+
return (0, import_object.deepAssign)({}, base, params.overrideRequestParams ?? {});
|
|
905
|
+
}
|
|
906
|
+
__name(buildResponseParams, "buildResponseParams");
|
|
715
907
|
async function* processStreamResponse(requestContext, iterator) {
|
|
716
908
|
let defaultRole = "assistant";
|
|
717
909
|
let errorCount = 0;
|
|
@@ -881,6 +1073,190 @@ async function processResponse(requestContext, response) {
|
|
|
881
1073
|
}
|
|
882
1074
|
}
|
|
883
1075
|
__name(processResponse, "processResponse");
|
|
1076
|
+
async function responseToChatGeneration(response, imageProvider) {
|
|
1077
|
+
if (response.error) {
|
|
1078
|
+
throw new import_error.ChatLunaError(
|
|
1079
|
+
import_error.ChatLunaErrorCode.API_REQUEST_FAILED,
|
|
1080
|
+
new Error(response.error.message ?? JSON.stringify(response.error))
|
|
1081
|
+
);
|
|
1082
|
+
}
|
|
1083
|
+
const text = responseOutputText(response);
|
|
1084
|
+
const toolCalls = responseOutputToolCalls(response);
|
|
1085
|
+
const images = imageProvider ? await Promise.all(
|
|
1086
|
+
responseOutputImageItems(response).map(
|
|
1087
|
+
(item) => imageProvider(item)
|
|
1088
|
+
)
|
|
1089
|
+
) : [];
|
|
1090
|
+
const usageMetadata = response.usage ? openAIResponseUsageToUsageMetadata(response.usage) : void 0;
|
|
1091
|
+
const message = new import_messages2.AIMessageChunk({
|
|
1092
|
+
content: images.length > 0 ? [
|
|
1093
|
+
...text.length > 0 ? [{ type: "text", text }] : [],
|
|
1094
|
+
...images.map((image) => ({
|
|
1095
|
+
type: "image_url",
|
|
1096
|
+
image_url: image
|
|
1097
|
+
}))
|
|
1098
|
+
] : text,
|
|
1099
|
+
tool_call_chunks: toolCalls.map((call, index) => ({
|
|
1100
|
+
name: call.name,
|
|
1101
|
+
args: call.arguments,
|
|
1102
|
+
id: call.call_id,
|
|
1103
|
+
index
|
|
1104
|
+
})),
|
|
1105
|
+
usage_metadata: usageMetadata,
|
|
1106
|
+
additional_kwargs: {
|
|
1107
|
+
conversation: response.conversation
|
|
1108
|
+
}
|
|
1109
|
+
});
|
|
1110
|
+
return new import_outputs.ChatGenerationChunk({
|
|
1111
|
+
generationInfo: usageMetadata == null ? void 0 : {
|
|
1112
|
+
usage_metadata: usageMetadata
|
|
1113
|
+
},
|
|
1114
|
+
message,
|
|
1115
|
+
text
|
|
1116
|
+
});
|
|
1117
|
+
}
|
|
1118
|
+
__name(responseToChatGeneration, "responseToChatGeneration");
|
|
1119
|
+
async function processResponseApiResponse(response, imageProvider) {
|
|
1120
|
+
if (response.status !== 200) {
|
|
1121
|
+
throw new import_error.ChatLunaError(
|
|
1122
|
+
import_error.ChatLunaErrorCode.API_REQUEST_FAILED,
|
|
1123
|
+
new Error(
|
|
1124
|
+
"Error when calling responses, Status: " + response.status + " " + response.statusText + ", Response: " + await response.text()
|
|
1125
|
+
)
|
|
1126
|
+
);
|
|
1127
|
+
}
|
|
1128
|
+
const responseText = await response.text();
|
|
1129
|
+
try {
|
|
1130
|
+
return await responseToChatGeneration(
|
|
1131
|
+
JSON.parse(responseText),
|
|
1132
|
+
imageProvider
|
|
1133
|
+
);
|
|
1134
|
+
} catch (e) {
|
|
1135
|
+
if (e instanceof import_error.ChatLunaError) throw e;
|
|
1136
|
+
throw new import_error.ChatLunaError(
|
|
1137
|
+
import_error.ChatLunaErrorCode.API_REQUEST_FAILED,
|
|
1138
|
+
new Error(
|
|
1139
|
+
"Error when calling responses, Error: " + e + ", Response: " + responseText
|
|
1140
|
+
)
|
|
1141
|
+
);
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
__name(processResponseApiResponse, "processResponseApiResponse");
|
|
1145
|
+
async function* processResponseApiStream(requestContext, iterator, imageProvider) {
|
|
1146
|
+
const args = /* @__PURE__ */ new Map();
|
|
1147
|
+
const calls = /* @__PURE__ */ new Map();
|
|
1148
|
+
let errorCount = 0;
|
|
1149
|
+
let sentConversation = false;
|
|
1150
|
+
for await (const event of iterator) {
|
|
1151
|
+
const chunk = event.data;
|
|
1152
|
+
if (chunk === "[DONE]") break;
|
|
1153
|
+
if (chunk === "" || chunk == null || chunk === "undefined") continue;
|
|
1154
|
+
try {
|
|
1155
|
+
const data = JSON.parse(chunk);
|
|
1156
|
+
if (data.type === "response.output_text.delta" && data.delta) {
|
|
1157
|
+
yield new import_outputs.ChatGenerationChunk({
|
|
1158
|
+
message: new import_messages2.AIMessageChunk(data.delta),
|
|
1159
|
+
text: data.delta
|
|
1160
|
+
});
|
|
1161
|
+
continue;
|
|
1162
|
+
}
|
|
1163
|
+
if (data.type === "response.output_item.added" && data.item?.type === "function_call") {
|
|
1164
|
+
const item = data.item;
|
|
1165
|
+
calls.set(data.output_index ?? calls.size, {
|
|
1166
|
+
name: item.name,
|
|
1167
|
+
callId: item.call_id,
|
|
1168
|
+
itemId: item.id
|
|
1169
|
+
});
|
|
1170
|
+
continue;
|
|
1171
|
+
}
|
|
1172
|
+
if (data.type === "response.function_call_arguments.delta") {
|
|
1173
|
+
const index = data.output_index ?? 0;
|
|
1174
|
+
args.set(index, (args.get(index) ?? "") + (data.delta ?? ""));
|
|
1175
|
+
continue;
|
|
1176
|
+
}
|
|
1177
|
+
if (data.type === "response.function_call_arguments.done") {
|
|
1178
|
+
const index = data.output_index ?? 0;
|
|
1179
|
+
const call = calls.get(index);
|
|
1180
|
+
yield new import_outputs.ChatGenerationChunk({
|
|
1181
|
+
message: new import_messages2.AIMessageChunk({
|
|
1182
|
+
content: "",
|
|
1183
|
+
tool_call_chunks: [
|
|
1184
|
+
{
|
|
1185
|
+
name: data.name ?? call?.name,
|
|
1186
|
+
args: data.arguments ?? args.get(index) ?? "",
|
|
1187
|
+
id: call?.callId ?? data.item_id,
|
|
1188
|
+
index
|
|
1189
|
+
}
|
|
1190
|
+
]
|
|
1191
|
+
}),
|
|
1192
|
+
text: ""
|
|
1193
|
+
});
|
|
1194
|
+
continue;
|
|
1195
|
+
}
|
|
1196
|
+
if (data.type === "response.completed" && data.response) {
|
|
1197
|
+
const usageMetadata = data.response.usage ? openAIResponseUsageToUsageMetadata(data.response.usage) : void 0;
|
|
1198
|
+
const images = imageProvider ? await Promise.all(
|
|
1199
|
+
responseOutputImageItems(data.response).map(
|
|
1200
|
+
(item) => imageProvider(item)
|
|
1201
|
+
)
|
|
1202
|
+
) : [];
|
|
1203
|
+
if (images.length > 0) {
|
|
1204
|
+
yield new import_outputs.ChatGenerationChunk({
|
|
1205
|
+
message: new import_messages2.AIMessageChunk({
|
|
1206
|
+
content: images.map((image) => ({
|
|
1207
|
+
type: "image_url",
|
|
1208
|
+
image_url: image
|
|
1209
|
+
}))
|
|
1210
|
+
}),
|
|
1211
|
+
text: ""
|
|
1212
|
+
});
|
|
1213
|
+
}
|
|
1214
|
+
if (!sentConversation) {
|
|
1215
|
+
sentConversation = true;
|
|
1216
|
+
yield new import_outputs.ChatGenerationChunk({
|
|
1217
|
+
message: new import_messages2.AIMessageChunk({
|
|
1218
|
+
content: "",
|
|
1219
|
+
additional_kwargs: {
|
|
1220
|
+
conversation: data.response.conversation
|
|
1221
|
+
}
|
|
1222
|
+
}),
|
|
1223
|
+
text: ""
|
|
1224
|
+
});
|
|
1225
|
+
}
|
|
1226
|
+
if (usageMetadata) {
|
|
1227
|
+
yield new import_outputs.ChatGenerationChunk({
|
|
1228
|
+
generationInfo: {
|
|
1229
|
+
usage_metadata: usageMetadata
|
|
1230
|
+
},
|
|
1231
|
+
message: new import_messages2.AIMessageChunk({
|
|
1232
|
+
content: "",
|
|
1233
|
+
usage_metadata: usageMetadata
|
|
1234
|
+
}),
|
|
1235
|
+
text: ""
|
|
1236
|
+
});
|
|
1237
|
+
}
|
|
1238
|
+
continue;
|
|
1239
|
+
}
|
|
1240
|
+
if (data.type === "response.failed" || data.type === "response.incomplete" || data.type === "response.error") {
|
|
1241
|
+
throw new import_error.ChatLunaError(
|
|
1242
|
+
import_error.ChatLunaErrorCode.API_REQUEST_FAILED,
|
|
1243
|
+
new Error(chunk)
|
|
1244
|
+
);
|
|
1245
|
+
}
|
|
1246
|
+
} catch (e) {
|
|
1247
|
+
if (e instanceof import_error.ChatLunaError) throw e;
|
|
1248
|
+
if (errorCount > 5) {
|
|
1249
|
+
requestContext.modelRequester.logger.error(
|
|
1250
|
+
"error with responses chunk",
|
|
1251
|
+
chunk
|
|
1252
|
+
);
|
|
1253
|
+
throw new import_error.ChatLunaError(import_error.ChatLunaErrorCode.API_REQUEST_FAILED, e);
|
|
1254
|
+
}
|
|
1255
|
+
errorCount++;
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1259
|
+
__name(processResponseApiStream, "processResponseApiStream");
|
|
884
1260
|
async function* completionStream(requestContext, params, completionUrl = "chat/completions", enableGoogleSearch, supportImageInput2) {
|
|
885
1261
|
const { modelRequester } = requestContext;
|
|
886
1262
|
const chatCompletionParams = await buildChatCompletionParams(
|
|
@@ -949,6 +1325,64 @@ async function completion(requestContext, params, completionUrl = "chat/completi
|
|
|
949
1325
|
}
|
|
950
1326
|
}
|
|
951
1327
|
__name(completion, "completion");
|
|
1328
|
+
async function* responseApiCompletionStream(requestContext, params, opts = {}, supportImageInput2, imageProvider) {
|
|
1329
|
+
const { modelRequester } = requestContext;
|
|
1330
|
+
const request = await buildResponseParams(
|
|
1331
|
+
params,
|
|
1332
|
+
requestContext.plugin,
|
|
1333
|
+
opts,
|
|
1334
|
+
supportImageInput2 ?? true
|
|
1335
|
+
);
|
|
1336
|
+
try {
|
|
1337
|
+
const response = await modelRequester.post("responses", request, {
|
|
1338
|
+
signal: params.signal
|
|
1339
|
+
});
|
|
1340
|
+
yield* processResponseApiStream(
|
|
1341
|
+
requestContext,
|
|
1342
|
+
(0, import_sse.sseIterable)(response),
|
|
1343
|
+
imageProvider
|
|
1344
|
+
);
|
|
1345
|
+
} catch (e) {
|
|
1346
|
+
if (requestContext.ctx.chatluna.currentConfig.isLog) {
|
|
1347
|
+
await (0, import_logger.trackLogToLocal)(
|
|
1348
|
+
"Request",
|
|
1349
|
+
JSON.stringify(request),
|
|
1350
|
+
requestContext.ctx.logger("")
|
|
1351
|
+
);
|
|
1352
|
+
}
|
|
1353
|
+
if (e instanceof import_error.ChatLunaError) throw e;
|
|
1354
|
+
throw new import_error.ChatLunaError(import_error.ChatLunaErrorCode.API_REQUEST_FAILED, e);
|
|
1355
|
+
}
|
|
1356
|
+
}
|
|
1357
|
+
__name(responseApiCompletionStream, "responseApiCompletionStream");
|
|
1358
|
+
async function responseApiCompletion(requestContext, params, opts = {}, supportImageInput2, imageProvider) {
|
|
1359
|
+
const { modelRequester } = requestContext;
|
|
1360
|
+
const request = await buildResponseParams(
|
|
1361
|
+
params,
|
|
1362
|
+
requestContext.plugin,
|
|
1363
|
+
opts,
|
|
1364
|
+
supportImageInput2 ?? true
|
|
1365
|
+
);
|
|
1366
|
+
delete request.stream;
|
|
1367
|
+
delete request.stream_options;
|
|
1368
|
+
try {
|
|
1369
|
+
const response = await modelRequester.post("responses", request, {
|
|
1370
|
+
signal: params.signal
|
|
1371
|
+
});
|
|
1372
|
+
return await processResponseApiResponse(response, imageProvider);
|
|
1373
|
+
} catch (e) {
|
|
1374
|
+
if (requestContext.ctx.chatluna.currentConfig.isLog) {
|
|
1375
|
+
await (0, import_logger.trackLogToLocal)(
|
|
1376
|
+
"Request",
|
|
1377
|
+
JSON.stringify(request),
|
|
1378
|
+
requestContext.ctx.logger("")
|
|
1379
|
+
);
|
|
1380
|
+
}
|
|
1381
|
+
if (e instanceof import_error.ChatLunaError) throw e;
|
|
1382
|
+
throw new import_error.ChatLunaError(import_error.ChatLunaErrorCode.API_REQUEST_FAILED, e);
|
|
1383
|
+
}
|
|
1384
|
+
}
|
|
1385
|
+
__name(responseApiCompletion, "responseApiCompletion");
|
|
952
1386
|
async function createEmbeddings(requestContext, params, embeddingUrl = "embeddings") {
|
|
953
1387
|
const { modelRequester } = requestContext;
|
|
954
1388
|
let data;
|
|
@@ -1026,6 +1460,7 @@ __name(createRequestContext, "createRequestContext");
|
|
|
1026
1460
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1027
1461
|
0 && (module.exports = {
|
|
1028
1462
|
buildChatCompletionParams,
|
|
1463
|
+
buildResponseParams,
|
|
1029
1464
|
completion,
|
|
1030
1465
|
completionStream,
|
|
1031
1466
|
convertDeltaToMessageChunk,
|
|
@@ -1038,20 +1473,33 @@ __name(createRequestContext, "createRequestContext");
|
|
|
1038
1473
|
fetchImageUrl,
|
|
1039
1474
|
formatToolToOpenAITool,
|
|
1040
1475
|
formatToolsToOpenAITools,
|
|
1476
|
+
formatToolsToResponseTools,
|
|
1041
1477
|
getModelMaxContextSize,
|
|
1042
1478
|
getModels,
|
|
1043
1479
|
isEmbeddingModel,
|
|
1480
|
+
isImageGenerationModel,
|
|
1044
1481
|
isNonLLMModel,
|
|
1045
1482
|
langchainMessageToOpenAIMessage,
|
|
1483
|
+
langchainMessageToResponseInput,
|
|
1046
1484
|
messageTypeToOpenAIRole,
|
|
1047
1485
|
normalizeOpenAIModelName,
|
|
1486
|
+
openAIResponseUsageToUsageMetadata,
|
|
1048
1487
|
openAIUsageToUsageMetadata,
|
|
1049
1488
|
parseOpenAIModelNameWithReasoningEffort,
|
|
1050
1489
|
processInterleavedThinkMessages,
|
|
1051
1490
|
processResponse,
|
|
1491
|
+
processResponseApiResponse,
|
|
1492
|
+
processResponseApiStream,
|
|
1052
1493
|
processStreamResponse,
|
|
1053
1494
|
reasoningEffortModelSuffixes,
|
|
1054
1495
|
removeAdditionalProperties,
|
|
1496
|
+
responseApiCompletion,
|
|
1497
|
+
responseApiCompletionStream,
|
|
1498
|
+
responseInputContent,
|
|
1499
|
+
responseOutputImageItems,
|
|
1500
|
+
responseOutputText,
|
|
1501
|
+
responseOutputToolCalls,
|
|
1502
|
+
responseToChatGeneration,
|
|
1055
1503
|
supportImageInput,
|
|
1056
1504
|
transformSystemMessages
|
|
1057
1505
|
});
|