@onlyflows/servicenow-mcp 2.3.0-dev.1 → 2.3.0-dev.3
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/CHANGELOG.md +447 -3
- package/README.md +241 -11
- package/dist/call-metrics.d.ts +42 -0
- package/dist/call-metrics.d.ts.map +1 -0
- package/dist/call-metrics.js +89 -0
- package/dist/call-metrics.js.map +1 -0
- package/dist/client.d.ts +18 -14
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +51 -36
- package/dist/client.js.map +1 -1
- package/dist/code-search-snippet.d.ts +25 -0
- package/dist/code-search-snippet.d.ts.map +1 -0
- package/dist/code-search-snippet.js +67 -0
- package/dist/code-search-snippet.js.map +1 -0
- package/dist/code-search-targets.d.ts +66 -0
- package/dist/code-search-targets.d.ts.map +1 -0
- package/dist/code-search-targets.js +82 -0
- package/dist/code-search-targets.js.map +1 -0
- package/dist/encoded-query-policy.d.ts +9 -0
- package/dist/encoded-query-policy.d.ts.map +1 -1
- package/dist/encoded-query-policy.js +19 -1
- package/dist/encoded-query-policy.js.map +1 -1
- package/dist/field-policy.d.ts.map +1 -1
- package/dist/field-policy.js +17 -5
- package/dist/field-policy.js.map +1 -1
- package/dist/http-observability.js +1 -0
- package/dist/http-observability.js.map +1 -1
- package/dist/http-runtime.js +16 -0
- package/dist/http-runtime.js.map +1 -1
- package/dist/metadata-cache.d.ts +97 -7
- package/dist/metadata-cache.d.ts.map +1 -1
- package/dist/metadata-cache.js +284 -21
- package/dist/metadata-cache.js.map +1 -1
- package/dist/policy-card.js +1 -1
- package/dist/policy-card.js.map +1 -1
- package/dist/response-too-large.d.ts +23 -0
- package/dist/response-too-large.d.ts.map +1 -0
- package/dist/response-too-large.js +42 -0
- package/dist/response-too-large.js.map +1 -0
- package/dist/risky-grants.d.ts.map +1 -1
- package/dist/risky-grants.js +1 -0
- package/dist/risky-grants.js.map +1 -1
- package/dist/setup-prompts.js +1 -1
- package/dist/setup-prompts.js.map +1 -1
- package/dist/setup.d.ts.map +1 -1
- package/dist/setup.js +28 -4
- package/dist/setup.js.map +1 -1
- package/dist/structured-query.d.ts +61 -2
- package/dist/structured-query.d.ts.map +1 -1
- package/dist/structured-query.js +376 -35
- package/dist/structured-query.js.map +1 -1
- package/dist/table-policy.d.ts +42 -2
- package/dist/table-policy.d.ts.map +1 -1
- package/dist/table-policy.js +90 -6
- package/dist/table-policy.js.map +1 -1
- package/dist/tool-error.d.ts +40 -4
- package/dist/tool-error.d.ts.map +1 -1
- package/dist/tool-error.js +81 -4
- package/dist/tool-error.js.map +1 -1
- package/dist/tool-table-access.d.ts +9 -2
- package/dist/tool-table-access.d.ts.map +1 -1
- package/dist/tool-table-access.js +93 -30
- package/dist/tool-table-access.js.map +1 -1
- package/dist/tools/batch.d.ts.map +1 -1
- package/dist/tools/batch.js +21 -5
- package/dist/tools/batch.js.map +1 -1
- package/dist/tools/catalog.d.ts.map +1 -1
- package/dist/tools/catalog.js +2 -1
- package/dist/tools/catalog.js.map +1 -1
- package/dist/tools/codesearch.d.ts +2 -2
- package/dist/tools/codesearch.d.ts.map +1 -1
- package/dist/tools/codesearch.js +536 -67
- package/dist/tools/codesearch.js.map +1 -1
- package/dist/tools/flow-view-contracts.d.ts +2 -2
- package/dist/tools/flow-view-module.js +1 -1
- package/dist/tools/flow-view-module.js.map +1 -1
- package/dist/tools/get-module.js +1 -1
- package/dist/tools/get-module.js.map +1 -1
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js +37 -13
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/query-module.d.ts.map +1 -1
- package/dist/tools/query-module.js +230 -63
- package/dist/tools/query-module.js.map +1 -1
- package/dist/tools/result-envelope.d.ts +313 -2
- package/dist/tools/result-envelope.d.ts.map +1 -1
- package/dist/tools/result-envelope.js +334 -33
- package/dist/tools/result-envelope.js.map +1 -1
- package/dist/tools/schema-module.d.ts.map +1 -1
- package/dist/tools/schema-module.js +1 -1
- package/dist/tools/schema-module.js.map +1 -1
- package/dist/tools/truncation-hint.d.ts +125 -0
- package/dist/tools/truncation-hint.d.ts.map +1 -0
- package/dist/tools/truncation-hint.js +298 -0
- package/dist/tools/truncation-hint.js.map +1 -0
- package/dist/unknown-filter-fields.d.ts +52 -0
- package/dist/unknown-filter-fields.d.ts.map +1 -0
- package/dist/unknown-filter-fields.js +87 -0
- package/dist/unknown-filter-fields.js.map +1 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/docs/CLIENT-SETUP.md +22 -1
- package/docs/PRODUCTION-SECURITY.md +21 -1
- package/docs/V2-MIGRATION.md +1 -1
- package/package.json +1 -1
- package/server.json +2 -2
|
@@ -4,6 +4,8 @@ exports.productionToolOutputSchemas = exports.ENVELOPE_HINT_KEY = exports.result
|
|
|
4
4
|
exports.withStructuredResultEnvelope = withStructuredResultEnvelope;
|
|
5
5
|
exports.isProductionToolName = isProductionToolName;
|
|
6
6
|
exports.registerEnvelopeTextRenderer = registerEnvelopeTextRenderer;
|
|
7
|
+
exports.withoutCallMetadata = withoutCallMetadata;
|
|
8
|
+
exports.withCallMetadata = withCallMetadata;
|
|
7
9
|
exports.envelopeCompatibilityResult = envelopeCompatibilityResult;
|
|
8
10
|
exports.finalizeEnvelopeResult = finalizeEnvelopeResult;
|
|
9
11
|
const atf_contracts_js_1 = require("./atf-contracts.js");
|
|
@@ -13,6 +15,7 @@ const batch_limits_js_1 = require("../batch-limits.js");
|
|
|
13
15
|
const field_policy_js_1 = require("../field-policy.js");
|
|
14
16
|
const utils_js_1 = require("../utils.js");
|
|
15
17
|
const markdown_js_1 = require("./markdown.js");
|
|
18
|
+
const truncation_hint_js_1 = require("./truncation-hint.js");
|
|
16
19
|
const tool_module_js_1 = require("./tool-module.js");
|
|
17
20
|
exports.DEFAULT_RESULT_RECORD_LIMIT = 1_000;
|
|
18
21
|
exports.DEFAULT_RESULT_BYTE_LIMIT = utils_js_1.MAX_RESPONSE_BYTES_DEFAULT;
|
|
@@ -34,11 +37,18 @@ const offsetPaginationSchema = zod_1.z
|
|
|
34
37
|
})
|
|
35
38
|
.strict()
|
|
36
39
|
.superRefine((value, context) => {
|
|
40
|
+
// has_more means rows exist that this response did not contain. Normally
|
|
41
|
+
// next_offset points at the first of them. When rows were dropped for
|
|
42
|
+
// size and not one fit, no offset can advance: `recovery` then marks that
|
|
43
|
+
// the request itself must change, and next_offset is left out.
|
|
37
44
|
if (value.has_more &&
|
|
38
|
-
(value.next_offset === undefined
|
|
45
|
+
(value.next_offset === undefined
|
|
46
|
+
? value.recovery === undefined
|
|
47
|
+
: value.next_offset <= value.offset)) {
|
|
39
48
|
context.addIssue({
|
|
40
49
|
code: zod_1.z.ZodIssueCode.custom,
|
|
41
|
-
message: "next_offset must advance beyond offset when has_more is true"
|
|
50
|
+
message: "next_offset must advance beyond offset when has_more is true, " +
|
|
51
|
+
"unless recovery says the request must change",
|
|
42
52
|
path: ["next_offset"],
|
|
43
53
|
});
|
|
44
54
|
}
|
|
@@ -55,6 +65,29 @@ exports.resultPaginationSchema = zod_1.z.union([
|
|
|
55
65
|
zod_1.z.object({ mode: zod_1.z.literal("none") }).strict(),
|
|
56
66
|
offsetPaginationSchema,
|
|
57
67
|
]);
|
|
68
|
+
/**
|
|
69
|
+
* What a call reports about itself (see `call-metrics.ts`): one compact
|
|
70
|
+
* definition, shared by every tool's output schema. `metadata.cache` and
|
|
71
|
+
* `metadata.timing` are both optional: a handler builds its envelope without
|
|
72
|
+
* them and the dispatcher adds them, and `cache` is absent when the call made
|
|
73
|
+
* no cacheable read.
|
|
74
|
+
*/
|
|
75
|
+
// Validation still bounds these to safe integers; only the advertised
|
|
76
|
+
// `maximum` (2^53 - 1, 81 characters per tool in `tools/list`) is left out,
|
|
77
|
+
// since no duration comes near it. The undefined meta value removes the key.
|
|
78
|
+
const wholeMilliseconds = nonNegativeInteger.meta({ maximum: undefined });
|
|
79
|
+
const callTimingSchema = zod_1.z
|
|
80
|
+
.object({
|
|
81
|
+
total_ms: wholeMilliseconds,
|
|
82
|
+
upstream_ms: wholeMilliseconds,
|
|
83
|
+
watermark_ms: wholeMilliseconds,
|
|
84
|
+
})
|
|
85
|
+
.strict();
|
|
86
|
+
const callMetadataShape = {
|
|
87
|
+
cache: zod_1.z.enum(["hit", "miss", "bypass", "mixed"]).optional(),
|
|
88
|
+
timing: callTimingSchema.optional(),
|
|
89
|
+
};
|
|
90
|
+
const callMetadataSchema = zod_1.z.object(callMetadataShape).strict();
|
|
58
91
|
exports.resultMetadataSchema = zod_1.z
|
|
59
92
|
.object({
|
|
60
93
|
kind: zod_1.z.enum(["single", "collection", "operation"]),
|
|
@@ -77,6 +110,7 @@ exports.resultMetadataSchema = zod_1.z
|
|
|
77
110
|
dropped_records: nonNegativeInteger.optional(),
|
|
78
111
|
})
|
|
79
112
|
.strict(),
|
|
113
|
+
...callMetadataShape,
|
|
80
114
|
})
|
|
81
115
|
.strict();
|
|
82
116
|
function withStructuredResultEnvelope(dataSchema) {
|
|
@@ -524,7 +558,10 @@ function effectiveOrder(name, args) {
|
|
|
524
558
|
case "sn_relationships":
|
|
525
559
|
return ["depth_first", "relationship_sys_id"];
|
|
526
560
|
case "sn_codesearch":
|
|
527
|
-
|
|
561
|
+
// One table: its own sys_id order. Several: each table's first match
|
|
562
|
+
// (script includes first), then each table's second, and so on; `round`
|
|
563
|
+
// is that turn, a position and not a field of the rows.
|
|
564
|
+
return typeof args.table === "string" ? ["sys_id"] : ["round", "table", "sys_id"];
|
|
528
565
|
case "sn_discover":
|
|
529
566
|
return args.type === "apps" ? ["source", "sys_id"] : ["sys_id"];
|
|
530
567
|
case "sn_aggregate":
|
|
@@ -550,7 +587,28 @@ function synchronizeRecordCount(data, count) {
|
|
|
550
587
|
}
|
|
551
588
|
}
|
|
552
589
|
}
|
|
553
|
-
|
|
590
|
+
/**
|
|
591
|
+
* The truncation hint a tool wrote, if `data` carries one this module's
|
|
592
|
+
* parser accepts, looked for where {@link synchronizePagingFields} restates it.
|
|
593
|
+
*/
|
|
594
|
+
function truncationHintParts(data, depth = 0) {
|
|
595
|
+
if (typeof data !== "object" || data === null || Array.isArray(data) || depth > 3) {
|
|
596
|
+
return undefined;
|
|
597
|
+
}
|
|
598
|
+
const record = data;
|
|
599
|
+
if (typeof record.hint === "string" && Object.hasOwn(record, "dropped_records")) {
|
|
600
|
+
const parts = (0, truncation_hint_js_1.parseTruncationHint)(record.hint);
|
|
601
|
+
if (parts !== undefined)
|
|
602
|
+
return parts;
|
|
603
|
+
}
|
|
604
|
+
for (const wrapper of ["meta", "request", "result"]) {
|
|
605
|
+
const parts = truncationHintParts(record[wrapper], depth + 1);
|
|
606
|
+
if (parts !== undefined)
|
|
607
|
+
return parts;
|
|
608
|
+
}
|
|
609
|
+
return undefined;
|
|
610
|
+
}
|
|
611
|
+
function synchronizePagingFields(data, pagination, truncation, depth = 0, neededBytes, capBytes, allBytes, withoutAdvice, widestHint) {
|
|
554
612
|
if (pagination.mode !== "offset" ||
|
|
555
613
|
typeof data !== "object" ||
|
|
556
614
|
data === null ||
|
|
@@ -571,13 +629,25 @@ function synchronizePagingFields(data, pagination, truncation, depth = 0) {
|
|
|
571
629
|
target.has_more = pagination.has_more;
|
|
572
630
|
}
|
|
573
631
|
if (pagination.has_more && pagination.next_offset !== undefined) {
|
|
574
|
-
|
|
632
|
+
// A tool that reports has_more reports where to continue, even when
|
|
633
|
+
// the fit created the continuation (rows dropped for size).
|
|
634
|
+
if (Object.hasOwn(target, "next_offset") || Object.hasOwn(target, "has_more")) {
|
|
575
635
|
target.next_offset = pagination.next_offset;
|
|
576
636
|
}
|
|
577
637
|
}
|
|
578
638
|
else if (Object.hasOwn(target, "next_offset")) {
|
|
579
639
|
delete target.next_offset;
|
|
580
640
|
}
|
|
641
|
+
if (pagination.has_more && typeof target.hint === "string") {
|
|
642
|
+
// The tool wrote its continuation from the page it fetched; if the fit
|
|
643
|
+
// dropped rows, the first row not returned is where to continue.
|
|
644
|
+
target.hint = (0, truncation_hint_js_1.restateContinuationHint)(target.hint, {
|
|
645
|
+
offset: pagination.offset,
|
|
646
|
+
...(pagination.next_offset === undefined
|
|
647
|
+
? {}
|
|
648
|
+
: { nextOffset: pagination.next_offset }),
|
|
649
|
+
});
|
|
650
|
+
}
|
|
581
651
|
if (Object.hasOwn(target, "truncated")) {
|
|
582
652
|
target.truncated = truncation.truncated;
|
|
583
653
|
}
|
|
@@ -587,6 +657,20 @@ function synchronizePagingFields(data, pagination, truncation, depth = 0) {
|
|
|
587
657
|
}
|
|
588
658
|
else {
|
|
589
659
|
target.dropped_records = truncation.dropped_records;
|
|
660
|
+
// A tool that explains the drop in a hint wrote it before this fit
|
|
661
|
+
// dropped more rows. Restate it from the final counts and
|
|
662
|
+
// continuation so the text agrees with the fields beside it.
|
|
663
|
+
if (typeof target.hint === "string") {
|
|
664
|
+
target.hint = (0, truncation_hint_js_1.refreshTruncationHint)(target.hint, {
|
|
665
|
+
dropped: truncation.dropped_records,
|
|
666
|
+
returned: pagination.returned,
|
|
667
|
+
...(neededBytes === undefined ? {} : { neededBytes }),
|
|
668
|
+
...(allBytes === undefined ? {} : { allBytes }),
|
|
669
|
+
...(withoutAdvice === true ? { withoutAdvice } : {}),
|
|
670
|
+
...(widestHint === true ? { widest: true } : {}),
|
|
671
|
+
...(capBytes === undefined ? {} : { capBytes }),
|
|
672
|
+
});
|
|
673
|
+
}
|
|
590
674
|
}
|
|
591
675
|
}
|
|
592
676
|
};
|
|
@@ -595,9 +679,77 @@ function synchronizePagingFields(data, pagination, truncation, depth = 0) {
|
|
|
595
679
|
synchronizeObject(record.meta);
|
|
596
680
|
}
|
|
597
681
|
for (const wrapper of ["request", "result"]) {
|
|
598
|
-
synchronizePagingFields(record[wrapper], pagination, truncation, depth + 1);
|
|
682
|
+
synchronizePagingFields(record[wrapper], pagination, truncation, depth + 1, neededBytes, capBytes, allBytes, withoutAdvice, widestHint);
|
|
599
683
|
}
|
|
600
684
|
}
|
|
685
|
+
/**
|
|
686
|
+
* Remove `metadata.cache` and `metadata.timing` from a handler's result. Only
|
|
687
|
+
* the dispatcher reports them, from what it observed, so a value a handler
|
|
688
|
+
* sets is never passed on, whether or not the dispatcher adds its own.
|
|
689
|
+
*/
|
|
690
|
+
function withoutCallMetadata(result) {
|
|
691
|
+
const content = result.structuredContent;
|
|
692
|
+
if (typeof content !== "object" || content === null)
|
|
693
|
+
return result;
|
|
694
|
+
const metadata = content.metadata;
|
|
695
|
+
if (typeof metadata !== "object" || metadata === null)
|
|
696
|
+
return result;
|
|
697
|
+
const rest = { ...metadata };
|
|
698
|
+
delete rest.cache;
|
|
699
|
+
delete rest.timing;
|
|
700
|
+
return { ...result, structuredContent: { ...content, metadata: rest } };
|
|
701
|
+
}
|
|
702
|
+
/**
|
|
703
|
+
* Add what the call reports about itself to a finished result's envelope
|
|
704
|
+
* metadata. The two fields are not counted against the result's byte limit.
|
|
705
|
+
*
|
|
706
|
+
* They are diagnostics, so they never cost the caller anything: the result is
|
|
707
|
+
* fitted to its limit first, exactly as before, with the fit, the paging
|
|
708
|
+
* fields and the truncation hint knowing nothing about them, and the fields
|
|
709
|
+
* are added after. A successful response therefore carries them always, on the
|
|
710
|
+
* calls where the timing matters most (a truncated one is often fitted to
|
|
711
|
+
* within a few bytes of the limit), and may exceed the limit by their size,
|
|
712
|
+
* under 100 bytes. A result that is not an envelope comes back untouched.
|
|
713
|
+
*/
|
|
714
|
+
function withCallMetadata(handlerResult, snapshot) {
|
|
715
|
+
// Whatever the handler put in the two fields never survives, fitted or not.
|
|
716
|
+
const result = withoutCallMetadata(handlerResult);
|
|
717
|
+
const content = result.structuredContent;
|
|
718
|
+
if (typeof content !== "object" || content === null)
|
|
719
|
+
return result;
|
|
720
|
+
const metadata = content.metadata;
|
|
721
|
+
if (typeof metadata !== "object" || metadata === null)
|
|
722
|
+
return result;
|
|
723
|
+
// Only an envelope has limits; anything else is not this function's to annotate.
|
|
724
|
+
const maxBytes = metadata.limits?.max_bytes;
|
|
725
|
+
if (typeof maxBytes !== "number")
|
|
726
|
+
return result;
|
|
727
|
+
const added = callMetadataSchema.safeParse({
|
|
728
|
+
...(snapshot.cache === undefined ? {} : { cache: snapshot.cache }),
|
|
729
|
+
timing: snapshot.timing,
|
|
730
|
+
});
|
|
731
|
+
if (!added.success)
|
|
732
|
+
return result;
|
|
733
|
+
return {
|
|
734
|
+
...result,
|
|
735
|
+
structuredContent: {
|
|
736
|
+
...content,
|
|
737
|
+
metadata: { ...metadata, ...added.data },
|
|
738
|
+
},
|
|
739
|
+
};
|
|
740
|
+
}
|
|
741
|
+
/**
|
|
742
|
+
* The tools whose handler reads one row past the page it was asked for (limit
|
|
743
|
+
* + 1) and hands that row back with the page. It is not a result: it only
|
|
744
|
+
* says another page exists, so the envelope cuts it, reports `has_more`, and
|
|
745
|
+
* does not count it as dropped. A tool not listed here that returns more rows
|
|
746
|
+
* than the limit has rows cut by the limit, and that is reported.
|
|
747
|
+
*/
|
|
748
|
+
const LOOK_AHEAD_TOOLS = new Set([
|
|
749
|
+
"sn_codesearch",
|
|
750
|
+
"sn_discover",
|
|
751
|
+
"sn_aggregate",
|
|
752
|
+
]);
|
|
601
753
|
function envelopeCompatibilityResult(name, args, result) {
|
|
602
754
|
if (result.isError)
|
|
603
755
|
return result;
|
|
@@ -616,14 +768,23 @@ function envelopeCompatibilityResult(name, args, result) {
|
|
|
616
768
|
const explicitNextOffset = numericObjectField(data, "next_offset");
|
|
617
769
|
const legacyTruncated = booleanObjectField(data, "truncated") === true;
|
|
618
770
|
const priorDropped = numericObjectField(data, "dropped_records") ?? 0;
|
|
771
|
+
// A tool that dropped rows for size says so (truncated, dropped_records):
|
|
772
|
+
// rows exist that its response does not hold, so there is more to fetch.
|
|
773
|
+
const droppedForSize = collection && legacyTruncated && priorDropped > 0;
|
|
619
774
|
let hasMore = collection &&
|
|
620
775
|
(originalCount > returned ||
|
|
621
776
|
explicitHasMore === true ||
|
|
777
|
+
droppedForSize ||
|
|
622
778
|
(explicitHasMore === undefined && returned >= limit));
|
|
623
779
|
let nextOffset;
|
|
624
780
|
let recovery = legacyTruncated ? "adjust_request_and_retry_same_offset" : undefined;
|
|
625
781
|
if (hasMore) {
|
|
626
|
-
if (
|
|
782
|
+
if (droppedForSize && returned > 0) {
|
|
783
|
+
// The rows returned are a prefix of the window, so the first row not
|
|
784
|
+
// returned is the one to continue at; nothing is skipped.
|
|
785
|
+
nextOffset = offset + returned;
|
|
786
|
+
}
|
|
787
|
+
else if (explicitNextOffset !== undefined && explicitNextOffset > offset) {
|
|
627
788
|
nextOffset = explicitNextOffset;
|
|
628
789
|
}
|
|
629
790
|
else if (originalCount > returned && returned > 0) {
|
|
@@ -635,12 +796,20 @@ function envelopeCompatibilityResult(name, args, result) {
|
|
|
635
796
|
// the effective limit prevents ACL-filtered windows from being replayed.
|
|
636
797
|
nextOffset = offset + limit;
|
|
637
798
|
}
|
|
799
|
+
else if (droppedForSize) {
|
|
800
|
+
// Not one row fit: has_more stays true, no offset can advance, and the
|
|
801
|
+
// request has to change.
|
|
802
|
+
recovery = "adjust_request_and_retry_same_offset";
|
|
803
|
+
}
|
|
638
804
|
else {
|
|
639
805
|
hasMore = false;
|
|
640
806
|
recovery = "adjust_request_and_retry_same_offset";
|
|
641
807
|
}
|
|
642
808
|
}
|
|
643
|
-
|
|
809
|
+
// The look-ahead row was read to find out whether more exists, not to be
|
|
810
|
+
// returned: it is not a dropped record. Rows past it still are.
|
|
811
|
+
const lookAheadRow = LOOK_AHEAD_TOOLS.has(name) && originalCount > returned ? 1 : 0;
|
|
812
|
+
const droppedByRecordLimit = originalCount - returned - lookAheadRow;
|
|
644
813
|
const droppedRecords = priorDropped + droppedByRecordLimit;
|
|
645
814
|
const truncation = legacyTruncated || droppedByRecordLimit > 0
|
|
646
815
|
? {
|
|
@@ -689,13 +858,20 @@ function isMutableEnvelope(candidate) {
|
|
|
689
858
|
}
|
|
690
859
|
function renderEnvelopeText(envelope) {
|
|
691
860
|
const pagination = envelope.metadata.pagination;
|
|
861
|
+
const noRowFit = pagination.mode === "offset" && pagination.returned === 0;
|
|
692
862
|
const continuation = pagination.mode === "offset" && pagination.has_more
|
|
693
|
-
?
|
|
863
|
+
? pagination.next_offset !== undefined
|
|
864
|
+
? ` More records are available at offset ${pagination.next_offset}.`
|
|
865
|
+
: noRowFit
|
|
866
|
+
? " More records are available, but none fit."
|
|
867
|
+
: " More records are available; this result has no offset to continue at."
|
|
694
868
|
: "";
|
|
695
869
|
const recovery = pagination.mode === "offset" && pagination.recovery !== undefined
|
|
696
|
-
? pagination.has_more
|
|
697
|
-
? `
|
|
698
|
-
:
|
|
870
|
+
? pagination.has_more && pagination.next_offset !== undefined
|
|
871
|
+
? ` Rows were dropped from the current window; next_offset is the first row not returned, or adjust the request and retry offset ${pagination.offset} to get them in one response.`
|
|
872
|
+
: noRowFit
|
|
873
|
+
? ` No row fit; adjust the request (fewer fields, a lower limit or a higher max_response_bytes) and retry offset ${pagination.offset}.`
|
|
874
|
+
: ` Rows were dropped from the current window; adjust the request (fewer fields, a lower limit or a higher max_response_bytes) and retry offset ${pagination.offset}.`
|
|
699
875
|
: "";
|
|
700
876
|
const truncated = envelope.metadata.truncation.truncated
|
|
701
877
|
? ` Output was truncated by ${envelope.metadata.truncation.reason}.`
|
|
@@ -803,7 +979,7 @@ function renderedEnvelopeText(renderer, envelope, context) {
|
|
|
803
979
|
*/
|
|
804
980
|
function finalizeEnvelopeResult(result, tool, options = {}) {
|
|
805
981
|
envelopeTextRenderersSealed = true;
|
|
806
|
-
const summarized = finalizeSummaryResult(result);
|
|
982
|
+
const summarized = finalizeSummaryResult(result, tool, options.maxResponseBytesCap);
|
|
807
983
|
const renderer = tool === undefined ? undefined : ENVELOPE_TEXT_RENDERERS.get(tool);
|
|
808
984
|
if (!summarized ||
|
|
809
985
|
!renderer ||
|
|
@@ -827,20 +1003,75 @@ function finalizeEnvelopeResult(result, tool, options = {}) {
|
|
|
827
1003
|
? rendered
|
|
828
1004
|
: summarized;
|
|
829
1005
|
}
|
|
830
|
-
|
|
1006
|
+
/**
|
|
1007
|
+
* The `max_response_bytes` to name for the whole window: what the tool
|
|
1008
|
+
* measured for all its rows, plus what the envelope adds around them. Null
|
|
1009
|
+
* when the tool named none (the window exceeded the maximum on its own) or
|
|
1010
|
+
* when the sum exceeds the maximum, so the hint leaves it out.
|
|
1011
|
+
*/
|
|
1012
|
+
function wholeWindowBytes(toolHint, envelopeBytes, current, cap) {
|
|
1013
|
+
if (toolHint.allBytes === undefined)
|
|
1014
|
+
return null;
|
|
1015
|
+
const needed = (0, truncation_hint_js_1.neededResponseBytes)(toolHint.allBytes + envelopeBytes, current, cap);
|
|
1016
|
+
return Number.isFinite(needed) ? needed : null;
|
|
1017
|
+
}
|
|
1018
|
+
/**
|
|
1019
|
+
* The tools whose `offset` argument is a row offset into the collection they
|
|
1020
|
+
* return, so that `offset + rows returned` is the first row not returned. For
|
|
1021
|
+
* these a fit that drops rows for size can offer that offset as
|
|
1022
|
+
* `next_offset`. Any other collection still reports `has_more`, but with no
|
|
1023
|
+
* offset to give: the request has to change.
|
|
1024
|
+
*/
|
|
1025
|
+
const OFFSET_PAGED_TOOLS = new Set([
|
|
1026
|
+
"sn_query",
|
|
1027
|
+
"sn_syslog",
|
|
1028
|
+
"sn_schema",
|
|
1029
|
+
"sn_relationships",
|
|
1030
|
+
"sn_codesearch",
|
|
1031
|
+
"sn_discover",
|
|
1032
|
+
"sn_attach",
|
|
1033
|
+
"sn_atf",
|
|
1034
|
+
]);
|
|
1035
|
+
/**
|
|
1036
|
+
* Fit the result to its byte limit by dropping whole rows from the tail.
|
|
1037
|
+
* Rows dropped for size mean rows exist that the response does not hold, so
|
|
1038
|
+
* `has_more` is true; the rows returned are a prefix of the window, so a
|
|
1039
|
+
* paged tool continues at `offset + rows returned` and loses nothing, and
|
|
1040
|
+
* when no row fits there is no offset to give. With no `tool` (a caller that
|
|
1041
|
+
* does not say which), the offset is offered as for a paged tool.
|
|
1042
|
+
*/
|
|
1043
|
+
function finalizeSummaryResult(result, tool, byteCap = utils_js_1.MAX_RESPONSE_BYTES_MAX) {
|
|
1044
|
+
const offsetPaged = tool === undefined || OFFSET_PAGED_TOOLS.has(tool);
|
|
831
1045
|
if (!isMutableEnvelope(result.structuredContent))
|
|
832
1046
|
return result;
|
|
833
1047
|
const sourceEnvelope = JSON.parse(JSON.stringify(result.structuredContent));
|
|
834
1048
|
let candidate = candidateResult(result, sourceEnvelope);
|
|
835
1049
|
const maximum = sourceEnvelope.metadata.limits.max_bytes;
|
|
836
|
-
|
|
837
|
-
|
|
1050
|
+
// A tool that dropped rows for size wrote the budget that would hold the
|
|
1051
|
+
// next row from what it could measure, the payload alone. Only here is the
|
|
1052
|
+
// rest of the result known.
|
|
1053
|
+
const toolHint = truncationHintParts(sourceEnvelope.data);
|
|
1054
|
+
if (resultBytes(candidate) <= maximum) {
|
|
1055
|
+
if (toolHint === undefined || !Number.isFinite(toolHint.neededBytes))
|
|
1056
|
+
return candidate;
|
|
1057
|
+
const overhead = resultBytes(candidate) - Buffer.byteLength(JSON.stringify(sourceEnvelope.data), "utf8");
|
|
1058
|
+
const restated = JSON.parse(JSON.stringify(sourceEnvelope));
|
|
1059
|
+
synchronizePagingFields(restated.data, restated.metadata.pagination, restated.metadata.truncation, 0, (0, truncation_hint_js_1.neededResponseBytes)(toolHint.neededBytes + overhead, maximum, byteCap), byteCap, wholeWindowBytes(toolHint, overhead, maximum, byteCap));
|
|
1060
|
+
const restatedCandidate = candidateResult(result, restated);
|
|
1061
|
+
// The larger number can add a digit; if that tips the result over its
|
|
1062
|
+
// limit, fit it like any other result that is too big.
|
|
1063
|
+
if (resultBytes(restatedCandidate) <= maximum)
|
|
1064
|
+
return restatedCandidate;
|
|
1065
|
+
}
|
|
838
1066
|
const sourceRecords = collectionArray(sourceEnvelope.data);
|
|
839
1067
|
let envelope = sourceEnvelope;
|
|
840
1068
|
if (sourceRecords) {
|
|
841
1069
|
const originalRecordCount = sourceRecords.length;
|
|
842
1070
|
const priorDropped = sourceEnvelope.metadata.truncation.dropped_records ?? 0;
|
|
843
|
-
|
|
1071
|
+
// While fitting, a hint carries the widest form of its byte budget (the
|
|
1072
|
+
// "exceeds the maximum" clause), which is never shorter than a number, so
|
|
1073
|
+
// a result that fits that way still fits with the real number.
|
|
1074
|
+
const envelopeForCount = (count, neededBytes, allBytes, withoutAdvice, widestHint) => {
|
|
844
1075
|
const probe = JSON.parse(JSON.stringify(sourceEnvelope));
|
|
845
1076
|
const probeRecords = collectionArray(probe.data);
|
|
846
1077
|
if (!probeRecords)
|
|
@@ -851,36 +1082,106 @@ function finalizeSummaryResult(result) {
|
|
|
851
1082
|
if (probe.metadata.pagination.mode === "offset") {
|
|
852
1083
|
probe.metadata.pagination.returned = count;
|
|
853
1084
|
if (count < originalRecordCount) {
|
|
854
|
-
probe.metadata.pagination
|
|
855
|
-
|
|
1085
|
+
const paging = probe.metadata.pagination;
|
|
1086
|
+
paging.recovery = "adjust_request_and_retry_same_offset";
|
|
1087
|
+
paging.has_more = true;
|
|
1088
|
+
if (offsetPaged && count > 0) {
|
|
1089
|
+
paging.next_offset = paging.offset + count;
|
|
1090
|
+
}
|
|
1091
|
+
else {
|
|
1092
|
+
delete paging.next_offset;
|
|
1093
|
+
}
|
|
856
1094
|
}
|
|
857
1095
|
}
|
|
858
1096
|
applyByteTruncation(probe, priorDropped + originalRecordCount - count);
|
|
859
|
-
synchronizePagingFields(probe.data, probe.metadata.pagination, probe.metadata.truncation);
|
|
1097
|
+
synchronizePagingFields(probe.data, probe.metadata.pagination, probe.metadata.truncation, 0, neededBytes, byteCap, allBytes, withoutAdvice, widestHint);
|
|
860
1098
|
return probe;
|
|
861
1099
|
};
|
|
862
|
-
|
|
863
|
-
|
|
1100
|
+
const widest = Number.POSITIVE_INFINITY;
|
|
1101
|
+
// The forms the hint can take while fitting, fullest first. The labelled
|
|
1102
|
+
// hint is longer than the bare one was, and at the smallest budgets the
|
|
1103
|
+
// envelope's own metadata leaves room for little more than the counts: the
|
|
1104
|
+
// page figure can go, then the field advice, which is the least that
|
|
1105
|
+
// still names the next row's budget. A narrower form is used only when no
|
|
1106
|
+
// row, not even zero, fits beside the fuller one. Each form's widest text
|
|
1107
|
+
// (the longest any real figures could make it) is what rows are sized
|
|
1108
|
+
// with, and the real text is rendered in the same form, so it is never
|
|
1109
|
+
// longer than the probe. The budget named for the next row is always
|
|
1110
|
+
// measured with the fullest form, so a call at that budget, which takes
|
|
1111
|
+
// the fullest form that fits, returns the row.
|
|
1112
|
+
const forms = toolHint === undefined
|
|
1113
|
+
? [{ all: widest, withoutAdvice: false }]
|
|
1114
|
+
: [
|
|
1115
|
+
{ all: widest, withoutAdvice: false },
|
|
1116
|
+
{ all: null, withoutAdvice: false },
|
|
1117
|
+
{ all: null, withoutAdvice: true },
|
|
1118
|
+
];
|
|
864
1119
|
let best = -1;
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
1120
|
+
let form = forms[0];
|
|
1121
|
+
for (const candidateForm of forms) {
|
|
1122
|
+
let low = 0;
|
|
1123
|
+
let high = originalRecordCount;
|
|
1124
|
+
let fitted = -1;
|
|
1125
|
+
while (low <= high) {
|
|
1126
|
+
const midpoint = Math.floor((low + high) / 2);
|
|
1127
|
+
const probe = candidateResult(result, envelopeForCount(midpoint, widest, candidateForm.all, candidateForm.withoutAdvice, true));
|
|
1128
|
+
if (resultBytes(probe) <= maximum) {
|
|
1129
|
+
fitted = midpoint;
|
|
1130
|
+
low = midpoint + 1;
|
|
1131
|
+
}
|
|
1132
|
+
else {
|
|
1133
|
+
high = midpoint - 1;
|
|
1134
|
+
}
|
|
874
1135
|
}
|
|
1136
|
+
// The first form that fits (zero rows is enough) is the one used.
|
|
1137
|
+
form = candidateForm;
|
|
1138
|
+
best = fitted;
|
|
1139
|
+
if (best >= 0)
|
|
1140
|
+
break;
|
|
875
1141
|
}
|
|
876
1142
|
// Collections are fitted only by removing complete rows. In particular,
|
|
877
1143
|
// never shorten a field merely to force one row into the envelope: callers
|
|
878
1144
|
// can retry this exact window with a narrower projection/larger budget,
|
|
879
1145
|
// while next_offset (when present) remains the separately authoritative
|
|
880
1146
|
// post-window resume point.
|
|
881
|
-
|
|
1147
|
+
const kept = Math.max(best, 0);
|
|
1148
|
+
envelope = envelopeForCount(kept, widest, form.all, form.withoutAdvice, true);
|
|
882
1149
|
candidate = candidateResult(result, envelope);
|
|
883
|
-
|
|
1150
|
+
if (resultBytes(candidate) > maximum)
|
|
1151
|
+
return undefined;
|
|
1152
|
+
if (toolHint === undefined)
|
|
1153
|
+
return candidate;
|
|
1154
|
+
// The rows the tool already dropped are not here to measure, so the whole
|
|
1155
|
+
// window is its payload size, which the tool measured, plus what this
|
|
1156
|
+
// envelope adds around the rows. The envelope of a whole page carries no
|
|
1157
|
+
// truncation fields, so this is never too small.
|
|
1158
|
+
const overhead = resultBytes(candidate) - Buffer.byteLength(JSON.stringify(envelope.data), "utf8");
|
|
1159
|
+
// The budget that would hold one more row is measured here, with the
|
|
1160
|
+
// envelope's own cost in it, not guessed. When the fit dropped nothing
|
|
1161
|
+
// (the tool had already dropped every row, and only a narrower form
|
|
1162
|
+
// fitted) there is no row of ours to add: it is the tool's figure plus
|
|
1163
|
+
// the envelope's cost, as in the first branch.
|
|
1164
|
+
// With no row returned the envelope carries no `next_offset`, which the
|
|
1165
|
+
// first row brings with it, in the data and in the pagination metadata. The
|
|
1166
|
+
// zero-row overhead would then name a budget a few bytes short of one row.
|
|
1167
|
+
const overheadWithARow = (zeroOrMore, rows, base) => {
|
|
1168
|
+
if (rows > 0 || !offsetPaged)
|
|
1169
|
+
return base;
|
|
1170
|
+
const withRow = JSON.parse(JSON.stringify(zeroOrMore));
|
|
1171
|
+
const paging = withRow.metadata.pagination;
|
|
1172
|
+
if (paging.mode !== "offset")
|
|
1173
|
+
return base;
|
|
1174
|
+
paging.returned = 1;
|
|
1175
|
+
paging.next_offset = paging.offset + 1;
|
|
1176
|
+
synchronizePagingFields(withRow.data, withRow.metadata.pagination, withRow.metadata.truncation, 0, undefined, byteCap);
|
|
1177
|
+
return (resultBytes(candidateResult(result, withRow)) -
|
|
1178
|
+
Buffer.byteLength(JSON.stringify(withRow.data), "utf8"));
|
|
1179
|
+
};
|
|
1180
|
+
const needed = kept >= originalRecordCount
|
|
1181
|
+
? (0, truncation_hint_js_1.neededResponseBytes)(toolHint.neededBytes + overheadWithARow(envelope, kept, overhead), maximum, byteCap)
|
|
1182
|
+
: (0, truncation_hint_js_1.neededResponseBytes)(resultBytes(candidateResult(result, envelopeForCount(kept + 1, widest, forms[0].all, false, true))), maximum, byteCap);
|
|
1183
|
+
const restated = candidateResult(result, envelopeForCount(kept, needed, form.all === null ? null : wholeWindowBytes(toolHint, overhead, maximum, byteCap), form.withoutAdvice));
|
|
1184
|
+
return resultBytes(restated) <= maximum ? restated : candidate;
|
|
884
1185
|
}
|
|
885
1186
|
while (resultBytes(candidate) > maximum && truncateLongestString(envelope.data)) {
|
|
886
1187
|
applyByteTruncation(envelope, envelope.metadata.truncation.dropped_records ?? 0);
|