@jsonstudio/llms 0.4.4 → 0.4.6

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 (160) hide show
  1. package/dist/conversion/codec-registry.js +11 -1
  2. package/dist/conversion/codecs/anthropic-openai-codec.d.ts +13 -0
  3. package/dist/conversion/codecs/anthropic-openai-codec.js +18 -473
  4. package/dist/conversion/codecs/gemini-openai-codec.js +91 -48
  5. package/dist/conversion/codecs/responses-openai-codec.js +9 -2
  6. package/dist/conversion/hub/format-adapters/anthropic-format-adapter.js +3 -0
  7. package/dist/conversion/hub/format-adapters/chat-format-adapter.js +3 -0
  8. package/dist/conversion/hub/format-adapters/gemini-format-adapter.js +3 -0
  9. package/dist/conversion/hub/format-adapters/responses-format-adapter.d.ts +19 -0
  10. package/dist/conversion/hub/format-adapters/responses-format-adapter.js +9 -0
  11. package/dist/conversion/hub/node-support.js +3 -1
  12. package/dist/conversion/hub/pipeline/hub-pipeline.js +37 -32
  13. package/dist/conversion/hub/response/provider-response.js +1 -1
  14. package/dist/conversion/hub/response/response-mappers.js +1 -1
  15. package/dist/conversion/hub/response/response-runtime.js +109 -10
  16. package/dist/conversion/hub/semantic-mappers/anthropic-mapper.js +70 -156
  17. package/dist/conversion/hub/semantic-mappers/chat-mapper.js +63 -52
  18. package/dist/conversion/hub/semantic-mappers/gemini-mapper.js +76 -143
  19. package/dist/conversion/hub/semantic-mappers/responses-mapper.js +40 -160
  20. package/dist/conversion/hub/standardized-bridge.js +3 -0
  21. package/dist/conversion/hub/tool-governance/rules.js +2 -2
  22. package/dist/conversion/index.d.ts +5 -0
  23. package/dist/conversion/index.js +5 -0
  24. package/dist/conversion/pipeline/codecs/v2/anthropic-openai-pipeline.d.ts +12 -0
  25. package/dist/conversion/pipeline/codecs/v2/anthropic-openai-pipeline.js +100 -0
  26. package/dist/conversion/pipeline/codecs/v2/openai-openai-pipeline.d.ts +15 -0
  27. package/dist/conversion/pipeline/codecs/v2/openai-openai-pipeline.js +174 -0
  28. package/dist/conversion/pipeline/codecs/v2/responses-openai-pipeline.d.ts +14 -0
  29. package/dist/conversion/pipeline/codecs/v2/responses-openai-pipeline.js +166 -0
  30. package/dist/conversion/pipeline/codecs/v2/shared/openai-chat-helpers.d.ts +13 -0
  31. package/dist/conversion/pipeline/codecs/v2/shared/openai-chat-helpers.js +66 -0
  32. package/dist/conversion/pipeline/hooks/adapter-context.d.ts +7 -0
  33. package/dist/conversion/pipeline/hooks/adapter-context.js +18 -0
  34. package/dist/conversion/pipeline/hooks/protocol-hooks.d.ts +67 -0
  35. package/dist/conversion/pipeline/hooks/protocol-hooks.js +1 -0
  36. package/dist/conversion/pipeline/index.d.ts +35 -0
  37. package/dist/conversion/pipeline/index.js +103 -0
  38. package/dist/conversion/pipeline/meta/meta-bag.d.ts +20 -0
  39. package/dist/conversion/pipeline/meta/meta-bag.js +81 -0
  40. package/dist/conversion/pipeline/schema/canonical-chat.d.ts +18 -0
  41. package/dist/conversion/pipeline/schema/canonical-chat.js +1 -0
  42. package/dist/conversion/pipeline/schema/index.d.ts +1 -0
  43. package/dist/conversion/pipeline/schema/index.js +1 -0
  44. package/dist/conversion/responses/responses-openai-bridge.d.ts +48 -0
  45. package/dist/conversion/responses/responses-openai-bridge.js +157 -1146
  46. package/dist/conversion/shared/anthropic-message-utils.d.ts +12 -0
  47. package/dist/conversion/shared/anthropic-message-utils.js +587 -0
  48. package/dist/conversion/shared/bridge-actions.d.ts +39 -0
  49. package/dist/conversion/shared/bridge-actions.js +709 -0
  50. package/dist/conversion/shared/bridge-conversation-store.d.ts +41 -0
  51. package/dist/conversion/shared/bridge-conversation-store.js +279 -0
  52. package/dist/conversion/shared/bridge-id-utils.d.ts +7 -0
  53. package/dist/conversion/shared/bridge-id-utils.js +42 -0
  54. package/dist/conversion/shared/bridge-instructions.d.ts +1 -0
  55. package/dist/conversion/shared/bridge-instructions.js +113 -0
  56. package/dist/conversion/shared/bridge-message-types.d.ts +39 -0
  57. package/dist/conversion/shared/bridge-message-types.js +1 -0
  58. package/dist/conversion/shared/bridge-message-utils.d.ts +22 -0
  59. package/dist/conversion/shared/bridge-message-utils.js +473 -0
  60. package/dist/conversion/shared/bridge-metadata.d.ts +1 -0
  61. package/dist/conversion/shared/bridge-metadata.js +1 -0
  62. package/dist/conversion/shared/bridge-policies.d.ts +18 -0
  63. package/dist/conversion/shared/bridge-policies.js +276 -0
  64. package/dist/conversion/shared/bridge-request-adapter.d.ts +28 -0
  65. package/dist/conversion/shared/bridge-request-adapter.js +430 -0
  66. package/dist/conversion/shared/chat-output-normalizer.d.ts +4 -0
  67. package/dist/conversion/shared/chat-output-normalizer.js +56 -0
  68. package/dist/conversion/shared/chat-request-filters.js +24 -1
  69. package/dist/conversion/shared/gemini-tool-utils.d.ts +5 -0
  70. package/dist/conversion/shared/gemini-tool-utils.js +130 -0
  71. package/dist/conversion/shared/metadata-passthrough.d.ts +11 -0
  72. package/dist/conversion/shared/metadata-passthrough.js +57 -0
  73. package/dist/conversion/shared/output-content-normalizer.d.ts +12 -0
  74. package/dist/conversion/shared/output-content-normalizer.js +119 -0
  75. package/dist/conversion/shared/reasoning-normalizer.d.ts +21 -0
  76. package/dist/conversion/shared/reasoning-normalizer.js +368 -0
  77. package/dist/conversion/shared/reasoning-tool-normalizer.d.ts +12 -0
  78. package/dist/conversion/shared/reasoning-tool-normalizer.js +132 -0
  79. package/dist/conversion/shared/reasoning-tool-parser.d.ts +10 -0
  80. package/dist/conversion/shared/reasoning-tool-parser.js +95 -0
  81. package/dist/conversion/shared/reasoning-utils.d.ts +2 -0
  82. package/dist/conversion/shared/reasoning-utils.js +42 -0
  83. package/dist/conversion/shared/responses-conversation-store.js +5 -11
  84. package/dist/conversion/shared/responses-message-utils.d.ts +15 -0
  85. package/dist/conversion/shared/responses-message-utils.js +206 -0
  86. package/dist/conversion/shared/responses-output-builder.d.ts +15 -0
  87. package/dist/conversion/shared/responses-output-builder.js +179 -0
  88. package/dist/conversion/shared/responses-output-utils.d.ts +7 -0
  89. package/dist/conversion/shared/responses-output-utils.js +108 -0
  90. package/dist/conversion/shared/responses-request-adapter.d.ts +28 -0
  91. package/dist/conversion/shared/responses-request-adapter.js +9 -40
  92. package/dist/conversion/shared/responses-response-utils.d.ts +3 -0
  93. package/dist/conversion/shared/responses-response-utils.js +209 -0
  94. package/dist/conversion/shared/responses-tool-utils.d.ts +12 -0
  95. package/dist/conversion/shared/responses-tool-utils.js +90 -0
  96. package/dist/conversion/shared/responses-types.d.ts +33 -0
  97. package/dist/conversion/shared/responses-types.js +1 -0
  98. package/dist/conversion/shared/tool-call-utils.d.ts +11 -0
  99. package/dist/conversion/shared/tool-call-utils.js +56 -0
  100. package/dist/conversion/shared/tool-governor.js +5 -0
  101. package/dist/conversion/shared/tool-mapping.d.ts +19 -0
  102. package/dist/conversion/shared/tool-mapping.js +124 -0
  103. package/dist/conversion/shared/tool-normalizers.d.ts +4 -0
  104. package/dist/conversion/shared/tool-normalizers.js +84 -0
  105. package/dist/router/virtual-router/bootstrap.js +18 -3
  106. package/dist/router/virtual-router/provider-registry.js +4 -2
  107. package/dist/router/virtual-router/types.d.ts +212 -0
  108. package/dist/sse/index.d.ts +38 -2
  109. package/dist/sse/index.js +27 -0
  110. package/dist/sse/json-to-sse/anthropic-json-to-sse-converter.d.ts +14 -0
  111. package/dist/sse/json-to-sse/anthropic-json-to-sse-converter.js +106 -73
  112. package/dist/sse/json-to-sse/chat-json-to-sse-converter.js +6 -2
  113. package/dist/sse/json-to-sse/gemini-json-to-sse-converter.d.ts +14 -0
  114. package/dist/sse/json-to-sse/gemini-json-to-sse-converter.js +99 -0
  115. package/dist/sse/json-to-sse/index.d.ts +7 -0
  116. package/dist/sse/json-to-sse/index.js +2 -0
  117. package/dist/sse/json-to-sse/sequencers/anthropic-sequencer.d.ts +13 -0
  118. package/dist/sse/json-to-sse/sequencers/anthropic-sequencer.js +150 -0
  119. package/dist/sse/json-to-sse/sequencers/chat-sequencer.d.ts +39 -0
  120. package/dist/sse/json-to-sse/sequencers/chat-sequencer.js +49 -3
  121. package/dist/sse/json-to-sse/sequencers/gemini-sequencer.d.ts +10 -0
  122. package/dist/sse/json-to-sse/sequencers/gemini-sequencer.js +95 -0
  123. package/dist/sse/json-to-sse/sequencers/responses-sequencer.js +31 -5
  124. package/dist/sse/registry/sse-codec-registry.d.ts +32 -0
  125. package/dist/sse/registry/sse-codec-registry.js +30 -1
  126. package/dist/sse/shared/reasoning-dispatcher.d.ts +10 -0
  127. package/dist/sse/shared/reasoning-dispatcher.js +25 -0
  128. package/dist/sse/shared/responses-output-normalizer.d.ts +12 -0
  129. package/dist/sse/shared/responses-output-normalizer.js +45 -0
  130. package/dist/sse/shared/serializers/anthropic-event-serializer.d.ts +2 -0
  131. package/dist/sse/shared/serializers/anthropic-event-serializer.js +9 -0
  132. package/dist/sse/shared/serializers/gemini-event-serializer.d.ts +2 -0
  133. package/dist/sse/shared/serializers/gemini-event-serializer.js +5 -0
  134. package/dist/sse/shared/serializers/index.d.ts +41 -0
  135. package/dist/sse/shared/serializers/index.js +2 -0
  136. package/dist/sse/shared/writer.d.ts +127 -0
  137. package/dist/sse/shared/writer.js +37 -1
  138. package/dist/sse/sse-to-json/anthropic-sse-to-json-converter.d.ts +11 -0
  139. package/dist/sse/sse-to-json/anthropic-sse-to-json-converter.js +92 -127
  140. package/dist/sse/sse-to-json/builders/anthropic-response-builder.d.ts +16 -0
  141. package/dist/sse/sse-to-json/builders/anthropic-response-builder.js +151 -0
  142. package/dist/sse/sse-to-json/builders/response-builder.d.ts +165 -0
  143. package/dist/sse/sse-to-json/builders/response-builder.js +27 -6
  144. package/dist/sse/sse-to-json/chat-sse-to-json-converter.d.ts +114 -0
  145. package/dist/sse/sse-to-json/chat-sse-to-json-converter.js +79 -3
  146. package/dist/sse/sse-to-json/gemini-sse-to-json-converter.d.ts +13 -0
  147. package/dist/sse/sse-to-json/gemini-sse-to-json-converter.js +160 -0
  148. package/dist/sse/sse-to-json/index.d.ts +7 -0
  149. package/dist/sse/sse-to-json/index.js +2 -0
  150. package/dist/sse/sse-to-json/parsers/sse-parser.js +53 -1
  151. package/dist/sse/types/anthropic-types.d.ts +170 -0
  152. package/dist/sse/types/anthropic-types.js +8 -5
  153. package/dist/sse/types/chat-types.d.ts +10 -0
  154. package/dist/sse/types/chat-types.js +2 -1
  155. package/dist/sse/types/core-interfaces.d.ts +1 -1
  156. package/dist/sse/types/gemini-types.d.ts +116 -0
  157. package/dist/sse/types/gemini-types.js +5 -0
  158. package/dist/sse/types/index.d.ts +5 -2
  159. package/dist/sse/types/index.js +2 -0
  160. package/package.json +1 -1
