Repository navigation
Expand file tree
/
Copy pathai-runtime.ts
More file actions
224 lines (218 loc) · 7.22 KB
/
Copy pathai-runtime.ts
File metadata and controls
224 lines (218 loc) · 7.22 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import { createRecordingClock } from "./ai-session-clock";
import type { AudioInputSource, EvenHubEvent } from "@evenrealities/even_hub_sdk";
import {
addDailyAiUsage,
costSummaryForCurrentPeriod,
resolveAiUsageLedger,
writeAiUsageLedger,
} from "./ai-cost";
import {
appendAiConversationExcerpt,
resolveAiConversationHistory,
writeAiConversationHistory,
} from "./ai-history";
import {
type AiHudSnapshot,
createAiHudSnapshot,
updateAiHudProtocol,
} from "./ai-hud-state";
import { createAiRealtimeSession } from "./ai-realtime-session";
import type { AiRealtimeProtocolState } from "./ai-realtime-protocol";
import { createAiPresentationPacer } from "./ai-presentation-pacer";
import { createAiRefreshScheduler } from "./ai-refresh-scheduler";
import type { EvenStorage } from "./live-cache";
import type { DataState, LocationValue } from "./live-state";
import { resolveMcpServers } from "./mcp-servers";
import type { PhoneLocale } from "./phone-types";
type AiBridge = EvenStorage & {
audioControl(isOpen: boolean, source?: AudioInputSource): Promise<boolean>;
onEvenHubEvent(listener: (event: EvenHubEvent) => void): () => void;
};
export type AiRuntime = {
start(): Promise<boolean>;
hold?(pressed: boolean): void;
interrupt(): Promise<void>;
stop(): Promise<void>;
dispose(): void;
};
export function createAiRuntime(options: {
readonly bridge: AiBridge;
readonly getKey: () => string | undefined;
readonly getLocale: () => PhoneLocale;
readonly getLocation?: () => DataState<LocationValue>;
readonly getSnapshot: () => AiHudSnapshot;
readonly onSnapshot: (snapshot: AiHudSnapshot) => void;
readonly refresh: () => void | Promise<void>;
readonly getPresentationIntervalMs?: () => number;
readonly createSession?: typeof createAiRealtimeSession;
}): AiRuntime {
let session: ReturnType<typeof createAiRealtimeSession> | undefined;
let disposed = false;
let epoch = 0;
let stopping: Promise<void> | undefined;
const scheduler = createAiRefreshScheduler(options.refresh, 100);
const clock = createRecordingClock(() => {
if (disposed) return;
options.onSnapshot({ ...options.getSnapshot(), recording: clock.get() }); scheduler.request();
});
const publish = (
snapshot: AiHudSnapshot,
final = false,
waitForPresentation = false,
) => {
if (disposed) return;
options.onSnapshot({ ...snapshot, recording: clock.get() });
if (final || waitForPresentation) return scheduler.final();
scheduler.request();
};
const pacer = createAiPresentationPacer({
onFrame: publish,
getIntervalMs: options.getPresentationIntervalMs,
graphemesPerTick: Number.MAX_SAFE_INTEGER,
});
const persist = async (
protocol: AiRealtimeProtocolState,
) => {
const now = new Date();
const currentTurn = {
user: protocol.userText.trim(),
assistant: protocol.assistantText.trim(),
};
const latestCompleted = [...protocol.turns].reverse().find(
(turn) => turn.user.trim() || turn.assistant.trim(),
);
const excerpt = currentTurn.user || currentTurn.assistant
? currentTurn
: latestCompleted;
const [history, ledger] = await Promise.all([
resolveAiConversationHistory(options.bridge),
resolveAiUsageLedger(options.bridge),
]);
const nextHistory = excerpt?.user || excerpt?.assistant
? appendAiConversationExcerpt(history, {
id: `${now.getTime()}-${Math.random().toString(36).slice(2, 8)}`,
endedAt: now.toISOString(),
user: excerpt.user,
assistant: excerpt.assistant,
sources: protocol.sources,
})
: history;
const nextLedger = addDailyAiUsage(
ledger,
now,
protocol.usage,
protocol.charge,
);
const costs = costSummaryForCurrentPeriod(nextLedger, now);
await Promise.all([
writeAiConversationHistory(options.bridge, nextHistory),
writeAiUsageLedger(options.bridge, nextLedger),
]);
publish(createAiHudSnapshot(
Boolean(options.getKey()),
nextHistory,
costs.weekUsd,
costs.monthUsd,
costs.hasUnpricedUsage,
), true);
};
return {
hold(pressed) {
const active = session;
if (!active?.capture) return;
if (pressed) void active.capture.begin(); else active.capture.end();
},
async start() {
if (disposed || session || stopping) return false;
const owner = ++epoch;
clock.reset();
const key = options.getKey();
if (!key) {
publish({
...options.getSnapshot(),
configured: false,
phase: "unconfigured",
error: "OpenAI key required",
}, true);
return false;
}
const mcpServers = await resolveMcpServers(options.bridge);
if (disposed || session || stopping || owner !== epoch) return false;
session = (options.createSession ?? createAiRealtimeSession)({
bridge: options.bridge,
captureMode: "hold",
key,
locale: options.getLocale(),
getLocation: options.getLocation,
mcpServers,
onState: (protocol) => {
if (disposed || owner !== epoch) return;
if (protocol.captureState) {
clock.watch();
if (protocol.captureState === "recording") clock.resume(); else clock.pause();
} else if (protocol.phase === "listening" && !clock.get()) clock.start();
if (protocol.phase === "error" || protocol.phase === "idle") clock.stop();
const snapshot = updateAiHudProtocol(
options.getSnapshot(),
protocol,
);
if (protocol.phase === "error") {
pacer.reset();
publish(snapshot, true);
} else if (protocol.pendingApproval) {
pacer.push(snapshot);
void pacer.flush();
} else {
pacer.push(snapshot);
}
},
});
try {
await session.start();
return owner === epoch && !disposed;
} catch {
if (owner === epoch) session = undefined;
return false;
}
},
async interrupt() {
const active = session;
if (disposed || !active) return;
if (active.approvePendingMcp?.()) return;
const current = active.getState();
const protocol = current.phase === "thinking"
? active.cancelResponse()
: current;
pacer.push(updateAiHudProtocol(options.getSnapshot(), protocol));
await pacer.flush();
},
async stop() {
if (stopping) return stopping;
clock.reset();
epoch += 1;
pacer.reset();
const active = session;
session = undefined;
if (!active) return;
publish({ ...options.getSnapshot(), phase: "idle", activeTool: undefined,
pendingApproval: undefined, responseComplete: false, canRevealFullResponse: false, error: undefined });
stopping = Promise.resolve().then(async () => {
try {
const protocol = await active.stop();
await persist(protocol);
} finally { stopping = undefined; }
});
return stopping;
},
dispose() {
clock.reset();
disposed = true;
epoch += 1;
pacer.dispose();
scheduler.dispose();
const active = session;
session = undefined;
void active?.stop();
},
};
}