@jsonstudio/rcc 0.89.333 → 0.89.548

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 (191) hide show
  1. package/dist/build-info.js +3 -3
  2. package/dist/build-info.js.map +1 -1
  3. package/dist/cli.js +110 -1
  4. package/dist/cli.js.map +1 -1
  5. package/dist/commands/token-daemon.d.ts +2 -0
  6. package/dist/commands/token-daemon.js +183 -0
  7. package/dist/commands/token-daemon.js.map +1 -0
  8. package/dist/index.js +20 -3
  9. package/dist/index.js.map +1 -1
  10. package/dist/modules/llmswitch/bridge.d.ts +1 -1
  11. package/dist/modules/llmswitch/bridge.js +3 -2
  12. package/dist/modules/llmswitch/bridge.js.map +1 -1
  13. package/dist/modules/pipeline/utils/colored-logger.js +3 -1
  14. package/dist/modules/pipeline/utils/colored-logger.js.map +1 -1
  15. package/dist/providers/auth/gemini-cli-userinfo-helper.js +12 -2
  16. package/dist/providers/auth/gemini-cli-userinfo-helper.js.map +1 -1
  17. package/dist/providers/auth/oauth-lifecycle.js +337 -25
  18. package/dist/providers/auth/oauth-lifecycle.js.map +1 -1
  19. package/dist/providers/core/config/oauth-flows.d.ts +23 -0
  20. package/dist/providers/core/config/oauth-flows.js +92 -5
  21. package/dist/providers/core/config/oauth-flows.js.map +1 -1
  22. package/dist/providers/core/config/provider-oauth-configs.js +9 -3
  23. package/dist/providers/core/config/provider-oauth-configs.js.map +1 -1
  24. package/dist/providers/core/config/service-profiles.js +18 -10
  25. package/dist/providers/core/config/service-profiles.js.map +1 -1
  26. package/dist/providers/core/runtime/base-provider.d.ts +2 -0
  27. package/dist/providers/core/runtime/base-provider.js +35 -1
  28. package/dist/providers/core/runtime/base-provider.js.map +1 -1
  29. package/dist/providers/core/runtime/gemini-cli-http-provider.js +87 -20
  30. package/dist/providers/core/runtime/gemini-cli-http-provider.js.map +1 -1
  31. package/dist/providers/core/runtime/http-request-executor.d.ts +1 -0
  32. package/dist/providers/core/runtime/http-request-executor.js +75 -1
  33. package/dist/providers/core/runtime/http-request-executor.js.map +1 -1
  34. package/dist/providers/core/runtime/http-transport-provider.d.ts +2 -0
  35. package/dist/providers/core/runtime/http-transport-provider.js +60 -2
  36. package/dist/providers/core/runtime/http-transport-provider.js.map +1 -1
  37. package/dist/providers/core/runtime/iflow-http-provider.d.ts +4 -0
  38. package/dist/providers/core/runtime/iflow-http-provider.js +28 -0
  39. package/dist/providers/core/runtime/iflow-http-provider.js.map +1 -1
  40. package/dist/providers/core/runtime/rate-limit-manager.d.ts +30 -0
  41. package/dist/providers/core/runtime/rate-limit-manager.js +136 -0
  42. package/dist/providers/core/runtime/rate-limit-manager.js.map +1 -0
  43. package/dist/providers/core/runtime/responses-provider.js +8 -3
  44. package/dist/providers/core/runtime/responses-provider.js.map +1 -1
  45. package/dist/providers/core/runtime/vision-debug-utils.d.ts +13 -0
  46. package/dist/providers/core/runtime/vision-debug-utils.js +114 -0
  47. package/dist/providers/core/runtime/vision-debug-utils.js.map +1 -0
  48. package/dist/providers/core/strategies/oauth-auth-code-flow.js +75 -26
  49. package/dist/providers/core/strategies/oauth-auth-code-flow.js.map +1 -1
  50. package/dist/providers/core/utils/http-client.js +2 -1
  51. package/dist/providers/core/utils/http-client.js.map +1 -1
  52. package/dist/providers/core/utils/provider-error-reporter.js +31 -5
  53. package/dist/providers/core/utils/provider-error-reporter.js.map +1 -1
  54. package/dist/providers/core/utils/provider-type-utils.js +1 -1
  55. package/dist/providers/core/utils/provider-type-utils.js.map +1 -1
  56. package/dist/providers/core/utils/snapshot-writer.d.ts +1 -1
  57. package/dist/providers/core/utils/snapshot-writer.js.map +1 -1
  58. package/dist/server/handlers/sse-dispatcher.js +22 -2
  59. package/dist/server/handlers/sse-dispatcher.js.map +1 -1
  60. package/dist/server/runtime/http-server/index.d.ts +9 -0
  61. package/dist/server/runtime/http-server/index.js +512 -144
  62. package/dist/server/runtime/http-server/index.js.map +1 -1
  63. package/dist/server/runtime/http-server/provider-utils.js +1 -1
  64. package/dist/server/runtime/http-server/provider-utils.js.map +1 -1
  65. package/dist/server/runtime/http-server/request-executor.d.ts +10 -0
  66. package/dist/server/runtime/http-server/request-executor.js +553 -159
  67. package/dist/server/runtime/http-server/request-executor.js.map +1 -1
  68. package/dist/server/runtime/http-server/routes.d.ts +5 -0
  69. package/dist/server/runtime/http-server/routes.js +29 -0
  70. package/dist/server/runtime/http-server/routes.js.map +1 -1
  71. package/dist/server/runtime/http-server/runtime-manager.js +33 -0
  72. package/dist/server/runtime/http-server/runtime-manager.js.map +1 -1
  73. package/dist/server/utils/utf8-chunk-buffer.d.ts +43 -0
  74. package/dist/server/utils/utf8-chunk-buffer.js +132 -0
  75. package/dist/server/utils/utf8-chunk-buffer.js.map +1 -0
  76. package/dist/token-daemon/history-store.d.ts +75 -0
  77. package/dist/token-daemon/history-store.js +207 -0
  78. package/dist/token-daemon/history-store.js.map +1 -0
  79. package/dist/token-daemon/index.d.ts +7 -0
  80. package/dist/token-daemon/index.js +336 -0
  81. package/dist/token-daemon/index.js.map +1 -0
  82. package/dist/token-daemon/server-utils.d.ts +33 -0
  83. package/dist/token-daemon/server-utils.js +155 -0
  84. package/dist/token-daemon/server-utils.js.map +1 -0
  85. package/dist/token-daemon/token-daemon.d.ts +23 -0
  86. package/dist/token-daemon/token-daemon.js +249 -0
  87. package/dist/token-daemon/token-daemon.js.map +1 -0
  88. package/dist/token-daemon/token-types.d.ts +44 -0
  89. package/dist/token-daemon/token-types.js +18 -0
  90. package/dist/token-daemon/token-types.js.map +1 -0
  91. package/dist/token-daemon/token-utils.d.ts +17 -0
  92. package/dist/token-daemon/token-utils.js +153 -0
  93. package/dist/token-daemon/token-utils.js.map +1 -0
  94. package/dist/token-portal/local-token-portal.d.ts +1 -0
  95. package/dist/token-portal/local-token-portal.js +89 -0
  96. package/dist/token-portal/local-token-portal.js.map +1 -0
  97. package/dist/token-portal/render.d.ts +10 -0
  98. package/dist/token-portal/render.js +56 -0
  99. package/dist/token-portal/render.js.map +1 -0
  100. package/dist/tools/semantic-replay.js +7 -6
  101. package/dist/tools/semantic-replay.js.map +1 -1
  102. package/dist/utils/error-handler-registry.d.ts +36 -0
  103. package/dist/utils/error-handler-registry.js +93 -7
  104. package/dist/utils/error-handler-registry.js.map +1 -1
  105. package/node_modules/@jsonstudio/llms/README.md +2 -0
  106. package/node_modules/@jsonstudio/llms/dist/conversion/codecs/gemini-openai-codec.js +137 -5
  107. package/node_modules/@jsonstudio/llms/dist/conversion/compat/actions/gemini-web-search.d.ts +17 -0
  108. package/node_modules/@jsonstudio/llms/dist/conversion/compat/actions/gemini-web-search.js +68 -0
  109. package/node_modules/@jsonstudio/llms/dist/conversion/compat/actions/glm-image-content.d.ts +2 -0
  110. package/node_modules/@jsonstudio/llms/dist/conversion/compat/actions/glm-image-content.js +83 -0
  111. package/node_modules/@jsonstudio/llms/dist/conversion/compat/actions/glm-vision-prompt.d.ts +11 -0
  112. package/node_modules/@jsonstudio/llms/dist/conversion/compat/actions/glm-vision-prompt.js +177 -0
  113. package/node_modules/@jsonstudio/llms/dist/conversion/compat/actions/glm-web-search.d.ts +2 -0
  114. package/node_modules/@jsonstudio/llms/dist/conversion/compat/actions/glm-web-search.js +63 -0
  115. package/node_modules/@jsonstudio/llms/dist/conversion/compat/actions/universal-shape-filter.js +11 -0
  116. package/node_modules/@jsonstudio/llms/dist/conversion/compat/profiles/chat-gemini.json +17 -0
  117. package/node_modules/@jsonstudio/llms/dist/conversion/compat/profiles/chat-glm.json +190 -181
  118. package/node_modules/@jsonstudio/llms/dist/conversion/compat/profiles/chat-iflow.json +195 -195
  119. package/node_modules/@jsonstudio/llms/dist/conversion/compat/profiles/chat-lmstudio.json +43 -43
  120. package/node_modules/@jsonstudio/llms/dist/conversion/compat/profiles/chat-qwen.json +20 -20
  121. package/node_modules/@jsonstudio/llms/dist/conversion/compat/profiles/responses-c4m.json +42 -42
  122. package/node_modules/@jsonstudio/llms/dist/conversion/config/sample-config.json +1 -1
  123. package/node_modules/@jsonstudio/llms/dist/conversion/hub/pipeline/compat/compat-pipeline-executor.js +24 -0
  124. package/node_modules/@jsonstudio/llms/dist/conversion/hub/pipeline/compat/compat-types.d.ts +8 -0
  125. package/node_modules/@jsonstudio/llms/dist/conversion/hub/pipeline/hub-pipeline.js +39 -4
  126. package/node_modules/@jsonstudio/llms/dist/conversion/hub/pipeline/target-utils.js +6 -0
  127. package/node_modules/@jsonstudio/llms/dist/conversion/hub/process/chat-process.js +213 -1
  128. package/node_modules/@jsonstudio/llms/dist/conversion/hub/response/provider-response.d.ts +34 -0
  129. package/node_modules/@jsonstudio/llms/dist/conversion/hub/response/provider-response.js +84 -24
  130. package/node_modules/@jsonstudio/llms/dist/conversion/hub/response/server-side-tools.d.ts +26 -0
  131. package/node_modules/@jsonstudio/llms/dist/conversion/hub/response/server-side-tools.js +383 -0
  132. package/node_modules/@jsonstudio/llms/dist/conversion/hub/semantic-mappers/gemini-mapper.js +241 -14
  133. package/node_modules/@jsonstudio/llms/dist/conversion/hub/semantic-mappers/responses-mapper.js +17 -1
  134. package/node_modules/@jsonstudio/llms/dist/conversion/hub/standardized-bridge.js +14 -0
  135. package/node_modules/@jsonstudio/llms/dist/conversion/hub/types/standardized.d.ts +1 -0
  136. package/node_modules/@jsonstudio/llms/dist/conversion/responses/responses-openai-bridge.js +82 -3
  137. package/node_modules/@jsonstudio/llms/dist/conversion/shared/anthropic-message-utils.js +92 -3
  138. package/node_modules/@jsonstudio/llms/dist/conversion/shared/bridge-message-utils.js +137 -10
  139. package/node_modules/@jsonstudio/llms/dist/conversion/shared/responses-output-builder.js +43 -2
  140. package/node_modules/@jsonstudio/llms/dist/conversion/shared/snapshot-utils.js +17 -47
  141. package/node_modules/@jsonstudio/llms/dist/conversion/shared/tool-filter-pipeline.js +1 -0
  142. package/node_modules/@jsonstudio/llms/dist/conversion/shared/tool-mapping.js +25 -2
  143. package/node_modules/@jsonstudio/llms/dist/index.d.ts +1 -0
  144. package/node_modules/@jsonstudio/llms/dist/index.js +1 -0
  145. package/node_modules/@jsonstudio/llms/dist/router/virtual-router/bootstrap.js +308 -43
  146. package/node_modules/@jsonstudio/llms/dist/router/virtual-router/classifier.js +11 -17
  147. package/node_modules/@jsonstudio/llms/dist/router/virtual-router/context-advisor.d.ts +0 -2
  148. package/node_modules/@jsonstudio/llms/dist/router/virtual-router/context-advisor.js +0 -12
  149. package/node_modules/@jsonstudio/llms/dist/router/virtual-router/engine.d.ts +17 -2
  150. package/node_modules/@jsonstudio/llms/dist/router/virtual-router/engine.js +332 -95
  151. package/node_modules/@jsonstudio/llms/dist/router/virtual-router/features.js +1 -1
  152. package/node_modules/@jsonstudio/llms/dist/router/virtual-router/message-utils.js +36 -24
  153. package/node_modules/@jsonstudio/llms/dist/router/virtual-router/provider-registry.js +2 -1
  154. package/node_modules/@jsonstudio/llms/dist/router/virtual-router/token-counter.js +14 -3
  155. package/node_modules/@jsonstudio/llms/dist/router/virtual-router/types.d.ts +66 -2
  156. package/node_modules/@jsonstudio/llms/dist/router/virtual-router/types.js +2 -1
  157. package/node_modules/@jsonstudio/llms/dist/servertool/engine.d.ts +27 -0
  158. package/node_modules/@jsonstudio/llms/dist/servertool/engine.js +60 -0
  159. package/node_modules/@jsonstudio/llms/dist/servertool/flow-types.d.ts +40 -0
  160. package/node_modules/@jsonstudio/llms/dist/servertool/flow-types.js +1 -0
  161. package/node_modules/@jsonstudio/llms/dist/servertool/handlers/vision.d.ts +1 -0
  162. package/node_modules/@jsonstudio/llms/dist/servertool/handlers/vision.js +194 -0
  163. package/node_modules/@jsonstudio/llms/dist/servertool/handlers/web-search.d.ts +1 -0
  164. package/node_modules/@jsonstudio/llms/dist/servertool/handlers/web-search.js +638 -0
  165. package/node_modules/@jsonstudio/llms/dist/servertool/orchestration-types.d.ts +33 -0
  166. package/node_modules/@jsonstudio/llms/dist/servertool/orchestration-types.js +1 -0
  167. package/node_modules/@jsonstudio/llms/dist/servertool/registry.d.ts +18 -0
  168. package/node_modules/@jsonstudio/llms/dist/servertool/registry.js +27 -0
  169. package/node_modules/@jsonstudio/llms/dist/servertool/server-side-tools.d.ts +8 -0
  170. package/node_modules/@jsonstudio/llms/dist/servertool/server-side-tools.js +208 -0
  171. package/node_modules/@jsonstudio/llms/dist/servertool/types.d.ts +88 -0
  172. package/node_modules/@jsonstudio/llms/dist/servertool/types.js +1 -0
  173. package/node_modules/@jsonstudio/llms/dist/servertool/vision-tool.d.ts +2 -0
  174. package/node_modules/@jsonstudio/llms/dist/servertool/vision-tool.js +185 -0
  175. package/node_modules/@jsonstudio/llms/dist/sse/json-to-sse/event-generators/responses.js +15 -3
  176. package/node_modules/@jsonstudio/llms/dist/sse/sse-to-json/builders/response-builder.js +6 -3
  177. package/node_modules/@jsonstudio/llms/dist/sse/sse-to-json/gemini-sse-to-json-converter.js +27 -1
  178. package/node_modules/@jsonstudio/llms/dist/sse/types/gemini-types.d.ts +20 -1
  179. package/node_modules/@jsonstudio/llms/dist/sse/types/responses-types.js +1 -1
  180. package/node_modules/@jsonstudio/llms/dist/telemetry/stats-center.d.ts +73 -0
  181. package/node_modules/@jsonstudio/llms/dist/telemetry/stats-center.js +280 -0
  182. package/node_modules/@jsonstudio/llms/package.json +1 -1
  183. package/package.json +3 -2
  184. package/scripts/pack-mode.mjs +2 -1
  185. package/scripts/publish-rcc.mjs +20 -4
  186. package/scripts/test-iflow-web-search.mjs +141 -0
  187. package/scripts/test-iflow.mjs +93 -1
  188. package/scripts/tests/virtual-router-health.mjs +141 -6
  189. package/dist/tools/replay-request.d.ts +0 -0
  190. package/dist/tools/replay-request.js +0 -2
  191. package/dist/tools/replay-request.js.map +0 -1