@@ -0,0 +1,90 @@
1
+ import { normalizeFunctionCallId, normalizeFunctionCallOutputId } from './bridge-id-utils.js';
2
+ export function createToolCallIdTransformer(style) {
3
+ if (style !== 'fc') {
4
+ return null;
5
+ }
6
+ let callCounter = 0;
7
+ let outputCounter = 0;
8
+ const alias = new Map();
9
+ const normalizeCallId = (raw) => {
10
+ const rawStr = typeof raw === 'string' && raw.trim().length ? raw.trim() : undefined;
11
+ if (rawStr && alias.has(rawStr)) {
12
+ return alias.get(rawStr);
13
+ }
14
+ const normalized = normalizeFunctionCallId({
15
+ callId: rawStr,
16
+ fallback: `fc_call_${++callCounter}`
17
+ });
18
+ if (rawStr) {
19
+ alias.set(rawStr, normalized);
20
+ }
21
+ return normalized;
22
+ };
23
+ const normalizeOutputId = (callId, raw) => {
24
+ const rawStr = typeof raw === 'string' && raw.trim().length ? raw.trim() : undefined;
25
+ return normalizeFunctionCallOutputId({
26
+ callId,
27
+ fallback: rawStr ?? `fc_tool_${++outputCounter}`
28
+ });
29
+ };
30
+ return {
31
+ normalizeCallId,
32
+ normalizeOutputId
33
+ };
34
+ }
35
+ export function enforceToolCallIdStyle(input, transformer) {
36
+ for (const entry of input) {
37
+ if (!entry || typeof entry !== 'object')
38
+ continue;
39
+ const type = typeof entry.type === 'string' ? entry.type.toLowerCase() : '';
40
+ if (type === 'function_call') {
41
+ const normalized = transformer.normalizeCallId(entry.call_id ?? entry.id);
42
+ entry.call_id = normalized;
43
+ entry.id = normalized;
44
+ continue;
45
+ }
46
+ if (type === 'function_call_output' || type === 'tool_result' || type === 'tool_message') {
47
+ const normalized = transformer.normalizeCallId(entry.call_id ?? entry.id);
48
+ entry.call_id = normalized;
49
+ entry.id = transformer.normalizeOutputId(normalized, entry.id);
50
+ }
51
+ }
52
+ }
53
+ export function resolveToolCallIdStyle(metadata) {
54
+ if (!metadata)
55
+ return 'fc';
56
+ const raw = metadata.toolCallIdStyle;
57
+ if (typeof raw === 'string') {
58
+ const lowered = raw.trim().toLowerCase();
59
+ if (lowered === 'fc') {
60
+ return 'fc';
61
+ }
62
+ if (lowered === 'preserve') {
63
+ return 'preserve';
64
+ }
65
+ }
66
+ return 'fc';
67
+ }
68
+ export function stripInternalToolingMetadata(metadata) {
69
+ if (!metadata || typeof metadata !== 'object')
70
+ return;
71
+ if ('toolCallIdStyle' in metadata) {
72
+ delete metadata.toolCallIdStyle;
73
+ }
74
+ }
75
+ export function sanitizeResponsesFunctionName(rawName) {
76
+ if (typeof rawName !== 'string') {
77
+ return undefined;
78
+ }
79
+ const trimmed = rawName.trim();
80
+ if (!trimmed) {
81
+ return undefined;
82
+ }
83
+ const dotIndex = trimmed.indexOf('.');
84
+ const base = dotIndex >= 0 ? trimmed.slice(dotIndex + 1) : trimmed;
85
+ const sanitized = base.replace(/[^A-Za-z0-9_-]/g, '_');
86
+ if (sanitized.length > 0) {
87
+ return sanitized;
88
+ }
89
+ return 'tool_call';
90
+ }
@@ -0,0 +1,33 @@
1
+ export type ResponsesContentPart = {
2
+ type: string;
3
+ text?: string;
4
+ content?: unknown;
5
+ };
6
+ export type ResponsesInputItem = {
7
+ type: string;
8
+ role?: string;
9
+ content?: Array<ResponsesContentPart> | null;
10
+ name?: string;
11
+ arguments?: unknown;
12
+ call_id?: string;
13
+ output?: unknown;
14
+ function?: {
15
+ name?: string;
16
+ arguments?: unknown;
17
+ };
18
+ message?: {
19
+ role?: string;
20
+ content?: Array<ResponsesContentPart>;
21
+ };
22
+ id?: string;
23
+ tool_call_id?: string;
24
+ tool_use_id?: string;
25
+ text?: string;
26
+ };
27
+ export type ResponsesToolDefinition = {
28
+ type: string;
29
+ name?: string;
30
+ description?: string;
31
+ strict?: boolean;
32
+ parameters?: unknown;
33
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,11 @@
1
+ type UnknownRecord = Record<string, unknown>;
2
+ /**
3
+ * Derive a deterministic key for a tool call by combining the normalized
4
+ * function name and serialized arguments. Used to deduplicate inferred calls.
5
+ */
6
+ export declare function deriveToolCallKey(call: UnknownRecord | null | undefined): string | null;
7
+ /**
8
+ * Merge tool call entries with deduplication (by derived key). Returns the new array.
9
+ */
10
+ export declare function mergeToolCalls(existing: UnknownRecord[] | undefined, additions: UnknownRecord[] | undefined): UnknownRecord[];
11
+ export {};
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Derive a deterministic key for a tool call by combining the normalized
3
+ * function name and serialized arguments. Used to deduplicate inferred calls.
4
+ */
5
+ export function deriveToolCallKey(call) {
6
+ if (!call || typeof call !== 'object') {
7
+ return null;
8
+ }
9
+ const fn = call.function;
10
+ const name = typeof fn?.name === 'string' ? fn.name.trim() : '';
11
+ const argsRaw = fn?.arguments;
12
+ let args = '';
13
+ if (typeof argsRaw === 'string') {
14
+ args = argsRaw.trim();
15
+ }
16
+ else if (argsRaw !== undefined) {
17
+ try {
18
+ args = JSON.stringify(argsRaw);
19
+ }
20
+ catch {
21
+ args = '';
22
+ }
23
+ }
24
+ if (!name && !args) {
25
+ return null;
26
+ }
27
+ return `${name}::${args}`;
28
+ }
29
+ /**
30
+ * Merge tool call entries with deduplication (by derived key). Returns the new array.
31
+ */
32
+ export function mergeToolCalls(existing, additions) {
33
+ const base = Array.isArray(existing) ? [...existing] : [];
34
+ const next = Array.isArray(additions) ? additions : [];
35
+ if (!next.length) {
36
+ return base;
37
+ }
38
+ const seen = new Set();
39
+ for (const call of base) {
40
+ const key = deriveToolCallKey(call);
41
+ if (key) {
42
+ seen.add(key);
43
+ }
44
+ }
45
+ for (const call of next) {
46
+ const key = deriveToolCallKey(call);
47
+ if (key && seen.has(key)) {
48
+ continue;
49
+ }
50
+ base.push(call);
51
+ if (key) {
52
+ seen.add(key);
53
+ }
54
+ }
55
+ return base;
56
+ }
@@ -1,5 +1,8 @@
1
1
  // Unified tool governance API (标准)
2
2
  // Centralizes tool augmentation, guidance injection/refinement, and textual→tool_calls canonicalization
3
+ // canonicalizer 按需加载(避免在请求侧仅注入时引入不必要的模块)
4
+ // enforceChatBudget: 为避免在请求侧引入多余依赖,这里提供最小实现(保留形状,不裁剪)。
5
+ import { augmentOpenAITools } from '../../guidance/index.js';
3
6
  function isObject(v) { return !!v && typeof v === 'object' && !Array.isArray(v); }
4
7
  // Note: tool schema strict augmentation removed per alignment
5
8
  function enforceChatBudget(chat, _modelId) { return chat; }
@@ -63,6 +66,8 @@ export function processChatRequestTools(request, opts) {
63
66
  t.function = { ...fn, parameters: {} };
64
67
  }
65
68
  }
