@jsonstudio/llms 0.6.0 → 0.6.6
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/hub/pipeline/stages/req_inbound/req_inbound_stage3_context_capture/index.js +13 -0
- package/dist/conversion/hub/process/chat-process.js +12 -6
- package/dist/conversion/hub/response/response-mappers.d.ts +1 -1
- package/dist/conversion/hub/response/response-mappers.js +12 -2
- package/dist/conversion/hub/semantic-mappers/anthropic-mapper.js +20 -0
- package/dist/conversion/hub/semantic-mappers/chat-mapper.js +6 -27
- package/dist/conversion/hub/semantic-mappers/gemini-mapper.js +5 -20
- package/dist/conversion/hub/standardized-bridge.js +6 -9
- package/dist/conversion/responses/responses-openai-bridge.js +0 -4
- package/dist/conversion/shared/anthropic-message-utils.js +80 -12
- package/dist/conversion/shared/bridge-actions.js +0 -55
- package/dist/conversion/shared/bridge-policies.js +4 -10
- package/dist/conversion/shared/chat-request-filters.js +1 -3
- package/dist/conversion/shared/protocol-state.d.ts +4 -0
- package/dist/conversion/shared/protocol-state.js +23 -0
- package/dist/conversion/shared/responses-conversation-store.d.ts +35 -0
- package/dist/conversion/shared/responses-conversation-store.js +64 -19
- package/dist/conversion/shared/responses-output-builder.js +43 -22
- package/dist/conversion/shared/responses-response-utils.js +47 -1
- package/dist/conversion/shared/text-markup-normalizer.js +2 -2
- package/dist/conversion/shared/tool-canonicalizer.js +118 -16
- package/dist/conversion/shared/tool-filter-pipeline.js +1 -2
- package/dist/conversion/shared/tool-mapping.js +30 -0
- package/dist/filters/index.d.ts +18 -0
- package/dist/filters/index.js +0 -1
- package/dist/filters/special/request-streaming-to-nonstreaming.d.ts +13 -0
- package/dist/filters/special/request-streaming-to-nonstreaming.js +13 -1
- package/dist/sse/sse-to-json/builders/response-builder.d.ts +2 -0
- package/dist/sse/sse-to-json/builders/response-builder.js +76 -12
- package/dist/sse/types/responses-types.d.ts +2 -0
- package/package.json +3 -3
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const TTL_MS = 1000 * 60 * 30; //
|
|
1
|
+
const TTL_MS = 1000 * 60 * 30; // 30min
|
|
2
2
|
function cloneDeep(value) {
|
|
3
3
|
try {
|
|
4
4
|
if (typeof globalThis.structuredClone === 'function') {
|
|
@@ -49,19 +49,26 @@ function pickPersistedFields(payload) {
|
|
|
49
49
|
'input_audio',
|
|
50
50
|
'output_audio'
|
|
51
51
|
];
|
|
52
|
-
const
|
|
52
|
+
const next = {};
|
|
53
53
|
for (const key of fields) {
|
|
54
54
|
if (payload[key] !== undefined) {
|
|
55
|
-
|
|
55
|
+
next[key] = cloneDeep(payload[key]);
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
|
-
return
|
|
58
|
+
return next;
|
|
59
59
|
}
|
|
60
60
|
function normalizeOutputItemToInput(item) {
|
|
61
|
+
if (!item || typeof item !== 'object') {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
61
64
|
const type = typeof item.type === 'string' ? item.type : '';
|
|
62
65
|
if (type === 'message' || type === 'reasoning') {
|
|
63
66
|
const role = typeof item.role === 'string' ? item.role : 'assistant';
|
|
64
|
-
const content = Array.isArray(item.content)
|
|
67
|
+
const content = Array.isArray(item.content)
|
|
68
|
+
? cloneDeep(item.content)
|
|
69
|
+
: typeof item.text === 'string'
|
|
70
|
+
? [{ type: 'text', text: item.text }]
|
|
71
|
+
: [];
|
|
65
72
|
return {
|
|
66
73
|
type: 'message',
|
|
67
74
|
role,
|
|
@@ -70,7 +77,11 @@ function normalizeOutputItemToInput(item) {
|
|
|
70
77
|
};
|
|
71
78
|
}
|
|
72
79
|
if (type === 'function_call') {
|
|
73
|
-
const callId = typeof item.call_id === 'string'
|
|
80
|
+
const callId = typeof item.call_id === 'string'
|
|
81
|
+
? item.call_id
|
|
82
|
+
: typeof item.id === 'string'
|
|
83
|
+
? item.id
|
|
84
|
+
: undefined;
|
|
74
85
|
return {
|
|
75
86
|
type: 'function_call',
|
|
76
87
|
role: 'assistant',
|
|
@@ -80,9 +91,9 @@ function normalizeOutputItemToInput(item) {
|
|
|
80
91
|
arguments: item.arguments,
|
|
81
92
|
function: isRecord(item.function)
|
|
82
93
|
? cloneDeep(item.function)
|
|
83
|
-
:
|
|
94
|
+
: typeof item.name === 'string'
|
|
84
95
|
? { name: item.name, arguments: item.arguments }
|
|
85
|
-
: undefined
|
|
96
|
+
: undefined
|
|
86
97
|
};
|
|
87
98
|
}
|
|
88
99
|
return null;
|
|
@@ -139,6 +150,18 @@ function normalizeSubmittedToolOutputs(toolOutputs) {
|
|
|
139
150
|
class ResponsesConversationStore {
|
|
140
151
|
requestMap = new Map();
|
|
141
152
|
responseIndex = new Map();
|
|
153
|
+
rebindRequestId(oldId, newId) {
|
|
154
|
+
if (!oldId || !newId || oldId === newId) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
const entry = this.requestMap.get(oldId);
|
|
158
|
+
if (!entry) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
this.requestMap.delete(oldId);
|
|
162
|
+
entry.requestId = newId;
|
|
163
|
+
this.requestMap.set(newId, entry);
|
|
164
|
+
}
|
|
142
165
|
captureRequestContext(args) {
|
|
143
166
|
const { requestId, payload, context } = args;
|
|
144
167
|
if (!requestId || !payload)
|
|
@@ -148,7 +171,8 @@ class ResponsesConversationStore {
|
|
|
148
171
|
requestId,
|
|
149
172
|
basePayload: pickPersistedFields(payload),
|
|
150
173
|
input: coerceInputArray(context.input),
|
|
151
|
-
tools: coerceTools(context.toolsRaw ||
|
|
174
|
+
tools: coerceTools(context.toolsRaw) ||
|
|
175
|
+
coerceTools(Array.isArray(payload.tools) ? payload.tools : undefined),
|
|
152
176
|
createdAt: Date.now(),
|
|
153
177
|
updatedAt: Date.now()
|
|
154
178
|
};
|
|
@@ -164,7 +188,7 @@ class ResponsesConversationStore {
|
|
|
164
188
|
this.requestMap.set(requestId, entry);
|
|
165
189
|
}
|
|
166
190
|
recordResponse(args) {
|
|
167
|
-
const entry = this.requestMap.get(args.requestId);
|
|
191
|
+
const entry = args.requestId ? this.requestMap.get(args.requestId) : undefined;
|
|
168
192
|
if (!entry)
|
|
169
193
|
return;
|
|
170
194
|
const response = args.response;
|
|
@@ -188,9 +212,7 @@ class ResponsesConversationStore {
|
|
|
188
212
|
if (!entry) {
|
|
189
213
|
throw new Error('Responses conversation expired or not found');
|
|
190
214
|
}
|
|
191
|
-
const toolOutputs = Array.isArray(submitPayload.tool_outputs)
|
|
192
|
-
? submitPayload.tool_outputs
|
|
193
|
-
: [];
|
|
215
|
+
const toolOutputs = Array.isArray(submitPayload.tool_outputs) ? submitPayload.tool_outputs : [];
|
|
194
216
|
if (!toolOutputs.length) {
|
|
195
217
|
throw new Error('tool_outputs array is required when submitting Responses tool results');
|
|
196
218
|
}
|
|
@@ -208,7 +230,8 @@ class ResponsesConversationStore {
|
|
|
208
230
|
payload.model = submitPayload.model.trim();
|
|
209
231
|
}
|
|
210
232
|
if (submitPayload.metadata && isRecord(submitPayload.metadata)) {
|
|
211
|
-
|
|
233
|
+
const baseMeta = isRecord(payload.metadata) ? payload.metadata : {};
|
|
234
|
+
payload.metadata = { ...baseMeta, ...cloneDeep(submitPayload.metadata) };
|
|
212
235
|
}
|
|
213
236
|
delete payload.tool_outputs;
|
|
214
237
|
delete payload.response_id;
|
|
@@ -225,6 +248,8 @@ class ResponsesConversationStore {
|
|
|
225
248
|
};
|
|
226
249
|
}
|
|
227
250
|
clearRequest(requestId) {
|
|
251
|
+
if (!requestId)
|
|
252
|
+
return;
|
|
228
253
|
const entry = this.requestMap.get(requestId);
|
|
229
254
|
if (!entry)
|
|
230
255
|
return;
|
|
@@ -254,10 +279,14 @@ class ResponsesConversationStore {
|
|
|
254
279
|
}
|
|
255
280
|
}
|
|
256
281
|
}
|
|
257
|
-
|
|
282
|
+
const store = new ResponsesConversationStore();
|
|
283
|
+
const RESPONSES_DEBUG = (process.env.ROUTECODEX_RESPONSES_DEBUG || '').trim() === '1';
|
|
258
284
|
export function captureResponsesRequestContext(args) {
|
|
259
285
|
try {
|
|
260
|
-
|
|
286
|
+
if (RESPONSES_DEBUG) {
|
|
287
|
+
console.log('[responses-store] capture', args.requestId);
|
|
288
|
+
}
|
|
289
|
+
store.captureRequestContext(args);
|
|
261
290
|
}
|
|
262
291
|
catch {
|
|
263
292
|
/* ignore capture failures */
|
|
@@ -265,15 +294,31 @@ export function captureResponsesRequestContext(args) {
|
|
|
265
294
|
}
|
|
266
295
|
export function recordResponsesResponse(args) {
|
|
267
296
|
try {
|
|
268
|
-
|
|
297
|
+
if (RESPONSES_DEBUG) {
|
|
298
|
+
console.log('[responses-store] record', args.requestId, args.response?.id);
|
|
299
|
+
}
|
|
300
|
+
store.recordResponse(args);
|
|
269
301
|
}
|
|
270
302
|
catch {
|
|
271
303
|
/* ignore */
|
|
272
304
|
}
|
|
273
305
|
}
|
|
274
306
|
export function resumeResponsesConversation(responseId, submitPayload, options) {
|
|
275
|
-
|
|
307
|
+
if (RESPONSES_DEBUG) {
|
|
308
|
+
console.log('[responses-store] resume', responseId);
|
|
309
|
+
}
|
|
310
|
+
return store.resumeConversation(responseId, submitPayload, options);
|
|
276
311
|
}
|
|
277
312
|
export function clearResponsesConversationByRequestId(requestId) {
|
|
278
|
-
|
|
313
|
+
if (RESPONSES_DEBUG && requestId) {
|
|
314
|
+
console.log('[responses-store] clear', requestId);
|
|
315
|
+
}
|
|
316
|
+
store.clearRequest(requestId);
|
|
317
|
+
}
|
|
318
|
+
export function rebindResponsesConversationRequestId(oldId, newId) {
|
|
319
|
+
if (RESPONSES_DEBUG && oldId && newId) {
|
|
320
|
+
console.log('[responses-store] rebind', oldId, '->', newId);
|
|
321
|
+
}
|
|
322
|
+
store.rebindRequestId(oldId, newId);
|
|
279
323
|
}
|
|
324
|
+
export { store as responsesConversationStore };
|
|
@@ -112,29 +112,50 @@ export function buildResponsesOutputFromChat(options) {
|
|
|
112
112
|
};
|
|
113
113
|
}
|
|
114
114
|
function normalizeUsage(usageRaw) {
|
|
115
|
-
if (usageRaw
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
115
|
+
if (!usageRaw || typeof usageRaw !== 'object') {
|
|
116
|
+
return undefined;
|
|
117
|
+
}
|
|
118
|
+
const source = usageRaw;
|
|
119
|
+
const usage = {};
|
|
120
|
+
const inputValue = normalizeNumber(source.input_tokens ?? source.prompt_tokens);
|
|
121
|
+
if (inputValue !== undefined) {
|
|
122
|
+
usage.input_tokens = inputValue;
|
|
123
|
+
}
|
|
124
|
+
const outputValue = normalizeNumber(source.output_tokens ?? source.completion_tokens);
|
|
125
|
+
if (outputValue !== undefined) {
|
|
126
|
+
usage.output_tokens = outputValue;
|
|
127
|
+
}
|
|
128
|
+
const totalValue = normalizeNumber(source.total_tokens);
|
|
129
|
+
if (totalValue !== undefined) {
|
|
130
|
+
usage.total_tokens = totalValue;
|
|
131
|
+
}
|
|
132
|
+
else if (inputValue !== undefined && outputValue !== undefined) {
|
|
133
|
+
usage.total_tokens = inputValue + outputValue;
|
|
134
|
+
}
|
|
135
|
+
const inputDetails = extractTokenDetails(source.input_tokens_details ??
|
|
136
|
+
source.prompt_tokens_details);
|
|
137
|
+
if (inputDetails) {
|
|
138
|
+
usage.input_tokens_details = inputDetails;
|
|
139
|
+
}
|
|
140
|
+
const outputDetails = extractTokenDetails(source.output_tokens_details ??
|
|
141
|
+
source.completion_tokens_details);
|
|
142
|
+
if (outputDetails) {
|
|
143
|
+
usage.output_tokens_details = outputDetails;
|
|
144
|
+
}
|
|
145
|
+
return Object.keys(usage).length ? usage : undefined;
|
|
146
|
+
}
|
|
147
|
+
function normalizeNumber(value) {
|
|
148
|
+
if (value == null) {
|
|
149
|
+
return undefined;
|
|
150
|
+
}
|
|
151
|
+
const parsed = Number(value);
|
|
152
|
+
return Number.isFinite(parsed) ? parsed : undefined;
|
|
153
|
+
}
|
|
154
|
+
function extractTokenDetails(raw) {
|
|
155
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) {
|
|
156
|
+
return undefined;
|
|
136
157
|
}
|
|
137
|
-
return
|
|
158
|
+
return { ...raw };
|
|
138
159
|
}
|
|
139
160
|
function buildFunctionCallOutput(call, allocateOutputId, sanitizeFunctionName, baseCount, offset) {
|
|
140
161
|
try {
|
|
@@ -167,7 +167,7 @@ export function buildChatResponseFromResponses(payload) {
|
|
|
167
167
|
const created = typeof response.created_at === 'number'
|
|
168
168
|
? response.created_at
|
|
169
169
|
: (response.created ?? Math.floor(Date.now() / 1000));
|
|
170
|
-
const usage = response.usage;
|
|
170
|
+
const usage = mapResponsesUsageToChat(response.usage);
|
|
171
171
|
const toolCalls = collectToolCallsFromResponses(response);
|
|
172
172
|
const { textParts, reasoningParts } = extractOutputSegments(response);
|
|
173
173
|
const rawReasoningSegments = collectRawReasoningSegments(response);
|
|
@@ -237,3 +237,49 @@ export function buildChatResponseFromResponses(payload) {
|
|
|
237
237
|
}
|
|
238
238
|
return chat;
|
|
239
239
|
}
|
|
240
|
+
function mapResponsesUsageToChat(usageRaw) {
|
|
241
|
+
if (!usageRaw || typeof usageRaw !== 'object') {
|
|
242
|
+
return undefined;
|
|
243
|
+
}
|
|
244
|
+
const source = usageRaw;
|
|
245
|
+
const usage = {};
|
|
246
|
+
const promptValue = toNumber(source.prompt_tokens ?? source.input_tokens);
|
|
247
|
+
if (promptValue !== undefined) {
|
|
248
|
+
usage.prompt_tokens = promptValue;
|
|
249
|
+
}
|
|
250
|
+
const completionValue = toNumber(source.completion_tokens ?? source.output_tokens);
|
|
251
|
+
if (completionValue !== undefined) {
|
|
252
|
+
usage.completion_tokens = completionValue;
|
|
253
|
+
}
|
|
254
|
+
const totalValue = toNumber(source.total_tokens);
|
|
255
|
+
if (totalValue !== undefined) {
|
|
256
|
+
usage.total_tokens = totalValue;
|
|
257
|
+
}
|
|
258
|
+
else if (promptValue !== undefined && completionValue !== undefined) {
|
|
259
|
+
usage.total_tokens = promptValue + completionValue;
|
|
260
|
+
}
|
|
261
|
+
const promptDetails = extractDetailObject(source.prompt_tokens_details ??
|
|
262
|
+
source.input_tokens_details);
|
|
263
|
+
if (promptDetails) {
|
|
264
|
+
usage.prompt_tokens_details = promptDetails;
|
|
265
|
+
}
|
|
266
|
+
const completionDetails = extractDetailObject(source.completion_tokens_details ??
|
|
267
|
+
source.output_tokens_details);
|
|
268
|
+
if (completionDetails) {
|
|
269
|
+
usage.completion_tokens_details = completionDetails;
|
|
270
|
+
}
|
|
271
|
+
return Object.keys(usage).length ? usage : undefined;
|
|
272
|
+
}
|
|
273
|
+
function toNumber(value) {
|
|
274
|
+
if (value == null) {
|
|
275
|
+
return undefined;
|
|
276
|
+
}
|
|
277
|
+
const parsed = Number(value);
|
|
278
|
+
return Number.isFinite(parsed) ? parsed : undefined;
|
|
279
|
+
}
|
|
280
|
+
function extractDetailObject(raw) {
|
|
281
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) {
|
|
282
|
+
return undefined;
|
|
283
|
+
}
|
|
284
|
+
return { ...raw };
|
|
285
|
+
}
|
|
@@ -95,10 +95,10 @@ export function extractApplyPatchCallsFromText(text) {
|
|
|
95
95
|
continue;
|
|
96
96
|
let argsStr = '{}';
|
|
97
97
|
try {
|
|
98
|
-
argsStr = JSON.stringify({ patch });
|
|
98
|
+
argsStr = JSON.stringify({ input: patch });
|
|
99
99
|
}
|
|
100
100
|
catch {
|
|
101
|
-
argsStr = '{"
|
|
101
|
+
argsStr = '{"input":""}';
|
|
102
102
|
}
|
|
103
103
|
out.push({ id: genId(), name: 'apply_patch', args: argsStr });
|
|
104
104
|
}
|
|
@@ -12,31 +12,133 @@ function repairArgumentsToString(args) {
|
|
|
12
12
|
return String(args);
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
|
+
function extractStringContent(value) {
|
|
16
|
+
if (typeof value === 'string') {
|
|
17
|
+
return value;
|
|
18
|
+
}
|
|
19
|
+
if (Array.isArray(value)) {
|
|
20
|
+
const parts = [];
|
|
21
|
+
for (const entry of value) {
|
|
22
|
+
if (typeof entry === 'string') {
|
|
23
|
+
parts.push(entry);
|
|
24
|
+
}
|
|
25
|
+
else if (entry && typeof entry === 'object') {
|
|
26
|
+
const text = entry.text;
|
|
27
|
+
if (typeof text === 'string') {
|
|
28
|
+
parts.push(text);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if (parts.length) {
|
|
33
|
+
return parts.join('\n');
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
function readApplyPatchArgument(args) {
|
|
39
|
+
if (!args || typeof args !== 'object')
|
|
40
|
+
return undefined;
|
|
41
|
+
const input = args.input;
|
|
42
|
+
if (typeof input === 'string' && input.trim().length > 0) {
|
|
43
|
+
return input;
|
|
44
|
+
}
|
|
45
|
+
const legacy = args.patch;
|
|
46
|
+
if (typeof legacy === 'string' && legacy.trim().length > 0) {
|
|
47
|
+
return legacy;
|
|
48
|
+
}
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
function hasApplyPatchInput(argText) {
|
|
52
|
+
if (!argText) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
try {
|
|
56
|
+
const parsed = JSON.parse(argText);
|
|
57
|
+
const content = readApplyPatchArgument(parsed);
|
|
58
|
+
return typeof content === 'string' && content.trim().length > 0;
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function extractUnifiedDiff(content) {
|
|
65
|
+
if (!content)
|
|
66
|
+
return undefined;
|
|
67
|
+
const begin = content.indexOf('*** Begin Patch');
|
|
68
|
+
const end = content.indexOf('*** End Patch');
|
|
69
|
+
if (begin >= 0 && end > begin) {
|
|
70
|
+
return content.slice(begin, end + '*** End Patch'.length).trim();
|
|
71
|
+
}
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
function createToolCallId() {
|
|
75
|
+
return `call_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
|
|
76
|
+
}
|
|
15
77
|
export function canonicalizeChatResponseTools(payload) {
|
|
16
78
|
try {
|
|
17
79
|
const out = isObject(payload) ? JSON.parse(JSON.stringify(payload)) : payload;
|
|
18
80
|
const choices = Array.isArray(out?.choices) ? out.choices : [];
|
|
19
81
|
for (const ch of choices) {
|
|
20
82
|
const msg = ch && ch.message ? ch.message : undefined;
|
|
21
|
-
const
|
|
22
|
-
|
|
83
|
+
const toolCalls = Array.isArray(msg?.tool_calls) ? msg.tool_calls : undefined;
|
|
84
|
+
const hasToolCalls = Array.isArray(toolCalls) && toolCalls.length > 0;
|
|
85
|
+
const originalContentText = extractStringContent(msg?.content);
|
|
86
|
+
const harvestedPatch = typeof originalContentText === 'string' ? extractUnifiedDiff(originalContentText) : undefined;
|
|
87
|
+
if (hasToolCalls) {
|
|
88
|
+
// ensure arguments is string and content is null when tool_calls present
|
|
89
|
+
try {
|
|
90
|
+
if (!ch.finish_reason)
|
|
91
|
+
ch.finish_reason = 'tool_calls';
|
|
92
|
+
}
|
|
93
|
+
catch { /* ignore */ }
|
|
94
|
+
try {
|
|
95
|
+
if (msg && typeof msg === 'object')
|
|
96
|
+
msg.content = null;
|
|
97
|
+
}
|
|
98
|
+
catch { /* ignore */ }
|
|
99
|
+
for (const tc of toolCalls) {
|
|
100
|
+
try {
|
|
101
|
+
const fn = tc && tc.function ? tc.function : undefined;
|
|
102
|
+
if (fn) {
|
|
103
|
+
const repaired = repairArgumentsToString(fn.arguments);
|
|
104
|
+
let nextArgs = repaired;
|
|
105
|
+
try {
|
|
106
|
+
const parsed = JSON.parse(repaired);
|
|
107
|
+
const diffText = readApplyPatchArgument(parsed);
|
|
108
|
+
if (typeof diffText === 'string' && diffText.trim().length > 0) {
|
|
109
|
+
nextArgs = JSON.stringify({ input: diffText });
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
// fallback to repaired string
|
|
114
|
+
}
|
|
115
|
+
fn.arguments = nextArgs;
|
|
116
|
+
if (typeof fn.name === 'string' &&
|
|
117
|
+
fn.name === 'apply_patch' &&
|
|
118
|
+
!hasApplyPatchInput(fn.arguments) &&
|
|
119
|
+
harvestedPatch) {
|
|
120
|
+
fn.arguments = JSON.stringify({ input: harvestedPatch });
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
catch { /* ignore */ }
|
|
125
|
+
}
|
|
23
126
|
continue;
|
|
24
|
-
// ensure arguments is string and content is null when tool_calls present
|
|
25
|
-
try {
|
|
26
|
-
if (!ch.finish_reason)
|
|
27
|
-
ch.finish_reason = 'tool_calls';
|
|
28
127
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
128
|
+
if (harvestedPatch) {
|
|
129
|
+
const id = createToolCallId();
|
|
130
|
+
const toolCall = {
|
|
131
|
+
id,
|
|
132
|
+
type: 'function',
|
|
133
|
+
function: {
|
|
134
|
+
name: 'apply_patch',
|
|
135
|
+
arguments: JSON.stringify({ input: harvestedPatch })
|
|
136
|
+
}
|
|
137
|
+
};
|
|
36
138
|
try {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
139
|
+
msg.tool_calls = [toolCall];
|
|
140
|
+
msg.content = null;
|
|
141
|
+
ch.finish_reason = 'tool_calls';
|
|
40
142
|
}
|
|
41
143
|
catch { /* ignore */ }
|
|
42
144
|
}
|
|
@@ -35,12 +35,11 @@ export async function runChatRequestToolFilters(chatRequest, options = {}) {
|
|
|
35
35
|
/* optional */
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
|
-
const { RequestToolCallsStringifyFilter, RequestToolChoicePolicyFilter
|
|
38
|
+
const { RequestToolCallsStringifyFilter, RequestToolChoicePolicyFilter } = await import('../../filters/index.js');
|
|
39
39
|
engine.registerFilter(new RequestToolCallsStringifyFilter());
|
|
40
40
|
if (!isAnthropic) {
|
|
41
41
|
engine.registerFilter(new RequestToolChoicePolicyFilter());
|
|
42
42
|
}
|
|
43
|
-
engine.registerFilter(new RequestStreamingToNonStreamingFilter());
|
|
44
43
|
try {
|
|
45
44
|
const cfg = await loadFieldMapConfig('openai-openai.fieldmap.json');
|
|
46
45
|
if (cfg)
|
|
@@ -15,6 +15,28 @@ const DEFAULT_SANITIZER = (value) => {
|
|
|
15
15
|
}
|
|
16
16
|
return undefined;
|
|
17
17
|
};
|
|
18
|
+
const APPLY_PATCH_TOOL_NAME = 'apply_patch';
|
|
19
|
+
const APPLY_PATCH_ARG_KEY = 'input';
|
|
20
|
+
function isApplyPatchTool(name) {
|
|
21
|
+
return typeof name === 'string' && name.trim() === APPLY_PATCH_TOOL_NAME;
|
|
22
|
+
}
|
|
23
|
+
function buildApplyPatchParameters() {
|
|
24
|
+
return {
|
|
25
|
+
type: 'object',
|
|
26
|
+
properties: {
|
|
27
|
+
[APPLY_PATCH_ARG_KEY]: {
|
|
28
|
+
type: 'string',
|
|
29
|
+
description: 'Unified diff patch content (*** Begin Patch ... *** End Patch)'
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
required: [APPLY_PATCH_ARG_KEY],
|
|
33
|
+
additionalProperties: false
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function applyPatchSchemaToFunction(fnNode) {
|
|
37
|
+
fnNode.parameters = buildApplyPatchParameters();
|
|
38
|
+
fnNode.strict = true;
|
|
39
|
+
}
|
|
18
40
|
function resolveToolName(candidate, options) {
|
|
19
41
|
const sanitized = options?.sanitizeName?.(candidate);
|
|
20
42
|
if (typeof sanitized === 'string' && sanitized.trim().length) {
|
|
@@ -80,6 +102,9 @@ export function bridgeToolToChatDefinition(rawTool, options) {
|
|
|
80
102
|
if (strict !== undefined) {
|
|
81
103
|
fnOut.strict = strict;
|
|
82
104
|
}
|
|
105
|
+
if (isApplyPatchTool(name)) {
|
|
106
|
+
applyPatchSchemaToFunction(fnOut);
|
|
107
|
+
}
|
|
83
108
|
return {
|
|
84
109
|
type: normalizedType,
|
|
85
110
|
function: fnOut
|
|
@@ -131,6 +156,11 @@ export function chatToolToBridgeDefinition(rawTool, options) {
|
|
|
131
156
|
if (strict !== undefined) {
|
|
132
157
|
fnOut.strict = strict;
|
|
133
158
|
}
|
|
159
|
+
if (isApplyPatchTool(name)) {
|
|
160
|
+
applyPatchSchemaToFunction(fnOut);
|
|
161
|
+
responseShape.parameters = buildApplyPatchParameters();
|
|
162
|
+
responseShape.strict = true;
|
|
163
|
+
}
|
|
134
164
|
responseShape.function = fnOut;
|
|
135
165
|
return responseShape;
|
|
136
166
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export * from './types.js';
|
|
2
|
+
export * from './engine.js';
|
|
3
|
+
export * from './builtin/whitelist-filter.js';
|
|
4
|
+
export * from './builtin/blacklist-filter.js';
|
|
5
|
+
export * from './builtin/add-fields-filter.js';
|
|
6
|
+
export * from './special/request-toolcalls-stringify.js';
|
|
7
|
+
export * from './special/request-tool-choice-policy.js';
|
|
8
|
+
export * from './special/request-tool-list-filter.js';
|
|
9
|
+
export * from './special/tool-filter-hooks.js';
|
|
10
|
+
export * from './special/response-tool-text-canonicalize.js';
|
|
11
|
+
export * from './special/response-tool-arguments-stringify.js';
|
|
12
|
+
export * from './special/response-finish-invariants.js';
|
|
13
|
+
export * from './special/request-tools-normalize.js';
|
|
14
|
+
export * from './special/response-tool-arguments-toon-decode.js';
|
|
15
|
+
export * from './special/response-tool-arguments-blacklist.js';
|
|
16
|
+
export * from './special/response-tool-arguments-schema-converge.js';
|
|
17
|
+
export * from './special/response-tool-arguments-whitelist.js';
|
|
18
|
+
export * from './special/tool-post-constraints.js';
|
package/dist/filters/index.js
CHANGED
|
@@ -9,7 +9,6 @@ export * from './special/request-toolcalls-stringify.js';
|
|
|
9
9
|
export * from './special/request-tool-choice-policy.js';
|
|
10
10
|
export * from './special/request-tool-list-filter.js';
|
|
11
11
|
export * from './special/tool-filter-hooks.js';
|
|
12
|
-
export * from './special/request-streaming-to-nonstreaming.js';
|
|
13
12
|
export * from './special/response-tool-text-canonicalize.js';
|
|
14
13
|
export * from './special/response-tool-arguments-stringify.js';
|
|
15
14
|
export * from './special/response-finish-invariants.js';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Filter, FilterContext, FilterResult, JsonObject } from '../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* RequestStreamingToNonStreamingFilter (request_post)
|
|
4
|
+
* - Unifies provider requests to non-streaming regardless of inbound stream flag.
|
|
5
|
+
* - Preserves original intent via `originalStream` and `_originalStreamOptions`,
|
|
6
|
+
* but ensures `stream: false` is sent to providers.
|
|
7
|
+
*/
|
|
8
|
+
export declare class RequestStreamingToNonStreamingFilter implements Filter<JsonObject> {
|
|
9
|
+
readonly name = "request_streaming_to_nonstreaming";
|
|
10
|
+
readonly stage: FilterContext['stage'];
|
|
11
|
+
apply(input: JsonObject, ctx: FilterContext): FilterResult<JsonObject>;
|
|
12
|
+
private shouldPreserveStream;
|
|
13
|
+
}
|
|
@@ -7,9 +7,12 @@
|
|
|
7
7
|
export class RequestStreamingToNonStreamingFilter {
|
|
8
8
|
name = 'request_streaming_to_nonstreaming';
|
|
9
9
|
stage = 'request_post';
|
|
10
|
-
apply(input) {
|
|
10
|
+
apply(input, ctx) {
|
|
11
11
|
try {
|
|
12
12
|
const out = JSON.parse(JSON.stringify(input || {}));
|
|
13
|
+
if (this.shouldPreserveStream(out, ctx)) {
|
|
14
|
+
return { ok: true, data: out };
|
|
15
|
+
}
|
|
13
16
|
if (out.stream === true) {
|
|
14
17
|
out.originalStream = true;
|
|
15
18
|
out.stream = false;
|
|
@@ -24,4 +27,13 @@ export class RequestStreamingToNonStreamingFilter {
|
|
|
24
27
|
return { ok: true, data: input };
|
|
25
28
|
}
|
|
26
29
|
}
|
|
30
|
+
shouldPreserveStream(payload, ctx) {
|
|
31
|
+
if (ctx.stream === true) {
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
if (payload.stream === true) {
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
27
39
|
}
|