@kirkchinese/harness-forge 0.0.0-stage → 0.5.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 +46 -2
- package/assets/icon.svg +12 -0
- package/cordis.patch.yml +28 -0
- package/docs/DETAIL.zh.md +47 -0
- package/lib/agents/presets.d.ts +98 -0
- package/lib/agents/presets.js +292 -0
- package/lib/agents/profiles.d.ts +58 -0
- package/lib/agents/profiles.js +279 -0
- package/lib/agents/row.d.ts +17 -0
- package/lib/agents/row.js +148 -0
- package/lib/client/paths.d.ts +12 -0
- package/lib/client/paths.js +68 -0
- package/lib/client/styles.d.ts +8 -0
- package/lib/client/styles.js +89 -0
- package/lib/client/typert.d.ts +25 -0
- package/lib/client/typert.js +158 -0
- package/lib/client.js +23823 -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 +110 -0
- package/lib/dsh/host.js +217 -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 +271 -0
- package/lib/forge/contract.js +45 -0
- package/lib/forge/follow.d.ts +30 -0
- package/lib/forge/follow.js +73 -0
- package/lib/forge/ledger.d.ts +206 -0
- package/lib/forge/ledger.js +1005 -0
- package/lib/forge/live-mounts.d.ts +45 -0
- package/lib/forge/live-mounts.js +78 -0
- package/lib/forge/npm.d.ts +31 -0
- package/lib/forge/npm.js +60 -0
- package/lib/forge/projects.d.ts +97 -0
- package/lib/forge/projects.js +343 -0
- package/lib/forge/receipts.d.ts +5 -0
- package/lib/forge/receipts.js +31 -0
- package/lib/forge/remote/archive.d.ts +65 -0
- package/lib/forge/remote/archive.js +565 -0
- package/lib/forge/remote/code-parts.d.ts +18 -0
- package/lib/forge/remote/code-parts.js +76 -0
- package/lib/forge/remote/fetch.d.ts +265 -0
- package/lib/forge/remote/fetch.js +1133 -0
- package/lib/forge/remote/service.d.ts +256 -0
- package/lib/forge/remote/service.js +868 -0
- package/lib/forge/row.d.ts +32 -0
- package/lib/forge/row.js +59 -0
- package/lib/forge/service.d.ts +115 -0
- package/lib/forge/service.js +290 -0
- package/lib/forge/settings.d.ts +43 -0
- package/lib/forge/settings.js +122 -0
- package/lib/forge/state.d.ts +129 -0
- package/lib/forge/state.js +266 -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 +722 -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 +27 -0
- package/lib/hooks/broker/session-events.js +205 -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 +264 -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 +87 -0
- package/lib/hooks/condition.js +602 -0
- package/lib/hooks/config.d.ts +85 -0
- package/lib/hooks/config.js +154 -0
- package/lib/hooks/glob.d.ts +41 -0
- package/lib/hooks/glob.js +114 -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 +19 -0
- package/lib/hooks/permissions.js +202 -0
- package/lib/hooks/powershell-aliases.d.ts +19 -0
- package/lib/hooks/powershell-aliases.js +58 -0
- package/lib/hooks/process.d.ts +64 -0
- package/lib/hooks/process.js +289 -0
- package/lib/hooks/row.d.ts +17 -0
- package/lib/hooks/row.js +96 -0
- package/lib/hooks/runner.d.ts +98 -0
- package/lib/hooks/runner.js +322 -0
- package/lib/hooks/shell-commands.d.ts +28 -0
- package/lib/hooks/shell-commands.js +241 -0
- package/lib/hooks/shell.d.ts +44 -0
- package/lib/hooks/shell.js +129 -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 +366 -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 +39 -0
- package/lib/mcp/catalog.js +69 -0
- package/lib/mcp/mount.d.ts +113 -0
- package/lib/mcp/mount.js +317 -0
- package/lib/mcp/policy.d.ts +42 -0
- package/lib/mcp/policy.js +43 -0
- package/lib/mcp/remote.d.ts +45 -0
- package/lib/mcp/remote.js +133 -0
- package/lib/mcp/row.d.ts +9 -0
- package/lib/mcp/row.js +225 -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 +231 -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 +33 -0
- package/lib/mods/service/turns.js +136 -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 +168 -0
- package/lib/opencode/convert.d.ts +53 -0
- package/lib/opencode/convert.js +255 -0
- package/lib/opencode/row.d.ts +22 -0
- package/lib/opencode/row.js +104 -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 +354 -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/adapter-paths.d.ts +13 -0
- package/lib/porting/adapter-paths.js +120 -0
- package/lib/porting/port-gap.d.ts +36 -0
- package/lib/porting/port-gap.js +158 -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 +51 -0
- package/lib/porting/row.d.ts +16 -0
- package/lib/porting/row.js +27 -0
- package/lib/porting/verify-port.d.ts +46 -0
- package/lib/porting/verify-port.js +82 -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/chat-import.d.ts +48 -0
- package/lib/sessions/chat-import.js +44 -0
- package/lib/sessions/claude.d.ts +35 -0
- package/lib/sessions/claude.js +104 -0
- package/lib/sessions/codex-discover.d.ts +34 -0
- package/lib/sessions/codex-discover.js +205 -0
- package/lib/sessions/codex-meta.d.ts +63 -0
- package/lib/sessions/codex-meta.js +148 -0
- package/lib/sessions/codex-text.d.ts +9 -0
- package/lib/sessions/codex-text.js +44 -0
- package/lib/sessions/codex.d.ts +42 -0
- package/lib/sessions/codex.js +270 -0
- package/lib/sessions/opencode.d.ts +38 -0
- package/lib/sessions/opencode.js +339 -0
- package/lib/sessions/persist.d.ts +162 -0
- package/lib/sessions/persist.js +377 -0
- package/lib/sessions/row.d.ts +15 -0
- package/lib/sessions/row.js +101 -0
- package/lib/sessions/turns.d.ts +31 -0
- package/lib/sessions/turns.js +63 -0
- package/lib/sessions/zstd.d.ts +15 -0
- package/lib/sessions/zstd.js +152 -0
- package/lib/shared/invocation.d.ts +169 -0
- package/lib/shared/invocation.js +325 -0
- package/lib/shared/render.d.ts +101 -0
- package/lib/shared/render.js +1353 -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 +74 -0
- package/lib/skills/provider.js +346 -0
- package/lib/skills/row.d.ts +22 -0
- package/lib/skills/row.js +204 -0
- package/lib/workbench/adapter-coverage.d.ts +65 -0
- package/lib/workbench/adapter-coverage.js +141 -0
- package/lib/workbench/model-tools.d.ts +10 -0
- package/lib/workbench/model-tools.js +122 -0
- package/lib/workbench/overview.d.ts +153 -0
- package/lib/workbench/overview.js +246 -0
- package/lib/workbench/remote.d.ts +161 -0
- package/lib/workbench/remote.js +561 -0
- package/lib/workbench/row.d.ts +15 -0
- package/lib/workbench/row.js +16 -0
- package/lib/workbench/settings.d.ts +19 -0
- package/lib/workbench/settings.js +60 -0
- package/lib/workbench/workbench.d.ts +130 -0
- package/lib/workbench/workbench.js +361 -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 +7 -0
- package/package.json +247 -3
- package/runtime/opencode-host.mjs +270 -0
- package/skills/harness-forge/SKILL.md +47 -0
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { CommandDefinition, CommandInvocation } from '@deepseek-ai/dsh-commands';
|
|
2
|
+
import { type CommandComponent } from '@kirkchinese/harness-forge-core';
|
|
3
|
+
import type { FrontmatterHooks, HooksApi } from '../forge/contract.js';
|
|
4
|
+
import { type TrustCheck } from '../forge/trust.js';
|
|
5
|
+
import type { ForeignShell } from '../hooks/shell.js';
|
|
6
|
+
import { type Invocations } from '../shared/invocation.js';
|
|
7
|
+
/** Host services a command needs while it runs. */
|
|
8
|
+
export interface CommandHost {
|
|
9
|
+
readonly shell: ForeignShell;
|
|
10
|
+
/** The hooks row, while it runs. */
|
|
11
|
+
hooks(): HooksApi | undefined;
|
|
12
|
+
readonly invocations?: Invocations;
|
|
13
|
+
directory(agent: CommandInvocation['agent'], signal: AbortSignal): Promise<string>;
|
|
14
|
+
observe(componentId: string, ok: boolean, detail: string): void;
|
|
15
|
+
/** A project's command runs shell lines only while its workspace is trusted, checked each time it renders. */
|
|
16
|
+
readonly trust?: TrustCheck;
|
|
17
|
+
/** OpenCode's `command.execute.before`: the prompt's parts as plugins leave them. */
|
|
18
|
+
beforeSubmit?(agent: CommandInvocation['agent'], command: {
|
|
19
|
+
name: string;
|
|
20
|
+
args: string;
|
|
21
|
+
}, prompt: string): Promise<string[] | {
|
|
22
|
+
stopped: string;
|
|
23
|
+
}>;
|
|
24
|
+
}
|
|
25
|
+
/** The plugin facts a command body may reference. */
|
|
26
|
+
export interface CommandOrigin {
|
|
27
|
+
readonly pluginRoot: string;
|
|
28
|
+
readonly dataDir: string;
|
|
29
|
+
/** The command's name in its own harness (`plugin:command`), for hook matchers. */
|
|
30
|
+
readonly foreignName?: string;
|
|
31
|
+
readonly pluginId?: string;
|
|
32
|
+
/** The command's frontmatter `hooks`: they run for the rest of the session once it is invoked, as a skill's do. */
|
|
33
|
+
readonly hooks?: FrontmatterHooks;
|
|
34
|
+
/** Whose command this is, for what `@file` may attach (default: an installed plugin's). */
|
|
35
|
+
readonly scope?: 'user' | 'project' | 'plugin';
|
|
36
|
+
/** A project's command: the project directory, whose workspace must be trusted for its shell lines to run. */
|
|
37
|
+
readonly projectRoot?: string;
|
|
38
|
+
}
|
|
39
|
+
/** Build the DSH command for one foreign command. */
|
|
40
|
+
export declare function commandDefinition(component: CommandComponent, name: string, origin: CommandOrigin, host: CommandHost): CommandDefinition;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Foreign Markdown commands as DSH commands. Invoking one renders its body the
|
|
3
|
+
* way Claude Code does (arguments, path variables, shell injection, `@` files),
|
|
4
|
+
* fires `UserPromptExpansion` hooks, and submits the result as the user's next
|
|
5
|
+
* message. Commands stay out of the model's tool list, as in Claude Code.
|
|
6
|
+
* @module @kirkchinese/harness-forge
|
|
7
|
+
*/
|
|
8
|
+
import { createUserMessage } from '@deepseek-ai/dsh-llm';
|
|
9
|
+
import { toolList } from '@kirkchinese/harness-forge-core';
|
|
10
|
+
import { untrustedShell } from '../forge/trust.js';
|
|
11
|
+
import { directivesOf } from '../shared/invocation.js';
|
|
12
|
+
import { renderBody, renderCodexPrompt, withAttachments } from '../shared/render.js';
|
|
13
|
+
/** Build the DSH command for one foreign command. */
|
|
14
|
+
export function commandDefinition(component, name, origin, host) {
|
|
15
|
+
const hint = typeof component.frontmatter['argument-hint'] === 'string' && component.frontmatter['argument-hint'].trim().length > 0
|
|
16
|
+
? component.frontmatter['argument-hint']
|
|
17
|
+
: undefined;
|
|
18
|
+
const description = component.description !== undefined && component.description.trim().length > 0
|
|
19
|
+
? component.description.trim()
|
|
20
|
+
: `Command from ${component.path ?? component.name}`;
|
|
21
|
+
return {
|
|
22
|
+
name,
|
|
23
|
+
description,
|
|
24
|
+
...(hint !== undefined ? { input: { hint } } : {}),
|
|
25
|
+
handler: async (invocation) => {
|
|
26
|
+
let selected = false;
|
|
27
|
+
let submitted = false;
|
|
28
|
+
let spec;
|
|
29
|
+
try {
|
|
30
|
+
const cwd = await host.directory(invocation.agent, invocation.signal);
|
|
31
|
+
const trust = origin.projectRoot !== undefined ? host.trust?.(origin.projectRoot) : undefined;
|
|
32
|
+
const rendered = component.syntax === 'codex' ? renderCodexPrompt(component.body, invocation.rawInput) : await renderBody(component.body, {
|
|
33
|
+
rawArguments: invocation.rawInput,
|
|
34
|
+
argumentNames: toolList(component.frontmatter['arguments']) ?? [],
|
|
35
|
+
cwd,
|
|
36
|
+
pluginRoot: origin.pluginRoot,
|
|
37
|
+
dataDir: origin.dataDir,
|
|
38
|
+
sessionId: invocation.agent.session.header.id,
|
|
39
|
+
shell: host.shell,
|
|
40
|
+
scope: origin.scope ?? 'plugin',
|
|
41
|
+
...(trust !== undefined && !trust.trusted ? { refuseShell: untrustedShell(trust.workspace) } : {}),
|
|
42
|
+
...(typeof component.frontmatter['shell'] === 'string' ? { shellKind: component.frontmatter['shell'] } : {}),
|
|
43
|
+
signal: invocation.signal,
|
|
44
|
+
});
|
|
45
|
+
const prompt = withAttachments(rendered);
|
|
46
|
+
if (prompt.length === 0)
|
|
47
|
+
return { kind: 'error', text: `/${name} has an empty body` };
|
|
48
|
+
const expansion = await (host.hooks()?.expandCommand(invocation.agent, {
|
|
49
|
+
name,
|
|
50
|
+
foreignName: origin.foreignName ?? name,
|
|
51
|
+
args: invocation.rawInput.trim(),
|
|
52
|
+
expanded: [prompt].join(''),
|
|
53
|
+
}, invocation.signal) ?? { context: [] });
|
|
54
|
+
if (expansion.blocked !== undefined) {
|
|
55
|
+
host.observe(component.id, true, 'blocked by a prompt hook');
|
|
56
|
+
return { kind: 'error', text: expansion.blocked };
|
|
57
|
+
}
|
|
58
|
+
if (origin.hooks !== undefined)
|
|
59
|
+
host.hooks()?.add({ ...origin.hooks, origin: 'skill', sessionId: invocation.agent.session.header.id });
|
|
60
|
+
spec = {
|
|
61
|
+
name,
|
|
62
|
+
kind: 'command',
|
|
63
|
+
pluginId: origin.pluginId ?? '',
|
|
64
|
+
componentId: component.id,
|
|
65
|
+
frontmatter: component.frontmatter,
|
|
66
|
+
places: { pluginRoot: origin.pluginRoot, dataDir: origin.dataDir },
|
|
67
|
+
};
|
|
68
|
+
const directives = directivesOf(component.frontmatter);
|
|
69
|
+
if (directives.fork !== undefined && host.invocations !== undefined) {
|
|
70
|
+
// `context: fork`: the prompt is the task of a forked agent, not a message of this conversation.
|
|
71
|
+
const result = await host.invocations.fork(invocation.agent, spec, directives, [prompt, ...expansion.context].join('\n\n'), invocation.signal);
|
|
72
|
+
host.observe(component.id, true, 'ran in a forked agent');
|
|
73
|
+
if (directives.fork.background)
|
|
74
|
+
return { kind: 'success', text: result };
|
|
75
|
+
invocation.agent.followup(createUserMessage({
|
|
76
|
+
content: [{ type: 'text', text: `<skill-result name="${name}">\n${result}\n</skill-result>` }],
|
|
77
|
+
source: { kind: 'user' },
|
|
78
|
+
}));
|
|
79
|
+
return { kind: 'success' };
|
|
80
|
+
}
|
|
81
|
+
// The route is selected before the prompt is submitted (a step's route is fixed before it runs); the
|
|
82
|
+
// frontmatter's grants are queued only once the prompt is actually submitted.
|
|
83
|
+
selected = true;
|
|
84
|
+
await host.invocations?.preselect(invocation.agent, spec, invocation.signal);
|
|
85
|
+
const parts = host.beforeSubmit === undefined ? [prompt] : await host.beforeSubmit(invocation.agent, { name: origin.foreignName ?? name, args: invocation.rawInput.trim() }, prompt);
|
|
86
|
+
if (!Array.isArray(parts)) {
|
|
87
|
+
host.invocations?.release(invocation.agent.session.header.id);
|
|
88
|
+
host.observe(component.id, true, `handled by a plugin: ${parts.stopped}`);
|
|
89
|
+
return { kind: 'success', text: parts.stopped };
|
|
90
|
+
}
|
|
91
|
+
host.invocations?.queue(invocation.agent.session.header.id, spec);
|
|
92
|
+
invocation.agent.followup(createUserMessage({
|
|
93
|
+
content: [...parts, ...expansion.context].map(text => ({ type: 'text', text })),
|
|
94
|
+
source: { kind: 'user' },
|
|
95
|
+
}));
|
|
96
|
+
submitted = true;
|
|
97
|
+
host.observe(component.id, true, `submitted${rendered.commands > 0 ? ` after ${rendered.commands} shell command(s)` : ''}`);
|
|
98
|
+
return { kind: 'success' };
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
// Nothing reached the conversation: no route or grant of this command waits for an unrelated turn.
|
|
102
|
+
if (selected && !submitted && spec !== undefined) {
|
|
103
|
+
const sessionId = invocation.agent.session.header.id;
|
|
104
|
+
host.invocations?.unqueue(sessionId, spec);
|
|
105
|
+
host.invocations?.release(sessionId);
|
|
106
|
+
}
|
|
107
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
108
|
+
host.observe(component.id, false, message);
|
|
109
|
+
return { kind: 'error', text: `/${name} failed: ${message}` };
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
2
|
+
/** One output style. */
|
|
3
|
+
export interface OutputStyle {
|
|
4
|
+
readonly name: string;
|
|
5
|
+
readonly description: string;
|
|
6
|
+
readonly instructions: string;
|
|
7
|
+
readonly keepCodingInstructions: boolean;
|
|
8
|
+
readonly forceForPlugin: boolean;
|
|
9
|
+
/** The plugin that ships it; undefined for built-in and user styles. */
|
|
10
|
+
readonly plugin?: string;
|
|
11
|
+
readonly componentId?: string;
|
|
12
|
+
}
|
|
13
|
+
/** Output styles and the current selection. */
|
|
14
|
+
export declare class OutputStyles {
|
|
15
|
+
private readonly load;
|
|
16
|
+
private readonly save;
|
|
17
|
+
/** The `outputStyle` imported settings name, the starting choice while the user has picked none. */
|
|
18
|
+
private readonly suggested;
|
|
19
|
+
private readonly styles;
|
|
20
|
+
private selected;
|
|
21
|
+
/**
|
|
22
|
+
* @param load - the saved selection, if any.
|
|
23
|
+
* @param save - persists a new selection (`undefined` is Default).
|
|
24
|
+
*/
|
|
25
|
+
constructor(load: () => string | undefined, save: (name: string | undefined) => void,
|
|
26
|
+
/** The `outputStyle` imported settings name, the starting choice while the user has picked none. */
|
|
27
|
+
suggested?: () => string | undefined);
|
|
28
|
+
/** Add one style file; returns its name and the disposer. */
|
|
29
|
+
add(path: string, plugin: string | undefined, componentId: string): {
|
|
30
|
+
readonly name: string;
|
|
31
|
+
dispose(): void;
|
|
32
|
+
};
|
|
33
|
+
/** Use a settings `outputStyle` as the starting choice when the user has not picked one. */
|
|
34
|
+
suggest(name: string): void;
|
|
35
|
+
list(): readonly OutputStyle[];
|
|
36
|
+
/** The style in effect: a forcing plugin style, else the selection (exact name, as Claude Code matches). */
|
|
37
|
+
current(): OutputStyle | undefined;
|
|
38
|
+
/** Select a style by name, ignoring case as `/output-style` does; `default` clears it. */
|
|
39
|
+
select(input: string): OutputStyle | 'default';
|
|
40
|
+
/** Add the style section to every main-conversation system prompt. Returns the disposer. */
|
|
41
|
+
install(ctx: Context): () => void;
|
|
42
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Claude Code output styles in DSH. A style is a set of instructions added to
|
|
3
|
+
* the system prompt of the main conversation (not of delegated agents). The
|
|
4
|
+
* user picks one with `/output-style <name>`; `outputStyle` in imported
|
|
5
|
+
* Claude settings is the starting choice, and a plugin style with
|
|
6
|
+
* `force-for-plugin: true` applies while its plugin is installed.
|
|
7
|
+
*
|
|
8
|
+
* The four built-in styles are written from Claude Code's documented
|
|
9
|
+
* behavior of each.
|
|
10
|
+
* @module @kirkchinese/harness-forge
|
|
11
|
+
*/
|
|
12
|
+
import { basename } from 'node:path';
|
|
13
|
+
import { booleanValue, readMarkdownAsset } from '@kirkchinese/harness-forge-core';
|
|
14
|
+
import { dshService } from '../dsh/host.js';
|
|
15
|
+
const BUILT_IN = [
|
|
16
|
+
{
|
|
17
|
+
name: 'Proactive',
|
|
18
|
+
description: 'Start work right away and make reasonable assumptions instead of asking about routine decisions',
|
|
19
|
+
keepCodingInstructions: true,
|
|
20
|
+
forceForPlugin: false,
|
|
21
|
+
instructions: [
|
|
22
|
+
'Start implementing as soon as the user sends a task. Make reasonable assumptions about routine decisions instead of stopping to ask, and state the assumption briefly when it matters. Do not switch to plan mode unless the user asks for a plan.',
|
|
23
|
+
'Before an action that deletes data or changes a shared or production system, check with the user in the conversation first.',
|
|
24
|
+
].join('\n\n'),
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
name: 'Concise',
|
|
28
|
+
description: 'Lead with the result; leave out preamble, narration and recaps',
|
|
29
|
+
keepCodingInstructions: true,
|
|
30
|
+
forceForPlugin: false,
|
|
31
|
+
instructions: [
|
|
32
|
+
'The first sentence of every response states what happened or what the answer is. Leave out lead-ins, step-by-step narration of what you are doing, and closing recaps. Answer a simple question in one to three sentences.',
|
|
33
|
+
'Do the engineering work as thoroughly as usual. Write at full length when the user asks for an explanation or detail, and keep the complete content of error reports, failing test output, security warnings and confirmations for destructive actions.',
|
|
34
|
+
].join('\n\n'),
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
name: 'Explanatory',
|
|
38
|
+
description: 'Add short Insight blocks that explain the choices behind the code',
|
|
39
|
+
keepCodingInstructions: true,
|
|
40
|
+
forceForPlugin: false,
|
|
41
|
+
instructions: [
|
|
42
|
+
'Do the task as usual, and add short explanations of why you made the choices you made. Put each explanation in the conversation, before or after the code it is about, never in the files as comments, in this form:',
|
|
43
|
+
'`★ Insight ─────────────────────────────────────`\n- two or three points specific to this codebase or the code you wrote\n`─────────────────────────────────────────────────`',
|
|
44
|
+
].join('\n\n'),
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
name: 'Learning',
|
|
48
|
+
description: 'Explain choices and leave small pieces of code for the user to write',
|
|
49
|
+
keepCodingInstructions: true,
|
|
50
|
+
forceForPlugin: false,
|
|
51
|
+
instructions: [
|
|
52
|
+
'Add the same Insight blocks as the Explanatory style:',
|
|
53
|
+
'`★ Insight ─────────────────────────────────────`\n- two or three points specific to this codebase or the code you wrote\n`─────────────────────────────────────────────────`',
|
|
54
|
+
'Handle routine implementation yourself. When you reach a piece with a real design decision (error handling, a data structure, business logic with more than one valid approach), leave a few lines for the user: mark the spot with a `TODO(human)` comment, then send a request headed "● Learn by Doing" with Context (what is built), Your Task (the file, the function, what to write) and Guidance (what to weigh). Then stop and wait. When the user says they are done, respond with one Insight about their code and continue the task.',
|
|
55
|
+
].join('\n\n'),
|
|
56
|
+
},
|
|
57
|
+
];
|
|
58
|
+
/** Output styles and the current selection. */
|
|
59
|
+
export class OutputStyles {
|
|
60
|
+
load;
|
|
61
|
+
save;
|
|
62
|
+
suggested;
|
|
63
|
+
styles = new Map();
|
|
64
|
+
selected;
|
|
65
|
+
/**
|
|
66
|
+
* @param load - the saved selection, if any.
|
|
67
|
+
* @param save - persists a new selection (`undefined` is Default).
|
|
68
|
+
*/
|
|
69
|
+
constructor(load, save,
|
|
70
|
+
/** The `outputStyle` imported settings name, the starting choice while the user has picked none. */
|
|
71
|
+
suggested = () => undefined) {
|
|
72
|
+
this.load = load;
|
|
73
|
+
this.save = save;
|
|
74
|
+
this.suggested = suggested;
|
|
75
|
+
for (const style of BUILT_IN)
|
|
76
|
+
this.styles.set(style.name, style);
|
|
77
|
+
this.selected = load();
|
|
78
|
+
}
|
|
79
|
+
/** Add one style file; returns its name and the disposer. */
|
|
80
|
+
add(path, plugin, componentId) {
|
|
81
|
+
const asset = readMarkdownAsset(path);
|
|
82
|
+
if (asset === undefined)
|
|
83
|
+
throw new Error(`cannot read output style ${path}`);
|
|
84
|
+
const { data, body } = asset;
|
|
85
|
+
const declared = typeof data['name'] === 'string' && data['name'].trim().length > 0 ? data['name'].trim() : basename(path).replace(/\.md$/iu, '');
|
|
86
|
+
const name = this.styles.has(declared) && plugin !== undefined ? `${plugin}:${declared}` : declared;
|
|
87
|
+
const style = {
|
|
88
|
+
name,
|
|
89
|
+
description: typeof data['description'] === 'string' ? data['description'] : '',
|
|
90
|
+
instructions: body.trim(),
|
|
91
|
+
keepCodingInstructions: booleanValue(data['keep-coding-instructions']) === true,
|
|
92
|
+
forceForPlugin: plugin !== undefined && booleanValue(data['force-for-plugin']) === true,
|
|
93
|
+
...(plugin !== undefined ? { plugin } : {}),
|
|
94
|
+
componentId,
|
|
95
|
+
};
|
|
96
|
+
this.styles.set(name, style);
|
|
97
|
+
return { name, dispose: () => { if (this.styles.get(name) === style)
|
|
98
|
+
this.styles.delete(name); } };
|
|
99
|
+
}
|
|
100
|
+
/** Use a settings `outputStyle` as the starting choice when the user has not picked one. */
|
|
101
|
+
suggest(name) {
|
|
102
|
+
if (this.selected === undefined && this.load() === undefined)
|
|
103
|
+
this.selected = name;
|
|
104
|
+
}
|
|
105
|
+
list() {
|
|
106
|
+
return [...this.styles.values()];
|
|
107
|
+
}
|
|
108
|
+
/** The style in effect: a forcing plugin style, else the selection (exact name, as Claude Code matches). */
|
|
109
|
+
current() {
|
|
110
|
+
const forced = [...this.styles.values()].find(style => style.forceForPlugin);
|
|
111
|
+
if (forced !== undefined)
|
|
112
|
+
return forced;
|
|
113
|
+
const selected = this.selected ?? (this.load() === undefined ? this.suggested() : undefined);
|
|
114
|
+
return selected === undefined ? undefined : this.styles.get(selected);
|
|
115
|
+
}
|
|
116
|
+
/** Select a style by name, ignoring case as `/output-style` does; `default` clears it. */
|
|
117
|
+
select(input) {
|
|
118
|
+
const wanted = input.trim().toLowerCase();
|
|
119
|
+
if (wanted === 'default') {
|
|
120
|
+
this.selected = undefined;
|
|
121
|
+
this.save(undefined);
|
|
122
|
+
return 'default';
|
|
123
|
+
}
|
|
124
|
+
const style = [...this.styles.values()].find(item => item.name.toLowerCase() === wanted);
|
|
125
|
+
if (style === undefined)
|
|
126
|
+
throw new Error(`no output style named "${input.trim()}"; available: default, ${[...this.styles.keys()].join(', ')}`);
|
|
127
|
+
this.selected = style.name;
|
|
128
|
+
this.save(style.name);
|
|
129
|
+
return style;
|
|
130
|
+
}
|
|
131
|
+
/** Add the style section to every main-conversation system prompt. Returns the disposer. */
|
|
132
|
+
install(ctx) {
|
|
133
|
+
const prompt = dshService(ctx, 'systemPrompt');
|
|
134
|
+
if (prompt === undefined)
|
|
135
|
+
return () => { };
|
|
136
|
+
return prompt.section({
|
|
137
|
+
name: 'harness-forge:output-style',
|
|
138
|
+
order: prompt.getSectionOrder('DEPLOYMENT_PERSONA_SUFFIX') - 1,
|
|
139
|
+
interpolate: false,
|
|
140
|
+
text: ({ scope }) => {
|
|
141
|
+
const style = this.current();
|
|
142
|
+
if (style === undefined)
|
|
143
|
+
return '';
|
|
144
|
+
// Delegated agents run their own system prompt.
|
|
145
|
+
const depth = scope?.session?.header?.delegationDepth ?? 0;
|
|
146
|
+
if (depth > 0)
|
|
147
|
+
return '';
|
|
148
|
+
return `# Output style: ${style.name}\n\n${style.instructions}`;
|
|
149
|
+
},
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
2
|
+
import type { Disposer } from '../forge/contract.js';
|
|
3
|
+
import { type HarnessForge } from '../forge/service.js';
|
|
4
|
+
export declare const name = "harness-forge-commands";
|
|
5
|
+
export declare const inject: string[];
|
|
6
|
+
/** The commands row. */
|
|
7
|
+
export declare function apply(ctx: Context): void;
|
|
8
|
+
/** Start the commands row on the shared service. Returns its teardown. */
|
|
9
|
+
export declare function startCommands(ctx: Context, forge: HarnessForge): Disposer;
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The commands row: imported slash commands (Claude Code commands, Codex
|
|
3
|
+
* prompts, OpenCode commands and the commands an OpenCode plugin adds), and
|
|
4
|
+
* Claude Code output styles with `/output-style`.
|
|
5
|
+
* @module @kirkchinese/harness-forge/commands
|
|
6
|
+
*/
|
|
7
|
+
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
8
|
+
import { join } from 'node:path';
|
|
9
|
+
import { HarnessError, kebabCase, uniqueName } from '@kirkchinese/harness-forge-core';
|
|
10
|
+
import { commandDefinition } from './definition.js';
|
|
11
|
+
import { interceptCommands } from '../dsh/commands.js';
|
|
12
|
+
import { dshService } from '../dsh/host.js';
|
|
13
|
+
import { FORGE_SERVICE } from '../forge/service.js';
|
|
14
|
+
import { hookSource, parseHookTable } from '../hooks/config.js';
|
|
15
|
+
import { isOwnAssets, projectRootOf, templateScope } from '../mcp/mount.js';
|
|
16
|
+
import { OutputStyles } from './output-styles.js';
|
|
17
|
+
import { messageOf } from '../shared/util.js';
|
|
18
|
+
import { mounted, Parts, rowEffect } from '../forge/row.js';
|
|
19
|
+
export const name = 'harness-forge-commands';
|
|
20
|
+
export const inject = [FORGE_SERVICE];
|
|
21
|
+
/** How long a slash command typed while imported plugins still mount waits for them at most. */
|
|
22
|
+
const READY_WAIT_MS = 30_000;
|
|
23
|
+
/** The commands row. */
|
|
24
|
+
export function apply(ctx) {
|
|
25
|
+
rowEffect(ctx, 'commands', forge => startCommands(ctx, forge));
|
|
26
|
+
}
|
|
27
|
+
/** The command registry of a scope: the session's for project assets. */
|
|
28
|
+
function registrarOf(ctx, scope) {
|
|
29
|
+
return dshService(scope.ctx ?? ctx, 'commands');
|
|
30
|
+
}
|
|
31
|
+
/** Start the commands row on the shared service. Returns its teardown. */
|
|
32
|
+
export function startCommands(ctx, forge) {
|
|
33
|
+
const stylePath = join(forge.dataRoot, 'output-style.json');
|
|
34
|
+
const outputStyles = new OutputStyles(() => {
|
|
35
|
+
try {
|
|
36
|
+
const saved = JSON.parse(readFileSync(stylePath, 'utf8')).selected;
|
|
37
|
+
return typeof saved === 'string' ? saved : undefined;
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
}, (selected) => {
|
|
43
|
+
mkdirSync(forge.dataRoot, { recursive: true });
|
|
44
|
+
writeFileSync(stylePath, JSON.stringify({ selected: selected ?? null }));
|
|
45
|
+
}, () => forge.settings.outputStyle());
|
|
46
|
+
let beforeSubmit;
|
|
47
|
+
const host = {
|
|
48
|
+
shell: forge.shell,
|
|
49
|
+
hooks: () => forge.lookup('hooks'),
|
|
50
|
+
invocations: forge.invocations,
|
|
51
|
+
directory: (agent, signal) => forge.directory(agent, signal),
|
|
52
|
+
observe: (componentId, ok, detail) => { forge.observe(componentId, 'command', ok, detail); },
|
|
53
|
+
trust: dir => forge.projects.trust.state(dir),
|
|
54
|
+
beforeSubmit: (agent, command, prompt) => beforeSubmit === undefined ? Promise.resolve([prompt]) : beforeSubmit(agent, command, prompt),
|
|
55
|
+
};
|
|
56
|
+
const api = {
|
|
57
|
+
registerAdded: (command, scope, taken) => {
|
|
58
|
+
const commands = registrarOf(ctx, scope);
|
|
59
|
+
if (commands === undefined)
|
|
60
|
+
return undefined;
|
|
61
|
+
const added = uniqueName(kebabCase(command.name), taken, command.component.id);
|
|
62
|
+
if (command.component.kind !== 'command')
|
|
63
|
+
return undefined;
|
|
64
|
+
const dispose = commands.register(commandDefinition(command.component, added, { pluginRoot: command.pluginRoot, dataDir: command.dataDir, foreignName: command.foreignName, pluginId: command.pluginId }, host));
|
|
65
|
+
return { name: added, dispose };
|
|
66
|
+
},
|
|
67
|
+
setBeforeSubmit: (hook) => {
|
|
68
|
+
beforeSubmit = hook;
|
|
69
|
+
return () => { if (beforeSubmit === hook)
|
|
70
|
+
beforeSubmit = undefined; };
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
const parts = new Parts(ctx, forge, 'commands');
|
|
74
|
+
parts.add('output style section', () => outputStyles.install(ctx));
|
|
75
|
+
parts.add('/output-style', () => registerOutputStyleCommand(ctx, outputStyles));
|
|
76
|
+
// A command typed while imported plugins still mount (DSH just started) waits for them, at most a while.
|
|
77
|
+
parts.add('readiness', () => {
|
|
78
|
+
const commands = dshService(ctx, 'commands');
|
|
79
|
+
if (commands === undefined)
|
|
80
|
+
return undefined;
|
|
81
|
+
return interceptCommands(commands, async (args, next) => {
|
|
82
|
+
await Promise.race([forge.ready(), new Promise((resolve) => { setTimeout(resolve, READY_WAIT_MS).unref?.(); })]);
|
|
83
|
+
return next(args);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
parts.add('command mounter', () => forge.registerMounter('command', {
|
|
87
|
+
mount: async ({ component, plugin, dataDir, scope, commandNames, takenNames }) => {
|
|
88
|
+
if (component.kind !== 'command')
|
|
89
|
+
throw new HarnessError('component-failed', `action expects a command: ${component.id}`);
|
|
90
|
+
const commands = registrarOf(ctx, scope);
|
|
91
|
+
if (commands === undefined)
|
|
92
|
+
throw new HarnessError('host-capability-missing', 'this DSH profile has no command registry');
|
|
93
|
+
// Claude Code names a user's or project's own commands without a plugin prefix.
|
|
94
|
+
const own = isOwnAssets(plugin);
|
|
95
|
+
const commandName = commandNames.get(component.id)
|
|
96
|
+
?? uniqueName(kebabCase(own ? component.name : `${plugin.name}-${component.name}`), takenNames(), component.id);
|
|
97
|
+
commandNames.set(component.id, commandName);
|
|
98
|
+
const local = component.invocationName ?? component.name;
|
|
99
|
+
const foreignName = own ? local : `${plugin.name}:${local}`;
|
|
100
|
+
const hooks = component.frontmatter['hooks'];
|
|
101
|
+
const hookTable = hooks === undefined ? undefined : hookSource(component.id, plugin, dataDir, parseHookTable(hooks), 'skill');
|
|
102
|
+
const projectRoot = projectRootOf(plugin);
|
|
103
|
+
const dispose = commands.register(commandDefinition(component, commandName, {
|
|
104
|
+
pluginRoot: plugin.root,
|
|
105
|
+
dataDir,
|
|
106
|
+
foreignName,
|
|
107
|
+
pluginId: plugin.id,
|
|
108
|
+
scope: templateScope(plugin),
|
|
109
|
+
...(projectRoot !== undefined ? { projectRoot } : {}),
|
|
110
|
+
...(hookTable !== undefined ? { hooks: hookTable } : {}),
|
|
111
|
+
}, host));
|
|
112
|
+
return { outcome: mounted(component, 'L1b', commandName), disposer: async () => { dispose(); } };
|
|
113
|
+
},
|
|
114
|
+
}));
|
|
115
|
+
parts.add('output style mounter', () => forge.registerMounter('output-style', {
|
|
116
|
+
mount: async ({ component, plugin }) => {
|
|
117
|
+
if (component.kind !== 'ui' || component.specPath === undefined)
|
|
118
|
+
throw new HarnessError('component-failed', `action expects an output style: ${component.id}`);
|
|
119
|
+
const style = outputStyles.add(component.specPath, isOwnAssets(plugin) ? undefined : plugin.name, component.id);
|
|
120
|
+
return { outcome: mounted(component, 'L1a', style.name), disposer: async () => { style.dispose(); } };
|
|
121
|
+
},
|
|
122
|
+
}));
|
|
123
|
+
// A command registry that starts after this row retries the commands that needed it.
|
|
124
|
+
parts.add('command registry', () => {
|
|
125
|
+
const fiber = ctx.inject(['commands'], () => { forge.ledger.reapply('command'); });
|
|
126
|
+
return () => fiber.dispose();
|
|
127
|
+
});
|
|
128
|
+
parts.add('api', () => forge.provide('commands', api));
|
|
129
|
+
return () => parts.dispose();
|
|
130
|
+
}
|
|
131
|
+
/** `/output-style [name]`: list the styles or switch to one, as in Claude Code. */
|
|
132
|
+
function registerOutputStyleCommand(ctx, outputStyles) {
|
|
133
|
+
const commands = dshService(ctx, 'commands');
|
|
134
|
+
if (commands === undefined)
|
|
135
|
+
return undefined;
|
|
136
|
+
return commands.register({
|
|
137
|
+
name: 'output-style',
|
|
138
|
+
description: 'Show or switch the Claude Code output style',
|
|
139
|
+
input: { hint: '[name]' },
|
|
140
|
+
handler: async (invocation) => {
|
|
141
|
+
const wanted = invocation.rawInput.trim();
|
|
142
|
+
if (wanted.length === 0) {
|
|
143
|
+
const current = outputStyles.current()?.name ?? 'default';
|
|
144
|
+
const lines = ['default', ...outputStyles.list().map(style => style.name)]
|
|
145
|
+
.map(name => `${name === current ? '●' : '○'} ${name}${name === 'default' ? '' : ` — ${outputStyles.list().find(style => style.name === name)?.description ?? ''}`}`);
|
|
146
|
+
return { kind: 'success', text: `Output style: ${current}\n${lines.join('\n')}\n\nSwitch with /output-style <name>.` };
|
|
147
|
+
}
|
|
148
|
+
try {
|
|
149
|
+
const selected = outputStyles.select(wanted);
|
|
150
|
+
return { kind: 'success', text: selected === 'default' ? 'Output style set to default.' : `Output style set to ${selected.name}. It applies from the next response.` };
|
|
151
|
+
}
|
|
152
|
+
catch (error) {
|
|
153
|
+
return { kind: 'error', text: messageOf(error) };
|
|
154
|
+
}
|
|
155
|
+
},
|
|
156
|
+
});
|
|
157
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
2
|
+
import type { ContextApi, ContextStatus } from '../forge/contract.js';
|
|
3
|
+
import type { HarnessForge } from '../forge/service.js';
|
|
4
|
+
/** What a system-prompt assembly offers the section: the agent (or its scope) whose session is being prompted. */
|
|
5
|
+
interface Assembly {
|
|
6
|
+
readonly agent?: {
|
|
7
|
+
readonly session?: unknown;
|
|
8
|
+
};
|
|
9
|
+
readonly scope?: {
|
|
10
|
+
readonly session?: unknown;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
export declare class ContextInstructions {
|
|
14
|
+
private readonly ctx;
|
|
15
|
+
private readonly forge;
|
|
16
|
+
private readonly reported;
|
|
17
|
+
constructor(ctx: Context, forge: HarnessForge);
|
|
18
|
+
/** The plugin that owns instruction injection when it runs, else undefined. */
|
|
19
|
+
yieldingTo(): string | undefined;
|
|
20
|
+
/** A mounted Codex user import carries the global instructions already; they are not added twice. */
|
|
21
|
+
private codexMounted;
|
|
22
|
+
private load;
|
|
23
|
+
/** What the instructions of `cwd` are, for the workbench. */
|
|
24
|
+
status(cwd: string | undefined): ContextStatus;
|
|
25
|
+
/** The prompt text for one assembly: empty while yielding, and when a failure keeps it from being read. */
|
|
26
|
+
text(assembly: Assembly): string;
|
|
27
|
+
private directoryOf;
|
|
28
|
+
/** Add the section to every system prompt. Returns the disposer. */
|
|
29
|
+
install(): () => void;
|
|
30
|
+
api(): ContextApi;
|
|
31
|
+
}
|
|
32
|
+
export {};
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Claude Code's instruction files in the system prompt, for what DSH's own
|
|
3
|
+
* instruction loader (`@deepseek-ai/dsh-agent-instructions`) does not read:
|
|
4
|
+
* `~/.claude/CLAUDE.md`, `.claude/CLAUDE.md`, the `@path` imports of every
|
|
5
|
+
* `CLAUDE.md`, and Codex's global `AGENTS.md`. DSH reads `AGENTS.md`,
|
|
6
|
+
* `CLAUDE.md` and their `.local.md` overlays from its project root down, so
|
|
7
|
+
* those are not repeated here. The whole part yields when the Recollect plugin
|
|
8
|
+
* (service `recollect`) runs: it owns instruction injection then.
|
|
9
|
+
* @module @kirkchinese/harness-forge
|
|
10
|
+
*/
|
|
11
|
+
import { homedir } from 'node:os';
|
|
12
|
+
import { codexHome, loadInstructions, renderInstructions } from '@kirkchinese/harness-forge-core';
|
|
13
|
+
import { dshService } from '../dsh/host.js';
|
|
14
|
+
import { messageOf } from '../shared/util.js';
|
|
15
|
+
/** The files DSH injects verbatim from its project root down to the working directory. */
|
|
16
|
+
const HOST_LOADS = ['CLAUDE.md', 'CLAUDE.local.md'];
|
|
17
|
+
/** The prompt text one workspace's files make: DSH's own loader caps each chain at this much too. */
|
|
18
|
+
const MAX_BYTES = 65_536;
|
|
19
|
+
/** The DSH service whose presence makes this part yield. */
|
|
20
|
+
const YIELDS_TO = 'recollect';
|
|
21
|
+
export class ContextInstructions {
|
|
22
|
+
ctx;
|
|
23
|
+
forge;
|
|
24
|
+
reported = new Set();
|
|
25
|
+
constructor(ctx, forge) {
|
|
26
|
+
this.ctx = ctx;
|
|
27
|
+
this.forge = forge;
|
|
28
|
+
}
|
|
29
|
+
/** The plugin that owns instruction injection when it runs, else undefined. */
|
|
30
|
+
yieldingTo() {
|
|
31
|
+
return dshService(this.ctx, YIELDS_TO) === undefined ? undefined : YIELDS_TO;
|
|
32
|
+
}
|
|
33
|
+
/** A mounted Codex user import carries the global instructions already; they are not added twice. */
|
|
34
|
+
codexMounted() {
|
|
35
|
+
return this.forge.list().some(record => record.source === 'codex' && (record.outcomes ?? []).some(outcome => outcome.status === 'mounted' && outcome.componentId.endsWith(':other:agents-md')));
|
|
36
|
+
}
|
|
37
|
+
load(cwd) {
|
|
38
|
+
const home = homedir();
|
|
39
|
+
return loadInstructions({ cwd, home, codexHome: codexHome(home), hostLoads: HOST_LOADS, skipCodex: this.codexMounted() });
|
|
40
|
+
}
|
|
41
|
+
/** What the instructions of `cwd` are, for the workbench. */
|
|
42
|
+
status(cwd) {
|
|
43
|
+
const yielding = this.yieldingTo();
|
|
44
|
+
if (yielding !== undefined)
|
|
45
|
+
return { active: false, yieldingTo: yielding, files: [] };
|
|
46
|
+
const directory = cwd ?? dshService(this.ctx, 'workingDirectory')?.defaultDirectory ?? process.cwd();
|
|
47
|
+
const { files, hostFiles, stopped } = this.load(directory);
|
|
48
|
+
return { active: true, files: files.map(file => ({ path: file.path, bytes: file.bytes })), hostFiles, ...(stopped !== undefined ? { stopped } : {}) };
|
|
49
|
+
}
|
|
50
|
+
/** The prompt text for one assembly: empty while yielding, and when a failure keeps it from being read. */
|
|
51
|
+
text(assembly) {
|
|
52
|
+
if (this.yieldingTo() !== undefined)
|
|
53
|
+
return '';
|
|
54
|
+
const directory = this.directoryOf(assembly);
|
|
55
|
+
try {
|
|
56
|
+
const { files, stopped } = this.load(directory);
|
|
57
|
+
return renderInstructions(files, MAX_BYTES, stopped).text;
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
// Once per directory: a step runs this again and again.
|
|
61
|
+
if (!this.reported.has(directory)) {
|
|
62
|
+
this.reported.add(directory);
|
|
63
|
+
this.forge.fault('context', new Error(`instruction files of ${directory} could not be read: ${messageOf(error)}`));
|
|
64
|
+
}
|
|
65
|
+
return '';
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
directoryOf(assembly) {
|
|
69
|
+
const service = dshService(this.ctx, 'workingDirectory');
|
|
70
|
+
const session = assembly.agent?.session ?? assembly.scope?.session;
|
|
71
|
+
return (session === undefined ? undefined : service?.get(session)) ?? service?.defaultDirectory ?? process.cwd();
|
|
72
|
+
}
|
|
73
|
+
/** Add the section to every system prompt. Returns the disposer. */
|
|
74
|
+
install() {
|
|
75
|
+
const prompt = dshService(this.ctx, 'systemPrompt');
|
|
76
|
+
if (prompt === undefined)
|
|
77
|
+
return () => { };
|
|
78
|
+
return prompt.section({
|
|
79
|
+
name: 'harness-forge:instructions',
|
|
80
|
+
order: prompt.getSectionOrder('DEPLOYMENT_PERSONA_SUFFIX') - 3,
|
|
81
|
+
interpolate: false,
|
|
82
|
+
text: assembly => this.text(assembly),
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
api() {
|
|
86
|
+
return { status: cwd => this.status(cwd) };
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The context row: Claude Code's instruction files (`~/.claude/CLAUDE.md`,
|
|
3
|
+
* `.claude/CLAUDE.md`, `@path` imports) and Codex's global `AGENTS.md` in the
|
|
4
|
+
* system prompt, for what DSH's own instruction loader does not read. It
|
|
5
|
+
* yields entirely when the Recollect plugin runs.
|
|
6
|
+
* @module @kirkchinese/harness-forge/context
|
|
7
|
+
*/
|
|
8
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
9
|
+
import type { Disposer } from '../forge/contract.js';
|
|
10
|
+
import { type HarnessForge } from '../forge/service.js';
|
|
11
|
+
export declare const name = "harness-forge-context";
|
|
12
|
+
export declare const inject: string[];
|
|
13
|
+
/** The context row. */
|
|
14
|
+
export declare function apply(ctx: Context): void;
|
|
15
|
+
/** Start the context row on the shared service. Returns its teardown. */
|
|
16
|
+
export declare function startContext(ctx: Context, forge: HarnessForge): Disposer;
|