69
+ // 严格化工具 schema(apply_patch/shell/MCP 等)保持在唯一治理点,避免重复注入
70
+ out.tools = augmentOpenAITools(tools);
66
71
  }
67
72
  }
68
73
  catch { /* best-effort: 保持原样 */ }
@@ -0,0 +1,19 @@
1
+ import type { ChatToolDefinition } from '../hub/types/chat-envelope.js';
2
+ import type { BridgeToolDefinition } from './bridge-message-types.js';
3
+ export interface ToolCallFunction {
4
+ name: string;
5
+ arguments: string;
6
+ }
7
+ export interface ToolCallItem {
8
+ id?: string;
9
+ type: 'function';
10
+ function: ToolCallFunction;
11
+ }
12
+ export declare function stringifyArgs(args: unknown): string;
13
+ export interface BridgeToolMapOptions {
14
+ sanitizeName?: (raw: unknown) => string | undefined;
15
+ }
16
+ export declare function bridgeToolToChatDefinition(rawTool: BridgeToolDefinition | Record<string, unknown> | null | undefined, options?: BridgeToolMapOptions): ChatToolDefinition | null;
17
+ export declare function mapBridgeToolsToChat(rawTools: unknown, options?: BridgeToolMapOptions): ChatToolDefinition[] | undefined;
18
+ export declare function chatToolToBridgeDefinition(rawTool: ChatToolDefinition | Record<string, unknown> | null | undefined, options?: BridgeToolMapOptions): BridgeToolDefinition | null;
19
+ export declare function mapChatToolsToBridge(rawTools: unknown, options?: BridgeToolMapOptions): BridgeToolDefinition[] | undefined;
@@ -8,3 +8,127 @@ export function stringifyArgs(args) {
8
8
  return String(args);
9
9
  }
