@cloud411716/fancy-webnovel 4.30.26 → 4.30.27
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/package.json
CHANGED
|
@@ -303,6 +303,82 @@ function textFromBlocks(blocks) {
|
|
|
303
303
|
.join('');
|
|
304
304
|
}
|
|
305
305
|
|
|
306
|
+
/** Find the first complete JSON object in a string, tolerating prose/fences. */
|
|
307
|
+
function extractJsonObject(text) {
|
|
308
|
+
const source = String(text || '');
|
|
309
|
+
for (let start = source.indexOf('{'); start >= 0; start = source.indexOf('{', start + 1)) {
|
|
310
|
+
let depth = 0;
|
|
311
|
+
let inString = false;
|
|
312
|
+
let escaped = false;
|
|
313
|
+
for (let index = start; index < source.length; index += 1) {
|
|
314
|
+
const char = source[index];
|
|
315
|
+
if (inString) {
|
|
316
|
+
if (escaped) escaped = false;
|
|
317
|
+
else if (char === '\\') escaped = true;
|
|
318
|
+
else if (char === '"') inString = false;
|
|
319
|
+
continue;
|
|
320
|
+
}
|
|
321
|
+
if (char === '"') { inString = true; continue; }
|
|
322
|
+
if (char === '{') depth += 1;
|
|
323
|
+
else if (char === '}') {
|
|
324
|
+
depth -= 1;
|
|
325
|
+
if (depth === 0) {
|
|
326
|
+
try { return JSON.parse(source.slice(start, index + 1)); } catch { break; }
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
return undefined;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/** Check the small JSON-Schema subset used by team structured outputs. */
|
|
335
|
+
function matchesSchema(value, schema) {
|
|
336
|
+
if (!schema || typeof schema !== 'object') return true;
|
|
337
|
+
if (schema.type === 'object') {
|
|
338
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) return false;
|
|
339
|
+
for (const key of schema.required || []) {
|
|
340
|
+
if (!(key in value)) return false;
|
|
341
|
+
}
|
|
342
|
+
if (schema.additionalProperties === false) {
|
|
343
|
+
const allowed = new Set(Object.keys(schema.properties || {}));
|
|
344
|
+
for (const key of Object.keys(value)) if (!allowed.has(key)) return false;
|
|
345
|
+
}
|
|
346
|
+
for (const [key, child] of Object.entries(schema.properties || {})) {
|
|
347
|
+
if (key in value && !matchesSchema(value[key], child)) return false;
|
|
348
|
+
}
|
|
349
|
+
return true;
|
|
350
|
+
}
|
|
351
|
+
if (schema.type === 'array') {
|
|
352
|
+
if (!Array.isArray(value)) return false;
|
|
353
|
+
return value.every((item) => matchesSchema(item, schema.items));
|
|
354
|
+
}
|
|
355
|
+
if (schema.type === 'string') {
|
|
356
|
+
if (typeof value !== 'string') return false;
|
|
357
|
+
return !Array.isArray(schema.enum) || schema.enum.includes(value);
|
|
358
|
+
}
|
|
359
|
+
if (schema.type === 'boolean') return typeof value === 'boolean';
|
|
360
|
+
if (schema.type === 'number' || schema.type === 'integer') return typeof value === 'number';
|
|
361
|
+
return true;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/** Recover a structured value when a model emits JSON in a text/tool-call block. */
|
|
365
|
+
function structuredFromBlocks(blocks, schema) {
|
|
366
|
+
const candidates = [];
|
|
367
|
+
for (const block of Array.isArray(blocks) ? blocks : []) {
|
|
368
|
+
if (!block) continue;
|
|
369
|
+
if (block.type === 'text') candidates.push(block.text);
|
|
370
|
+
if (block.type === 'tool-call') {
|
|
371
|
+
if (typeof block.arguments === 'string') candidates.push(block.arguments);
|
|
372
|
+
else if (block.arguments && typeof block.arguments === 'object') candidates.push(JSON.stringify(block.arguments));
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
for (const candidate of candidates) {
|
|
376
|
+
const parsed = extractJsonObject(candidate);
|
|
377
|
+
if (parsed !== undefined && matchesSchema(parsed, schema)) return parsed;
|
|
378
|
+
}
|
|
379
|
+
return undefined;
|
|
380
|
+
}
|
|
381
|
+
|
|
306
382
|
async function callSubagent({ ctx, subagents, parent, prompt, route, label, schema, signal }) {
|
|
307
383
|
let activation;
|
|
308
384
|
try {
|
|
@@ -328,16 +404,24 @@ async function callSubagent({ ctx, subagents, parent, prompt, route, label, sche
|
|
|
328
404
|
if (timer && typeof timer.unref === 'function') timer.unref();
|
|
329
405
|
}),
|
|
330
406
|
]);
|
|
407
|
+
if (schema) {
|
|
408
|
+
const value = result && result.structured !== undefined
|
|
409
|
+
? result.structured
|
|
410
|
+
: structuredFromBlocks(result && result.output, schema);
|
|
411
|
+
if (value !== undefined) return { ok: true, value };
|
|
412
|
+
}
|
|
331
413
|
if (!result || result.stopReason !== 'completed') {
|
|
414
|
+
const preview = textFromBlocks(result && result.output).slice(0, 300);
|
|
332
415
|
const detail = (result && result.diagnostic)
|
|
333
416
|
|| await subagentFailureDetail(ctx, activation.childId)
|
|
417
|
+
|| (preview ? `子 agent 未按 schema 返回结果:${preview}` : '')
|
|
334
418
|
|| (result && result.stopReason)
|
|
335
419
|
|| '子 agent 未完成';
|
|
336
420
|
return { ok: false, error: detail, childId: activation.childId };
|
|
337
421
|
}
|
|
338
422
|
if (schema) {
|
|
339
|
-
|
|
340
|
-
return { ok:
|
|
423
|
+
const preview = textFromBlocks(result.output).slice(0, 300);
|
|
424
|
+
return { ok: false, error: '子 agent 没有返回结构化结果' + (preview ? ':' + preview : ''), childId: activation.childId };
|
|
341
425
|
}
|
|
342
426
|
return { ok: true, value: textFromBlocks(result.output) };
|
|
343
427
|
} catch (err) {
|