@onlyflows/servicenow-mcp 1.0.0 → 2.0.0
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/.env.example +56 -0
- package/CHANGELOG.md +153 -0
- package/README.md +939 -101
- package/dist/auth.d.ts +88 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +396 -0
- package/dist/auth.js.map +1 -0
- package/dist/client.d.ts +111 -17
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +554 -79
- package/dist/client.js.map +1 -1
- package/dist/config.d.ts +41 -4
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +44 -34
- package/dist/config.js.map +1 -1
- package/dist/encoded-query-policy.d.ts +72 -0
- package/dist/encoded-query-policy.d.ts.map +1 -0
- package/dist/encoded-query-policy.js +409 -0
- package/dist/encoded-query-policy.js.map +1 -0
- package/dist/execution-context.d.ts +235 -0
- package/dist/execution-context.d.ts.map +1 -0
- package/dist/execution-context.js +439 -0
- package/dist/execution-context.js.map +1 -0
- package/dist/field-policy.d.ts +117 -0
- package/dist/field-policy.d.ts.map +1 -0
- package/dist/field-policy.js +1684 -0
- package/dist/field-policy.js.map +1 -0
- package/dist/http-auth.d.ts +131 -0
- package/dist/http-auth.d.ts.map +1 -0
- package/dist/http-auth.js +347 -0
- package/dist/http-auth.js.map +1 -0
- package/dist/http-entrypoint.d.ts +26 -0
- package/dist/http-entrypoint.d.ts.map +1 -0
- package/dist/http-entrypoint.js +255 -0
- package/dist/http-entrypoint.js.map +1 -0
- package/dist/http-observability.d.ts +147 -0
- package/dist/http-observability.d.ts.map +1 -0
- package/dist/http-observability.js +703 -0
- package/dist/http-observability.js.map +1 -0
- package/dist/http-request-policy.d.ts +72 -0
- package/dist/http-request-policy.d.ts.map +1 -0
- package/dist/http-request-policy.js +463 -0
- package/dist/http-request-policy.js.map +1 -0
- package/dist/http-request-signal.d.ts +12 -0
- package/dist/http-request-signal.d.ts.map +1 -0
- package/dist/http-request-signal.js +39 -0
- package/dist/http-request-signal.js.map +1 -0
- package/dist/http-runtime.d.ts +77 -0
- package/dist/http-runtime.d.ts.map +1 -0
- package/dist/http-runtime.js +1321 -0
- package/dist/http-runtime.js.map +1 -0
- package/dist/incident-journal-policy.d.ts +22 -0
- package/dist/incident-journal-policy.d.ts.map +1 -0
- package/dist/incident-journal-policy.js +115 -0
- package/dist/incident-journal-policy.js.map +1 -0
- package/dist/incident-write-policy.d.ts +20 -0
- package/dist/incident-write-policy.d.ts.map +1 -0
- package/dist/incident-write-policy.js +196 -0
- package/dist/incident-write-policy.js.map +1 -0
- package/dist/index.d.ts +14 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +140 -36
- package/dist/index.js.map +1 -1
- package/dist/metadata-cache.d.ts +71 -0
- package/dist/metadata-cache.d.ts.map +1 -0
- package/dist/metadata-cache.js +397 -0
- package/dist/metadata-cache.js.map +1 -0
- package/dist/profile-admin.d.ts +64 -0
- package/dist/profile-admin.d.ts.map +1 -0
- package/dist/profile-admin.js +351 -0
- package/dist/profile-admin.js.map +1 -0
- package/dist/profile-credentials.d.ts +68 -0
- package/dist/profile-credentials.d.ts.map +1 -0
- package/dist/profile-credentials.js +329 -0
- package/dist/profile-credentials.js.map +1 -0
- package/dist/profile-manager.d.ts +187 -0
- package/dist/profile-manager.d.ts.map +1 -0
- package/dist/profile-manager.js +1026 -0
- package/dist/profile-manager.js.map +1 -0
- package/dist/record-identifier-policy.d.ts +39 -0
- package/dist/record-identifier-policy.d.ts.map +1 -0
- package/dist/record-identifier-policy.js +203 -0
- package/dist/record-identifier-policy.js.map +1 -0
- package/dist/runtime-environment.d.ts +23 -0
- package/dist/runtime-environment.d.ts.map +1 -0
- package/dist/runtime-environment.js +58 -0
- package/dist/runtime-environment.js.map +1 -0
- package/dist/server.d.ts +33 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +73 -0
- package/dist/server.js.map +1 -0
- package/dist/servicenow-identifiers.d.ts +8 -0
- package/dist/servicenow-identifiers.d.ts.map +1 -0
- package/dist/servicenow-identifiers.js +22 -0
- package/dist/servicenow-identifiers.js.map +1 -0
- package/dist/setup-prompts.d.ts +7 -0
- package/dist/setup-prompts.d.ts.map +1 -0
- package/dist/setup-prompts.js +123 -0
- package/dist/setup-prompts.js.map +1 -0
- package/dist/setup.d.ts +109 -0
- package/dist/setup.d.ts.map +1 -0
- package/dist/setup.js +1903 -0
- package/dist/setup.js.map +1 -0
- package/dist/startup.d.ts +43 -0
- package/dist/startup.d.ts.map +1 -0
- package/dist/startup.js +75 -0
- package/dist/startup.js.map +1 -0
- package/dist/stdio-runtime.d.ts +106 -0
- package/dist/stdio-runtime.d.ts.map +1 -0
- package/dist/stdio-runtime.js +231 -0
- package/dist/stdio-runtime.js.map +1 -0
- package/dist/structured-query.d.ts +187 -0
- package/dist/structured-query.d.ts.map +1 -0
- package/dist/structured-query.js +503 -0
- package/dist/structured-query.js.map +1 -0
- package/dist/table-defaults.d.ts +28 -0
- package/dist/table-defaults.d.ts.map +1 -0
- package/dist/table-defaults.js +85 -0
- package/dist/table-defaults.js.map +1 -0
- package/dist/table-policy.d.ts +71 -0
- package/dist/table-policy.d.ts.map +1 -0
- package/dist/table-policy.js +290 -0
- package/dist/table-policy.js.map +1 -0
- package/dist/tool-error.d.ts +48 -0
- package/dist/tool-error.d.ts.map +1 -0
- package/dist/tool-error.js +139 -0
- package/dist/tool-error.js.map +1 -0
- package/dist/tool-table-access.d.ts +19 -0
- package/dist/tool-table-access.d.ts.map +1 -0
- package/dist/tool-table-access.js +388 -0
- package/dist/tool-table-access.js.map +1 -0
- package/dist/tools/aggregate.d.ts +22 -42
- package/dist/tools/aggregate.d.ts.map +1 -1
- package/dist/tools/aggregate.js +103 -47
- package/dist/tools/aggregate.js.map +1 -1
- package/dist/tools/all-fields-cap.d.ts +45 -0
- package/dist/tools/all-fields-cap.d.ts.map +1 -0
- package/dist/tools/all-fields-cap.js +151 -0
- package/dist/tools/all-fields-cap.js.map +1 -0
- package/dist/tools/atf.d.ts +21 -60
- package/dist/tools/atf.d.ts.map +1 -1
- package/dist/tools/atf.js +180 -116
- package/dist/tools/atf.js.map +1 -1
- package/dist/tools/attach.d.ts +31 -45
- package/dist/tools/attach.d.ts.map +1 -1
- package/dist/tools/attach.js +175 -137
- package/dist/tools/attach.js.map +1 -1
- package/dist/tools/batch.d.ts +78 -42
- package/dist/tools/batch.d.ts.map +1 -1
- package/dist/tools/batch.js +172 -68
- package/dist/tools/batch.js.map +1 -1
- package/dist/tools/catalog.d.ts +6 -0
- package/dist/tools/catalog.d.ts.map +1 -0
- package/dist/tools/catalog.js +203 -0
- package/dist/tools/catalog.js.map +1 -0
- package/dist/tools/codesearch.d.ts +14 -25
- package/dist/tools/codesearch.d.ts.map +1 -1
- package/dist/tools/codesearch.js +60 -48
- package/dist/tools/codesearch.js.map +1 -1
- package/dist/tools/create-module.d.ts +48 -0
- package/dist/tools/create-module.d.ts.map +1 -0
- package/dist/tools/create-module.js +94 -0
- package/dist/tools/create-module.js.map +1 -0
- package/dist/tools/create.d.ts +2 -38
- package/dist/tools/create.d.ts.map +1 -1
- package/dist/tools/create.js +9 -41
- package/dist/tools/create.js.map +1 -1
- package/dist/tools/delete.d.ts +13 -23
- package/dist/tools/delete.d.ts.map +1 -1
- package/dist/tools/delete.js +30 -33
- package/dist/tools/delete.js.map +1 -1
- package/dist/tools/discover.d.ts +15 -27
- package/dist/tools/discover.d.ts.map +1 -1
- package/dist/tools/discover.js +103 -60
- package/dist/tools/discover.js.map +1 -1
- package/dist/tools/get-module.d.ts +85 -0
- package/dist/tools/get-module.d.ts.map +1 -0
- package/dist/tools/get-module.js +221 -0
- package/dist/tools/get-module.js.map +1 -0
- package/dist/tools/get.d.ts +2 -51
- package/dist/tools/get.d.ts.map +1 -1
- package/dist/tools/get.js +9 -49
- package/dist/tools/get.js.map +1 -1
- package/dist/tools/handler-result.d.ts +14 -0
- package/dist/tools/handler-result.d.ts.map +1 -0
- package/dist/tools/handler-result.js +177 -0
- package/dist/tools/handler-result.js.map +1 -0
- package/dist/tools/health.d.ts +12 -15
- package/dist/tools/health.d.ts.map +1 -1
- package/dist/tools/health.js +55 -36
- package/dist/tools/health.js.map +1 -1
- package/dist/tools/incident-journal-module.d.ts +12 -0
- package/dist/tools/incident-journal-module.d.ts.map +1 -0
- package/dist/tools/incident-journal-module.js +61 -0
- package/dist/tools/incident-journal-module.js.map +1 -0
- package/dist/tools/incident-write-shared.d.ts +6 -0
- package/dist/tools/incident-write-shared.d.ts.map +1 -0
- package/dist/tools/incident-write-shared.js +64 -0
- package/dist/tools/incident-write-shared.js.map +1 -0
- package/dist/tools/index.d.ts +47 -36
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js +795 -35
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/nl.d.ts +10 -24
- package/dist/tools/nl.d.ts.map +1 -1
- package/dist/tools/nl.js +37 -147
- package/dist/tools/nl.js.map +1 -1
- package/dist/tools/profile.d.ts +37 -0
- package/dist/tools/profile.d.ts.map +1 -0
- package/dist/tools/profile.js +39 -0
- package/dist/tools/profile.js.map +1 -0
- package/dist/tools/query-module.d.ts +119 -0
- package/dist/tools/query-module.d.ts.map +1 -0
- package/dist/tools/query-module.js +324 -0
- package/dist/tools/query-module.js.map +1 -0
- package/dist/tools/query.d.ts +2 -72
- package/dist/tools/query.d.ts.map +1 -1
- package/dist/tools/query.js +8 -72
- package/dist/tools/query.js.map +1 -1
- package/dist/tools/relationships.d.ts +21 -42
- package/dist/tools/relationships.d.ts.map +1 -1
- package/dist/tools/relationships.js +70 -56
- package/dist/tools/relationships.js.map +1 -1
- package/dist/tools/result-envelope.d.ts +603 -0
- package/dist/tools/result-envelope.d.ts.map +1 -0
- package/dist/tools/result-envelope.js +767 -0
- package/dist/tools/result-envelope.js.map +1 -0
- package/dist/tools/schema-module.d.ts +64 -0
- package/dist/tools/schema-module.d.ts.map +1 -0
- package/dist/tools/schema-module.js +367 -0
- package/dist/tools/schema-module.js.map +1 -0
- package/dist/tools/schema.d.ts +2 -37
- package/dist/tools/schema.d.ts.map +1 -1
- package/dist/tools/schema.js +6 -57
- package/dist/tools/schema.js.map +1 -1
- package/dist/tools/script.d.ts +33 -27
- package/dist/tools/script.d.ts.map +1 -1
- package/dist/tools/script.js +43 -37
- package/dist/tools/script.js.map +1 -1
- package/dist/tools/syslog.d.ts +19 -46
- package/dist/tools/syslog.d.ts.map +1 -1
- package/dist/tools/syslog.js +101 -67
- package/dist/tools/syslog.js.map +1 -1
- package/dist/tools/tool-module.d.ts +143 -0
- package/dist/tools/tool-module.d.ts.map +1 -0
- package/dist/tools/tool-module.js +1095 -0
- package/dist/tools/tool-module.js.map +1 -0
- package/dist/tools/update-module.d.ts +51 -0
- package/dist/tools/update-module.d.ts.map +1 -0
- package/dist/tools/update-module.js +107 -0
- package/dist/tools/update-module.js.map +1 -0
- package/dist/tools/update.d.ts +2 -45
- package/dist/tools/update.d.ts.map +1 -1
- package/dist/tools/update.js +9 -41
- package/dist/tools/update.js.map +1 -1
- package/dist/utils.d.ts +68 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +114 -16
- package/dist/utils.js.map +1 -1
- package/dist/version.d.ts +10 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +13 -0
- package/dist/version.js.map +1 -0
- package/docs/ADDING-TOOLS.md +227 -0
- package/docs/CLIENT-SETUP.md +914 -0
- package/docs/CONTAINER-DEPLOYMENT.md +310 -0
- package/docs/CROSS-CLIENT-RELEASE-MATRIX.md +63 -0
- package/docs/ENTERPRISE-RELEASE-BOUNDARY.md +149 -0
- package/docs/OPERATIONS-RUNBOOK.md +383 -0
- package/docs/PRIVATE-CHATGPT-CONNECTIVITY.md +347 -0
- package/docs/PRODUCTION-SECURITY.md +368 -0
- package/docs/PROFILE-CREDENTIALS.md +126 -0
- package/docs/RELEASE-VALIDATION.md +140 -0
- package/docs/TESTING-OAUTH.md +101 -0
- package/docs/V2-MIGRATION.md +493 -0
- package/package.json +60 -13
- package/scripts/inspector.mjs +140 -0
- package/scripts/release-validate.mjs +201 -0
- package/scripts/smoke-test.mjs +274 -0
- package/tools/inspector/package-lock.json +3812 -0
- package/tools/inspector/package.json +12 -0
|
@@ -0,0 +1,767 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/** Shared SNSDK-33 structured-result contracts and compatibility adapter. */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.productionToolOutputSchemas = exports.ENVELOPE_HINT_KEY = exports.resultMetadataSchema = exports.resultPaginationSchema = exports.ATTACHMENT_RESULT_BYTE_LIMIT = exports.DEFAULT_RESULT_BYTE_LIMIT = exports.DEFAULT_RESULT_RECORD_LIMIT = void 0;
|
|
5
|
+
exports.withStructuredResultEnvelope = withStructuredResultEnvelope;
|
|
6
|
+
exports.isProductionToolName = isProductionToolName;
|
|
7
|
+
exports.envelopeCompatibilityResult = envelopeCompatibilityResult;
|
|
8
|
+
exports.finalizeEnvelopeResult = finalizeEnvelopeResult;
|
|
9
|
+
const zod_1 = require("zod");
|
|
10
|
+
const utils_js_1 = require("../utils.js");
|
|
11
|
+
const tool_module_js_1 = require("./tool-module.js");
|
|
12
|
+
exports.DEFAULT_RESULT_RECORD_LIMIT = 1_000;
|
|
13
|
+
exports.DEFAULT_RESULT_BYTE_LIMIT = utils_js_1.MAX_RESPONSE_BYTES_DEFAULT;
|
|
14
|
+
exports.ATTACHMENT_RESULT_BYTE_LIMIT = 14 * 1024 * 1024;
|
|
15
|
+
const nonNegativeInteger = zod_1.z.number().int().nonnegative();
|
|
16
|
+
const positiveInteger = zod_1.z.number().int().positive();
|
|
17
|
+
const offsetPaginationSchema = zod_1.z
|
|
18
|
+
.object({
|
|
19
|
+
mode: zod_1.z.literal("offset"),
|
|
20
|
+
limit: positiveInteger,
|
|
21
|
+
offset: nonNegativeInteger,
|
|
22
|
+
returned: nonNegativeInteger,
|
|
23
|
+
has_more: zod_1.z.boolean(),
|
|
24
|
+
next_offset: nonNegativeInteger.optional(),
|
|
25
|
+
recovery: zod_1.z
|
|
26
|
+
.literal("adjust_request_and_retry_same_offset")
|
|
27
|
+
.optional(),
|
|
28
|
+
order_by: zod_1.z.array(zod_1.z.string().min(1)).min(1).max(8),
|
|
29
|
+
})
|
|
30
|
+
.strict()
|
|
31
|
+
.superRefine((value, context) => {
|
|
32
|
+
if (value.has_more &&
|
|
33
|
+
(value.next_offset === undefined || value.next_offset <= value.offset)) {
|
|
34
|
+
context.addIssue({
|
|
35
|
+
code: zod_1.z.ZodIssueCode.custom,
|
|
36
|
+
message: "next_offset must advance beyond offset when has_more is true",
|
|
37
|
+
path: ["next_offset"],
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
if (!value.has_more && value.next_offset !== undefined) {
|
|
41
|
+
context.addIssue({
|
|
42
|
+
code: zod_1.z.ZodIssueCode.custom,
|
|
43
|
+
message: "next_offset requires has_more",
|
|
44
|
+
path: ["next_offset"],
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
/** Offset is the sole production continuation mode until a real cursor exists. */
|
|
49
|
+
exports.resultPaginationSchema = zod_1.z.union([
|
|
50
|
+
zod_1.z.object({ mode: zod_1.z.literal("none") }).strict(),
|
|
51
|
+
offsetPaginationSchema,
|
|
52
|
+
]);
|
|
53
|
+
exports.resultMetadataSchema = zod_1.z
|
|
54
|
+
.object({
|
|
55
|
+
kind: zod_1.z.enum(["single", "collection", "operation"]),
|
|
56
|
+
record_count: nonNegativeInteger,
|
|
57
|
+
limits: zod_1.z
|
|
58
|
+
.object({
|
|
59
|
+
max_records: positiveInteger,
|
|
60
|
+
max_bytes: zod_1.z
|
|
61
|
+
.number()
|
|
62
|
+
.int()
|
|
63
|
+
.min(utils_js_1.MAX_RESPONSE_BYTES_MIN)
|
|
64
|
+
.max(exports.ATTACHMENT_RESULT_BYTE_LIMIT),
|
|
65
|
+
})
|
|
66
|
+
.strict(),
|
|
67
|
+
pagination: exports.resultPaginationSchema,
|
|
68
|
+
truncation: zod_1.z
|
|
69
|
+
.object({
|
|
70
|
+
truncated: zod_1.z.boolean(),
|
|
71
|
+
reason: zod_1.z.enum(["record_limit", "byte_limit"]).optional(),
|
|
72
|
+
dropped_records: nonNegativeInteger.optional(),
|
|
73
|
+
})
|
|
74
|
+
.strict(),
|
|
75
|
+
})
|
|
76
|
+
.strict();
|
|
77
|
+
function withStructuredResultEnvelope(dataSchema) {
|
|
78
|
+
return (0, tool_module_js_1.withResolvedProfileOutput)(zod_1.z
|
|
79
|
+
.object({
|
|
80
|
+
data: dataSchema,
|
|
81
|
+
metadata: exports.resultMetadataSchema,
|
|
82
|
+
})
|
|
83
|
+
.strict());
|
|
84
|
+
}
|
|
85
|
+
const dynamicRecordSchema = zod_1.z.record(zod_1.z.unknown());
|
|
86
|
+
const canonicalSysIdSchema = zod_1.z.string().regex(/^[0-9a-f]{32}$/u);
|
|
87
|
+
const dynamicArraySchema = zod_1.z.array(zod_1.z.unknown());
|
|
88
|
+
const dynamicJsonRootSchema = zod_1.z.union([
|
|
89
|
+
zod_1.z.null(),
|
|
90
|
+
zod_1.z.string(),
|
|
91
|
+
zod_1.z.number(),
|
|
92
|
+
zod_1.z.boolean(),
|
|
93
|
+
dynamicRecordSchema,
|
|
94
|
+
dynamicArraySchema,
|
|
95
|
+
]);
|
|
96
|
+
const wrappedRecordSchema = zod_1.z
|
|
97
|
+
.object({
|
|
98
|
+
record: dynamicRecordSchema,
|
|
99
|
+
// Optional caller-facing notice, mirroring sn_query's `hint`. Optional so
|
|
100
|
+
// existing consumers are unaffected: it is present only when the tool has
|
|
101
|
+
// something to say, such as an all-fields projection having been capped.
|
|
102
|
+
hint: zod_1.z.string().optional(),
|
|
103
|
+
})
|
|
104
|
+
.strict();
|
|
105
|
+
/**
|
|
106
|
+
* Reserved key a single-record handler uses to carry an envelope-level hint
|
|
107
|
+
* alongside its record.
|
|
108
|
+
*
|
|
109
|
+
* It is deliberately not a legal ServiceNow element name (those match
|
|
110
|
+
* /^[a-z][a-z0-9_]{0,79}$/), so it can never collide with a real column --
|
|
111
|
+
* which is exactly why the hint is not carried inside the record namespace.
|
|
112
|
+
*/
|
|
113
|
+
exports.ENVELOPE_HINT_KEY = "$hint";
|
|
114
|
+
const wrappedResultSchema = zod_1.z.object({ result: dynamicJsonRootSchema }).strict();
|
|
115
|
+
const schemaResultSchema = zod_1.z.object({ fields: dynamicArraySchema }).strict();
|
|
116
|
+
const healthResultSchema = zod_1.z.object({ health: dynamicRecordSchema }).strict();
|
|
117
|
+
const naturalLanguageResultSchema = zod_1.z
|
|
118
|
+
.object({ request: dynamicRecordSchema })
|
|
119
|
+
.strict();
|
|
120
|
+
const listResultSchema = zod_1.z
|
|
121
|
+
.object({
|
|
122
|
+
results: dynamicArraySchema,
|
|
123
|
+
warnings: zod_1.z.array(zod_1.z.string()).optional(),
|
|
124
|
+
})
|
|
125
|
+
.strict();
|
|
126
|
+
const attachmentResultEnvelopeSchema = zod_1.z
|
|
127
|
+
.object({
|
|
128
|
+
action: zod_1.z.enum(["list", "download", "upload"]),
|
|
129
|
+
result: dynamicJsonRootSchema,
|
|
130
|
+
})
|
|
131
|
+
.strict();
|
|
132
|
+
const atfResultEnvelopeSchema = zod_1.z
|
|
133
|
+
.object({
|
|
134
|
+
action: zod_1.z.enum(["list", "suites", "run", "run-suite", "results"]),
|
|
135
|
+
result: dynamicJsonRootSchema,
|
|
136
|
+
})
|
|
137
|
+
.strict();
|
|
138
|
+
const queryResultSchema = zod_1.z
|
|
139
|
+
.object({
|
|
140
|
+
record_count: nonNegativeInteger,
|
|
141
|
+
total: nonNegativeInteger.optional(),
|
|
142
|
+
has_more: zod_1.z.boolean(),
|
|
143
|
+
next_offset: nonNegativeInteger.optional(),
|
|
144
|
+
hint: zod_1.z.string().optional(),
|
|
145
|
+
truncated: zod_1.z.boolean().optional(),
|
|
146
|
+
dropped_records: nonNegativeInteger.optional(),
|
|
147
|
+
results: dynamicArraySchema,
|
|
148
|
+
})
|
|
149
|
+
.strict();
|
|
150
|
+
const createResultSchema = zod_1.z
|
|
151
|
+
.object({
|
|
152
|
+
sys_id: canonicalSysIdSchema,
|
|
153
|
+
number: zod_1.z.string().regex(/^[A-Za-z0-9_-]{1,80}$/u).optional(),
|
|
154
|
+
table: zod_1.z.literal("incident"),
|
|
155
|
+
record: dynamicRecordSchema,
|
|
156
|
+
})
|
|
157
|
+
.strict();
|
|
158
|
+
const updateResultSchema = zod_1.z
|
|
159
|
+
.object({
|
|
160
|
+
sys_id: canonicalSysIdSchema,
|
|
161
|
+
record: dynamicRecordSchema,
|
|
162
|
+
})
|
|
163
|
+
.strict();
|
|
164
|
+
const incidentJournalResultSchema = zod_1.z
|
|
165
|
+
.object({
|
|
166
|
+
status: zod_1.z.literal("appended"),
|
|
167
|
+
sys_id: canonicalSysIdSchema,
|
|
168
|
+
journal_field: zod_1.z.enum(["comments", "work_notes"]),
|
|
169
|
+
})
|
|
170
|
+
.strict();
|
|
171
|
+
const deleteResultSchema = zod_1.z
|
|
172
|
+
.object({
|
|
173
|
+
status: zod_1.z.literal("deleted"),
|
|
174
|
+
sys_id: zod_1.z.string(),
|
|
175
|
+
table: zod_1.z.string(),
|
|
176
|
+
})
|
|
177
|
+
.strict();
|
|
178
|
+
const batchResultSchema = zod_1.z
|
|
179
|
+
.object({
|
|
180
|
+
action: zod_1.z.enum(["update", "delete"]),
|
|
181
|
+
table: zod_1.z.string(),
|
|
182
|
+
matched: nonNegativeInteger,
|
|
183
|
+
dry_run: zod_1.z.boolean().optional(),
|
|
184
|
+
message: zod_1.z.string().optional(),
|
|
185
|
+
processed: nonNegativeInteger.optional(),
|
|
186
|
+
failed: nonNegativeInteger.optional(),
|
|
187
|
+
})
|
|
188
|
+
.strict();
|
|
189
|
+
const relationshipResultSchema = zod_1.z
|
|
190
|
+
.object({
|
|
191
|
+
root: dynamicRecordSchema,
|
|
192
|
+
relationships: dynamicArraySchema,
|
|
193
|
+
meta: dynamicRecordSchema,
|
|
194
|
+
warnings: zod_1.z.array(zod_1.z.string()).optional(),
|
|
195
|
+
})
|
|
196
|
+
.strict();
|
|
197
|
+
const profileResultSchema = zod_1.z
|
|
198
|
+
.object({
|
|
199
|
+
name: zod_1.z.string(),
|
|
200
|
+
instance: zod_1.z.string(),
|
|
201
|
+
auth_type: zod_1.z.enum(["apikey", "basic", "oauth"]),
|
|
202
|
+
})
|
|
203
|
+
.strict();
|
|
204
|
+
const productionDataSchemas = Object.freeze({
|
|
205
|
+
sn_query: queryResultSchema,
|
|
206
|
+
sn_get: wrappedRecordSchema,
|
|
207
|
+
sn_create: createResultSchema,
|
|
208
|
+
sn_update: updateResultSchema,
|
|
209
|
+
sn_incident_add_comment: incidentJournalResultSchema,
|
|
210
|
+
sn_incident_add_work_note: incidentJournalResultSchema,
|
|
211
|
+
sn_delete: deleteResultSchema,
|
|
212
|
+
sn_batch: batchResultSchema,
|
|
213
|
+
sn_aggregate: wrappedResultSchema,
|
|
214
|
+
sn_schema: schemaResultSchema,
|
|
215
|
+
sn_health: healthResultSchema,
|
|
216
|
+
sn_attach: attachmentResultEnvelopeSchema,
|
|
217
|
+
sn_relationships: relationshipResultSchema,
|
|
218
|
+
sn_syslog: queryResultSchema,
|
|
219
|
+
sn_codesearch: listResultSchema,
|
|
220
|
+
sn_discover: listResultSchema,
|
|
221
|
+
sn_atf: atfResultEnvelopeSchema,
|
|
222
|
+
sn_nl: naturalLanguageResultSchema,
|
|
223
|
+
sn_profile: profileResultSchema,
|
|
224
|
+
});
|
|
225
|
+
exports.productionToolOutputSchemas = Object.freeze({
|
|
226
|
+
sn_query: withStructuredResultEnvelope(productionDataSchemas.sn_query),
|
|
227
|
+
sn_get: withStructuredResultEnvelope(productionDataSchemas.sn_get),
|
|
228
|
+
sn_create: withStructuredResultEnvelope(productionDataSchemas.sn_create),
|
|
229
|
+
sn_update: withStructuredResultEnvelope(productionDataSchemas.sn_update),
|
|
230
|
+
sn_incident_add_comment: withStructuredResultEnvelope(productionDataSchemas.sn_incident_add_comment),
|
|
231
|
+
sn_incident_add_work_note: withStructuredResultEnvelope(productionDataSchemas.sn_incident_add_work_note),
|
|
232
|
+
sn_delete: withStructuredResultEnvelope(productionDataSchemas.sn_delete),
|
|
233
|
+
sn_batch: withStructuredResultEnvelope(productionDataSchemas.sn_batch),
|
|
234
|
+
sn_aggregate: withStructuredResultEnvelope(productionDataSchemas.sn_aggregate),
|
|
235
|
+
sn_schema: withStructuredResultEnvelope(productionDataSchemas.sn_schema),
|
|
236
|
+
sn_health: withStructuredResultEnvelope(productionDataSchemas.sn_health),
|
|
237
|
+
sn_attach: withStructuredResultEnvelope(productionDataSchemas.sn_attach),
|
|
238
|
+
sn_relationships: withStructuredResultEnvelope(productionDataSchemas.sn_relationships),
|
|
239
|
+
sn_syslog: withStructuredResultEnvelope(productionDataSchemas.sn_syslog),
|
|
240
|
+
sn_codesearch: withStructuredResultEnvelope(productionDataSchemas.sn_codesearch),
|
|
241
|
+
sn_discover: withStructuredResultEnvelope(productionDataSchemas.sn_discover),
|
|
242
|
+
sn_atf: withStructuredResultEnvelope(productionDataSchemas.sn_atf),
|
|
243
|
+
sn_nl: withStructuredResultEnvelope(productionDataSchemas.sn_nl),
|
|
244
|
+
sn_profile: withStructuredResultEnvelope(productionDataSchemas.sn_profile),
|
|
245
|
+
});
|
|
246
|
+
function isProductionToolName(name) {
|
|
247
|
+
return Object.hasOwn(exports.productionToolOutputSchemas, name);
|
|
248
|
+
}
|
|
249
|
+
function parseHandlerJson(result) {
|
|
250
|
+
if (result.content.length !== 1 || result.content[0]?.type !== "text") {
|
|
251
|
+
throw new TypeError("successful compatibility handler must return one text block");
|
|
252
|
+
}
|
|
253
|
+
try {
|
|
254
|
+
return JSON.parse(result.content[0].text);
|
|
255
|
+
}
|
|
256
|
+
catch {
|
|
257
|
+
throw new TypeError("successful compatibility handler returned non-JSON text");
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
function normalizedListResult(data) {
|
|
261
|
+
if (Array.isArray(data))
|
|
262
|
+
return { results: data };
|
|
263
|
+
if (typeof data !== "object" || data === null)
|
|
264
|
+
return data;
|
|
265
|
+
const results = Reflect.get(data, "results");
|
|
266
|
+
const warnings = Reflect.get(data, "warnings");
|
|
267
|
+
if (!Array.isArray(results))
|
|
268
|
+
return data;
|
|
269
|
+
return {
|
|
270
|
+
results,
|
|
271
|
+
...(Array.isArray(warnings) ? { warnings } : {}),
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
/** Recognize the reserved single-record hint wrapper, if present. */
|
|
275
|
+
function envelopeHint(data) {
|
|
276
|
+
if (typeof data !== "object" || data === null || Array.isArray(data)) {
|
|
277
|
+
return undefined;
|
|
278
|
+
}
|
|
279
|
+
const candidate = data;
|
|
280
|
+
const hint = candidate[exports.ENVELOPE_HINT_KEY];
|
|
281
|
+
if (typeof hint !== "string" || !Object.hasOwn(candidate, "record")) {
|
|
282
|
+
return undefined;
|
|
283
|
+
}
|
|
284
|
+
return { record: candidate.record, hint };
|
|
285
|
+
}
|
|
286
|
+
function normalizeToolData(name, args, data) {
|
|
287
|
+
switch (name) {
|
|
288
|
+
case "sn_get": {
|
|
289
|
+
const hinted = envelopeHint(data);
|
|
290
|
+
return hinted === undefined
|
|
291
|
+
? { record: data }
|
|
292
|
+
: { record: hinted.record, hint: hinted.hint };
|
|
293
|
+
}
|
|
294
|
+
case "sn_aggregate":
|
|
295
|
+
return { result: data };
|
|
296
|
+
case "sn_schema":
|
|
297
|
+
return { fields: data };
|
|
298
|
+
case "sn_health":
|
|
299
|
+
return { health: data };
|
|
300
|
+
case "sn_attach":
|
|
301
|
+
return { action: args.action, result: data };
|
|
302
|
+
case "sn_codesearch":
|
|
303
|
+
case "sn_discover":
|
|
304
|
+
return normalizedListResult(data);
|
|
305
|
+
case "sn_atf":
|
|
306
|
+
return { action: args.action, result: data };
|
|
307
|
+
case "sn_nl":
|
|
308
|
+
return { request: data };
|
|
309
|
+
default:
|
|
310
|
+
return data;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
function collectionArray(data) {
|
|
314
|
+
if (Array.isArray(data))
|
|
315
|
+
return data;
|
|
316
|
+
if (typeof data !== "object" || data === null)
|
|
317
|
+
return undefined;
|
|
318
|
+
const record = data;
|
|
319
|
+
if (Array.isArray(record.results))
|
|
320
|
+
return record.results;
|
|
321
|
+
if (Array.isArray(record.relationships))
|
|
322
|
+
return record.relationships;
|
|
323
|
+
if (Array.isArray(record.fields))
|
|
324
|
+
return record.fields;
|
|
325
|
+
if (Array.isArray(record.result))
|
|
326
|
+
return record.result;
|
|
327
|
+
if (typeof record.result === "object" && record.result !== null) {
|
|
328
|
+
const nestedResults = Reflect.get(record.result, "results");
|
|
329
|
+
if (Array.isArray(nestedResults))
|
|
330
|
+
return nestedResults;
|
|
331
|
+
}
|
|
332
|
+
if (typeof record.request === "object" && record.request !== null) {
|
|
333
|
+
const nestedResults = Reflect.get(record.request, "results");
|
|
334
|
+
if (Array.isArray(nestedResults))
|
|
335
|
+
return nestedResults;
|
|
336
|
+
}
|
|
337
|
+
return undefined;
|
|
338
|
+
}
|
|
339
|
+
function requestedLimit(args, maximum, data) {
|
|
340
|
+
const dataLimit = numericObjectField(data, "limit");
|
|
341
|
+
const candidate = typeof dataLimit === "number"
|
|
342
|
+
? dataLimit
|
|
343
|
+
: typeof args.limit === "number"
|
|
344
|
+
? args.limit
|
|
345
|
+
: undefined;
|
|
346
|
+
return typeof candidate === "number" && Number.isSafeInteger(candidate)
|
|
347
|
+
? Math.min(Math.max(candidate, 1), maximum)
|
|
348
|
+
: maximum;
|
|
349
|
+
}
|
|
350
|
+
function nestedObjectField(data, field, depth = 0) {
|
|
351
|
+
if (typeof data !== "object" || data === null || Array.isArray(data)) {
|
|
352
|
+
return undefined;
|
|
353
|
+
}
|
|
354
|
+
if (depth > 3)
|
|
355
|
+
return undefined;
|
|
356
|
+
const value = Reflect.get(data, field);
|
|
357
|
+
if (value !== undefined)
|
|
358
|
+
return value;
|
|
359
|
+
const meta = Reflect.get(data, "meta");
|
|
360
|
+
if (typeof meta === "object" && meta !== null && !Array.isArray(meta)) {
|
|
361
|
+
const metaValue = Reflect.get(meta, field);
|
|
362
|
+
if (metaValue !== undefined)
|
|
363
|
+
return metaValue;
|
|
364
|
+
}
|
|
365
|
+
for (const wrapper of ["request", "result"]) {
|
|
366
|
+
const nested = Reflect.get(data, wrapper);
|
|
367
|
+
if (typeof nested !== "object" || nested === null || Array.isArray(nested)) {
|
|
368
|
+
continue;
|
|
369
|
+
}
|
|
370
|
+
const nestedValue = nestedObjectField(nested, field, depth + 1);
|
|
371
|
+
if (nestedValue !== undefined)
|
|
372
|
+
return nestedValue;
|
|
373
|
+
}
|
|
374
|
+
return undefined;
|
|
375
|
+
}
|
|
376
|
+
function numericObjectField(data, field) {
|
|
377
|
+
const value = nestedObjectField(data, field);
|
|
378
|
+
return typeof value === "number" && Number.isSafeInteger(value)
|
|
379
|
+
? value
|
|
380
|
+
: undefined;
|
|
381
|
+
}
|
|
382
|
+
function booleanObjectField(data, field) {
|
|
383
|
+
const value = nestedObjectField(data, field);
|
|
384
|
+
return typeof value === "boolean" ? value : undefined;
|
|
385
|
+
}
|
|
386
|
+
function requestedOffset(args, data) {
|
|
387
|
+
const candidate = typeof args.offset === "number"
|
|
388
|
+
? args.offset
|
|
389
|
+
: numericObjectField(data, "offset");
|
|
390
|
+
return typeof candidate === "number" && Number.isSafeInteger(candidate)
|
|
391
|
+
? Math.max(candidate, 0)
|
|
392
|
+
: 0;
|
|
393
|
+
}
|
|
394
|
+
function byteLimit(name, args) {
|
|
395
|
+
if (name === "sn_attach" && args.action === "download") {
|
|
396
|
+
return exports.ATTACHMENT_RESULT_BYTE_LIMIT;
|
|
397
|
+
}
|
|
398
|
+
if ((name === "sn_query" || name === "sn_get") &&
|
|
399
|
+
typeof args.max_response_bytes === "number" &&
|
|
400
|
+
Number.isSafeInteger(args.max_response_bytes)) {
|
|
401
|
+
return Math.min(Math.max(args.max_response_bytes, utils_js_1.MAX_RESPONSE_BYTES_MIN), utils_js_1.MAX_RESPONSE_BYTES_MAX);
|
|
402
|
+
}
|
|
403
|
+
return exports.DEFAULT_RESULT_BYTE_LIMIT;
|
|
404
|
+
}
|
|
405
|
+
function resultKind(name, data) {
|
|
406
|
+
if (collectionArray(data))
|
|
407
|
+
return "collection";
|
|
408
|
+
if ([
|
|
409
|
+
"sn_create",
|
|
410
|
+
"sn_update",
|
|
411
|
+
"sn_incident_add_comment",
|
|
412
|
+
"sn_incident_add_work_note",
|
|
413
|
+
"sn_delete",
|
|
414
|
+
"sn_batch",
|
|
415
|
+
].includes(name)) {
|
|
416
|
+
return "operation";
|
|
417
|
+
}
|
|
418
|
+
return "single";
|
|
419
|
+
}
|
|
420
|
+
function effectiveOrder(name, args) {
|
|
421
|
+
switch (name) {
|
|
422
|
+
case "sn_query": {
|
|
423
|
+
const structured = Reflect.get(args, "structured_query");
|
|
424
|
+
const orderBy = typeof structured === "object" && structured !== null
|
|
425
|
+
? Reflect.get(structured, "order_by")
|
|
426
|
+
: undefined;
|
|
427
|
+
const structuredOrder = Array.isArray(orderBy)
|
|
428
|
+
? orderBy.flatMap((entry) => {
|
|
429
|
+
if (typeof entry !== "object" || entry === null)
|
|
430
|
+
return [];
|
|
431
|
+
const field = Reflect.get(entry, "field");
|
|
432
|
+
const direction = Reflect.get(entry, "direction");
|
|
433
|
+
return typeof field === "string"
|
|
434
|
+
? [`${direction === "desc" ? "-" : ""}${field}`]
|
|
435
|
+
: [];
|
|
436
|
+
})
|
|
437
|
+
: [];
|
|
438
|
+
const requestedOrder = structuredOrder.length > 0
|
|
439
|
+
? structuredOrder
|
|
440
|
+
: typeof args.orderby === "string" && args.orderby.length > 0
|
|
441
|
+
? [args.orderby]
|
|
442
|
+
: [];
|
|
443
|
+
return [...requestedOrder, "sys_id"].filter((value, index, values) => values.findIndex((candidate) => candidate.replace(/^-/, "") === value.replace(/^-/, "")) === index);
|
|
444
|
+
}
|
|
445
|
+
case "sn_syslog":
|
|
446
|
+
return ["-sys_created_on", "-sys_id"];
|
|
447
|
+
case "sn_schema":
|
|
448
|
+
return ["field", "sys_id"];
|
|
449
|
+
case "sn_relationships":
|
|
450
|
+
return ["depth_first", "relationship_sys_id"];
|
|
451
|
+
case "sn_codesearch":
|
|
452
|
+
return ["table", "sys_id"];
|
|
453
|
+
case "sn_discover":
|
|
454
|
+
return args.type === "apps" ? ["source", "sys_id"] : ["sys_id"];
|
|
455
|
+
case "sn_aggregate":
|
|
456
|
+
return typeof args.group_by === "string" ? [args.group_by] : ["aggregate"];
|
|
457
|
+
case "sn_atf":
|
|
458
|
+
return args.action === "run-suite"
|
|
459
|
+
? ["-sys_created_on", "-sys_id"]
|
|
460
|
+
: ["sys_id"];
|
|
461
|
+
default:
|
|
462
|
+
return ["sys_id"];
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
function synchronizeRecordCount(data, count) {
|
|
466
|
+
if (typeof data !== "object" || data === null || Array.isArray(data))
|
|
467
|
+
return;
|
|
468
|
+
const record = data;
|
|
469
|
+
if (Object.hasOwn(record, "record_count"))
|
|
470
|
+
record.record_count = count;
|
|
471
|
+
for (const wrapper of ["request", "result"]) {
|
|
472
|
+
const nested = record[wrapper];
|
|
473
|
+
if (typeof nested === "object" && nested !== null) {
|
|
474
|
+
synchronizeRecordCount(nested, count);
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
function synchronizePagingFields(data, pagination, truncation, depth = 0) {
|
|
479
|
+
if (pagination.mode !== "offset" ||
|
|
480
|
+
typeof data !== "object" ||
|
|
481
|
+
data === null ||
|
|
482
|
+
Array.isArray(data) ||
|
|
483
|
+
depth > 3) {
|
|
484
|
+
return;
|
|
485
|
+
}
|
|
486
|
+
const record = data;
|
|
487
|
+
const synchronizeObject = (target) => {
|
|
488
|
+
if (Object.hasOwn(target, "record_count")) {
|
|
489
|
+
target.record_count = pagination.returned;
|
|
490
|
+
}
|
|
491
|
+
if (Object.hasOwn(target, "limit"))
|
|
492
|
+
target.limit = pagination.limit;
|
|
493
|
+
if (Object.hasOwn(target, "offset"))
|
|
494
|
+
target.offset = pagination.offset;
|
|
495
|
+
if (Object.hasOwn(target, "has_more")) {
|
|
496
|
+
target.has_more = pagination.has_more;
|
|
497
|
+
}
|
|
498
|
+
if (pagination.has_more && pagination.next_offset !== undefined) {
|
|
499
|
+
if (Object.hasOwn(target, "next_offset")) {
|
|
500
|
+
target.next_offset = pagination.next_offset;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
else if (Object.hasOwn(target, "next_offset")) {
|
|
504
|
+
delete target.next_offset;
|
|
505
|
+
}
|
|
506
|
+
if (Object.hasOwn(target, "truncated")) {
|
|
507
|
+
target.truncated = truncation.truncated;
|
|
508
|
+
}
|
|
509
|
+
if (Object.hasOwn(target, "dropped_records")) {
|
|
510
|
+
if (truncation.dropped_records === undefined) {
|
|
511
|
+
delete target.dropped_records;
|
|
512
|
+
}
|
|
513
|
+
else {
|
|
514
|
+
target.dropped_records = truncation.dropped_records;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
};
|
|
518
|
+
synchronizeObject(record);
|
|
519
|
+
if (typeof record.meta === "object" && record.meta !== null) {
|
|
520
|
+
synchronizeObject(record.meta);
|
|
521
|
+
}
|
|
522
|
+
for (const wrapper of ["request", "result"]) {
|
|
523
|
+
synchronizePagingFields(record[wrapper], pagination, truncation, depth + 1);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
function envelopeCompatibilityResult(name, args, result) {
|
|
527
|
+
if (result.isError)
|
|
528
|
+
return result;
|
|
529
|
+
const data = normalizeToolData(name, args, parseHandlerJson(result));
|
|
530
|
+
const records = collectionArray(data);
|
|
531
|
+
const maximumRecords = exports.DEFAULT_RESULT_RECORD_LIMIT;
|
|
532
|
+
const limit = requestedLimit(args, maximumRecords, data);
|
|
533
|
+
const originalCount = records?.length ?? 1;
|
|
534
|
+
if (records && records.length > limit)
|
|
535
|
+
records.splice(limit);
|
|
536
|
+
const returned = records?.length ?? 1;
|
|
537
|
+
synchronizeRecordCount(data, returned);
|
|
538
|
+
const collection = records !== undefined;
|
|
539
|
+
const offset = requestedOffset(args, data);
|
|
540
|
+
const explicitHasMore = booleanObjectField(data, "has_more");
|
|
541
|
+
const explicitNextOffset = numericObjectField(data, "next_offset");
|
|
542
|
+
const legacyTruncated = booleanObjectField(data, "truncated") === true;
|
|
543
|
+
const priorDropped = numericObjectField(data, "dropped_records") ?? 0;
|
|
544
|
+
let hasMore = collection &&
|
|
545
|
+
(originalCount > returned ||
|
|
546
|
+
explicitHasMore === true ||
|
|
547
|
+
(explicitHasMore === undefined && returned >= limit));
|
|
548
|
+
let nextOffset;
|
|
549
|
+
let recovery = legacyTruncated ? "adjust_request_and_retry_same_offset" : undefined;
|
|
550
|
+
if (hasMore) {
|
|
551
|
+
if (explicitNextOffset !== undefined && explicitNextOffset > offset) {
|
|
552
|
+
nextOffset = explicitNextOffset;
|
|
553
|
+
}
|
|
554
|
+
else if (originalCount > returned && returned > 0) {
|
|
555
|
+
nextOffset = offset + returned;
|
|
556
|
+
}
|
|
557
|
+
else if (returned > 0) {
|
|
558
|
+
// An explicit has_more without a usable next offset means the upstream
|
|
559
|
+
// window, not merely the delivered rows, is authoritative. Advancing by
|
|
560
|
+
// the effective limit prevents ACL-filtered windows from being replayed.
|
|
561
|
+
nextOffset = offset + limit;
|
|
562
|
+
}
|
|
563
|
+
else {
|
|
564
|
+
hasMore = false;
|
|
565
|
+
recovery = "adjust_request_and_retry_same_offset";
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
const droppedByRecordLimit = originalCount - returned;
|
|
569
|
+
const droppedRecords = priorDropped + droppedByRecordLimit;
|
|
570
|
+
const truncation = legacyTruncated || droppedByRecordLimit > 0
|
|
571
|
+
? {
|
|
572
|
+
truncated: true,
|
|
573
|
+
reason: legacyTruncated ? "byte_limit" : "record_limit",
|
|
574
|
+
...(droppedRecords > 0 ? { dropped_records: droppedRecords } : {}),
|
|
575
|
+
}
|
|
576
|
+
: { truncated: false };
|
|
577
|
+
const metadata = {
|
|
578
|
+
kind: resultKind(name, data),
|
|
579
|
+
record_count: returned,
|
|
580
|
+
limits: {
|
|
581
|
+
max_records: maximumRecords,
|
|
582
|
+
max_bytes: byteLimit(name, args),
|
|
583
|
+
},
|
|
584
|
+
pagination: collection
|
|
585
|
+
? {
|
|
586
|
+
mode: "offset",
|
|
587
|
+
limit,
|
|
588
|
+
offset,
|
|
589
|
+
returned,
|
|
590
|
+
has_more: hasMore,
|
|
591
|
+
...(hasMore && nextOffset !== undefined
|
|
592
|
+
? { next_offset: nextOffset }
|
|
593
|
+
: {}),
|
|
594
|
+
...(recovery === undefined ? {} : { recovery }),
|
|
595
|
+
order_by: effectiveOrder(name, args),
|
|
596
|
+
}
|
|
597
|
+
: { mode: "none" },
|
|
598
|
+
truncation,
|
|
599
|
+
};
|
|
600
|
+
synchronizePagingFields(data, metadata.pagination, metadata.truncation);
|
|
601
|
+
return {
|
|
602
|
+
...result,
|
|
603
|
+
structuredContent: { data, metadata },
|
|
604
|
+
};
|
|
605
|
+
}
|
|
606
|
+
function isMutableEnvelope(candidate) {
|
|
607
|
+
if (typeof candidate !== "object" || candidate === null)
|
|
608
|
+
return false;
|
|
609
|
+
const record = candidate;
|
|
610
|
+
return (Object.hasOwn(record, "data") &&
|
|
611
|
+
typeof record.metadata === "object" &&
|
|
612
|
+
record.metadata !== null &&
|
|
613
|
+
Object.hasOwn(record.metadata, "limits"));
|
|
614
|
+
}
|
|
615
|
+
function renderEnvelopeText(envelope) {
|
|
616
|
+
const pagination = envelope.metadata.pagination;
|
|
617
|
+
const continuation = pagination.mode === "offset" && pagination.has_more
|
|
618
|
+
? ` More records are available at offset ${pagination.next_offset}.`
|
|
619
|
+
: "";
|
|
620
|
+
const recovery = pagination.mode === "offset" && pagination.recovery !== undefined
|
|
621
|
+
? pagination.has_more
|
|
622
|
+
? ` 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.`
|
|
623
|
+
: ` No automatic post-window continuation is available; adjust the request and retry offset ${pagination.offset}.`
|
|
624
|
+
: "";
|
|
625
|
+
const truncated = envelope.metadata.truncation.truncated
|
|
626
|
+
? ` Output was truncated by ${envelope.metadata.truncation.reason}.`
|
|
627
|
+
: "";
|
|
628
|
+
return (`Success for profile ${JSON.stringify(envelope.profile)}: ` +
|
|
629
|
+
`${envelope.metadata.kind} result with ${envelope.metadata.record_count} ` +
|
|
630
|
+
`record${envelope.metadata.record_count === 1 ? "" : "s"}.` +
|
|
631
|
+
continuation +
|
|
632
|
+
recovery +
|
|
633
|
+
truncated);
|
|
634
|
+
}
|
|
635
|
+
function resultBytes(result) {
|
|
636
|
+
return Buffer.byteLength(JSON.stringify(result), "utf8");
|
|
637
|
+
}
|
|
638
|
+
function candidateResult(base, envelope) {
|
|
639
|
+
return {
|
|
640
|
+
...base,
|
|
641
|
+
content: [{ type: "text", text: renderEnvelopeText(envelope) }],
|
|
642
|
+
structuredContent: envelope,
|
|
643
|
+
};
|
|
644
|
+
}
|
|
645
|
+
function applyByteTruncation(envelope, droppedRecords) {
|
|
646
|
+
envelope.metadata.truncation = {
|
|
647
|
+
truncated: true,
|
|
648
|
+
reason: "byte_limit",
|
|
649
|
+
...(droppedRecords > 0 ? { dropped_records: droppedRecords } : {}),
|
|
650
|
+
};
|
|
651
|
+
}
|
|
652
|
+
function truncateLongestString(data) {
|
|
653
|
+
const candidates = [];
|
|
654
|
+
const visit = (value) => {
|
|
655
|
+
if (Array.isArray(value)) {
|
|
656
|
+
value.forEach((entry, index) => {
|
|
657
|
+
if (typeof entry === "string") {
|
|
658
|
+
candidates.push({ parent: value, key: index, value: entry });
|
|
659
|
+
}
|
|
660
|
+
else {
|
|
661
|
+
visit(entry);
|
|
662
|
+
}
|
|
663
|
+
});
|
|
664
|
+
return;
|
|
665
|
+
}
|
|
666
|
+
if (typeof value !== "object" || value === null)
|
|
667
|
+
return;
|
|
668
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
669
|
+
if (typeof entry === "string") {
|
|
670
|
+
candidates.push({
|
|
671
|
+
parent: value,
|
|
672
|
+
key,
|
|
673
|
+
value: entry,
|
|
674
|
+
});
|
|
675
|
+
}
|
|
676
|
+
else {
|
|
677
|
+
visit(entry);
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
};
|
|
681
|
+
visit(data);
|
|
682
|
+
candidates.sort((left, right) => right.value.length - left.value.length);
|
|
683
|
+
const longest = candidates[0];
|
|
684
|
+
if (!longest || longest.value.length <= 64)
|
|
685
|
+
return false;
|
|
686
|
+
const keep = Math.max(32, Math.floor(longest.value.length / 2));
|
|
687
|
+
const shortened = longest.value.slice(0, keep) +
|
|
688
|
+
`...[truncated ${longest.value.length - keep} characters]`;
|
|
689
|
+
if (Array.isArray(longest.parent) && typeof longest.key === "number") {
|
|
690
|
+
longest.parent[longest.key] = shortened;
|
|
691
|
+
}
|
|
692
|
+
else if (!Array.isArray(longest.parent) && typeof longest.key === "string") {
|
|
693
|
+
longest.parent[longest.key] = shortened;
|
|
694
|
+
}
|
|
695
|
+
else {
|
|
696
|
+
return false;
|
|
697
|
+
}
|
|
698
|
+
return true;
|
|
699
|
+
}
|
|
700
|
+
/**
|
|
701
|
+
* Replace handler text with a coordinator-rendered summary and enforce the
|
|
702
|
+
* declared final MCP result byte limit. Returns undefined when even a fully
|
|
703
|
+
* minimized valid envelope cannot fit, causing the caller to fail closed.
|
|
704
|
+
*/
|
|
705
|
+
function finalizeEnvelopeResult(result) {
|
|
706
|
+
if (!isMutableEnvelope(result.structuredContent))
|
|
707
|
+
return result;
|
|
708
|
+
const sourceEnvelope = JSON.parse(JSON.stringify(result.structuredContent));
|
|
709
|
+
let candidate = candidateResult(result, sourceEnvelope);
|
|
710
|
+
const maximum = sourceEnvelope.metadata.limits.max_bytes;
|
|
711
|
+
if (resultBytes(candidate) <= maximum)
|
|
712
|
+
return candidate;
|
|
713
|
+
const sourceRecords = collectionArray(sourceEnvelope.data);
|
|
714
|
+
let envelope = sourceEnvelope;
|
|
715
|
+
if (sourceRecords) {
|
|
716
|
+
const originalRecordCount = sourceRecords.length;
|
|
717
|
+
const priorDropped = sourceEnvelope.metadata.truncation.dropped_records ?? 0;
|
|
718
|
+
const envelopeForCount = (count) => {
|
|
719
|
+
const probe = JSON.parse(JSON.stringify(sourceEnvelope));
|
|
720
|
+
const probeRecords = collectionArray(probe.data);
|
|
721
|
+
if (!probeRecords)
|
|
722
|
+
throw new TypeError("collection envelope lost its records");
|
|
723
|
+
probeRecords.splice(count);
|
|
724
|
+
probe.metadata.record_count = count;
|
|
725
|
+
synchronizeRecordCount(probe.data, count);
|
|
726
|
+
if (probe.metadata.pagination.mode === "offset") {
|
|
727
|
+
probe.metadata.pagination.returned = count;
|
|
728
|
+
if (count < originalRecordCount) {
|
|
729
|
+
probe.metadata.pagination.recovery =
|
|
730
|
+
"adjust_request_and_retry_same_offset";
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
applyByteTruncation(probe, priorDropped + originalRecordCount - count);
|
|
734
|
+
synchronizePagingFields(probe.data, probe.metadata.pagination, probe.metadata.truncation);
|
|
735
|
+
return probe;
|
|
736
|
+
};
|
|
737
|
+
let low = 0;
|
|
738
|
+
let high = originalRecordCount;
|
|
739
|
+
let best = -1;
|
|
740
|
+
while (low <= high) {
|
|
741
|
+
const midpoint = Math.floor((low + high) / 2);
|
|
742
|
+
const probe = candidateResult(result, envelopeForCount(midpoint));
|
|
743
|
+
if (resultBytes(probe) <= maximum) {
|
|
744
|
+
best = midpoint;
|
|
745
|
+
low = midpoint + 1;
|
|
746
|
+
}
|
|
747
|
+
else {
|
|
748
|
+
high = midpoint - 1;
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
// Collections are fitted only by removing complete rows. In particular,
|
|
752
|
+
// never shorten a field merely to force one row into the envelope: callers
|
|
753
|
+
// can retry this exact window with a narrower projection/larger budget,
|
|
754
|
+
// while next_offset (when present) remains the separately authoritative
|
|
755
|
+
// post-window resume point.
|
|
756
|
+
envelope = envelopeForCount(Math.max(best, 0));
|
|
757
|
+
candidate = candidateResult(result, envelope);
|
|
758
|
+
return resultBytes(candidate) <= maximum ? candidate : undefined;
|
|
759
|
+
}
|
|
760
|
+
while (resultBytes(candidate) > maximum && truncateLongestString(envelope.data)) {
|
|
761
|
+
applyByteTruncation(envelope, envelope.metadata.truncation.dropped_records ?? 0);
|
|
762
|
+
synchronizePagingFields(envelope.data, envelope.metadata.pagination, envelope.metadata.truncation);
|
|
763
|
+
candidate = candidateResult(result, envelope);
|
|
764
|
+
}
|
|
765
|
+
return resultBytes(candidate) <= maximum ? candidate : undefined;
|
|
766
|
+
}
|
|
767
|
+
//# sourceMappingURL=result-envelope.js.map
|