@jsonstudio/llms 0.6.3214 → 0.6.3271

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 (38) hide show
  1. package/dist/conversion/bridge-actions.js +37 -322
  2. package/dist/conversion/bridge-instructions.js +12 -109
  3. package/dist/conversion/codecs/anthropic-openai-codec.js +1 -1
  4. package/dist/conversion/compat/actions/deepseek-web-request.js +43 -110
  5. package/dist/conversion/compat/actions/deepseek-web-response.d.ts +3 -0
  6. package/dist/conversion/compat/actions/deepseek-web-response.js +150 -11
  7. package/dist/conversion/hub/operation-table/semantic-mappers/archive/chat-mapper.archive.d.ts +8 -0
  8. package/dist/conversion/hub/operation-table/semantic-mappers/archive/chat-mapper.archive.js +404 -0
  9. package/dist/conversion/hub/operation-table/semantic-mappers/chat-mapper.js +5 -384
  10. package/dist/conversion/hub/response/response-runtime.d.ts +1 -0
  11. package/dist/conversion/hub/response/response-runtime.js +26 -0
  12. package/dist/conversion/hub/snapshot-recorder.js +2 -91
  13. package/dist/conversion/hub/tool-governance/engine.d.ts +1 -1
  14. package/dist/conversion/hub/tool-governance/engine.js +17 -127
  15. package/dist/conversion/shared/anthropic-message-utils.d.ts +3 -1
  16. package/dist/conversion/shared/anthropic-message-utils.js +23 -15
  17. package/dist/conversion/shared/openai-finalizer.d.ts +0 -3
  18. package/dist/conversion/shared/openai-finalizer.js +11 -169
  19. package/dist/conversion/shared/openai-message-normalize.js +11 -72
  20. package/dist/conversion/shared/tool-mapping.js +5 -0
  21. package/dist/native/router_hotpath_napi.node +0 -0
  22. package/dist/router/virtual-router/bootstrap/provider-normalization.js +11 -3
  23. package/dist/router/virtual-router/engine-selection/native-hub-bridge-action-semantics.d.ts +20 -0
  24. package/dist/router/virtual-router/engine-selection/native-hub-bridge-action-semantics.js +71 -0
  25. package/dist/router/virtual-router/engine-selection/native-hub-pipeline-governance-semantics.d.ts +8 -0
  26. package/dist/router/virtual-router/engine-selection/native-hub-pipeline-governance-semantics.js +48 -0
  27. package/dist/router/virtual-router/engine-selection/native-hub-pipeline-req-inbound-semantics.d.ts +1 -0
  28. package/dist/router/virtual-router/engine-selection/native-hub-pipeline-req-inbound-semantics.js +30 -0
  29. package/dist/router/virtual-router/engine-selection/native-hub-pipeline-semantic-mappers.d.ts +2 -0
  30. package/dist/router/virtual-router/engine-selection/native-hub-pipeline-semantic-mappers.js +83 -0
  31. package/dist/router/virtual-router/engine-selection/native-router-hotpath-loader.js +11 -0
  32. package/dist/router/virtual-router/engine-selection/native-shared-conversion-semantics.d.ts +2 -0
  33. package/dist/router/virtual-router/engine-selection/native-shared-conversion-semantics.js +61 -0
  34. package/dist/router/virtual-router/engine-selection/native-snapshot-hooks.d.ts +1 -0
  35. package/dist/router/virtual-router/engine-selection/native-snapshot-hooks.js +40 -0
  36. package/dist/router/virtual-router/engine.js +58 -1
  37. package/dist/router/virtual-router/types.d.ts +1 -1
  38. package/package.json +1 -1
