@jsonstudio/llms 0.6.795 → 0.6.938
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/bridge/routecodex-adapter.d.ts +74 -0
- package/dist/config-unified/enhanced-path-resolver.d.ts +5 -0
- package/dist/config-unified/unified-config.d.ts +26 -0
- package/dist/conversion/codec-registry.d.ts +10 -0
- package/dist/conversion/codecs/gemini-openai-codec.d.ts +16 -0
- package/dist/conversion/codecs/openai-openai-codec.d.ts +12 -0
- package/dist/conversion/codecs/responses-openai-codec.d.ts +12 -0
- package/dist/conversion/compat/profiles/chat-gemini.json +12 -0
- package/dist/conversion/config/config-manager.d.ts +212 -0
- package/dist/conversion/hub/config/types.d.ts +26 -0
- package/dist/conversion/hub/core/detour-registry.d.ts +9 -0
- package/dist/conversion/hub/core/hub-context.d.ts +21 -0
- package/dist/conversion/hub/core/index.d.ts +3 -0
- package/dist/conversion/hub/core/stage-driver.d.ts +30 -0
- package/dist/conversion/hub/format-adapters/anthropic-format-adapter.d.ts +16 -0
- package/dist/conversion/hub/format-adapters/chat-format-adapter.d.ts +17 -0
- package/dist/conversion/hub/format-adapters/gemini-format-adapter.d.ts +16 -0
- package/dist/conversion/hub/format-adapters/index.d.ts +21 -0
- package/dist/conversion/hub/hub-feature.d.ts +1 -0
- package/dist/conversion/hub/node-support.d.ts +19 -0
- package/dist/conversion/hub/pipeline/compat/compat-pipeline-executor.js +11 -0
- package/dist/conversion/hub/pipeline/compat/compat-types.d.ts +3 -0
- package/dist/conversion/hub/pipeline/hub-pipeline.d.ts +7 -0
- package/dist/conversion/hub/pipeline/hub-pipeline.js +71 -14
- package/dist/conversion/hub/pipeline/stages/req_process/req_process_stage1_tool_governance/index.js +4 -0
- package/dist/conversion/hub/pipeline/stages/resp_outbound/resp_outbound_stage1_client_remap/index.js +23 -1
- package/dist/conversion/hub/pipelines/inbound.d.ts +22 -0
- package/dist/conversion/hub/pipelines/outbound.d.ts +22 -0
- package/dist/conversion/hub/policy/policy-engine.d.ts +46 -0
- package/dist/conversion/hub/policy/policy-engine.js +176 -0
- package/dist/conversion/hub/policy/protocol-spec.d.ts +50 -0
- package/dist/conversion/hub/policy/protocol-spec.js +105 -0
- package/dist/conversion/hub/process/chat-process.d.ts +32 -0
- package/dist/conversion/hub/registry.d.ts +28 -0
- package/dist/conversion/hub/response/chat-response-utils.d.ts +6 -0
- package/dist/conversion/hub/response/provider-response.js +31 -0
- package/dist/conversion/hub/semantic-mappers/gemini-mapper.d.ts +7 -0
- package/dist/conversion/hub/semantic-mappers/gemini-mapper.js +87 -1
- package/dist/conversion/hub/semantic-mappers/index.d.ts +4 -0
- package/dist/conversion/hub/semantic-mappers/responses-mapper.d.ts +21 -0
- package/dist/conversion/hub/standardized-bridge.d.ts +12 -0
- package/dist/conversion/hub/types/chat-schema.d.ts +112 -0
- package/dist/conversion/hub/types/errors.d.ts +5 -0
- package/dist/conversion/hub/types/format-envelope.d.ts +7 -0
- package/dist/conversion/hub/types/index.d.ts +6 -0
- package/dist/conversion/hub/types/json.d.ts +9 -0
- package/dist/conversion/hub/types/node.d.ts +31 -0
- package/dist/conversion/responses/responses-openai-bridge.js +263 -10
- package/dist/conversion/schema-validator.d.ts +7 -0
- package/dist/conversion/shared/args-mapping.d.ts +18 -0
- package/dist/conversion/shared/chat-request-filters.d.ts +9 -0
- package/dist/conversion/shared/errors.d.ts +1 -1
- package/dist/conversion/shared/gemini-tool-utils.js +61 -0
- package/dist/conversion/shared/jsonish.d.ts +3 -0
- package/dist/conversion/shared/mcp-injection.d.ts +2 -0
- package/dist/conversion/shared/media.d.ts +1 -0
- package/dist/conversion/shared/openai-message-normalize.d.ts +1 -0
- package/dist/conversion/shared/payload-budget.d.ts +13 -0
- package/dist/conversion/shared/reasoning-mapping.d.ts +5 -0
- package/dist/conversion/shared/responses-request-adapter.d.ts +1 -28
- package/dist/conversion/shared/responses-request-adapter.js +1 -430
- package/dist/conversion/shared/snapshot-hooks.js +112 -4
- package/dist/conversion/shared/tool-governor.js +8 -2
- package/dist/conversion/shared/tool-harvester.d.ts +31 -0
- package/dist/conversion/shared/tool-mapping.js +10 -29
- package/dist/conversion/types.d.ts +33 -0
- package/dist/filters/builtin/add-fields-filter.d.ts +8 -0
- package/dist/filters/builtin/blacklist-filter.d.ts +8 -0
- package/dist/filters/builtin/whitelist-filter.d.ts +8 -0
- package/dist/filters/engine.d.ts +16 -0
- package/dist/filters/special/request-tool-choice-policy.d.ts +11 -0
- package/dist/filters/special/response-finish-invariants.d.ts +11 -0
- package/dist/filters/special/response-openai-to-responses-bridge.d.ts +13 -0
- package/dist/filters/special/response-tool-arguments-blacklist.d.ts +12 -0
- package/dist/filters/special/response-tool-arguments-schema-converge.d.ts +13 -0
- package/dist/filters/special/response-tool-arguments-stringify.d.ts +9 -0
- package/dist/filters/special/response-tool-arguments-whitelist.d.ts +11 -0
- package/dist/filters/special/tool-filter-hooks.d.ts +19 -0
- package/dist/filters/special/tool-post-constraints.d.ts +31 -0
- package/dist/filters/types.d.ts +68 -0
- package/dist/filters/utils/fieldmap-loader.d.ts +2 -0
- package/dist/filters/utils/snapshot-writer.d.ts +10 -0
- package/dist/guidance/index.d.ts +3 -0
- package/dist/guidance/index.js +78 -83
- package/dist/http/sse-response.d.ts +22 -0
- package/dist/router/virtual-router/bootstrap.d.ts +6 -0
- package/dist/router/virtual-router/bootstrap.js +49 -5
- package/dist/router/virtual-router/classifier.d.ts +10 -0
- package/dist/router/virtual-router/engine-selection.js +147 -15
- package/dist/router/virtual-router/engine.js +177 -31
- package/dist/router/virtual-router/error-center.d.ts +10 -0
- package/dist/router/virtual-router/features.d.ts +3 -0
- package/dist/router/virtual-router/routing-instructions.d.ts +23 -1
- package/dist/router/virtual-router/routing-instructions.js +120 -30
- package/dist/router/virtual-router/types.d.ts +11 -0
- package/dist/servertool/engine.js +189 -16
- package/dist/servertool/handlers/apply-patch-guard.js +269 -0
- package/dist/servertool/handlers/exec-command-guard.js +558 -0
- package/dist/servertool/handlers/followup-message-trimmer.d.ts +16 -0
- package/dist/servertool/handlers/followup-message-trimmer.js +198 -0
- package/dist/servertool/handlers/followup-request-builder.d.ts +17 -0
- package/dist/servertool/handlers/followup-request-builder.js +122 -0
- package/dist/servertool/handlers/gemini-empty-reply-continue.js +252 -51
- package/dist/servertool/handlers/iflow-model-error-retry.js +12 -22
- package/dist/servertool/handlers/stop-message-auto.js +237 -75
- package/dist/servertool/handlers/vision.js +15 -27
- package/dist/servertool/handlers/web-search.js +17 -43
- package/dist/servertool/server-side-tools.d.ts +3 -0
- package/dist/servertool/server-side-tools.js +3 -0
- package/dist/sse/json-to-sse/anthropic-json-to-sse-converter.d.ts +2 -1
- package/dist/sse/json-to-sse/chat-json-to-sse-converter.d.ts +80 -0
- package/dist/sse/json-to-sse/event-generators/chat.d.ts +55 -0
- package/dist/sse/json-to-sse/event-generators/responses.d.ts +99 -0
- package/dist/sse/json-to-sse/gemini-json-to-sse-converter.d.ts +2 -1
- package/dist/sse/json-to-sse/responses-json-to-sse-converter.d.ts +80 -0
- package/dist/sse/json-to-sse/sequencers/anthropic-sequencer.d.ts +1 -1
- package/dist/sse/json-to-sse/sequencers/chat-sequencer.d.ts +2 -2
- package/dist/sse/json-to-sse/sequencers/gemini-sequencer.d.ts +1 -1
- package/dist/sse/json-to-sse/sequencers/responses-sequencer.d.ts +40 -0
- package/dist/sse/shared/chat-serializer.d.ts +4 -0
- package/dist/sse/shared/constants.d.ts +272 -0
- package/dist/sse/shared/serializers/anthropic-event-serializer.d.ts +1 -1
- package/dist/sse/shared/serializers/base-serializer.d.ts +158 -0
- package/dist/sse/shared/serializers/chat-event-serializer.d.ts +82 -0
- package/dist/sse/shared/serializers/gemini-event-serializer.d.ts +1 -1
- package/dist/sse/shared/serializers/index.d.ts +2 -1
- package/dist/sse/shared/serializers/responses-event-serializer.d.ts +123 -0
- package/dist/sse/shared/serializers/types.d.ts +51 -0
- package/dist/sse/shared/utils.d.ts +254 -0
- package/dist/sse/shared/writer.d.ts +2 -2
- package/dist/sse/sse-to-json/anthropic-sse-to-json-converter.d.ts +1 -1
- package/dist/sse/sse-to-json/builders/anthropic-response-builder.d.ts +1 -1
- package/dist/sse/sse-to-json/builders/response-builder.d.ts +1 -1
- package/dist/sse/sse-to-json/chat-sse-to-json-converter.d.ts +2 -1
- package/dist/sse/sse-to-json/gemini-sse-to-json-converter.d.ts +2 -1
- package/dist/sse/sse-to-json/parsers/sse-parser.d.ts +73 -0
- package/dist/sse/sse-to-json/responses-sse-to-json-converter.d.ts +1 -1
- package/dist/sse/types/chat-types.d.ts +1 -1
- package/dist/sse/types/responses-types.d.ts +1 -1
- package/dist/tools/apply-patch/execution-capturer.d.ts +13 -0
- package/dist/tools/apply-patch/execution-capturer.js +158 -0
- package/dist/tools/apply-patch/regression-capturer.d.ts +1 -0
- package/dist/tools/apply-patch/regression-capturer.js +5 -4
- package/dist/tools/apply-patch/structured.js +109 -13
- package/dist/tools/apply-patch/validator.js +261 -17
- package/dist/tools/tool-registry.d.ts +8 -0
- package/dist/tools/tool-registry.js +2 -1
- package/package.json +4 -4
- package/dist/conversion/compat/actions/apply-patch-format-fixer.js +0 -233
- package/dist/conversion/config/compat-profiles.json +0 -38
- package/dist/conversion/hub/response/server-side-tools.d.ts +0 -26
- package/dist/conversion/hub/response/server-side-tools.js +0 -383
- package/dist/conversion/shared/bridge-conversation-store.d.ts +0 -41
- package/dist/conversion/shared/bridge-conversation-store.js +0 -279
- package/dist/conversion/shared/bridge-request-adapter.d.ts +0 -28
- package/dist/conversion/shared/bridge-request-adapter.js +0 -430
- package/dist/conversion/shared/responses-id-utils.js +0 -42
- package/dist/conversion/shared/responses-instructions.js +0 -113
- package/dist/conversion/shared/responses-message-utils.d.ts +0 -15
- package/dist/conversion/shared/responses-message-utils.js +0 -206
- package/dist/conversion/shared/responses-metadata.js +0 -1
- package/dist/conversion/shared/responses-output-utils.d.ts +0 -7
- package/dist/conversion/shared/responses-output-utils.js +0 -108
- package/dist/conversion/shared/responses-types.d.ts +0 -33
- package/dist/conversion/shared/tool-normalizers.d.ts +0 -4
- package/dist/conversion/shared/tool-normalizers.js +0 -84
- package/dist/filters/special/request-streaming-to-nonstreaming.d.ts +0 -13
- package/dist/filters/special/request-streaming-to-nonstreaming.js +0 -39
- package/dist/filters/special/response-apply-patch-toon-decode.d.ts +0 -23
- package/dist/filters/special/response-apply-patch-toon-decode.js +0 -460
- package/dist/filters/special/response-tool-arguments-toon-decode.d.ts +0 -10
- package/dist/filters/special/response-tool-arguments-toon-decode.js +0 -154
- package/dist/servertool/flow-types.d.ts +0 -40
- package/dist/servertool/flow-types.js +0 -1
- package/dist/servertool/orchestration-types.d.ts +0 -33
- package/dist/servertool/orchestration-types.js +0 -1
- package/dist/servertool/vision-tool.d.ts +0 -2
- package/dist/servertool/vision-tool.js +0 -185
- package/dist/tools/patch-args-normalizer.d.ts +0 -15
- package/dist/tools/patch-args-normalizer.js +0 -472
- package/dist/utils/toon.d.ts +0 -4
- package/dist/utils/toon.js +0 -75
- /package/dist/{conversion/compat/actions/apply-patch-format-fixer.d.ts → servertool/handlers/apply-patch-guard.d.ts} +0 -0
- /package/dist/{conversion/shared/responses-types.js → servertool/handlers/exec-command-guard.d.ts} +0 -0
|
@@ -1,430 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { ensureResponsesInstructions } from './responses-instructions.js';
|
|
3
|
-
import { serializeToolArguments, coerceBridgeRole, serializeToolOutput } from './bridge-message-utils.js';
|
|
4
|
-
export function captureResponsesContext(payload, dto) {
|
|
5
|
-
ensureResponsesInstructions(payload);
|
|
6
|
-
const context = {
|
|
7
|
-
requestId: dto?.route?.requestId,
|
|
8
|
-
instructions: typeof payload.instructions === 'string' ? payload.instructions : undefined,
|
|
9
|
-
input: Array.isArray(payload.input) ? payload.input : undefined,
|
|
10
|
-
include: payload.include,
|
|
11
|
-
store: payload.store,
|
|
12
|
-
toolChoice: payload.tool_choice,
|
|
13
|
-
parallelToolCalls: typeof payload.parallel_tool_calls === 'boolean' ? payload.parallel_tool_calls : undefined,
|
|
14
|
-
metadata: (payload.metadata && typeof payload.metadata === 'object') ? { ...payload.metadata } : undefined,
|
|
15
|
-
responseFormat: payload.response_format,
|
|
16
|
-
stream: typeof payload.stream === 'boolean' ? payload.stream : undefined,
|
|
17
|
-
instructionsIsRaw: payload.instructions_is_raw === true,
|
|
18
|
-
isChatPayload: Array.isArray(payload.messages)
|
|
19
|
-
};
|
|
20
|
-
if (Array.isArray(payload.tools)) {
|
|
21
|
-
context.toolsRaw = payload.tools;
|
|
22
|
-
}
|
|
23
|
-
context.isResponsesPayload = !context.isChatPayload && Array.isArray(context.input);
|
|
24
|
-
return context;
|
|
25
|
-
}
|
|
26
|
-
export function buildChatRequestFromResponses(payload, context) {
|
|
27
|
-
const toolOutputs = Array.isArray(payload.tool_outputs)
|
|
28
|
-
? payload.tool_outputs
|
|
29
|
-
: undefined;
|
|
30
|
-
const responseId = typeof payload.response_id === 'string' ? payload.response_id : undefined;
|
|
31
|
-
const hasInput = Array.isArray(payload.input) && payload.input.length > 0;
|
|
32
|
-
const submitOnlyMode = !!toolOutputs && !!responseId && !hasInput;
|
|
33
|
-
if (submitOnlyMode) {
|
|
34
|
-
return {
|
|
35
|
-
request: {
|
|
36
|
-
type: 'submit_tool_outputs',
|
|
37
|
-
model: payload.model,
|
|
38
|
-
response_id: responseId,
|
|
39
|
-
tool_outputs: toolOutputs,
|
|
40
|
-
stream: payload.stream === true
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
let toolsNormalized = Array.isArray(payload.tools)
|
|
45
|
-
? normalizeTools(payload.tools)
|
|
46
|
-
: undefined;
|
|
47
|
-
// Inject MCP tools (CCR style) if enabled
|
|
48
|
-
try {
|
|
49
|
-
const enableMcp = String(process.env?.ROUTECODEX_MCP_ENABLE ?? '1') !== '0';
|
|
50
|
-
if (enableMcp) {
|
|
51
|
-
// Discover servers from prior inputs if present (best-effort; depends on client payload)
|
|
52
|
-
const discovered = new Set();
|
|
53
|
-
try {
|
|
54
|
-
const input = Array.isArray(payload.input) ? payload.input : [];
|
|
55
|
-
for (const it of input) {
|
|
56
|
-
const t = (it && typeof it === 'object') ? it : {};
|
|
57
|
-
const isToolCall = (t.type === 'function_call' || t.type === 'tool_call');
|
|
58
|
-
const isToolMsg = (t.type === 'tool_message' || t.type === 'tool_result');
|
|
59
|
-
if (isToolCall) {
|
|
60
|
-
const args = typeof t.arguments === 'string' ? (() => { try {
|
|
61
|
-
return JSON.parse(t.arguments);
|
|
62
|
-
}
|
|
63
|
-
catch {
|
|
64
|
-
return {};
|
|
65
|
-
} })() : (t.arguments || {});
|
|
66
|
-
const sv = (args && typeof args === 'object') ? args.server : undefined;
|
|
67
|
-
if (typeof sv === 'string' && sv.trim())
|
|
68
|
-
discovered.add(sv.trim());
|
|
69
|
-
}
|
|
70
|
-
else if (isToolMsg) {
|
|
71
|
-
const output = t?.output;
|
|
72
|
-
const val = (typeof output === 'string') ? (() => { try {
|
|
73
|
-
return JSON.parse(output);
|
|
74
|
-
}
|
|
75
|
-
catch {
|
|
76
|
-
return null;
|
|
77
|
-
} })() : (output || null);
|
|
78
|
-
const args = (val && typeof val === 'object') ? val.arguments : undefined;
|
|
79
|
-
const sv = (args && typeof args === 'object') ? args.server : undefined;
|
|
80
|
-
if (typeof sv === 'string' && sv.trim())
|
|
81
|
-
discovered.add(sv.trim());
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
catch { /* ignore */ }
|
|
86
|
-
const list = toolsNormalized && Array.isArray(toolsNormalized) ? toolsNormalized : [];
|
|
87
|
-
const have = new Set(list.map((t) => (t?.function?.name || '').toString()));
|
|
88
|
-
const addTool = (def) => { if (!have.has(def.function.name)) {
|
|
89
|
-
list.push(def);
|
|
90
|
-
have.add(def.function.name);
|
|
91
|
-
} };
|
|
92
|
-
const obj = (props, req) => ({ type: 'object', properties: props, required: req, additionalProperties: false });
|
|
93
|
-
const serversRaw = String(process.env?.ROUTECODEX_MCP_SERVERS || '').trim();
|
|
94
|
-
const envServers = serversRaw ? serversRaw.split(',').map((s) => s.trim()).filter(Boolean) : [];
|
|
95
|
-
const merged = Array.from(new Set([...envServers, ...Array.from(discovered)]));
|
|
96
|
-
const serverProp = merged.length ? { type: 'string', enum: merged } : { type: 'string' };
|
|
97
|
-
// 初始仅注入 list_mcp_resources;有已知 server 后再注入其余 MCP 工具
|
|
98
|
-
// 与 Chat 路径保持一致:server 可选(不强制要求)
|
|
99
|
-
addTool({ type: 'function', function: { name: 'list_mcp_resources', strict: true, description: 'List resources from a given MCP server (arguments.server = server label).', parameters: obj({ server: serverProp, filter: { type: 'string' }, root: { type: 'string' } }, [] /* server optional */) } });
|
|
100
|
-
if (merged.length > 0) {
|
|
101
|
-
addTool({ type: 'function', function: { name: 'read_mcp_resource', strict: true, description: 'Read a resource via MCP server.', parameters: obj({ server: serverProp, uri: { type: 'string' } }, ['server', 'uri']) } });
|
|
102
|
-
addTool({ type: 'function', function: { name: 'list_mcp_resource_templates', strict: true, description: 'List resource templates via MCP server.', parameters: obj({ server: serverProp }, ['server']) } });
|
|
103
|
-
}
|
|
104
|
-
toolsNormalized = list;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
catch { /* ignore MCP injection */ }
|
|
108
|
-
const mergedInput = mergeToolOutputsIntoInput(context.input, payload.tool_outputs);
|
|
109
|
-
let messages = mapResponsesInputToChat({
|
|
110
|
-
instructions: context.instructions,
|
|
111
|
-
input: mergedInput,
|
|
112
|
-
toolsNormalized
|
|
113
|
-
});
|
|
114
|
-
const preservedSystemMessages = Array.isArray(context.originalSystemMessages)
|
|
115
|
-
? context.originalSystemMessages
|
|
116
|
-
.map((text) => (typeof text === 'string' ? text.trim() : ''))
|
|
117
|
-
.filter((text) => text.length > 0)
|
|
118
|
-
.map((text) => ({ role: 'system', content: text }))
|
|
119
|
-
: undefined;
|
|
120
|
-
if (preservedSystemMessages && preservedSystemMessages.length) {
|
|
121
|
-
const nonSystemMessages = messages.filter((msg) => String(msg?.role || '').toLowerCase() !== 'system');
|
|
122
|
-
messages = [...preservedSystemMessages, ...nonSystemMessages];
|
|
123
|
-
}
|
|
124
|
-
const request = {
|
|
125
|
-
model: payload.model || 'gpt-4.1-mini',
|
|
126
|
-
messages
|
|
127
|
-
};
|
|
128
|
-
for (const key of ['temperature', 'top_p', 'max_tokens', 'tool_choice', 'parallel_tool_calls', 'user', 'logit_bias', 'seed', 'response_format']) {
|
|
129
|
-
if (payload[key] !== undefined) {
|
|
130
|
-
request[key] = payload[key];
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
if (Array.isArray(toolsNormalized) && toolsNormalized.length) {
|
|
134
|
-
request.tools = toolsNormalized;
|
|
135
|
-
}
|
|
136
|
-
if (typeof payload.stream === 'boolean') {
|
|
137
|
-
request.stream = payload.stream;
|
|
138
|
-
}
|
|
139
|
-
return {
|
|
140
|
-
request,
|
|
141
|
-
toolsNormalized
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
function mergeToolOutputsIntoInput(input, toolOutputsRaw) {
|
|
145
|
-
const result = Array.isArray(input) ? [...input] : [];
|
|
146
|
-
if (!Array.isArray(toolOutputsRaw)) {
|
|
147
|
-
return result;
|
|
148
|
-
}
|
|
149
|
-
for (const entry of toolOutputsRaw) {
|
|
150
|
-
if (!entry || typeof entry !== 'object')
|
|
151
|
-
continue;
|
|
152
|
-
const data = entry;
|
|
153
|
-
const callIdRaw = data.tool_call_id ?? data.call_id ?? data.id;
|
|
154
|
-
if (typeof callIdRaw !== 'string' || !callIdRaw.trim())
|
|
155
|
-
continue;
|
|
156
|
-
const outputRaw = data.output;
|
|
157
|
-
const outputText = normalizeToolOutputPayload(outputRaw);
|
|
158
|
-
const normalizedCallId = normalizeFunctionCallId({ callId: callIdRaw, fallback: callIdRaw });
|
|
159
|
-
const outputId = normalizeFunctionCallOutputId({ callId: normalizedCallId, fallback: normalizedCallId });
|
|
160
|
-
result.push({
|
|
161
|
-
type: 'function_call_output',
|
|
162
|
-
id: outputId,
|
|
163
|
-
call_id: normalizedCallId,
|
|
164
|
-
output: outputText
|
|
165
|
-
});
|
|
166
|
-
}
|
|
167
|
-
return result;
|
|
168
|
-
}
|
|
169
|
-
function normalizeToolOutputPayload(payload) {
|
|
170
|
-
if (typeof payload === 'string') {
|
|
171
|
-
return payload;
|
|
172
|
-
}
|
|
173
|
-
if (payload == null) {
|
|
174
|
-
return '';
|
|
175
|
-
}
|
|
176
|
-
try {
|
|
177
|
-
return JSON.stringify(payload);
|
|
178
|
-
}
|
|
179
|
-
catch {
|
|
180
|
-
return String(payload);
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
function mapResponsesInputToChat(options) {
|
|
184
|
-
const { instructions, input, toolsNormalized } = options;
|
|
185
|
-
const messages = [];
|
|
186
|
-
if (typeof instructions === 'string' && instructions.length) {
|
|
187
|
-
messages.push({ role: 'system', content: instructions });
|
|
188
|
-
}
|
|
189
|
-
if (!Array.isArray(input))
|
|
190
|
-
return messages;
|
|
191
|
-
const toolNameById = new Map();
|
|
192
|
-
const toolCallIdAliases = new Map();
|
|
193
|
-
let lastToolCallId = null;
|
|
194
|
-
for (const entry of input) {
|
|
195
|
-
if (!entry || typeof entry !== 'object')
|
|
196
|
-
continue;
|
|
197
|
-
const entryType = typeof entry.type === 'string' ? entry.type.toLowerCase() : 'message';
|
|
198
|
-
if (typeof entry.content === 'string') {
|
|
199
|
-
const directText = (entry.content || '').toString().trim();
|
|
200
|
-
if (directText.length) {
|
|
201
|
-
const normalizedRole = coerceBridgeRole(entry.role || 'user');
|
|
202
|
-
messages.push({ role: normalizedRole, content: directText });
|
|
203
|
-
continue;
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
if (entryType === 'function_call' || entryType === 'tool_call') {
|
|
207
|
-
const rawName = typeof entry.name === 'string' ? entry.name : (typeof entry?.function?.name === 'string' ? entry.function.name : undefined);
|
|
208
|
-
const name = (typeof rawName === 'string' && rawName.includes('.')) ? rawName.slice(rawName.indexOf('.') + 1).trim() : rawName;
|
|
209
|
-
if (typeof name !== 'string' || !name.trim() || name.toLowerCase() === 'tool') {
|
|
210
|
-
continue;
|
|
211
|
-
}
|
|
212
|
-
const args = entry.arguments ?? entry?.function?.arguments ?? {};
|
|
213
|
-
const parsedArgs = args;
|
|
214
|
-
const callIdRaw = typeof entry.id === 'string' ? entry.id : (typeof entry.call_id === 'string' ? entry.call_id : `call_${Math.random().toString(36).slice(2, 8)}`);
|
|
215
|
-
const callId = normalizeFunctionCallId({ callId: callIdRaw, fallback: `fc_call_${messages.length + 1}` });
|
|
216
|
-
if (callIdRaw && callIdRaw !== callId) {
|
|
217
|
-
toolCallIdAliases.set(callIdRaw, callId);
|
|
218
|
-
}
|
|
219
|
-
const serialized = serializeToolArguments(parsedArgs, name, toolsNormalized).trim();
|
|
220
|
-
toolNameById.set(callId, name);
|
|
221
|
-
messages.push({ role: 'assistant', tool_calls: [{ id: callId, type: 'function', function: { name, arguments: serialized } }] });
|
|
222
|
-
lastToolCallId = callId;
|
|
223
|
-
continue;
|
|
224
|
-
}
|
|
225
|
-
if (entryType === 'function_call_output' || entryType === 'tool_result' || entryType === 'tool_message') {
|
|
226
|
-
let toolCallId = entry.tool_call_id || entry.call_id || entry.tool_use_id || entry.id || lastToolCallId;
|
|
227
|
-
if (toolCallId && toolCallIdAliases.has(String(toolCallId))) {
|
|
228
|
-
toolCallId = toolCallIdAliases.get(String(toolCallId)) || toolCallId;
|
|
229
|
-
}
|
|
230
|
-
else if (typeof toolCallId === 'string' && toolCallId.trim().length) {
|
|
231
|
-
const normalized = normalizeFunctionCallId({ callId: toolCallId, fallback: toolCallId });
|
|
232
|
-
if (normalized !== toolCallId) {
|
|
233
|
-
toolCallIdAliases.set(toolCallId, normalized);
|
|
234
|
-
}
|
|
235
|
-
toolCallId = normalized;
|
|
236
|
-
}
|
|
237
|
-
const output = serializeToolOutput(entry);
|
|
238
|
-
if (toolCallId) {
|
|
239
|
-
try {
|
|
240
|
-
let contentStr = output != null ? String(output) : '';
|
|
241
|
-
if (!contentStr || contentStr.trim().length === 0) {
|
|
242
|
-
contentStr = 'Command succeeded (no output).';
|
|
243
|
-
}
|
|
244
|
-
const nm = toolNameById.get(String(toolCallId));
|
|
245
|
-
const toolMsg = { role: 'tool', tool_call_id: String(toolCallId), content: contentStr };
|
|
246
|
-
if (typeof nm === 'string' && nm.trim().length)
|
|
247
|
-
toolMsg.name = nm;
|
|
248
|
-
messages.push(toolMsg);
|
|
249
|
-
}
|
|
250
|
-
catch {
|
|
251
|
-
const fallback = (output ?? '');
|
|
252
|
-
const nm = toolNameById.get(String(toolCallId));
|
|
253
|
-
const toolMsg = { role: 'tool', tool_call_id: String(toolCallId), content: String(fallback) };
|
|
254
|
-
if (typeof nm === 'string' && nm.trim().length)
|
|
255
|
-
toolMsg.name = nm;
|
|
256
|
-
messages.push(toolMsg);
|
|
257
|
-
}
|
|
258
|
-
lastToolCallId = null;
|
|
259
|
-
}
|
|
260
|
-
continue;
|
|
261
|
-
}
|
|
262
|
-
let handledViaExplicitMessage = false;
|
|
263
|
-
if (entry && typeof entry.message === 'object' && Array.isArray(entry.message?.content)) {
|
|
264
|
-
const explicit = entry.message;
|
|
265
|
-
const { text, toolCalls, toolMessages, lastCallId } = processMessageBlocks(Array.isArray(explicit.content) ? explicit.content : [], toolsNormalized, toolNameById, toolCallIdAliases, lastToolCallId);
|
|
266
|
-
if (toolCalls.length)
|
|
267
|
-
messages.push({ role: 'assistant', tool_calls: toolCalls });
|
|
268
|
-
for (const msg of toolMessages)
|
|
269
|
-
messages.push(msg);
|
|
270
|
-
const normalizedRole = coerceBridgeRole((explicit.role ?? entry.role) || 'user');
|
|
271
|
-
if (typeof text === 'string' && text.trim().length) {
|
|
272
|
-
messages.push({ role: normalizedRole, content: text.trim() });
|
|
273
|
-
}
|
|
274
|
-
lastToolCallId = lastCallId;
|
|
275
|
-
handledViaExplicitMessage = true;
|
|
276
|
-
}
|
|
277
|
-
if (!handledViaExplicitMessage) {
|
|
278
|
-
const { text, toolCalls, toolMessages, lastCallId } = processMessageBlocks(Array.isArray(entry.content) ? entry.content : [], toolsNormalized, toolNameById, toolCallIdAliases, lastToolCallId);
|
|
279
|
-
if (toolCalls.length)
|
|
280
|
-
messages.push({ role: 'assistant', tool_calls: toolCalls });
|
|
281
|
-
for (const msg of toolMessages)
|
|
282
|
-
messages.push(msg);
|
|
283
|
-
const normalizedRole = coerceBridgeRole(entry.role || 'user');
|
|
284
|
-
if (typeof text === 'string' && text.trim().length) {
|
|
285
|
-
messages.push({ role: normalizedRole, content: text.trim() });
|
|
286
|
-
}
|
|
287
|
-
lastToolCallId = lastCallId;
|
|
288
|
-
}
|
|
289
|
-
try {
|
|
290
|
-
const t = String(entry.type || '').toLowerCase();
|
|
291
|
-
if ((t === 'input_text' || t === 'text' || t === 'output_text' || t === 'commentary') && typeof entry.text === 'string') {
|
|
292
|
-
const normalizedRole = coerceBridgeRole(entry.role || 'user');
|
|
293
|
-
const s = entry.text;
|
|
294
|
-
if (s && s.length)
|
|
295
|
-
messages.push({ role: normalizedRole, content: s });
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
catch { /* ignore */ }
|
|
299
|
-
}
|
|
300
|
-
return messages;
|
|
301
|
-
}
|
|
302
|
-
function processMessageBlocks(blocks, toolsNormalized, toolNameById, toolCallIdAliases, lastToolCallId) {
|
|
303
|
-
const textParts = [];
|
|
304
|
-
const toolCalls = [];
|
|
305
|
-
const toolMessages = [];
|
|
306
|
-
let currentLastCall = lastToolCallId;
|
|
307
|
-
for (const block of blocks) {
|
|
308
|
-
if (!block || typeof block !== 'object')
|
|
309
|
-
continue;
|
|
310
|
-
const type = typeof block.type === 'string' ? block.type.toLowerCase() : '';
|
|
311
|
-
if (type === 'input_text' || type === 'output_text' || type === 'text' || type === 'commentary') {
|
|
312
|
-
if (typeof block.text === 'string')
|
|
313
|
-
textParts.push(block.text);
|
|
314
|
-
else if (typeof block.content === 'string')
|
|
315
|
-
textParts.push(block.content);
|
|
316
|
-
continue;
|
|
317
|
-
}
|
|
318
|
-
if (type === 'message' && Array.isArray(block.content)) {
|
|
319
|
-
const nested = processMessageBlocks(block.content, toolsNormalized, toolNameById, toolCallIdAliases, currentLastCall);
|
|
320
|
-
if (nested.text)
|
|
321
|
-
textParts.push(nested.text);
|
|
322
|
-
for (const tc of nested.toolCalls)
|
|
323
|
-
toolCalls.push(tc);
|
|
324
|
-
for (const tm of nested.toolMessages)
|
|
325
|
-
toolMessages.push(tm);
|
|
326
|
-
currentLastCall = nested.lastCallId;
|
|
327
|
-
continue;
|
|
328
|
-
}
|
|
329
|
-
if (type === 'function_call') {
|
|
330
|
-
const rawName = typeof block.name === 'string' ? block.name : (typeof block?.function?.name === 'string' ? block.function.name : undefined);
|
|
331
|
-
const name = (typeof rawName === 'string' && rawName.includes('.')) ? rawName.slice(rawName.indexOf('.') + 1).trim() : rawName;
|
|
332
|
-
const args = block.arguments ?? block?.function?.arguments ?? {};
|
|
333
|
-
const parsedArgs = args;
|
|
334
|
-
const callIdRaw = typeof block.id === 'string' ? block.id : (typeof block.call_id === 'string' ? block.call_id : `call_${Math.random().toString(36).slice(2, 8)}`);
|
|
335
|
-
const callId = normalizeFunctionCallId({ callId: callIdRaw, fallback: `fc_call_${toolCalls.length + 1}` });
|
|
336
|
-
if (callIdRaw && callIdRaw !== callId) {
|
|
337
|
-
toolCallIdAliases.set(callIdRaw, callId);
|
|
338
|
-
}
|
|
339
|
-
if (typeof name !== 'string' || !name.trim()) {
|
|
340
|
-
currentLastCall = null;
|
|
341
|
-
continue;
|
|
342
|
-
}
|
|
343
|
-
const serialized = serializeToolArguments(parsedArgs, name, toolsNormalized).trim();
|
|
344
|
-
toolNameById.set(callId, name);
|
|
345
|
-
toolCalls.push({ id: callId, type: 'function', function: { name, arguments: serialized } });
|
|
346
|
-
currentLastCall = callId;
|
|
347
|
-
continue;
|
|
348
|
-
}
|
|
349
|
-
if (type === 'function_call_output' || type === 'tool_result' || type === 'tool_message') {
|
|
350
|
-
let toolCallId = block.tool_call_id || block.call_id || block.tool_use_id || block.id || currentLastCall;
|
|
351
|
-
if (toolCallId && toolCallIdAliases.has(String(toolCallId))) {
|
|
352
|
-
toolCallId = toolCallIdAliases.get(String(toolCallId)) || toolCallId;
|
|
353
|
-
}
|
|
354
|
-
else if (typeof toolCallId === 'string' && toolCallId.trim().length) {
|
|
355
|
-
const normalized = normalizeFunctionCallId({ callId: toolCallId, fallback: toolCallId });
|
|
356
|
-
if (normalized !== toolCallId) {
|
|
357
|
-
toolCallIdAliases.set(toolCallId, normalized);
|
|
358
|
-
}
|
|
359
|
-
toolCallId = normalized;
|
|
360
|
-
}
|
|
361
|
-
const output = serializeToolOutput(block);
|
|
362
|
-
if (toolCallId) {
|
|
363
|
-
try {
|
|
364
|
-
let contentStr = output != null ? String(output) : '';
|
|
365
|
-
if (!contentStr || contentStr.trim().length === 0) {
|
|
366
|
-
contentStr = 'Command succeeded (no output).';
|
|
367
|
-
}
|
|
368
|
-
const nm = toolNameById.get(String(toolCallId));
|
|
369
|
-
const toolMsg = { role: 'tool', tool_call_id: String(toolCallId), content: contentStr };
|
|
370
|
-
if (typeof nm === 'string' && nm.trim().length)
|
|
371
|
-
toolMsg.name = nm;
|
|
372
|
-
toolMessages.push(toolMsg);
|
|
373
|
-
}
|
|
374
|
-
catch {
|
|
375
|
-
const fallback = (output ?? '');
|
|
376
|
-
const nm = toolNameById.get(String(toolCallId));
|
|
377
|
-
const toolMsg = { role: 'tool', tool_call_id: String(toolCallId), content: String(fallback) };
|
|
378
|
-
if (typeof nm === 'string' && nm.trim().length)
|
|
379
|
-
toolMsg.name = nm;
|
|
380
|
-
toolMessages.push(toolMsg);
|
|
381
|
-
}
|
|
382
|
-
currentLastCall = null;
|
|
383
|
-
}
|
|
384
|
-
continue;
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
return {
|
|
388
|
-
text: textParts.length ? textParts.join('\n') : null,
|
|
389
|
-
toolCalls,
|
|
390
|
-
toolMessages,
|
|
391
|
-
lastCallId: currentLastCall
|
|
392
|
-
};
|
|
393
|
-
}
|
|
394
|
-
function normalizeTools(tools) {
|
|
395
|
-
const normalized = [];
|
|
396
|
-
for (const tool of tools) {
|
|
397
|
-
if (!tool || typeof tool !== 'object')
|
|
398
|
-
continue;
|
|
399
|
-
const originalType = typeof tool.type === 'string' ? tool.type : 'function';
|
|
400
|
-
const mappedType = ['function', 'custom'].includes(originalType.toLowerCase())
|
|
401
|
-
? 'function'
|
|
402
|
-
: originalType;
|
|
403
|
-
const fn = (tool.function && typeof tool.function === 'object')
|
|
404
|
-
? tool.function
|
|
405
|
-
: {};
|
|
406
|
-
const topName = typeof tool.name === 'string' ? tool.name : undefined;
|
|
407
|
-
const fnName = typeof fn.name === 'string' ? fn.name : undefined;
|
|
408
|
-
const name = (fnName || topName || '').trim();
|
|
409
|
-
if (!name)
|
|
410
|
-
continue;
|
|
411
|
-
const topDesc = typeof tool.description === 'string' ? tool.description : undefined;
|
|
412
|
-
const fnDesc = typeof fn.description === 'string' ? fn.description : undefined;
|
|
413
|
-
const description = fnDesc || topDesc;
|
|
414
|
-
const fnParams = Object.prototype.hasOwnProperty.call(fn, 'parameters') ? fn.parameters : undefined;
|
|
415
|
-
const topParams = Object.prototype.hasOwnProperty.call(tool, 'parameters') ? tool.parameters : undefined;
|
|
416
|
-
const parameters = fnParams !== undefined ? fnParams : topParams;
|
|
417
|
-
const strict = typeof fn.strict === 'boolean' ? fn.strict :
|
|
418
|
-
typeof tool.strict === 'boolean' ? tool.strict :
|
|
419
|
-
undefined;
|
|
420
|
-
const fnOut = { name };
|
|
421
|
-
if (description)
|
|
422
|
-
fnOut.description = description;
|
|
423
|
-
if (parameters !== undefined)
|
|
424
|
-
fnOut.parameters = parameters;
|
|
425
|
-
if (strict !== undefined)
|
|
426
|
-
fnOut.strict = strict;
|
|
427
|
-
normalized.push({ type: mappedType, function: fnOut });
|
|
428
|
-
}
|
|
429
|
-
return normalized;
|
|
430
|
-
}
|
|
1
|
+
export { captureResponsesContext, buildChatRequestFromResponses } from '../responses/responses-openai-bridge.js';
|
|
@@ -3,6 +3,7 @@ import path from 'node:path';
|
|
|
3
3
|
import os from 'node:os';
|
|
4
4
|
const DEFAULT_SNAPSHOT_ROOT = path.join(os.homedir(), '.routecodex', 'codex-samples');
|
|
5
5
|
const PENDING_PROVIDER_DIR = '__pending__';
|
|
6
|
+
const POLICY_VIOLATIONS_DIR = '__policy_violations__';
|
|
6
7
|
function resolveSnapshotRoot() {
|
|
7
8
|
const envOverride = process.env.RCC_SNAPSHOT_DIR ||
|
|
8
9
|
process.env.ROUTECODEX_SNAPSHOT_DIR;
|
|
@@ -110,6 +111,59 @@ function extractNestedGroupRequestId(value) {
|
|
|
110
111
|
}
|
|
111
112
|
return undefined;
|
|
112
113
|
}
|
|
114
|
+
function extractNestedEntryEndpoint(value) {
|
|
115
|
+
if (!value || typeof value !== 'object') {
|
|
116
|
+
return undefined;
|
|
117
|
+
}
|
|
118
|
+
const obj = value;
|
|
119
|
+
const direct = readStringField(obj.entryEndpoint) ||
|
|
120
|
+
readStringField(obj.entry_endpoint) ||
|
|
121
|
+
readStringField(obj.endpoint);
|
|
122
|
+
if (direct) {
|
|
123
|
+
return direct;
|
|
124
|
+
}
|
|
125
|
+
const meta = obj.meta;
|
|
126
|
+
if (meta && typeof meta === 'object') {
|
|
127
|
+
const m = meta;
|
|
128
|
+
const fromMeta = readStringField(m.entryEndpoint) ||
|
|
129
|
+
readStringField(m.entry_endpoint) ||
|
|
130
|
+
readStringField(m.endpoint);
|
|
131
|
+
if (fromMeta) {
|
|
132
|
+
return fromMeta;
|
|
133
|
+
}
|
|
134
|
+
const ctx = m.context;
|
|
135
|
+
if (ctx && typeof ctx === 'object') {
|
|
136
|
+
const c = ctx;
|
|
137
|
+
const fromCtx = readStringField(c.entryEndpoint) ||
|
|
138
|
+
readStringField(c.entry_endpoint) ||
|
|
139
|
+
readStringField(c.endpoint);
|
|
140
|
+
if (fromCtx) {
|
|
141
|
+
return fromCtx;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
const metadata = obj.metadata;
|
|
146
|
+
if (metadata && typeof metadata === 'object') {
|
|
147
|
+
const md = metadata;
|
|
148
|
+
const fromMetadata = readStringField(md.entryEndpoint) ||
|
|
149
|
+
readStringField(md.entry_endpoint) ||
|
|
150
|
+
readStringField(md.endpoint);
|
|
151
|
+
if (fromMetadata) {
|
|
152
|
+
return fromMetadata;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
const runtime = obj.runtime;
|
|
156
|
+
if (runtime && typeof runtime === 'object') {
|
|
157
|
+
const r = runtime;
|
|
158
|
+
const fromRuntime = readStringField(r.entryEndpoint) ||
|
|
159
|
+
readStringField(r.entry_endpoint) ||
|
|
160
|
+
readStringField(r.endpoint);
|
|
161
|
+
if (fromRuntime) {
|
|
162
|
+
return fromRuntime;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return undefined;
|
|
166
|
+
}
|
|
113
167
|
function toErrorCode(error) {
|
|
114
168
|
if (!error || typeof error !== 'object') {
|
|
115
169
|
return undefined;
|
|
@@ -117,6 +171,45 @@ function toErrorCode(error) {
|
|
|
117
171
|
const code = error.code;
|
|
118
172
|
return typeof code === 'string' && code.trim() ? code : undefined;
|
|
119
173
|
}
|
|
174
|
+
function readNumberField(value) {
|
|
175
|
+
return typeof value === 'number' && Number.isFinite(value) ? value : undefined;
|
|
176
|
+
}
|
|
177
|
+
function isHubPolicyStage(stage) {
|
|
178
|
+
return typeof stage === 'string' && stage.startsWith('hub_policy.');
|
|
179
|
+
}
|
|
180
|
+
function hasPolicyViolations(value) {
|
|
181
|
+
if (!value || typeof value !== 'object') {
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
const obj = value;
|
|
185
|
+
const violations = obj.violations;
|
|
186
|
+
if (Array.isArray(violations) && violations.length > 0) {
|
|
187
|
+
return true;
|
|
188
|
+
}
|
|
189
|
+
const summary = obj.summary;
|
|
190
|
+
if (summary && typeof summary === 'object') {
|
|
191
|
+
const total = readNumberField(summary.totalViolations);
|
|
192
|
+
if (typeof total === 'number' && total > 0) {
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
function hasPolicyEnforcementChanges(value) {
|
|
199
|
+
if (!value || typeof value !== 'object') {
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
const obj = value;
|
|
203
|
+
const removed = obj.removedTopLevelKeys;
|
|
204
|
+
if (Array.isArray(removed) && removed.length > 0) {
|
|
205
|
+
return true;
|
|
206
|
+
}
|
|
207
|
+
const flattened = obj.flattenedWrappers;
|
|
208
|
+
if (Array.isArray(flattened) && flattened.length > 0) {
|
|
209
|
+
return true;
|
|
210
|
+
}
|
|
211
|
+
return false;
|
|
212
|
+
}
|
|
120
213
|
async function writeUniqueFile(dir, baseName, contents) {
|
|
121
214
|
const parsed = path.parse(baseName);
|
|
122
215
|
const ext = parsed.ext || '.json';
|
|
@@ -203,9 +296,9 @@ async function promotePendingDir(options) {
|
|
|
203
296
|
await mergeDirs(pending, dest);
|
|
204
297
|
}
|
|
205
298
|
}
|
|
206
|
-
async function writeSnapshotFile(options) {
|
|
207
|
-
const root = resolveSnapshotRoot();
|
|
208
|
-
const folder = resolveSnapshotFolder(options.endpoint);
|
|
299
|
+
async function writeSnapshotFile(options, rootOverride) {
|
|
300
|
+
const root = rootOverride || resolveSnapshotRoot();
|
|
301
|
+
const folder = resolveSnapshotFolder(extractNestedEntryEndpoint(options.data) || options.endpoint);
|
|
209
302
|
const stageToken = sanitizeToken(options.stage, 'snapshot');
|
|
210
303
|
const groupRequestToken = sanitizeToken(options.groupRequestId ||
|
|
211
304
|
extractNestedGroupRequestId(options.data) ||
|
|
@@ -228,10 +321,25 @@ async function writeSnapshotFile(options) {
|
|
|
228
321
|
await writeUniqueFile(dir, fileName, payload);
|
|
229
322
|
}
|
|
230
323
|
export async function writeSnapshotViaHooks(options) {
|
|
324
|
+
const stage = sanitizeToken(options.stage, 'snapshot');
|
|
325
|
+
// 1) Normal snapshots (always).
|
|
231
326
|
try {
|
|
232
|
-
await writeSnapshotFile(options);
|
|
327
|
+
await writeSnapshotFile({ ...options, stage });
|
|
233
328
|
}
|
|
234
329
|
catch {
|
|
235
330
|
// snapshot writes must never block callers
|
|
236
331
|
}
|
|
332
|
+
// 2) Policy violations snapshots (copy-on-violation only).
|
|
333
|
+
// This allows leaving observe mode always on without drowning normal codex-samples traffic.
|
|
334
|
+
try {
|
|
335
|
+
if (isHubPolicyStage(stage) &&
|
|
336
|
+
(hasPolicyViolations(options.data) || hasPolicyEnforcementChanges(options.data))) {
|
|
337
|
+
const base = resolveSnapshotRoot();
|
|
338
|
+
const policyRoot = path.join(base, POLICY_VIOLATIONS_DIR);
|
|
339
|
+
await writeSnapshotFile({ ...options, stage }, policyRoot);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
catch {
|
|
343
|
+
// never block callers
|
|
344
|
+
}
|
|
237
345
|
}
|
|
@@ -9,6 +9,10 @@ import { normalizeExecCommandArgs } from '../../tools/exec-command/normalize.js'
|
|
|
9
9
|
function isObject(v) { return !!v && typeof v === 'object' && !Array.isArray(v); }
|
|
10
10
|
// Note: tool schema strict augmentation removed per alignment
|
|
11
11
|
function enforceChatBudget(chat, _modelId) { return chat; }
|
|
12
|
+
function isTruthyEnv(value) {
|
|
13
|
+
const v = typeof value === 'string' ? value.trim().toLowerCase() : '';
|
|
14
|
+
return v === '1' || v === 'true' || v === 'yes' || v === 'on';
|
|
15
|
+
}
|
|
12
16
|
function tryWriteSnapshot(options, stage, data) {
|
|
13
17
|
try {
|
|
14
18
|
// 仅在 verbose 级别保存快照(环境变量)
|
|
@@ -251,7 +255,8 @@ export function normalizeApplyPatchToolCallsOnResponse(chat) {
|
|
|
251
255
|
originalArgs: rawArgs,
|
|
252
256
|
normalizedArgs: argsStr,
|
|
253
257
|
validationError: reason,
|
|
254
|
-
source: 'tool-governor.response'
|
|
258
|
+
source: 'tool-governor.response',
|
|
259
|
+
meta: { applyPatchToolMode: 'freeform' }
|
|
255
260
|
});
|
|
256
261
|
const snippet = typeof argsStr === 'string' && argsStr.trim().length
|
|
257
262
|
? argsStr.trim().slice(0, 200).replace(/\s+/g, ' ')
|
|
@@ -325,7 +330,8 @@ function normalizeSpecialToolCallsOnRequest(request) {
|
|
|
325
330
|
originalArgs: rawArgs,
|
|
326
331
|
normalizedArgs: argsStr,
|
|
327
332
|
validationError: reason,
|
|
328
|
-
source: 'tool-governor.request'
|
|
333
|
+
source: 'tool-governor.request',
|
|
334
|
+
meta: { applyPatchToolMode: 'freeform' }
|
|
329
335
|
});
|
|
330
336
|
const snippet = typeof argsStr === 'string' && argsStr.trim().length
|
|
331
337
|
? argsStr.trim().slice(0, 200).replace(/\s+/g, ' ')
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
type Unknown = Record<string, unknown>;
|
|
2
|
+
export interface HarvestContext {
|
|
3
|
+
requestId?: string;
|
|
4
|
+
idPrefix?: string;
|
|
5
|
+
chunkSize?: number;
|
|
6
|
+
source?: 'chat' | 'responses' | 'messages';
|
|
7
|
+
}
|
|
8
|
+
export interface HarvestSignal {
|
|
9
|
+
type: 'delta' | 'final' | 'compat';
|
|
10
|
+
payload: Unknown;
|
|
11
|
+
}
|
|
12
|
+
export interface DeltaEvent {
|
|
13
|
+
tool_calls?: Array<{
|
|
14
|
+
index: number;
|
|
15
|
+
id: string;
|
|
16
|
+
type: 'function';
|
|
17
|
+
function: {
|
|
18
|
+
name?: string;
|
|
19
|
+
arguments?: string;
|
|
20
|
+
};
|
|
21
|
+
}>;
|
|
22
|
+
content?: string;
|
|
23
|
+
role?: string;
|
|
24
|
+
}
|
|
25
|
+
export interface HarvestResult {
|
|
26
|
+
deltaEvents: DeltaEvent[];
|
|
27
|
+
normalized?: Unknown;
|
|
28
|
+
stats?: Unknown;
|
|
29
|
+
}
|
|
30
|
+
export declare function harvestTools(signal: HarvestSignal, ctx?: HarvestContext): HarvestResult;
|
|
31
|
+
export {};
|