@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.
Files changed (62) hide show
  1. package/dist/bot/auto-responder.d.ts +19 -23
  2. package/dist/bot/auto-responder.d.ts.map +1 -1
  3. package/dist/bot/auto-responder.js +86 -68
  4. package/dist/bot/feedback-loop.d.ts +13 -17
  5. package/dist/bot/feedback-loop.d.ts.map +1 -1
  6. package/dist/bot/feedback-loop.js +38 -34
  7. package/dist/bot/index.d.ts +4 -4
  8. package/dist/bot/index.d.ts.map +1 -0
  9. package/dist/bot/index.js +268 -4
  10. package/dist/bot/tools.d.ts +9 -13
  11. package/dist/bot/tools.d.ts.map +1 -1
  12. package/dist/bot/tools.js +118 -123
  13. package/dist/browser/bot/auto-responder.js +101 -0
  14. package/dist/browser/bot/feedback-loop.js +38 -0
  15. package/dist/browser/bot/index.js +268 -0
  16. package/dist/browser/bot/tools.js +131 -0
  17. package/dist/browser/index.js +517 -0
  18. package/dist/browser/rag/index.js +65 -0
  19. package/dist/browser/rag/ticket-resolver.js +65 -0
  20. package/dist/browser/spec.js +33 -0
  21. package/dist/browser/tickets/classifier.js +156 -0
  22. package/dist/browser/tickets/index.js +156 -0
  23. package/dist/browser/types.js +0 -0
  24. package/dist/index.d.ts +6 -11
  25. package/dist/index.d.ts.map +1 -0
  26. package/dist/index.js +518 -9
  27. package/dist/node/bot/auto-responder.js +101 -0
  28. package/dist/node/bot/feedback-loop.js +38 -0
  29. package/dist/node/bot/index.js +268 -0
  30. package/dist/node/bot/tools.js +131 -0
  31. package/dist/node/index.js +517 -0
  32. package/dist/node/rag/index.js +65 -0
  33. package/dist/node/rag/ticket-resolver.js +65 -0
  34. package/dist/node/spec.js +33 -0
  35. package/dist/node/tickets/classifier.js +156 -0
  36. package/dist/node/tickets/index.js +156 -0
  37. package/dist/node/types.js +0 -0
  38. package/dist/rag/index.d.ts +2 -2
  39. package/dist/rag/index.d.ts.map +1 -0
  40. package/dist/rag/index.js +66 -3
  41. package/dist/rag/ticket-resolver.d.ts +17 -21
  42. package/dist/rag/ticket-resolver.d.ts.map +1 -1
  43. package/dist/rag/ticket-resolver.js +65 -63
  44. package/dist/spec.d.ts +7 -11
  45. package/dist/spec.d.ts.map +1 -1
  46. package/dist/spec.js +31 -32
  47. package/dist/tickets/classifier.d.ts +18 -22
  48. package/dist/tickets/classifier.d.ts.map +1 -1
  49. package/dist/tickets/classifier.js +153 -195
  50. package/dist/tickets/index.d.ts +2 -2
  51. package/dist/tickets/index.d.ts.map +1 -0
  52. package/dist/tickets/index.js +156 -2
  53. package/dist/types.d.ts +62 -66
  54. package/dist/types.d.ts.map +1 -1
  55. package/dist/types.js +1 -0
  56. package/package.json +127 -38
  57. package/dist/bot/auto-responder.js.map +0 -1
  58. package/dist/bot/feedback-loop.js.map +0 -1
  59. package/dist/bot/tools.js.map +0 -1
  60. package/dist/rag/ticket-resolver.js.map +0 -1
  61. package/dist/spec.js.map +0 -1
  62. 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
@@ -1,2 +1,2 @@
1
- import { KnowledgeRetriever, TicketResolver, TicketResolverOptions } from "./ticket-resolver.js";
2
- export { KnowledgeRetriever, TicketResolver, TicketResolverOptions };
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
- import { TicketResolver } from "./ticket-resolver.js";
2
-
3
- export { TicketResolver };
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 { SupportResolution, SupportTicket } from "../types.js";
2
- import { KnowledgeAnswer } from "@contractspec/lib.knowledge/query/service";
3
-
4
- //#region src/rag/ticket-resolver.d.ts
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
- knowledge: KnowledgeRetriever;
10
- minConfidence?: number;
11
- prependPrompt?: string;
6
+ export interface TicketResolverOptions {
7
+ knowledge: KnowledgeRetriever;
8
+ minConfidence?: number;
9
+ prependPrompt?: string;
12
10
  }