@@ -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 {
@@ -146,17 +146,96 @@ function normalizeBridgeHistory(seed) {
146
146
  originalSystemMessages: systemMessages
147
147
  };
148
148
  }
149
+ function mergeResponsesTools(originalTools, fromChat) {
150
+ const result = [];
151
+ const byKey = new Map();
152
+ const norm = (value) => {
153
+ if (typeof value !== 'string')
154
+ return undefined;
155
+ const trimmed = value.trim();
156
+ return trimmed.length ? trimmed.toLowerCase() : undefined;
157
+ };
158
+ const register = (tool) => {
159
+ const fn = tool.function ?? tool.function;
160
+ const baseName = norm(fn?.name ?? tool.name);
161
+ const typeName = norm(tool.type);
162
+ const key = baseName || typeName;
163
+ if (!key || byKey.has(key))
164
+ return;
165
+ byKey.set(key, tool);
166
+ result.push(tool);
167
+ };
168
+ if (Array.isArray(fromChat)) {
169
+ for (const t of fromChat) {
170
+ if (t && typeof t === 'object') {
171
+ register(t);
172
+ }
173
+ }
174
+ }
175
+ if (Array.isArray(originalTools)) {
176
+ for (const t of originalTools) {
177
+ if (!t || typeof t !== 'object')
178
+ continue;
179
+ const typeName = norm(t.type);
180
+ const isWebSearch = typeName === 'web_search' || (typeof typeName === 'string' && typeName.startsWith('web_search'));
181
+ if (!isWebSearch) {
182
+ // 目前仅恢复原始载荷中的 builtin web_search 工具,其它非函数工具保持忽略,避免意外改变行为。
183
+ continue;
184
+ }
185
+ register(t);
186
+ }
187
+ }
188
+ return result.length ? result : undefined;
189
+ }
149
190
  export function buildResponsesRequestFromChat(payload, ctx, extras) {
150
191
  const chat = unwrapData(payload);
151
192
  const out = {};
193
+ const forceWebSearch = !!ctx &&
194
+ isObject(ctx.metadata) &&
195
+ isObject(ctx.metadata.webSearch) &&
196
+ ctx.metadata.webSearch.force === true;
152
197
  // 基本字段
153
198
  out.model = chat.model;
154
199
  // tools: 反向映射为 ResponsesToolDefinition 形状
155
- const responsesTools = mapChatToolsToBridge(chat.tools, {
200
+ const chatTools = Array.isArray(chat.tools) ? chat.tools : [];
201
+ // 对于 openai-responses upstream,内建 web_search 由官方服务器处理。
202
+ // Chat 侧注入的 server-side web_search 函数(带 engine/query/recency/count)
203
+ // 仅用于非 Responses provider 的 server-tool 回环;在这里构造真正的
204
+ // `/v1/responses` 请求时,需要:
205
+ // 1) 不再把函数版 web_search 透传上游;
206
+ // 2) 若检测到 Chat 侧启用了 web_search 且原始请求中没有 builtin web_search,
207
+ // 则补一个 `{ type: "web_search" }` 内建工具给 OpenAI Responses。
208
+ const hasServerSideWebSearch = !forceWebSearch && chatTools.some((tool) => {
209
+ const fn = tool && typeof tool === 'object' ? tool.function : undefined;
210
+ const name = typeof fn?.name === 'string' ? fn.name.trim().toLowerCase() : '';
211
+ return name === 'web_search';
212
+ });
213
+ const toolsForBridge = hasServerSideWebSearch
214
+ ? chatTools.filter((tool) => {
215
+ const fn = tool && typeof tool === 'object' ? tool.function : undefined;
216
+ const name = typeof fn?.name === 'string' ? fn.name.trim().toLowerCase() : '';
217
+ return name !== 'web_search';
218
+ })
219
+ : chatTools;
220
+ const responsesToolsFromChat = mapChatToolsToBridge(toolsForBridge, {
156
221
  sanitizeName: sanitizeResponsesFunctionName
157
222
  });
158
- if (responsesTools?.length) {
159
- out.tools = responsesTools;
223
+ // Prefer Chat‑normalized tools, but if the original Responses payload carried
224
+ // non‑function tools (such as builtin `web_search`), merge them back so that
225
+ // upstream `/v1/responses` providers see their original tool definitions.
226
+ const originalTools = Array.isArray(ctx?.toolsRaw) ? ctx.toolsRaw : undefined;
227
+ let mergedTools = mergeResponsesTools(originalTools, responsesToolsFromChat);
228
+ if (hasServerSideWebSearch) {
229
+ const normalizeType = (value) => typeof value === 'string' ? value.trim().toLowerCase() : '';
230
+ const hasBuiltinWebSearch = (mergedTools && mergedTools.some((tool) => normalizeType(tool.type) === 'web_search')) ||
231
+ (originalTools && originalTools.some((tool) => normalizeType(tool.type) === 'web_search'));
232
+ if (!hasBuiltinWebSearch) {
233
+ const injected = { type: 'web_search' };
234
+ mergedTools = mergedTools ? [...mergedTools, injected] : [injected];
235
+ }
236
+ }
237
+ if (mergedTools?.length) {
238
+ out.tools = mergedTools;
160
239
  }
161
240
  const passthroughKeys = [
162
241
  'tool_choice',
@@ -266,6 +266,7 @@ export function buildOpenAIChatFromAnthropic(payload) {
266
266
  continue;
267
267
  }
268
268
  const textParts = [];
269
+ const imageBlocks = [];
269
270
  const toolCalls = [];
270
271
  const reasoningParts = [];
271
272
  const toolResults = [];
@@ -284,6 +285,29 @@ export function buildOpenAIChatFromAnthropic(payload) {
284
285
  reasoningParts.push(thinkingText);
285
286
  }
286
287
  }
288
+ else if (t === 'image') {
289
+ const source = block.source;
290
+ if (source && typeof source === 'object') {
291
+ const s = source;
292
+ const srcType = typeof s.type === 'string' ? s.type.toLowerCase() : '';
293
+ let url;
294
+ if (srcType === 'url' && typeof s.url === 'string') {
295
+ url = s.url;
296
+ }
297
+ else if (srcType === 'base64' && typeof s.data === 'string') {
298
+ const mediaType = typeof s.media_type === 'string' && s.media_type.trim().length
299
+ ? s.media_type.trim()
300
+ : 'image/png';
301
+ url = `data:${mediaType};base64,${s.data}`;
302
+ }
303
+ if (url && url.trim().length) {
304
+ imageBlocks.push({
305
+ type: 'image_url',
306
+ image_url: { url: url.trim() }
307
+ });
308
+ }
309
+ }
310
+ }
287
311
  else if (t === 'tool_use') {
288
312
  const name = requireTrimmedString(block.name, 'tool_use.name');
289
313
  const id = requireTrimmedString(block.id, 'tool_use.id');
@@ -310,10 +334,22 @@ export function buildOpenAIChatFromAnthropic(payload) {
310
334
  }
311
335
  const hasText = typeof normalized.contentText === 'string' && normalized.contentText.length > 0;
312
336
  const hasReasoning = mergedReasoning.length > 0;
313
- if (hasText || hasRawText || toolCalls.length > 0 || hasReasoning) {
337
+ if (hasText || hasRawText || toolCalls.length > 0 || hasReasoning || imageBlocks.length > 0) {
338
+ let contentNode = (hasText ? normalized.contentText : undefined) ?? combinedText ?? '';
339
+ if (imageBlocks.length > 0) {
340
+ const blocks = [];
341
+ const textPayload = (hasText ? normalized.contentText : undefined) ?? combinedText ?? '';
342
+ if (typeof textPayload === 'string' && textPayload.trim().length) {
343
+ blocks.push({ type: 'text', text: textPayload.trim() });
344
+ }
345
+ for (const img of imageBlocks) {
346
+ blocks.push(jsonClone(img));
347
+ }
348
+ contentNode = blocks;
349
+ }
314
350
  const msg = {
315
351
  role,
316
- content: (hasText ? normalized.contentText : undefined) ?? combinedText ?? ''
352
+ content: contentNode
317
353
  };
318
354
  if (toolCalls.length)
319
355
  msg.tool_calls = toolCalls;
@@ -690,7 +726,8 @@ export function buildAnthropicRequestFromOpenAIChat(chatReq) {
690
726
  targetShape = mirrorShapes[mirrorIndex];
691
727
  mirrorIndex += 1;
692
728
  }
693
- const text = collectText(m.content).trim();
729
+ const contentNode = m.content;
730
+ const text = collectText(contentNode).trim();
694
731
  if (role === 'system') {
695
732
  if (!text) {
696
733
  throw new Error('Anthropic bridge constraint violated: Chat system message must contain text');
@@ -715,6 +752,58 @@ export function buildAnthropicRequestFromOpenAIChat(chatReq) {
715
752
  continue;
716
753
  }
717
754
  const blocks = [];
755
+ if (Array.isArray(contentNode)) {
756
+ // Preserve or synthesize image blocks where possible, and fall back to text for the rest.
757
+ for (const entry of contentNode) {
758
+ if (!entry || typeof entry !== 'object')
759
+ continue;
760
+ const node = entry;
761
+ const t = typeof node.type === 'string' ? node.type.toLowerCase() : '';
762
+ if (t === 'image' && node.source && typeof node.source === 'object') {
763
+ // Pass-through Anthropic image block as-is.
764
+ blocks.push({
765
+ type: 'image',
766
+ source: jsonClone(node.source)
767
+ });
768
+ continue;
769
+ }
770
+ if (t === 'image_url') {
771
+ let url = '';
772
+ const imageUrl = node.image_url;
773
+ if (typeof imageUrl === 'string') {
774
+ url = imageUrl;
775
+ }
776
+ else if (imageUrl && typeof imageUrl === 'object' && typeof imageUrl.url === 'string') {
777
+ url = imageUrl.url;
778
+ }
779
+ const trimmed = url.trim();
780
+ if (!trimmed.length)
781
+ continue;
782
+ const source = {};
783
+ if (trimmed.startsWith('data:')) {
784
+ const match = /^data:([^;,]+)?(?:;base64)?,(.*)$/s.exec(trimmed);
785
+ if (match) {
786
+ const mediaType = (match[1] || '').trim() || 'image/png';
787
+ source.type = 'base64';
788
+ source.media_type = mediaType;
789
+ source.data = match[2] || '';
790
+ }
791
+ else {
792
+ source.type = 'url';
793
+ source.url = trimmed;
794
+ }
795
+ }
796
+ else {
797
+ source.type = 'url';
798
+ source.url = trimmed;
799
+ }
800
+ blocks.push({
801
+ type: 'image',
802
+ source
803
+ });
804
+ }
805
+ }
806
+ }
718
807
  if (text) {
719
808
  blocks.push({ type: 'text', text });
720
809
  }
@@ -66,6 +66,59 @@ function collectText(value) {
66
66
  }
67
67
  return '';
68
68
  }
69
+ function extractImageBlocksFromContent(content) {
70
+ const images = [];
71
+ const visit = (value) => {
72
+ if (!value)
73
+ return;
74
+ if (Array.isArray(value)) {
75
+ for (const entry of value)
76
+ visit(entry);
77
+ return;
78
+ }
79
+ if (typeof value !== 'object') {
80
+ return;
81
+ }
82
+ const record = value;
83
+ const typeValue = typeof record.type === 'string' ? record.type.toLowerCase() : '';
84
+ if (typeValue === 'image' || typeValue === 'image_url' || typeValue === 'input_image') {
85
+ let url = '';
86
+ const imageUrl = record.image_url;
87
+ if (typeof imageUrl === 'string') {
88
+ url = imageUrl;
89
+ }
90
+ else if (imageUrl && typeof imageUrl === 'object' && typeof imageUrl.url === 'string') {
91
+ url = imageUrl.url;
92
+ }
93
+ else if (typeof record.url === 'string') {
94
+ url = record.url;
95
+ }
96
+ else if (typeof record.uri === 'string') {
97
+ url = record.uri;
98
+ }
99
+ else if (typeof record.data === 'string') {
100
+ url = record.data;
101
+ }
102
+ const trimmed = url.trim();
103
+ if (trimmed.length) {
104
+ let detail;
105
+ if (imageUrl && typeof imageUrl === 'object' && typeof imageUrl.detail === 'string') {
106
+ detail = imageUrl.detail.trim() || undefined;
107
+ }
108
+ else if (typeof record.detail === 'string') {
109
+ detail = record.detail.trim() || undefined;
110
+ }
111
+ images.push({ url: trimmed, detail });
112
+ }
113
+ return;
114
+ }
115
+ if (Array.isArray(record.content)) {
116
+ visit(record.content);
117
+ }
118
+ };
119
+ visit(content);
120
+ return images;
121
+ }
69
122
  function extractUserTextFromEntry(entry) {
70
123
  if (!entry || typeof entry !== 'object')
71
124
  return '';
@@ -94,6 +147,7 @@ export function convertMessagesToBridgeInput(options) {
94
147
  const role = coerceBridgeRole(m.role || 'user');
95
148
  const content = m.content;
96
149
  const collectedText = collectText(content);
150
+ const imageBlocks = extractImageBlocksFromContent(content);
97
151
  const text = role === 'system' ? collectedText : collectedText.trim();
98
152
  if (role === 'system') {
99
153
  if (collectedText && collectedText.length) {
@@ -170,13 +224,29 @@ export function convertMessagesToBridgeInput(options) {
170
224
  }
171
225
  continue;
172
226
  }
173
- if (typeof text === 'string') {
227
+ if (typeof text === 'string' || imageBlocks.length) {
174
228
  const tRole = role === 'assistant' ? 'output_text' : 'input_text';
175
- const entry = {
176
- role,
177
- content: [{ type: tRole, text }]
178
- };
179
- input.push(entry);
229
+ const blocks = [];
230
+ if (typeof text === 'string' && text.length) {
231
+ blocks.push({ type: tRole, text });
232
+ }
233
+ for (const img of imageBlocks) {
234
+ const block = {
235
+ type: 'input_image',
236
+ image_url: img.url
237
+ };
238
+ if (img.detail) {
239
+ block.detail = img.detail;
240
+ }
241
+ blocks.push(block);
242
+ }
243
+ if (blocks.length) {
244
+ const entry = {
245
+ role,
246
+ content: blocks
247
+ };
248
+ input.push(entry);
249
+ }
180
250
  if (role === 'user') {
181
251
  const trimmed = typeof text === 'string' ? text.trim() : '';
182
252
  if (trimmed.length) {
@@ -260,6 +330,7 @@ function processMessageBlocks(blocks, normalizeFunctionName, tools, toolNameById
260
330
  const toolMessages = [];
261
331
  let currentLastCall = lastToolCallId;
262
332
  const reasoningSegments = [];
333
+ const images = [];
263
334
  for (const block of blocks) {
264
335
  if (!block || typeof block !== 'object')
265
336
  continue;
@@ -282,6 +353,18 @@ function processMessageBlocks(blocks, normalizeFunctionName, tools, toolNameById
282
353
  toolMessages.push(tm);
283
354
  currentLastCall = nested.lastCallId;
284
355
  reasoningSegments.push(...nested.reasoningSegments);
356
+ if (nested.images.length)
357
+ images.push(...nested.images);
358
+ continue;
359
+ }
360
+ if (type === 'input_image') {
361
+ const url = typeof block.image_url === 'string' ? block.image_url.trim() : '';
362
+ if (url) {
363
+ const detail = typeof block.detail === 'string' && block.detail.trim()
364
+ ? block.detail.trim()
365
+ : undefined;
366
+ images.push({ url, detail });
367
+ }
285
368
  continue;
286
369
  }
287
370
  if (type === 'function_call') {
@@ -344,7 +427,7 @@ function processMessageBlocks(blocks, normalizeFunctionName, tools, toolNameById
344
427
  }
345
428
  }
346
429
  const text = textParts.length ? textParts.join('\n').trim() : null;
347
- return { text, toolCalls, toolMessages, lastCallId: currentLastCall, reasoningSegments };
430
+ return { text, images, toolCalls, toolMessages, lastCallId: currentLastCall, reasoningSegments };
348
431
  }
349
432
  export function convertBridgeInputToChatMessages(options) {
350
433
  const { input, tools, normalizeFunctionName, toolResultFallbackText } = options;
@@ -470,7 +553,29 @@ export function convertBridgeInputToChatMessages(options) {
470
553
  for (const msg of nested.toolMessages)
471
554
  messages.push(msg);
472
555
  const normalizedRole = coerceBridgeRole((explicit.role ?? entry.role) || 'user');
473
- if (typeof nested.text === 'string') {
556
+ if (nested.images.length) {
557
+ const contentBlocks = [];
558
+ if (typeof nested.text === 'string' && nested.text.trim().length) {
559
+ contentBlocks.push({ type: 'text', text: nested.text });
560
+ }
561
+ for (const img of nested.images) {
562
+ const imgBlock = { type: 'image_url', image_url: { url: img.url } };
563
+ if (img.detail) {
564
+ imgBlock.image_url.detail = img.detail;
565
+ }
566
+ contentBlocks.push(imgBlock);
567
+ }
568
+ const msg = {
569
+ role: normalizedRole,
570
+ content: contentBlocks
571
+ };
572
+ const combinedReasoning = combineReasoningSegments(consumeEntryReasoning(), nested.reasoningSegments);
573
+ if (combinedReasoning.length) {
574
+ msg.reasoning_content = combinedReasoning.join('\n');
575
+ }
576
+ messages.push(msg);
577
+ }
578
+ else if (typeof nested.text === 'string') {
474
579
  pushNormalizedChatMessage(messages, normalizedRole, nested.text, {
475
580
  reasoningSegments: combineReasoningSegments(consumeEntryReasoning(), nested.reasoningSegments)
476
581
  });
@@ -491,9 +596,31 @@ export function convertBridgeInputToChatMessages(options) {
491
596
  for (const msg of nested.toolMessages)
492
597
  messages.push(msg);
493
598
  const normalizedRole = coerceBridgeRole(entry.role || 'user');
494
- if (typeof nested.text === 'string') {
599
+ if (nested.images.length) {
600
+ const contentBlocks = [];
601
+ if (typeof nested.text === 'string' && nested.text.trim().length) {
602
+ contentBlocks.push({ type: 'text', text: nested.text });
603
+ }
604
+ for (const img of nested.images) {
605
+ const imgBlock = { type: 'image_url', image_url: { url: img.url } };
606
+ if (img.detail) {
607
+ imgBlock.image_url.detail = img.detail;
608
+ }
609
+ contentBlocks.push(imgBlock);
610
+ }
611
+ const msg = {
612
+ role: normalizedRole,
613
+ content: contentBlocks
614
+ };
615
+ const combinedReasoning = combineReasoningSegments(consumeEntryReasoning(), nested.reasoningSegments);
616
+ if (combinedReasoning.length) {
617
+ msg.reasoning_content = combinedReasoning.join('\n');
618
+ }
619
+ messages.push(msg);
620
+ }
621
+ else if (typeof nested.text === 'string') {
495
622
  pushNormalizedChatMessage(messages, normalizedRole, nested.text, {
496
- reasoningSegments: combineReasoningSegments(consumeEntryReasoning(), nested.reasoningSegments)
623
+ reasoningSegments: consumeEntryReasoning()
497
624
  });
498
625
  }
499
626
  lastToolCallId = nested.lastCallId;
@@ -1,6 +1,41 @@
1
1
  import { normalizeFunctionCallId } from './bridge-id-utils.js';
2
2
  import { normalizeContentPart } from './output-content-normalizer.js';
3
3
  import { expandResponsesMessageItem } from '../../sse/shared/responses-output-normalizer.js';
4
+ function buildToolOutputIndex(response) {
5
+ const ids = new Set();
6
+ try {
7
+ const primary = Array.isArray(response.tool_outputs)
8
+ ? response.tool_outputs
9
+ : [];
10
+ for (const entry of primary) {
11
+ if (!entry || typeof entry !== 'object')
12
+ continue;
13
+ const raw = entry.tool_call_id ||
14
+ entry.call_id ||
15
+ entry.id;
16
+ if (typeof raw === 'string' && raw.trim().length) {
17
+ const trimmed = raw.trim();
18
+ // 记录原始 ID(例如 OpenAI 的 toolu_ 前缀),以兼容直接使用
19
+ // tool_call_id 的客户端;同时记录归一化后的 fc_ 形式,保证与
20
+ // buildFunctionCallOutput 中 normalizeFunctionCallId 的结果对齐。
21
+ ids.add(trimmed);
22
+ try {
23
+ const normalized = normalizeFunctionCallId({ callId: trimmed, fallback: trimmed });
24
+ if (normalized && normalized !== trimmed) {
25
+ ids.add(normalized);
26
+ }
27
+ }
28
+ catch {
29
+ // 归一化失败不应影响主流程
30
+ }
31
+ }
32
+ }
33
+ }
34
+ catch {
35
+ // best-effort: 不因索引构建失败影响主流程
36
+ }
37
+ return ids;
38
+ }
4
39
  function appendReasoningSegments(target, raw) {
5
40
  if (typeof raw !== 'string' || !raw.length) {
6
41
  return;
@@ -91,7 +126,13 @@ export function buildResponsesOutputFromChat(options) {
91
126
  const usage = normalizeUsage(response.usage);
92
127
  const outputTextMeta = response?.__responses_output_text_meta;
93
128
  const outputText = resolveOutputText(convertedContent, outputTextMeta);
94
- const hasNormalizedToolCalls = normalizedToolCalls.length > 0;
129
+ // 如果顶层 tool_outputs 已经为所有 tool_calls 提供了结果,说明这些函数调用
130
+ // 已在服务端(例如 server-side web_search)完成,不应再对客户端暴露
131
+ // required_action/submit_tool_outputs。此时只需返回 completed 状态即可,避免
132
+ // 再触发一轮工具回合。
133
+ const executedIds = buildToolOutputIndex(response);
134
+ const pendingToolCalls = normalizedToolCalls.filter((entry) => !executedIds.has(entry.id));
135
+ const hasNormalizedToolCalls = pendingToolCalls.length > 0;
95
136
  if (hasNormalizedToolCalls) {
96
137
  for (const item of outputItems) {
97
138
  if (item.type === 'message') {
@@ -100,7 +141,7 @@ export function buildResponsesOutputFromChat(options) {
100
141
  }
101
142
  }
102
143
  const requiredAction = hasNormalizedToolCalls
103
- ? buildRequiredActionFromNormalized(normalizedToolCalls)
144
+ ? buildRequiredActionFromNormalized(pendingToolCalls)
104
145
  : undefined;
105
146
  const status = hasNormalizedToolCalls ? 'requires_action' : 'completed';
106
147
  return {
@@ -1,35 +1,25 @@
1
- import os from 'node:os';
2
- import path from 'node:path';
3
- import fs from 'node:fs/promises';
4
1
  import { writeSnapshotViaHooks } from './snapshot-hooks.js';
5
- const SNAPSHOT_BASE = path.join(os.homedir(), '.routecodex', 'golden_samples');
6
- function mapEndpointToFolder(endpoint, hint) {
7
- if (hint)
8
- return hint;
9
- const ep = String(endpoint || '').toLowerCase();
10
- if (ep.includes('/responses'))
11
- return 'openai-responses';
12
- if (ep.includes('/messages'))
13
- return 'anthropic-messages';
14
- if (ep.includes('/gemini'))
15
- return 'gemini-chat';
16
- return 'openai-chat';
17
- }
18
- async function ensureDir(dir) {
19
- try {
20
- await fs.mkdir(dir, { recursive: true });
2
+ function resolveBoolFromEnv(value, fallback) {
3
+ if (!value) {
4
+ return fallback;
21
5
  }
22
- catch {
23
- // ignore fs errors
6
+ const normalized = value.trim().toLowerCase();
7
+ if (['1', 'true', 'yes', 'on'].includes(normalized)) {
8
+ return true;
24
9
  }
25
- }
26
- function sanitize(value) {
27
- return value.replace(/[^\w.-]/g, '_');
10
+ if (['0', 'false', 'no', 'off'].includes(normalized)) {
11
+ return false;
12
+ }
13
+ return fallback;
28
14
  }
29
15
  export function shouldRecordSnapshots() {
30
- const flag = process.env.ROUTECODEX_HUB_SNAPSHOTS;
31
- if (flag && flag.trim() === '0') {
32
- return false;
16
+ const hubFlag = process.env.ROUTECODEX_HUB_SNAPSHOTS;
17
+ if (hubFlag && hubFlag.trim().length) {
18
+ return resolveBoolFromEnv(hubFlag, true);
19
+ }
20
+ const sharedFlag = process.env.ROUTECODEX_SNAPSHOT ?? process.env.ROUTECODEX_SNAPSHOTS;
21
+ if (sharedFlag && sharedFlag.trim().length) {
22
+ return resolveBoolFromEnv(sharedFlag, true);
33
23
  }
34
24
  return true;
35
25
  }
@@ -37,26 +27,6 @@ export async function recordSnapshot(options) {
37
27
  if (!shouldRecordSnapshots())
38
28
  return;
39
29
  const endpoint = options.endpoint || '/v1/chat/completions';
40
- const folder = mapEndpointToFolder(endpoint, options.folderHint);
41
- const dir = path.join(SNAPSHOT_BASE, folder);
42
- try {
43
- await ensureDir(dir);
44
- const safeStage = sanitize(options.stage);
45
- const safeRequestId = sanitize(options.requestId);
46
- const file = path.join(dir, `${safeRequestId}_${safeStage}.json`);
47
- const payload = {
48
- meta: {
49
- stage: options.stage,
50
- timestamp: Date.now(),
51
- endpoint
52
- },
53
- body: options.data
54
- };
55
- await fs.writeFile(file, JSON.stringify(payload, null, 2), 'utf-8');
56
- }
57
- catch (error) {
58
- console.warn('[snapshot-utils] failed to write snapshot', error);
59
- }
60
30
  void writeSnapshotViaHooks({
61
31
  endpoint,
62
32
  stage: options.stage,
@@ -111,6 +111,7 @@ function applyLocalToolGovernance(chatRequest, rawPayload) {
111
111
  }
112
112
  const hasImageHint = detectImageHint(messages, rawPayload);
113
113
  if (hasImageHint) {
114
+ // 有图片线索时不干预工具列表,保持由上游(Codex 等)决定 view_image 的暴露与使用。
114
115
  return chatRequest;
115
116
  }
116
117
  const filteredTools = tools.filter((tool) => {