@@ -1,82 +1,4 @@
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
- }
1
+ import { mapOpenaiChatFromChatWithNative, mapOpenaiChatToChatWithNative } from '../../../../router/virtual-router/engine-selection/native-hub-pipeline-semantic-mappers.js';
80
2
  export function maybeAugmentApplyPatchErrorContent(content, toolName) {
81
3
  if (!content)
82
4
  return content;
@@ -86,319 +8,18 @@ export function maybeAugmentApplyPatchErrorContent(content, toolName) {
86
8
  if (!isApplyPatch) {
87
9
  return content;
88
10
  }
89
- // 避免重复追加提示。
11
+ // Avoid duplicate hints.
90
12
  if (content.includes('[apply_patch hint]')) {
91
13
  return content;
92
14
  }
93
- const hint = '\n\n[apply_patch hint] 在使用 apply_patch 之前,请先读取目标文件的最新内容,并基于该内容生成补丁;同时确保补丁格式符合工具规范(统一补丁格式或结构化参数),避免上下文不匹配或语法错误。';
15
+ const hint = '\n\n[apply_patch hint] \u5728\u4f7f\u7528 apply_patch \u4e4b\u524d\uff0c\u8bf7\u5148\u8bfb\u53d6\u76ee\u6807\u6587\u4ef6\u7684\u6700\u65b0\u5185\u5bb9\uff0c\u5e76\u57fa\u4e8e\u8be5\u5185\u5bb9\u751f\u6210\u8865\u4e01\uff1b\u540c\u65f6\u786e\u4fdd\u8865\u4e01\u683c\u5f0f\u7b26\u5408\u5de5\u5177\u89c4\u8303\uff08\u7edf\u4e00\u8865\u4e01\u683c\u5f0f\u6216\u7ed3\u6784\u5316\u53c2\u6570\uff09\uff0c\u907f\u514d\u4e0a\u4e0b\u6587\u4e0d\u5339\u914d\u6216\u8bed\u6cd5\u9519\u8bef\u3002';
94
16
  return content + hint;
95
17
  }
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
18
  export class ChatSemanticMapper {
342
19
  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
- };
20
+ return mapOpenaiChatToChatWithNative((format.payload ?? {}), ctx);
376
21
  }
377
22
  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
- };
23
+ return mapOpenaiChatFromChatWithNative(chat, ctx);
403
24
  }
404
25
  }
@@ -2,6 +2,7 @@ import type { JsonObject } from '../types/json.js';
2
2
  type ToolAliasMap = Record<string, string>;
3
3
  interface AnthropicResponseOptions {
4
4
  aliasMap?: ToolAliasMap;
5
+ includeToolCallIds?: boolean;
5
6
  }
6
7
  export declare function buildOpenAIChatFromAnthropicMessage(payload: JsonObject, options?: AnthropicResponseOptions): JsonObject;
7
8
  export declare function buildAnthropicResponseFromChat(chatResponse: JsonObject, options?: AnthropicResponseOptions): JsonObject;
@@ -221,8 +221,10 @@ export function buildOpenAIChatFromAnthropicMessage(payload, options) {
221
221
  default: return 'stop';
222
222
  }
223
223
  };
224
+ const includeToolCallIds = options?.includeToolCallIds === true;
224
225
  const canonicalToolCalls = toolCalls.map((tc) => ({
225
226
  id: tc.id,
227
+ ...(includeToolCallIds ? { call_id: tc.id, tool_call_id: tc.id } : {}),
226
228
  type: 'function',
227
229
  function: { name: tc.name, arguments: tc.args }
228
230
  }));
@@ -237,11 +239,35 @@ export function buildOpenAIChatFromAnthropicMessage(payload, options) {
237
239
  const key = deriveToolCallKey(inferred);
238
240
  if (key && seen.has(key))
239
241
  continue;
242
+ if (includeToolCallIds && typeof inferred.id === 'string') {
243
+ const inferredId = String(inferred.id);
244
+ if (!("call_id" in inferred))
245
+ inferred.call_id = inferredId;
246
+ if (!("tool_call_id" in inferred))
247
+ inferred.tool_call_id = inferredId;
248
+ }
240
249
  canonicalToolCalls.push(inferred);
241
250
  if (key)
242
251
  seen.add(key);
243
252
  }
244
253
  }
