@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.
Files changed (246) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +42 -2
  3. package/assets/icon.svg +12 -0
  4. package/cordis.patch.yml +28 -0
  5. package/lib/agents/presets.d.ts +91 -0
  6. package/lib/agents/presets.js +282 -0
  7. package/lib/agents/profiles.d.ts +58 -0
  8. package/lib/agents/profiles.js +278 -0
  9. package/lib/agents/row.d.ts +17 -0
  10. package/lib/agents/row.js +146 -0
  11. package/lib/client/styles.d.ts +8 -0
  12. package/lib/client/styles.js +89 -0
  13. package/lib/client/typert.d.ts +14 -0
  14. package/lib/client/typert.js +110 -0
  15. package/lib/client.js +22500 -0
  16. package/lib/commands/definition.d.ts +40 -0
  17. package/lib/commands/definition.js +113 -0
  18. package/lib/commands/output-styles.d.ts +42 -0
  19. package/lib/commands/output-styles.js +152 -0
  20. package/lib/commands/row.d.ts +9 -0
  21. package/lib/commands/row.js +157 -0
  22. package/lib/context/instructions.d.ts +32 -0
  23. package/lib/context/instructions.js +88 -0
  24. package/lib/context/row.d.ts +16 -0
  25. package/lib/context/row.js +17 -0
  26. package/lib/dsh/commands.d.ts +30 -0
  27. package/lib/dsh/commands.js +61 -0
  28. package/lib/dsh/host.d.ts +107 -0
  29. package/lib/dsh/host.js +214 -0
  30. package/lib/dsh/model-route.d.ts +21 -0
  31. package/lib/dsh/model-route.js +37 -0
  32. package/lib/dsh/session-records.d.ts +78 -0
  33. package/lib/dsh/session-records.js +36 -0
  34. package/lib/dsh/subagents.d.ts +46 -0
  35. package/lib/dsh/subagents.js +14 -0
  36. package/lib/forge/contract.d.ts +231 -0
  37. package/lib/forge/contract.js +43 -0
  38. package/lib/forge/ledger.d.ts +143 -0
  39. package/lib/forge/ledger.js +733 -0
  40. package/lib/forge/live-mounts.d.ts +45 -0
  41. package/lib/forge/live-mounts.js +78 -0
  42. package/lib/forge/marketplace-install.d.ts +3 -0
  43. package/lib/forge/marketplace-install.js +64 -0
  44. package/lib/forge/npm.d.ts +31 -0
  45. package/lib/forge/npm.js +60 -0
  46. package/lib/forge/projects.d.ts +94 -0
  47. package/lib/forge/projects.js +336 -0
  48. package/lib/forge/receipts.d.ts +5 -0
  49. package/lib/forge/receipts.js +31 -0
  50. package/lib/forge/row.d.ts +32 -0
  51. package/lib/forge/row.js +59 -0
  52. package/lib/forge/service.d.ts +105 -0
  53. package/lib/forge/service.js +274 -0
  54. package/lib/forge/settings.d.ts +41 -0
  55. package/lib/forge/settings.js +115 -0
  56. package/lib/forge/state.d.ts +69 -0
  57. package/lib/forge/state.js +150 -0
  58. package/lib/forge/trust.d.ts +48 -0
  59. package/lib/forge/trust.js +107 -0
  60. package/lib/forge/turns.d.ts +13 -0
  61. package/lib/forge/turns.js +56 -0
  62. package/lib/hooks/broker/engine.d.ts +112 -0
  63. package/lib/hooks/broker/engine.js +728 -0
  64. package/lib/hooks/broker/helpers.d.ts +10 -0
  65. package/lib/hooks/broker/helpers.js +22 -0
  66. package/lib/hooks/broker/lifecycle.d.ts +65 -0
  67. package/lib/hooks/broker/lifecycle.js +342 -0
  68. package/lib/hooks/broker/session-events.d.ts +22 -0
  69. package/lib/hooks/broker/session-events.js +196 -0
  70. package/lib/hooks/broker/tools.d.ts +52 -0
  71. package/lib/hooks/broker/tools.js +424 -0
  72. package/lib/hooks/broker/types.d.ts +262 -0
  73. package/lib/hooks/broker/types.js +1 -0
  74. package/lib/hooks/broker.d.ts +80 -0
  75. package/lib/hooks/broker.js +95 -0
  76. package/lib/hooks/codex-user.d.ts +5 -0
  77. package/lib/hooks/codex-user.js +109 -0
  78. package/lib/hooks/condition.d.ts +11 -0
  79. package/lib/hooks/condition.js +181 -0
  80. package/lib/hooks/config.d.ts +85 -0
  81. package/lib/hooks/config.js +150 -0
  82. package/lib/hooks/matcher.d.ts +20 -0
  83. package/lib/hooks/matcher.js +39 -0
  84. package/lib/hooks/notice.d.ts +17 -0
  85. package/lib/hooks/notice.js +39 -0
  86. package/lib/hooks/output.d.ts +56 -0
  87. package/lib/hooks/output.js +214 -0
  88. package/lib/hooks/permissions.d.ts +14 -0
  89. package/lib/hooks/permissions.js +109 -0
  90. package/lib/hooks/process.d.ts +47 -0
  91. package/lib/hooks/process.js +134 -0
  92. package/lib/hooks/row.d.ts +17 -0
  93. package/lib/hooks/row.js +94 -0
  94. package/lib/hooks/runner.d.ts +85 -0
  95. package/lib/hooks/runner.js +293 -0
  96. package/lib/hooks/shell-commands.d.ts +19 -0
  97. package/lib/hooks/shell-commands.js +177 -0
  98. package/lib/hooks/shell.d.ts +43 -0
  99. package/lib/hooks/shell.js +126 -0
  100. package/lib/hooks/transcript.d.ts +28 -0
  101. package/lib/hooks/transcript.js +141 -0
  102. package/lib/hooks/vocabulary.d.ts +57 -0
  103. package/lib/hooks/vocabulary.js +364 -0
  104. package/lib/index.d.ts +31 -0
  105. package/lib/index.js +287 -0
  106. package/lib/mcp/auth.d.ts +35 -0
  107. package/lib/mcp/auth.js +178 -0
  108. package/lib/mcp/catalog.d.ts +36 -0
  109. package/lib/mcp/catalog.js +61 -0
  110. package/lib/mcp/mount.d.ts +75 -0
  111. package/lib/mcp/mount.js +217 -0
  112. package/lib/mcp/remote.d.ts +45 -0
  113. package/lib/mcp/remote.js +143 -0
  114. package/lib/mcp/row.d.ts +9 -0
  115. package/lib/mcp/row.js +131 -0
  116. package/lib/mods/bundler.d.ts +38 -0
  117. package/lib/mods/bundler.js +157 -0
  118. package/lib/mods/chain.d.ts +103 -0
  119. package/lib/mods/chain.js +436 -0
  120. package/lib/mods/claude.d.ts +27 -0
  121. package/lib/mods/claude.js +56 -0
  122. package/lib/mods/engine.d.ts +160 -0
  123. package/lib/mods/engine.js +701 -0
  124. package/lib/mods/expect.d.ts +8 -0
  125. package/lib/mods/expect.js +248 -0
  126. package/lib/mods/host.d.ts +105 -0
  127. package/lib/mods/host.js +462 -0
  128. package/lib/mods/loader.d.ts +53 -0
  129. package/lib/mods/loader.js +227 -0
  130. package/lib/mods/ops.d.ts +13 -0
  131. package/lib/mods/ops.js +117 -0
  132. package/lib/mods/row.d.ts +9 -0
  133. package/lib/mods/row.js +77 -0
  134. package/lib/mods/runtime.d.ts +60 -0
  135. package/lib/mods/runtime.js +133 -0
  136. package/lib/mods/service/commands.d.ts +17 -0
  137. package/lib/mods/service/commands.js +121 -0
  138. package/lib/mods/service/drawings.d.ts +36 -0
  139. package/lib/mods/service/drawings.js +64 -0
  140. package/lib/mods/service/sessions.d.ts +63 -0
  141. package/lib/mods/service/sessions.js +345 -0
  142. package/lib/mods/service/tools.d.ts +32 -0
  143. package/lib/mods/service/tools.js +253 -0
  144. package/lib/mods/service/turns.d.ts +32 -0
  145. package/lib/mods/service/turns.js +161 -0
  146. package/lib/mods/service/types.d.ts +151 -0
  147. package/lib/mods/service/types.js +1 -0
  148. package/lib/mods/service.d.ts +72 -0
  149. package/lib/mods/service.js +104 -0
  150. package/lib/mods/surfaces.d.ts +93 -0
  151. package/lib/mods/surfaces.js +309 -0
  152. package/lib/mods/values.d.ts +30 -0
  153. package/lib/mods/values.js +95 -0
  154. package/lib/opencode/bun.d.ts +40 -0
  155. package/lib/opencode/bun.js +156 -0
  156. package/lib/opencode/convert.d.ts +53 -0
  157. package/lib/opencode/convert.js +255 -0
  158. package/lib/opencode/row.d.ts +9 -0
  159. package/lib/opencode/row.js +86 -0
  160. package/lib/opencode/runtime/core.d.ts +74 -0
  161. package/lib/opencode/runtime/core.js +249 -0
  162. package/lib/opencode/runtime/events.d.ts +80 -0
  163. package/lib/opencode/runtime/events.js +281 -0
  164. package/lib/opencode/runtime/mount.d.ts +27 -0
  165. package/lib/opencode/runtime/mount.js +283 -0
  166. package/lib/opencode/runtime/server.d.ts +27 -0
  167. package/lib/opencode/runtime/server.js +353 -0
  168. package/lib/opencode/runtime/types.d.ts +109 -0
  169. package/lib/opencode/runtime/types.js +1 -0
  170. package/lib/opencode/runtime.d.ts +55 -0
  171. package/lib/opencode/runtime.js +79 -0
  172. package/lib/porting/port-gap.d.ts +30 -0
  173. package/lib/porting/port-gap.js +122 -0
  174. package/lib/porting/port-sessions.d.ts +41 -0
  175. package/lib/porting/port-sessions.js +98 -0
  176. package/lib/porting/prompt.d.ts +23 -0
  177. package/lib/porting/prompt.js +49 -0
  178. package/lib/porting/row.d.ts +16 -0
  179. package/lib/porting/row.js +25 -0
  180. package/lib/porting/verify-port.d.ts +53 -0
  181. package/lib/porting/verify-port.js +104 -0
  182. package/lib/presets/fs-search.d.ts +25 -0
  183. package/lib/presets/fs-search.js +17 -0
  184. package/lib/presets/tool-filter.d.ts +35 -0
  185. package/lib/presets/tool-filter.js +91 -0
  186. package/lib/sessions/claude.d.ts +29 -0
  187. package/lib/sessions/claude.js +83 -0
  188. package/lib/sessions/codex.d.ts +30 -0
  189. package/lib/sessions/codex.js +303 -0
  190. package/lib/sessions/opencode.d.ts +32 -0
  191. package/lib/sessions/opencode.js +320 -0
  192. package/lib/sessions/row.d.ts +14 -0
  193. package/lib/sessions/row.js +35 -0
  194. package/lib/shared/invocation.d.ts +169 -0
  195. package/lib/shared/invocation.js +325 -0
  196. package/lib/shared/render.d.ts +72 -0
  197. package/lib/shared/render.js +588 -0
  198. package/lib/shared/util.d.ts +21 -0
  199. package/lib/shared/util.js +32 -0
  200. package/lib/skills/bundled.d.ts +3 -0
  201. package/lib/skills/bundled.js +42 -0
  202. package/lib/skills/provider.d.ts +72 -0
  203. package/lib/skills/provider.js +340 -0
  204. package/lib/skills/row.d.ts +22 -0
  205. package/lib/skills/row.js +204 -0
  206. package/lib/workbench/adapter-coverage.d.ts +47 -0
  207. package/lib/workbench/adapter-coverage.js +102 -0
  208. package/lib/workbench/model-tools.d.ts +10 -0
  209. package/lib/workbench/model-tools.js +122 -0
  210. package/lib/workbench/overview.d.ts +103 -0
  211. package/lib/workbench/overview.js +159 -0
  212. package/lib/workbench/remote.d.ts +118 -0
  213. package/lib/workbench/remote.js +422 -0
  214. package/lib/workbench/row.d.ts +15 -0
  215. package/lib/workbench/row.js +16 -0
  216. package/lib/workbench/settings.d.ts +15 -0
  217. package/lib/workbench/settings.js +56 -0
  218. package/lib/workbench/workbench.d.ts +100 -0
  219. package/lib/workbench/workbench.js +260 -0
  220. package/locale/agents/en.json +6 -0
  221. package/locale/agents/zh.json +6 -0
  222. package/locale/commands/en.json +6 -0
  223. package/locale/commands/zh.json +6 -0
  224. package/locale/context/en.json +6 -0
  225. package/locale/context/zh.json +6 -0
  226. package/locale/en.json +6 -0
  227. package/locale/hooks/en.json +6 -0
  228. package/locale/hooks/zh.json +6 -0
  229. package/locale/mcp/en.json +6 -0
  230. package/locale/mcp/zh.json +6 -0
  231. package/locale/mods/en.json +6 -0
  232. package/locale/mods/zh.json +6 -0
  233. package/locale/opencode/en.json +6 -0
  234. package/locale/opencode/zh.json +6 -0
  235. package/locale/porter/en.json +6 -0
  236. package/locale/porter/zh.json +6 -0
  237. package/locale/sessions/en.json +6 -0
  238. package/locale/sessions/zh.json +6 -0
  239. package/locale/skills/en.json +6 -0
  240. package/locale/skills/zh.json +6 -0
  241. package/locale/workbench/en.json +6 -0
  242. package/locale/workbench/zh.json +6 -0
  243. package/locale/zh.json +6 -0
  244. package/package.json +246 -3
  245. package/runtime/opencode-host.mjs +270 -0
  246. package/skills/harness-forge/SKILL.md +47 -0
