@workover/cli 0.27.0 → 0.29.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/bin/workover.mjs +2244 -4
- package/contacts.mjs +1561 -0
- package/domains.mjs +417 -0
- package/email-studio.mjs +829 -0
- package/feedback-board.mjs +321 -0
- package/journeys.mjs +409 -0
- package/package.json +20 -1
- package/support-agent-tests.mjs +424 -0
- package/support-config.mjs +302 -0
- package/support-email.mjs +458 -0
- package/support-fields.mjs +294 -0
- package/support-freshdesk.mjs +176 -0
- package/support-inbox.mjs +313 -0
- package/support-label-rules.mjs +136 -0
- package/support-labels.mjs +244 -0
- package/support-outcomes.mjs +276 -0
- package/support-outside-bot.mjs +145 -0
- package/support-setup.mjs +660 -0
- package/support-signatures.mjs +156 -0
- package/support-sources.mjs +233 -0
- package/support-walter.mjs +310 -0
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
// Support's custom conversation fields from a terminal: `workover support fields ...` and the MCP
|
|
2
|
+
// server's `workover_support_*field*` tools (WO-1752, S38, Support settings > Conversation fields).
|
|
3
|
+
//
|
|
4
|
+
// THE SAME FILE ships in @workover/cli (apps/cli/support-fields.mjs) and @workover/mcp
|
|
5
|
+
// (apps/mcp/support-fields.mjs), because the two packages are published separately and neither
|
|
6
|
+
// may depend on the other. `apps/mcp/support-fields.test.mjs` fails when the copies differ.
|
|
7
|
+
//
|
|
8
|
+
// Fields are the team's own facts about a conversation (a plan, a renewal date, an account id):
|
|
9
|
+
// fifteen per Messenger, each text, number, date or choice. Every call goes to a route opened to
|
|
10
|
+
// tokens holding the `support` scope (`workover login --support`). Defining a field is the
|
|
11
|
+
// workspace owner's or an admin's; reading them and setting a conversation's values is any
|
|
12
|
+
// teammate's. Each write is safe to repeat: setting the same field again answers `changed: false`,
|
|
13
|
+
// removing one already gone answers `removed: false`, and the same values again change nothing.
|
|
14
|
+
// The inbox filters by a field with `field=<key>:<value>`.
|
|
15
|
+
//
|
|
16
|
+
// `call(path, { method, body })` does the requests and resolves the parsed JSON, or throws an Error
|
|
17
|
+
// whose `.status` is the HTTP status and whose message is the API's own sentence. The renderers
|
|
18
|
+
// return lines, for a terminal or an MCP text result alike.
|
|
19
|
+
|
|
20
|
+
import { apiError, resolveWorkspace } from './site-setup.mjs';
|
|
21
|
+
|
|
22
|
+
export const FIELD_TYPES = ['text', 'number', 'date', 'choice'];
|
|
23
|
+
|
|
24
|
+
/** The key the api makes from a label: "Renewal date" is `renewal_date`. */
|
|
25
|
+
export function fieldKeyOf(label) {
|
|
26
|
+
return String(label ?? '')
|
|
27
|
+
.trim()
|
|
28
|
+
.toLowerCase()
|
|
29
|
+
.replace(/[^a-z0-9]+/g, '_')
|
|
30
|
+
.replace(/^_+|_+$/g, '')
|
|
31
|
+
.slice(0, 30)
|
|
32
|
+
.replace(/_+$/g, '');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The Messenger whose fields these are: Support has to exist first. */
|
|
36
|
+
async function messengerOf(call, workspace) {
|
|
37
|
+
const ws = await resolveWorkspace(call, workspace);
|
|
38
|
+
const status = await call(`/messenger/agent-setup?organizationId=${encodeURIComponent(ws.id)}`);
|
|
39
|
+
const id = status?.messenger?.id;
|
|
40
|
+
if (!id) throw apiError(404, 'Support is not set up in this workspace yet. Run `workover support setup` first.');
|
|
41
|
+
return { ws, messengerId: id };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** A field named by its key or its label, whatever the case. */
|
|
45
|
+
function findField(fields, ref) {
|
|
46
|
+
const raw = String(ref ?? '').trim();
|
|
47
|
+
if (!raw) throw apiError(400, 'Name the field, by its key or its label. `workover support fields` lists them.');
|
|
48
|
+
const hit =
|
|
49
|
+
fields.find((f) => f.key === raw) ?? fields.find((f) => f.label.toLowerCase() === raw.toLowerCase());
|
|
50
|
+
if (!hit) throw apiError(404, `There is no field "${raw}". \`workover support fields\` lists them.`);
|
|
51
|
+
return hit;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const listOf = (v) =>
|
|
55
|
+
(Array.isArray(v) ? v : String(v ?? '').split(','))
|
|
56
|
+
.map((s) => String(s).trim())
|
|
57
|
+
.filter(Boolean);
|
|
58
|
+
|
|
59
|
+
/** Every field, in order, with its type, options and how many conversations have a value. */
|
|
60
|
+
export async function listFields(call, { workspace } = {}) {
|
|
61
|
+
const { ws, messengerId } = await messengerOf(call, workspace);
|
|
62
|
+
const res = await call(`/messenger/${messengerId}/conversation-fields`);
|
|
63
|
+
return { workspace: ws, ...res };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Create or change a field. Its key is `key` when given, else made from the label, so running the
|
|
68
|
+
* same setup twice leaves one field. A choice takes its options as a list or "a,b,c".
|
|
69
|
+
*/
|
|
70
|
+
export async function setField(call, { workspace, key, label, type, options, position }) {
|
|
71
|
+
const name = String(label ?? '').trim();
|
|
72
|
+
if (!name) throw apiError(400, 'Give the field a label.');
|
|
73
|
+
const t = String(type ?? 'text').toLowerCase();
|
|
74
|
+
if (!FIELD_TYPES.includes(t)) throw apiError(400, `type is one of ${FIELD_TYPES.join(', ')}.`);
|
|
75
|
+
const k = key === undefined || key === null || key === '' ? fieldKeyOf(name) : String(key).trim();
|
|
76
|
+
if (!k) throw apiError(400, 'Give the field a label with a letter or a digit in it, or a --key.');
|
|
77
|
+
const body = { label: name, type: t };
|
|
78
|
+
if (t === 'choice') {
|
|
79
|
+
const opts = listOf(options);
|
|
80
|
+
if (!opts.length) throw apiError(400, 'A choice field needs its options: --options "Free,Pro,Business".');
|
|
81
|
+
body.options = opts;
|
|
82
|
+
}
|
|
83
|
+
if (position !== undefined && position !== null && position !== '') body.position = Number(position);
|
|
84
|
+
const { ws, messengerId } = await messengerOf(call, workspace);
|
|
85
|
+
const res = await call(`/messenger/${messengerId}/conversation-fields/${encodeURIComponent(k)}`, {
|
|
86
|
+
method: 'PUT',
|
|
87
|
+
body,
|
|
88
|
+
});
|
|
89
|
+
return { workspace: ws, ...res };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Remove a field, by key or label. Its values stay on conversations, hidden. */
|
|
93
|
+
export async function removeField(call, { workspace, field }) {
|
|
94
|
+
const { ws, messengerId } = await messengerOf(call, workspace);
|
|
95
|
+
const { fields = [] } = await call(`/messenger/${messengerId}/conversation-fields`);
|
|
96
|
+
const raw = String(field ?? '').trim();
|
|
97
|
+
const hit = fields.find((f) => f.key === raw || f.label.toLowerCase() === raw.toLowerCase());
|
|
98
|
+
if (!hit) {
|
|
99
|
+
if (!raw) throw apiError(400, 'Name the field, by its key or its label.');
|
|
100
|
+
return { workspace: ws, removed: false, key: raw };
|
|
101
|
+
}
|
|
102
|
+
const res = await call(`/messenger/${messengerId}/conversation-fields/${encodeURIComponent(hit.key)}`, {
|
|
103
|
+
method: 'DELETE',
|
|
104
|
+
});
|
|
105
|
+
return { workspace: ws, key: hit.key, ...res };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Put the fields in this order (keys or labels, every field once). */
|
|
109
|
+
export async function orderFields(call, { workspace, fields: order }) {
|
|
110
|
+
const { ws, messengerId } = await messengerOf(call, workspace);
|
|
111
|
+
const { fields = [] } = await call(`/messenger/${messengerId}/conversation-fields`);
|
|
112
|
+
const keys = listOf(order).map((ref) => findField(fields, ref).key);
|
|
113
|
+
const res = await call(`/messenger/${messengerId}/conversation-field-order`, { method: 'PUT', body: { keys } });
|
|
114
|
+
return { workspace: ws, ...res };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** One conversation's fields with their values. The conversation is its id or public id. */
|
|
118
|
+
export async function conversationFields(call, { conversation }) {
|
|
119
|
+
const ref = String(conversation ?? '').trim();
|
|
120
|
+
if (!ref) throw apiError(400, 'Name the conversation, by its id or its public id.');
|
|
121
|
+
return call(`/messenger/conversations/${encodeURIComponent(ref)}/fields`);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Set some of a conversation's fields. `values` is an object of key (or label) to value, or a list
|
|
126
|
+
* of "key=value" strings; an empty value clears one.
|
|
127
|
+
*/
|
|
128
|
+
export async function setConversationFields(call, { conversation, values }) {
|
|
129
|
+
const current = await conversationFields(call, { conversation });
|
|
130
|
+
const pairs = Array.isArray(values)
|
|
131
|
+
? values.map((s) => {
|
|
132
|
+
const str = String(s);
|
|
133
|
+
const at = str.indexOf('=');
|
|
134
|
+
if (at <= 0) throw apiError(400, `Give each value as key=value: "${str}".`);
|
|
135
|
+
return [str.slice(0, at).trim(), str.slice(at + 1).trim()];
|
|
136
|
+
})
|
|
137
|
+
: Object.entries(values ?? {});
|
|
138
|
+
if (!pairs.length) throw apiError(400, 'Give at least one value: plan=Pro.');
|
|
139
|
+
const body = {};
|
|
140
|
+
for (const [ref, v] of pairs) body[findField(current.fields ?? [], ref).key] = v === '' ? null : v;
|
|
141
|
+
return call(`/messenger/conversations/${encodeURIComponent(String(conversation).trim())}/fields`, {
|
|
142
|
+
method: 'PATCH',
|
|
143
|
+
body: { values: body },
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const plural = (n, one, many) => `${n} ${n === 1 ? one : many}`;
|
|
148
|
+
|
|
149
|
+
export function renderFieldLine(f) {
|
|
150
|
+
const opts = f.type === 'choice' && f.options?.length ? `: ${f.options.join(', ')}` : '';
|
|
151
|
+
return ` ${f.key} ${f.label} (${f.type}${opts}; ${plural(f.used ?? 0, 'conversation', 'conversations')})`;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function renderFields(res) {
|
|
155
|
+
const out = [''];
|
|
156
|
+
const fields = res.fields ?? [];
|
|
157
|
+
if (!fields.length) out.push(' No conversation fields yet.');
|
|
158
|
+
else out.push(` Conversation fields (${fields.length} of ${res.max ?? 15}):`, ...fields.map(renderFieldLine));
|
|
159
|
+
out.push('', ' Filter the inbox by one with field=<key>:<value>.');
|
|
160
|
+
if (res.canManage === false) out.push('', ' Only workspace owners and admins can change conversation fields.');
|
|
161
|
+
out.push('');
|
|
162
|
+
return out;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function renderFieldSaved(res) {
|
|
166
|
+
const verb = res.created ? 'Added' : res.changed ? 'Saved' : 'Already so';
|
|
167
|
+
return ['', ` ✓ ${verb}:`, renderFieldLine(res.field), ''];
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export function renderFieldRemoved(res) {
|
|
171
|
+
return [
|
|
172
|
+
'',
|
|
173
|
+
res.removed
|
|
174
|
+
? ` ✓ Removed ${res.key}. Its values stay on conversations and come back if it is added again.`
|
|
175
|
+
: ` Nothing to remove: there is no field "${res.key}".`,
|
|
176
|
+
'',
|
|
177
|
+
];
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function renderConversationFields(res) {
|
|
181
|
+
const out = ['', ` Conversation ${res.conversationId}:`];
|
|
182
|
+
const fields = res.fields ?? [];
|
|
183
|
+
if (!fields.length) out.push(' No conversation fields are set up.');
|
|
184
|
+
for (const f of fields) out.push(` ${f.label}: ${f.value === null || f.value === undefined ? '(not set)' : f.value}`);
|
|
185
|
+
if (res.changed) out.push('', res.changed.length ? ` ✓ Changed: ${res.changed.join(', ')}` : ' Nothing changed.');
|
|
186
|
+
out.push('');
|
|
187
|
+
return out;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const WORKSPACE = {
|
|
191
|
+
type: 'string',
|
|
192
|
+
description: 'The workspace, by slug or id. Needed only when the account has more than one.',
|
|
193
|
+
};
|
|
194
|
+
const FIELD_REF = { type: 'string', description: 'The field, by its key ("plan") or its label ("Plan").' };
|
|
195
|
+
const CONVERSATION = { type: 'string', description: 'The conversation, by its id or its public id.' };
|
|
196
|
+
const SCOPE =
|
|
197
|
+
' Needs a token from `workover login --support`; without it this returns a permission error naming that command.';
|
|
198
|
+
|
|
199
|
+
/** The MCP server's field tools, listed with the other Support tools (index.mjs). */
|
|
200
|
+
export const FIELD_TOOL_DEFS = [
|
|
201
|
+
{
|
|
202
|
+
name: 'workover_support_fields',
|
|
203
|
+
description:
|
|
204
|
+
"Support's custom conversation fields: each one's key, label, type (text, number, date or choice), a choice's options, and how many conversations have a value. The inbox filters by one with field=<key>:<value>." +
|
|
205
|
+
SCOPE,
|
|
206
|
+
inputSchema: { type: 'object', properties: { workspace: WORKSPACE } },
|
|
207
|
+
},
|
|
208
|
+
{
|
|
209
|
+
name: 'workover_support_set_field',
|
|
210
|
+
description:
|
|
211
|
+
'Create or change a conversation field (15 at most). Its key is `key` when given, else made from the label ("Renewal date" is renewal_date) and never changes. A choice needs options. Its type cannot change while conversations hold values. Owners and admins. Safe to repeat.' +
|
|
212
|
+
SCOPE,
|
|
213
|
+
inputSchema: {
|
|
214
|
+
type: 'object',
|
|
215
|
+
properties: {
|
|
216
|
+
workspace: WORKSPACE,
|
|
217
|
+
label: { type: 'string', description: 'What the team sees, up to 40 characters.' },
|
|
218
|
+
type: { type: 'string', enum: FIELD_TYPES },
|
|
219
|
+
options: { type: 'array', items: { type: 'string' }, description: 'A choice field’s options, 1 to 20, no commas.' },
|
|
220
|
+
key: { type: 'string', description: 'Lower-case letters, digits and underscores. Optional.' },
|
|
221
|
+
position: { type: 'number', description: 'Its place in the order, lowest first. Optional.' },
|
|
222
|
+
},
|
|
223
|
+
required: ['label', 'type'],
|
|
224
|
+
},
|
|
225
|
+
},
|
|
226
|
+
{
|
|
227
|
+
name: 'workover_support_remove_field',
|
|
228
|
+
description:
|
|
229
|
+
'Remove a conversation field. Its values stay on conversations, hidden, and come back if it is added again with the same key. Ask the person first. Owners and admins. Safe to repeat.' +
|
|
230
|
+
SCOPE,
|
|
231
|
+
inputSchema: { type: 'object', properties: { workspace: WORKSPACE, field: FIELD_REF }, required: ['field'] },
|
|
232
|
+
},
|
|
233
|
+
{
|
|
234
|
+
name: 'workover_support_order_fields',
|
|
235
|
+
description:
|
|
236
|
+
'Put the conversation fields in this order (every field once, by key or label), as the details panel, the settings page and the CSV export show them. Owners and admins. Safe to repeat.' +
|
|
237
|
+
SCOPE,
|
|
238
|
+
inputSchema: {
|
|
239
|
+
type: 'object',
|
|
240
|
+
properties: { workspace: WORKSPACE, fields: { type: 'array', items: { type: 'string' } } },
|
|
241
|
+
required: ['fields'],
|
|
242
|
+
},
|
|
243
|
+
},
|
|
244
|
+
{
|
|
245
|
+
name: 'workover_support_conversation_fields',
|
|
246
|
+
description: "One conversation's custom fields with their values." + SCOPE,
|
|
247
|
+
inputSchema: { type: 'object', properties: { conversation: CONVERSATION }, required: ['conversation'] },
|
|
248
|
+
},
|
|
249
|
+
{
|
|
250
|
+
name: 'workover_support_set_conversation_fields',
|
|
251
|
+
description:
|
|
252
|
+
"Set some of a conversation's fields: `values` maps a field (key or label) to its value; an empty string or null clears it. Every value is checked before anything is written. Any teammate. Safe to repeat." +
|
|
253
|
+
SCOPE,
|
|
254
|
+
inputSchema: {
|
|
255
|
+
type: 'object',
|
|
256
|
+
properties: {
|
|
257
|
+
conversation: CONVERSATION,
|
|
258
|
+
values: { type: 'object', description: 'For example {"plan": "Pro", "renewal_date": "2026-12-01"}.' },
|
|
259
|
+
},
|
|
260
|
+
required: ['conversation', 'values'],
|
|
261
|
+
},
|
|
262
|
+
},
|
|
263
|
+
];
|
|
264
|
+
|
|
265
|
+
/** The field tools' names, for the MCP dispatcher. */
|
|
266
|
+
export const FIELD_TOOLS = new Set(FIELD_TOOL_DEFS.map((t) => t.name));
|
|
267
|
+
|
|
268
|
+
/** One field tool, run: the lines to answer with. Throws as `call` does. */
|
|
269
|
+
export async function runFieldTool(call, name, args = {}) {
|
|
270
|
+
const workspace = args.workspace === undefined || args.workspace === null ? undefined : String(args.workspace);
|
|
271
|
+
if (name === 'workover_support_fields') return renderFields(await listFields(call, { workspace }));
|
|
272
|
+
if (name === 'workover_support_set_field')
|
|
273
|
+
return renderFieldSaved(
|
|
274
|
+
await setField(call, {
|
|
275
|
+
workspace,
|
|
276
|
+
key: args.key,
|
|
277
|
+
label: args.label,
|
|
278
|
+
type: args.type,
|
|
279
|
+
options: args.options,
|
|
280
|
+
position: args.position,
|
|
281
|
+
}),
|
|
282
|
+
);
|
|
283
|
+
if (name === 'workover_support_remove_field')
|
|
284
|
+
return renderFieldRemoved(await removeField(call, { workspace, field: args.field }));
|
|
285
|
+
if (name === 'workover_support_order_fields')
|
|
286
|
+
return renderFields(await orderFields(call, { workspace, fields: args.fields }));
|
|
287
|
+
if (name === 'workover_support_conversation_fields')
|
|
288
|
+
return renderConversationFields(await conversationFields(call, { conversation: args.conversation }));
|
|
289
|
+
if (name === 'workover_support_set_conversation_fields')
|
|
290
|
+
return renderConversationFields(
|
|
291
|
+
await setConversationFields(call, { conversation: args.conversation, values: args.values }),
|
|
292
|
+
);
|
|
293
|
+
throw apiError(400, `Unknown tool: ${name}`);
|
|
294
|
+
}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
// Freshdesk escalation, from a terminal: `workover support freshdesk ...` and the MCP server's
|
|
2
|
+
// `workover_support_*freshdesk*` tools (WO-1752, S18).
|
|
3
|
+
//
|
|
4
|
+
// THE SAME FILE ships in @workover/cli (apps/cli/support-freshdesk.mjs) and @workover/mcp
|
|
5
|
+
// (apps/mcp/support-freshdesk.mjs), because the two packages are published separately and neither
|
|
6
|
+
// may depend on the other. `apps/mcp/support-freshdesk.test.mjs` fails when the copies differ.
|
|
7
|
+
//
|
|
8
|
+
// Setup only: the helpdesk address, the API key (sealed by the api and never returned), test or
|
|
9
|
+
// live mode, the default priority, group and tags, and whether an escalated conversation is marked
|
|
10
|
+
// Done. Escalating a conversation stays a teammate's click in the inbox. Every call goes to a route
|
|
11
|
+
// opened to tokens holding the `support` scope (`workover login --support`). Each write is safe to
|
|
12
|
+
// repeat: saving the same values changes nothing, and removing again answers `removed: false`.
|
|
13
|
+
// Checking the key only reads from Freshdesk (whose key it is) and never writes there.
|
|
14
|
+
//
|
|
15
|
+
// `call(path, { method, body })` does the requests and resolves the parsed JSON, or throws an Error
|
|
16
|
+
// whose `.status` is the HTTP status and whose message is the API's own sentence. The renderers
|
|
17
|
+
// return lines, for a terminal or an MCP text result alike.
|
|
18
|
+
|
|
19
|
+
import { apiError, resolveWorkspace } from './site-setup.mjs';
|
|
20
|
+
|
|
21
|
+
const PRIORITY = { low: 1, medium: 2, high: 3, urgent: 4 };
|
|
22
|
+
const PRIORITY_NAME = { 1: 'Low', 2: 'Medium', 3: 'High', 4: 'Urgent' };
|
|
23
|
+
|
|
24
|
+
/** The Messenger these settings belong to: Support has to exist first. */
|
|
25
|
+
async function messengerOf(call, workspace) {
|
|
26
|
+
const ws = await resolveWorkspace(call, workspace);
|
|
27
|
+
const status = await call(`/messenger/agent-setup?organizationId=${encodeURIComponent(ws.id)}`);
|
|
28
|
+
const id = status?.messenger?.id;
|
|
29
|
+
if (!id) throw apiError(404, 'Support is not set up in this workspace yet. Run `workover support setup` first.');
|
|
30
|
+
return { ws, messengerId: id };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const base = (messengerId) => `/messenger/${messengerId}/freshdesk`;
|
|
34
|
+
|
|
35
|
+
/** `2`, `"high"` or `"High"` as Freshdesk's 1 to 4. */
|
|
36
|
+
export function priorityOf(value) {
|
|
37
|
+
if (value === undefined || value === null || value === '') return undefined;
|
|
38
|
+
const n = typeof value === 'number' ? value : PRIORITY[String(value).trim().toLowerCase()] ?? Number(value);
|
|
39
|
+
if (![1, 2, 3, 4].includes(n)) throw apiError(400, 'Priority is low, medium, high or urgent (or 1 to 4).');
|
|
40
|
+
return n;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The settings, with whether a key is stored (never the key). */
|
|
44
|
+
export async function getFreshdesk(call, { workspace } = {}) {
|
|
45
|
+
const { ws, messengerId } = await messengerOf(call, workspace);
|
|
46
|
+
return { workspace: ws, settings: await call(base(messengerId)) };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Change what is given; the rest keep their value. The first save needs the address and the key. */
|
|
50
|
+
export async function setFreshdesk(call, { workspace, domain, apiKey, mode, priority, groupId, tags, closeAfter }) {
|
|
51
|
+
const body = {};
|
|
52
|
+
if (typeof domain === 'string' && domain.trim()) body.domain = domain.trim();
|
|
53
|
+
if (typeof apiKey === 'string' && apiKey.trim()) body.apiKey = apiKey.trim();
|
|
54
|
+
if (mode !== undefined) {
|
|
55
|
+
if (mode !== 'test' && mode !== 'live') throw apiError(400, 'Mode is test or live.');
|
|
56
|
+
body.mode = mode;
|
|
57
|
+
}
|
|
58
|
+
const p = priorityOf(priority);
|
|
59
|
+
if (p !== undefined) body.priority = p;
|
|
60
|
+
if (groupId !== undefined) body.groupId = groupId === null || groupId === '' || groupId === 'none' ? null : String(groupId).trim();
|
|
61
|
+
if (tags !== undefined)
|
|
62
|
+
body.tags = (Array.isArray(tags) ? tags : String(tags).split(','))
|
|
63
|
+
.map((t) => String(t).trim())
|
|
64
|
+
.filter(Boolean);
|
|
65
|
+
if (closeAfter !== undefined) body.closeAfter = !!closeAfter;
|
|
66
|
+
if (!Object.keys(body).length) throw apiError(400, 'Give something to change: the address, the key, the mode, the priority, the group, the tags or close-after.');
|
|
67
|
+
const { ws, messengerId } = await messengerOf(call, workspace);
|
|
68
|
+
const settings = await call(base(messengerId), { method: 'PUT', body });
|
|
69
|
+
return { workspace: ws, settings };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Whose key it is, read from Freshdesk. A failed check takes Support back to test mode. */
|
|
73
|
+
export async function checkFreshdesk(call, { workspace } = {}) {
|
|
74
|
+
const { ws, messengerId } = await messengerOf(call, workspace);
|
|
75
|
+
const res = await call(`${base(messengerId)}/check`, { method: 'POST' });
|
|
76
|
+
return { workspace: ws, ...res };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Forget the key and the settings. Tickets already made stay on their conversations. */
|
|
80
|
+
export async function removeFreshdesk(call, { workspace } = {}) {
|
|
81
|
+
const { ws, messengerId } = await messengerOf(call, workspace);
|
|
82
|
+
const res = await call(base(messengerId), { method: 'DELETE' });
|
|
83
|
+
return { workspace: ws, ...res };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// ------------------------------------------------------------------ rendering
|
|
87
|
+
|
|
88
|
+
export function renderFreshdesk(res) {
|
|
89
|
+
const s = res.settings || {};
|
|
90
|
+
const where = res.workspace?.name ?? 'this workspace';
|
|
91
|
+
if (!s.configured)
|
|
92
|
+
return [
|
|
93
|
+
'',
|
|
94
|
+
` Freshdesk escalation in ${where}: not connected.`,
|
|
95
|
+
' Connect it with `workover support freshdesk set --domain <name>.freshdesk.com --key-file <file>`.',
|
|
96
|
+
'',
|
|
97
|
+
];
|
|
98
|
+
const out = [
|
|
99
|
+
'',
|
|
100
|
+
` Freshdesk escalation in ${where}`,
|
|
101
|
+
'',
|
|
102
|
+
` Address ${s.domain}`,
|
|
103
|
+
` API key ${s.keySet ? 'stored (never shown)' : 'none'}`,
|
|
104
|
+
` Mode ${s.mode === 'live' ? 'live: escalating creates real tickets' : 'test: escalating creates nothing'}`,
|
|
105
|
+
` Priority ${PRIORITY_NAME[s.priority] ?? s.priority}`,
|
|
106
|
+
` Group ${s.groupId ?? 'none'}`,
|
|
107
|
+
` Tags ${s.tags?.length ? s.tags.join(', ') : 'none'}`,
|
|
108
|
+
` Then ${s.closeAfter ? 'the conversation is marked Done' : 'the conversation stays open'}`,
|
|
109
|
+
` Key check ${s.checkedAt ? `${s.checkOk ? 'worked' : 'failed'}: ${s.checkNote ?? ''}` : 'not checked yet'}`,
|
|
110
|
+
];
|
|
111
|
+
if (s.mode !== 'live')
|
|
112
|
+
out.push('', s.canGoLive ? ' Ready to go live, with the person\'s yes: `--mode live`.' : ' Check the key before going live: `workover support freshdesk check`.');
|
|
113
|
+
out.push('');
|
|
114
|
+
return out;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function renderFreshdeskCheck(res) {
|
|
118
|
+
return ['', res.ok ? ` ✓ The key works. ${res.note}` : ` ✗ ${res.note} Support is in test mode.`, ''];
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function renderFreshdeskRemoved(res) {
|
|
122
|
+
return ['', res.removed ? ' ✓ Disconnected. The key is forgotten.' : ' ✓ Freshdesk was not connected; nothing changed.', ''];
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// ------------------------------------------------------------------ MCP tool definitions
|
|
126
|
+
|
|
127
|
+
const WORKSPACE = {
|
|
128
|
+
type: ['string', 'number'],
|
|
129
|
+
description: 'The workspace, by slug or id. Needed only when the account has more than one.',
|
|
130
|
+
};
|
|
131
|
+
const SCOPE =
|
|
132
|
+
' Needs a token from `workover login --support`; without it this returns a permission error naming that command.';
|
|
133
|
+
|
|
134
|
+
/** The MCP server's Freshdesk tools, listed with the other Support tools (index.mjs). */
|
|
135
|
+
export const FRESHDESK_TOOL_DEFS = [
|
|
136
|
+
{
|
|
137
|
+
name: 'workover_support_freshdesk',
|
|
138
|
+
description:
|
|
139
|
+
"Whether Support escalates conversations to Freshdesk: the helpdesk address, whether a key is stored (the key is never returned), test or live mode, the default priority, group and tags, whether an escalated conversation is marked Done, and the last key check. A teammate escalates a conversation from the inbox's details panel; these tools only set it up." +
|
|
140
|
+
SCOPE,
|
|
141
|
+
inputSchema: { type: 'object', properties: { workspace: WORKSPACE } },
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
name: 'workover_support_set_freshdesk',
|
|
145
|
+
description:
|
|
146
|
+
'Connect or change Freshdesk escalation. Only what is given changes. The first call needs `domain` and `apiKey` (the agent\'s key, from Freshdesk: Profile settings, View API key). It starts in test mode, where escalating shows the ticket and creates nothing; `mode: "live"` needs a key check that worked, and the person\'s yes, because live tickets email their customers. Safe to repeat. Owners and admins.' +
|
|
147
|
+
SCOPE,
|
|
148
|
+
inputSchema: {
|
|
149
|
+
type: 'object',
|
|
150
|
+
properties: {
|
|
151
|
+
workspace: WORKSPACE,
|
|
152
|
+
domain: { type: 'string', description: 'The helpdesk address, such as yourcompany.freshdesk.com.' },
|
|
153
|
+
apiKey: { type: 'string', description: 'The Freshdesk API key. Sealed by Workover and never shown again.' },
|
|
154
|
+
mode: { type: 'string', enum: ['test', 'live'] },
|
|
155
|
+
priority: { type: ['string', 'number'], description: 'For a conversation with none: low, medium, high or urgent.' },
|
|
156
|
+
groupId: { type: ['string', 'number', 'null'], description: "The Freshdesk group's number, or null for none." },
|
|
157
|
+
tags: { type: 'array', items: { type: 'string' }, description: 'Added to every ticket, up to ten.' },
|
|
158
|
+
closeAfter: { type: 'boolean', description: 'Mark the conversation Done after a live escalation.' },
|
|
159
|
+
},
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
name: 'workover_support_check_freshdesk',
|
|
164
|
+
description:
|
|
165
|
+
'Check the stored key: reads whose key it is from Freshdesk and writes nothing there. A failed check takes Support back to test mode. Owners and admins.' +
|
|
166
|
+
SCOPE,
|
|
167
|
+
inputSchema: { type: 'object', properties: { workspace: WORKSPACE } },
|
|
168
|
+
},
|
|
169
|
+
{
|
|
170
|
+
name: 'workover_support_remove_freshdesk',
|
|
171
|
+
description:
|
|
172
|
+
'Disconnect Freshdesk: the key is forgotten, and tickets already made stay on their conversations. Safe to repeat. Owners and admins.' +
|
|
173
|
+
SCOPE,
|
|
174
|
+
inputSchema: { type: 'object', properties: { workspace: WORKSPACE } },
|
|
175
|
+
},
|
|
176
|
+
];
|