@jsonstudio/llms 0.6.3409 → 0.6.3541

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.
Files changed (85) hide show
  1. package/dist/conversion/codecs/anthropic-openai-codec.d.ts +12 -3
  2. package/dist/conversion/codecs/anthropic-openai-codec.js +32 -92
  3. package/dist/conversion/codecs/gemini-openai-codec.d.ts +6 -5
  4. package/dist/conversion/codecs/gemini-openai-codec.js +48 -685
  5. package/dist/conversion/codecs/openai-openai-codec.d.ts +1 -1
  6. package/dist/conversion/codecs/openai-openai-codec.js +34 -100
  7. package/dist/conversion/codecs/responses-openai-codec.d.ts +1 -1
  8. package/dist/conversion/codecs/responses-openai-codec.js +47 -159
  9. package/dist/conversion/compat/actions/anthropic-claude-code-system-prompt.d.ts +2 -6
  10. package/dist/conversion/compat/actions/anthropic-claude-code-system-prompt.js +29 -245
  11. package/dist/conversion/compat/actions/anthropic-claude-code-user-id.d.ts +3 -0
  12. package/dist/conversion/compat/actions/anthropic-claude-code-user-id.js +30 -0
  13. package/dist/conversion/compat/actions/antigravity-thought-signature-prepare.js +21 -232
  14. package/dist/conversion/compat/actions/deepseek-web-request.js +41 -276
  15. package/dist/conversion/compat/actions/deepseek-web-response.js +117 -855
  16. package/dist/conversion/compat/actions/gemini-cli-request.d.ts +1 -1
  17. package/dist/conversion/compat/actions/gemini-cli-request.js +20 -613
  18. package/dist/conversion/compat/actions/gemini-web-search.d.ts +1 -15
  19. package/dist/conversion/compat/actions/gemini-web-search.js +22 -69
  20. package/dist/conversion/compat/actions/glm-tool-extraction.d.ts +3 -2
  21. package/dist/conversion/compat/actions/glm-tool-extraction.js +28 -257
  22. package/dist/conversion/compat/actions/iflow-tool-text-fallback.d.ts +0 -8
  23. package/dist/conversion/compat/actions/iflow-tool-text-fallback.js +24 -206
  24. package/dist/conversion/compat/actions/qwen-transform.d.ts +3 -2
  25. package/dist/conversion/compat/actions/qwen-transform.js +30 -271
  26. package/dist/conversion/compat/actions/tool-text-request-guidance.js +3 -173
  27. package/dist/conversion/compat/actions/universal-shape-filter.d.ts +6 -23
  28. package/dist/conversion/compat/actions/universal-shape-filter.js +4 -383
  29. package/dist/conversion/hub/pipeline/compat/native-adapter-context.js +1 -0
  30. package/dist/conversion/pipeline/codecs/v2/anthropic-openai-pipeline.d.ts +1 -2
  31. package/dist/conversion/pipeline/codecs/v2/anthropic-openai-pipeline.js +50 -104
  32. package/dist/conversion/pipeline/codecs/v2/openai-openai-pipeline.js +12 -10
  33. package/dist/conversion/pipeline/codecs/v2/responses-openai-pipeline.d.ts +0 -2
  34. package/dist/conversion/pipeline/codecs/v2/responses-openai-pipeline.js +46 -67
  35. package/dist/conversion/pipeline/codecs/v2/shared/openai-chat-helpers.js +15 -40
  36. package/dist/conversion/responses/responses-openai-bridge/response-payload.js +47 -348
  37. package/dist/conversion/responses/responses-openai-bridge.js +129 -611
  38. package/dist/conversion/shared/chat-output-normalizer.js +6 -0
  39. package/dist/conversion/shared/chat-request-filters.js +1 -1
  40. package/dist/conversion/shared/output-content-normalizer.js +10 -0
  41. package/dist/conversion/shared/responses-conversation-store.js +22 -135
  42. package/dist/conversion/shared/responses-output-builder.d.ts +0 -2
  43. package/dist/conversion/shared/responses-output-builder.js +28 -318
  44. package/dist/conversion/shared/responses-response-utils.js +35 -86
  45. package/dist/conversion/shared/streaming-text-extractor.d.ts +1 -2
  46. package/dist/conversion/shared/streaming-text-extractor.js +13 -14
  47. package/dist/native/router_hotpath_napi.node +0 -0
  48. package/dist/quota/quota-state.js +29 -7
  49. package/dist/quota/types.d.ts +1 -0
  50. package/dist/router/virtual-router/bootstrap/routing-config.js +11 -3
  51. package/dist/router/virtual-router/engine-legacy.d.ts +3 -3
  52. package/dist/router/virtual-router/engine-legacy.js +15 -7
  53. package/dist/router/virtual-router/engine-selection/native-compat-action-semantics.d.ts +16 -0
  54. package/dist/router/virtual-router/engine-selection/native-compat-action-semantics.js +434 -46
  55. package/dist/router/virtual-router/engine-selection/native-hub-bridge-action-semantics.d.ts +83 -0
  56. package/dist/router/virtual-router/engine-selection/native-hub-bridge-action-semantics.js +295 -0
  57. package/dist/router/virtual-router/engine-selection/native-hub-pipeline-req-outbound-semantics.d.ts +1 -0
  58. package/dist/router/virtual-router/engine-selection/native-hub-pipeline-resp-semantics.d.ts +7 -0
  59. package/dist/router/virtual-router/engine-selection/native-hub-pipeline-resp-semantics.js +8 -1
  60. package/dist/router/virtual-router/engine-selection/native-router-hotpath-loader.js +383 -298
  61. package/dist/router/virtual-router/engine-selection/native-shared-conversion-semantics.d.ts +20 -0
  62. package/dist/router/virtual-router/engine-selection/native-shared-conversion-semantics.js +201 -0
  63. package/dist/router/virtual-router/engine-selection/native-virtual-router-routing-instructions-semantics.d.ts +1 -0
  64. package/dist/router/virtual-router/engine-selection/native-virtual-router-routing-instructions-semantics.js +37 -0
  65. package/dist/router/virtual-router/engine.js +0 -38
  66. package/dist/router/virtual-router/features.js +44 -3
  67. package/dist/router/virtual-router/routing-instructions/parse.d.ts +0 -12
  68. package/dist/router/virtual-router/routing-instructions/parse.js +9 -389
  69. package/dist/router/virtual-router/stop-message-state-sync.d.ts +3 -6
  70. package/dist/router/virtual-router/stop-message-state-sync.js +50 -21
  71. package/dist/servertool/handlers/followup-request-builder.js +12 -2
  72. package/dist/sse/sse-to-json/anthropic-sse-to-json-converter.d.ts +1 -0
  73. package/dist/sse/sse-to-json/anthropic-sse-to-json-converter.js +26 -0
  74. package/dist/sse/sse-to-json/builders/anthropic-response-builder.js +12 -2
  75. package/package.json +1 -1
  76. package/dist/router/virtual-router/engine-legacy/route-finalize.d.ts +0 -9
  77. package/dist/router/virtual-router/engine-legacy/route-finalize.js +0 -84
  78. package/dist/router/virtual-router/engine-legacy/route-selection.d.ts +0 -17
  79. package/dist/router/virtual-router/engine-legacy/route-selection.js +0 -205
  80. package/dist/router/virtual-router/engine-legacy/route-state-allowlist.d.ts +0 -3
  81. package/dist/router/virtual-router/engine-legacy/route-state-allowlist.js +0 -36
  82. package/dist/router/virtual-router/engine-legacy/route-state.d.ts +0 -12
  83. package/dist/router/virtual-router/engine-legacy/route-state.js +0 -386
  84. package/dist/router/virtual-router/engine-legacy/routing.d.ts +0 -8
  85. package/dist/router/virtual-router/engine-legacy/routing.js +0 -8