254
+ for (const call of canonicalToolCalls) {
255
+ const cid = typeof call.id === 'string' ? String(call.id) : '';
256
+ if (includeToolCallIds) {
257
+ if (cid) {
258
+ if (!("call_id" in call))
259
+ call.call_id = cid;
260
+ if (!("tool_call_id" in call))
261
+ call.tool_call_id = cid;
262
+ }
263
+ }
264
+ else {
265
+ if ("call_id" in call)
266
+ delete call.call_id;
267
+ if ("tool_call_id" in call)
268
+ delete call.tool_call_id;
269
+ }
270
+ }
245
271
  const message = {
246
272
  role: typeof payload.role === 'string' ? payload.role : 'assistant',
247
273
  content: textParts.join('\n')
@@ -1,6 +1,5 @@
1
1
  import { createSnapshotWriter } from '../snapshot-utils.js';
2
- import { jsonClone } from './types/json.js';
3
- import { parseLenientJsonishWithNative } from '../../router/virtual-router/engine-selection/native-shared-conversion-semantics.js';
2
+ import { normalizeSnapshotStagePayloadWithNative } from '../../router/virtual-router/engine-selection/native-snapshot-hooks.js';
4
3
  export class SnapshotStageRecorder {
5
4
  options;
6
5
  writer;
@@ -22,7 +21,7 @@ export class SnapshotStageRecorder {
22
21
  if (!this.writer) {
23
22
  return;
24
23
  }
25
- const normalized = normalizeStagePayload(stage, payload);
24
+ const normalized = normalizeSnapshotStagePayloadWithNative(stage, payload);
26
25
  if (!normalized) {
27
26
  return;
28
27
  }
@@ -37,91 +36,3 @@ export class SnapshotStageRecorder {
37
36
  export function createSnapshotRecorder(context, endpoint) {
38
37
  return new SnapshotStageRecorder({ context, endpoint });
39
38
  }
40
- const STAGE_KIND_MAP = {
41
- req_inbound_stage2_semantic_map: 'request_inbound',
42
- 'chat_process.req.stage2.semantic_map': 'request_inbound',
43
- req_outbound_stage1_semantic_map: 'request_outbound',
44
- 'chat_process.req.stage6.outbound.semantic_map': 'request_outbound',
45
- resp_inbound_stage3_semantic_map: 'response_inbound',
46
- 'chat_process.resp.stage4.semantic_map_to_chat': 'response_inbound',
47
- resp_outbound_stage1_client_remap: 'response_outbound',
48
- 'chat_process.resp.stage9.client_remap': 'response_outbound'
49
- };
50
- function normalizeStagePayload(stage, payload) {
51
- const kind = STAGE_KIND_MAP[stage];
52
- if (!kind) {
53
- return payload;
54
- }
55
- if ((kind === 'request_inbound' || kind === 'request_outbound') && isChatEnvelope(payload)) {
56
- return buildOpenAIChatSnapshot(payload);
57
- }
58
- if (kind === 'response_inbound' || kind === 'response_outbound') {
59
- return cloneJson(payload);
60
- }
61
- return payload;
62
- }
63
- function isChatEnvelope(value) {
64
- return Boolean(value &&
65
- typeof value === 'object' &&
66
- Array.isArray(value.messages) &&
67
- value.metadata &&
68
- typeof value.metadata === 'object');
69
- }
70
- function buildOpenAIChatSnapshot(envelope) {
71
- const snapshot = {};
72
- if (envelope.parameters && Object.keys(envelope.parameters).length) {
73
- Object.assign(snapshot, jsonClone(envelope.parameters));
74
- }
75
- snapshot.messages = jsonClone(envelope.messages);
76
- if (envelope.tools && envelope.tools.length) {
77
- snapshot.tools = jsonClone(envelope.tools);
78
- }
79
- if (envelope.toolOutputs && envelope.toolOutputs.length) {
80
- snapshot.tool_outputs = jsonClone(envelope.toolOutputs);
81
- }
82
- const meta = buildMetaSnapshot(envelope.metadata);
83
- if (meta) {
84
- snapshot.meta = meta;
85
- }
86
- return snapshot;
87
- }
88
- function buildMetaSnapshot(metadata) {
89
- const meta = {};
90
- if (metadata.context) {
91
- meta.context = jsonClone(metadata.context);
92
- }
93
- if (Array.isArray(metadata.missingFields) && metadata.missingFields.length) {
94
- meta.missing_fields = jsonClone(metadata.missingFields);
95
- }
96
- const extraKeys = Object.keys(metadata).filter((key) => key !== 'context' && key !== 'missingFields');
97
- if (extraKeys.length) {
98
- const extras = {};
99
- for (const key of extraKeys) {
100
- const value = metadata[key];
101
- if (value !== undefined) {
102
- extras[key] = jsonClone(value);
103
- }
104
- }
105
- if (Object.keys(extras).length) {
106
- meta.extra = extras;
107
- }
108
- }
109
- return Object.keys(meta).length ? meta : undefined;
110
- }
111
- function cloneJson(payload) {
112
- try {
113
- const nativeCloned = parseLenientJsonishWithNative(payload);
114
- if (nativeCloned && typeof nativeCloned === 'object' && !Array.isArray(nativeCloned)) {
115
- return nativeCloned;
116
- }
117
- }
118
- catch {
119
- // native clone is best-effort
120
- }
121
- try {
122
- return JSON.parse(JSON.stringify(payload));
123
- }
124
- catch {
125
- return payload;
126
- }
127
- }
@@ -4,7 +4,7 @@ import type { GovernedChatCompletionPayload, GovernedStandardizedRequest, ToolGo
4
4
  /**
5
5
  * Hybrid governance engine:
6
6
  * - request path: native-primary
7
- * - response path: legacy TS rules (until native response-governance export is available)
7
+ * - response path: native-primary
8
8
  *
9
9
  * Legacy-only snapshot retained at:
10
10
  * - src/conversion/hub/tool-governance/archive/engine.legacy.ts