@kirkchinese/harness-forge 0.0.0-stage → 0.4.0
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 +42 -2
- package/assets/icon.svg +12 -0
- package/cordis.patch.yml +28 -0
- package/lib/agents/presets.d.ts +91 -0
- package/lib/agents/presets.js +282 -0
- package/lib/agents/profiles.d.ts +58 -0
- package/lib/agents/profiles.js +278 -0
- package/lib/agents/row.d.ts +17 -0
- package/lib/agents/row.js +146 -0
- package/lib/client/styles.d.ts +8 -0
- package/lib/client/styles.js +89 -0
- package/lib/client/typert.d.ts +14 -0
- package/lib/client/typert.js +110 -0
- package/lib/client.js +22500 -0
- package/lib/commands/definition.d.ts +40 -0
- package/lib/commands/definition.js +113 -0
- package/lib/commands/output-styles.d.ts +42 -0
- package/lib/commands/output-styles.js +152 -0
- package/lib/commands/row.d.ts +9 -0
- package/lib/commands/row.js +157 -0
- package/lib/context/instructions.d.ts +32 -0
- package/lib/context/instructions.js +88 -0
- package/lib/context/row.d.ts +16 -0
- package/lib/context/row.js +17 -0
- package/lib/dsh/commands.d.ts +30 -0
- package/lib/dsh/commands.js +61 -0
- package/lib/dsh/host.d.ts +107 -0
- package/lib/dsh/host.js +214 -0
- package/lib/dsh/model-route.d.ts +21 -0
- package/lib/dsh/model-route.js +37 -0
- package/lib/dsh/session-records.d.ts +78 -0
- package/lib/dsh/session-records.js +36 -0
- package/lib/dsh/subagents.d.ts +46 -0
- package/lib/dsh/subagents.js +14 -0
- package/lib/forge/contract.d.ts +231 -0
- package/lib/forge/contract.js +43 -0
- package/lib/forge/ledger.d.ts +143 -0
- package/lib/forge/ledger.js +733 -0
- package/lib/forge/live-mounts.d.ts +45 -0
- package/lib/forge/live-mounts.js +78 -0
- package/lib/forge/marketplace-install.d.ts +3 -0
- package/lib/forge/marketplace-install.js +64 -0
- package/lib/forge/npm.d.ts +31 -0
- package/lib/forge/npm.js +60 -0
- package/lib/forge/projects.d.ts +94 -0
- package/lib/forge/projects.js +336 -0
- package/lib/forge/receipts.d.ts +5 -0
- package/lib/forge/receipts.js +31 -0
- package/lib/forge/row.d.ts +32 -0
- package/lib/forge/row.js +59 -0
- package/lib/forge/service.d.ts +105 -0
- package/lib/forge/service.js +274 -0
- package/lib/forge/settings.d.ts +41 -0
- package/lib/forge/settings.js +115 -0
- package/lib/forge/state.d.ts +69 -0
- package/lib/forge/state.js +150 -0
- package/lib/forge/trust.d.ts +48 -0
- package/lib/forge/trust.js +107 -0
- package/lib/forge/turns.d.ts +13 -0
- package/lib/forge/turns.js +56 -0
- package/lib/hooks/broker/engine.d.ts +112 -0
- package/lib/hooks/broker/engine.js +728 -0
- package/lib/hooks/broker/helpers.d.ts +10 -0
- package/lib/hooks/broker/helpers.js +22 -0
- package/lib/hooks/broker/lifecycle.d.ts +65 -0
- package/lib/hooks/broker/lifecycle.js +342 -0
- package/lib/hooks/broker/session-events.d.ts +22 -0
- package/lib/hooks/broker/session-events.js +196 -0
- package/lib/hooks/broker/tools.d.ts +52 -0
- package/lib/hooks/broker/tools.js +424 -0
- package/lib/hooks/broker/types.d.ts +262 -0
- package/lib/hooks/broker/types.js +1 -0
- package/lib/hooks/broker.d.ts +80 -0
- package/lib/hooks/broker.js +95 -0
- package/lib/hooks/codex-user.d.ts +5 -0
- package/lib/hooks/codex-user.js +109 -0
- package/lib/hooks/condition.d.ts +11 -0
- package/lib/hooks/condition.js +181 -0
- package/lib/hooks/config.d.ts +85 -0
- package/lib/hooks/config.js +150 -0
- package/lib/hooks/matcher.d.ts +20 -0
- package/lib/hooks/matcher.js +39 -0
- package/lib/hooks/notice.d.ts +17 -0
- package/lib/hooks/notice.js +39 -0
- package/lib/hooks/output.d.ts +56 -0
- package/lib/hooks/output.js +214 -0
- package/lib/hooks/permissions.d.ts +14 -0
- package/lib/hooks/permissions.js +109 -0
- package/lib/hooks/process.d.ts +47 -0
- package/lib/hooks/process.js +134 -0
- package/lib/hooks/row.d.ts +17 -0
- package/lib/hooks/row.js +94 -0
- package/lib/hooks/runner.d.ts +85 -0
- package/lib/hooks/runner.js +293 -0
- package/lib/hooks/shell-commands.d.ts +19 -0
- package/lib/hooks/shell-commands.js +177 -0
- package/lib/hooks/shell.d.ts +43 -0
- package/lib/hooks/shell.js +126 -0
- package/lib/hooks/transcript.d.ts +28 -0
- package/lib/hooks/transcript.js +141 -0
- package/lib/hooks/vocabulary.d.ts +57 -0
- package/lib/hooks/vocabulary.js +364 -0
- package/lib/index.d.ts +31 -0
- package/lib/index.js +287 -0
- package/lib/mcp/auth.d.ts +35 -0
- package/lib/mcp/auth.js +178 -0
- package/lib/mcp/catalog.d.ts +36 -0
- package/lib/mcp/catalog.js +61 -0
- package/lib/mcp/mount.d.ts +75 -0
- package/lib/mcp/mount.js +217 -0
- package/lib/mcp/remote.d.ts +45 -0
- package/lib/mcp/remote.js +143 -0
- package/lib/mcp/row.d.ts +9 -0
- package/lib/mcp/row.js +131 -0
- package/lib/mods/bundler.d.ts +38 -0
- package/lib/mods/bundler.js +157 -0
- package/lib/mods/chain.d.ts +103 -0
- package/lib/mods/chain.js +436 -0
- package/lib/mods/claude.d.ts +27 -0
- package/lib/mods/claude.js +56 -0
- package/lib/mods/engine.d.ts +160 -0
- package/lib/mods/engine.js +701 -0
- package/lib/mods/expect.d.ts +8 -0
- package/lib/mods/expect.js +248 -0
- package/lib/mods/host.d.ts +105 -0
- package/lib/mods/host.js +462 -0
- package/lib/mods/loader.d.ts +53 -0
- package/lib/mods/loader.js +227 -0
- package/lib/mods/ops.d.ts +13 -0
- package/lib/mods/ops.js +117 -0
- package/lib/mods/row.d.ts +9 -0
- package/lib/mods/row.js +77 -0
- package/lib/mods/runtime.d.ts +60 -0
- package/lib/mods/runtime.js +133 -0
- package/lib/mods/service/commands.d.ts +17 -0
- package/lib/mods/service/commands.js +121 -0
- package/lib/mods/service/drawings.d.ts +36 -0
- package/lib/mods/service/drawings.js +64 -0
- package/lib/mods/service/sessions.d.ts +63 -0
- package/lib/mods/service/sessions.js +345 -0
- package/lib/mods/service/tools.d.ts +32 -0
- package/lib/mods/service/tools.js +253 -0
- package/lib/mods/service/turns.d.ts +32 -0
- package/lib/mods/service/turns.js +161 -0
- package/lib/mods/service/types.d.ts +151 -0
- package/lib/mods/service/types.js +1 -0
- package/lib/mods/service.d.ts +72 -0
- package/lib/mods/service.js +104 -0
- package/lib/mods/surfaces.d.ts +93 -0
- package/lib/mods/surfaces.js +309 -0
- package/lib/mods/values.d.ts +30 -0
- package/lib/mods/values.js +95 -0
- package/lib/opencode/bun.d.ts +40 -0
- package/lib/opencode/bun.js +156 -0
- package/lib/opencode/convert.d.ts +53 -0
- package/lib/opencode/convert.js +255 -0
- package/lib/opencode/row.d.ts +9 -0
- package/lib/opencode/row.js +86 -0
- package/lib/opencode/runtime/core.d.ts +74 -0
- package/lib/opencode/runtime/core.js +249 -0
- package/lib/opencode/runtime/events.d.ts +80 -0
- package/lib/opencode/runtime/events.js +281 -0
- package/lib/opencode/runtime/mount.d.ts +27 -0
- package/lib/opencode/runtime/mount.js +283 -0
- package/lib/opencode/runtime/server.d.ts +27 -0
- package/lib/opencode/runtime/server.js +353 -0
- package/lib/opencode/runtime/types.d.ts +109 -0
- package/lib/opencode/runtime/types.js +1 -0
- package/lib/opencode/runtime.d.ts +55 -0
- package/lib/opencode/runtime.js +79 -0
- package/lib/porting/port-gap.d.ts +30 -0
- package/lib/porting/port-gap.js +122 -0
- package/lib/porting/port-sessions.d.ts +41 -0
- package/lib/porting/port-sessions.js +98 -0
- package/lib/porting/prompt.d.ts +23 -0
- package/lib/porting/prompt.js +49 -0
- package/lib/porting/row.d.ts +16 -0
- package/lib/porting/row.js +25 -0
- package/lib/porting/verify-port.d.ts +53 -0
- package/lib/porting/verify-port.js +104 -0
- package/lib/presets/fs-search.d.ts +25 -0
- package/lib/presets/fs-search.js +17 -0
- package/lib/presets/tool-filter.d.ts +35 -0
- package/lib/presets/tool-filter.js +91 -0
- package/lib/sessions/claude.d.ts +29 -0
- package/lib/sessions/claude.js +83 -0
- package/lib/sessions/codex.d.ts +30 -0
- package/lib/sessions/codex.js +303 -0
- package/lib/sessions/opencode.d.ts +32 -0
- package/lib/sessions/opencode.js +320 -0
- package/lib/sessions/row.d.ts +14 -0
- package/lib/sessions/row.js +35 -0
- package/lib/shared/invocation.d.ts +169 -0
- package/lib/shared/invocation.js +325 -0
- package/lib/shared/render.d.ts +72 -0
- package/lib/shared/render.js +588 -0
- package/lib/shared/util.d.ts +21 -0
- package/lib/shared/util.js +32 -0
- package/lib/skills/bundled.d.ts +3 -0
- package/lib/skills/bundled.js +42 -0
- package/lib/skills/provider.d.ts +72 -0
- package/lib/skills/provider.js +340 -0
- package/lib/skills/row.d.ts +22 -0
- package/lib/skills/row.js +204 -0
- package/lib/workbench/adapter-coverage.d.ts +47 -0
- package/lib/workbench/adapter-coverage.js +102 -0
- package/lib/workbench/model-tools.d.ts +10 -0
- package/lib/workbench/model-tools.js +122 -0
- package/lib/workbench/overview.d.ts +103 -0
- package/lib/workbench/overview.js +159 -0
- package/lib/workbench/remote.d.ts +118 -0
- package/lib/workbench/remote.js +422 -0
- package/lib/workbench/row.d.ts +15 -0
- package/lib/workbench/row.js +16 -0
- package/lib/workbench/settings.d.ts +15 -0
- package/lib/workbench/settings.js +56 -0
- package/lib/workbench/workbench.d.ts +100 -0
- package/lib/workbench/workbench.js +260 -0
- package/locale/agents/en.json +6 -0
- package/locale/agents/zh.json +6 -0
- package/locale/commands/en.json +6 -0
- package/locale/commands/zh.json +6 -0
- package/locale/context/en.json +6 -0
- package/locale/context/zh.json +6 -0
- package/locale/en.json +6 -0
- package/locale/hooks/en.json +6 -0
- package/locale/hooks/zh.json +6 -0
- package/locale/mcp/en.json +6 -0
- package/locale/mcp/zh.json +6 -0
- package/locale/mods/en.json +6 -0
- package/locale/mods/zh.json +6 -0
- package/locale/opencode/en.json +6 -0
- package/locale/opencode/zh.json +6 -0
- package/locale/porter/en.json +6 -0
- package/locale/porter/zh.json +6 -0
- package/locale/sessions/en.json +6 -0
- package/locale/sessions/zh.json +6 -0
- package/locale/skills/en.json +6 -0
- package/locale/skills/zh.json +6 -0
- package/locale/workbench/en.json +6 -0
- package/locale/workbench/zh.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +246 -3
- package/runtime/opencode-host.mjs +270 -0
- package/skills/harness-forge/SKILL.md +47 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type SessionRegistry } from './sessions.js';
|
|
2
|
+
import type { SessionMods } from './types.js';
|
|
3
|
+
/** Command routing for the sessions' mods. */
|
|
4
|
+
export declare class ModCommands {
|
|
5
|
+
private readonly registry;
|
|
6
|
+
constructor(registry: SessionRegistry);
|
|
7
|
+
/**
|
|
8
|
+
* Every slash command a session runs goes through `command.run`: DSH's
|
|
9
|
+
* command registry raises no event, so the shared interception point
|
|
10
|
+
* (`dsh/commands`) is used. A command a mod registered raises the event
|
|
11
|
+
* from its own handler.
|
|
12
|
+
*/
|
|
13
|
+
wrapCommands(): () => void;
|
|
14
|
+
/** One typed command, through the session's `command.run` hooks when a mod has them. */
|
|
15
|
+
private commandRun;
|
|
16
|
+
registerCommand(session: SessionMods, plugin: string, spec: Record<string, unknown>): () => void;
|
|
17
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Slash commands and mods: every typed command through `command.run`, and
|
|
3
|
+
* the commands mods register.
|
|
4
|
+
* @module @kirkchinese/harness-forge/mods
|
|
5
|
+
*/
|
|
6
|
+
import { createUserMessage } from '@deepseek-ai/dsh-llm';
|
|
7
|
+
import { interceptCommands } from '../../dsh/commands.js';
|
|
8
|
+
import { messageOf } from '../values.js';
|
|
9
|
+
import { dshService } from '../../dsh/host.js';
|
|
10
|
+
import { hasHooks } from './sessions.js';
|
|
11
|
+
/** Command routing for the sessions' mods. */
|
|
12
|
+
export class ModCommands {
|
|
13
|
+
registry;
|
|
14
|
+
constructor(registry) {
|
|
15
|
+
this.registry = registry;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Every slash command a session runs goes through `command.run`: DSH's
|
|
19
|
+
* command registry raises no event, so the shared interception point
|
|
20
|
+
* (`dsh/commands`) is used. A command a mod registered raises the event
|
|
21
|
+
* from its own handler.
|
|
22
|
+
*/
|
|
23
|
+
wrapCommands() {
|
|
24
|
+
const commands = dshService(this.registry.ctx, 'commands');
|
|
25
|
+
if (commands === undefined)
|
|
26
|
+
return () => { };
|
|
27
|
+
return interceptCommands(commands, async (args, next) => {
|
|
28
|
+
const [agent, line, attachments, signal] = args;
|
|
29
|
+
return this.commandRun(agent, line, signal, (text) => next([agent, text, attachments, signal]));
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
/** One typed command, through the session's `command.run` hooks when a mod has them. */
|
|
33
|
+
async commandRun(agent, line, signal, run) {
|
|
34
|
+
const found = this.registry.sessionOf(agent);
|
|
35
|
+
const parsed = /^\/(\S+)(?:\s+([\s\S]*))?$/u.exec(line.trim());
|
|
36
|
+
if (found === undefined || parsed === null)
|
|
37
|
+
return run(line);
|
|
38
|
+
// A resumed session's agent is created when the person acts: the command may arrive while its mods still load
|
|
39
|
+
// (session.start not run, their commands not registered). Route it only once they are ready.
|
|
40
|
+
await found.session.ready;
|
|
41
|
+
if (!hasHooks(found.session.engine, 'command.run') || found.session.commands.has(parsed[1].toLowerCase()))
|
|
42
|
+
return run(line);
|
|
43
|
+
const session = found.session;
|
|
44
|
+
let execution;
|
|
45
|
+
session.nativeCommands.push(async (command, args) => {
|
|
46
|
+
execution = await run(args.length > 0 ? `/${command} ${args}` : `/${command}`);
|
|
47
|
+
if (execution === undefined)
|
|
48
|
+
throw new Error(`/${command} is not a command`);
|
|
49
|
+
if (execution.result.kind === 'error')
|
|
50
|
+
throw new Error(execution.result.text ?? `/${command} failed`);
|
|
51
|
+
return execution.result.text === undefined ? {} : { text: execution.result.text };
|
|
52
|
+
});
|
|
53
|
+
try {
|
|
54
|
+
const result = await session.engine.raise('command.run', {
|
|
55
|
+
command: parsed[1],
|
|
56
|
+
args: (parsed[2] ?? '').trim(),
|
|
57
|
+
origin: { kind: 'composer' },
|
|
58
|
+
presentation: { isFullscreen: false, columns: 120 },
|
|
59
|
+
}, { signal });
|
|
60
|
+
if (Array.isArray(result['context'])) {
|
|
61
|
+
for (const text of result['context'])
|
|
62
|
+
agent.followup(createUserMessage({ content: [{ type: 'text', text: String(text) }], source: { kind: 'user' } }));
|
|
63
|
+
}
|
|
64
|
+
// A hook that answered without the command keeps it from running; its text is the answer.
|
|
65
|
+
if (execution === undefined)
|
|
66
|
+
return { commandId: `mod-${crypto.randomUUID()}`, result: { kind: 'success', ...(typeof result['text'] === 'string' ? { text: result['text'] } : {}) } };
|
|
67
|
+
if (typeof result['text'] === 'string' && result['text'] !== execution.result.text && execution.result.kind === 'success')
|
|
68
|
+
return { ...execution, result: { ...execution.result, text: result['text'] } };
|
|
69
|
+
return execution;
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
if (execution !== undefined)
|
|
73
|
+
return execution;
|
|
74
|
+
return { commandId: `mod-${crypto.randomUUID()}`, result: { kind: 'error', text: messageOf(error) } };
|
|
75
|
+
}
|
|
76
|
+
finally {
|
|
77
|
+
session.nativeCommands.pop();
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
registerCommand(session, plugin, spec) {
|
|
81
|
+
const name = String(spec['name'] ?? '');
|
|
82
|
+
if (!/^[A-Za-z0-9_-]{1,64}$/u.test(name))
|
|
83
|
+
throw new Error('command names are letters, digits, "_" and "-", up to 64 characters');
|
|
84
|
+
const agentCtx = session.agent.ctx;
|
|
85
|
+
const commands = (dshService(agentCtx, 'commands') ?? dshService(this.registry.ctx, 'commands'));
|
|
86
|
+
if (commands === undefined)
|
|
87
|
+
throw new Error('this DSH profile composes no command registry');
|
|
88
|
+
const lowered = name.toLowerCase();
|
|
89
|
+
session.commands.get(lowered)?.();
|
|
90
|
+
const withdraw = commands.register({
|
|
91
|
+
name: lowered,
|
|
92
|
+
description: String(spec['description'] ?? `${plugin}'s /${lowered}`),
|
|
93
|
+
...(typeof spec['argumentHint'] === 'string' ? { input: { hint: spec['argumentHint'] } } : {}),
|
|
94
|
+
handler: async (invocation) => {
|
|
95
|
+
try {
|
|
96
|
+
const result = await session.engine.raise('command.run', {
|
|
97
|
+
command: lowered,
|
|
98
|
+
args: invocation.rawInput.trim(),
|
|
99
|
+
origin: { kind: 'composer' },
|
|
100
|
+
presentation: { isFullscreen: false, columns: 120 },
|
|
101
|
+
}, { signal: invocation.signal });
|
|
102
|
+
if (Array.isArray(result['context'])) {
|
|
103
|
+
for (const text of result['context'])
|
|
104
|
+
session.agent.followup(createUserMessage({ content: [{ type: 'text', text: String(text) }], source: { kind: 'user' } }));
|
|
105
|
+
}
|
|
106
|
+
return { kind: 'success', ...(typeof result['text'] === 'string' && result['text'].length > 0 ? { text: result['text'] } : {}) };
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
return { kind: 'error', text: messageOf(error) };
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
});
|
|
113
|
+
session.commands.set(lowered, withdraw);
|
|
114
|
+
session.commandOwners.set(lowered, plugin);
|
|
115
|
+
return () => {
|
|
116
|
+
withdraw();
|
|
117
|
+
if (session.commands.get(lowered) === withdraw)
|
|
118
|
+
session.commands.delete(lowered);
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The sessions' drawings as the workbench sees them: watching, drawing
|
|
3
|
+
* afresh, and the person's actions on them.
|
|
4
|
+
* @module @kirkchinese/harness-forge/mods
|
|
5
|
+
*/
|
|
6
|
+
import type { SurfaceSnapshot } from '../surfaces.js';
|
|
7
|
+
import type { SessionRegistry } from './sessions.js';
|
|
8
|
+
/** The band and panes of the sessions' mods. */
|
|
9
|
+
export declare class ModDrawings {
|
|
10
|
+
private readonly registry;
|
|
11
|
+
constructor(registry: SessionRegistry);
|
|
12
|
+
/**
|
|
13
|
+
* Watch the band and panes of a session, across the agents it is given (a
|
|
14
|
+
* resumed session gets a new agent when the person acts). Undefined when no
|
|
15
|
+
* mod is loaded at all.
|
|
16
|
+
*/
|
|
17
|
+
watch(sessionId: string, listener: (snapshot: SurfaceSnapshot) => void): (() => void) | undefined;
|
|
18
|
+
/** A session's drawings, drawn afresh; undefined when the session has no mods. */
|
|
19
|
+
drawn(sessionId: string): Promise<SurfaceSnapshot | undefined>;
|
|
20
|
+
/** The person acted on a control of a session's drawing. */
|
|
21
|
+
act(sessionId: string, generation: number, actionId: string, value?: unknown): Promise<SurfaceSnapshot>;
|
|
22
|
+
/**
|
|
23
|
+
* The person closed a pane: `ui.close` with origin `person`; a hook may
|
|
24
|
+
* keep it open with `{ deny }`.
|
|
25
|
+
* @returns the drawing after, and the refusal when a hook kept the pane.
|
|
26
|
+
*/
|
|
27
|
+
closePane(sessionId: string, id: string): Promise<{
|
|
28
|
+
snapshot: SurfaceSnapshot;
|
|
29
|
+
refused?: string;
|
|
30
|
+
}>;
|
|
31
|
+
/** The person moved focus or scrolled in a session's drawing. */
|
|
32
|
+
move(sessionId: string, kind: 'focus' | 'scroll', target: Readonly<Record<string, unknown>>): Promise<{
|
|
33
|
+
snapshot: SurfaceSnapshot;
|
|
34
|
+
refused?: string;
|
|
35
|
+
}>;
|
|
36
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { messageOf } from '../values.js';
|
|
2
|
+
/** The band and panes of the sessions' mods. */
|
|
3
|
+
export class ModDrawings {
|
|
4
|
+
registry;
|
|
5
|
+
constructor(registry) {
|
|
6
|
+
this.registry = registry;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Watch the band and panes of a session, across the agents it is given (a
|
|
10
|
+
* resumed session gets a new agent when the person acts). Undefined when no
|
|
11
|
+
* mod is loaded at all.
|
|
12
|
+
*/
|
|
13
|
+
watch(sessionId, listener) {
|
|
14
|
+
if (this.registry.sources.size === 0 && !this.registry.sessions.has(sessionId))
|
|
15
|
+
return undefined;
|
|
16
|
+
const set = this.registry.watchers.get(sessionId) ?? new Set();
|
|
17
|
+
set.add(listener);
|
|
18
|
+
this.registry.watchers.set(sessionId, set);
|
|
19
|
+
const current = this.registry.sessions.get(sessionId);
|
|
20
|
+
if (current !== undefined)
|
|
21
|
+
listener(current.surfaces.current());
|
|
22
|
+
return () => {
|
|
23
|
+
set.delete(listener);
|
|
24
|
+
if (set.size === 0)
|
|
25
|
+
this.registry.watchers.delete(sessionId);
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/** A session's drawings, drawn afresh; undefined when the session has no mods. */
|
|
29
|
+
async drawn(sessionId) {
|
|
30
|
+
const session = this.registry.sessions.get(sessionId);
|
|
31
|
+
if (session === undefined)
|
|
32
|
+
return undefined;
|
|
33
|
+
await session.ready;
|
|
34
|
+
return session.surfaces.refresh();
|
|
35
|
+
}
|
|
36
|
+
/** The person acted on a control of a session's drawing. */
|
|
37
|
+
async act(sessionId, generation, actionId, value) {
|
|
38
|
+
const session = this.registry.sessions.get(sessionId);
|
|
39
|
+
if (session === undefined)
|
|
40
|
+
throw new Error('that session has no mods loaded');
|
|
41
|
+
return session.surfaces.act(generation, actionId, value);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The person closed a pane: `ui.close` with origin `person`; a hook may
|
|
45
|
+
* keep it open with `{ deny }`.
|
|
46
|
+
* @returns the drawing after, and the refusal when a hook kept the pane.
|
|
47
|
+
*/
|
|
48
|
+
async closePane(sessionId, id) {
|
|
49
|
+
const session = this.registry.sessions.get(sessionId);
|
|
50
|
+
if (session === undefined)
|
|
51
|
+
throw new Error('that session has no mods loaded');
|
|
52
|
+
const result = await session.engine.raise('ui.close', { id, origin: { kind: 'person' } }).catch((error) => ({ deny: messageOf(error) }));
|
|
53
|
+
const refused = typeof result['deny'] === 'string' ? result['deny'] : undefined;
|
|
54
|
+
const snapshot = await session.surfaces.refresh();
|
|
55
|
+
return { snapshot, ...(refused !== undefined ? { refused } : {}) };
|
|
56
|
+
}
|
|
57
|
+
/** The person moved focus or scrolled in a session's drawing. */
|
|
58
|
+
async move(sessionId, kind, target) {
|
|
59
|
+
const session = this.registry.sessions.get(sessionId);
|
|
60
|
+
if (session === undefined)
|
|
61
|
+
throw new Error('that session has no mods loaded');
|
|
62
|
+
return session.surfaces.move(kind, target);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
2
|
+
import { ModsEngine, type CoreContext } from '../engine.js';
|
|
3
|
+
import { type SurfaceSnapshot } from '../surfaces.js';
|
|
4
|
+
import type { ClaudeVerdict, ModSource, ModsServiceHost, SessionMods, ToolExec } from './types.js';
|
|
5
|
+
/** What a session's engine and host reach in the other parts of the mods service. */
|
|
6
|
+
export interface SessionWiring {
|
|
7
|
+
registerCommand(session: SessionMods, plugin: string, spec: Record<string, unknown>): () => void;
|
|
8
|
+
toolCore(session: SessionMods, e: Record<string, unknown>, context: CoreContext): Promise<unknown>;
|
|
9
|
+
checkCore(session: SessionMods, e: Record<string, unknown>): ClaudeVerdict;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* The live sessions' mods: each root session's engine and host, the mods
|
|
13
|
+
* loaded into it, and what the engine does beneath the hooks.
|
|
14
|
+
*/
|
|
15
|
+
export declare class SessionRegistry {
|
|
16
|
+
readonly ctx: Context;
|
|
17
|
+
readonly host: ModsServiceHost;
|
|
18
|
+
private readonly wiring;
|
|
19
|
+
readonly sources: Map<string, ModSource>;
|
|
20
|
+
readonly sessions: Map<string, SessionMods>;
|
|
21
|
+
/** Who watches a session's drawings, by session id: they follow the session across the agents it is given. */
|
|
22
|
+
readonly watchers: Map<string, Set<(snapshot: SurfaceSnapshot) => void>>;
|
|
23
|
+
/** The latest drawing generation of each session, so a new agent's drawings number on from it. */
|
|
24
|
+
private readonly generations;
|
|
25
|
+
/** The model a mod chose for a starting subagent (`agent.spawn`), by the child's session. */
|
|
26
|
+
private readonly childRoutes;
|
|
27
|
+
/** Model choices waiting for the next child a parent session starts. */
|
|
28
|
+
readonly spawnRoutes: Map<string, {
|
|
29
|
+
provider?: string;
|
|
30
|
+
model?: string;
|
|
31
|
+
reasoningEffort?: string;
|
|
32
|
+
}[]>;
|
|
33
|
+
constructor(ctx: Context, host: ModsServiceHost, wiring: SessionWiring);
|
|
34
|
+
/** Add a mod: loaded into every live session now and every session after. Returns the removal. */
|
|
35
|
+
add(source: ModSource): Promise<() => Promise<void>>;
|
|
36
|
+
/** Take one mod out of a live session: its hooks and timers, the commands it registered, the panes it opened. */
|
|
37
|
+
private unloadFrom;
|
|
38
|
+
private recordDisposal;
|
|
39
|
+
/** The loaded mods of a session, for evidence and the workbench. */
|
|
40
|
+
loaded(sessionId: string): readonly string[];
|
|
41
|
+
/**
|
|
42
|
+
* The process is going away: every live session's mods hear
|
|
43
|
+
* `session.end` (reason `other`), as Claude Code's do at exit.
|
|
44
|
+
*/
|
|
45
|
+
shutdown(): Promise<void>;
|
|
46
|
+
private isRoot;
|
|
47
|
+
/** The root session a (sub)agent's events belong to, and the subagent's id. */
|
|
48
|
+
sessionOf(agent: ToolExec['agent']): {
|
|
49
|
+
session: SessionMods;
|
|
50
|
+
agentId?: string;
|
|
51
|
+
} | undefined;
|
|
52
|
+
created(agent: NonNullable<ToolExec['agent']>, signal: AbortSignal | undefined): Promise<void>;
|
|
53
|
+
private loadInto;
|
|
54
|
+
disposed(agent: NonNullable<ToolExec['agent']>): void;
|
|
55
|
+
close(session: SessionMods): void;
|
|
56
|
+
private report;
|
|
57
|
+
private show;
|
|
58
|
+
private engineHost;
|
|
59
|
+
/** Record, once per session, that a mod's hook ran for an event. */
|
|
60
|
+
private ran;
|
|
61
|
+
private core;
|
|
62
|
+
}
|
|
63
|
+
export declare function hasHooks(engine: ModsEngine, event: string): boolean;
|
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-session mod loading and the session host.
|
|
3
|
+
* @module @kirkchinese/harness-forge/mods
|
|
4
|
+
*/
|
|
5
|
+
import { appendFileSync, mkdirSync } from 'node:fs';
|
|
6
|
+
import { dirname } from 'node:path';
|
|
7
|
+
import { createUserMessage } from '@deepseek-ai/dsh-llm';
|
|
8
|
+
import { appendRecord } from '../../dsh/session-records.js';
|
|
9
|
+
import { appendNotice } from '../../hooks/notice.js';
|
|
10
|
+
import { ModsEngine } from '../engine.js';
|
|
11
|
+
import { SessionHost } from '../host.js';
|
|
12
|
+
import { evaluateBundle, createEnvironment, realmOf } from '../loader.js';
|
|
13
|
+
import { OPS } from '../ops.js';
|
|
14
|
+
import { SessionSurfaces } from '../surfaces.js';
|
|
15
|
+
import { elementTable } from '../runtime.js';
|
|
16
|
+
import { isRecord, messageOf } from '../values.js';
|
|
17
|
+
import { dshService } from '../../dsh/host.js';
|
|
18
|
+
import { selectRoute } from '../../dsh/model-route.js';
|
|
19
|
+
/**
|
|
20
|
+
* The live sessions' mods: each root session's engine and host, the mods
|
|
21
|
+
* loaded into it, and what the engine does beneath the hooks.
|
|
22
|
+
*/
|
|
23
|
+
export class SessionRegistry {
|
|
24
|
+
ctx;
|
|
25
|
+
host;
|
|
26
|
+
wiring;
|
|
27
|
+
sources = new Map();
|
|
28
|
+
sessions = new Map();
|
|
29
|
+
/** Who watches a session's drawings, by session id: they follow the session across the agents it is given. */
|
|
30
|
+
watchers = new Map();
|
|
31
|
+
/** The latest drawing generation of each session, so a new agent's drawings number on from it. */
|
|
32
|
+
generations = new Map();
|
|
33
|
+
/** The model a mod chose for a starting subagent (`agent.spawn`), by the child's session. */
|
|
34
|
+
childRoutes = new Map();
|
|
35
|
+
/** Model choices waiting for the next child a parent session starts. */
|
|
36
|
+
spawnRoutes = new Map();
|
|
37
|
+
constructor(ctx, host, wiring) {
|
|
38
|
+
this.ctx = ctx;
|
|
39
|
+
this.host = host;
|
|
40
|
+
this.wiring = wiring;
|
|
41
|
+
}
|
|
42
|
+
/** Add a mod: loaded into every live session now and every session after. Returns the removal. */
|
|
43
|
+
async add(source) {
|
|
44
|
+
this.sources.set(source.name, source);
|
|
45
|
+
for (const session of this.sessions.values())
|
|
46
|
+
await this.loadInto(session, [source]);
|
|
47
|
+
return async () => {
|
|
48
|
+
if (this.sources.get(source.name) !== source)
|
|
49
|
+
return;
|
|
50
|
+
this.sources.delete(source.name);
|
|
51
|
+
const removed = { sessions: 0, commands: 0, panes: 0 };
|
|
52
|
+
let error;
|
|
53
|
+
for (const session of this.sessions.values()) {
|
|
54
|
+
try {
|
|
55
|
+
const one = await this.unloadFrom(session, source.name);
|
|
56
|
+
removed.sessions += 1;
|
|
57
|
+
removed.commands += one.commands;
|
|
58
|
+
removed.panes += one.panes;
|
|
59
|
+
}
|
|
60
|
+
catch (cause) {
|
|
61
|
+
error = messageOf(cause);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
this.recordDisposal(source, removed, error);
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
/** Take one mod out of a live session: its hooks and timers, the commands it registered, the panes it opened. */
|
|
68
|
+
async unloadFrom(session, name) {
|
|
69
|
+
await session.engine.unload(name);
|
|
70
|
+
let commands = 0;
|
|
71
|
+
for (const [command, owner] of [...session.commandOwners]) {
|
|
72
|
+
if (owner !== name)
|
|
73
|
+
continue;
|
|
74
|
+
session.commands.get(command)?.();
|
|
75
|
+
session.commands.delete(command);
|
|
76
|
+
session.commandOwners.delete(command);
|
|
77
|
+
commands += 1;
|
|
78
|
+
}
|
|
79
|
+
const panes = session.surfaces.list().filter(pane => pane.plugin === name);
|
|
80
|
+
for (const pane of panes)
|
|
81
|
+
session.surfaces.close(pane.id);
|
|
82
|
+
return { commands, panes: panes.length };
|
|
83
|
+
}
|
|
84
|
+
recordDisposal(source, removed, error) {
|
|
85
|
+
if (this.host.disposedLog === undefined)
|
|
86
|
+
return;
|
|
87
|
+
try {
|
|
88
|
+
mkdirSync(dirname(this.host.disposedLog), { recursive: true });
|
|
89
|
+
appendFileSync(this.host.disposedLog, `${JSON.stringify({ componentId: source.componentId, mod: source.name, ok: error === undefined, ...removed, ...(error !== undefined ? { error } : {}), at: new Date().toISOString() })}\n`);
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
// Evidence only.
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/** The loaded mods of a session, for evidence and the workbench. */
|
|
96
|
+
loaded(sessionId) {
|
|
97
|
+
return this.sessions.get(sessionId)?.engine.mods.map(mod => mod.ref.name) ?? [];
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* The process is going away: every live session's mods hear
|
|
101
|
+
* `session.end` (reason `other`), as Claude Code's do at exit.
|
|
102
|
+
*/
|
|
103
|
+
async shutdown() {
|
|
104
|
+
const live = [...this.sessions.values()];
|
|
105
|
+
this.sessions.clear();
|
|
106
|
+
await Promise.all(live.map(session => Promise.race([
|
|
107
|
+
session.engine.raise('session.end', { reason: 'other', sessionId: session.agent.session.header.id, resume: { kind: 'none' } }).catch(() => undefined),
|
|
108
|
+
new Promise(resolve => setTimeout(resolve, 5_000).unref()),
|
|
109
|
+
]).finally(() => { this.close(session); })));
|
|
110
|
+
}
|
|
111
|
+
// ---------------------------------------------------------------------------
|
|
112
|
+
// Sessions
|
|
113
|
+
// ---------------------------------------------------------------------------
|
|
114
|
+
isRoot(agent) {
|
|
115
|
+
return (agent.session.header.delegationDepth ?? 0) === 0 && agent.session.header.parentSession === undefined;
|
|
116
|
+
}
|
|
117
|
+
/** The root session a (sub)agent's events belong to, and the subagent's id. */
|
|
118
|
+
sessionOf(agent) {
|
|
119
|
+
if (agent === undefined)
|
|
120
|
+
return undefined;
|
|
121
|
+
const own = this.sessions.get(agent.session.header.id);
|
|
122
|
+
if (own !== undefined)
|
|
123
|
+
return { session: own };
|
|
124
|
+
let parent = agent.session.header.parentSession;
|
|
125
|
+
const agents = dshService(this.ctx, 'agents');
|
|
126
|
+
for (let depth = 0; parent !== undefined && depth < 16; depth++) {
|
|
127
|
+
const session = this.sessions.get(parent);
|
|
128
|
+
if (session !== undefined)
|
|
129
|
+
return { session, agentId: agent.session.header.id };
|
|
130
|
+
parent = agents?.get(parent)?.session.header.parentSession;
|
|
131
|
+
}
|
|
132
|
+
return undefined;
|
|
133
|
+
}
|
|
134
|
+
async created(agent, signal) {
|
|
135
|
+
const parent = agent.session.header.parentSession;
|
|
136
|
+
const waiting = parent === undefined ? undefined : this.spawnRoutes.get(parent);
|
|
137
|
+
const route = waiting?.shift();
|
|
138
|
+
if (route !== undefined) {
|
|
139
|
+
this.childRoutes.set(agent.session.header.id, route);
|
|
140
|
+
selectRoute(agent, route);
|
|
141
|
+
}
|
|
142
|
+
if (!this.isRoot(agent))
|
|
143
|
+
return;
|
|
144
|
+
const interactive = dshService(this.ctx, 'userQuestions') !== undefined;
|
|
145
|
+
const host = new SessionHost({
|
|
146
|
+
ctx: this.ctx,
|
|
147
|
+
shell: this.host.shell,
|
|
148
|
+
storeRoot: this.host.storeRoot,
|
|
149
|
+
directory: (target, abort) => this.host.directory(target, abort),
|
|
150
|
+
settings: source => this.host.settings(agent.session.header, source),
|
|
151
|
+
show: (kind, plugin, text) => { this.show(agent, kind, plugin, text); },
|
|
152
|
+
pane: (plugin, action, args) => (action === 'open' ? session.surfaces.open(plugin, args) : (session.surfaces.close(String(args['id'] ?? '')), { isPlaced: true })),
|
|
153
|
+
panes: () => session.surfaces.list(),
|
|
154
|
+
registerCommand: (plugin, spec) => this.wiring.registerCommand(session, plugin, spec),
|
|
155
|
+
invalidate: () => { session.surfaces.invalidate(); },
|
|
156
|
+
}, agent, interactive);
|
|
157
|
+
const engine = new ModsEngine(this.engineHost(() => session));
|
|
158
|
+
const surfaces = new SessionSurfaces({
|
|
159
|
+
raise: (event, input) => engine.raise(event, input),
|
|
160
|
+
report: (line) => { this.report(line); },
|
|
161
|
+
isWorking: () => session.turns.size > 0,
|
|
162
|
+
detached: (fn) => { engine.outside(fn); },
|
|
163
|
+
}, this.generations.get(agent.session.header.id) ?? 0);
|
|
164
|
+
const sessionId = agent.session.header.id;
|
|
165
|
+
surfaces.watch((snapshot) => {
|
|
166
|
+
this.generations.set(sessionId, snapshot.generation);
|
|
167
|
+
for (const listener of [...this.watchers.get(sessionId) ?? []])
|
|
168
|
+
listener(snapshot);
|
|
169
|
+
});
|
|
170
|
+
const session = { agent, engine, host, surfaces, calls: new Map(), checks: new Map(), turns: new Map(), commands: new Map(), commandOwners: new Map(), nativeCommands: [], recorded: new Set(), isContextRaised: false, ready: Promise.resolve() };
|
|
171
|
+
// A session given a new agent (a resumed one, when the person acts): the previous agent's mods close now.
|
|
172
|
+
const previous = this.sessions.get(sessionId);
|
|
173
|
+
if (previous !== undefined && previous.agent !== agent)
|
|
174
|
+
this.close(previous);
|
|
175
|
+
this.sessions.set(sessionId, session);
|
|
176
|
+
session.ready = (async () => {
|
|
177
|
+
await this.loadInto(session, [...this.sources.values()]);
|
|
178
|
+
if (session.engine.mods.length === 0)
|
|
179
|
+
return;
|
|
180
|
+
const cwd = await this.host.directory(agent, signal ?? new AbortController().signal).catch(() => agent.session.header.cwd ?? process.cwd());
|
|
181
|
+
await engine.raise('session.start', { cwd, surface: interactive ? 'desktop' : null, isInteractive: interactive }).catch((error) => {
|
|
182
|
+
this.report(`session.start: ${messageOf(error)}`);
|
|
183
|
+
});
|
|
184
|
+
if (hasHooks(engine, 'ui.render'))
|
|
185
|
+
session.surfaces.invalidate();
|
|
186
|
+
})();
|
|
187
|
+
await session.ready;
|
|
188
|
+
}
|
|
189
|
+
async loadInto(session, sources) {
|
|
190
|
+
if (sources.length === 0)
|
|
191
|
+
return;
|
|
192
|
+
const definitions = sources.map((source) => {
|
|
193
|
+
const environment = createEnvironment();
|
|
194
|
+
const exports = evaluateBundle(source.bundle, environment);
|
|
195
|
+
const register = exports['register'] ?? exports['default'];
|
|
196
|
+
return {
|
|
197
|
+
name: source.name,
|
|
198
|
+
tier: source.tier,
|
|
199
|
+
root: source.root,
|
|
200
|
+
options: source.options,
|
|
201
|
+
provenance: source.provenance,
|
|
202
|
+
uses: source.uses,
|
|
203
|
+
realm: realmOf(environment),
|
|
204
|
+
...(source.version !== undefined ? { version: source.version } : {}),
|
|
205
|
+
...(source.refusal !== undefined ? { refusal: source.refusal } : typeof register !== 'function' ? { refusal: 'the hooks module exports no register(on, options)' } : {}),
|
|
206
|
+
register: (typeof register === 'function' ? register : () => { }),
|
|
207
|
+
};
|
|
208
|
+
});
|
|
209
|
+
const result = await session.engine.load(definitions);
|
|
210
|
+
for (const refused of result.refused)
|
|
211
|
+
appendNotice(session.agent.session, { kind: 'hook-error', event: 'plugin.register', plugin: refused.name, text: `${refused.name} was refused by ${refused.by}: ${refused.reason}` });
|
|
212
|
+
for (const failed of result.failed)
|
|
213
|
+
appendNotice(session.agent.session, { kind: 'hook-error', event: 'plugin.register', plugin: failed.name, text: `${failed.name}: hooks module did not load: ${failed.error}` });
|
|
214
|
+
}
|
|
215
|
+
disposed(agent) {
|
|
216
|
+
const session = this.sessions.get(agent.session.header.id);
|
|
217
|
+
// The session may already have its next agent; only this agent's mods go.
|
|
218
|
+
if (session === undefined || session.agent !== agent)
|
|
219
|
+
return;
|
|
220
|
+
this.sessions.delete(agent.session.header.id);
|
|
221
|
+
void session.engine.raise('session.end', { reason: 'other', sessionId: agent.session.header.id, resume: { kind: 'none' } })
|
|
222
|
+
.catch(() => undefined)
|
|
223
|
+
.finally(() => { this.close(session); });
|
|
224
|
+
}
|
|
225
|
+
close(session) {
|
|
226
|
+
session.surfaces.dispose();
|
|
227
|
+
session.engine.dispose();
|
|
228
|
+
for (const withdraw of session.host.registrations)
|
|
229
|
+
withdraw();
|
|
230
|
+
for (const withdraw of session.commands.values())
|
|
231
|
+
withdraw();
|
|
232
|
+
}
|
|
233
|
+
report(line) {
|
|
234
|
+
this.ctx.logger.warn(`harness-forge mods: ${line}`);
|
|
235
|
+
}
|
|
236
|
+
show(agent, kind, plugin, text) {
|
|
237
|
+
if (kind === 'debug') {
|
|
238
|
+
this.ctx.logger.debug(`${plugin}: ${text ?? ''}`);
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
if (text === undefined)
|
|
242
|
+
return;
|
|
243
|
+
appendNotice(agent.session, { kind: kind === 'transcript' ? 'system-message' : 'feedback', event: `ui.${kind === 'transcript' ? 'log' : kind}`, plugin, text: `${plugin}: ${text}` });
|
|
244
|
+
}
|
|
245
|
+
// ---------------------------------------------------------------------------
|
|
246
|
+
// Core: what the engine does beneath the hooks
|
|
247
|
+
// ---------------------------------------------------------------------------
|
|
248
|
+
engineHost(session) {
|
|
249
|
+
return {
|
|
250
|
+
core: (event, e, context) => this.core(session(), event, e, context),
|
|
251
|
+
report: (line) => { this.report(line); },
|
|
252
|
+
elements: (surface, _component, plugin) => elementTable(surface, plugin),
|
|
253
|
+
ran: (mod, event) => { this.ran(session(), mod, event); },
|
|
254
|
+
// A drawing that read state is drawn again when the state changes.
|
|
255
|
+
stateWritten: () => { session().surfaces.invalidate(); },
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
/** Record, once per session, that a mod's hook ran for an event. */
|
|
259
|
+
ran(session, mod, event) {
|
|
260
|
+
const key = `${mod}\u0000${event}`;
|
|
261
|
+
if (session.recorded.has(key))
|
|
262
|
+
return;
|
|
263
|
+
session.recorded.add(key);
|
|
264
|
+
try {
|
|
265
|
+
appendRecord(session.agent.session, 'plugin:harness-forge/mod-event', { mod, event });
|
|
266
|
+
}
|
|
267
|
+
catch {
|
|
268
|
+
// Evidence only.
|
|
269
|
+
}
|
|
270
|
+
const source = this.sources.get(mod);
|
|
271
|
+
if (source !== undefined)
|
|
272
|
+
this.host.observe(source.componentId, event);
|
|
273
|
+
}
|
|
274
|
+
async core(session, event, e, context) {
|
|
275
|
+
const input = isRecord(e) ? e : {};
|
|
276
|
+
const op = OPS[event];
|
|
277
|
+
if (op !== undefined && op.kind !== 'event') {
|
|
278
|
+
const answer = await session.host.op(event, input, context);
|
|
279
|
+
// A mod's own `$` call reached the host and was answered: evidence that its code uses the host's API.
|
|
280
|
+
const source = context.mod === undefined ? undefined : this.sources.get(context.mod.ref.name);
|
|
281
|
+
if (source !== undefined && !(isRecord(answer) && typeof answer['deny'] === 'string'))
|
|
282
|
+
this.host.observe(source.componentId, `call:${event}`);
|
|
283
|
+
return answer;
|
|
284
|
+
}
|
|
285
|
+
if (event === 'clock.after' || event === 'clock.every')
|
|
286
|
+
return session.host.op(event, input, context);
|
|
287
|
+
switch (event) {
|
|
288
|
+
case 'plugin.register': return { allow: true };
|
|
289
|
+
case 'session.start': return { cwd: input['cwd'] };
|
|
290
|
+
case 'session.end': return { sessionId: input['sessionId'] };
|
|
291
|
+
case 'turn.start': return { turnId: input['turnId'] };
|
|
292
|
+
case 'turn.complete': return { text: '' };
|
|
293
|
+
case 'prompt.submit': {
|
|
294
|
+
if (context.mod !== undefined) {
|
|
295
|
+
// A mod's own submission: the text arrives as the next prompt, its sender named unless asUser.
|
|
296
|
+
const text = String(input['text'] ?? '');
|
|
297
|
+
const framed = input['asUser'] === true ? text : `Message from the "${context.mod.ref.name}" mod:\n${text}`;
|
|
298
|
+
session.agent.followup(createUserMessage({ content: [{ type: 'text', text: framed }], source: { kind: 'user' } }));
|
|
299
|
+
return { text: framed };
|
|
300
|
+
}
|
|
301
|
+
return { text: input['text'], ...(Array.isArray(input['context']) ? { context: input['context'] } : {}) };
|
|
302
|
+
}
|
|
303
|
+
case 'prompt.context': return { blocks: input['blocks'] ?? [], ...(input['instructionFiles'] !== undefined ? { instructionFiles: input['instructionFiles'] } : {}) };
|
|
304
|
+
case 'command.run': {
|
|
305
|
+
const name = String(input['command'] ?? '');
|
|
306
|
+
if (session.commands.has(name))
|
|
307
|
+
return { text: `no command.run hook answered /${name}` };
|
|
308
|
+
const native = context.mod === undefined ? session.nativeCommands.at(-1) : undefined;
|
|
309
|
+
if (native !== undefined)
|
|
310
|
+
return native(name, String(input['args'] ?? ''));
|
|
311
|
+
return session.host.runCommand(name, String(input['args'] ?? ''), context.signal);
|
|
312
|
+
}
|
|
313
|
+
case 'agent.spawn': {
|
|
314
|
+
// The run starts in DSH's own delegation; a changed model applies to the child it creates.
|
|
315
|
+
const model = typeof input['model'] === 'string' ? input['model'] : undefined;
|
|
316
|
+
return { model: model ?? String(input['parentModel'] ?? '') };
|
|
317
|
+
}
|
|
318
|
+
case 'tool.call': return this.wiring.toolCore(session, input, context);
|
|
319
|
+
case 'tool.check': return this.wiring.checkCore(session, input);
|
|
320
|
+
case 'session.append': return session.host.appendNote(context.mod?.ref.name ?? 'engine', input);
|
|
321
|
+
case 'ui.render': return { type: 'engine', ref: 0 };
|
|
322
|
+
case 'ui.press':
|
|
323
|
+
case 'ui.input':
|
|
324
|
+
case 'ui.select': return session.surfaces.runCallback(event, input);
|
|
325
|
+
case 'ui.focus':
|
|
326
|
+
case 'ui.scroll': return {};
|
|
327
|
+
case 'agent.offer': return { isOffered: true };
|
|
328
|
+
case 'tool.describe': return { description: input['description'] ?? '' };
|
|
329
|
+
case 'command.describe': return { description: input['description'] ?? '', isHidden: input['isHidden'] === true };
|
|
330
|
+
case 'skill.prompt':
|
|
331
|
+
case 'attribution.text':
|
|
332
|
+
case 'prompt.section':
|
|
333
|
+
case 'prompt.attachment': return { text: input['text'] ?? null };
|
|
334
|
+
case 'prompt.compose': return { sections: [] };
|
|
335
|
+
case 'session.compact': return { skip: 'this session compacts on DSH\'s own schedule' };
|
|
336
|
+
default:
|
|
337
|
+
if (event.startsWith('classic.'))
|
|
338
|
+
return {};
|
|
339
|
+
throw new Error(`${event} is not served by this host`);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
export function hasHooks(engine, event) {
|
|
344
|
+
return engine.registered.some(hook => hook.pattern === event || hook.pattern === '*' || (hook.pattern.endsWith('.*') && event.startsWith(hook.pattern.slice(0, -1))) || (hook.pattern.startsWith('!') && hook.pattern.slice(1) !== event));
|
|
345
|
+
}
|