10
10
  }
11
+ const DEFAULT_SANITIZER = (value) => {
12
+ if (typeof value === 'string') {
13
+ const trimmed = value.trim();
14
+ return trimmed.length ? trimmed : undefined;
15
+ }
16
+ return undefined;
17
+ };
18
+ function resolveToolName(candidate, options) {
19
+ const sanitized = options?.sanitizeName?.(candidate);
20
+ if (typeof sanitized === 'string' && sanitized.trim().length) {
21
+ return sanitized.trim();
22
+ }
23
+ return DEFAULT_SANITIZER(candidate);
24
+ }
25
+ function pickToolName(candidates, options) {
26
+ for (const candidate of candidates) {
27
+ const name = resolveToolName(candidate, options);
28
+ if (name) {
29
+ return name;
30
+ }
31
+ }
32
+ return undefined;
33
+ }
34
+ function resolveToolDescription(candidate) {
35
+ if (typeof candidate === 'string' && candidate.trim().length) {
36
+ return candidate.trim();
37
+ }
38
+ return undefined;
39
+ }
40
+ function resolveToolParameters(container, fallback) {
41
+ if (container && Object.prototype.hasOwnProperty.call(container, 'parameters')) {
42
+ return container.parameters;
43
+ }
44
+ if (fallback && Object.prototype.hasOwnProperty.call(fallback, 'parameters')) {
45
+ return fallback.parameters;
46
+ }
47
+ return undefined;
48
+ }
49
+ function resolveToolStrict(container, fallback) {
50
+ if (container && typeof container.strict === 'boolean') {
51
+ return container.strict;
52
+ }
53
+ if (fallback && typeof fallback.strict === 'boolean') {
54
+ return fallback.strict;
55
+ }
56
+ return undefined;
57
+ }
58
+ export function bridgeToolToChatDefinition(rawTool, options) {
59
+ if (!rawTool || typeof rawTool !== 'object') {
60
+ return null;
61
+ }
62
+ const tool = rawTool;
63
+ const fnNode = tool.function && typeof tool.function === 'object' ? tool.function : undefined;
64
+ const name = pickToolName([fnNode?.name, tool.name], options);
65
+ if (!name) {
66
+ return null;
67
+ }
68
+ const description = resolveToolDescription(fnNode?.description ?? tool.description);
69
+ const parameters = resolveToolParameters(fnNode, tool);
70
+ const strict = resolveToolStrict(fnNode, tool);
71
+ const rawType = typeof tool.type === 'string' && tool.type.trim().length ? tool.type.trim() : 'function';
72
+ const normalizedType = rawType.toLowerCase() === 'custom' ? 'function' : rawType;
73
+ const fnOut = { name };
74
+ if (description !== undefined) {
75
+ fnOut.description = description;
76
+ }
77
+ if (parameters !== undefined) {
78
+ fnOut.parameters = parameters;
79
+ }
80
+ if (strict !== undefined) {
81
+ fnOut.strict = strict;
82
+ }
83
+ return {
84
+ type: normalizedType,
85
+ function: fnOut
86
+ };
87
+ }
88
+ export function mapBridgeToolsToChat(rawTools, options) {
89
+ if (!Array.isArray(rawTools) || rawTools.length === 0) {
90
+ return undefined;
91
+ }
92
+ const mapped = rawTools
93
+ .map((entry) => bridgeToolToChatDefinition(entry, options))
94
+ .filter((entry) => !!entry);
95
+ return mapped.length ? mapped : undefined;
96
+ }
97
+ export function chatToolToBridgeDefinition(rawTool, options) {
98
+ if (!rawTool || typeof rawTool !== 'object') {
99
+ return null;
100
+ }
101
+ const tool = rawTool;
102
+ const fnNode = tool.function && typeof tool.function === 'object' ? tool.function : undefined;
103
+ const name = pickToolName([fnNode?.name, tool.name], options);
104
+ if (!name) {
105
+ return null;
106
+ }
107
+ const description = resolveToolDescription(fnNode?.description);
108
+ const parameters = resolveToolParameters(fnNode, undefined);
109
+ const strict = resolveToolStrict(fnNode, undefined);
110
+ const normalizedType = typeof tool.type === 'string' && tool.type.trim().length ? tool.type.trim() : 'function';
111
+ const responseShape = {
112
+ type: normalizedType,
113
+ name
114
+ };
115
+ if (description !== undefined) {
116
+ responseShape.description = description;
117
+ }
118
+ if (parameters !== undefined) {
119
+ responseShape.parameters = parameters;
120
+ }
121
+ if (strict !== undefined) {
122
+ responseShape.strict = strict;
123
+ }
124
+ return responseShape;
125
+ }
126
+ export function mapChatToolsToBridge(rawTools, options) {
127
+ if (!Array.isArray(rawTools) || rawTools.length === 0) {
128
+ return undefined;
129
+ }
130
+ const mapped = rawTools
131
+ .map((entry) => chatToolToBridgeDefinition(entry, options))
132
+ .filter((entry) => !!entry);
133
+ return mapped.length ? mapped : undefined;
134
+ }
@@ -0,0 +1,4 @@
1
+ import type { ChatToolDefinition, MissingField } from '../hub/types/chat-envelope.js';
2
+ import type { JsonValue } from '../hub/types/json.js';
3
+ export declare function normalizeAnthropicTools(rawTools: JsonValue | undefined, missing?: MissingField[]): ChatToolDefinition[] | undefined;
4
+ export declare function normalizeGeminiTools(rawTools: JsonValue | undefined, missing?: MissingField[]): ChatToolDefinition[] | undefined;
@@ -0,0 +1,84 @@
1
+ import { isJsonObject, jsonClone } from '../hub/types/json.js';
2
+ export function normalizeAnthropicTools(rawTools, missing) {
3
+ if (!Array.isArray(rawTools) || rawTools.length === 0)
4
+ return undefined;
5
+ const tools = [];
6
+ rawTools.forEach((entry, index) => {
7
+ if (!entry || typeof entry !== 'object' || Array.isArray(entry)) {
8
+ missing?.push({
9
+ path: `tools[${index}]`,
10
+ reason: 'invalid_entry',
11
+ originalValue: jsonClone(entry)
12
+ });
13
+ return;
14
+ }
15
+ const nameValue = entry.name;
16
+ if (typeof nameValue !== 'string' || !nameValue.trim())
17
+ return;
18
+ const desc = entry.description;
19
+ const schema = entry.input_schema;
20
+ const parameters = schema && typeof schema === 'object' && !Array.isArray(schema)
21
+ ? schema
22
+ : { type: 'object', properties: {} };
23
+ tools.push({
24
+ type: 'function',
25
+ function: {
26
+ name: nameValue,
27
+ description: typeof desc === 'string' ? desc : undefined,
28
+ parameters
29
+ }
30
+ });
31
+ });
32
+ return tools.length ? tools : undefined;
33
+ }
34
+ export function normalizeGeminiTools(rawTools, missing) {
35
+ if (!rawTools)
36
+ return undefined;
37
+ const arr = Array.isArray(rawTools) ? rawTools : [rawTools];
38
+ const tools = [];
39
+ arr.forEach((entry, index) => {
40
+ if (!entry || typeof entry !== 'object') {
41
+ missing?.push({
42
+ path: `tools[${index}]`,
43
+ reason: 'invalid_entry',
44
+ originalValue: jsonClone(entry)
45
+ });
46
+ return;
47
+ }
48
+ const obj = entry;
49
+ const declarations = Array.isArray(obj.functionDeclarations) ? obj.functionDeclarations : undefined;
50
+ if (declarations && declarations.length) {
51
+ declarations.forEach((decl, declIndex) => {
52
+ if (!decl || typeof decl !== 'object') {
53
+ missing?.push({ path: `tools[${index}].functionDeclarations[${declIndex}]`, reason: 'invalid_entry' });
54
+ return;
55
+ }
56
+ const def = decl;
57
+ const name = typeof def.name === 'string' ? def.name : undefined;
58
+ if (!name)
59
+ return;
60
+ tools.push({
61
+ type: 'function',
62
+ function: {
63
+ name,
64
+ description: typeof def.description === 'string' ? def.description : undefined,
65
+ parameters: isJsonObject(def.parameters) ? jsonClone(def.parameters) : { type: 'object', properties: {} }
66
+ }
67
+ });
68
+ });
69
+ return;
70
+ }
71
+ const name = typeof obj.name === 'string' ? obj.name : undefined;
72
+ if (!name)
73
+ return;
74
+ tools.push({
75
+ type: 'function',
76
+ function: {
77
+ name,
78
+ description: typeof obj.description === 'string' ? obj.description : undefined,
79
+ parameters: isJsonObject(obj.parameters) ? jsonClone(obj.parameters) : { type: 'object', properties: {} }
80
+ }
81
+ });
82
+ });
83
+ return tools.length ? tools : undefined;
84
+ }
@@ -99,7 +99,8 @@ function buildProviderRuntimeEntries(providers) {
99
99
  auth: runtimeAuth,
100
100
  outboundProfile: normalizedProvider.outboundProfile,
101
101
  compatibilityProfile: normalizedProvider.compatibilityProfile,
102
- processMode: normalizedProvider.processMode
102
+ processMode: normalizedProvider.processMode,
103
+ responsesConfig: normalizedProvider.responsesConfig
103
104
  };
