@jsonstudio/llms 0.6.215 → 0.6.375

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 (73) hide show
  1. package/README.md +2 -0
  2. package/dist/conversion/codecs/gemini-openai-codec.js +92 -2
  3. package/dist/conversion/compat/actions/gemini-web-search.d.ts +17 -0
  4. package/dist/conversion/compat/actions/gemini-web-search.js +68 -0
  5. package/dist/conversion/compat/actions/glm-image-content.d.ts +2 -0
  6. package/dist/conversion/compat/actions/glm-image-content.js +83 -0
  7. package/dist/conversion/compat/actions/glm-vision-prompt.d.ts +11 -0
  8. package/dist/conversion/compat/actions/glm-vision-prompt.js +177 -0
  9. package/dist/conversion/compat/actions/glm-web-search.d.ts +2 -0
  10. package/dist/conversion/compat/actions/glm-web-search.js +63 -0
  11. package/dist/conversion/compat/actions/universal-shape-filter.js +11 -0
  12. package/dist/conversion/compat/profiles/chat-gemini.json +17 -0
  13. package/dist/conversion/compat/profiles/chat-glm.json +190 -181
  14. package/dist/conversion/compat/profiles/chat-iflow.json +195 -195
  15. package/dist/conversion/compat/profiles/chat-lmstudio.json +43 -43
  16. package/dist/conversion/compat/profiles/chat-qwen.json +20 -20
  17. package/dist/conversion/compat/profiles/responses-c4m.json +42 -42
  18. package/dist/conversion/config/sample-config.json +1 -1
  19. package/dist/conversion/hub/pipeline/compat/compat-pipeline-executor.js +24 -0
  20. package/dist/conversion/hub/pipeline/compat/compat-types.d.ts +8 -0
  21. package/dist/conversion/hub/pipeline/hub-pipeline.js +37 -2
  22. package/dist/conversion/hub/pipeline/target-utils.js +6 -0
  23. package/dist/conversion/hub/process/chat-process.js +213 -1
  24. package/dist/conversion/hub/response/provider-response.d.ts +34 -0
  25. package/dist/conversion/hub/response/provider-response.js +84 -24
  26. package/dist/conversion/hub/response/server-side-tools.d.ts +26 -0
  27. package/dist/conversion/hub/response/server-side-tools.js +383 -0
  28. package/dist/conversion/hub/semantic-mappers/gemini-mapper.js +123 -3
  29. package/dist/conversion/hub/semantic-mappers/responses-mapper.js +17 -1
  30. package/dist/conversion/hub/standardized-bridge.js +14 -0
  31. package/dist/conversion/hub/types/standardized.d.ts +1 -0
  32. package/dist/conversion/responses/responses-openai-bridge.js +82 -3
  33. package/dist/conversion/shared/anthropic-message-utils.js +92 -3
  34. package/dist/conversion/shared/bridge-message-utils.js +137 -10
  35. package/dist/conversion/shared/responses-output-builder.js +43 -2
  36. package/dist/conversion/shared/tool-filter-pipeline.js +1 -0
  37. package/dist/conversion/shared/tool-mapping.js +25 -2
  38. package/dist/router/virtual-router/bootstrap.js +308 -43
  39. package/dist/router/virtual-router/classifier.js +11 -17
  40. package/dist/router/virtual-router/context-advisor.d.ts +0 -2
  41. package/dist/router/virtual-router/context-advisor.js +0 -12
  42. package/dist/router/virtual-router/engine.d.ts +16 -2
  43. package/dist/router/virtual-router/engine.js +317 -96
  44. package/dist/router/virtual-router/features.js +1 -1
  45. package/dist/router/virtual-router/message-utils.js +36 -24
  46. package/dist/router/virtual-router/provider-registry.js +2 -1
  47. package/dist/router/virtual-router/token-counter.js +14 -3
  48. package/dist/router/virtual-router/types.d.ts +66 -2
  49. package/dist/router/virtual-router/types.js +2 -1
  50. package/dist/servertool/engine.d.ts +27 -0
  51. package/dist/servertool/engine.js +60 -0
  52. package/dist/servertool/flow-types.d.ts +40 -0
  53. package/dist/servertool/flow-types.js +1 -0
  54. package/dist/servertool/handlers/vision.d.ts +1 -0
  55. package/dist/servertool/handlers/vision.js +194 -0
  56. package/dist/servertool/handlers/web-search.d.ts +1 -0
  57. package/dist/servertool/handlers/web-search.js +638 -0
  58. package/dist/servertool/orchestration-types.d.ts +33 -0
  59. package/dist/servertool/orchestration-types.js +1 -0
  60. package/dist/servertool/registry.d.ts +18 -0
  61. package/dist/servertool/registry.js +27 -0
  62. package/dist/servertool/server-side-tools.d.ts +8 -0
  63. package/dist/servertool/server-side-tools.js +208 -0
  64. package/dist/servertool/types.d.ts +88 -0
  65. package/dist/servertool/types.js +1 -0
  66. package/dist/servertool/vision-tool.d.ts +2 -0
  67. package/dist/servertool/vision-tool.js +185 -0
  68. package/dist/sse/json-to-sse/event-generators/responses.js +15 -3
  69. package/dist/sse/sse-to-json/builders/response-builder.js +6 -3
  70. package/dist/sse/sse-to-json/gemini-sse-to-json-converter.js +27 -1
  71. package/dist/sse/types/gemini-types.d.ts +20 -1
  72. package/dist/sse/types/responses-types.js +1 -1
  73. package/package.json +1 -1
