@onlyflows/servicenow-mcp 2.3.0-dev.2 → 2.3.0-rc.1
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 +0 -253
- package/README.md +7 -141
- package/dist/client.d.ts +2 -14
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +10 -40
- package/dist/client.js.map +1 -1
- package/dist/encoded-query-policy.d.ts +0 -9
- package/dist/encoded-query-policy.d.ts.map +1 -1
- package/dist/encoded-query-policy.js +1 -19
- package/dist/encoded-query-policy.js.map +1 -1
- package/dist/http-runtime.js +0 -16
- package/dist/http-runtime.js.map +1 -1
- package/dist/metadata-cache.d.ts +7 -97
- package/dist/metadata-cache.d.ts.map +1 -1
- package/dist/metadata-cache.js +21 -283
- package/dist/metadata-cache.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 +1 -10
- package/dist/setup.js.map +1 -1
- package/dist/structured-query.d.ts +2 -61
- package/dist/structured-query.d.ts.map +1 -1
- package/dist/structured-query.js +35 -376
- package/dist/structured-query.js.map +1 -1
- package/dist/tools/codesearch.d.ts.map +1 -1
- package/dist/tools/codesearch.js +4 -24
- package/dist/tools/codesearch.js.map +1 -1
- 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 +11 -24
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/query-module.d.ts.map +1 -1
- package/dist/tools/query-module.js +59 -205
- package/dist/tools/query-module.js.map +1 -1
- package/dist/tools/result-envelope.d.ts +0 -311
- package/dist/tools/result-envelope.d.ts.map +1 -1
- package/dist/tools/result-envelope.js +31 -314
- 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/version.d.ts +1 -1
- package/dist/version.d.ts.map +1 -1
- package/dist/version.js +1 -1
- package/dist/version.js.map +1 -1
- package/docs/CLIENT-SETUP.md +1 -11
- package/docs/PRODUCTION-SECURITY.md +1 -17
- package/package.json +1 -1
- package/server.json +2 -2
- package/dist/call-metrics.d.ts +0 -42
- package/dist/call-metrics.d.ts.map +0 -1
- package/dist/call-metrics.js +0 -89
- package/dist/call-metrics.js.map +0 -1
- package/dist/tools/truncation-hint.d.ts +0 -125
- package/dist/tools/truncation-hint.d.ts.map +0 -1
- package/dist/tools/truncation-hint.js +0 -298
- package/dist/tools/truncation-hint.js.map +0 -1
|
@@ -4,8 +4,6 @@ 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;
|
|
9
7
|
exports.envelopeCompatibilityResult = envelopeCompatibilityResult;
|
|
10
8
|
exports.finalizeEnvelopeResult = finalizeEnvelopeResult;
|
|
11
9
|
const atf_contracts_js_1 = require("./atf-contracts.js");
|
|
@@ -15,7 +13,6 @@ const batch_limits_js_1 = require("../batch-limits.js");
|
|
|
15
13
|
const field_policy_js_1 = require("../field-policy.js");
|
|
16
14
|
const utils_js_1 = require("../utils.js");
|
|
17
15
|
const markdown_js_1 = require("./markdown.js");
|
|
18
|
-
const truncation_hint_js_1 = require("./truncation-hint.js");
|
|
19
16
|
const tool_module_js_1 = require("./tool-module.js");
|
|
20
17
|
exports.DEFAULT_RESULT_RECORD_LIMIT = 1_000;
|
|
21
18
|
exports.DEFAULT_RESULT_BYTE_LIMIT = utils_js_1.MAX_RESPONSE_BYTES_DEFAULT;
|
|
@@ -37,18 +34,11 @@ const offsetPaginationSchema = zod_1.z
|
|
|
37
34
|
})
|
|
38
35
|
.strict()
|
|
39
36
|
.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.
|
|
44
37
|
if (value.has_more &&
|
|
45
|
-
(value.next_offset === undefined
|
|
46
|
-
? value.recovery === undefined
|
|
47
|
-
: value.next_offset <= value.offset)) {
|
|
38
|
+
(value.next_offset === undefined || value.next_offset <= value.offset)) {
|
|
48
39
|
context.addIssue({
|
|
49
40
|
code: zod_1.z.ZodIssueCode.custom,
|
|
50
|
-
message: "next_offset must advance beyond offset when has_more is true,
|
|
51
|
-
"unless recovery says the request must change",
|
|
41
|
+
message: "next_offset must advance beyond offset when has_more is true",
|
|
52
42
|
path: ["next_offset"],
|
|
53
43
|
});
|
|
54
44
|
}
|
|
@@ -65,29 +55,6 @@ exports.resultPaginationSchema = zod_1.z.union([
|
|
|
65
55
|
zod_1.z.object({ mode: zod_1.z.literal("none") }).strict(),
|
|
66
56
|
offsetPaginationSchema,
|
|
67
57
|
]);
|
|
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();
|
|
91
58
|
exports.resultMetadataSchema = zod_1.z
|
|
92
59
|
.object({
|
|
93
60
|
kind: zod_1.z.enum(["single", "collection", "operation"]),
|
|
@@ -110,7 +77,6 @@ exports.resultMetadataSchema = zod_1.z
|
|
|
110
77
|
dropped_records: nonNegativeInteger.optional(),
|
|
111
78
|
})
|
|
112
79
|
.strict(),
|
|
113
|
-
...callMetadataShape,
|
|
114
80
|
})
|
|
115
81
|
.strict();
|
|
116
82
|
function withStructuredResultEnvelope(dataSchema) {
|
|
@@ -584,28 +550,7 @@ function synchronizeRecordCount(data, count) {
|
|
|
584
550
|
}
|
|
585
551
|
}
|
|
586
552
|
}
|
|
587
|
-
|
|
588
|
-
* The truncation hint a tool wrote, if `data` carries one this module's
|
|
589
|
-
* parser accepts, looked for where {@link synchronizePagingFields} restates it.
|
|
590
|
-
*/
|
|
591
|
-
function truncationHintParts(data, depth = 0) {
|
|
592
|
-
if (typeof data !== "object" || data === null || Array.isArray(data) || depth > 3) {
|
|
593
|
-
return undefined;
|
|
594
|
-
}
|
|
595
|
-
const record = data;
|
|
596
|
-
if (typeof record.hint === "string" && Object.hasOwn(record, "dropped_records")) {
|
|
597
|
-
const parts = (0, truncation_hint_js_1.parseTruncationHint)(record.hint);
|
|
598
|
-
if (parts !== undefined)
|
|
599
|
-
return parts;
|
|
600
|
-
}
|
|
601
|
-
for (const wrapper of ["meta", "request", "result"]) {
|
|
602
|
-
const parts = truncationHintParts(record[wrapper], depth + 1);
|
|
603
|
-
if (parts !== undefined)
|
|
604
|
-
return parts;
|
|
605
|
-
}
|
|
606
|
-
return undefined;
|
|
607
|
-
}
|
|
608
|
-
function synchronizePagingFields(data, pagination, truncation, depth = 0, neededBytes, capBytes, allBytes, withoutAdvice, widestHint) {
|
|
553
|
+
function synchronizePagingFields(data, pagination, truncation, depth = 0) {
|
|
609
554
|
if (pagination.mode !== "offset" ||
|
|
610
555
|
typeof data !== "object" ||
|
|
611
556
|
data === null ||
|
|
@@ -626,25 +571,13 @@ function synchronizePagingFields(data, pagination, truncation, depth = 0, needed
|
|
|
626
571
|
target.has_more = pagination.has_more;
|
|
627
572
|
}
|
|
628
573
|
if (pagination.has_more && pagination.next_offset !== undefined) {
|
|
629
|
-
|
|
630
|
-
// the fit created the continuation (rows dropped for size).
|
|
631
|
-
if (Object.hasOwn(target, "next_offset") || Object.hasOwn(target, "has_more")) {
|
|
574
|
+
if (Object.hasOwn(target, "next_offset")) {
|
|
632
575
|
target.next_offset = pagination.next_offset;
|
|
633
576
|
}
|
|
634
577
|
}
|
|
635
578
|
else if (Object.hasOwn(target, "next_offset")) {
|
|
636
579
|
delete target.next_offset;
|
|
637
580
|
}
|
|
638
|
-
if (pagination.has_more && typeof target.hint === "string") {
|
|
639
|
-
// The tool wrote its continuation from the page it fetched; if the fit
|
|
640
|
-
// dropped rows, the first row not returned is where to continue.
|
|
641
|
-
target.hint = (0, truncation_hint_js_1.restateContinuationHint)(target.hint, {
|
|
642
|
-
offset: pagination.offset,
|
|
643
|
-
...(pagination.next_offset === undefined
|
|
644
|
-
? {}
|
|
645
|
-
: { nextOffset: pagination.next_offset }),
|
|
646
|
-
});
|
|
647
|
-
}
|
|
648
581
|
if (Object.hasOwn(target, "truncated")) {
|
|
649
582
|
target.truncated = truncation.truncated;
|
|
650
583
|
}
|
|
@@ -654,20 +587,6 @@ function synchronizePagingFields(data, pagination, truncation, depth = 0, needed
|
|
|
654
587
|
}
|
|
655
588
|
else {
|
|
656
589
|
target.dropped_records = truncation.dropped_records;
|
|
657
|
-
// A tool that explains the drop in a hint wrote it before this fit
|
|
658
|
-
// dropped more rows. Restate it from the final counts and
|
|
659
|
-
// continuation so the text agrees with the fields beside it.
|
|
660
|
-
if (typeof target.hint === "string") {
|
|
661
|
-
target.hint = (0, truncation_hint_js_1.refreshTruncationHint)(target.hint, {
|
|
662
|
-
dropped: truncation.dropped_records,
|
|
663
|
-
returned: pagination.returned,
|
|
664
|
-
...(neededBytes === undefined ? {} : { neededBytes }),
|
|
665
|
-
...(allBytes === undefined ? {} : { allBytes }),
|
|
666
|
-
...(withoutAdvice === true ? { withoutAdvice } : {}),
|
|
667
|
-
...(widestHint === true ? { widest: true } : {}),
|
|
668
|
-
...(capBytes === undefined ? {} : { capBytes }),
|
|
669
|
-
});
|
|
670
|
-
}
|
|
671
590
|
}
|
|
672
591
|
}
|
|
673
592
|
};
|
|
@@ -676,65 +595,9 @@ function synchronizePagingFields(data, pagination, truncation, depth = 0, needed
|
|
|
676
595
|
synchronizeObject(record.meta);
|
|
677
596
|
}
|
|
678
597
|
for (const wrapper of ["request", "result"]) {
|
|
679
|
-
synchronizePagingFields(record[wrapper], pagination, truncation, depth + 1
|
|
598
|
+
synchronizePagingFields(record[wrapper], pagination, truncation, depth + 1);
|
|
680
599
|
}
|
|
681
600
|
}
|
|
682
|
-
/**
|
|
683
|
-
* Remove `metadata.cache` and `metadata.timing` from a handler's result. Only
|
|
684
|
-
* the dispatcher reports them, from what it observed, so a value a handler
|
|
685
|
-
* sets is never passed on, whether or not the dispatcher adds its own.
|
|
686
|
-
*/
|
|
687
|
-
function withoutCallMetadata(result) {
|
|
688
|
-
const content = result.structuredContent;
|
|
689
|
-
if (typeof content !== "object" || content === null)
|
|
690
|
-
return result;
|
|
691
|
-
const metadata = content.metadata;
|
|
692
|
-
if (typeof metadata !== "object" || metadata === null)
|
|
693
|
-
return result;
|
|
694
|
-
const rest = { ...metadata };
|
|
695
|
-
delete rest.cache;
|
|
696
|
-
delete rest.timing;
|
|
697
|
-
return { ...result, structuredContent: { ...content, metadata: rest } };
|
|
698
|
-
}
|
|
699
|
-
/**
|
|
700
|
-
* Add what the call reports about itself to a finished result's envelope
|
|
701
|
-
* metadata. The two fields are not counted against the result's byte limit.
|
|
702
|
-
*
|
|
703
|
-
* They are diagnostics, so they never cost the caller anything: the result is
|
|
704
|
-
* fitted to its limit first, exactly as before, with the fit, the paging
|
|
705
|
-
* fields and the truncation hint knowing nothing about them, and the fields
|
|
706
|
-
* are added after. A successful response therefore carries them always, on the
|
|
707
|
-
* calls where the timing matters most (a truncated one is often fitted to
|
|
708
|
-
* within a few bytes of the limit), and may exceed the limit by their size,
|
|
709
|
-
* under 100 bytes. A result that is not an envelope comes back untouched.
|
|
710
|
-
*/
|
|
711
|
-
function withCallMetadata(handlerResult, snapshot) {
|
|
712
|
-
// Whatever the handler put in the two fields never survives, fitted or not.
|
|
713
|
-
const result = withoutCallMetadata(handlerResult);
|
|
714
|
-
const content = result.structuredContent;
|
|
715
|
-
if (typeof content !== "object" || content === null)
|
|
716
|
-
return result;
|
|
717
|
-
const metadata = content.metadata;
|
|
718
|
-
if (typeof metadata !== "object" || metadata === null)
|
|
719
|
-
return result;
|
|
720
|
-
// Only an envelope has limits; anything else is not this function's to annotate.
|
|
721
|
-
const maxBytes = metadata.limits?.max_bytes;
|
|
722
|
-
if (typeof maxBytes !== "number")
|
|
723
|
-
return result;
|
|
724
|
-
const added = callMetadataSchema.safeParse({
|
|
725
|
-
...(snapshot.cache === undefined ? {} : { cache: snapshot.cache }),
|
|
726
|
-
timing: snapshot.timing,
|
|
727
|
-
});
|
|
728
|
-
if (!added.success)
|
|
729
|
-
return result;
|
|
730
|
-
return {
|
|
731
|
-
...result,
|
|
732
|
-
structuredContent: {
|
|
733
|
-
...content,
|
|
734
|
-
metadata: { ...metadata, ...added.data },
|
|
735
|
-
},
|
|
736
|
-
};
|
|
737
|
-
}
|
|
738
601
|
function envelopeCompatibilityResult(name, args, result) {
|
|
739
602
|
if (result.isError)
|
|
740
603
|
return result;
|
|
@@ -753,23 +616,14 @@ function envelopeCompatibilityResult(name, args, result) {
|
|
|
753
616
|
const explicitNextOffset = numericObjectField(data, "next_offset");
|
|
754
617
|
const legacyTruncated = booleanObjectField(data, "truncated") === true;
|
|
755
618
|
const priorDropped = numericObjectField(data, "dropped_records") ?? 0;
|
|
756
|
-
// A tool that dropped rows for size says so (truncated, dropped_records):
|
|
757
|
-
// rows exist that its response does not hold, so there is more to fetch.
|
|
758
|
-
const droppedForSize = collection && legacyTruncated && priorDropped > 0;
|
|
759
619
|
let hasMore = collection &&
|
|
760
620
|
(originalCount > returned ||
|
|
761
621
|
explicitHasMore === true ||
|
|
762
|
-
droppedForSize ||
|
|
763
622
|
(explicitHasMore === undefined && returned >= limit));
|
|
764
623
|
let nextOffset;
|
|
765
624
|
let recovery = legacyTruncated ? "adjust_request_and_retry_same_offset" : undefined;
|
|
766
625
|
if (hasMore) {
|
|
767
|
-
if (
|
|
768
|
-
// The rows returned are a prefix of the window, so the first row not
|
|
769
|
-
// returned is the one to continue at; nothing is skipped.
|
|
770
|
-
nextOffset = offset + returned;
|
|
771
|
-
}
|
|
772
|
-
else if (explicitNextOffset !== undefined && explicitNextOffset > offset) {
|
|
626
|
+
if (explicitNextOffset !== undefined && explicitNextOffset > offset) {
|
|
773
627
|
nextOffset = explicitNextOffset;
|
|
774
628
|
}
|
|
775
629
|
else if (originalCount > returned && returned > 0) {
|
|
@@ -781,11 +635,6 @@ function envelopeCompatibilityResult(name, args, result) {
|
|
|
781
635
|
// the effective limit prevents ACL-filtered windows from being replayed.
|
|
782
636
|
nextOffset = offset + limit;
|
|
783
637
|
}
|
|
784
|
-
else if (droppedForSize) {
|
|
785
|
-
// Not one row fit: has_more stays true, no offset can advance, and the
|
|
786
|
-
// request has to change.
|
|
787
|
-
recovery = "adjust_request_and_retry_same_offset";
|
|
788
|
-
}
|
|
789
638
|
else {
|
|
790
639
|
hasMore = false;
|
|
791
640
|
recovery = "adjust_request_and_retry_same_offset";
|
|
@@ -840,20 +689,13 @@ function isMutableEnvelope(candidate) {
|
|
|
840
689
|
}
|
|
841
690
|
function renderEnvelopeText(envelope) {
|
|
842
691
|
const pagination = envelope.metadata.pagination;
|
|
843
|
-
const noRowFit = pagination.mode === "offset" && pagination.returned === 0;
|
|
844
692
|
const continuation = pagination.mode === "offset" && pagination.has_more
|
|
845
|
-
? pagination.next_offset
|
|
846
|
-
? ` More records are available at offset ${pagination.next_offset}.`
|
|
847
|
-
: noRowFit
|
|
848
|
-
? " More records are available, but none fit."
|
|
849
|
-
: " More records are available; this result has no offset to continue at."
|
|
693
|
+
? ` More records are available at offset ${pagination.next_offset}.`
|
|
850
694
|
: "";
|
|
851
695
|
const recovery = pagination.mode === "offset" && pagination.recovery !== undefined
|
|
852
|
-
? pagination.has_more
|
|
853
|
-
? `
|
|
854
|
-
:
|
|
855
|
-
? ` No row fit; adjust the request (fewer fields, a lower limit or a higher max_response_bytes) and retry offset ${pagination.offset}.`
|
|
856
|
-
: ` 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}.`
|
|
696
|
+
? pagination.has_more
|
|
697
|
+
? ` To recover records dropped from the current window, adjust the request and retry offset ${pagination.offset}; use offset ${pagination.next_offset} only to resume after the consumed window.`
|
|
698
|
+
: ` No automatic post-window continuation is available; adjust the request and retry offset ${pagination.offset}.`
|
|
857
699
|
: "";
|
|
858
700
|
const truncated = envelope.metadata.truncation.truncated
|
|
859
701
|
? ` Output was truncated by ${envelope.metadata.truncation.reason}.`
|
|
@@ -961,7 +803,7 @@ function renderedEnvelopeText(renderer, envelope, context) {
|
|
|
961
803
|
*/
|
|
962
804
|
function finalizeEnvelopeResult(result, tool, options = {}) {
|
|
963
805
|
envelopeTextRenderersSealed = true;
|
|
964
|
-
const summarized = finalizeSummaryResult(result
|
|
806
|
+
const summarized = finalizeSummaryResult(result);
|
|
965
807
|
const renderer = tool === undefined ? undefined : ENVELOPE_TEXT_RENDERERS.get(tool);
|
|
966
808
|
if (!summarized ||
|
|
967
809
|
!renderer ||
|
|
@@ -985,75 +827,20 @@ function finalizeEnvelopeResult(result, tool, options = {}) {
|
|
|
985
827
|
? rendered
|
|
986
828
|
: summarized;
|
|
987
829
|
}
|
|
988
|
-
|
|
989
|
-
* The `max_response_bytes` to name for the whole window: what the tool
|
|
990
|
-
* measured for all its rows, plus what the envelope adds around them. Null
|
|
991
|
-
* when the tool named none (the window exceeded the maximum on its own) or
|
|
992
|
-
* when the sum exceeds the maximum, so the hint leaves it out.
|
|
993
|
-
*/
|
|
994
|
-
function wholeWindowBytes(toolHint, envelopeBytes, current, cap) {
|
|
995
|
-
if (toolHint.allBytes === undefined)
|
|
996
|
-
return null;
|
|
997
|
-
const needed = (0, truncation_hint_js_1.neededResponseBytes)(toolHint.allBytes + envelopeBytes, current, cap);
|
|
998
|
-
return Number.isFinite(needed) ? needed : null;
|
|
999
|
-
}
|
|
1000
|
-
/**
|
|
1001
|
-
* The tools whose `offset` argument is a row offset into the collection they
|
|
1002
|
-
* return, so that `offset + rows returned` is the first row not returned. For
|
|
1003
|
-
* these a fit that drops rows for size can offer that offset as
|
|
1004
|
-
* `next_offset`. Any other collection still reports `has_more`, but with no
|
|
1005
|
-
* offset to give: the request has to change.
|
|
1006
|
-
*/
|
|
1007
|
-
const OFFSET_PAGED_TOOLS = new Set([
|
|
1008
|
-
"sn_query",
|
|
1009
|
-
"sn_syslog",
|
|
1010
|
-
"sn_schema",
|
|
1011
|
-
"sn_relationships",
|
|
1012
|
-
"sn_codesearch",
|
|
1013
|
-
"sn_discover",
|
|
1014
|
-
"sn_attach",
|
|
1015
|
-
"sn_atf",
|
|
1016
|
-
]);
|
|
1017
|
-
/**
|
|
1018
|
-
* Fit the result to its byte limit by dropping whole rows from the tail.
|
|
1019
|
-
* Rows dropped for size mean rows exist that the response does not hold, so
|
|
1020
|
-
* `has_more` is true; the rows returned are a prefix of the window, so a
|
|
1021
|
-
* paged tool continues at `offset + rows returned` and loses nothing, and
|
|
1022
|
-
* when no row fits there is no offset to give. With no `tool` (a caller that
|
|
1023
|
-
* does not say which), the offset is offered as for a paged tool.
|
|
1024
|
-
*/
|
|
1025
|
-
function finalizeSummaryResult(result, tool, byteCap = utils_js_1.MAX_RESPONSE_BYTES_MAX) {
|
|
1026
|
-
const offsetPaged = tool === undefined || OFFSET_PAGED_TOOLS.has(tool);
|
|
830
|
+
function finalizeSummaryResult(result) {
|
|
1027
831
|
if (!isMutableEnvelope(result.structuredContent))
|
|
1028
832
|
return result;
|
|
1029
833
|
const sourceEnvelope = JSON.parse(JSON.stringify(result.structuredContent));
|
|
1030
834
|
let candidate = candidateResult(result, sourceEnvelope);
|
|
1031
835
|
const maximum = sourceEnvelope.metadata.limits.max_bytes;
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
// rest of the result known.
|
|
1035
|
-
const toolHint = truncationHintParts(sourceEnvelope.data);
|
|
1036
|
-
if (resultBytes(candidate) <= maximum) {
|
|
1037
|
-
if (toolHint === undefined || !Number.isFinite(toolHint.neededBytes))
|
|
1038
|
-
return candidate;
|
|
1039
|
-
const overhead = resultBytes(candidate) - Buffer.byteLength(JSON.stringify(sourceEnvelope.data), "utf8");
|
|
1040
|
-
const restated = JSON.parse(JSON.stringify(sourceEnvelope));
|
|
1041
|
-
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));
|
|
1042
|
-
const restatedCandidate = candidateResult(result, restated);
|
|
1043
|
-
// The larger number can add a digit; if that tips the result over its
|
|
1044
|
-
// limit, fit it like any other result that is too big.
|
|
1045
|
-
if (resultBytes(restatedCandidate) <= maximum)
|
|
1046
|
-
return restatedCandidate;
|
|
1047
|
-
}
|
|
836
|
+
if (resultBytes(candidate) <= maximum)
|
|
837
|
+
return candidate;
|
|
1048
838
|
const sourceRecords = collectionArray(sourceEnvelope.data);
|
|
1049
839
|
let envelope = sourceEnvelope;
|
|
1050
840
|
if (sourceRecords) {
|
|
1051
841
|
const originalRecordCount = sourceRecords.length;
|
|
1052
842
|
const priorDropped = sourceEnvelope.metadata.truncation.dropped_records ?? 0;
|
|
1053
|
-
|
|
1054
|
-
// "exceeds the maximum" clause), which is never shorter than a number, so
|
|
1055
|
-
// a result that fits that way still fits with the real number.
|
|
1056
|
-
const envelopeForCount = (count, neededBytes, allBytes, withoutAdvice, widestHint) => {
|
|
843
|
+
const envelopeForCount = (count) => {
|
|
1057
844
|
const probe = JSON.parse(JSON.stringify(sourceEnvelope));
|
|
1058
845
|
const probeRecords = collectionArray(probe.data);
|
|
1059
846
|
if (!probeRecords)
|
|
@@ -1064,106 +851,36 @@ function finalizeSummaryResult(result, tool, byteCap = utils_js_1.MAX_RESPONSE_B
|
|
|
1064
851
|
if (probe.metadata.pagination.mode === "offset") {
|
|
1065
852
|
probe.metadata.pagination.returned = count;
|
|
1066
853
|
if (count < originalRecordCount) {
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
paging.has_more = true;
|
|
1070
|
-
if (offsetPaged && count > 0) {
|
|
1071
|
-
paging.next_offset = paging.offset + count;
|
|
1072
|
-
}
|
|
1073
|
-
else {
|
|
1074
|
-
delete paging.next_offset;
|
|
1075
|
-
}
|
|
854
|
+
probe.metadata.pagination.recovery =
|
|
855
|
+
"adjust_request_and_retry_same_offset";
|
|
1076
856
|
}
|
|
1077
857
|
}
|
|
1078
858
|
applyByteTruncation(probe, priorDropped + originalRecordCount - count);
|
|
1079
|
-
synchronizePagingFields(probe.data, probe.metadata.pagination, probe.metadata.truncation
|
|
859
|
+
synchronizePagingFields(probe.data, probe.metadata.pagination, probe.metadata.truncation);
|
|
1080
860
|
return probe;
|
|
1081
861
|
};
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
// hint is longer than the bare one was, and at the smallest budgets the
|
|
1085
|
-
// envelope's own metadata leaves room for little more than the counts: the
|
|
1086
|
-
// page figure can go, then the field advice, which is the least that
|
|
1087
|
-
// still names the next row's budget. A narrower form is used only when no
|
|
1088
|
-
// row, not even zero, fits beside the fuller one. Each form's widest text
|
|
1089
|
-
// (the longest any real figures could make it) is what rows are sized
|
|
1090
|
-
// with, and the real text is rendered in the same form, so it is never
|
|
1091
|
-
// longer than the probe. The budget named for the next row is always
|
|
1092
|
-
// measured with the fullest form, so a call at that budget, which takes
|
|
1093
|
-
// the fullest form that fits, returns the row.
|
|
1094
|
-
const forms = toolHint === undefined
|
|
1095
|
-
? [{ all: widest, withoutAdvice: false }]
|
|
1096
|
-
: [
|
|
1097
|
-
{ all: widest, withoutAdvice: false },
|
|
1098
|
-
{ all: null, withoutAdvice: false },
|
|
1099
|
-
{ all: null, withoutAdvice: true },
|
|
1100
|
-
];
|
|
862
|
+
let low = 0;
|
|
863
|
+
let high = originalRecordCount;
|
|
1101
864
|
let best = -1;
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
fitted = midpoint;
|
|
1112
|
-
low = midpoint + 1;
|
|
1113
|
-
}
|
|
1114
|
-
else {
|
|
1115
|
-
high = midpoint - 1;
|
|
1116
|
-
}
|
|
865
|
+
while (low <= high) {
|
|
866
|
+
const midpoint = Math.floor((low + high) / 2);
|
|
867
|
+
const probe = candidateResult(result, envelopeForCount(midpoint));
|
|
868
|
+
if (resultBytes(probe) <= maximum) {
|
|
869
|
+
best = midpoint;
|
|
870
|
+
low = midpoint + 1;
|
|
871
|
+
}
|
|
872
|
+
else {
|
|
873
|
+
high = midpoint - 1;
|
|
1117
874
|
}
|
|
1118
|
-
// The first form that fits (zero rows is enough) is the one used.
|
|
1119
|
-
form = candidateForm;
|
|
1120
|
-
best = fitted;
|
|
1121
|
-
if (best >= 0)
|
|
1122
|
-
break;
|
|
1123
875
|
}
|
|
1124
876
|
// Collections are fitted only by removing complete rows. In particular,
|
|
1125
877
|
// never shorten a field merely to force one row into the envelope: callers
|
|
1126
878
|
// can retry this exact window with a narrower projection/larger budget,
|
|
1127
879
|
// while next_offset (when present) remains the separately authoritative
|
|
1128
880
|
// post-window resume point.
|
|
1129
|
-
|
|
1130
|
-
envelope = envelopeForCount(kept, widest, form.all, form.withoutAdvice, true);
|
|
881
|
+
envelope = envelopeForCount(Math.max(best, 0));
|
|
1131
882
|
candidate = candidateResult(result, envelope);
|
|
1132
|
-
|
|
1133
|
-
return undefined;
|
|
1134
|
-
if (toolHint === undefined)
|
|
1135
|
-
return candidate;
|
|
1136
|
-
// The rows the tool already dropped are not here to measure, so the whole
|
|
1137
|
-
// window is its payload size, which the tool measured, plus what this
|
|
1138
|
-
// envelope adds around the rows. The envelope of a whole page carries no
|
|
1139
|
-
// truncation fields, so this is never too small.
|
|
1140
|
-
const overhead = resultBytes(candidate) - Buffer.byteLength(JSON.stringify(envelope.data), "utf8");
|
|
1141
|
-
// The budget that would hold one more row is measured here, with the
|
|
1142
|
-
// envelope's own cost in it, not guessed. When the fit dropped nothing
|
|
1143
|
-
// (the tool had already dropped every row, and only a narrower form
|
|
1144
|
-
// fitted) there is no row of ours to add: it is the tool's figure plus
|
|
1145
|
-
// the envelope's cost, as in the first branch.
|
|
1146
|
-
// With no row returned the envelope carries no `next_offset`, which the
|
|
1147
|
-
// first row brings with it, in the data and in the pagination metadata. The
|
|
1148
|
-
// zero-row overhead would then name a budget a few bytes short of one row.
|
|
1149
|
-
const overheadWithARow = (zeroOrMore, rows, base) => {
|
|
1150
|
-
if (rows > 0 || !offsetPaged)
|
|
1151
|
-
return base;
|
|
1152
|
-
const withRow = JSON.parse(JSON.stringify(zeroOrMore));
|
|
1153
|
-
const paging = withRow.metadata.pagination;
|
|
1154
|
-
if (paging.mode !== "offset")
|
|
1155
|
-
return base;
|
|
1156
|
-
paging.returned = 1;
|
|
1157
|
-
paging.next_offset = paging.offset + 1;
|
|
1158
|
-
synchronizePagingFields(withRow.data, withRow.metadata.pagination, withRow.metadata.truncation, 0, undefined, byteCap);
|
|
1159
|
-
return (resultBytes(candidateResult(result, withRow)) -
|
|
1160
|
-
Buffer.byteLength(JSON.stringify(withRow.data), "utf8"));
|
|
1161
|
-
};
|
|
1162
|
-
const needed = kept >= originalRecordCount
|
|
1163
|
-
? (0, truncation_hint_js_1.neededResponseBytes)(toolHint.neededBytes + overheadWithARow(envelope, kept, overhead), maximum, byteCap)
|
|
1164
|
-
: (0, truncation_hint_js_1.neededResponseBytes)(resultBytes(candidateResult(result, envelopeForCount(kept + 1, widest, forms[0].all, false, true))), maximum, byteCap);
|
|
1165
|
-
const restated = candidateResult(result, envelopeForCount(kept, needed, form.all === null ? null : wholeWindowBytes(toolHint, overhead, maximum, byteCap), form.withoutAdvice));
|
|
1166
|
-
return resultBytes(restated) <= maximum ? restated : candidate;
|
|
883
|
+
return resultBytes(candidate) <= maximum ? candidate : undefined;
|
|
1167
884
|
}
|
|
1168
885
|
while (resultBytes(candidate) > maximum && truncateLongestString(envelope.data)) {
|
|
1169
886
|
applyByteTruncation(envelope, envelope.metadata.truncation.dropped_records ?? 0);
|