@chatluna/v1-shared-adapter 1.0.28 → 1.0.29
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 +96 -38
- package/lib/index.mjs +94 -37
- package/lib/requester.d.ts +0 -8
- package/lib/types.d.ts +19 -5
- package/lib/utils.d.ts +13 -2
- package/package.json +2 -2
package/lib/index.cjs
CHANGED
|
@@ -27,6 +27,7 @@ __export(index_exports, {
|
|
|
27
27
|
convertMessageToMessageChunk: () => convertMessageToMessageChunk,
|
|
28
28
|
createEmbeddings: () => createEmbeddings,
|
|
29
29
|
createRequestContext: () => createRequestContext,
|
|
30
|
+
createUsageMetadata: () => createUsageMetadata,
|
|
30
31
|
expandReasoningEffortModelVariants: () => expandReasoningEffortModelVariants,
|
|
31
32
|
fetchFileLikeUrl: () => fetchFileLikeUrl,
|
|
32
33
|
fetchImageUrl: () => fetchImageUrl,
|
|
@@ -39,9 +40,9 @@ __export(index_exports, {
|
|
|
39
40
|
langchainMessageToOpenAIMessage: () => langchainMessageToOpenAIMessage,
|
|
40
41
|
messageTypeToOpenAIRole: () => messageTypeToOpenAIRole,
|
|
41
42
|
normalizeOpenAIModelName: () => normalizeOpenAIModelName,
|
|
43
|
+
openAIUsageToUsageMetadata: () => openAIUsageToUsageMetadata,
|
|
42
44
|
parseOpenAIModelNameWithReasoningEffort: () => parseOpenAIModelNameWithReasoningEffort,
|
|
43
45
|
processDeepSeekThinkMessages: () => processDeepSeekThinkMessages,
|
|
44
|
-
processReasoningContent: () => processReasoningContent,
|
|
45
46
|
processResponse: () => processResponse,
|
|
46
47
|
processStreamResponse: () => processStreamResponse,
|
|
47
48
|
reasoningEffortModelSuffixes: () => reasoningEffortModelSuffixes,
|
|
@@ -164,6 +165,8 @@ var imageModelMatchers = [
|
|
|
164
165
|
"qwen*-omni",
|
|
165
166
|
"qwen-omni",
|
|
166
167
|
"qwen*-vl",
|
|
168
|
+
"qwen-3.5",
|
|
169
|
+
"qwen3.5",
|
|
167
170
|
"qvq",
|
|
168
171
|
"o1",
|
|
169
172
|
"o3",
|
|
@@ -191,6 +194,37 @@ var import_messages = require("@langchain/core/messages");
|
|
|
191
194
|
var import_zod_to_json_schema = require("zod-to-json-schema");
|
|
192
195
|
var import_string = require("koishi-plugin-chatluna/utils/string");
|
|
193
196
|
var import_types = require("@langchain/core/utils/types");
|
|
197
|
+
function createUsageMetadata(data) {
|
|
198
|
+
const inputTokenDetails = {
|
|
199
|
+
...data.inputAudioTokens != null ? { audio: data.inputAudioTokens } : {},
|
|
200
|
+
...data.cacheReadTokens != null ? { cache_read: data.cacheReadTokens } : {},
|
|
201
|
+
...data.cacheCreationTokens != null ? { cache_creation: data.cacheCreationTokens } : {}
|
|
202
|
+
};
|
|
203
|
+
const outputTokenDetails = {
|
|
204
|
+
...data.outputAudioTokens != null ? { audio: data.outputAudioTokens } : {},
|
|
205
|
+
...data.reasoningTokens != null ? { reasoning: data.reasoningTokens } : {}
|
|
206
|
+
};
|
|
207
|
+
return {
|
|
208
|
+
input_tokens: data.inputTokens,
|
|
209
|
+
output_tokens: data.outputTokens,
|
|
210
|
+
total_tokens: data.totalTokens,
|
|
211
|
+
...Object.keys(inputTokenDetails).length > 0 ? { input_token_details: inputTokenDetails } : {},
|
|
212
|
+
...Object.keys(outputTokenDetails).length > 0 ? { output_token_details: outputTokenDetails } : {}
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
__name(createUsageMetadata, "createUsageMetadata");
|
|
216
|
+
function openAIUsageToUsageMetadata(usage) {
|
|
217
|
+
return createUsageMetadata({
|
|
218
|
+
inputTokens: usage.prompt_tokens,
|
|
219
|
+
outputTokens: usage.completion_tokens,
|
|
220
|
+
totalTokens: usage.total_tokens,
|
|
221
|
+
inputAudioTokens: usage.prompt_tokens_details?.audio_tokens,
|
|
222
|
+
outputAudioTokens: usage.completion_tokens_details?.audio_tokens,
|
|
223
|
+
cacheReadTokens: usage.prompt_tokens_details?.cached_tokens,
|
|
224
|
+
reasoningTokens: usage.completion_tokens_details?.reasoning_tokens
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
__name(openAIUsageToUsageMetadata, "openAIUsageToUsageMetadata");
|
|
194
228
|
async function langchainMessageToOpenAIMessage(messages, plugin, model, supportImageInputType, removeSystemMessage) {
|
|
195
229
|
const result = [];
|
|
196
230
|
const normalizedModel = model ? normalizeOpenAIModelName(model) : model;
|
|
@@ -537,8 +571,12 @@ function convertMessageToMessageChunk(message) {
|
|
|
537
571
|
const toolCallChunks = [];
|
|
538
572
|
if (Array.isArray(message.tool_calls)) {
|
|
539
573
|
for (const rawToolCall of message.tool_calls) {
|
|
574
|
+
let name = rawToolCall.function?.name;
|
|
575
|
+
if (name != null && name.length < 1) {
|
|
576
|
+
name = void 0;
|
|
577
|
+
}
|
|
540
578
|
toolCallChunks.push({
|
|
541
|
-
name
|
|
579
|
+
name,
|
|
542
580
|
args: rawToolCall.function?.arguments,
|
|
543
581
|
id: rawToolCall.id
|
|
544
582
|
});
|
|
@@ -577,10 +615,6 @@ function convertDeltaToMessageChunk(delta, defaultRole) {
|
|
|
577
615
|
additionalKwargs = {
|
|
578
616
|
function_call: delta.function_call
|
|
579
617
|
};
|
|
580
|
-
} else if (delta.tool_calls) {
|
|
581
|
-
additionalKwargs = {
|
|
582
|
-
tool_calls: delta.tool_calls
|
|
583
|
-
};
|
|
584
618
|
} else {
|
|
585
619
|
additionalKwargs = {};
|
|
586
620
|
}
|
|
@@ -593,12 +627,16 @@ function convertDeltaToMessageChunk(delta, defaultRole) {
|
|
|
593
627
|
const toolCallChunks = [];
|
|
594
628
|
if (Array.isArray(delta.tool_calls)) {
|
|
595
629
|
for (const rawToolCall of delta.tool_calls) {
|
|
596
|
-
|
|
630
|
+
const toolCall = {
|
|
597
631
|
name: rawToolCall.function?.name,
|
|
598
632
|
args: rawToolCall.function?.arguments,
|
|
599
633
|
id: rawToolCall.id,
|
|
600
634
|
index: rawToolCall.index
|
|
601
|
-
}
|
|
635
|
+
};
|
|
636
|
+
if (toolCall.name != null && toolCall.name.length < 1) {
|
|
637
|
+
delete toolCall.name;
|
|
638
|
+
}
|
|
639
|
+
toolCallChunks.push(toolCall);
|
|
602
640
|
}
|
|
603
641
|
}
|
|
604
642
|
return new import_messages.AIMessageChunk({
|
|
@@ -678,25 +716,15 @@ async function buildChatCompletionParams(params, plugin, enableGoogleSearch, sup
|
|
|
678
716
|
return (0, import_object.deepAssign)({}, base, params.overrideRequestParams ?? {});
|
|
679
717
|
}
|
|
680
718
|
__name(buildChatCompletionParams, "buildChatCompletionParams");
|
|
681
|
-
function processReasoningContent(delta, reasoningState) {
|
|
682
|
-
if (delta.reasoning_content) {
|
|
683
|
-
reasoningState.content += delta.reasoning_content;
|
|
684
|
-
if (reasoningState.time === 0) {
|
|
685
|
-
reasoningState.time = Date.now();
|
|
686
|
-
}
|
|
687
|
-
}
|
|
688
|
-
if ((delta.reasoning_content == null || delta.reasoning_content === "") && delta.content && delta.content.length > 0 && reasoningState.time > 0 && !reasoningState.isSet) {
|
|
689
|
-
const reasoningTime = Date.now() - reasoningState.time;
|
|
690
|
-
reasoningState.time = reasoningTime;
|
|
691
|
-
reasoningState.isSet = true;
|
|
692
|
-
return reasoningTime;
|
|
693
|
-
}
|
|
694
|
-
}
|
|
695
|
-
__name(processReasoningContent, "processReasoningContent");
|
|
696
719
|
async function* processStreamResponse(requestContext, iterator) {
|
|
697
720
|
let defaultRole = "assistant";
|
|
698
721
|
let errorCount = 0;
|
|
699
|
-
const reasoningState = {
|
|
722
|
+
const reasoningState = {
|
|
723
|
+
content: "",
|
|
724
|
+
startedAt: 0,
|
|
725
|
+
duration: 0,
|
|
726
|
+
done: false
|
|
727
|
+
};
|
|
700
728
|
for await (const event of iterator) {
|
|
701
729
|
const chunk = event.data;
|
|
702
730
|
if (chunk === "[DONE]") break;
|
|
@@ -711,16 +739,14 @@ async function* processStreamResponse(requestContext, iterator) {
|
|
|
711
739
|
}
|
|
712
740
|
const choice = data.choices?.[0];
|
|
713
741
|
if (data.usage) {
|
|
742
|
+
const usageMetadata = openAIUsageToUsageMetadata(data.usage);
|
|
714
743
|
yield new import_outputs.ChatGenerationChunk({
|
|
744
|
+
generationInfo: {
|
|
745
|
+
usage_metadata: usageMetadata
|
|
746
|
+
},
|
|
715
747
|
message: new import_messages2.AIMessageChunk({
|
|
716
748
|
content: "",
|
|
717
|
-
|
|
718
|
-
tokenUsage: {
|
|
719
|
-
promptTokens: data.usage.prompt_tokens,
|
|
720
|
-
completionTokens: data.usage.completion_tokens,
|
|
721
|
-
totalTokens: data.usage.total_tokens
|
|
722
|
-
}
|
|
723
|
-
}
|
|
749
|
+
usage_metadata: usageMetadata
|
|
724
750
|
}),
|
|
725
751
|
text: ""
|
|
726
752
|
});
|
|
@@ -728,9 +754,16 @@ async function* processStreamResponse(requestContext, iterator) {
|
|
|
728
754
|
if (!choice) continue;
|
|
729
755
|
const { delta } = choice;
|
|
730
756
|
const messageChunk = convertDeltaToMessageChunk(delta, defaultRole);
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
757
|
+
if (delta.reasoning_content) {
|
|
758
|
+
reasoningState.content += delta.reasoning_content;
|
|
759
|
+
if (reasoningState.startedAt === 0) {
|
|
760
|
+
reasoningState.startedAt = Date.now();
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
if (!reasoningState.done && reasoningState.startedAt > 0 && ((delta.content?.length ?? 0) > 0 || (delta.tool_calls?.length ?? 0) > 0 || delta.function_call != null)) {
|
|
764
|
+
reasoningState.duration = Date.now() - reasoningState.startedAt;
|
|
765
|
+
reasoningState.done = true;
|
|
766
|
+
messageChunk.additional_kwargs.reasoning_time = reasoningState.duration;
|
|
734
767
|
}
|
|
735
768
|
defaultRole = (delta.role?.length ?? 0) > 0 ? delta.role : defaultRole;
|
|
736
769
|
yield new import_outputs.ChatGenerationChunk({
|
|
@@ -738,6 +771,13 @@ async function* processStreamResponse(requestContext, iterator) {
|
|
|
738
771
|
text: messageChunk.content
|
|
739
772
|
});
|
|
740
773
|
} catch (e) {
|
|
774
|
+
if (chunk.includes("tool_calls") || chunk.includes("function_call") || chunk.includes("tool_call_id")) {
|
|
775
|
+
requestContext.modelRequester.logger.error(
|
|
776
|
+
"error with chunk",
|
|
777
|
+
chunk
|
|
778
|
+
);
|
|
779
|
+
throw new import_error.ChatLunaError(import_error.ChatLunaErrorCode.API_REQUEST_FAILED, e);
|
|
780
|
+
}
|
|
741
781
|
if (errorCount > 5) {
|
|
742
782
|
requestContext.modelRequester.logger.error(
|
|
743
783
|
"error with chunk",
|
|
@@ -749,8 +789,21 @@ async function* processStreamResponse(requestContext, iterator) {
|
|
|
749
789
|
}
|
|
750
790
|
}
|
|
751
791
|
if (reasoningState.content.length > 0) {
|
|
792
|
+
if (!reasoningState.done && reasoningState.startedAt > 0) {
|
|
793
|
+
reasoningState.duration = Date.now() - reasoningState.startedAt;
|
|
794
|
+
reasoningState.done = true;
|
|
795
|
+
yield new import_outputs.ChatGenerationChunk({
|
|
796
|
+
message: new import_messages2.AIMessageChunk({
|
|
797
|
+
content: "",
|
|
798
|
+
additional_kwargs: {
|
|
799
|
+
reasoning_time: reasoningState.duration
|
|
800
|
+
}
|
|
801
|
+
}),
|
|
802
|
+
text: ""
|
|
803
|
+
});
|
|
804
|
+
}
|
|
752
805
|
requestContext.modelRequester.logger.debug(
|
|
753
|
-
`reasoning content: ${reasoningState.content}. Use time: ${reasoningState.
|
|
806
|
+
`reasoning content: ${reasoningState.content}. Use time: ${reasoningState.duration / 1e3}s`
|
|
754
807
|
);
|
|
755
808
|
}
|
|
756
809
|
}
|
|
@@ -785,11 +838,15 @@ async function processResponse(requestContext, response) {
|
|
|
785
838
|
);
|
|
786
839
|
}
|
|
787
840
|
const messageChunk = convertMessageToMessageChunk(choice.message);
|
|
841
|
+
const usageMetadata = data.usage ? openAIUsageToUsageMetadata(data.usage) : void 0;
|
|
842
|
+
if (messageChunk instanceof import_messages2.AIMessageChunk) {
|
|
843
|
+
messageChunk.usage_metadata = usageMetadata;
|
|
844
|
+
}
|
|
788
845
|
return new import_outputs.ChatGenerationChunk({
|
|
789
846
|
message: messageChunk,
|
|
790
847
|
text: (0, import_string2.getMessageContent)(messageChunk.content),
|
|
791
|
-
generationInfo: {
|
|
792
|
-
|
|
848
|
+
generationInfo: usageMetadata == null ? void 0 : {
|
|
849
|
+
usage_metadata: usageMetadata
|
|
793
850
|
}
|
|
794
851
|
});
|
|
795
852
|
} catch (e) {
|
|
@@ -957,6 +1014,7 @@ __name(createRequestContext, "createRequestContext");
|
|
|
957
1014
|
convertMessageToMessageChunk,
|
|
958
1015
|
createEmbeddings,
|
|
959
1016
|
createRequestContext,
|
|
1017
|
+
createUsageMetadata,
|
|
960
1018
|
expandReasoningEffortModelVariants,
|
|
961
1019
|
fetchFileLikeUrl,
|
|
962
1020
|
fetchImageUrl,
|
|
@@ -969,9 +1027,9 @@ __name(createRequestContext, "createRequestContext");
|
|
|
969
1027
|
langchainMessageToOpenAIMessage,
|
|
970
1028
|
messageTypeToOpenAIRole,
|
|
971
1029
|
normalizeOpenAIModelName,
|
|
1030
|
+
openAIUsageToUsageMetadata,
|
|
972
1031
|
parseOpenAIModelNameWithReasoningEffort,
|
|
973
1032
|
processDeepSeekThinkMessages,
|
|
974
|
-
processReasoningContent,
|
|
975
1033
|
processResponse,
|
|
976
1034
|
processStreamResponse,
|
|
977
1035
|
reasoningEffortModelSuffixes,
|
package/lib/index.mjs
CHANGED
|
@@ -114,6 +114,8 @@ var imageModelMatchers = [
|
|
|
114
114
|
"qwen*-omni",
|
|
115
115
|
"qwen-omni",
|
|
116
116
|
"qwen*-vl",
|
|
117
|
+
"qwen-3.5",
|
|
118
|
+
"qwen3.5",
|
|
117
119
|
"qvq",
|
|
118
120
|
"o1",
|
|
119
121
|
"o3",
|
|
@@ -155,6 +157,37 @@ import {
|
|
|
155
157
|
isMessageContentImageUrl
|
|
156
158
|
} from "koishi-plugin-chatluna/utils/string";
|
|
157
159
|
import { isZodSchemaV3 } from "@langchain/core/utils/types";
|
|
160
|
+
function createUsageMetadata(data) {
|
|
161
|
+
const inputTokenDetails = {
|
|
162
|
+
...data.inputAudioTokens != null ? { audio: data.inputAudioTokens } : {},
|
|
163
|
+
...data.cacheReadTokens != null ? { cache_read: data.cacheReadTokens } : {},
|
|
164
|
+
...data.cacheCreationTokens != null ? { cache_creation: data.cacheCreationTokens } : {}
|
|
165
|
+
};
|
|
166
|
+
const outputTokenDetails = {
|
|
167
|
+
...data.outputAudioTokens != null ? { audio: data.outputAudioTokens } : {},
|
|
168
|
+
...data.reasoningTokens != null ? { reasoning: data.reasoningTokens } : {}
|
|
169
|
+
};
|
|
170
|
+
return {
|
|
171
|
+
input_tokens: data.inputTokens,
|
|
172
|
+
output_tokens: data.outputTokens,
|
|
173
|
+
total_tokens: data.totalTokens,
|
|
174
|
+
...Object.keys(inputTokenDetails).length > 0 ? { input_token_details: inputTokenDetails } : {},
|
|
175
|
+
...Object.keys(outputTokenDetails).length > 0 ? { output_token_details: outputTokenDetails } : {}
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
__name(createUsageMetadata, "createUsageMetadata");
|
|
179
|
+
function openAIUsageToUsageMetadata(usage) {
|
|
180
|
+
return createUsageMetadata({
|
|
181
|
+
inputTokens: usage.prompt_tokens,
|
|
182
|
+
outputTokens: usage.completion_tokens,
|
|
183
|
+
totalTokens: usage.total_tokens,
|
|
184
|
+
inputAudioTokens: usage.prompt_tokens_details?.audio_tokens,
|
|
185
|
+
outputAudioTokens: usage.completion_tokens_details?.audio_tokens,
|
|
186
|
+
cacheReadTokens: usage.prompt_tokens_details?.cached_tokens,
|
|
187
|
+
reasoningTokens: usage.completion_tokens_details?.reasoning_tokens
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
__name(openAIUsageToUsageMetadata, "openAIUsageToUsageMetadata");
|
|
158
191
|
async function langchainMessageToOpenAIMessage(messages, plugin, model, supportImageInputType, removeSystemMessage) {
|
|
159
192
|
const result = [];
|
|
160
193
|
const normalizedModel = model ? normalizeOpenAIModelName(model) : model;
|
|
@@ -501,8 +534,12 @@ function convertMessageToMessageChunk(message) {
|
|
|
501
534
|
const toolCallChunks = [];
|
|
502
535
|
if (Array.isArray(message.tool_calls)) {
|
|
503
536
|
for (const rawToolCall of message.tool_calls) {
|
|
537
|
+
let name = rawToolCall.function?.name;
|
|
538
|
+
if (name != null && name.length < 1) {
|
|
539
|
+
name = void 0;
|
|
540
|
+
}
|
|
504
541
|
toolCallChunks.push({
|
|
505
|
-
name
|
|
542
|
+
name,
|
|
506
543
|
args: rawToolCall.function?.arguments,
|
|
507
544
|
id: rawToolCall.id
|
|
508
545
|
});
|
|
@@ -541,10 +578,6 @@ function convertDeltaToMessageChunk(delta, defaultRole) {
|
|
|
541
578
|
additionalKwargs = {
|
|
542
579
|
function_call: delta.function_call
|
|
543
580
|
};
|
|
544
|
-
} else if (delta.tool_calls) {
|
|
545
|
-
additionalKwargs = {
|
|
546
|
-
tool_calls: delta.tool_calls
|
|
547
|
-
};
|
|
548
581
|
} else {
|
|
549
582
|
additionalKwargs = {};
|
|
550
583
|
}
|
|
@@ -557,12 +590,16 @@ function convertDeltaToMessageChunk(delta, defaultRole) {
|
|
|
557
590
|
const toolCallChunks = [];
|
|
558
591
|
if (Array.isArray(delta.tool_calls)) {
|
|
559
592
|
for (const rawToolCall of delta.tool_calls) {
|
|
560
|
-
|
|
593
|
+
const toolCall = {
|
|
561
594
|
name: rawToolCall.function?.name,
|
|
562
595
|
args: rawToolCall.function?.arguments,
|
|
563
596
|
id: rawToolCall.id,
|
|
564
597
|
index: rawToolCall.index
|
|
565
|
-
}
|
|
598
|
+
};
|
|
599
|
+
if (toolCall.name != null && toolCall.name.length < 1) {
|
|
600
|
+
delete toolCall.name;
|
|
601
|
+
}
|
|
602
|
+
toolCallChunks.push(toolCall);
|
|
566
603
|
}
|
|
567
604
|
}
|
|
568
605
|
return new AIMessageChunk({
|
|
@@ -642,25 +679,15 @@ async function buildChatCompletionParams(params, plugin, enableGoogleSearch, sup
|
|
|
642
679
|
return deepAssign({}, base, params.overrideRequestParams ?? {});
|
|
643
680
|
}
|
|
644
681
|
__name(buildChatCompletionParams, "buildChatCompletionParams");
|
|
645
|
-
function processReasoningContent(delta, reasoningState) {
|
|
646
|
-
if (delta.reasoning_content) {
|
|
647
|
-
reasoningState.content += delta.reasoning_content;
|
|
648
|
-
if (reasoningState.time === 0) {
|
|
649
|
-
reasoningState.time = Date.now();
|
|
650
|
-
}
|
|
651
|
-
}
|
|
652
|
-
if ((delta.reasoning_content == null || delta.reasoning_content === "") && delta.content && delta.content.length > 0 && reasoningState.time > 0 && !reasoningState.isSet) {
|
|
653
|
-
const reasoningTime = Date.now() - reasoningState.time;
|
|
654
|
-
reasoningState.time = reasoningTime;
|
|
655
|
-
reasoningState.isSet = true;
|
|
656
|
-
return reasoningTime;
|
|
657
|
-
}
|
|
658
|
-
}
|
|
659
|
-
__name(processReasoningContent, "processReasoningContent");
|
|
660
682
|
async function* processStreamResponse(requestContext, iterator) {
|
|
661
683
|
let defaultRole = "assistant";
|
|
662
684
|
let errorCount = 0;
|
|
663
|
-
const reasoningState = {
|
|
685
|
+
const reasoningState = {
|
|
686
|
+
content: "",
|
|
687
|
+
startedAt: 0,
|
|
688
|
+
duration: 0,
|
|
689
|
+
done: false
|
|
690
|
+
};
|
|
664
691
|
for await (const event of iterator) {
|
|
665
692
|
const chunk = event.data;
|
|
666
693
|
if (chunk === "[DONE]") break;
|
|
@@ -675,16 +702,14 @@ async function* processStreamResponse(requestContext, iterator) {
|
|
|
675
702
|
}
|
|
676
703
|
const choice = data.choices?.[0];
|
|
677
704
|
if (data.usage) {
|
|
705
|
+
const usageMetadata = openAIUsageToUsageMetadata(data.usage);
|
|
678
706
|
yield new ChatGenerationChunk({
|
|
707
|
+
generationInfo: {
|
|
708
|
+
usage_metadata: usageMetadata
|
|
709
|
+
},
|
|
679
710
|
message: new AIMessageChunk2({
|
|
680
711
|
content: "",
|
|
681
|
-
|
|
682
|
-
tokenUsage: {
|
|
683
|
-
promptTokens: data.usage.prompt_tokens,
|
|
684
|
-
completionTokens: data.usage.completion_tokens,
|
|
685
|
-
totalTokens: data.usage.total_tokens
|
|
686
|
-
}
|
|
687
|
-
}
|
|
712
|
+
usage_metadata: usageMetadata
|
|
688
713
|
}),
|
|
689
714
|
text: ""
|
|
690
715
|
});
|
|
@@ -692,9 +717,16 @@ async function* processStreamResponse(requestContext, iterator) {
|
|
|
692
717
|
if (!choice) continue;
|
|
693
718
|
const { delta } = choice;
|
|
694
719
|
const messageChunk = convertDeltaToMessageChunk(delta, defaultRole);
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
720
|
+
if (delta.reasoning_content) {
|
|
721
|
+
reasoningState.content += delta.reasoning_content;
|
|
722
|
+
if (reasoningState.startedAt === 0) {
|
|
723
|
+
reasoningState.startedAt = Date.now();
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
if (!reasoningState.done && reasoningState.startedAt > 0 && ((delta.content?.length ?? 0) > 0 || (delta.tool_calls?.length ?? 0) > 0 || delta.function_call != null)) {
|
|
727
|
+
reasoningState.duration = Date.now() - reasoningState.startedAt;
|
|
728
|
+
reasoningState.done = true;
|
|
729
|
+
messageChunk.additional_kwargs.reasoning_time = reasoningState.duration;
|
|
698
730
|
}
|
|
699
731
|
defaultRole = (delta.role?.length ?? 0) > 0 ? delta.role : defaultRole;
|
|
700
732
|
yield new ChatGenerationChunk({
|
|
@@ -702,6 +734,13 @@ async function* processStreamResponse(requestContext, iterator) {
|
|
|
702
734
|
text: messageChunk.content
|
|
703
735
|
});
|
|
704
736
|
} catch (e) {
|
|
737
|
+
if (chunk.includes("tool_calls") || chunk.includes("function_call") || chunk.includes("tool_call_id")) {
|
|
738
|
+
requestContext.modelRequester.logger.error(
|
|
739
|
+
"error with chunk",
|
|
740
|
+
chunk
|
|
741
|
+
);
|
|
742
|
+
throw new ChatLunaError(ChatLunaErrorCode.API_REQUEST_FAILED, e);
|
|
743
|
+
}
|
|
705
744
|
if (errorCount > 5) {
|
|
706
745
|
requestContext.modelRequester.logger.error(
|
|
707
746
|
"error with chunk",
|
|
@@ -713,8 +752,21 @@ async function* processStreamResponse(requestContext, iterator) {
|
|
|
713
752
|
}
|
|
714
753
|
}
|
|
715
754
|
if (reasoningState.content.length > 0) {
|
|
755
|
+
if (!reasoningState.done && reasoningState.startedAt > 0) {
|
|
756
|
+
reasoningState.duration = Date.now() - reasoningState.startedAt;
|
|
757
|
+
reasoningState.done = true;
|
|
758
|
+
yield new ChatGenerationChunk({
|
|
759
|
+
message: new AIMessageChunk2({
|
|
760
|
+
content: "",
|
|
761
|
+
additional_kwargs: {
|
|
762
|
+
reasoning_time: reasoningState.duration
|
|
763
|
+
}
|
|
764
|
+
}),
|
|
765
|
+
text: ""
|
|
766
|
+
});
|
|
767
|
+
}
|
|
716
768
|
requestContext.modelRequester.logger.debug(
|
|
717
|
-
`reasoning content: ${reasoningState.content}. Use time: ${reasoningState.
|
|
769
|
+
`reasoning content: ${reasoningState.content}. Use time: ${reasoningState.duration / 1e3}s`
|
|
718
770
|
);
|
|
719
771
|
}
|
|
720
772
|
}
|
|
@@ -749,11 +801,15 @@ async function processResponse(requestContext, response) {
|
|
|
749
801
|
);
|
|
750
802
|
}
|
|
751
803
|
const messageChunk = convertMessageToMessageChunk(choice.message);
|
|
804
|
+
const usageMetadata = data.usage ? openAIUsageToUsageMetadata(data.usage) : void 0;
|
|
805
|
+
if (messageChunk instanceof AIMessageChunk2) {
|
|
806
|
+
messageChunk.usage_metadata = usageMetadata;
|
|
807
|
+
}
|
|
752
808
|
return new ChatGenerationChunk({
|
|
753
809
|
message: messageChunk,
|
|
754
810
|
text: getMessageContent(messageChunk.content),
|
|
755
|
-
generationInfo: {
|
|
756
|
-
|
|
811
|
+
generationInfo: usageMetadata == null ? void 0 : {
|
|
812
|
+
usage_metadata: usageMetadata
|
|
757
813
|
}
|
|
758
814
|
});
|
|
759
815
|
} catch (e) {
|
|
@@ -920,6 +976,7 @@ export {
|
|
|
920
976
|
convertMessageToMessageChunk,
|
|
921
977
|
createEmbeddings,
|
|
922
978
|
createRequestContext,
|
|
979
|
+
createUsageMetadata,
|
|
923
980
|
expandReasoningEffortModelVariants,
|
|
924
981
|
fetchFileLikeUrl,
|
|
925
982
|
fetchImageUrl,
|
|
@@ -932,9 +989,9 @@ export {
|
|
|
932
989
|
langchainMessageToOpenAIMessage,
|
|
933
990
|
messageTypeToOpenAIRole,
|
|
934
991
|
normalizeOpenAIModelName,
|
|
992
|
+
openAIUsageToUsageMetadata,
|
|
935
993
|
parseOpenAIModelNameWithReasoningEffort,
|
|
936
994
|
processDeepSeekThinkMessages,
|
|
937
|
-
processReasoningContent,
|
|
938
995
|
processResponse,
|
|
939
996
|
processStreamResponse,
|
|
940
997
|
reasoningEffortModelSuffixes,
|
package/lib/requester.d.ts
CHANGED
|
@@ -37,14 +37,6 @@ export declare function buildChatCompletionParams(params: ModelRequestParams, pl
|
|
|
37
37
|
include_usage: boolean;
|
|
38
38
|
};
|
|
39
39
|
} & Record<string, any>>;
|
|
40
|
-
export declare function processReasoningContent(delta: {
|
|
41
|
-
reasoning_content?: string;
|
|
42
|
-
content?: string;
|
|
43
|
-
}, reasoningState: {
|
|
44
|
-
content: string;
|
|
45
|
-
time: number;
|
|
46
|
-
isSet: boolean;
|
|
47
|
-
}): number;
|
|
48
40
|
export declare function processStreamResponse<T extends ClientConfig, R extends ChatLunaPlugin.Config>(requestContext: RequestContext<T, R>, iterator: AsyncGenerator<SSEEvent, string, unknown>): AsyncGenerator<ChatGenerationChunk, void, unknown>;
|
|
49
41
|
export declare function processResponse<T extends ClientConfig, R extends ChatLunaPlugin.Config>(requestContext: RequestContext<T, R>, response: Response): Promise<ChatGenerationChunk>;
|
|
50
42
|
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>;
|
package/lib/types.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export interface ChatCompletionResponse {
|
|
|
7
7
|
role?: string;
|
|
8
8
|
reasoning_content?: string;
|
|
9
9
|
function_call?: ChatCompletionRequestMessageToolCall;
|
|
10
|
+
tool_calls?: ChatCompletionRequestMessageToolCall[];
|
|
10
11
|
};
|
|
11
12
|
message: ChatCompletionResponseMessage;
|
|
12
13
|
}[];
|
|
@@ -14,11 +15,24 @@ export interface ChatCompletionResponse {
|
|
|
14
15
|
object: string;
|
|
15
16
|
created: number;
|
|
16
17
|
model: string;
|
|
17
|
-
usage
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
usage?: ChatCompletionUsage;
|
|
19
|
+
}
|
|
20
|
+
export interface ChatCompletionPromptTokensDetails {
|
|
21
|
+
audio_tokens?: number;
|
|
22
|
+
cached_tokens?: number;
|
|
23
|
+
}
|
|
24
|
+
export interface ChatCompletionCompletionTokensDetails {
|
|
25
|
+
reasoning_tokens?: number;
|
|
26
|
+
audio_tokens?: number;
|
|
27
|
+
accepted_prediction_tokens?: number;
|
|
28
|
+
rejected_prediction_tokens?: number;
|
|
29
|
+
}
|
|
30
|
+
export interface ChatCompletionUsage {
|
|
31
|
+
prompt_tokens: number;
|
|
32
|
+
completion_tokens: number;
|
|
33
|
+
total_tokens: number;
|
|
34
|
+
prompt_tokens_details?: ChatCompletionPromptTokensDetails;
|
|
35
|
+
completion_tokens_details?: ChatCompletionCompletionTokensDetails;
|
|
22
36
|
}
|
|
23
37
|
export interface ChatCompletionTextPart {
|
|
24
38
|
type: 'text';
|
package/lib/utils.d.ts
CHANGED
|
@@ -1,8 +1,19 @@
|
|
|
1
|
-
import { AIMessageChunk, BaseMessage, ChatMessageChunk, FunctionMessageChunk, HumanMessageChunk, MessageContentComplex, MessageContentImageUrl, MessageType, SystemMessageChunk, ToolMessageChunk } from '@langchain/core/messages';
|
|
1
|
+
import { AIMessageChunk, BaseMessage, ChatMessageChunk, FunctionMessageChunk, HumanMessageChunk, MessageContentComplex, MessageContentImageUrl, MessageType, SystemMessageChunk, type UsageMetadata, ToolMessageChunk } from '@langchain/core/messages';
|
|
2
2
|
import { StructuredTool } from '@langchain/core/tools';
|
|
3
3
|
import { JsonSchema7Type } from 'zod-to-json-schema';
|
|
4
|
-
import { ChatCompletionResponseMessage, ChatCompletionResponseMessageRoleEnum, ChatCompletionTool } from './types';
|
|
4
|
+
import { ChatCompletionUsage, ChatCompletionResponseMessage, ChatCompletionResponseMessageRoleEnum, ChatCompletionTool } from './types';
|
|
5
5
|
import { ChatLunaPlugin } from 'koishi-plugin-chatluna/services/chat';
|
|
6
|
+
export declare function createUsageMetadata(data: {
|
|
7
|
+
inputTokens: number;
|
|
8
|
+
outputTokens: number;
|
|
9
|
+
totalTokens: number;
|
|
10
|
+
inputAudioTokens?: number;
|
|
11
|
+
outputAudioTokens?: number;
|
|
12
|
+
cacheReadTokens?: number;
|
|
13
|
+
cacheCreationTokens?: number;
|
|
14
|
+
reasoningTokens?: number;
|
|
15
|
+
}): UsageMetadata;
|
|
16
|
+
export declare function openAIUsageToUsageMetadata(usage: ChatCompletionUsage): UsageMetadata;
|
|
6
17
|
export declare function langchainMessageToOpenAIMessage(messages: BaseMessage[], plugin: ChatLunaPlugin, model?: string, supportImageInputType?: boolean, removeSystemMessage?: boolean): Promise<ChatCompletionResponseMessage[]>;
|
|
7
18
|
export declare function processDeepSeekThinkMessages(convertedMessages: ChatCompletionResponseMessage[], originalMessages: BaseMessage[]): ChatCompletionResponseMessage[];
|
|
8
19
|
export declare function transformSystemMessages(messages: ChatCompletionResponseMessage[]): ChatCompletionResponseMessage[];
|
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.29",
|
|
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.9",
|
|
73
|
-
"koishi-plugin-chatluna": "^1.3.
|
|
73
|
+
"koishi-plugin-chatluna": "^1.3.33"
|
|
74
74
|
}
|
|
75
75
|
}
|