@@ -0,0 +1,383 @@
1
+ import { buildOpenAIChatFromGeminiResponse } from '../../codecs/gemini-openai-codec.js';
2
+ function asObject(value) {
3
+ return value && typeof value === 'object' && !Array.isArray(value) ? value : null;
4
+ }
5
+ function getArray(value) {
6
+ return Array.isArray(value) ? value : [];
7
+ }
8
+ function extractToolCalls(chatResponse) {
9
+ const choices = getArray(chatResponse.choices);
10
+ const calls = [];
11
+ for (const choice of choices) {
12
+ const choiceObj = asObject(choice);
13
+ if (!choiceObj)
14
+ continue;
15
+ const message = asObject(choiceObj.message);
16
+ if (!message)
17
+ continue;
18
+ const toolCalls = getArray(message.tool_calls);
19
+ for (const raw of toolCalls) {
20
+ const tc = asObject(raw);
21
+ if (!tc)
22
+ continue;
23
+ const id = typeof tc.id === 'string' && tc.id.trim() ? tc.id.trim() : '';
24
+ const fn = asObject(tc.function);
25
+ const name = fn && typeof fn.name === 'string' && fn.name.trim() ? fn.name.trim() : '';
26
+ const args = fn && typeof fn.arguments === 'string' ? fn.arguments : '';
27
+ if (!id || !name)
28
+ continue;
29
+ calls.push({ id, name, arguments: args });
30
+ }
31
+ }
32
+ return calls;
33
+ }
34
+ function extractTextFromChatLike(payload) {
35
+ // 1) 解包常见包装层:data / response 节点
36
+ let current = payload;
37
+ const visited = new Set();
38
+ while (current && typeof current === 'object' && !Array.isArray(current) && !visited.has(current)) {
39
+ visited.add(current);
40
+ if (Array.isArray(current.choices) || Array.isArray(current.output)) {
41
+ break;
42
+ }
43
+ const data = current.data;
44
+ if (data && typeof data === 'object' && !Array.isArray(data)) {
45
+ current = data;
46
+ continue;
47
+ }
48
+ const response = current.response;
49
+ if (response && typeof response === 'object' && !Array.isArray(response)) {
50
+ current = response;
51
+ continue;
52
+ }
53
+ break;
54
+ }
55
+ // 2) 优先从 choices[].message.content 提取(OpenAI/GLM 兼容)
56
+ const choices = getArray(current.choices);
57
+ if (!choices.length)
58
+ return '';
59
+ const first = asObject(choices[0]);
60
+ if (!first)
61
+ return '';
62
+ const message = asObject(first.message);
63
+ if (!message)
64
+ return '';
65
+ const content = message.content;
66
+ if (typeof content === 'string')
67
+ return content.trim();
68
+ const parts = getArray(content);
69
+ const texts = [];
70
+ for (const part of parts) {
71
+ if (typeof part === 'string') {
72
+ texts.push(part);
73
+ }
74
+ else if (part && typeof part === 'object') {
75
+ const record = part;
76
+ if (typeof record.text === 'string') {
77
+ texts.push(record.text);
78
+ }
79
+ else if (typeof record.content === 'string') {
80
+ texts.push(record.content);
81
+ }
82
+ }
83
+ }
84
+ const joinedFromChoices = texts.join('\n').trim();
85
+ if (joinedFromChoices) {
86
+ return joinedFromChoices;
87
+ }
88
+ // 3) 回退:从 output[].content[] 中提取(部分 Responses/自定义后端)
89
+ const output = current.output;
90
+ if (Array.isArray(output)) {
91
+ const altTexts = [];
92
+ for (const entry of output) {
93
+ if (!entry || typeof entry !== 'object')
94
+ continue;
95
+ const blocks = entry.content;
96
+ const blockArray = Array.isArray(blocks) ? blocks : [];
97
+ for (const block of blockArray) {
98
+ if (!block || typeof block !== 'object')
99
+ continue;
100
+ const record = block;
101
+ if (typeof record.text === 'string') {
102
+ altTexts.push(record.text);
103
+ }
104
+ else if (typeof record.output_text === 'string') {
105
+ altTexts.push(record.output_text);
106
+ }
107
+ else if (typeof record.content === 'string') {
108
+ altTexts.push(record.content);
109
+ }
110
+ }
111
+ }
112
+ const joined = altTexts.join('\n').trim();
113
+ if (joined) {
114
+ return joined;
115
+ }
116
+ }
117
+ return '';
118
+ }
119
+ function getWebSearchConfig(ctx) {
120
+ const raw = ctx.webSearch;
121
+ const record = raw && typeof raw === 'object' && !Array.isArray(raw) ? raw : null;
122
+ if (!record)
123
+ return undefined;
124
+ const enginesRaw = Array.isArray(record.engines) ? record.engines : [];
125
+ const engines = [];
126
+ for (const entry of enginesRaw) {
127
+ const obj = entry && typeof entry === 'object' && !Array.isArray(entry) ? entry : null;
128
+ if (!obj)
129
+ continue;
130
+ const id = typeof obj.id === 'string' && obj.id.trim() ? obj.id.trim() : undefined;
131
+ const providerKey = typeof obj.providerKey === 'string' && obj.providerKey.trim()
132
+ ? obj.providerKey.trim()
133
+ : undefined;
134
+ if (!id || !providerKey)
135
+ continue;
136
+ engines.push({
137
+ id,
138
+ providerKey,
139
+ description: typeof obj.description === 'string' && obj.description.trim() ? obj.description.trim() : undefined,
140
+ default: obj.default === true
141
+ });
142
+ }
143
+ if (!engines.length) {
144
+ return undefined;
145
+ }
146
+ const config = { engines };
147
+ if (typeof record.injectPolicy === 'string') {
148
+ const val = String(record.injectPolicy).trim().toLowerCase();
149
+ if (val === 'always' || val === 'selective') {
150
+ config.injectPolicy = val;
151
+ }
152
+ }
153
+ return config;
154
+ }
155
+ function resolveWebSearchEngine(config, engineId) {
156
+ const trimmedId = engineId && typeof engineId === 'string' && engineId.trim() ? engineId.trim() : undefined;
157
+ if (trimmedId) {
158
+ const byId = config.engines.find((e) => e.id === trimmedId);
159
+ if (byId) {
160
+ return byId;
161
+ }
162
+ }
163
+ const byDefault = config.engines.find((e) => e.default);
164
+ if (byDefault) {
165
+ return byDefault;
166
+ }
167
+ if (config.engines.length === 1) {
168
+ return config.engines[0];
169
+ }
170
+ return undefined;
171
+ }
172
+ function isGeminiWebSearchEngine(engine) {
173
+ const key = engine.providerKey.toLowerCase();
174
+ return key.startsWith('gemini-cli.') || key.startsWith('antigravity.');
175
+ }
176
+ function logServerToolWebSearch(engine, requestId, query) {
177
+ const providerAlias = engine.providerKey.split('.')[0] || engine.providerKey;
178
+ const backendLabel = `${providerAlias}:${engine.id}`;
179
+ const prefix = `[server-tool][web_search][${backendLabel}]`;
180
+ const line = `${prefix} requestId=${requestId} query=${JSON.stringify(query)}`;
181
+ // 深蓝色输出
182
+ // eslint-disable-next-line no-console
183
+ console.log(`\x1b[38;5;27m${line}\x1b[0m`);
184
+ }
185
+ function resolveEnvServerSideToolsEnabled() {
186
+ const raw = (process.env.ROUTECODEX_SERVER_SIDE_TOOLS || process.env.RCC_SERVER_SIDE_TOOLS || '').trim().toLowerCase();
187
+ if (!raw)
188
+ return false;
189
+ if (raw === '1' || raw === 'true' || raw === 'yes')
190
+ return true;
191
+ if (raw === 'web_search')
192
+ return true;
193
+ return false;
194
+ }
195
+ export async function runServerSideToolEngine(options) {
196
+ const base = asObject(options.chatResponse);
197
+ if (!base) {
198
+ return { mode: 'passthrough', finalChatResponse: options.chatResponse };
199
+ }
200
+ // 内建 OpenAI Responses `/v1/responses` 已经支持 server-side web_search。
201
+ // 仅当“入口端点为 /v1/responses 且 providerProtocol 也是 openai-responses”时保持透传,
202
+ // 避免对 Responses→Responses 的链路重复执行搜索回环;
203
+ // 其它场景(例如 /v1/responses → gemini/glm 后端)仍允许 server-side web_search 生效。
204
+ const entry = (options.entryEndpoint || '').toLowerCase();
205
+ if (options.providerProtocol === 'openai-responses' && entry.includes('/v1/responses')) {
206
+ return { mode: 'passthrough', finalChatResponse: base };
207
+ }
208
+ // Feature flag: keep behaviour fully backwards-compatible unless explicitly enabled.
209
+ const toolsEnabled = resolveEnvServerSideToolsEnabled();
210
+ const toolCalls = extractToolCalls(base);
211
+ const webSearchCall = toolCalls.find((tc) => tc.name === 'web_search');
212
+ if (!toolsEnabled || !webSearchCall || !options.providerInvoker) {
213
+ return { mode: 'passthrough', finalChatResponse: base };
214
+ }
215
+ const webSearchConfig = getWebSearchConfig(options.adapterContext);
216
+ if (!webSearchConfig) {
217
+ return { mode: 'passthrough', finalChatResponse: base };
218
+ }
219
+ let parsedArgs = {};
220
+ if (webSearchCall.arguments && typeof webSearchCall.arguments === 'string') {
221
+ try {
222
+ parsedArgs = JSON.parse(webSearchCall.arguments);
223
+ }
224
+ catch {
225
+ parsedArgs = {};
226
+ }
227
+ }
228
+ const query = typeof parsedArgs?.query === 'string' && parsedArgs.query.trim()
229
+ ? parsedArgs.query.trim()
230
+ : undefined;
231
+ const engineId = typeof parsedArgs?.engine === 'string' && parsedArgs.engine.trim()
232
+ ? parsedArgs.engine.trim()
233
+ : undefined;
234
+ const recency = typeof parsedArgs?.recency === 'string' && parsedArgs.recency.trim()
235
+ ? parsedArgs.recency.trim()
236
+ : undefined;
237
+ const count = typeof parsedArgs?.count === 'number' && Number.isFinite(parsedArgs.count)
238
+ ? Math.floor(parsedArgs.count)
239
+ : undefined;
240
+ if (!query) {
241
+ return { mode: 'passthrough', finalChatResponse: base };
242
+ }
243
+ const engine = resolveWebSearchEngine(webSearchConfig, engineId);
244
+ if (!engine) {
245
+ return { mode: 'passthrough', finalChatResponse: base };
246
+ }
247
+ const providerInvoker = options.providerInvoker;
248
+ // 1) Call search backend (secondary request)
249
+ let searchSummary = '';
250
+ try {
251
+ logServerToolWebSearch(engine, options.requestId, query);
252
+ if (isGeminiWebSearchEngine(engine)) {
253
+ const geminiPayload = {
254
+ model: engine.id,
255
+ contents: [
256
+ {
257
+ role: 'user',
258
+ parts: [
259
+ {
260
+ text: query
261
+ }
262
+ ]
263
+ }
264
+ ],
265
+ // Cloud Code search models treat googleSearch as the web search tool.
266
+ tools: [
267
+ {
268
+ googleSearch: {}
269
+ }
270
+ ]
271
+ };
272
+ const backend = await providerInvoker({
273
+ providerKey: engine.providerKey,
274
+ providerType: undefined,
275
+ modelId: engine.id,
276
+ providerProtocol: 'gemini-chat',
277
+ payload: geminiPayload,
278
+ entryEndpoint: '/v1/models/gemini:generateContent',
279
+ requestId: `${options.requestId}:web_search:${engine.id}`
280
+ });
281
+ const backendResponse = asObject(backend.providerResponse);
282
+ if (backendResponse) {
283
+ const chatLike = asObject(buildOpenAIChatFromGeminiResponse(backendResponse));
284
+ if (chatLike) {
285
+ searchSummary = extractTextFromChatLike(chatLike);
286
+ }
287
+ }
288
+ }
289
+ else {
290
+ const backendPayload = {
291
+ model: engine.providerKey,
292
+ messages: [
293
+ {
294
+ role: 'system',
295
+ content: 'You are a web search engine. Answer with up-to-date information based on the open internet.'
296
+ },
297
+ {
298
+ role: 'user',
299
+ content: query
300
+ }
301
+ ],
302
+ stream: false,
303
+ web_search: {
304
+ query,
305
+ ...(recency ? { recency } : {}),
306
+ ...(typeof count === 'number' ? { count } : {}),
307
+ engine: engine.id
308
+ }
309
+ };
310
+ const backend = await providerInvoker({
311
+ providerKey: engine.providerKey,
312
+ providerType: undefined,
313
+ modelId: undefined,
314
+ providerProtocol: options.providerProtocol,
315
+ payload: backendPayload,
316
+ entryEndpoint: '/v1/chat/completions',
317
+ requestId: `${options.requestId}:web_search:${engine.id}`
318
+ });
319
+ const backendResponse = asObject(backend.providerResponse);
320
+ if (backendResponse) {
321
+ searchSummary = extractTextFromChatLike(backendResponse);
322
+ }
323
+ }
324
+ }
325
+ catch {
326
+ // fall back to passthrough if backend fails
327
+ return { mode: 'passthrough', finalChatResponse: base };
328
+ }
329
+ if (!searchSummary) {
330
+ return { mode: 'passthrough', finalChatResponse: base };
331
+ }
332
+ // 2) Call main provider again with synthesized prompt (third request)
333
+ const ctxTarget = options.adapterContext.target;
334
+ const target = ctxTarget && typeof ctxTarget === 'object' && !Array.isArray(ctxTarget)
335
+ ? ctxTarget
336
+ : undefined;
337
+ const mainProviderKey = typeof target?.providerKey === 'string' && target.providerKey.trim()
338
+ ? target.providerKey.trim()
339
+ : undefined;
340
+ const mainModelId = (typeof base.model === 'string' && base.model.trim()
341
+ ? base.model.trim()
342
+ : typeof target?.modelId === 'string' && target.modelId.trim()
343
+ ? target.modelId.trim()
344
+ : undefined) ?? 'gpt-4o-mini';
345
+ if (!mainProviderKey) {
346
+ return { mode: 'passthrough', finalChatResponse: base };
347
+ }
348
+ let finalResponse = base;
349
+ try {
350
+ const followupPayload = {
351
+ model: mainModelId,
352
+ messages: [
353
+ {
354
+ role: 'system',
355
+ content: 'You are an assistant that answers using the provided web_search results. Use them as the primary source.'
356
+ },
357
+ {
358
+ role: 'user',
359
+ content: `User question: ${query}\n\nWeb search results:\n${searchSummary}`
360
+ }
361
+ ],
362
+ stream: false
363
+ };
364
+ const followup = await providerInvoker({
365
+ providerKey: mainProviderKey,
366
+ providerType: undefined,
367
+ modelId: mainModelId,
368
+ providerProtocol: options.providerProtocol,
369
+ payload: followupPayload,
370
+ entryEndpoint: '/v1/chat/completions',
371
+ requestId: `${options.requestId}:web_search_followup`
372
+ });
373
+ const followupResponse = asObject(followup.providerResponse);
374
+ if (followupResponse) {
375
+ finalResponse = followupResponse;
376
+ }
377
+ }
378
+ catch {
379
+ // If follow-up fails, keep original base response to avoid breaking clients.
380
+ finalResponse = base;
381
+ }
382
+ return { mode: 'web_search_flow', finalChatResponse: finalResponse };
383
+ }
@@ -171,6 +171,117 @@ function collectParameters(payload) {
171
171
  }
