@agy-daddy/plugin-pet 0.0.0-stage → 1.1.2
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.
- package/LICENSE +21 -0
- package/README.md +10 -2
- package/agy-plugin.json +102 -0
- package/locales/en.json +10 -0
- package/locales/zh-CN.json +10 -0
- package/package.json +28 -4
- package/settings.schema.json +136 -0
- package/src/node.js +620 -0
- package/src/renderer.js +39 -0
- package/src/session-watch-aggregator.js +271 -0
- package/src/shared/pet-canvas-stage.d.ts +36 -0
- package/src/shared/pet-canvas-stage.js +218 -0
- package/src/shared/pet-rig-evaluator.js +172 -0
- package/src/shared/themes.d.ts +20 -0
- package/src/shared/themes.js +456 -0
- package/src/theme-pack-sandbox.js +501 -0
package/src/node.js
ADDED
|
@@ -0,0 +1,620 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const path = require('node:path');
|
|
4
|
+
const { getBuiltinThemes, getBuiltinThemeById } = require('./shared/themes');
|
|
5
|
+
const { mapTpsToMotion, createSpeedSmoother } = require('./shared/pet-rig-evaluator');
|
|
6
|
+
const {
|
|
7
|
+
isSessionWatched,
|
|
8
|
+
resolveSessionOrgInfo,
|
|
9
|
+
classifyStepAttention,
|
|
10
|
+
TailStepTpsEstimator
|
|
11
|
+
} = require('./session-watch-aggregator');
|
|
12
|
+
const {
|
|
13
|
+
importThemePack,
|
|
14
|
+
listCustomThemes,
|
|
15
|
+
deleteCustomTheme
|
|
16
|
+
} = require('./theme-pack-sandbox');
|
|
17
|
+
|
|
18
|
+
let activeHost = null;
|
|
19
|
+
let disposers = [];
|
|
20
|
+
|
|
21
|
+
function getCustomThemesDir(host) {
|
|
22
|
+
if (host.storage?.dir) {
|
|
23
|
+
return path.join(host.storage.dir, 'custom-themes');
|
|
24
|
+
}
|
|
25
|
+
const stateDir = process.env.AGY_DADDY_STATE_DIR || path.join(process.cwd(), '.agy-pet-data');
|
|
26
|
+
return path.join(stateDir, 'plugin-data', host.pluginId, 'custom-themes');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function normalizeSettings(raw = {}) {
|
|
30
|
+
return {
|
|
31
|
+
enabled: raw.enabled !== false,
|
|
32
|
+
desktopVisible: raw.desktopVisible !== false,
|
|
33
|
+
webVisible: raw.webVisible !== false,
|
|
34
|
+
themeId: String(raw.themeId || 'daddy-orb').trim() || 'daddy-orb',
|
|
35
|
+
petScale: Math.min(2.0, Math.max(0.6, Number(raw.petScale || 1.0))),
|
|
36
|
+
roamMode: ['none', 'bottom-edge', 'screen-edges', 'free-workarea'].includes(raw.roamMode)
|
|
37
|
+
? raw.roamMode
|
|
38
|
+
: 'bottom-edge',
|
|
39
|
+
followCurrentSession: raw.followCurrentSession !== false,
|
|
40
|
+
excludeCurrentSession: raw.excludeCurrentSession === true,
|
|
41
|
+
projectWhitelist: Array.isArray(raw.projectWhitelist) ? raw.projectWhitelist.map(String).filter(Boolean) : [],
|
|
42
|
+
groupWhitelist: Array.isArray(raw.groupWhitelist) ? raw.groupWhitelist.map(String).filter(Boolean) : []
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async function activate(host) {
|
|
47
|
+
activeHost = host;
|
|
48
|
+
const customThemesDir = getCustomThemesDir(host);
|
|
49
|
+
const smoother = createSpeedSmoother(0.35);
|
|
50
|
+
const tpsEstimator = new TailStepTpsEstimator(3000);
|
|
51
|
+
const ackedBubbleIds = new Set();
|
|
52
|
+
const lastAckedStepBySession = new Map();
|
|
53
|
+
const stepCacheBySession = new Map();
|
|
54
|
+
const lastErrorBySession = new Map();
|
|
55
|
+
|
|
56
|
+
let overlayHandle = null;
|
|
57
|
+
let overlaySupported = { supported: false, backend: 'none' };
|
|
58
|
+
let liveContextState = null;
|
|
59
|
+
let contextSnapshotState = null;
|
|
60
|
+
let sessionOrgState = null;
|
|
61
|
+
let readStateSnapshot = {};
|
|
62
|
+
let activeSessionState = {
|
|
63
|
+
desktopSessionId: null,
|
|
64
|
+
desktopBusy: false,
|
|
65
|
+
webSessionId: null,
|
|
66
|
+
activeSurface: 'desktop',
|
|
67
|
+
updatedAt: 0
|
|
68
|
+
};
|
|
69
|
+
let trajectorySummaries = {};
|
|
70
|
+
let lastActiveAt = Date.now();
|
|
71
|
+
let lastBroadcastJson = '';
|
|
72
|
+
let pollTimer = null;
|
|
73
|
+
let tailTimer = null;
|
|
74
|
+
|
|
75
|
+
const savedPersisted = host.storage?.readJson?.('pet-ui.json', {})?.data || {};
|
|
76
|
+
let webAnchor = savedPersisted.webAnchor && typeof savedPersisted.webAnchor === 'object'
|
|
77
|
+
? {
|
|
78
|
+
xRatio: Math.min(0.95, Math.max(0.02, Number(savedPersisted.webAnchor.xRatio ?? 0.88))),
|
|
79
|
+
yRatio: Math.min(0.95, Math.max(0.05, Number(savedPersisted.webAnchor.yRatio ?? 0.82)))
|
|
80
|
+
}
|
|
81
|
+
: { xRatio: 0.88, yRatio: 0.82 };
|
|
82
|
+
let desktopBounds = savedPersisted.desktopBounds && typeof savedPersisted.desktopBounds === 'object'
|
|
83
|
+
? savedPersisted.desktopBounds
|
|
84
|
+
: { width: 220, height: 200, anchor: 'bottom-right' };
|
|
85
|
+
|
|
86
|
+
function persistUiPrefs() {
|
|
87
|
+
try {
|
|
88
|
+
host.storage?.writeJson?.('pet-ui.json', { webAnchor, desktopBounds });
|
|
89
|
+
} catch {}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function listAllThemes() {
|
|
93
|
+
const builtins = getBuiltinThemes();
|
|
94
|
+
const customs = listCustomThemes(customThemesDir);
|
|
95
|
+
return [...builtins, ...customs];
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function resolveTheme(themeId) {
|
|
99
|
+
const customs = listCustomThemes(customThemesDir);
|
|
100
|
+
const customMatch = customs.find((t) => t.id === themeId);
|
|
101
|
+
if (customMatch) return customMatch;
|
|
102
|
+
return getBuiltinThemeById(themeId);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function getCurrentFocusSessionId() {
|
|
106
|
+
if (activeSessionState.activeSurface === 'pc-web' && activeSessionState.webSessionId) {
|
|
107
|
+
return activeSessionState.webSessionId;
|
|
108
|
+
}
|
|
109
|
+
return activeSessionState.desktopSessionId || contextSnapshotState?.cascadeId || null;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function buildPetState() {
|
|
113
|
+
const settings = normalizeSettings(host.settings?.get?.() || {});
|
|
114
|
+
const focusSessionId = getCurrentFocusSessionId();
|
|
115
|
+
const pendingApprovals = [];
|
|
116
|
+
const unreadReplies = [];
|
|
117
|
+
const watchedSessions = [];
|
|
118
|
+
|
|
119
|
+
for (const [cid, summary] of Object.entries(trajectorySummaries)) {
|
|
120
|
+
const wsUri = summary?.workspaces?.[0]?.workspaceFolderAbsoluteUri || summary?.workspaceUri || '';
|
|
121
|
+
const sessionObj = {
|
|
122
|
+
cascadeId: cid,
|
|
123
|
+
sessionTitle: summary?.annotations?.title || summary?.summary || cid.slice(0, 8),
|
|
124
|
+
workspaceUri: wsUri,
|
|
125
|
+
isSubagent: Boolean(summary?.isSubagent)
|
|
126
|
+
};
|
|
127
|
+
if (!isSessionWatched(sessionObj, settings, sessionOrgState, focusSessionId)) {
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
const orgInfo = resolveSessionOrgInfo(sessionObj, sessionOrgState);
|
|
131
|
+
const statusStr = String(summary?.status || '');
|
|
132
|
+
const isRunning = /RUNNING/i.test(statusStr);
|
|
133
|
+
const cached = stepCacheBySession.get(cid);
|
|
134
|
+
const readRec = readStateSnapshot?.[cid];
|
|
135
|
+
const readStepCount = Number(readRec?.readStepCount || 0);
|
|
136
|
+
|
|
137
|
+
watchedSessions.push({
|
|
138
|
+
cascadeId: cid,
|
|
139
|
+
sessionTitle: sessionObj.sessionTitle,
|
|
140
|
+
isRunning,
|
|
141
|
+
isFocus: cid === focusSessionId,
|
|
142
|
+
stepCount: Number(summary?.stepCount || 0),
|
|
143
|
+
orgInfo
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
if (cached && Array.isArray(cached.steps)) {
|
|
147
|
+
const startIdx = Math.max(0, cached.steps.length - 4);
|
|
148
|
+
for (let i = cached.steps.length - 1; i >= startIdx; i--) {
|
|
149
|
+
const item = classifyStepAttention(cached.steps[i], i, {
|
|
150
|
+
cascadeId: cid,
|
|
151
|
+
sessionTitle: sessionObj.sessionTitle,
|
|
152
|
+
...orgInfo
|
|
153
|
+
});
|
|
154
|
+
if (!item) continue;
|
|
155
|
+
if (item.kind === 'approval') {
|
|
156
|
+
if (!ackedBubbleIds.has(item.id)) {
|
|
157
|
+
pendingApprovals.push(item);
|
|
158
|
+
}
|
|
159
|
+
} else if (item.kind === 'reply') {
|
|
160
|
+
const totalSteps = Number(summary?.stepCount || cached.steps.length);
|
|
161
|
+
const lastAckStep = lastAckedStepBySession.get(cid) || 0;
|
|
162
|
+
if (!isRunning && totalSteps > readStepCount && i > lastAckStep && !ackedBubbleIds.has(item.id)) {
|
|
163
|
+
unreadReplies.push(item);
|
|
164
|
+
}
|
|
165
|
+
break;
|
|
166
|
+
} else if (item.kind === 'error') {
|
|
167
|
+
if (!ackedBubbleIds.has(item.id)) {
|
|
168
|
+
lastErrorBySession.set(cid, item);
|
|
169
|
+
}
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Determine primary TPS & busy source
|
|
177
|
+
const liveValid = liveContextState && (Date.now() - Number(liveContextState.updatedAt || 0) < 15000);
|
|
178
|
+
let activeTps = null;
|
|
179
|
+
let activeBusy = false;
|
|
180
|
+
let primarySessionId = focusSessionId;
|
|
181
|
+
let primarySessionTitle = '';
|
|
182
|
+
|
|
183
|
+
const focusWatched = watchedSessions.find((s) => s.isFocus);
|
|
184
|
+
const runningWatched = watchedSessions.filter((s) => s.isRunning);
|
|
185
|
+
|
|
186
|
+
if (
|
|
187
|
+
settings.followCurrentSession &&
|
|
188
|
+
!settings.excludeCurrentSession &&
|
|
189
|
+
liveValid &&
|
|
190
|
+
liveContextState.busy &&
|
|
191
|
+
(!focusWatched || focusWatched.isRunning || liveContextState.tps !== null)
|
|
192
|
+
) {
|
|
193
|
+
activeBusy = true;
|
|
194
|
+
activeTps = liveContextState.tps ?? null;
|
|
195
|
+
primarySessionId = liveContextState.cascadeId || focusSessionId;
|
|
196
|
+
} else if (runningWatched.length > 0) {
|
|
197
|
+
let best = runningWatched[0];
|
|
198
|
+
let bestTps = tpsEstimator.sessions.get(best.cascadeId)?.lastTps ?? null;
|
|
199
|
+
for (const s of runningWatched) {
|
|
200
|
+
const candTps = (liveValid && liveContextState.cascadeId === s.cascadeId && liveContextState.tps !== null)
|
|
201
|
+
? liveContextState.tps
|
|
202
|
+
: (stepCacheBySession.get(s.cascadeId)?.liveTps ?? null);
|
|
203
|
+
if (candTps !== null && (bestTps === null || candTps > bestTps)) {
|
|
204
|
+
best = s;
|
|
205
|
+
bestTps = candTps;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
activeBusy = true;
|
|
209
|
+
activeTps = bestTps;
|
|
210
|
+
primarySessionId = best.cascadeId;
|
|
211
|
+
primarySessionTitle = best.sessionTitle;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (!primarySessionTitle && primarySessionId) {
|
|
215
|
+
primarySessionTitle = trajectorySummaries[primarySessionId]?.annotations?.title
|
|
216
|
+
|| trajectorySummaries[primarySessionId]?.summary
|
|
217
|
+
|| '';
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (activeBusy || pendingApprovals.length > 0 || unreadReplies.length > 0) {
|
|
221
|
+
lastActiveAt = Date.now();
|
|
222
|
+
}
|
|
223
|
+
const idleMs = Math.max(0, Date.now() - lastActiveAt);
|
|
224
|
+
const hasApproval = pendingApprovals.length > 0;
|
|
225
|
+
const activeError = Array.from(lastErrorBySession.values())[0] || null;
|
|
226
|
+
const motionRaw = mapTpsToMotion({
|
|
227
|
+
tps: activeTps,
|
|
228
|
+
busy: activeBusy,
|
|
229
|
+
idleMs,
|
|
230
|
+
hasApproval,
|
|
231
|
+
hasError: !hasApproval && Boolean(activeError)
|
|
232
|
+
});
|
|
233
|
+
const speedFactor = smoother.push(motionRaw.rawSpeedFactor);
|
|
234
|
+
|
|
235
|
+
const activeBubble = pendingApprovals[0] || unreadReplies[0] || activeError || null;
|
|
236
|
+
const theme = resolveTheme(settings.themeId);
|
|
237
|
+
const projects = Array.isArray(sessionOrgState?.projects)
|
|
238
|
+
? sessionOrgState.projects.map((p) => ({ id: p.id, name: p.name }))
|
|
239
|
+
: [];
|
|
240
|
+
const groups = Array.isArray(sessionOrgState?.groups)
|
|
241
|
+
? sessionOrgState.groups.map((g) => ({ id: g.id, name: g.name }))
|
|
242
|
+
: [];
|
|
243
|
+
|
|
244
|
+
return {
|
|
245
|
+
state: {
|
|
246
|
+
enabled: settings.enabled,
|
|
247
|
+
desktopVisible: settings.desktopVisible,
|
|
248
|
+
webVisible: settings.webVisible,
|
|
249
|
+
themeId: theme.id,
|
|
250
|
+
petScale: settings.petScale,
|
|
251
|
+
roamMode: settings.roamMode,
|
|
252
|
+
webAnchor,
|
|
253
|
+
motion: {
|
|
254
|
+
animState: motionRaw.animState,
|
|
255
|
+
tps: activeTps,
|
|
256
|
+
speedFactor,
|
|
257
|
+
roamSpeedPxPerSec: motionRaw.roamSpeedPxPerSec,
|
|
258
|
+
primarySessionId,
|
|
259
|
+
primarySessionTitle,
|
|
260
|
+
watchedRunningCount: runningWatched.length
|
|
261
|
+
},
|
|
262
|
+
attention: {
|
|
263
|
+
pendingApprovals,
|
|
264
|
+
unreadReplies,
|
|
265
|
+
activeBubble
|
|
266
|
+
},
|
|
267
|
+
orgCatalog: { projects, groups },
|
|
268
|
+
updatedAt: Date.now()
|
|
269
|
+
},
|
|
270
|
+
theme
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
async function syncOutputs(force = false) {
|
|
275
|
+
const { state: petState, theme } = buildPetState();
|
|
276
|
+
const signature = JSON.stringify({
|
|
277
|
+
...petState,
|
|
278
|
+
updatedAt: 0
|
|
279
|
+
});
|
|
280
|
+
if (force || signature !== lastBroadcastJson) {
|
|
281
|
+
lastBroadcastJson = signature;
|
|
282
|
+
host.state?.set?.('state:pet.state@1', petState);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
const wantDesktop = Boolean(petState.enabled && petState.desktopVisible && overlaySupported.supported);
|
|
286
|
+
if (!wantDesktop) {
|
|
287
|
+
if (overlayHandle) {
|
|
288
|
+
try { await overlayHandle.dispose(); } catch {}
|
|
289
|
+
overlayHandle = null;
|
|
290
|
+
}
|
|
291
|
+
return petState;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
if (!overlayHandle && typeof host.overlay?.createWindow === 'function') {
|
|
295
|
+
try {
|
|
296
|
+
overlayHandle = await host.overlay.createWindow({
|
|
297
|
+
id: 'pet-companion',
|
|
298
|
+
bounds: desktopBounds,
|
|
299
|
+
visible: true
|
|
300
|
+
});
|
|
301
|
+
overlayHandle.onInteraction((ev) => {
|
|
302
|
+
void handleInteractionEvent({
|
|
303
|
+
action: ev.type,
|
|
304
|
+
bubbleId: ev.bubbleId,
|
|
305
|
+
itemId: ev.itemId,
|
|
306
|
+
bounds: ev.bounds
|
|
307
|
+
});
|
|
308
|
+
});
|
|
309
|
+
await overlayHandle.setScene(theme.scene);
|
|
310
|
+
} catch {
|
|
311
|
+
overlayHandle = null;
|
|
312
|
+
return petState;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
if (overlayHandle) {
|
|
317
|
+
const bubble = petState.attention.activeBubble;
|
|
318
|
+
await overlayHandle.updateState({
|
|
319
|
+
animState: petState.motion.animState,
|
|
320
|
+
speedFactor: petState.motion.speedFactor,
|
|
321
|
+
tps: petState.motion.tps,
|
|
322
|
+
scale: petState.petScale,
|
|
323
|
+
roam: {
|
|
324
|
+
enabled: petState.roamMode !== 'none',
|
|
325
|
+
mode: petState.roamMode,
|
|
326
|
+
baseSpeedPxPerSec: petState.motion.roamSpeedPxPerSec
|
|
327
|
+
},
|
|
328
|
+
bubble: bubble
|
|
329
|
+
? {
|
|
330
|
+
id: bubble.id,
|
|
331
|
+
kind: bubble.kind,
|
|
332
|
+
title: bubble.sessionTitle ? `[${bubble.sessionTitle}]` : '会话动态',
|
|
333
|
+
subtitle: bubble.summary,
|
|
334
|
+
accentHex: theme.accentHex,
|
|
335
|
+
dismissible: true
|
|
336
|
+
}
|
|
337
|
+
: null,
|
|
338
|
+
contextMenu: [
|
|
339
|
+
{ id: 'open-settings', type: 'normal', label: '打开桌宠设置' },
|
|
340
|
+
{ id: 'hide-desktop', type: 'normal', label: '隐藏桌面置顶宠' }
|
|
341
|
+
]
|
|
342
|
+
}).catch(() => {});
|
|
343
|
+
}
|
|
344
|
+
return petState;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
async function handleInteractionEvent(payload = {}) {
|
|
348
|
+
const action = String(payload.action || '');
|
|
349
|
+
if (action === 'position-changed' && payload.bounds) {
|
|
350
|
+
desktopBounds = { ...desktopBounds, ...payload.bounds };
|
|
351
|
+
persistUiPrefs();
|
|
352
|
+
return { ok: true };
|
|
353
|
+
}
|
|
354
|
+
if (action === 'update-web-anchor' && payload.webAnchor) {
|
|
355
|
+
webAnchor = {
|
|
356
|
+
xRatio: Math.min(0.95, Math.max(0.02, Number(payload.webAnchor.xRatio ?? webAnchor.xRatio))),
|
|
357
|
+
yRatio: Math.min(0.95, Math.max(0.05, Number(payload.webAnchor.yRatio ?? webAnchor.yRatio)))
|
|
358
|
+
};
|
|
359
|
+
persistUiPrefs();
|
|
360
|
+
return await syncOutputs(true);
|
|
361
|
+
}
|
|
362
|
+
if (action === 'ack-bubble' || action === 'bubble-dismiss' || action === 'bubble-click') {
|
|
363
|
+
const bid = String(payload.bubbleId || '');
|
|
364
|
+
if (bid) {
|
|
365
|
+
ackedBubbleIds.add(bid);
|
|
366
|
+
const parts = bid.split(':');
|
|
367
|
+
if (parts.length >= 3) {
|
|
368
|
+
const cid = parts[0];
|
|
369
|
+
const stepIdx = Number(parts[1]);
|
|
370
|
+
if (Number.isFinite(stepIdx)) {
|
|
371
|
+
lastAckedStepBySession.set(cid, Math.max(lastAckedStepBySession.get(cid) || 0, stepIdx));
|
|
372
|
+
}
|
|
373
|
+
lastErrorBySession.delete(cid);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
if (action === 'bubble-click' && payload.cascadeId) {
|
|
377
|
+
host.events?.emit?.('pet:focus-session@1', { cascadeId: String(payload.cascadeId), bubbleId: bid });
|
|
378
|
+
} else if (action === 'bubble-click' && bid.includes(':')) {
|
|
379
|
+
host.events?.emit?.('pet:focus-session@1', { cascadeId: bid.split(':')[0], bubbleId: bid });
|
|
380
|
+
}
|
|
381
|
+
await syncOutputs(true);
|
|
382
|
+
return { ok: true };
|
|
383
|
+
}
|
|
384
|
+
if (action === 'pet-double-click' || (action === 'menu-click' && payload.itemId === 'open-settings')) {
|
|
385
|
+
host.events?.emit?.('pet:open-settings@1', { pluginId: host.pluginId });
|
|
386
|
+
return { ok: true };
|
|
387
|
+
}
|
|
388
|
+
if (action === 'menu-click' && payload.itemId === 'hide-desktop') {
|
|
389
|
+
await host.settings?.update?.({ desktopVisible: false });
|
|
390
|
+
return { ok: true };
|
|
391
|
+
}
|
|
392
|
+
return { ok: true };
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
async function pollTrajectories() {
|
|
396
|
+
const settings = normalizeSettings(host.settings?.get?.() || {});
|
|
397
|
+
if (!settings.enabled || (!settings.desktopVisible && !settings.webVisible)) return;
|
|
398
|
+
try {
|
|
399
|
+
const resp = await host.rpc.call('GetAllCascadeTrajectories', {
|
|
400
|
+
metadata: { ideName: 'antigravity', extensionName: 'antigravity' }
|
|
401
|
+
});
|
|
402
|
+
if (resp && typeof resp.trajectorySummaries === 'object') {
|
|
403
|
+
trajectorySummaries = resp.trajectorySummaries;
|
|
404
|
+
}
|
|
405
|
+
await syncOutputs(false);
|
|
406
|
+
} catch {}
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
async function pollTailSteps() {
|
|
410
|
+
const settings = normalizeSettings(host.settings?.get?.() || {});
|
|
411
|
+
if (!settings.enabled || (!settings.desktopVisible && !settings.webVisible)) return;
|
|
412
|
+
const focusId = getCurrentFocusSessionId();
|
|
413
|
+
for (const [cid, summary] of Object.entries(trajectorySummaries)) {
|
|
414
|
+
const isRunning = /RUNNING/i.test(String(summary?.status || ''));
|
|
415
|
+
const totalSteps = Number(summary?.stepCount || 0);
|
|
416
|
+
const cached = stepCacheBySession.get(cid);
|
|
417
|
+
const needsRefresh = isRunning || !cached || cached.stepCount !== totalSteps;
|
|
418
|
+
if (!needsRefresh) continue;
|
|
419
|
+
if (!isSessionWatched({ cascadeId: cid, workspaceUri: summary?.workspaces?.[0]?.workspaceFolderAbsoluteUri }, settings, sessionOrgState, focusId)) {
|
|
420
|
+
continue;
|
|
421
|
+
}
|
|
422
|
+
try {
|
|
423
|
+
const startOffset = Math.max(0, totalSteps - 2);
|
|
424
|
+
const res = await host.rpc.call('GetCascadeTrajectorySteps', {
|
|
425
|
+
cascadeId: cid,
|
|
426
|
+
stepOffset: startOffset
|
|
427
|
+
});
|
|
428
|
+
const steps = Array.isArray(res?.steps) ? res.steps : [];
|
|
429
|
+
const tailText = steps.map((s) => s?.plannerResponse?.response || s?.plannerResponse?.thinking || '').join('\n');
|
|
430
|
+
const liveTps = tpsEstimator.sample(cid, isRunning, tailText);
|
|
431
|
+
const fullArr = cached?.steps ? [...cached.steps] : [];
|
|
432
|
+
for (let i = 0; i < steps.length; i++) {
|
|
433
|
+
fullArr[startOffset + i] = steps[i];
|
|
434
|
+
}
|
|
435
|
+
stepCacheBySession.set(cid, { stepCount: totalSteps, steps: fullArr, liveTps });
|
|
436
|
+
} catch {}
|
|
437
|
+
}
|
|
438
|
+
await syncOutputs(false);
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
try {
|
|
442
|
+
if (typeof host.overlay?.isSupported === 'function') {
|
|
443
|
+
overlaySupported = await host.overlay.isSupported();
|
|
444
|
+
}
|
|
445
|
+
} catch {}
|
|
446
|
+
|
|
447
|
+
const serviceMethods = {
|
|
448
|
+
getSnapshot() {
|
|
449
|
+
const { state: petState, theme } = buildPetState();
|
|
450
|
+
return {
|
|
451
|
+
ok: true,
|
|
452
|
+
available: true,
|
|
453
|
+
overlaySupported,
|
|
454
|
+
state: petState,
|
|
455
|
+
scene: theme.scene,
|
|
456
|
+
themes: listAllThemes().map((t) => ({
|
|
457
|
+
id: t.id,
|
|
458
|
+
name: t.name,
|
|
459
|
+
description: t.description,
|
|
460
|
+
accentHex: t.accentHex,
|
|
461
|
+
builtin: Boolean(t.builtin)
|
|
462
|
+
}))
|
|
463
|
+
};
|
|
464
|
+
},
|
|
465
|
+
getThemeRig(themeId) {
|
|
466
|
+
return resolveTheme(themeId);
|
|
467
|
+
},
|
|
468
|
+
listThemes() {
|
|
469
|
+
return listAllThemes();
|
|
470
|
+
},
|
|
471
|
+
setActiveWebSession(cascadeId) {
|
|
472
|
+
activeSessionState = {
|
|
473
|
+
...activeSessionState,
|
|
474
|
+
webSessionId: cascadeId ? String(cascadeId) : null,
|
|
475
|
+
activeSurface: cascadeId ? 'pc-web' : 'desktop',
|
|
476
|
+
updatedAt: Date.now()
|
|
477
|
+
};
|
|
478
|
+
host.state?.set?.('state:session.active@1', activeSessionState);
|
|
479
|
+
void syncOutputs(false);
|
|
480
|
+
return activeSessionState;
|
|
481
|
+
},
|
|
482
|
+
async handleInteraction(payload) {
|
|
483
|
+
return await handleInteractionEvent(payload || {});
|
|
484
|
+
},
|
|
485
|
+
async importThemePack(payload = {}) {
|
|
486
|
+
const imported = importThemePack({
|
|
487
|
+
customThemesDir,
|
|
488
|
+
archiveBase64: payload.archiveBase64,
|
|
489
|
+
jsonText: payload.jsonText,
|
|
490
|
+
svgText: payload.svgText,
|
|
491
|
+
fileName: payload.fileName
|
|
492
|
+
});
|
|
493
|
+
await host.settings?.update?.({ themeId: imported.id });
|
|
494
|
+
await syncOutputs(true);
|
|
495
|
+
return { ok: true, theme: imported };
|
|
496
|
+
},
|
|
497
|
+
async deleteCustomTheme(themeId) {
|
|
498
|
+
const res = deleteCustomTheme(customThemesDir, themeId);
|
|
499
|
+
if (res.ok && host.settings?.get?.()?.themeId === res.id) {
|
|
500
|
+
await host.settings.update({ themeId: 'daddy-orb' });
|
|
501
|
+
}
|
|
502
|
+
await syncOutputs(true);
|
|
503
|
+
return res;
|
|
504
|
+
}
|
|
505
|
+
};
|
|
506
|
+
|
|
507
|
+
disposers.push(
|
|
508
|
+
host.services.provide('pet', serviceMethods, { realm: 'node', version: '1.0.0' }),
|
|
509
|
+
await host.bridge.registerRoute('/state', async () => serviceMethods.getSnapshot()),
|
|
510
|
+
await host.bridge.registerRoute('/interact', async (body) => serviceMethods.handleInteraction(body || {})),
|
|
511
|
+
await host.bridge.registerRoute('/import-theme', async (body) => serviceMethods.importThemePack(body || {})),
|
|
512
|
+
await host.bridge.registerRoute('/active-session', async (body) => {
|
|
513
|
+
activeSessionState = {
|
|
514
|
+
...activeSessionState,
|
|
515
|
+
desktopSessionId: body?.cascadeId ? String(body.cascadeId) : null,
|
|
516
|
+
desktopBusy: Boolean(body?.busy),
|
|
517
|
+
activeSurface: 'desktop',
|
|
518
|
+
updatedAt: Date.now()
|
|
519
|
+
};
|
|
520
|
+
host.state?.set?.('state:session.active@1', activeSessionState);
|
|
521
|
+
void syncOutputs(false);
|
|
522
|
+
return { ok: true };
|
|
523
|
+
}),
|
|
524
|
+
host.state.subscribe('state:context.live@1', (val) => {
|
|
525
|
+
liveContextState = val || null;
|
|
526
|
+
void syncOutputs(false);
|
|
527
|
+
}),
|
|
528
|
+
host.state.subscribe('state:context.snapshot@1', (val) => {
|
|
529
|
+
contextSnapshotState = val || null;
|
|
530
|
+
void syncOutputs(false);
|
|
531
|
+
}),
|
|
532
|
+
host.state.subscribe('state:session.org@1', (val) => {
|
|
533
|
+
sessionOrgState = val || null;
|
|
534
|
+
void syncOutputs(true);
|
|
535
|
+
}),
|
|
536
|
+
host.state.subscribe('state:session.readState@1', (val) => {
|
|
537
|
+
readStateSnapshot = val && typeof val === 'object' ? val : {};
|
|
538
|
+
void syncOutputs(false);
|
|
539
|
+
}),
|
|
540
|
+
host.settings.onChange(async (nextValues, changedKeys) => {
|
|
541
|
+
if (Array.isArray(changedKeys) && changedKeys.includes('themeId')) {
|
|
542
|
+
const theme = resolveTheme(nextValues.themeId);
|
|
543
|
+
if (overlayHandle) {
|
|
544
|
+
await overlayHandle.setScene(theme.scene).catch(() => {});
|
|
545
|
+
}
|
|
546
|
+
host.events?.emit?.('pet:theme-changed@1', { themeId: theme.id });
|
|
547
|
+
}
|
|
548
|
+
await syncOutputs(true);
|
|
549
|
+
}),
|
|
550
|
+
await host.commands.register('pet.toggleDesktop', async () => {
|
|
551
|
+
const cur = normalizeSettings(host.settings?.get?.() || {});
|
|
552
|
+
return await host.settings.update({ desktopVisible: !cur.desktopVisible });
|
|
553
|
+
}),
|
|
554
|
+
await host.commands.register('pet.toggleWeb', async () => {
|
|
555
|
+
const cur = normalizeSettings(host.settings?.get?.() || {});
|
|
556
|
+
return await host.settings.update({ webVisible: !cur.webVisible });
|
|
557
|
+
}),
|
|
558
|
+
await host.commands.register('pet.listThemes', async () => listAllThemes().map((t) => ({
|
|
559
|
+
value: t.id,
|
|
560
|
+
label: t.name
|
|
561
|
+
}))),
|
|
562
|
+
await host.commands.register('pet.listProjectOptions', async () => {
|
|
563
|
+
const projects = Array.isArray(sessionOrgState?.projects) ? sessionOrgState.projects : [];
|
|
564
|
+
return projects.map((p) => ({ id: p.id, label: p.name || p.id }));
|
|
565
|
+
}),
|
|
566
|
+
await host.commands.register('pet.listGroupOptions', async () => {
|
|
567
|
+
const groups = Array.isArray(sessionOrgState?.groups) ? sessionOrgState.groups : [];
|
|
568
|
+
return groups.map((g) => ({ id: g.id, label: g.name || g.id }));
|
|
569
|
+
})
|
|
570
|
+
);
|
|
571
|
+
|
|
572
|
+
try {
|
|
573
|
+
const trayHandle = await host.tray?.registerMenuItem?.({
|
|
574
|
+
id: 'pet-toggle',
|
|
575
|
+
label: '桌面伴侣宠物',
|
|
576
|
+
type: 'checkbox',
|
|
577
|
+
checked: normalizeSettings(host.settings?.get?.() || {}).desktopVisible,
|
|
578
|
+
position: 'plugins',
|
|
579
|
+
onClick: async (tCtx) => {
|
|
580
|
+
const cur = normalizeSettings(host.settings?.get?.() || {});
|
|
581
|
+
const next = !cur.desktopVisible;
|
|
582
|
+
await host.settings.update({ desktopVisible: next });
|
|
583
|
+
await tCtx.update({ checked: next });
|
|
584
|
+
}
|
|
585
|
+
});
|
|
586
|
+
if (trayHandle) disposers.push(() => trayHandle.dispose());
|
|
587
|
+
} catch {}
|
|
588
|
+
|
|
589
|
+
pollTimer = setInterval(() => { void pollTrajectories(); }, 2000);
|
|
590
|
+
if (typeof pollTimer.unref === 'function') pollTimer.unref();
|
|
591
|
+
tailTimer = setInterval(() => { void pollTailSteps(); }, 500);
|
|
592
|
+
if (typeof tailTimer.unref === 'function') tailTimer.unref();
|
|
593
|
+
disposers.push(
|
|
594
|
+
() => { if (pollTimer) clearInterval(pollTimer); pollTimer = null; },
|
|
595
|
+
() => { if (tailTimer) clearInterval(tailTimer); tailTimer = null; }
|
|
596
|
+
);
|
|
597
|
+
|
|
598
|
+
await syncOutputs(true);
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
async function deactivate() {
|
|
602
|
+
for (const fn of disposers.splice(0).reverse()) {
|
|
603
|
+
try {
|
|
604
|
+
if (typeof fn === 'function') await fn();
|
|
605
|
+
} catch {}
|
|
606
|
+
}
|
|
607
|
+
const host = activeHost;
|
|
608
|
+
activeHost = null;
|
|
609
|
+
try {
|
|
610
|
+
await host?.overlay?.disposeAll?.();
|
|
611
|
+
host?.state?.set?.('state:pet.state@1', null);
|
|
612
|
+
host?.state?.set?.('state:session.active@1', null);
|
|
613
|
+
} catch {}
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
module.exports = {
|
|
617
|
+
activate,
|
|
618
|
+
deactivate,
|
|
619
|
+
normalizeSettings
|
|
620
|
+
};
|
package/src/renderer.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
let disposers = [];
|
|
4
|
+
|
|
5
|
+
async function mount(ctx) {
|
|
6
|
+
dispose();
|
|
7
|
+
|
|
8
|
+
let lastSessionKey = '';
|
|
9
|
+
|
|
10
|
+
function syncActiveSession() {
|
|
11
|
+
const cascadeId = ctx.hostDom?.getSessionId?.() || null;
|
|
12
|
+
const busy = Boolean(ctx.hostDom?.isAssistantBusy?.());
|
|
13
|
+
const key = `${cascadeId || ''}:${busy ? 1 : 0}`;
|
|
14
|
+
if (key === lastSessionKey) return;
|
|
15
|
+
lastSessionKey = key;
|
|
16
|
+
Promise.resolve(ctx.bridge?.post?.('/active-session', { cascadeId, busy })).catch(() => {});
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const stopActivePoll = ctx.schedule.every(1000, () => {
|
|
20
|
+
syncActiveSession();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
syncActiveSession();
|
|
24
|
+
|
|
25
|
+
disposers.push(
|
|
26
|
+
() => { if (typeof stopActivePoll === 'function') stopActivePoll(); }
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function dispose() {
|
|
31
|
+
for (const fn of disposers.splice(0)) {
|
|
32
|
+
try { if (typeof fn === 'function') fn(); } catch {}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
module.exports = {
|
|
37
|
+
mount,
|
|
38
|
+
dispose
|
|
39
|
+
};
|