@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,156 @@
|
|
|
1
|
+
// src/tickets/classifier.ts
|
|
2
|
+
var CATEGORY_KEYWORDS = {
|
|
3
|
+
billing: ["invoice", "payout", "refund", "charge", "billing", "payment"],
|
|
4
|
+
technical: ["bug", "error", "crash", "issue", "failed", "timeout"],
|
|
5
|
+
product: ["feature", "roadmap", "idea", "request", "feedback"],
|
|
6
|
+
account: ["login", "password", "2fa", "account", "profile", "email change"],
|
|
7
|
+
compliance: ["kyc", "aml", "compliance", "regulation", "gdpr"],
|
|
8
|
+
other: []
|
|
9
|
+
};
|
|
10
|
+
var PRIORITY_HINTS = {
|
|
11
|
+
urgent: ["urgent", "asap", "immediately", "today", "right away"],
|
|
12
|
+
high: ["high priority", "blocking", "major", "critical"],
|
|
13
|
+
medium: ["soon", "next few days"],
|
|
14
|
+
low: ["nice to have", "when possible", "later"]
|
|
15
|
+
};
|
|
16
|
+
var SENTIMENT_HINTS = {
|
|
17
|
+
positive: ["love", "great", "awesome", "thank you"],
|
|
18
|
+
neutral: ["question", "wonder", "curious"],
|
|
19
|
+
negative: ["unhappy", "bad", "terrible", "awful", "angry"],
|
|
20
|
+
frustrated: ["furious", "frustrated", "fed up", "ridiculous"]
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
class TicketClassifier {
|
|
24
|
+
keywords;
|
|
25
|
+
llm;
|
|
26
|
+
llmModel;
|
|
27
|
+
constructor(options) {
|
|
28
|
+
this.keywords = {
|
|
29
|
+
...CATEGORY_KEYWORDS,
|
|
30
|
+
...options?.keywords ?? {}
|
|
31
|
+
};
|
|
32
|
+
this.llm = options?.llm;
|
|
33
|
+
this.llmModel = options?.llmModel;
|
|
34
|
+
}
|
|
35
|
+
async classify(ticket) {
|
|
36
|
+
const heuristics = this.heuristicClassification(ticket);
|
|
37
|
+
if (!this.llm)
|
|
38
|
+
return heuristics;
|
|
39
|
+
try {
|
|
40
|
+
const llmResult = await this.llm.chat([
|
|
41
|
+
{
|
|
42
|
+
role: "system",
|
|
43
|
+
content: [{ type: "text", text: "Classify the support ticket." }]
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
role: "user",
|
|
47
|
+
content: [
|
|
48
|
+
{
|
|
49
|
+
type: "text",
|
|
50
|
+
text: JSON.stringify({
|
|
51
|
+
subject: ticket.subject,
|
|
52
|
+
body: ticket.body,
|
|
53
|
+
channel: ticket.channel
|
|
54
|
+
})
|
|
55
|
+
}
|
|
56
|
+
]
|
|
57
|
+
}
|
|
58
|
+
], {
|
|
59
|
+
responseFormat: "json",
|
|
60
|
+
model: this.llmModel
|
|
61
|
+
});
|
|
62
|
+
const content = llmResult.message.content.find((part) => ("text" in part));
|
|
63
|
+
if (content && "text" in content) {
|
|
64
|
+
const parsed = JSON.parse(content.text);
|
|
65
|
+
return {
|
|
66
|
+
...heuristics,
|
|
67
|
+
...parsed,
|
|
68
|
+
intents: parsed.intents ?? heuristics.intents,
|
|
69
|
+
tags: parsed.tags ?? heuristics.tags
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
} catch {}
|
|
73
|
+
return heuristics;
|
|
74
|
+
}
|
|
75
|
+
heuristicClassification(ticket) {
|
|
76
|
+
const text = `${ticket.subject}
|
|
77
|
+
${ticket.body}`.toLowerCase();
|
|
78
|
+
const category = this.detectCategory(text);
|
|
79
|
+
const priority = this.detectPriority(text);
|
|
80
|
+
const sentiment = this.detectSentiment(text);
|
|
81
|
+
const intents = this.extractIntents(text);
|
|
82
|
+
const tags = intents.slice(0, 3);
|
|
83
|
+
const confidence = this.estimateConfidence(category, priority, sentiment);
|
|
84
|
+
return {
|
|
85
|
+
ticketId: ticket.id,
|
|
86
|
+
category,
|
|
87
|
+
priority,
|
|
88
|
+
sentiment,
|
|
89
|
+
intents,
|
|
90
|
+
tags,
|
|
91
|
+
confidence,
|
|
92
|
+
escalationRequired: priority === "urgent" || category === "compliance"
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
detectCategory(text) {
|
|
96
|
+
for (const [category, keywords] of Object.entries(this.keywords)) {
|
|
97
|
+
if (keywords.some((keyword) => text.includes(keyword))) {
|
|
98
|
+
return category;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return "other";
|
|
102
|
+
}
|
|
103
|
+
detectPriority(text) {
|
|
104
|
+
for (const priority of [
|
|
105
|
+
"urgent",
|
|
106
|
+
"high",
|
|
107
|
+
"medium",
|
|
108
|
+
"low"
|
|
109
|
+
]) {
|
|
110
|
+
if (PRIORITY_HINTS[priority].some((word) => text.includes(word))) {
|
|
111
|
+
return priority;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return "medium";
|
|
115
|
+
}
|
|
116
|
+
detectSentiment(text) {
|
|
117
|
+
for (const sentiment of [
|
|
118
|
+
"frustrated",
|
|
119
|
+
"negative",
|
|
120
|
+
"neutral",
|
|
121
|
+
"positive"
|
|
122
|
+
]) {
|
|
123
|
+
if (SENTIMENT_HINTS[sentiment].some((word) => text.includes(word))) {
|
|
124
|
+
return sentiment;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return "neutral";
|
|
128
|
+
}
|
|
129
|
+
extractIntents(text) {
|
|
130
|
+
const intents = [];
|
|
131
|
+
if (text.includes("refund") || text.includes("chargeback"))
|
|
132
|
+
intents.push("refund");
|
|
133
|
+
if (text.includes("payout"))
|
|
134
|
+
intents.push("payout");
|
|
135
|
+
if (text.includes("login"))
|
|
136
|
+
intents.push("login-help");
|
|
137
|
+
if (text.includes("feature"))
|
|
138
|
+
intents.push("feature-request");
|
|
139
|
+
if (text.includes("bug") || text.includes("error"))
|
|
140
|
+
intents.push("bug-report");
|
|
141
|
+
return intents.length ? intents : ["general"];
|
|
142
|
+
}
|
|
143
|
+
estimateConfidence(category, priority, sentiment) {
|
|
144
|
+
let base = 0.6;
|
|
145
|
+
if (category !== "other")
|
|
146
|
+
base += 0.1;
|
|
147
|
+
if (priority === "urgent" || priority === "low")
|
|
148
|
+
base += 0.05;
|
|
149
|
+
if (sentiment === "frustrated")
|
|
150
|
+
base -= 0.05;
|
|
151
|
+
return Math.min(0.95, Math.max(0.4, Number(base.toFixed(2))));
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
export {
|
|
155
|
+
TicketClassifier
|
|
156
|
+
};
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
// src/tickets/classifier.ts
|
|
2
|
+
var CATEGORY_KEYWORDS = {
|
|
3
|
+
billing: ["invoice", "payout", "refund", "charge", "billing", "payment"],
|
|
4
|
+
technical: ["bug", "error", "crash", "issue", "failed", "timeout"],
|
|
5
|
+
product: ["feature", "roadmap", "idea", "request", "feedback"],
|
|
6
|
+
account: ["login", "password", "2fa", "account", "profile", "email change"],
|
|
7
|
+
compliance: ["kyc", "aml", "compliance", "regulation", "gdpr"],
|
|
8
|
+
other: []
|
|
9
|
+
};
|
|
10
|
+
var PRIORITY_HINTS = {
|
|
11
|
+
urgent: ["urgent", "asap", "immediately", "today", "right away"],
|
|
12
|
+
high: ["high priority", "blocking", "major", "critical"],
|
|
13
|
+
medium: ["soon", "next few days"],
|
|
14
|
+
low: ["nice to have", "when possible", "later"]
|
|
15
|
+
};
|
|
16
|
+
var SENTIMENT_HINTS = {
|
|
17
|
+
positive: ["love", "great", "awesome", "thank you"],
|
|
18
|
+
neutral: ["question", "wonder", "curious"],
|
|
19
|
+
negative: ["unhappy", "bad", "terrible", "awful", "angry"],
|
|
20
|
+
frustrated: ["furious", "frustrated", "fed up", "ridiculous"]
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
class TicketClassifier {
|
|
24
|
+
keywords;
|
|
25
|
+
llm;
|
|
26
|
+
llmModel;
|
|
27
|
+
constructor(options) {
|
|
28
|
+
this.keywords = {
|
|
29
|
+
...CATEGORY_KEYWORDS,
|
|
30
|
+
...options?.keywords ?? {}
|
|
31
|
+
};
|
|
32
|
+
this.llm = options?.llm;
|
|
33
|
+
this.llmModel = options?.llmModel;
|
|
34
|
+
}
|
|
35
|
+
async classify(ticket) {
|
|
36
|
+
const heuristics = this.heuristicClassification(ticket);
|
|
37
|
+
if (!this.llm)
|
|
38
|
+
return heuristics;
|
|
39
|
+
try {
|
|
40
|
+
const llmResult = await this.llm.chat([
|
|
41
|
+
{
|
|
42
|
+
role: "system",
|
|
43
|
+
content: [{ type: "text", text: "Classify the support ticket." }]
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
role: "user",
|
|
47
|
+
content: [
|
|
48
|
+
{
|
|
49
|
+
type: "text",
|
|
50
|
+
text: JSON.stringify({
|
|
51
|
+
subject: ticket.subject,
|
|
52
|
+
body: ticket.body,
|
|
53
|
+
channel: ticket.channel
|
|
54
|
+
})
|
|
55
|
+
}
|
|
56
|
+
]
|
|
57
|
+
}
|
|
58
|
+
], {
|
|
59
|
+
responseFormat: "json",
|
|
60
|
+
model: this.llmModel
|
|
61
|
+
});
|
|
62
|
+
const content = llmResult.message.content.find((part) => ("text" in part));
|
|
63
|
+
if (content && "text" in content) {
|
|
64
|
+
const parsed = JSON.parse(content.text);
|
|
65
|
+
return {
|
|
66
|
+
...heuristics,
|
|
67
|
+
...parsed,
|
|
68
|
+
intents: parsed.intents ?? heuristics.intents,
|
|
69
|
+
tags: parsed.tags ?? heuristics.tags
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
} catch {}
|
|
73
|
+
return heuristics;
|
|
74
|
+
}
|
|
75
|
+
heuristicClassification(ticket) {
|
|
76
|
+
const text = `${ticket.subject}
|
|
77
|
+
${ticket.body}`.toLowerCase();
|
|
78
|
+
const category = this.detectCategory(text);
|
|
79
|
+
const priority = this.detectPriority(text);
|
|
80
|
+
const sentiment = this.detectSentiment(text);
|
|
81
|
+
const intents = this.extractIntents(text);
|
|
82
|
+
const tags = intents.slice(0, 3);
|
|
83
|
+
const confidence = this.estimateConfidence(category, priority, sentiment);
|
|
84
|
+
return {
|
|
85
|
+
ticketId: ticket.id,
|
|
86
|
+
category,
|
|
87
|
+
priority,
|
|
88
|
+
sentiment,
|
|
89
|
+
intents,
|
|
90
|
+
tags,
|
|
91
|
+
confidence,
|
|
92
|
+
escalationRequired: priority === "urgent" || category === "compliance"
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
detectCategory(text) {
|
|
96
|
+
for (const [category, keywords] of Object.entries(this.keywords)) {
|
|
97
|
+
if (keywords.some((keyword) => text.includes(keyword))) {
|
|
98
|
+
return category;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return "other";
|
|
102
|
+
}
|
|
103
|
+
detectPriority(text) {
|
|
104
|
+
for (const priority of [
|
|
105
|
+
"urgent",
|
|
106
|
+
"high",
|
|
107
|
+
"medium",
|
|
108
|
+
"low"
|
|
109
|
+
]) {
|
|
110
|
+
if (PRIORITY_HINTS[priority].some((word) => text.includes(word))) {
|
|
111
|
+
return priority;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return "medium";
|
|
115
|
+
}
|
|
116
|
+
detectSentiment(text) {
|
|
117
|
+
for (const sentiment of [
|
|
118
|
+
"frustrated",
|
|
119
|
+
"negative",
|
|
120
|
+
"neutral",
|
|
121
|
+
"positive"
|
|
122
|
+
]) {
|
|
123
|
+
if (SENTIMENT_HINTS[sentiment].some((word) => text.includes(word))) {
|
|
124
|
+
return sentiment;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return "neutral";
|
|
128
|
+
}
|
|
129
|
+
extractIntents(text) {
|
|
130
|
+
const intents = [];
|
|
131
|
+
if (text.includes("refund") || text.includes("chargeback"))
|
|
132
|
+
intents.push("refund");
|
|
133
|
+
if (text.includes("payout"))
|
|
134
|
+
intents.push("payout");
|
|
135
|
+
if (text.includes("login"))
|
|
136
|
+
intents.push("login-help");
|
|
137
|
+
if (text.includes("feature"))
|
|
138
|
+
intents.push("feature-request");
|
|
139
|
+
if (text.includes("bug") || text.includes("error"))
|
|
140
|
+
intents.push("bug-report");
|
|
141
|
+
return intents.length ? intents : ["general"];
|
|
142
|
+
}
|
|
143
|
+
estimateConfidence(category, priority, sentiment) {
|
|
144
|
+
let base = 0.6;
|
|
145
|
+
if (category !== "other")
|
|
146
|
+
base += 0.1;
|
|
147
|
+
if (priority === "urgent" || priority === "low")
|
|
148
|
+
base += 0.05;
|
|
149
|
+
if (sentiment === "frustrated")
|
|
150
|
+
base -= 0.05;
|
|
151
|
+
return Math.min(0.95, Math.max(0.4, Number(base.toFixed(2))));
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
export {
|
|
155
|
+
TicketClassifier
|
|
156
|
+
};
|
|
File without changes
|
package/dist/rag/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
export * from './ticket-resolver';
|
|
2
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/rag/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC"}
|
package/dist/rag/index.js
CHANGED
|
@@ -1,3 +1,66 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
// @bun
|
|
2
|
+
// src/rag/ticket-resolver.ts
|
|
3
|
+
class TicketResolver {
|
|
4
|
+
knowledge;
|
|
5
|
+
minConfidence;
|
|
6
|
+
prependPrompt;
|
|
7
|
+
constructor(options) {
|
|
8
|
+
this.knowledge = options.knowledge;
|
|
9
|
+
this.minConfidence = options.minConfidence ?? 0.65;
|
|
10
|
+
this.prependPrompt = options.prependPrompt;
|
|
11
|
+
}
|
|
12
|
+
async resolve(ticket) {
|
|
13
|
+
const question = this.buildQuestion(ticket);
|
|
14
|
+
const answer = await this.knowledge.query(question);
|
|
15
|
+
return this.toResolution(ticket, answer);
|
|
16
|
+
}
|
|
17
|
+
buildQuestion(ticket) {
|
|
18
|
+
const header = [`Subject: ${ticket.subject}`, `Channel: ${ticket.channel}`];
|
|
19
|
+
if (ticket.customerName)
|
|
20
|
+
header.push(`Customer: ${ticket.customerName}`);
|
|
21
|
+
const sections = [
|
|
22
|
+
this.prependPrompt,
|
|
23
|
+
header.join(`
|
|
24
|
+
`),
|
|
25
|
+
"---",
|
|
26
|
+
ticket.body
|
|
27
|
+
].filter(Boolean);
|
|
28
|
+
return sections.join(`
|
|
29
|
+
`);
|
|
30
|
+
}
|
|
31
|
+
toResolution(ticket, answer) {
|
|
32
|
+
const citations = answer.references.map((ref) => {
|
|
33
|
+
const label = typeof ref.payload?.title === "string" ? ref.payload.title : typeof ref.payload?.documentId === "string" ? ref.payload.documentId : ref.id;
|
|
34
|
+
return {
|
|
35
|
+
label,
|
|
36
|
+
url: typeof ref.payload?.url === "string" ? ref.payload.url : undefined,
|
|
37
|
+
snippet: typeof ref.payload?.text === "string" ? ref.payload.text.slice(0, 280) : undefined,
|
|
38
|
+
score: ref.score
|
|
39
|
+
};
|
|
40
|
+
});
|
|
41
|
+
const confidence = this.deriveConfidence(answer);
|
|
42
|
+
const escalate = confidence < this.minConfidence || citations.length === 0;
|
|
43
|
+
return {
|
|
44
|
+
ticketId: ticket.id,
|
|
45
|
+
answer: answer.answer,
|
|
46
|
+
confidence,
|
|
47
|
+
citations,
|
|
48
|
+
actions: [
|
|
49
|
+
escalate ? { type: "escalate", label: "Escalate for human review" } : { type: "respond", label: "Send automated response" }
|
|
50
|
+
],
|
|
51
|
+
escalationReason: escalate ? "Insufficient confidence or missing knowledge references" : undefined,
|
|
52
|
+
knowledgeUpdates: escalate ? [ticket.body.slice(0, 200)] : undefined
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
deriveConfidence(answer) {
|
|
56
|
+
if (!answer.references.length)
|
|
57
|
+
return 0.3;
|
|
58
|
+
const topScore = answer.references[0]?.score ?? 0.4;
|
|
59
|
+
const normalized = Math.min(1, Math.max(0, topScore));
|
|
60
|
+
const tokenPenalty = answer.usage?.completionTokens ? Math.min(answer.usage.completionTokens / 1000, 0.2) : 0;
|
|
61
|
+
return Number((normalized - tokenPenalty).toFixed(2));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
export {
|
|
65
|
+
TicketResolver
|
|
66
|
+
};
|
|
@@ -1,25 +1,21 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
interface KnowledgeRetriever {
|
|
6
|
-
query(question: string): Promise<KnowledgeAnswer>;
|
|
1
|
+
import type { KnowledgeAnswer } from '@contractspec/lib.knowledge/query/service';
|
|
2
|
+
import type { SupportResolution, SupportTicket } from '../types';
|
|
3
|
+
export interface KnowledgeRetriever {
|
|
4
|
+
query(question: string): Promise<KnowledgeAnswer>;
|
|
7
5
|
}
|
|
8
|
-
interface TicketResolverOptions {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
6
|
+
export interface TicketResolverOptions {
|
|
7
|
+
knowledge: KnowledgeRetriever;
|
|
8
|
+
minConfidence?: number;
|
|
9
|
+
prependPrompt?: string;
|
|
12
10
|
}
|
|
13
|
-
declare class TicketResolver {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
11
|
+
export declare class TicketResolver {
|
|
12
|
+
private readonly knowledge;
|
|
13
|
+
private readonly minConfidence;
|
|
14
|
+
private readonly prependPrompt?;
|
|
15
|
+
constructor(options: TicketResolverOptions);
|
|
16
|
+
resolve(ticket: SupportTicket): Promise<SupportResolution>;
|
|
17
|
+
private buildQuestion;
|
|
18
|
+
private toResolution;
|
|
19
|
+
private deriveConfidence;
|
|
22
20
|
}
|
|
23
|
-
//#endregion
|
|
24
|
-
export { KnowledgeRetriever, TicketResolver, TicketResolverOptions };
|
|
25
21
|
//# sourceMappingURL=ticket-resolver.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ticket-resolver.d.ts","
|
|
1
|
+
{"version":3,"file":"ticket-resolver.d.ts","sourceRoot":"","sources":["../../src/rag/ticket-resolver.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,2CAA2C,CAAC;AACjF,OAAO,KAAK,EAAE,iBAAiB,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEjE,MAAM,WAAW,kBAAkB;IACjC,KAAK,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,CAAC,CAAC;CACnD;AAED,MAAM,WAAW,qBAAqB;IACpC,SAAS,EAAE,kBAAkB,CAAC;IAC9B,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB;AAED,qBAAa,cAAc;IACzB,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAqB;IAC/C,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAS;IACvC,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAS;gBAE5B,OAAO,EAAE,qBAAqB;IAMpC,OAAO,CAAC,MAAM,EAAE,aAAa,GAAG,OAAO,CAAC,iBAAiB,CAAC;IAMhE,OAAO,CAAC,aAAa;IAYrB,OAAO,CAAC,YAAY;IA0CpB,OAAO,CAAC,gBAAgB;CASzB"}
|
|
@@ -1,64 +1,66 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
1
|
+
// @bun
|
|
2
|
+
// src/rag/ticket-resolver.ts
|
|
3
|
+
class TicketResolver {
|
|
4
|
+
knowledge;
|
|
5
|
+
minConfidence;
|
|
6
|
+
prependPrompt;
|
|
7
|
+
constructor(options) {
|
|
8
|
+
this.knowledge = options.knowledge;
|
|
9
|
+
this.minConfidence = options.minConfidence ?? 0.65;
|
|
10
|
+
this.prependPrompt = options.prependPrompt;
|
|
11
|
+
}
|
|
12
|
+
async resolve(ticket) {
|
|
13
|
+
const question = this.buildQuestion(ticket);
|
|
14
|
+
const answer = await this.knowledge.query(question);
|
|
15
|
+
return this.toResolution(ticket, answer);
|
|
16
|
+
}
|
|
17
|
+
buildQuestion(ticket) {
|
|
18
|
+
const header = [`Subject: ${ticket.subject}`, `Channel: ${ticket.channel}`];
|
|
19
|
+
if (ticket.customerName)
|
|
20
|
+
header.push(`Customer: ${ticket.customerName}`);
|
|
21
|
+
const sections = [
|
|
22
|
+
this.prependPrompt,
|
|
23
|
+
header.join(`
|
|
24
|
+
`),
|
|
25
|
+
"---",
|
|
26
|
+
ticket.body
|
|
27
|
+
].filter(Boolean);
|
|
28
|
+
return sections.join(`
|
|
29
|
+
`);
|
|
30
|
+
}
|
|
31
|
+
toResolution(ticket, answer) {
|
|
32
|
+
const citations = answer.references.map((ref) => {
|
|
33
|
+
const label = typeof ref.payload?.title === "string" ? ref.payload.title : typeof ref.payload?.documentId === "string" ? ref.payload.documentId : ref.id;
|
|
34
|
+
return {
|
|
35
|
+
label,
|
|
36
|
+
url: typeof ref.payload?.url === "string" ? ref.payload.url : undefined,
|
|
37
|
+
snippet: typeof ref.payload?.text === "string" ? ref.payload.text.slice(0, 280) : undefined,
|
|
38
|
+
score: ref.score
|
|
39
|
+
};
|
|
40
|
+
});
|
|
41
|
+
const confidence = this.deriveConfidence(answer);
|
|
42
|
+
const escalate = confidence < this.minConfidence || citations.length === 0;
|
|
43
|
+
return {
|
|
44
|
+
ticketId: ticket.id,
|
|
45
|
+
answer: answer.answer,
|
|
46
|
+
confidence,
|
|
47
|
+
citations,
|
|
48
|
+
actions: [
|
|
49
|
+
escalate ? { type: "escalate", label: "Escalate for human review" } : { type: "respond", label: "Send automated response" }
|
|
50
|
+
],
|
|
51
|
+
escalationReason: escalate ? "Insufficient confidence or missing knowledge references" : undefined,
|
|
52
|
+
knowledgeUpdates: escalate ? [ticket.body.slice(0, 200)] : undefined
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
deriveConfidence(answer) {
|
|
56
|
+
if (!answer.references.length)
|
|
57
|
+
return 0.3;
|
|
58
|
+
const topScore = answer.references[0]?.score ?? 0.4;
|
|
59
|
+
const normalized = Math.min(1, Math.max(0, topScore));
|
|
60
|
+
const tokenPenalty = answer.usage?.completionTokens ? Math.min(answer.usage.completionTokens / 1000, 0.2) : 0;
|
|
61
|
+
return Number((normalized - tokenPenalty).toFixed(2));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
export {
|
|
65
|
+
TicketResolver
|
|
60
66
|
};
|
|
61
|
-
|
|
62
|
-
//#endregion
|
|
63
|
-
export { TicketResolver };
|
|
64
|
-
//# sourceMappingURL=ticket-resolver.js.map
|
package/dist/spec.d.ts
CHANGED
|
@@ -1,13 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
tools?: AgentToolConfig[];
|
|
8
|
-
autoEscalateThreshold?: number;
|
|
1
|
+
import type { AgentSpec, AgentToolConfig } from '@contractspec/lib.ai-agent';
|
|
2
|
+
import type { SupportBotSpec } from './types';
|
|
3
|
+
export interface SupportBotDefinition {
|
|
4
|
+
base: AgentSpec;
|
|
5
|
+
tools?: AgentToolConfig[];
|
|
6
|
+
autoEscalateThreshold?: number;
|
|
9
7
|
}
|
|
10
|
-
declare function defineSupportBot(definition: SupportBotDefinition): SupportBotSpec;
|
|
11
|
-
//#endregion
|
|
12
|
-
export { SupportBotDefinition, defineSupportBot };
|
|
8
|
+
export declare function defineSupportBot(definition: SupportBotDefinition): SupportBotSpec;
|
|
13
9
|
//# sourceMappingURL=spec.d.ts.map
|
package/dist/spec.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"spec.d.ts","
|
|
1
|
+
{"version":3,"file":"spec.d.ts","sourceRoot":"","sources":["../src/spec.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAE7E,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAE9C,MAAM,WAAW,oBAAoB;IACnC,IAAI,EAAE,SAAS,CAAC;IAChB,KAAK,CAAC,EAAE,eAAe,EAAE,CAAC;IAC1B,qBAAqB,CAAC,EAAE,MAAM,CAAC;CAChC;AAED,wBAAgB,gBAAgB,CAC9B,UAAU,EAAE,oBAAoB,GAC/B,cAAc,CA8BhB"}
|
package/dist/spec.js
CHANGED
|
@@ -1,35 +1,34 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/spec.ts
|
|
1
3
|
import { defineAgent } from "@contractspec/lib.ai-agent";
|
|
2
|
-
|
|
3
|
-
//#region src/spec.ts
|
|
4
4
|
function defineSupportBot(definition) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
ttlMinutes: 120
|
|
22
|
-
},
|
|
23
|
-
tools: definition.tools ?? definition.base.tools,
|
|
24
|
-
instructions: `${definition.base.instructions}\n\nAlways cite support knowledge sources and flag compliance/billing issues for human review when unsure.`
|
|
25
|
-
}),
|
|
26
|
-
thresholds: {
|
|
27
|
-
autoResolveMinConfidence: definition.autoEscalateThreshold ?? .75,
|
|
28
|
-
maxIterations: 6
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
|
-
}
|
|
5
|
+
const base = defineAgent({
|
|
6
|
+
...definition.base,
|
|
7
|
+
policy: {
|
|
8
|
+
...definition.base.policy,
|
|
9
|
+
confidence: {
|
|
10
|
+
min: definition.base.policy?.confidence?.min ?? 0.7,
|
|
11
|
+
default: definition.base.policy?.confidence?.default ?? 0.6
|
|
12
|
+
},
|
|
13
|
+
escalation: {
|
|
14
|
+
confidenceThreshold: definition.autoEscalateThreshold ?? definition.base.policy?.escalation?.confidenceThreshold ?? definition.base.policy?.confidence?.min ?? 0.7,
|
|
15
|
+
...definition.base.policy?.escalation
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
memory: definition.base.memory ?? { maxEntries: 120, ttlMinutes: 120 },
|
|
19
|
+
tools: definition.tools ?? definition.base.tools,
|
|
20
|
+
instructions: `${definition.base.instructions}
|
|
32
21
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
22
|
+
Always cite support knowledge sources and flag compliance/billing issues for human review when unsure.`
|
|
23
|
+
});
|
|
24
|
+
return {
|
|
25
|
+
...base,
|
|
26
|
+
thresholds: {
|
|
27
|
+
autoResolveMinConfidence: definition.autoEscalateThreshold ?? 0.75,
|
|
28
|
+
maxIterations: 6
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
export {
|
|
33
|
+
defineSupportBot
|
|
34
|
+
};
|