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
|
import type { Extension, HostAPI, Manifest } from "@dispatch/kernel";
import {
cacheWarmHandle,
conversationClosed,
turnSettled,
turnStarted,
warmCompleted,
} from "@dispatch/session-orchestrator";
import type { SurfaceContext, SurfaceProvider } from "@dispatch/surface-registry";
import { surfaceRegistryHandle } from "@dispatch/surface-registry";
import type { SurfaceSpec } from "@dispatch/ui-contract";
import {
buildConversationSpec,
buildDefaultSpec,
parseIntervalPayload,
secondsToMs,
} from "./pure.js";
import { createCacheWarmer } from "./warmer.js";
export const manifest: Manifest = {
id: "cache-warming",
name: "Cache Warming",
version: "0.0.0",
apiVersion: "^0.1.0",
trust: "bundled",
activation: "eager",
dependsOn: ["session-orchestrator", "surface-registry"],
capabilities: {},
contributes: {
services: ["cache-warming/surface"],
},
};
export function activate(host: HostAPI): void {
const warmService = host.getService(cacheWarmHandle);
const registry = host.getService(surfaceRegistryHandle);
const storage = host.storage("cache-warming");
const subscribers = new Set<() => void>();
const timeoutMap = new Map<number, ReturnType<typeof setTimeout>>();
let nextTimerId = 1;
const warmer = createCacheWarmer({
warm: warmService.warm,
storage,
logger: host.logger,
timers: {
setTimer(fn, ms) {
const id = nextTimerId++;
timeoutMap.set(id, setTimeout(fn, ms));
return id;
},
clearTimer(id) {
const timeout = timeoutMap.get(id);
if (timeout !== undefined) {
clearTimeout(timeout);
timeoutMap.delete(id);
}
},
},
now: () => Date.now(),
onSurfaceChange: () => {
for (const notify of subscribers) {
notify();
}
},
});
host.on(turnStarted, (payload) => {
warmer.onTurnStarted(payload.conversationId);
});
host.on(turnSettled, (payload) => {
warmer.onTurnSettled(payload.conversationId, {
...(payload.cwd !== undefined ? { cwd: payload.cwd } : {}),
...(payload.modelName !== undefined ? { modelName: payload.modelName } : {}),
});
});
host.on(warmCompleted, (payload) => {
warmer.onWarmCompleted(payload);
});
host.on(conversationClosed, (payload) => {
// Sync part (cancel + disable) runs before the first await inside;
// only the settings persist is deferred.
void warmer.onConversationClosed(payload.conversationId);
});
function getSpec(context?: SurfaceContext): SurfaceSpec {
const convId = context?.conversationId;
if (convId === undefined) {
return buildDefaultSpec();
}
const state = warmer.getState(convId);
return buildConversationSpec(
state.enabled,
state.intervalMs,
state.lastPct,
state.lastExpectedPct,
state.nextWarmAt,
state.lastWarmAt,
);
}
async function invoke(
actionId: string,
payload?: unknown,
context?: SurfaceContext,
): Promise<void> {
const convId = context?.conversationId;
if (convId === undefined) return;
if (actionId === "cache-warming/toggle") {
const current = warmer.getState(convId);
await warmer.setEnabled(convId, !current.enabled);
}
if (actionId === "cache-warming/set-interval") {
const seconds = parseIntervalPayload(payload);
if (seconds === null) return;
const ms = secondsToMs(seconds);
if (ms === null) return;
await warmer.setIntervalMs(convId, ms);
}
}
const provider: SurfaceProvider = {
catalogEntry: {
id: "cache-warming",
region: "side",
title: "Cache Warming",
scope: "conversation",
},
getSpec,
invoke,
subscribe(onChange) {
subscribers.add(onChange);
return () => {
subscribers.delete(onChange);
};
},
};
registry.register(provider);
host.logger.info("cache-warming: registered");
}
export const extension: Extension = {
manifest,
activate,
};
|