172
172
  return Object.keys(params).length ? params : undefined;
173
173
  }
174
+ function appendChatContentToGeminiParts(message, targetParts) {
175
+ const content = message.content;
176
+ if (typeof content === 'string') {
177
+ const text = content.trim();
178
+ if (text.length) {
179
+ targetParts.push({ text });
180
+ }
181
+ return;
182
+ }
183
+ if (!Array.isArray(content)) {
184
+ return;
185
+ }
186
+ const items = content;
187
+ for (const block of items) {
188
+ if (block == null)
189
+ continue;
190
+ if (typeof block === 'string') {
191
+ const text = block.trim();
192
+ if (text.length) {
193
+ targetParts.push({ text });
194
+ }
195
+ continue;
196
+ }
197
+ if (typeof block !== 'object') {
198
+ const text = String(block);
199
+ if (text.trim().length) {
200
+ targetParts.push({ text: text.trim() });
201
+ }
202
+ continue;
203
+ }
204
+ const record = block;
205
+ const rawType = record.type;
206
+ const type = typeof rawType === 'string' ? rawType.toLowerCase() : '';
207
+ // Text-style blocks
208
+ if (!type || type === 'text') {
209
+ const textValue = typeof record.text === 'string'
210
+ ? record.text
211
+ : typeof record.content === 'string'
212
+ ? record.content
213
+ : '';
214
+ const text = textValue.trim();
215
+ if (text.length) {
216
+ targetParts.push({ text });
217
+ }
218
+ continue;
219
+ }
220
+ // Image-style blocks -> Gemini inlineData
221
+ if (type === 'image' || type === 'image_url') {
222
+ // Prefer OpenAI-style image_url.url, but also accept uri/url/data.
223
+ let url;
224
+ const imageUrlRaw = record.image_url;
225
+ if (typeof imageUrlRaw === 'string') {
226
+ url = imageUrlRaw;
227
+ }
228
+ else if (imageUrlRaw && typeof imageUrlRaw === 'object' && typeof imageUrlRaw.url === 'string') {
229
+ url = imageUrlRaw.url;
230
+ }
231
+ else if (typeof record.uri === 'string') {
232
+ url = record.uri;
233
+ }
234
+ else if (typeof record.url === 'string') {
235
+ url = record.url;
236
+ }
237
+ else if (typeof record.data === 'string') {
238
+ url = record.data;
239
+ }
240
+ const trimmed = (url ?? '').trim();
241
+ if (!trimmed.length) {
242
+ // Fallback: at least emit a textual marker so内容不会完全丢失
243
+ targetParts.push({ text: '[image]' });
244
+ continue;
245
+ }
246
+ let mimeType;
247
+ let data;
248
+ // data:URL → inlineData { mimeType, data }
249
+ if (trimmed.startsWith('data:')) {
250
+ const match = /^data:([^;,]+)?(?:;base64)?,(.*)$/s.exec(trimmed);
251
+ if (match) {
252
+ mimeType = (match[1] || '').trim() || undefined;
253
+ data = match[2] || '';
254
+ }
255
+ }
256
+ if (data && data.trim().length) {
257
+ const inline = {
258
+ inlineData: {
259
+ data: data.trim()
260
+ }
261
+ };
262
+ if (mimeType && mimeType.length) {
263
+ inline.inlineData.mimeType = mimeType;
264
+ }
265
+ targetParts.push(inline);
266
+ }
267
+ else {
268
+ // 非 data: URL 暂时作为文本 URL 传递,保持语义可见
269
+ targetParts.push({ text: trimmed });
270
+ }
271
+ continue;
272
+ }
273
+ // 默认:回退为文本 JSON 表示,避免静默丢失内容
274
+ try {
275
+ const jsonText = JSON.stringify(record);
276
+ if (jsonText.trim().length) {
277
+ targetParts.push({ text: jsonText });
278
+ }
279
+ }
280
+ catch {
281
+ // ignore malformed block
282
+ }
283
+ }
284
+ }
174
285
  function buildGeminiRequestFromChat(chat, metadata) {
175
286
  const contents = [];
176
287
  const emittedToolOutputs = new Set();
@@ -191,9 +302,7 @@ function buildGeminiRequestFromChat(chat, metadata) {
191
302
  role: mapChatRoleToGemini(message.role),
192
303
  parts: []
193
304
  };
194
- if (typeof message.content === 'string' && message.content.trim().length) {
195
- entry.parts.push({ text: message.content });
196
- }
305
+ appendChatContentToGeminiParts(message, entry.parts);
197
306
  const toolCalls = Array.isArray(message.tool_calls) ? message.tool_calls : [];
198
307
  for (const tc of toolCalls) {
199
308
  if (!tc || typeof tc !== 'object')
@@ -335,7 +444,18 @@ function safeParseJson(value) {
335
444
  }
336
445
  }
337
446
  function ensureFunctionResponsePayload(value) {
447
+ // Gemini function_response.response 字段在 CloudCode/Gemini CLI 协议里对应的是
448
+ // protobuf Struct(JSON object),而不是顶层数组。
449
+ // 这里做一层规范化:
450
+ // - 对象:直接透传;
451
+ // - 数组:包一层 { result: [...] } 避免把数组作为 Struct 根节点;
452
+ // - 原始值:包一层 { result: value },并把 undefined 映射为 null。
338
453
  if (value && typeof value === 'object') {
454
+ if (Array.isArray(value)) {
455
+ return {
456
+ result: value
457
+ };
458
+ }
339
459
  return value;
340
460
  }
341
461
  return {
@@ -148,6 +148,20 @@ function serializeSystemContent(message) {
148
148
  }
149
149
  return undefined;
150
150
  }
151
+ function mergeMetadata(a, b) {
152
+ if (!a && !b) {
153
+ return undefined;
154
+ }
155
+ if (!a && b) {
156
+ return jsonClone(b);
157
+ }
158
+ if (a && !b) {
159
+ return jsonClone(a);
160
+ }
161
+ const left = jsonClone(a);
162
+ const right = jsonClone(b);
163
+ return { ...left, ...right };
164
+ }
151
165
  export class ResponsesSemanticMapper {
152
166
  async toChat(format, ctx) {
153
167
  const payload = format.payload || {};
@@ -209,13 +223,15 @@ export class ResponsesSemanticMapper {
209
223
  .map(message => serializeSystemContent(message))
210
224
  .filter((content) => typeof content === 'string' && content.length > 0);
211
225
  const capturedContext = chat.metadata?.responsesContext;
226
+ const envelopeMetadata = chat.metadata && isJsonObject(chat.metadata) ? chat.metadata : undefined;
212
227
  const responsesContext = isJsonObject(capturedContext)
213
228
  ? {
214
229
  ...capturedContext,
230
+ metadata: mergeMetadata(capturedContext.metadata, envelopeMetadata),
215
231
  originalSystemMessages
216
232
  }
217
233
  : {
218
- metadata: (chat.metadata && isJsonObject(chat.metadata) ? chat.metadata : undefined),
234
+ metadata: envelopeMetadata,
219
235
  originalSystemMessages
220
236
  };
221
237
  const responsesResult = buildResponsesRequestFromChat(requestShape, responsesContext);
@@ -60,6 +60,20 @@ export function standardizedToChatEnvelope(request, options) {
60
60
  const metadata = {
61
61
  context: adapterContext
62
62
  };
63
+ const sourceMeta = (request.metadata && typeof request.metadata === 'object'
64
+ ? request.metadata
65
+ : undefined);
66
+ if (sourceMeta) {
67
+ if (sourceMeta.webSearch && typeof sourceMeta.webSearch === 'object') {
68
+ metadata.webSearch = sourceMeta.webSearch;
69
+ }
70
+ if (sourceMeta.forceWebSearch === true) {
71
+ metadata.forceWebSearch = true;
72
+ }
73
+ if (sourceMeta.forceVision === true) {
74
+ metadata.forceVision = true;
75
+ }
76
+ }
63
77
  if (typeof adapterContext.toolCallIdStyle === 'string' && adapterContext.toolCallIdStyle.length) {
64
78
  metadata.toolCallIdStyle = adapterContext.toolCallIdStyle;
65
79
  }
@@ -65,6 +65,7 @@ export interface StandardizedMetadata {
65
65
  providerType?: string;
66
66
  processMode?: 'chat' | 'passthrough';
67
67
  routeHint?: string;
68
+ webSearchEnabled?: boolean;
68
69
  [key: string]: unknown;
69
70
  }
70
71
  export interface StandardizedRequest {