@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.
- package/dist/conversion/codec-registry.js +11 -1
- package/dist/conversion/codecs/anthropic-openai-codec.d.ts +13 -0
- package/dist/conversion/codecs/anthropic-openai-codec.js +18 -473
- package/dist/conversion/codecs/gemini-openai-codec.js +91 -48
- package/dist/conversion/codecs/responses-openai-codec.js +9 -2
- package/dist/conversion/hub/format-adapters/anthropic-format-adapter.js +3 -0
- package/dist/conversion/hub/format-adapters/chat-format-adapter.js +3 -0
- package/dist/conversion/hub/format-adapters/gemini-format-adapter.js +3 -0
- package/dist/conversion/hub/format-adapters/responses-format-adapter.d.ts +19 -0
- package/dist/conversion/hub/format-adapters/responses-format-adapter.js +9 -0
- package/dist/conversion/hub/node-support.js +3 -1
- package/dist/conversion/hub/pipeline/hub-pipeline.js +37 -32
- package/dist/conversion/hub/response/provider-response.js +1 -1
- package/dist/conversion/hub/response/response-mappers.js +1 -1
- package/dist/conversion/hub/response/response-runtime.js +109 -10
- package/dist/conversion/hub/semantic-mappers/anthropic-mapper.js +70 -156
- package/dist/conversion/hub/semantic-mappers/chat-mapper.js +63 -52
- package/dist/conversion/hub/semantic-mappers/gemini-mapper.js +76 -143
- package/dist/conversion/hub/semantic-mappers/responses-mapper.js +40 -160
- package/dist/conversion/hub/standardized-bridge.js +3 -0
- package/dist/conversion/hub/tool-governance/rules.js +2 -2
- package/dist/conversion/index.d.ts +5 -0
- package/dist/conversion/index.js +5 -0
- package/dist/conversion/pipeline/codecs/v2/anthropic-openai-pipeline.d.ts +12 -0
- package/dist/conversion/pipeline/codecs/v2/anthropic-openai-pipeline.js +100 -0
- package/dist/conversion/pipeline/codecs/v2/openai-openai-pipeline.d.ts +15 -0
- package/dist/conversion/pipeline/codecs/v2/openai-openai-pipeline.js +174 -0
- package/dist/conversion/pipeline/codecs/v2/responses-openai-pipeline.d.ts +14 -0
- package/dist/conversion/pipeline/codecs/v2/responses-openai-pipeline.js +166 -0
- package/dist/conversion/pipeline/codecs/v2/shared/openai-chat-helpers.d.ts +13 -0
- package/dist/conversion/pipeline/codecs/v2/shared/openai-chat-helpers.js +66 -0
- package/dist/conversion/pipeline/hooks/adapter-context.d.ts +7 -0
- package/dist/conversion/pipeline/hooks/adapter-context.js +18 -0
- package/dist/conversion/pipeline/hooks/protocol-hooks.d.ts +67 -0
- package/dist/conversion/pipeline/hooks/protocol-hooks.js +1 -0
- package/dist/conversion/pipeline/index.d.ts +35 -0
- package/dist/conversion/pipeline/index.js +103 -0
- package/dist/conversion/pipeline/meta/meta-bag.d.ts +20 -0
- package/dist/conversion/pipeline/meta/meta-bag.js +81 -0
- package/dist/conversion/pipeline/schema/canonical-chat.d.ts +18 -0
- package/dist/conversion/pipeline/schema/canonical-chat.js +1 -0
- package/dist/conversion/pipeline/schema/index.d.ts +1 -0
- package/dist/conversion/pipeline/schema/index.js +1 -0
- package/dist/conversion/responses/responses-openai-bridge.d.ts +48 -0
- package/dist/conversion/responses/responses-openai-bridge.js +157 -1146
- package/dist/conversion/shared/anthropic-message-utils.d.ts +12 -0
- package/dist/conversion/shared/anthropic-message-utils.js +587 -0
- package/dist/conversion/shared/bridge-actions.d.ts +39 -0
- package/dist/conversion/shared/bridge-actions.js +709 -0
- package/dist/conversion/shared/bridge-conversation-store.d.ts +41 -0
- package/dist/conversion/shared/bridge-conversation-store.js +279 -0
- package/dist/conversion/shared/bridge-id-utils.d.ts +7 -0
- package/dist/conversion/shared/bridge-id-utils.js +42 -0
- package/dist/conversion/shared/bridge-instructions.d.ts +1 -0
- package/dist/conversion/shared/bridge-instructions.js +113 -0
- package/dist/conversion/shared/bridge-message-types.d.ts +39 -0
- package/dist/conversion/shared/bridge-message-types.js +1 -0
- package/dist/conversion/shared/bridge-message-utils.d.ts +22 -0
- package/dist/conversion/shared/bridge-message-utils.js +473 -0
- package/dist/conversion/shared/bridge-metadata.d.ts +1 -0
- package/dist/conversion/shared/bridge-metadata.js +1 -0
- package/dist/conversion/shared/bridge-policies.d.ts +18 -0
- package/dist/conversion/shared/bridge-policies.js +276 -0
- package/dist/conversion/shared/bridge-request-adapter.d.ts +28 -0
- package/dist/conversion/shared/bridge-request-adapter.js +430 -0
- package/dist/conversion/shared/chat-output-normalizer.d.ts +4 -0
- package/dist/conversion/shared/chat-output-normalizer.js +56 -0
- package/dist/conversion/shared/chat-request-filters.js +24 -1
- package/dist/conversion/shared/gemini-tool-utils.d.ts +5 -0
- package/dist/conversion/shared/gemini-tool-utils.js +130 -0
- package/dist/conversion/shared/metadata-passthrough.d.ts +11 -0
- package/dist/conversion/shared/metadata-passthrough.js +57 -0
- package/dist/conversion/shared/output-content-normalizer.d.ts +12 -0
- package/dist/conversion/shared/output-content-normalizer.js +119 -0
- package/dist/conversion/shared/reasoning-normalizer.d.ts +21 -0
- package/dist/conversion/shared/reasoning-normalizer.js +368 -0
- package/dist/conversion/shared/reasoning-tool-normalizer.d.ts +12 -0
- package/dist/conversion/shared/reasoning-tool-normalizer.js +132 -0
- package/dist/conversion/shared/reasoning-tool-parser.d.ts +10 -0
- package/dist/conversion/shared/reasoning-tool-parser.js +95 -0
- package/dist/conversion/shared/reasoning-utils.d.ts +2 -0
- package/dist/conversion/shared/reasoning-utils.js +42 -0
- package/dist/conversion/shared/responses-conversation-store.js +5 -11
- package/dist/conversion/shared/responses-message-utils.d.ts +15 -0
- package/dist/conversion/shared/responses-message-utils.js +206 -0
- package/dist/conversion/shared/responses-output-builder.d.ts +15 -0
- package/dist/conversion/shared/responses-output-builder.js +179 -0
- package/dist/conversion/shared/responses-output-utils.d.ts +7 -0
- package/dist/conversion/shared/responses-output-utils.js +108 -0
- package/dist/conversion/shared/responses-request-adapter.d.ts +28 -0
- package/dist/conversion/shared/responses-request-adapter.js +9 -40
- package/dist/conversion/shared/responses-response-utils.d.ts +3 -0
- package/dist/conversion/shared/responses-response-utils.js +209 -0
- package/dist/conversion/shared/responses-tool-utils.d.ts +12 -0
- package/dist/conversion/shared/responses-tool-utils.js +90 -0
- package/dist/conversion/shared/responses-types.d.ts +33 -0
- package/dist/conversion/shared/responses-types.js +1 -0
- package/dist/conversion/shared/tool-call-utils.d.ts +11 -0
- package/dist/conversion/shared/tool-call-utils.js +56 -0
- package/dist/conversion/shared/tool-governor.js +5 -0
- package/dist/conversion/shared/tool-mapping.d.ts +19 -0
- package/dist/conversion/shared/tool-mapping.js +124 -0
- package/dist/conversion/shared/tool-normalizers.d.ts +4 -0
- package/dist/conversion/shared/tool-normalizers.js +84 -0
- package/dist/router/virtual-router/bootstrap.js +18 -3
- package/dist/router/virtual-router/provider-registry.js +4 -2
- package/dist/router/virtual-router/types.d.ts +212 -0
- package/dist/sse/index.d.ts +38 -2
- package/dist/sse/index.js +27 -0
- package/dist/sse/json-to-sse/anthropic-json-to-sse-converter.d.ts +14 -0
- package/dist/sse/json-to-sse/anthropic-json-to-sse-converter.js +106 -73
- package/dist/sse/json-to-sse/chat-json-to-sse-converter.js +6 -2
- package/dist/sse/json-to-sse/gemini-json-to-sse-converter.d.ts +14 -0
- package/dist/sse/json-to-sse/gemini-json-to-sse-converter.js +99 -0
- package/dist/sse/json-to-sse/index.d.ts +7 -0
- package/dist/sse/json-to-sse/index.js +2 -0
- package/dist/sse/json-to-sse/sequencers/anthropic-sequencer.d.ts +13 -0
- package/dist/sse/json-to-sse/sequencers/anthropic-sequencer.js +150 -0
- package/dist/sse/json-to-sse/sequencers/chat-sequencer.d.ts +39 -0
- package/dist/sse/json-to-sse/sequencers/chat-sequencer.js +49 -3
- package/dist/sse/json-to-sse/sequencers/gemini-sequencer.d.ts +10 -0
- package/dist/sse/json-to-sse/sequencers/gemini-sequencer.js +95 -0
- package/dist/sse/json-to-sse/sequencers/responses-sequencer.js +31 -5
- package/dist/sse/registry/sse-codec-registry.d.ts +32 -0
- package/dist/sse/registry/sse-codec-registry.js +30 -1
- package/dist/sse/shared/reasoning-dispatcher.d.ts +10 -0
- package/dist/sse/shared/reasoning-dispatcher.js +25 -0
- package/dist/sse/shared/responses-output-normalizer.d.ts +12 -0
- package/dist/sse/shared/responses-output-normalizer.js +45 -0
- package/dist/sse/shared/serializers/anthropic-event-serializer.d.ts +2 -0
- package/dist/sse/shared/serializers/anthropic-event-serializer.js +9 -0
- package/dist/sse/shared/serializers/gemini-event-serializer.d.ts +2 -0
- package/dist/sse/shared/serializers/gemini-event-serializer.js +5 -0
- package/dist/sse/shared/serializers/index.d.ts +41 -0
- package/dist/sse/shared/serializers/index.js +2 -0
- package/dist/sse/shared/writer.d.ts +127 -0
- package/dist/sse/shared/writer.js +37 -1
- package/dist/sse/sse-to-json/anthropic-sse-to-json-converter.d.ts +11 -0
- package/dist/sse/sse-to-json/anthropic-sse-to-json-converter.js +92 -127
- package/dist/sse/sse-to-json/builders/anthropic-response-builder.d.ts +16 -0
- package/dist/sse/sse-to-json/builders/anthropic-response-builder.js +151 -0
- package/dist/sse/sse-to-json/builders/response-builder.d.ts +165 -0
- package/dist/sse/sse-to-json/builders/response-builder.js +27 -6
- package/dist/sse/sse-to-json/chat-sse-to-json-converter.d.ts +114 -0
- package/dist/sse/sse-to-json/chat-sse-to-json-converter.js +79 -3
- package/dist/sse/sse-to-json/gemini-sse-to-json-converter.d.ts +13 -0
- package/dist/sse/sse-to-json/gemini-sse-to-json-converter.js +160 -0
- package/dist/sse/sse-to-json/index.d.ts +7 -0
- package/dist/sse/sse-to-json/index.js +2 -0
- package/dist/sse/sse-to-json/parsers/sse-parser.js +53 -1
- package/dist/sse/types/anthropic-types.d.ts +170 -0
- package/dist/sse/types/anthropic-types.js +8 -5
- package/dist/sse/types/chat-types.d.ts +10 -0
- package/dist/sse/types/chat-types.js +2 -1
- package/dist/sse/types/core-interfaces.d.ts +1 -1
- package/dist/sse/types/gemini-types.d.ts +116 -0
- package/dist/sse/types/gemini-types.js +5 -0
- package/dist/sse/types/index.d.ts +5 -2
- package/dist/sse/types/index.js +2 -0
- package/package.json +1 -1
|
@@ -0,0 +1,430 @@
|
|
|
1
|
+
import { normalizeFunctionCallId, normalizeFunctionCallOutputId } from './bridge-id-utils.js';
|
|
2
|
+
import { ensureBridgeInstructions } from './bridge-instructions.js';
|
|
3
|
+
import { serializeToolArguments, coerceBridgeRole, serializeToolOutput } from './bridge-message-utils.js';
|
|
4
|
+
export function captureResponsesContext(payload, dto) {
|
|
5
|
+
ensureBridgeInstructions(payload);
|
|
6
|
+
const context = {
|
|
7
|
+
requestId: dto?.route?.requestId,
|
|
8
|
+
instructions: typeof payload.instructions === 'string' ? payload.instructions : undefined,
|
|
9
|
+
input: Array.isArray(payload.input) ? payload.input : undefined,
|
|
10
|
+
include: payload.include,
|
|
11
|
+
store: payload.store,
|
|
12
|
+
toolChoice: payload.tool_choice,
|
|
13
|
+
parallelToolCalls: typeof payload.parallel_tool_calls === 'boolean' ? payload.parallel_tool_calls : undefined,
|
|
14
|
+
metadata: (payload.metadata && typeof payload.metadata === 'object') ? { ...payload.metadata } : undefined,
|
|
15
|
+
responseFormat: payload.response_format,
|
|
16
|
+
stream: typeof payload.stream === 'boolean' ? payload.stream : undefined,
|
|
17
|
+
instructionsIsRaw: payload.instructions_is_raw === true,
|
|
18
|
+
isChatPayload: Array.isArray(payload.messages)
|
|
19
|
+
};
|
|
20
|
+
if (Array.isArray(payload.tools)) {
|
|
21
|
+
context.toolsRaw = payload.tools;
|
|
22
|
+
}
|
|
23
|
+
context.isResponsesPayload = !context.isChatPayload && Array.isArray(context.input);
|
|
24
|
+
return context;
|
|
25
|
+
}
|
|
26
|
+
export function buildChatRequestFromResponses(payload, context) {
|
|
27
|
+
const toolOutputs = Array.isArray(payload.tool_outputs)
|
|
28
|
+
? payload.tool_outputs
|
|
29
|
+
: undefined;
|
|
30
|
+
const responseId = typeof payload.response_id === 'string' ? payload.response_id : undefined;
|
|
31
|
+
const hasInput = Array.isArray(payload.input) && payload.input.length > 0;
|
|
32
|
+
const submitOnlyMode = !!toolOutputs && !!responseId && !hasInput;
|
|
33
|
+
if (submitOnlyMode) {
|
|
34
|
+
return {
|
|
35
|
+
request: {
|
|
36
|
+
type: 'submit_tool_outputs',
|
|
37
|
+
model: payload.model,
|
|
38
|
+
response_id: responseId,
|
|
39
|
+
tool_outputs: toolOutputs,
|
|
40
|
+
stream: payload.stream === true
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
let toolsNormalized = Array.isArray(payload.tools)
|
|
45
|
+
? normalizeTools(payload.tools)
|
|
46
|
+
: undefined;
|
|
47
|
+
// Inject MCP tools (CCR style) if enabled
|
|
48
|
+
try {
|
|
49
|
+
const enableMcp = String(process.env?.ROUTECODEX_MCP_ENABLE ?? '1') !== '0';
|
|
50
|
+
if (enableMcp) {
|
|
51
|
+
// Discover servers from prior inputs if present (best-effort; depends on client payload)
|
|
52
|
+
const discovered = new Set();
|
|
53
|
+
try {
|
|
54
|
+
const input = Array.isArray(payload.input) ? payload.input : [];
|
|
55
|
+
for (const it of input) {
|
|
56
|
+
const t = (it && typeof it === 'object') ? it : {};
|
|
57
|
+
const isToolCall = (t.type === 'function_call' || t.type === 'tool_call');
|
|
58
|
+
const isToolMsg = (t.type === 'tool_message' || t.type === 'tool_result');
|
|
59
|
+
if (isToolCall) {
|
|
60
|
+
const args = typeof t.arguments === 'string' ? (() => { try {
|
|
61
|
+
return JSON.parse(t.arguments);
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return {};
|
|
65
|
+
} })() : (t.arguments || {});
|
|
66
|
+
const sv = (args && typeof args === 'object') ? args.server : undefined;
|
|
67
|
+
if (typeof sv === 'string' && sv.trim())
|
|
68
|
+
discovered.add(sv.trim());
|
|
69
|
+
}
|
|
70
|
+
else if (isToolMsg) {
|
|
71
|
+
const output = t?.output;
|
|
72
|
+
const val = (typeof output === 'string') ? (() => { try {
|
|
73
|
+
return JSON.parse(output);
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
return null;
|
|
77
|
+
} })() : (output || null);
|
|
78
|
+
const args = (val && typeof val === 'object') ? val.arguments : undefined;
|
|
79
|
+
const sv = (args && typeof args === 'object') ? args.server : undefined;
|
|
80
|
+
if (typeof sv === 'string' && sv.trim())
|
|
81
|
+
discovered.add(sv.trim());
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
catch { /* ignore */ }
|
|
86
|
+
const list = toolsNormalized && Array.isArray(toolsNormalized) ? toolsNormalized : [];
|
|
87
|
+
const have = new Set(list.map((t) => (t?.function?.name || '').toString()));
|
|
88
|
+
const addTool = (def) => { if (!have.has(def.function.name)) {
|
|
89
|
+
list.push(def);
|
|
90
|
+
have.add(def.function.name);
|
|
91
|
+
} };
|
|
92
|
+
const obj = (props, req) => ({ type: 'object', properties: props, required: req, additionalProperties: false });
|
|
93
|
+
const serversRaw = String(process.env?.ROUTECODEX_MCP_SERVERS || '').trim();
|
|
94
|
+
const envServers = serversRaw ? serversRaw.split(',').map((s) => s.trim()).filter(Boolean) : [];
|
|
95
|
+
const merged = Array.from(new Set([...envServers, ...Array.from(discovered)]));
|
|
96
|
+
const serverProp = merged.length ? { type: 'string', enum: merged } : { type: 'string' };
|
|
97
|
+
// 初始仅注入 list_mcp_resources;有已知 server 后再注入其余 MCP 工具
|
|
98
|
+
// 与 Chat 路径保持一致:server 可选(不强制要求)
|
|
99
|
+
addTool({ type: 'function', function: { name: 'list_mcp_resources', strict: true, description: 'List resources from a given MCP server (arguments.server = server label).', parameters: obj({ server: serverProp, filter: { type: 'string' }, root: { type: 'string' } }, [] /* server optional */) } });
|
|
100
|
+
if (merged.length > 0) {
|
|
101
|
+
addTool({ type: 'function', function: { name: 'read_mcp_resource', strict: true, description: 'Read a resource via MCP server.', parameters: obj({ server: serverProp, uri: { type: 'string' } }, ['server', 'uri']) } });
|
|
102
|
+
addTool({ type: 'function', function: { name: 'list_mcp_resource_templates', strict: true, description: 'List resource templates via MCP server.', parameters: obj({ server: serverProp }, ['server']) } });
|
|
103
|
+
}
|
|
104
|
+
toolsNormalized = list;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
catch { /* ignore MCP injection */ }
|
|
108
|
+
const mergedInput = mergeToolOutputsIntoInput(context.input, payload.tool_outputs);
|
|
109
|
+
let messages = mapResponsesInputToChat({
|
|
110
|
+
instructions: context.instructions,
|
|
111
|
+
input: mergedInput,
|
|
112
|
+
toolsNormalized
|
|
113
|
+
});
|
|
114
|
+
const preservedSystemMessages = Array.isArray(context.originalSystemMessages)
|
|
115
|
+
? context.originalSystemMessages
|
|
116
|
+
.map((text) => (typeof text === 'string' ? text.trim() : ''))
|
|
117
|
+
.filter((text) => text.length > 0)
|
|
118
|
+
.map((text) => ({ role: 'system', content: text }))
|
|
119
|
+
: undefined;
|
|
120
|
+
if (preservedSystemMessages && preservedSystemMessages.length) {
|
|
121
|
+
const nonSystemMessages = messages.filter((msg) => String(msg?.role || '').toLowerCase() !== 'system');
|
|
122
|
+
messages = [...preservedSystemMessages, ...nonSystemMessages];
|
|
123
|
+
}
|
|
124
|
+
const request = {
|
|
125
|
+
model: payload.model || 'gpt-4.1-mini',
|
|
126
|
+
messages
|
|
127
|
+
};
|
|
128
|
+
for (const key of ['temperature', 'top_p', 'max_tokens', 'tool_choice', 'parallel_tool_calls', 'user', 'logit_bias', 'seed', 'response_format']) {
|
|
129
|
+
if (payload[key] !== undefined) {
|
|
130
|
+
request[key] = payload[key];
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (Array.isArray(toolsNormalized) && toolsNormalized.length) {
|
|
134
|
+
request.tools = toolsNormalized;
|
|
135
|
+
}
|
|
136
|
+
if (typeof payload.stream === 'boolean') {
|
|
137
|
+
request.stream = payload.stream;
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
request,
|
|
141
|
+
toolsNormalized
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
function mergeToolOutputsIntoInput(input, toolOutputsRaw) {
|
|
145
|
+
const result = Array.isArray(input) ? [...input] : [];
|
|
146
|
+
if (!Array.isArray(toolOutputsRaw)) {
|
|
147
|
+
return result;
|
|
148
|
+
}
|
|
149
|
+
for (const entry of toolOutputsRaw) {
|
|
150
|
+
if (!entry || typeof entry !== 'object')
|
|
151
|
+
continue;
|
|
152
|
+
const data = entry;
|
|
153
|
+
const callIdRaw = data.tool_call_id ?? data.call_id ?? data.id;
|
|
154
|
+
if (typeof callIdRaw !== 'string' || !callIdRaw.trim())
|
|
155
|
+
continue;
|
|
156
|
+
const outputRaw = data.output;
|
|
157
|
+
const outputText = normalizeToolOutputPayload(outputRaw);
|
|
158
|
+
const normalizedCallId = normalizeFunctionCallId({ callId: callIdRaw, fallback: callIdRaw });
|
|
159
|
+
const outputId = normalizeFunctionCallOutputId({ callId: normalizedCallId, fallback: normalizedCallId });
|
|
160
|
+
result.push({
|
|
161
|
+
type: 'function_call_output',
|
|
162
|
+
id: outputId,
|
|
163
|
+
call_id: normalizedCallId,
|
|
164
|
+
output: outputText
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
return result;
|
|
168
|
+
}
|
|
169
|
+
function normalizeToolOutputPayload(payload) {
|
|
170
|
+
if (typeof payload === 'string') {
|
|
171
|
+
return payload;
|
|
172
|
+
}
|
|
173
|
+
if (payload == null) {
|
|
174
|
+
return '';
|
|
175
|
+
}
|
|
176
|
+
try {
|
|
177
|
+
return JSON.stringify(payload);
|
|
178
|
+
}
|
|
179
|
+
catch {
|
|
180
|
+
return String(payload);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
function mapResponsesInputToChat(options) {
|
|
184
|
+
const { instructions, input, toolsNormalized } = options;
|
|
185
|
+
const messages = [];
|
|
186
|
+
if (typeof instructions === 'string' && instructions.length) {
|
|
187
|
+
messages.push({ role: 'system', content: instructions });
|
|
188
|
+
}
|
|
189
|
+
if (!Array.isArray(input))
|
|
190
|
+
return messages;
|
|
191
|
+
const toolNameById = new Map();
|
|
192
|
+
const toolCallIdAliases = new Map();
|
|
193
|
+
let lastToolCallId = null;
|
|
194
|
+
for (const entry of input) {
|
|
195
|
+
if (!entry || typeof entry !== 'object')
|
|
196
|
+
continue;
|
|
197
|
+
const entryType = typeof entry.type === 'string' ? entry.type.toLowerCase() : 'message';
|
|
198
|
+
if (typeof entry.content === 'string') {
|
|
199
|
+
const directText = (entry.content || '').toString().trim();
|
|
200
|
+
if (directText.length) {
|
|
201
|
+
const normalizedRole = coerceBridgeRole(entry.role || 'user');
|
|
202
|
+
messages.push({ role: normalizedRole, content: directText });
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if (entryType === 'function_call' || entryType === 'tool_call') {
|
|
207
|
+
const rawName = typeof entry.name === 'string' ? entry.name : (typeof entry?.function?.name === 'string' ? entry.function.name : undefined);
|
|
208
|
+
const name = (typeof rawName === 'string' && rawName.includes('.')) ? rawName.slice(rawName.indexOf('.') + 1).trim() : rawName;
|
|
209
|
+
if (typeof name !== 'string' || !name.trim() || name.toLowerCase() === 'tool') {
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
const args = entry.arguments ?? entry?.function?.arguments ?? {};
|
|
213
|
+
const parsedArgs = args;
|
|
214
|
+
const callIdRaw = typeof entry.id === 'string' ? entry.id : (typeof entry.call_id === 'string' ? entry.call_id : `call_${Math.random().toString(36).slice(2, 8)}`);
|
|
215
|
+
const callId = normalizeFunctionCallId({ callId: callIdRaw, fallback: `fc_call_${messages.length + 1}` });
|
|
216
|
+
if (callIdRaw && callIdRaw !== callId) {
|
|
217
|
+
toolCallIdAliases.set(callIdRaw, callId);
|
|
218
|
+
}
|
|
219
|
+
const serialized = serializeToolArguments(parsedArgs, name, toolsNormalized).trim();
|
|
220
|
+
toolNameById.set(callId, name);
|
|
221
|
+
messages.push({ role: 'assistant', tool_calls: [{ id: callId, type: 'function', function: { name, arguments: serialized } }] });
|
|
222
|
+
lastToolCallId = callId;
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
if (entryType === 'function_call_output' || entryType === 'tool_result' || entryType === 'tool_message') {
|
|
226
|
+
let toolCallId = entry.tool_call_id || entry.call_id || entry.tool_use_id || entry.id || lastToolCallId;
|
|
227
|
+
if (toolCallId && toolCallIdAliases.has(String(toolCallId))) {
|
|
228
|
+
toolCallId = toolCallIdAliases.get(String(toolCallId)) || toolCallId;
|
|
229
|
+
}
|
|
230
|
+
else if (typeof toolCallId === 'string' && toolCallId.trim().length) {
|
|
231
|
+
const normalized = normalizeFunctionCallId({ callId: toolCallId, fallback: toolCallId });
|
|
232
|
+
if (normalized !== toolCallId) {
|
|
233
|
+
toolCallIdAliases.set(toolCallId, normalized);
|
|
234
|
+
}
|
|
235
|
+
toolCallId = normalized;
|
|
236
|
+
}
|
|
237
|
+
const output = serializeToolOutput(entry);
|
|
238
|
+
if (toolCallId) {
|
|
239
|
+
try {
|
|
240
|
+
let contentStr = output != null ? String(output) : '';
|
|
241
|
+
if (!contentStr || contentStr.trim().length === 0) {
|
|
242
|
+
contentStr = 'Command succeeded (no output).';
|
|
243
|
+
}
|
|
244
|
+
const nm = toolNameById.get(String(toolCallId));
|
|
245
|
+
const toolMsg = { role: 'tool', tool_call_id: String(toolCallId), content: contentStr };
|
|
246
|
+
if (typeof nm === 'string' && nm.trim().length)
|
|
247
|
+
toolMsg.name = nm;
|
|
248
|
+
messages.push(toolMsg);
|
|
249
|
+
}
|
|
250
|
+
catch {
|
|
251
|
+
const fallback = (output ?? '');
|
|
252
|
+
const nm = toolNameById.get(String(toolCallId));
|
|
253
|
+
const toolMsg = { role: 'tool', tool_call_id: String(toolCallId), content: String(fallback) };
|
|
254
|
+
if (typeof nm === 'string' && nm.trim().length)
|
|
255
|
+
toolMsg.name = nm;
|
|
256
|
+
messages.push(toolMsg);
|
|
257
|
+
}
|
|
258
|
+
lastToolCallId = null;
|
|
259
|
+
}
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
let handledViaExplicitMessage = false;
|
|
263
|
+
if (entry && typeof entry.message === 'object' && Array.isArray(entry.message?.content)) {
|
|
264
|
+
const explicit = entry.message;
|
|
265
|
+
const { text, toolCalls, toolMessages, lastCallId } = processMessageBlocks(Array.isArray(explicit.content) ? explicit.content : [], toolsNormalized, toolNameById, toolCallIdAliases, lastToolCallId);
|
|
266
|
+
if (toolCalls.length)
|
|
267
|
+
messages.push({ role: 'assistant', tool_calls: toolCalls });
|
|
268
|
+
for (const msg of toolMessages)
|
|
269
|
+
messages.push(msg);
|
|
270
|
+
const normalizedRole = coerceBridgeRole((explicit.role ?? entry.role) || 'user');
|
|
271
|
+
if (typeof text === 'string' && text.trim().length) {
|
|
272
|
+
messages.push({ role: normalizedRole, content: text.trim() });
|
|
273
|
+
}
|
|
274
|
+
lastToolCallId = lastCallId;
|
|
275
|
+
handledViaExplicitMessage = true;
|
|
276
|
+
}
|
|
277
|
+
if (!handledViaExplicitMessage) {
|
|
278
|
+
const { text, toolCalls, toolMessages, lastCallId } = processMessageBlocks(Array.isArray(entry.content) ? entry.content : [], toolsNormalized, toolNameById, toolCallIdAliases, lastToolCallId);
|
|
279
|
+
if (toolCalls.length)
|
|
280
|
+
messages.push({ role: 'assistant', tool_calls: toolCalls });
|
|
281
|
+
for (const msg of toolMessages)
|
|
282
|
+
messages.push(msg);
|
|
283
|
+
const normalizedRole = coerceBridgeRole(entry.role || 'user');
|
|
284
|
+
if (typeof text === 'string' && text.trim().length) {
|
|
285
|
+
messages.push({ role: normalizedRole, content: text.trim() });
|
|
286
|
+
}
|
|
287
|
+
lastToolCallId = lastCallId;
|
|
288
|
+
}
|
|
289
|
+
try {
|
|
290
|
+
const t = String(entry.type || '').toLowerCase();
|
|
291
|
+
if ((t === 'input_text' || t === 'text' || t === 'output_text' || t === 'commentary') && typeof entry.text === 'string') {
|
|
292
|
+
const normalizedRole = coerceBridgeRole(entry.role || 'user');
|
|
293
|
+
const s = entry.text;
|
|
294
|
+
if (s && s.length)
|
|
295
|
+
messages.push({ role: normalizedRole, content: s });
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
catch { /* ignore */ }
|
|
299
|
+
}
|
|
300
|
+
return messages;
|
|
301
|
+
}
|
|
302
|
+
function processMessageBlocks(blocks, toolsNormalized, toolNameById, toolCallIdAliases, lastToolCallId) {
|
|
303
|
+
const textParts = [];
|
|
304
|
+
const toolCalls = [];
|
|
305
|
+
const toolMessages = [];
|
|
306
|
+
let currentLastCall = lastToolCallId;
|
|
307
|
+
for (const block of blocks) {
|
|
308
|
+
if (!block || typeof block !== 'object')
|
|
309
|
+
continue;
|
|
310
|
+
const type = typeof block.type === 'string' ? block.type.toLowerCase() : '';
|
|
311
|
+
if (type === 'input_text' || type === 'output_text' || type === 'text' || type === 'commentary') {
|
|
312
|
+
if (typeof block.text === 'string')
|
|
313
|
+
textParts.push(block.text);
|
|
314
|
+
else if (typeof block.content === 'string')
|
|
315
|
+
textParts.push(block.content);
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
318
|
+
if (type === 'message' && Array.isArray(block.content)) {
|
|
319
|
+
const nested = processMessageBlocks(block.content, toolsNormalized, toolNameById, toolCallIdAliases, currentLastCall);
|
|
320
|
+
if (nested.text)
|
|
321
|
+
textParts.push(nested.text);
|
|
322
|
+
for (const tc of nested.toolCalls)
|
|
323
|
+
toolCalls.push(tc);
|
|
324
|
+
for (const tm of nested.toolMessages)
|
|
325
|
+
toolMessages.push(tm);
|
|
326
|
+
currentLastCall = nested.lastCallId;
|
|
327
|
+
continue;
|
|
328
|
+
}
|
|
329
|
+
if (type === 'function_call') {
|
|
330
|
+
const rawName = typeof block.name === 'string' ? block.name : (typeof block?.function?.name === 'string' ? block.function.name : undefined);
|
|
331
|
+
const name = (typeof rawName === 'string' && rawName.includes('.')) ? rawName.slice(rawName.indexOf('.') + 1).trim() : rawName;
|
|
332
|
+
const args = block.arguments ?? block?.function?.arguments ?? {};
|
|
333
|
+
const parsedArgs = args;
|
|
334
|
+
const callIdRaw = typeof block.id === 'string' ? block.id : (typeof block.call_id === 'string' ? block.call_id : `call_${Math.random().toString(36).slice(2, 8)}`);
|
|
335
|
+
const callId = normalizeFunctionCallId({ callId: callIdRaw, fallback: `fc_call_${toolCalls.length + 1}` });
|
|
336
|
+
if (callIdRaw && callIdRaw !== callId) {
|
|
337
|
+
toolCallIdAliases.set(callIdRaw, callId);
|
|
338
|
+
}
|
|
339
|
+
if (typeof name !== 'string' || !name.trim()) {
|
|
340
|
+
currentLastCall = null;
|
|
341
|
+
continue;
|
|
342
|
+
}
|
|
343
|
+
const serialized = serializeToolArguments(parsedArgs, name, toolsNormalized).trim();
|
|
344
|
+
toolNameById.set(callId, name);
|
|
345
|
+
toolCalls.push({ id: callId, type: 'function', function: { name, arguments: serialized } });
|
|
346
|
+
currentLastCall = callId;
|
|
347
|
+
continue;
|
|
348
|
+
}
|
|
349
|
+
if (type === 'function_call_output' || type === 'tool_result' || type === 'tool_message') {
|
|
350
|
+
let toolCallId = block.tool_call_id || block.call_id || block.tool_use_id || block.id || currentLastCall;
|
|
351
|
+
if (toolCallId && toolCallIdAliases.has(String(toolCallId))) {
|
|
352
|
+
toolCallId = toolCallIdAliases.get(String(toolCallId)) || toolCallId;
|
|
353
|
+
}
|
|
354
|
+
else if (typeof toolCallId === 'string' && toolCallId.trim().length) {
|
|
355
|
+
const normalized = normalizeFunctionCallId({ callId: toolCallId, fallback: toolCallId });
|
|
356
|
+
if (normalized !== toolCallId) {
|
|
357
|
+
toolCallIdAliases.set(toolCallId, normalized);
|
|
358
|
+
}
|
|
359
|
+
toolCallId = normalized;
|
|
360
|
+
}
|
|
361
|
+
const output = serializeToolOutput(block);
|
|
362
|
+
if (toolCallId) {
|
|
363
|
+
try {
|
|
364
|
+
let contentStr = output != null ? String(output) : '';
|
|
365
|
+
if (!contentStr || contentStr.trim().length === 0) {
|
|
366
|
+
contentStr = 'Command succeeded (no output).';
|
|
367
|
+
}
|
|
368
|
+
const nm = toolNameById.get(String(toolCallId));
|
|
369
|
+
const toolMsg = { role: 'tool', tool_call_id: String(toolCallId), content: contentStr };
|
|
370
|
+
if (typeof nm === 'string' && nm.trim().length)
|
|
371
|
+
toolMsg.name = nm;
|
|
372
|
+
toolMessages.push(toolMsg);
|
|
373
|
+
}
|
|
374
|
+
catch {
|
|
375
|
+
const fallback = (output ?? '');
|
|
376
|
+
const nm = toolNameById.get(String(toolCallId));
|
|
377
|
+
const toolMsg = { role: 'tool', tool_call_id: String(toolCallId), content: String(fallback) };
|
|
378
|
+
if (typeof nm === 'string' && nm.trim().length)
|
|
379
|
+
toolMsg.name = nm;
|
|
380
|
+
toolMessages.push(toolMsg);
|
|
381
|
+
}
|
|
382
|
+
currentLastCall = null;
|
|
383
|
+
}
|
|
384
|
+
continue;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
return {
|
|
388
|
+
text: textParts.length ? textParts.join('\n') : null,
|
|
389
|
+
toolCalls,
|
|
390
|
+
toolMessages,
|
|
391
|
+
lastCallId: currentLastCall
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
function normalizeTools(tools) {
|
|
395
|
+
const normalized = [];
|
|
396
|
+
for (const tool of tools) {
|
|
397
|
+
if (!tool || typeof tool !== 'object')
|
|
398
|
+
continue;
|
|
399
|
+
const originalType = typeof tool.type === 'string' ? tool.type : 'function';
|
|
400
|
+
const mappedType = ['function', 'custom'].includes(originalType.toLowerCase())
|
|
401
|
+
? 'function'
|
|
402
|
+
: originalType;
|
|
403
|
+
const fn = (tool.function && typeof tool.function === 'object')
|
|
404
|
+
? tool.function
|
|
405
|
+
: {};
|
|
406
|
+
const topName = typeof tool.name === 'string' ? tool.name : undefined;
|
|
407
|
+
const fnName = typeof fn.name === 'string' ? fn.name : undefined;
|
|
408
|
+
const name = (fnName || topName || '').trim();
|
|
409
|
+
if (!name)
|
|
410
|
+
continue;
|
|
411
|
+
const topDesc = typeof tool.description === 'string' ? tool.description : undefined;
|
|
412
|
+
const fnDesc = typeof fn.description === 'string' ? fn.description : undefined;
|
|
413
|
+
const description = fnDesc || topDesc;
|
|
414
|
+
const fnParams = Object.prototype.hasOwnProperty.call(fn, 'parameters') ? fn.parameters : undefined;
|
|
415
|
+
const topParams = Object.prototype.hasOwnProperty.call(tool, 'parameters') ? tool.parameters : undefined;
|
|
416
|
+
const parameters = fnParams !== undefined ? fnParams : topParams;
|
|
417
|
+
const strict = typeof fn.strict === 'boolean' ? fn.strict :
|
|
418
|
+
typeof tool.strict === 'boolean' ? tool.strict :
|
|
419
|
+
undefined;
|
|
420
|
+
const fnOut = { name };
|
|
421
|
+
if (description)
|
|
422
|
+
fnOut.description = description;
|
|
423
|
+
if (parameters !== undefined)
|
|
424
|
+
fnOut.parameters = parameters;
|
|
425
|
+
if (strict !== undefined)
|
|
426
|
+
fnOut.strict = strict;
|
|
427
|
+
normalized.push({ type: mappedType, function: fnOut });
|
|
428
|
+
}
|
|
429
|
+
return normalized;
|
|
430
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { extractReasoningSegments, sanitizeReasoningTaggedText } from './reasoning-utils.js';
|
|
2
|
+
function collectText(value, collector) {
|
|
3
|
+
if (value == null) {
|
|
4
|
+
return;
|
|
5
|
+
}
|
|
6
|
+
if (typeof value === 'string') {
|
|
7
|
+
const cleaned = extractReasoningSegments(value, collector.reasoningParts);
|
|
8
|
+
if (cleaned.length) {
|
|
9
|
+
collector.textParts.push(cleaned);
|
|
10
|
+
}
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
if (Array.isArray(value)) {
|
|
14
|
+
for (const entry of value) {
|
|
15
|
+
collectText(entry, collector);
|
|
16
|
+
}
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
if (typeof value === 'object') {
|
|
20
|
+
const record = value;
|
|
21
|
+
if (typeof record.text === 'string') {
|
|
22
|
+
collectText(record.text, collector);
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
if (typeof record.content === 'string') {
|
|
26
|
+
collectText(record.content, collector);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
if (Array.isArray(record.content)) {
|
|
30
|
+
collectText(record.content, collector);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
if (record.type === 'image_url' && typeof record.image_url === 'string') {
|
|
34
|
+
collector.textParts.push(`[image:${record.image_url}]`);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
try {
|
|
38
|
+
collector.textParts.push(sanitizeReasoningTaggedText(JSON.stringify(record)));
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
collector.textParts.push(String(record));
|
|
42
|
+
}
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
collector.textParts.push(sanitizeReasoningTaggedText(String(value)));
|
|
46
|
+
}
|
|
47
|
+
export function normalizeChatMessageContent(content) {
|
|
48
|
+
const collector = { textParts: [], reasoningParts: [] };
|
|
49
|
+
collectText(content, collector);
|
|
50
|
+
const text = collector.textParts.join('');
|
|
51
|
+
const reasoning = collector.reasoningParts.join('\n');
|
|
52
|
+
return {
|
|
53
|
+
contentText: text.length ? text : undefined,
|
|
54
|
+
reasoningText: reasoning.length ? reasoning : undefined
|
|
55
|
+
};
|
|
56
|
+
}
|
|
@@ -70,5 +70,28 @@ export async function runStandardChatRequestFilters(chatRequest, profile, contex
|
|
|
70
70
|
catch {
|
|
71
71
|
// 可选工具归一过滤器,失败时保持已归一化结果
|
|
72
72
|
}
|
|
73
|
-
return normalized;
|
|
73
|
+
return ensureToolParameterDefaults(normalized);
|
|
74
|
+
}
|
|
75
|
+
function ensureToolParameterDefaults(chatRequest) {
|
|
76
|
+
if (!chatRequest || typeof chatRequest !== 'object')
|
|
77
|
+
return chatRequest;
|
|
78
|
+
const next = { ...chatRequest };
|
|
79
|
+
if (!Array.isArray(next.tools))
|
|
80
|
+
return next;
|
|
81
|
+
next.tools = next.tools.map((tool) => {
|
|
82
|
+
if (!tool || typeof tool !== 'object')
|
|
83
|
+
return tool;
|
|
84
|
+
if (tool.type !== 'function')
|
|
85
|
+
return tool;
|
|
86
|
+
const fn = tool.function && typeof tool.function === 'object' ? { ...tool.function } : tool.function;
|
|
87
|
+
if (fn && typeof fn === 'object' && fn.parameters && typeof fn.parameters === 'object') {
|
|
88
|
+
const params = { ...fn.parameters };
|
|
89
|
+
if (!Object.prototype.hasOwnProperty.call(params, 'additionalProperties')) {
|
|
90
|
+
params.additionalProperties = true;
|
|
91
|
+
}
|
|
92
|
+
fn.parameters = params;
|
|
93
|
+
}
|
|
94
|
+
return { ...tool, function: fn };
|
|
95
|
+
});
|
|
96
|
+
return next;
|
|
74
97
|
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { BridgeToolDefinition } from './bridge-message-types.js';
|
|
2
|
+
import type { MissingField } from '../hub/types/chat-envelope.js';
|
|
3
|
+
import type { JsonValue, JsonObject } from '../hub/types/json.js';
|
|
4
|
+
export declare function prepareGeminiToolsForBridge(rawTools: JsonValue | undefined, missing?: MissingField[]): BridgeToolDefinition[] | undefined;
|
|
5
|
+
export declare function buildGeminiToolsFromBridge(defs: BridgeToolDefinition[] | undefined): JsonObject[] | undefined;
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { jsonClone } from '../hub/types/json.js';
|
|
2
|
+
function isPlainRecord(value) {
|
|
3
|
+
return !!value && typeof value === 'object' && !Array.isArray(value);
|
|
4
|
+
}
|
|
5
|
+
function cloneParameters(value) {
|
|
6
|
+
if (value === null || typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
|
|
7
|
+
return value;
|
|
8
|
+
}
|
|
9
|
+
if (Array.isArray(value)) {
|
|
10
|
+
return value.map((entry) => cloneParameters(entry));
|
|
11
|
+
}
|
|
12
|
+
if (isPlainRecord(value)) {
|
|
13
|
+
const cloned = {};
|
|
14
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
15
|
+
cloned[key] = cloneParameters(entry);
|
|
16
|
+
}
|
|
17
|
+
return cloned;
|
|
18
|
+
}
|
|
19
|
+
return { type: 'object', properties: {} };
|
|
20
|
+
}
|
|
21
|
+
function declarationToBridge(declaration) {
|
|
22
|
+
if (!declaration || typeof declaration !== 'object') {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
const def = declaration;
|
|
26
|
+
const name = typeof def.name === 'string' ? def.name : undefined;
|
|
27
|
+
if (!name) {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
const description = typeof def.description === 'string' ? def.description : undefined;
|
|
31
|
+
const parameters = cloneParameters(def.parameters ?? { type: 'object', properties: {} });
|
|
32
|
+
return {
|
|
33
|
+
type: 'function',
|
|
34
|
+
function: {
|
|
35
|
+
name,
|
|
36
|
+
description,
|
|
37
|
+
parameters
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function legacyToolToBridge(entry) {
|
|
42
|
+
const name = typeof entry.name === 'string' ? entry.name : undefined;
|
|
43
|
+
if (!name) {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
const description = typeof entry.description === 'string' ? entry.description : undefined;
|
|
47
|
+
const parameters = cloneParameters(entry.parameters ?? { type: 'object', properties: {} });
|
|
48
|
+
return {
|
|
49
|
+
type: 'function',
|
|
50
|
+
function: {
|
|
51
|
+
name,
|
|
52
|
+
description,
|
|
53
|
+
parameters
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
export function prepareGeminiToolsForBridge(rawTools, missing) {
|
|
58
|
+
if (!rawTools) {
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
const arr = Array.isArray(rawTools) ? rawTools : [rawTools];
|
|
62
|
+
const defs = [];
|
|
63
|
+
arr.forEach((entry, index) => {
|
|
64
|
+
if (!entry || typeof entry !== 'object') {
|
|
65
|
+
missing?.push({
|
|
66
|
+
path: `tools[${index}]`,
|
|
67
|
+
reason: 'invalid_entry',
|
|
68
|
+
originalValue: jsonClone(entry)
|
|
69
|
+
});
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const obj = entry;
|
|
73
|
+
const declarations = Array.isArray(obj.functionDeclarations) ? obj.functionDeclarations : undefined;
|
|
74
|
+
if (declarations && declarations.length) {
|
|
75
|
+
declarations.forEach((decl, declIndex) => {
|
|
76
|
+
const converted = declarationToBridge(decl);
|
|
77
|
+
if (converted) {
|
|
78
|
+
defs.push(converted);
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
missing?.push({ path: `tools[${index}].functionDeclarations[${declIndex}]`, reason: 'invalid_entry' });
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const converted = legacyToolToBridge(obj);
|
|
87
|
+
if (converted) {
|
|
88
|
+
defs.push(converted);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
missing?.push({ path: `tools[${index}]`, reason: 'invalid_entry' });
|
|
92
|
+
});
|
|
93
|
+
return defs.length ? defs : undefined;
|
|
94
|
+
}
|
|
95
|
+
export function buildGeminiToolsFromBridge(defs) {
|
|
96
|
+
if (!defs || !defs.length) {
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
99
|
+
const tools = [];
|
|
100
|
+
defs.forEach((def) => {
|
|
101
|
+
if (!def || typeof def !== 'object') {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
const fnNode = def.function && typeof def.function === 'object' ? def.function : undefined;
|
|
105
|
+
const name = typeof fnNode?.name === 'string'
|
|
106
|
+
? fnNode.name
|
|
107
|
+
: typeof def.name === 'string'
|
|
108
|
+
? def.name
|
|
109
|
+
: undefined;
|
|
110
|
+
if (!name) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
const description = typeof fnNode?.description === 'string'
|
|
114
|
+
? fnNode.description
|
|
115
|
+
: typeof def.description === 'string'
|
|
116
|
+
? def.description
|
|
117
|
+
: undefined;
|
|
118
|
+
const parameters = cloneParameters(fnNode?.parameters ?? def.parameters ?? { type: 'object', properties: {} });
|
|
119
|
+
tools.push({
|
|
120
|
+
functionDeclarations: [
|
|
121
|
+
{
|
|
122
|
+
name,
|
|
123
|
+
description,
|
|
124
|
+
parameters
|
|
125
|
+
}
|
|
126
|
+
]
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
return tools.length ? tools : undefined;
|
|
130
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type JsonObject, type JsonValue } from '../hub/types/json.js';
|
|
2
|
+
interface PassthroughOptions {
|
|
3
|
+
prefix: string;
|
|
4
|
+
keys: readonly string[];
|
|
5
|
+
}
|
|
6
|
+
export declare function encodeMetadataPassthrough(parameters: JsonObject | undefined, options: PassthroughOptions): Record<string, string> | undefined;
|
|
7
|
+
export declare function extractMetadataPassthrough(metadataField: JsonValue | undefined, options: PassthroughOptions): {
|
|
8
|
+
metadata?: JsonObject;
|
|
9
|
+
passthrough?: JsonObject;
|
|
10
|
+
};
|
|
11
|
+
export {};
|