@@ -1,7 +1,16 @@
1
1
  import type { ConversionCodec, ConversionContext, ConversionProfile } from '../types.js';
2
- import { buildAnthropicFromOpenAIChat, buildOpenAIChatFromAnthropic } from '../shared/anthropic-message-utils.js';
3
- export { buildAnthropicFromOpenAIChat, buildOpenAIChatFromAnthropic };
4
- export { buildAnthropicRequestFromOpenAIChat } from '../shared/anthropic-message-utils.js';
2
+ import { buildAnthropicRequestFromOpenAIChat } from '../shared/anthropic-message-utils.js';
3
+ export declare function buildOpenAIChatFromAnthropic(payload: unknown, options?: {
4
+ includeToolCallIds?: boolean;
5
+ }): {
6
+ messages: unknown[];
7
+ } & Record<string, unknown>;
8
+ export declare function buildAnthropicFromOpenAIChat(payload: unknown, options?: {
9
+ toolNameMap?: Record<string, string>;
10
+ requestId?: string;
11
+ entryEndpoint?: string;
12
+ }): Record<string, unknown>;
13
+ export { buildAnthropicRequestFromOpenAIChat };
5
14
  export declare class AnthropicOpenAIConversionCodec implements ConversionCodec {
6
15
  private readonly _dependencies;
7
16
  readonly id = "anthropic-openai";
@@ -1,8 +1,13 @@
1
- import { runStandardChatRequestFilters } from '../index.js';
2
- import { FilterEngine, ResponseToolTextCanonicalizeFilter, ResponseToolArgumentsStringifyFilter, ResponseFinishInvariantsFilter } from '../../filters/index.js';
3
- import { buildAnthropicFromOpenAIChat, buildOpenAIChatFromAnthropic, mapAnthropicToolsToChat, buildAnthropicToolAliasMap } from '../shared/anthropic-message-utils.js';
4
- export { buildAnthropicFromOpenAIChat, buildOpenAIChatFromAnthropic };
5
- export { buildAnthropicRequestFromOpenAIChat } from '../shared/anthropic-message-utils.js';
1
+ import { buildAnthropicRequestFromOpenAIChat } from '../shared/anthropic-message-utils.js';
2
+ import { buildAnthropicFromOpenAIChatWithNative, buildOpenAIChatFromAnthropicWithNative } from '../../router/virtual-router/engine-selection/native-compat-action-semantics.js';
3
+ export function buildOpenAIChatFromAnthropic(payload, options) {
4
+ const result = buildOpenAIChatFromAnthropicWithNative((payload ?? {}), options);
5
+ return (result.request ?? {});
6
+ }
7
+ export function buildAnthropicFromOpenAIChat(payload, options) {
8
+ return buildAnthropicFromOpenAIChatWithNative((payload ?? {}), options);
9
+ }
10
+ export { buildAnthropicRequestFromOpenAIChat };
6
11
  export class AnthropicOpenAIConversionCodec {
7
12
  _dependencies;
8
13
  id = 'anthropic-openai';
@@ -11,102 +16,37 @@ export class AnthropicOpenAIConversionCodec {
11
16
  constructor(_dependencies) {
12
17
  this._dependencies = _dependencies;
13
18
  }
14
- async initialize() { this.initialized = true; }
19
+ async initialize() {
20
+ this.initialized = true;
21
+ }
15
22
  async convertRequest(payload, _profile, context) {
16
- if (!this.initialized) {
23
+ if (!this.initialized)
17
24
  await this.initialize();
18
- }
19
- const model = String(payload?.model || 'unknown');
20
- const { messages } = buildOpenAIChatFromAnthropic(payload, { includeToolCallIds: true });
21
- const out = { model, messages };
22
- const aliasMap = buildAnthropicToolAliasMap(payload?.tools);
23
- if (aliasMap) {
25
+ const native = buildOpenAIChatFromAnthropicWithNative(payload ?? {}, {
26
+ includeToolCallIds: true
27
+ });
28
+ const request = native.request && typeof native.request === 'object'
29
+ ? { ...native.request }
30
+ : {};
31
+ const aliasMap = native.anthropicToolNameMap && typeof native.anthropicToolNameMap === 'object'
32
+ ? native.anthropicToolNameMap
33
+ : undefined;
34
+ if (aliasMap && Object.keys(aliasMap).length > 0) {
24
35
  context.metadata = context.metadata ?? {};
25
36
  context.metadata.anthropicToolNameMap = aliasMap;
26
37
  }
27
- // 最小必要的形状转换:Anthropic tools → OpenAI Chat function 工具
28
- try {
29
- const normalizedTools = mapAnthropicToolsToChat(payload?.tools);
30
- if (normalizedTools) {
31
- out.tools = normalizedTools;
32
- }
33
- }
34
- catch { /* keep tools undefined on failure */ }
35
- if (Array.isArray(payload?.stop_sequences)) {
36
- out.stop = payload.stop_sequences;
37
- }
38
- if (typeof payload?.temperature === 'number') {
39
- out.temperature = payload.temperature;
40
- }
41
- if (typeof payload?.top_p === 'number') {
42
- out.top_p = payload.top_p;
43
- }
44
- if (typeof payload?.max_tokens === 'number') {
45
- out.max_tokens = payload.max_tokens;
46
- }
47
- // 不设置默认 tool_choice;统一由 Chat 后半段治理
48
- // 统一流控:非流式由核心过滤器处理;此处不设置 stream
49
- // 统一走 Chat 请求过滤链路,使 /v1/messages 与 /v1/chat 在工具治理上保持一致
50
- const ctxForFilters = {
51
- ...context,
52
- requestId: context.requestId ?? `req_${Date.now()}`,
53
- entryEndpoint: context.entryEndpoint ?? context.endpoint,
54
- endpoint: context.endpoint
55
- };
56
- return runStandardChatRequestFilters(out, _profile, ctxForFilters);
38
+ return request;
57
39
  }
58
40
  async convertResponse(payload, _profile, context) {
59
- if (!this.initialized) {
41
+ if (!this.initialized)
60
42
  await this.initialize();
61
- }
62
- // 如果是 /v1/messages 且请求端声明了流式(或需要伪流式),合成 Anthropic SSE 流并透传给上层
63
- // 对非流式 JSON 响应,先在 OpenAI Chat canonical 上运行统一的响应工具治理,
64
- // 再映射回 Anthropic Messages 形状,保证与 /v1/chat 路径的行为一致。
65
- const dto = {
66
- data: payload,
67
- metadata: {
68
- requestId: context.requestId ?? `req_${Date.now()}`,
69
- pipelineId: context.metadata?.pipelineId ?? 'conversion-router',
70
- processingTime: 0,
71
- stages: []
72
- }
73
- };
74
- const resCtxBase = {
75
- requestId: context.requestId ?? dto.metadata.requestId,
76
- model: undefined,
77
- endpoint: context.entryEndpoint || context.endpoint,
78
- profile: 'openai-chat',
79
- debug: { emit: () => { } }
80
- };
81
- const engine = new FilterEngine();
82
- engine.registerFilter(new ResponseToolTextCanonicalizeFilter());
83
- engine.registerFilter(new ResponseToolArgumentsStringifyFilter());
84
- engine.registerFilter(new ResponseFinishInvariantsFilter());
85
- let staged = await engine.run('response_pre', dto.data, resCtxBase);
86
- staged = await engine.run('response_map', staged, resCtxBase);
87
- staged = await engine.run('response_post', staged, resCtxBase);
88
- const governedChat = staged;
89
- const aliasMap = coerceAliasMap(context.metadata?.anthropicToolNameMap);
90
- return buildAnthropicFromOpenAIChat(governedChat, {
43
+ const aliasMap = context.metadata && typeof context.metadata === 'object'
44
+ ? context.metadata.anthropicToolNameMap
45
+ : undefined;
46
+ return buildAnthropicFromOpenAIChatWithNative((payload ?? {}), {
91
47
  toolNameMap: aliasMap,
92
- requestId: context.requestId
48
+ requestId: context.requestId,
49
+ entryEndpoint: context.entryEndpoint ?? context.endpoint
93
50
  });
94
51
  }
95
52
  }
96
- function coerceAliasMap(candidate) {
97
- if (!candidate || typeof candidate !== 'object' || Array.isArray(candidate)) {
98
- return undefined;
99
- }
100
- let populated = false;
101
- const map = {};
102
- for (const [key, value] of Object.entries(candidate)) {
103
- if (typeof key !== 'string' || typeof value !== 'string')
104
- continue;
105
- const trimmedKey = key.trim();
106
- if (!trimmedKey.length)
107
- continue;
108
- map[trimmedKey] = value;
109
- populated = true;
110
- }
111
- return populated ? map : undefined;
112
- }
@@ -1,9 +1,10 @@
1
1
  import type { ConversionCodec, ConversionContext, ConversionProfile } from '../types.js';
2
- export declare function buildOpenAIChatFromGeminiRequest(payload: any): {
3
- messages: any[];
4
- };
5
- export declare function buildOpenAIChatFromGeminiResponse(payload: any): any;
6
- export declare function buildGeminiFromOpenAIChat(chatResp: any): any;
2
+ import type { JsonObject, JsonValue } from '../hub/types/json.js';
3
+ export declare function buildOpenAIChatFromGeminiRequest(payload: unknown): {
4
+ messages: JsonValue[];
5
+ } & JsonObject;
6
+ export declare function buildOpenAIChatFromGeminiResponse(payload: unknown): JsonObject;
7
+ export declare function buildGeminiFromOpenAIChat(chatResp: unknown): JsonObject;
7
8
  export declare class GeminiOpenAIConversionCodec implements ConversionCodec {
8
9
  private readonly _dependencies;
9
10
  readonly id = "gemini-openai";