@zanichelli/freshdesk-mcp 0.0.0 → 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 +205 -0
- 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 +40 -3
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
// Canned responses (SPEC §7.1, §8.2): the reads T-034's
|
|
2
|
+
// `list_canned_responses` and `get_canned_response` stand on. Create and
|
|
3
|
+
// update are `canned_write`'s (SPEC §8.3), not here.
|
|
4
|
+
//
|
|
5
|
+
// - `listCannedFolders`: every page of `GET /canned_response_folders`,
|
|
6
|
+
// through `list-all.ts`: 100 per page, at most 10 pages, `truncated` past
|
|
7
|
+
// the cap; a malformed page and an HTTP failure as `listAll` returns them.
|
|
8
|
+
// Freshdesk documents no pagination for it; `per_page` and a `Link` are
|
|
9
|
+
// followed if sent.
|
|
10
|
+
// - `listCannedResponses`: `GET /canned_response_folders/{id}`, one
|
|
11
|
+
// request: the folder with its responses' ids and titles
|
|
12
|
+
// (`canned_responses`). Not `/canned_response_folders/{id}/responses`,
|
|
13
|
+
// which sends every response's content and attachments too.
|
|
14
|
+
// - `getCannedResponse`: `GET /canned_responses/{id}`.
|
|
15
|
+
// Both lists take an optional trailing cost class (`cheap` unless given),
|
|
16
|
+
// for a tool that walks folders.
|
|
17
|
+
//
|
|
18
|
+
// Freshdesk documents these endpoints as admin-only: for any other key they
|
|
19
|
+
// fail as Freshdesk answers (a 403 is `forbidden`). Shapes are Freshdesk's API
|
|
20
|
+
// v2 documentation, not measured on an account.
|
|
21
|
+
//
|
|
22
|
+
// What is returned is a fixed allow-list built field by field, not a
|
|
23
|
+
// pass-through: a folder is `{id, name, personal}`, a listed response
|
|
24
|
+
// `{id, title}`, a viewed response `{id, title, text, folder_id, visibility}`
|
|
25
|
+
// with `text` Freshdesk's plain-text `content` (the HTML `content_html` is
|
|
26
|
+
// dropped, as are attachments and `group_ids`; `null` stays `null`). Each
|
|
27
|
+
// needs a whole-number `id`, a string `name` or `title`, and a `content`
|
|
28
|
+
// string or null. `personal` (documented on a listed folder: true when only
|
|
29
|
+
// its owner sees it) is a boolean, or `null` when missing or unreadable.
|
|
30
|
+
// `visibility` (documented on create: 0 all agents, 1 personal, 2 selected
|
|
31
|
+
// groups; not in the documented view sample) is a whole number, or `null`
|
|
32
|
+
// likewise, so a caller can label an unknown one as unknown; `folder_id` a
|
|
33
|
+
// positive safe integer, or `null` likewise. A body in any other shape is
|
|
34
|
+
// `server` with the response's real status, not retryable, and a fixed
|
|
35
|
+
// message that quotes nothing from it. An HTTP failure (a 404 is
|
|
36
|
+
// `not_found`) is returned unchanged.
|
|
37
|
+
//
|
|
38
|
+
// Values are checked before anything is sent, whatever a cast let through: an
|
|
39
|
+
// id is a positive safe integer. Any other is `validation` with
|
|
40
|
+
// `http_status: null`, naming the parameter, not the value, and no request is
|
|
41
|
+
// sent.
|
|
42
|
+
//
|
|
43
|
+
// `attempts` is the requests sent, retries included: what a tool reports as
|
|
44
|
+
// API calls. A `listCannedResponses` or `getCannedResponse` that fails after
|
|
45
|
+
// its id was accepted carries it too (0 when nothing was sent). `deadline`
|
|
46
|
+
// (epoch ms, optional) is the caller's: see `requestWithRetry` and `paginate`.
|
|
47
|
+
import { z } from "zod";
|
|
48
|
+
import { listAll } from "./list-all.js";
|
|
49
|
+
// `z.object` strips unknown keys; the results are still built field by field.
|
|
50
|
+
const folder = z.object({
|
|
51
|
+
id: z.number().int(),
|
|
52
|
+
name: z.string(),
|
|
53
|
+
personal: z.boolean().nullable().catch(null),
|
|
54
|
+
});
|
|
55
|
+
const listed = z.object({ id: z.number().int(), title: z.string() });
|
|
56
|
+
const folderView = z.object({ canned_responses: listed.array() });
|
|
57
|
+
const viewed = listed.extend({
|
|
58
|
+
content: z.string().nullable(),
|
|
59
|
+
folder_id: z.number().int().positive().max(Number.MAX_SAFE_INTEGER).nullable().catch(null),
|
|
60
|
+
visibility: z.number().int().nullable().catch(null),
|
|
61
|
+
});
|
|
62
|
+
export async function listCannedFolders(http, deadline, cost) {
|
|
63
|
+
const result = await listAll(http, "/canned_response_folders", folder, "canned response folders", deadline, undefined, cost);
|
|
64
|
+
if (!result.ok)
|
|
65
|
+
return result;
|
|
66
|
+
const items = result.items.map((item) => ({
|
|
67
|
+
id: item.id,
|
|
68
|
+
name: item.name,
|
|
69
|
+
personal: item.personal,
|
|
70
|
+
}));
|
|
71
|
+
return { ...result, items };
|
|
72
|
+
}
|
|
73
|
+
export async function listCannedResponses(http, folderId, deadline, cost) {
|
|
74
|
+
if (!isPositiveInt(folderId))
|
|
75
|
+
return refused("folder id");
|
|
76
|
+
const result = await http.requestWithRetry({
|
|
77
|
+
path: "/canned_response_folders/{id}",
|
|
78
|
+
params: { id: folderId },
|
|
79
|
+
...(cost === undefined ? {} : { cost }),
|
|
80
|
+
}, deadline);
|
|
81
|
+
if (!result.ok)
|
|
82
|
+
return { ok: false, error: result.error, attempts: result.attempts };
|
|
83
|
+
const parsed = folderView.safeParse(result.body);
|
|
84
|
+
if (!parsed.success) {
|
|
85
|
+
return { ...malformed("a canned response folder", result.status), attempts: result.attempts };
|
|
86
|
+
}
|
|
87
|
+
const responses = parsed.data.canned_responses.map((item) => ({
|
|
88
|
+
id: item.id,
|
|
89
|
+
title: item.title,
|
|
90
|
+
}));
|
|
91
|
+
return { ok: true, responses, attempts: result.attempts };
|
|
92
|
+
}
|
|
93
|
+
export async function getCannedResponse(http, id, deadline) {
|
|
94
|
+
if (!isPositiveInt(id))
|
|
95
|
+
return refused("id");
|
|
96
|
+
const result = await http.requestWithRetry({ path: "/canned_responses/{id}", params: { id } }, deadline);
|
|
97
|
+
if (!result.ok)
|
|
98
|
+
return { ok: false, error: result.error, attempts: result.attempts };
|
|
99
|
+
const parsed = viewed.safeParse(result.body);
|
|
100
|
+
if (!parsed.success) {
|
|
101
|
+
return { ...malformed("a canned response", result.status), attempts: result.attempts };
|
|
102
|
+
}
|
|
103
|
+
const { title, content, folder_id, visibility } = parsed.data;
|
|
104
|
+
return {
|
|
105
|
+
ok: true,
|
|
106
|
+
response: { id: parsed.data.id, title, text: content, folder_id, visibility },
|
|
107
|
+
attempts: result.attempts,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
function malformed(noun, status) {
|
|
111
|
+
return {
|
|
112
|
+
ok: false,
|
|
113
|
+
error: {
|
|
114
|
+
code: "server",
|
|
115
|
+
http_status: status,
|
|
116
|
+
message: `Freshdesk sent ${noun} in an unexpected shape (HTTP ${status}).`,
|
|
117
|
+
retryable: false,
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
function isPositiveInt(value) {
|
|
122
|
+
return Number.isSafeInteger(value) && value > 0;
|
|
123
|
+
}
|
|
124
|
+
function refused(name) {
|
|
125
|
+
return {
|
|
126
|
+
ok: false,
|
|
127
|
+
error: {
|
|
128
|
+
code: "validation",
|
|
129
|
+
http_status: null,
|
|
130
|
+
message: `Not sent: ${name} is not a valid value.`,
|
|
131
|
+
retryable: false,
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// Companies (SPEC §7.1, §8.2): `getCompany`, one `GET /companies/{id}`, for
|
|
2
|
+
// the company name in `get_ticket`'s requester block.
|
|
3
|
+
//
|
|
4
|
+
// The returned company is a fixed allow-list, not a pass-through: `id` and
|
|
5
|
+
// `name`, nothing else (notes, domains, custom fields and every other key
|
|
6
|
+
// are dropped here). A company needs a whole-number `id` and a string
|
|
7
|
+
// `name`. A body in any other shape is `server` with the response's real
|
|
8
|
+
// status, not retryable, and a fixed message that quotes nothing from it. An
|
|
9
|
+
// HTTP failure (a 404 is `not_found`) is returned unchanged.
|
|
10
|
+
//
|
|
11
|
+
// The id is checked before anything is sent, whatever a cast let through: a
|
|
12
|
+
// positive safe integer, or `validation` with `http_status: null`, naming
|
|
13
|
+
// the parameter, not the value, and no request is sent.
|
|
14
|
+
//
|
|
15
|
+
// `attempts` is the requests sent, retries included. `deadline` (epoch ms,
|
|
16
|
+
// optional) is the caller's: see `requestWithRetry`.
|
|
17
|
+
import { z } from "zod";
|
|
18
|
+
// `z.object` strips unknown keys; the result is still built field by field.
|
|
19
|
+
const body = z.object({
|
|
20
|
+
id: z.number().int(),
|
|
21
|
+
name: z.string(),
|
|
22
|
+
});
|
|
23
|
+
export async function getCompany(http, id, deadline) {
|
|
24
|
+
if (!(Number.isSafeInteger(id) && id > 0))
|
|
25
|
+
return refused("id");
|
|
26
|
+
const result = await http.requestWithRetry({ path: "/companies/{id}", params: { id } }, deadline);
|
|
27
|
+
if (!result.ok)
|
|
28
|
+
return { ok: false, error: result.error };
|
|
29
|
+
const parsed = body.safeParse(result.body);
|
|
30
|
+
if (!parsed.success) {
|
|
31
|
+
return {
|
|
32
|
+
ok: false,
|
|
33
|
+
error: {
|
|
34
|
+
code: "server",
|
|
35
|
+
http_status: result.status,
|
|
36
|
+
message: `Freshdesk sent a company in an unexpected shape (HTTP ${result.status}).`,
|
|
37
|
+
retryable: false,
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
ok: true,
|
|
43
|
+
company: { id: parsed.data.id, name: parsed.data.name },
|
|
44
|
+
attempts: result.attempts,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function refused(name) {
|
|
48
|
+
return {
|
|
49
|
+
ok: false,
|
|
50
|
+
error: {
|
|
51
|
+
code: "validation",
|
|
52
|
+
http_status: null,
|
|
53
|
+
message: `Not sent: ${name} is not a valid value.`,
|
|
54
|
+
retryable: false,
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// Contacts (SPEC §7.1, §8.2): `getContact`, one `GET /contacts/{id}`, for
|
|
2
|
+
// `get_ticket`'s requester block (name, email, company).
|
|
3
|
+
//
|
|
4
|
+
// The returned contact is a fixed allow-list, not a pass-through: `name`,
|
|
5
|
+
// `email` and `company_id`, nothing else. Phone, mobile, address and every
|
|
6
|
+
// other key Freshdesk sends are dropped here, so no caller can return them.
|
|
7
|
+
// A contact needs a string `name`; `email` is a string or null (a contact
|
|
8
|
+
// may have only a phone), and a missing `email` key is `server`: Freshdesk
|
|
9
|
+
// always sends it; `company_id` a whole number or null, a missing key
|
|
10
|
+
// read as null (no company). A body in any other shape is `server` with the
|
|
11
|
+
// response's real status, not retryable, and a fixed message that quotes
|
|
12
|
+
// nothing from it. An HTTP failure (a 404 is `not_found`) is returned
|
|
13
|
+
// unchanged.
|
|
14
|
+
//
|
|
15
|
+
// The id is checked before anything is sent, whatever a cast let through: a
|
|
16
|
+
// positive safe integer, or `validation` with `http_status: null`, naming
|
|
17
|
+
// the parameter, not the value, and no request is sent.
|
|
18
|
+
//
|
|
19
|
+
// `attempts` is the requests sent, retries included. `deadline` (epoch ms,
|
|
20
|
+
// optional) is the caller's: see `requestWithRetry`.
|
|
21
|
+
import { z } from "zod";
|
|
22
|
+
// `z.object` strips unknown keys; the result is still built field by field.
|
|
23
|
+
const body = z.object({
|
|
24
|
+
name: z.string(),
|
|
25
|
+
email: z.string().nullable(),
|
|
26
|
+
company_id: z.number().int().nullish(),
|
|
27
|
+
});
|
|
28
|
+
export async function getContact(http, id, deadline) {
|
|
29
|
+
if (!(Number.isSafeInteger(id) && id > 0))
|
|
30
|
+
return refused("id");
|
|
31
|
+
const result = await http.requestWithRetry({ path: "/contacts/{id}", params: { id } }, deadline);
|
|
32
|
+
if (!result.ok)
|
|
33
|
+
return { ok: false, error: result.error };
|
|
34
|
+
const parsed = body.safeParse(result.body);
|
|
35
|
+
if (!parsed.success) {
|
|
36
|
+
return {
|
|
37
|
+
ok: false,
|
|
38
|
+
error: {
|
|
39
|
+
code: "server",
|
|
40
|
+
http_status: result.status,
|
|
41
|
+
message: `Freshdesk sent a contact in an unexpected shape (HTTP ${result.status}).`,
|
|
42
|
+
retryable: false,
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
const { name, email, company_id } = parsed.data;
|
|
47
|
+
return {
|
|
48
|
+
ok: true,
|
|
49
|
+
contact: { name, email, company_id: company_id ?? null },
|
|
50
|
+
attempts: result.attempts,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
function refused(name) {
|
|
54
|
+
return {
|
|
55
|
+
ok: false,
|
|
56
|
+
error: {
|
|
57
|
+
code: "validation",
|
|
58
|
+
http_status: null,
|
|
59
|
+
message: `Not sent: ${name} is not a valid value.`,
|
|
60
|
+
retryable: false,
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Groups (SPEC §7.3): `GET /groups`, every page, for the schema service's
|
|
2
|
+
// id → name map.
|
|
3
|
+
//
|
|
4
|
+
// A group needs a whole-number `id` and a non-empty `name`; keys this module
|
|
5
|
+
// does not know are passed through. Paging and the malformed page error: see
|
|
6
|
+
// `list-all.ts`.
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
import { listAll } from "./list-all.js";
|
|
9
|
+
const group = z.looseObject({
|
|
10
|
+
id: z.number().int(),
|
|
11
|
+
name: z.string().min(1),
|
|
12
|
+
});
|
|
13
|
+
export function listGroups(http, deadline) {
|
|
14
|
+
return listAll(http, "/groups", group, "groups", deadline);
|
|
15
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// Every item of a paged list, each page checked by a zod schema before the
|
|
2
|
+
// next is asked for. Its callers: `agents.ts` and `groups.ts` (SPEC §7.3),
|
|
3
|
+
// `tickets.ts`'s `listConversations`, `solutions.ts`'s three lists,
|
|
4
|
+
// `canned.ts`'s `listCannedFolders`.
|
|
5
|
+
//
|
|
6
|
+
// Pages of PER_PAGE (Freshdesk's maximum) through `paginate`, at most
|
|
7
|
+
// MAX_PAGES of them: past the cap the list ends where it stopped, and
|
|
8
|
+
// `truncated` says so. `attempts` is the requests sent for all the pages,
|
|
9
|
+
// retries included (`paginate`'s count): what the caller reports as API calls.
|
|
10
|
+
// A failed request carries it too (the earlier pages' requests included);
|
|
11
|
+
// a page in an unexpected shape does not.
|
|
12
|
+
//
|
|
13
|
+
// A page in any other shape is `server` with that page's real status, not
|
|
14
|
+
// retryable, and no further page is asked for. Its message is fixed text: it
|
|
15
|
+
// quotes nothing from the body. An HTTP failure is returned unchanged.
|
|
16
|
+
//
|
|
17
|
+
// `deadline` (epoch ms, optional) is the caller's, for all the pages: see
|
|
18
|
+
// `paginate`. `params` fill the path template (`/tickets/{id}/conversations`).
|
|
19
|
+
// `cost` (optional) is every page's cost class for the limiter, `cheap`
|
|
20
|
+
// unless given.
|
|
21
|
+
export const PER_PAGE = 100;
|
|
22
|
+
export const MAX_PAGES = 10;
|
|
23
|
+
/** `noun` names the list in the error message: "agents", "groups", "conversations". */
|
|
24
|
+
export async function listAll(http, path, item, noun, deadline, params, cost) {
|
|
25
|
+
const items = [];
|
|
26
|
+
const pages = http.paginate({ path, params, query: { per_page: PER_PAGE }, ...(cost === undefined ? {} : { cost }) }, { pages: MAX_PAGES }, deadline);
|
|
27
|
+
for (;;) {
|
|
28
|
+
// By hand, not `for await`: the return value is where `truncated` is.
|
|
29
|
+
const step = await pages.next();
|
|
30
|
+
if (step.done) {
|
|
31
|
+
const { truncated, attempts } = step.value;
|
|
32
|
+
return { ok: true, items, truncated, attempts };
|
|
33
|
+
}
|
|
34
|
+
const result = step.value;
|
|
35
|
+
if (!result.ok) {
|
|
36
|
+
// After a failed page `paginate` ends, sending nothing more, and returns its count.
|
|
37
|
+
const end = await pages.next();
|
|
38
|
+
return { ...result, attempts: end.done ? end.value.attempts : 0 };
|
|
39
|
+
}
|
|
40
|
+
const parsed = item.array().safeParse(result.body);
|
|
41
|
+
if (!parsed.success) {
|
|
42
|
+
return {
|
|
43
|
+
ok: false,
|
|
44
|
+
error: {
|
|
45
|
+
code: "server",
|
|
46
|
+
http_status: result.status,
|
|
47
|
+
message: `Freshdesk sent ${noun} in an unexpected shape (HTTP ${result.status}).`,
|
|
48
|
+
retryable: false,
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
items.push(...parsed.data);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// Ticket search (SPEC §5, §7.1): one page of `GET /search/tickets`, with the
|
|
2
|
+
// limits docs/architecture/ticket-queries.md ("Parameters") records. Building
|
|
3
|
+
// the query from filters is T-043's; the tool is T-028's.
|
|
4
|
+
//
|
|
5
|
+
// - The caller gives the conditions (`status:2 AND priority:3`); this module
|
|
6
|
+
// puts them in double quotes and `HttpClient` percent-encodes them with
|
|
7
|
+
// `URLSearchParams`, a space as `+` (measured to work, 2026-11-24).
|
|
8
|
+
// - `query` is a non-blank string with no double quote (it would end the
|
|
9
|
+
// quoted query) and no backslash (it could escape one), at most
|
|
10
|
+
// MAX_QUERY_LENGTH UTF-16 units once quoted:
|
|
11
|
+
// Freshdesk documents 512, and counting the quotes is the cautious
|
|
12
|
+
// reading. `page` is a whole number 1–10: page 11 is refused by
|
|
13
|
+
// Freshdesk (measured 2026-11-24). Any other value, whatever a cast let
|
|
14
|
+
// through, is `validation` with `http_status: null`, naming the
|
|
15
|
+
// parameter, not the value, and no request is sent.
|
|
16
|
+
// - No `include`: search refuses `include=stats` (measured 2026-11-24).
|
|
17
|
+
// - Cost class `cheap` unless the caller gives `expensive`
|
|
18
|
+
// (`find_similar_tickets`, T-030; ticket-queries.md, "Cost class for the
|
|
19
|
+
// limiter").
|
|
20
|
+
//
|
|
21
|
+
// The body is `{total, results}`: `total` a whole number from 0 (it can
|
|
22
|
+
// exceed 300, the 10 pages' reach), `results` tickets in `tickets.ts`'s shape,
|
|
23
|
+
// unknown keys passed through. A body in any other shape is `server` with the
|
|
24
|
+
// response's real status, not retryable, and a fixed message that quotes
|
|
25
|
+
// nothing from it. An HTTP failure is returned unchanged (a 400 is
|
|
26
|
+
// `validation` with Freshdesk's `errors[]`).
|
|
27
|
+
//
|
|
28
|
+
// `attempts` is the requests sent, retries included; a page that fails after
|
|
29
|
+
// its values were accepted carries it too (0 when the limiter refused it
|
|
30
|
+
// before any request). `deadline` (epoch ms, optional) is the caller's: see
|
|
31
|
+
// `requestWithRetry`.
|
|
32
|
+
import { z } from "zod";
|
|
33
|
+
import { ticket } from "./tickets.js";
|
|
34
|
+
/** Freshdesk's limit on the query, counted here with its two quotes. */
|
|
35
|
+
export const MAX_QUERY_LENGTH = 512;
|
|
36
|
+
/** Results on a full page of search (documented; a page of 30 measured 2026-11-24). */
|
|
37
|
+
export const SEARCH_PER_PAGE = 30;
|
|
38
|
+
/** Search refuses any page past this one. */
|
|
39
|
+
export const MAX_SEARCH_PAGE = 10;
|
|
40
|
+
/** The last cursor position of a tool whose position is a search page. */
|
|
41
|
+
export const MAX_SEARCH_POSITION = MAX_SEARCH_PAGE - 1;
|
|
42
|
+
const body = z.object({
|
|
43
|
+
total: z.number().int().nonnegative(),
|
|
44
|
+
results: ticket.array(),
|
|
45
|
+
});
|
|
46
|
+
export async function searchTickets(http, query, page = 1, deadline, cost) {
|
|
47
|
+
if (typeof query !== "string" ||
|
|
48
|
+
query.trim() === "" ||
|
|
49
|
+
query.includes('"') ||
|
|
50
|
+
query.includes("\\")) {
|
|
51
|
+
return refused("query");
|
|
52
|
+
}
|
|
53
|
+
const quoted = `"${query}"`;
|
|
54
|
+
if (quoted.length > MAX_QUERY_LENGTH)
|
|
55
|
+
return refused("query");
|
|
56
|
+
if (!(Number.isInteger(page) && page >= 1 && page <= MAX_SEARCH_PAGE))
|
|
57
|
+
return refused("page");
|
|
58
|
+
const result = await http.requestWithRetry({
|
|
59
|
+
path: "/search/tickets",
|
|
60
|
+
query: { query: quoted, page },
|
|
61
|
+
...(cost === undefined ? {} : { cost }),
|
|
62
|
+
}, deadline);
|
|
63
|
+
if (!result.ok)
|
|
64
|
+
return { ok: false, error: result.error, attempts: result.attempts };
|
|
65
|
+
const parsed = body.safeParse(result.body);
|
|
66
|
+
if (!parsed.success) {
|
|
67
|
+
return {
|
|
68
|
+
ok: false,
|
|
69
|
+
error: {
|
|
70
|
+
code: "server",
|
|
71
|
+
http_status: result.status,
|
|
72
|
+
message: `Freshdesk sent search results in an unexpected shape (HTTP ${result.status}).`,
|
|
73
|
+
retryable: false,
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
return {
|
|
78
|
+
ok: true,
|
|
79
|
+
results: parsed.data.results,
|
|
80
|
+
total: parsed.data.total,
|
|
81
|
+
attempts: result.attempts,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
function refused(name) {
|
|
85
|
+
return {
|
|
86
|
+
ok: false,
|
|
87
|
+
error: {
|
|
88
|
+
code: "validation",
|
|
89
|
+
http_status: null,
|
|
90
|
+
message: `Not sent: ${name} is not a valid value.`,
|
|
91
|
+
retryable: false,
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
// Solutions, the knowledge base (SPEC §7.1, §8.2): the reads T-032's
|
|
2
|
+
// `search_solutions`, `get_solution_article` and `list_solution_tree` stand on.
|
|
3
|
+
// Create, update and translate are `kb_write`'s (SPEC §8.3), not here.
|
|
4
|
+
//
|
|
5
|
+
// - `listCategories`: every page of `GET /solutions/categories`;
|
|
6
|
+
// `listFolders`: of `GET /solutions/categories/{id}/folders`;
|
|
7
|
+
// `listArticles`: of `GET /solutions/folders/{id}/articles`. All three
|
|
8
|
+
// through `list-all.ts`: 100 per page, at most 10 pages, `truncated` past
|
|
9
|
+
// the cap; a malformed page and an HTTP failure as `listAll` returns them.
|
|
10
|
+
// Each takes an optional trailing cost class for every page (`cheap`
|
|
11
|
+
// unless given; `expensive` for `list_solution_tree`'s walk, T-032).
|
|
12
|
+
// Subfolders (`GET /solutions/folders/{id}/subfolders`) are not read:
|
|
13
|
+
// an article in a subfolder is reached only through search.
|
|
14
|
+
// - `getCategory`: `GET /solutions/categories/{id}`, the one category
|
|
15
|
+
// `list_solution_tree` is asked for (T-058): a 404 is `not_found`. It
|
|
16
|
+
// takes the same optional trailing cost class as the lists.
|
|
17
|
+
// - `getArticle`: `GET /solutions/articles/{id}`.
|
|
18
|
+
// - `getFolder`: `GET /solutions/folders/{id}`, for the visibility of a
|
|
19
|
+
// viewed article's folder (T-032).
|
|
20
|
+
// - `searchArticles`: `GET /search/solutions?term=`, one request; the body
|
|
21
|
+
// is a list of articles. `HttpClient` percent-encodes the term with
|
|
22
|
+
// `URLSearchParams`, a space as `+`. Freshdesk documents no page
|
|
23
|
+
// parameter for it.
|
|
24
|
+
//
|
|
25
|
+
// Shapes are Freshdesk's API v2 documentation, not measured on an account.
|
|
26
|
+
// What is returned is a fixed allow-list built field by field, not a
|
|
27
|
+
// pass-through: a category is `{id, name}`, a folder `{id, name, visibility}`,
|
|
28
|
+
// an article in a list `{id, title, status, updated_at}`, a search hit the
|
|
29
|
+
// same plus `folder_visibility`, a viewed article `{id, title, text, status,
|
|
30
|
+
// updated_at, folder_id}` with `text` Freshdesk's plain-text `description_text` (the
|
|
31
|
+
// HTML `description` is dropped; `null` stays `null`, an empty article). A
|
|
32
|
+
// category's `visible_in_portals` is dropped: portal ids, which no tool can
|
|
33
|
+
// label. Each needs a whole-number `id` and `status`, a string `name` and
|
|
34
|
+
// `title`, and a `description_text` string or null. `visibility` and
|
|
35
|
+
// `folder_visibility` are whole numbers, or `null` when missing or
|
|
36
|
+
// unreadable, so a caller can treat an unknown one as not public; a viewed
|
|
37
|
+
// article's `folder_id` a positive safe integer, or `null` likewise;
|
|
38
|
+
// `updated_at` an ISO 8601 date-time with a zone, or `null` likewise. A body
|
|
39
|
+
// in any other shape is
|
|
40
|
+
// `server` with the response's real status, not retryable, and a fixed
|
|
41
|
+
// message that quotes nothing from it. An HTTP failure (a 404 is
|
|
42
|
+
// `not_found`) is returned unchanged.
|
|
43
|
+
//
|
|
44
|
+
// Values are checked before anything is sent, whatever a cast let through: an
|
|
45
|
+
// id is a positive safe integer; the term a non-blank string of at most
|
|
46
|
+
// MAX_TERM_LENGTH UTF-16 units. Any other is `validation` with
|
|
47
|
+
// `http_status: null`, naming the parameter, not the value, and no request is
|
|
48
|
+
// sent.
|
|
49
|
+
//
|
|
50
|
+
// `attempts` is the requests sent, retries included: what a tool reports as
|
|
51
|
+
// API calls. A `getArticle`, `getCategory` or `searchArticles` that fails
|
|
52
|
+
// after its values were accepted carries it too (0 when nothing was sent).
|
|
53
|
+
// `deadline` (epoch ms, optional) is the caller's: see `requestWithRetry` and
|
|
54
|
+
// `paginate`. Every request is `cheap` unless its caller gives a cost class
|
|
55
|
+
// (the lists and `getCategory`): each tool call is a bounded number of them.
|
|
56
|
+
import { z } from "zod";
|
|
57
|
+
import { listAll } from "./list-all.js";
|
|
58
|
+
/**
|
|
59
|
+
* The longest term sent. Freshdesk documents no limit; this one, the ticket
|
|
60
|
+
* search's (search.ts), keeps the URL short.
|
|
61
|
+
*/
|
|
62
|
+
export const MAX_TERM_LENGTH = 512;
|
|
63
|
+
// `z.object` strips unknown keys; the results are still built field by field.
|
|
64
|
+
const wholeOrNull = z.number().int().nullable().catch(null);
|
|
65
|
+
const named = z.object({ id: z.number().int(), name: z.string() });
|
|
66
|
+
const folder = named.extend({ visibility: wholeOrNull });
|
|
67
|
+
const listed = z.object({
|
|
68
|
+
id: z.number().int(),
|
|
69
|
+
title: z.string(),
|
|
70
|
+
status: z.number().int(),
|
|
71
|
+
updated_at: z.iso.datetime({ offset: true }).nullable().catch(null),
|
|
72
|
+
});
|
|
73
|
+
const hit = listed.extend({ folder_visibility: wholeOrNull });
|
|
74
|
+
const positiveOrNull = z
|
|
75
|
+
.number()
|
|
76
|
+
.int()
|
|
77
|
+
.positive()
|
|
78
|
+
.max(Number.MAX_SAFE_INTEGER)
|
|
79
|
+
.nullable()
|
|
80
|
+
.catch(null);
|
|
81
|
+
const viewed = listed.extend({
|
|
82
|
+
description_text: z.string().nullable(),
|
|
83
|
+
folder_id: positiveOrNull,
|
|
84
|
+
});
|
|
85
|
+
export async function listCategories(http, deadline, cost) {
|
|
86
|
+
const result = await listAll(http, "/solutions/categories", named, "solution categories", deadline, undefined, cost);
|
|
87
|
+
return result.ok ? { ...result, items: result.items.map(toNamed) } : result;
|
|
88
|
+
}
|
|
89
|
+
export async function listFolders(http, categoryId, deadline, cost) {
|
|
90
|
+
if (!isPositiveInt(categoryId))
|
|
91
|
+
return refused("category id");
|
|
92
|
+
const result = await listAll(http, "/solutions/categories/{id}/folders", folder, "solution folders", deadline, { id: categoryId }, cost);
|
|
93
|
+
return result.ok ? { ...result, items: result.items.map(toFolder) } : result;
|
|
94
|
+
}
|
|
95
|
+
export async function listArticles(http, folderId, deadline, cost) {
|
|
96
|
+
if (!isPositiveInt(folderId))
|
|
97
|
+
return refused("folder id");
|
|
98
|
+
const result = await listAll(http, "/solutions/folders/{id}/articles", listed, "solution articles", deadline, { id: folderId }, cost);
|
|
99
|
+
return result.ok ? { ...result, items: result.items.map(toSummary) } : result;
|
|
100
|
+
}
|
|
101
|
+
export async function getArticle(http, id, deadline) {
|
|
102
|
+
if (!isPositiveInt(id))
|
|
103
|
+
return refused("id");
|
|
104
|
+
const result = await http.requestWithRetry({ path: "/solutions/articles/{id}", params: { id } }, deadline);
|
|
105
|
+
if (!result.ok)
|
|
106
|
+
return { ok: false, error: result.error, attempts: result.attempts };
|
|
107
|
+
const parsed = viewed.safeParse(result.body);
|
|
108
|
+
if (!parsed.success) {
|
|
109
|
+
return { ...malformed("a solution article", result.status), attempts: result.attempts };
|
|
110
|
+
}
|
|
111
|
+
const { title, status, updated_at, description_text, folder_id } = parsed.data;
|
|
112
|
+
return {
|
|
113
|
+
ok: true,
|
|
114
|
+
article: { id: parsed.data.id, title, text: description_text, status, updated_at, folder_id },
|
|
115
|
+
attempts: result.attempts,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
export async function getCategory(http, id, deadline, cost) {
|
|
119
|
+
if (!isPositiveInt(id))
|
|
120
|
+
return refused("category id");
|
|
121
|
+
const result = await http.requestWithRetry({ path: "/solutions/categories/{id}", params: { id }, ...(cost === undefined ? {} : { cost }) }, deadline);
|
|
122
|
+
if (!result.ok)
|
|
123
|
+
return { ok: false, error: result.error, attempts: result.attempts };
|
|
124
|
+
const parsed = named.safeParse(result.body);
|
|
125
|
+
if (!parsed.success) {
|
|
126
|
+
return { ...malformed("a solution category", result.status), attempts: result.attempts };
|
|
127
|
+
}
|
|
128
|
+
return { ok: true, category: toNamed(parsed.data), attempts: result.attempts };
|
|
129
|
+
}
|
|
130
|
+
export async function getFolder(http, id, deadline) {
|
|
131
|
+
if (!isPositiveInt(id))
|
|
132
|
+
return refused("folder id");
|
|
133
|
+
const result = await http.requestWithRetry({ path: "/solutions/folders/{id}", params: { id } }, deadline);
|
|
134
|
+
if (!result.ok)
|
|
135
|
+
return { ok: false, error: result.error, attempts: result.attempts };
|
|
136
|
+
const parsed = folder.safeParse(result.body);
|
|
137
|
+
if (!parsed.success) {
|
|
138
|
+
return { ...malformed("a solution folder", result.status), attempts: result.attempts };
|
|
139
|
+
}
|
|
140
|
+
return { ok: true, folder: toFolder(parsed.data), attempts: result.attempts };
|
|
141
|
+
}
|
|
142
|
+
export async function searchArticles(http, term, deadline) {
|
|
143
|
+
if (typeof term !== "string" || term.trim() === "" || term.length > MAX_TERM_LENGTH) {
|
|
144
|
+
return refused("term");
|
|
145
|
+
}
|
|
146
|
+
const result = await http.requestWithRetry({ path: "/search/solutions", query: { term } }, deadline);
|
|
147
|
+
if (!result.ok)
|
|
148
|
+
return { ok: false, error: result.error, attempts: result.attempts };
|
|
149
|
+
const parsed = hit.array().safeParse(result.body);
|
|
150
|
+
if (!parsed.success) {
|
|
151
|
+
return { ...malformed("solution search results", result.status), attempts: result.attempts };
|
|
152
|
+
}
|
|
153
|
+
const results = parsed.data.map((item) => ({
|
|
154
|
+
...toSummary(item),
|
|
155
|
+
folder_visibility: item.folder_visibility,
|
|
156
|
+
}));
|
|
157
|
+
return { ok: true, results, attempts: result.attempts };
|
|
158
|
+
}
|
|
159
|
+
function toNamed(item) {
|
|
160
|
+
return { id: item.id, name: item.name };
|
|
161
|
+
}
|
|
162
|
+
function toFolder(item) {
|
|
163
|
+
return { id: item.id, name: item.name, visibility: item.visibility };
|
|
164
|
+
}
|
|
165
|
+
function toSummary(item) {
|
|
166
|
+
return { id: item.id, title: item.title, status: item.status, updated_at: item.updated_at };
|
|
167
|
+
}
|
|
168
|
+
function malformed(noun, status) {
|
|
169
|
+
return {
|
|
170
|
+
ok: false,
|
|
171
|
+
error: {
|
|
172
|
+
code: "server",
|
|
173
|
+
http_status: status,
|
|
174
|
+
message: `Freshdesk sent ${noun} in an unexpected shape (HTTP ${status}).`,
|
|
175
|
+
retryable: false,
|
|
176
|
+
},
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
function isPositiveInt(value) {
|
|
180
|
+
return Number.isSafeInteger(value) && value > 0;
|
|
181
|
+
}
|
|
182
|
+
function refused(name) {
|
|
183
|
+
return {
|
|
184
|
+
ok: false,
|
|
185
|
+
error: {
|
|
186
|
+
code: "validation",
|
|
187
|
+
http_status: null,
|
|
188
|
+
message: `Not sent: ${name} is not a valid value.`,
|
|
189
|
+
retryable: false,
|
|
190
|
+
},
|
|
191
|
+
};
|
|
192
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// Ticket fields (SPEC §7.3): `GET /ticket_fields`, the list the schema
|
|
2
|
+
// service maps labels and choices from. One request, not paginated.
|
|
3
|
+
//
|
|
4
|
+
// The body is checked here, not trusted: a field needs a whole-number `id`, a
|
|
5
|
+
// non-empty `name` and `label`, a `type` and a boolean `default`; keys this
|
|
6
|
+
// module does not know are passed through. `choices` takes the shapes API v2 sends:
|
|
7
|
+
// - a dropdown's list of names ["A", "B"]
|
|
8
|
+
// - a nested field's tree, two or three levels deep {"A": {"B": ["C"]}}
|
|
9
|
+
// - a status's id → [agent name, customer name] {"2": ["Open", "..."]}
|
|
10
|
+
// - a priority's or source's name → id {"Low": 1}
|
|
11
|
+
// A tree deeper than three levels is refused: Freshdesk has none. A status's
|
|
12
|
+
// choices and a 2-level tree share one shape: tell them apart by `type`.
|
|
13
|
+
// `null` for `choices` or `nested_ticket_fields` is taken as absent.
|
|
14
|
+
//
|
|
15
|
+
// A body in any other shape is `server` with the response's real status, not
|
|
16
|
+
// retryable. Its message is fixed text: it quotes nothing from the body.
|
|
17
|
+
//
|
|
18
|
+
// On success `attempts` is the requests sent, retries included
|
|
19
|
+
// (`requestWithRetry`'s count), for the schema service to report. A failure
|
|
20
|
+
// carries none.
|
|
21
|
+
import { z } from "zod";
|
|
22
|
+
const names = z.array(z.string());
|
|
23
|
+
const choiceTree = z.union([
|
|
24
|
+
names,
|
|
25
|
+
z.record(z.string(), z.union([names, z.record(z.string(), names)])),
|
|
26
|
+
]);
|
|
27
|
+
const ids = z.record(z.string(), z.number());
|
|
28
|
+
const nestedLevel = z.looseObject({
|
|
29
|
+
name: z.string(),
|
|
30
|
+
label: z.string(),
|
|
31
|
+
level: z.number().int(),
|
|
32
|
+
});
|
|
33
|
+
const ticketField = z.looseObject({
|
|
34
|
+
id: z.number().int(),
|
|
35
|
+
name: z.string().min(1),
|
|
36
|
+
label: z.string().min(1),
|
|
37
|
+
type: z.string(),
|
|
38
|
+
default: z.boolean(),
|
|
39
|
+
choices: z.union([choiceTree, ids]).nullish(),
|
|
40
|
+
nested_ticket_fields: z.array(nestedLevel).nullish(),
|
|
41
|
+
});
|
|
42
|
+
const ticketFields = z.array(ticketField);
|
|
43
|
+
/** `deadline` (epoch ms) is the caller's: see `requestWithRetry`. */
|
|
44
|
+
export async function listTicketFields(http, deadline) {
|
|
45
|
+
const result = await http.requestWithRetry({ path: "/ticket_fields" }, deadline);
|
|
46
|
+
// The error only: a failure reports no `attempts`.
|
|
47
|
+
if (!result.ok)
|
|
48
|
+
return { ok: false, error: result.error };
|
|
49
|
+
const parsed = ticketFields.safeParse(result.body);
|
|
50
|
+
if (!parsed.success) {
|
|
51
|
+
return {
|
|
52
|
+
ok: false,
|
|
53
|
+
error: {
|
|
54
|
+
code: "server",
|
|
55
|
+
http_status: result.status,
|
|
56
|
+
message: `Freshdesk sent ticket fields in an unexpected shape (HTTP ${result.status}).`,
|
|
57
|
+
retryable: false,
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
return { ok: true, fields: parsed.data, attempts: result.attempts };
|
|
62
|
+
}
|