@jsonstudio/llms 0.6.2979 → 0.6.3238
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/args-mapping.js +8 -0
- package/dist/conversion/{shared/bridge-actions.js → bridge-actions.js} +2 -1
- package/dist/conversion/{shared/bridge-id-utils.js → bridge-id-utils.js} +1 -1
- package/dist/conversion/{shared/bridge-instructions.js → bridge-instructions.js} +1 -1
- package/dist/conversion/{shared/bridge-message-utils.d.ts → bridge-message-utils.d.ts} +1 -1
- package/dist/conversion/{shared/bridge-message-utils.js → bridge-message-utils.js} +5 -149
- package/dist/conversion/{shared/bridge-metadata.js → bridge-metadata.js} +1 -1
- package/dist/conversion/{shared/bridge-policies.js → bridge-policies.js} +1 -1
- package/dist/conversion/codecs/gemini-openai-codec.js +27 -8
- package/dist/conversion/codecs/responses-openai-codec.js +1 -1
- package/dist/conversion/{shared/compaction-detect.d.ts → compaction-detect.d.ts} +1 -1
- package/dist/conversion/compaction-detect.js +4 -0
- package/dist/conversion/compat/actions/apply-patch-fixer.js +2 -2
- package/dist/conversion/compat/actions/deepseek-web-response.d.ts +0 -1
- package/dist/conversion/compat/actions/deepseek-web-response.js +15 -405
- package/dist/conversion/compat/actions/harvest-tool-calls-from-text.js +1 -1
- package/dist/conversion/compat/actions/lmstudio-responses-fc-ids.js +1 -1
- package/dist/conversion/compat/actions/qwen-transform.js +74 -2
- package/dist/conversion/compat/actions/snapshot.js +1 -1
- package/dist/conversion/compat/antigravity-session-signature.js +36 -0
- package/dist/conversion/compat/profiles/chat-deepseek-web.json +0 -22
- package/dist/conversion/compat/profiles/chat-glm.json +251 -72
- package/dist/conversion/compat/profiles/chat-iflow.json +174 -39
- package/dist/conversion/compat/profiles/chat-lmstudio.json +43 -14
- package/dist/conversion/hub/operation-table/operation-table-runner.js +2 -2
- package/dist/conversion/hub/operation-table/semantic-mappers/anthropic-mapper.js +1 -1
- package/dist/conversion/hub/operation-table/semantic-mappers/archive/chat-mapper.archive.d.ts +8 -0
- package/dist/conversion/hub/operation-table/semantic-mappers/archive/chat-mapper.archive.js +404 -0
- package/dist/conversion/hub/operation-table/semantic-mappers/chat-mapper.js +5 -381
- package/dist/conversion/hub/operation-table/semantic-mappers/gemini-mapper.js +2 -2
- package/dist/conversion/hub/operation-table/semantic-mappers/responses-mapper.js +2 -8
- package/dist/conversion/hub/pipeline/hub-pipeline.d.ts +1 -0
- package/dist/conversion/hub/pipeline/hub-pipeline.js +50 -3
- package/dist/conversion/hub/pipeline/stages/req_inbound/req_inbound_stage2_semantic_map/index.d.ts +1 -1
- package/dist/conversion/hub/pipeline/stages/req_inbound/req_inbound_stage2_semantic_map/index.js +62 -0
- package/dist/conversion/hub/pipeline/stages/req_process/req_process_stage2_route_select/index.js +3 -1
- package/dist/conversion/hub/pipeline/stages/resp_inbound/resp_inbound_stage1_sse_decode/index.js +1 -1
- package/dist/conversion/hub/pipeline/stages/resp_outbound/resp_outbound_stage1_client_remap/chat-process-semantics-bridge.d.ts +1 -1
- package/dist/conversion/hub/pipeline/stages/resp_process/resp_process_stage1_tool_governance/index.js +42 -29
- package/dist/conversion/hub/pipeline/stages/resp_process/resp_process_stage2_finalize/index.js +12 -0
- package/dist/conversion/hub/policy/protocol-spec.js +1 -1
- package/dist/conversion/hub/process/chat-process-clock-reminders.js +1 -1
- package/dist/conversion/hub/process/chat-process-clock-tools.js +1 -1
- package/dist/conversion/hub/process/chat-process-continue-execution.js +1 -1
- package/dist/conversion/hub/process/chat-process-servertool-orchestration.js +1 -1
- package/dist/conversion/hub/process/chat-process-web-search.js +1 -1
- package/dist/conversion/hub/response/provider-response.js +14 -5
- package/dist/conversion/hub/response/response-mappers.js +23 -1
- package/dist/conversion/hub/response/response-runtime.js +28 -5
- package/dist/conversion/hub/snapshot-recorder.js +3 -92
- package/dist/conversion/hub/tool-governance/engine.d.ts +8 -0
- package/dist/conversion/hub/tool-governance/engine.js +40 -193
- package/dist/conversion/hub/tool-governance/rules.js +73 -69
- package/dist/conversion/hub/tool-surface/tool-surface-engine.js +1 -1
- package/dist/conversion/index.d.ts +1 -2
- package/dist/conversion/index.js +1 -2
- package/dist/conversion/{shared/jsonish.js → jsonish.js} +1 -1
- package/dist/conversion/{shared/mcp-injection.js → mcp-injection.js} +1 -1
- package/dist/conversion/media.js +4 -0
- package/dist/conversion/{shared/metadata-passthrough.d.ts → metadata-passthrough.d.ts} +1 -1
- package/dist/conversion/{shared/metadata-passthrough.js → metadata-passthrough.js} +2 -2
- package/dist/conversion/payload-budget.js +47 -0
- package/dist/conversion/protocol-field-allowlists.d.ts +7 -0
- package/dist/conversion/protocol-field-allowlists.js +9 -0
- package/dist/conversion/{shared/protocol-state.d.ts → protocol-state.d.ts} +2 -2
- package/dist/conversion/{shared/protocol-state.js → protocol-state.js} +2 -2
- package/dist/conversion/{shared/errors.d.ts → provider-protocol-error.d.ts} +0 -3
- package/dist/conversion/provider-protocol-error.js +25 -0
- package/dist/conversion/responses/responses-openai-bridge/response-payload.js +8 -5
- package/dist/conversion/responses/responses-openai-bridge/types.d.ts +1 -1
- package/dist/conversion/responses/responses-openai-bridge.d.ts +1 -1
- package/dist/conversion/responses/responses-openai-bridge.js +43 -10
- package/dist/conversion/{shared/runtime-metadata.d.ts → runtime-metadata.d.ts} +1 -1
- package/dist/conversion/{shared/runtime-metadata.js → runtime-metadata.js} +2 -2
- package/dist/conversion/shared/anthropic-message-utils.js +19 -8
- package/dist/conversion/shared/chat-request-filters.d.ts +3 -4
- package/dist/conversion/shared/chat-request-filters.js +22 -78
- package/dist/conversion/shared/gemini-tool-utils.d.ts +1 -1
- package/dist/conversion/shared/openai-finalizer.js +1 -0
- package/dist/conversion/shared/openai-message-normalize.js +2 -2
- package/dist/conversion/shared/reasoning-normalizer.js +6 -0
- package/dist/conversion/shared/reasoning-utils.js +5 -2
- package/dist/conversion/shared/responses-conversation-store.js +1 -1
- package/dist/conversion/shared/responses-output-builder.js +55 -11
- package/dist/conversion/shared/responses-reasoning-registry.d.ts +14 -2
- package/dist/conversion/shared/responses-reasoning-registry.js +34 -6
- package/dist/conversion/shared/responses-response-utils.js +99 -9
- package/dist/conversion/shared/responses-tool-utils.js +1 -1
- package/dist/conversion/shared/text-markup-normalizer/normalize.d.ts +1 -1
- package/dist/conversion/shared/text-markup-normalizer.d.ts +2 -2
- package/dist/conversion/shared/text-markup-normalizer.js +1 -1
- package/dist/conversion/shared/tool-filter-pipeline.js +1 -1
- package/dist/conversion/shared/tool-governor.js +3 -3
- package/dist/conversion/shared/tool-mapping.d.ts +1 -1
- package/dist/conversion/{shared/snapshot-utils.d.ts → snapshot-utils.d.ts} +11 -0
- package/dist/conversion/{shared/snapshot-utils.js → snapshot-utils.js} +14 -23
- package/dist/conversion/types/text-markup-normalizer.d.ts +13 -0
- package/dist/conversion/types/text-markup-normalizer.js +1 -0
- package/dist/filters/special/request-tools-normalize.js +1 -1
- package/dist/filters/special/response-tool-text-canonicalize.js +2 -2
- package/dist/native/router_hotpath_napi.node +0 -0
- package/dist/quota/quota-manager.js +31 -59
- package/dist/quota/quota-state.js +14 -7
- package/dist/router/virtual-router/bootstrap/profile-builder.d.ts +1 -0
- package/dist/router/virtual-router/bootstrap/profile-builder.js +13 -0
- package/dist/router/virtual-router/bootstrap/provider-normalization.d.ts +2 -0
- package/dist/router/virtual-router/bootstrap/provider-normalization.js +4 -1
- package/dist/router/virtual-router/bootstrap/streaming-helpers.d.ts +7 -0
- package/dist/router/virtual-router/bootstrap/streaming-helpers.js +44 -0
- package/dist/router/virtual-router/bootstrap.js +2 -0
- package/dist/router/virtual-router/engine/routing-state/store.d.ts +1 -2
- package/dist/router/virtual-router/engine/routing-state/store.js +2 -2
- package/dist/router/virtual-router/engine-legacy/config.d.ts +11 -0
- package/dist/router/virtual-router/engine-legacy/config.js +108 -0
- package/dist/router/virtual-router/engine-legacy/direct-model.d.ts +10 -0
- package/dist/router/virtual-router/engine-legacy/direct-model.js +38 -0
- package/dist/router/virtual-router/engine-legacy/health.d.ts +13 -0
- package/dist/router/virtual-router/engine-legacy/health.js +104 -0
- package/dist/router/virtual-router/engine-legacy/helpers.d.ts +16 -0
- package/dist/router/virtual-router/engine-legacy/helpers.js +226 -0
- package/dist/router/virtual-router/engine-legacy/route-finalize.d.ts +9 -0
- package/dist/router/virtual-router/engine-legacy/route-finalize.js +84 -0
- package/dist/router/virtual-router/engine-legacy/route-selection.d.ts +17 -0
- package/dist/router/virtual-router/engine-legacy/route-selection.js +205 -0
- package/dist/router/virtual-router/engine-legacy/route-state-allowlist.d.ts +3 -0
- package/dist/router/virtual-router/engine-legacy/route-state-allowlist.js +36 -0
- package/dist/router/virtual-router/engine-legacy/route-state.d.ts +12 -0
- package/dist/router/virtual-router/engine-legacy/route-state.js +386 -0
- package/dist/router/virtual-router/engine-legacy/route-utils.d.ts +19 -0
- package/dist/router/virtual-router/engine-legacy/route-utils.js +212 -0
- package/dist/router/virtual-router/engine-legacy/routing.d.ts +8 -0
- package/dist/router/virtual-router/engine-legacy/routing.js +8 -0
- package/dist/router/virtual-router/engine-legacy/selection-core.d.ts +28 -0
- package/dist/router/virtual-router/engine-legacy/selection-core.js +112 -0
- package/dist/router/virtual-router/engine-legacy/selection-state.d.ts +16 -0
- package/dist/router/virtual-router/engine-legacy/selection-state.js +187 -0
- package/dist/router/virtual-router/engine-legacy/state-accessors.d.ts +21 -0
- package/dist/router/virtual-router/engine-legacy/state-accessors.js +118 -0
- package/dist/router/virtual-router/engine-legacy.d.ts +123 -0
- package/dist/router/virtual-router/engine-legacy.js +194 -0
- package/dist/router/virtual-router/engine-logging.d.ts +2 -0
- package/dist/router/virtual-router/engine-logging.js +7 -2
- package/dist/router/virtual-router/engine-selection/key-parsing.js +0 -3
- package/dist/router/virtual-router/engine-selection/native-chat-request-filter-semantics.d.ts +1 -0
- package/dist/router/virtual-router/engine-selection/native-chat-request-filter-semantics.js +54 -0
- package/dist/router/virtual-router/engine-selection/native-hub-bridge-policy-semantics.d.ts +10 -0
- package/dist/router/virtual-router/engine-selection/native-hub-bridge-policy-semantics.js +67 -0
- package/dist/router/virtual-router/engine-selection/native-hub-pipeline-governance-semantics.d.ts +30 -0
- package/dist/router/virtual-router/engine-selection/native-hub-pipeline-governance-semantics.js +202 -0
- package/dist/router/virtual-router/engine-selection/native-hub-pipeline-semantic-mappers.d.ts +2 -0
- package/dist/router/virtual-router/engine-selection/native-hub-pipeline-semantic-mappers.js +83 -0
- package/dist/router/virtual-router/engine-selection/native-router-hotpath-loader.js +43 -2
- package/dist/router/virtual-router/engine-selection/native-shared-conversion-semantics.d.ts +75 -0
- package/dist/router/virtual-router/engine-selection/native-shared-conversion-semantics.js +205 -0
- package/dist/router/virtual-router/engine-selection/native-snapshot-hooks.d.ts +3 -0
- package/dist/router/virtual-router/engine-selection/native-snapshot-hooks.js +109 -0
- package/dist/router/virtual-router/engine-selection/native-virtual-router-engine-proxy.d.ts +16 -0
- package/dist/router/virtual-router/engine-selection/native-virtual-router-engine-proxy.js +14 -0
- package/dist/router/virtual-router/engine-selection/native-virtual-router-routing-instructions-semantics.d.ts +2 -0
- package/dist/router/virtual-router/engine-selection/native-virtual-router-routing-instructions-semantics.js +86 -0
- package/dist/router/virtual-router/engine-selection/tier-selection-quota-integration.js +100 -0
- package/dist/router/virtual-router/engine-selection/tier-selection-select.js +99 -0
- package/dist/router/virtual-router/engine.d.ts +22 -105
- package/dist/router/virtual-router/engine.js +274 -1641
- package/dist/router/virtual-router/load-balancer.d.ts +8 -0
- package/dist/router/virtual-router/load-balancer.js +65 -2
- package/dist/router/virtual-router/provider-registry.js +2 -0
- package/dist/router/virtual-router/routing-instructions/clean.d.ts +3 -0
- package/dist/router/virtual-router/routing-instructions/clean.js +34 -0
- package/dist/router/virtual-router/routing-instructions/parse.d.ts +18 -0
- package/dist/router/virtual-router/routing-instructions/parse.js +377 -0
- package/dist/router/virtual-router/routing-instructions/state.d.ts +4 -0
- package/dist/router/virtual-router/routing-instructions/state.js +245 -0
- package/dist/router/virtual-router/routing-instructions/types.d.ts +70 -0
- package/dist/router/virtual-router/routing-instructions/types.js +2 -0
- package/dist/router/virtual-router/routing-instructions.d.ts +5 -89
- package/dist/router/virtual-router/routing-instructions.js +4 -655
- package/dist/router/virtual-router/sticky-session-store.d.ts +4 -0
- package/dist/router/virtual-router/sticky-session-store.js +19 -81
- package/dist/router/virtual-router/tool-signals.js +21 -3
- package/dist/router/virtual-router/types.d.ts +4 -0
- package/dist/servertool/clock/session-scope.js +32 -1
- package/dist/servertool/engine.js +79 -8
- package/dist/servertool/handlers/antigravity-thought-signature-bootstrap.js +1 -1
- package/dist/servertool/handlers/clock-auto.js +1 -1
- package/dist/servertool/handlers/clock.js +1 -1
- package/dist/servertool/handlers/compaction-detect.d.ts +1 -1
- package/dist/servertool/handlers/compaction-detect.js +1 -1
- package/dist/servertool/handlers/gemini-empty-reply-continue.js +1 -1
- package/dist/servertool/handlers/iflow-model-error-retry.js +1 -1
- package/dist/servertool/handlers/recursive-detection-guard.js +1 -1
- package/dist/servertool/handlers/review.js +1 -1
- package/dist/servertool/handlers/stop-message-auto/iflow-followup.js +1 -1
- package/dist/servertool/handlers/stop-message-auto/runtime-utils.js +1 -1
- package/dist/servertool/handlers/stop-message-auto.js +1 -1
- package/dist/servertool/handlers/vision.js +1 -1
- package/dist/servertool/handlers/web-search.js +1 -1
- package/dist/servertool/reenter-backend.js +1 -1
- package/dist/servertool/server-side-tools.js +2 -2
- package/dist/servertool/stop-gateway-context.js +1 -1
- package/dist/servertool/stop-message-compare-context.js +1 -1
- package/dist/sse/json-to-sse/event-generators/responses.d.ts +4 -0
- package/dist/sse/json-to-sse/event-generators/responses.js +95 -1
- package/dist/sse/json-to-sse/sequencers/responses-sequencer.js +6 -4
- package/dist/sse/sse-to-json/builders/response-builder.d.ts +8 -0
- package/dist/sse/sse-to-json/builders/response-builder.js +162 -4
- package/dist/sse/sse-to-json/responses-sse-to-json-converter.js +2 -0
- package/dist/sse/types/responses-types.d.ts +6 -2
- package/dist/tools/apply-patch/structured/coercion.js +5 -0
- package/dist/tools/args-json.js +29 -0
- package/package.json +8 -5
- package/dist/conversion/shared/args-mapping.js +0 -77
- package/dist/conversion/shared/compaction-detect.js +0 -4
- package/dist/conversion/shared/errors.js +0 -31
- package/dist/conversion/shared/media.js +0 -4
- package/dist/conversion/shared/payload-budget.js +0 -165
- package/dist/conversion/shared/protocol-field-allowlists.d.ts +0 -7
- package/dist/conversion/shared/protocol-field-allowlists.js +0 -149
- package/dist/conversion/shared/snapshot-hooks.d.ts +0 -11
- package/dist/conversion/shared/snapshot-hooks.js +0 -503
- package/dist/conversion/shared/text-markup-normalizer/extractors-apply-patch.d.ts +0 -2
- package/dist/conversion/shared/text-markup-normalizer/extractors-apply-patch.js +0 -129
- package/dist/conversion/shared/text-markup-normalizer/extractors-json.d.ts +0 -4
- package/dist/conversion/shared/text-markup-normalizer/extractors-json.js +0 -637
- package/dist/conversion/shared/text-markup-normalizer/extractors-shared.d.ts +0 -21
- package/dist/conversion/shared/text-markup-normalizer/extractors-shared.js +0 -177
- package/dist/conversion/shared/text-markup-normalizer/extractors-transcript.d.ts +0 -5
- package/dist/conversion/shared/text-markup-normalizer/extractors-transcript.js +0 -385
- package/dist/conversion/shared/text-markup-normalizer/extractors-xml.d.ts +0 -10
- package/dist/conversion/shared/text-markup-normalizer/extractors-xml.js +0 -602
- package/dist/conversion/shared/text-markup-normalizer/extractors.d.ts +0 -5
- package/dist/conversion/shared/text-markup-normalizer/extractors.js +0 -4
- package/dist/conversion/shared/tool-canonicalizer.d.ts +0 -2
- package/dist/conversion/shared/tool-canonicalizer.js +0 -38
- /package/dist/conversion/{shared/args-mapping.d.ts → args-mapping.d.ts} +0 -0
- /package/dist/conversion/{shared/bridge-actions.d.ts → bridge-actions.d.ts} +0 -0
- /package/dist/conversion/{shared/bridge-id-utils.d.ts → bridge-id-utils.d.ts} +0 -0
- /package/dist/conversion/{shared/bridge-instructions.d.ts → bridge-instructions.d.ts} +0 -0
- /package/dist/conversion/{shared/bridge-metadata.d.ts → bridge-metadata.d.ts} +0 -0
- /package/dist/conversion/{shared/bridge-policies.d.ts → bridge-policies.d.ts} +0 -0
- /package/dist/conversion/{shared/jsonish.d.ts → jsonish.d.ts} +0 -0
- /package/dist/conversion/{shared/mcp-injection.d.ts → mcp-injection.d.ts} +0 -0
- /package/dist/conversion/{shared/media.d.ts → media.d.ts} +0 -0
- /package/dist/conversion/{shared/payload-budget.d.ts → payload-budget.d.ts} +0 -0
- /package/dist/conversion/{shared → types}/bridge-message-types.d.ts +0 -0
- /package/dist/conversion/{shared → types}/bridge-message-types.js +0 -0
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
import { isJsonObject, jsonClone } from '../../../types/json.js';
|
|
2
|
+
import { normalizeChatMessageContentWithNative, normalizeOpenaiChatMessagesWithNative } from '../../../../../router/virtual-router/engine-selection/native-shared-conversion-semantics.js';
|
|
3
|
+
import { ensureProtocolState } from '../../../../protocol-state.js';
|
|
4
|
+
import { mapReqInboundBridgeToolsToChatWithNative } from '../../../../../router/virtual-router/engine-selection/native-hub-pipeline-req-inbound-semantics.js';
|
|
5
|
+
const CHAT_PARAMETER_KEYS = [
|
|
6
|
+
'model',
|
|
7
|
+
'temperature',
|
|
8
|
+
'top_p',
|
|
9
|
+
'top_k',
|
|
10
|
+
'max_tokens',
|
|
11
|
+
'frequency_penalty',
|
|
12
|
+
'presence_penalty',
|
|
13
|
+
'logit_bias',
|
|
14
|
+
'response_format',
|
|
15
|
+
'parallel_tool_calls',
|
|
16
|
+
'tool_choice',
|
|
17
|
+
'seed',
|
|
18
|
+
'user',
|
|
19
|
+
'metadata',
|
|
20
|
+
'stop',
|
|
21
|
+
'stop_sequences',
|
|
22
|
+
'stream'
|
|
23
|
+
];
|
|
24
|
+
const KNOWN_TOP_LEVEL_FIELDS = new Set([
|
|
25
|
+
'messages',
|
|
26
|
+
'tools',
|
|
27
|
+
'tool_outputs',
|
|
28
|
+
...CHAT_PARAMETER_KEYS,
|
|
29
|
+
'stageExpectations',
|
|
30
|
+
'stages'
|
|
31
|
+
]);
|
|
32
|
+
function flattenSystemContent(content) {
|
|
33
|
+
if (typeof content === 'string')
|
|
34
|
+
return content;
|
|
35
|
+
if (Array.isArray(content)) {
|
|
36
|
+
return content.map(flattenSystemContent).filter(Boolean).join('\n');
|
|
37
|
+
}
|
|
38
|
+
if (content && typeof content === 'object') {
|
|
39
|
+
const obj = content;
|
|
40
|
+
if (typeof obj.text === 'string')
|
|
41
|
+
return obj.text;
|
|
42
|
+
if (typeof obj.content === 'string')
|
|
43
|
+
return obj.content;
|
|
44
|
+
if (Array.isArray(obj.content))
|
|
45
|
+
return obj.content.map(flattenSystemContent).join('\n');
|
|
46
|
+
}
|
|
47
|
+
return '';
|
|
48
|
+
}
|
|
49
|
+
function normalizeToolContent(content) {
|
|
50
|
+
if (typeof content === 'string')
|
|
51
|
+
return content;
|
|
52
|
+
if (content === null || content === undefined)
|
|
53
|
+
return '';
|
|
54
|
+
try {
|
|
55
|
+
return JSON.stringify(content);
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
return String(content ?? '');
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function maybeAugmentRouteCodexApplyPatchPrecheck(content) {
|
|
62
|
+
if (!content || typeof content !== 'string') {
|
|
63
|
+
return content;
|
|
64
|
+
}
|
|
65
|
+
if (content.includes('[RouteCodex precheck]')) {
|
|
66
|
+
return content;
|
|
67
|
+
}
|
|
68
|
+
const lower = content.toLowerCase();
|
|
69
|
+
if (!lower.includes('failed to parse function arguments')) {
|
|
70
|
+
return content;
|
|
71
|
+
}
|
|
72
|
+
if (content.includes('missing field `input`')) {
|
|
73
|
+
return `${content}\n\n[RouteCodex precheck] apply_patch 参数解析失败:缺少字段 "input"。当前 RouteCodex 期望 { input, patch } 形态,并且两个字段都应包含完整统一 diff 文本。`;
|
|
74
|
+
}
|
|
75
|
+
if (content.includes('invalid type: map, expected a string')) {
|
|
76
|
+
return `${content}\n\n[RouteCodex precheck] apply_patch 参数类型错误:检测到 JSON 对象(map),但客户端期望字符串。请先对参数做 JSON.stringify 再写入 arguments,或直接提供 { patch: "<统一 diff>" } 形式。`;
|
|
77
|
+
}
|
|
78
|
+
return content;
|
|
79
|
+
}
|
|
80
|
+
export function maybeAugmentApplyPatchErrorContent(content, toolName) {
|
|
81
|
+
if (!content)
|
|
82
|
+
return content;
|
|
83
|
+
const lower = content.toLowerCase();
|
|
84
|
+
const isApplyPatch = (typeof toolName === 'string' && toolName.trim() === 'apply_patch') ||
|
|
85
|
+
lower.includes('apply_patch verification failed');
|
|
86
|
+
if (!isApplyPatch) {
|
|
87
|
+
return content;
|
|
88
|
+
}
|
|
89
|
+
// 避免重复追加提示。
|
|
90
|
+
if (content.includes('[apply_patch hint]')) {
|
|
91
|
+
return content;
|
|
92
|
+
}
|
|
93
|
+
const hint = '\n\n[apply_patch hint] 在使用 apply_patch 之前,请先读取目标文件的最新内容,并基于该内容生成补丁;同时确保补丁格式符合工具规范(统一补丁格式或结构化参数),避免上下文不匹配或语法错误。';
|
|
94
|
+
return content + hint;
|
|
95
|
+
}
|
|
96
|
+
function recordToolCallIssues(message, messageIndex, missing) {
|
|
97
|
+
const toolCalls = Array.isArray(message.tool_calls) ? message.tool_calls : undefined;
|
|
98
|
+
if (!toolCalls?.length)
|
|
99
|
+
return;
|
|
100
|
+
toolCalls.forEach((entry, callIndex) => {
|
|
101
|
+
if (!isJsonObject(entry)) {
|
|
102
|
+
missing.push({
|
|
103
|
+
path: `messages[${messageIndex}].tool_calls[${callIndex}]`,
|
|
104
|
+
reason: 'invalid_tool_call_entry',
|
|
105
|
+
originalValue: jsonClone(entry)
|
|
106
|
+
});
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
const fnBlock = entry.function;
|
|
110
|
+
if (!isJsonObject(fnBlock)) {
|
|
111
|
+
missing.push({
|
|
112
|
+
path: `messages[${messageIndex}].tool_calls[${callIndex}].function`,
|
|
113
|
+
reason: 'missing_tool_function',
|
|
114
|
+
originalValue: jsonClone(fnBlock)
|
|
115
|
+
});
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
const fnName = fnBlock.name;
|
|
119
|
+
if (typeof fnName !== 'string' || !fnName.trim().length) {
|
|
120
|
+
missing.push({
|
|
121
|
+
path: `messages[${messageIndex}].tool_calls[${callIndex}].function.name`,
|
|
122
|
+
reason: 'missing_tool_name'
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
function collectSystemRawBlocks(raw) {
|
|
128
|
+
if (!Array.isArray(raw))
|
|
129
|
+
return undefined;
|
|
130
|
+
const blocks = [];
|
|
131
|
+
raw.forEach((entry) => {
|
|
132
|
+
if (!isJsonObject(entry))
|
|
133
|
+
return;
|
|
134
|
+
if (String(entry.role ?? '').toLowerCase() !== 'system')
|
|
135
|
+
return;
|
|
136
|
+
blocks.push(jsonClone(entry));
|
|
137
|
+
});
|
|
138
|
+
return blocks.length ? blocks : undefined;
|
|
139
|
+
}
|
|
140
|
+
function normalizeChatMessages(raw) {
|
|
141
|
+
const norm = {
|
|
142
|
+
messages: [],
|
|
143
|
+
systemSegments: [],
|
|
144
|
+
toolOutputs: [],
|
|
145
|
+
missingFields: []
|
|
146
|
+
};
|
|
147
|
+
if (raw === undefined) {
|
|
148
|
+
norm.missingFields.push({ path: 'messages', reason: 'absent' });
|
|
149
|
+
return norm;
|
|
150
|
+
}
|
|
151
|
+
const normalizedRaw = Array.isArray(raw)
|
|
152
|
+
? normalizeOpenaiChatMessagesWithNative(raw)
|
|
153
|
+
: raw;
|
|
154
|
+
if (!Array.isArray(normalizedRaw)) {
|
|
155
|
+
norm.missingFields.push({ path: 'messages', reason: 'invalid_type', originalValue: jsonClone(raw) });
|
|
156
|
+
return norm;
|
|
157
|
+
}
|
|
158
|
+
normalizedRaw.forEach((value, index) => {
|
|
159
|
+
if (!isJsonObject(value)) {
|
|
160
|
+
norm.missingFields.push({ path: `messages[${index}]`, reason: 'invalid_entry', originalValue: jsonClone(value) });
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
const roleValue = value.role;
|
|
164
|
+
if (typeof roleValue !== 'string') {
|
|
165
|
+
norm.missingFields.push({ path: `messages[${index}].role`, reason: 'missing_role' });
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
const chatMessage = value;
|
|
169
|
+
if (roleValue !== 'system' && roleValue !== 'tool') {
|
|
170
|
+
const normalizedContent = normalizeChatMessageContentWithNative(chatMessage.content);
|
|
171
|
+
const shouldOverwriteContent = !Array.isArray(chatMessage.content);
|
|
172
|
+
if (shouldOverwriteContent && normalizedContent.contentText !== undefined) {
|
|
173
|
+
chatMessage.content = normalizedContent.contentText;
|
|
174
|
+
}
|
|
175
|
+
if (typeof normalizedContent.reasoningText === 'string' && normalizedContent.reasoningText.trim().length) {
|
|
176
|
+
chatMessage.reasoning_content = normalizedContent.reasoningText.trim();
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
norm.messages.push(chatMessage);
|
|
180
|
+
const toolCallCandidate = value.tool_calls;
|
|
181
|
+
if (Array.isArray(toolCallCandidate) && toolCallCandidate.length) {
|
|
182
|
+
recordToolCallIssues(value, index, norm.missingFields);
|
|
183
|
+
}
|
|
184
|
+
if (roleValue === 'system') {
|
|
185
|
+
const segment = flattenSystemContent(chatMessage.content);
|
|
186
|
+
if (segment.trim().length) {
|
|
187
|
+
norm.systemSegments.push(segment);
|
|
188
|
+
}
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
if (roleValue === 'tool') {
|
|
192
|
+
const rawCallId = (value.tool_call_id ?? value.call_id ?? value.id);
|
|
193
|
+
const toolCallId = typeof rawCallId === 'string' && rawCallId.trim().length ? rawCallId.trim() : undefined;
|
|
194
|
+
if (!toolCallId) {
|
|
195
|
+
norm.missingFields.push({ path: `messages[${index}].tool_call_id`, reason: 'missing_tool_call_id' });
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
const nameValue = typeof value.name === 'string' && value.name.trim().length ? value.name : undefined;
|
|
199
|
+
const normalizedToolOutput = normalizeToolContent(value.content ?? value.output);
|
|
200
|
+
const routeCodexPrechecked = maybeAugmentRouteCodexApplyPatchPrecheck(normalizedToolOutput);
|
|
201
|
+
if (routeCodexPrechecked !== normalizedToolOutput) {
|
|
202
|
+
// Keep tool role message content aligned with outbound provider requests (e.g. Chat→Responses),
|
|
203
|
+
// while avoiding double-injection.
|
|
204
|
+
if (typeof chatMessage.content === 'string' || chatMessage.content === undefined || chatMessage.content === null) {
|
|
205
|
+
chatMessage.content = routeCodexPrechecked;
|
|
206
|
+
}
|
|
207
|
+
else if (typeof chatMessage.output === 'string') {
|
|
208
|
+
chatMessage.output = routeCodexPrechecked;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
const outputEntry = {
|
|
212
|
+
tool_call_id: toolCallId,
|
|
213
|
+
content: routeCodexPrechecked,
|
|
214
|
+
name: nameValue
|
|
215
|
+
};
|
|
216
|
+
outputEntry.content = maybeAugmentApplyPatchErrorContent(outputEntry.content, outputEntry.name);
|
|
217
|
+
norm.toolOutputs.push(outputEntry);
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
return norm;
|
|
221
|
+
}
|
|
222
|
+
function normalizeStandaloneToolOutputs(raw, missing) {
|
|
223
|
+
if (!Array.isArray(raw) || raw.length === 0)
|
|
224
|
+
return [];
|
|
225
|
+
const outputs = [];
|
|
226
|
+
raw.forEach((entry, index) => {
|
|
227
|
+
if (!isJsonObject(entry)) {
|
|
228
|
+
missing.push({ path: `tool_outputs[${index}]`, reason: 'invalid_entry', originalValue: jsonClone(entry) });
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
const rawCallId = entry.tool_call_id ?? entry.call_id ?? entry.id;
|
|
232
|
+
const toolCallId = typeof rawCallId === 'string' && rawCallId.trim().length ? rawCallId.trim() : undefined;
|
|
233
|
+
if (!toolCallId) {
|
|
234
|
+
missing.push({ path: `tool_outputs[${index}].tool_call_id`, reason: 'missing_tool_call_id' });
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
const nameValue = typeof entry.name === 'string' && entry.name.trim().length ? entry.name : undefined;
|
|
238
|
+
const rawContent = normalizeToolContent(entry.content ?? entry.output);
|
|
239
|
+
const content = maybeAugmentApplyPatchErrorContent(rawContent, nameValue);
|
|
240
|
+
outputs.push({
|
|
241
|
+
tool_call_id: toolCallId,
|
|
242
|
+
content,
|
|
243
|
+
name: nameValue
|
|
244
|
+
});
|
|
245
|
+
});
|
|
246
|
+
return outputs;
|
|
247
|
+
}
|
|
248
|
+
function normalizeTools(raw, missing) {
|
|
249
|
+
if (!Array.isArray(raw) || raw.length === 0) {
|
|
250
|
+
return undefined;
|
|
251
|
+
}
|
|
252
|
+
const tools = mapReqInboundBridgeToolsToChatWithNative(raw);
|
|
253
|
+
if (tools.length === 0) {
|
|
254
|
+
raw.forEach((entry, index) => {
|
|
255
|
+
missing.push({ path: `tools[${index}]`, reason: 'invalid_entry', originalValue: jsonClone(entry) });
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
return tools.length ? tools : undefined;
|
|
259
|
+
}
|
|
260
|
+
function extractParameters(body) {
|
|
261
|
+
const params = {};
|
|
262
|
+
for (const key of CHAT_PARAMETER_KEYS) {
|
|
263
|
+
if (body[key] !== undefined) {
|
|
264
|
+
params[key] = body[key];
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
return Object.keys(params).length ? params : undefined;
|
|
268
|
+
}
|
|
269
|
+
function collectExtraFields(body) {
|
|
270
|
+
const extras = {};
|
|
271
|
+
for (const [key, value] of Object.entries(body)) {
|
|
272
|
+
if (KNOWN_TOP_LEVEL_FIELDS.has(key)) {
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
if (value !== undefined) {
|
|
276
|
+
extras[key] = jsonClone(value);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return Object.keys(extras).length ? extras : undefined;
|
|
280
|
+
}
|
|
281
|
+
function extractOpenAIExtraFieldsFromSemantics(semantics) {
|
|
282
|
+
if (!semantics || !semantics.providerExtras || !isJsonObject(semantics.providerExtras)) {
|
|
283
|
+
return undefined;
|
|
284
|
+
}
|
|
285
|
+
const openaiExtras = semantics.providerExtras.openaiChat;
|
|
286
|
+
if (!openaiExtras || !isJsonObject(openaiExtras)) {
|
|
287
|
+
return undefined;
|
|
288
|
+
}
|
|
289
|
+
const stored = openaiExtras.extraFields;
|
|
290
|
+
if (!stored || !isJsonObject(stored)) {
|
|
291
|
+
return undefined;
|
|
292
|
+
}
|
|
293
|
+
return stored;
|
|
294
|
+
}
|
|
295
|
+
function hasExplicitEmptyToolsSemantics(semantics) {
|
|
296
|
+
if (!semantics || !semantics.tools || !isJsonObject(semantics.tools)) {
|
|
297
|
+
return false;
|
|
298
|
+
}
|
|
299
|
+
const flag = semantics.tools.explicitEmpty;
|
|
300
|
+
return flag === true;
|
|
301
|
+
}
|
|
302
|
+
function buildOpenAISemantics(options) {
|
|
303
|
+
const semantics = {};
|
|
304
|
+
if (options.systemSegments && options.systemSegments.length) {
|
|
305
|
+
semantics.system = {
|
|
306
|
+
textBlocks: options.systemSegments.map((segment) => segment)
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
if (options.extraFields && Object.keys(options.extraFields).length) {
|
|
310
|
+
semantics.providerExtras = {
|
|
311
|
+
openaiChat: {
|
|
312
|
+
extraFields: jsonClone(options.extraFields)
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
if (options.explicitEmptyTools) {
|
|
317
|
+
semantics.tools = {
|
|
318
|
+
explicitEmpty: true
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
return Object.keys(semantics).length ? semantics : undefined;
|
|
322
|
+
}
|
|
323
|
+
function applyExtraFields(body, metadata, semantics) {
|
|
324
|
+
const sources = [];
|
|
325
|
+
const semanticsExtras = extractOpenAIExtraFieldsFromSemantics(semantics);
|
|
326
|
+
if (semanticsExtras) {
|
|
327
|
+
sources.push(semanticsExtras);
|
|
328
|
+
}
|
|
329
|
+
if (!sources.length) {
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
for (const source of sources) {
|
|
333
|
+
for (const [key, value] of Object.entries(source)) {
|
|
334
|
+
if (body[key] !== undefined) {
|
|
335
|
+
continue;
|
|
336
|
+
}
|
|
337
|
+
body[key] = jsonClone(value);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
export class ChatSemanticMapper {
|
|
342
|
+
async toChat(format, ctx) {
|
|
343
|
+
const payload = (format.payload ?? {});
|
|
344
|
+
const normalized = normalizeChatMessages(payload.messages);
|
|
345
|
+
const topLevelOutputs = normalizeStandaloneToolOutputs(payload.tool_outputs, normalized.missingFields);
|
|
346
|
+
const toolOutputs = [...normalized.toolOutputs];
|
|
347
|
+
for (const entry of topLevelOutputs) {
|
|
348
|
+
if (!toolOutputs.find(item => item.tool_call_id === entry.tool_call_id)) {
|
|
349
|
+
toolOutputs.push(entry);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
const metadata = { context: ctx };
|
|
353
|
+
const rawSystemBlocks = collectSystemRawBlocks(payload.messages);
|
|
354
|
+
if (rawSystemBlocks) {
|
|
355
|
+
const protocolState = ensureProtocolState(metadata, 'openai');
|
|
356
|
+
protocolState.systemMessages = jsonClone(rawSystemBlocks);
|
|
357
|
+
}
|
|
358
|
+
if (normalized.missingFields.length) {
|
|
359
|
+
metadata.missingFields = normalized.missingFields;
|
|
360
|
+
}
|
|
361
|
+
const extraFields = collectExtraFields(payload);
|
|
362
|
+
const explicitEmptyTools = Array.isArray(payload.tools) && payload.tools.length === 0;
|
|
363
|
+
const semantics = buildOpenAISemantics({
|
|
364
|
+
systemSegments: normalized.systemSegments,
|
|
365
|
+
extraFields,
|
|
366
|
+
explicitEmptyTools
|
|
367
|
+
});
|
|
368
|
+
return {
|
|
369
|
+
messages: normalized.messages,
|
|
370
|
+
tools: normalizeTools(payload.tools, normalized.missingFields),
|
|
371
|
+
toolOutputs: toolOutputs.length ? toolOutputs : undefined,
|
|
372
|
+
parameters: extractParameters(payload),
|
|
373
|
+
semantics,
|
|
374
|
+
metadata
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
async fromChat(chat, ctx) {
|
|
378
|
+
const shouldEmitEmptyTools = hasExplicitEmptyToolsSemantics(chat.semantics);
|
|
379
|
+
const payload = {
|
|
380
|
+
messages: chat.messages,
|
|
381
|
+
tools: chat.tools ?? (shouldEmitEmptyTools ? [] : undefined),
|
|
382
|
+
...(chat.parameters || {})
|
|
383
|
+
};
|
|
384
|
+
applyExtraFields(payload, chat.metadata, chat.semantics);
|
|
385
|
+
// Do not forward tool_outputs to provider wire formats. OpenAI Chat
|
|
386
|
+
// endpoints expect tool results to appear as tool role messages, and
|
|
387
|
+
// sending the legacy top-level field causes upstream HTTP 400 responses.
|
|
388
|
+
// Concrete translation happens earlier when responses input is unfolded
|
|
389
|
+
// into ChatEnvelope.messages, so the provider request only needs the
|
|
390
|
+
// canonical message list.
|
|
391
|
+
if (payload.max_tokens === undefined && typeof payload.max_output_tokens === 'number') {
|
|
392
|
+
payload.max_tokens = payload.max_output_tokens;
|
|
393
|
+
delete payload.max_output_tokens;
|
|
394
|
+
}
|
|
395
|
+
return {
|
|
396
|
+
protocol: 'openai-chat',
|
|
397
|
+
direction: 'response',
|
|
398
|
+
payload,
|
|
399
|
+
meta: {
|
|
400
|
+
context: ctx
|
|
401
|
+
}
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
}
|