@jsonstudio/llms 0.6.3275 → 0.6.3405
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/actions/claude-thinking-tools.d.ts +1 -14
- package/dist/conversion/compat/actions/claude-thinking-tools.js +3 -71
- package/dist/conversion/compat/actions/lmstudio-responses-fc-ids.d.ts +0 -8
- package/dist/conversion/compat/actions/lmstudio-responses-fc-ids.js +2 -57
- package/dist/conversion/compat/actions/normalize-tool-call-ids.d.ts +0 -9
- package/dist/conversion/compat/actions/normalize-tool-call-ids.js +6 -136
- package/dist/conversion/compat/actions/request-rules.js +2 -61
- package/dist/conversion/compat/actions/response-blacklist.d.ts +0 -4
- package/dist/conversion/compat/actions/response-blacklist.js +2 -77
- package/dist/conversion/compat/actions/response-normalize.js +2 -119
- package/dist/conversion/compat/actions/response-validate.js +2 -74
- package/dist/conversion/compat/actions/strip-orphan-function-calls-tag.js +2 -150
- package/dist/conversion/compat/profiles/responses-crs.json +15 -0
- package/dist/conversion/hub/operation-table/semantic-mappers/anthropic-mapper.js +24 -1
- package/dist/conversion/hub/operation-table/semantic-mappers/chat-mapper.js +16 -5
- package/dist/conversion/hub/pipeline/hub-pipeline.js +91 -0
- 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-tool-parser.js +7 -8
- package/dist/conversion/shared/reasoning-utils.js +13 -35
- package/dist/conversion/shared/responses-response-utils.js +3 -48
- package/dist/conversion/shared/responses-tool-utils.d.ts +1 -1
- package/dist/conversion/shared/responses-tool-utils.js +74 -180
- 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/bootstrap/web-search-config.js +25 -0
- package/dist/router/virtual-router/bootstrap.js +21 -16
- package/dist/router/virtual-router/engine-legacy/helpers.js +1 -1
- package/dist/router/virtual-router/engine-selection/native-compat-action-semantics.d.ts +6 -0
- package/dist/router/virtual-router/engine-selection/native-compat-action-semantics.js +171 -0
- 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 +45 -0
- package/dist/router/virtual-router/engine-selection/native-shared-conversion-semantics.d.ts +70 -1
- package/dist/router/virtual-router/engine-selection/native-shared-conversion-semantics.js +993 -55
- package/dist/router/virtual-router/engine.js +3 -2
- package/dist/router/virtual-router/routing-instructions/parse.js +30 -3
- package/dist/router/virtual-router/types.d.ts +23 -0
- package/dist/servertool/handlers/web-search.js +26 -1
- package/dist/servertool/server-side-tools.js +11 -2
- package/dist/servertool/types.d.ts +4 -0
- 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,31 +61,837 @@ 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)) {
|
|
67
90
|
return null;
|
|
68
91
|
}
|
|
69
|
-
const out = [];
|
|
70
|
-
for (const entry of parsed) {
|
|
71
|
-
if (!entry || typeof entry !== 'object' || Array.isArray(entry)) {
|
|
72
|
-
return null;
|
|
92
|
+
const out = [];
|
|
93
|
+
for (const entry of parsed) {
|
|
94
|
+
if (!entry || typeof entry !== 'object' || Array.isArray(entry)) {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
const row = entry;
|
|
98
|
+
if (typeof row.name !== 'string' || typeof row.args !== 'string') {
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
const id = typeof row.id === 'string' && row.id.trim().length ? row.id : undefined;
|
|
102
|
+
out.push({ id, name: row.name, args: row.args });
|
|
103
|
+
}
|
|
104
|
+
return out;
|
|
105
|
+
}
|
|
106
|
+
function parseArray(raw) {
|
|
107
|
+
const parsed = parseJson(raw);
|
|
108
|
+
return Array.isArray(parsed) ? parsed : null;
|
|
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 parseString(raw) {
|
|
127
|
+
const parsed = parseJson(raw);
|
|
128
|
+
return typeof parsed === 'string' ? parsed : null;
|
|
129
|
+
}
|
|
130
|
+
function parseStringArray(raw) {
|
|
131
|
+
const parsed = parseArray(raw);
|
|
132
|
+
if (!parsed)
|
|
133
|
+
return null;
|
|
134
|
+
const out = [];
|
|
135
|
+
for (const item of parsed) {
|
|
136
|
+
if (typeof item !== "string") {
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
out.push(item);
|
|
140
|
+
}
|
|
141
|
+
return out;
|
|
142
|
+
}
|
|
143
|
+
export function parseLenientJsonishWithNative(value) {
|
|
144
|
+
const capability = 'parseLenientJsonishJson';
|
|
145
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
146
|
+
if (isNativeDisabledByEnv()) {
|
|
147
|
+
return fail('native disabled');
|
|
148
|
+
}
|
|
149
|
+
const fn = readNativeFunction(capability);
|
|
150
|
+
if (!fn) {
|
|
151
|
+
return fail();
|
|
152
|
+
}
|
|
153
|
+
const valueJson = safeStringify(value ?? null);
|
|
154
|
+
if (!valueJson) {
|
|
155
|
+
return fail('json stringify failed');
|
|
156
|
+
}
|
|
157
|
+
try {
|
|
158
|
+
const raw = fn(valueJson);
|
|
159
|
+
if (typeof raw !== 'string' || !raw) {
|
|
160
|
+
return fail('empty result');
|
|
161
|
+
}
|
|
162
|
+
const parsed = parseJson(raw);
|
|
163
|
+
return parsed === null ? fail('invalid payload') : parsed;
|
|
164
|
+
}
|
|
165
|
+
catch (error) {
|
|
166
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
167
|
+
return fail(reason);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
export function repairArgumentsToStringWithNative(value) {
|
|
171
|
+
const capability = 'repairArgumentsToStringJsonishJson';
|
|
172
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
173
|
+
if (isNativeDisabledByEnv()) {
|
|
174
|
+
return fail('native disabled');
|
|
175
|
+
}
|
|
176
|
+
const fn = readNativeFunction(capability);
|
|
177
|
+
if (!fn) {
|
|
178
|
+
return fail();
|
|
179
|
+
}
|
|
180
|
+
const valueJson = safeStringify(value ?? null);
|
|
181
|
+
if (!valueJson) {
|
|
182
|
+
return fail('json stringify failed');
|
|
183
|
+
}
|
|
184
|
+
try {
|
|
185
|
+
const raw = fn(valueJson);
|
|
186
|
+
if (typeof raw !== 'string') {
|
|
187
|
+
return fail('invalid payload');
|
|
188
|
+
}
|
|
189
|
+
return raw;
|
|
190
|
+
}
|
|
191
|
+
catch (error) {
|
|
192
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
193
|
+
return fail(reason);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
export function extractToolCallsFromReasoningTextWithNative(text, idPrefix) {
|
|
197
|
+
const capability = 'extractToolCallsFromReasoningTextJson';
|
|
198
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
199
|
+
if (isNativeDisabledByEnv()) {
|
|
200
|
+
return fail('native disabled');
|
|
201
|
+
}
|
|
202
|
+
const fn = readNativeFunction(capability);
|
|
203
|
+
if (!fn) {
|
|
204
|
+
return fail();
|
|
205
|
+
}
|
|
206
|
+
try {
|
|
207
|
+
const raw = fn(String(text ?? ''), idPrefix);
|
|
208
|
+
if (typeof raw !== 'string' || !raw) {
|
|
209
|
+
return fail('empty result');
|
|
210
|
+
}
|
|
211
|
+
const parsed = parseExtractToolCallsOutput(raw);
|
|
212
|
+
return parsed ?? fail('invalid payload');
|
|
213
|
+
}
|
|
214
|
+
catch (error) {
|
|
215
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
216
|
+
return fail(reason);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
export function extractReasoningSegmentsWithNative(source) {
|
|
220
|
+
const capability = 'extractReasoningSegmentsJson';
|
|
221
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
222
|
+
if (isNativeDisabledByEnv()) {
|
|
223
|
+
return fail('native disabled');
|
|
224
|
+
}
|
|
225
|
+
const fn = readNativeFunction(capability);
|
|
226
|
+
if (!fn) {
|
|
227
|
+
return fail();
|
|
228
|
+
}
|
|
229
|
+
const payloadJson = safeStringify({ source: String(source ?? '') });
|
|
230
|
+
if (!payloadJson) {
|
|
231
|
+
return fail('json stringify failed');
|
|
232
|
+
}
|
|
233
|
+
try {
|
|
234
|
+
const raw = fn(payloadJson);
|
|
235
|
+
if (typeof raw !== 'string' || !raw) {
|
|
236
|
+
return fail('empty result');
|
|
237
|
+
}
|
|
238
|
+
const parsed = parseExtractReasoningSegmentsOutput(raw);
|
|
239
|
+
return parsed ?? fail('invalid payload');
|
|
240
|
+
}
|
|
241
|
+
catch (error) {
|
|
242
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
243
|
+
return fail(reason);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
export function normalizeAssistantTextToToolCallsWithNative(message, options) {
|
|
247
|
+
const capability = 'normalizeAssistantTextToToolCallsJson';
|
|
248
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
249
|
+
if (isNativeDisabledByEnv()) {
|
|
250
|
+
return fail('native disabled');
|
|
251
|
+
}
|
|
252
|
+
const fn = readNativeFunction(capability);
|
|
253
|
+
if (!fn) {
|
|
254
|
+
return fail();
|
|
255
|
+
}
|
|
256
|
+
const msgJson = safeStringify(message ?? null);
|
|
257
|
+
if (!msgJson) {
|
|
258
|
+
return fail('json stringify failed');
|
|
259
|
+
}
|
|
260
|
+
const optionsJson = options ? safeStringify(options) : undefined;
|
|
261
|
+
try {
|
|
262
|
+
const raw = fn(msgJson, optionsJson);
|
|
263
|
+
if (typeof raw !== 'string' || !raw) {
|
|
264
|
+
return fail('empty result');
|
|
265
|
+
}
|
|
266
|
+
const parsed = parseRecord(raw);
|
|
267
|
+
return parsed ?? fail('invalid payload');
|
|
268
|
+
}
|
|
269
|
+
catch (error) {
|
|
270
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
271
|
+
return fail(reason);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
export function normalizeReasoningInChatPayloadWithNative(payload) {
|
|
275
|
+
const capability = 'normalizeReasoningInChatPayloadJson';
|
|
276
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
277
|
+
if (isNativeDisabledByEnv()) {
|
|
278
|
+
return fail('native disabled');
|
|
279
|
+
}
|
|
280
|
+
const fn = readNativeFunction(capability);
|
|
281
|
+
if (!fn) {
|
|
282
|
+
return fail();
|
|
283
|
+
}
|
|
284
|
+
const payloadJson = safeStringify({ payload: payload ?? null });
|
|
285
|
+
if (!payloadJson) {
|
|
286
|
+
return fail('json stringify failed');
|
|
287
|
+
}
|
|
288
|
+
try {
|
|
289
|
+
const raw = fn(payloadJson);
|
|
290
|
+
if (typeof raw !== 'string' || !raw) {
|
|
291
|
+
return fail('empty result');
|
|
292
|
+
}
|
|
293
|
+
const parsed = parseNormalizeReasoningOutput(raw);
|
|
294
|
+
return parsed ? parsed.payload : fail('invalid payload');
|
|
295
|
+
}
|
|
296
|
+
catch (error) {
|
|
297
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
298
|
+
return fail(reason);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
export function normalizeReasoningInResponsesPayloadWithNative(payload, options) {
|
|
302
|
+
const capability = 'normalizeReasoningInResponsesPayloadJson';
|
|
303
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
304
|
+
if (isNativeDisabledByEnv()) {
|
|
305
|
+
return fail('native disabled');
|
|
306
|
+
}
|
|
307
|
+
const fn = readNativeFunction(capability);
|
|
308
|
+
if (!fn) {
|
|
309
|
+
return fail();
|
|
310
|
+
}
|
|
311
|
+
const payloadJson = safeStringify({ payload: payload ?? null, options: options ?? null });
|
|
312
|
+
if (!payloadJson) {
|
|
313
|
+
return fail('json stringify failed');
|
|
314
|
+
}
|
|
315
|
+
try {
|
|
316
|
+
const raw = fn(payloadJson);
|
|
317
|
+
if (typeof raw !== 'string' || !raw) {
|
|
318
|
+
return fail('empty result');
|
|
319
|
+
}
|
|
320
|
+
const parsed = parseNormalizeReasoningOutput(raw);
|
|
321
|
+
return parsed ? parsed.payload : fail('invalid payload');
|
|
322
|
+
}
|
|
323
|
+
catch (error) {
|
|
324
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
325
|
+
return fail(reason);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
export function normalizeReasoningInGeminiPayloadWithNative(payload) {
|
|
329
|
+
const capability = 'normalizeReasoningInGeminiPayloadJson';
|
|
330
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
331
|
+
if (isNativeDisabledByEnv()) {
|
|
332
|
+
return fail('native disabled');
|
|
333
|
+
}
|
|
334
|
+
const fn = readNativeFunction(capability);
|
|
335
|
+
if (!fn) {
|
|
336
|
+
return fail();
|
|
337
|
+
}
|
|
338
|
+
const payloadJson = safeStringify({ payload: payload ?? null });
|
|
339
|
+
if (!payloadJson) {
|
|
340
|
+
return fail('json stringify failed');
|
|
341
|
+
}
|
|
342
|
+
try {
|
|
343
|
+
const raw = fn(payloadJson);
|
|
344
|
+
if (typeof raw !== 'string' || !raw) {
|
|
345
|
+
return fail('empty result');
|
|
346
|
+
}
|
|
347
|
+
const parsed = parseNormalizeReasoningOutput(raw);
|
|
348
|
+
return parsed ? parsed.payload : fail('invalid payload');
|
|
349
|
+
}
|
|
350
|
+
catch (error) {
|
|
351
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
352
|
+
return fail(reason);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
export function normalizeReasoningInAnthropicPayloadWithNative(payload) {
|
|
356
|
+
const capability = 'normalizeReasoningInAnthropicPayloadJson';
|
|
357
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
358
|
+
if (isNativeDisabledByEnv()) {
|
|
359
|
+
return fail('native disabled');
|
|
360
|
+
}
|
|
361
|
+
const fn = readNativeFunction(capability);
|
|
362
|
+
if (!fn) {
|
|
363
|
+
return fail();
|
|
364
|
+
}
|
|
365
|
+
const payloadJson = safeStringify({ payload: payload ?? null });
|
|
366
|
+
if (!payloadJson) {
|
|
367
|
+
return fail('json stringify failed');
|
|
368
|
+
}
|
|
369
|
+
try {
|
|
370
|
+
const raw = fn(payloadJson);
|
|
371
|
+
if (typeof raw !== 'string' || !raw) {
|
|
372
|
+
return fail('empty result');
|
|
373
|
+
}
|
|
374
|
+
const parsed = parseNormalizeReasoningOutput(raw);
|
|
375
|
+
return parsed ? parsed.payload : fail('invalid payload');
|
|
376
|
+
}
|
|
377
|
+
catch (error) {
|
|
378
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
379
|
+
return fail(reason);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
export function normalizeReasoningInOpenAIPayloadWithNative(payload) {
|
|
383
|
+
const capability = 'normalizeReasoningInOpenAIPayloadJson';
|
|
384
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
385
|
+
if (isNativeDisabledByEnv()) {
|
|
386
|
+
return fail('native disabled');
|
|
387
|
+
}
|
|
388
|
+
const fn = readNativeFunction(capability);
|
|
389
|
+
if (!fn) {
|
|
390
|
+
return fail();
|
|
391
|
+
}
|
|
392
|
+
const payloadJson = safeStringify({ payload: payload ?? null });
|
|
393
|
+
if (!payloadJson) {
|
|
394
|
+
return fail('json stringify failed');
|
|
395
|
+
}
|
|
396
|
+
try {
|
|
397
|
+
const raw = fn(payloadJson);
|
|
398
|
+
if (typeof raw !== 'string' || !raw) {
|
|
399
|
+
return fail('empty result');
|
|
400
|
+
}
|
|
401
|
+
const parsed = parseNormalizeReasoningOutput(raw);
|
|
402
|
+
return parsed ? parsed.payload : fail('invalid payload');
|
|
403
|
+
}
|
|
404
|
+
catch (error) {
|
|
405
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
406
|
+
return fail(reason);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
export function bridgeToolToChatDefinitionWithNative(tool, options) {
|
|
410
|
+
const capability = 'bridgeToolToChatDefinitionJson';
|
|
411
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
412
|
+
if (isNativeDisabledByEnv()) {
|
|
413
|
+
return fail('native disabled');
|
|
414
|
+
}
|
|
415
|
+
const fn = readNativeFunction(capability);
|
|
416
|
+
if (!fn) {
|
|
417
|
+
return fail();
|
|
418
|
+
}
|
|
419
|
+
const payloadJson = safeStringify({ tool, options: options ?? null });
|
|
420
|
+
if (!payloadJson) {
|
|
421
|
+
return fail('json stringify failed');
|
|
422
|
+
}
|
|
423
|
+
try {
|
|
424
|
+
const raw = fn(payloadJson);
|
|
425
|
+
if (typeof raw !== 'string' || !raw) {
|
|
426
|
+
return fail('empty result');
|
|
427
|
+
}
|
|
428
|
+
const parsed = parseToolDefinitionOutput(raw);
|
|
429
|
+
return parsed ?? fail('invalid payload');
|
|
430
|
+
}
|
|
431
|
+
catch (error) {
|
|
432
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
433
|
+
return fail(reason);
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
export function chatToolToBridgeDefinitionWithNative(tool, options) {
|
|
437
|
+
const capability = 'chatToolToBridgeDefinitionJson';
|
|
438
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
439
|
+
if (isNativeDisabledByEnv()) {
|
|
440
|
+
return fail('native disabled');
|
|
441
|
+
}
|
|
442
|
+
const fn = readNativeFunction(capability);
|
|
443
|
+
if (!fn) {
|
|
444
|
+
return fail();
|
|
445
|
+
}
|
|
446
|
+
const payloadJson = safeStringify({ tool, options: options ?? null });
|
|
447
|
+
if (!payloadJson) {
|
|
448
|
+
return fail('json stringify failed');
|
|
449
|
+
}
|
|
450
|
+
try {
|
|
451
|
+
const raw = fn(payloadJson);
|
|
452
|
+
if (typeof raw !== 'string' || !raw) {
|
|
453
|
+
return fail('empty result');
|
|
454
|
+
}
|
|
455
|
+
const parsed = parseToolDefinitionOutput(raw);
|
|
456
|
+
return parsed ?? fail('invalid payload');
|
|
457
|
+
}
|
|
458
|
+
catch (error) {
|
|
459
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
460
|
+
return fail(reason);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
export function mapBridgeToolsToChatWithNative(rawTools, options) {
|
|
464
|
+
const capability = 'mapBridgeToolsToChatWithOptionsJson';
|
|
465
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
466
|
+
if (isNativeDisabledByEnv()) {
|
|
467
|
+
return fail('native disabled');
|
|
468
|
+
}
|
|
469
|
+
const fn = readNativeFunction(capability);
|
|
470
|
+
if (!fn) {
|
|
471
|
+
return fail();
|
|
472
|
+
}
|
|
473
|
+
const payloadJson = safeStringify({ tools: Array.isArray(rawTools) ? rawTools : [], options: options ?? null });
|
|
474
|
+
if (!payloadJson) {
|
|
475
|
+
return fail('json stringify failed');
|
|
476
|
+
}
|
|
477
|
+
try {
|
|
478
|
+
const raw = fn(payloadJson);
|
|
479
|
+
if (typeof raw !== 'string' || !raw) {
|
|
480
|
+
return fail('empty result');
|
|
481
|
+
}
|
|
482
|
+
const parsed = parseToolDefinitionArray(raw);
|
|
483
|
+
return parsed ?? fail('invalid payload');
|
|
484
|
+
}
|
|
485
|
+
catch (error) {
|
|
486
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
487
|
+
return fail(reason);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
export function mapChatToolsToBridgeWithNative(rawTools, options) {
|
|
491
|
+
const capability = 'mapChatToolsToBridgeWithOptionsJson';
|
|
492
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
493
|
+
if (isNativeDisabledByEnv()) {
|
|
494
|
+
return fail('native disabled');
|
|
495
|
+
}
|
|
496
|
+
const fn = readNativeFunction(capability);
|
|
497
|
+
if (!fn) {
|
|
498
|
+
return fail();
|
|
499
|
+
}
|
|
500
|
+
const payloadJson = safeStringify({ tools: Array.isArray(rawTools) ? rawTools : [], options: options ?? null });
|
|
501
|
+
if (!payloadJson) {
|
|
502
|
+
return fail('json stringify failed');
|
|
503
|
+
}
|
|
504
|
+
try {
|
|
505
|
+
const raw = fn(payloadJson);
|
|
506
|
+
if (typeof raw !== 'string' || !raw) {
|
|
507
|
+
return fail('empty result');
|
|
508
|
+
}
|
|
509
|
+
const parsed = parseToolDefinitionArray(raw);
|
|
510
|
+
return parsed ?? fail('invalid payload');
|
|
511
|
+
}
|
|
512
|
+
catch (error) {
|
|
513
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
514
|
+
return fail(reason);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
export function collectToolCallsFromResponsesWithNative(response) {
|
|
518
|
+
const capability = 'collectToolCallsFromResponsesJson';
|
|
519
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
520
|
+
if (isNativeDisabledByEnv()) {
|
|
521
|
+
return fail('native disabled');
|
|
522
|
+
}
|
|
523
|
+
const fn = readNativeFunction(capability);
|
|
524
|
+
if (!fn) {
|
|
525
|
+
return fail();
|
|
526
|
+
}
|
|
527
|
+
const payloadJson = safeStringify(response ?? {});
|
|
528
|
+
if (!payloadJson) {
|
|
529
|
+
return fail('json stringify failed');
|
|
530
|
+
}
|
|
531
|
+
try {
|
|
532
|
+
const raw = fn(payloadJson);
|
|
533
|
+
if (typeof raw !== 'string' || !raw) {
|
|
534
|
+
return fail('empty result');
|
|
535
|
+
}
|
|
536
|
+
const parsed = parseToolDefinitionArray(raw);
|
|
537
|
+
return parsed ?? fail('invalid payload');
|
|
538
|
+
}
|
|
539
|
+
catch (error) {
|
|
540
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
541
|
+
return fail(reason);
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
export function resolveFinishReasonWithNative(response, toolCalls) {
|
|
545
|
+
const capability = 'resolveFinishReasonJson';
|
|
546
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
547
|
+
if (isNativeDisabledByEnv()) {
|
|
548
|
+
return fail('native disabled');
|
|
549
|
+
}
|
|
550
|
+
const fn = readNativeFunction(capability);
|
|
551
|
+
if (!fn) {
|
|
552
|
+
return fail();
|
|
553
|
+
}
|
|
554
|
+
const responseJson = safeStringify(response ?? {});
|
|
555
|
+
const toolCallsJson = safeStringify(Array.isArray(toolCalls) ? toolCalls : []);
|
|
556
|
+
if (!responseJson || !toolCallsJson) {
|
|
557
|
+
return fail('json stringify failed');
|
|
558
|
+
}
|
|
559
|
+
try {
|
|
560
|
+
const raw = fn(responseJson, toolCallsJson);
|
|
561
|
+
if (typeof raw !== 'string' || !raw) {
|
|
562
|
+
return fail('empty result');
|
|
563
|
+
}
|
|
564
|
+
return parseString(raw) ?? fail('invalid payload');
|
|
565
|
+
}
|
|
566
|
+
catch (error) {
|
|
567
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
568
|
+
return fail(reason);
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
export function hasValidThoughtSignatureWithNative(block, options) {
|
|
572
|
+
const capability = 'hasValidThoughtSignatureJson';
|
|
573
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
574
|
+
if (isNativeDisabledByEnv()) {
|
|
575
|
+
return fail('native disabled');
|
|
576
|
+
}
|
|
577
|
+
const fn = readNativeFunction(capability);
|
|
578
|
+
if (!fn) {
|
|
579
|
+
return fail();
|
|
580
|
+
}
|
|
581
|
+
const payloadJson = safeStringify({ block: block ?? null, options: options ?? null });
|
|
582
|
+
if (!payloadJson) {
|
|
583
|
+
return fail('json stringify failed');
|
|
584
|
+
}
|
|
585
|
+
try {
|
|
586
|
+
const raw = fn(payloadJson);
|
|
587
|
+
if (typeof raw !== 'string' || !raw) {
|
|
588
|
+
return fail('empty result');
|
|
589
|
+
}
|
|
590
|
+
const parsed = parseJson(raw);
|
|
591
|
+
return typeof parsed === 'boolean' ? parsed : fail('invalid payload');
|
|
592
|
+
}
|
|
593
|
+
catch (error) {
|
|
594
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
595
|
+
return fail(reason);
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
export function sanitizeThinkingBlockWithNative(block) {
|
|
599
|
+
const capability = 'sanitizeThinkingBlockJson';
|
|
600
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
601
|
+
if (isNativeDisabledByEnv()) {
|
|
602
|
+
return fail('native disabled');
|
|
603
|
+
}
|
|
604
|
+
const fn = readNativeFunction(capability);
|
|
605
|
+
if (!fn) {
|
|
606
|
+
return fail();
|
|
607
|
+
}
|
|
608
|
+
const payloadJson = safeStringify({ block: block ?? null });
|
|
609
|
+
if (!payloadJson) {
|
|
610
|
+
return fail('json stringify failed');
|
|
611
|
+
}
|
|
612
|
+
try {
|
|
613
|
+
const raw = fn(payloadJson);
|
|
614
|
+
if (typeof raw !== 'string' || !raw) {
|
|
615
|
+
return fail('empty result');
|
|
616
|
+
}
|
|
617
|
+
const parsed = parseRecord(raw);
|
|
618
|
+
return parsed ?? fail('invalid payload');
|
|
619
|
+
}
|
|
620
|
+
catch (error) {
|
|
621
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
622
|
+
return fail(reason);
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
export function filterInvalidThinkingBlocksWithNative(messages, options) {
|
|
626
|
+
const capability = 'filterInvalidThinkingBlocksJson';
|
|
627
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
628
|
+
if (isNativeDisabledByEnv()) {
|
|
629
|
+
return fail('native disabled');
|
|
630
|
+
}
|
|
631
|
+
const fn = readNativeFunction(capability);
|
|
632
|
+
if (!fn) {
|
|
633
|
+
return fail();
|
|
634
|
+
}
|
|
635
|
+
const payloadJson = safeStringify({ messages: Array.isArray(messages) ? messages : [], options: options ?? null });
|
|
636
|
+
if (!payloadJson) {
|
|
637
|
+
return fail('json stringify failed');
|
|
638
|
+
}
|
|
639
|
+
try {
|
|
640
|
+
const raw = fn(payloadJson);
|
|
641
|
+
if (typeof raw !== 'string' || !raw) {
|
|
642
|
+
return fail('empty result');
|
|
643
|
+
}
|
|
644
|
+
const parsed = parseArray(raw);
|
|
645
|
+
return parsed ?? fail('invalid payload');
|
|
646
|
+
}
|
|
647
|
+
catch (error) {
|
|
648
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
649
|
+
return fail(reason);
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
export function removeTrailingUnsignedThinkingBlocksWithNative(blocks, options) {
|
|
653
|
+
const capability = 'removeTrailingUnsignedThinkingBlocksJson';
|
|
654
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
655
|
+
if (isNativeDisabledByEnv()) {
|
|
656
|
+
return fail('native disabled');
|
|
657
|
+
}
|
|
658
|
+
const fn = readNativeFunction(capability);
|
|
659
|
+
if (!fn) {
|
|
660
|
+
return fail();
|
|
661
|
+
}
|
|
662
|
+
const payloadJson = safeStringify({ blocks: Array.isArray(blocks) ? blocks : [], options: options ?? null });
|
|
663
|
+
if (!payloadJson) {
|
|
664
|
+
return fail('json stringify failed');
|
|
665
|
+
}
|
|
666
|
+
try {
|
|
667
|
+
const raw = fn(payloadJson);
|
|
668
|
+
if (typeof raw !== 'string' || !raw) {
|
|
669
|
+
return fail('empty result');
|
|
670
|
+
}
|
|
671
|
+
const parsed = parseArray(raw);
|
|
672
|
+
return parsed ?? fail('invalid payload');
|
|
673
|
+
}
|
|
674
|
+
catch (error) {
|
|
675
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
676
|
+
return fail(reason);
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
export function sanitizeReasoningTaggedTextWithNative(text) {
|
|
680
|
+
const capability = 'sanitizeReasoningTaggedTextJson';
|
|
681
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
682
|
+
if (isNativeDisabledByEnv()) {
|
|
683
|
+
return fail('native disabled');
|
|
684
|
+
}
|
|
685
|
+
const fn = readNativeFunction(capability);
|
|
686
|
+
if (!fn) {
|
|
687
|
+
return fail();
|
|
688
|
+
}
|
|
689
|
+
try {
|
|
690
|
+
const raw = fn(String(text ?? ''));
|
|
691
|
+
if (typeof raw !== 'string') {
|
|
692
|
+
return fail('invalid payload');
|
|
693
|
+
}
|
|
694
|
+
return raw;
|
|
695
|
+
}
|
|
696
|
+
catch (error) {
|
|
697
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
698
|
+
return fail(reason);
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
export function ensureBridgeInstructionsWithNative(payload) {
|
|
702
|
+
const capability = 'ensureBridgeInstructionsJson';
|
|
703
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
704
|
+
if (isNativeDisabledByEnv()) {
|
|
705
|
+
return fail('native disabled');
|
|
706
|
+
}
|
|
707
|
+
const fn = readNativeFunction(capability);
|
|
708
|
+
if (!fn) {
|
|
709
|
+
return fail();
|
|
710
|
+
}
|
|
711
|
+
const payloadJson = safeStringify(payload ?? {});
|
|
712
|
+
if (!payloadJson) {
|
|
713
|
+
return fail('json stringify failed');
|
|
714
|
+
}
|
|
715
|
+
try {
|
|
716
|
+
const raw = fn(payloadJson);
|
|
717
|
+
if (typeof raw !== 'string' || !raw) {
|
|
718
|
+
return fail('empty result');
|
|
719
|
+
}
|
|
720
|
+
const parsed = parseRecord(raw);
|
|
721
|
+
return parsed ?? fail('invalid payload');
|
|
722
|
+
}
|
|
723
|
+
catch (error) {
|
|
724
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
725
|
+
return fail(reason);
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
export function repairFindMetaWithNative(script) {
|
|
729
|
+
const capability = 'repairFindMetaJson';
|
|
730
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
731
|
+
if (isNativeDisabledByEnv()) {
|
|
732
|
+
return fail('native disabled');
|
|
733
|
+
}
|
|
734
|
+
const fn = readNativeFunction(capability);
|
|
735
|
+
if (!fn) {
|
|
736
|
+
return fail();
|
|
737
|
+
}
|
|
738
|
+
const payloadJson = safeStringify(script ?? '');
|
|
739
|
+
if (!payloadJson) {
|
|
740
|
+
return fail('json stringify failed');
|
|
741
|
+
}
|
|
742
|
+
try {
|
|
743
|
+
const raw = fn(payloadJson);
|
|
744
|
+
if (typeof raw !== 'string' || !raw) {
|
|
745
|
+
return fail('empty result');
|
|
746
|
+
}
|
|
747
|
+
const parsed = parseJson(raw);
|
|
748
|
+
return typeof parsed === 'string' ? parsed : fail('invalid payload');
|
|
749
|
+
}
|
|
750
|
+
catch (error) {
|
|
751
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
752
|
+
return fail(reason);
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
export function splitCommandStringWithNative(input) {
|
|
756
|
+
const capability = 'splitCommandStringJson';
|
|
757
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
758
|
+
if (isNativeDisabledByEnv()) {
|
|
759
|
+
return fail('native disabled');
|
|
760
|
+
}
|
|
761
|
+
const fn = readNativeFunction(capability);
|
|
762
|
+
if (!fn) {
|
|
763
|
+
return fail();
|
|
764
|
+
}
|
|
765
|
+
const payloadJson = safeStringify(input ?? '');
|
|
766
|
+
if (!payloadJson) {
|
|
767
|
+
return fail('json stringify failed');
|
|
768
|
+
}
|
|
769
|
+
try {
|
|
770
|
+
const raw = fn(payloadJson);
|
|
771
|
+
if (typeof raw !== 'string' || !raw) {
|
|
772
|
+
return fail('empty result');
|
|
773
|
+
}
|
|
774
|
+
const parsed = parseStringArray(raw);
|
|
775
|
+
return parsed ?? fail('invalid payload');
|
|
776
|
+
}
|
|
777
|
+
catch (error) {
|
|
778
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
779
|
+
return fail(reason);
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
export function packShellArgsWithNative(input) {
|
|
783
|
+
const capability = 'packShellArgsJson';
|
|
784
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
785
|
+
if (isNativeDisabledByEnv()) {
|
|
786
|
+
return fail('native disabled');
|
|
787
|
+
}
|
|
788
|
+
const fn = readNativeFunction(capability);
|
|
789
|
+
if (!fn) {
|
|
790
|
+
return fail();
|
|
791
|
+
}
|
|
792
|
+
const payloadJson = safeStringify(input ?? {});
|
|
793
|
+
if (!payloadJson) {
|
|
794
|
+
return fail('json stringify failed');
|
|
795
|
+
}
|
|
796
|
+
try {
|
|
797
|
+
const raw = fn(payloadJson);
|
|
798
|
+
if (typeof raw !== 'string' || !raw) {
|
|
799
|
+
return fail('empty result');
|
|
800
|
+
}
|
|
801
|
+
const parsed = parseRecord(raw);
|
|
802
|
+
return parsed ?? fail('invalid payload');
|
|
803
|
+
}
|
|
804
|
+
catch (error) {
|
|
805
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
806
|
+
return fail(reason);
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
export function flattenByCommaWithNative(items) {
|
|
810
|
+
const capability = 'flattenByCommaJson';
|
|
811
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
812
|
+
if (isNativeDisabledByEnv()) {
|
|
813
|
+
return fail('native disabled');
|
|
814
|
+
}
|
|
815
|
+
const fn = readNativeFunction(capability);
|
|
816
|
+
if (!fn) {
|
|
817
|
+
return fail();
|
|
818
|
+
}
|
|
819
|
+
const payloadJson = safeStringify(Array.isArray(items) ? items : []);
|
|
820
|
+
if (!payloadJson) {
|
|
821
|
+
return fail('json stringify failed');
|
|
822
|
+
}
|
|
823
|
+
try {
|
|
824
|
+
const raw = fn(payloadJson);
|
|
825
|
+
if (typeof raw !== 'string' || !raw) {
|
|
826
|
+
return fail('empty result');
|
|
73
827
|
}
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
828
|
+
const parsed = parseStringArray(raw);
|
|
829
|
+
return parsed ?? fail('invalid payload');
|
|
830
|
+
}
|
|
831
|
+
catch (error) {
|
|
832
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
833
|
+
return fail(reason);
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
export function chunkStringWithNative(s, minParts = 3, maxParts = 12, targetChunk = 12) {
|
|
837
|
+
const capability = 'chunkStringJson';
|
|
838
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
839
|
+
if (isNativeDisabledByEnv()) {
|
|
840
|
+
return fail('native disabled');
|
|
841
|
+
}
|
|
842
|
+
const fn = readNativeFunction(capability);
|
|
843
|
+
if (!fn) {
|
|
844
|
+
return fail();
|
|
845
|
+
}
|
|
846
|
+
const payloadJson = safeStringify({ s, minParts, maxParts, targetChunk });
|
|
847
|
+
if (!payloadJson) {
|
|
848
|
+
return fail('json stringify failed');
|
|
849
|
+
}
|
|
850
|
+
try {
|
|
851
|
+
const raw = fn(payloadJson);
|
|
852
|
+
if (typeof raw !== 'string' || !raw) {
|
|
853
|
+
return fail('empty result');
|
|
77
854
|
}
|
|
78
|
-
const
|
|
79
|
-
|
|
855
|
+
const parsed = parseStringArray(raw);
|
|
856
|
+
return parsed ?? fail('invalid payload');
|
|
857
|
+
}
|
|
858
|
+
catch (error) {
|
|
859
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
860
|
+
return fail(reason);
|
|
80
861
|
}
|
|
81
|
-
return out;
|
|
82
862
|
}
|
|
83
|
-
function
|
|
84
|
-
const
|
|
85
|
-
|
|
863
|
+
export function deriveToolCallKeyWithNative(call) {
|
|
864
|
+
const capability = 'deriveToolCallKeyJson';
|
|
865
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
866
|
+
if (isNativeDisabledByEnv()) {
|
|
867
|
+
return fail('native disabled');
|
|
868
|
+
}
|
|
869
|
+
const fn = readNativeFunction(capability);
|
|
870
|
+
if (!fn) {
|
|
871
|
+
return fail();
|
|
872
|
+
}
|
|
873
|
+
const callJson = safeStringify(call ?? null);
|
|
874
|
+
if (!callJson) {
|
|
875
|
+
return fail('json stringify failed');
|
|
876
|
+
}
|
|
877
|
+
try {
|
|
878
|
+
const raw = fn(callJson);
|
|
879
|
+
if (typeof raw !== 'string' || !raw) {
|
|
880
|
+
return fail('empty result');
|
|
881
|
+
}
|
|
882
|
+
const parsed = parseJson(raw);
|
|
883
|
+
if (parsed === null) {
|
|
884
|
+
return null;
|
|
885
|
+
}
|
|
886
|
+
return typeof parsed === 'string' ? parsed : fail('invalid payload');
|
|
887
|
+
}
|
|
888
|
+
catch (error) {
|
|
889
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
890
|
+
return fail(reason);
|
|
891
|
+
}
|
|
86
892
|
}
|
|
87
|
-
export function
|
|
88
|
-
const capability = '
|
|
893
|
+
export function normalizeIdValueWithNative(value, forceGenerate = false) {
|
|
894
|
+
const capability = 'normalizeIdValueJson';
|
|
89
895
|
const fail = (reason) => failNativeRequired(capability, reason);
|
|
90
896
|
if (isNativeDisabledByEnv()) {
|
|
91
897
|
return fail('native disabled');
|
|
@@ -94,25 +900,25 @@ export function parseLenientJsonishWithNative(value) {
|
|
|
94
900
|
if (!fn) {
|
|
95
901
|
return fail();
|
|
96
902
|
}
|
|
97
|
-
const
|
|
98
|
-
if (!
|
|
903
|
+
const payloadJson = safeStringify({ value, forceGenerate });
|
|
904
|
+
if (!payloadJson) {
|
|
99
905
|
return fail('json stringify failed');
|
|
100
906
|
}
|
|
101
907
|
try {
|
|
102
|
-
const raw = fn(
|
|
908
|
+
const raw = fn(payloadJson);
|
|
103
909
|
if (typeof raw !== 'string' || !raw) {
|
|
104
910
|
return fail('empty result');
|
|
105
911
|
}
|
|
106
912
|
const parsed = parseJson(raw);
|
|
107
|
-
return parsed ===
|
|
913
|
+
return typeof parsed === 'string' ? parsed : fail('invalid payload');
|
|
108
914
|
}
|
|
109
915
|
catch (error) {
|
|
110
916
|
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
111
917
|
return fail(reason);
|
|
112
918
|
}
|
|
113
919
|
}
|
|
114
|
-
export function
|
|
115
|
-
const capability = '
|
|
920
|
+
export function extractToolCallIdWithNative(obj) {
|
|
921
|
+
const capability = 'extractToolCallIdJson';
|
|
116
922
|
const fail = (reason) => failNativeRequired(capability, reason);
|
|
117
923
|
if (isNativeDisabledByEnv()) {
|
|
118
924
|
return fail('native disabled');
|
|
@@ -121,24 +927,25 @@ export function repairArgumentsToStringWithNative(value) {
|
|
|
121
927
|
if (!fn) {
|
|
122
928
|
return fail();
|
|
123
929
|
}
|
|
124
|
-
const
|
|
125
|
-
if (!
|
|
930
|
+
const payloadJson = safeStringify({ obj: obj ?? null });
|
|
931
|
+
if (!payloadJson) {
|
|
126
932
|
return fail('json stringify failed');
|
|
127
933
|
}
|
|
128
934
|
try {
|
|
129
|
-
const raw = fn(
|
|
130
|
-
if (typeof raw !== 'string') {
|
|
131
|
-
return fail('
|
|
935
|
+
const raw = fn(payloadJson);
|
|
936
|
+
if (typeof raw !== 'string' || !raw) {
|
|
937
|
+
return fail('empty result');
|
|
132
938
|
}
|
|
133
|
-
|
|
939
|
+
const parsed = parseJson(raw);
|
|
940
|
+
return typeof parsed === 'string' ? parsed : undefined;
|
|
134
941
|
}
|
|
135
942
|
catch (error) {
|
|
136
943
|
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
137
944
|
return fail(reason);
|
|
138
945
|
}
|
|
139
946
|
}
|
|
140
|
-
export function
|
|
141
|
-
const capability = '
|
|
947
|
+
export function createToolCallIdTransformerWithNative(style) {
|
|
948
|
+
const capability = 'createToolCallIdTransformerJson';
|
|
142
949
|
const fail = (reason) => failNativeRequired(capability, reason);
|
|
143
950
|
if (isNativeDisabledByEnv()) {
|
|
144
951
|
return fail('native disabled');
|
|
@@ -147,12 +954,16 @@ export function extractToolCallsFromReasoningTextWithNative(text, idPrefix) {
|
|
|
147
954
|
if (!fn) {
|
|
148
955
|
return fail();
|
|
149
956
|
}
|
|
957
|
+
const payloadJson = safeStringify({ style });
|
|
958
|
+
if (!payloadJson) {
|
|
959
|
+
return fail('json stringify failed');
|
|
960
|
+
}
|
|
150
961
|
try {
|
|
151
|
-
const raw = fn(
|
|
962
|
+
const raw = fn(payloadJson);
|
|
152
963
|
if (typeof raw !== 'string' || !raw) {
|
|
153
964
|
return fail('empty result');
|
|
154
965
|
}
|
|
155
|
-
const parsed =
|
|
966
|
+
const parsed = parseRecord(raw);
|
|
156
967
|
return parsed ?? fail('invalid payload');
|
|
157
968
|
}
|
|
158
969
|
catch (error) {
|
|
@@ -160,8 +971,8 @@ export function extractToolCallsFromReasoningTextWithNative(text, idPrefix) {
|
|
|
160
971
|
return fail(reason);
|
|
161
972
|
}
|
|
162
973
|
}
|
|
163
|
-
export function
|
|
164
|
-
const capability = '
|
|
974
|
+
export function transformToolCallIdWithNative(state, id) {
|
|
975
|
+
const capability = 'transformToolCallIdJson';
|
|
165
976
|
const fail = (reason) => failNativeRequired(capability, reason);
|
|
166
977
|
if (isNativeDisabledByEnv()) {
|
|
167
978
|
return fail('native disabled');
|
|
@@ -170,7 +981,7 @@ export function mapChatToolsToBridgeWithNative(rawTools) {
|
|
|
170
981
|
if (!fn) {
|
|
171
982
|
return fail();
|
|
172
983
|
}
|
|
173
|
-
const payloadJson = safeStringify(
|
|
984
|
+
const payloadJson = safeStringify({ state, id });
|
|
174
985
|
if (!payloadJson) {
|
|
175
986
|
return fail('json stringify failed');
|
|
176
987
|
}
|
|
@@ -179,19 +990,19 @@ export function mapChatToolsToBridgeWithNative(rawTools) {
|
|
|
179
990
|
if (typeof raw !== 'string' || !raw) {
|
|
180
991
|
return fail('empty result');
|
|
181
992
|
}
|
|
182
|
-
const parsed =
|
|
183
|
-
if (!parsed) {
|
|
993
|
+
const parsed = parseRecord(raw);
|
|
994
|
+
if (!parsed || typeof parsed.id !== 'string' || !parsed.state || typeof parsed.state !== 'object') {
|
|
184
995
|
return fail('invalid payload');
|
|
185
996
|
}
|
|
186
|
-
return parsed
|
|
997
|
+
return parsed;
|
|
187
998
|
}
|
|
188
999
|
catch (error) {
|
|
189
1000
|
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
190
1001
|
return fail(reason);
|
|
191
1002
|
}
|
|
192
1003
|
}
|
|
193
|
-
export function
|
|
194
|
-
const capability = '
|
|
1004
|
+
export function enforceToolCallIdStyleWithNative(messages, state) {
|
|
1005
|
+
const capability = 'enforceToolCallIdStyleJson';
|
|
195
1006
|
const fail = (reason) => failNativeRequired(capability, reason);
|
|
196
1007
|
if (isNativeDisabledByEnv()) {
|
|
197
1008
|
return fail('native disabled');
|
|
@@ -200,20 +1011,28 @@ export function sanitizeReasoningTaggedTextWithNative(text) {
|
|
|
200
1011
|
if (!fn) {
|
|
201
1012
|
return fail();
|
|
202
1013
|
}
|
|
1014
|
+
const payloadJson = safeStringify({ messages: Array.isArray(messages) ? messages : [], state });
|
|
1015
|
+
if (!payloadJson) {
|
|
1016
|
+
return fail('json stringify failed');
|
|
1017
|
+
}
|
|
203
1018
|
try {
|
|
204
|
-
const raw = fn(
|
|
205
|
-
if (typeof raw !== 'string') {
|
|
1019
|
+
const raw = fn(payloadJson);
|
|
1020
|
+
if (typeof raw !== 'string' || !raw) {
|
|
1021
|
+
return fail('empty result');
|
|
1022
|
+
}
|
|
1023
|
+
const parsed = parseRecord(raw);
|
|
1024
|
+
if (!parsed || !Array.isArray(parsed.messages) || !parsed.state || typeof parsed.state !== 'object') {
|
|
206
1025
|
return fail('invalid payload');
|
|
207
1026
|
}
|
|
208
|
-
return
|
|
1027
|
+
return parsed;
|
|
209
1028
|
}
|
|
210
1029
|
catch (error) {
|
|
211
1030
|
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
212
1031
|
return fail(reason);
|
|
213
1032
|
}
|
|
214
1033
|
}
|
|
215
|
-
export function
|
|
216
|
-
const capability = '
|
|
1034
|
+
export function normalizeResponsesToolCallIdsWithNative(payload) {
|
|
1035
|
+
const capability = 'normalizeResponsesToolCallIdsJson';
|
|
217
1036
|
const fail = (reason) => failNativeRequired(capability, reason);
|
|
218
1037
|
if (isNativeDisabledByEnv()) {
|
|
219
1038
|
return fail('native disabled');
|
|
@@ -222,7 +1041,7 @@ export function ensureBridgeInstructionsWithNative(payload) {
|
|
|
222
1041
|
if (!fn) {
|
|
223
1042
|
return fail();
|
|
224
1043
|
}
|
|
225
|
-
const payloadJson = safeStringify(payload ??
|
|
1044
|
+
const payloadJson = safeStringify(payload ?? null);
|
|
226
1045
|
if (!payloadJson) {
|
|
227
1046
|
return fail('json stringify failed');
|
|
228
1047
|
}
|
|
@@ -231,16 +1050,15 @@ export function ensureBridgeInstructionsWithNative(payload) {
|
|
|
231
1050
|
if (typeof raw !== 'string' || !raw) {
|
|
232
1051
|
return fail('empty result');
|
|
233
1052
|
}
|
|
234
|
-
|
|
235
|
-
return parsed ?? fail('invalid payload');
|
|
1053
|
+
return parseRecord(raw) ?? fail('invalid payload');
|
|
236
1054
|
}
|
|
237
1055
|
catch (error) {
|
|
238
1056
|
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
239
1057
|
return fail(reason);
|
|
240
1058
|
}
|
|
241
1059
|
}
|
|
242
|
-
export function
|
|
243
|
-
const capability = '
|
|
1060
|
+
export function resolveToolCallIdStyleWithNative(metadata) {
|
|
1061
|
+
const capability = 'resolveToolCallIdStyleJson';
|
|
244
1062
|
const fail = (reason) => failNativeRequired(capability, reason);
|
|
245
1063
|
if (isNativeDisabledByEnv()) {
|
|
246
1064
|
return fail('native disabled');
|
|
@@ -249,19 +1067,16 @@ export function deriveToolCallKeyWithNative(call) {
|
|
|
249
1067
|
if (!fn) {
|
|
250
1068
|
return fail();
|
|
251
1069
|
}
|
|
252
|
-
const
|
|
253
|
-
if (!
|
|
1070
|
+
const metadataJson = safeStringify(metadata ?? null);
|
|
1071
|
+
if (!metadataJson) {
|
|
254
1072
|
return fail('json stringify failed');
|
|
255
1073
|
}
|
|
256
1074
|
try {
|
|
257
|
-
const raw = fn(
|
|
1075
|
+
const raw = fn(metadataJson);
|
|
258
1076
|
if (typeof raw !== 'string' || !raw) {
|
|
259
1077
|
return fail('empty result');
|
|
260
1078
|
}
|
|
261
1079
|
const parsed = parseJson(raw);
|
|
262
|
-
if (parsed === null) {
|
|
263
|
-
return null;
|
|
264
|
-
}
|
|
265
1080
|
return typeof parsed === 'string' ? parsed : fail('invalid payload');
|
|
266
1081
|
}
|
|
267
1082
|
catch (error) {
|
|
@@ -269,6 +1084,32 @@ export function deriveToolCallKeyWithNative(call) {
|
|
|
269
1084
|
return fail(reason);
|
|
270
1085
|
}
|
|
271
1086
|
}
|
|
1087
|
+
export function stripInternalToolingMetadataWithNative(metadata) {
|
|
1088
|
+
const capability = 'stripInternalToolingMetadataJson';
|
|
1089
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
1090
|
+
if (isNativeDisabledByEnv()) {
|
|
1091
|
+
return fail('native disabled');
|
|
1092
|
+
}
|
|
1093
|
+
const fn = readNativeFunction(capability);
|
|
1094
|
+
if (!fn) {
|
|
1095
|
+
return fail();
|
|
1096
|
+
}
|
|
1097
|
+
const metadataJson = safeStringify(metadata ?? null);
|
|
1098
|
+
if (!metadataJson) {
|
|
1099
|
+
return fail('json stringify failed');
|
|
1100
|
+
}
|
|
1101
|
+
try {
|
|
1102
|
+
const raw = fn(metadataJson);
|
|
1103
|
+
if (typeof raw !== 'string' || !raw) {
|
|
1104
|
+
return fail('empty result');
|
|
1105
|
+
}
|
|
1106
|
+
return parseRecord(raw) ?? fail('invalid payload');
|
|
1107
|
+
}
|
|
1108
|
+
catch (error) {
|
|
1109
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
1110
|
+
return fail(reason);
|
|
1111
|
+
}
|
|
1112
|
+
}
|
|
272
1113
|
export function buildProviderProtocolErrorWithNative(input) {
|
|
273
1114
|
const capability = 'buildProviderProtocolErrorJson';
|
|
274
1115
|
const fail = (reason) => failNativeRequired(capability, reason);
|
|
@@ -449,6 +1290,67 @@ export function mapReasoningContentToResponsesOutputWithNative(reasoningContent)
|
|
|
449
1290
|
return fail(reason);
|
|
450
1291
|
}
|
|
451
1292
|
}
|
|
1293
|
+
export function validateToolArgumentsWithNative(toolName, args) {
|
|
1294
|
+
const capability = 'validateToolArgumentsJson';
|
|
1295
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
1296
|
+
if (isNativeDisabledByEnv()) {
|
|
1297
|
+
return fail('native disabled');
|
|
1298
|
+
}
|
|
1299
|
+
const fn = readNativeFunction(capability);
|
|
1300
|
+
if (!fn) {
|
|
1301
|
+
return fail();
|
|
1302
|
+
}
|
|
1303
|
+
const payloadJson = safeStringify({ toolName, args: args ?? null });
|
|
1304
|
+
if (!payloadJson) {
|
|
1305
|
+
return fail('json stringify failed');
|
|
1306
|
+
}
|
|
1307
|
+
try {
|
|
1308
|
+
const raw = fn(payloadJson);
|
|
1309
|
+
if (typeof raw !== 'string' || !raw) {
|
|
1310
|
+
return fail('empty result');
|
|
1311
|
+
}
|
|
1312
|
+
const parsed = parseRecord(raw);
|
|
1313
|
+
if (!parsed || typeof parsed.repaired !== 'string' || typeof parsed.success !== 'boolean') {
|
|
1314
|
+
return fail('invalid payload');
|
|
1315
|
+
}
|
|
1316
|
+
const error = typeof parsed.error === 'string' ? parsed.error : undefined;
|
|
1317
|
+
return { repaired: parsed.repaired, success: parsed.success, ...(error ? { error } : {}) };
|
|
1318
|
+
}
|
|
1319
|
+
catch (error) {
|
|
1320
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
1321
|
+
return fail(reason);
|
|
1322
|
+
}
|
|
1323
|
+
}
|
|
1324
|
+
export function repairToolCallsWithNative(toolCalls) {
|
|
1325
|
+
const capability = 'repairToolCallsJson';
|
|
1326
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
1327
|
+
if (isNativeDisabledByEnv()) {
|
|
1328
|
+
return fail('native disabled');
|
|
1329
|
+
}
|
|
1330
|
+
const fn = readNativeFunction(capability);
|
|
1331
|
+
if (!fn) {
|
|
1332
|
+
return fail();
|
|
1333
|
+
}
|
|
1334
|
+
const payloadJson = safeStringify(Array.isArray(toolCalls) ? toolCalls : []);
|
|
1335
|
+
if (!payloadJson) {
|
|
1336
|
+
return fail('json stringify failed');
|
|
1337
|
+
}
|
|
1338
|
+
try {
|
|
1339
|
+
const raw = fn(payloadJson);
|
|
1340
|
+
if (typeof raw !== 'string' || !raw) {
|
|
1341
|
+
return fail('empty result');
|
|
1342
|
+
}
|
|
1343
|
+
const parsed = parseArray(raw);
|
|
1344
|
+
if (!parsed) {
|
|
1345
|
+
return fail('invalid payload');
|
|
1346
|
+
}
|
|
1347
|
+
return parsed.filter((entry) => entry && typeof entry === 'object' && !Array.isArray(entry) && typeof entry.arguments === 'string');
|
|
1348
|
+
}
|
|
1349
|
+
catch (error) {
|
|
1350
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
1351
|
+
return fail(reason);
|
|
1352
|
+
}
|
|
1353
|
+
}
|
|
452
1354
|
export function isImagePathWithNative(pathValue) {
|
|
453
1355
|
const capability = 'isImagePathJson';
|
|
454
1356
|
const fail = (reason) => failNativeRequired(capability, reason);
|
|
@@ -476,6 +1378,42 @@ export function isImagePathWithNative(pathValue) {
|
|
|
476
1378
|
return fail(reason);
|
|
477
1379
|
}
|
|
478
1380
|
}
|
|
1381
|
+
export function extractStreamingToolCallsWithNative(input) {
|
|
1382
|
+
const capability = 'extractStreamingToolCallsJson';
|
|
1383
|
+
const fail = (reason) => failNativeRequired(capability, reason);
|
|
1384
|
+
if (isNativeDisabledByEnv()) {
|
|
1385
|
+
return fail('native disabled');
|
|
1386
|
+
}
|
|
1387
|
+
const fn = readNativeFunction(capability);
|
|
1388
|
+
if (!fn) {
|
|
1389
|
+
return fail();
|
|
1390
|
+
}
|
|
1391
|
+
const payloadJson = safeStringify(input ?? {});
|
|
1392
|
+
if (!payloadJson) {
|
|
1393
|
+
return fail('json stringify failed');
|
|
1394
|
+
}
|
|
1395
|
+
try {
|
|
1396
|
+
const raw = fn(payloadJson);
|
|
1397
|
+
if (typeof raw !== 'string' || !raw) {
|
|
1398
|
+
return fail('empty result');
|
|
1399
|
+
}
|
|
1400
|
+
const parsed = parseRecord(raw);
|
|
1401
|
+
if (!parsed) {
|
|
1402
|
+
return fail('invalid payload');
|
|
1403
|
+
}
|
|
1404
|
+
const buffer = typeof parsed.buffer === 'string' ? parsed.buffer : '';
|
|
1405
|
+
const idCounter = typeof parsed.idCounter === 'number' ? parsed.idCounter : input.idCounter;
|
|
1406
|
+
const toolCalls = Array.isArray(parsed.toolCalls)
|
|
1407
|
+
? parsed.toolCalls.filter((entry) => entry && typeof entry === 'object' && !Array.isArray(entry))
|
|
1408
|
+
.map((entry) => entry)
|
|
1409
|
+
: [];
|
|
1410
|
+
return { buffer, idCounter, toolCalls };
|
|
1411
|
+
}
|
|
1412
|
+
catch (error) {
|
|
1413
|
+
const reason = error instanceof Error ? error.message : String(error ?? 'unknown');
|
|
1414
|
+
return fail(reason);
|
|
1415
|
+
}
|
|
1416
|
+
}
|
|
479
1417
|
export function isCompactionRequestWithNative(payload) {
|
|
480
1418
|
const capability = 'isCompactionRequestJson';
|
|
481
1419
|
const fail = (reason) => failNativeRequired(capability, reason);
|