dsh-desktop-enhance 0.7.0 → 0.7.1
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/lib/index.d.ts +5 -26
- package/lib/index.js +85 -161
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -290,19 +290,8 @@ interface ContextHookHost extends HostContext {
|
|
|
290
290
|
declare function installContextHooks(ctx: ContextHookHost, control: ReturnType<typeof createContextControl>, options: ContextControlOptions): () => void;
|
|
291
291
|
//#endregion
|
|
292
292
|
//#region src/shared/skill-recovery.d.ts
|
|
293
|
-
type SkillLoadSource = {
|
|
294
|
-
readonly kind: 'user';
|
|
295
|
-
readonly name: string;
|
|
296
|
-
} | {
|
|
297
|
-
readonly kind: 'tool';
|
|
298
|
-
readonly name: string;
|
|
299
|
-
} | {
|
|
300
|
-
readonly kind: 'recovery';
|
|
301
|
-
readonly name: string;
|
|
302
|
-
};
|
|
303
293
|
interface SkillLoad {
|
|
304
294
|
readonly name: string;
|
|
305
|
-
readonly source: SkillLoadSource;
|
|
306
295
|
readonly seq: number;
|
|
307
296
|
readonly onSurface: boolean;
|
|
308
297
|
}
|
|
@@ -311,15 +300,12 @@ interface CompactionSpan {
|
|
|
311
300
|
}
|
|
312
301
|
interface RecoveryInput {
|
|
313
302
|
readonly loads: readonly SkillLoad[];
|
|
314
|
-
readonly recoveries: readonly SkillLoad[];
|
|
315
303
|
readonly latestCompaction: CompactionSpan | null;
|
|
316
304
|
readonly available: (name: string) => boolean;
|
|
317
305
|
}
|
|
318
|
-
|
|
306
|
+
declare function decideRecovery(input: RecoveryInput): {
|
|
319
307
|
readonly names: readonly string[];
|
|
320
|
-
|
|
321
|
-
}
|
|
322
|
-
declare function decideRecovery(input: RecoveryInput): RecoveryDecision;
|
|
308
|
+
};
|
|
323
309
|
declare function renderSkillContentForRecovery(skill: {
|
|
324
310
|
readonly name: string;
|
|
325
311
|
readonly provider: string;
|
|
@@ -331,26 +317,20 @@ declare function renderSkillContentForRecovery(skill: {
|
|
|
331
317
|
readonly description?: string;
|
|
332
318
|
};
|
|
333
319
|
}): string;
|
|
334
|
-
declare function buildRecoveryText(skill: Parameters<typeof renderSkillContentForRecovery>[0]): string;
|
|
335
320
|
//#endregion
|
|
336
321
|
//#region src/skill-recovery/host.d.ts
|
|
337
322
|
interface HostSessionEventLike {
|
|
338
323
|
readonly type?: string;
|
|
339
324
|
readonly seq?: number;
|
|
340
325
|
readonly data?: unknown;
|
|
341
|
-
readonly surfaceOp?: unknown;
|
|
342
|
-
readonly sourceEventSeqs?: readonly number[];
|
|
343
326
|
}
|
|
344
327
|
interface RecoveryHookHost extends HostContext {
|
|
345
|
-
on?(event: string, listener: (...args: never[]) => unknown
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
readonly name: string;
|
|
349
|
-
readonly text: string;
|
|
328
|
+
on?(event: string, listener: (...args: never[]) => unknown, options?: {
|
|
329
|
+
readonly prepend?: boolean;
|
|
330
|
+
}): () => void;
|
|
350
331
|
}
|
|
351
332
|
declare function projectRecoveryState(events: readonly HostSessionEventLike[], surfaceNodes: readonly number[]): {
|
|
352
333
|
loads: SkillLoad[];
|
|
353
|
-
recoveries: Array<SkillLoad & RecoveryBody>;
|
|
354
334
|
latestCompaction: CompactionSpan | null;
|
|
355
335
|
};
|
|
356
336
|
interface SkillRecoveryOptions {
|
|
@@ -477,7 +457,6 @@ export declare const __test: {
|
|
|
477
457
|
encodeCaps: typeof encodeCaps;
|
|
478
458
|
resolvePolicy: typeof resolvePolicy;
|
|
479
459
|
withCap: typeof withCap;
|
|
480
|
-
buildRecoveryText: typeof buildRecoveryText;
|
|
481
460
|
decideRecovery: typeof decideRecovery;
|
|
482
461
|
renderSkillContentForRecovery: typeof renderSkillContentForRecovery;
|
|
483
462
|
installSkillRecovery: typeof installSkillRecovery;
|
package/lib/index.js
CHANGED
|
@@ -3170,33 +3170,13 @@ function installContextHooks(ctx, control, options) {
|
|
|
3170
3170
|
//#endregion
|
|
3171
3171
|
//#region src/shared/skill-recovery.ts
|
|
3172
3172
|
function decideRecovery(input) {
|
|
3173
|
-
|
|
3174
|
-
if (compaction === null) return {
|
|
3175
|
-
names: [],
|
|
3176
|
-
compactionSeq: null
|
|
3177
|
-
};
|
|
3173
|
+
if (input.latestCompaction === null) return { names: [] };
|
|
3178
3174
|
const latestByName = new Map();
|
|
3179
3175
|
for (const load of input.loads) {
|
|
3180
3176
|
const previous = latestByName.get(load.name);
|
|
3181
3177
|
if (previous === void 0 || load.seq >= previous.seq) latestByName.set(load.name, load);
|
|
3182
3178
|
}
|
|
3183
|
-
|
|
3184
|
-
for (const record of input.recoveries) if (record.onSurface) retainedBodies.add(record.name);
|
|
3185
|
-
const candidates = [];
|
|
3186
|
-
for (const [name, load] of latestByName) {
|
|
3187
|
-
if (load.onSurface) continue;
|
|
3188
|
-
if (retainedBodies.has(name)) continue;
|
|
3189
|
-
if (!input.available(name)) continue;
|
|
3190
|
-
candidates.push({
|
|
3191
|
-
name,
|
|
3192
|
-
seq: load.seq
|
|
3193
|
-
});
|
|
3194
|
-
}
|
|
3195
|
-
candidates.sort((left, right) => left.seq - right.seq || (left.name < right.name ? -1 : left.name > right.name ? 1 : 0));
|
|
3196
|
-
return {
|
|
3197
|
-
names: candidates.map((candidate) => candidate.name),
|
|
3198
|
-
compactionSeq: compaction.seq
|
|
3199
|
-
};
|
|
3179
|
+
return { names: [...latestByName.values()].filter((load) => !load.onSurface && input.available(load.name)).sort((left, right) => left.seq - right.seq || left.name.localeCompare(right.name)).map((load) => load.name) };
|
|
3200
3180
|
}
|
|
3201
3181
|
function renderSkillContentForRecovery(skill) {
|
|
3202
3182
|
const base = skill.resourceBase;
|
|
@@ -3224,98 +3204,39 @@ function escapeAttr(value) {
|
|
|
3224
3204
|
function escapeText(value) {
|
|
3225
3205
|
return value.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">");
|
|
3226
3206
|
}
|
|
3227
|
-
function buildRecoveryText(skill) {
|
|
3228
|
-
return renderSkillContentForRecovery(skill);
|
|
3229
|
-
}
|
|
3230
3207
|
//#endregion
|
|
3231
3208
|
//#region src/skill-recovery/host.ts
|
|
3232
|
-
const RECOVERY_CONTEXT = "desktop-enhance-skill-recovery";
|
|
3233
|
-
function readUserSource(event) {
|
|
3234
|
-
if (event.type !== "user/message") return void 0;
|
|
3235
|
-
return event.data?.source;
|
|
3236
|
-
}
|
|
3237
3209
|
function projectRecoveryState(events, surfaceNodes) {
|
|
3238
3210
|
const loads = [];
|
|
3239
|
-
const recoveries = [];
|
|
3240
3211
|
let latestCompaction = null;
|
|
3241
3212
|
const live = new Set(surfaceNodes);
|
|
3242
|
-
let latestRuntimeSeq;
|
|
3243
|
-
for (const event of events) if (typeof event.seq === "number" && live.has(event.seq) && readUserSource(event)?.kind === "runtime-context") latestRuntimeSeq = event.seq;
|
|
3244
3213
|
for (const event of events) {
|
|
3245
3214
|
if (event.type === "compaction/summary") {
|
|
3246
3215
|
const data = event.data;
|
|
3247
|
-
|
|
3248
|
-
const seq = event.seq;
|
|
3249
|
-
if (shadowed.length > 0 && typeof seq === "number") latestCompaction = { seq };
|
|
3250
|
-
continue;
|
|
3251
|
-
}
|
|
3252
|
-
if (event.type !== "user/message" && event.type !== "tool/result") continue;
|
|
3253
|
-
const seq = event.seq;
|
|
3254
|
-
if (typeof seq !== "number") continue;
|
|
3255
|
-
const source = readUserSource(event);
|
|
3256
|
-
if (source?.kind === "runtime-context") {
|
|
3257
|
-
for (const body of readRecoveryBodies(source.sections)) recoveries.push({
|
|
3258
|
-
...body,
|
|
3259
|
-
source: {
|
|
3260
|
-
kind: "recovery",
|
|
3261
|
-
name: body.name
|
|
3262
|
-
},
|
|
3263
|
-
seq,
|
|
3264
|
-
onSurface: seq === latestRuntimeSeq
|
|
3265
|
-
});
|
|
3216
|
+
if (Array.isArray(data?.shadowedSeqs) && data.shadowedSeqs.length > 0 && typeof event.seq === "number") latestCompaction = { seq: event.seq };
|
|
3266
3217
|
continue;
|
|
3267
3218
|
}
|
|
3268
|
-
const
|
|
3269
|
-
if (
|
|
3270
|
-
|
|
3271
|
-
|
|
3272
|
-
|
|
3273
|
-
name: parsed.name
|
|
3274
|
-
},
|
|
3275
|
-
seq,
|
|
3276
|
-
onSurface: live.has(seq)
|
|
3219
|
+
const load = readSkillLoad(event);
|
|
3220
|
+
if (load !== null && typeof event.seq === "number") loads.push({
|
|
3221
|
+
...load,
|
|
3222
|
+
seq: event.seq,
|
|
3223
|
+
onSurface: live.has(event.seq)
|
|
3277
3224
|
});
|
|
3278
3225
|
}
|
|
3279
3226
|
return {
|
|
3280
3227
|
loads,
|
|
3281
|
-
recoveries,
|
|
3282
3228
|
latestCompaction
|
|
3283
3229
|
};
|
|
3284
3230
|
}
|
|
3285
|
-
function readRecoveryBodies(sections) {
|
|
3286
|
-
if (!Array.isArray(sections)) return [];
|
|
3287
|
-
const bodies = [];
|
|
3288
|
-
for (const section of sections) {
|
|
3289
|
-
if (section?.name !== RECOVERY_CONTEXT || typeof section.text !== "string") continue;
|
|
3290
|
-
const blocks = section.text.matchAll(/(?:^|\n\n)(<skill_content name="([^"]+)">[\s\S]*?<\/skill_content>)(?=\n\n<skill_content name="|$)/g);
|
|
3291
|
-
for (const match of blocks) if (match[1] !== void 0 && match[2] !== void 0) bodies.push({
|
|
3292
|
-
name: match[2],
|
|
3293
|
-
text: match[1]
|
|
3294
|
-
});
|
|
3295
|
-
}
|
|
3296
|
-
return bodies;
|
|
3297
|
-
}
|
|
3298
3231
|
function readSkillLoad(event) {
|
|
3299
3232
|
const data = event.data;
|
|
3300
|
-
|
|
3301
|
-
if (
|
|
3302
|
-
|
|
3303
|
-
name: source.name
|
|
3304
|
-
};
|
|
3305
|
-
const blocks = event.type === "tool/result" ? data?.message?.content : data?.content;
|
|
3233
|
+
if (event.type === "user/message" && data?.source?.kind === "skill-invocation" && typeof data.source.name === "string") return { name: data.source.name };
|
|
3234
|
+
if (event.type !== "tool/result") return null;
|
|
3235
|
+
const blocks = data?.message?.content;
|
|
3306
3236
|
if (!Array.isArray(blocks)) return null;
|
|
3307
|
-
|
|
3308
|
-
|
|
3309
|
-
|
|
3310
|
-
if (event.type === "tool/result" && text.trimStart().startsWith("<skill_content")) {
|
|
3311
|
-
const match = /<skill_content name="([^"]+)"/.exec(text);
|
|
3312
|
-
if (match?.[1] === void 0) return null;
|
|
3313
|
-
return {
|
|
3314
|
-
kind: "tool",
|
|
3315
|
-
name: match[1]
|
|
3316
|
-
};
|
|
3317
|
-
}
|
|
3318
|
-
return null;
|
|
3237
|
+
const text = blocks.filter((block) => block.type === "text").map((block) => block.text ?? "").join("");
|
|
3238
|
+
const match = /^\s*<skill_content name="([^"]+)"/.exec(text);
|
|
3239
|
+
return match?.[1] === void 0 ? null : { name: match[1] };
|
|
3319
3240
|
}
|
|
3320
3241
|
function readSkillRecoveryEnabled(config) {
|
|
3321
3242
|
if (config === null || typeof config !== "object") return true;
|
|
@@ -3330,82 +3251,86 @@ function readSkillRecoveryEnabled(config) {
|
|
|
3330
3251
|
return true;
|
|
3331
3252
|
}
|
|
3332
3253
|
function installSkillRecovery(ctx, options) {
|
|
3333
|
-
const disposers = [];
|
|
3334
|
-
const pendingByAgent = new WeakMap();
|
|
3335
3254
|
const on = ctx.on;
|
|
3336
3255
|
if (typeof on !== "function") return () => {};
|
|
3337
|
-
|
|
3338
|
-
|
|
3339
|
-
|
|
3340
|
-
|
|
3256
|
+
const lifetime = new AbortController();
|
|
3257
|
+
const available = (name, agent) => options.available?.(name, agent.id) ?? true;
|
|
3258
|
+
async function recover(payload, pending = []) {
|
|
3259
|
+
if (!options.enabled() || lifetime.signal.aborted) return [];
|
|
3260
|
+
const { agent } = payload;
|
|
3261
|
+
const signal = AbortSignal.any([payload.signal, lifetime.signal]);
|
|
3262
|
+
signal.throwIfAborted();
|
|
3263
|
+
const projected = projectRecoveryState(agent.session.snapshotEvents(), agent.session.surface.nodes);
|
|
3264
|
+
const pendingNames = new Set(pending.filter((message) => message.source.kind === "skill-invocation").map((message) => message.source.name));
|
|
3341
3265
|
const decision = decideRecovery({
|
|
3342
|
-
|
|
3343
|
-
|
|
3344
|
-
latestCompaction: projected.latestCompaction,
|
|
3345
|
-
available
|
|
3266
|
+
...projected,
|
|
3267
|
+
available: (name) => !pendingNames.has(name) && available(name, agent)
|
|
3346
3268
|
});
|
|
3347
|
-
|
|
3348
|
-
for (const body of projected.recoveries) if (body.onSurface && !projected.loads.some((load) => load.name === body.name && load.onSurface)) sections.set(body.name, body);
|
|
3349
|
-
pendingByAgent.set(agent, [...sections.values()]);
|
|
3350
|
-
if (decision.names.length === 0 || decision.compactionSeq === null) return;
|
|
3269
|
+
if (decision.names.length === 0) return [];
|
|
3351
3270
|
const skills = ctx.get("skills");
|
|
3352
|
-
if (skills
|
|
3353
|
-
const
|
|
3354
|
-
|
|
3355
|
-
|
|
3356
|
-
|
|
3357
|
-
|
|
3358
|
-
|
|
3359
|
-
|
|
3360
|
-
|
|
3361
|
-
|
|
3362
|
-
|
|
3363
|
-
|
|
3364
|
-
|
|
3271
|
+
if (skills === void 0) return [];
|
|
3272
|
+
const cwd = await ctx.get("workingDirectory")?.ensure(agent, signal);
|
|
3273
|
+
const messages = [];
|
|
3274
|
+
for (const name of decision.names) {
|
|
3275
|
+
signal.throwIfAborted();
|
|
3276
|
+
let skill;
|
|
3277
|
+
try {
|
|
3278
|
+
skill = await skills.get(name, {
|
|
3279
|
+
scope: agent,
|
|
3280
|
+
cwd,
|
|
3281
|
+
signal
|
|
3282
|
+
});
|
|
3283
|
+
} catch {
|
|
3284
|
+
signal.throwIfAborted();
|
|
3285
|
+
ctx.logger?.warn(`desktop-enhance: could not restore Skill ${name}; will retry at the next admission`);
|
|
3286
|
+
continue;
|
|
3287
|
+
}
|
|
3365
3288
|
if (skill === void 0 || typeof skill.content !== "string" || skill.content === "") continue;
|
|
3366
|
-
|
|
3367
|
-
|
|
3368
|
-
|
|
3369
|
-
|
|
3370
|
-
|
|
3371
|
-
|
|
3372
|
-
|
|
3373
|
-
}
|
|
3289
|
+
messages.push({
|
|
3290
|
+
id: randomUUID(),
|
|
3291
|
+
role: "user",
|
|
3292
|
+
source: {
|
|
3293
|
+
kind: "skill-invocation",
|
|
3294
|
+
name,
|
|
3295
|
+
form: "instructions"
|
|
3296
|
+
},
|
|
3297
|
+
content: [{
|
|
3298
|
+
type: "text",
|
|
3299
|
+
text: renderSkillContentForRecovery({
|
|
3300
|
+
...skill,
|
|
3301
|
+
provider: skill.provider ?? "",
|
|
3302
|
+
content: skill.content
|
|
3303
|
+
})
|
|
3304
|
+
}]
|
|
3374
3305
|
});
|
|
3375
|
-
} catch {}
|
|
3376
|
-
pendingByAgent.set(agent, [...sections.values()]);
|
|
3377
|
-
}
|
|
3378
|
-
disposers.push(on.call(ctx, "agent/request", async (payload, next) => {
|
|
3379
|
-
if (options.enabled()) {
|
|
3380
|
-
const agent = payload.agent;
|
|
3381
|
-
if (agent !== void 0) try {
|
|
3382
|
-
await prepare(agent);
|
|
3383
|
-
} catch {}
|
|
3384
3306
|
}
|
|
3385
|
-
|
|
3386
|
-
|
|
3387
|
-
|
|
3388
|
-
|
|
3389
|
-
|
|
3390
|
-
|
|
3391
|
-
|
|
3392
|
-
|
|
3393
|
-
|
|
3394
|
-
|
|
3395
|
-
|
|
3396
|
-
|
|
3397
|
-
|
|
3398
|
-
|
|
3399
|
-
|
|
3400
|
-
|
|
3401
|
-
|
|
3402
|
-
|
|
3403
|
-
|
|
3404
|
-
|
|
3307
|
+
signal.throwIfAborted();
|
|
3308
|
+
if (!options.enabled()) return [];
|
|
3309
|
+
return messages.filter((message) => available(message.source.name, agent));
|
|
3310
|
+
}
|
|
3311
|
+
const disposers = [on.call(ctx, "agent/pre-step", async (payload, next) => {
|
|
3312
|
+
const decision = await next();
|
|
3313
|
+
if (decision.kind === "reject" || payload.step === 1 && decision.messages.length === 0) return decision;
|
|
3314
|
+
const messages = await recover(payload, decision.messages);
|
|
3315
|
+
return messages.length === 0 ? decision : {
|
|
3316
|
+
...decision,
|
|
3317
|
+
messages: [...decision.messages, ...messages]
|
|
3318
|
+
};
|
|
3319
|
+
}, { prepend: true }), on.call(ctx, "agent/request-error", async (payload, next) => {
|
|
3320
|
+
const decision = await next();
|
|
3321
|
+
if (decision?.kind !== "retry") return decision;
|
|
3322
|
+
const messages = await recover(payload);
|
|
3323
|
+
for (const message of messages) {
|
|
3324
|
+
payload.signal.throwIfAborted();
|
|
3325
|
+
lifetime.signal.throwIfAborted();
|
|
3326
|
+
if (!options.enabled() || !available(message.source.name, payload.agent)) continue;
|
|
3327
|
+
payload.agent.session.append("user/message", message, { surfaceOp: "append" });
|
|
3328
|
+
}
|
|
3329
|
+
return decision;
|
|
3330
|
+
}, { prepend: true })];
|
|
3405
3331
|
return () => {
|
|
3406
|
-
|
|
3407
|
-
|
|
3408
|
-
} catch {}
|
|
3332
|
+
lifetime.abort(new Error("Skill 恢复已卸载"));
|
|
3333
|
+
for (const dispose of disposers) dispose();
|
|
3409
3334
|
};
|
|
3410
3335
|
}
|
|
3411
3336
|
//#endregion
|
|
@@ -3501,7 +3426,6 @@ const __test = {
|
|
|
3501
3426
|
encodeCaps,
|
|
3502
3427
|
resolvePolicy,
|
|
3503
3428
|
withCap,
|
|
3504
|
-
buildRecoveryText,
|
|
3505
3429
|
decideRecovery,
|
|
3506
3430
|
renderSkillContentForRecovery,
|
|
3507
3431
|
installSkillRecovery,
|
package/package.json
CHANGED