@operor/core 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,74 @@
1
+ import type { Intent, IntentClassifier, AgentConfig, ConversationMessage } from './types.js';
2
+
3
+ /**
4
+ * Default keyword-based intent classifier.
5
+ * Uses simple keyword matching to classify user messages.
6
+ */
7
+ export class KeywordIntentClassifier implements IntentClassifier {
8
+ async classify(
9
+ message: string,
10
+ agents: AgentConfig[],
11
+ history?: ConversationMessage[]
12
+ ): Promise<Intent> {
13
+ const lowerText = message.toLowerCase();
14
+
15
+ // Try to match against agent triggers
16
+ for (const agent of agents) {
17
+ if (!agent.triggers) continue;
18
+
19
+ for (const trigger of agent.triggers) {
20
+ if (lowerText.includes(trigger.toLowerCase())) {
21
+ return {
22
+ intent: trigger,
23
+ confidence: 0.9,
24
+ entities: {},
25
+ };
26
+ }
27
+ }
28
+ }
29
+
30
+ // Fallback keyword matching
31
+ if (
32
+ lowerText.includes('order') ||
33
+ lowerText.includes('tracking') ||
34
+ lowerText.includes('where is') ||
35
+ lowerText.includes('delivery')
36
+ ) {
37
+ return {
38
+ intent: 'order_tracking',
39
+ confidence: 0.95,
40
+ entities: {},
41
+ };
42
+ }
43
+
44
+ if (
45
+ lowerText.includes('product') ||
46
+ lowerText.includes('buy') ||
47
+ lowerText.includes('price')
48
+ ) {
49
+ return {
50
+ intent: 'product_inquiry',
51
+ confidence: 0.9,
52
+ entities: {},
53
+ };
54
+ }
55
+
56
+ if (
57
+ lowerText.includes('help') ||
58
+ lowerText.includes('support') ||
59
+ lowerText.includes('problem')
60
+ ) {
61
+ return {
62
+ intent: 'support',
63
+ confidence: 0.85,
64
+ entities: {},
65
+ };
66
+ }
67
+
68
+ return {
69
+ intent: 'general',
70
+ confidence: 0.5,
71
+ entities: {},
72
+ };
73
+ }
74
+ }
@@ -0,0 +1,68 @@
1
+ import type { Intent, IntentClassifier, AgentConfig, ConversationMessage } from './types.js';
2
+
3
+ /**
4
+ * LLM-based intent classifier.
5
+ * Uses an LLM to classify user messages based on agent configurations.
6
+ * Requires an LLM provider (AIProvider from @operor/llm).
7
+ */
8
+ export class LLMIntentClassifier implements IntentClassifier {
9
+ constructor(private llm: any) {}
10
+
11
+ async classify(
12
+ message: string,
13
+ agents: AgentConfig[],
14
+ history?: ConversationMessage[]
15
+ ): Promise<Intent> {
16
+ const agentDescriptions = agents
17
+ .map(
18
+ (a) =>
19
+ `- ${a.name}: triggers=[${a.triggers?.join(', ')}], purpose="${a.purpose || 'N/A'}"`
20
+ )
21
+ .join('\n');
22
+
23
+ const systemPrompt = `You are an intent classifier for a customer service system. Classify the user message into one of these agent categories:
24
+
25
+ ${agentDescriptions}
26
+
27
+ Respond with JSON only (no markdown, no code blocks):
28
+ {
29
+ "intent": "trigger_name",
30
+ "confidence": 0.0-1.0,
31
+ "entities": {}
32
+ }
33
+
34
+ Choose the most appropriate trigger from the list above. If none match well, use "general".`;
35
+
36
+ try {
37
+ const response = await this.llm.complete(
38
+ [
39
+ { role: 'system', content: systemPrompt },
40
+ { role: 'user', content: message },
41
+ ],
42
+ { maxTokens: 150, temperature: 0 }
43
+ );
44
+
45
+ // Parse JSON response
46
+ const text = response.text.trim();
47
+ const jsonMatch = text.match(/\{[\s\S]*\}/);
48
+ if (!jsonMatch) {
49
+ throw new Error('No JSON found in LLM response');
50
+ }
51
+
52
+ const parsed = JSON.parse(jsonMatch[0]);
53
+ return {
54
+ intent: parsed.intent || 'general',
55
+ confidence: parsed.confidence || 0.5,
56
+ entities: parsed.entities || {},
57
+ };
58
+ } catch (error) {
59
+ console.error('LLM intent classification failed:', error);
60
+ // Fallback to general intent
61
+ return {
62
+ intent: 'general',
63
+ confidence: 0.3,
64
+ entities: {},
65
+ };
66
+ }
67
+ }
68
+ }