@jsonstudio/llms 0.6.230 → 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 (63) hide show
  1. package/README.md +2 -0
  2. package/dist/conversion/codecs/gemini-openai-codec.js +9 -1
  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.js +25 -28
  10. package/dist/conversion/compat/actions/universal-shape-filter.js +11 -0
  11. package/dist/conversion/compat/profiles/chat-gemini.json +17 -0
  12. package/dist/conversion/compat/profiles/chat-glm.json +190 -184
  13. package/dist/conversion/compat/profiles/chat-iflow.json +195 -195
  14. package/dist/conversion/compat/profiles/chat-lmstudio.json +43 -43
  15. package/dist/conversion/compat/profiles/chat-qwen.json +20 -20
  16. package/dist/conversion/compat/profiles/responses-c4m.json +42 -42
  17. package/dist/conversion/config/sample-config.json +1 -1
  18. package/dist/conversion/hub/pipeline/compat/compat-pipeline-executor.js +18 -0
  19. package/dist/conversion/hub/pipeline/compat/compat-types.d.ts +6 -0
  20. package/dist/conversion/hub/pipeline/hub-pipeline.js +28 -1
  21. package/dist/conversion/hub/pipeline/target-utils.js +6 -0
  22. package/dist/conversion/hub/process/chat-process.js +100 -18
  23. package/dist/conversion/hub/response/provider-response.d.ts +13 -1
  24. package/dist/conversion/hub/response/provider-response.js +84 -35
  25. package/dist/conversion/hub/response/server-side-tools.js +61 -4
  26. package/dist/conversion/hub/semantic-mappers/gemini-mapper.js +123 -3
  27. package/dist/conversion/hub/semantic-mappers/responses-mapper.js +17 -1
  28. package/dist/conversion/hub/standardized-bridge.js +14 -0
  29. package/dist/conversion/responses/responses-openai-bridge.js +35 -2
  30. package/dist/conversion/shared/anthropic-message-utils.js +92 -3
  31. package/dist/conversion/shared/bridge-message-utils.js +137 -10
  32. package/dist/conversion/shared/responses-output-builder.js +43 -2
  33. package/dist/conversion/shared/tool-filter-pipeline.js +1 -0
  34. package/dist/router/virtual-router/bootstrap.js +44 -12
  35. package/dist/router/virtual-router/classifier.js +11 -17
  36. package/dist/router/virtual-router/engine.d.ts +9 -0
  37. package/dist/router/virtual-router/engine.js +160 -18
  38. package/dist/router/virtual-router/features.js +1 -1
  39. package/dist/router/virtual-router/message-utils.js +36 -24
  40. package/dist/router/virtual-router/provider-registry.js +2 -1
  41. package/dist/router/virtual-router/token-counter.js +14 -3
  42. package/dist/router/virtual-router/types.d.ts +45 -0
  43. package/dist/router/virtual-router/types.js +2 -1
  44. package/dist/servertool/engine.d.ts +27 -0
  45. package/dist/servertool/engine.js +60 -0
  46. package/dist/servertool/flow-types.d.ts +40 -0
  47. package/dist/servertool/flow-types.js +1 -0
  48. package/dist/servertool/handlers/vision.d.ts +1 -0
  49. package/dist/servertool/handlers/vision.js +194 -0
  50. package/dist/servertool/handlers/web-search.d.ts +1 -0
  51. package/dist/servertool/handlers/web-search.js +638 -0
  52. package/dist/servertool/orchestration-types.d.ts +33 -0
  53. package/dist/servertool/orchestration-types.js +1 -0
  54. package/dist/servertool/registry.d.ts +18 -0
  55. package/dist/servertool/registry.js +27 -0
  56. package/dist/servertool/server-side-tools.d.ts +8 -0
  57. package/dist/servertool/server-side-tools.js +208 -0
  58. package/dist/servertool/types.d.ts +88 -0
  59. package/dist/servertool/types.js +1 -0
  60. package/dist/servertool/vision-tool.d.ts +2 -0
  61. package/dist/servertool/vision-tool.js +185 -0
  62. package/dist/sse/sse-to-json/builders/response-builder.js +6 -3
  63. package/package.json +1 -1
