@onlyflows/servicenow-mcp 2.2.0-dev.5 → 2.2.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 +6 -3
- package/CHANGELOG.md +312 -0
- package/README.md +504 -101
- package/dist/atf-form.d.ts +3 -15
- package/dist/atf-form.d.ts.map +1 -1
- package/dist/atf-result-cache.d.ts +23 -59
- package/dist/atf-result-cache.d.ts.map +1 -1
- package/dist/auth.d.ts +1 -0
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +17 -0
- package/dist/auth.js.map +1 -1
- package/dist/batch-limits.d.ts +18 -0
- package/dist/batch-limits.d.ts.map +1 -0
- package/dist/batch-limits.js +21 -0
- package/dist/batch-limits.js.map +1 -0
- package/dist/bounded-input.d.ts +49 -0
- package/dist/bounded-input.d.ts.map +1 -0
- package/dist/bounded-input.js +81 -0
- package/dist/bounded-input.js.map +1 -0
- package/dist/client.d.ts +25 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +92 -18
- package/dist/client.js.map +1 -1
- package/dist/command-resolution.d.ts +53 -0
- package/dist/command-resolution.d.ts.map +1 -0
- package/dist/command-resolution.js +154 -0
- package/dist/command-resolution.js.map +1 -0
- package/dist/encoded-query-policy.d.ts +11 -0
- package/dist/encoded-query-policy.d.ts.map +1 -1
- package/dist/encoded-query-policy.js +10 -1
- package/dist/encoded-query-policy.js.map +1 -1
- package/dist/field-policy.d.ts +82 -3
- package/dist/field-policy.d.ts.map +1 -1
- package/dist/field-policy.js +609 -80
- package/dist/field-policy.js.map +1 -1
- package/dist/html-response.d.ts +34 -0
- package/dist/html-response.d.ts.map +1 -0
- package/dist/html-response.js +72 -0
- package/dist/html-response.js.map +1 -0
- package/dist/http-runtime.d.ts.map +1 -1
- package/dist/http-runtime.js +8 -0
- package/dist/http-runtime.js.map +1 -1
- package/dist/incident-journal-policy.d.ts +27 -5
- package/dist/incident-journal-policy.d.ts.map +1 -1
- package/dist/incident-journal-policy.js +34 -21
- package/dist/incident-journal-policy.js.map +1 -1
- package/dist/json-shape-limits.d.ts +71 -0
- package/dist/json-shape-limits.d.ts.map +1 -0
- package/dist/json-shape-limits.js +131 -0
- package/dist/json-shape-limits.js.map +1 -0
- package/dist/metadata-cache.d.ts +46 -21
- package/dist/metadata-cache.d.ts.map +1 -1
- package/dist/metadata-cache.js +228 -195
- package/dist/metadata-cache.js.map +1 -1
- package/dist/policy-card.d.ts +131 -0
- package/dist/policy-card.d.ts.map +1 -0
- package/dist/policy-card.js +409 -0
- package/dist/policy-card.js.map +1 -0
- package/dist/profile-admin.d.ts.map +1 -1
- package/dist/profile-admin.js +3 -0
- package/dist/profile-admin.js.map +1 -1
- package/dist/profile-manager.d.ts +17 -3
- package/dist/profile-manager.d.ts.map +1 -1
- package/dist/profile-manager.js +98 -6
- package/dist/profile-manager.js.map +1 -1
- package/dist/record-identifier-policy.d.ts +2 -8
- package/dist/record-identifier-policy.d.ts.map +1 -1
- package/dist/risky-grants.d.ts +25 -0
- package/dist/risky-grants.d.ts.map +1 -0
- package/dist/risky-grants.js +77 -0
- package/dist/risky-grants.js.map +1 -0
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +74 -0
- package/dist/server.js.map +1 -1
- package/dist/setup-prompts.js +1 -0
- package/dist/setup-prompts.js.map +1 -1
- package/dist/setup.d.ts +8 -1
- package/dist/setup.d.ts.map +1 -1
- package/dist/setup.js +492 -111
- package/dist/setup.js.map +1 -1
- package/dist/stdio-runtime.d.ts +15 -0
- package/dist/stdio-runtime.d.ts.map +1 -1
- package/dist/stdio-runtime.js +60 -0
- package/dist/stdio-runtime.js.map +1 -1
- package/dist/structured-query.d.ts +100 -115
- package/dist/structured-query.d.ts.map +1 -1
- package/dist/structured-query.js +132 -16
- package/dist/structured-query.js.map +1 -1
- package/dist/table-policy.d.ts +10 -4
- package/dist/table-policy.d.ts.map +1 -1
- package/dist/table-policy.js +1 -1
- package/dist/table-policy.js.map +1 -1
- package/dist/tool-error.d.ts +18 -0
- package/dist/tool-error.d.ts.map +1 -1
- package/dist/tool-error.js +69 -0
- package/dist/tool-error.js.map +1 -1
- package/dist/tool-table-access.d.ts.map +1 -1
- package/dist/tool-table-access.js +32 -2
- package/dist/tool-table-access.js.map +1 -1
- package/dist/tools/aggregate.d.ts +13 -19
- package/dist/tools/aggregate.d.ts.map +1 -1
- package/dist/tools/aggregate.js +2 -3
- package/dist/tools/aggregate.js.map +1 -1
- package/dist/tools/all-fields-cap.d.ts.map +1 -1
- package/dist/tools/all-fields-cap.js +3 -38
- package/dist/tools/all-fields-cap.js.map +1 -1
- package/dist/tools/atf-author-module.d.ts +28 -7
- package/dist/tools/atf-author-module.d.ts.map +1 -1
- package/dist/tools/atf-author-module.js +4 -2
- package/dist/tools/atf-author-module.js.map +1 -1
- package/dist/tools/atf-contracts.d.ts +135 -829
- package/dist/tools/atf-contracts.d.ts.map +1 -1
- package/dist/tools/atf-shared.js +2 -2
- package/dist/tools/atf-shared.js.map +1 -1
- package/dist/tools/atf-step-catalog.d.ts +3 -7
- package/dist/tools/atf-step-catalog.d.ts.map +1 -1
- package/dist/tools/atf-step-catalog.js +12 -5
- package/dist/tools/atf-step-catalog.js.map +1 -1
- package/dist/tools/atf.d.ts +8 -24
- package/dist/tools/atf.d.ts.map +1 -1
- package/dist/tools/atf.js +2 -2
- package/dist/tools/atf.js.map +1 -1
- package/dist/tools/attach.d.ts +6 -22
- package/dist/tools/attach.d.ts.map +1 -1
- package/dist/tools/batch.d.ts +18 -71
- package/dist/tools/batch.d.ts.map +1 -1
- package/dist/tools/batch.js +544 -96
- package/dist/tools/batch.js.map +1 -1
- package/dist/tools/catalog.d.ts +2 -2
- package/dist/tools/catalog.d.ts.map +1 -1
- package/dist/tools/catalog.js +7 -1
- package/dist/tools/catalog.js.map +1 -1
- package/dist/tools/codesearch.d.ts +1 -13
- package/dist/tools/codesearch.d.ts.map +1 -1
- package/dist/tools/create-module.d.ts +9 -14
- package/dist/tools/create-module.d.ts.map +1 -1
- package/dist/tools/create-module.js +4 -8
- package/dist/tools/create-module.js.map +1 -1
- package/dist/tools/delete.d.ts +1 -9
- package/dist/tools/delete.d.ts.map +1 -1
- package/dist/tools/delete.js +2 -3
- package/dist/tools/delete.js.map +1 -1
- package/dist/tools/discover.d.ts +6 -14
- package/dist/tools/discover.d.ts.map +1 -1
- package/dist/tools/flow-view-contracts.d.ts +1023 -0
- package/dist/tools/flow-view-contracts.d.ts.map +1 -0
- package/dist/tools/flow-view-contracts.js +570 -0
- package/dist/tools/flow-view-contracts.js.map +1 -0
- package/dist/tools/flow-view-graph.d.ts +86 -0
- package/dist/tools/flow-view-graph.d.ts.map +1 -0
- package/dist/tools/flow-view-graph.js +993 -0
- package/dist/tools/flow-view-graph.js.map +1 -0
- package/dist/tools/flow-view-gzip.d.ts +52 -0
- package/dist/tools/flow-view-gzip.d.ts.map +1 -0
- package/dist/tools/flow-view-gzip.js +179 -0
- package/dist/tools/flow-view-gzip.js.map +1 -0
- package/dist/tools/flow-view-mermaid.d.ts +37 -0
- package/dist/tools/flow-view-mermaid.d.ts.map +1 -0
- package/dist/tools/flow-view-mermaid.js +337 -0
- package/dist/tools/flow-view-mermaid.js.map +1 -0
- package/dist/tools/flow-view-module.d.ts +61 -0
- package/dist/tools/flow-view-module.d.ts.map +1 -0
- package/dist/tools/flow-view-module.js +378 -0
- package/dist/tools/flow-view-module.js.map +1 -0
- package/dist/tools/flow-view-policy.d.ts +106 -0
- package/dist/tools/flow-view-policy.d.ts.map +1 -0
- package/dist/tools/flow-view-policy.js +461 -0
- package/dist/tools/flow-view-policy.js.map +1 -0
- package/dist/tools/flow-view-renderers.d.ts +68 -0
- package/dist/tools/flow-view-renderers.d.ts.map +1 -0
- package/dist/tools/flow-view-renderers.js +513 -0
- package/dist/tools/flow-view-renderers.js.map +1 -0
- package/dist/tools/flow-view-table.d.ts +115 -0
- package/dist/tools/flow-view-table.d.ts.map +1 -0
- package/dist/tools/flow-view-table.js +1002 -0
- package/dist/tools/flow-view-table.js.map +1 -0
- package/dist/tools/get-module.d.ts +36 -42
- package/dist/tools/get-module.d.ts.map +1 -1
- package/dist/tools/health.d.ts +9 -6
- package/dist/tools/health.d.ts.map +1 -1
- package/dist/tools/incident-journal-module.d.ts +8 -7
- package/dist/tools/incident-journal-module.d.ts.map +1 -1
- package/dist/tools/index.d.ts +4 -4
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js +43 -6
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/markdown.d.ts +27 -0
- package/dist/tools/markdown.d.ts.map +1 -1
- package/dist/tools/markdown.js +104 -1
- package/dist/tools/markdown.js.map +1 -1
- package/dist/tools/nl.d.ts +1 -11
- package/dist/tools/nl.d.ts.map +1 -1
- package/dist/tools/profile.d.ts +1 -1
- package/dist/tools/profile.d.ts.map +1 -1
- package/dist/tools/profile.js +19 -1
- package/dist/tools/profile.js.map +1 -1
- package/dist/tools/query-module.d.ts +59 -71
- package/dist/tools/query-module.d.ts.map +1 -1
- package/dist/tools/query-module.js +2 -2
- package/dist/tools/query-module.js.map +1 -1
- package/dist/tools/record-write-shared.d.ts.map +1 -1
- package/dist/tools/record-write-shared.js +19 -2
- package/dist/tools/record-write-shared.js.map +1 -1
- package/dist/tools/relationships.d.ts +6 -22
- package/dist/tools/relationships.d.ts.map +1 -1
- package/dist/tools/result-envelope.d.ts +2255 -1357
- package/dist/tools/result-envelope.d.ts.map +1 -1
- package/dist/tools/result-envelope.js +24 -2
- package/dist/tools/result-envelope.js.map +1 -1
- package/dist/tools/schema-module.d.ts +36 -21
- package/dist/tools/schema-module.d.ts.map +1 -1
- package/dist/tools/schema-module.js +106 -65
- package/dist/tools/schema-module.js.map +1 -1
- package/dist/tools/script.d.ts +1 -11
- package/dist/tools/script.d.ts.map +1 -1
- package/dist/tools/syslog.d.ts +11 -18
- package/dist/tools/syslog.d.ts.map +1 -1
- package/dist/tools/syslog.js +2 -3
- package/dist/tools/syslog.js.map +1 -1
- package/dist/tools/table-hierarchy.d.ts +35 -0
- package/dist/tools/table-hierarchy.d.ts.map +1 -0
- package/dist/tools/table-hierarchy.js +74 -0
- package/dist/tools/table-hierarchy.js.map +1 -0
- package/dist/tools/tool-module.d.ts +40 -15
- package/dist/tools/tool-module.d.ts.map +1 -1
- package/dist/tools/tool-module.js +755 -205
- package/dist/tools/tool-module.js.map +1 -1
- package/dist/tools/update-module.d.ts +11 -17
- package/dist/tools/update-module.d.ts.map +1 -1
- package/dist/tools/update-module.js +10 -12
- package/dist/tools/update-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/dist/write-value-policy.d.ts +20 -12
- package/dist/write-value-policy.d.ts.map +1 -1
- package/dist/write-value-policy.js +179 -67
- package/dist/write-value-policy.js.map +1 -1
- package/docs/ADDING-TOOLS.md +22 -13
- package/docs/CLIENT-SETUP.md +110 -52
- package/docs/CROSS-CLIENT-RELEASE-MATRIX.md +2 -2
- package/docs/FLOW-VIEW.md +214 -0
- package/docs/PRODUCTION-SECURITY.md +154 -25
- package/docs/RELEASE-VALIDATION.md +2 -2
- package/docs/TESTING-OAUTH.md +1 -1
- package/docs/V2-MIGRATION.md +404 -30
- package/package.json +21 -6
- package/scripts/release-validate.mjs +56 -0
- package/server.json +22 -0
- package/tools/inspector/package-lock.json +163 -201
package/dist/tools/batch.js
CHANGED
|
@@ -2,13 +2,18 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.schema = exports.definition = void 0;
|
|
4
4
|
exports.handler = handler;
|
|
5
|
+
const node_buffer_1 = require("node:buffer");
|
|
5
6
|
const zod_1 = require("zod");
|
|
7
|
+
const client_js_1 = require("../client.js");
|
|
8
|
+
const batch_limits_js_1 = require("../batch-limits.js");
|
|
9
|
+
const metadata_cache_js_1 = require("../metadata-cache.js");
|
|
6
10
|
const encoded_query_policy_js_1 = require("../encoded-query-policy.js");
|
|
7
11
|
const field_policy_js_1 = require("../field-policy.js");
|
|
8
|
-
const incident_journal_policy_js_1 = require("../incident-journal-policy.js");
|
|
9
12
|
const utils_js_1 = require("../utils.js");
|
|
10
13
|
const servicenow_identifiers_js_1 = require("../servicenow-identifiers.js");
|
|
11
14
|
const structured_query_js_1 = require("../structured-query.js");
|
|
15
|
+
const tool_error_js_1 = require("../tool-error.js");
|
|
16
|
+
const write_value_policy_js_1 = require("../write-value-policy.js");
|
|
12
17
|
const structuredWriteQuerySchema = structured_query_js_1.structuredQuerySchema.refine((query) => query.filter !== undefined, { message: "Batch mutations require structured_query.filter" });
|
|
13
18
|
exports.definition = {
|
|
14
19
|
name: "sn_batch",
|
|
@@ -24,44 +29,42 @@ exports.definition = {
|
|
|
24
29
|
},
|
|
25
30
|
};
|
|
26
31
|
exports.schema = zod_1.z
|
|
27
|
-
.
|
|
32
|
+
.strictObject({
|
|
28
33
|
table: zod_1.z.string().describe("ServiceNow table name"),
|
|
29
34
|
structured_query: structuredWriteQuerySchema.describe("Required policy-authorized structured filter selecting records. Raw encoded queries are prohibited."),
|
|
30
35
|
action: zod_1.z
|
|
31
36
|
.enum(["update", "delete"])
|
|
32
37
|
.describe("Operation to perform: update or delete"),
|
|
33
38
|
fields: zod_1.z
|
|
34
|
-
.record(zod_1.z.unknown())
|
|
39
|
+
.record(zod_1.z.string(), zod_1.z.unknown())
|
|
35
40
|
.optional()
|
|
36
41
|
.describe('JSON fields to set on each record (required for update action). e.g. {"state":"7","close_notes":"Bulk closed"}'),
|
|
37
42
|
limit: zod_1.z
|
|
38
43
|
.number()
|
|
39
44
|
.int()
|
|
40
45
|
.min(1)
|
|
41
|
-
.max(
|
|
46
|
+
.max(batch_limits_js_1.BATCH_LIMITS.maxLimit)
|
|
42
47
|
.optional()
|
|
43
|
-
.default(
|
|
48
|
+
.default(batch_limits_js_1.BATCH_LIMITS.defaultLimit)
|
|
44
49
|
.describe("Max records to affect (default 200, safety cap 10000)"),
|
|
45
50
|
confirm: zod_1.z
|
|
46
51
|
.boolean()
|
|
47
52
|
.optional()
|
|
48
53
|
.default(false)
|
|
49
54
|
.describe("Set to true to actually execute. Default is dry-run."),
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
async function handler(args, client, _config, _context) {
|
|
55
|
+
}, encoded_query_policy_js_1.ENCODED_QUERY_STRICT_OBJECT_PARAMS);
|
|
56
|
+
async function handler(args, client, _config, context) {
|
|
53
57
|
try {
|
|
54
58
|
(0, encoded_query_policy_js_1.rejectRawEncodedWrite)(args.query);
|
|
55
|
-
// The append-only journal discipline is uniform across every write path.
|
|
56
|
-
// Without this, sn_batch was the one way to set comments/work_notes as
|
|
57
|
-
// generic fields, which is exactly what makes the journal audit trail
|
|
58
|
-
// meaningless. Matches sn_create and sn_update.
|
|
59
|
-
(0, incident_journal_policy_js_1.rejectGenericIncidentJournalFields)(args.fields);
|
|
60
59
|
if (args.action === "update" && !args.fields) {
|
|
61
60
|
return (0, utils_js_1.err)("--fields is required for update action");
|
|
62
61
|
}
|
|
62
|
+
// The same field and value policy as sn_create and sn_update, including
|
|
63
|
+
// the journal bounds for comments/work_notes. Preflight already applied
|
|
64
|
+
// it; repeating it keeps a direct handler call equally bounded, and the
|
|
65
|
+
// canonical strings are what goes upstream.
|
|
63
66
|
const writableFields = args.action === "update"
|
|
64
|
-
? (0,
|
|
67
|
+
? (0, write_value_policy_js_1.prepareWriteFieldValues)("update", args.table, args.fields)
|
|
65
68
|
: undefined;
|
|
66
69
|
const readableFields = (0, field_policy_js_1.preparedReadableFields)(args, args.table) ??
|
|
67
70
|
(0, field_policy_js_1.resolveReadableFields)(args.table, { fields: "sys_id" });
|
|
@@ -69,9 +72,13 @@ async function handler(args, client, _config, _context) {
|
|
|
69
72
|
if (structuredPlan.structuredQuery.filter === undefined) {
|
|
70
73
|
throw new encoded_query_policy_js_1.EncodedQueryPolicyError("encoded_write_prohibited");
|
|
71
74
|
}
|
|
72
|
-
const limit = Math.min(args.limit,
|
|
73
|
-
// Step 1: Query matching records (sys_id only)
|
|
74
|
-
|
|
75
|
+
const limit = Math.min(args.limit, batch_limits_js_1.BATCH_LIMITS.maxLimit);
|
|
76
|
+
// Step 1: Query matching records (sys_id only). These are the records the
|
|
77
|
+
// run will change, so they come from the instance on every run, never
|
|
78
|
+
// from the metadata cache: a cached answer can be stale within the
|
|
79
|
+
// watermark's gaps, and would update or delete records that no longer
|
|
80
|
+
// match.
|
|
81
|
+
const resp = await (0, metadata_cache_js_1.uncachedOperations)(client).get(`/api/now/table/${args.table}`, {
|
|
75
82
|
sysparm_fields: "sys_id",
|
|
76
83
|
sysparm_limit: String(limit),
|
|
77
84
|
sysparm_query: structuredPlan.encodedQuery,
|
|
@@ -80,15 +87,23 @@ async function handler(args, client, _config, _context) {
|
|
|
80
87
|
const results = Array.isArray(filtered)
|
|
81
88
|
? (filtered.filter((record) => typeof record === "object" && record !== null && !Array.isArray(record)))
|
|
82
89
|
: [];
|
|
83
|
-
const
|
|
90
|
+
const targets = targetSysIds(results);
|
|
91
|
+
const matched = targets.sysIds.length + targets.invalid;
|
|
84
92
|
// Step 2: Dry-run?
|
|
85
93
|
if (!args.confirm) {
|
|
94
|
+
const sample = await dryRunSample(client, args.table, targets.sysIds, (0, field_policy_js_1.resolveReadableFields)(args.table, { fields: "all" }), context?.signal);
|
|
86
95
|
return (0, utils_js_1.ok)({
|
|
87
96
|
action: args.action,
|
|
88
97
|
table: args.table,
|
|
89
98
|
matched,
|
|
90
99
|
dry_run: true,
|
|
91
100
|
message: "Dry run — no changes made. Set confirm to true to execute.",
|
|
101
|
+
...(sample.length === 0 ? {} : { sample }),
|
|
102
|
+
// Names only, never values: the values are the caller's own input.
|
|
103
|
+
...(writableFields === undefined
|
|
104
|
+
? {}
|
|
105
|
+
: { fields_to_write: Object.keys(writableFields) }),
|
|
106
|
+
note: DRY_RUN_NOTE,
|
|
92
107
|
});
|
|
93
108
|
}
|
|
94
109
|
if (matched === 0) {
|
|
@@ -100,125 +115,558 @@ async function handler(args, client, _config, _context) {
|
|
|
100
115
|
failed: 0,
|
|
101
116
|
});
|
|
102
117
|
}
|
|
103
|
-
// Step 3: Execute through ServiceNow REST Batch API
|
|
104
|
-
//
|
|
105
|
-
//
|
|
106
|
-
const execution = await executeBatchMutation(client, args.table, args.action,
|
|
107
|
-
|
|
118
|
+
// Step 3: Execute through the ServiceNow REST Batch API. Serial Table API
|
|
119
|
+
// calls are used only when the instance refused a batch request before
|
|
120
|
+
// running any of it, so no change is ever sent twice.
|
|
121
|
+
const execution = await executeBatchMutation(client, args.table, args.action, targets, writableFields,
|
|
122
|
+
// A direct handler call may pass no context; it then keeps the single
|
|
123
|
+
// budget of whatever scope it runs in.
|
|
124
|
+
context?.signal);
|
|
125
|
+
const { processed, failed, message } = execution;
|
|
108
126
|
return (0, utils_js_1.ok)({
|
|
109
127
|
action: args.action,
|
|
110
128
|
table: args.table,
|
|
111
129
|
matched,
|
|
112
130
|
processed,
|
|
113
131
|
failed,
|
|
132
|
+
...(message === undefined ? {} : { message }),
|
|
114
133
|
});
|
|
115
134
|
}
|
|
116
135
|
catch (error) {
|
|
117
136
|
throw error;
|
|
118
137
|
}
|
|
119
138
|
}
|
|
120
|
-
|
|
121
|
-
|
|
139
|
+
/** Records a dry run shows. */
|
|
140
|
+
const DRY_RUN_SAMPLE_SIZE = batch_limits_js_1.BATCH_LIMITS.dryRunSampleSize;
|
|
141
|
+
/** Longest display value a sample row carries. */
|
|
142
|
+
const DRY_RUN_DISPLAY_MAX_CHARS = 120;
|
|
143
|
+
/** Readable fields that identify a record to a person, in order of preference. */
|
|
144
|
+
const DRY_RUN_DISPLAY_FIELDS = Object.freeze([
|
|
145
|
+
"number",
|
|
146
|
+
"name",
|
|
147
|
+
"short_description",
|
|
148
|
+
"title",
|
|
149
|
+
]);
|
|
150
|
+
const DRY_RUN_SAMPLE_DEADLINE_MS = 5_000;
|
|
151
|
+
const DRY_RUN_NOTE = "The sample is a preview of the first matches, not a list of what will change. " +
|
|
152
|
+
"Running again with confirm true re-runs the query, so it can match different " +
|
|
153
|
+
"records than this dry run did.";
|
|
154
|
+
/**
|
|
155
|
+
* The one extra read a dry run makes: at most DRY_RUN_SAMPLE_SIZE records,
|
|
156
|
+
* addressed by the sys_ids the lookup already returned, so the preview shows
|
|
157
|
+
* rows from that same result rather than a second evaluation of the filter.
|
|
158
|
+
* It asks only for sys_id and one display field the profile may read, and the
|
|
159
|
+
* rows pass the same readable-field filter as the lookup. It is skipped when
|
|
160
|
+
* nothing matched, and a failed response yields no sample rather than failing
|
|
161
|
+
* the dry run: the count is already in hand.
|
|
162
|
+
*/
|
|
163
|
+
async function dryRunSample(client, table, sysIds, readable, signal) {
|
|
164
|
+
const ids = sysIds.slice(0, DRY_RUN_SAMPLE_SIZE);
|
|
165
|
+
if (ids.length === 0)
|
|
166
|
+
return [];
|
|
167
|
+
const readableAll = (0, field_policy_js_1.isAllFieldSelection)(readable);
|
|
168
|
+
const allows = (field) => readableAll || readable.includes(field);
|
|
169
|
+
if (!allows("sys_id"))
|
|
170
|
+
return [];
|
|
171
|
+
const displayField = DRY_RUN_DISPLAY_FIELDS.find(allows);
|
|
172
|
+
const selection = Object.freeze(displayField === undefined ? ["sys_id"] : ["sys_id", displayField]);
|
|
173
|
+
let response;
|
|
122
174
|
try {
|
|
123
|
-
|
|
175
|
+
// A short deadline: the count is already in hand, so a slow instance
|
|
176
|
+
// costs the preview, never the dry run.
|
|
177
|
+
const deadline = AbortSignal.timeout(DRY_RUN_SAMPLE_DEADLINE_MS);
|
|
178
|
+
response = await inOwnResponseBudget(signal === undefined ? deadline : AbortSignal.any([signal, deadline]), () => (0, metadata_cache_js_1.uncachedOperations)(client).get(`/api/now/table/${table}`, {
|
|
179
|
+
sysparm_fields: selection.join(","),
|
|
180
|
+
sysparm_limit: String(DRY_RUN_SAMPLE_SIZE),
|
|
181
|
+
sysparm_query: `sys_idIN${ids.join(",")}`,
|
|
182
|
+
}));
|
|
183
|
+
}
|
|
184
|
+
catch (error) {
|
|
185
|
+
// A cancelled call stays cancelled; any other failure only drops the preview.
|
|
186
|
+
if (signal?.aborted)
|
|
187
|
+
throw error;
|
|
188
|
+
return [];
|
|
124
189
|
}
|
|
125
|
-
|
|
126
|
-
|
|
190
|
+
const filtered = (0, field_policy_js_1.filterReadableRecord)(Array.isArray(response?.result) ? response.result : [], selection);
|
|
191
|
+
const rows = [];
|
|
192
|
+
// Only rows for the sys_ids asked for, each once: the sample must come from
|
|
193
|
+
// the matched set, whatever the instance answers.
|
|
194
|
+
const requested = new Set(ids);
|
|
195
|
+
const shown = new Set();
|
|
196
|
+
for (const record of Array.isArray(filtered) ? filtered : []) {
|
|
197
|
+
const item = plainRecord(record);
|
|
198
|
+
if (item === undefined)
|
|
199
|
+
continue;
|
|
200
|
+
let sysId;
|
|
201
|
+
try {
|
|
202
|
+
sysId = (0, servicenow_identifiers_js_1.serviceNowSysIdPathSegment)(item.sys_id);
|
|
203
|
+
}
|
|
204
|
+
catch {
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
if (!requested.has(sysId) || shown.has(sysId))
|
|
208
|
+
continue;
|
|
209
|
+
shown.add(sysId);
|
|
210
|
+
const display = displayField === undefined ? undefined : displayText(item[displayField]);
|
|
211
|
+
rows.push(display === undefined ? { sys_id: sysId } : { sys_id: sysId, display });
|
|
212
|
+
if (rows.length === DRY_RUN_SAMPLE_SIZE)
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
return rows;
|
|
216
|
+
}
|
|
217
|
+
/** A display value is text; a reference object contributes its own display text. */
|
|
218
|
+
function displayText(candidate) {
|
|
219
|
+
const reference = plainRecord(candidate);
|
|
220
|
+
const value = typeof candidate === "string"
|
|
221
|
+
? candidate
|
|
222
|
+
: typeof reference?.display_value === "string"
|
|
223
|
+
? reference.display_value
|
|
224
|
+
: undefined;
|
|
225
|
+
if (value === undefined || value === "")
|
|
226
|
+
return undefined;
|
|
227
|
+
return value.length > DRY_RUN_DISPLAY_MAX_CHARS
|
|
228
|
+
? `${value.slice(0, DRY_RUN_DISPLAY_MAX_CHARS)}…`
|
|
229
|
+
: value;
|
|
230
|
+
}
|
|
231
|
+
const REST_BATCH_CHUNK_SIZE = batch_limits_js_1.BATCH_LIMITS.restBatchChunkSize;
|
|
232
|
+
const REST_BATCH_PATH = "/api/now/v1/batch";
|
|
233
|
+
/**
|
|
234
|
+
* Target size of one batch request body. ServiceNow caps REST request
|
|
235
|
+
* payloads (about 10 MB by default), and an oversized batch that is refused
|
|
236
|
+
* has an unknown outcome, so a chunk holds fewer records when values are
|
|
237
|
+
* large. A request stays under this size unless one record's fields alone
|
|
238
|
+
* exceed about 3 MiB of JSON; such a record is sent one per request, because
|
|
239
|
+
* a chunk never holds fewer than one record.
|
|
240
|
+
*/
|
|
241
|
+
const MAX_BATCH_REQUEST_BYTES = 4 * 1024 * 1024;
|
|
242
|
+
/**
|
|
243
|
+
* Upper bound on the JSON one `rest_requests` entry adds besides its body and
|
|
244
|
+
* table name: id, method, url, headers and exclude_response_headers.
|
|
245
|
+
*/
|
|
246
|
+
const REST_BATCH_REQUEST_OVERHEAD_BYTES = 300;
|
|
247
|
+
/**
|
|
248
|
+
* A sub-request that returns a 5xx or 408 inside a readable batch response
|
|
249
|
+
* is unconfirmed and never retried, but the run continues: the instance is
|
|
250
|
+
* answering, and every other record is still sent at most once. Past this
|
|
251
|
+
* many unconfirmed records in one run, the run stops and reports the rest as
|
|
252
|
+
* not sent, because that many server errors suggest an unhealthy instance.
|
|
253
|
+
*/
|
|
254
|
+
const MAX_UNCONFIRMED_SUB_REQUESTS = 10;
|
|
255
|
+
/**
|
|
256
|
+
* Failure categories that mean ServiceNow refused a batch request before
|
|
257
|
+
* running any request in it, and that serial calls can work around: the
|
|
258
|
+
* Batch API is missing or not permitted for this credential. Only these fall
|
|
259
|
+
* back to serial calls. Any other failure, and every 2xx response, can follow
|
|
260
|
+
* a batch ServiceNow already applied in whole or in part. Replaying it would
|
|
261
|
+
* apply a change twice, which for a journal field is a duplicate entry.
|
|
262
|
+
*
|
|
263
|
+
* A rate limit (429) also means nothing ran, but it is not in the set: serial
|
|
264
|
+
* calls would add one request per record to the limit the instance is
|
|
265
|
+
* enforcing. The run stops instead and reports the Retry-After hint.
|
|
266
|
+
*/
|
|
267
|
+
const BATCH_REFUSED_BEFORE_PROCESSING = new Set(["authentication", "authorization", "not_found"]);
|
|
268
|
+
/** Per-request headers, as the documented Batch API takes them. */
|
|
269
|
+
const JSON_BODY_HEADERS = Object.freeze([
|
|
270
|
+
Object.freeze({ name: "Content-Type", value: "application/json" }),
|
|
271
|
+
Object.freeze({ name: "Accept", value: "application/json" }),
|
|
272
|
+
]);
|
|
273
|
+
const JSON_ACCEPT_HEADERS = Object.freeze([
|
|
274
|
+
Object.freeze({ name: "Accept", value: "application/json" }),
|
|
275
|
+
]);
|
|
276
|
+
/**
|
|
277
|
+
* Validate and de-duplicate the sys_ids the lookup returned. The lookup can
|
|
278
|
+
* name a record more than once; each record is changed at most once per run,
|
|
279
|
+
* so a journal entry is never appended twice to it.
|
|
280
|
+
*/
|
|
281
|
+
function targetSysIds(records) {
|
|
282
|
+
const unique = new Set();
|
|
283
|
+
let invalid = 0;
|
|
284
|
+
for (const record of records) {
|
|
285
|
+
try {
|
|
286
|
+
unique.add((0, servicenow_identifiers_js_1.serviceNowSysIdPathSegment)(record.sys_id));
|
|
287
|
+
}
|
|
288
|
+
catch {
|
|
289
|
+
invalid += 1;
|
|
290
|
+
}
|
|
127
291
|
}
|
|
292
|
+
return { sysIds: [...unique], invalid };
|
|
128
293
|
}
|
|
129
|
-
|
|
294
|
+
/**
|
|
295
|
+
* Apply one update or delete to every matched record through the documented
|
|
296
|
+
* REST Batch API (`POST /api/now/v1/batch`), up to 50 requests per batch and
|
|
297
|
+
* at most MAX_BATCH_REQUEST_BYTES per request body.
|
|
298
|
+
*
|
|
299
|
+
* Request: `rest_requests[]` with `id`, `method`, `url`, `headers` as
|
|
300
|
+
* name/value pairs, and a base64-encoded `body`. Response:
|
|
301
|
+
* `serviced_requests[]` with `id` and `status_code`, plus
|
|
302
|
+
* `unserviced_requests[]` with the ids ServiceNow did not run.
|
|
303
|
+
*
|
|
304
|
+
* A batch request is never replayed. Serial Table API calls replace it only
|
|
305
|
+
* when ServiceNow refused the request before running any of it.
|
|
306
|
+
*
|
|
307
|
+
* The run stops, reporting the rest as not sent, when a batch request fails
|
|
308
|
+
* or its response cannot be read or attributed, when every serviced
|
|
309
|
+
* sub-request in a batch returns a server error, or when more than
|
|
310
|
+
* MAX_UNCONFIRMED_SUB_REQUESTS records are unconfirmed. An isolated
|
|
311
|
+
* sub-request server error is reported as unconfirmed and the run continues.
|
|
312
|
+
*/
|
|
313
|
+
async function executeBatchMutation(client, table, action, targets, fields, signal) {
|
|
130
314
|
let processed = 0;
|
|
131
|
-
let failed =
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
315
|
+
let failed = targets.invalid;
|
|
316
|
+
let unserviced = 0;
|
|
317
|
+
let unconfirmed = 0;
|
|
318
|
+
let serverErrors = 0;
|
|
319
|
+
const { sysIds } = targets;
|
|
320
|
+
const body = action === "update" ? encodeBatchBody(fields ?? {}) : undefined;
|
|
321
|
+
const chunkSize = batchChunkSize(table, body);
|
|
322
|
+
for (let offset = 0; offset < sysIds.length; offset += chunkSize) {
|
|
323
|
+
const chunk = sysIds.slice(offset, offset + chunkSize);
|
|
324
|
+
const requests = chunk.map((sysId, index) => restBatchRequest(table, action, sysId, String(offset + index + 1), body));
|
|
325
|
+
let response;
|
|
326
|
+
try {
|
|
327
|
+
response = await inOwnResponseBudget(signal, () => client.post(REST_BATCH_PATH, {
|
|
328
|
+
batch_request_id: `sn_batch_${Date.now()}_${offset}`,
|
|
329
|
+
rest_requests: requests,
|
|
330
|
+
}));
|
|
331
|
+
}
|
|
332
|
+
catch (error) {
|
|
333
|
+
const descriptor = (0, tool_error_js_1.trustedToolErrorDescriptor)(error);
|
|
334
|
+
if (descriptor !== undefined &&
|
|
335
|
+
BATCH_REFUSED_BEFORE_PROCESSING.has(descriptor.category)) {
|
|
336
|
+
// Nothing in this batch ran. The records from here on go through the
|
|
337
|
+
// Table API one at a time; earlier batches are confirmed and are not
|
|
338
|
+
// sent again.
|
|
339
|
+
const serial = await executeSerialMutation(client, table, action, sysIds.slice(offset), fields, signal);
|
|
340
|
+
return batchResult({
|
|
341
|
+
...serial,
|
|
342
|
+
processed: processed + serial.processed,
|
|
343
|
+
failed: failed + serial.failed,
|
|
344
|
+
unserviced,
|
|
345
|
+
unconfirmed: unconfirmed + serial.unconfirmed,
|
|
346
|
+
serverErrors,
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
if (descriptor?.category === "rate_limit") {
|
|
350
|
+
// Nothing in this batch ran, and the client already retried it.
|
|
351
|
+
return batchResult({
|
|
352
|
+
processed,
|
|
353
|
+
failed,
|
|
354
|
+
unserviced,
|
|
355
|
+
unconfirmed,
|
|
356
|
+
serverErrors,
|
|
357
|
+
notSent: sysIds.length - offset,
|
|
358
|
+
rateLimited: { retryAfterSeconds: descriptor.retryAfterSeconds },
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
// The request may have reached ServiceNow and been applied.
|
|
362
|
+
return batchResult({
|
|
363
|
+
processed,
|
|
364
|
+
failed,
|
|
365
|
+
unserviced,
|
|
366
|
+
unconfirmed: unconfirmed + chunk.length,
|
|
367
|
+
serverErrors,
|
|
368
|
+
notSent: sysIds.length - offset - chunk.length,
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
const outcome = chunkOutcome(response, requests.map(({ id }) => id));
|
|
372
|
+
processed += outcome.processed;
|
|
373
|
+
failed += outcome.failed;
|
|
374
|
+
unserviced += outcome.unserviced;
|
|
375
|
+
unconfirmed += outcome.serverErrors + outcome.unattributed;
|
|
376
|
+
serverErrors += outcome.serverErrors;
|
|
377
|
+
const serviced = outcome.processed + outcome.failed - outcome.unserviced + outcome.serverErrors;
|
|
378
|
+
const stoppedOn = outcome.unattributed > 0
|
|
379
|
+
? "unreadable_response"
|
|
380
|
+
: outcome.serverErrors > 0 && outcome.serverErrors === serviced
|
|
381
|
+
? "server_errors"
|
|
382
|
+
: unconfirmed > MAX_UNCONFIRMED_SUB_REQUESTS
|
|
383
|
+
? "unconfirmed_limit"
|
|
384
|
+
: undefined;
|
|
385
|
+
if (stoppedOn !== undefined) {
|
|
386
|
+
return batchResult({
|
|
387
|
+
processed,
|
|
388
|
+
failed,
|
|
389
|
+
unserviced,
|
|
390
|
+
unconfirmed,
|
|
391
|
+
serverErrors,
|
|
392
|
+
notSent: sysIds.length - offset - chunk.length,
|
|
393
|
+
stoppedOn,
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
return batchResult({
|
|
398
|
+
processed,
|
|
399
|
+
failed,
|
|
400
|
+
unserviced,
|
|
401
|
+
unconfirmed,
|
|
402
|
+
serverErrors,
|
|
403
|
+
notSent: 0,
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Run one upstream request with its own response-byte budget, bound to the
|
|
408
|
+
* tool call's cancellation signal.
|
|
409
|
+
*
|
|
410
|
+
* A single per-call budget would cut a large run short: the sys_id lookup and
|
|
411
|
+
* every batch response draw on it, so a 10,000-record update would stop after
|
|
412
|
+
* about 3,400 records. Each response is still capped by the client, and is
|
|
413
|
+
* parsed and dropped before the next request, so memory stays bounded by one
|
|
414
|
+
* chunk. Without a signal (a direct handler call) the request runs in the
|
|
415
|
+
* caller's scope.
|
|
416
|
+
*/
|
|
417
|
+
function inOwnResponseBudget(signal, operation) {
|
|
418
|
+
return signal instanceof AbortSignal
|
|
419
|
+
? (0, client_js_1.runWithServiceNowRequestSignal)(signal, operation)
|
|
420
|
+
: operation();
|
|
421
|
+
}
|
|
422
|
+
/** Up to 50 records, fewer when the encoded body is large. */
|
|
423
|
+
function batchChunkSize(table, body) {
|
|
424
|
+
const perRequestBytes = (body?.length ?? 0) + table.length + REST_BATCH_REQUEST_OVERHEAD_BYTES;
|
|
425
|
+
return Math.max(1, Math.min(REST_BATCH_CHUNK_SIZE, Math.floor(MAX_BATCH_REQUEST_BYTES / perRequestBytes)));
|
|
426
|
+
}
|
|
427
|
+
function restBatchRequest(table, action, sysId, id, body) {
|
|
428
|
+
if (action === "update") {
|
|
429
|
+
return {
|
|
430
|
+
id,
|
|
431
|
+
method: "PATCH",
|
|
432
|
+
// Only sys_id comes back per record, which keeps each batch response
|
|
433
|
+
// small and its parse cheap.
|
|
434
|
+
url: `/api/now/table/${table}/${sysId}?sysparm_fields=sys_id`,
|
|
435
|
+
headers: JSON_BODY_HEADERS,
|
|
436
|
+
exclude_response_headers: true,
|
|
437
|
+
body,
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
return {
|
|
441
|
+
id,
|
|
442
|
+
method: "DELETE",
|
|
443
|
+
url: `/api/now/table/${table}/${sysId}`,
|
|
444
|
+
headers: JSON_ACCEPT_HEADERS,
|
|
445
|
+
exclude_response_headers: true,
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
/** Base64 text is ASCII, so its length is its byte count. */
|
|
449
|
+
function encodeBatchBody(fields) {
|
|
450
|
+
return node_buffer_1.Buffer.from(JSON.stringify(fields), "utf8").toString("base64");
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* Count one batch response against the ids that were sent. An id ServiceNow
|
|
454
|
+
* neither serviced nor listed as unserviced is unattributed, and so is an id
|
|
455
|
+
* reported more than once, because its result cannot be attributed.
|
|
456
|
+
*
|
|
457
|
+
* A sub-request answered with a 5xx or a 408 is a server error. It is
|
|
458
|
+
* reported as unconfirmed, matching the serial path: a server error can
|
|
459
|
+
* follow a change that was partly applied (ServiceNow does not roll back a
|
|
460
|
+
* journal insert when a later business rule fails), so it must not read as
|
|
461
|
+
* "failed, safe to re-run".
|
|
462
|
+
*/
|
|
463
|
+
function chunkOutcome(response, ids) {
|
|
464
|
+
const requested = new Set(ids);
|
|
465
|
+
const statuses = new Map();
|
|
466
|
+
const notServiced = new Set();
|
|
467
|
+
const root = plainRecord(response);
|
|
468
|
+
const serviced = root?.serviced_requests;
|
|
469
|
+
if (Array.isArray(serviced)) {
|
|
470
|
+
for (const entry of serviced) {
|
|
471
|
+
const item = plainRecord(entry);
|
|
472
|
+
if (item === undefined)
|
|
473
|
+
continue;
|
|
474
|
+
const id = batchRequestId(item.id);
|
|
475
|
+
if (id === undefined || !requested.has(id))
|
|
476
|
+
continue;
|
|
477
|
+
statuses.set(id, statuses.has(id) ? undefined : statusCode(item.status_code));
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
const unservicedEntries = root?.unserviced_requests;
|
|
481
|
+
if (Array.isArray(unservicedEntries)) {
|
|
482
|
+
for (const entry of unservicedEntries) {
|
|
483
|
+
const id = batchRequestId(entry) ?? batchRequestId(plainRecord(entry)?.id);
|
|
484
|
+
if (id !== undefined && requested.has(id) && !statuses.has(id)) {
|
|
485
|
+
notServiced.add(id);
|
|
486
|
+
}
|
|
150
487
|
}
|
|
151
|
-
|
|
152
|
-
|
|
488
|
+
}
|
|
489
|
+
let processed = 0;
|
|
490
|
+
let failed = 0;
|
|
491
|
+
let serverErrors = 0;
|
|
492
|
+
let unattributed = 0;
|
|
493
|
+
for (const id of ids) {
|
|
494
|
+
const status = statuses.get(id);
|
|
495
|
+
if (status !== undefined) {
|
|
496
|
+
if (status >= 200 && status < 300) {
|
|
153
497
|
processed += 1;
|
|
154
|
-
|
|
498
|
+
}
|
|
499
|
+
else if (status >= 500 || status === 408) {
|
|
500
|
+
serverErrors += 1;
|
|
501
|
+
}
|
|
502
|
+
else {
|
|
155
503
|
failed += 1;
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
else if (notServiced.has(id)) {
|
|
507
|
+
failed += 1;
|
|
508
|
+
}
|
|
509
|
+
else {
|
|
510
|
+
unattributed += 1;
|
|
156
511
|
}
|
|
157
512
|
}
|
|
158
|
-
return {
|
|
513
|
+
return {
|
|
514
|
+
processed,
|
|
515
|
+
failed,
|
|
516
|
+
unserviced: notServiced.size,
|
|
517
|
+
serverErrors,
|
|
518
|
+
unattributed,
|
|
519
|
+
};
|
|
159
520
|
}
|
|
160
|
-
function
|
|
161
|
-
const
|
|
162
|
-
if (
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
if (
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
521
|
+
function batchResult(tally) {
|
|
522
|
+
const notes = [];
|
|
523
|
+
if (tally.unconfirmed > 0) {
|
|
524
|
+
const serverErrors = tally.serverErrors ?? 0;
|
|
525
|
+
notes.push(`ServiceNow did not confirm the outcome for ${tally.unconfirmed} record(s)` +
|
|
526
|
+
(serverErrors === 0
|
|
527
|
+
? ". "
|
|
528
|
+
: `; ${serverErrors} of them returned a server error (HTTP 5xx or ` +
|
|
529
|
+
"408), which can follow a partly applied change. ") +
|
|
530
|
+
"They may have been changed. The run has not retried them. A journal " +
|
|
531
|
+
"write (comments, work_notes) is never re-sent, but when records are " +
|
|
532
|
+
"sent one at a time, other fields may have been sent up to three " +
|
|
533
|
+
"times by the HTTP client's retry on a transient error. Check them " +
|
|
534
|
+
"before running the batch again.");
|
|
535
|
+
}
|
|
536
|
+
if (tally.stoppedOn === "server_errors") {
|
|
537
|
+
notes.push("Every serviced record in one batch returned a server error, so nothing " +
|
|
538
|
+
"more was sent.");
|
|
539
|
+
}
|
|
540
|
+
else if (tally.stoppedOn === "unconfirmed_limit") {
|
|
541
|
+
notes.push(`More than ${MAX_UNCONFIRMED_SUB_REQUESTS} records had an unconfirmed ` +
|
|
542
|
+
"outcome, so nothing more was sent.");
|
|
543
|
+
}
|
|
544
|
+
if (tally.authenticationFailed === true) {
|
|
545
|
+
notes.push("ServiceNow rejected the credentials (HTTP 401), so nothing more was sent.");
|
|
546
|
+
}
|
|
547
|
+
if (tally.rateLimited !== undefined) {
|
|
548
|
+
const retryAfter = tally.rateLimited.retryAfterSeconds;
|
|
549
|
+
notes.push("ServiceNow rate-limited the request (HTTP 429), so nothing more was sent." +
|
|
550
|
+
(retryAfter === undefined ? "" : ` Retry after ${retryAfter} seconds.`));
|
|
551
|
+
}
|
|
552
|
+
if (tally.notSent > 0) {
|
|
553
|
+
notes.push(`The remaining ${tally.notSent} record(s) were not sent and are counted as failed.`);
|
|
554
|
+
}
|
|
555
|
+
if (tally.unserviced > 0) {
|
|
556
|
+
notes.push(`ServiceNow did not process ${tally.unserviced} record(s) because a batch ` +
|
|
557
|
+
"reached a size or time limit. They are counted as failed.");
|
|
558
|
+
}
|
|
559
|
+
return {
|
|
560
|
+
processed: tally.processed,
|
|
561
|
+
failed: tally.failed + tally.notSent,
|
|
562
|
+
...(notes.length === 0 ? {} : { message: notes.join(" ") }),
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
function plainRecord(candidate) {
|
|
566
|
+
return typeof candidate === "object" && candidate !== null && !Array.isArray(candidate)
|
|
567
|
+
? candidate
|
|
568
|
+
: undefined;
|
|
569
|
+
}
|
|
570
|
+
function batchRequestId(candidate) {
|
|
571
|
+
if (typeof candidate === "string" && candidate !== "")
|
|
572
|
+
return candidate;
|
|
573
|
+
if (typeof candidate === "number" && Number.isSafeInteger(candidate)) {
|
|
574
|
+
return String(candidate);
|
|
197
575
|
}
|
|
198
576
|
return undefined;
|
|
199
577
|
}
|
|
200
|
-
|
|
578
|
+
function statusCode(candidate) {
|
|
579
|
+
const status = typeof candidate === "number"
|
|
580
|
+
? candidate
|
|
581
|
+
: typeof candidate === "string" && /^[1-5][0-9]{2}$/u.test(candidate)
|
|
582
|
+
? Number(candidate)
|
|
583
|
+
: undefined;
|
|
584
|
+
return status !== undefined &&
|
|
585
|
+
Number.isSafeInteger(status) &&
|
|
586
|
+
status >= 100 &&
|
|
587
|
+
status <= 599
|
|
588
|
+
? status
|
|
589
|
+
: undefined;
|
|
590
|
+
}
|
|
591
|
+
/**
|
|
592
|
+
* One Table API call per record, each with its own response budget. The run
|
|
593
|
+
* stops at the first failure whose outcome is unknown, at a rate limit, or at
|
|
594
|
+
* an authentication failure (a 401 the client could not refresh); the records
|
|
595
|
+
* after it are reported as not sent. Sending them would only repeat the same
|
|
596
|
+
* rejection, and repeated failed logins can trip an account lockout.
|
|
597
|
+
*/
|
|
598
|
+
async function executeSerialMutation(client, table, action, sysIds, fields, signal) {
|
|
201
599
|
let processed = 0;
|
|
202
600
|
let failed = 0;
|
|
203
|
-
for (const
|
|
601
|
+
for (const [index, sysId] of sysIds.entries()) {
|
|
602
|
+
const path = `/api/now/table/${table}/${sysId}`;
|
|
204
603
|
try {
|
|
205
|
-
const sysId = (0, servicenow_identifiers_js_1.serviceNowSysIdPathSegment)(record.sys_id);
|
|
206
604
|
if (action === "update") {
|
|
207
|
-
await client.patch(
|
|
605
|
+
await inOwnResponseBudget(signal, () => client.patch(path, fields));
|
|
208
606
|
processed += 1;
|
|
209
607
|
}
|
|
210
608
|
else {
|
|
211
|
-
const delResp = await client.delete(
|
|
609
|
+
const delResp = await inOwnResponseBudget(signal, () => client.delete(path));
|
|
212
610
|
if (delResp.status === 204 || delResp.status === 200)
|
|
213
611
|
processed += 1;
|
|
214
612
|
else
|
|
215
613
|
failed += 1;
|
|
216
614
|
}
|
|
217
615
|
}
|
|
218
|
-
catch {
|
|
616
|
+
catch (error) {
|
|
617
|
+
const notSent = sysIds.length - index - 1;
|
|
618
|
+
const descriptor = (0, tool_error_js_1.trustedToolErrorDescriptor)(error);
|
|
619
|
+
if (descriptor?.category === "rate_limit") {
|
|
620
|
+
return {
|
|
621
|
+
processed,
|
|
622
|
+
failed: failed + 1,
|
|
623
|
+
unserviced: 0,
|
|
624
|
+
unconfirmed: 0,
|
|
625
|
+
notSent,
|
|
626
|
+
rateLimited: { retryAfterSeconds: descriptor.retryAfterSeconds },
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
if (descriptor?.category === "authentication") {
|
|
630
|
+
// Rejected before processing, so this record failed definitely.
|
|
631
|
+
return {
|
|
632
|
+
processed,
|
|
633
|
+
failed: failed + 1,
|
|
634
|
+
unserviced: 0,
|
|
635
|
+
unconfirmed: 0,
|
|
636
|
+
notSent,
|
|
637
|
+
authenticationFailed: true,
|
|
638
|
+
};
|
|
639
|
+
}
|
|
640
|
+
if (!serialFailureIsDefinite(descriptor)) {
|
|
641
|
+
return { processed, failed, unserviced: 0, unconfirmed: 1, notSent };
|
|
642
|
+
}
|
|
219
643
|
failed += 1;
|
|
220
644
|
}
|
|
221
645
|
}
|
|
222
|
-
return { processed, failed };
|
|
646
|
+
return { processed, failed, unserviced: 0, unconfirmed: 0, notSent: 0 };
|
|
647
|
+
}
|
|
648
|
+
/**
|
|
649
|
+
* True when a single-record PATCH or DELETE failed with a status that means
|
|
650
|
+
* ServiceNow did not apply it, and the run can continue with the next record.
|
|
651
|
+
* For these methods the client classifies a 4xx it has no category for as
|
|
652
|
+
* `upstream` with `do_not_retry`, and a 5xx or connection failure as
|
|
653
|
+
* `upstream` with `retry_if_safe_and_idempotent`. A 5xx, a timeout, a
|
|
654
|
+
* connection failure, or an unreadable or over-budget response may follow an
|
|
655
|
+
* applied write, so its outcome is unknown. Authentication and rate-limit
|
|
656
|
+
* failures stop the run before this is consulted.
|
|
657
|
+
*/
|
|
658
|
+
function serialFailureIsDefinite(descriptor) {
|
|
659
|
+
if (descriptor === undefined)
|
|
660
|
+
return false;
|
|
661
|
+
switch (descriptor.category) {
|
|
662
|
+
case "authorization":
|
|
663
|
+
case "not_found":
|
|
664
|
+
case "conflict":
|
|
665
|
+
return true;
|
|
666
|
+
case "upstream":
|
|
667
|
+
return descriptor.retry === "do_not_retry";
|
|
668
|
+
default:
|
|
669
|
+
return false;
|
|
670
|
+
}
|
|
223
671
|
}
|
|
224
672
|
//# sourceMappingURL=batch.js.map
|