@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,91 @@
|
|
|
1
|
+
import z from '@deepseek-ai/schemastery';
|
|
2
|
+
import { CATALOG_CALL_TOOL, CATALOG_TOOLS, catalogToolName } from '../mcp/catalog.js';
|
|
3
|
+
import { onDsh } from '../dsh/host.js';
|
|
4
|
+
/** Schemastery validation for the filter row. */
|
|
5
|
+
export const Config = z.object({
|
|
6
|
+
allow: z.array(z.string()),
|
|
7
|
+
deny: z.array(z.string()),
|
|
8
|
+
});
|
|
9
|
+
/** Cordis plugin name. */
|
|
10
|
+
export const name = 'harness-forge-tool-filter';
|
|
11
|
+
/** The filter needs the scoped tool registry. */
|
|
12
|
+
export const inject = ['tools'];
|
|
13
|
+
const CATALOG = new Set(CATALOG_TOOLS);
|
|
14
|
+
/** Whether an MCP entry of an agent's tool list names this MCP tool. */
|
|
15
|
+
export function mcpEntryMatches(entry, tool) {
|
|
16
|
+
const base = entry.endsWith('__*') ? entry.slice(0, -3) : entry;
|
|
17
|
+
return tool === base || tool.startsWith(`${base}__`);
|
|
18
|
+
}
|
|
19
|
+
/** Why a call is outside an agent's tool list, or undefined when it is inside. */
|
|
20
|
+
export function exclusion(config, tool, args) {
|
|
21
|
+
const allow = config.allow;
|
|
22
|
+
const deny = config.deny ?? [];
|
|
23
|
+
const mcpAllow = (allow ?? []).filter(entry => entry.startsWith('mcp__'));
|
|
24
|
+
const mcpDeny = deny.filter(entry => entry.startsWith('mcp__'));
|
|
25
|
+
const refused = `${tool} is not available to this agent`;
|
|
26
|
+
if (CATALOG.has(tool)) {
|
|
27
|
+
if (tool === CATALOG_CALL_TOOL) {
|
|
28
|
+
const mcpName = catalogToolName(args?.id);
|
|
29
|
+
if (mcpName !== undefined) {
|
|
30
|
+
if (mcpDeny.some(entry => mcpEntryMatches(entry, mcpName)))
|
|
31
|
+
return `${mcpName} is not available to this agent`;
|
|
32
|
+
if (allow !== undefined && !mcpAllow.some(entry => mcpEntryMatches(entry, mcpName)))
|
|
33
|
+
return `${mcpName} is not available to this agent`;
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return allow !== undefined && mcpAllow.length === 0 ? refused : undefined;
|
|
38
|
+
}
|
|
39
|
+
if (tool.startsWith('mcp__')) {
|
|
40
|
+
if (mcpDeny.some(entry => mcpEntryMatches(entry, tool)))
|
|
41
|
+
return refused;
|
|
42
|
+
return allow !== undefined && !mcpAllow.some(entry => mcpEntryMatches(entry, tool)) ? refused : undefined;
|
|
43
|
+
}
|
|
44
|
+
if (deny.includes(tool))
|
|
45
|
+
return refused;
|
|
46
|
+
return allow !== undefined && !allow.includes(tool) ? refused : undefined;
|
|
47
|
+
}
|
|
48
|
+
/** Apply the restriction for the lifetime of the preset row. */
|
|
49
|
+
export function apply(ctx, config) {
|
|
50
|
+
if (config.allow === undefined && (config.deny ?? []).length === 0)
|
|
51
|
+
return;
|
|
52
|
+
ctx.effect(() => ctx.tools.guard(execution => exclusion(config, execution.name, execution.arguments)), 'harness-forge.tool-guard');
|
|
53
|
+
// A catalog meta-tool stays visible when any MCP tool is allowed; its calls are judged one by one.
|
|
54
|
+
// `restrict` reaches global tools only; a tool a kept preset row registers beside a listed one
|
|
55
|
+
// (`glob` beside `grep`) stays visible, and the guard refuses its calls. Global tools registered after
|
|
56
|
+
// the preset started (a delegation tool, an MCP server that connected later) are hidden as they appear.
|
|
57
|
+
const excluded = (tool) => exclusion(config, tool, undefined) !== undefined && !(tool === CATALOG_CALL_TOOL && (config.allow ?? []).some(entry => entry.startsWith('mcp__')));
|
|
58
|
+
const restricted = new Map();
|
|
59
|
+
let hiding = false;
|
|
60
|
+
const hide = () => {
|
|
61
|
+
// Restricting changes the tools, which raises tools/change again: one pass at a time.
|
|
62
|
+
if (hiding)
|
|
63
|
+
return;
|
|
64
|
+
hiding = true;
|
|
65
|
+
try {
|
|
66
|
+
hidePass();
|
|
67
|
+
}
|
|
68
|
+
finally {
|
|
69
|
+
hiding = false;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
const hidePass = () => {
|
|
73
|
+
for (const tool of ctx.tools.schemas().map(schema => schema.name)) {
|
|
74
|
+
if (restricted.has(tool))
|
|
75
|
+
continue;
|
|
76
|
+
if (!excluded(tool))
|
|
77
|
+
continue;
|
|
78
|
+
try {
|
|
79
|
+
restricted.set(tool, ctx.tools.restrict({ deny: [tool] }));
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
// A tool of this preset's own rows cannot be masked; the guard refuses its calls.
|
|
83
|
+
restricted.set(tool, () => { });
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
hide();
|
|
88
|
+
onDsh(ctx, 'tools/change', (() => { hide(); }));
|
|
89
|
+
ctx.effect(() => () => { for (const dispose of restricted.values())
|
|
90
|
+
dispose(); }, 'harness-forge.tool-filter');
|
|
91
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
2
|
+
/** One discoverable Claude Code transcript. */
|
|
3
|
+
export interface SessionCandidate {
|
|
4
|
+
readonly path: string;
|
|
5
|
+
readonly sourceId: string;
|
|
6
|
+
readonly source?: 'claude-code' | 'codex' | 'opencode';
|
|
7
|
+
readonly title?: string;
|
|
8
|
+
readonly cwd?: string;
|
|
9
|
+
readonly createdAt?: number;
|
|
10
|
+
readonly origin?: 'subagent';
|
|
11
|
+
}
|
|
12
|
+
/** Result of one session import. */
|
|
13
|
+
export interface SessionImportReport {
|
|
14
|
+
readonly id: string;
|
|
15
|
+
readonly sourcePath: string;
|
|
16
|
+
readonly title?: string;
|
|
17
|
+
readonly turns: number;
|
|
18
|
+
readonly events: number;
|
|
19
|
+
readonly stats: Record<string, number>;
|
|
20
|
+
}
|
|
21
|
+
/** Default Claude Code projects directory. */
|
|
22
|
+
export declare function defaultClaudeProjectsRoot(home?: string): string;
|
|
23
|
+
/** Discover Claude Code main transcripts under a root. */
|
|
24
|
+
export declare function discoverClaudeSessions(root?: string, limit?: number): Promise<SessionCandidate[]>;
|
|
25
|
+
/** Import one Claude Code transcript into DSH session persistence. */
|
|
26
|
+
export declare function importClaudeSession(ctx: Context, sourcePath: string, options?: {
|
|
27
|
+
readonly includeSubagents?: boolean;
|
|
28
|
+
readonly targetId?: string;
|
|
29
|
+
}): Promise<SessionImportReport>;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Claude Code session migration. The adapter normalizes parent-chain JSONL;
|
|
3
|
+
* the shared synthesizer emits DSH v4 events; this module owns discovery and
|
|
4
|
+
* the persistence hand-off so the UI never writes session storage directly.
|
|
5
|
+
* @module @kirkchinese/harness-forge
|
|
6
|
+
*/
|
|
7
|
+
import { homedir } from 'node:os';
|
|
8
|
+
import { join } from 'node:path';
|
|
9
|
+
import { claudeCodeAdapter, synthesizeDshSession } from '@kirkchinese/harness-forge-core';
|
|
10
|
+
import { dshService } from '../dsh/host.js';
|
|
11
|
+
/** Default Claude Code projects directory. */
|
|
12
|
+
export function defaultClaudeProjectsRoot(home = homedir()) {
|
|
13
|
+
return join(home, '.claude', 'projects');
|
|
14
|
+
}
|
|
15
|
+
/** Discover Claude Code main transcripts under a root. */
|
|
16
|
+
export async function discoverClaudeSessions(root = defaultClaudeProjectsRoot(), limit = 200) {
|
|
17
|
+
const out = [];
|
|
18
|
+
for await (const item of claudeCodeAdapter.discover(root, { recursive: true })) {
|
|
19
|
+
out.push({
|
|
20
|
+
path: item.ref,
|
|
21
|
+
sourceId: item.sourceId ?? item.ref,
|
|
22
|
+
source: 'claude-code',
|
|
23
|
+
...(item.title !== undefined ? { title: item.title } : {}),
|
|
24
|
+
...(item.cwd !== undefined ? { cwd: item.cwd } : {}),
|
|
25
|
+
...(item.createdAt !== undefined ? { createdAt: item.createdAt } : {}),
|
|
26
|
+
...(item.origin !== undefined ? { origin: item.origin } : {}),
|
|
27
|
+
});
|
|
28
|
+
if (out.length >= limit)
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
out.sort((a, b) => (b.createdAt ?? 0) - (a.createdAt ?? 0));
|
|
32
|
+
return out;
|
|
33
|
+
}
|
|
34
|
+
/** Import one Claude Code transcript into DSH session persistence. */
|
|
35
|
+
export async function importClaudeSession(ctx, sourcePath, options = {}) {
|
|
36
|
+
const persistence = dshService(ctx, 'sessionPersistence');
|
|
37
|
+
if (persistence === undefined)
|
|
38
|
+
throw new Error('sessionPersistence service is not available');
|
|
39
|
+
const candidate = {
|
|
40
|
+
ref: sourcePath,
|
|
41
|
+
sourceId: sourcePath,
|
|
42
|
+
};
|
|
43
|
+
const parsed = await claudeCodeAdapter.readSession(candidate, {
|
|
44
|
+
...(options.includeSubagents === true ? { includeAuxiliary: true } : {}),
|
|
45
|
+
...(options.targetId !== undefined ? { targetId: options.targetId } : {}),
|
|
46
|
+
});
|
|
47
|
+
// A transcript with no conversation on its main chain would become an empty session: say why instead.
|
|
48
|
+
if (parsed.session.turns.length === 0)
|
|
49
|
+
throw new Error(`nothing to import from ${sourcePath}: ${parsed.stats.reasons.join('; ') || 'no conversation in the transcript'}`);
|
|
50
|
+
const synthesized = synthesizeDshSession(parsed.session);
|
|
51
|
+
const events = synthesized.events;
|
|
52
|
+
const handle = await persistence.create({
|
|
53
|
+
version: 4,
|
|
54
|
+
id: synthesized.meta.id,
|
|
55
|
+
createdAt: synthesized.meta.createdAt,
|
|
56
|
+
isSeeded: false,
|
|
57
|
+
...(synthesized.meta.cwd !== undefined ? { cwd: synthesized.meta.cwd } : {}),
|
|
58
|
+
delegationDepth: synthesized.meta.delegationDepth,
|
|
59
|
+
...(synthesized.meta.origin !== undefined ? { origin: synthesized.meta.origin } : {}),
|
|
60
|
+
...(synthesized.meta.parentSession !== undefined ? { parentSession: synthesized.meta.parentSession } : {}),
|
|
61
|
+
});
|
|
62
|
+
try {
|
|
63
|
+
await handle.append(events);
|
|
64
|
+
await handle.flush();
|
|
65
|
+
}
|
|
66
|
+
finally {
|
|
67
|
+
await handle.close();
|
|
68
|
+
}
|
|
69
|
+
return {
|
|
70
|
+
id: synthesized.meta.id,
|
|
71
|
+
sourcePath,
|
|
72
|
+
...(parsed.session.title !== undefined ? { title: parsed.session.title } : {}),
|
|
73
|
+
turns: synthesized.stats.turns,
|
|
74
|
+
events: events.length,
|
|
75
|
+
stats: {
|
|
76
|
+
steps: synthesized.stats.steps,
|
|
77
|
+
messages: synthesized.stats.messages,
|
|
78
|
+
toolCalls: synthesized.stats.toolCalls,
|
|
79
|
+
toolResults: synthesized.stats.toolResults,
|
|
80
|
+
synthesizedToolResults: synthesized.stats.synthesizedToolResults,
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
2
|
+
import type { NormalizedSession } from '@kirkchinese/harness-forge-core';
|
|
3
|
+
export interface CodexSessionCandidate {
|
|
4
|
+
readonly path: string;
|
|
5
|
+
readonly sourceId: string;
|
|
6
|
+
readonly title?: string;
|
|
7
|
+
readonly cwd?: string;
|
|
8
|
+
readonly createdAt?: number;
|
|
9
|
+
}
|
|
10
|
+
export interface CodexImportReport {
|
|
11
|
+
readonly id: string;
|
|
12
|
+
readonly sourcePath: string;
|
|
13
|
+
readonly title?: string;
|
|
14
|
+
readonly turns: number;
|
|
15
|
+
readonly events: number;
|
|
16
|
+
readonly stats: Record<string, number>;
|
|
17
|
+
}
|
|
18
|
+
/** Default Codex rollout directory. */
|
|
19
|
+
export declare function defaultCodexSessionsRoot(home?: string): string;
|
|
20
|
+
/** Discover `rollout-*.jsonl` files newest first. */
|
|
21
|
+
export declare function discoverCodexSessions(root?: string, limit?: number): Promise<CodexSessionCandidate[]>;
|
|
22
|
+
/** Parse one Codex rollout into the shared normalized model. */
|
|
23
|
+
export declare function readCodexSession(path: string, targetId?: string): Promise<{
|
|
24
|
+
session: NormalizedSession;
|
|
25
|
+
title?: string;
|
|
26
|
+
}>;
|
|
27
|
+
/** Import one Codex rollout into DSH session persistence. */
|
|
28
|
+
export declare function importCodexSession(ctx: Context, sourcePath: string, options?: {
|
|
29
|
+
readonly targetId?: string;
|
|
30
|
+
}): Promise<CodexImportReport>;
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Codex CLI rollout migration. Codex stores one JSONL rollout per session with
|
|
3
|
+
* `session_meta`, `response_item` messages/tool calls, and event markers. This
|
|
4
|
+
* adapter reduces that vocabulary to the shared NormalizedSession model, then
|
|
5
|
+
* the shared session synthesizer emits DSH v4 events.
|
|
6
|
+
* @module @kirkchinese/harness-forge
|
|
7
|
+
*/
|
|
8
|
+
import { readdir } from 'node:fs/promises';
|
|
9
|
+
import { homedir } from 'node:os';
|
|
10
|
+
import { join } from 'node:path';
|
|
11
|
+
import { synthesizeDshSession } from '@kirkchinese/harness-forge-core';
|
|
12
|
+
import { dshService } from '../dsh/host.js';
|
|
13
|
+
/** Default Codex rollout directory. */
|
|
14
|
+
export function defaultCodexSessionsRoot(home = homedir()) {
|
|
15
|
+
return join(home, '.codex', 'sessions');
|
|
16
|
+
}
|
|
17
|
+
/** Discover `rollout-*.jsonl` files newest first. */
|
|
18
|
+
export async function discoverCodexSessions(root = defaultCodexSessionsRoot(), limit = 200) {
|
|
19
|
+
const files = await listRollouts(root);
|
|
20
|
+
const out = [];
|
|
21
|
+
for (const path of files) {
|
|
22
|
+
const meta = await readMeta(path);
|
|
23
|
+
if (meta === undefined)
|
|
24
|
+
continue;
|
|
25
|
+
out.push(meta);
|
|
26
|
+
if (out.length >= limit)
|
|
27
|
+
break;
|
|
28
|
+
}
|
|
29
|
+
out.sort((a, b) => (b.createdAt ?? 0) - (a.createdAt ?? 0));
|
|
30
|
+
return out;
|
|
31
|
+
}
|
|
32
|
+
/** Parse one Codex rollout into the shared normalized model. */
|
|
33
|
+
export async function readCodexSession(path, targetId) {
|
|
34
|
+
const records = await readRecords(path);
|
|
35
|
+
const meta = records.find(record => record.type === 'session_meta')?.payload;
|
|
36
|
+
if (meta === undefined)
|
|
37
|
+
throw new Error(`Codex rollout has no session_meta: ${path}`);
|
|
38
|
+
const id = targetId ?? stringField(meta['session_id']) ?? stringField(meta['id']);
|
|
39
|
+
if (id === undefined)
|
|
40
|
+
throw new Error(`Codex rollout has no session id: ${path}`);
|
|
41
|
+
const createdAt = timestamp(meta['timestamp']) ?? timestamp(records[0]?.timestamp) ?? 0;
|
|
42
|
+
const cwd = stringField(meta['cwd']);
|
|
43
|
+
const turns = [];
|
|
44
|
+
let turn;
|
|
45
|
+
let step;
|
|
46
|
+
const pending = new Map();
|
|
47
|
+
let title;
|
|
48
|
+
const startTurn = (prompt, at) => {
|
|
49
|
+
turn = { number: turns.length + 1, prompt, ...(at !== undefined ? { timestamp: at } : {}), steps: [] };
|
|
50
|
+
turns.push(turn);
|
|
51
|
+
step = undefined;
|
|
52
|
+
if (title === undefined && prompt.trim().length > 0)
|
|
53
|
+
title = prompt.trim().slice(0, 80);
|
|
54
|
+
};
|
|
55
|
+
const ensureTurn = () => {
|
|
56
|
+
if (turn === undefined)
|
|
57
|
+
startTurn('');
|
|
58
|
+
return turn;
|
|
59
|
+
};
|
|
60
|
+
const ensureStep = (at) => {
|
|
61
|
+
const owner = ensureTurn();
|
|
62
|
+
if (step === undefined) {
|
|
63
|
+
step = { number: owner.steps.length + 1, ...(at !== undefined ? { timestamp: at } : {}), content: [], toolCalls: [], toolResults: [] };
|
|
64
|
+
owner.steps = [...owner.steps, step];
|
|
65
|
+
}
|
|
66
|
+
return step;
|
|
67
|
+
};
|
|
68
|
+
for (const record of records) {
|
|
69
|
+
const payload = record.payload;
|
|
70
|
+
if (payload === undefined)
|
|
71
|
+
continue;
|
|
72
|
+
const at = timestamp(record.timestamp) ?? timestamp(payload['timestamp']);
|
|
73
|
+
if (record.type === 'event_msg' && payload['type'] === 'user_message') {
|
|
74
|
+
const prompt = stringField(payload['message']);
|
|
75
|
+
if (prompt !== undefined && turn?.prompt !== prompt)
|
|
76
|
+
startTurn(prompt, at);
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
if (record.type !== 'response_item')
|
|
80
|
+
continue;
|
|
81
|
+
const kind = payload['type'];
|
|
82
|
+
if (kind === 'message') {
|
|
83
|
+
const role = payload['role'];
|
|
84
|
+
const text = textOf(payload['content']);
|
|
85
|
+
if (role === 'assistant') {
|
|
86
|
+
const current = ensureStep(at);
|
|
87
|
+
if (text.length > 0)
|
|
88
|
+
current.content = [...current.content, { type: 'text', text }];
|
|
89
|
+
}
|
|
90
|
+
else if (role === 'user' && usablePrompt(text) && (turn === undefined || turn.prompt.length === 0 || step !== undefined)) {
|
|
91
|
+
if (turn?.prompt !== text)
|
|
92
|
+
startTurn(text, at);
|
|
93
|
+
}
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
if (kind === 'reasoning') {
|
|
97
|
+
const text = textOf(payload['summary']) || textOf(payload['content']);
|
|
98
|
+
if (text.length > 0) {
|
|
99
|
+
const current = ensureStep(at);
|
|
100
|
+
current.content = [...current.content, { type: 'reasoning', text }];
|
|
101
|
+
}
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
const callId = stringField(payload['call_id']);
|
|
105
|
+
if (kind === 'function_call' || kind === 'custom_tool_call') {
|
|
106
|
+
if (callId === undefined)
|
|
107
|
+
continue;
|
|
108
|
+
const name = stringField(payload['name']) ?? 'tool';
|
|
109
|
+
const raw = kind === 'function_call' ? parseJson(stringField(payload['arguments'])) : stringField(payload['input']);
|
|
110
|
+
const call = { type: 'tool-call', id: callId, name, arguments: raw ?? {} };
|
|
111
|
+
const current = ensureStep(at);
|
|
112
|
+
current.content = [...current.content, call];
|
|
113
|
+
current.toolCalls = [...current.toolCalls, { id: callId, name, arguments: raw ?? {} }];
|
|
114
|
+
pending.set(callId, { call, step: current });
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
if (kind === 'function_call_output' || kind === 'custom_tool_call_output') {
|
|
118
|
+
if (callId === undefined)
|
|
119
|
+
continue;
|
|
120
|
+
const owner = pending.get(callId);
|
|
121
|
+
if (owner === undefined)
|
|
122
|
+
continue;
|
|
123
|
+
const result = {
|
|
124
|
+
toolCallId: callId,
|
|
125
|
+
content: [{ type: 'text', text: textOf(payload['output']) || stringField(payload['output']) || '' }],
|
|
126
|
+
...(at !== undefined ? { timestamp: at } : {}),
|
|
127
|
+
};
|
|
128
|
+
owner.step.toolResults = [...owner.step.toolResults, result];
|
|
129
|
+
pending.delete(callId);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
if (turns.length === 0) {
|
|
133
|
+
const fallback = records
|
|
134
|
+
.filter(record => record.type === 'response_item' && record.payload?.['type'] === 'message' && record.payload['role'] === 'user')
|
|
135
|
+
.map(record => textOf(record.payload?.['content']))
|
|
136
|
+
.find(text => text.trim().length > 0 && !text.trimStart().startsWith('<'));
|
|
137
|
+
if (fallback !== undefined)
|
|
138
|
+
startTurn(fallback);
|
|
139
|
+
}
|
|
140
|
+
return {
|
|
141
|
+
session: {
|
|
142
|
+
id,
|
|
143
|
+
source: { tool: 'codex', path, sessionId: id },
|
|
144
|
+
createdAt,
|
|
145
|
+
...(cwd !== undefined ? { cwd } : {}),
|
|
146
|
+
...(title !== undefined ? { title } : {}),
|
|
147
|
+
turns: turns,
|
|
148
|
+
},
|
|
149
|
+
...(title !== undefined ? { title } : {}),
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
/** Import one Codex rollout into DSH session persistence. */
|
|
153
|
+
export async function importCodexSession(ctx, sourcePath, options = {}) {
|
|
154
|
+
const persistence = dshService(ctx, 'sessionPersistence');
|
|
155
|
+
if (persistence === undefined)
|
|
156
|
+
throw new Error('sessionPersistence service is not available');
|
|
157
|
+
const parsed = await readCodexSession(sourcePath, options.targetId);
|
|
158
|
+
if (parsed.session.turns.length === 0)
|
|
159
|
+
throw new Error(`nothing to import from ${sourcePath}: no conversation in the rollout`);
|
|
160
|
+
const synthesized = synthesizeDshSession(parsed.session);
|
|
161
|
+
const events = synthesized.events;
|
|
162
|
+
const handle = await persistence.create({
|
|
163
|
+
version: 4,
|
|
164
|
+
id: synthesized.meta.id,
|
|
165
|
+
createdAt: synthesized.meta.createdAt,
|
|
166
|
+
isSeeded: false,
|
|
167
|
+
...(synthesized.meta.cwd !== undefined ? { cwd: synthesized.meta.cwd } : {}),
|
|
168
|
+
delegationDepth: synthesized.meta.delegationDepth,
|
|
169
|
+
...(synthesized.meta.origin !== undefined ? { origin: synthesized.meta.origin } : {}),
|
|
170
|
+
...(synthesized.meta.parentSession !== undefined ? { parentSession: synthesized.meta.parentSession } : {}),
|
|
171
|
+
});
|
|
172
|
+
try {
|
|
173
|
+
await handle.append(events);
|
|
174
|
+
await handle.flush();
|
|
175
|
+
}
|
|
176
|
+
finally {
|
|
177
|
+
await handle.close();
|
|
178
|
+
}
|
|
179
|
+
return {
|
|
180
|
+
id: synthesized.meta.id,
|
|
181
|
+
sourcePath,
|
|
182
|
+
...(parsed.title !== undefined ? { title: parsed.title } : {}),
|
|
183
|
+
turns: synthesized.stats.turns,
|
|
184
|
+
events: events.length,
|
|
185
|
+
stats: {
|
|
186
|
+
steps: synthesized.stats.steps,
|
|
187
|
+
messages: synthesized.stats.messages,
|
|
188
|
+
toolCalls: synthesized.stats.toolCalls,
|
|
189
|
+
toolResults: synthesized.stats.toolResults,
|
|
190
|
+
synthesizedToolResults: synthesized.stats.synthesizedToolResults,
|
|
191
|
+
},
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
async function listRollouts(root) {
|
|
195
|
+
const out = [];
|
|
196
|
+
const walk = async (dir) => {
|
|
197
|
+
let entries;
|
|
198
|
+
try {
|
|
199
|
+
entries = await readdir(dir, { withFileTypes: true });
|
|
200
|
+
}
|
|
201
|
+
catch {
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
for (const entry of entries) {
|
|
205
|
+
const path = join(dir, entry.name);
|
|
206
|
+
if (entry.isDirectory())
|
|
207
|
+
await walk(path);
|
|
208
|
+
else if (entry.isFile() && /^rollout-.*\.jsonl$/u.test(entry.name))
|
|
209
|
+
out.push(path);
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
await walk(root);
|
|
213
|
+
return out;
|
|
214
|
+
}
|
|
215
|
+
async function readMeta(path) {
|
|
216
|
+
const records = await readRecords(path).catch(() => []);
|
|
217
|
+
const meta = records.find(record => record.type === 'session_meta')?.payload;
|
|
218
|
+
if (meta === undefined)
|
|
219
|
+
return undefined;
|
|
220
|
+
const sourceId = stringField(meta['session_id']) ?? stringField(meta['id']);
|
|
221
|
+
if (sourceId === undefined)
|
|
222
|
+
return undefined;
|
|
223
|
+
const cwd = stringField(meta['cwd']);
|
|
224
|
+
const createdAt = timestamp(meta['timestamp']) ?? timestamp(records[0]?.timestamp);
|
|
225
|
+
const firstPrompt = records
|
|
226
|
+
.find(record => record.type === 'event_msg' && record.payload?.['type'] === 'user_message')
|
|
227
|
+
?.payload?.['message'];
|
|
228
|
+
const title = typeof firstPrompt === 'string' && firstPrompt.trim().length > 0 ? firstPrompt.trim().slice(0, 80) : undefined;
|
|
229
|
+
return {
|
|
230
|
+
path,
|
|
231
|
+
sourceId,
|
|
232
|
+
...(title !== undefined ? { title } : {}),
|
|
233
|
+
...(cwd !== undefined ? { cwd } : {}),
|
|
234
|
+
...(createdAt !== undefined ? { createdAt } : {}),
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
async function readRecords(path) {
|
|
238
|
+
const { readFile } = await import('node:fs/promises');
|
|
239
|
+
const text = await readFile(path, 'utf8');
|
|
240
|
+
const records = [];
|
|
241
|
+
for (const line of text.split(/\r?\n/u)) {
|
|
242
|
+
if (line.trim().length === 0)
|
|
243
|
+
continue;
|
|
244
|
+
try {
|
|
245
|
+
const value = JSON.parse(line);
|
|
246
|
+
if (typeof value === 'object' && value !== null)
|
|
247
|
+
records.push(value);
|
|
248
|
+
}
|
|
249
|
+
catch {
|
|
250
|
+
// A partially written rollout line is ignored; the rest remains readable.
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
return records;
|
|
254
|
+
}
|
|
255
|
+
function usablePrompt(text) {
|
|
256
|
+
const value = text.trim();
|
|
257
|
+
if (value.length === 0)
|
|
258
|
+
return false;
|
|
259
|
+
if (value.startsWith('<') || value.startsWith('# AGENTS.md'))
|
|
260
|
+
return false;
|
|
261
|
+
return !(value.includes('<environment_context>')
|
|
262
|
+
|| value.includes('<skills_instructions>')
|
|
263
|
+
|| value.includes('<permissions instructions>')
|
|
264
|
+
|| value.includes('<user_instructions>'));
|
|
265
|
+
}
|
|
266
|
+
function textOf(value) {
|
|
267
|
+
if (typeof value === 'string')
|
|
268
|
+
return value;
|
|
269
|
+
if (!Array.isArray(value))
|
|
270
|
+
return '';
|
|
271
|
+
return value
|
|
272
|
+
.map((item) => {
|
|
273
|
+
if (typeof item === 'string')
|
|
274
|
+
return item;
|
|
275
|
+
if (typeof item !== 'object' || item === null)
|
|
276
|
+
return '';
|
|
277
|
+
const record = item;
|
|
278
|
+
return typeof record['text'] === 'string' ? record['text'] : '';
|
|
279
|
+
})
|
|
280
|
+
.filter(Boolean)
|
|
281
|
+
.join('\n');
|
|
282
|
+
}
|
|
283
|
+
function parseJson(value) {
|
|
284
|
+
if (value === undefined)
|
|
285
|
+
return undefined;
|
|
286
|
+
try {
|
|
287
|
+
return JSON.parse(value);
|
|
288
|
+
}
|
|
289
|
+
catch {
|
|
290
|
+
return value;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
function stringField(value) {
|
|
294
|
+
return typeof value === 'string' && value.length > 0 ? value : undefined;
|
|
295
|
+
}
|
|
296
|
+
function timestamp(value) {
|
|
297
|
+
if (typeof value === 'string') {
|
|
298
|
+
const parsed = Date.parse(value);
|
|
299
|
+
if (Number.isFinite(parsed))
|
|
300
|
+
return parsed;
|
|
301
|
+
}
|
|
302
|
+
return undefined;
|
|
303
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
2
|
+
import type { NormalizedSession } from '@kirkchinese/harness-forge-core';
|
|
3
|
+
/** One discoverable OpenCode session in its shared database. */
|
|
4
|
+
export interface OpenCodeSessionCandidate {
|
|
5
|
+
readonly path: string;
|
|
6
|
+
readonly sourceId: string;
|
|
7
|
+
readonly source: 'opencode';
|
|
8
|
+
readonly title?: string;
|
|
9
|
+
readonly cwd?: string;
|
|
10
|
+
readonly createdAt?: number;
|
|
11
|
+
}
|
|
12
|
+
export interface OpenCodeImportReport {
|
|
13
|
+
readonly id: string;
|
|
14
|
+
readonly sourcePath: string;
|
|
15
|
+
readonly title?: string;
|
|
16
|
+
readonly turns: number;
|
|
17
|
+
readonly events: number;
|
|
18
|
+
readonly stats: Record<string, number>;
|
|
19
|
+
}
|
|
20
|
+
/** Default OpenCode database path (the desktop build uses ~/.local/share). */
|
|
21
|
+
export declare function defaultOpenCodeDatabase(home?: string): string;
|
|
22
|
+
/** Discover top-level OpenCode sessions (subagent sessions are excluded). */
|
|
23
|
+
export declare function discoverOpenCodeSessions(path?: string, limit?: number): Promise<OpenCodeSessionCandidate[]>;
|
|
24
|
+
/** Read one OpenCode session into the shared normalized model. */
|
|
25
|
+
export declare function readOpenCodeSession(path: string, sourceId: string, targetId?: string): Promise<{
|
|
26
|
+
session: NormalizedSession;
|
|
27
|
+
title?: string;
|
|
28
|
+
}>;
|
|
29
|
+
/** Import one OpenCode session into DSH session persistence. */
|
|
30
|
+
export declare function importOpenCodeSession(ctx: Context, path: string, sourceId: string, options?: {
|
|
31
|
+
readonly targetId?: string;
|
|
32
|
+
}): Promise<OpenCodeImportReport>;
|