@contractspec/lib.support-bot 1.57.0 → 1.58.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/dist/bot/auto-responder.d.ts +19 -23
- package/dist/bot/auto-responder.d.ts.map +1 -1
- package/dist/bot/auto-responder.js +86 -68
- package/dist/bot/feedback-loop.d.ts +13 -17
- package/dist/bot/feedback-loop.d.ts.map +1 -1
- package/dist/bot/feedback-loop.js +38 -34
- package/dist/bot/index.d.ts +4 -4
- package/dist/bot/index.d.ts.map +1 -0
- package/dist/bot/index.js +268 -4
- package/dist/bot/tools.d.ts +9 -13
- package/dist/bot/tools.d.ts.map +1 -1
- package/dist/bot/tools.js +118 -123
- package/dist/browser/bot/auto-responder.js +101 -0
- package/dist/browser/bot/feedback-loop.js +38 -0
- package/dist/browser/bot/index.js +268 -0
- package/dist/browser/bot/tools.js +131 -0
- package/dist/browser/index.js +517 -0
- package/dist/browser/rag/index.js +65 -0
- package/dist/browser/rag/ticket-resolver.js +65 -0
- package/dist/browser/spec.js +33 -0
- package/dist/browser/tickets/classifier.js +156 -0
- package/dist/browser/tickets/index.js +156 -0
- package/dist/browser/types.js +0 -0
- package/dist/index.d.ts +6 -11
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +518 -9
- package/dist/node/bot/auto-responder.js +101 -0
- package/dist/node/bot/feedback-loop.js +38 -0
- package/dist/node/bot/index.js +268 -0
- package/dist/node/bot/tools.js +131 -0
- package/dist/node/index.js +517 -0
- package/dist/node/rag/index.js +65 -0
- package/dist/node/rag/ticket-resolver.js +65 -0
- package/dist/node/spec.js +33 -0
- package/dist/node/tickets/classifier.js +156 -0
- package/dist/node/tickets/index.js +156 -0
- package/dist/node/types.js +0 -0
- package/dist/rag/index.d.ts +2 -2
- package/dist/rag/index.d.ts.map +1 -0
- package/dist/rag/index.js +66 -3
- package/dist/rag/ticket-resolver.d.ts +17 -21
- package/dist/rag/ticket-resolver.d.ts.map +1 -1
- package/dist/rag/ticket-resolver.js +65 -63
- package/dist/spec.d.ts +7 -11
- package/dist/spec.d.ts.map +1 -1
- package/dist/spec.js +31 -32
- package/dist/tickets/classifier.d.ts +18 -22
- package/dist/tickets/classifier.d.ts.map +1 -1
- package/dist/tickets/classifier.js +153 -195
- package/dist/tickets/index.d.ts +2 -2
- package/dist/tickets/index.d.ts.map +1 -0
- package/dist/tickets/index.js +156 -2
- package/dist/types.d.ts +62 -66
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -0
- package/package.json +127 -38
- package/dist/bot/auto-responder.js.map +0 -1
- package/dist/bot/feedback-loop.js.map +0 -1
- package/dist/bot/tools.js.map +0 -1
- package/dist/rag/ticket-resolver.js.map +0 -1
- package/dist/spec.js.map +0 -1
- package/dist/tickets/classifier.js.map +0 -1
|
@@ -0,0 +1,517 @@
|
|
|
1
|
+
// src/bot/auto-responder.ts
|
|
2
|
+
class AutoResponder {
|
|
3
|
+
llm;
|
|
4
|
+
model;
|
|
5
|
+
tone;
|
|
6
|
+
closing;
|
|
7
|
+
constructor(options) {
|
|
8
|
+
this.llm = options?.llm;
|
|
9
|
+
this.model = options?.model;
|
|
10
|
+
this.tone = options?.tone ?? "friendly";
|
|
11
|
+
this.closing = options?.closing ?? (this.tone === "friendly" ? "We remain available if you need anything else." : "Please let us know if you require additional assistance.");
|
|
12
|
+
}
|
|
13
|
+
async draft(ticket, resolution, classification) {
|
|
14
|
+
if (this.llm) {
|
|
15
|
+
return this.generateWithLLM(ticket, resolution, classification);
|
|
16
|
+
}
|
|
17
|
+
return this.generateTemplate(ticket, resolution, classification);
|
|
18
|
+
}
|
|
19
|
+
async generateWithLLM(ticket, resolution, classification) {
|
|
20
|
+
const prompt = `You are a ${this.tone} support agent. Draft an email response.
|
|
21
|
+
Ticket Subject: ${ticket.subject}
|
|
22
|
+
Ticket Body: ${ticket.body}
|
|
23
|
+
Detected Category: ${classification.category}
|
|
24
|
+
Detected Priority: ${classification.priority}
|
|
25
|
+
Resolution:
|
|
26
|
+
${resolution.answer}
|
|
27
|
+
Citations: ${resolution.citations.map((c) => c.label).join(", ")}`;
|
|
28
|
+
const response = await this.llm.chat([
|
|
29
|
+
{
|
|
30
|
+
role: "system",
|
|
31
|
+
content: [
|
|
32
|
+
{
|
|
33
|
+
type: "text",
|
|
34
|
+
text: "Write empathetic, accurate support replies that cite sources when relevant."
|
|
35
|
+
}
|
|
36
|
+
]
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
role: "user",
|
|
40
|
+
content: [{ type: "text", text: prompt }]
|
|
41
|
+
}
|
|
42
|
+
], { model: this.model });
|
|
43
|
+
const body = response.message.content.map((part) => ("text" in part) ? part.text : "").join("").trim();
|
|
44
|
+
return this.buildDraft(ticket, resolution, classification, body);
|
|
45
|
+
}
|
|
46
|
+
generateTemplate(ticket, resolution, classification) {
|
|
47
|
+
const greeting = ticket.customerName ? `Hi ${ticket.customerName},` : "Hi there,";
|
|
48
|
+
const body = `${greeting}
|
|
49
|
+
|
|
50
|
+
Thanks for contacting us about "${ticket.subject}". ${this.renderCategoryIntro(classification)}
|
|
51
|
+
|
|
52
|
+
${resolution.answer}
|
|
53
|
+
|
|
54
|
+
${this.renderCitations(resolution)}
|
|
55
|
+
${this.closing}
|
|
56
|
+
|
|
57
|
+
— ContractSpec Support`;
|
|
58
|
+
return this.buildDraft(ticket, resolution, classification, body);
|
|
59
|
+
}
|
|
60
|
+
buildDraft(ticket, resolution, classification, body) {
|
|
61
|
+
return {
|
|
62
|
+
ticketId: ticket.id,
|
|
63
|
+
subject: ticket.subject.startsWith("Re:") ? ticket.subject : `Re: ${ticket.subject}`,
|
|
64
|
+
body,
|
|
65
|
+
confidence: Math.min(resolution.confidence, classification.confidence),
|
|
66
|
+
requiresEscalation: resolution.actions.some((action) => action.type === "escalate") || Boolean(classification.escalationRequired),
|
|
67
|
+
citations: resolution.citations
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
renderCategoryIntro(classification) {
|
|
71
|
+
switch (classification.category) {
|
|
72
|
+
case "billing":
|
|
73
|
+
return "I understand billing issues can be stressful, so let me clarify the situation.";
|
|
74
|
+
case "technical":
|
|
75
|
+
return "I see you encountered a technical issue. Here is what happened and how to fix it.";
|
|
76
|
+
case "product":
|
|
77
|
+
return "Thanks for sharing feedback about the product. Here are the next steps.";
|
|
78
|
+
case "account":
|
|
79
|
+
return "Account access is critical, so let me walk you through the resolution.";
|
|
80
|
+
case "compliance":
|
|
81
|
+
return "Compliance questions require precision. See the policy-aligned answer below.";
|
|
82
|
+
default:
|
|
83
|
+
return "Here is what we found after reviewing your request.";
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
renderCitations(resolution) {
|
|
87
|
+
if (!resolution.citations.length)
|
|
88
|
+
return "";
|
|
89
|
+
const lines = resolution.citations.map((citation, index) => {
|
|
90
|
+
const label = citation.label || `Source ${index + 1}`;
|
|
91
|
+
const link = citation.url ? ` (${citation.url})` : "";
|
|
92
|
+
return `- ${label}${link}`;
|
|
93
|
+
});
|
|
94
|
+
return `References:
|
|
95
|
+
${lines.join(`
|
|
96
|
+
`)}`;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// src/bot/feedback-loop.ts
|
|
101
|
+
class SupportFeedbackLoop {
|
|
102
|
+
history = [];
|
|
103
|
+
responseTimes = new Map;
|
|
104
|
+
recordResolution(payload, responseTimeMs) {
|
|
105
|
+
this.history.push(payload);
|
|
106
|
+
if (responseTimeMs != null) {
|
|
107
|
+
this.responseTimes.set(payload.ticket.id, responseTimeMs);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
metrics() {
|
|
111
|
+
const total = this.history.length;
|
|
112
|
+
const autoResolved = this.history.filter((entry) => !entry.resolution.actions.some((action) => action.type === "escalate")).length;
|
|
113
|
+
const escalated = total - autoResolved;
|
|
114
|
+
const avgConfidence = total === 0 ? 0 : this.history.reduce((sum, entry) => sum + entry.resolution.confidence, 0) / total;
|
|
115
|
+
const avgResponseTimeMs = this.responseTimes.size === 0 ? 0 : [...this.responseTimes.values()].reduce((a, b) => a + b, 0) / this.responseTimes.size;
|
|
116
|
+
return {
|
|
117
|
+
totalTickets: total,
|
|
118
|
+
autoResolved,
|
|
119
|
+
escalated,
|
|
120
|
+
avgConfidence: Number(avgConfidence.toFixed(2)),
|
|
121
|
+
avgResponseTimeMs: Math.round(avgResponseTimeMs)
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
feedbackSummary(limit = 5) {
|
|
125
|
+
const recent = this.history.slice(-limit);
|
|
126
|
+
if (!recent.length)
|
|
127
|
+
return "No feedback recorded yet.";
|
|
128
|
+
return recent.map((entry) => {
|
|
129
|
+
const status = entry.resolution.actions.some((action) => action.type === "escalate") ? "Escalated" : "Auto-resolved";
|
|
130
|
+
return `${entry.ticket.subject} – ${status} (confidence: ${entry.resolution.confidence})`;
|
|
131
|
+
}).join(`
|
|
132
|
+
`);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// src/bot/tools.ts
|
|
137
|
+
import * as z from "zod";
|
|
138
|
+
var ticketSchema = z.object({
|
|
139
|
+
id: z.string(),
|
|
140
|
+
subject: z.string(),
|
|
141
|
+
body: z.string(),
|
|
142
|
+
channel: z.enum(["email", "chat", "phone", "portal"]),
|
|
143
|
+
customerName: z.string().optional(),
|
|
144
|
+
customerEmail: z.string().optional(),
|
|
145
|
+
metadata: z.object().optional()
|
|
146
|
+
});
|
|
147
|
+
var supportCitationSchema = z.object({
|
|
148
|
+
label: z.string(),
|
|
149
|
+
url: z.string().optional(),
|
|
150
|
+
snippet: z.string().optional(),
|
|
151
|
+
score: z.number().optional()
|
|
152
|
+
});
|
|
153
|
+
var supportActionSchema = z.object({
|
|
154
|
+
type: z.enum(["respond", "escalate", "refund", "manual"]),
|
|
155
|
+
label: z.string(),
|
|
156
|
+
payload: z.record(z.string(), z.string())
|
|
157
|
+
});
|
|
158
|
+
var supportResolutionSchema = z.object({
|
|
159
|
+
ticketId: z.string(),
|
|
160
|
+
answer: z.string(),
|
|
161
|
+
confidence: z.number(),
|
|
162
|
+
citations: supportCitationSchema.array(),
|
|
163
|
+
actions: supportActionSchema.array(),
|
|
164
|
+
escalationReason: z.string().optional(),
|
|
165
|
+
knowledgeUpdates: z.array(z.string()).optional()
|
|
166
|
+
});
|
|
167
|
+
var ticketClassificationSchema = z.object({
|
|
168
|
+
ticketId: z.string(),
|
|
169
|
+
category: z.enum([
|
|
170
|
+
"billing",
|
|
171
|
+
"technical",
|
|
172
|
+
"product",
|
|
173
|
+
"account",
|
|
174
|
+
"compliance",
|
|
175
|
+
"other"
|
|
176
|
+
]),
|
|
177
|
+
priority: z.enum([
|
|
178
|
+
"urgent",
|
|
179
|
+
"high",
|
|
180
|
+
"medium",
|
|
181
|
+
"low"
|
|
182
|
+
]),
|
|
183
|
+
sentiment: z.enum([
|
|
184
|
+
"positive",
|
|
185
|
+
"neutral",
|
|
186
|
+
"negative",
|
|
187
|
+
"frustrated"
|
|
188
|
+
]),
|
|
189
|
+
intents: z.array(z.string()),
|
|
190
|
+
tags: z.array(z.string()),
|
|
191
|
+
confidence: z.number(),
|
|
192
|
+
escalationRequired: z.boolean().optional()
|
|
193
|
+
});
|
|
194
|
+
function ensureTicket(input) {
|
|
195
|
+
if (!input || typeof input !== "object" || !("ticket" in input)) {
|
|
196
|
+
throw new Error("Input must include ticket");
|
|
197
|
+
}
|
|
198
|
+
const ticket = input.ticket;
|
|
199
|
+
if (!ticket?.id)
|
|
200
|
+
throw new Error("Ticket is missing id");
|
|
201
|
+
return ticket;
|
|
202
|
+
}
|
|
203
|
+
function extractResolution(input) {
|
|
204
|
+
if (!input || typeof input !== "object" || !("resolution" in input))
|
|
205
|
+
return;
|
|
206
|
+
return input.resolution;
|
|
207
|
+
}
|
|
208
|
+
function extractClassification(input) {
|
|
209
|
+
if (!input || typeof input !== "object" || !("classification" in input))
|
|
210
|
+
return;
|
|
211
|
+
return input.classification;
|
|
212
|
+
}
|
|
213
|
+
function createSupportTools(options) {
|
|
214
|
+
const classifyTool = {
|
|
215
|
+
title: "support_classify_ticket",
|
|
216
|
+
description: "Classify a ticket for priority, sentiment, and category",
|
|
217
|
+
inputSchema: z.object({ ticket: ticketSchema }),
|
|
218
|
+
execute: async (input) => {
|
|
219
|
+
const ticket = ensureTicket(input);
|
|
220
|
+
const classification = await options.classifier.classify(ticket);
|
|
221
|
+
return {
|
|
222
|
+
content: JSON.stringify(classification),
|
|
223
|
+
metadata: { ticketId: ticket.id }
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
const resolveTool = {
|
|
228
|
+
title: "support_resolve_ticket",
|
|
229
|
+
description: "Generate a knowledge-grounded resolution for a ticket",
|
|
230
|
+
inputSchema: z.object({ ticket: ticketSchema }),
|
|
231
|
+
execute: async (input) => {
|
|
232
|
+
const ticket = ensureTicket(input);
|
|
233
|
+
const resolution = await options.resolver.resolve(ticket);
|
|
234
|
+
return {
|
|
235
|
+
content: JSON.stringify(resolution),
|
|
236
|
+
metadata: { ticketId: ticket.id }
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
const responderTool = {
|
|
241
|
+
title: "support_draft_response",
|
|
242
|
+
description: "Draft a user-facing reply based on resolution + classification",
|
|
243
|
+
inputSchema: z.object({
|
|
244
|
+
ticket: ticketSchema,
|
|
245
|
+
resolution: supportResolutionSchema,
|
|
246
|
+
classification: ticketClassificationSchema
|
|
247
|
+
}),
|
|
248
|
+
execute: async (input) => {
|
|
249
|
+
const ticket = ensureTicket(input);
|
|
250
|
+
const resolution = extractResolution(input);
|
|
251
|
+
const classification = extractClassification(input);
|
|
252
|
+
if (!resolution || !classification) {
|
|
253
|
+
throw new Error("resolution and classification are required");
|
|
254
|
+
}
|
|
255
|
+
const draft = await options.responder.draft(ticket, resolution, classification);
|
|
256
|
+
return {
|
|
257
|
+
content: JSON.stringify(draft),
|
|
258
|
+
metadata: { ticketId: ticket.id }
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
return [classifyTool, resolveTool, responderTool];
|
|
263
|
+
}
|
|
264
|
+
// src/spec.ts
|
|
265
|
+
import { defineAgent } from "@contractspec/lib.ai-agent";
|
|
266
|
+
function defineSupportBot(definition) {
|
|
267
|
+
const base = defineAgent({
|
|
268
|
+
...definition.base,
|
|
269
|
+
policy: {
|
|
270
|
+
...definition.base.policy,
|
|
271
|
+
confidence: {
|
|
272
|
+
min: definition.base.policy?.confidence?.min ?? 0.7,
|
|
273
|
+
default: definition.base.policy?.confidence?.default ?? 0.6
|
|
274
|
+
},
|
|
275
|
+
escalation: {
|
|
276
|
+
confidenceThreshold: definition.autoEscalateThreshold ?? definition.base.policy?.escalation?.confidenceThreshold ?? definition.base.policy?.confidence?.min ?? 0.7,
|
|
277
|
+
...definition.base.policy?.escalation
|
|
278
|
+
}
|
|
279
|
+
},
|
|
280
|
+
memory: definition.base.memory ?? { maxEntries: 120, ttlMinutes: 120 },
|
|
281
|
+
tools: definition.tools ?? definition.base.tools,
|
|
282
|
+
instructions: `${definition.base.instructions}
|
|
283
|
+
|
|
284
|
+
Always cite support knowledge sources and flag compliance/billing issues for human review when unsure.`
|
|
285
|
+
});
|
|
286
|
+
return {
|
|
287
|
+
...base,
|
|
288
|
+
thresholds: {
|
|
289
|
+
autoResolveMinConfidence: definition.autoEscalateThreshold ?? 0.75,
|
|
290
|
+
maxIterations: 6
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// src/rag/ticket-resolver.ts
|
|
296
|
+
class TicketResolver {
|
|
297
|
+
knowledge;
|
|
298
|
+
minConfidence;
|
|
299
|
+
prependPrompt;
|
|
300
|
+
constructor(options) {
|
|
301
|
+
this.knowledge = options.knowledge;
|
|
302
|
+
this.minConfidence = options.minConfidence ?? 0.65;
|
|
303
|
+
this.prependPrompt = options.prependPrompt;
|
|
304
|
+
}
|
|
305
|
+
async resolve(ticket) {
|
|
306
|
+
const question = this.buildQuestion(ticket);
|
|
307
|
+
const answer = await this.knowledge.query(question);
|
|
308
|
+
return this.toResolution(ticket, answer);
|
|
309
|
+
}
|
|
310
|
+
buildQuestion(ticket) {
|
|
311
|
+
const header = [`Subject: ${ticket.subject}`, `Channel: ${ticket.channel}`];
|
|
312
|
+
if (ticket.customerName)
|
|
313
|
+
header.push(`Customer: ${ticket.customerName}`);
|
|
314
|
+
const sections = [
|
|
315
|
+
this.prependPrompt,
|
|
316
|
+
header.join(`
|
|
317
|
+
`),
|
|
318
|
+
"---",
|
|
319
|
+
ticket.body
|
|
320
|
+
].filter(Boolean);
|
|
321
|
+
return sections.join(`
|
|
322
|
+
`);
|
|
323
|
+
}
|
|
324
|
+
toResolution(ticket, answer) {
|
|
325
|
+
const citations = answer.references.map((ref) => {
|
|
326
|
+
const label = typeof ref.payload?.title === "string" ? ref.payload.title : typeof ref.payload?.documentId === "string" ? ref.payload.documentId : ref.id;
|
|
327
|
+
return {
|
|
328
|
+
label,
|
|
329
|
+
url: typeof ref.payload?.url === "string" ? ref.payload.url : undefined,
|
|
330
|
+
snippet: typeof ref.payload?.text === "string" ? ref.payload.text.slice(0, 280) : undefined,
|
|
331
|
+
score: ref.score
|
|
332
|
+
};
|
|
333
|
+
});
|
|
334
|
+
const confidence = this.deriveConfidence(answer);
|
|
335
|
+
const escalate = confidence < this.minConfidence || citations.length === 0;
|
|
336
|
+
return {
|
|
337
|
+
ticketId: ticket.id,
|
|
338
|
+
answer: answer.answer,
|
|
339
|
+
confidence,
|
|
340
|
+
citations,
|
|
341
|
+
actions: [
|
|
342
|
+
escalate ? { type: "escalate", label: "Escalate for human review" } : { type: "respond", label: "Send automated response" }
|
|
343
|
+
],
|
|
344
|
+
escalationReason: escalate ? "Insufficient confidence or missing knowledge references" : undefined,
|
|
345
|
+
knowledgeUpdates: escalate ? [ticket.body.slice(0, 200)] : undefined
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
deriveConfidence(answer) {
|
|
349
|
+
if (!answer.references.length)
|
|
350
|
+
return 0.3;
|
|
351
|
+
const topScore = answer.references[0]?.score ?? 0.4;
|
|
352
|
+
const normalized = Math.min(1, Math.max(0, topScore));
|
|
353
|
+
const tokenPenalty = answer.usage?.completionTokens ? Math.min(answer.usage.completionTokens / 1000, 0.2) : 0;
|
|
354
|
+
return Number((normalized - tokenPenalty).toFixed(2));
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
// src/tickets/classifier.ts
|
|
358
|
+
var CATEGORY_KEYWORDS = {
|
|
359
|
+
billing: ["invoice", "payout", "refund", "charge", "billing", "payment"],
|
|
360
|
+
technical: ["bug", "error", "crash", "issue", "failed", "timeout"],
|
|
361
|
+
product: ["feature", "roadmap", "idea", "request", "feedback"],
|
|
362
|
+
account: ["login", "password", "2fa", "account", "profile", "email change"],
|
|
363
|
+
compliance: ["kyc", "aml", "compliance", "regulation", "gdpr"],
|
|
364
|
+
other: []
|
|
365
|
+
};
|
|
366
|
+
var PRIORITY_HINTS = {
|
|
367
|
+
urgent: ["urgent", "asap", "immediately", "today", "right away"],
|
|
368
|
+
high: ["high priority", "blocking", "major", "critical"],
|
|
369
|
+
medium: ["soon", "next few days"],
|
|
370
|
+
low: ["nice to have", "when possible", "later"]
|
|
371
|
+
};
|
|
372
|
+
var SENTIMENT_HINTS = {
|
|
373
|
+
positive: ["love", "great", "awesome", "thank you"],
|
|
374
|
+
neutral: ["question", "wonder", "curious"],
|
|
375
|
+
negative: ["unhappy", "bad", "terrible", "awful", "angry"],
|
|
376
|
+
frustrated: ["furious", "frustrated", "fed up", "ridiculous"]
|
|
377
|
+
};
|
|
378
|
+
|
|
379
|
+
class TicketClassifier {
|
|
380
|
+
keywords;
|
|
381
|
+
llm;
|
|
382
|
+
llmModel;
|
|
383
|
+
constructor(options) {
|
|
384
|
+
this.keywords = {
|
|
385
|
+
...CATEGORY_KEYWORDS,
|
|
386
|
+
...options?.keywords ?? {}
|
|
387
|
+
};
|
|
388
|
+
this.llm = options?.llm;
|
|
389
|
+
this.llmModel = options?.llmModel;
|
|
390
|
+
}
|
|
391
|
+
async classify(ticket) {
|
|
392
|
+
const heuristics = this.heuristicClassification(ticket);
|
|
393
|
+
if (!this.llm)
|
|
394
|
+
return heuristics;
|
|
395
|
+
try {
|
|
396
|
+
const llmResult = await this.llm.chat([
|
|
397
|
+
{
|
|
398
|
+
role: "system",
|
|
399
|
+
content: [{ type: "text", text: "Classify the support ticket." }]
|
|
400
|
+
},
|
|
401
|
+
{
|
|
402
|
+
role: "user",
|
|
403
|
+
content: [
|
|
404
|
+
{
|
|
405
|
+
type: "text",
|
|
406
|
+
text: JSON.stringify({
|
|
407
|
+
subject: ticket.subject,
|
|
408
|
+
body: ticket.body,
|
|
409
|
+
channel: ticket.channel
|
|
410
|
+
})
|
|
411
|
+
}
|
|
412
|
+
]
|
|
413
|
+
}
|
|
414
|
+
], {
|
|
415
|
+
responseFormat: "json",
|
|
416
|
+
model: this.llmModel
|
|
417
|
+
});
|
|
418
|
+
const content = llmResult.message.content.find((part) => ("text" in part));
|
|
419
|
+
if (content && "text" in content) {
|
|
420
|
+
const parsed = JSON.parse(content.text);
|
|
421
|
+
return {
|
|
422
|
+
...heuristics,
|
|
423
|
+
...parsed,
|
|
424
|
+
intents: parsed.intents ?? heuristics.intents,
|
|
425
|
+
tags: parsed.tags ?? heuristics.tags
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
} catch {}
|
|
429
|
+
return heuristics;
|
|
430
|
+
}
|
|
431
|
+
heuristicClassification(ticket) {
|
|
432
|
+
const text = `${ticket.subject}
|
|
433
|
+
${ticket.body}`.toLowerCase();
|
|
434
|
+
const category = this.detectCategory(text);
|
|
435
|
+
const priority = this.detectPriority(text);
|
|
436
|
+
const sentiment = this.detectSentiment(text);
|
|
437
|
+
const intents = this.extractIntents(text);
|
|
438
|
+
const tags = intents.slice(0, 3);
|
|
439
|
+
const confidence = this.estimateConfidence(category, priority, sentiment);
|
|
440
|
+
return {
|
|
441
|
+
ticketId: ticket.id,
|
|
442
|
+
category,
|
|
443
|
+
priority,
|
|
444
|
+
sentiment,
|
|
445
|
+
intents,
|
|
446
|
+
tags,
|
|
447
|
+
confidence,
|
|
448
|
+
escalationRequired: priority === "urgent" || category === "compliance"
|
|
449
|
+
};
|
|
450
|
+
}
|
|
451
|
+
detectCategory(text) {
|
|
452
|
+
for (const [category, keywords] of Object.entries(this.keywords)) {
|
|
453
|
+
if (keywords.some((keyword) => text.includes(keyword))) {
|
|
454
|
+
return category;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
return "other";
|
|
458
|
+
}
|
|
459
|
+
detectPriority(text) {
|
|
460
|
+
for (const priority of [
|
|
461
|
+
"urgent",
|
|
462
|
+
"high",
|
|
463
|
+
"medium",
|
|
464
|
+
"low"
|
|
465
|
+
]) {
|
|
466
|
+
if (PRIORITY_HINTS[priority].some((word) => text.includes(word))) {
|
|
467
|
+
return priority;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
return "medium";
|
|
471
|
+
}
|
|
472
|
+
detectSentiment(text) {
|
|
473
|
+
for (const sentiment of [
|
|
474
|
+
"frustrated",
|
|
475
|
+
"negative",
|
|
476
|
+
"neutral",
|
|
477
|
+
"positive"
|
|
478
|
+
]) {
|
|
479
|
+
if (SENTIMENT_HINTS[sentiment].some((word) => text.includes(word))) {
|
|
480
|
+
return sentiment;
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
return "neutral";
|
|
484
|
+
}
|
|
485
|
+
extractIntents(text) {
|
|
486
|
+
const intents = [];
|
|
487
|
+
if (text.includes("refund") || text.includes("chargeback"))
|
|
488
|
+
intents.push("refund");
|
|
489
|
+
if (text.includes("payout"))
|
|
490
|
+
intents.push("payout");
|
|
491
|
+
if (text.includes("login"))
|
|
492
|
+
intents.push("login-help");
|
|
493
|
+
if (text.includes("feature"))
|
|
494
|
+
intents.push("feature-request");
|
|
495
|
+
if (text.includes("bug") || text.includes("error"))
|
|
496
|
+
intents.push("bug-report");
|
|
497
|
+
return intents.length ? intents : ["general"];
|
|
498
|
+
}
|
|
499
|
+
estimateConfidence(category, priority, sentiment) {
|
|
500
|
+
let base = 0.6;
|
|
501
|
+
if (category !== "other")
|
|
502
|
+
base += 0.1;
|
|
503
|
+
if (priority === "urgent" || priority === "low")
|
|
504
|
+
base += 0.05;
|
|
505
|
+
if (sentiment === "frustrated")
|
|
506
|
+
base -= 0.05;
|
|
507
|
+
return Math.min(0.95, Math.max(0.4, Number(base.toFixed(2))));
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
export {
|
|
511
|
+
defineSupportBot,
|
|
512
|
+
createSupportTools,
|
|
513
|
+
TicketResolver,
|
|
514
|
+
TicketClassifier,
|
|
515
|
+
SupportFeedbackLoop,
|
|
516
|
+
AutoResponder
|
|
517
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// src/rag/ticket-resolver.ts
|
|
2
|
+
class TicketResolver {
|
|
3
|
+
knowledge;
|
|
4
|
+
minConfidence;
|
|
5
|
+
prependPrompt;
|
|
6
|
+
constructor(options) {
|
|
7
|
+
this.knowledge = options.knowledge;
|
|
8
|
+
this.minConfidence = options.minConfidence ?? 0.65;
|
|
9
|
+
this.prependPrompt = options.prependPrompt;
|
|
10
|
+
}
|
|
11
|
+
async resolve(ticket) {
|
|
12
|
+
const question = this.buildQuestion(ticket);
|
|
13
|
+
const answer = await this.knowledge.query(question);
|
|
14
|
+
return this.toResolution(ticket, answer);
|
|
15
|
+
}
|
|
16
|
+
buildQuestion(ticket) {
|
|
17
|
+
const header = [`Subject: ${ticket.subject}`, `Channel: ${ticket.channel}`];
|
|
18
|
+
if (ticket.customerName)
|
|
19
|
+
header.push(`Customer: ${ticket.customerName}`);
|
|
20
|
+
const sections = [
|
|
21
|
+
this.prependPrompt,
|
|
22
|
+
header.join(`
|
|
23
|
+
`),
|
|
24
|
+
"---",
|
|
25
|
+
ticket.body
|
|
26
|
+
].filter(Boolean);
|
|
27
|
+
return sections.join(`
|
|
28
|
+
`);
|
|
29
|
+
}
|
|
30
|
+
toResolution(ticket, answer) {
|
|
31
|
+
const citations = answer.references.map((ref) => {
|
|
32
|
+
const label = typeof ref.payload?.title === "string" ? ref.payload.title : typeof ref.payload?.documentId === "string" ? ref.payload.documentId : ref.id;
|
|
33
|
+
return {
|
|
34
|
+
label,
|
|
35
|
+
url: typeof ref.payload?.url === "string" ? ref.payload.url : undefined,
|
|
36
|
+
snippet: typeof ref.payload?.text === "string" ? ref.payload.text.slice(0, 280) : undefined,
|
|
37
|
+
score: ref.score
|
|
38
|
+
};
|
|
39
|
+
});
|
|
40
|
+
const confidence = this.deriveConfidence(answer);
|
|
41
|
+
const escalate = confidence < this.minConfidence || citations.length === 0;
|
|
42
|
+
return {
|
|
43
|
+
ticketId: ticket.id,
|
|
44
|
+
answer: answer.answer,
|
|
45
|
+
confidence,
|
|
46
|
+
citations,
|
|
47
|
+
actions: [
|
|
48
|
+
escalate ? { type: "escalate", label: "Escalate for human review" } : { type: "respond", label: "Send automated response" }
|
|
49
|
+
],
|
|
50
|
+
escalationReason: escalate ? "Insufficient confidence or missing knowledge references" : undefined,
|
|
51
|
+
knowledgeUpdates: escalate ? [ticket.body.slice(0, 200)] : undefined
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
deriveConfidence(answer) {
|
|
55
|
+
if (!answer.references.length)
|
|
56
|
+
return 0.3;
|
|
57
|
+
const topScore = answer.references[0]?.score ?? 0.4;
|
|
58
|
+
const normalized = Math.min(1, Math.max(0, topScore));
|
|
59
|
+
const tokenPenalty = answer.usage?.completionTokens ? Math.min(answer.usage.completionTokens / 1000, 0.2) : 0;
|
|
60
|
+
return Number((normalized - tokenPenalty).toFixed(2));
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
export {
|
|
64
|
+
TicketResolver
|
|
65
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// src/rag/ticket-resolver.ts
|
|
2
|
+
class TicketResolver {
|
|
3
|
+
knowledge;
|
|
4
|
+
minConfidence;
|
|
5
|
+
prependPrompt;
|
|
6
|
+
constructor(options) {
|
|
7
|
+
this.knowledge = options.knowledge;
|
|
8
|
+
this.minConfidence = options.minConfidence ?? 0.65;
|
|
9
|
+
this.prependPrompt = options.prependPrompt;
|
|
10
|
+
}
|
|
11
|
+
async resolve(ticket) {
|
|
12
|
+
const question = this.buildQuestion(ticket);
|
|
13
|
+
const answer = await this.knowledge.query(question);
|
|
14
|
+
return this.toResolution(ticket, answer);
|
|
15
|
+
}
|
|
16
|
+
buildQuestion(ticket) {
|
|
17
|
+
const header = [`Subject: ${ticket.subject}`, `Channel: ${ticket.channel}`];
|
|
18
|
+
if (ticket.customerName)
|
|
19
|
+
header.push(`Customer: ${ticket.customerName}`);
|
|
20
|
+
const sections = [
|
|
21
|
+
this.prependPrompt,
|
|
22
|
+
header.join(`
|
|
23
|
+
`),
|
|
24
|
+
"---",
|
|
25
|
+
ticket.body
|
|
26
|
+
].filter(Boolean);
|
|
27
|
+
return sections.join(`
|
|
28
|
+
`);
|
|
29
|
+
}
|
|
30
|
+
toResolution(ticket, answer) {
|
|
31
|
+
const citations = answer.references.map((ref) => {
|
|
32
|
+
const label = typeof ref.payload?.title === "string" ? ref.payload.title : typeof ref.payload?.documentId === "string" ? ref.payload.documentId : ref.id;
|
|
33
|
+
return {
|
|
34
|
+
label,
|
|
35
|
+
url: typeof ref.payload?.url === "string" ? ref.payload.url : undefined,
|
|
36
|
+
snippet: typeof ref.payload?.text === "string" ? ref.payload.text.slice(0, 280) : undefined,
|
|
37
|
+
score: ref.score
|
|
38
|
+
};
|
|
39
|
+
});
|
|
40
|
+
const confidence = this.deriveConfidence(answer);
|
|
41
|
+
const escalate = confidence < this.minConfidence || citations.length === 0;
|
|
42
|
+
return {
|
|
43
|
+
ticketId: ticket.id,
|
|
44
|
+
answer: answer.answer,
|
|
45
|
+
confidence,
|
|
46
|
+
citations,
|
|
47
|
+
actions: [
|
|
48
|
+
escalate ? { type: "escalate", label: "Escalate for human review" } : { type: "respond", label: "Send automated response" }
|
|
49
|
+
],
|
|
50
|
+
escalationReason: escalate ? "Insufficient confidence or missing knowledge references" : undefined,
|
|
51
|
+
knowledgeUpdates: escalate ? [ticket.body.slice(0, 200)] : undefined
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
deriveConfidence(answer) {
|
|
55
|
+
if (!answer.references.length)
|
|
56
|
+
return 0.3;
|
|
57
|
+
const topScore = answer.references[0]?.score ?? 0.4;
|
|
58
|
+
const normalized = Math.min(1, Math.max(0, topScore));
|
|
59
|
+
const tokenPenalty = answer.usage?.completionTokens ? Math.min(answer.usage.completionTokens / 1000, 0.2) : 0;
|
|
60
|
+
return Number((normalized - tokenPenalty).toFixed(2));
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
export {
|
|
64
|
+
TicketResolver
|
|
65
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// src/spec.ts
|
|
2
|
+
import { defineAgent } from "@contractspec/lib.ai-agent";
|
|
3
|
+
function defineSupportBot(definition) {
|
|
4
|
+
const base = defineAgent({
|
|
5
|
+
...definition.base,
|
|
6
|
+
policy: {
|
|
7
|
+
...definition.base.policy,
|
|
8
|
+
confidence: {
|
|
9
|
+
min: definition.base.policy?.confidence?.min ?? 0.7,
|
|
10
|
+
default: definition.base.policy?.confidence?.default ?? 0.6
|
|
11
|
+
},
|
|
12
|
+
escalation: {
|
|
13
|
+
confidenceThreshold: definition.autoEscalateThreshold ?? definition.base.policy?.escalation?.confidenceThreshold ?? definition.base.policy?.confidence?.min ?? 0.7,
|
|
14
|
+
...definition.base.policy?.escalation
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
memory: definition.base.memory ?? { maxEntries: 120, ttlMinutes: 120 },
|
|
18
|
+
tools: definition.tools ?? definition.base.tools,
|
|
19
|
+
instructions: `${definition.base.instructions}
|
|
20
|
+
|
|
21
|
+
Always cite support knowledge sources and flag compliance/billing issues for human review when unsure.`
|
|
22
|
+
});
|
|
23
|
+
return {
|
|
24
|
+
...base,
|
|
25
|
+
thresholds: {
|
|
26
|
+
autoResolveMinConfidence: definition.autoEscalateThreshold ?? 0.75,
|
|
27
|
+
maxIterations: 6
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
export {
|
|
32
|
+
defineSupportBot
|
|
33
|
+
};
|