@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,325 @@
|
|
|
1
|
+
import { createUserMessage } from '@deepseek-ai/dsh-llm';
|
|
2
|
+
import { booleanValue, toolList } from '@kirkchinese/harness-forge-core';
|
|
3
|
+
import { appendNotice } from '../hooks/notice.js';
|
|
4
|
+
import { dshService } from '../dsh/host.js';
|
|
5
|
+
import { startActivation } from '../dsh/subagents.js';
|
|
6
|
+
import { selectRoute } from '../dsh/model-route.js';
|
|
7
|
+
/** Read the invocation fields of a frontmatter. */
|
|
8
|
+
export function directivesOf(frontmatter) {
|
|
9
|
+
const model = typeof frontmatter['model'] === 'string' && frontmatter['model'].trim().length > 0 ? frontmatter['model'].trim() : undefined;
|
|
10
|
+
const effort = typeof frontmatter['effort'] === 'string' && frontmatter['effort'].trim().length > 0 ? frontmatter['effort'].trim().toLowerCase() : undefined;
|
|
11
|
+
// OpenCode's `subtask: true` runs a command as a subagent and waits for its answer.
|
|
12
|
+
const subtask = frontmatter['subtask'] === true;
|
|
13
|
+
const fork = frontmatter['context'] === 'fork' || subtask;
|
|
14
|
+
const agent = typeof frontmatter['agent'] === 'string' && frontmatter['agent'].trim().length > 0 ? frontmatter['agent'].trim() : undefined;
|
|
15
|
+
return {
|
|
16
|
+
allowedTools: toolList(frontmatter['allowed-tools'] ?? frontmatter['allowedTools']) ?? [],
|
|
17
|
+
disallowedTools: toolList(frontmatter['disallowed-tools'] ?? frontmatter['disallowedTools']) ?? [],
|
|
18
|
+
...(model !== undefined && model !== 'inherit' ? { model } : {}),
|
|
19
|
+
...(effort !== undefined ? { effort } : {}),
|
|
20
|
+
...(fork ? { fork: { ...(agent !== undefined ? { agent } : {}), background: subtask ? false : booleanValue(frontmatter['background']) !== false } } : {}),
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/** Expand the path variables a rule may name. */
|
|
24
|
+
export function expandRule(rule, places) {
|
|
25
|
+
const posix = (path) => path?.replace(/\\/gu, '/');
|
|
26
|
+
return rule.replace(/\$\{(CLAUDE_SKILL_DIR|CLAUDE_PROJECT_DIR|CLAUDE_PLUGIN_ROOT|CLAUDE_PLUGIN_DATA)\}/gu, (match, name) => {
|
|
27
|
+
const value = name === 'CLAUDE_SKILL_DIR' ? posix(places.skillDir)
|
|
28
|
+
: name === 'CLAUDE_PROJECT_DIR' ? posix(places.projectDir)
|
|
29
|
+
: name === 'CLAUDE_PLUGIN_ROOT' ? posix(places.pluginRoot)
|
|
30
|
+
: posix(places.dataDir);
|
|
31
|
+
return value ?? match;
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
/** Claude effort levels, least to greatest. */
|
|
35
|
+
const CLAUDE_EFFORTS = ['low', 'medium', 'high', 'xhigh', 'max'];
|
|
36
|
+
/** Per-session invocation state of foreign skills and commands. */
|
|
37
|
+
export class Invocations {
|
|
38
|
+
ctx;
|
|
39
|
+
presetOf;
|
|
40
|
+
directory;
|
|
41
|
+
specs = new Map();
|
|
42
|
+
turns = new Map();
|
|
43
|
+
pending = new Map();
|
|
44
|
+
/**
|
|
45
|
+
* A command submitted its prompt: its frontmatter takes effect in the turn
|
|
46
|
+
* that prompt starts, after that turn clears the previous grants.
|
|
47
|
+
*/
|
|
48
|
+
queue(sessionId, spec) {
|
|
49
|
+
this.pending.set(sessionId, [...this.pending.get(sessionId) ?? [], spec]);
|
|
50
|
+
}
|
|
51
|
+
/** Withdraw a spec queued for a session's next turn (its prompt was not submitted after all). */
|
|
52
|
+
unqueue(sessionId, spec) {
|
|
53
|
+
const left = (this.pending.get(sessionId) ?? []).filter(queued => queued !== spec);
|
|
54
|
+
if (left.length > 0)
|
|
55
|
+
this.pending.set(sessionId, left);
|
|
56
|
+
else
|
|
57
|
+
this.pending.delete(sessionId);
|
|
58
|
+
}
|
|
59
|
+
/** Take the specs queued for a session's next turn. */
|
|
60
|
+
takeQueued(sessionId) {
|
|
61
|
+
const specs = this.pending.get(sessionId) ?? [];
|
|
62
|
+
this.pending.delete(sessionId);
|
|
63
|
+
return specs;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* @param presetOf - the DSH preset of a foreign agent name, for `agent`.
|
|
67
|
+
* @param directory - the session's working directory.
|
|
68
|
+
*/
|
|
69
|
+
constructor(ctx, presetOf, directory) {
|
|
70
|
+
this.ctx = ctx;
|
|
71
|
+
this.presetOf = presetOf;
|
|
72
|
+
this.directory = directory;
|
|
73
|
+
}
|
|
74
|
+
/** Make a skill's frontmatter take effect when the skill is invoked. Returns the withdrawal. */
|
|
75
|
+
registerSkill(spec) {
|
|
76
|
+
this.specs.set(spec.name, spec);
|
|
77
|
+
return () => { if (this.specs.get(spec.name) === spec)
|
|
78
|
+
this.specs.delete(spec.name); };
|
|
79
|
+
}
|
|
80
|
+
/** The registered spec of a skill name. */
|
|
81
|
+
skill(name) {
|
|
82
|
+
return this.specs.get(name);
|
|
83
|
+
}
|
|
84
|
+
/** The turn's tool grants of a session, as allow and deny rules. */
|
|
85
|
+
rules(sessionId) {
|
|
86
|
+
const turn = this.turns.get(sessionId);
|
|
87
|
+
return { allow: turn?.allow ?? [], deny: turn?.deny ?? [] };
|
|
88
|
+
}
|
|
89
|
+
/** The skills and commands active in a session's turn. */
|
|
90
|
+
active(sessionId) {
|
|
91
|
+
return this.turns.get(sessionId)?.active ?? [];
|
|
92
|
+
}
|
|
93
|
+
/** The user sent a new message: grants and overrides end. */
|
|
94
|
+
clear(sessionId) {
|
|
95
|
+
this.turns.delete(sessionId);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* DSH 0.2.2 fixes a step's route before `agent/pre-step`, so a route is selected on the agent ahead of the step
|
|
99
|
+
* that must run on it: a typed command's or skill's when the line is dispatched (its turn has not begun), a
|
|
100
|
+
* model-invoked skill's during the step that loads it. The route the agent had is put back when that turn ends.
|
|
101
|
+
*/
|
|
102
|
+
selected = new Map();
|
|
103
|
+
applyRoute(agent, route, armed) {
|
|
104
|
+
const sessionId = agent.session.header.id;
|
|
105
|
+
const held = this.selected.get(sessionId);
|
|
106
|
+
const selection = selectRoute(agent, route);
|
|
107
|
+
if (held !== undefined) {
|
|
108
|
+
held.armed ||= armed;
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
if (selection !== undefined)
|
|
112
|
+
this.selected.set(sessionId, { restore: selection.restore, armed });
|
|
113
|
+
}
|
|
114
|
+
/** A typed command or skill line is about to run: its frontmatter route is selected for the turn it starts. */
|
|
115
|
+
async preselect(agent, spec, signal) {
|
|
116
|
+
const directives = directivesOf(spec.frontmatter);
|
|
117
|
+
if (directives.fork !== undefined || (directives.model === undefined && directives.effort === undefined))
|
|
118
|
+
return;
|
|
119
|
+
const route = await this.route(agent, directives, signal);
|
|
120
|
+
if (route.route !== undefined)
|
|
121
|
+
this.applyRoute(agent, route.route, false);
|
|
122
|
+
}
|
|
123
|
+
/** A session's turn started: a route selected ahead of it now belongs to it. */
|
|
124
|
+
turnStarted(sessionId) {
|
|
125
|
+
const held = this.selected.get(sessionId);
|
|
126
|
+
if (held !== undefined)
|
|
127
|
+
held.armed = true;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* A session's turn ended: the route it ran on is put back. Called from a `session/event` listener, which runs
|
|
131
|
+
* while DSH publishes the event and may not append (selecting a route appends `model/selection`), so the
|
|
132
|
+
* selection is put back right after; the next step reads the route only later.
|
|
133
|
+
*/
|
|
134
|
+
turnEnded(sessionId) {
|
|
135
|
+
const held = this.selected.get(sessionId);
|
|
136
|
+
if (held === undefined || !held.armed)
|
|
137
|
+
return;
|
|
138
|
+
this.selected.delete(sessionId);
|
|
139
|
+
queueMicrotask(() => {
|
|
140
|
+
try {
|
|
141
|
+
held.restore();
|
|
142
|
+
}
|
|
143
|
+
catch {
|
|
144
|
+
// The session no longer takes records (closed or taken out of its store meanwhile).
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
/** A typed line was answered without starting a turn: the route it selected is put back now. */
|
|
149
|
+
release(sessionId) {
|
|
150
|
+
const held = this.selected.get(sessionId);
|
|
151
|
+
if (held === undefined || held.armed)
|
|
152
|
+
return;
|
|
153
|
+
this.selected.delete(sessionId);
|
|
154
|
+
held.restore();
|
|
155
|
+
}
|
|
156
|
+
/** Apply a spec's grants and overrides to the agent's current turn. */
|
|
157
|
+
async activate(agent, spec, signal) {
|
|
158
|
+
const directives = directivesOf(spec.frontmatter);
|
|
159
|
+
const sessionId = agent.session.header.id;
|
|
160
|
+
const turn = this.turns.get(sessionId) ?? { allow: [], deny: [], active: [] };
|
|
161
|
+
// A forked skill's tool grants belong to its forked agent (see `fork`).
|
|
162
|
+
if (directives.fork === undefined) {
|
|
163
|
+
const rules = await this.expandedRules(agent, spec, directives, signal);
|
|
164
|
+
turn.allow.push(...rules.allow);
|
|
165
|
+
turn.deny.push(...rules.deny);
|
|
166
|
+
}
|
|
167
|
+
turn.active.push(spec.name);
|
|
168
|
+
// With `context: fork` the model and effort belong to the forked agent.
|
|
169
|
+
if (directives.fork === undefined && (directives.model !== undefined || directives.effort !== undefined)) {
|
|
170
|
+
const route = await this.route(agent, directives, signal);
|
|
171
|
+
if (route.route !== undefined) {
|
|
172
|
+
turn.route = { ...turn.route, ...route.route };
|
|
173
|
+
// DSH 0.2.2: selected on the agent for the turn's next steps (a typed line's was already, see `preselect`).
|
|
174
|
+
this.applyRoute(agent, route.route, true);
|
|
175
|
+
}
|
|
176
|
+
if (route.note !== undefined)
|
|
177
|
+
appendNotice(agent.session, { kind: 'feedback', event: 'SkillInvocation', subject: spec.name, plugin: spec.pluginId, text: route.note });
|
|
178
|
+
}
|
|
179
|
+
this.turns.set(sessionId, turn);
|
|
180
|
+
return directives;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Run rendered content in a forked agent. With `background`, return at once
|
|
184
|
+
* and deliver the result to the parent when the agent finishes.
|
|
185
|
+
* @returns the result text, or the launch note in the background case.
|
|
186
|
+
*/
|
|
187
|
+
async fork(agent, spec, directives, content, signal) {
|
|
188
|
+
const subagents = dshService(this.ctx, 'subagents');
|
|
189
|
+
if (subagents === undefined)
|
|
190
|
+
throw new Error(`/${spec.name} runs in a forked agent (context: fork), and this DSH profile composes no subagent runtime`);
|
|
191
|
+
const foreignAgent = directives.fork?.agent;
|
|
192
|
+
const preset = foreignAgent === undefined || /^(general-purpose|Explore|Plan)$/u.test(foreignAgent) ? undefined : this.presetOf(foreignAgent);
|
|
193
|
+
const background = directives.fork?.background !== false;
|
|
194
|
+
// The forked agent runs with the skill's model and effort (start options, which every DSH release takes) and its tool grants.
|
|
195
|
+
const route = directives.model !== undefined || directives.effort !== undefined ? await this.route(agent, directives, signal) : {};
|
|
196
|
+
const activation = await startActivation(subagents, {
|
|
197
|
+
parent: agent,
|
|
198
|
+
prompt: [{ type: 'text', text: content }],
|
|
199
|
+
...(preset !== undefined ? { agentPreset: preset } : {}),
|
|
200
|
+
...(route.route !== undefined ? { agentOptions: route.route } : {}),
|
|
201
|
+
}, { provider: 'spawn', label: `${spec.name} (forked skill)`, signal: background ? AbortSignal.timeout(3_600_000) : signal, delivery: 'caller' });
|
|
202
|
+
if (activation.childId !== undefined) {
|
|
203
|
+
const rules = await this.expandedRules(agent, spec, directives, signal);
|
|
204
|
+
this.turns.set(activation.childId, { allow: rules.allow, deny: rules.deny, ...(route.route !== undefined ? { route: route.route } : {}), active: [spec.name] });
|
|
205
|
+
}
|
|
206
|
+
const text = async () => {
|
|
207
|
+
const result = await activation.result;
|
|
208
|
+
return (result.output ?? []).map(block => block.type === 'text' ? block.text ?? '' : '').join('').trim();
|
|
209
|
+
};
|
|
210
|
+
if (!background)
|
|
211
|
+
return text();
|
|
212
|
+
void text().then((output) => {
|
|
213
|
+
agent.followup(createUserMessage({
|
|
214
|
+
content: [{ type: 'text', text: `<skill-result name="${spec.name}">\n${output.length > 0 ? output : '(the forked agent returned no text)'}\n</skill-result>` }],
|
|
215
|
+
source: { kind: 'user' },
|
|
216
|
+
}));
|
|
217
|
+
}).catch((error) => {
|
|
218
|
+
appendNotice(agent.session, { kind: 'hook-error', event: 'SkillInvocation', subject: spec.name, plugin: spec.pluginId, text: `the forked agent of ${spec.name} failed: ${error instanceof Error ? error.message : String(error)}` });
|
|
219
|
+
});
|
|
220
|
+
return `Skill ${spec.name} is running in a forked ${foreignAgent ?? 'general-purpose'} agent in the background. Its result will arrive as a message when it finishes; continue with other work meanwhile.`;
|
|
221
|
+
}
|
|
222
|
+
async expandedRules(agent, spec, directives, signal) {
|
|
223
|
+
const projectDir = await this.directory(agent, signal).catch(() => agent.session.header.cwd ?? process.cwd());
|
|
224
|
+
return {
|
|
225
|
+
allow: directives.allowedTools.map(rule => expandRule(rule, { ...spec.places, projectDir })),
|
|
226
|
+
deny: directives.disallowedTools.map(rule => expandRule(rule, { ...spec.places, projectDir })),
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Resolve a frontmatter `model` and `effort` to a DSH route: an exact
|
|
231
|
+
* `provider/model`, a model id or name one provider lists (the current
|
|
232
|
+
* provider first), and an effort the model offers by name or, failing that,
|
|
233
|
+
* by rank.
|
|
234
|
+
*/
|
|
235
|
+
route(agent, directives, signal) {
|
|
236
|
+
return resolveRoute(this.ctx, agent.options ?? {}, directives.model, directives.effort, signal);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Resolve a Claude `model` and `effort` to a DSH route: an exact
|
|
241
|
+
* `provider/model`, else a model id or name one provider lists (the current
|
|
242
|
+
* provider first); an effort the model offers by id or name, else by rank
|
|
243
|
+
* among Claude's `low`…`max`. `inherit` keeps the current model.
|
|
244
|
+
*/
|
|
245
|
+
export async function resolveRoute(ctx, current, wantedModel, wantedEffort, signal) {
|
|
246
|
+
const llm = dshService(ctx, 'llm');
|
|
247
|
+
if (llm === undefined)
|
|
248
|
+
return {};
|
|
249
|
+
let provider = current.provider;
|
|
250
|
+
let model = current.model;
|
|
251
|
+
const notes = [];
|
|
252
|
+
if (wantedModel !== undefined && wantedModel !== 'inherit') {
|
|
253
|
+
const found = await findModel(llm, wantedModel, current.provider);
|
|
254
|
+
const family = found === undefined ? claudeFamily(wantedModel) : undefined;
|
|
255
|
+
// A Claude alias runs on the model the person mapped it to, else on the session's model.
|
|
256
|
+
const mapped = family === undefined ? undefined : modelAliases[family];
|
|
257
|
+
const routed = mapped === undefined ? undefined : await findModel(llm, mapped, current.provider);
|
|
258
|
+
if (found !== undefined) {
|
|
259
|
+
provider = found.provider;
|
|
260
|
+
model = found.model;
|
|
261
|
+
}
|
|
262
|
+
else if (routed !== undefined) {
|
|
263
|
+
provider = routed.provider;
|
|
264
|
+
model = routed.model;
|
|
265
|
+
notes.push(`model "${wantedModel}" runs on ${routed.provider}/${routed.model} (modelAliases.${family ?? ''})`);
|
|
266
|
+
}
|
|
267
|
+
else if (family !== undefined) {
|
|
268
|
+
notes.push(`model "${wantedModel}" is Claude's ${family}; it runs on ${current.model ?? 'the session model'}${mapped !== undefined ? ` (modelAliases.${family} names no served model)` : ''}`);
|
|
269
|
+
}
|
|
270
|
+
else {
|
|
271
|
+
notes.push(`model "${wantedModel}" names no model this DSH profile serves; ${current.model ?? 'the session model'} is kept`);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
let reasoningEffort;
|
|
275
|
+
const effort = wantedEffort?.toLowerCase();
|
|
276
|
+
if (effort !== undefined && provider !== undefined && model !== undefined) {
|
|
277
|
+
const info = await llm.resolveModelInfo(provider, model, signal).catch(() => undefined);
|
|
278
|
+
const efforts = info?.reasoning?.efforts ?? [];
|
|
279
|
+
const exact = efforts.find(item => item.id.toLowerCase() === effort || item.name.toLowerCase() === effort);
|
|
280
|
+
const rank = CLAUDE_EFFORTS.indexOf(effort);
|
|
281
|
+
const ranked = exact ?? (rank >= 0 && efforts.length > 0 ? efforts[Math.round(rank / (CLAUDE_EFFORTS.length - 1) * (efforts.length - 1))] : undefined);
|
|
282
|
+
if (ranked !== undefined)
|
|
283
|
+
reasoningEffort = ranked.id;
|
|
284
|
+
else
|
|
285
|
+
notes.push(`effort "${effort}" is not offered by ${model}; its effort is kept`);
|
|
286
|
+
}
|
|
287
|
+
const changed = (provider !== current.provider || model !== current.model) || reasoningEffort !== undefined;
|
|
288
|
+
return {
|
|
289
|
+
...(changed ? { route: { ...(provider !== undefined ? { provider } : {}), ...(model !== undefined ? { model } : {}), ...(reasoningEffort !== undefined ? { reasoningEffort } : {}) } } : {}),
|
|
290
|
+
...(notes.length > 0 ? { note: notes.join('; ') } : {}),
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
/** The Claude family a `model` value names: an alias (`haiku`, `sonnet`, `opus`, `opusplan`) or a `claude-…` id. */
|
|
294
|
+
export function claudeFamily(wanted) {
|
|
295
|
+
const lower = wanted.trim().toLowerCase();
|
|
296
|
+
if (!/^(?:haiku|sonnet|opus|opusplan)(?:\[1m\])?$|^claude-/u.test(lower))
|
|
297
|
+
return undefined;
|
|
298
|
+
if (lower.includes('haiku'))
|
|
299
|
+
return 'haiku';
|
|
300
|
+
if (lower.includes('opus'))
|
|
301
|
+
return 'opus';
|
|
302
|
+
if (lower.includes('sonnet'))
|
|
303
|
+
return 'sonnet';
|
|
304
|
+
return undefined;
|
|
305
|
+
}
|
|
306
|
+
let modelAliases = {};
|
|
307
|
+
/** The person's mapping of Claude aliases to served models (Harness Forge's `modelAliases` config). */
|
|
308
|
+
export function setModelAliases(aliases) {
|
|
309
|
+
modelAliases = aliases ?? {};
|
|
310
|
+
}
|
|
311
|
+
async function findModel(llm, wanted, preferred) {
|
|
312
|
+
const providers = llm.listProviders().map(provider => provider.id);
|
|
313
|
+
const slash = wanted.indexOf('/');
|
|
314
|
+
if (slash > 0 && providers.includes(wanted.slice(0, slash)))
|
|
315
|
+
return { provider: wanted.slice(0, slash), model: wanted.slice(slash + 1) };
|
|
316
|
+
const order = preferred !== undefined && providers.includes(preferred) ? [preferred, ...providers.filter(id => id !== preferred)] : providers;
|
|
317
|
+
const lower = wanted.toLowerCase();
|
|
318
|
+
for (const provider of order) {
|
|
319
|
+
const models = await llm.listModels(provider).catch(() => []);
|
|
320
|
+
const match = models.find(model => model.id.toLowerCase() === lower || model.name.toLowerCase() === lower);
|
|
321
|
+
if (match !== undefined)
|
|
322
|
+
return { provider, model: match.id };
|
|
323
|
+
}
|
|
324
|
+
return undefined;
|
|
325
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { ForeignShell } from '../hooks/shell.js';
|
|
2
|
+
/** Everything a body may reference. */
|
|
3
|
+
export interface RenderContext {
|
|
4
|
+
/** Raw text after the command name; empty for a model-loaded skill. */
|
|
5
|
+
readonly rawArguments: string;
|
|
6
|
+
/** Names from the `arguments` frontmatter, mapped to positions in order. */
|
|
7
|
+
readonly argumentNames: readonly string[];
|
|
8
|
+
readonly cwd: string;
|
|
9
|
+
readonly pluginRoot?: string;
|
|
10
|
+
readonly dataDir?: string;
|
|
11
|
+
readonly skillDir?: string;
|
|
12
|
+
readonly sessionId?: string;
|
|
13
|
+
readonly shell: ForeignShell;
|
|
14
|
+
/** `bash` (default) or `powershell`, from the `shell` frontmatter key. */
|
|
15
|
+
readonly shellKind?: string;
|
|
16
|
+
readonly signal?: AbortSignal;
|
|
17
|
+
/** The home directory whose `.claude` `@file` references may read (tests). */
|
|
18
|
+
readonly home?: string;
|
|
19
|
+
/**
|
|
20
|
+
* Whose template this is, for what `@file` may attach: `user` (the person's
|
|
21
|
+
* own assets: the workspace, the template's directory and `~/.claude`),
|
|
22
|
+
* `plugin` (default: the plugin and the workspace) or `project` (the workspace only).
|
|
23
|
+
*/
|
|
24
|
+
readonly scope?: 'user' | 'project' | 'plugin';
|
|
25
|
+
/** Set when no shell line of this body may run (the workspace is not trusted): why, shown in place of each. */
|
|
26
|
+
readonly refuseShell?: string;
|
|
27
|
+
}
|
|
28
|
+
/** The rendered prompt plus files referenced with `@`. */
|
|
29
|
+
export interface RenderedBody {
|
|
30
|
+
readonly text: string;
|
|
31
|
+
readonly files: ReadonlyArray<{
|
|
32
|
+
readonly path: string;
|
|
33
|
+
readonly content: string;
|
|
34
|
+
}>;
|
|
35
|
+
/** Shell commands that ran, for evidence. */
|
|
36
|
+
readonly commands: number;
|
|
37
|
+
}
|
|
38
|
+
/** Render one body. */
|
|
39
|
+
export declare function renderBody(body: string, context: RenderContext): Promise<RenderedBody>;
|
|
40
|
+
/**
|
|
41
|
+
* A Codex custom prompt with its arguments, as Codex expands it: `$1`…`$9`
|
|
42
|
+
* are the positional arguments, `$ARGUMENTS` all of them, `$NAME` the value
|
|
43
|
+
* of `NAME=value`, `$$` a literal `$`. Codex runs no commands and attaches
|
|
44
|
+
* no files from a prompt body.
|
|
45
|
+
*/
|
|
46
|
+
export declare function renderCodexPrompt(body: string, rawArguments: string): RenderedBody;
|
|
47
|
+
/** Shell-style split: quotes group words; backslash escapes the next char. */
|
|
48
|
+
export declare function splitArguments(raw: string): string[];
|
|
49
|
+
/** The environment variable that will hold a placeholder's value, or undefined to leave the placeholder as written. */
|
|
50
|
+
type ValueOf = (match: string, variable: string | undefined, indexed: string | undefined, short: string | undefined, name: string | undefined) => string | undefined;
|
|
51
|
+
/** A shell line ready to run, or why it may not run. */
|
|
52
|
+
export type ShellLine = {
|
|
53
|
+
readonly command: string;
|
|
54
|
+
} | {
|
|
55
|
+
readonly refused: string;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* A shell line with each placeholder replaced by a reference to an
|
|
59
|
+
* environment variable holding its value. Values never enter the source: a
|
|
60
|
+
* reference is written only where it provably stays one word of data (bash:
|
|
61
|
+
* unquoted or inside plain double quotes; PowerShell: outside quotes or inside
|
|
62
|
+
* plain double quotes), and only in an argument of a simple command that does
|
|
63
|
+
* not run it (see `argumentOf`), in a line that defines no function or alias.
|
|
64
|
+
* A placeholder anywhere else (single or ANSI-C quotes, here-documents and
|
|
65
|
+
* here-strings, backticks, `$(…)`, parameter expansions, arithmetic, `[[ ]]`,
|
|
66
|
+
* comments, the command position, an assignment, the arguments of `eval` and
|
|
67
|
+
* the like) makes the whole line refused. `\$` stays as written, for the shell.
|
|
68
|
+
*/
|
|
69
|
+
export declare function shellCommand(command: string, kind: 'bash' | 'powershell', value: ValueOf): ShellLine;
|
|
70
|
+
/** Append `@` attachments to a rendered prompt. */
|
|
71
|
+
export declare function withAttachments(rendered: RenderedBody): string;
|
|
72
|
+
export {};
|