package/README.md CHANGED
@@ -21,6 +21,8 @@
21
21
 
22
22
  👉 **Hub Pipeline 是当前唯一入口。** 如果 Host 需要直接编排 Virtual Router,请参见 `docs/HUB_PIPELINE_USAGE.md`,了解如何通过 `bootstrapVirtualRouterConfig` + `HubPipeline` 完成初始化与热更新。
23
23
 
24
+ 👉 **ServerTool 是所有 server-side 工具的唯一统一框架。** web_search / vision followup 等服务端工具均在 llmswitch-core 内部通过 ServerTool 执行,Host 只需提供 providerInvoker/reenterPipeline 即可。设计与接入方式详见 `docs/SERVERTOOL_DESIGN.md`。
25
+
24
26
  ## 总览
25
27
 
26
28
  ```
@@ -185,6 +185,8 @@ export function buildOpenAIChatFromGeminiResponse(payload) {
185
185
  const toolCalls = [];
186
186
  const toolResultTexts = [];
187
187
  const toolOutputs = [];
188
+ // 为当前响应内生成稳定的工具调用 ID,避免下游 ServerTool 因缺少 id 而跳过。
189
+ let toolCallCounter = 0;
188
190
  for (const part of parts) {
189
191
  if (!part || typeof part !== 'object')
190
192
  continue;
@@ -227,7 +229,7 @@ export function buildOpenAIChatFromGeminiResponse(payload) {
227
229
  const name = typeof fc.name === 'string' ? String(fc.name) : undefined;
228
230
  if (!name)
229
231
  continue;
230
- const id = typeof fc.id === 'string' ? String(fc.id) : undefined;
232
+ let id = typeof fc.id === 'string' && fc.id.trim().length ? String(fc.id).trim() : undefined;
231
233
  const argsRaw = (fc.args ?? fc.arguments);
232
234
  let argsStr;
233
235
  if (typeof argsRaw === 'string') {
@@ -237,6 +239,12 @@ export function buildOpenAIChatFromGeminiResponse(payload) {
237
239
  argsStr = safeJson(argsRaw);
238
240
  }
239
241
  const thoughtSignature = coerceThoughtSignature(pObj.thoughtSignature);
242
+ // Gemini 某些响应中的 functionCall 不带 id,但下游 servertool 需要稳定的
243
+ // tool_call.id 才会识别为有效工具调用;此处在缺失时生成本响应内唯一的占位 ID。
244
+ if (!id) {
245
+ const suffix = toolCallCounter++;
246
+ id = `gemini_tool_${suffix}`;
247
+ }
240
248
  const toolCall = {
241
249
  id,
242
250
  type: 'function',
@@ -0,0 +1,17 @@
1
+ import type { JsonObject } from '../../hub/types/json.js';
2
+ import type { AdapterContext } from '../../hub/types/chat-envelope.js';
3
+ /**
4
+ * Gemini web_search 请求适配(作用于 Gemini 请求 payload,而不是 ChatEnvelope):
5
+ *
6
+ * - 仅在 routeId 以 `web_search` 开头时生效(来自 AdapterContext.routeId);
7
+ * - 针对 Gemini 请求中的 `tools` 字段进行清洗:
8
+ * - 保留 name === 'web_search' 的 functionDeclarations(如果存在);
9
+ * - 丢弃其它 Codex 自身工具(exec_command / MCP 等),避免 Cloud Code 报
10
+ * “Multiple tools are supported only when they are all search tools.”;
11
+ * - 如果清洗后没有任何工具,则删除 `tools` 字段。
12
+ *
13
+ * 注意:
14
+ * - 这里处理的是 llmswitch-core 为 gemini-chat 构造的中间 payload(buildGeminiRequestFromChat 输出);
15
+ * - 对于 Gemini CLI 后续在传输层添加的 googleSearch 工具,本适配不负责构造或修改。
16
+ */
17
+ export declare function applyGeminiWebSearchCompat(payload: JsonObject, adapterContext?: AdapterContext): JsonObject;
@@ -0,0 +1,68 @@
1
+ const isRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
2
+ /**
3
+ * Gemini web_search 请求适配(作用于 Gemini 请求 payload,而不是 ChatEnvelope):
4
+ *
5
+ * - 仅在 routeId 以 `web_search` 开头时生效(来自 AdapterContext.routeId);
6
+ * - 针对 Gemini 请求中的 `tools` 字段进行清洗:
7
+ * - 保留 name === 'web_search' 的 functionDeclarations(如果存在);
8
+ * - 丢弃其它 Codex 自身工具(exec_command / MCP 等),避免 Cloud Code 报
9
+ * “Multiple tools are supported only when they are all search tools.”;
10
+ * - 如果清洗后没有任何工具,则删除 `tools` 字段。
11
+ *
12
+ * 注意:
13
+ * - 这里处理的是 llmswitch-core 为 gemini-chat 构造的中间 payload(buildGeminiRequestFromChat 输出);
14
+ * - 对于 Gemini CLI 后续在传输层添加的 googleSearch 工具,本适配不负责构造或修改。
15
+ */
16
+ export function applyGeminiWebSearchCompat(payload, adapterContext) {
17
+ const routeId = typeof adapterContext?.routeId === 'string' ? adapterContext.routeId : '';
18
+ if (!routeId || !routeId.toLowerCase().startsWith('web_search')) {
19
+ return payload;
20
+ }
21
+ const cloned = { ...payload };
22
+ // 对于 Gemini Chat 后端,请求根节点不支持自定义 `web_search` 字段,
23
+ // 统一在 compat 层移除,避免触发
24
+ // "Unknown name \"web_search\" at 'request': Cannot find field." 错误。
25
+ if (cloned.web_search !== undefined) {
26
+ delete cloned.web_search;
27
+ }
28
+ const toolsRaw = cloned.tools;
29
+ // 当 web_search 路由下完全没有 tools 时,为 Gemini 搜索模型注入最小 googleSearch 工具,
30
+ // 保持与 gcli2api 类似的“搜索模型自动启用 Search 工具”行为。
31
+ if (!Array.isArray(toolsRaw)) {
32
+ cloned.tools = [{ googleSearch: {} }];
33
+ return cloned;
34
+ }
35
+ const nextTools = [];
36
+ for (const entry of toolsRaw) {
37
+ if (!isRecord(entry))
38
+ continue;
39
+ // 1) 保留 name === 'web_search' 的 functionDeclarations(单函数声明)。
40
+ const decls = Array.isArray(entry.functionDeclarations)
41
+ ? entry.functionDeclarations
42
+ : [];
43
+ const webSearchDecls = decls.filter((fn) => {
44
+ if (!isRecord(fn))
45
+ return false;
46
+ const name = typeof fn.name === 'string' ? fn.name.trim().toLowerCase() : '';
47
+ return name === 'web_search';
48
+ });
49
+ if (webSearchDecls.length) {
50
+ nextTools.push({
51
+ functionDeclarations: webSearchDecls
52
+ });
53
+ continue;
54
+ }
55
+ // 2) 若上游已经构造了 googleSearch 工具,则保留。
56
+ if (isRecord(entry.googleSearch)) {
57
+ nextTools.push({ googleSearch: entry.googleSearch });
58
+ }
59
+ }
60
+ if (nextTools.length > 0) {
61
+ cloned.tools = nextTools;
62
+ }
63
+ else {
64
+ // 3) 清洗后没有任何工具时,再次注入一个最小 googleSearch 工具,确保搜索模型仍然启用 Search。
65
+ cloned.tools = [{ googleSearch: {} }];
66
+ }
67
+ return cloned;
68
+ }
@@ -0,0 +1,2 @@
1
+ import type { JsonObject } from '../../hub/types/json.js';
2
+ export declare function applyGlmImageContentTransform(payload: JsonObject): JsonObject;
@@ -0,0 +1,83 @@
1
+ const isRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
2
+ function normalizeImagePart(part) {
3
+ const rawType = typeof part.type === 'string' ? part.type.toLowerCase() : '';
4
+ if (rawType !== 'image' && rawType !== 'image_url') {
5
+ return null;
6
+ }
7
+ // Try multiple locations for the URL, since different inbound
8
+ // protocols may populate different keys.
9
+ const imageUrlBlock = isRecord(part.image_url)
10
+ ? part.image_url
11
+ : undefined;
12
+ let url;
13
+ if (imageUrlBlock && typeof imageUrlBlock.url === 'string') {
14
+ url = imageUrlBlock.url;
15
+ }
16
+ else if (typeof part.image_url === 'string') {
17
+ url = part.image_url;
18
+ }
19
+ else if (typeof part.url === 'string') {
20
+ url = part.url;
21
+ }
22
+ else if (typeof part.uri === 'string') {
23
+ url = part.uri;
24
+ }
25
+ else if (typeof part.data === 'string') {
26
+ // If caller passed a raw base64/data URI string, forward as-is.
27
+ url = part.data;
28
+ }
29
+ if (!url || !url.trim().length) {
30
+ return null;
31
+ }
32
+ const normalized = {
33
+ type: 'image_url',
34
+ image_url: {
35
+ url: url.trim()
36
+ }
37
+ };
38
+ // Preserve a best-effort "detail" field when present.
39
+ const detailValue = (imageUrlBlock && imageUrlBlock.detail) ?? part.detail;
40
+ if (typeof detailValue === 'string' && detailValue.trim().length) {
41
+ normalized.image_url.detail = detailValue.trim();
42
+ }
43
+ return normalized;
44
+ }
45
+ export function applyGlmImageContentTransform(payload) {
46
+ const root = structuredClone(payload);
47
+ const messagesValue = root.messages;
48
+ if (!Array.isArray(messagesValue)) {
49
+ return root;
50
+ }
51
+ const messages = [];
52
+ for (const msg of messagesValue) {
53
+ if (!isRecord(msg)) {
54
+ messages.push(msg);
55
+ continue;
56
+ }
57
+ const contentValue = msg.content;
58
+ if (!Array.isArray(contentValue)) {
59
+ messages.push(msg);
60
+ continue;
61
+ }
62
+ const newContent = [];
63
+ for (const part of contentValue) {
64
+ if (!isRecord(part)) {
65
+ newContent.push(part);
66
+ continue;
67
+ }
68
+ const normalizedImage = normalizeImagePart(part);
69
+ if (normalizedImage) {
70
+ newContent.push(normalizedImage);
71
+ }
72
+ else {
73
+ newContent.push(part);
74
+ }
75
+ }
76
+ messages.push({
77
+ ...msg,
78
+ content: newContent
79
+ });
80
+ }
81
+ root.messages = messages;
82
+ return root;
83
+ }
@@ -0,0 +1,11 @@
1
+ import type { JsonObject } from '../../hub/types/json.js';
2
+ /**
3
+ * GLM 4.6V 专用视觉提示裁剪:
4
+ * - 仅在 model 为 glm-4.6v 且存在携带图片的 user 消息时生效;
5
+ * - 丢弃原有 system 与历史对话,只保留一条新的 system + 一条 user;
6
+ * - system:要求模型以结构化 JSON 描述截图内容、标记(圈/箭头等)及其大致 bbox;
7
+ * - user:保留原始用户文字(如果有)+ 单一 image_url 块。
8
+ *
9
+ * 其他模型(包括 glm-4.7、Gemini 等)不受影响。
10
+ */
11
+ export declare function applyGlmVisionPromptTransform(payload: JsonObject): JsonObject;
@@ -0,0 +1,177 @@
1
+ const isRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
2
+ function extractImageUrlFromPart(part) {
3
+ const imageUrlBlock = isRecord(part.image_url)
4
+ ? part.image_url
5
+ : undefined;
6
+ let url;
7
+ if (imageUrlBlock && typeof imageUrlBlock.url === 'string') {
8
+ url = imageUrlBlock.url;
9
+ }
10
+ else if (typeof part.image_url === 'string') {
11
+ url = part.image_url;
12
+ }
13
+ else if (typeof part.url === 'string') {
14
+ url = part.url;
15
+ }
16
+ else if (typeof part.uri === 'string') {
17
+ url = part.uri;
18
+ }
19
+ else if (typeof part.data === 'string') {
20
+ url = part.data;
21
+ }
22
+ const trimmed = (url ?? '').trim();
23
+ return trimmed.length ? trimmed : null;
24
+ }
25
+ function collectUserTextFromMessage(msg) {
26
+ const content = msg.content;
27
+ if (!Array.isArray(content)) {
28
+ if (typeof content === 'string') {
29
+ return content;
30
+ }
31
+ return '';
32
+ }
33
+ const parts = [];
34
+ for (const entry of content) {
35
+ if (!isRecord(entry))
36
+ continue;
37
+ const text = entry.text;
38
+ if (typeof text === 'string' && text.trim().length) {
39
+ parts.push(text.trim());
40
+ }
41
+ }
42
+ return parts.join('\n');
43
+ }
44
+ /**
45
+ * GLM 4.6V 专用视觉提示裁剪:
46
+ * - 仅在 model 为 glm-4.6v 且存在携带图片的 user 消息时生效;
47
+ * - 丢弃原有 system 与历史对话,只保留一条新的 system + 一条 user;
48
+ * - system:要求模型以结构化 JSON 描述截图内容、标记(圈/箭头等)及其大致 bbox;
49
+ * - user:保留原始用户文字(如果有)+ 单一 image_url 块。
50
+ *
51
+ * 其他模型(包括 glm-4.7、Gemini 等)不受影响。
52
+ */
53
+ export function applyGlmVisionPromptTransform(payload) {
54
+ const root = structuredClone(payload);
55
+ const modelRaw = root.model;
56
+ const model = typeof modelRaw === 'string' ? modelRaw.trim() : '';
57
+ if (!model.startsWith('glm-4.6v')) {
58
+ return root;
59
+ }
60
+ const messagesValue = root.messages;
61
+ if (!Array.isArray(messagesValue)) {
62
+ return root;
63
+ }
64
+ const messages = messagesValue.filter((msg) => isRecord(msg));
65
+ if (!messages.length) {
66
+ return root;
67
+ }
68
+ // 从末尾开始查找最近一条带图片的 user 消息。
69
+ let latestUserWithImage = null;
70
+ let imageUrl = null;
71
+ for (let i = messages.length - 1; i >= 0; i -= 1) {
72
+ const msg = messages[i];
73
+ const role = typeof msg.role === 'string' ? msg.role.toLowerCase() : '';
74
+ if (role !== 'user') {
75
+ continue;
76
+ }
77
+ const content = msg.content;
78
+ if (!Array.isArray(content)) {
79
+ continue;
80
+ }
81
+ for (const part of content) {
82
+ if (!isRecord(part))
83
+ continue;
84
+ const typeValue = typeof part.type === 'string' ? part.type.toLowerCase() : '';
85
+ if (typeValue === 'image' || typeValue === 'image_url' || typeValue === 'input_image') {
86
+ const candidateUrl = extractImageUrlFromPart(part);
87
+ if (candidateUrl) {
88
+ latestUserWithImage = msg;
89
+ imageUrl = candidateUrl;
90
+ break;
91
+ }
92
+ }
93
+ }
94
+ if (latestUserWithImage && imageUrl) {
95
+ break;
96
+ }
97
+ }
98
+ if (!latestUserWithImage || !imageUrl) {
99
+ return root;
100
+ }
101
+ const systemContent = '你是 Codex 的截图理解子系统,专门用于分析 UI 截图和网页图片。请仅根据用户提供的图片,输出一个结构化的 JSON,用于后续自动化处理,不要输出额外解释性文字或自然语言说明。\n' +
102
+ '\n' +
103
+ '输出必须是**单个合法 JSON 对象**,不要包含 Markdown、代码块标记或多余文本。请严格遵循下面的结构(字段可以为空数组,但必须存在):\n' +
104
+ '{\n' +
105
+ ' "summary": "用 1-3 句整体描述这张图片(例如页面类型、主要区域、核心信息)",\n' +
106
+ ' "marks": [\n' +
107
+ ' {\n' +
108
+ ' "type": "circle | arrow | underline | box | other",\n' +
109
+ ' "color": "red | green | blue | yellow | other",\n' +
110
+ ' "bbox": [x, y, width, height],\n' +
111
+ ' "description": "该标记所圈出/指向/强调的内容,包含相关文字或 UI 元素描述"\n' +
112
+ ' }\n' +
113
+ ' ],\n' +
114
+ ' "regions": [\n' +
115
+ ' {\n' +
116
+ ' "bbox": [x, y, width, height],\n' +
117
+ ' "description": "该区域的可见内容(控件/图标/布局,以及其中出现的所有清晰可辨的文字)",\n' +
118
+ ' "is_marked": true | false\n' +
119
+ ' }\n' +
120
+ ' ],\n' +
121
+ ' "metadata": {\n' +
122
+ ' "image_size_hint": "如果能推断出大致分辨率,请给出类似 1920x1080 的字符串;无法判断时用 null",\n' +
123
+ ' "screenshot": true\n' +
124
+ ' }\n' +
125
+ '}\n' +
126
+ '\n' +
127
+ '细则:\n' +
128
+ '1. 文字提取要求:\n' +
129
+ ' - 如果图片中存在清晰可辨的文字(包括标题、菜单、按钮、标签、提示信息、弹窗、错误信息等),必须在对应的 regions.description 中**完整抄写**这些文字,按自然阅读顺序组织,避免遗漏。\n' +
130
+ ' - 如果有多行文字,可以用换行符分隔,但仍放在同一个 description 字段中。\n' +
131
+ ' - 对确实无法看清的文字,用类似 "(模糊,无法辨认)" 标注即可;没有任何文字也视为正常情况,此时只需描述界面结构。\n' +
132
+ '2. 标记识别(marks):\n' +
133
+ ' - 对所有明显的圈选、箭头、下划线、高亮框等标记,必须在 marks 中列出,每一项提供大致 bbox、颜色和简短说明,说明其强调或指向的内容。\n' +
134
+ '3. 区域划分(regions):\n' +
135
+ ' - 将截图拆分为若干有意义的区域:如导航栏、侧边栏、主内容区、弹窗、对话框、表格、代码块、表单等。\n' +
136
+ ' - 每个区域的 description 中,既要描述布局/控件类型,也要包含该区域内的全部清晰文字内容。\n' +
137
+ ' - is_marked 为 true 表示该区域与某个标记(marks)相关或被标记强调。\n' +
138
+ '4. 坐标规范:所有 bbox 使用相对于当前图片的像素坐标,左上角为 (0,0),width/height 为正数近似值。\n' +
139
+ '5. 无论图片内容如何,最终回答必须是合法 JSON,不能在 JSON 前后添加任何额外文本。';
140
+ const systemMessage = {
141
+ role: 'system',
142
+ content: systemContent
143
+ };
144
+ const originalUserText = collectUserTextFromMessage(latestUserWithImage);
145
+ const userBlocks = [];
146
+ if (originalUserText && originalUserText.trim().length) {
147
+ userBlocks.push({
148
+ type: 'text',
149
+ text: originalUserText.trim()
150
+ });
151
+ }
152
+ else {
153
+ userBlocks.push({
154
+ type: 'text',
155
+ text: '请按照上面的 JSON 结构,详细描述这张图片的内容和标记。'
156
+ });
157
+ }
158
+ userBlocks.push({
159
+ type: 'image_url',
160
+ image_url: {
161
+ url: imageUrl
162
+ }
163
+ });
164
+ const userMessage = {
165
+ role: 'user',
166
+ content: userBlocks
167
+ };
168
+ // 丢弃原有 messages,仅保留新的 system + user。
169
+ root.messages = [systemMessage, userMessage];
170
+ // 对于专用视觉模型,限制 max_tokens,避免过大的 completion 预算进一步触发上下文相关错误。
171
+ const maxTokensValue = root.max_tokens;
172
+ if (typeof maxTokensValue === 'number' && Number.isFinite(maxTokensValue)) {
173
+ // 将超大值收敛到一个相对安全的上限;真实上限由上游再校验。
174
+ root.max_tokens = Math.min(maxTokensValue, 4096);
175
+ }
176
+ return root;
177
+ }
@@ -1,4 +1,5 @@
1
1
  const isRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
2
+ const DEBUG_GLM_WEB_SEARCH = (process.env.ROUTECODEX_DEBUG_GLM_WEB_SEARCH || '').trim() === '1';
2
3
  export function applyGlmWebSearchRequestTransform(payload) {
3
4
  const root = structuredClone(payload);
4
5
  const webSearchRaw = root.web_search;
@@ -23,19 +24,13 @@ export function applyGlmWebSearchRequestTransform(payload) {
23
24
  delete root.web_search;
24
25
  return root;
25
26
  }
26
- const toolsValue = root.tools;
27
- const tools = Array.isArray(toolsValue) ? [...toolsValue] : [];
28
- let existingIndex = -1;
29
- for (let i = 0; i < tools.length; i += 1) {
30
- const tool = tools[i];
31
- if (isRecord(tool) && typeof tool.type === 'string' && tool.type === 'web_search') {
32
- existingIndex = i;
33
- break;
34
- }
35
- }
36
27
  const webSearchConfig = {
28
+ // 按 GLM 文档要求:search_engine 为必填,默认使用 search_std。
29
+ search_engine: 'search_std',
37
30
  enable: true,
38
- search_query: query
31
+ search_query: query,
32
+ // 返回搜索结果详情,便于我们在响应中提取摘要或调试。
33
+ search_result: true
39
34
  };
40
35
  if (recency) {
41
36
  webSearchConfig.search_recency_filter = recency;
@@ -43,24 +38,26 @@ export function applyGlmWebSearchRequestTransform(payload) {
43
38
  if (typeof count === 'number') {
44
39
  webSearchConfig.count = count;
45
40
  }
46
- const baseTool = existingIndex >= 0 && isRecord(tools[existingIndex])
47
- ? { ...tools[existingIndex] }
48
- : {};
49
- baseTool.type = 'web_search';
50
- const existingWebSearch = isRecord(baseTool.web_search)
51
- ? baseTool.web_search
52
- : {};
53
- baseTool.web_search = {
54
- ...existingWebSearch,
55
- ...webSearchConfig
41
+ // 根据 OpenAPI:tools.anyOf[*] = WebSearchToolSchema[],即一次只能选择一种工具类型。
42
+ // web_search 路由下,我们只保留 WebSearchToolSchema,丢弃其它 function/retrieval/MCP 工具,
43
+ // 避免混用导致后端忽略 web_search 配置。
44
+ const baseTool = {
45
+ type: 'web_search',
46
+ web_search: webSearchConfig
56
47
  };
57
- if (existingIndex >= 0) {
58
- tools[existingIndex] = baseTool;
59
- }
60
- else {
61
- tools.push(baseTool);
62
- }
63
- root.tools = tools;
48
+ root.tools = [baseTool];
64
49
  delete root.web_search;
50
+ if (DEBUG_GLM_WEB_SEARCH) {
51
+ try {
52
+ // eslint-disable-next-line no-console
53
+ console.log('\x1b[38;5;27m[compat][glm_web_search_request] applied web_search transform ' +
54
+ `search_engine=${String(baseTool?.web_search?.search_engine ?? 'search_std')} ` +
55
+ `query=${JSON.stringify(query).slice(0, 200)} ` +
56
+ `count=${String(count ?? '')}\x1b[0m`);
57
+ }
58
+ catch {
59
+ // logging best-effort
60
+ }
61
+ }
65
62
  return root;
66
63
  }
@@ -127,6 +127,17 @@ export class UniversalShapeFilter {
127
127
  normalized.tool_call_id = msg.tool_call_id;
128
128
  }
129
129
  }
130
+ else if (Array.isArray(msg.content)) {
131
+ normalized.content = msg.content.map((part) => {
132
+ if (typeof part === 'string') {
133
+ return part;
134
+ }
135
+ if (isRecord(part)) {
136
+ return { ...part };
137
+ }
138
+ return part;
139
+ });
140
+ }
130
141
  else {
131
142
  normalized.content = (msg.content !== null && msg.content !== undefined) ? String(msg.content) : '';
132
143
  }
@@ -0,0 +1,17 @@
1
+ {
2
+ "id": "chat:gemini",
3
+ "protocol": "gemini-chat",
4
+ "request": {
5
+ "mappings": [
6
+ { "action": "snapshot", "phase": "compat-pre" },
7
+ {
8
+ "action": "gemini_web_search_request"
9
+ },
10
+ { "action": "snapshot", "phase": "compat-post" }
11
+ ]
12
+ },
13
+ "response": {
14
+ "mappings": []
15
+ }
16
+ }
17
+