@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,280 @@
|
|
|
1
|
+
// `get_ticket` (SPEC §8.2, C3): one ticket with its fields by label, its
|
|
2
|
+
// conversation thread (paged), its requester and, when asked, the requester's
|
|
3
|
+
// recent tickets.
|
|
4
|
+
//
|
|
5
|
+
// - The cursor is read first, before any request, with MAX_POSITION: one
|
|
6
|
+
// from another call, or past the last page, sends nothing.
|
|
7
|
+
// - Every call then takes the schema (no request while it is cached): the
|
|
8
|
+
// first call for the fields, every call to know whether the team hides
|
|
9
|
+
// the requester.
|
|
10
|
+
// - The first call (no cursor) returns `ticket` (`ticketDetail`: every
|
|
11
|
+
// field `ticketOutput` gives, each value through `fieldValue` by its own
|
|
12
|
+
// `TicketField`, keys through `plainName`), `requester`, `recent_tickets`
|
|
13
|
+
// when `include_recent` is true, and the first THREAD_PAGE conversations
|
|
14
|
+
// as the list envelope's `items`. A call with a cursor returns the
|
|
15
|
+
// envelope only: the next THREAD_PAGE conversations.
|
|
16
|
+
// - The thread is read whole on every call through `listConversations` (1
|
|
17
|
+
// request per 100 conversations, at most 10, `truncated: true` past
|
|
18
|
+
// THREAD_CAP) and cut locally, oldest first as Freshdesk gives it. A
|
|
19
|
+
// position is a page of THREAD_PAGE conversations, so the last one is
|
|
20
|
+
// THREAD_CAP / THREAD_PAGE - 1.
|
|
21
|
+
// - A conversation is `{id, created_at, user_id, incoming, private, source,
|
|
22
|
+
// from, to, cc, attachments, body}`. `created_at` through rows.ts's
|
|
23
|
+
// `isoUpdated`, as a ticket's `updated`: a UTC instant or `null`.
|
|
24
|
+
// `source` is Freshdesk's number for the kind of entry, as sent (a whole
|
|
25
|
+
// number, else `null`): not mapped to a name, no mapping being measured.
|
|
26
|
+
// `from`, each of `to` and `cc`, and each attachment's name (the name
|
|
27
|
+
// only) are customer text (SPEC §7.4): each inside its own
|
|
28
|
+
// `<ticket_content>`, cut at SHORT_CAP. The body as text (`body_text`,
|
|
29
|
+
// else the HTML converted), inside `<ticket_content>`, cut at BODY_CAP
|
|
30
|
+
// with the marker; the ticket's description keeps format.ts's TEXT_CAP.
|
|
31
|
+
// - The requester is `{id, name, email, company}`. `getContact` and
|
|
32
|
+
// `getCompany` give the name and the company name raw: each goes through
|
|
33
|
+
// `plainName` (tags stripped, capped), then inside `<ticket_content>`.
|
|
34
|
+
// The email comes back as written only in a plain shape (PLAIN_EMAIL, at
|
|
35
|
+
// most EMAIL_CAP characters); any other is inside `<ticket_content>`.
|
|
36
|
+
// `company` is the contact's company, not the ticket's `company_id`
|
|
37
|
+
// (which the `ticket` fields show): `null`, with no request, when the
|
|
38
|
+
// contact has none.
|
|
39
|
+
// - Hidden (SPEC §8.4), decided by `shows`, which fails closed. A team that
|
|
40
|
+
// hides the requester field gets no `requester` key and no contact
|
|
41
|
+
// request; every conversation's `user_id` is `null` (whatever its
|
|
42
|
+
// `incoming`: the requester's own entry can be marked outgoing) and none
|
|
43
|
+
// has `from`, `to` or `cc`; `include_recent` gives
|
|
44
|
+
// `recent_tickets_unavailable` instead of `recent_tickets`, with no
|
|
45
|
+
// request. A team that hides the company field gets no `company` key and
|
|
46
|
+
// no company request. Not covered: a body, a subject or an attachment
|
|
47
|
+
// name that itself names the requester; and, when the schema lacks the
|
|
48
|
+
// field, a team key that is the field's localised label rather than one
|
|
49
|
+
// of its usual names.
|
|
50
|
+
// - Recent tickets: the requester's last RECENT_COUNT by creation, this
|
|
51
|
+
// ticket left out, as `ticketRow`s, by the request
|
|
52
|
+
// docs/architecture/ticket-queries.md names (`requester_id`,
|
|
53
|
+
// `order_by=created_at`, `order_type=desc`, no window). A row whose
|
|
54
|
+
// `requester_id` is not the ticket's is dropped, whatever Freshdesk sent.
|
|
55
|
+
// - A failed contact, company or recent-tickets read fails the call with
|
|
56
|
+
// its code and a fixed message naming the part, so a contact's 404 is not
|
|
57
|
+
// read as an unknown ticket; nothing of the failed response is quoted.
|
|
58
|
+
// - One deadline for the whole call, CALL_DEADLINE_MS from its start, shared
|
|
59
|
+
// by every request and by the schema's load.
|
|
60
|
+
// - `api_calls_used` is every request this call sent, retries included:
|
|
61
|
+
// for the ticket, the requester, the recent tickets and the thread, and
|
|
62
|
+
// the schema's load when this call started it (the service's `attempts`:
|
|
63
|
+
// 0 from the cache or for a load another call started).
|
|
64
|
+
import { z } from "zod";
|
|
65
|
+
import { getCompany } from "../../../client/resources/companies.js";
|
|
66
|
+
import { getContact } from "../../../client/resources/contacts.js";
|
|
67
|
+
import { getTicket, listConversations, listTickets, } from "../../../client/resources/tickets.js";
|
|
68
|
+
import { bodyText, plainName, untrusted } from "../../format.js";
|
|
69
|
+
import { listEnvelope, readCursor } from "../../list.js";
|
|
70
|
+
import { isoUpdated, ticketDetail, ticketRow } from "../../rows.js";
|
|
71
|
+
/** One call's requests end within this, all of them together. */
|
|
72
|
+
const CALL_DEADLINE_MS = 30_000;
|
|
73
|
+
/** Conversations in one response. */
|
|
74
|
+
export const THREAD_PAGE = 10;
|
|
75
|
+
/** The most conversations `listConversations` reads: 10 pages of 100. */
|
|
76
|
+
export const THREAD_CAP = 1000;
|
|
77
|
+
/** The last cursor position this tool serves: the last page of a thread at its cap. */
|
|
78
|
+
export const MAX_POSITION = THREAD_CAP / THREAD_PAGE - 1;
|
|
79
|
+
/** Code points of one conversation's body. */
|
|
80
|
+
export const BODY_CAP = 4000;
|
|
81
|
+
/** The requester's recent tickets shown. */
|
|
82
|
+
export const RECENT_COUNT = 10;
|
|
83
|
+
/** An email address's longest form (RFC 5321 path limit). */
|
|
84
|
+
const EMAIL_CAP = 254;
|
|
85
|
+
/** Code points of one address or one attachment name. */
|
|
86
|
+
const SHORT_CAP = 300;
|
|
87
|
+
/** The only email shape returned unwrapped: one `@`, ASCII letters, digits and a few signs. */
|
|
88
|
+
const PLAIN_EMAIL = /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+$/;
|
|
89
|
+
/** The team keys that hide the requester or the company when the schema lacks the field. */
|
|
90
|
+
export const REQUESTER = {
|
|
91
|
+
type: "default_requester",
|
|
92
|
+
keys: ["requester", "requester_id"],
|
|
93
|
+
};
|
|
94
|
+
export const COMPANY = { type: "default_company", keys: ["company", "company_id"] };
|
|
95
|
+
const input = z.object({
|
|
96
|
+
ticket_id: z.number().int().positive(),
|
|
97
|
+
include_recent: z.boolean().optional(),
|
|
98
|
+
cursor: z.string().optional(),
|
|
99
|
+
});
|
|
100
|
+
/**
|
|
101
|
+
* Whether the team shows the requester or the company. With the field in the
|
|
102
|
+
* schema, what `ticketOutput` does with it (label, API name and ticket key
|
|
103
|
+
* alike). Without it, fail closed: hidden when a hidden team key is one of
|
|
104
|
+
* the field's usual names, compared as `ticketOutput` compares. Shared with
|
|
105
|
+
* `find_similar_tickets`.
|
|
106
|
+
*/
|
|
107
|
+
export function shows(context, schema, { type, keys }) {
|
|
108
|
+
const field = schema.fields.find((candidate) => candidate.type === type);
|
|
109
|
+
if (field !== undefined) {
|
|
110
|
+
const probe = { requester_id: 1, company_id: 1 };
|
|
111
|
+
return Object.keys(context.ticketOutput({ ...schema, fields: [field] }, probe)).length > 0;
|
|
112
|
+
}
|
|
113
|
+
const names = keys;
|
|
114
|
+
return !Object.entries(context.team.fields).some(([key, entry]) => entry.hidden && names.includes(key.normalize("NFC").trim().toLowerCase()));
|
|
115
|
+
}
|
|
116
|
+
/** The error of a part read after the ticket: its code, a fixed message naming the part. */
|
|
117
|
+
function partFailure(error, part) {
|
|
118
|
+
const { code, http_status, retryable, retry_after_s } = error;
|
|
119
|
+
const status = http_status === null ? "" : ` (HTTP ${http_status})`;
|
|
120
|
+
return {
|
|
121
|
+
ok: false,
|
|
122
|
+
error: {
|
|
123
|
+
code,
|
|
124
|
+
http_status,
|
|
125
|
+
message: `The ticket was read; its requester's ${part} could not be read${status}.`,
|
|
126
|
+
retryable,
|
|
127
|
+
...(retry_after_s === undefined ? {} : { retry_after_s }),
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
function short(value) {
|
|
132
|
+
return typeof value === "string" ? [untrusted(value, { cap: SHORT_CAP })] : [];
|
|
133
|
+
}
|
|
134
|
+
function shortList(value) {
|
|
135
|
+
return Array.isArray(value) ? value.flatMap(short) : [];
|
|
136
|
+
}
|
|
137
|
+
function conversationItem(conversation, requesterShown) {
|
|
138
|
+
const { id, user_id, incoming, source, attachments } = conversation;
|
|
139
|
+
const names = Array.isArray(attachments)
|
|
140
|
+
? attachments.flatMap((file) => typeof file === "object" && file !== null ? short(file.name) : [])
|
|
141
|
+
: [];
|
|
142
|
+
return {
|
|
143
|
+
id,
|
|
144
|
+
created_at: isoUpdated(conversation.created_at),
|
|
145
|
+
// Hidden requester: no author at all. Freshdesk's `incoming` does not
|
|
146
|
+
// say which entries are the requester's.
|
|
147
|
+
user_id: requesterShown ? user_id : null,
|
|
148
|
+
incoming,
|
|
149
|
+
private: conversation.private,
|
|
150
|
+
source: Number.isSafeInteger(source) ? source : null,
|
|
151
|
+
...(requesterShown
|
|
152
|
+
? {
|
|
153
|
+
from: short(conversation.from_email)[0] ?? null,
|
|
154
|
+
to: shortList(conversation.to_emails),
|
|
155
|
+
cc: shortList(conversation.cc_emails),
|
|
156
|
+
}
|
|
157
|
+
: {}),
|
|
158
|
+
attachments: names,
|
|
159
|
+
body: bodyText(conversation.body_text, conversation.body, { cap: BODY_CAP }),
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
/** The requester block and the requests it took. */
|
|
163
|
+
async function requesterOf(context, schema, ticket, deadline) {
|
|
164
|
+
const found = await getContact(context.http, ticket.requester_id, deadline);
|
|
165
|
+
if (!found.ok)
|
|
166
|
+
return partFailure(found.error, "contact");
|
|
167
|
+
const { name, email, company_id } = found.contact;
|
|
168
|
+
const plain = email !== null && email.length <= EMAIL_CAP && PLAIN_EMAIL.test(email);
|
|
169
|
+
const requester = {
|
|
170
|
+
id: ticket.requester_id,
|
|
171
|
+
name: untrusted(plainName(name)),
|
|
172
|
+
email: plain ? email : untrusted(email, { cap: SHORT_CAP }),
|
|
173
|
+
};
|
|
174
|
+
if (!shows(context, schema, COMPANY))
|
|
175
|
+
return { ok: true, requester, calls: found.attempts };
|
|
176
|
+
if (company_id === null) {
|
|
177
|
+
return { ok: true, requester: { ...requester, company: null }, calls: found.attempts };
|
|
178
|
+
}
|
|
179
|
+
const company = await getCompany(context.http, company_id, deadline);
|
|
180
|
+
if (!company.ok)
|
|
181
|
+
return partFailure(company.error, "company");
|
|
182
|
+
return {
|
|
183
|
+
ok: true,
|
|
184
|
+
requester: {
|
|
185
|
+
...requester,
|
|
186
|
+
company: { id: company.company.id, name: untrusted(plainName(company.company.name)) },
|
|
187
|
+
},
|
|
188
|
+
calls: found.attempts + company.attempts,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
export const getTicketTool = {
|
|
192
|
+
name: "get_ticket",
|
|
193
|
+
description: [
|
|
194
|
+
"One ticket: its fields by label (status, priority, agent and group as names), its requester (name, email, company) and its conversation thread, oldest first.",
|
|
195
|
+
"The requester's company is the contact's company, which can differ from the ticket's own Company field.",
|
|
196
|
+
`The thread comes ${THREAD_PAGE} conversations at a time as items; while has_more is true, call again with the same arguments and next_cursor for the next ${THREAD_PAGE}. A call with a cursor returns conversations only.`,
|
|
197
|
+
`A conversation has its author (user_id), direction (incoming), private flag, Freshdesk's source number, its from, to and cc addresses, its attachments' names and its body as text; a body over ${BODY_CAP.toLocaleString("en-US")} characters is cut and says how many are not shown.`,
|
|
198
|
+
`A thread longer than ${THREAD_CAP.toLocaleString("en-US")} conversations is cut and says truncated: true.`,
|
|
199
|
+
`include_recent: true adds recent_tickets, the requester's last ${RECENT_COUNT} tickets by creation as compact rows.`,
|
|
200
|
+
"A field the team configuration hides is left out. When it hides the requester there is no requester, no author (user_id is null) and no address on any conversation, and recent_tickets_unavailable in place of recent_tickets.",
|
|
201
|
+
"api_calls_used counts every request sent for this call, retries included, and the ticket-field schema's load when this call starts it.",
|
|
202
|
+
].join(" "),
|
|
203
|
+
input,
|
|
204
|
+
annotations: {
|
|
205
|
+
readOnlyHint: true,
|
|
206
|
+
destructiveHint: false,
|
|
207
|
+
idempotentHint: true,
|
|
208
|
+
openWorldHint: true,
|
|
209
|
+
},
|
|
210
|
+
cost: "without a cursor, 1 API call for the ticket, 1 for the requester, 1 for the company if there is one, 1 with include_recent, and 1 per 100 conversations (at most 10); with a cursor, the conversation calls only; all within 30 s, plus one per retry after an error, plus at least 3 on any call, with or without a cursor, when the ticket-field schema is not cached",
|
|
211
|
+
customerText: true,
|
|
212
|
+
async handler(args, context) {
|
|
213
|
+
const call = { tool: "get_ticket", args };
|
|
214
|
+
const cursor = readCursor(call, MAX_POSITION);
|
|
215
|
+
if (!cursor.ok)
|
|
216
|
+
return cursor;
|
|
217
|
+
const deadline = Date.now() + CALL_DEADLINE_MS;
|
|
218
|
+
const id = args.ticket_id;
|
|
219
|
+
const loaded = await context.schema.get();
|
|
220
|
+
if (!loaded.ok)
|
|
221
|
+
return loaded;
|
|
222
|
+
const { schema } = loaded;
|
|
223
|
+
const requesterShown = shows(context, schema, REQUESTER);
|
|
224
|
+
let calls = loaded.attempts;
|
|
225
|
+
let head = {};
|
|
226
|
+
if (cursor.position === 0) {
|
|
227
|
+
const got = await getTicket(context.http, id, [], deadline);
|
|
228
|
+
if (!got.ok)
|
|
229
|
+
return got;
|
|
230
|
+
const { ticket } = got;
|
|
231
|
+
calls += got.attempts;
|
|
232
|
+
head = { ticket: ticketDetail(context, schema, ticket) };
|
|
233
|
+
if (requesterShown) {
|
|
234
|
+
const asked = await requesterOf(context, schema, ticket, deadline);
|
|
235
|
+
if (!asked.ok)
|
|
236
|
+
return asked;
|
|
237
|
+
calls += asked.calls;
|
|
238
|
+
head.requester = asked.requester;
|
|
239
|
+
}
|
|
240
|
+
if (args.include_recent === true && !requesterShown) {
|
|
241
|
+
head.recent_tickets_unavailable = "requester hidden";
|
|
242
|
+
}
|
|
243
|
+
else if (args.include_recent === true) {
|
|
244
|
+
const listed = await listTickets(context.http, {
|
|
245
|
+
requester_id: ticket.requester_id,
|
|
246
|
+
order_by: "created_at",
|
|
247
|
+
order_type: "desc",
|
|
248
|
+
// One more: this ticket is likely among them and is left out.
|
|
249
|
+
per_page: RECENT_COUNT + 1,
|
|
250
|
+
}, deadline);
|
|
251
|
+
if (!listed.ok)
|
|
252
|
+
return partFailure(listed.error, "recent tickets");
|
|
253
|
+
calls += listed.attempts;
|
|
254
|
+
head.recent_tickets = listed.tickets
|
|
255
|
+
.filter((other) => other.id !== ticket.id && other.requester_id === ticket.requester_id)
|
|
256
|
+
.slice(0, RECENT_COUNT)
|
|
257
|
+
.map((other) => ticketRow(context, schema, other));
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
const thread = await listConversations(context.http, id, deadline);
|
|
261
|
+
if (!thread.ok)
|
|
262
|
+
return thread;
|
|
263
|
+
const start = cursor.position * THREAD_PAGE;
|
|
264
|
+
const end = start + THREAD_PAGE;
|
|
265
|
+
return {
|
|
266
|
+
ok: true,
|
|
267
|
+
value: {
|
|
268
|
+
...head,
|
|
269
|
+
...listEnvelope(call, {
|
|
270
|
+
items: thread.items
|
|
271
|
+
.slice(start, end)
|
|
272
|
+
.map((conversation) => conversationItem(conversation, requesterShown)),
|
|
273
|
+
...(thread.items.length > end ? { next: cursor.position + 1 } : {}),
|
|
274
|
+
truncated: thread.truncated,
|
|
275
|
+
api_calls_used: calls + thread.attempts,
|
|
276
|
+
}),
|
|
277
|
+
},
|
|
278
|
+
};
|
|
279
|
+
},
|
|
280
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// The `core` toolset (SPEC §8.2): read tools, in the order they are listed.
|
|
2
|
+
import { getCannedResponseTool, listCannedResponsesTool } from "./canned.js";
|
|
3
|
+
import { countTicketsTool } from "./count-tickets.js";
|
|
4
|
+
import { exportTicketsTool } from "./export-tickets.js";
|
|
5
|
+
import { findSimilarTicketsTool } from "./find-similar-tickets.js";
|
|
6
|
+
import { getTicketTool } from "./get-ticket.js";
|
|
7
|
+
import { getTicketSchema } from "./get-ticket-schema.js";
|
|
8
|
+
import { listAgentsTool } from "./list-agents.js";
|
|
9
|
+
import { listGroupsTool } from "./list-groups.js";
|
|
10
|
+
import { listTicketsTool } from "./list-tickets.js";
|
|
11
|
+
import { searchTicketsTool } from "./search-tickets.js";
|
|
12
|
+
import { getSolutionArticleTool, listSolutionTreeTool, searchSolutionsTool } from "./solutions.js";
|
|
13
|
+
import { triageQueueTool } from "./triage-queue.js";
|
|
14
|
+
export const CORE_TOOLS = Object.freeze([
|
|
15
|
+
getTicketSchema,
|
|
16
|
+
listAgentsTool,
|
|
17
|
+
listGroupsTool,
|
|
18
|
+
getTicketTool,
|
|
19
|
+
listTicketsTool,
|
|
20
|
+
searchTicketsTool,
|
|
21
|
+
triageQueueTool,
|
|
22
|
+
findSimilarTicketsTool,
|
|
23
|
+
searchSolutionsTool,
|
|
24
|
+
getSolutionArticleTool,
|
|
25
|
+
listSolutionTreeTool,
|
|
26
|
+
listCannedResponsesTool,
|
|
27
|
+
getCannedResponseTool,
|
|
28
|
+
countTicketsTool,
|
|
29
|
+
exportTicketsTool,
|
|
30
|
+
]);
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// `list_agents` (SPEC §8.2): agents for assignment and notification, in the
|
|
2
|
+
// list envelope (SPEC §8.4).
|
|
3
|
+
//
|
|
4
|
+
// - Each agent is `{id, name, email}`: the work email, on the contact; the
|
|
5
|
+
// phone and every other key are left out (owner, 2026-11-23, OD-8).
|
|
6
|
+
// - Name and email are admin-set: through `plainName` (tags stripped,
|
|
7
|
+
// capped), not wrapped (owner, 2026-11-23). The email's cap is EMAIL_CAP.
|
|
8
|
+
// The resource's schema does not check the email (an odd one must not
|
|
9
|
+
// fail the schema service's load): a string is kept, anything else is
|
|
10
|
+
// `null`.
|
|
11
|
+
// - The whole list in one response, through `listAgents`: at most MAX_PAGES
|
|
12
|
+
// pages, `truncated: true` past them. It never gives a `next_cursor`, so
|
|
13
|
+
// its last position is 0; a cursor is still read first, before any
|
|
14
|
+
// request, so one from another call or past 0 sends nothing.
|
|
15
|
+
// - `api_calls_used` is every request sent for the list, retries included
|
|
16
|
+
// (`listAll`'s `attempts`), as the description and the cost say.
|
|
17
|
+
// - One deadline for the whole call, CALL_DEADLINE_MS from its start, shared
|
|
18
|
+
// by every page (as the schema service's load): a page it cuts, or one
|
|
19
|
+
// not asked for because it has passed, fails the call.
|
|
20
|
+
import { z } from "zod";
|
|
21
|
+
import { listAgents } from "../../../client/resources/agents.js";
|
|
22
|
+
import { NAME_CAP, plainName } from "../../format.js";
|
|
23
|
+
import { listEnvelope, readCursor } from "../../list.js";
|
|
24
|
+
/** One call's pages end within this, all of them together. */
|
|
25
|
+
const CALL_DEADLINE_MS = 30_000;
|
|
26
|
+
/** The last cursor position this tool serves: it returns one page only. */
|
|
27
|
+
export const MAX_POSITION = 0;
|
|
28
|
+
/** An email address's longest form (RFC 5321 path limit). */
|
|
29
|
+
const EMAIL_CAP = 254;
|
|
30
|
+
const input = z.object({ cursor: z.string().optional() });
|
|
31
|
+
export const listAgentsTool = {
|
|
32
|
+
name: "list_agents",
|
|
33
|
+
description: [
|
|
34
|
+
"The help desk's agents, for assigning a ticket or notifying someone: each one's id, name and work email.",
|
|
35
|
+
"The whole list comes in one response; a list longer than 1,000 agents is cut and says truncated: true.",
|
|
36
|
+
"api_calls_used counts every request sent, retries included.",
|
|
37
|
+
`Names are shown without HTML and cut at ${NAME_CAP} characters (ending with …).`,
|
|
38
|
+
].join(" "),
|
|
39
|
+
input,
|
|
40
|
+
annotations: {
|
|
41
|
+
readOnlyHint: true,
|
|
42
|
+
destructiveHint: false,
|
|
43
|
+
idempotentHint: true,
|
|
44
|
+
openWorldHint: true,
|
|
45
|
+
},
|
|
46
|
+
cost: "1 API call per 100 agents, at most 10, within 30 s, plus one per retry of a page after an error",
|
|
47
|
+
customerText: false,
|
|
48
|
+
async handler(args, context) {
|
|
49
|
+
const call = { tool: "list_agents", args };
|
|
50
|
+
const cursor = readCursor(call, MAX_POSITION);
|
|
51
|
+
if (!cursor.ok)
|
|
52
|
+
return cursor;
|
|
53
|
+
const listed = await listAgents(context.http, Date.now() + CALL_DEADLINE_MS);
|
|
54
|
+
if (!listed.ok)
|
|
55
|
+
return listed;
|
|
56
|
+
const items = listed.items.map(({ id, contact }) => ({
|
|
57
|
+
id,
|
|
58
|
+
name: plainName(contact.name),
|
|
59
|
+
email: typeof contact.email === "string" ? plainName(contact.email, EMAIL_CAP) : null,
|
|
60
|
+
}));
|
|
61
|
+
return {
|
|
62
|
+
ok: true,
|
|
63
|
+
value: listEnvelope(call, {
|
|
64
|
+
items,
|
|
65
|
+
truncated: listed.truncated,
|
|
66
|
+
api_calls_used: listed.attempts,
|
|
67
|
+
}),
|
|
68
|
+
};
|
|
69
|
+
},
|
|
70
|
+
};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// `list_groups` (SPEC §8.2): groups for assignment, in the list envelope
|
|
2
|
+
// (SPEC §8.4).
|
|
3
|
+
//
|
|
4
|
+
// - Each group is `{id, name}`; every other key is left out.
|
|
5
|
+
// - The name is admin-set: through `plainName` (tags stripped, capped), not
|
|
6
|
+
// wrapped (owner, 2026-11-23).
|
|
7
|
+
// - The whole list in one response, through `listGroups`: at most MAX_PAGES
|
|
8
|
+
// pages, `truncated: true` past them. It never gives a `next_cursor`, so
|
|
9
|
+
// its last position is 0; a cursor is still read first, before any
|
|
10
|
+
// request, so one from another call or past 0 sends nothing.
|
|
11
|
+
// - `api_calls_used` is every request sent for the list, retries included
|
|
12
|
+
// (`listAll`'s `attempts`), as the description and the cost say.
|
|
13
|
+
// - One deadline for the whole call, CALL_DEADLINE_MS from its start, shared
|
|
14
|
+
// by every page (as the schema service's load): a page it cuts, or one
|
|
15
|
+
// not asked for because it has passed, fails the call.
|
|
16
|
+
import { z } from "zod";
|
|
17
|
+
import { listGroups } from "../../../client/resources/groups.js";
|
|
18
|
+
import { NAME_CAP, plainName } from "../../format.js";
|
|
19
|
+
import { listEnvelope, readCursor } from "../../list.js";
|
|
20
|
+
/** One call's pages end within this, all of them together. */
|
|
21
|
+
const CALL_DEADLINE_MS = 30_000;
|
|
22
|
+
/** The last cursor position this tool serves: it returns one page only. */
|
|
23
|
+
export const MAX_POSITION = 0;
|
|
24
|
+
const input = z.object({ cursor: z.string().optional() });
|
|
25
|
+
export const listGroupsTool = {
|
|
26
|
+
name: "list_groups",
|
|
27
|
+
description: [
|
|
28
|
+
"The help desk's groups, for assigning a ticket: each one's id and name.",
|
|
29
|
+
"The whole list comes in one response; a list longer than 1,000 groups is cut and says truncated: true.",
|
|
30
|
+
"api_calls_used counts every request sent, retries included.",
|
|
31
|
+
`Names are shown without HTML and cut at ${NAME_CAP} characters (ending with …).`,
|
|
32
|
+
].join(" "),
|
|
33
|
+
input,
|
|
34
|
+
annotations: {
|
|
35
|
+
readOnlyHint: true,
|
|
36
|
+
destructiveHint: false,
|
|
37
|
+
idempotentHint: true,
|
|
38
|
+
openWorldHint: true,
|
|
39
|
+
},
|
|
40
|
+
cost: "1 API call per 100 groups, at most 10, within 30 s, plus one per retry of a page after an error",
|
|
41
|
+
customerText: false,
|
|
42
|
+
async handler(args, context) {
|
|
43
|
+
const call = { tool: "list_groups", args };
|
|
44
|
+
const cursor = readCursor(call, MAX_POSITION);
|
|
45
|
+
if (!cursor.ok)
|
|
46
|
+
return cursor;
|
|
47
|
+
const listed = await listGroups(context.http, Date.now() + CALL_DEADLINE_MS);
|
|
48
|
+
if (!listed.ok)
|
|
49
|
+
return listed;
|
|
50
|
+
const items = listed.items.map((group) => ({ id: group.id, name: plainName(group.name) }));
|
|
51
|
+
return {
|
|
52
|
+
ok: true,
|
|
53
|
+
value: listEnvelope(call, {
|
|
54
|
+
items,
|
|
55
|
+
truncated: listed.truncated,
|
|
56
|
+
api_calls_used: listed.attempts,
|
|
57
|
+
}),
|
|
58
|
+
};
|
|
59
|
+
},
|
|
60
|
+
};
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
// `list_tickets` (SPEC §8.2, §8.4, C1): the filtered, paged ticket list, as
|
|
2
|
+
// compact rows (rows.ts's `ticketRow`) or, with `detail: true`, each ticket in
|
|
3
|
+
// full (`ticketDetail`), in the list envelope.
|
|
4
|
+
//
|
|
5
|
+
// - The nine filters become one request through `buildTicketQuery`
|
|
6
|
+
// (ticket-query.ts; docs/architecture/ticket-queries.md): status,
|
|
7
|
+
// priority, group, agent, type, tag and `created_between` are one search
|
|
8
|
+
// query; with none of them the list endpoint serves the call.
|
|
9
|
+
// `matchesLocal` is applied to every ticket either endpoint returns: the
|
|
10
|
+
// dates of a search and `uncategorized` are checked here.
|
|
11
|
+
// - The cursor is read first, before any request. A position is a page of
|
|
12
|
+
// the source: of search (30 results) with a search filter, of the list
|
|
13
|
+
// endpoint without. Which source a call uses is known from its arguments
|
|
14
|
+
// alone (`SEARCH_FILTERS`, the filters `buildTicketQuery` turns into
|
|
15
|
+
// search conditions), so the bound is search.ts's MAX_SEARCH_POSITION or
|
|
16
|
+
// MAX_LIST_POSITION and a cursor past it sends nothing. Should the query
|
|
17
|
+
// built ever name the other source, the handler throws (the registry
|
|
18
|
+
// answers with its fixed internal error) and no ticket request is sent.
|
|
19
|
+
// - Search: one page a call. More exists while the page is not the last of
|
|
20
|
+
// the 10 and `total` reaches past it. `total` over the 300 results the 10
|
|
21
|
+
// pages hold is `truncated: true`, on every page of that search.
|
|
22
|
+
// - The list endpoint: one page of LIST_ROWS a call, more while Freshdesk
|
|
23
|
+
// links a next page. With `uncategorized` (alone or with `updated_since`)
|
|
24
|
+
// the call is a scan: pages of SCAN_PER_PAGE (DETAIL_SCAN_PER_PAGE with
|
|
25
|
+
// `detail: true`), each sent as `expensive`, read until the list ends,
|
|
26
|
+
// the rows kept reach LIST_ROWS, SCAN_PAGES pages are read, or
|
|
27
|
+
// SCAN_START_MS have passed since the call started (a slow page took
|
|
28
|
+
// about 8 s once: no page is started that the deadline would likely
|
|
29
|
+
// cut). The next call resumes at the next list page. The page at
|
|
30
|
+
// MAX_LIST_PAGE is the last Freshdesk returns: when it links a next one,
|
|
31
|
+
// or comes back full (whether it carries a `Link` is documented, not
|
|
32
|
+
// measured), the list is `truncated: true`.
|
|
33
|
+
// - A short page is never passed off as the end: `has_more` and
|
|
34
|
+
// `next_cursor` come from the source (search's `total`, the list's
|
|
35
|
+
// `Link`), never from the number of rows left after the local checks. A
|
|
36
|
+
// call can so return fewer rows than a page, or none, with `has_more:
|
|
37
|
+
// true`; a scan can return up to SCAN_MAX_ROWS rows (DETAIL_SCAN_MAX_ROWS
|
|
38
|
+
// tickets with `detail: true`): one short of LIST_ROWS, then a whole page.
|
|
39
|
+
// `truncated` is the source's too: with `updated_since` the search counts
|
|
40
|
+
// its widened days, so it can be cut at 300 when fewer tickets match.
|
|
41
|
+
// - `detail: true` is bounded: each wrapped value, the description
|
|
42
|
+
// included, is cut at DETAIL_VALUE_CAP code points with the marker
|
|
43
|
+
// (`get_ticket` returns more of the text), and a scan reads the smaller page.
|
|
44
|
+
// - A failed request fails the call: no partial list. One exception: a
|
|
45
|
+
// scan that has read at least one page and whose next page the limiter
|
|
46
|
+
// refuses (`rate_limited` with no HTTP status) returns the rows kept so
|
|
47
|
+
// far, with `next_cursor` at the refused page, so a retry resumes there
|
|
48
|
+
// instead of reading the pages again. That page's own requests, if a
|
|
49
|
+
// retry of it was what the limiter refused (after a 5xx, or after a 429
|
|
50
|
+
// whose hold outlasts the limiter's wait), are counted. A limiter wait
|
|
51
|
+
// that would reach the call's deadline is refused the same way
|
|
52
|
+
// (`rate_limited`, no status), so near the 30 s mark a later page takes
|
|
53
|
+
// this branch too. Any other failure of a later page fails the call: a
|
|
54
|
+
// 403, or an attempt the deadline cuts in flight (`network`).
|
|
55
|
+
// - One deadline for the whole call, CALL_DEADLINE_MS from its start, shared
|
|
56
|
+
// by every request.
|
|
57
|
+
// - `api_calls_used` is every request this call sent, retries included, and
|
|
58
|
+
// the schema's load when this call started it (the service's `attempts`).
|
|
59
|
+
import { z } from "zod";
|
|
60
|
+
import { MAX_SEARCH_PAGE, MAX_SEARCH_POSITION, SEARCH_PER_PAGE, searchTickets, } from "../../../client/resources/search.js";
|
|
61
|
+
import { listTickets, MAX_LIST_PAGE } from "../../../client/resources/tickets.js";
|
|
62
|
+
import { listEnvelope, readCursor } from "../../list.js";
|
|
63
|
+
import { ticketDetail, ticketRow } from "../../rows.js";
|
|
64
|
+
import { buildTicketQuery, matchesLocal } from "../../ticket-query.js";
|
|
65
|
+
/** One call's requests end within this, all of them together. */
|
|
66
|
+
const CALL_DEADLINE_MS = 30_000;
|
|
67
|
+
/** Rows of one list-endpoint page, and where a scan stops reading: search's page size. */
|
|
68
|
+
export const LIST_ROWS = SEARCH_PER_PAGE;
|
|
69
|
+
/** Tickets of one list page of a scan: the endpoint's largest page. */
|
|
70
|
+
export const SCAN_PER_PAGE = 100;
|
|
71
|
+
/** The same with `detail: true`, where each ticket is far larger. */
|
|
72
|
+
export const DETAIL_SCAN_PER_PAGE = LIST_ROWS;
|
|
73
|
+
/** The most rows a scan returns: one short of LIST_ROWS, then a whole page. */
|
|
74
|
+
export const SCAN_MAX_ROWS = LIST_ROWS - 1 + SCAN_PER_PAGE;
|
|
75
|
+
export const DETAIL_SCAN_MAX_ROWS = LIST_ROWS - 1 + DETAIL_SCAN_PER_PAGE;
|
|
76
|
+
/** Code points of one text value of a ticket with `detail: true`. */
|
|
77
|
+
export const DETAIL_VALUE_CAP = 1000;
|
|
78
|
+
/** The most list pages one call scans. */
|
|
79
|
+
export const SCAN_PAGES = 10;
|
|
80
|
+
/** A scan starts no further page once this much of the call's deadline is spent. */
|
|
81
|
+
export const SCAN_START_MS = CALL_DEADLINE_MS / 2;
|
|
82
|
+
/** The last cursor position of a call the list endpoint serves: its last page. */
|
|
83
|
+
export const MAX_LIST_POSITION = MAX_LIST_PAGE - 1;
|
|
84
|
+
/** The filters `buildTicketQuery` makes search conditions: any of them given, search serves the call. */
|
|
85
|
+
const SEARCH_FILTERS = [
|
|
86
|
+
"status",
|
|
87
|
+
"priority",
|
|
88
|
+
"group",
|
|
89
|
+
"agent",
|
|
90
|
+
"type",
|
|
91
|
+
"tag",
|
|
92
|
+
"created_between",
|
|
93
|
+
];
|
|
94
|
+
const nameOrId = z.union([z.string(), z.number()]);
|
|
95
|
+
const input = z.object({
|
|
96
|
+
status: nameOrId.optional(),
|
|
97
|
+
priority: nameOrId.optional(),
|
|
98
|
+
group: nameOrId.optional(),
|
|
99
|
+
agent: nameOrId.optional(),
|
|
100
|
+
type: z.string().optional(),
|
|
101
|
+
tag: z.string().optional(),
|
|
102
|
+
uncategorized: z.boolean().optional(),
|
|
103
|
+
updated_since: z.string().optional(),
|
|
104
|
+
created_between: z.object({ from: z.string(), to: z.string() }).optional(),
|
|
105
|
+
detail: z.boolean().optional(),
|
|
106
|
+
cursor: z.string().optional(),
|
|
107
|
+
});
|
|
108
|
+
export const listTicketsTool = {
|
|
109
|
+
name: "list_tickets",
|
|
110
|
+
description: [
|
|
111
|
+
`Tickets matching every filter given, as compact rows: id, subject, status, priority, category, last update, agent and group, by name. detail: true returns every field of each ticket by label instead (much larger), each text cut at ${DETAIL_VALUE_CAP.toLocaleString("en-US")} characters, saying how many are not shown: get_ticket returns more of the text.`,
|
|
112
|
+
"Filters: status, priority, group and agent by name or id; type by name; tag (one tag, exact); uncategorized: true (a category field is empty); updated_since (an ISO 8601 date-time with a zone, such as 2026-01-02T03:04:05Z); created_between {from, to} (days as YYYY-MM-DD, UTC, both included). A filter that is not valid is refused with the valid names, and nothing is sent. For a custom field use search_tickets.",
|
|
113
|
+
`With status, priority, group, agent, type, tag or created_between the list comes ${SEARCH_PER_PAGE} tickets a call from Freshdesk's search, which returns the first ${MAX_SEARCH_PAGE * SEARCH_PER_PAGE} matches only: past them the list is cut and says truncated: true (narrow it with one more filter or a shorter created_between), and a ticket changed in the last few minutes can be missing.`,
|
|
114
|
+
`Without any of those the list is the tickets created in the last 30 days, newest first, ${LIST_ROWS} a call; updated_since replaces that window with the tickets updated since then. That list reaches ${MAX_LIST_PAGE} calls (${(MAX_LIST_PAGE * LIST_ROWS).toLocaleString("en-US")} tickets): past them it is cut and says truncated: true (narrow it with a later updated_since or a filter).`,
|
|
115
|
+
`uncategorized and the dates are checked on each ticket after Freshdesk answers, so a call can return fewer rows than a page, or none, and still not be the end: has_more says whether there is more, never the number of rows. While has_more is true, call again with the same arguments and next_cursor.`,
|
|
116
|
+
`uncategorized without status, priority, group, agent, type, tag or created_between reads the list ${SCAN_PER_PAGE} tickets a request (${DETAIL_SCAN_PER_PAGE} with detail: true), up to ${SCAN_PAGES} requests a call, until it has ${LIST_ROWS} rows: a call returns at most ${SCAN_MAX_ROWS} rows, or ${DETAIL_SCAN_MAX_ROWS} tickets with detail: true. It counts as an expensive operation and is refused when the API budget is low; refused part-way, it returns what it has with next_cursor.`,
|
|
117
|
+
"api_calls_used counts every request sent for this call, retries included, and the ticket-field schema's load when this call starts it.",
|
|
118
|
+
].join(" "),
|
|
119
|
+
input,
|
|
120
|
+
annotations: {
|
|
121
|
+
readOnlyHint: true,
|
|
122
|
+
destructiveHint: false,
|
|
123
|
+
idempotentHint: true,
|
|
124
|
+
openWorldHint: true,
|
|
125
|
+
},
|
|
126
|
+
cost: `1 API call; with uncategorized and none of status, priority, group, agent, type, tag or created_between, up to ${SCAN_PAGES} (1 per ${SCAN_PER_PAGE} tickets read, per ${DETAIL_SCAN_PER_PAGE} with detail: true); all within 30 s, plus one per retry after an error, plus at least 3 when the ticket-field schema is not cached`,
|
|
127
|
+
customerText: true,
|
|
128
|
+
async handler(args, context) {
|
|
129
|
+
const call = { tool: "list_tickets", args };
|
|
130
|
+
const searched = SEARCH_FILTERS.some((name) => args[name] !== undefined);
|
|
131
|
+
const cursor = readCursor(call, searched ? MAX_SEARCH_POSITION : MAX_LIST_POSITION);
|
|
132
|
+
if (!cursor.ok)
|
|
133
|
+
return cursor;
|
|
134
|
+
const started = Date.now();
|
|
135
|
+
const deadline = started + CALL_DEADLINE_MS;
|
|
136
|
+
const loaded = await context.schema.get();
|
|
137
|
+
if (!loaded.ok)
|
|
138
|
+
return loaded;
|
|
139
|
+
const { schema } = loaded;
|
|
140
|
+
const query = buildTicketQuery(context, schema, args);
|
|
141
|
+
if (!query.ok)
|
|
142
|
+
return query;
|
|
143
|
+
if (searched !== (query.endpoint === "search")) {
|
|
144
|
+
// The cursor's bound was chosen for the other source: fail closed.
|
|
145
|
+
throw new Error("list_tickets: SEARCH_FILTERS and buildTicketQuery disagree");
|
|
146
|
+
}
|
|
147
|
+
const detail = args.detail === true;
|
|
148
|
+
let calls = loaded.attempts;
|
|
149
|
+
const kept = [];
|
|
150
|
+
let next;
|
|
151
|
+
let truncated = false;
|
|
152
|
+
let page = cursor.position + 1;
|
|
153
|
+
if (query.endpoint === "search") {
|
|
154
|
+
const found = await searchTickets(context.http, query.query, page, deadline);
|
|
155
|
+
if (!found.ok)
|
|
156
|
+
return found;
|
|
157
|
+
calls += found.attempts;
|
|
158
|
+
kept.push(...found.results.filter((ticket) => matchesLocal(query, ticket)));
|
|
159
|
+
truncated = found.total > MAX_SEARCH_PAGE * SEARCH_PER_PAGE;
|
|
160
|
+
if (page < MAX_SEARCH_PAGE && page * SEARCH_PER_PAGE < found.total)
|
|
161
|
+
next = page;
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
const scan = query.uncategorized !== undefined;
|
|
165
|
+
const first = page;
|
|
166
|
+
const last = scan ? page + SCAN_PAGES - 1 : page;
|
|
167
|
+
const perPage = !scan ? LIST_ROWS : detail ? DETAIL_SCAN_PER_PAGE : SCAN_PER_PAGE;
|
|
168
|
+
for (;;) {
|
|
169
|
+
const listed = await listTickets(context.http, { per_page: perPage, page, ...query.list }, deadline, scan ? "expensive" : "cheap");
|
|
170
|
+
if (!listed.ok) {
|
|
171
|
+
const { code, http_status } = listed.error;
|
|
172
|
+
if (page === first || code !== "rate_limited" || http_status !== null)
|
|
173
|
+
return listed;
|
|
174
|
+
// The limiter refused a later page of a scan: keep what was read.
|
|
175
|
+
calls += listed.attempts ?? 0;
|
|
176
|
+
next = page - 1;
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
calls += listed.attempts;
|
|
180
|
+
kept.push(...listed.tickets.filter((ticket) => matchesLocal(query, ticket)));
|
|
181
|
+
if (page === MAX_LIST_PAGE) {
|
|
182
|
+
truncated = listed.has_more || listed.tickets.length === perPage;
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
if (!listed.has_more)
|
|
186
|
+
break;
|
|
187
|
+
if (page === last || kept.length >= LIST_ROWS || Date.now() - started >= SCAN_START_MS) {
|
|
188
|
+
// The position of list page `page + 1`.
|
|
189
|
+
next = page;
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
page += 1;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return {
|
|
196
|
+
ok: true,
|
|
197
|
+
value: listEnvelope(call, {
|
|
198
|
+
items: kept.map((ticket) => detail
|
|
199
|
+
? ticketDetail(context, schema, ticket, {
|
|
200
|
+
cap: DETAIL_VALUE_CAP,
|
|
201
|
+
rest: "get_ticket returns more of the text",
|
|
202
|
+
})
|
|
203
|
+
: ticketRow(context, schema, ticket)),
|
|
204
|
+
...(next === undefined ? {} : { next }),
|
|
205
|
+
truncated,
|
|
206
|
+
api_calls_used: calls,
|
|
207
|
+
}),
|
|
208
|
+
};
|
|
209
|
+
},
|
|
210
|
+
};
|