@chatluna/v1-shared-adapter 1.0.29 → 1.0.30

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
@@ -42,7 +42,7 @@ __export(index_exports, {
42
42
  normalizeOpenAIModelName: () => normalizeOpenAIModelName,
43
43
  openAIUsageToUsageMetadata: () => openAIUsageToUsageMetadata,
44
44
  parseOpenAIModelNameWithReasoningEffort: () => parseOpenAIModelNameWithReasoningEffort,
45
- processDeepSeekThinkMessages: () => processDeepSeekThinkMessages,
45
+ processInterleavedThinkMessages: () => processInterleavedThinkMessages,
46
46
  processResponse: () => processResponse,
47
47
  processStreamResponse: () => processStreamResponse,
48
48
  reasoningEffortModelSuffixes: () => reasoningEffortModelSuffixes,
@@ -228,7 +228,6 @@ __name(openAIUsageToUsageMetadata, "openAIUsageToUsageMetadata");
228
228
  async function langchainMessageToOpenAIMessage(messages, plugin, model, supportImageInputType, removeSystemMessage) {
229
229
  const result = [];
230
230
  const normalizedModel = model ? normalizeOpenAIModelName(model) : model;
231
- const isDeepseekThinkModel = normalizedModel?.includes("deepseek-reasoner");
232
231
  for (const rawMessage of messages) {
233
232
  const role = messageTypeToOpenAIRole(rawMessage.getType());
234
233
  const msg = {
@@ -343,13 +342,10 @@ async function langchainMessageToOpenAIMessage(messages, plugin, model, supportI
343
342
  if (removeSystemMessage) {
344
343
  return transformSystemMessages(result);
345
344
  }
346
- if (isDeepseekThinkModel) {
347
- return processDeepSeekThinkMessages(result, messages);
348
- }
349
- return result;
345
+ return processInterleavedThinkMessages(result, messages);
350
346
  }
351
347
  __name(langchainMessageToOpenAIMessage, "langchainMessageToOpenAIMessage");
352
- function processDeepSeekThinkMessages(convertedMessages, originalMessages) {
348
+ function processInterleavedThinkMessages(convertedMessages, originalMessages) {
353
349
  if (originalMessages.length === 0) {
354
350
  return convertedMessages;
355
351
  }
@@ -378,7 +374,7 @@ function processDeepSeekThinkMessages(convertedMessages, originalMessages) {
378
374
  return message;
379
375
  });
380
376
  }
381
- __name(processDeepSeekThinkMessages, "processDeepSeekThinkMessages");
377
+ __name(processInterleavedThinkMessages, "processInterleavedThinkMessages");
382
378
  function transformSystemMessages(messages) {
383
379
  const mappedMessage = [];
384
380
  for (let i = 0; i < messages.length; i++) {
@@ -721,9 +717,8 @@ async function* processStreamResponse(requestContext, iterator) {
721
717
  let errorCount = 0;
722
718
  const reasoningState = {
723
719
  content: "",
724
- startedAt: 0,
725
- duration: 0,
726
- done: false
720
+ startedAt: Date.now(),
721
+ endedAt: void 0
727
722
  };
728
723
  for await (const event of iterator) {
729
724
  const chunk = event.data;
@@ -753,17 +748,24 @@ async function* processStreamResponse(requestContext, iterator) {
753
748
  }
754
749
  if (!choice) continue;
755
750
  const { delta } = choice;
756
- const messageChunk = convertDeltaToMessageChunk(delta, defaultRole);
757
- if (delta.reasoning_content) {
751
+ const hasResult = (delta.content?.length ?? 0) > 0 || (delta.tool_calls?.length ?? 0) > 0 || delta.function_call != null;
752
+ if (reasoningState.endedAt == null && hasResult) {
753
+ reasoningState.endedAt = Date.now();
754
+ }
755
+ if (reasoningState.endedAt == null && !hasResult && delta.reasoning_content) {
758
756
  reasoningState.content += delta.reasoning_content;
759
- if (reasoningState.startedAt === 0) {
760
- reasoningState.startedAt = Date.now();
761
- }
762
757
  }
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;
758
+ const messageChunk = convertDeltaToMessageChunk(
759
+ {
760
+ ...delta,
761
+ reasoning_content: void 0
762
+ },
763
+ defaultRole
764
+ );
765
+ const hasMessageChunk = (typeof messageChunk.content === "string" ? messageChunk.content.length > 0 : Array.isArray(messageChunk.content) && messageChunk.content.length > 0) || messageChunk instanceof import_messages2.AIMessageChunk && (messageChunk.tool_call_chunks?.length ?? 0) > 0 || messageChunk.additional_kwargs.function_call != null;
766
+ if (!hasMessageChunk) {
767
+ defaultRole = (delta.role?.length ?? 0) > 0 ? delta.role : defaultRole;
768
+ continue;
767
769
  }
768
770
  defaultRole = (delta.role?.length ?? 0) > 0 ? delta.role : defaultRole;
769
771
  yield new import_outputs.ChatGenerationChunk({
@@ -789,21 +791,19 @@ async function* processStreamResponse(requestContext, iterator) {
789
791
  }
790
792
  }
791
793
  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
- }
794
+ const reasoningTime = (reasoningState.endedAt ?? Date.now()) - reasoningState.startedAt;
795
+ yield new import_outputs.ChatGenerationChunk({
796
+ message: new import_messages2.AIMessageChunk({
797
+ content: "",
798
+ additional_kwargs: {
799
+ reasoning_content: reasoningState.content,
800
+ ...reasoningTime != null ? { reasoning_time: reasoningTime } : {}
801
+ }
802
+ }),
803
+ text: ""
804
+ });
805
805
  requestContext.modelRequester.logger.debug(
806
- `reasoning content: ${reasoningState.content}. Use time: ${reasoningState.duration / 1e3}s`
806
+ `Reasoning Content: ${reasoningState.content}. Thought for: ${(reasoningTime ?? 0) / 1e3}s`
807
807
  );
808
808
  }
809
809
  }
@@ -1029,7 +1029,7 @@ __name(createRequestContext, "createRequestContext");
1029
1029
  normalizeOpenAIModelName,
1030
1030
  openAIUsageToUsageMetadata,
1031
1031
  parseOpenAIModelNameWithReasoningEffort,
1032
- processDeepSeekThinkMessages,
1032
+ processInterleavedThinkMessages,
1033
1033
  processResponse,
1034
1034
  processStreamResponse,
1035
1035
  reasoningEffortModelSuffixes,
package/lib/index.mjs CHANGED
@@ -191,7 +191,6 @@ __name(openAIUsageToUsageMetadata, "openAIUsageToUsageMetadata");
191
191
  async function langchainMessageToOpenAIMessage(messages, plugin, model, supportImageInputType, removeSystemMessage) {
192
192
  const result = [];
193
193
  const normalizedModel = model ? normalizeOpenAIModelName(model) : model;
194
- const isDeepseekThinkModel = normalizedModel?.includes("deepseek-reasoner");
195
194
  for (const rawMessage of messages) {
196
195
  const role = messageTypeToOpenAIRole(rawMessage.getType());
197
196
  const msg = {
@@ -306,13 +305,10 @@ async function langchainMessageToOpenAIMessage(messages, plugin, model, supportI
306
305
  if (removeSystemMessage) {
307
306
  return transformSystemMessages(result);
308
307
  }
309
- if (isDeepseekThinkModel) {
310
- return processDeepSeekThinkMessages(result, messages);
311
- }
312
- return result;
308
+ return processInterleavedThinkMessages(result, messages);
313
309
  }
314
310
  __name(langchainMessageToOpenAIMessage, "langchainMessageToOpenAIMessage");
315
- function processDeepSeekThinkMessages(convertedMessages, originalMessages) {
311
+ function processInterleavedThinkMessages(convertedMessages, originalMessages) {
316
312
  if (originalMessages.length === 0) {
317
313
  return convertedMessages;
318
314
  }
@@ -341,7 +337,7 @@ function processDeepSeekThinkMessages(convertedMessages, originalMessages) {
341
337
  return message;
342
338
  });
343
339
  }
344
- __name(processDeepSeekThinkMessages, "processDeepSeekThinkMessages");
340
+ __name(processInterleavedThinkMessages, "processInterleavedThinkMessages");
345
341
  function transformSystemMessages(messages) {
346
342
  const mappedMessage = [];
347
343
  for (let i = 0; i < messages.length; i++) {
@@ -684,9 +680,8 @@ async function* processStreamResponse(requestContext, iterator) {
684
680
  let errorCount = 0;
685
681
  const reasoningState = {
686
682
  content: "",
687
- startedAt: 0,
688
- duration: 0,
689
- done: false
683
+ startedAt: Date.now(),
684
+ endedAt: void 0
690
685
  };
691
686
  for await (const event of iterator) {
692
687
  const chunk = event.data;
@@ -716,17 +711,24 @@ async function* processStreamResponse(requestContext, iterator) {
716
711
  }
717
712
  if (!choice) continue;
718
713
  const { delta } = choice;
719
- const messageChunk = convertDeltaToMessageChunk(delta, defaultRole);
720
- if (delta.reasoning_content) {
714
+ const hasResult = (delta.content?.length ?? 0) > 0 || (delta.tool_calls?.length ?? 0) > 0 || delta.function_call != null;
715
+ if (reasoningState.endedAt == null && hasResult) {
716
+ reasoningState.endedAt = Date.now();
717
+ }
718
+ if (reasoningState.endedAt == null && !hasResult && delta.reasoning_content) {
721
719
  reasoningState.content += delta.reasoning_content;
722
- if (reasoningState.startedAt === 0) {
723
- reasoningState.startedAt = Date.now();
724
- }
725
720
  }
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;
721
+ const messageChunk = convertDeltaToMessageChunk(
722
+ {
723
+ ...delta,
724
+ reasoning_content: void 0
725
+ },
726
+ defaultRole
727
+ );
728
+ const hasMessageChunk = (typeof messageChunk.content === "string" ? messageChunk.content.length > 0 : Array.isArray(messageChunk.content) && messageChunk.content.length > 0) || messageChunk instanceof AIMessageChunk2 && (messageChunk.tool_call_chunks?.length ?? 0) > 0 || messageChunk.additional_kwargs.function_call != null;
729
+ if (!hasMessageChunk) {
730
+ defaultRole = (delta.role?.length ?? 0) > 0 ? delta.role : defaultRole;
731
+ continue;
730
732
  }
731
733
  defaultRole = (delta.role?.length ?? 0) > 0 ? delta.role : defaultRole;
732
734
  yield new ChatGenerationChunk({
@@ -752,21 +754,19 @@ async function* processStreamResponse(requestContext, iterator) {
752
754
  }
753
755
  }
754
756
  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
- }
757
+ const reasoningTime = (reasoningState.endedAt ?? Date.now()) - reasoningState.startedAt;
758
+ yield new ChatGenerationChunk({
759
+ message: new AIMessageChunk2({
760
+ content: "",
761
+ additional_kwargs: {
762
+ reasoning_content: reasoningState.content,
763
+ ...reasoningTime != null ? { reasoning_time: reasoningTime } : {}
764
+ }
765
+ }),
766
+ text: ""
767
+ });
768
768
  requestContext.modelRequester.logger.debug(
769
- `reasoning content: ${reasoningState.content}. Use time: ${reasoningState.duration / 1e3}s`
769
+ `Reasoning Content: ${reasoningState.content}. Thought for: ${(reasoningTime ?? 0) / 1e3}s`
770
770
  );
771
771
  }
772
772
  }
@@ -991,7 +991,7 @@ export {
991
991
  normalizeOpenAIModelName,
992
992
  openAIUsageToUsageMetadata,
993
993
  parseOpenAIModelNameWithReasoningEffort,
994
- processDeepSeekThinkMessages,
994
+ processInterleavedThinkMessages,
995
995
  processResponse,
996
996
  processStreamResponse,
997
997
  reasoningEffortModelSuffixes,
package/lib/utils.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { AIMessageChunk, BaseMessage, ChatMessageChunk, FunctionMessageChunk, HumanMessageChunk, MessageContentComplex, MessageContentImageUrl, MessageType, SystemMessageChunk, type UsageMetadata, ToolMessageChunk } from '@langchain/core/messages';
1
+ import { AIMessageChunk, BaseMessage, ChatMessageChunk, FunctionMessageChunk, HumanMessageChunk, MessageContentComplex, MessageContentImageUrl, MessageType, SystemMessageChunk, ToolMessageChunk, type UsageMetadata } from '@langchain/core/messages';
2
2
  import { StructuredTool } from '@langchain/core/tools';
3
3
  import { JsonSchema7Type } from 'zod-to-json-schema';
4
- import { ChatCompletionUsage, ChatCompletionResponseMessage, ChatCompletionResponseMessageRoleEnum, ChatCompletionTool } from './types';
4
+ import { ChatCompletionResponseMessage, ChatCompletionResponseMessageRoleEnum, ChatCompletionTool, ChatCompletionUsage } from './types';
5
5
  import { ChatLunaPlugin } from 'koishi-plugin-chatluna/services/chat';
6
6
  export declare function createUsageMetadata(data: {
7
7
  inputTokens: number;
@@ -15,7 +15,7 @@ export declare function createUsageMetadata(data: {
15
15
  }): UsageMetadata;
16
16
  export declare function openAIUsageToUsageMetadata(usage: ChatCompletionUsage): UsageMetadata;
17
17
  export declare function langchainMessageToOpenAIMessage(messages: BaseMessage[], plugin: ChatLunaPlugin, model?: string, supportImageInputType?: boolean, removeSystemMessage?: boolean): Promise<ChatCompletionResponseMessage[]>;
18
- export declare function processDeepSeekThinkMessages(convertedMessages: ChatCompletionResponseMessage[], originalMessages: BaseMessage[]): ChatCompletionResponseMessage[];
18
+ export declare function processInterleavedThinkMessages(convertedMessages: ChatCompletionResponseMessage[], originalMessages: BaseMessage[]): ChatCompletionResponseMessage[];
19
19
  export declare function transformSystemMessages(messages: ChatCompletionResponseMessage[]): ChatCompletionResponseMessage[];
20
20
  export declare function fetchImageUrl(plugin: ChatLunaPlugin, content: MessageContentImageUrl): Promise<string>;
21
21
  type MessageContentFileLike = MessageContentComplex & ({
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.29",
4
+ "version": "1.0.30",
5
5
  "main": "lib/index.cjs",
6
6
  "module": "lib/index.mjs",
7
7
  "typings": "lib/index.d.ts",