forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesktopIntentRouter.ts
More file actions
150 lines (137 loc) · 4.74 KB
/
Copy pathdesktopIntentRouter.ts
File metadata and controls
150 lines (137 loc) · 4.74 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Desktop intent router — Windows port of the macOS agent runtime's
// desktop-intent-router.ts (desktop/macos/agent/src/runtime/desktop-intent-router.ts).
//
// Pure, deterministic classifier. Given an utterance plus the current action
// queue and session candidates, it decides how the coordinator should route the
// turn: resolve a blocking dispatch, resume a healthy related session, fork off a
// stale/failed one, answer from local state, delegate long-running work, or start
// a new run. The kernel injects the result into the main-chat prompt as untrusted
// routing metadata (see turnContext.ts).
//
// Note this is the regex router, NOT the bar's Haiku chat-vs-agent classifier —
// they are different components that both live under "intent routing".
import type { DesktopActionQueueItem } from './desktopActionQueue'
export type DesktopIntentRouteKind =
| 'quick_answer'
| 'resume'
| 'fork'
| 'delegate'
| 'dispatch'
| 'new_run'
export interface DesktopIntentSessionCandidate {
sessionId: string
runId?: string | null
surfaceKind: string
taskId?: string | null
title?: string | null
status: 'healthy' | 'stale' | 'failed' | 'orphaned' | 'closed'
relevance: number
lastActivityAtMs: number
}
export interface DesktopIntentRouteInput {
utterance: string
surfaceKind: string
taskId?: string | null
nowMs: number
actionQueue?: readonly DesktopActionQueueItem[]
sessionCandidates?: readonly DesktopIntentSessionCandidate[]
}
export interface DesktopIntentRoute {
intent: DesktopIntentRouteKind
explanation: string
sessionId?: string
runId?: string | null
dispatchId?: string
queueItemId?: string
}
function isExternalSendAmbiguous(utterance: string): boolean {
return (
/\b(send|email|post|share|submit)\b/i.test(utterance) &&
!/\b(draft|prepare|do not send|don't send)\b/i.test(utterance)
)
}
function isQuickAnswer(utterance: string): boolean {
return /\b(status|what'?s running|list agents|open loops|what happened)\b/i.test(utterance)
}
function isLongRunning(utterance: string): boolean {
return /\b(research|implement|build|fix|test|audit|review|refactor|investigate|long[- ]running|background)\b/i.test(
utterance
)
}
function chooseCandidate(
input: DesktopIntentRouteInput
): DesktopIntentSessionCandidate | undefined {
const longRunningNewWork = isLongRunning(input.utterance)
const candidates = [...(input.sessionCandidates ?? [])].sort(
(left, right) =>
right.relevance - left.relevance || right.lastActivityAtMs - left.lastActivityAtMs
)
return candidates.find((candidate) => {
if (candidate.relevance < 0.55) return false
if (input.taskId) return candidate.taskId === input.taskId
if (longRunningNewWork && candidate.surfaceKind === input.surfaceKind) return false
return candidate.surfaceKind === input.surfaceKind || candidate.taskId === input.taskId
})
}
export function routeDesktopIntent(input: DesktopIntentRouteInput): DesktopIntentRoute {
const dispatchItem = (input.actionQueue ?? []).find((item) => item.kind === 'dispatch')
if (dispatchItem) {
return {
intent: 'dispatch',
dispatchId: dispatchItem.subjectId,
queueItemId: dispatchItem.itemId,
explanation: 'A pending dispatch must be resolved before routing additional local agent work.'
}
}
if (isExternalSendAmbiguous(input.utterance)) {
return {
intent: 'dispatch',
explanation: 'External send/share intent is ambiguous and requires a durable dispatch.'
}
}
const candidate = chooseCandidate(input)
if (candidate) {
if (candidate.status === 'healthy') {
return {
intent: 'resume',
sessionId: candidate.sessionId,
runId: candidate.runId ?? null,
explanation: 'The request matches a healthy recent session on the same task or surface.'
}
}
if (
candidate.status === 'stale' ||
candidate.status === 'failed' ||
candidate.status === 'orphaned'
) {
return {
intent: 'fork',
sessionId: candidate.sessionId,
runId: candidate.runId ?? null,
explanation:
'Related prior context is useful, but the existing run is stale or failed, so isolate follow-up work.'
}
}
}
if (isQuickAnswer(input.utterance)) {
return {
intent: 'quick_answer',
explanation: 'The request can be answered from local coordinator state.'
}
}
if (isLongRunning(input.utterance)) {
return {
intent: 'delegate',
explanation: 'The request appears to require long-running or specialist work.'
}
}
return {
intent: 'new_run',
explanation: 'No reusable session or blocking dispatch matched the request.'
}
}
export const desktopIntentRouterInternals = {
isExternalSendAmbiguous,
isQuickAnswer,
isLongRunning
}