13
- declare class TicketResolver {
14
- private readonly knowledge;
15
- private readonly minConfidence;
16
- private readonly prependPrompt?;
17
- constructor(options: TicketResolverOptions);
18
- resolve(ticket: SupportTicket): Promise<SupportResolution>;
19
- private buildQuestion;
20
- private toResolution;
21
- private deriveConfidence;
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","names":[],"sources":["../../src/rag/ticket-resolver.ts"],"mappings":";;;;UAGiB,kBAAA;EACf,KAAA,CAAM,QAAA,WAAmB,OAAA,CAAQ,eAAA;AAAA;AAAA,UAGlB,qBAAA;EACf,SAAA,EAAW,kBAAA;EACX,aAAA;EACA,aAAA;AAAA;AAAA,cAGW,cAAA;EAAA,iBACM,SAAA;EAAA,iBACA,aAAA;EAAA,iBACA,aAAA;cAEL,OAAA,EAAS,qBAAA;EAMf,OAAA,CAAQ,MAAA,EAAQ,aAAA,GAAgB,OAAA,CAAQ,iBAAA;EAAA,QAMtC,aAAA;EAAA,QAYA,YAAA;EAAA,QA0CA,gBAAA;AAAA"}
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
- //#region src/rag/ticket-resolver.ts
2
- var TicketResolver = class {
3
- knowledge;
4
- minConfidence;
5
- prependPrompt;
6
- constructor(options) {
7
- this.knowledge = options.knowledge;
8
- this.minConfidence = options.minConfidence ?? .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) header.push(`Customer: ${ticket.customerName}`);
19
- return [
20
- this.prependPrompt,
21
- header.join("\n"),
22
- "---",
23
- ticket.body
24
- ].filter(Boolean).join("\n");
25
- }
26
- toResolution(ticket, answer) {
27
- const citations = answer.references.map((ref) => {
28
- return {
29
- label: typeof ref.payload?.title === "string" ? ref.payload.title : typeof ref.payload?.documentId === "string" ? ref.payload.documentId : ref.id,
30
- url: typeof ref.payload?.url === "string" ? ref.payload.url : void 0,
31
- snippet: typeof ref.payload?.text === "string" ? ref.payload.text.slice(0, 280) : void 0,
32
- score: ref.score
33
- };
34
- });
35
- const confidence = this.deriveConfidence(answer);
36
- const escalate = confidence < this.minConfidence || citations.length === 0;
37
- return {
38
- ticketId: ticket.id,
39
- answer: answer.answer,
40
- confidence,
41
- citations,
42
- actions: [escalate ? {
43
- type: "escalate",
44
- label: "Escalate for human review"
45
- } : {
46
- type: "respond",
47
- label: "Send automated response"
48
- }],
49
- escalationReason: escalate ? "Insufficient confidence or missing knowledge references" : void 0,
50
- knowledgeUpdates: escalate ? [ticket.body.slice(0, 200)] : void 0
51
- };
52
- }
53
- deriveConfidence(answer) {
54
- if (!answer.references.length) return .3;
55
- const topScore = answer.references[0]?.score ?? .4;
56
- const normalized = Math.min(1, Math.max(0, topScore));
57
- const tokenPenalty = answer.usage?.completionTokens ? Math.min(answer.usage.completionTokens / 1e3, .2) : 0;
58
- return Number((normalized - tokenPenalty).toFixed(2));
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 { SupportBotSpec } from "./types.js";
2
- import { AgentSpec, AgentToolConfig } from "@contractspec/lib.ai-agent";
3
-
4
- //#region src/spec.d.ts
5
- interface SupportBotDefinition {
6
- base: AgentSpec;
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
@@ -1 +1 @@
1
- {"version":3,"file":"spec.d.ts","names":[],"sources":["../src/spec.ts"],"mappings":";;;;UAIiB,oBAAA;EACf,IAAA,EAAM,SAAA;EACN,KAAA,GAAQ,eAAA;EACR,qBAAA;AAAA;AAAA,iBAGc,gBAAA,CACd,UAAA,EAAY,oBAAA,GACX,cAAA"}
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
- return {
6
- ...defineAgent({
7
- ...definition.base,
8
- policy: {
9
- ...definition.base.policy,
10
- confidence: {
11
- min: definition.base.policy?.confidence?.min ?? .7,
12
- default: definition.base.policy?.confidence?.default ?? .6
13
- },
14
- escalation: {
15
- confidenceThreshold: definition.autoEscalateThreshold ?? definition.base.policy?.escalation?.confidenceThreshold ?? definition.base.policy?.confidence?.min ?? .7,
16
- ...definition.base.policy?.escalation
17
- }
18
- },
19
- memory: definition.base.memory ?? {
20
- maxEntries: 120,
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
- //#endregion
34
- export { defineSupportBot };
35
- //# sourceMappingURL=spec.js.map
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
+ };