104
105
  }
105
106
  }
@@ -154,7 +155,8 @@ function buildProviderProfiles(targetKeys, runtimeEntries) {
154
155
  compatibilityProfile: runtime.compatibilityProfile,
155
156
  runtimeKey,
156
157
  modelId: parsed.modelId,
157
- processMode: runtime.processMode || 'chat'
158
+ processMode: runtime.processMode || 'chat',
159
+ responsesConfig: runtime.responsesConfig
158
160
  };
159
161
  targetRuntime[targetKey] = {
160
162
  ...runtime,
@@ -207,6 +209,7 @@ function normalizeProvider(providerId, raw) {
207
209
  : '';
208
210
  const headers = normalizeHeaders(provider.headers);
209
211
  const compatibilityProfile = resolveCompatibilityProfile(provider);
212
+ const responsesConfig = normalizeResponsesConfig(provider);
210
213
  const processMode = normalizeProcessMode(provider.process);
211
214
  return {
212
215
  providerId,
@@ -215,9 +218,21 @@ function normalizeProvider(providerId, raw) {
215
218
  headers,
216
219
  outboundProfile: mapOutboundProfile(providerType),
217
220
  compatibilityProfile,
218
- processMode
221
+ processMode,
222
+ responsesConfig
219
223
  };
220
224
  }
225
+ function normalizeResponsesConfig(provider) {
226
+ const node = asRecord(provider.responses);
227
+ if (!node) {
228
+ return undefined;
229
+ }
230
+ const rawStyle = typeof node.toolCallIdStyle === 'string' ? node.toolCallIdStyle.trim().toLowerCase() : undefined;
231
+ if (rawStyle === 'fc' || rawStyle === 'preserve') {
232
+ return { toolCallIdStyle: rawStyle };
233
+ }
234
+ return undefined;
235
+ }
221
236
  function resolveCompatibilityProfile(provider) {
222
237
  const compat = provider.compat;
223
238
  if (typeof compat === 'string' && compat.trim().length > 0) {
@@ -41,7 +41,8 @@ export class ProviderRegistry {
41
41
  compatibilityProfile: profile.compatibilityProfile,
42
42
  runtimeKey: profile.runtimeKey,
43
43
  modelId,
44
- processMode: profile.processMode || 'chat'
44
+ processMode: profile.processMode || 'chat',
45
+ responsesConfig: profile.responsesConfig
45
46
  };
46
47
  }
47
48
  static normalizeProfile(key, profile) {
@@ -56,7 +57,8 @@ export class ProviderRegistry {
56
57
  compatibilityProfile: profile.compatibilityProfile,
57
58
  runtimeKey: profile.runtimeKey,
58
59
  modelId,
59
- processMode: profile.processMode || 'chat'
60
+ processMode: profile.processMode || 'chat',
61
+ responsesConfig: profile.responsesConfig
60
62
  };
61
63
  }
62
64
  }