@jsonstudio/llms 0.6.3275 → 0.6.3379
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/bridge-message-utils.d.ts +4 -4
- package/dist/conversion/bridge-message-utils.js +28 -538
- package/dist/conversion/compat/profiles/responses-crs.json +15 -0
- package/dist/conversion/hub/operation-table/semantic-mappers/chat-mapper.js +16 -5
- package/dist/conversion/hub/pipeline/stages/resp_process/resp_process_stage1_tool_governance/index.js +1 -6
- package/dist/conversion/hub/response/response-runtime.js +14 -6
- package/dist/conversion/responses/responses-openai-bridge/response-payload.js +11 -11
- package/dist/conversion/shared/anthropic-message-utils.js +2 -12
- package/dist/conversion/shared/chat-request-filters.js +2 -61
- package/dist/conversion/shared/reasoning-mapping.js +3 -0
- package/dist/conversion/shared/reasoning-normalizer.d.ts +1 -0
- package/dist/conversion/shared/reasoning-normalizer.js +35 -388
- package/dist/conversion/shared/reasoning-tool-normalizer.js +8 -15
- package/dist/conversion/shared/reasoning-utils.js +13 -35
- package/dist/conversion/shared/responses-tool-utils.d.ts +1 -1
- package/dist/conversion/shared/responses-tool-utils.js +63 -65
- package/dist/conversion/shared/streaming-text-extractor.d.ts +0 -5
- package/dist/conversion/shared/streaming-text-extractor.js +18 -111
- package/dist/conversion/shared/text-markup-normalizer/normalize.d.ts +1 -1
- package/dist/conversion/shared/text-markup-normalizer/normalize.js +3 -91
- package/dist/conversion/shared/thought-signature-validator.js +19 -133
- package/dist/conversion/shared/tool-argument-repairer.js +16 -19
- package/dist/conversion/shared/tool-call-id-manager.d.ts +1 -5
- package/dist/conversion/shared/tool-call-id-manager.js +74 -98
- package/dist/conversion/shared/tool-harvester.js +19 -382
- package/dist/conversion/shared/tool-mapping.d.ts +2 -3
- package/dist/conversion/shared/tool-mapping.js +28 -184
- package/dist/conversion/shared/tooling.js +20 -151
- package/dist/filters/special/response-tool-arguments-stringify.js +9 -1
- package/dist/guidance/index.js +2 -2
- package/dist/native/router_hotpath_napi.node +0 -0
- package/dist/router/virtual-router/engine-legacy/helpers.js +1 -1
- package/dist/router/virtual-router/engine-selection/native-hub-bridge-action-semantics.d.ts +39 -0
- package/dist/router/virtual-router/engine-selection/native-hub-bridge-action-semantics.js +196 -0
- package/dist/router/virtual-router/engine-selection/native-hub-pipeline-req-inbound-semantics.d.ts +1 -0
- package/dist/router/virtual-router/engine-selection/native-hub-pipeline-req-inbound-semantics.js +27 -0
- package/dist/router/virtual-router/engine-selection/native-router-hotpath-loader.js +34 -0
- package/dist/router/virtual-router/engine-selection/native-shared-conversion-semantics.d.ts +65 -1
- package/dist/router/virtual-router/engine-selection/native-shared-conversion-semantics.js +836 -35
- package/dist/router/virtual-router/engine.js +3 -2
- package/dist/router/virtual-router/routing-instructions/parse.js +30 -3
- package/dist/sse/sse-to-json/builders/anthropic-response-builder.js +28 -3
- package/dist/sse/types/anthropic-types.d.ts +3 -1
- package/dist/tools/apply-patch/args-normalizer/extract-patch.js +2 -2
- package/dist/tools/apply-patch/patch-text/looks-like-patch.js +3 -6
- package/dist/tools/apply-patch/patch-text/normalize.js +14 -3
- package/dist/tools/tool-registry.js +12 -0
- package/package.json +6 -1
|
@@ -61,6 +61,29 @@ function parseReasoningItems(raw) {
|
|
|
61
61
|
}
|
|
62
62
|
return out;
|
|
63
63
|
}
|
|
64
|
+
function parseExtractReasoningSegmentsOutput(raw) {
|
|
65
|
+
const parsed = parseJson(raw);
|
|
66
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
const row = parsed;
|
|
70
|
+
if (typeof row.text !== 'string' || !Array.isArray(row.segments)) {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
const segments = row.segments.filter((entry) => typeof entry === 'string');
|
|
74
|
+
if (segments.length !== row.segments.length) {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
return { text: row.text, segments };
|
|
78
|
+
}
|
|
79
|
+
function parseNormalizeReasoningOutput(raw) {
|
|
80
|
+
const parsed = parseJson(raw);
|
|
81
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
const row = parsed;
|
|
85
|
+
return { payload: row.payload };
|
|
86
|
+
}
|
|
64
87
|
function parseToolCallLiteArray(raw) {
|
|
65
88
|
const parsed = parseJson(raw);
|
|
66
89
|
if (!Array.isArray(parsed)) {
|
|
@@ -84,6 +107,35 @@ function parseArray(raw) {
|
|
|
84
107
|
const parsed = parseJson(raw);
|
|
85
108
|
return Array.isArray(parsed) ? parsed : null;
|
|
86
109
|
}
|
|
110
|
+
function parseToolDefinitionOutput(raw) {
|
|
111
|
+
const parsed = parseRecord(raw);
|
|
112
|
+
return parsed;
|
|
113
|
+
}
|
|
114
|
+
function parseToolDefinitionArray(raw) {
|
|
115
|
+
const parsed = parseArray(raw);
|
|
116
|
+
if (!parsed)
|
|
117
|
+
return null;
|
|
118
|
+
const output = [];
|
|
119
|
+
for (const entry of parsed) {
|
|
120
|
+
if (entry && typeof entry === 'object' && !Array.isArray(entry)) {
|
|
121
|
+
output.push(entry);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return output;
|
|
125
|
+
}
|
|
126
|
+
function parseStringArray(raw) {
|
|
127
|
+
const parsed = parseArray(raw);
|
|
128
|
+
if (!parsed)
|
|
129
|
+
return null;
|
|
130
|
+
const out = [];
|
|
131
|
+
for (const item of parsed) {
|
|
132
|
+
if (typeof item !== "string") {
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
out.push(item);
|
|
136
|
+
}
|
|
137
|
+
return out;
|
|
138
|
+
}
|
|
87
139
|
export function parseLenientJsonishWithNative(value) {
|
|
88
140
|
const capability = 'parseLenientJsonishJson';
|
|
89
141
|
const fail = (reason) => failNativeRequired(capability, reason);
|
|
@@ -94,25 +146,529 @@ export function parseLenientJsonishWithNative(value) {
|
|
|
94
146
|
if (!fn) {
|
|
95
147
|
return fail();
|
|
96
148
|
}
|
|
97
|
-
const valueJson = safeStringify(value ?? null);
|
|
98
|
-
if (!valueJson) {
|
|
149
|
+
const valueJson = safeStringify(value ?? null);
|
|
150
|
+
if (!valueJson) {
|
|
151
|
+
return fail('json stringify failed');
|
|
152
|
+
}
|
|
153
|
+
try {
|
|
154
|
+
const raw = fn(valueJson);
|
|
155
|
+
if (typeof raw !== 'string' || !raw) {
|
|
156
|
+
return fail('empty result');
|
|
157
|
+
}
|
|
158
|
+
const parsed = parseJson(raw);
|
|
159
|
+
return parsed === null ? fail('invalid payload') : parsed;
|
|
160
|
+
}
|
|
161
|
+
catch (error) {
|
|
162
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
163
|
+
return fail(reason);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
export function repairArgumentsToStringWithNative(value) {
|
|
167
|
+
const capability = 'repairArgumentsToStringJsonishJson';
|
|
168
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
169
|
+
if (isNativeDisabledByEnv()) {
|
|
170
|
+
return fail('native disabled');
|
|
171
|
+
}
|
|
172
|
+
const fn = readNativeFunction(capability);
|
|
173
|
+
if (!fn) {
|
|
174
|
+
return fail();
|
|
175
|
+
}
|
|
176
|
+
const valueJson = safeStringify(value ?? null);
|
|
177
|
+
if (!valueJson) {
|
|
178
|
+
return fail('json stringify failed');
|
|
179
|
+
}
|
|
180
|
+
try {
|
|
181
|
+
const raw = fn(valueJson);
|
|
182
|
+
if (typeof raw !== 'string') {
|
|
183
|
+
return fail('invalid payload');
|
|
184
|
+
}
|
|
185
|
+
return raw;
|
|
186
|
+
}
|
|
187
|
+
catch (error) {
|
|
188
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
189
|
+
return fail(reason);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
export function extractToolCallsFromReasoningTextWithNative(text, idPrefix) {
|
|
193
|
+
const capability = 'extractToolCallsFromReasoningTextJson';
|
|
194
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
195
|
+
if (isNativeDisabledByEnv()) {
|
|
196
|
+
return fail('native disabled');
|
|
197
|
+
}
|
|
198
|
+
const fn = readNativeFunction(capability);
|
|
199
|
+
if (!fn) {
|
|
200
|
+
return fail();
|
|
201
|
+
}
|
|
202
|
+
try {
|
|
203
|
+
const raw = fn(String(text ?? ''), idPrefix);
|
|
204
|
+
if (typeof raw !== 'string' || !raw) {
|
|
205
|
+
return fail('empty result');
|
|
206
|
+
}
|
|
207
|
+
const parsed = parseExtractToolCallsOutput(raw);
|
|
208
|
+
return parsed ?? fail('invalid payload');
|
|
209
|
+
}
|
|
210
|
+
catch (error) {
|
|
211
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
212
|
+
return fail(reason);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
export function extractReasoningSegmentsWithNative(source) {
|
|
216
|
+
const capability = 'extractReasoningSegmentsJson';
|
|
217
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
218
|
+
if (isNativeDisabledByEnv()) {
|
|
219
|
+
return fail('native disabled');
|
|
220
|
+
}
|
|
221
|
+
const fn = readNativeFunction(capability);
|
|
222
|
+
if (!fn) {
|
|
223
|
+
return fail();
|
|
224
|
+
}
|
|
225
|
+
const payloadJson = safeStringify({ source: String(source ?? '') });
|
|
226
|
+
if (!payloadJson) {
|
|
227
|
+
return fail('json stringify failed');
|
|
228
|
+
}
|
|
229
|
+
try {
|
|
230
|
+
const raw = fn(payloadJson);
|
|
231
|
+
if (typeof raw !== 'string' || !raw) {
|
|
232
|
+
return fail('empty result');
|
|
233
|
+
}
|
|
234
|
+
const parsed = parseExtractReasoningSegmentsOutput(raw);
|
|
235
|
+
return parsed ?? fail('invalid payload');
|
|
236
|
+
}
|
|
237
|
+
catch (error) {
|
|
238
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
239
|
+
return fail(reason);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
export function normalizeAssistantTextToToolCallsWithNative(message, options) {
|
|
243
|
+
const capability = 'normalizeAssistantTextToToolCallsJson';
|
|
244
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
245
|
+
if (isNativeDisabledByEnv()) {
|
|
246
|
+
return fail('native disabled');
|
|
247
|
+
}
|
|
248
|
+
const fn = readNativeFunction(capability);
|
|
249
|
+
if (!fn) {
|
|
250
|
+
return fail();
|
|
251
|
+
}
|
|
252
|
+
const msgJson = safeStringify(message ?? null);
|
|
253
|
+
if (!msgJson) {
|
|
254
|
+
return fail('json stringify failed');
|
|
255
|
+
}
|
|
256
|
+
const optionsJson = options ? safeStringify(options) : undefined;
|
|
257
|
+
try {
|
|
258
|
+
const raw = fn(msgJson, optionsJson);
|
|
259
|
+
if (typeof raw !== 'string' || !raw) {
|
|
260
|
+
return fail('empty result');
|
|
261
|
+
}
|
|
262
|
+
const parsed = parseRecord(raw);
|
|
263
|
+
return parsed ?? fail('invalid payload');
|
|
264
|
+
}
|
|
265
|
+
catch (error) {
|
|
266
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
267
|
+
return fail(reason);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
export function normalizeReasoningInChatPayloadWithNative(payload) {
|
|
271
|
+
const capability = 'normalizeReasoningInChatPayloadJson';
|
|
272
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
273
|
+
if (isNativeDisabledByEnv()) {
|
|
274
|
+
return fail('native disabled');
|
|
275
|
+
}
|
|
276
|
+
const fn = readNativeFunction(capability);
|
|
277
|
+
if (!fn) {
|
|
278
|
+
return fail();
|
|
279
|
+
}
|
|
280
|
+
const payloadJson = safeStringify({ payload: payload ?? null });
|
|
281
|
+
if (!payloadJson) {
|
|
282
|
+
return fail('json stringify failed');
|
|
283
|
+
}
|
|
284
|
+
try {
|
|
285
|
+
const raw = fn(payloadJson);
|
|
286
|
+
if (typeof raw !== 'string' || !raw) {
|
|
287
|
+
return fail('empty result');
|
|
288
|
+
}
|
|
289
|
+
const parsed = parseNormalizeReasoningOutput(raw);
|
|
290
|
+
return parsed ? parsed.payload : fail('invalid payload');
|
|
291
|
+
}
|
|
292
|
+
catch (error) {
|
|
293
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
294
|
+
return fail(reason);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
export function normalizeReasoningInResponsesPayloadWithNative(payload, options) {
|
|
298
|
+
const capability = 'normalizeReasoningInResponsesPayloadJson';
|
|
299
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
300
|
+
if (isNativeDisabledByEnv()) {
|
|
301
|
+
return fail('native disabled');
|
|
302
|
+
}
|
|
303
|
+
const fn = readNativeFunction(capability);
|
|
304
|
+
if (!fn) {
|
|
305
|
+
return fail();
|
|
306
|
+
}
|
|
307
|
+
const payloadJson = safeStringify({ payload: payload ?? null, options: options ?? null });
|
|
308
|
+
if (!payloadJson) {
|
|
309
|
+
return fail('json stringify failed');
|
|
310
|
+
}
|
|
311
|
+
try {
|
|
312
|
+
const raw = fn(payloadJson);
|
|
313
|
+
if (typeof raw !== 'string' || !raw) {
|
|
314
|
+
return fail('empty result');
|
|
315
|
+
}
|
|
316
|
+
const parsed = parseNormalizeReasoningOutput(raw);
|
|
317
|
+
return parsed ? parsed.payload : fail('invalid payload');
|
|
318
|
+
}
|
|
319
|
+
catch (error) {
|
|
320
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
321
|
+
return fail(reason);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
export function normalizeReasoningInGeminiPayloadWithNative(payload) {
|
|
325
|
+
const capability = 'normalizeReasoningInGeminiPayloadJson';
|
|
326
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
327
|
+
if (isNativeDisabledByEnv()) {
|
|
328
|
+
return fail('native disabled');
|
|
329
|
+
}
|
|
330
|
+
const fn = readNativeFunction(capability);
|
|
331
|
+
if (!fn) {
|
|
332
|
+
return fail();
|
|
333
|
+
}
|
|
334
|
+
const payloadJson = safeStringify({ payload: payload ?? null });
|
|
335
|
+
if (!payloadJson) {
|
|
336
|
+
return fail('json stringify failed');
|
|
337
|
+
}
|
|
338
|
+
try {
|
|
339
|
+
const raw = fn(payloadJson);
|
|
340
|
+
if (typeof raw !== 'string' || !raw) {
|
|
341
|
+
return fail('empty result');
|
|
342
|
+
}
|
|
343
|
+
const parsed = parseNormalizeReasoningOutput(raw);
|
|
344
|
+
return parsed ? parsed.payload : fail('invalid payload');
|
|
345
|
+
}
|
|
346
|
+
catch (error) {
|
|
347
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
348
|
+
return fail(reason);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
export function normalizeReasoningInAnthropicPayloadWithNative(payload) {
|
|
352
|
+
const capability = 'normalizeReasoningInAnthropicPayloadJson';
|
|
353
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
354
|
+
if (isNativeDisabledByEnv()) {
|
|
355
|
+
return fail('native disabled');
|
|
356
|
+
}
|
|
357
|
+
const fn = readNativeFunction(capability);
|
|
358
|
+
if (!fn) {
|
|
359
|
+
return fail();
|
|
360
|
+
}
|
|
361
|
+
const payloadJson = safeStringify({ payload: payload ?? null });
|
|
362
|
+
if (!payloadJson) {
|
|
363
|
+
return fail('json stringify failed');
|
|
364
|
+
}
|
|
365
|
+
try {
|
|
366
|
+
const raw = fn(payloadJson);
|
|
367
|
+
if (typeof raw !== 'string' || !raw) {
|
|
368
|
+
return fail('empty result');
|
|
369
|
+
}
|
|
370
|
+
const parsed = parseNormalizeReasoningOutput(raw);
|
|
371
|
+
return parsed ? parsed.payload : fail('invalid payload');
|
|
372
|
+
}
|
|
373
|
+
catch (error) {
|
|
374
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
375
|
+
return fail(reason);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
export function normalizeReasoningInOpenAIPayloadWithNative(payload) {
|
|
379
|
+
const capability = 'normalizeReasoningInOpenAIPayloadJson';
|
|
380
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
381
|
+
if (isNativeDisabledByEnv()) {
|
|
382
|
+
return fail('native disabled');
|
|
383
|
+
}
|
|
384
|
+
const fn = readNativeFunction(capability);
|
|
385
|
+
if (!fn) {
|
|
386
|
+
return fail();
|
|
387
|
+
}
|
|
388
|
+
const payloadJson = safeStringify({ payload: payload ?? null });
|
|
389
|
+
if (!payloadJson) {
|
|
390
|
+
return fail('json stringify failed');
|
|
391
|
+
}
|
|
392
|
+
try {
|
|
393
|
+
const raw = fn(payloadJson);
|
|
394
|
+
if (typeof raw !== 'string' || !raw) {
|
|
395
|
+
return fail('empty result');
|
|
396
|
+
}
|
|
397
|
+
const parsed = parseNormalizeReasoningOutput(raw);
|
|
398
|
+
return parsed ? parsed.payload : fail('invalid payload');
|
|
399
|
+
}
|
|
400
|
+
catch (error) {
|
|
401
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
402
|
+
return fail(reason);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
export function bridgeToolToChatDefinitionWithNative(tool, options) {
|
|
406
|
+
const capability = 'bridgeToolToChatDefinitionJson';
|
|
407
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
408
|
+
if (isNativeDisabledByEnv()) {
|
|
409
|
+
return fail('native disabled');
|
|
410
|
+
}
|
|
411
|
+
const fn = readNativeFunction(capability);
|
|
412
|
+
if (!fn) {
|
|
413
|
+
return fail();
|
|
414
|
+
}
|
|
415
|
+
const payloadJson = safeStringify({ tool, options: options ?? null });
|
|
416
|
+
if (!payloadJson) {
|
|
417
|
+
return fail('json stringify failed');
|
|
418
|
+
}
|
|
419
|
+
try {
|
|
420
|
+
const raw = fn(payloadJson);
|
|
421
|
+
if (typeof raw !== 'string' || !raw) {
|
|
422
|
+
return fail('empty result');
|
|
423
|
+
}
|
|
424
|
+
const parsed = parseToolDefinitionOutput(raw);
|
|
425
|
+
return parsed ?? fail('invalid payload');
|
|
426
|
+
}
|
|
427
|
+
catch (error) {
|
|
428
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
429
|
+
return fail(reason);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
export function chatToolToBridgeDefinitionWithNative(tool, options) {
|
|
433
|
+
const capability = 'chatToolToBridgeDefinitionJson';
|
|
434
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
435
|
+
if (isNativeDisabledByEnv()) {
|
|
436
|
+
return fail('native disabled');
|
|
437
|
+
}
|
|
438
|
+
const fn = readNativeFunction(capability);
|
|
439
|
+
if (!fn) {
|
|
440
|
+
return fail();
|
|
441
|
+
}
|
|
442
|
+
const payloadJson = safeStringify({ tool, options: options ?? null });
|
|
443
|
+
if (!payloadJson) {
|
|
444
|
+
return fail('json stringify failed');
|
|
445
|
+
}
|
|
446
|
+
try {
|
|
447
|
+
const raw = fn(payloadJson);
|
|
448
|
+
if (typeof raw !== 'string' || !raw) {
|
|
449
|
+
return fail('empty result');
|
|
450
|
+
}
|
|
451
|
+
const parsed = parseToolDefinitionOutput(raw);
|
|
452
|
+
return parsed ?? fail('invalid payload');
|
|
453
|
+
}
|
|
454
|
+
catch (error) {
|
|
455
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
456
|
+
return fail(reason);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
export function mapBridgeToolsToChatWithNative(rawTools, options) {
|
|
460
|
+
const capability = 'mapBridgeToolsToChatWithOptionsJson';
|
|
461
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
462
|
+
if (isNativeDisabledByEnv()) {
|
|
463
|
+
return fail('native disabled');
|
|
464
|
+
}
|
|
465
|
+
const fn = readNativeFunction(capability);
|
|
466
|
+
if (!fn) {
|
|
467
|
+
return fail();
|
|
468
|
+
}
|
|
469
|
+
const payloadJson = safeStringify({ tools: Array.isArray(rawTools) ? rawTools : [], options: options ?? null });
|
|
470
|
+
if (!payloadJson) {
|
|
471
|
+
return fail('json stringify failed');
|
|
472
|
+
}
|
|
473
|
+
try {
|
|
474
|
+
const raw = fn(payloadJson);
|
|
475
|
+
if (typeof raw !== 'string' || !raw) {
|
|
476
|
+
return fail('empty result');
|
|
477
|
+
}
|
|
478
|
+
const parsed = parseToolDefinitionArray(raw);
|
|
479
|
+
return parsed ?? fail('invalid payload');
|
|
480
|
+
}
|
|
481
|
+
catch (error) {
|
|
482
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
483
|
+
return fail(reason);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
export function mapChatToolsToBridgeWithNative(rawTools, options) {
|
|
487
|
+
const capability = 'mapChatToolsToBridgeWithOptionsJson';
|
|
488
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
489
|
+
if (isNativeDisabledByEnv()) {
|
|
490
|
+
return fail('native disabled');
|
|
491
|
+
}
|
|
492
|
+
const fn = readNativeFunction(capability);
|
|
493
|
+
if (!fn) {
|
|
494
|
+
return fail();
|
|
495
|
+
}
|
|
496
|
+
const payloadJson = safeStringify({ tools: Array.isArray(rawTools) ? rawTools : [], options: options ?? null });
|
|
497
|
+
if (!payloadJson) {
|
|
498
|
+
return fail('json stringify failed');
|
|
499
|
+
}
|
|
500
|
+
try {
|
|
501
|
+
const raw = fn(payloadJson);
|
|
502
|
+
if (typeof raw !== 'string' || !raw) {
|
|
503
|
+
return fail('empty result');
|
|
504
|
+
}
|
|
505
|
+
const parsed = parseToolDefinitionArray(raw);
|
|
506
|
+
return parsed ?? fail('invalid payload');
|
|
507
|
+
}
|
|
508
|
+
catch (error) {
|
|
509
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
510
|
+
return fail(reason);
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
export function hasValidThoughtSignatureWithNative(block, options) {
|
|
514
|
+
const capability = 'hasValidThoughtSignatureJson';
|
|
515
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
516
|
+
if (isNativeDisabledByEnv()) {
|
|
517
|
+
return fail('native disabled');
|
|
518
|
+
}
|
|
519
|
+
const fn = readNativeFunction(capability);
|
|
520
|
+
if (!fn) {
|
|
521
|
+
return fail();
|
|
522
|
+
}
|
|
523
|
+
const payloadJson = safeStringify({ block: block ?? null, options: options ?? null });
|
|
524
|
+
if (!payloadJson) {
|
|
525
|
+
return fail('json stringify failed');
|
|
526
|
+
}
|
|
527
|
+
try {
|
|
528
|
+
const raw = fn(payloadJson);
|
|
529
|
+
if (typeof raw !== 'string' || !raw) {
|
|
530
|
+
return fail('empty result');
|
|
531
|
+
}
|
|
532
|
+
const parsed = parseJson(raw);
|
|
533
|
+
return typeof parsed === 'boolean' ? parsed : fail('invalid payload');
|
|
534
|
+
}
|
|
535
|
+
catch (error) {
|
|
536
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
537
|
+
return fail(reason);
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
export function sanitizeThinkingBlockWithNative(block) {
|
|
541
|
+
const capability = 'sanitizeThinkingBlockJson';
|
|
542
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
543
|
+
if (isNativeDisabledByEnv()) {
|
|
544
|
+
return fail('native disabled');
|
|
545
|
+
}
|
|
546
|
+
const fn = readNativeFunction(capability);
|
|
547
|
+
if (!fn) {
|
|
548
|
+
return fail();
|
|
549
|
+
}
|
|
550
|
+
const payloadJson = safeStringify({ block: block ?? null });
|
|
551
|
+
if (!payloadJson) {
|
|
552
|
+
return fail('json stringify failed');
|
|
553
|
+
}
|
|
554
|
+
try {
|
|
555
|
+
const raw = fn(payloadJson);
|
|
556
|
+
if (typeof raw !== 'string' || !raw) {
|
|
557
|
+
return fail('empty result');
|
|
558
|
+
}
|
|
559
|
+
const parsed = parseRecord(raw);
|
|
560
|
+
return parsed ?? fail('invalid payload');
|
|
561
|
+
}
|
|
562
|
+
catch (error) {
|
|
563
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
564
|
+
return fail(reason);
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
export function filterInvalidThinkingBlocksWithNative(messages, options) {
|
|
568
|
+
const capability = 'filterInvalidThinkingBlocksJson';
|
|
569
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
570
|
+
if (isNativeDisabledByEnv()) {
|
|
571
|
+
return fail('native disabled');
|
|
572
|
+
}
|
|
573
|
+
const fn = readNativeFunction(capability);
|
|
574
|
+
if (!fn) {
|
|
575
|
+
return fail();
|
|
576
|
+
}
|
|
577
|
+
const payloadJson = safeStringify({ messages: Array.isArray(messages) ? messages : [], options: options ?? null });
|
|
578
|
+
if (!payloadJson) {
|
|
579
|
+
return fail('json stringify failed');
|
|
580
|
+
}
|
|
581
|
+
try {
|
|
582
|
+
const raw = fn(payloadJson);
|
|
583
|
+
if (typeof raw !== 'string' || !raw) {
|
|
584
|
+
return fail('empty result');
|
|
585
|
+
}
|
|
586
|
+
const parsed = parseArray(raw);
|
|
587
|
+
return parsed ?? fail('invalid payload');
|
|
588
|
+
}
|
|
589
|
+
catch (error) {
|
|
590
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
591
|
+
return fail(reason);
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
export function removeTrailingUnsignedThinkingBlocksWithNative(blocks, options) {
|
|
595
|
+
const capability = 'removeTrailingUnsignedThinkingBlocksJson';
|
|
596
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
597
|
+
if (isNativeDisabledByEnv()) {
|
|
598
|
+
return fail('native disabled');
|
|
599
|
+
}
|
|
600
|
+
const fn = readNativeFunction(capability);
|
|
601
|
+
if (!fn) {
|
|
602
|
+
return fail();
|
|
603
|
+
}
|
|
604
|
+
const payloadJson = safeStringify({ blocks: Array.isArray(blocks) ? blocks : [], options: options ?? null });
|
|
605
|
+
if (!payloadJson) {
|
|
606
|
+
return fail('json stringify failed');
|
|
607
|
+
}
|
|
608
|
+
try {
|
|
609
|
+
const raw = fn(payloadJson);
|
|
610
|
+
if (typeof raw !== 'string' || !raw) {
|
|
611
|
+
return fail('empty result');
|
|
612
|
+
}
|
|
613
|
+
const parsed = parseArray(raw);
|
|
614
|
+
return parsed ?? fail('invalid payload');
|
|
615
|
+
}
|
|
616
|
+
catch (error) {
|
|
617
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
618
|
+
return fail(reason);
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
export function sanitizeReasoningTaggedTextWithNative(text) {
|
|
622
|
+
const capability = 'sanitizeReasoningTaggedTextJson';
|
|
623
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
624
|
+
if (isNativeDisabledByEnv()) {
|
|
625
|
+
return fail('native disabled');
|
|
626
|
+
}
|
|
627
|
+
const fn = readNativeFunction(capability);
|
|
628
|
+
if (!fn) {
|
|
629
|
+
return fail();
|
|
630
|
+
}
|
|
631
|
+
try {
|
|
632
|
+
const raw = fn(String(text ?? ''));
|
|
633
|
+
if (typeof raw !== 'string') {
|
|
634
|
+
return fail('invalid payload');
|
|
635
|
+
}
|
|
636
|
+
return raw;
|
|
637
|
+
}
|
|
638
|
+
catch (error) {
|
|
639
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
640
|
+
return fail(reason);
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
export function ensureBridgeInstructionsWithNative(payload) {
|
|
644
|
+
const capability = 'ensureBridgeInstructionsJson';
|
|
645
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
646
|
+
if (isNativeDisabledByEnv()) {
|
|
647
|
+
return fail('native disabled');
|
|
648
|
+
}
|
|
649
|
+
const fn = readNativeFunction(capability);
|
|
650
|
+
if (!fn) {
|
|
651
|
+
return fail();
|
|
652
|
+
}
|
|
653
|
+
const payloadJson = safeStringify(payload ?? {});
|
|
654
|
+
if (!payloadJson) {
|
|
99
655
|
return fail('json stringify failed');
|
|
100
656
|
}
|
|
101
657
|
try {
|
|
102
|
-
const raw = fn(
|
|
658
|
+
const raw = fn(payloadJson);
|
|
103
659
|
if (typeof raw !== 'string' || !raw) {
|
|
104
660
|
return fail('empty result');
|
|
105
661
|
}
|
|
106
|
-
const parsed =
|
|
107
|
-
return parsed
|
|
662
|
+
const parsed = parseRecord(raw);
|
|
663
|
+
return parsed ?? fail('invalid payload');
|
|
108
664
|
}
|
|
109
665
|
catch (error) {
|
|
110
666
|
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
111
667
|
return fail(reason);
|
|
112
668
|
}
|
|
113
669
|
}
|
|
114
|
-
export function
|
|
115
|
-
const capability = '
|
|
670
|
+
export function repairFindMetaWithNative(script) {
|
|
671
|
+
const capability = 'repairFindMetaJson';
|
|
116
672
|
const fail = (reason) => failNativeRequired(capability, reason);
|
|
117
673
|
if (isNativeDisabledByEnv()) {
|
|
118
674
|
return fail('native disabled');
|
|
@@ -121,24 +677,25 @@ export function repairArgumentsToStringWithNative(value) {
|
|
|
121
677
|
if (!fn) {
|
|
122
678
|
return fail();
|
|
123
679
|
}
|
|
124
|
-
const
|
|
125
|
-
if (!
|
|
680
|
+
const payloadJson = safeStringify(script ?? '');
|
|
681
|
+
if (!payloadJson) {
|
|
126
682
|
return fail('json stringify failed');
|
|
127
683
|
}
|
|
128
684
|
try {
|
|
129
|
-
const raw = fn(
|
|
130
|
-
if (typeof raw !== 'string') {
|
|
131
|
-
return fail('
|
|
685
|
+
const raw = fn(payloadJson);
|
|
686
|
+
if (typeof raw !== 'string' || !raw) {
|
|
687
|
+
return fail('empty result');
|
|
132
688
|
}
|
|
133
|
-
|
|
689
|
+
const parsed = parseJson(raw);
|
|
690
|
+
return typeof parsed === 'string' ? parsed : fail('invalid payload');
|
|
134
691
|
}
|
|
135
692
|
catch (error) {
|
|
136
693
|
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
137
694
|
return fail(reason);
|
|
138
695
|
}
|
|
139
696
|
}
|
|
140
|
-
export function
|
|
141
|
-
const capability = '
|
|
697
|
+
export function splitCommandStringWithNative(input) {
|
|
698
|
+
const capability = 'splitCommandStringJson';
|
|
142
699
|
const fail = (reason) => failNativeRequired(capability, reason);
|
|
143
700
|
if (isNativeDisabledByEnv()) {
|
|
144
701
|
return fail('native disabled');
|
|
@@ -147,12 +704,16 @@ export function extractToolCallsFromReasoningTextWithNative(text, idPrefix) {
|
|
|
147
704
|
if (!fn) {
|
|
148
705
|
return fail();
|
|
149
706
|
}
|
|
707
|
+
const payloadJson = safeStringify(input ?? '');
|
|
708
|
+
if (!payloadJson) {
|
|
709
|
+
return fail('json stringify failed');
|
|
710
|
+
}
|
|
150
711
|
try {
|
|
151
|
-
const raw = fn(
|
|
712
|
+
const raw = fn(payloadJson);
|
|
152
713
|
if (typeof raw !== 'string' || !raw) {
|
|
153
714
|
return fail('empty result');
|
|
154
715
|
}
|
|
155
|
-
const parsed =
|
|
716
|
+
const parsed = parseStringArray(raw);
|
|
156
717
|
return parsed ?? fail('invalid payload');
|
|
157
718
|
}
|
|
158
719
|
catch (error) {
|
|
@@ -160,8 +721,8 @@ export function extractToolCallsFromReasoningTextWithNative(text, idPrefix) {
|
|
|
160
721
|
return fail(reason);
|
|
161
722
|
}
|
|
162
723
|
}
|
|
163
|
-
export function
|
|
164
|
-
const capability = '
|
|
724
|
+
export function packShellArgsWithNative(input) {
|
|
725
|
+
const capability = 'packShellArgsJson';
|
|
165
726
|
const fail = (reason) => failNativeRequired(capability, reason);
|
|
166
727
|
if (isNativeDisabledByEnv()) {
|
|
167
728
|
return fail('native disabled');
|
|
@@ -170,7 +731,7 @@ export function mapChatToolsToBridgeWithNative(rawTools) {
|
|
|
170
731
|
if (!fn) {
|
|
171
732
|
return fail();
|
|
172
733
|
}
|
|
173
|
-
const payloadJson = safeStringify(
|
|
734
|
+
const payloadJson = safeStringify(input ?? {});
|
|
174
735
|
if (!payloadJson) {
|
|
175
736
|
return fail('json stringify failed');
|
|
176
737
|
}
|
|
@@ -179,19 +740,16 @@ export function mapChatToolsToBridgeWithNative(rawTools) {
|
|
|
179
740
|
if (typeof raw !== 'string' || !raw) {
|
|
180
741
|
return fail('empty result');
|
|
181
742
|
}
|
|
182
|
-
const parsed =
|
|
183
|
-
|
|
184
|
-
return fail('invalid payload');
|
|
185
|
-
}
|
|
186
|
-
return parsed.filter((entry) => !!entry && typeof entry === 'object' && !Array.isArray(entry));
|
|
743
|
+
const parsed = parseRecord(raw);
|
|
744
|
+
return parsed ?? fail('invalid payload');
|
|
187
745
|
}
|
|
188
746
|
catch (error) {
|
|
189
747
|
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
190
748
|
return fail(reason);
|
|
191
749
|
}
|
|
192
750
|
}
|
|
193
|
-
export function
|
|
194
|
-
const capability = '
|
|
751
|
+
export function flattenByCommaWithNative(items) {
|
|
752
|
+
const capability = 'flattenByCommaJson';
|
|
195
753
|
const fail = (reason) => failNativeRequired(capability, reason);
|
|
196
754
|
if (isNativeDisabledByEnv()) {
|
|
197
755
|
return fail('native disabled');
|
|
@@ -200,20 +758,25 @@ export function sanitizeReasoningTaggedTextWithNative(text) {
|
|
|
200
758
|
if (!fn) {
|
|
201
759
|
return fail();
|
|
202
760
|
}
|
|
761
|
+
const payloadJson = safeStringify(Array.isArray(items) ? items : []);
|
|
762
|
+
if (!payloadJson) {
|
|
763
|
+
return fail('json stringify failed');
|
|
764
|
+
}
|
|
203
765
|
try {
|
|
204
|
-
const raw = fn(
|
|
205
|
-
if (typeof raw !== 'string') {
|
|
206
|
-
return fail('
|
|
766
|
+
const raw = fn(payloadJson);
|
|
767
|
+
if (typeof raw !== 'string' || !raw) {
|
|
768
|
+
return fail('empty result');
|
|
207
769
|
}
|
|
208
|
-
|
|
770
|
+
const parsed = parseStringArray(raw);
|
|
771
|
+
return parsed ?? fail('invalid payload');
|
|
209
772
|
}
|
|
210
773
|
catch (error) {
|
|
211
774
|
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
212
775
|
return fail(reason);
|
|
213
776
|
}
|
|
214
777
|
}
|
|
215
|
-
export function
|
|
216
|
-
const capability = '
|
|
778
|
+
export function chunkStringWithNative(s, minParts = 3, maxParts = 12, targetChunk = 12) {
|
|
779
|
+
const capability = 'chunkStringJson';
|
|
217
780
|
const fail = (reason) => failNativeRequired(capability, reason);
|
|
218
781
|
if (isNativeDisabledByEnv()) {
|
|
219
782
|
return fail('native disabled');
|
|
@@ -222,7 +785,7 @@ export function ensureBridgeInstructionsWithNative(payload) {
|
|
|
222
785
|
if (!fn) {
|
|
223
786
|
return fail();
|
|
224
787
|
}
|
|
225
|
-
const payloadJson = safeStringify(
|
|
788
|
+
const payloadJson = safeStringify({ s, minParts, maxParts, targetChunk });
|
|
226
789
|
if (!payloadJson) {
|
|
227
790
|
return fail('json stringify failed');
|
|
228
791
|
}
|
|
@@ -231,7 +794,7 @@ export function ensureBridgeInstructionsWithNative(payload) {
|
|
|
231
794
|
if (typeof raw !== 'string' || !raw) {
|
|
232
795
|
return fail('empty result');
|
|
233
796
|
}
|
|
234
|
-
const parsed =
|
|
797
|
+
const parsed = parseStringArray(raw);
|
|
235
798
|
return parsed ?? fail('invalid payload');
|
|
236
799
|
}
|
|
237
800
|
catch (error) {
|
|
@@ -269,6 +832,147 @@ export function deriveToolCallKeyWithNative(call) {
|
|
|
269
832
|
return fail(reason);
|
|
270
833
|
}
|
|
271
834
|
}
|
|
835
|
+
export function normalizeIdValueWithNative(value, forceGenerate = false) {
|
|
836
|
+
const capability = 'normalizeIdValueJson';
|
|
837
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
838
|
+
if (isNativeDisabledByEnv()) {
|
|
839
|
+
return fail('native disabled');
|
|
840
|
+
}
|
|
841
|
+
const fn = readNativeFunction(capability);
|
|
842
|
+
if (!fn) {
|
|
843
|
+
return fail();
|
|
844
|
+
}
|
|
845
|
+
const payloadJson = safeStringify({ value, forceGenerate });
|
|
846
|
+
if (!payloadJson) {
|
|
847
|
+
return fail('json stringify failed');
|
|
848
|
+
}
|
|
849
|
+
try {
|
|
850
|
+
const raw = fn(payloadJson);
|
|
851
|
+
if (typeof raw !== 'string' || !raw) {
|
|
852
|
+
return fail('empty result');
|
|
853
|
+
}
|
|
854
|
+
const parsed = parseJson(raw);
|
|
855
|
+
return typeof parsed === 'string' ? parsed : fail('invalid payload');
|
|
856
|
+
}
|
|
857
|
+
catch (error) {
|
|
858
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
859
|
+
return fail(reason);
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
export function extractToolCallIdWithNative(obj) {
|
|
863
|
+
const capability = 'extractToolCallIdJson';
|
|
864
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
865
|
+
if (isNativeDisabledByEnv()) {
|
|
866
|
+
return fail('native disabled');
|
|
867
|
+
}
|
|
868
|
+
const fn = readNativeFunction(capability);
|
|
869
|
+
if (!fn) {
|
|
870
|
+
return fail();
|
|
871
|
+
}
|
|
872
|
+
const payloadJson = safeStringify({ obj: obj ?? null });
|
|
873
|
+
if (!payloadJson) {
|
|
874
|
+
return fail('json stringify failed');
|
|
875
|
+
}
|
|
876
|
+
try {
|
|
877
|
+
const raw = fn(payloadJson);
|
|
878
|
+
if (typeof raw !== 'string' || !raw) {
|
|
879
|
+
return fail('empty result');
|
|
880
|
+
}
|
|
881
|
+
const parsed = parseJson(raw);
|
|
882
|
+
return typeof parsed === 'string' ? parsed : undefined;
|
|
883
|
+
}
|
|
884
|
+
catch (error) {
|
|
885
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
886
|
+
return fail(reason);
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
export function createToolCallIdTransformerWithNative(style) {
|
|
890
|
+
const capability = 'createToolCallIdTransformerJson';
|
|
891
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
892
|
+
if (isNativeDisabledByEnv()) {
|
|
893
|
+
return fail('native disabled');
|
|
894
|
+
}
|
|
895
|
+
const fn = readNativeFunction(capability);
|
|
896
|
+
if (!fn) {
|
|
897
|
+
return fail();
|
|
898
|
+
}
|
|
899
|
+
const payloadJson = safeStringify({ style });
|
|
900
|
+
if (!payloadJson) {
|
|
901
|
+
return fail('json stringify failed');
|
|
902
|
+
}
|
|
903
|
+
try {
|
|
904
|
+
const raw = fn(payloadJson);
|
|
905
|
+
if (typeof raw !== 'string' || !raw) {
|
|
906
|
+
return fail('empty result');
|
|
907
|
+
}
|
|
908
|
+
const parsed = parseRecord(raw);
|
|
909
|
+
return parsed ?? fail('invalid payload');
|
|
910
|
+
}
|
|
911
|
+
catch (error) {
|
|
912
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
913
|
+
return fail(reason);
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
export function transformToolCallIdWithNative(state, id) {
|
|
917
|
+
const capability = 'transformToolCallIdJson';
|
|
918
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
919
|
+
if (isNativeDisabledByEnv()) {
|
|
920
|
+
return fail('native disabled');
|
|
921
|
+
}
|
|
922
|
+
const fn = readNativeFunction(capability);
|
|
923
|
+
if (!fn) {
|
|
924
|
+
return fail();
|
|
925
|
+
}
|
|
926
|
+
const payloadJson = safeStringify({ state, id });
|
|
927
|
+
if (!payloadJson) {
|
|
928
|
+
return fail('json stringify failed');
|
|
929
|
+
}
|
|
930
|
+
try {
|
|
931
|
+
const raw = fn(payloadJson);
|
|
932
|
+
if (typeof raw !== 'string' || !raw) {
|
|
933
|
+
return fail('empty result');
|
|
934
|
+
}
|
|
935
|
+
const parsed = parseRecord(raw);
|
|
936
|
+
if (!parsed || typeof parsed.id !== 'string' || !parsed.state || typeof parsed.state !== 'object') {
|
|
937
|
+
return fail('invalid payload');
|
|
938
|
+
}
|
|
939
|
+
return parsed;
|
|
940
|
+
}
|
|
941
|
+
catch (error) {
|
|
942
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
943
|
+
return fail(reason);
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
export function enforceToolCallIdStyleWithNative(messages, state) {
|
|
947
|
+
const capability = 'enforceToolCallIdStyleJson';
|
|
948
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
949
|
+
if (isNativeDisabledByEnv()) {
|
|
950
|
+
return fail('native disabled');
|
|
951
|
+
}
|
|
952
|
+
const fn = readNativeFunction(capability);
|
|
953
|
+
if (!fn) {
|
|
954
|
+
return fail();
|
|
955
|
+
}
|
|
956
|
+
const payloadJson = safeStringify({ messages: Array.isArray(messages) ? messages : [], state });
|
|
957
|
+
if (!payloadJson) {
|
|
958
|
+
return fail('json stringify failed');
|
|
959
|
+
}
|
|
960
|
+
try {
|
|
961
|
+
const raw = fn(payloadJson);
|
|
962
|
+
if (typeof raw !== 'string' || !raw) {
|
|
963
|
+
return fail('empty result');
|
|
964
|
+
}
|
|
965
|
+
const parsed = parseRecord(raw);
|
|
966
|
+
if (!parsed || !Array.isArray(parsed.messages) || !parsed.state || typeof parsed.state !== 'object') {
|
|
967
|
+
return fail('invalid payload');
|
|
968
|
+
}
|
|
969
|
+
return parsed;
|
|
970
|
+
}
|
|
971
|
+
catch (error) {
|
|
972
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
973
|
+
return fail(reason);
|
|
974
|
+
}
|
|
975
|
+
}
|
|
272
976
|
export function buildProviderProtocolErrorWithNative(input) {
|
|
273
977
|
const capability = 'buildProviderProtocolErrorJson';
|
|
274
978
|
const fail = (reason) => failNativeRequired(capability, reason);
|
|
@@ -449,6 +1153,67 @@ export function mapReasoningContentToResponsesOutputWithNative(reasoningContent)
|
|
|
449
1153
|
return fail(reason);
|
|
450
1154
|
}
|
|
451
1155
|
}
|
|
1156
|
+
export function validateToolArgumentsWithNative(toolName, args) {
|
|
1157
|
+
const capability = 'validateToolArgumentsJson';
|
|
1158
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
1159
|
+
if (isNativeDisabledByEnv()) {
|
|
1160
|
+
return fail('native disabled');
|
|
1161
|
+
}
|
|
1162
|
+
const fn = readNativeFunction(capability);
|
|
1163
|
+
if (!fn) {
|
|
1164
|
+
return fail();
|
|
1165
|
+
}
|
|
1166
|
+
const payloadJson = safeStringify({ toolName, args: args ?? null });
|
|
1167
|
+
if (!payloadJson) {
|
|
1168
|
+
return fail('json stringify failed');
|
|
1169
|
+
}
|
|
1170
|
+
try {
|
|
1171
|
+
const raw = fn(payloadJson);
|
|
1172
|
+
if (typeof raw !== 'string' || !raw) {
|
|
1173
|
+
return fail('empty result');
|
|
1174
|
+
}
|
|
1175
|
+
const parsed = parseRecord(raw);
|
|
1176
|
+
if (!parsed || typeof parsed.repaired !== 'string' || typeof parsed.success !== 'boolean') {
|
|
1177
|
+
return fail('invalid payload');
|
|
1178
|
+
}
|
|
1179
|
+
const error = typeof parsed.error === 'string' ? parsed.error : undefined;
|
|
1180
|
+
return { repaired: parsed.repaired, success: parsed.success, ...(error ? { error } : {}) };
|
|
1181
|
+
}
|
|
1182
|
+
catch (error) {
|
|
1183
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
1184
|
+
return fail(reason);
|
|
1185
|
+
}
|
|
1186
|
+
}
|
|
1187
|
+
export function repairToolCallsWithNative(toolCalls) {
|
|
1188
|
+
const capability = 'repairToolCallsJson';
|
|
1189
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
1190
|
+
if (isNativeDisabledByEnv()) {
|
|
1191
|
+
return fail('native disabled');
|
|
1192
|
+
}
|
|
1193
|
+
const fn = readNativeFunction(capability);
|
|
1194
|
+
if (!fn) {
|
|
1195
|
+
return fail();
|
|
1196
|
+
}
|
|
1197
|
+
const payloadJson = safeStringify(Array.isArray(toolCalls) ? toolCalls : []);
|
|
1198
|
+
if (!payloadJson) {
|
|
1199
|
+
return fail('json stringify failed');
|
|
1200
|
+
}
|
|
1201
|
+
try {
|
|
1202
|
+
const raw = fn(payloadJson);
|
|
1203
|
+
if (typeof raw !== 'string' || !raw) {
|
|
1204
|
+
return fail('empty result');
|
|
1205
|
+
}
|
|
1206
|
+
const parsed = parseArray(raw);
|
|
1207
|
+
if (!parsed) {
|
|
1208
|
+
return fail('invalid payload');
|
|
1209
|
+
}
|
|
1210
|
+
return parsed.filter((entry) => entry && typeof entry === 'object' && !Array.isArray(entry) && typeof entry.arguments === 'string');
|
|
1211
|
+
}
|
|
1212
|
+
catch (error) {
|
|
1213
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
1214
|
+
return fail(reason);
|
|
1215
|
+
}
|
|
1216
|
+
}
|
|
452
1217
|
export function isImagePathWithNative(pathValue) {
|
|
453
1218
|
const capability = 'isImagePathJson';
|
|
454
1219
|
const fail = (reason) => failNativeRequired(capability, reason);
|
|
@@ -476,6 +1241,42 @@ export function isImagePathWithNative(pathValue) {
|
|
|
476
1241
|
return fail(reason);
|
|
477
1242
|
}
|
|
478
1243
|
}
|
|
1244
|
+
export function extractStreamingToolCallsWithNative(input) {
|
|
1245
|
+
const capability = 'extractStreamingToolCallsJson';
|
|
1246
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
1247
|
+
if (isNativeDisabledByEnv()) {
|
|
1248
|
+
return fail('native disabled');
|
|
1249
|
+
}
|
|
1250
|
+
const fn = readNativeFunction(capability);
|
|
1251
|
+
if (!fn) {
|
|
1252
|
+
return fail();
|
|
1253
|
+
}
|
|
1254
|
+
const payloadJson = safeStringify(input ?? {});
|
|
1255
|
+
if (!payloadJson) {
|
|
1256
|
+
return fail('json stringify failed');
|
|
1257
|
+
}
|
|
1258
|
+
try {
|
|
1259
|
+
const raw = fn(payloadJson);
|
|
1260
|
+
if (typeof raw !== 'string' || !raw) {
|
|
1261
|
+
return fail('empty result');
|
|
1262
|
+
}
|
|
1263
|
+
const parsed = parseRecord(raw);
|
|
1264
|
+
if (!parsed) {
|
|
1265
|
+
return fail('invalid payload');
|
|
1266
|
+
}
|
|
1267
|
+
const buffer = typeof parsed.buffer === 'string' ? parsed.buffer : '';
|
|
1268
|
+
const idCounter = typeof parsed.idCounter === 'number' ? parsed.idCounter : input.idCounter;
|
|
1269
|
+
const toolCalls = Array.isArray(parsed.toolCalls)
|
|
1270
|
+
? parsed.toolCalls.filter((entry) => entry && typeof entry === 'object' && !Array.isArray(entry))
|
|
1271
|
+
.map((entry) => entry)
|
|
1272
|
+
: [];
|
|
1273
|
+
return { buffer, idCounter, toolCalls };
|
|
1274
|
+
}
|
|
1275
|
+
catch (error) {
|
|
1276
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
1277
|
+
return fail(reason);
|
|
1278
|
+
}
|
|
1279
|
+
}
|
|
479
1280
|
export function isCompactionRequestWithNative(payload) {
|
|
480
1281
|
const capability = 'isCompactionRequestJson';
|
|
481
1282
|
const fail = (reason) => failNativeRequired(capability, reason);
|