@kirkchinese/harness-forge 0.0.0-stage → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +46 -2
- package/assets/icon.svg +12 -0
- package/cordis.patch.yml +28 -0
- package/docs/DETAIL.zh.md +47 -0
- package/lib/agents/presets.d.ts +98 -0
- package/lib/agents/presets.js +292 -0
- package/lib/agents/profiles.d.ts +58 -0
- package/lib/agents/profiles.js +279 -0
- package/lib/agents/row.d.ts +17 -0
- package/lib/agents/row.js +148 -0
- package/lib/client/paths.d.ts +12 -0
- package/lib/client/paths.js +68 -0
- package/lib/client/styles.d.ts +8 -0
- package/lib/client/styles.js +89 -0
- package/lib/client/typert.d.ts +25 -0
- package/lib/client/typert.js +158 -0
- package/lib/client.js +23823 -0
- package/lib/commands/definition.d.ts +40 -0
- package/lib/commands/definition.js +113 -0
- package/lib/commands/output-styles.d.ts +42 -0
- package/lib/commands/output-styles.js +152 -0
- package/lib/commands/row.d.ts +9 -0
- package/lib/commands/row.js +157 -0
- package/lib/context/instructions.d.ts +32 -0
- package/lib/context/instructions.js +88 -0
- package/lib/context/row.d.ts +16 -0
- package/lib/context/row.js +17 -0
- package/lib/dsh/commands.d.ts +30 -0
- package/lib/dsh/commands.js +61 -0
- package/lib/dsh/host.d.ts +110 -0
- package/lib/dsh/host.js +217 -0
- package/lib/dsh/model-route.d.ts +21 -0
- package/lib/dsh/model-route.js +37 -0
- package/lib/dsh/session-records.d.ts +78 -0
- package/lib/dsh/session-records.js +36 -0
- package/lib/dsh/subagents.d.ts +46 -0
- package/lib/dsh/subagents.js +14 -0
- package/lib/forge/contract.d.ts +271 -0
- package/lib/forge/contract.js +45 -0
- package/lib/forge/follow.d.ts +30 -0
- package/lib/forge/follow.js +73 -0
- package/lib/forge/ledger.d.ts +206 -0
- package/lib/forge/ledger.js +1005 -0
- package/lib/forge/live-mounts.d.ts +45 -0
- package/lib/forge/live-mounts.js +78 -0
- package/lib/forge/npm.d.ts +31 -0
- package/lib/forge/npm.js +60 -0
- package/lib/forge/projects.d.ts +97 -0
- package/lib/forge/projects.js +343 -0
- package/lib/forge/receipts.d.ts +5 -0
- package/lib/forge/receipts.js +31 -0
- package/lib/forge/remote/archive.d.ts +65 -0
- package/lib/forge/remote/archive.js +565 -0
- package/lib/forge/remote/code-parts.d.ts +18 -0
- package/lib/forge/remote/code-parts.js +76 -0
- package/lib/forge/remote/fetch.d.ts +265 -0
- package/lib/forge/remote/fetch.js +1133 -0
- package/lib/forge/remote/service.d.ts +256 -0
- package/lib/forge/remote/service.js +868 -0
- package/lib/forge/row.d.ts +32 -0
- package/lib/forge/row.js +59 -0
- package/lib/forge/service.d.ts +115 -0
- package/lib/forge/service.js +290 -0
- package/lib/forge/settings.d.ts +43 -0
- package/lib/forge/settings.js +122 -0
- package/lib/forge/state.d.ts +129 -0
- package/lib/forge/state.js +266 -0
- package/lib/forge/trust.d.ts +48 -0
- package/lib/forge/trust.js +107 -0
- package/lib/forge/turns.d.ts +13 -0
- package/lib/forge/turns.js +56 -0
- package/lib/hooks/broker/engine.d.ts +112 -0
- package/lib/hooks/broker/engine.js +722 -0
- package/lib/hooks/broker/helpers.d.ts +10 -0
- package/lib/hooks/broker/helpers.js +22 -0
- package/lib/hooks/broker/lifecycle.d.ts +65 -0
- package/lib/hooks/broker/lifecycle.js +342 -0
- package/lib/hooks/broker/session-events.d.ts +27 -0
- package/lib/hooks/broker/session-events.js +205 -0
- package/lib/hooks/broker/tools.d.ts +52 -0
- package/lib/hooks/broker/tools.js +424 -0
- package/lib/hooks/broker/types.d.ts +264 -0
- package/lib/hooks/broker/types.js +1 -0
- package/lib/hooks/broker.d.ts +80 -0
- package/lib/hooks/broker.js +95 -0
- package/lib/hooks/codex-user.d.ts +5 -0
- package/lib/hooks/codex-user.js +109 -0
- package/lib/hooks/condition.d.ts +87 -0
- package/lib/hooks/condition.js +602 -0
- package/lib/hooks/config.d.ts +85 -0
- package/lib/hooks/config.js +154 -0
- package/lib/hooks/glob.d.ts +41 -0
- package/lib/hooks/glob.js +114 -0
- package/lib/hooks/matcher.d.ts +20 -0
- package/lib/hooks/matcher.js +39 -0
- package/lib/hooks/notice.d.ts +17 -0
- package/lib/hooks/notice.js +39 -0
- package/lib/hooks/output.d.ts +56 -0
- package/lib/hooks/output.js +214 -0
- package/lib/hooks/permissions.d.ts +19 -0
- package/lib/hooks/permissions.js +202 -0
- package/lib/hooks/powershell-aliases.d.ts +19 -0
- package/lib/hooks/powershell-aliases.js +58 -0
- package/lib/hooks/process.d.ts +64 -0
- package/lib/hooks/process.js +289 -0
- package/lib/hooks/row.d.ts +17 -0
- package/lib/hooks/row.js +96 -0
- package/lib/hooks/runner.d.ts +98 -0
- package/lib/hooks/runner.js +322 -0
- package/lib/hooks/shell-commands.d.ts +28 -0
- package/lib/hooks/shell-commands.js +241 -0
- package/lib/hooks/shell.d.ts +44 -0
- package/lib/hooks/shell.js +129 -0
- package/lib/hooks/transcript.d.ts +28 -0
- package/lib/hooks/transcript.js +141 -0
- package/lib/hooks/vocabulary.d.ts +57 -0
- package/lib/hooks/vocabulary.js +366 -0
- package/lib/index.d.ts +31 -0
- package/lib/index.js +287 -0
- package/lib/mcp/auth.d.ts +35 -0
- package/lib/mcp/auth.js +178 -0
- package/lib/mcp/catalog.d.ts +39 -0
- package/lib/mcp/catalog.js +69 -0
- package/lib/mcp/mount.d.ts +113 -0
- package/lib/mcp/mount.js +317 -0
- package/lib/mcp/policy.d.ts +42 -0
- package/lib/mcp/policy.js +43 -0
- package/lib/mcp/remote.d.ts +45 -0
- package/lib/mcp/remote.js +133 -0
- package/lib/mcp/row.d.ts +9 -0
- package/lib/mcp/row.js +225 -0
- package/lib/mods/bundler.d.ts +38 -0
- package/lib/mods/bundler.js +157 -0
- package/lib/mods/chain.d.ts +103 -0
- package/lib/mods/chain.js +436 -0
- package/lib/mods/claude.d.ts +27 -0
- package/lib/mods/claude.js +56 -0
- package/lib/mods/engine.d.ts +160 -0
- package/lib/mods/engine.js +701 -0
- package/lib/mods/expect.d.ts +8 -0
- package/lib/mods/expect.js +248 -0
- package/lib/mods/host.d.ts +105 -0
- package/lib/mods/host.js +462 -0
- package/lib/mods/loader.d.ts +53 -0
- package/lib/mods/loader.js +231 -0
- package/lib/mods/ops.d.ts +13 -0
- package/lib/mods/ops.js +117 -0
- package/lib/mods/row.d.ts +9 -0
- package/lib/mods/row.js +77 -0
- package/lib/mods/runtime.d.ts +60 -0
- package/lib/mods/runtime.js +133 -0
- package/lib/mods/service/commands.d.ts +17 -0
- package/lib/mods/service/commands.js +121 -0
- package/lib/mods/service/drawings.d.ts +36 -0
- package/lib/mods/service/drawings.js +64 -0
- package/lib/mods/service/sessions.d.ts +63 -0
- package/lib/mods/service/sessions.js +345 -0
- package/lib/mods/service/tools.d.ts +32 -0
- package/lib/mods/service/tools.js +253 -0
- package/lib/mods/service/turns.d.ts +33 -0
- package/lib/mods/service/turns.js +136 -0
- package/lib/mods/service/types.d.ts +151 -0
- package/lib/mods/service/types.js +1 -0
- package/lib/mods/service.d.ts +72 -0
- package/lib/mods/service.js +104 -0
- package/lib/mods/surfaces.d.ts +93 -0
- package/lib/mods/surfaces.js +309 -0
- package/lib/mods/values.d.ts +30 -0
- package/lib/mods/values.js +95 -0
- package/lib/opencode/bun.d.ts +40 -0
- package/lib/opencode/bun.js +168 -0
- package/lib/opencode/convert.d.ts +53 -0
- package/lib/opencode/convert.js +255 -0
- package/lib/opencode/row.d.ts +22 -0
- package/lib/opencode/row.js +104 -0
- package/lib/opencode/runtime/core.d.ts +74 -0
- package/lib/opencode/runtime/core.js +249 -0
- package/lib/opencode/runtime/events.d.ts +80 -0
- package/lib/opencode/runtime/events.js +281 -0
- package/lib/opencode/runtime/mount.d.ts +27 -0
- package/lib/opencode/runtime/mount.js +283 -0
- package/lib/opencode/runtime/server.d.ts +27 -0
- package/lib/opencode/runtime/server.js +354 -0
- package/lib/opencode/runtime/types.d.ts +109 -0
- package/lib/opencode/runtime/types.js +1 -0
- package/lib/opencode/runtime.d.ts +55 -0
- package/lib/opencode/runtime.js +79 -0
- package/lib/porting/adapter-paths.d.ts +13 -0
- package/lib/porting/adapter-paths.js +120 -0
- package/lib/porting/port-gap.d.ts +36 -0
- package/lib/porting/port-gap.js +158 -0
- package/lib/porting/port-sessions.d.ts +41 -0
- package/lib/porting/port-sessions.js +98 -0
- package/lib/porting/prompt.d.ts +23 -0
- package/lib/porting/prompt.js +51 -0
- package/lib/porting/row.d.ts +16 -0
- package/lib/porting/row.js +27 -0
- package/lib/porting/verify-port.d.ts +46 -0
- package/lib/porting/verify-port.js +82 -0
- package/lib/presets/fs-search.d.ts +25 -0
- package/lib/presets/fs-search.js +17 -0
- package/lib/presets/tool-filter.d.ts +35 -0
- package/lib/presets/tool-filter.js +91 -0
- package/lib/sessions/chat-import.d.ts +48 -0
- package/lib/sessions/chat-import.js +44 -0
- package/lib/sessions/claude.d.ts +35 -0
- package/lib/sessions/claude.js +104 -0
- package/lib/sessions/codex-discover.d.ts +34 -0
- package/lib/sessions/codex-discover.js +205 -0
- package/lib/sessions/codex-meta.d.ts +63 -0
- package/lib/sessions/codex-meta.js +148 -0
- package/lib/sessions/codex-text.d.ts +9 -0
- package/lib/sessions/codex-text.js +44 -0
- package/lib/sessions/codex.d.ts +42 -0
- package/lib/sessions/codex.js +270 -0
- package/lib/sessions/opencode.d.ts +38 -0
- package/lib/sessions/opencode.js +339 -0
- package/lib/sessions/persist.d.ts +162 -0
- package/lib/sessions/persist.js +377 -0
- package/lib/sessions/row.d.ts +15 -0
- package/lib/sessions/row.js +101 -0
- package/lib/sessions/turns.d.ts +31 -0
- package/lib/sessions/turns.js +63 -0
- package/lib/sessions/zstd.d.ts +15 -0
- package/lib/sessions/zstd.js +152 -0
- package/lib/shared/invocation.d.ts +169 -0
- package/lib/shared/invocation.js +325 -0
- package/lib/shared/render.d.ts +101 -0
- package/lib/shared/render.js +1353 -0
- package/lib/shared/util.d.ts +21 -0
- package/lib/shared/util.js +32 -0
- package/lib/skills/bundled.d.ts +3 -0
- package/lib/skills/bundled.js +42 -0
- package/lib/skills/provider.d.ts +74 -0
- package/lib/skills/provider.js +346 -0
- package/lib/skills/row.d.ts +22 -0
- package/lib/skills/row.js +204 -0
- package/lib/workbench/adapter-coverage.d.ts +65 -0
- package/lib/workbench/adapter-coverage.js +141 -0
- package/lib/workbench/model-tools.d.ts +10 -0
- package/lib/workbench/model-tools.js +122 -0
- package/lib/workbench/overview.d.ts +153 -0
- package/lib/workbench/overview.js +246 -0
- package/lib/workbench/remote.d.ts +161 -0
- package/lib/workbench/remote.js +561 -0
- package/lib/workbench/row.d.ts +15 -0
- package/lib/workbench/row.js +16 -0
- package/lib/workbench/settings.d.ts +19 -0
- package/lib/workbench/settings.js +60 -0
- package/lib/workbench/workbench.d.ts +130 -0
- package/lib/workbench/workbench.js +361 -0
- package/locale/agents/en.json +6 -0
- package/locale/agents/zh.json +6 -0
- package/locale/commands/en.json +6 -0
- package/locale/commands/zh.json +6 -0
- package/locale/context/en.json +6 -0
- package/locale/context/zh.json +6 -0
- package/locale/en.json +6 -0
- package/locale/hooks/en.json +6 -0
- package/locale/hooks/zh.json +6 -0
- package/locale/mcp/en.json +6 -0
- package/locale/mcp/zh.json +6 -0
- package/locale/mods/en.json +6 -0
- package/locale/mods/zh.json +6 -0
- package/locale/opencode/en.json +6 -0
- package/locale/opencode/zh.json +6 -0
- package/locale/porter/en.json +6 -0
- package/locale/porter/zh.json +6 -0
- package/locale/sessions/en.json +6 -0
- package/locale/sessions/zh.json +6 -0
- package/locale/skills/en.json +6 -0
- package/locale/skills/zh.json +6 -0
- package/locale/workbench/en.json +6 -0
- package/locale/workbench/zh.json +6 -0
- package/locale/zh.json +7 -0
- package/package.json +247 -3
- package/runtime/opencode-host.mjs +270 -0
- package/skills/harness-forge/SKILL.md +47 -0
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
2
|
+
import { type NormalizedSession, type SynthesizedSession } from '@kirkchinese/harness-forge-core';
|
|
3
|
+
import { type TurnReader } from './turns.js';
|
|
4
|
+
interface PersistenceHandle {
|
|
5
|
+
append(events: readonly unknown[]): Promise<void>;
|
|
6
|
+
flush(): Promise<void>;
|
|
7
|
+
close(): Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
interface ReadHandle {
|
|
10
|
+
/** `length` events from `offset`: fewer at the end of the log. */
|
|
11
|
+
read(offset?: number, length?: number): Promise<{
|
|
12
|
+
readonly events: readonly unknown[];
|
|
13
|
+
}>;
|
|
14
|
+
close(): Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
interface SessionPersistence {
|
|
17
|
+
create(header: Record<string, unknown>): Promise<PersistenceHandle>;
|
|
18
|
+
list?(): Promise<readonly unknown[]>;
|
|
19
|
+
open?(id: string, access: 'read'): Promise<ReadHandle>;
|
|
20
|
+
}
|
|
21
|
+
/** Where an imported session ended up. */
|
|
22
|
+
export interface PersistedSession {
|
|
23
|
+
readonly id: string;
|
|
24
|
+
/** The session was already in DSH (imported before): it was opened, not written again. */
|
|
25
|
+
readonly reused: boolean;
|
|
26
|
+
/** The session is live in this DSH, so the browser can open it now. */
|
|
27
|
+
readonly live: boolean;
|
|
28
|
+
/** The workspace the session is grouped under, when its directory is one. */
|
|
29
|
+
readonly workspace?: string;
|
|
30
|
+
}
|
|
31
|
+
/** What DSH stores of a session that says which source session it holds. */
|
|
32
|
+
export interface StoredSession {
|
|
33
|
+
readonly id: string;
|
|
34
|
+
/** The header's creation time; importers write the source session's own. */
|
|
35
|
+
readonly createdAt?: number;
|
|
36
|
+
/** The directory the session was in: its workspace. */
|
|
37
|
+
readonly cwd?: string;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* What tells which source session a stored one holds, when an id may have been given to several: Harness Forge
|
|
41
|
+
* 0.4.0 gave a Codex thread and its forks one id, the bare `session_id` they share.
|
|
42
|
+
*/
|
|
43
|
+
export interface Provenance {
|
|
44
|
+
/** When the source session was created; every version stored it as the DSH session's `createdAt`. */
|
|
45
|
+
readonly createdAt: number;
|
|
46
|
+
/** Written under when `id` holds another session. */
|
|
47
|
+
readonly alternateId: string;
|
|
48
|
+
/**
|
|
49
|
+
* The other source sessions created at the same time that an earlier version may have stored under `legacyId`
|
|
50
|
+
* (forks can be created in one millisecond): a reader of each one's turns, opened when called.
|
|
51
|
+
*/
|
|
52
|
+
rivals(legacyId: string): Promise<ReadonlyArray<() => TurnReader>>;
|
|
53
|
+
/** A reader of this source session's turns. */
|
|
54
|
+
turns(): TurnReader;
|
|
55
|
+
}
|
|
56
|
+
/** How an import finds the DSH session that already holds its source session, and the id it writes under. */
|
|
57
|
+
export interface ImportIdentity {
|
|
58
|
+
/** The id this version gives the session. */
|
|
59
|
+
readonly id: string;
|
|
60
|
+
/** Ids earlier versions gave it; one stored is reused. */
|
|
61
|
+
readonly legacyIds: readonly string[];
|
|
62
|
+
/**
|
|
63
|
+
* Set when an earlier version's id may have been given to another source session too: see {@link matchStored}.
|
|
64
|
+
* Without it, a session stored under any of the ids holds this one.
|
|
65
|
+
*/
|
|
66
|
+
readonly provenance?: Provenance;
|
|
67
|
+
}
|
|
68
|
+
/** Whether DSH already holds a source session. */
|
|
69
|
+
export type StoredMatch =
|
|
70
|
+
/** It does, as `id`. */
|
|
71
|
+
{
|
|
72
|
+
readonly kind: 'imported';
|
|
73
|
+
readonly id: string;
|
|
74
|
+
}
|
|
75
|
+
/** `id` may hold it, and nothing says whether it does; importing it as new writes `newId`. */
|
|
76
|
+
| {
|
|
77
|
+
readonly kind: 'maybe';
|
|
78
|
+
readonly id: string;
|
|
79
|
+
readonly newId: string;
|
|
80
|
+
}
|
|
81
|
+
/** It does not; an import writes `id`. */
|
|
82
|
+
| {
|
|
83
|
+
readonly kind: 'new';
|
|
84
|
+
readonly id: string;
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* The same for listing and import: find the stored session that holds the source session, by id and provenance.
|
|
88
|
+
*
|
|
89
|
+
* With provenance, a session under this version's own ids holds the source unless its creation time says otherwise.
|
|
90
|
+
* One under an earlier version's id holds it when it was created when the source was, and no other source session
|
|
91
|
+
* created then may be stored under that id; else only when, of all those source sessions, this is the one whose
|
|
92
|
+
* every turn (prompt and answer) is the stored session's ({@link uniqueFullMatch}). A session whose time is unknown,
|
|
93
|
+
* or that more than one or none of them match in full, is left to the person.
|
|
94
|
+
*/
|
|
95
|
+
export declare function matchStored(identity: ImportIdentity, stored: StoredSessions): Promise<StoredMatch>;
|
|
96
|
+
/** A session stored under an id that may or may not hold the one being imported; the person decides. */
|
|
97
|
+
export declare class SessionMaybeImportedError extends Error {
|
|
98
|
+
readonly existingId: string;
|
|
99
|
+
readonly code = "session-maybe-imported";
|
|
100
|
+
constructor(existingId: string);
|
|
101
|
+
}
|
|
102
|
+
/** The identity of an import into a named session: that one, reused when it is stored. */
|
|
103
|
+
export declare function targetIdentity(targetId: string): ImportIdentity;
|
|
104
|
+
/**
|
|
105
|
+
* Persist a normalized session, or reuse the one an earlier import wrote, and make it live. The session is written
|
|
106
|
+
* under the id `identity` resolves to, whatever id it carries.
|
|
107
|
+
*/
|
|
108
|
+
export declare function persistImported(ctx: Context, session: NormalizedSession, identity: ImportIdentity): Promise<{
|
|
109
|
+
readonly persisted: PersistedSession;
|
|
110
|
+
readonly synthesized: SynthesizedSession;
|
|
111
|
+
}>;
|
|
112
|
+
/** What opening a stored session did. */
|
|
113
|
+
export interface OpenedSession {
|
|
114
|
+
readonly id: string;
|
|
115
|
+
/** The session is live in this DSH, so the browser can open it now. */
|
|
116
|
+
readonly live: boolean;
|
|
117
|
+
/** The person had archived it in DSH, and opening took it out of the archive. */
|
|
118
|
+
readonly unarchived: boolean;
|
|
119
|
+
/** It is archived in DSH and stays so (DSH offers no way to unarchive): it is under the archive, not in the list. */
|
|
120
|
+
readonly archived: boolean;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Make a session an import stored known to DSH again, so opening it shows it: placed in the workspace of its
|
|
124
|
+
* directory and live, as a new import does, and out of the archive when the person archived it (an archived
|
|
125
|
+
* session is hidden from every grouping, and opening it lands on an empty new-session page).
|
|
126
|
+
*/
|
|
127
|
+
export declare function openStored(ctx: Context, id: string): Promise<OpenedSession>;
|
|
128
|
+
/** What one import did, as every importer reports it. */
|
|
129
|
+
export interface ImportReport extends PersistedSession {
|
|
130
|
+
readonly sourcePath: string;
|
|
131
|
+
readonly title?: string;
|
|
132
|
+
readonly turns: number;
|
|
133
|
+
readonly events: number;
|
|
134
|
+
readonly stats: Record<string, number>;
|
|
135
|
+
}
|
|
136
|
+
/** The report of an import that {@link persistImported} carried out. */
|
|
137
|
+
export declare function importReport(sourcePath: string, title: string | undefined, result: Awaited<ReturnType<typeof persistImported>>): ImportReport;
|
|
138
|
+
/** The sessions DSH stores, by id; none when it cannot list them. */
|
|
139
|
+
export declare function storedSessions(ctx: Context): Promise<StoredSessions>;
|
|
140
|
+
/** The sessions DSH stores: their headers, listed once, and what was said in them, read when asked. */
|
|
141
|
+
export declare class StoredSessions {
|
|
142
|
+
private readonly persistence;
|
|
143
|
+
private readonly headers;
|
|
144
|
+
private readonly readers;
|
|
145
|
+
private constructor();
|
|
146
|
+
static of(persistence: SessionPersistence | undefined): Promise<StoredSessions>;
|
|
147
|
+
get(id: string): StoredSession | undefined;
|
|
148
|
+
has(id: string): boolean;
|
|
149
|
+
/** A reader of a stored session's turns, shared by every comparison until {@link close}; its log is read in pages. */
|
|
150
|
+
turns(id: string): TurnReader;
|
|
151
|
+
/** Let go of the stored sessions read. */
|
|
152
|
+
close(): Promise<void>;
|
|
153
|
+
/** The turns of a stored session's log (`turn/start`, its first user message, the assistant's messages), as they are read. */
|
|
154
|
+
private storedTurns;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* The home folder and the system's temporary folders (`os.tmpdir()`, `/tmp`, `/var/tmp`, macOS's per-user
|
|
158
|
+
* `/var/folders/…/T`), which hold everyone's odds and ends and are no project: a session there gets no workspace.
|
|
159
|
+
* A folder inside one is a project of its own.
|
|
160
|
+
*/
|
|
161
|
+
export declare function isSharedFolder(path: string): boolean;
|
|
162
|
+
export {};
|
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The hand-off of a synthesized session to DSH, shared by every importer: write it through
|
|
3
|
+
* `sessionPersistence`, place it in the workspace of its directory, and make it live, so the sidebar lists it
|
|
4
|
+
* with its title under its project and "Open" works at once. Writing the log alone leaves it unknown to the
|
|
5
|
+
* running DSH (`sessions.retain: unknown session`, "Untitled" under "Ungrouped") until a restart.
|
|
6
|
+
* @module @kirkchinese/harness-forge
|
|
7
|
+
*/
|
|
8
|
+
import { realpathSync, statSync } from 'node:fs';
|
|
9
|
+
import { homedir, tmpdir } from 'node:os';
|
|
10
|
+
import { resolve } from 'node:path';
|
|
11
|
+
import { synthesizeDshSession } from '@kirkchinese/harness-forge-core';
|
|
12
|
+
import { dshService } from '../dsh/host.js';
|
|
13
|
+
import { compareTurns, replyText, turnReader, turnText } from './turns.js';
|
|
14
|
+
/**
|
|
15
|
+
* The same for listing and import: find the stored session that holds the source session, by id and provenance.
|
|
16
|
+
*
|
|
17
|
+
* With provenance, a session under this version's own ids holds the source unless its creation time says otherwise.
|
|
18
|
+
* One under an earlier version's id holds it when it was created when the source was, and no other source session
|
|
19
|
+
* created then may be stored under that id; else only when, of all those source sessions, this is the one whose
|
|
20
|
+
* every turn (prompt and answer) is the stored session's ({@link uniqueFullMatch}). A session whose time is unknown,
|
|
21
|
+
* or that more than one or none of them match in full, is left to the person.
|
|
22
|
+
*/
|
|
23
|
+
export async function matchStored(identity, stored) {
|
|
24
|
+
const provenance = identity.provenance;
|
|
25
|
+
const own = provenance === undefined ? [identity.id] : [identity.id, provenance.alternateId];
|
|
26
|
+
let maybe;
|
|
27
|
+
for (const id of new Set([...own, ...identity.legacyIds])) {
|
|
28
|
+
const header = stored.get(id);
|
|
29
|
+
if (header === undefined)
|
|
30
|
+
continue;
|
|
31
|
+
if (provenance === undefined)
|
|
32
|
+
return { kind: 'imported', id };
|
|
33
|
+
const same = sameOrigin(header.createdAt, provenance.createdAt);
|
|
34
|
+
// Without times to compare, this version's own ids are taken at their word: it gives them to this session only.
|
|
35
|
+
if (own.includes(id) && same !== false)
|
|
36
|
+
return { kind: 'imported', id };
|
|
37
|
+
if (same === undefined)
|
|
38
|
+
maybe ??= id;
|
|
39
|
+
if (same !== true)
|
|
40
|
+
continue;
|
|
41
|
+
const rivals = await provenance.rivals(id);
|
|
42
|
+
if (rivals.length === 0)
|
|
43
|
+
return { kind: 'imported', id };
|
|
44
|
+
const verdict = await uniqueFullMatch(stored.turns(id), provenance, rivals);
|
|
45
|
+
if (verdict === 'this')
|
|
46
|
+
return { kind: 'imported', id };
|
|
47
|
+
if (verdict === 'unknown')
|
|
48
|
+
maybe ??= id;
|
|
49
|
+
}
|
|
50
|
+
const free = own.find(id => !stored.has(id));
|
|
51
|
+
if (free === undefined)
|
|
52
|
+
throw new Error(`cannot import as ${own.join(' or ')}: DSH holds other sessions under those ids`);
|
|
53
|
+
return maybe !== undefined ? { kind: 'maybe', id: maybe, newId: free } : { kind: 'new', id: free };
|
|
54
|
+
}
|
|
55
|
+
/** Whether two creation times are one session's; `undefined` when one is not known (a source without timestamps). */
|
|
56
|
+
function sameOrigin(stored, source) {
|
|
57
|
+
if (stored === undefined || !Number.isFinite(stored) || stored <= 0 || !Number.isFinite(source) || source <= 0)
|
|
58
|
+
return undefined;
|
|
59
|
+
return stored === source;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Which of the source sessions that may be stored under one id it holds: `this` one, when its every turn is the
|
|
63
|
+
* stored session's and no rival's is; `other` when its turns part ways with the stored session's, or a rival alone
|
|
64
|
+
* matches in full; else `unknown` (several match, or none does in full: one only begins like the other).
|
|
65
|
+
*/
|
|
66
|
+
async function uniqueFullMatch(stored, provenance, rivals) {
|
|
67
|
+
const compare = async (open) => {
|
|
68
|
+
const source = open();
|
|
69
|
+
try {
|
|
70
|
+
return await compareTurns(stored, source);
|
|
71
|
+
}
|
|
72
|
+
finally {
|
|
73
|
+
await source.close();
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
const mine = await compare(() => provenance.turns());
|
|
77
|
+
if (mine === 'different')
|
|
78
|
+
return 'other';
|
|
79
|
+
let others = 0;
|
|
80
|
+
for (const rival of rivals) {
|
|
81
|
+
if (await compare(rival) === 'same')
|
|
82
|
+
others += 1;
|
|
83
|
+
// A second full match settles it: no one of them is told apart.
|
|
84
|
+
if (others + (mine === 'same' ? 1 : 0) > 1)
|
|
85
|
+
return 'unknown';
|
|
86
|
+
}
|
|
87
|
+
if (mine === 'same')
|
|
88
|
+
return others === 0 ? 'this' : 'unknown';
|
|
89
|
+
return others === 1 ? 'other' : 'unknown';
|
|
90
|
+
}
|
|
91
|
+
/** A session stored under an id that may or may not hold the one being imported; the person decides. */
|
|
92
|
+
export class SessionMaybeImportedError extends Error {
|
|
93
|
+
existingId;
|
|
94
|
+
code = 'session-maybe-imported';
|
|
95
|
+
constructor(existingId) {
|
|
96
|
+
super(`DSH session ${existingId} may already hold this session; open it, or import this one as a new session.`);
|
|
97
|
+
this.existingId = existingId;
|
|
98
|
+
this.name = 'SessionMaybeImportedError';
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/** The identity of an import into a named session: that one, reused when it is stored. */
|
|
102
|
+
export function targetIdentity(targetId) {
|
|
103
|
+
return { id: targetId, legacyIds: [] };
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Persist a normalized session, or reuse the one an earlier import wrote, and make it live. The session is written
|
|
107
|
+
* under the id `identity` resolves to, whatever id it carries.
|
|
108
|
+
*/
|
|
109
|
+
export async function persistImported(ctx, session, identity) {
|
|
110
|
+
const persistence = dshService(ctx, 'sessionPersistence');
|
|
111
|
+
if (persistence === undefined)
|
|
112
|
+
throw new Error('sessionPersistence service is not available');
|
|
113
|
+
const stored = await StoredSessions.of(persistence);
|
|
114
|
+
let match;
|
|
115
|
+
try {
|
|
116
|
+
match = await matchStored(identity, stored);
|
|
117
|
+
}
|
|
118
|
+
finally {
|
|
119
|
+
await stored.close();
|
|
120
|
+
}
|
|
121
|
+
if (match.kind === 'maybe')
|
|
122
|
+
throw new SessionMaybeImportedError(match.id);
|
|
123
|
+
const id = match.id;
|
|
124
|
+
const workspace = await workspaceFor(ctx, session.cwd);
|
|
125
|
+
const synthesized = synthesizeDshSession({ ...session, id, source: { ...session.source, sessionId: id } });
|
|
126
|
+
if (match.kind === 'new') {
|
|
127
|
+
const cwd = workspace?.path ?? synthesized.meta.cwd;
|
|
128
|
+
const handle = await persistence.create({
|
|
129
|
+
version: 4,
|
|
130
|
+
id,
|
|
131
|
+
createdAt: synthesized.meta.createdAt,
|
|
132
|
+
isSeeded: false,
|
|
133
|
+
...(cwd !== undefined ? { cwd } : {}),
|
|
134
|
+
delegationDepth: synthesized.meta.delegationDepth,
|
|
135
|
+
...(synthesized.meta.origin !== undefined ? { origin: synthesized.meta.origin } : {}),
|
|
136
|
+
...(synthesized.meta.parentSession !== undefined ? { parentSession: synthesized.meta.parentSession } : {}),
|
|
137
|
+
});
|
|
138
|
+
try {
|
|
139
|
+
await handle.append(synthesized.events);
|
|
140
|
+
await handle.flush();
|
|
141
|
+
}
|
|
142
|
+
finally {
|
|
143
|
+
await handle.close();
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
const grouped = workspace !== undefined && await attach(workspace, id);
|
|
147
|
+
const persisted = {
|
|
148
|
+
id,
|
|
149
|
+
reused: match.kind === 'imported',
|
|
150
|
+
live: await makeLive(ctx, id),
|
|
151
|
+
...(grouped ? { workspace: workspace.path } : {}),
|
|
152
|
+
};
|
|
153
|
+
return { persisted, synthesized };
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Make a session an import stored known to DSH again, so opening it shows it: placed in the workspace of its
|
|
157
|
+
* directory and live, as a new import does, and out of the archive when the person archived it (an archived
|
|
158
|
+
* session is hidden from every grouping, and opening it lands on an empty new-session page).
|
|
159
|
+
*/
|
|
160
|
+
export async function openStored(ctx, id) {
|
|
161
|
+
const stored = await storedSessions(ctx);
|
|
162
|
+
const header = stored.get(id);
|
|
163
|
+
await stored.close();
|
|
164
|
+
if (header === undefined)
|
|
165
|
+
throw new Error(`DSH does not hold a session ${id}`);
|
|
166
|
+
const workspace = await workspaceFor(ctx, header.cwd);
|
|
167
|
+
if (workspace !== undefined)
|
|
168
|
+
await attach(workspace, id);
|
|
169
|
+
const registry = dshService(ctx, 'workspaceRegistry');
|
|
170
|
+
let unarchived = false;
|
|
171
|
+
if (registry?.archivedSessionIds?.includes(id) === true && typeof registry.unarchiveSession === 'function') {
|
|
172
|
+
await registry.unarchiveSession(id);
|
|
173
|
+
unarchived = true;
|
|
174
|
+
}
|
|
175
|
+
return { id, live: await makeLive(ctx, id), unarchived, archived: registry?.archivedSessionIds?.includes(id) === true };
|
|
176
|
+
}
|
|
177
|
+
/** The report of an import that {@link persistImported} carried out. */
|
|
178
|
+
export function importReport(sourcePath, title, result) {
|
|
179
|
+
const { persisted, synthesized } = result;
|
|
180
|
+
return {
|
|
181
|
+
...persisted,
|
|
182
|
+
sourcePath,
|
|
183
|
+
...(title !== undefined ? { title } : {}),
|
|
184
|
+
turns: synthesized.stats.turns,
|
|
185
|
+
events: synthesized.events.length,
|
|
186
|
+
stats: {
|
|
187
|
+
steps: synthesized.stats.steps,
|
|
188
|
+
messages: synthesized.stats.messages,
|
|
189
|
+
toolCalls: synthesized.stats.toolCalls,
|
|
190
|
+
toolResults: synthesized.stats.toolResults,
|
|
191
|
+
synthesizedToolResults: synthesized.stats.synthesizedToolResults,
|
|
192
|
+
},
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
/** How many events of a stored session's log are read at once: a page holds a few turns, and reading stops at the turn that tells. */
|
|
196
|
+
const STORED_EVENTS_PAGE = 256;
|
|
197
|
+
/** The sessions DSH stores, by id; none when it cannot list them. */
|
|
198
|
+
export async function storedSessions(ctx) {
|
|
199
|
+
return StoredSessions.of(dshService(ctx, 'sessionPersistence'));
|
|
200
|
+
}
|
|
201
|
+
/** The sessions DSH stores: their headers, listed once, and what was said in them, read when asked. */
|
|
202
|
+
export class StoredSessions {
|
|
203
|
+
persistence;
|
|
204
|
+
headers;
|
|
205
|
+
readers = new Map();
|
|
206
|
+
constructor(persistence, headers) {
|
|
207
|
+
this.persistence = persistence;
|
|
208
|
+
this.headers = headers;
|
|
209
|
+
}
|
|
210
|
+
static async of(persistence) {
|
|
211
|
+
const headers = new Map();
|
|
212
|
+
if (typeof persistence?.list === 'function') {
|
|
213
|
+
try {
|
|
214
|
+
for (const entry of await persistence.list()) {
|
|
215
|
+
// Current hosts list `{ header, revision, sizeBytes }`; older ones listed the headers.
|
|
216
|
+
const record = entry;
|
|
217
|
+
const header = record?.header ?? record;
|
|
218
|
+
const id = header?.id;
|
|
219
|
+
const createdAt = header?.createdAt;
|
|
220
|
+
const cwd = header?.cwd;
|
|
221
|
+
if (typeof id === 'string')
|
|
222
|
+
headers.set(id, { id, ...(typeof createdAt === 'number' ? { createdAt } : {}), ...(typeof cwd === 'string' ? { cwd } : {}) });
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
catch {
|
|
226
|
+
// An unreadable listing only means the import cannot tell it ran before.
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return new StoredSessions(persistence, headers);
|
|
230
|
+
}
|
|
231
|
+
get(id) {
|
|
232
|
+
return this.headers.get(id);
|
|
233
|
+
}
|
|
234
|
+
has(id) {
|
|
235
|
+
return this.headers.has(id);
|
|
236
|
+
}
|
|
237
|
+
/** A reader of a stored session's turns, shared by every comparison until {@link close}; its log is read in pages. */
|
|
238
|
+
turns(id) {
|
|
239
|
+
let reader = this.readers.get(id);
|
|
240
|
+
if (reader === undefined) {
|
|
241
|
+
reader = turnReader(this.storedTurns(id));
|
|
242
|
+
this.readers.set(id, reader);
|
|
243
|
+
}
|
|
244
|
+
return reader;
|
|
245
|
+
}
|
|
246
|
+
/** Let go of the stored sessions read. */
|
|
247
|
+
async close() {
|
|
248
|
+
const readers = [...this.readers.values()];
|
|
249
|
+
this.readers.clear();
|
|
250
|
+
await Promise.all(readers.map(reader => reader.close()));
|
|
251
|
+
}
|
|
252
|
+
/** The turns of a stored session's log (`turn/start`, its first user message, the assistant's messages), as they are read. */
|
|
253
|
+
async *storedTurns(id) {
|
|
254
|
+
if (!this.headers.has(id) || typeof this.persistence?.open !== 'function')
|
|
255
|
+
throw new Error(`cannot read DSH session ${id}`);
|
|
256
|
+
const handle = await this.persistence.open(id, 'read');
|
|
257
|
+
try {
|
|
258
|
+
let turn;
|
|
259
|
+
for (let offset = 0;;) {
|
|
260
|
+
const { events } = await handle.read(offset, STORED_EVENTS_PAGE);
|
|
261
|
+
offset += events.length;
|
|
262
|
+
for (const entry of events) {
|
|
263
|
+
const event = entry;
|
|
264
|
+
if (event?.type === 'turn/start') {
|
|
265
|
+
if (turn !== undefined)
|
|
266
|
+
yield turnText(turn.prompt ?? '', turn.replies);
|
|
267
|
+
turn = { prompt: undefined, replies: [] };
|
|
268
|
+
}
|
|
269
|
+
else if (turn !== undefined && turn.prompt === undefined && event?.type === 'user/message' && event.data?.role === 'user') {
|
|
270
|
+
turn.prompt = replyText(Array.isArray(event.data.content) ? event.data.content : []).join('\n');
|
|
271
|
+
}
|
|
272
|
+
else if (turn !== undefined && event?.type === 'assistant/message') {
|
|
273
|
+
const content = event.data?.message?.content;
|
|
274
|
+
turn.replies.push(...replyText(Array.isArray(content) ? content : []));
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
if (events.length < STORED_EVENTS_PAGE)
|
|
278
|
+
break;
|
|
279
|
+
}
|
|
280
|
+
if (turn !== undefined)
|
|
281
|
+
yield turnText(turn.prompt ?? '', turn.replies);
|
|
282
|
+
}
|
|
283
|
+
finally {
|
|
284
|
+
await handle.close();
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
/** The workspace of a session's directory: DSH groups a session only under the workspace whose path is its cwd. */
|
|
289
|
+
async function workspaceFor(ctx, cwd) {
|
|
290
|
+
const registry = dshService(ctx, 'workspaceRegistry');
|
|
291
|
+
if (registry === undefined || cwd === undefined || !isDirectory(cwd) || isSharedFolder(cwd))
|
|
292
|
+
return undefined;
|
|
293
|
+
try {
|
|
294
|
+
return await registry.create(cwd);
|
|
295
|
+
}
|
|
296
|
+
catch {
|
|
297
|
+
return undefined;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* The home folder and the system's temporary folders (`os.tmpdir()`, `/tmp`, `/var/tmp`, macOS's per-user
|
|
302
|
+
* `/var/folders/…/T`), which hold everyone's odds and ends and are no project: a session there gets no workspace.
|
|
303
|
+
* A folder inside one is a project of its own.
|
|
304
|
+
*/
|
|
305
|
+
export function isSharedFolder(path) {
|
|
306
|
+
const target = resolve(path);
|
|
307
|
+
const shared = [homedir(), tmpdir()].map(folder => resolve(folder));
|
|
308
|
+
for (const folder of [...shared]) {
|
|
309
|
+
try {
|
|
310
|
+
shared.push(realpathSync(folder));
|
|
311
|
+
}
|
|
312
|
+
catch {
|
|
313
|
+
// A temp folder that cannot be resolved is compared as named.
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
if (shared.includes(target))
|
|
317
|
+
return true;
|
|
318
|
+
if (process.platform === 'win32')
|
|
319
|
+
return false;
|
|
320
|
+
return /^(?:\/private)?\/(?:var\/)?tmp$/u.test(target) || /^(?:\/private)?\/var\/folders\/[^/]+\/[^/]+\/T$/u.test(target);
|
|
321
|
+
}
|
|
322
|
+
async function attach(workspace, id) {
|
|
323
|
+
if (workspace.sessionIds?.includes(id) === true)
|
|
324
|
+
return true;
|
|
325
|
+
try {
|
|
326
|
+
await workspace.attachSession(id);
|
|
327
|
+
return true;
|
|
328
|
+
}
|
|
329
|
+
catch {
|
|
330
|
+
return false;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Resume the session the way opening it in the browser does (`sessionController.resolveAgent`), which also
|
|
335
|
+
* announces it to the session list; without the controller (a headless profile), through `agents.resume`.
|
|
336
|
+
*/
|
|
337
|
+
async function makeLive(ctx, id) {
|
|
338
|
+
const agents = dshService(ctx, 'agents');
|
|
339
|
+
const live = () => agents?.get?.(id) !== undefined;
|
|
340
|
+
if (live())
|
|
341
|
+
return true;
|
|
342
|
+
const controller = dshService(ctx, 'sessionController');
|
|
343
|
+
try {
|
|
344
|
+
if (typeof controller?.resolveAgent === 'function') {
|
|
345
|
+
const result = await controller.resolveAgent(id);
|
|
346
|
+
if (result.error === undefined)
|
|
347
|
+
return true;
|
|
348
|
+
}
|
|
349
|
+
else if (typeof agents?.resume === 'function') {
|
|
350
|
+
const defaults = dshService(ctx, 'agentDefaultModel');
|
|
351
|
+
await agents.resume({
|
|
352
|
+
resumeSessionId: id,
|
|
353
|
+
...(defaults !== undefined
|
|
354
|
+
? {
|
|
355
|
+
agentOptions: {
|
|
356
|
+
get provider() { return defaults.currentSelection().provider; },
|
|
357
|
+
get model() { return defaults.currentSelection().model; },
|
|
358
|
+
get reasoningEffort() { return defaults.currentSelection().reasoningEffort; },
|
|
359
|
+
},
|
|
360
|
+
}
|
|
361
|
+
: {}),
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
catch {
|
|
366
|
+
// The session is stored either way; it becomes live when it is opened after a restart.
|
|
367
|
+
}
|
|
368
|
+
return live();
|
|
369
|
+
}
|
|
370
|
+
function isDirectory(path) {
|
|
371
|
+
try {
|
|
372
|
+
return statSync(path).isDirectory();
|
|
373
|
+
}
|
|
374
|
+
catch {
|
|
375
|
+
return false;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The sessions row: Claude Code, Codex and OpenCode sessions imported into
|
|
3
|
+
* DSH on demand, from the workbench. It holds no listener. While
|
|
4
|
+
* dsh-chat-import runs, it yields: session import is that plugin's.
|
|
5
|
+
* @module @kirkchinese/harness-forge/sessions
|
|
6
|
+
*/
|
|
7
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
8
|
+
import type { Disposer } from '../forge/contract.js';
|
|
9
|
+
import { type HarnessForge } from '../forge/service.js';
|
|
10
|
+
export declare const name = "harness-forge-sessions";
|
|
11
|
+
export declare const inject: string[];
|
|
12
|
+
/** The sessions row. */
|
|
13
|
+
export declare function apply(ctx: Context): void;
|
|
14
|
+
/** Start the sessions row on the shared service. Returns its teardown. */
|
|
15
|
+
export declare function startSessions(ctx: Context, forge: HarnessForge): Disposer;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { MAX_SESSION_BYTES } from '@kirkchinese/harness-forge-core';
|
|
2
|
+
import { defaultCodexSessionsRoot, discoverCodexSessions, importCodexSession, rolloutIdentity } from './codex.js';
|
|
3
|
+
import { codexThreads } from './codex-discover.js';
|
|
4
|
+
import { FORGE_SERVICE } from '../forge/service.js';
|
|
5
|
+
import { discoverOpenCodeSessions, importOpenCodeSession, openCodeIdentity } from './opencode.js';
|
|
6
|
+
import { matchStored, openStored, storedSessions } from './persist.js';
|
|
7
|
+
import { claudeIdentity, discoverClaudeSessions, importClaudeSession } from './claude.js';
|
|
8
|
+
import { rowEffect } from '../forge/row.js';
|
|
9
|
+
import { chatImportRunning, YIELD_MESSAGE } from './chat-import.js';
|
|
10
|
+
export const name = 'harness-forge-sessions';
|
|
11
|
+
export const inject = [FORGE_SERVICE];
|
|
12
|
+
/** The sessions row. */
|
|
13
|
+
export function apply(ctx) {
|
|
14
|
+
rowEffect(ctx, 'sessions', forge => startSessions(ctx, forge));
|
|
15
|
+
}
|
|
16
|
+
/** Start the sessions row on the shared service. Returns its teardown. */
|
|
17
|
+
export function startSessions(ctx, forge) {
|
|
18
|
+
const api = {
|
|
19
|
+
sessions: async (root, source, page) => {
|
|
20
|
+
if (chatImportRunning(ctx))
|
|
21
|
+
throw new Error(YIELD_MESSAGE);
|
|
22
|
+
// The ones DSH already holds: found the way an import finds them, under the id of this version or of earlier ones.
|
|
23
|
+
const stored = await storedSessions(ctx);
|
|
24
|
+
const known = { threads: sharedThreads() };
|
|
25
|
+
// One row at a time: rows that compare turns share the stored session's reader.
|
|
26
|
+
const matched = new Map();
|
|
27
|
+
const matchOf = (session) => {
|
|
28
|
+
let match = matched.get(session.sourceId);
|
|
29
|
+
if (match === undefined) {
|
|
30
|
+
match = matchStored(identityOf(source, session, known), stored);
|
|
31
|
+
matched.set(session.sourceId, match);
|
|
32
|
+
}
|
|
33
|
+
return match;
|
|
34
|
+
};
|
|
35
|
+
let found;
|
|
36
|
+
const matches = [];
|
|
37
|
+
try {
|
|
38
|
+
// A subagent's thread an earlier version imported is listed anyway: that copy has to stay reachable.
|
|
39
|
+
found = source === 'codex'
|
|
40
|
+
? await discoverCodexSessions(root ?? defaultCodexSessionsRoot(), page, async (subagent) => (await matchOf(subagent)).kind === 'imported')
|
|
41
|
+
: source === 'opencode'
|
|
42
|
+
? await discoverOpenCodeSessions(root, page)
|
|
43
|
+
: await discoverClaudeSessions(root, page);
|
|
44
|
+
for (const session of found.sessions)
|
|
45
|
+
matches.push(await matchOf(session));
|
|
46
|
+
}
|
|
47
|
+
finally {
|
|
48
|
+
await stored.close();
|
|
49
|
+
}
|
|
50
|
+
const sessions = found.sessions.map((session, index) => {
|
|
51
|
+
const match = matches[index];
|
|
52
|
+
// Too large to import: said here so the list can show why its button is off. A compressed rollout's decoded size is not known until it is read.
|
|
53
|
+
const tooLarge = session.bytes !== undefined && session.bytes > MAX_SESSION_BYTES;
|
|
54
|
+
return {
|
|
55
|
+
...session,
|
|
56
|
+
source,
|
|
57
|
+
...(tooLarge ? { tooLarge, limit: MAX_SESSION_BYTES } : {}),
|
|
58
|
+
...(match.kind === 'imported' ? { importedId: match.id } : {}),
|
|
59
|
+
// Stored under an id it may hold, and nothing tells: the person opens that one, or imports this one as `newId`.
|
|
60
|
+
...(match.kind === 'maybe' ? { maybeImportedId: match.id, newId: match.newId } : {}),
|
|
61
|
+
};
|
|
62
|
+
});
|
|
63
|
+
return { sessions, ...(found.next !== undefined ? { next: found.next } : {}) };
|
|
64
|
+
},
|
|
65
|
+
importSession: (path, targetId, source, sourceId) => {
|
|
66
|
+
if (chatImportRunning(ctx))
|
|
67
|
+
throw new Error(YIELD_MESSAGE);
|
|
68
|
+
const options = targetId === undefined ? {} : { targetId };
|
|
69
|
+
if (source === 'codex')
|
|
70
|
+
return importCodexSession(ctx, path, options);
|
|
71
|
+
if (source === 'opencode') {
|
|
72
|
+
if (sourceId === undefined)
|
|
73
|
+
throw new Error('OpenCode import requires a sourceId');
|
|
74
|
+
return importOpenCodeSession(ctx, path, sourceId, options);
|
|
75
|
+
}
|
|
76
|
+
return importClaudeSession(ctx, path, options);
|
|
77
|
+
},
|
|
78
|
+
open: id => openStored(ctx, id),
|
|
79
|
+
};
|
|
80
|
+
return forge.provide('sessions', api);
|
|
81
|
+
}
|
|
82
|
+
/** How a listed session is found among the stored ones: the identity its import uses. */
|
|
83
|
+
function identityOf(source, session, known) {
|
|
84
|
+
if (source === 'codex')
|
|
85
|
+
return rolloutIdentity(session.path, { id: session.sourceId, sessionId: session.parentId ?? session.sourceId }, session.createdAt ?? 0, known);
|
|
86
|
+
if (source === 'opencode')
|
|
87
|
+
return openCodeIdentity(session.sourceId);
|
|
88
|
+
return claudeIdentity(session.sourceId);
|
|
89
|
+
}
|
|
90
|
+
/** The threads under a sessions folder, read once for all the rows of one list. */
|
|
91
|
+
function sharedThreads() {
|
|
92
|
+
const read = new Map();
|
|
93
|
+
return (root) => {
|
|
94
|
+
let threads = read.get(root);
|
|
95
|
+
if (threads === undefined) {
|
|
96
|
+
threads = codexThreads(root);
|
|
97
|
+
read.set(root, threads);
|
|
98
|
+
}
|
|
99
|
+
return threads;
|
|
100
|
+
};
|
|
101
|
+
}
|