@@ -0,0 +1,126 @@
1
+ /**
2
+ * The shells foreign hooks and command `!` lines run in, located the way
3
+ * Claude Code locates them: Git Bash on Windows (`CLAUDE_CODE_GIT_BASH_PATH`,
4
+ * then next to `git.exe`, then the standard install directories), bash
5
+ * elsewhere (`/bin/sh` without it); PowerShell 7 (`pwsh.exe`) before Windows
6
+ * PowerShell. Processes inherit the user's environment, outside any model-tool
7
+ * sandbox, apart from the `OTEL_*` exporter variables Claude Code strips from
8
+ * every subprocess.
9
+ * @module @kirkchinese/harness-forge
10
+ */
11
+ import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
12
+ import { delimiter, dirname, join } from 'node:path';
13
+ import { runProcess } from './process.js';
14
+ /** Locate the POSIX shell foreign hooks expect. */
15
+ export function findPosixShell(env = process.env) {
16
+ if (process.platform !== 'win32') {
17
+ // Claude Code runs hooks with bash; `/bin/sh` is dash on Debian and Ubuntu, which rejects bash syntax.
18
+ const bash = ['/bin/bash', ...pathEntries(env).map(entry => join(entry, 'bash'))].find(candidate => existsSync(candidate));
19
+ return { shell: bash ?? '/bin/sh', pathPrefix: [] };
20
+ }
21
+ const candidates = [];
22
+ const declared = env['CLAUDE_CODE_GIT_BASH_PATH'];
23
+ if (declared !== undefined && declared.length > 0)
24
+ candidates.push(declared);
25
+ for (const entry of pathEntries(env)) {
26
+ if (existsSync(join(entry, 'git.exe'))) {
27
+ const root = dirname(entry);
28
+ candidates.push(join(root, 'bin', 'bash.exe'), join(dirname(root), 'bin', 'bash.exe'));
29
+ }
30
+ }
31
+ for (const base of [env['ProgramFiles'], env['ProgramW6432'], env['ProgramFiles(x86)'], env['LOCALAPPDATA'] && join(env['LOCALAPPDATA'], 'Programs')]) {
32
+ if (base !== undefined && base.length > 0)
33
+ candidates.push(join(base, 'Git', 'bin', 'bash.exe'));
34
+ }
35
+ for (const candidate of candidates) {
36
+ if (!existsSync(candidate))
37
+ continue;
38
+ const root = dirname(dirname(candidate));
39
+ return { shell: candidate, pathPrefix: [join(root, 'usr', 'bin'), join(root, 'bin')] };
40
+ }
41
+ return undefined;
42
+ }
43
+ /** PowerShell 7 when it is on PATH, else Windows PowerShell. */
44
+ export function findPowerShell(env = process.env) {
45
+ const name = process.platform === 'win32' ? 'pwsh.exe' : 'pwsh';
46
+ for (const entry of pathEntries(env)) {
47
+ if (existsSync(join(entry, name)))
48
+ return join(entry, name);
49
+ }
50
+ return process.platform === 'win32' ? 'powershell.exe' : 'pwsh';
51
+ }
52
+ /**
53
+ * Hook scripts call `python3`. On Windows that name is usually the Microsoft
54
+ * Store placeholder while the real interpreter is `python`; a one-line shim
55
+ * makes `python3` run it, so Python hooks work without changing the machine.
56
+ * @returns the shim directory to put first on PATH, when one is needed.
57
+ */
58
+ export function python3Shim(shimRoot, env = process.env) {
59
+ if (process.platform !== 'win32')
60
+ return undefined;
61
+ const entries = pathEntries(env);
62
+ const real = (name) => entries.some(entry => !/WindowsApps/iu.test(entry) && existsSync(join(entry, name)));
63
+ if (real('python3.exe') || !real('python.exe'))
64
+ return undefined;
65
+ const dir = join(shimRoot, 'bin');
66
+ const shim = join(dir, 'python3');
67
+ if (!existsSync(shim)) {
68
+ mkdirSync(dir, { recursive: true });
69
+ writeFileSync(shim, '#!/bin/sh\nexec python "$@"\n');
70
+ }
71
+ return dir;
72
+ }
73
+ /**
74
+ * Build the executors.
75
+ * @param shimRoot - writable directory for command shims (see {@link python3Shim}).
76
+ */
77
+ export function createForeignShell(base = process.env, shimRoot) {
78
+ const posix = findPosixShell(base);
79
+ const powershell = findPowerShell(base);
80
+ const shims = shimRoot === undefined ? undefined : python3Shim(shimRoot, base);
81
+ const prefix = [...(shims !== undefined ? [shims] : []), ...(posix?.pathPrefix ?? [])];
82
+ const environment = (extra = {}) => {
83
+ const env = {};
84
+ for (const [key, value] of Object.entries(base)) {
85
+ if (!key.startsWith('OTEL_'))
86
+ env[key] = value;
87
+ }
88
+ if (prefix.length > 0) {
89
+ const key = Object.keys(env).find(name => name.toUpperCase() === 'PATH') ?? 'PATH';
90
+ env[key] = [...prefix, env[key] ?? ''].join(delimiter);
91
+ }
92
+ for (const [key, value] of Object.entries(extra)) {
93
+ if (value === undefined)
94
+ delete env[key];
95
+ else
96
+ env[key] = value;
97
+ }
98
+ return env;
99
+ };
100
+ const run = (file, args, options) => runProcess({
101
+ file,
102
+ args,
103
+ cwd: options.cwd,
104
+ env: environment(options.env),
105
+ stdin: options.stdin ?? '',
106
+ ...(options.timeoutMs !== undefined ? { timeoutMs: options.timeoutMs } : {}),
107
+ ...(options.signal !== undefined ? { signal: options.signal } : {}),
108
+ });
109
+ return {
110
+ ...(posix !== undefined ? { bash: posix.shell } : {}),
111
+ powershell,
112
+ defaultKind: posix !== undefined ? 'bash' : 'powershell',
113
+ environment,
114
+ runShell(command, kind, options) {
115
+ if (kind === 'bash' && posix !== undefined)
116
+ return run(posix.shell, ['-c', command], options);
117
+ return run(powershell, ['-NoLogo', '-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-Command', command], options);
118
+ },
119
+ runExec(file, args, options) {
120
+ return run(file, args, options);
121
+ },
122
+ };
123
+ }
124
+ function pathEntries(env) {
125
+ return (env['PATH'] ?? env['Path'] ?? '').split(delimiter).filter(entry => entry.length > 0);
126
+ }
@@ -0,0 +1,28 @@
1
+ import { type ToolContext } from './vocabulary.js';
2
+ /** The session surface the mirror reads. */
3
+ export interface TranscriptSession {
4
+ readonly header: {
5
+ readonly id: string;
6
+ readonly cwd?: string;
7
+ readonly parentSession?: string;
8
+ };
9
+ readonly seq: number;
10
+ snapshotEvents(): ReadonlyArray<{
11
+ readonly type: string;
12
+ readonly time: number;
13
+ readonly data: unknown;
14
+ }>;
15
+ }
16
+ /** Keeps one JSONL mirror per session under a root directory. */
17
+ export declare class TranscriptMirror {
18
+ private readonly root;
19
+ private readonly tools;
20
+ private readonly written;
21
+ constructor(root: string, tools?: ToolContext);
22
+ /** The mirror path of a session, refreshed to its current events. */
23
+ path(session: TranscriptSession, cwd: string): string;
24
+ /** The path without writing. */
25
+ pathOf(session: TranscriptSession): string;
26
+ }
27
+ /** Render a session's message events as Claude Code JSONL. */
28
+ export declare function renderTranscript(session: TranscriptSession, cwd: string, tools?: ToolContext): string;
@@ -0,0 +1,141 @@
1
+ /**
2
+ * A Claude Code–format transcript mirror of each DSH session. Hooks receive
3
+ * its path as `transcript_path` and read it the way they read Claude Code's
4
+ * own JSONL: one record per line, `type` `user`/`assistant`, Anthropic
5
+ * content blocks (`text`, `thinking`, `tool_use`, `tool_result`) with Claude
6
+ * tool names and input shapes. The file is rewritten when the session has
7
+ * new events since the last read.
8
+ * @module @kirkchinese/harness-forge
9
+ */
10
+ import { mkdirSync, writeFileSync } from 'node:fs';
11
+ import { dirname, join } from 'node:path';
12
+ import { foreignTool } from './vocabulary.js';
13
+ /** Keeps one JSONL mirror per session under a root directory. */
14
+ export class TranscriptMirror {
15
+ root;
16
+ tools;
17
+ written = new Map();
18
+ constructor(root, tools = {}) {
19
+ this.root = root;
20
+ this.tools = tools;
21
+ }
22
+ /** The mirror path of a session, refreshed to its current events. */
23
+ path(session, cwd) {
24
+ const path = this.pathOf(session);
25
+ if (this.written.get(path) === session.seq)
26
+ return path;
27
+ try {
28
+ mkdirSync(dirname(path), { recursive: true });
29
+ writeFileSync(path, renderTranscript(session, cwd, this.tools));
30
+ this.written.set(path, session.seq);
31
+ }
32
+ catch {
33
+ // A transcript that cannot be written leaves the previous copy in place.
34
+ }
35
+ return path;
36
+ }
37
+ /** The path without writing. */
38
+ pathOf(session) {
39
+ const parent = session.header.parentSession;
40
+ return parent === undefined
41
+ ? join(this.root, `${session.header.id}.jsonl`)
42
+ : join(this.root, parent, 'subagents', `agent-${session.header.id}.jsonl`);
43
+ }
44
+ }
45
+ /** Render a session's message events as Claude Code JSONL. */
46
+ export function renderTranscript(session, cwd, tools = {}) {
47
+ const lines = [];
48
+ let parent = null;
49
+ let index = 0;
50
+ const sessionId = session.header.id;
51
+ const push = (record, time) => {
52
+ const uuid = `${sessionId}-${String(index++).padStart(6, '0')}`;
53
+ lines.push(JSON.stringify({
54
+ parentUuid: parent,
55
+ isSidechain: session.header.parentSession !== undefined,
56
+ userType: 'external',
57
+ cwd,
58
+ sessionId,
59
+ version: 'dsh-harness-forge',
60
+ ...record,
61
+ uuid,
62
+ timestamp: new Date(time).toISOString(),
63
+ }));
64
+ parent = uuid;
65
+ };
66
+ for (const event of session.snapshotEvents()) {
67
+ const data = isRecord(event.data) ? event.data : {};
68
+ if (event.type === 'user/message') {
69
+ const content = blocks(data['content']).flatMap(userBlock);
70
+ if (content.length === 0)
71
+ continue;
72
+ push({ type: 'user', message: { role: 'user', content } }, event.time);
73
+ }
74
+ else if (event.type === 'assistant/message') {
75
+ const message = isRecord(data['message']) ? data['message'] : {};
76
+ const content = blocks(message['content']).flatMap(block => assistantBlock(block, cwd, tools));
77
+ if (content.length === 0)
78
+ continue;
79
+ const source = isRecord(message['source']) ? message['source'] : {};
80
+ push({
81
+ type: 'assistant',
82
+ message: {
83
+ id: typeof message['id'] === 'string' ? message['id'] : `msg_${String(index)}`,
84
+ type: 'message',
85
+ role: 'assistant',
86
+ model: typeof source['model'] === 'string' ? source['model'] : 'dsh',
87
+ content,
88
+ stop_reason: content.some(block => block['type'] === 'tool_use') ? 'tool_use' : 'end_turn',
89
+ stop_sequence: null,
90
+ usage: { input_tokens: 0, output_tokens: 0 },
91
+ },
92
+ }, event.time);
93
+ }
94
+ else if (event.type === 'tool/result') {
95
+ const message = isRecord(data['message']) ? data['message'] : {};
96
+ const callId = message['toolCallId'] ?? (isRecord(message['source']) ? message['source']['callId'] : undefined);
97
+ if (typeof callId !== 'string')
98
+ continue;
99
+ const text = blocks(message['content']).map(block => typeof block['text'] === 'string' ? block['text'] : '').join('');
100
+ push({
101
+ type: 'user',
102
+ message: { role: 'user', content: [{ type: 'tool_result', tool_use_id: callId, content: text, is_error: message['isError'] === true }] },
103
+ }, event.time);
104
+ }
105
+ }
106
+ return lines.length === 0 ? '' : `${lines.join('\n')}\n`;
107
+ }
108
+ function userBlock(block) {
109
+ if (block['type'] === 'text' && typeof block['text'] === 'string')
110
+ return [{ type: 'text', text: block['text'] }];
111
+ if (block['type'] === 'image')
112
+ return [{ type: 'image', source: block['source'] ?? {} }];
113
+ return [];
114
+ }
115
+ function assistantBlock(block, cwd, tools) {
116
+ if (block['type'] === 'text' && typeof block['text'] === 'string')
117
+ return [{ type: 'text', text: block['text'] }];
118
+ if (block['type'] === 'reasoning' && typeof block['text'] === 'string')
119
+ return [{ type: 'thinking', thinking: block['text'], signature: '' }];
120
+ if (block['type'] === 'tool-call') {
121
+ const name = String(block['name'] ?? '');
122
+ const args = typeof block['arguments'] === 'string' ? parse(block['arguments']) : block['arguments'];
123
+ const tool = foreignTool('claude-code', name, args, { ...tools, cwd });
124
+ return [{ type: 'tool_use', id: block['id'], name: tool.name, input: tool.input }];
125
+ }
126
+ return [];
127
+ }
128
+ function blocks(value) {
129
+ return Array.isArray(value) ? value.filter(isRecord) : [];
130
+ }
131
+ function parse(text) {
132
+ try {
133
+ return JSON.parse(text);
134
+ }
135
+ catch {
136
+ return {};
137
+ }
138
+ }
139
+ function isRecord(value) {
140
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
141
+ }
@@ -0,0 +1,57 @@
1
+ /** Foreign dialects with their own tool vocabulary. */
2
+ export type HookDialect = 'claude-code' | 'codex';
3
+ /** How one DSH tool call is seen by a foreign hook. */
4
+ export interface ForeignTool {
5
+ /** The DSH tool name. */
6
+ readonly dshName: string;
7
+ /** The canonical foreign name placed in `tool_name`. */
8
+ readonly name: string;
9
+ /** Every name a matcher or rule may use for this call (canonical first, DSH name last). */
10
+ readonly aliases: readonly string[];
11
+ /** The tool input in the foreign shape. */
12
+ readonly input: Record<string, unknown>;
13
+ }
14
+ /** What a translation needs beyond the call itself. */
15
+ export interface ToolContext {
16
+ /** The session's working directory; relative file paths resolve against it. */
17
+ readonly cwd?: string;
18
+ /** Maps a delegation preset id to the foreign agent name (`plugin:agent`). */
19
+ readonly agentName?: (presetId: string) => string | undefined;
20
+ /** Maps a foreign agent name back to a DSH preset id. */
21
+ readonly presetOf?: (foreignName: string) => string | undefined;
22
+ }
23
+ /** The foreign view of one DSH tool call. MCP and unknown tools keep their names and input. */
24
+ export declare function foreignTool(dialect: HookDialect, dshName: string, args: unknown, context?: ToolContext): ForeignTool;
25
+ /**
26
+ * Map a foreign tool name from an agent/skill allow list or a rule to DSH
27
+ * names. An MCP tool is reachable directly or through the capability catalog.
28
+ */
29
+ export declare function dshToolsFor(foreignName: string, dialect?: 'claude-code' | 'codex'): string[];
30
+ /**
31
+ * DSH arguments for a hook's `updatedInput`. The foreign input replaces the
32
+ * whole input; DSH-only fields the hook cannot see (working directory,
33
+ * sandbox escalation) keep their original values.
34
+ * @throws when the replacement cannot be expressed as DSH arguments.
35
+ */
36
+ export declare function dshInput(dialect: HookDialect, dshName: string, original: unknown, updated: Record<string, unknown>, context?: ToolContext): Record<string, unknown>;
37
+ /**
38
+ * A shell call that ran but failed, as Claude Code reports it: a non-zero
39
+ * exit, a timeout or an abort is a tool failure (`PostToolUseFailure`), with
40
+ * `Exit code N` on the first line of the error. DSH returns such a call as a
41
+ * value carrying the exit code.
42
+ */
43
+ export declare function shellFailure(dshName: string, value: unknown): string | undefined;
44
+ /** The structured result a foreign PostToolUse hook receives as `tool_response`. */
45
+ export declare function foreignResponse(dialect: HookDialect, dshName: string, args: unknown, value: unknown, text: string): unknown;
46
+ /**
47
+ * The DSH value for a hook's `updatedToolOutput`, when the replacement has the
48
+ * foreign output shape of a tool whose DSH value can carry it.
49
+ * @returns undefined when only the model-facing text can be replaced.
50
+ */
51
+ export declare function dshValue(dshName: string, original: unknown, updated: unknown): unknown;
52
+ /** The value a rule such as `Edit(src
53
+
54
+ /** Whether a DSH tool runs shell commands. */
55
+ export declare function isShellTool(dshName: string): boolean;
56
+ /** An absolute path in the platform's native form; `~` expands to the home directory. */
57
+ export declare function absolutePath(path: string, cwd: string | undefined): string;