@zanichelli/freshdesk-mcp 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +204 -2
- package/dist/cli.js +56 -0
- package/dist/client/errors.js +136 -0
- package/dist/client/http.js +389 -0
- package/dist/client/rate-limit.js +198 -0
- package/dist/client/resources/agents.js +15 -0
- package/dist/client/resources/canned.js +134 -0
- package/dist/client/resources/companies.js +57 -0
- package/dist/client/resources/contacts.js +63 -0
- package/dist/client/resources/groups.js +15 -0
- package/dist/client/resources/list-all.js +54 -0
- package/dist/client/resources/search.js +94 -0
- package/dist/client/resources/solutions.js +192 -0
- package/dist/client/resources/ticket-fields.js +62 -0
- package/dist/client/resources/tickets.js +204 -0
- package/dist/config.js +174 -0
- package/dist/schema/index.js +145 -0
- package/dist/schema/input.js +260 -0
- package/dist/schema/output.js +138 -0
- package/dist/server.js +31 -0
- package/dist/tools/budget.js +63 -0
- package/dist/tools/context.js +62 -0
- package/dist/tools/csv.js +35 -0
- package/dist/tools/format.js +191 -0
- package/dist/tools/list.js +130 -0
- package/dist/tools/rows.js +132 -0
- package/dist/tools/ticket-query.js +408 -0
- package/dist/tools/toolsets/core/canned.js +163 -0
- package/dist/tools/toolsets/core/count-tickets.js +602 -0
- package/dist/tools/toolsets/core/export-tickets.js +461 -0
- package/dist/tools/toolsets/core/find-similar-tickets.js +309 -0
- package/dist/tools/toolsets/core/get-ticket-schema.js +134 -0
- package/dist/tools/toolsets/core/get-ticket.js +280 -0
- package/dist/tools/toolsets/core/index.js +30 -0
- package/dist/tools/toolsets/core/list-agents.js +70 -0
- package/dist/tools/toolsets/core/list-groups.js +60 -0
- package/dist/tools/toolsets/core/list-tickets.js +210 -0
- package/dist/tools/toolsets/core/search-tickets.js +82 -0
- package/dist/tools/toolsets/core/solutions.js +282 -0
- package/dist/tools/toolsets/core/triage-queue.js +253 -0
- package/dist/tools/toolsets/index.js +110 -0
- package/dist/version.js +4 -0
- package/examples/team-config.example.json +20 -0
- package/package.json +38 -4
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
// Untrusted content (SPEC §7.4): what a tool does to text before returning it.
|
|
2
|
+
//
|
|
3
|
+
// - Customer-written text (subject, description, conversation bodies,
|
|
4
|
+
// attachment names, requester and company names, a conversation's from,
|
|
5
|
+
// to and cc, KB articles and canned responses) goes through `untrusted`:
|
|
6
|
+
// inside one `<ticket_content>…</ticket_content>` block. A string is the
|
|
7
|
+
// block's text; an array or object, its JSON. `null`, `undefined`,
|
|
8
|
+
// numbers and booleans pass unchanged.
|
|
9
|
+
// - An embedded delimiter cannot end or open the block: inside it, every
|
|
10
|
+
// `<` becomes `‹`, and so does every entity spelling of it (`<`,
|
|
11
|
+
// `<`, `<`, any case, any leading zeros, with or without `;`), in
|
|
12
|
+
// plain text and in a decoded HTML body alike. No delimiter word is
|
|
13
|
+
// looked for, so invisible characters and look-alike letters inside the
|
|
14
|
+
// word change nothing. A look-alike of `<` itself (`<` U+FF1C, `‹`, or a
|
|
15
|
+
// `‹` the customer typed) is left as written: it is not the delimiter.
|
|
16
|
+
// In JSON (an array or object) a `<` is written `\u003c`, so a `<` in a
|
|
17
|
+
// key or value parses back the same; its `<` / `<` / `<`
|
|
18
|
+
// spellings become `‹` as in any block.
|
|
19
|
+
// - Text, not HTML: `bodyText` takes `body_text` / `description_text` when
|
|
20
|
+
// it is a string, else converts the HTML body with `htmlToText`. No HTML
|
|
21
|
+
// parser: comments, script and style are dropped, `<br>` and the end of a
|
|
22
|
+
// block become a newline, every other tag is removed, entities decoded
|
|
23
|
+
// once (`&lt;` gives `<`), whitespace collapsed.
|
|
24
|
+
// - A text over its cap (`TEXT_CAP` code points unless the caller gives
|
|
25
|
+
// one) is cut on a code point, and a marker after the block, outside it,
|
|
26
|
+
// says how many characters were cut and, from the caller's `rest`, how to
|
|
27
|
+
// fetch them: marker-like text inside the block is the customer's.
|
|
28
|
+
// - Admin-set text (agent and group names, field labels, choice names,
|
|
29
|
+
// article, folder and canned titles) is not wrapped (owner, 2026-11-23):
|
|
30
|
+
// `plainName` strips tags, collapses whitespace, turns `<` and its
|
|
31
|
+
// entities into `‹` and caps it at `NAME_CAP`, ending a cut one with `…`.
|
|
32
|
+
// - A ticket field's value (`fieldValue`) is wrapped unless its type is a
|
|
33
|
+
// known closed one and the value is of that type: a number (or decimal
|
|
34
|
+
// string) for `custom_number` / `custom_decimal`, a boolean (which
|
|
35
|
+
// `untrusted` passes unchanged), an ISO 8601 date for `custom_date` /
|
|
36
|
+
// `custom_date_time`, a choice found in the field's own choices for
|
|
37
|
+
// `custom_dropdown`, `default_ticket_type` and `nested_field` (each level
|
|
38
|
+
// of the " > " path). A validated choice, and a status, priority, source,
|
|
39
|
+
// agent or group name (a string or a number; any other value is wrapped),
|
|
40
|
+
// goes through `plainName`. Any other type is wrapped.
|
|
41
|
+
//
|
|
42
|
+
// Every pass over the text is linear in its length: no regex here can
|
|
43
|
+
// backtrack over a long run (a tag strip stops at the last `>`).
|
|
44
|
+
export const OPEN = "<ticket_content>";
|
|
45
|
+
export const CLOSE = "</ticket_content>";
|
|
46
|
+
/** Code points of customer text kept by default. */
|
|
47
|
+
export const TEXT_CAP = 10_000;
|
|
48
|
+
/** Code points of an admin-set name kept by default. */
|
|
49
|
+
export const NAME_CAP = 100;
|
|
50
|
+
/** `<` and its entity spellings; a numeric one only when no digit follows (`ɘ` is not one). */
|
|
51
|
+
const LESS_THAN = /<|<?|�*60(?![0-9]);?|�*3c(?![0-9a-f]);?/gi;
|
|
52
|
+
/** Every `<`, written or as an entity, becomes `‹`: no delimiter can form. */
|
|
53
|
+
function neutralise(text) {
|
|
54
|
+
return text.replace(LESS_THAN, "‹");
|
|
55
|
+
}
|
|
56
|
+
function cut(text, cap) {
|
|
57
|
+
if (text.length <= cap)
|
|
58
|
+
return { kept: text, more: 0 };
|
|
59
|
+
const points = Array.from(text);
|
|
60
|
+
if (points.length <= cap)
|
|
61
|
+
return { kept: text, more: 0 };
|
|
62
|
+
return { kept: points.slice(0, cap).join(""), more: points.length - cap };
|
|
63
|
+
}
|
|
64
|
+
function wrap(text, options = {}) {
|
|
65
|
+
const { kept, more } = cut(neutralise(text), options.cap ?? TEXT_CAP);
|
|
66
|
+
const marker = more === 0
|
|
67
|
+
? ""
|
|
68
|
+
: `\n[… ${more} more characters not shown${options.rest ? `; ${options.rest}` : ""}]`;
|
|
69
|
+
return `${OPEN}${kept}${CLOSE}${marker}`;
|
|
70
|
+
}
|
|
71
|
+
export function untrusted(value, options) {
|
|
72
|
+
if (value === null || value === undefined)
|
|
73
|
+
return value;
|
|
74
|
+
if (typeof value === "number" || typeof value === "boolean")
|
|
75
|
+
return value;
|
|
76
|
+
if (typeof value === "string")
|
|
77
|
+
return wrap(value, options);
|
|
78
|
+
// `<` in a key or a value written as its JSON escape: it parses the same
|
|
79
|
+
// (the entity spellings still become `‹` in wrap).
|
|
80
|
+
return wrap(JSON.stringify(value).replaceAll("<", "\\u003c"), options);
|
|
81
|
+
}
|
|
82
|
+
/** Remove every `<…>`; only up to the last `>`, so a run of `<` is not rescanned. */
|
|
83
|
+
function stripTags(text) {
|
|
84
|
+
const end = text.lastIndexOf(">") + 1;
|
|
85
|
+
return text.slice(0, end).replace(/<[^>]*>/g, "") + text.slice(end);
|
|
86
|
+
}
|
|
87
|
+
const NAMED = {
|
|
88
|
+
amp: "&",
|
|
89
|
+
lt: "<",
|
|
90
|
+
gt: ">",
|
|
91
|
+
quot: '"',
|
|
92
|
+
apos: "'",
|
|
93
|
+
nbsp: " ",
|
|
94
|
+
};
|
|
95
|
+
function codePoint(n) {
|
|
96
|
+
return Number.isSafeInteger(n) && n > 0 && n <= 0x10ffff && !(n >= 0xd800 && n <= 0xdfff)
|
|
97
|
+
? String.fromCodePoint(n)
|
|
98
|
+
: "�";
|
|
99
|
+
}
|
|
100
|
+
/** One pass: `&lt;` gives `<`, never `<`. An unknown name stays as written. */
|
|
101
|
+
function decodeEntities(text) {
|
|
102
|
+
return text.replace(/&(#x[0-9a-f]+|#[0-9]+|[a-z]+);?/gi, (whole, ref) => {
|
|
103
|
+
if (ref[0] === "#") {
|
|
104
|
+
const hex = ref[1] === "x" || ref[1] === "X";
|
|
105
|
+
return codePoint(Number.parseInt(ref.slice(hex ? 2 : 1), hex ? 16 : 10));
|
|
106
|
+
}
|
|
107
|
+
return NAMED[ref.toLowerCase()] ?? whole;
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
/** An HTML body as plain text, with no tag. */
|
|
111
|
+
export function htmlToText(html) {
|
|
112
|
+
const text = stripTags(html
|
|
113
|
+
.replace(/<!--[\s\S]*?(?:-->|$)/g, "")
|
|
114
|
+
.replace(/<(script|style)\b[\s\S]*?(?:<\/\1\s*>|$)/gi, "")
|
|
115
|
+
.replace(/<br\s*\/?>/gi, "\n")
|
|
116
|
+
.replace(/<\/(?:p|div|li|tr|h[1-6]|blockquote|pre)\s*>/gi, "\n"));
|
|
117
|
+
return decodeEntities(text)
|
|
118
|
+
.replace(/[ \t\r\f\v ]+/g, " ")
|
|
119
|
+
.replace(/ ?\n ?/g, "\n")
|
|
120
|
+
.replace(/\n{3,}/g, "\n\n")
|
|
121
|
+
.trim();
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* A body or description: its text form when Freshdesk gives one (a string,
|
|
125
|
+
* even empty), else its HTML converted; wrapped. `null` when neither is a string.
|
|
126
|
+
*/
|
|
127
|
+
export function bodyText(text, html, options) {
|
|
128
|
+
if (typeof text === "string")
|
|
129
|
+
return wrap(text, options);
|
|
130
|
+
if (typeof html === "string")
|
|
131
|
+
return wrap(htmlToText(html), options);
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
export function plainName(value, cap = NAME_CAP) {
|
|
135
|
+
if (typeof value !== "string")
|
|
136
|
+
return value;
|
|
137
|
+
const { kept, more } = cut(neutralise(stripTags(value).replace(/\s+/g, " ").trim()), cap);
|
|
138
|
+
return more === 0 ? kept : `${kept}…`;
|
|
139
|
+
}
|
|
140
|
+
function isRecord(value) {
|
|
141
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
142
|
+
}
|
|
143
|
+
/** Whether each part is a choice at its level of the field's list or tree. */
|
|
144
|
+
function inChoices(choices, parts) {
|
|
145
|
+
let node = choices;
|
|
146
|
+
for (const part of parts) {
|
|
147
|
+
if (Array.isArray(node)) {
|
|
148
|
+
if (!node.includes(part))
|
|
149
|
+
return false;
|
|
150
|
+
node = undefined;
|
|
151
|
+
}
|
|
152
|
+
else if (isRecord(node) && Object.hasOwn(node, part)) {
|
|
153
|
+
node = node[part];
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return parts.length > 0;
|
|
160
|
+
}
|
|
161
|
+
const NUMBER_TYPES = new Set(["custom_number", "custom_decimal"]);
|
|
162
|
+
const DATE_TYPES = new Set(["custom_date", "custom_date_time"]);
|
|
163
|
+
const CHOICE_TYPES = new Set(["custom_dropdown", "default_ticket_type"]);
|
|
164
|
+
const NAME_TYPES = new Set([
|
|
165
|
+
"default_status",
|
|
166
|
+
"default_priority",
|
|
167
|
+
"default_source",
|
|
168
|
+
"default_agent",
|
|
169
|
+
"default_group",
|
|
170
|
+
]);
|
|
171
|
+
const DECIMAL = /^-?\d+(?:\.\d+)?$/;
|
|
172
|
+
const ISO_DATE = /^\d{4}-\d{2}-\d{2}(?:T\d{2}:\d{2}(?::\d{2}(?:\.\d{1,9})?)?(?:Z|[+-]\d{2}:?\d{2})?)?$/;
|
|
173
|
+
/** A ticket field's value: wrapped unless its type is a known closed one and the value fits it. */
|
|
174
|
+
export function fieldValue(field, value, options) {
|
|
175
|
+
const { type } = field;
|
|
176
|
+
if (NAME_TYPES.has(type) && (typeof value === "string" || typeof value === "number")) {
|
|
177
|
+
return plainName(value);
|
|
178
|
+
}
|
|
179
|
+
if (typeof value === "string") {
|
|
180
|
+
if (NUMBER_TYPES.has(type) && DECIMAL.test(value))
|
|
181
|
+
return value;
|
|
182
|
+
if (DATE_TYPES.has(type) && ISO_DATE.test(value))
|
|
183
|
+
return value;
|
|
184
|
+
if (CHOICE_TYPES.has(type) && inChoices(field.choices, [value]))
|
|
185
|
+
return plainName(value);
|
|
186
|
+
if (type === "nested_field" && inChoices(field.choices, value.split(" > "))) {
|
|
187
|
+
return plainName(value);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return untrusted(value, options);
|
|
191
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
// List envelope and cursor (SPEC §8.4): what every list tool returns, built
|
|
2
|
+
// in one place.
|
|
3
|
+
//
|
|
4
|
+
// - Envelope: `{items, next_cursor?, has_more, truncated?, api_calls_used}`.
|
|
5
|
+
// `has_more` is true exactly when the tool gives a `next` position, and
|
|
6
|
+
// `next_cursor` is present exactly then. `truncated: true` is carried
|
|
7
|
+
// from a source a cap cut (`listAll`, `paginate`); an uncut source gives
|
|
8
|
+
// no `truncated` key.
|
|
9
|
+
// - Cursor: `<position>.<digest>`. The position is a whole number whose
|
|
10
|
+
// meaning is the tool's (an offset, a page); the digest is SHA-256,
|
|
11
|
+
// base64url, over the tool name, every argument of the call except the
|
|
12
|
+
// `cursor` argument itself, and the position. Arguments are serialised
|
|
13
|
+
// with object keys sorted, so their order does not matter; arrays keep
|
|
14
|
+
// theirs. They are the raw JSON arguments: any other value (a Date, a
|
|
15
|
+
// Map, a class instance, NaN, a BigInt) throws a `TypeError`.
|
|
16
|
+
// - The digest is unkeyed: it catches a cursor used with another tool or
|
|
17
|
+
// with any argument changed, and an edited one, not a forged one. A
|
|
18
|
+
// forged cursor can only carry a whole number to the tool that reads it,
|
|
19
|
+
// the same input as a model choosing a page, so it needs no key.
|
|
20
|
+
// - A cursor is model-supplied input. `readCursor` checks it before any
|
|
21
|
+
// request: not a string, over CURSOR_MAX_LENGTH, not in the form above,
|
|
22
|
+
// a digest that does not match this call, or a position above the bound
|
|
23
|
+
// the tool passes (required: each list tool passes its own) is a
|
|
24
|
+
// `validation` error with `http_status: null`, and its message never quotes the cursor. What it
|
|
25
|
+
// returns is a position and nothing else: no cursor content can reach a
|
|
26
|
+
// URL, a host or a path.
|
|
27
|
+
import { createHash } from "node:crypto";
|
|
28
|
+
/** The argument that carries the cursor; left out of the digest. */
|
|
29
|
+
const CURSOR_ARG = "cursor";
|
|
30
|
+
/** Longer than any cursor this module gives (16 digits, a dot, 43 digest characters). */
|
|
31
|
+
export const CURSOR_MAX_LENGTH = 64;
|
|
32
|
+
const CURSOR_FORM = /^(0|[1-9][0-9]{0,15})\.([A-Za-z0-9_-]{43})$/;
|
|
33
|
+
/** Throws a `TypeError` for a `next` that is not a whole number, zero or more. */
|
|
34
|
+
export function listEnvelope(call, page) {
|
|
35
|
+
const envelope = {
|
|
36
|
+
items: page.items,
|
|
37
|
+
has_more: page.next !== undefined,
|
|
38
|
+
api_calls_used: page.api_calls_used,
|
|
39
|
+
};
|
|
40
|
+
if (page.next !== undefined) {
|
|
41
|
+
if (!Number.isSafeInteger(page.next) || page.next < 0) {
|
|
42
|
+
throw new TypeError("A list's next position is a whole number, zero or more.");
|
|
43
|
+
}
|
|
44
|
+
envelope.next_cursor = `${page.next}.${digest(call, page.next)}`;
|
|
45
|
+
}
|
|
46
|
+
if (page.truncated === true)
|
|
47
|
+
envelope.truncated = true;
|
|
48
|
+
return envelope;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The position the call's `cursor` argument gives: 0 without one.
|
|
52
|
+
* `maxPosition` is the tool's own bound, required (each list tool passes the
|
|
53
|
+
* last position it can serve); a position above it is refused like a bad
|
|
54
|
+
* cursor. Throws a `TypeError` for a bound that is missing or not a whole
|
|
55
|
+
* number, zero or more, and for arguments that are not JSON (see `canonical`).
|
|
56
|
+
*/
|
|
57
|
+
export function readCursor(call, maxPosition) {
|
|
58
|
+
if (!(Number.isSafeInteger(maxPosition) && maxPosition >= 0)) {
|
|
59
|
+
throw new TypeError("A cursor's maximum position is a whole number, zero or more.");
|
|
60
|
+
}
|
|
61
|
+
const cursor = call.args[CURSOR_ARG];
|
|
62
|
+
if (cursor === undefined)
|
|
63
|
+
return { ok: true, position: 0 };
|
|
64
|
+
if (typeof cursor !== "string") {
|
|
65
|
+
return refuse("the cursor is not one this server gave");
|
|
66
|
+
}
|
|
67
|
+
if (cursor.length > CURSOR_MAX_LENGTH) {
|
|
68
|
+
return refuse(`the cursor is longer than ${CURSOR_MAX_LENGTH} characters, so it is not one this server gave`);
|
|
69
|
+
}
|
|
70
|
+
const match = CURSOR_FORM.exec(cursor);
|
|
71
|
+
const position = Number(match?.[1]);
|
|
72
|
+
if (match === null || !Number.isSafeInteger(position)) {
|
|
73
|
+
return refuse("the cursor is not one this server gave");
|
|
74
|
+
}
|
|
75
|
+
if (match[2] !== digest(call, position)) {
|
|
76
|
+
return refuse("the cursor belongs to another call; use it only with the tool and arguments it came with");
|
|
77
|
+
}
|
|
78
|
+
if (position > maxPosition) {
|
|
79
|
+
return refuse("the cursor points past the last position this tool serves");
|
|
80
|
+
}
|
|
81
|
+
return { ok: true, position };
|
|
82
|
+
}
|
|
83
|
+
function refuse(why) {
|
|
84
|
+
return {
|
|
85
|
+
ok: false,
|
|
86
|
+
error: {
|
|
87
|
+
code: "validation",
|
|
88
|
+
http_status: null,
|
|
89
|
+
message: `Not sent: ${why}. Call again without a cursor to start over.`,
|
|
90
|
+
retryable: false,
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
function digest(call, position) {
|
|
95
|
+
const text = `[${JSON.stringify(call.tool)},${canonical(call.args, CURSOR_ARG)},${position}]`;
|
|
96
|
+
return createHash("sha256").update(text).digest("base64url");
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* JSON with object keys sorted, `omit` left out of the top-level object.
|
|
100
|
+
* Fails closed: anything that is not a plain object (prototype
|
|
101
|
+
* `Object.prototype` or `null`), an array, a string, a finite number, a
|
|
102
|
+
* boolean or `null` throws a `TypeError` (a caller passing other than the raw
|
|
103
|
+
* JSON arguments). `undefined` is absent, as in JSON: left out of an object,
|
|
104
|
+
* `null` in an array. `-0` is written `0`, as JSON does.
|
|
105
|
+
*/
|
|
106
|
+
function canonical(value, omit) {
|
|
107
|
+
if (value === null || typeof value === "string" || typeof value === "boolean") {
|
|
108
|
+
return JSON.stringify(value);
|
|
109
|
+
}
|
|
110
|
+
if (typeof value === "number") {
|
|
111
|
+
if (!Number.isFinite(value))
|
|
112
|
+
throw new TypeError("A list call's argument is not JSON.");
|
|
113
|
+
return JSON.stringify(value);
|
|
114
|
+
}
|
|
115
|
+
if (Array.isArray(value)) {
|
|
116
|
+
return `[${Array.from(value, (item) => (item === undefined ? "null" : canonical(item))).join(",")}]`;
|
|
117
|
+
}
|
|
118
|
+
if (typeof value === "object") {
|
|
119
|
+
const proto = Object.getPrototypeOf(value);
|
|
120
|
+
if (proto === Object.prototype || proto === null) {
|
|
121
|
+
const record = value;
|
|
122
|
+
const parts = Object.keys(record)
|
|
123
|
+
.sort()
|
|
124
|
+
.filter((key) => key !== omit && record[key] !== undefined)
|
|
125
|
+
.map((key) => `${JSON.stringify(key)}:${canonical(record[key])}`);
|
|
126
|
+
return `{${parts.join(",")}}`;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
throw new TypeError("A list call's argument is not JSON.");
|
|
130
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
// Ticket rows (SPEC §8.4): what the list tools and `get_ticket`'s recent
|
|
2
|
+
// tickets return for a ticket, built on `ticketOutput` as it is.
|
|
3
|
+
//
|
|
4
|
+
// - `ticketRow`: `{id, subject, status, priority, category, updated,
|
|
5
|
+
// agent, group}`. The subject is customer text: cut to SUBJECT_CAP code
|
|
6
|
+
// points, inside `<ticket_content>`, the marker for a cut one after the
|
|
7
|
+
// block. Status, priority, agent and group are names through `fieldValue`
|
|
8
|
+
// (stripped of HTML, capped, not wrapped); an id it cannot name is said
|
|
9
|
+
// so by `ticketOutput` (an agent missing from a truncated list "not
|
|
10
|
+
// loaded"). A field the schema lacks, or the team hides, leaves its key
|
|
11
|
+
// out (status, priority, agent, group and each category field alike);
|
|
12
|
+
// the subject is always shown: `hidden` is not guaranteed in rows (SPEC
|
|
13
|
+
// §7.3). An unassigned agent or group is `null`.
|
|
14
|
+
// - `category` reuses the uncategorised definition (SPEC §10), so the
|
|
15
|
+
// ticket queries read the same setting the same way: `{label: value}`
|
|
16
|
+
// for the fields the team's `uncategorised_fields` names (by label or API
|
|
17
|
+
// name, trimmed, NFC, any case; a name of a nested field's level selects
|
|
18
|
+
// that nested field, shown as its usual path), else for the first nested
|
|
19
|
+
// field. The label goes through `plainName`, the value through
|
|
20
|
+
// `fieldValue`: a validated choice path is plain, anything else wrapped.
|
|
21
|
+
// - `updated`: the ticket's `updated_at` as a UTC ISO 8601 instant when it
|
|
22
|
+
// is a valid ISO 8601 date-time with a zone of `Z` or `±HH:MM`, else
|
|
23
|
+
// `null` (`+0100` included).
|
|
24
|
+
// - `ticketDetail`: `{id, updated, fields}`, `fields` every field
|
|
25
|
+
// `ticketOutput` gives, each value through `fieldValue` by its own
|
|
26
|
+
// field's type; the description as text (`bodyText`), wrapped. A caller's
|
|
27
|
+
// `CutOptions` (a cap, how to fetch the rest) apply to every wrapped
|
|
28
|
+
// value, the description included; without them, format.ts's TEXT_CAP.
|
|
29
|
+
// - Keys of `category` and `fields`: the label through `plainName`; a key
|
|
30
|
+
// already used keys the later field `Label (api_name)`, and if that is
|
|
31
|
+
// taken too, `Label (api_name) (2)`, `(3)`, ...: no value is dropped.
|
|
32
|
+
import { bodyText, fieldValue, plainName, untrusted } from "./format.js";
|
|
33
|
+
/** Code points of a row's subject. */
|
|
34
|
+
export const SUBJECT_CAP = 120;
|
|
35
|
+
/** The row's name keys and the field type each is read from. */
|
|
36
|
+
const ROW_TYPES = {
|
|
37
|
+
status: "default_status",
|
|
38
|
+
priority: "default_priority",
|
|
39
|
+
agent: "default_agent",
|
|
40
|
+
group: "default_group",
|
|
41
|
+
};
|
|
42
|
+
const ISO_WITH_ZONE = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d{1,9})?)?(?:Z|[+-]\d{2}:\d{2})$/;
|
|
43
|
+
function fold(text) {
|
|
44
|
+
return text.normalize("NFC").trim().toLowerCase();
|
|
45
|
+
}
|
|
46
|
+
/** One field through `ticketOutput`: `{ found: false }` when it gives no key (absent or hidden). */
|
|
47
|
+
function outputOf(ctx, schema, field, ticket) {
|
|
48
|
+
const out = ctx.ticketOutput({ ...schema, fields: [field] }, ticket);
|
|
49
|
+
const values = Object.values(out);
|
|
50
|
+
return values.length === 0 ? { found: false } : { found: true, value: values[0] };
|
|
51
|
+
}
|
|
52
|
+
/** A UTC ISO 8601 instant from an ISO 8601 date-time with a zone (`Z` or `±HH:MM`), else `null`. */
|
|
53
|
+
export function isoUpdated(value) {
|
|
54
|
+
if (typeof value !== "string" || !ISO_WITH_ZONE.test(value))
|
|
55
|
+
return null;
|
|
56
|
+
const time = Date.parse(value);
|
|
57
|
+
return Number.isNaN(time) ? null : new Date(time).toISOString();
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The category fields (SPEC §10), shared with ticket-query.ts's
|
|
61
|
+
* `uncategorized`: the fields `uncategorised_fields` names, else the first
|
|
62
|
+
* nested field.
|
|
63
|
+
*/
|
|
64
|
+
export function categoryFields(ctx, schema) {
|
|
65
|
+
const listed = ctx.team.uncategorised_fields;
|
|
66
|
+
if (listed === undefined) {
|
|
67
|
+
const nested = schema.fields.find((field) => field.type === "nested_field");
|
|
68
|
+
return nested ? [nested] : [];
|
|
69
|
+
}
|
|
70
|
+
const keys = new Set(listed.map(fold));
|
|
71
|
+
const named = (...names) => names.some((name) => keys.has(fold(name)));
|
|
72
|
+
return schema.fields.filter((field) => named(field.label, field.name) ||
|
|
73
|
+
(field.type === "nested_field" &&
|
|
74
|
+
(field.nested_ticket_fields ?? []).some((level) => named(level.label, level.name))));
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Under the field's cleaned label, or `Label (api_name)` when that is used,
|
|
78
|
+
* then a counter: no value is dropped. Defined, not assigned: a key
|
|
79
|
+
* `__proto__` stays an own key.
|
|
80
|
+
*/
|
|
81
|
+
function put(out, field, value) {
|
|
82
|
+
const label = plainName(field.label);
|
|
83
|
+
let key = label;
|
|
84
|
+
if (Object.hasOwn(out, key)) {
|
|
85
|
+
const base = `${label} (${plainName(field.name)})`;
|
|
86
|
+
key = base;
|
|
87
|
+
for (let n = 2; Object.hasOwn(out, key); n++)
|
|
88
|
+
key = `${base} (${n})`;
|
|
89
|
+
}
|
|
90
|
+
Object.defineProperty(out, key, { value, enumerable: true, writable: true, configurable: true });
|
|
91
|
+
}
|
|
92
|
+
export function ticketRow(ctx, schema, ticket) {
|
|
93
|
+
const named = (key) => {
|
|
94
|
+
const field = schema.fields.find((candidate) => candidate.type === ROW_TYPES[key]);
|
|
95
|
+
if (!field)
|
|
96
|
+
return {};
|
|
97
|
+
const shown = outputOf(ctx, schema, field, ticket);
|
|
98
|
+
return shown.found ? { [key]: fieldValue(field, shown.value) } : {};
|
|
99
|
+
};
|
|
100
|
+
const category = {};
|
|
101
|
+
for (const field of categoryFields(ctx, schema)) {
|
|
102
|
+
const shown = outputOf(ctx, schema, field, ticket);
|
|
103
|
+
if (shown.found)
|
|
104
|
+
put(category, field, fieldValue(field, shown.value));
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
id: ticket.id,
|
|
108
|
+
subject: untrusted(ticket.subject, {
|
|
109
|
+
cap: SUBJECT_CAP,
|
|
110
|
+
rest: "get_ticket returns the full subject",
|
|
111
|
+
}),
|
|
112
|
+
...named("status"),
|
|
113
|
+
...named("priority"),
|
|
114
|
+
category,
|
|
115
|
+
updated: isoUpdated(ticket.updated_at),
|
|
116
|
+
...named("agent"),
|
|
117
|
+
...named("group"),
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
export function ticketDetail(ctx, schema, ticket, options) {
|
|
121
|
+
const fields = {};
|
|
122
|
+
for (const field of schema.fields) {
|
|
123
|
+
const shown = outputOf(ctx, schema, field, ticket);
|
|
124
|
+
if (!shown.found)
|
|
125
|
+
continue;
|
|
126
|
+
const value = field.type === "default_description"
|
|
127
|
+
? bodyText(ticket.description_text, shown.value, options)
|
|
128
|
+
: fieldValue(field, shown.value, options);
|
|
129
|
+
put(fields, field, value);
|
|
130
|
+
}
|
|
131
|
+
return { id: ticket.id, updated: isoUpdated(ticket.updated_at), fields };
|
|
132
|
+
}
|