@fleetagent/pi-coding-agent 0.0.6 → 0.0.8
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/CHANGELOG.md +35 -0
- package/README.md +3 -3
- package/dist/cli/file-processor.d.ts.map +1 -1
- package/dist/cli/file-processor.js +2 -3
- package/dist/cli/file-processor.js.map +1 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +15 -2
- package/dist/config.js.map +1 -1
- package/dist/core/agent-session.d.ts +12 -0
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +123 -18
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/export-html/template.js +6 -3
- package/dist/core/extensions/runner.d.ts +1 -1
- package/dist/core/extensions/runner.d.ts.map +1 -1
- package/dist/core/extensions/runner.js +8 -2
- package/dist/core/extensions/runner.js.map +1 -1
- package/dist/core/extensions/types.d.ts +4 -2
- package/dist/core/extensions/types.d.ts.map +1 -1
- package/dist/core/extensions/types.js.map +1 -1
- package/dist/core/model-registry.d.ts.map +1 -1
- package/dist/core/model-registry.js +65 -13
- package/dist/core/model-registry.js.map +1 -1
- package/dist/core/output-guard.d.ts +1 -0
- package/dist/core/output-guard.d.ts.map +1 -1
- package/dist/core/output-guard.js +52 -22
- package/dist/core/output-guard.js.map +1 -1
- package/dist/core/package-manager.d.ts.map +1 -1
- package/dist/core/package-manager.js +31 -12
- package/dist/core/package-manager.js.map +1 -1
- package/dist/core/pi-agent.d.ts.map +1 -1
- package/dist/core/pi-agent.js +12 -3
- package/dist/core/pi-agent.js.map +1 -1
- package/dist/core/resolve-config-value.d.ts +9 -1
- package/dist/core/resolve-config-value.d.ts.map +1 -1
- package/dist/core/resolve-config-value.js +134 -11
- package/dist/core/resolve-config-value.js.map +1 -1
- package/dist/core/session/jsonl-helpers.d.ts +2 -1
- package/dist/core/session/jsonl-helpers.d.ts.map +1 -1
- package/dist/core/session/jsonl-helpers.js +6 -3
- package/dist/core/session/jsonl-helpers.js.map +1 -1
- package/dist/core/session/local-session-manager.d.ts +1 -0
- package/dist/core/session/local-session-manager.d.ts.map +1 -1
- package/dist/core/session/local-session-manager.js +12 -4
- package/dist/core/session/local-session-manager.js.map +1 -1
- package/dist/core/session/session-manager.d.ts +1 -0
- package/dist/core/session/session-manager.d.ts.map +1 -1
- package/dist/core/session/session-manager.js.map +1 -1
- package/dist/core/session/stores/jsonl-session-store.d.ts +2 -1
- package/dist/core/session/stores/jsonl-session-store.d.ts.map +1 -1
- package/dist/core/session/stores/jsonl-session-store.js +105 -78
- package/dist/core/session/stores/jsonl-session-store.js.map +1 -1
- package/dist/core/settings-manager.d.ts +2 -0
- package/dist/core/settings-manager.d.ts.map +1 -1
- package/dist/core/settings-manager.js +14 -9
- package/dist/core/settings-manager.js.map +1 -1
- package/dist/core/tools/bash.d.ts.map +1 -1
- package/dist/core/tools/bash.js +73 -63
- package/dist/core/tools/bash.js.map +1 -1
- package/dist/core/tools/edit.d.ts.map +1 -1
- package/dist/core/tools/edit.js +45 -76
- package/dist/core/tools/edit.js.map +1 -1
- package/dist/core/tools/file-mutation-queue.d.ts.map +1 -1
- package/dist/core/tools/file-mutation-queue.js +27 -12
- package/dist/core/tools/file-mutation-queue.js.map +1 -1
- package/dist/core/tools/find.d.ts.map +1 -1
- package/dist/core/tools/find.js +11 -2
- package/dist/core/tools/find.js.map +1 -1
- package/dist/core/tools/grep.d.ts.map +1 -1
- package/dist/core/tools/grep.js +3 -3
- package/dist/core/tools/grep.js.map +1 -1
- package/dist/core/tools/ls.d.ts.map +1 -1
- package/dist/core/tools/ls.js +13 -4
- package/dist/core/tools/ls.js.map +1 -1
- package/dist/core/tools/path-utils.d.ts +1 -0
- package/dist/core/tools/path-utils.d.ts.map +1 -1
- package/dist/core/tools/path-utils.js +37 -0
- package/dist/core/tools/path-utils.js.map +1 -1
- package/dist/core/tools/read.d.ts.map +1 -1
- package/dist/core/tools/read.js +7 -6
- package/dist/core/tools/read.js.map +1 -1
- package/dist/core/tools/write.d.ts.map +1 -1
- package/dist/core/tools/write.js +24 -32
- package/dist/core/tools/write.js.map +1 -1
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +3 -2
- package/dist/main.js.map +1 -1
- package/dist/migrations.d.ts.map +1 -1
- package/dist/migrations.js +118 -1
- package/dist/migrations.js.map +1 -1
- package/dist/modes/interactive/components/footer.d.ts +1 -0
- package/dist/modes/interactive/components/footer.d.ts.map +1 -1
- package/dist/modes/interactive/components/footer.js +14 -5
- package/dist/modes/interactive/components/footer.js.map +1 -1
- package/dist/modes/interactive/components/user-message.d.ts.map +1 -1
- package/dist/modes/interactive/components/user-message.js +1 -1
- package/dist/modes/interactive/components/user-message.js.map +1 -1
- package/dist/modes/interactive/interactive-mode.d.ts +1 -0
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +34 -8
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/modes/interactive/theme/theme.d.ts.map +1 -1
- package/dist/modes/interactive/theme/theme.js +10 -0
- package/dist/modes/interactive/theme/theme.js.map +1 -1
- package/dist/modes/rpc/rpc-client.d.ts +3 -0
- package/dist/modes/rpc/rpc-client.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-client.js +64 -7
- package/dist/modes/rpc/rpc-client.js.map +1 -1
- package/dist/modes/rpc/rpc-mode.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-mode.js +15 -3
- package/dist/modes/rpc/rpc-mode.js.map +1 -1
- package/dist/utils/clipboard-native.d.ts +3 -1
- package/dist/utils/clipboard-native.d.ts.map +1 -1
- package/dist/utils/clipboard-native.js +14 -8
- package/dist/utils/clipboard-native.js.map +1 -1
- package/dist/utils/deprecation.d.ts +4 -0
- package/dist/utils/deprecation.d.ts.map +1 -0
- package/dist/utils/deprecation.js +13 -0
- package/dist/utils/deprecation.js.map +1 -0
- package/dist/utils/image-resize-core.d.ts +30 -0
- package/dist/utils/image-resize-core.d.ts.map +1 -0
- package/dist/utils/image-resize-core.js +124 -0
- package/dist/utils/image-resize-core.js.map +1 -0
- package/dist/utils/image-resize-worker.d.ts +2 -0
- package/dist/utils/image-resize-worker.d.ts.map +1 -0
- package/dist/utils/image-resize-worker.js +31 -0
- package/dist/utils/image-resize-worker.js.map +1 -0
- package/dist/utils/image-resize.d.ts +6 -27
- package/dist/utils/image-resize.d.ts.map +1 -1
- package/dist/utils/image-resize.js +60 -116
- package/dist/utils/image-resize.js.map +1 -1
- package/dist/utils/json.d.ts +3 -0
- package/dist/utils/json.d.ts.map +1 -0
- package/dist/utils/json.js +7 -0
- package/dist/utils/json.js.map +1 -0
- package/dist/utils/version-check.d.ts.map +1 -1
- package/dist/utils/version-check.js +10 -4
- package/dist/utils/version-check.js.map +1 -1
- package/docs/custom-provider.md +22 -9
- package/docs/extensions.md +4 -3
- package/docs/models.md +34 -12
- package/docs/packages.md +5 -4
- package/docs/providers.md +13 -5
- package/docs/sdk.md +56 -0
- package/docs/settings.md +4 -2
- package/docs/terminal-setup.md +6 -0
- package/docs/usage.md +3 -3
- package/examples/extensions/README.md +1 -0
- package/examples/extensions/custom-provider-anthropic/index.ts +1 -1
- package/examples/extensions/custom-provider-anthropic/package.json +1 -1
- package/examples/extensions/custom-provider-gitlab-duo/index.ts +54 -3
- package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
- package/examples/extensions/git-merge-and-resolve.ts +115 -0
- package/examples/extensions/sandbox/package.json +1 -1
- package/examples/extensions/with-deps/package.json +1 -1
- package/npm-shrinkwrap.json +13 -12
- package/package.json +5 -5
|
@@ -6,17 +6,130 @@ import { execSync, spawnSync } from "child_process";
|
|
|
6
6
|
import { getShellConfig } from "../utils/shell.js";
|
|
7
7
|
// Cache for shell command results (persists for process lifetime)
|
|
8
8
|
const commandResultCache = new Map();
|
|
9
|
+
const ENV_VAR_NAME_RE = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
10
|
+
const ENV_VAR_NAME_PREFIX_RE = /^[A-Za-z_][A-Za-z0-9_]*/;
|
|
11
|
+
const LEGACY_ENV_VAR_NAME_RE = /^[A-Z_][A-Z0-9_]*$/;
|
|
12
|
+
function appendLiteral(parts, value) {
|
|
13
|
+
if (!value)
|
|
14
|
+
return;
|
|
15
|
+
const previousPart = parts[parts.length - 1];
|
|
16
|
+
if (previousPart?.type === "literal") {
|
|
17
|
+
previousPart.value += value;
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
parts.push({ type: "literal", value });
|
|
21
|
+
}
|
|
22
|
+
function parseConfigValueTemplate(config) {
|
|
23
|
+
const parts = [];
|
|
24
|
+
let index = 0;
|
|
25
|
+
while (index < config.length) {
|
|
26
|
+
const dollarIndex = config.indexOf("$", index);
|
|
27
|
+
if (dollarIndex < 0) {
|
|
28
|
+
appendLiteral(parts, config.slice(index));
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
appendLiteral(parts, config.slice(index, dollarIndex));
|
|
32
|
+
const nextChar = config[dollarIndex + 1];
|
|
33
|
+
if (nextChar === "$" || nextChar === "!") {
|
|
34
|
+
appendLiteral(parts, nextChar);
|
|
35
|
+
index = dollarIndex + 2;
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
if (nextChar === "{") {
|
|
39
|
+
const endIndex = config.indexOf("}", dollarIndex + 2);
|
|
40
|
+
if (endIndex < 0) {
|
|
41
|
+
appendLiteral(parts, "$");
|
|
42
|
+
index = dollarIndex + 1;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
const name = config.slice(dollarIndex + 2, endIndex);
|
|
46
|
+
if (ENV_VAR_NAME_RE.test(name)) {
|
|
47
|
+
parts.push({ type: "env", name });
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
appendLiteral(parts, config.slice(dollarIndex, endIndex + 1));
|
|
51
|
+
}
|
|
52
|
+
index = endIndex + 1;
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
const match = config.slice(dollarIndex + 1).match(ENV_VAR_NAME_PREFIX_RE);
|
|
56
|
+
if (match) {
|
|
57
|
+
parts.push({ type: "env", name: match[0] });
|
|
58
|
+
index = dollarIndex + 1 + match[0].length;
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
appendLiteral(parts, "$");
|
|
62
|
+
index = dollarIndex + 1;
|
|
63
|
+
}
|
|
64
|
+
return parts;
|
|
65
|
+
}
|
|
66
|
+
function parseConfigValueReference(config) {
|
|
67
|
+
if (config.startsWith("!")) {
|
|
68
|
+
return { type: "command", config };
|
|
69
|
+
}
|
|
70
|
+
return { type: "template", parts: parseConfigValueTemplate(config) };
|
|
71
|
+
}
|
|
72
|
+
function resolveEnvConfigValue(name) {
|
|
73
|
+
return process.env[name] || undefined;
|
|
74
|
+
}
|
|
75
|
+
function getTemplateEnvVarNames(parts) {
|
|
76
|
+
const names = [];
|
|
77
|
+
for (const part of parts) {
|
|
78
|
+
if (part.type !== "env" || names.includes(part.name))
|
|
79
|
+
continue;
|
|
80
|
+
names.push(part.name);
|
|
81
|
+
}
|
|
82
|
+
return names;
|
|
83
|
+
}
|
|
84
|
+
function resolveTemplate(parts) {
|
|
85
|
+
let resolved = "";
|
|
86
|
+
for (const part of parts) {
|
|
87
|
+
if (part.type === "literal") {
|
|
88
|
+
resolved += part.value;
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
const envValue = resolveEnvConfigValue(part.name);
|
|
92
|
+
if (envValue === undefined)
|
|
93
|
+
return undefined;
|
|
94
|
+
resolved += envValue;
|
|
95
|
+
}
|
|
96
|
+
return resolved;
|
|
97
|
+
}
|
|
98
|
+
export function getConfigValueEnvVarName(config) {
|
|
99
|
+
const reference = parseConfigValueReference(config);
|
|
100
|
+
if (reference.type !== "template")
|
|
101
|
+
return undefined;
|
|
102
|
+
return reference.parts.length === 1 && reference.parts[0]?.type === "env" ? reference.parts[0].name : undefined;
|
|
103
|
+
}
|
|
104
|
+
export function getConfigValueEnvVarNames(config) {
|
|
105
|
+
const reference = parseConfigValueReference(config);
|
|
106
|
+
return reference.type === "template" ? getTemplateEnvVarNames(reference.parts) : [];
|
|
107
|
+
}
|
|
108
|
+
export function getMissingConfigValueEnvVarNames(config) {
|
|
109
|
+
return getConfigValueEnvVarNames(config).filter((name) => resolveEnvConfigValue(name) === undefined);
|
|
110
|
+
}
|
|
111
|
+
export function isCommandConfigValue(config) {
|
|
112
|
+
return parseConfigValueReference(config).type === "command";
|
|
113
|
+
}
|
|
114
|
+
export function isConfigValueConfigured(config) {
|
|
115
|
+
return getMissingConfigValueEnvVarNames(config).length === 0;
|
|
116
|
+
}
|
|
117
|
+
export function isLegacyEnvVarNameConfigValue(config) {
|
|
118
|
+
return LEGACY_ENV_VAR_NAME_RE.test(config);
|
|
119
|
+
}
|
|
9
120
|
/**
|
|
10
121
|
* Resolve a config value (API key, header value, etc.) to an actual value.
|
|
11
122
|
* - If starts with "!", executes the rest as a shell command and uses stdout (cached)
|
|
12
|
-
* -
|
|
123
|
+
* - Interpolates "$ENV_VAR" or "${ENV_VAR}" references with the named environment variable
|
|
124
|
+
* - In non-command values, "$$" escapes a literal "$" and "$!" escapes a literal "!"
|
|
125
|
+
* - Otherwise treats the value as a literal
|
|
13
126
|
*/
|
|
14
127
|
export function resolveConfigValue(config) {
|
|
15
|
-
|
|
16
|
-
|
|
128
|
+
const reference = parseConfigValueReference(config);
|
|
129
|
+
if (reference.type === "command") {
|
|
130
|
+
return executeCommand(reference.config);
|
|
17
131
|
}
|
|
18
|
-
|
|
19
|
-
return envValue || config;
|
|
132
|
+
return resolveTemplate(reference.parts);
|
|
20
133
|
}
|
|
21
134
|
function executeWithConfiguredShell(command) {
|
|
22
135
|
try {
|
|
@@ -79,19 +192,29 @@ function executeCommand(commandConfig) {
|
|
|
79
192
|
* Resolve all header values using the same resolution logic as API keys.
|
|
80
193
|
*/
|
|
81
194
|
export function resolveConfigValueUncached(config) {
|
|
82
|
-
|
|
83
|
-
|
|
195
|
+
const reference = parseConfigValueReference(config);
|
|
196
|
+
if (reference.type === "command") {
|
|
197
|
+
return executeCommandUncached(reference.config);
|
|
84
198
|
}
|
|
85
|
-
|
|
86
|
-
return envValue || config;
|
|
199
|
+
return resolveTemplate(reference.parts);
|
|
87
200
|
}
|
|
88
201
|
export function resolveConfigValueOrThrow(config, description) {
|
|
89
202
|
const resolvedValue = resolveConfigValueUncached(config);
|
|
90
203
|
if (resolvedValue !== undefined) {
|
|
91
204
|
return resolvedValue;
|
|
92
205
|
}
|
|
93
|
-
|
|
94
|
-
|
|
206
|
+
const reference = parseConfigValueReference(config);
|
|
207
|
+
if (reference.type === "command") {
|
|
208
|
+
throw new Error(`Failed to resolve ${description} from shell command: ${reference.config.slice(1)}`);
|
|
209
|
+
}
|
|
210
|
+
if (reference.type === "template") {
|
|
211
|
+
const missingEnvVars = getMissingConfigValueEnvVarNames(config);
|
|
212
|
+
if (missingEnvVars.length === 1) {
|
|
213
|
+
throw new Error(`Failed to resolve ${description} from environment variable: ${missingEnvVars[0]}`);
|
|
214
|
+
}
|
|
215
|
+
if (missingEnvVars.length > 1) {
|
|
216
|
+
throw new Error(`Failed to resolve ${description} from environment variables: ${missingEnvVars.join(", ")}`);
|
|
217
|
+
}
|
|
95
218
|
}
|
|
96
219
|
throw new Error(`Failed to resolve ${description}`);
|
|
97
220
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolve-config-value.js","sourceRoot":"","sources":["../../src/core/resolve-config-value.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AACpD,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAEnD,kEAAkE;AAClE,MAAM,kBAAkB,GAAG,IAAI,GAAG,EAA8B,CAAC;AAEjE;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAAC,MAAc,EAAsB;IACtE,IAAI,MAAM,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAA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AAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC;AAAA,CAC/D;AAED,MAAM,UAAU,qBAAqB,CACpC,OAA2C,EAC3C,WAAmB,EACkB;IACrC,IAAI,CAAC,OAAO;QAAE,OAAO,SAAS,CAAC;IAC/B,MAAM,QAAQ,GAA2B,EAAE,CAAC;IAC5C,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;QACpD,QAAQ,CAAC,GAAG,CAAC,GAAG,yBAAyB,CAAC,KAAK,EAAE,GAAG,WAAW,YAAY,GAAG,GAAG,CAAC,CAAC;IACpF,CAAC;IACD,OAAO,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC;AAAA,CAC/D;AAED,kEAAkE;AAClE,MAAM,UAAU,qBAAqB,GAAS;IAC7C,kBAAkB,CAAC,KAAK,EAAE,CAAC;AAAA,CAC3B","sourcesContent":["/**\n * Resolve configuration values that may be shell commands, environment variables, or literals.\n * Used by auth-storage.ts and model-registry.ts.\n */\n\nimport { execSync, spawnSync } from \"child_process\";\nimport { getShellConfig } from \"../utils/shell.ts\";\n\n// Cache for shell command results (persists for process lifetime)\nconst commandResultCache = new Map<string, string | undefined>();\n\n/**\n * Resolve a config value (API key, header value, etc.) to an actual value.\n * - If starts with \"!\", executes the rest as a shell command and uses stdout (cached)\n * - Otherwise checks environment variable first, then treats as literal (not cached)\n */\nexport function resolveConfigValue(config: string): string | undefined {\n\tif (config.startsWith(\"!\")) {\n\t\treturn executeCommand(config);\n\t}\n\tconst envValue = process.env[config];\n\treturn envValue || config;\n}\n\nfunction executeWithConfiguredShell(command: string): { executed: boolean; value: string | undefined } {\n\ttry {\n\t\tconst { shell, args } = getShellConfig();\n\t\tconst result = spawnSync(shell, [...args, command], {\n\t\t\tencoding: \"utf-8\",\n\t\t\ttimeout: 10000,\n\t\t\tstdio: [\"ignore\", \"pipe\", \"ignore\"],\n\t\t\tshell: false,\n\t\t\twindowsHide: true,\n\t\t});\n\n\t\tif (result.error) {\n\t\t\tconst error = result.error as NodeJS.ErrnoException;\n\t\t\tif (error.code === \"ENOENT\") {\n\t\t\t\treturn { executed: false, value: undefined };\n\t\t\t}\n\t\t\treturn { executed: true, value: undefined };\n\t\t}\n\n\t\tif (result.status !== 0) {\n\t\t\treturn { executed: true, value: undefined };\n\t\t}\n\n\t\tconst value = (result.stdout ?? \"\").trim();\n\t\treturn { executed: true, value: value || undefined };\n\t} catch {\n\t\treturn { executed: false, value: undefined };\n\t}\n}\n\nfunction executeWithDefaultShell(command: string): string | undefined {\n\ttry {\n\t\tconst output = execSync(command, {\n\t\t\tencoding: \"utf-8\",\n\t\t\ttimeout: 10000,\n\t\t\tstdio: [\"ignore\", \"pipe\", \"ignore\"],\n\t\t});\n\t\treturn output.trim() || undefined;\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\nfunction executeCommandUncached(commandConfig: string): string | undefined {\n\tconst command = commandConfig.slice(1);\n\treturn process.platform === \"win32\"\n\t\t? (() => {\n\t\t\t\tconst configuredResult = executeWithConfiguredShell(command);\n\t\t\t\treturn configuredResult.executed ? configuredResult.value : executeWithDefaultShell(command);\n\t\t\t})()\n\t\t: executeWithDefaultShell(command);\n}\n\nfunction executeCommand(commandConfig: string): string | undefined {\n\tif (commandResultCache.has(commandConfig)) {\n\t\treturn commandResultCache.get(commandConfig);\n\t}\n\n\tconst result = executeCommandUncached(commandConfig);\n\tcommandResultCache.set(commandConfig, result);\n\treturn result;\n}\n\n/**\n * Resolve all header values using the same resolution logic as API keys.\n */\nexport function resolveConfigValueUncached(config: string): string | undefined {\n\tif (config.startsWith(\"!\")) {\n\t\treturn executeCommandUncached(config);\n\t}\n\tconst envValue = process.env[config];\n\treturn envValue || config;\n}\n\nexport function resolveConfigValueOrThrow(config: string, description: string): string {\n\tconst resolvedValue = resolveConfigValueUncached(config);\n\tif (resolvedValue !== undefined) {\n\t\treturn resolvedValue;\n\t}\n\n\tif (config.startsWith(\"!\")) {\n\t\tthrow new Error(`Failed to resolve ${description} from shell command: ${config.slice(1)}`);\n\t}\n\n\tthrow new Error(`Failed to resolve ${description}`);\n}\n\n/**\n * Resolve all header values using the same resolution logic as API keys.\n */\nexport function resolveHeaders(headers: Record<string, string> | undefined): Record<string, string> | undefined {\n\tif (!headers) return undefined;\n\tconst resolved: Record<string, string> = {};\n\tfor (const [key, value] of Object.entries(headers)) {\n\t\tconst resolvedValue = resolveConfigValue(value);\n\t\tif (resolvedValue) {\n\t\t\tresolved[key] = resolvedValue;\n\t\t}\n\t}\n\treturn Object.keys(resolved).length > 0 ? resolved : undefined;\n}\n\nexport function resolveHeadersOrThrow(\n\theaders: Record<string, string> | undefined,\n\tdescription: string,\n): Record<string, string> | undefined {\n\tif (!headers) return undefined;\n\tconst resolved: Record<string, string> = {};\n\tfor (const [key, value] of Object.entries(headers)) {\n\t\tresolved[key] = resolveConfigValueOrThrow(value, `${description} header \"${key}\"`);\n\t}\n\treturn Object.keys(resolved).length > 0 ? resolved : undefined;\n}\n\n/** Clear the config value command cache. Exported for testing. */\nexport function clearConfigValueCache(): void {\n\tcommandResultCache.clear();\n}\n"]}
|
|
1
|
+
{"version":3,"file":"resolve-config-value.js","sourceRoot":"","sources":["../../src/core/resolve-config-value.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AACpD,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAEnD,kEAAkE;AAClE,MAAM,kBAAkB,GAAG,IAAI,GAAG,EAA8B,CAAC;AACjE,MAAM,eAAe,GAAG,0BAA0B,CAAC;AACnD,MAAM,sBAAsB,GAAG,yBAAyB,CAAC;AACzD,MAAM,sBAAsB,GAAG,oBAAoB,CAAC;AAMpD,SAAS,aAA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C,OAA2C,EAAsC;IAC/G,IAAI,CAAC,OAAO;QAAE,OAAO,SAAS,CAAC;IAC/B,MAAM,QAAQ,GAA2B,EAAE,CAAC;IAC5C,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;QACpD,MAAM,aAAa,GAAG,kBAAkB,CAAC,KAAK,CAAC,CAAC;QAChD,IAAI,aAAa,EAAE,CAAC;YACnB,QAAQ,CAAC,GAAG,CAAC,GAAG,aAAa,CAAC;QAC/B,CAAC;IACF,CAAC;IACD,OAAO,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC;AAAA,CAC/D;AAED,MAAM,UAAU,qBAAqB,CACpC,OAA2C,EAC3C,WAAmB,EACkB;IACrC,IAAI,CAAC,OAAO;QAAE,OAAO,SAAS,CAAC;IAC/B,MAAM,QAAQ,GAA2B,EAAE,CAAC;IAC5C,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;QACpD,QAAQ,CAAC,GAAG,CAAC,GAAG,yBAAyB,CAAC,KAAK,EAAE,GAAG,WAAW,YAAY,GAAG,GAAG,CAAC,CAAC;IACpF,CAAC;IACD,OAAO,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC;AAAA,CAC/D;AAED,kEAAkE;AAClE,MAAM,UAAU,qBAAqB,GAAS;IAC7C,kBAAkB,CAAC,KAAK,EAAE,CAAC;AAAA,CAC3B","sourcesContent":["/**\n * Resolve configuration values that may be shell commands, environment variables, or literals.\n * Used by auth-storage.ts and model-registry.ts.\n */\n\nimport { execSync, spawnSync } from \"child_process\";\nimport { getShellConfig } from \"../utils/shell.ts\";\n\n// Cache for shell command results (persists for process lifetime)\nconst commandResultCache = new Map<string, string | undefined>();\nconst ENV_VAR_NAME_RE = /^[A-Za-z_][A-Za-z0-9_]*$/;\nconst ENV_VAR_NAME_PREFIX_RE = /^[A-Za-z_][A-Za-z0-9_]*/;\nconst LEGACY_ENV_VAR_NAME_RE = /^[A-Z_][A-Z0-9_]*$/;\n\ntype TemplatePart = { type: \"literal\"; value: string } | { type: \"env\"; name: string };\n\ntype ConfigValueReference = { type: \"command\"; config: string } | { type: \"template\"; parts: TemplatePart[] };\n\nfunction appendLiteral(parts: TemplatePart[], value: string): void {\n\tif (!value) return;\n\tconst previousPart = parts[parts.length - 1];\n\tif (previousPart?.type === \"literal\") {\n\t\tpreviousPart.value += value;\n\t\treturn;\n\t}\n\tparts.push({ type: \"literal\", value });\n}\n\nfunction parseConfigValueTemplate(config: string): TemplatePart[] {\n\tconst parts: TemplatePart[] = [];\n\tlet index = 0;\n\n\twhile (index < config.length) {\n\t\tconst dollarIndex = config.indexOf(\"$\", index);\n\t\tif (dollarIndex < 0) {\n\t\t\tappendLiteral(parts, config.slice(index));\n\t\t\tbreak;\n\t\t}\n\n\t\tappendLiteral(parts, config.slice(index, dollarIndex));\n\t\tconst nextChar = config[dollarIndex + 1];\n\n\t\tif (nextChar === \"$\" || nextChar === \"!\") {\n\t\t\tappendLiteral(parts, nextChar);\n\t\t\tindex = dollarIndex + 2;\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (nextChar === \"{\") {\n\t\t\tconst endIndex = config.indexOf(\"}\", dollarIndex + 2);\n\t\t\tif (endIndex < 0) {\n\t\t\t\tappendLiteral(parts, \"$\");\n\t\t\t\tindex = dollarIndex + 1;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tconst name = config.slice(dollarIndex + 2, endIndex);\n\t\t\tif (ENV_VAR_NAME_RE.test(name)) {\n\t\t\t\tparts.push({ type: \"env\", name });\n\t\t\t} else {\n\t\t\t\tappendLiteral(parts, config.slice(dollarIndex, endIndex + 1));\n\t\t\t}\n\t\t\tindex = endIndex + 1;\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst match = config.slice(dollarIndex + 1).match(ENV_VAR_NAME_PREFIX_RE);\n\t\tif (match) {\n\t\t\tparts.push({ type: \"env\", name: match[0] });\n\t\t\tindex = dollarIndex + 1 + match[0].length;\n\t\t\tcontinue;\n\t\t}\n\n\t\tappendLiteral(parts, \"$\");\n\t\tindex = dollarIndex + 1;\n\t}\n\n\treturn parts;\n}\n\nfunction parseConfigValueReference(config: string): ConfigValueReference {\n\tif (config.startsWith(\"!\")) {\n\t\treturn { type: \"command\", config };\n\t}\n\n\treturn { type: \"template\", parts: parseConfigValueTemplate(config) };\n}\n\nfunction resolveEnvConfigValue(name: string): string | undefined {\n\treturn process.env[name] || undefined;\n}\n\nfunction getTemplateEnvVarNames(parts: TemplatePart[]): string[] {\n\tconst names: string[] = [];\n\tfor (const part of parts) {\n\t\tif (part.type !== \"env\" || names.includes(part.name)) continue;\n\t\tnames.push(part.name);\n\t}\n\treturn names;\n}\n\nfunction resolveTemplate(parts: TemplatePart[]): string | undefined {\n\tlet resolved = \"\";\n\tfor (const part of parts) {\n\t\tif (part.type === \"literal\") {\n\t\t\tresolved += part.value;\n\t\t\tcontinue;\n\t\t}\n\t\tconst envValue = resolveEnvConfigValue(part.name);\n\t\tif (envValue === undefined) return undefined;\n\t\tresolved += envValue;\n\t}\n\treturn resolved;\n}\n\nexport function getConfigValueEnvVarName(config: string): string | undefined {\n\tconst reference = parseConfigValueReference(config);\n\tif (reference.type !== \"template\") return undefined;\n\treturn reference.parts.length === 1 && reference.parts[0]?.type === \"env\" ? reference.parts[0].name : undefined;\n}\n\nexport function getConfigValueEnvVarNames(config: string): string[] {\n\tconst reference = parseConfigValueReference(config);\n\treturn reference.type === \"template\" ? getTemplateEnvVarNames(reference.parts) : [];\n}\n\nexport function getMissingConfigValueEnvVarNames(config: string): string[] {\n\treturn getConfigValueEnvVarNames(config).filter((name) => resolveEnvConfigValue(name) === undefined);\n}\n\nexport function isCommandConfigValue(config: string): boolean {\n\treturn parseConfigValueReference(config).type === \"command\";\n}\n\nexport function isConfigValueConfigured(config: string): boolean {\n\treturn getMissingConfigValueEnvVarNames(config).length === 0;\n}\n\nexport function isLegacyEnvVarNameConfigValue(config: string): boolean {\n\treturn LEGACY_ENV_VAR_NAME_RE.test(config);\n}\n\n/**\n * Resolve a config value (API key, header value, etc.) to an actual value.\n * - If starts with \"!\", executes the rest as a shell command and uses stdout (cached)\n * - Interpolates \"$ENV_VAR\" or \"${ENV_VAR}\" references with the named environment variable\n * - In non-command values, \"$$\" escapes a literal \"$\" and \"$!\" escapes a literal \"!\"\n * - Otherwise treats the value as a literal\n */\nexport function resolveConfigValue(config: string): string | undefined {\n\tconst reference = parseConfigValueReference(config);\n\tif (reference.type === \"command\") {\n\t\treturn executeCommand(reference.config);\n\t}\n\treturn resolveTemplate(reference.parts);\n}\n\nfunction executeWithConfiguredShell(command: string): { executed: boolean; value: string | undefined } {\n\ttry {\n\t\tconst { shell, args } = getShellConfig();\n\t\tconst result = spawnSync(shell, [...args, command], {\n\t\t\tencoding: \"utf-8\",\n\t\t\ttimeout: 10000,\n\t\t\tstdio: [\"ignore\", \"pipe\", \"ignore\"],\n\t\t\tshell: false,\n\t\t\twindowsHide: true,\n\t\t});\n\n\t\tif (result.error) {\n\t\t\tconst error = result.error as NodeJS.ErrnoException;\n\t\t\tif (error.code === \"ENOENT\") {\n\t\t\t\treturn { executed: false, value: undefined };\n\t\t\t}\n\t\t\treturn { executed: true, value: undefined };\n\t\t}\n\n\t\tif (result.status !== 0) {\n\t\t\treturn { executed: true, value: undefined };\n\t\t}\n\n\t\tconst value = (result.stdout ?? \"\").trim();\n\t\treturn { executed: true, value: value || undefined };\n\t} catch {\n\t\treturn { executed: false, value: undefined };\n\t}\n}\n\nfunction executeWithDefaultShell(command: string): string | undefined {\n\ttry {\n\t\tconst output = execSync(command, {\n\t\t\tencoding: \"utf-8\",\n\t\t\ttimeout: 10000,\n\t\t\tstdio: [\"ignore\", \"pipe\", \"ignore\"],\n\t\t});\n\t\treturn output.trim() || undefined;\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\nfunction executeCommandUncached(commandConfig: string): string | undefined {\n\tconst command = commandConfig.slice(1);\n\treturn process.platform === \"win32\"\n\t\t? (() => {\n\t\t\t\tconst configuredResult = executeWithConfiguredShell(command);\n\t\t\t\treturn configuredResult.executed ? configuredResult.value : executeWithDefaultShell(command);\n\t\t\t})()\n\t\t: executeWithDefaultShell(command);\n}\n\nfunction executeCommand(commandConfig: string): string | undefined {\n\tif (commandResultCache.has(commandConfig)) {\n\t\treturn commandResultCache.get(commandConfig);\n\t}\n\n\tconst result = executeCommandUncached(commandConfig);\n\tcommandResultCache.set(commandConfig, result);\n\treturn result;\n}\n\n/**\n * Resolve all header values using the same resolution logic as API keys.\n */\nexport function resolveConfigValueUncached(config: string): string | undefined {\n\tconst reference = parseConfigValueReference(config);\n\tif (reference.type === \"command\") {\n\t\treturn executeCommandUncached(reference.config);\n\t}\n\treturn resolveTemplate(reference.parts);\n}\n\nexport function resolveConfigValueOrThrow(config: string, description: string): string {\n\tconst resolvedValue = resolveConfigValueUncached(config);\n\tif (resolvedValue !== undefined) {\n\t\treturn resolvedValue;\n\t}\n\n\tconst reference = parseConfigValueReference(config);\n\tif (reference.type === \"command\") {\n\t\tthrow new Error(`Failed to resolve ${description} from shell command: ${reference.config.slice(1)}`);\n\t}\n\n\tif (reference.type === \"template\") {\n\t\tconst missingEnvVars = getMissingConfigValueEnvVarNames(config);\n\t\tif (missingEnvVars.length === 1) {\n\t\t\tthrow new Error(`Failed to resolve ${description} from environment variable: ${missingEnvVars[0]}`);\n\t\t}\n\t\tif (missingEnvVars.length > 1) {\n\t\t\tthrow new Error(`Failed to resolve ${description} from environment variables: ${missingEnvVars.join(\", \")}`);\n\t\t}\n\t}\n\n\tthrow new Error(`Failed to resolve ${description}`);\n}\n\n/**\n * Resolve all header values using the same resolution logic as API keys.\n */\nexport function resolveHeaders(headers: Record<string, string> | undefined): Record<string, string> | undefined {\n\tif (!headers) return undefined;\n\tconst resolved: Record<string, string> = {};\n\tfor (const [key, value] of Object.entries(headers)) {\n\t\tconst resolvedValue = resolveConfigValue(value);\n\t\tif (resolvedValue) {\n\t\t\tresolved[key] = resolvedValue;\n\t\t}\n\t}\n\treturn Object.keys(resolved).length > 0 ? resolved : undefined;\n}\n\nexport function resolveHeadersOrThrow(\n\theaders: Record<string, string> | undefined,\n\tdescription: string,\n): Record<string, string> | undefined {\n\tif (!headers) return undefined;\n\tconst resolved: Record<string, string> = {};\n\tfor (const [key, value] of Object.entries(headers)) {\n\t\tresolved[key] = resolveConfigValueOrThrow(value, `${description} header \"${key}\"`);\n\t}\n\treturn Object.keys(resolved).length > 0 ? resolved : undefined;\n}\n\n/** Clear the config value command cache. Exported for testing. */\nexport function clearConfigValueCache(): void {\n\tcommandResultCache.clear();\n}\n"]}
|
|
@@ -8,6 +8,7 @@ export declare const jsonlSessionStore: JsonlSessionStore;
|
|
|
8
8
|
export declare function getDefaultSessionDir(cwd: string, agentDir?: string): string;
|
|
9
9
|
/** Exported for testing. */
|
|
10
10
|
export declare function loadEntriesFromFile(filePath: string): FileEntry[];
|
|
11
|
+
export declare function getDefaultSessionDirPath(cwd: string, agentDir?: string): string;
|
|
11
12
|
/** Exported for testing. */
|
|
12
|
-
export declare function findMostRecentSession(sessionDir: string): string | null;
|
|
13
|
+
export declare function findMostRecentSession(sessionDir: string, cwd?: string): string | null;
|
|
13
14
|
//# sourceMappingURL=jsonl-helpers.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jsonl-helpers.d.ts","sourceRoot":"","sources":["../../../src/core/session/jsonl-helpers.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"jsonl-helpers.d.ts","sourceRoot":"","sources":["../../../src/core/session/jsonl-helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAIN,iBAAiB,EAEjB,MAAM,iCAAiC,CAAC;AACzC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAE5C,eAAO,MAAM,iBAAiB,mBAA0B,CAAC;AAEzD;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM,CAE3E;AAED,4BAA4B;AAC5B,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,GAAG,SAAS,EAAE,CAEjE;AAED,wBAAgB,wBAAwB,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM,CAE/E;AAED,4BAA4B;AAC5B,wBAAgB,qBAAqB,CAAC,UAAU,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAErF","sourcesContent":["import {\n\tfindMostRecent,\n\tgetDefaultSessionDir as getDefaultJsonlSessionDir,\n\tgetDefaultSessionDirPath as getDefaultJsonlSessionDirPath,\n\tJsonlSessionStore,\n\tload,\n} from \"./stores/jsonl-session-store.ts\";\nimport type { FileEntry } from \"./types.ts\";\n\nexport const jsonlSessionStore = new JsonlSessionStore();\n\n/**\n * Compute the default session directory for a cwd.\n * Encodes cwd into a safe directory name under ~/.pi/agent/sessions/.\n */\nexport function getDefaultSessionDir(cwd: string, agentDir?: string): string {\n\treturn getDefaultJsonlSessionDir(cwd, agentDir);\n}\n\n/** Exported for testing. */\nexport function loadEntriesFromFile(filePath: string): FileEntry[] {\n\treturn load(filePath);\n}\n\nexport function getDefaultSessionDirPath(cwd: string, agentDir?: string): string {\n\treturn getDefaultJsonlSessionDirPath(cwd, agentDir);\n}\n\n/** Exported for testing. */\nexport function findMostRecentSession(sessionDir: string, cwd?: string): string | null {\n\treturn findMostRecent(sessionDir, cwd);\n}\n"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { findMostRecent, getDefaultSessionDir as getDefaultJsonlSessionDir, JsonlSessionStore, load, } from "./stores/jsonl-session-store.js";
|
|
1
|
+
import { findMostRecent, getDefaultSessionDir as getDefaultJsonlSessionDir, getDefaultSessionDirPath as getDefaultJsonlSessionDirPath, JsonlSessionStore, load, } from "./stores/jsonl-session-store.js";
|
|
2
2
|
export const jsonlSessionStore = new JsonlSessionStore();
|
|
3
3
|
/**
|
|
4
4
|
* Compute the default session directory for a cwd.
|
|
@@ -11,8 +11,11 @@ export function getDefaultSessionDir(cwd, agentDir) {
|
|
|
11
11
|
export function loadEntriesFromFile(filePath) {
|
|
12
12
|
return load(filePath);
|
|
13
13
|
}
|
|
14
|
+
export function getDefaultSessionDirPath(cwd, agentDir) {
|
|
15
|
+
return getDefaultJsonlSessionDirPath(cwd, agentDir);
|
|
16
|
+
}
|
|
14
17
|
/** Exported for testing. */
|
|
15
|
-
export function findMostRecentSession(sessionDir) {
|
|
16
|
-
return findMostRecent(sessionDir);
|
|
18
|
+
export function findMostRecentSession(sessionDir, cwd) {
|
|
19
|
+
return findMostRecent(sessionDir, cwd);
|
|
17
20
|
}
|
|
18
21
|
//# sourceMappingURL=jsonl-helpers.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jsonl-helpers.js","sourceRoot":"","sources":["../../../src/core/session/jsonl-helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,cAAc,EACd,oBAAoB,IAAI,yBAAyB,EACjD,iBAAiB,EACjB,IAAI,GACJ,MAAM,iCAAiC,CAAC;AAGzC,MAAM,CAAC,MAAM,iBAAiB,GAAG,IAAI,iBAAiB,EAAE,CAAC;AAEzD;;;GAGG;AACH,MAAM,UAAU,oBAAoB,CAAC,GAAW,EAAE,QAAiB,EAAU;IAC5E,OAAO,yBAAyB,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC;AAAA,CAChD;AAED,4BAA4B;AAC5B,MAAM,UAAU,mBAAmB,CAAC,QAAgB,EAAe;IAClE,OAAO,IAAI,CAAC,QAAQ,CAAC,CAAC;AAAA,CACtB;AAED,4BAA4B;AAC5B,MAAM,UAAU,qBAAqB,CAAC,UAAkB,EAAiB;
|
|
1
|
+
{"version":3,"file":"jsonl-helpers.js","sourceRoot":"","sources":["../../../src/core/session/jsonl-helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,cAAc,EACd,oBAAoB,IAAI,yBAAyB,EACjD,wBAAwB,IAAI,6BAA6B,EACzD,iBAAiB,EACjB,IAAI,GACJ,MAAM,iCAAiC,CAAC;AAGzC,MAAM,CAAC,MAAM,iBAAiB,GAAG,IAAI,iBAAiB,EAAE,CAAC;AAEzD;;;GAGG;AACH,MAAM,UAAU,oBAAoB,CAAC,GAAW,EAAE,QAAiB,EAAU;IAC5E,OAAO,yBAAyB,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC;AAAA,CAChD;AAED,4BAA4B;AAC5B,MAAM,UAAU,mBAAmB,CAAC,QAAgB,EAAe;IAClE,OAAO,IAAI,CAAC,QAAQ,CAAC,CAAC;AAAA,CACtB;AAED,MAAM,UAAU,wBAAwB,CAAC,GAAW,EAAE,QAAiB,EAAU;IAChF,OAAO,6BAA6B,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC;AAAA,CACpD;AAED,4BAA4B;AAC5B,MAAM,UAAU,qBAAqB,CAAC,UAAkB,EAAE,GAAY,EAAiB;IACtF,OAAO,cAAc,CAAC,UAAU,EAAE,GAAG,CAAC,CAAC;AAAA,CACvC","sourcesContent":["import {\n\tfindMostRecent,\n\tgetDefaultSessionDir as getDefaultJsonlSessionDir,\n\tgetDefaultSessionDirPath as getDefaultJsonlSessionDirPath,\n\tJsonlSessionStore,\n\tload,\n} from \"./stores/jsonl-session-store.ts\";\nimport type { FileEntry } from \"./types.ts\";\n\nexport const jsonlSessionStore = new JsonlSessionStore();\n\n/**\n * Compute the default session directory for a cwd.\n * Encodes cwd into a safe directory name under ~/.pi/agent/sessions/.\n */\nexport function getDefaultSessionDir(cwd: string, agentDir?: string): string {\n\treturn getDefaultJsonlSessionDir(cwd, agentDir);\n}\n\n/** Exported for testing. */\nexport function loadEntriesFromFile(filePath: string): FileEntry[] {\n\treturn load(filePath);\n}\n\nexport function getDefaultSessionDirPath(cwd: string, agentDir?: string): string {\n\treturn getDefaultJsonlSessionDirPath(cwd, agentDir);\n}\n\n/** Exported for testing. */\nexport function findMostRecentSession(sessionDir: string, cwd?: string): string | null {\n\treturn findMostRecent(sessionDir, cwd);\n}\n"]}
|
|
@@ -13,6 +13,7 @@ export declare class LocalSessionManager implements SessionManager {
|
|
|
13
13
|
create(options?: NewSessionOptions): LocalSession;
|
|
14
14
|
openReference(reference: string, options?: OpenSessionOptions): LocalSession;
|
|
15
15
|
continueRecent(): LocalSession;
|
|
16
|
+
usesDefaultSessionDir(): boolean;
|
|
16
17
|
forkFrom(reference: string): LocalSession;
|
|
17
18
|
forkSession(source: Session, targetLeafId: string | null): LocalSession;
|
|
18
19
|
importJsonl(inputPath: string, options?: OpenSessionOptions): LocalSession;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"local-session-manager.d.ts","sourceRoot":"","sources":["../../../src/core/session/local-session-manager.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"local-session-manager.d.ts","sourceRoot":"","sources":["../../../src/core/session/local-session-manager.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,KAAK,EAAE,kBAAkB,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAS/E,OAAO,KAAK,EAAE,iBAAiB,EAAiB,WAAW,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAErG,MAAM,WAAW,0BAA0B;IAC1C,GAAG,EAAE,MAAM,CAAC;IACZ,UAAU,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,qBAAa,mBAAoB,YAAW,cAAc;IACzD,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAS;IAC7B,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAS;IAErC,YAAY,OAAO,EAAE,0BAA0B,EAG9C;IAED,MAAM,CAAC,OAAO,CAAC,EAAE,iBAAiB,GAAG,YAAY,CAOhD;IAED,aAAa,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,kBAAkB,GAAG,YAAY,CAM3E;IAED,cAAc,IAAI,YAAY,CAQ7B;IAED,qBAAqB,IAAI,OAAO,CAE/B;IAED,QAAQ,CAAC,SAAS,EAAE,MAAM,GAAG,YAAY,CA2BxC;IAED,WAAW,CAAC,MAAM,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,GAAG,IAAI,GAAG,YAAY,CActE;IAED,WAAW,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,kBAAkB,GAAG,YAAY,CAgBzE;IAEK,IAAI,CAAC,UAAU,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC,CASnE;IAEK,OAAO,CAAC,UAAU,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC,CAMtE;CACD","sourcesContent":["import { copyFileSync, existsSync, mkdirSync } from \"node:fs\";\nimport { basename, join, resolve } from \"node:path\";\nimport { CURRENT_SESSION_VERSION } from \"./constants.ts\";\nimport { createSessionId } from \"./ids.ts\";\nimport {\n\tfindMostRecentSession,\n\tgetDefaultSessionDir,\n\tgetDefaultSessionDirPath,\n\tloadEntriesFromFile,\n} from \"./jsonl-helpers.ts\";\nimport { LocalSession } from \"./local-session.ts\";\nimport type { Session } from \"./session.ts\";\nimport type { OpenSessionOptions, SessionManager } from \"./session-manager.ts\";\nimport {\n\tensureDir,\n\tforkSession as forkJsonlSession,\n\tgetSessionDirForReference,\n\tgetSessionsRoot,\n\tlistAll as listAllJsonlSessions,\n\tlist as listJsonlSessions,\n} from \"./stores/jsonl-session-store.ts\";\nimport type { NewSessionOptions, SessionHeader, SessionInfo, SessionListProgress } from \"./types.ts\";\n\nexport interface LocalSessionManagerOptions {\n\tcwd: string;\n\tsessionDir?: string;\n}\n\nexport class LocalSessionManager implements SessionManager {\n\tprivate readonly cwd: string;\n\tprivate readonly sessionDir?: string;\n\n\tconstructor(options: LocalSessionManagerOptions) {\n\t\tthis.cwd = options.cwd;\n\t\tthis.sessionDir = options.sessionDir;\n\t}\n\n\tcreate(options?: NewSessionOptions): LocalSession {\n\t\tconst dir = this.sessionDir ?? getDefaultSessionDir(this.cwd);\n\t\tconst session = new LocalSession(this.cwd, dir, undefined, this);\n\t\tif (options?.id || options?.parentSession) {\n\t\t\tsession.newSession(options);\n\t\t}\n\t\treturn session;\n\t}\n\n\topenReference(reference: string, options?: OpenSessionOptions): LocalSession {\n\t\tconst entries = loadEntriesFromFile(reference);\n\t\tconst header = entries.find((entry) => entry.type === \"session\") as SessionHeader | undefined;\n\t\tconst cwd = options?.cwdOverride ?? header?.cwd ?? this.cwd;\n\t\tconst dir = this.sessionDir ?? getSessionDirForReference(reference);\n\t\treturn new LocalSession(cwd, dir, reference, this);\n\t}\n\n\tcontinueRecent(): LocalSession {\n\t\tconst dir = this.sessionDir ?? getDefaultSessionDir(this.cwd);\n\t\tconst filterCwd = this.sessionDir !== undefined && dir !== getDefaultSessionDirPath(this.cwd);\n\t\tconst mostRecent = findMostRecentSession(dir, filterCwd ? this.cwd : undefined);\n\t\tif (mostRecent) {\n\t\t\treturn new LocalSession(this.cwd, dir, mostRecent, this);\n\t\t}\n\t\treturn new LocalSession(this.cwd, dir, undefined, this);\n\t}\n\n\tusesDefaultSessionDir(): boolean {\n\t\treturn (this.sessionDir ?? getDefaultSessionDir(this.cwd)) === getDefaultSessionDirPath(this.cwd);\n\t}\n\n\tforkFrom(reference: string): LocalSession {\n\t\tconst sourceEntries = loadEntriesFromFile(reference);\n\t\tif (sourceEntries.length === 0) {\n\t\t\tthrow new Error(`Cannot fork: source session is empty or invalid: ${reference}`);\n\t\t}\n\n\t\tconst sourceHeader = sourceEntries.find((entry) => entry.type === \"session\") as SessionHeader | undefined;\n\t\tif (!sourceHeader) {\n\t\t\tthrow new Error(`Cannot fork: source session has no header: ${reference}`);\n\t\t}\n\n\t\tconst dir = this.sessionDir ?? getDefaultSessionDir(this.cwd);\n\t\tensureDir(dir);\n\n\t\tconst newSessionId = createSessionId();\n\t\tconst timestamp = new Date().toISOString();\n\t\tconst newHeader: SessionHeader = {\n\t\t\ttype: \"session\",\n\t\t\tversion: CURRENT_SESSION_VERSION,\n\t\t\tid: newSessionId,\n\t\t\ttimestamp,\n\t\t\tcwd: this.cwd,\n\t\t\tparentSession: reference,\n\t\t};\n\t\tconst newSessionReference = forkJsonlSession(dir, newHeader, sourceEntries);\n\n\t\treturn new LocalSession(this.cwd, dir, newSessionReference, this);\n\t}\n\n\tforkSession(source: Session, targetLeafId: string | null): LocalSession {\n\t\tconst parentSession = source.getSessionReference();\n\t\tif (!targetLeafId) {\n\t\t\tconst session = this.create();\n\t\t\tsession.newSession({ parentSession });\n\t\t\treturn session;\n\t\t}\n\n\t\tconst branchSource = parentSession ? this.openReference(parentSession) : source;\n\t\tconst forkedReference = branchSource.createBranchedSession(targetLeafId);\n\t\tif (!forkedReference) {\n\t\t\tthrow new Error(\"Failed to create forked session\");\n\t\t}\n\t\treturn this.openReference(forkedReference);\n\t}\n\n\timportJsonl(inputPath: string, options?: OpenSessionOptions): LocalSession {\n\t\tconst resolvedPath = resolve(inputPath);\n\t\tif (!existsSync(resolvedPath)) {\n\t\t\tthrow new Error(`File not found: ${resolvedPath}`);\n\t\t}\n\n\t\tconst dir = this.sessionDir ?? getDefaultSessionDir(this.cwd);\n\t\tif (!existsSync(dir)) {\n\t\t\tmkdirSync(dir, { recursive: true });\n\t\t}\n\n\t\tconst destinationPath = join(dir, basename(resolvedPath));\n\t\tif (resolve(destinationPath) !== resolvedPath) {\n\t\t\tcopyFileSync(resolvedPath, destinationPath);\n\t\t}\n\t\treturn this.openReference(destinationPath, options);\n\t}\n\n\tasync list(onProgress?: SessionListProgress): Promise<SessionInfo[]> {\n\t\tconst dir = this.sessionDir ?? getDefaultSessionDir(this.cwd);\n\t\tconst filterCwd = this.sessionDir !== undefined && dir !== getDefaultSessionDirPath(this.cwd);\n\t\tconst resolvedCwd = resolve(this.cwd);\n\t\tconst sessions = (await listJsonlSessions(dir, onProgress)).filter(\n\t\t\t(session) => !filterCwd || (session.cwd !== undefined && resolve(session.cwd) === resolvedCwd),\n\t\t);\n\t\tsessions.sort((a, b) => b.modified.getTime() - a.modified.getTime());\n\t\treturn sessions;\n\t}\n\n\tasync listAll(onProgress?: SessionListProgress): Promise<SessionInfo[]> {\n\t\tconst sessions = this.sessionDir\n\t\t\t? await listJsonlSessions(this.sessionDir, onProgress)\n\t\t\t: await listAllJsonlSessions(getSessionsRoot(), onProgress);\n\t\tsessions.sort((a, b) => b.modified.getTime() - a.modified.getTime());\n\t\treturn sessions;\n\t}\n}\n"]}
|
|
@@ -2,7 +2,7 @@ import { copyFileSync, existsSync, mkdirSync } from "node:fs";
|
|
|
2
2
|
import { basename, join, resolve } from "node:path";
|
|
3
3
|
import { CURRENT_SESSION_VERSION } from "./constants.js";
|
|
4
4
|
import { createSessionId } from "./ids.js";
|
|
5
|
-
import { findMostRecentSession, getDefaultSessionDir, loadEntriesFromFile } from "./jsonl-helpers.js";
|
|
5
|
+
import { findMostRecentSession, getDefaultSessionDir, getDefaultSessionDirPath, loadEntriesFromFile, } from "./jsonl-helpers.js";
|
|
6
6
|
import { LocalSession } from "./local-session.js";
|
|
7
7
|
import { ensureDir, forkSession as forkJsonlSession, getSessionDirForReference, getSessionsRoot, listAll as listAllJsonlSessions, list as listJsonlSessions, } from "./stores/jsonl-session-store.js";
|
|
8
8
|
export class LocalSessionManager {
|
|
@@ -29,12 +29,16 @@ export class LocalSessionManager {
|
|
|
29
29
|
}
|
|
30
30
|
continueRecent() {
|
|
31
31
|
const dir = this.sessionDir ?? getDefaultSessionDir(this.cwd);
|
|
32
|
-
const
|
|
32
|
+
const filterCwd = this.sessionDir !== undefined && dir !== getDefaultSessionDirPath(this.cwd);
|
|
33
|
+
const mostRecent = findMostRecentSession(dir, filterCwd ? this.cwd : undefined);
|
|
33
34
|
if (mostRecent) {
|
|
34
35
|
return new LocalSession(this.cwd, dir, mostRecent, this);
|
|
35
36
|
}
|
|
36
37
|
return new LocalSession(this.cwd, dir, undefined, this);
|
|
37
38
|
}
|
|
39
|
+
usesDefaultSessionDir() {
|
|
40
|
+
return (this.sessionDir ?? getDefaultSessionDir(this.cwd)) === getDefaultSessionDirPath(this.cwd);
|
|
41
|
+
}
|
|
38
42
|
forkFrom(reference) {
|
|
39
43
|
const sourceEntries = loadEntriesFromFile(reference);
|
|
40
44
|
if (sourceEntries.length === 0) {
|
|
@@ -90,12 +94,16 @@ export class LocalSessionManager {
|
|
|
90
94
|
}
|
|
91
95
|
async list(onProgress) {
|
|
92
96
|
const dir = this.sessionDir ?? getDefaultSessionDir(this.cwd);
|
|
93
|
-
const
|
|
97
|
+
const filterCwd = this.sessionDir !== undefined && dir !== getDefaultSessionDirPath(this.cwd);
|
|
98
|
+
const resolvedCwd = resolve(this.cwd);
|
|
99
|
+
const sessions = (await listJsonlSessions(dir, onProgress)).filter((session) => !filterCwd || (session.cwd !== undefined && resolve(session.cwd) === resolvedCwd));
|
|
94
100
|
sessions.sort((a, b) => b.modified.getTime() - a.modified.getTime());
|
|
95
101
|
return sessions;
|
|
96
102
|
}
|
|
97
103
|
async listAll(onProgress) {
|
|
98
|
-
const sessions =
|
|
104
|
+
const sessions = this.sessionDir
|
|
105
|
+
? await listJsonlSessions(this.sessionDir, onProgress)
|
|
106
|
+
: await listAllJsonlSessions(getSessionsRoot(), onProgress);
|
|
99
107
|
sessions.sort((a, b) => b.modified.getTime() - a.modified.getTime());
|
|
100
108
|
return sessions;
|
|
101
109
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version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AC,OAAO,EAAE,CAAC,CAAC;QACrE,OAAO,QAAQ,CAAC;IAAA,CAChB;IAED,KAAK,CAAC,OAAO,CAAC,UAAgC,EAA0B;QACvE,MAAM,QAAQ,GAAG,MAAM,oBAAoB,CAAC,eAAe,EAAE,EAAE,UAAU,CAAC,CAAC;QAC3E,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAC;QACrE,OAAO,QAAQ,CAAC;IAAA,CAChB;CACD","sourcesContent":["import { copyFileSync, existsSync, mkdirSync } from \"node:fs\";\nimport { basename, join, resolve } from \"node:path\";\nimport { CURRENT_SESSION_VERSION } from \"./constants.ts\";\nimport { createSessionId } from \"./ids.ts\";\nimport { findMostRecentSession, getDefaultSessionDir, loadEntriesFromFile } from \"./jsonl-helpers.ts\";\nimport { LocalSession } from \"./local-session.ts\";\nimport type { Session } from \"./session.ts\";\nimport type { OpenSessionOptions, SessionManager } from \"./session-manager.ts\";\nimport {\n\tensureDir,\n\tforkSession as forkJsonlSession,\n\tgetSessionDirForReference,\n\tgetSessionsRoot,\n\tlistAll as listAllJsonlSessions,\n\tlist as listJsonlSessions,\n} from \"./stores/jsonl-session-store.ts\";\nimport type { NewSessionOptions, SessionHeader, SessionInfo, SessionListProgress } from \"./types.ts\";\n\nexport interface LocalSessionManagerOptions {\n\tcwd: string;\n\tsessionDir?: string;\n}\n\nexport class LocalSessionManager implements SessionManager {\n\tprivate readonly cwd: string;\n\tprivate readonly sessionDir?: string;\n\n\tconstructor(options: LocalSessionManagerOptions) {\n\t\tthis.cwd = options.cwd;\n\t\tthis.sessionDir = options.sessionDir;\n\t}\n\n\tcreate(options?: NewSessionOptions): LocalSession {\n\t\tconst dir = this.sessionDir ?? getDefaultSessionDir(this.cwd);\n\t\tconst session = new LocalSession(this.cwd, dir, undefined, this);\n\t\tif (options?.id || options?.parentSession) {\n\t\t\tsession.newSession(options);\n\t\t}\n\t\treturn session;\n\t}\n\n\topenReference(reference: string, options?: OpenSessionOptions): LocalSession {\n\t\tconst entries = loadEntriesFromFile(reference);\n\t\tconst header = entries.find((entry) => entry.type === \"session\") as SessionHeader | undefined;\n\t\tconst cwd = options?.cwdOverride ?? header?.cwd ?? this.cwd;\n\t\tconst dir = this.sessionDir ?? getSessionDirForReference(reference);\n\t\treturn new LocalSession(cwd, dir, reference, this);\n\t}\n\n\tcontinueRecent(): LocalSession {\n\t\tconst dir = this.sessionDir ?? getDefaultSessionDir(this.cwd);\n\t\tconst mostRecent = findMostRecentSession(dir);\n\t\tif (mostRecent) {\n\t\t\treturn new LocalSession(this.cwd, dir, mostRecent, this);\n\t\t}\n\t\treturn new LocalSession(this.cwd, dir, undefined, this);\n\t}\n\n\tforkFrom(reference: string): LocalSession {\n\t\tconst sourceEntries = loadEntriesFromFile(reference);\n\t\tif (sourceEntries.length === 0) {\n\t\t\tthrow new Error(`Cannot fork: source session is empty or invalid: ${reference}`);\n\t\t}\n\n\t\tconst sourceHeader = sourceEntries.find((entry) => entry.type === \"session\") as SessionHeader | undefined;\n\t\tif (!sourceHeader) {\n\t\t\tthrow new Error(`Cannot fork: source session has no header: ${reference}`);\n\t\t}\n\n\t\tconst dir = this.sessionDir ?? getDefaultSessionDir(this.cwd);\n\t\tensureDir(dir);\n\n\t\tconst newSessionId = createSessionId();\n\t\tconst timestamp = new Date().toISOString();\n\t\tconst newHeader: SessionHeader = {\n\t\t\ttype: \"session\",\n\t\t\tversion: CURRENT_SESSION_VERSION,\n\t\t\tid: newSessionId,\n\t\t\ttimestamp,\n\t\t\tcwd: this.cwd,\n\t\t\tparentSession: reference,\n\t\t};\n\t\tconst newSessionReference = forkJsonlSession(dir, newHeader, sourceEntries);\n\n\t\treturn new LocalSession(this.cwd, dir, newSessionReference, this);\n\t}\n\n\tforkSession(source: Session, targetLeafId: string | null): LocalSession {\n\t\tconst parentSession = source.getSessionReference();\n\t\tif (!targetLeafId) {\n\t\t\tconst session = this.create();\n\t\t\tsession.newSession({ parentSession });\n\t\t\treturn session;\n\t\t}\n\n\t\tconst branchSource = parentSession ? this.openReference(parentSession) : source;\n\t\tconst forkedReference = branchSource.createBranchedSession(targetLeafId);\n\t\tif (!forkedReference) {\n\t\t\tthrow new Error(\"Failed to create forked session\");\n\t\t}\n\t\treturn this.openReference(forkedReference);\n\t}\n\n\timportJsonl(inputPath: string, options?: OpenSessionOptions): LocalSession {\n\t\tconst resolvedPath = resolve(inputPath);\n\t\tif (!existsSync(resolvedPath)) {\n\t\t\tthrow new Error(`File not found: ${resolvedPath}`);\n\t\t}\n\n\t\tconst dir = this.sessionDir ?? getDefaultSessionDir(this.cwd);\n\t\tif (!existsSync(dir)) {\n\t\t\tmkdirSync(dir, { recursive: true });\n\t\t}\n\n\t\tconst destinationPath = join(dir, basename(resolvedPath));\n\t\tif (resolve(destinationPath) !== resolvedPath) {\n\t\t\tcopyFileSync(resolvedPath, destinationPath);\n\t\t}\n\t\treturn this.openReference(destinationPath, options);\n\t}\n\n\tasync list(onProgress?: SessionListProgress): Promise<SessionInfo[]> {\n\t\tconst dir = this.sessionDir ?? getDefaultSessionDir(this.cwd);\n\t\tconst sessions = await listJsonlSessions(dir, onProgress);\n\t\tsessions.sort((a, b) => b.modified.getTime() - a.modified.getTime());\n\t\treturn sessions;\n\t}\n\n\tasync listAll(onProgress?: SessionListProgress): Promise<SessionInfo[]> {\n\t\tconst sessions = await listAllJsonlSessions(getSessionsRoot(), onProgress);\n\t\tsessions.sort((a, b) => b.modified.getTime() - a.modified.getTime());\n\t\treturn sessions;\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"local-session-manager.js","sourceRoot":"","sources":["../../../src/core/session/local-session-manager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAC9D,OAAO,EAAE,QAAQ,E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S,GAAG,IAAI,CAAC,UAAU,KAAK,SAAS,IAAI,GAAG,KAAK,wBAAwB,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAC9F,MAAM,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACtC,MAAM,QAAQ,GAAG,CAAC,MAAM,iBAAiB,CAAC,GAAG,EAAE,UAAU,CAAC,CAAC,CAAC,MAAM,CACjE,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,SAAS,IAAI,CAAC,OAAO,CAAC,GAAG,KAAK,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,WAAW,CAAC,CAC9F,CAAC;QACF,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAC;QACrE,OAAO,QAAQ,CAAC;IAAA,CAChB;IAED,KAAK,CAAC,OAAO,CAAC,UAAgC,EAA0B;QACvE,MAAM,QAAQ,GAAG,IAAI,CAAC,UAAU;YAC/B,CAAC,CAAC,MAAM,iBAAiB,CAAC,IAAI,CAAC,UAAU,EAAE,UAAU,CAAC;YACtD,CAAC,CAAC,MAAM,oBAAoB,CAAC,eAAe,EAAE,EAAE,UAAU,CAAC,CAAC;QAC7D,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAC;QACrE,OAAO,QAAQ,CAAC;IAAA,CAChB;CACD","sourcesContent":["import { copyFileSync, existsSync, mkdirSync } from \"node:fs\";\nimport { basename, join, resolve } from \"node:path\";\nimport { CURRENT_SESSION_VERSION } from \"./constants.ts\";\nimport { createSessionId } from \"./ids.ts\";\nimport {\n\tfindMostRecentSession,\n\tgetDefaultSessionDir,\n\tgetDefaultSessionDirPath,\n\tloadEntriesFromFile,\n} from \"./jsonl-helpers.ts\";\nimport { LocalSession } from \"./local-session.ts\";\nimport type { Session } from \"./session.ts\";\nimport type { OpenSessionOptions, SessionManager } from \"./session-manager.ts\";\nimport {\n\tensureDir,\n\tforkSession as forkJsonlSession,\n\tgetSessionDirForReference,\n\tgetSessionsRoot,\n\tlistAll as listAllJsonlSessions,\n\tlist as listJsonlSessions,\n} from \"./stores/jsonl-session-store.ts\";\nimport type { NewSessionOptions, SessionHeader, SessionInfo, SessionListProgress } from \"./types.ts\";\n\nexport interface LocalSessionManagerOptions {\n\tcwd: string;\n\tsessionDir?: string;\n}\n\nexport class LocalSessionManager implements SessionManager {\n\tprivate readonly cwd: string;\n\tprivate readonly sessionDir?: string;\n\n\tconstructor(options: LocalSessionManagerOptions) {\n\t\tthis.cwd = options.cwd;\n\t\tthis.sessionDir = options.sessionDir;\n\t}\n\n\tcreate(options?: NewSessionOptions): LocalSession {\n\t\tconst dir = this.sessionDir ?? getDefaultSessionDir(this.cwd);\n\t\tconst session = new LocalSession(this.cwd, dir, undefined, this);\n\t\tif (options?.id || options?.parentSession) {\n\t\t\tsession.newSession(options);\n\t\t}\n\t\treturn session;\n\t}\n\n\topenReference(reference: string, options?: OpenSessionOptions): LocalSession {\n\t\tconst entries = loadEntriesFromFile(reference);\n\t\tconst header = entries.find((entry) => entry.type === \"session\") as SessionHeader | undefined;\n\t\tconst cwd = options?.cwdOverride ?? header?.cwd ?? this.cwd;\n\t\tconst dir = this.sessionDir ?? getSessionDirForReference(reference);\n\t\treturn new LocalSession(cwd, dir, reference, this);\n\t}\n\n\tcontinueRecent(): LocalSession {\n\t\tconst dir = this.sessionDir ?? getDefaultSessionDir(this.cwd);\n\t\tconst filterCwd = this.sessionDir !== undefined && dir !== getDefaultSessionDirPath(this.cwd);\n\t\tconst mostRecent = findMostRecentSession(dir, filterCwd ? this.cwd : undefined);\n\t\tif (mostRecent) {\n\t\t\treturn new LocalSession(this.cwd, dir, mostRecent, this);\n\t\t}\n\t\treturn new LocalSession(this.cwd, dir, undefined, this);\n\t}\n\n\tusesDefaultSessionDir(): boolean {\n\t\treturn (this.sessionDir ?? getDefaultSessionDir(this.cwd)) === getDefaultSessionDirPath(this.cwd);\n\t}\n\n\tforkFrom(reference: string): LocalSession {\n\t\tconst sourceEntries = loadEntriesFromFile(reference);\n\t\tif (sourceEntries.length === 0) {\n\t\t\tthrow new Error(`Cannot fork: source session is empty or invalid: ${reference}`);\n\t\t}\n\n\t\tconst sourceHeader = sourceEntries.find((entry) => entry.type === \"session\") as SessionHeader | undefined;\n\t\tif (!sourceHeader) {\n\t\t\tthrow new Error(`Cannot fork: source session has no header: ${reference}`);\n\t\t}\n\n\t\tconst dir = this.sessionDir ?? getDefaultSessionDir(this.cwd);\n\t\tensureDir(dir);\n\n\t\tconst newSessionId = createSessionId();\n\t\tconst timestamp = new Date().toISOString();\n\t\tconst newHeader: SessionHeader = {\n\t\t\ttype: \"session\",\n\t\t\tversion: CURRENT_SESSION_VERSION,\n\t\t\tid: newSessionId,\n\t\t\ttimestamp,\n\t\t\tcwd: this.cwd,\n\t\t\tparentSession: reference,\n\t\t};\n\t\tconst newSessionReference = forkJsonlSession(dir, newHeader, sourceEntries);\n\n\t\treturn new LocalSession(this.cwd, dir, newSessionReference, this);\n\t}\n\n\tforkSession(source: Session, targetLeafId: string | null): LocalSession {\n\t\tconst parentSession = source.getSessionReference();\n\t\tif (!targetLeafId) {\n\t\t\tconst session = this.create();\n\t\t\tsession.newSession({ parentSession });\n\t\t\treturn session;\n\t\t}\n\n\t\tconst branchSource = parentSession ? this.openReference(parentSession) : source;\n\t\tconst forkedReference = branchSource.createBranchedSession(targetLeafId);\n\t\tif (!forkedReference) {\n\t\t\tthrow new Error(\"Failed to create forked session\");\n\t\t}\n\t\treturn this.openReference(forkedReference);\n\t}\n\n\timportJsonl(inputPath: string, options?: OpenSessionOptions): LocalSession {\n\t\tconst resolvedPath = resolve(inputPath);\n\t\tif (!existsSync(resolvedPath)) {\n\t\t\tthrow new Error(`File not found: ${resolvedPath}`);\n\t\t}\n\n\t\tconst dir = this.sessionDir ?? getDefaultSessionDir(this.cwd);\n\t\tif (!existsSync(dir)) {\n\t\t\tmkdirSync(dir, { recursive: true });\n\t\t}\n\n\t\tconst destinationPath = join(dir, basename(resolvedPath));\n\t\tif (resolve(destinationPath) !== resolvedPath) {\n\t\t\tcopyFileSync(resolvedPath, destinationPath);\n\t\t}\n\t\treturn this.openReference(destinationPath, options);\n\t}\n\n\tasync list(onProgress?: SessionListProgress): Promise<SessionInfo[]> {\n\t\tconst dir = this.sessionDir ?? getDefaultSessionDir(this.cwd);\n\t\tconst filterCwd = this.sessionDir !== undefined && dir !== getDefaultSessionDirPath(this.cwd);\n\t\tconst resolvedCwd = resolve(this.cwd);\n\t\tconst sessions = (await listJsonlSessions(dir, onProgress)).filter(\n\t\t\t(session) => !filterCwd || (session.cwd !== undefined && resolve(session.cwd) === resolvedCwd),\n\t\t);\n\t\tsessions.sort((a, b) => b.modified.getTime() - a.modified.getTime());\n\t\treturn sessions;\n\t}\n\n\tasync listAll(onProgress?: SessionListProgress): Promise<SessionInfo[]> {\n\t\tconst sessions = this.sessionDir\n\t\t\t? await listJsonlSessions(this.sessionDir, onProgress)\n\t\t\t: await listAllJsonlSessions(getSessionsRoot(), onProgress);\n\t\tsessions.sort((a, b) => b.modified.getTime() - a.modified.getTime());\n\t\treturn sessions;\n\t}\n}\n"]}
|
|
@@ -13,5 +13,6 @@ export interface SessionManager {
|
|
|
13
13
|
importJsonl(inputPath: string, options?: OpenSessionOptions): SessionResult;
|
|
14
14
|
list(onProgress?: SessionListProgress): Promise<SessionInfo[]>;
|
|
15
15
|
listAll(onProgress?: SessionListProgress): Promise<SessionInfo[]>;
|
|
16
|
+
usesDefaultSessionDir?(): boolean;
|
|
16
17
|
}
|
|
17
18
|
//# sourceMappingURL=session-manager.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session-manager.d.ts","sourceRoot":"","sources":["../../../src/core/session/session-manager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,KAAK,EAAE,iBAAiB,EAAE,WAAW,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAEtF,MAAM,MAAM,aAAa,GAAG,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;AAEvD,MAAM,WAAW,kBAAkB;IAClC,WAAW,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,cAAc;IAC9B,MAAM,CAAC,OAAO,CAAC,EAAE,iBAAiB,GAAG,aAAa,CAAC;IACnD,aAAa,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,kBAAkB,GAAG,aAAa,CAAC;IAC9E,cAAc,IAAI,aAAa,CAAC;IAChC,QAAQ,CAAC,SAAS,EAAE,MAAM,GAAG,aAAa,CAAC;IAC3C,WAAW,CAAC,MAAM,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,GAAG,IAAI,GAAG,aAAa,CAAC;IACzE,WAAW,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,kBAAkB,GAAG,aAAa,CAAC;IAC5E,IAAI,CAAC,UAAU,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC,CAAC;IAC/D,OAAO,CAAC,UAAU,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"session-manager.d.ts","sourceRoot":"","sources":["../../../src/core/session/session-manager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,KAAK,EAAE,iBAAiB,EAAE,WAAW,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAEtF,MAAM,MAAM,aAAa,GAAG,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;AAEvD,MAAM,WAAW,kBAAkB;IAClC,WAAW,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,cAAc;IAC9B,MAAM,CAAC,OAAO,CAAC,EAAE,iBAAiB,GAAG,aAAa,CAAC;IACnD,aAAa,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,kBAAkB,GAAG,aAAa,CAAC;IAC9E,cAAc,IAAI,aAAa,CAAC;IAChC,QAAQ,CAAC,SAAS,EAAE,MAAM,GAAG,aAAa,CAAC;IAC3C,WAAW,CAAC,MAAM,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,GAAG,IAAI,GAAG,aAAa,CAAC;IACzE,WAAW,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,kBAAkB,GAAG,aAAa,CAAC;IAC5E,IAAI,CAAC,UAAU,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC,CAAC;IAC/D,OAAO,CAAC,UAAU,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC,CAAC;IAClE,qBAAqB,CAAC,IAAI,OAAO,CAAC;CAClC","sourcesContent":["import type { Session } from \"./session.ts\";\nimport type { NewSessionOptions, SessionInfo, SessionListProgress } from \"./types.ts\";\n\nexport type SessionResult = Session | Promise<Session>;\n\nexport interface OpenSessionOptions {\n\tcwdOverride?: string;\n}\n\nexport interface SessionManager {\n\tcreate(options?: NewSessionOptions): SessionResult;\n\topenReference(reference: string, options?: OpenSessionOptions): SessionResult;\n\tcontinueRecent(): SessionResult;\n\tforkFrom(reference: string): SessionResult;\n\tforkSession(source: Session, targetLeafId: string | null): SessionResult;\n\timportJsonl(inputPath: string, options?: OpenSessionOptions): SessionResult;\n\tlist(onProgress?: SessionListProgress): Promise<SessionInfo[]>;\n\tlistAll(onProgress?: SessionListProgress): Promise<SessionInfo[]>;\n\tusesDefaultSessionDir?(): boolean;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session-manager.js","sourceRoot":"","sources":["../../../src/core/session/session-manager.ts"],"names":[],"mappings":"","sourcesContent":["import type { Session } from \"./session.ts\";\nimport type { NewSessionOptions, SessionInfo, SessionListProgress } from \"./types.ts\";\n\nexport type SessionResult = Session | Promise<Session>;\n\nexport interface OpenSessionOptions {\n\tcwdOverride?: string;\n}\n\nexport interface SessionManager {\n\tcreate(options?: NewSessionOptions): SessionResult;\n\topenReference(reference: string, options?: OpenSessionOptions): SessionResult;\n\tcontinueRecent(): SessionResult;\n\tforkFrom(reference: string): SessionResult;\n\tforkSession(source: Session, targetLeafId: string | null): SessionResult;\n\timportJsonl(inputPath: string, options?: OpenSessionOptions): SessionResult;\n\tlist(onProgress?: SessionListProgress): Promise<SessionInfo[]>;\n\tlistAll(onProgress?: SessionListProgress): Promise<SessionInfo[]>;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"session-manager.js","sourceRoot":"","sources":["../../../src/core/session/session-manager.ts"],"names":[],"mappings":"","sourcesContent":["import type { Session } from \"./session.ts\";\nimport type { NewSessionOptions, SessionInfo, SessionListProgress } from \"./types.ts\";\n\nexport type SessionResult = Session | Promise<Session>;\n\nexport interface OpenSessionOptions {\n\tcwdOverride?: string;\n}\n\nexport interface SessionManager {\n\tcreate(options?: NewSessionOptions): SessionResult;\n\topenReference(reference: string, options?: OpenSessionOptions): SessionResult;\n\tcontinueRecent(): SessionResult;\n\tforkFrom(reference: string): SessionResult;\n\tforkSession(source: Session, targetLeafId: string | null): SessionResult;\n\timportJsonl(inputPath: string, options?: OpenSessionOptions): SessionResult;\n\tlist(onProgress?: SessionListProgress): Promise<SessionInfo[]>;\n\tlistAll(onProgress?: SessionListProgress): Promise<SessionInfo[]>;\n\tusesDefaultSessionDir?(): boolean;\n}\n"]}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { FileEntry, SessionEntry, SessionHeader, SessionInfo, SessionListProgress } from "../types.ts";
|
|
2
2
|
import { InMemorySessionStore } from "./in-memory-session-store.ts";
|
|
3
3
|
export declare function getSessionDirForReference(reference: string): string;
|
|
4
|
+
export declare function getDefaultSessionDirPath(cwd: string, agentDir?: string): string;
|
|
4
5
|
export declare function getDefaultSessionDir(cwd: string, agentDir?: string): string;
|
|
5
6
|
export declare function getSessionsRoot(): string;
|
|
6
7
|
export declare function prepareSessionReference(sessionDir: string, sessionId: string, timestamp: string): string;
|
|
@@ -10,7 +11,7 @@ export declare function load(filePath: string): FileEntry[];
|
|
|
10
11
|
export declare function append(filePath: string, entry: FileEntry): void;
|
|
11
12
|
export declare function rewrite(filePath: string, entries: FileEntry[]): void;
|
|
12
13
|
export declare function forkSession(sessionDir: string, header: SessionHeader, sourceEntries: FileEntry[]): string;
|
|
13
|
-
export declare function findMostRecent(sessionDir: string): string | null;
|
|
14
|
+
export declare function findMostRecent(sessionDir: string, cwd?: string): string | null;
|
|
14
15
|
export declare function list(dir: string, onProgress?: SessionListProgress): Promise<SessionInfo[]>;
|
|
15
16
|
export declare function listAll(sessionsDir: string, onProgress?: SessionListProgress): Promise<SessionInfo[]>;
|
|
16
17
|
export declare class JsonlSessionStore extends InMemorySessionStore {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jsonl-session-store.d.ts","sourceRoot":"","sources":["../../../../src/core/session/stores/jsonl-session-store.ts"],"names":[],"mappings":"AAiBA,OAAO,KAAK,EACX,SAAS,EACT,YAAY,EAEZ,aAAa,EACb,WAAW,EAEX,mBAAmB,EAEnB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AA8IpE,wBAAgB,yBAAyB,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAEnE;AAED,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,GAAE,MAA6B,GAAG,MAAM,CAKjG;AAED,wBAA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;IAED,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAE5B;IAED,IAAI,CAAC,QAAQ,EAAE,MAAM,GAAG,SAAS,EAAE,CAIlC;IAED,WAAW,CAAC,KAAK,EAAE,YAAY,GAAG,IAAI,CAGrC;IAED,OAAO,CAAC,oBAAoB;IAkB5B,YAAY,IAAI,IAAI,CAInB;IAED,cAAc,IAAI,IAAI,CAMrB;CACD","sourcesContent":["import type { AgentMessage } from \"@fleetagent/pi-agent-core\";\nimport type { Message, TextContent } from \"@fleetagent/pi-ai\";\nimport {\n\tappendFileSync,\n\tcloseSync,\n\texistsSync,\n\tmkdirSync,\n\topenSync,\n\treaddirSync,\n\treadFileSync,\n\treadSync,\n\tstatSync,\n\twriteFileSync,\n} from \"fs\";\nimport { readdir, readFile, stat } from \"fs/promises\";\nimport { join, resolve } from \"path\";\nimport { getAgentDir as getDefaultAgentDir, getSessionsDir } from \"../../../config.ts\";\nimport type {\n\tFileEntry,\n\tSessionEntry,\n\tSessionEntryBase,\n\tSessionHeader,\n\tSessionInfo,\n\tSessionInfoEntry,\n\tSessionListProgress,\n\tSessionMessageEntry,\n} from \"../types.ts\";\nimport { InMemorySessionStore } from \"./in-memory-session-store.ts\";\n\nfunction isMessageWithContent(message: AgentMessage): message is Message {\n\treturn typeof (message as Message).role === \"string\" && \"content\" in message;\n}\n\nfunction extractTextContent(message: Message): string {\n\tconst content = message.content;\n\tif (typeof content === \"string\") {\n\t\treturn content;\n\t}\n\treturn content\n\t\t.filter((block): block is TextContent => block.type === \"text\")\n\t\t.map((block) => block.text)\n\t\t.join(\" \");\n}\n\nfunction getLastActivityTime(entries: FileEntry[]): number | undefined {\n\tlet lastActivityTime: number | undefined;\n\n\tfor (const entry of entries) {\n\t\tif (entry.type !== \"message\") continue;\n\n\t\tconst message = (entry as SessionMessageEntry).message;\n\t\tif (!isMessageWithContent(message)) continue;\n\t\tif (message.role !== \"user\" && message.role !== \"assistant\") continue;\n\n\t\tconst msgTimestamp = (message as { timestamp?: number }).timestamp;\n\t\tif (typeof msgTimestamp === \"number\") {\n\t\t\tlastActivityTime = Math.max(lastActivityTime ?? 0, msgTimestamp);\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst entryTimestamp = (entry as SessionEntryBase).timestamp;\n\t\tif (typeof entryTimestamp === \"string\") {\n\t\t\tconst t = new Date(entryTimestamp).getTime();\n\t\t\tif (!Number.isNaN(t)) {\n\t\t\t\tlastActivityTime = Math.max(lastActivityTime ?? 0, t);\n\t\t\t}\n\t\t}\n\t}\n\n\treturn lastActivityTime;\n}\n\nfunction getSessionModifiedDate(entries: FileEntry[], header: SessionHeader, statsMtime: Date): Date {\n\tconst lastActivityTime = getLastActivityTime(entries);\n\tif (typeof lastActivityTime === \"number\" && lastActivityTime > 0) {\n\t\treturn new Date(lastActivityTime);\n\t}\n\n\tconst headerTime = typeof header.timestamp === \"string\" ? new Date(header.timestamp).getTime() : NaN;\n\treturn !Number.isNaN(headerTime) ? new Date(headerTime) : statsMtime;\n}\n\nfunction isValidSessionFile(filePath: string): boolean {\n\ttry {\n\t\tconst fd = openSync(filePath, \"r\");\n\t\tconst buffer = Buffer.alloc(512);\n\t\tconst bytesRead = readSync(fd, buffer, 0, 512, 0);\n\t\tcloseSync(fd);\n\t\tconst firstLine = buffer.toString(\"utf8\", 0, bytesRead).split(\"\\n\")[0];\n\t\tif (!firstLine) return false;\n\t\tconst header = JSON.parse(firstLine) as Partial<SessionHeader>;\n\t\treturn header.type === \"session\" && typeof header.id === \"string\";\n\t} catch {\n\t\treturn false;\n\t}\n}\n\nasync function buildSessionInfo(filePath: string): Promise<SessionInfo | null> {\n\ttry {\n\t\tconst content = await readFile(filePath, \"utf8\");\n\t\tconst entries: FileEntry[] = [];\n\t\tconst lines = content.trim().split(\"\\n\");\n\n\t\tfor (const line of lines) {\n\t\t\tif (!line.trim()) continue;\n\t\t\ttry {\n\t\t\t\tentries.push(JSON.parse(line) as FileEntry);\n\t\t\t} catch {\n\t\t\t\t// Skip malformed lines\n\t\t\t}\n\t\t}\n\n\t\tif (entries.length === 0) return null;\n\t\tconst header = entries[0];\n\t\tif (header.type !== \"session\") return null;\n\n\t\tconst stats = await stat(filePath);\n\t\tlet messageCount = 0;\n\t\tlet firstMessage = \"\";\n\t\tconst allMessages: string[] = [];\n\t\tlet name: string | undefined;\n\n\t\tfor (const entry of entries) {\n\t\t\tif (entry.type === \"session_info\") {\n\t\t\t\tconst infoEntry = entry as SessionInfoEntry;\n\t\t\t\tname = infoEntry.name?.trim() || undefined;\n\t\t\t}\n\n\t\t\tif (entry.type !== \"message\") continue;\n\t\t\tmessageCount++;\n\n\t\t\tconst message = (entry as SessionMessageEntry).message;\n\t\t\tif (!isMessageWithContent(message)) continue;\n\t\t\tif (message.role !== \"user\" && message.role !== \"assistant\") continue;\n\n\t\t\tconst textContent = extractTextContent(message);\n\t\t\tif (!textContent) continue;\n\n\t\t\tallMessages.push(textContent);\n\t\t\tif (!firstMessage && message.role === \"user\") {\n\t\t\t\tfirstMessage = textContent;\n\t\t\t}\n\t\t}\n\n\t\tconst sessionHeader = header as SessionHeader;\n\t\tconst cwd = typeof sessionHeader.cwd === \"string\" ? sessionHeader.cwd : \"\";\n\t\tconst parentSessionPath = sessionHeader.parentSession;\n\t\tconst modified = getSessionModifiedDate(entries, sessionHeader, stats.mtime);\n\n\t\treturn {\n\t\t\treference: filePath,\n\t\t\tpath: filePath,\n\t\t\tid: sessionHeader.id,\n\t\t\tcwd,\n\t\t\tname,\n\t\t\tparentSessionPath,\n\t\t\tcreated: new Date(sessionHeader.timestamp),\n\t\t\tmodified,\n\t\t\tmessageCount,\n\t\t\tfirstMessage: firstMessage || \"(no messages)\",\n\t\t\tallMessagesText: allMessages.join(\" \"),\n\t\t};\n\t} catch {\n\t\treturn null;\n\t}\n}\n\nconst MAX_CONCURRENT_SESSION_INFO_LOADS = 10;\n\nexport function getSessionDirForReference(reference: string): string {\n\treturn resolve(reference, \"..\");\n}\n\nexport function getDefaultSessionDir(cwd: string, agentDir: string = getDefaultAgentDir()): string {\n\tconst safePath = `--${cwd.replace(/^[/\\\\]/, \"\").replace(/[/\\\\:]/g, \"-\")}--`;\n\tconst sessionDir = join(agentDir, \"sessions\", safePath);\n\tensureDir(sessionDir);\n\treturn sessionDir;\n}\n\nexport function getSessionsRoot(): string {\n\treturn getSessionsDir();\n}\n\nexport function prepareSessionReference(sessionDir: string, sessionId: string, timestamp: string): string {\n\tconst fileTimestamp = timestamp.replace(/[:.]/g, \"-\");\n\treturn join(sessionDir, `${fileTimestamp}_${sessionId}.jsonl`);\n}\n\nexport function exists(path: string): boolean {\n\treturn existsSync(path);\n}\n\nexport function ensureDir(path: string): void {\n\tif (!existsSync(path)) {\n\t\tmkdirSync(path, { recursive: true });\n\t}\n}\n\nexport function load(filePath: string): FileEntry[] {\n\tif (!existsSync(filePath)) return [];\n\n\tconst content = readFileSync(filePath, \"utf8\");\n\tconst entries: FileEntry[] = [];\n\tconst lines = content.trim().split(\"\\n\");\n\n\tfor (const line of lines) {\n\t\tif (!line.trim()) continue;\n\t\ttry {\n\t\t\tconst entry = JSON.parse(line) as FileEntry;\n\t\t\tentries.push(entry);\n\t\t} catch {\n\t\t\t// Skip malformed lines\n\t\t}\n\t}\n\n\tif (entries.length === 0) return entries;\n\tconst header = entries[0];\n\tif (header.type !== \"session\" || typeof (header as Partial<SessionHeader>).id !== \"string\") {\n\t\treturn [];\n\t}\n\n\treturn entries;\n}\n\nexport function append(filePath: string, entry: FileEntry): void {\n\tappendFileSync(filePath, `${JSON.stringify(entry)}\\n`);\n}\n\nexport function rewrite(filePath: string, entries: FileEntry[]): void {\n\tconst content = `${entries.map((entry) => JSON.stringify(entry)).join(\"\\n\")}\\n`;\n\twriteFileSync(filePath, content);\n}\n\nexport function forkSession(sessionDir: string, header: SessionHeader, sourceEntries: FileEntry[]): string {\n\tconst reference = prepareSessionReference(sessionDir, header.id, header.timestamp);\n\tappend(reference, header);\n\tfor (const entry of sourceEntries) {\n\t\tif (entry.type !== \"session\") {\n\t\t\tappend(reference, entry);\n\t\t}\n\t}\n\treturn reference;\n}\n\nexport function findMostRecent(sessionDir: string): string | null {\n\ttry {\n\t\tconst files = readdirSync(sessionDir)\n\t\t\t.filter((file) => file.endsWith(\".jsonl\"))\n\t\t\t.map((file) => join(sessionDir, file))\n\t\t\t.filter(isValidSessionFile)\n\t\t\t.map((path) => ({ path, mtime: statSync(path).mtime }))\n\t\t\t.sort((a, b) => b.mtime.getTime() - a.mtime.getTime());\n\n\t\treturn files[0]?.path || null;\n\t} catch {\n\t\treturn null;\n\t}\n}\n\nexport async function list(dir: string, onProgress?: SessionListProgress): Promise<SessionInfo[]> {\n\tconst sessions: SessionInfo[] = [];\n\tif (!existsSync(dir)) {\n\t\treturn sessions;\n\t}\n\n\ttry {\n\t\tconst dirEntries = await readdir(dir);\n\t\tconst files = dirEntries.filter((file) => file.endsWith(\".jsonl\")).map((file) => join(dir, file));\n\t\tlet loaded = 0;\n\t\tconst results = await buildSessionInfosWithConcurrency(files, () => {\n\t\t\tloaded++;\n\t\t\tonProgress?.(loaded, files.length);\n\t\t});\n\t\tfor (const info of results) {\n\t\t\tif (info) {\n\t\t\t\tsessions.push(info);\n\t\t\t}\n\t\t}\n\t} catch {\n\t\t// Return empty list on error\n\t}\n\n\treturn sessions;\n}\n\nexport async function listAll(sessionsDir: string, onProgress?: SessionListProgress): Promise<SessionInfo[]> {\n\ttry {\n\t\tif (!existsSync(sessionsDir)) {\n\t\t\treturn [];\n\t\t}\n\t\tconst entries = await readdir(sessionsDir, { withFileTypes: true });\n\t\tconst dirs = entries.filter((entry) => entry.isDirectory()).map((entry) => join(sessionsDir, entry.name));\n\n\t\tlet totalFiles = 0;\n\t\tconst dirFiles: string[][] = [];\n\t\tfor (const dir of dirs) {\n\t\t\ttry {\n\t\t\t\tconst files = (await readdir(dir)).filter((file) => file.endsWith(\".jsonl\"));\n\t\t\t\tdirFiles.push(files.map((file) => join(dir, file)));\n\t\t\t\ttotalFiles += files.length;\n\t\t\t} catch {\n\t\t\t\tdirFiles.push([]);\n\t\t\t}\n\t\t}\n\n\t\tlet loaded = 0;\n\t\tconst sessions: SessionInfo[] = [];\n\t\tconst allFiles = dirFiles.flat();\n\t\tconst results = await buildSessionInfosWithConcurrency(allFiles, () => {\n\t\t\tloaded++;\n\t\t\tonProgress?.(loaded, totalFiles);\n\t\t});\n\n\t\tfor (const info of results) {\n\t\t\tif (info) {\n\t\t\t\tsessions.push(info);\n\t\t\t}\n\t\t}\n\n\t\treturn sessions;\n\t} catch {\n\t\treturn [];\n\t}\n}\n\nasync function buildSessionInfosWithConcurrency(\n\tfiles: string[],\n\tonLoaded: () => void,\n): Promise<(SessionInfo | null)[]> {\n\tconst results: (SessionInfo | null)[] = new Array(files.length).fill(null);\n\tconst inFlight = new Set<Promise<void>>();\n\tlet nextIndex = 0;\n\n\tconst startNext = (): void => {\n\t\tconst index = nextIndex++;\n\t\tconst file = files[index];\n\t\tif (!file) return;\n\n\t\tlet task: Promise<void>;\n\t\ttask = buildSessionInfo(file)\n\t\t\t.then((info) => {\n\t\t\t\tresults[index] = info;\n\t\t\t})\n\t\t\t.catch(() => {\n\t\t\t\tresults[index] = null;\n\t\t\t})\n\t\t\t.finally(() => {\n\t\t\t\tinFlight.delete(task);\n\t\t\t\tonLoaded();\n\t\t\t});\n\t\tinFlight.add(task);\n\t};\n\n\twhile (nextIndex < files.length || inFlight.size > 0) {\n\t\twhile (nextIndex < files.length && inFlight.size < MAX_CONCURRENT_SESSION_INFO_LOADS) {\n\t\t\tstartNext();\n\t\t}\n\t\tif (inFlight.size > 0) {\n\t\t\tawait Promise.race(inFlight);\n\t\t}\n\t}\n\n\treturn results;\n}\n\nexport class JsonlSessionStore extends InMemorySessionStore {\n\tprivate reference: string | undefined;\n\tprivate flushed = false;\n\n\tisPersisted(): boolean {\n\t\treturn true;\n\t}\n\n\tgetSessionReference(): string | undefined {\n\t\treturn this.reference;\n\t}\n\n\tsetSessionReference(reference: string): void {\n\t\tthis.reference = resolve(reference);\n\t\tthis.flushed = false;\n\t}\n\n\texists(path: string): boolean {\n\t\treturn exists(path);\n\t}\n\n\tensureDir(path: string): void {\n\t\tensureDir(path);\n\t}\n\n\tload(filePath: string): FileEntry[] {\n\t\tif (!exists(filePath)) return [];\n\t\tthis.flushed = true;\n\t\treturn load(filePath);\n\t}\n\n\tappendEntry(entry: SessionEntry): void {\n\t\tsuper.appendEntry(entry);\n\t\tthis.persistAppendedEntry(entry);\n\t}\n\n\tprivate persistAppendedEntry(entry: SessionEntry): void {\n\t\tif (!this.reference) return;\n\n\t\tif (!this.hasAssistantMessage()) {\n\t\t\tthis.flushed = false;\n\t\t\treturn;\n\t\t}\n\n\t\tif (!this.flushed) {\n\t\t\tfor (const fileEntry of this.getFileEntries()) {\n\t\t\t\tappend(this.reference, fileEntry);\n\t\t\t}\n\t\t\tthis.flushed = true;\n\t\t} else {\n\t\t\tappend(this.reference, entry);\n\t\t}\n\t}\n\n\tsaveSnapshot(): void {\n\t\tif (!this.reference) return;\n\t\trewrite(this.reference, this.getFileEntries());\n\t\tthis.flushed = true;\n\t}\n\n\tcommitSnapshot(): void {\n\t\tif (!this.hasAssistantMessage()) {\n\t\t\tthis.flushed = false;\n\t\t\treturn;\n\t\t}\n\t\tthis.saveSnapshot();\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"jsonl-session-store.d.ts","sourceRoot":"","sources":["../../../../src/core/session/stores/jsonl-session-store.ts"],"names":[],"mappings":"AAmBA,OAAO,KAAK,EACX,SAAS,EACT,YAAY,EACZ,aAAa,EACb,WAAW,EACX,mBAAmB,EAEnB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AAgJpE,wBAAgB,yBAAyB,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAEnE;AAED,wBAAgB,wBAAwB,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,GAAE,MAA6B,GAAG,MAAM,CAIrG;AAED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,GAAG,SAAS,CAExC;IAED,mBAAmB,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAG3C;IAED,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAE5B;IAED,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAE5B;IAED,IAAI,CAAC,QAAQ,EAAE,MAAM,GAAG,SAAS,EAAE,CAIlC;IAED,WAAW,CAAC,KAAK,EAAE,YAAY,GAAG,IAAI,CAGrC;IAED,OAAO,CAAC,oBAAoB;IAkB5B,YAAY,IAAI,IAAI,CAInB;IAED,cAAc,IAAI,IAAI,CAMrB;CACD","sourcesContent":["import type { AgentMessage } from \"@fleetagent/pi-agent-core\";\nimport type { Message, TextContent } from \"@fleetagent/pi-ai\";\nimport {\n\tappendFileSync,\n\tcloseSync,\n\tcreateReadStream,\n\texistsSync,\n\tmkdirSync,\n\topenSync,\n\treaddirSync,\n\treadSync,\n\tstatSync,\n\twriteFileSync,\n} from \"fs\";\nimport { readdir, stat } from \"fs/promises\";\nimport { join, resolve } from \"path\";\nimport { createInterface } from \"readline\";\nimport { StringDecoder } from \"string_decoder\";\nimport { getAgentDir as getDefaultAgentDir, getSessionsDir } from \"../../../config.ts\";\nimport type {\n\tFileEntry,\n\tSessionEntry,\n\tSessionHeader,\n\tSessionInfo,\n\tSessionListProgress,\n\tSessionMessageEntry,\n} from \"../types.ts\";\nimport { InMemorySessionStore } from \"./in-memory-session-store.ts\";\n\nfunction isMessageWithContent(message: AgentMessage): message is Message {\n\treturn typeof (message as Message).role === \"string\" && \"content\" in message;\n}\n\nfunction extractTextContent(message: Message): string {\n\tconst content = message.content;\n\tif (typeof content === \"string\") {\n\t\treturn content;\n\t}\n\treturn content\n\t\t.filter((block): block is TextContent => block.type === \"text\")\n\t\t.map((block) => block.text)\n\t\t.join(\" \");\n}\n\nfunction getMessageActivityTime(entry: SessionMessageEntry): number | undefined {\n\tconst message = entry.message;\n\tif (!isMessageWithContent(message)) return undefined;\n\tif (message.role !== \"user\" && message.role !== \"assistant\") return undefined;\n\n\tconst msgTimestamp = (message as { timestamp?: number }).timestamp;\n\tif (typeof msgTimestamp === \"number\") {\n\t\treturn msgTimestamp;\n\t}\n\n\tconst t = new Date(entry.timestamp).getTime();\n\treturn Number.isNaN(t) ? undefined : t;\n}\n\nfunction parseSessionEntryLine(line: string): FileEntry | null {\n\tif (!line.trim()) return null;\n\ttry {\n\t\treturn JSON.parse(line) as FileEntry;\n\t} catch {\n\t\treturn null;\n\t}\n}\n\nfunction readSessionHeader(filePath: string): SessionHeader | null {\n\ttry {\n\t\tconst fd = openSync(filePath, \"r\");\n\t\tconst buffer = Buffer.alloc(512);\n\t\tconst bytesRead = readSync(fd, buffer, 0, 512, 0);\n\t\tcloseSync(fd);\n\t\tconst firstLine = buffer.toString(\"utf8\", 0, bytesRead).split(\"\\n\")[0];\n\t\tif (!firstLine) return null;\n\t\tconst header = JSON.parse(firstLine) as Partial<SessionHeader>;\n\t\tif (header.type !== \"session\" || typeof header.id !== \"string\") {\n\t\t\treturn null;\n\t\t}\n\t\treturn header as SessionHeader;\n\t} catch {\n\t\treturn null;\n\t}\n}\n\nfunction sessionCwdMatches(cwd: string | undefined, resolvedCwd: string): boolean {\n\treturn cwd !== undefined && cwd !== \"\" && resolve(cwd) === resolvedCwd;\n}\n\nasync function buildSessionInfo(filePath: string): Promise<SessionInfo | null> {\n\ttry {\n\t\tconst stats = await stat(filePath);\n\t\tlet header: SessionHeader | null = null;\n\t\tlet messageCount = 0;\n\t\tlet firstMessage = \"\";\n\t\tconst allMessages: string[] = [];\n\t\tlet name: string | undefined;\n\t\tlet lastActivityTime: number | undefined;\n\n\t\tconst rl = createInterface({\n\t\t\tinput: createReadStream(filePath, { encoding: \"utf8\" }),\n\t\t\tcrlfDelay: Infinity,\n\t\t});\n\n\t\tfor await (const line of rl) {\n\t\t\tconst entry = parseSessionEntryLine(line);\n\t\t\tif (!entry) continue;\n\n\t\t\tif (!header) {\n\t\t\t\tif (entry.type !== \"session\") return null;\n\t\t\t\theader = entry;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif (entry.type === \"session_info\") {\n\t\t\t\tname = entry.name?.trim() || undefined;\n\t\t\t}\n\n\t\t\tif (entry.type !== \"message\") continue;\n\t\t\tmessageCount++;\n\n\t\t\tconst activityTime = getMessageActivityTime(entry);\n\t\t\tif (typeof activityTime === \"number\") {\n\t\t\t\tlastActivityTime = Math.max(lastActivityTime ?? 0, activityTime);\n\t\t\t}\n\n\t\t\tconst message = entry.message;\n\t\t\tif (!isMessageWithContent(message)) continue;\n\t\t\tif (message.role !== \"user\" && message.role !== \"assistant\") continue;\n\n\t\t\tconst textContent = extractTextContent(message);\n\t\t\tif (!textContent) continue;\n\n\t\t\tallMessages.push(textContent);\n\t\t\tif (!firstMessage && message.role === \"user\") {\n\t\t\t\tfirstMessage = textContent;\n\t\t\t}\n\t\t}\n\n\t\tif (!header) return null;\n\n\t\tconst cwd = typeof header.cwd === \"string\" ? header.cwd : \"\";\n\t\tconst parentSessionPath = header.parentSession;\n\t\tconst headerTime = typeof header.timestamp === \"string\" ? new Date(header.timestamp).getTime() : NaN;\n\t\tconst modified =\n\t\t\ttypeof lastActivityTime === \"number\" && lastActivityTime > 0\n\t\t\t\t? new Date(lastActivityTime)\n\t\t\t\t: !Number.isNaN(headerTime)\n\t\t\t\t\t? new Date(headerTime)\n\t\t\t\t\t: stats.mtime;\n\n\t\treturn {\n\t\t\treference: filePath,\n\t\t\tpath: filePath,\n\t\t\tid: header.id,\n\t\t\tcwd,\n\t\t\tname,\n\t\t\tparentSessionPath,\n\t\t\tcreated: new Date(header.timestamp),\n\t\t\tmodified,\n\t\t\tmessageCount,\n\t\t\tfirstMessage: firstMessage || \"(no messages)\",\n\t\t\tallMessagesText: allMessages.join(\" \"),\n\t\t};\n\t} catch {\n\t\treturn null;\n\t}\n}\n\nconst MAX_CONCURRENT_SESSION_INFO_LOADS = 10;\n\nexport function getSessionDirForReference(reference: string): string {\n\treturn resolve(reference, \"..\");\n}\n\nexport function getDefaultSessionDirPath(cwd: string, agentDir: string = getDefaultAgentDir()): string {\n\tconst resolvedCwd = resolve(cwd);\n\tconst safePath = `--${resolvedCwd.replace(/^[/\\\\]/, \"\").replace(/[/\\\\:]/g, \"-\")}--`;\n\treturn join(resolve(agentDir), \"sessions\", safePath);\n}\n\nexport function getDefaultSessionDir(cwd: string, agentDir: string = getDefaultAgentDir()): string {\n\tconst sessionDir = getDefaultSessionDirPath(cwd, agentDir);\n\tensureDir(sessionDir);\n\treturn sessionDir;\n}\n\nexport function getSessionsRoot(): string {\n\treturn getSessionsDir();\n}\n\nexport function prepareSessionReference(sessionDir: string, sessionId: string, timestamp: string): string {\n\tconst fileTimestamp = timestamp.replace(/[:.]/g, \"-\");\n\treturn join(sessionDir, `${fileTimestamp}_${sessionId}.jsonl`);\n}\n\nexport function exists(path: string): boolean {\n\treturn existsSync(path);\n}\n\nexport function ensureDir(path: string): void {\n\tif (!existsSync(path)) {\n\t\tmkdirSync(path, { recursive: true });\n\t}\n}\n\nconst SESSION_READ_BUFFER_SIZE = 1024 * 1024;\n\nexport function load(filePath: string): FileEntry[] {\n\tif (!existsSync(filePath)) return [];\n\n\tconst entries: FileEntry[] = [];\n\tconst fd = openSync(filePath, \"r\");\n\ttry {\n\t\tconst decoder = new StringDecoder(\"utf8\");\n\t\tconst buffer = Buffer.allocUnsafe(SESSION_READ_BUFFER_SIZE);\n\t\tlet pending = \"\";\n\n\t\twhile (true) {\n\t\t\tconst bytesRead = readSync(fd, buffer, 0, buffer.length, null);\n\t\t\tif (bytesRead === 0) break;\n\n\t\t\tpending += decoder.write(buffer.subarray(0, bytesRead));\n\t\t\tlet lineStart = 0;\n\t\t\tlet newlineIndex = pending.indexOf(\"\\n\", lineStart);\n\t\t\twhile (newlineIndex !== -1) {\n\t\t\t\tconst entry = parseSessionEntryLine(pending.slice(lineStart, newlineIndex));\n\t\t\t\tif (entry) entries.push(entry);\n\t\t\t\tlineStart = newlineIndex + 1;\n\t\t\t\tnewlineIndex = pending.indexOf(\"\\n\", lineStart);\n\t\t\t}\n\t\t\tpending = pending.slice(lineStart);\n\t\t}\n\n\t\tpending += decoder.end();\n\t\tconst finalEntry = parseSessionEntryLine(pending);\n\t\tif (finalEntry) entries.push(finalEntry);\n\t} finally {\n\t\tcloseSync(fd);\n\t}\n\n\tif (entries.length === 0) return entries;\n\tconst header = entries[0];\n\tif (header.type !== \"session\" || typeof (header as Partial<SessionHeader>).id !== \"string\") {\n\t\treturn [];\n\t}\n\n\treturn entries;\n}\n\nexport function append(filePath: string, entry: FileEntry): void {\n\tappendFileSync(filePath, `${JSON.stringify(entry)}\\n`);\n}\n\nexport function rewrite(filePath: string, entries: FileEntry[]): void {\n\tconst content = `${entries.map((entry) => JSON.stringify(entry)).join(\"\\n\")}\\n`;\n\twriteFileSync(filePath, content);\n}\n\nexport function forkSession(sessionDir: string, header: SessionHeader, sourceEntries: FileEntry[]): string {\n\tconst reference = prepareSessionReference(sessionDir, header.id, header.timestamp);\n\tappend(reference, header);\n\tfor (const entry of sourceEntries) {\n\t\tif (entry.type !== \"session\") {\n\t\t\tappend(reference, entry);\n\t\t}\n\t}\n\treturn reference;\n}\n\nexport function findMostRecent(sessionDir: string, cwd?: string): string | null {\n\tconst resolvedCwd = cwd ? resolve(cwd) : undefined;\n\ttry {\n\t\tconst files = readdirSync(sessionDir)\n\t\t\t.filter((file) => file.endsWith(\".jsonl\"))\n\t\t\t.map((file) => join(sessionDir, file))\n\t\t\t.map((path) => ({ path, header: readSessionHeader(path) }))\n\t\t\t.filter(\n\t\t\t\t(file): file is { path: string; header: SessionHeader } =>\n\t\t\t\t\tfile.header !== null && (!resolvedCwd || sessionCwdMatches(file.header.cwd, resolvedCwd)),\n\t\t\t)\n\t\t\t.map(({ path }) => ({ path, mtime: statSync(path).mtime }))\n\t\t\t.sort((a, b) => b.mtime.getTime() - a.mtime.getTime());\n\n\t\treturn files[0]?.path || null;\n\t} catch {\n\t\treturn null;\n\t}\n}\n\nexport async function list(dir: string, onProgress?: SessionListProgress): Promise<SessionInfo[]> {\n\tconst sessions: SessionInfo[] = [];\n\tif (!existsSync(dir)) {\n\t\treturn sessions;\n\t}\n\n\ttry {\n\t\tconst dirEntries = await readdir(dir);\n\t\tconst files = dirEntries.filter((file) => file.endsWith(\".jsonl\")).map((file) => join(dir, file));\n\t\tlet loaded = 0;\n\t\tconst results = await buildSessionInfosWithConcurrency(files, () => {\n\t\t\tloaded++;\n\t\t\tonProgress?.(loaded, files.length);\n\t\t});\n\t\tfor (const info of results) {\n\t\t\tif (info) {\n\t\t\t\tsessions.push(info);\n\t\t\t}\n\t\t}\n\t} catch {\n\t\t// Return empty list on error\n\t}\n\n\treturn sessions;\n}\n\nexport async function listAll(sessionsDir: string, onProgress?: SessionListProgress): Promise<SessionInfo[]> {\n\ttry {\n\t\tif (!existsSync(sessionsDir)) {\n\t\t\treturn [];\n\t\t}\n\t\tconst entries = await readdir(sessionsDir, { withFileTypes: true });\n\t\tconst dirs = entries.filter((entry) => entry.isDirectory()).map((entry) => join(sessionsDir, entry.name));\n\n\t\tlet totalFiles = 0;\n\t\tconst dirFiles: string[][] = [];\n\t\tfor (const dir of dirs) {\n\t\t\ttry {\n\t\t\t\tconst files = (await readdir(dir)).filter((file) => file.endsWith(\".jsonl\"));\n\t\t\t\tdirFiles.push(files.map((file) => join(dir, file)));\n\t\t\t\ttotalFiles += files.length;\n\t\t\t} catch {\n\t\t\t\tdirFiles.push([]);\n\t\t\t}\n\t\t}\n\n\t\tlet loaded = 0;\n\t\tconst sessions: SessionInfo[] = [];\n\t\tconst allFiles = dirFiles.flat();\n\t\tconst results = await buildSessionInfosWithConcurrency(allFiles, () => {\n\t\t\tloaded++;\n\t\t\tonProgress?.(loaded, totalFiles);\n\t\t});\n\n\t\tfor (const info of results) {\n\t\t\tif (info) {\n\t\t\t\tsessions.push(info);\n\t\t\t}\n\t\t}\n\n\t\treturn sessions;\n\t} catch {\n\t\treturn [];\n\t}\n}\n\nasync function buildSessionInfosWithConcurrency(\n\tfiles: string[],\n\tonLoaded: () => void,\n): Promise<(SessionInfo | null)[]> {\n\tconst results: (SessionInfo | null)[] = new Array(files.length).fill(null);\n\tconst inFlight = new Set<Promise<void>>();\n\tlet nextIndex = 0;\n\n\tconst startNext = (): void => {\n\t\tconst index = nextIndex++;\n\t\tconst file = files[index];\n\t\tif (!file) return;\n\n\t\tlet task: Promise<void>;\n\t\ttask = buildSessionInfo(file)\n\t\t\t.then((info) => {\n\t\t\t\tresults[index] = info;\n\t\t\t})\n\t\t\t.catch(() => {\n\t\t\t\tresults[index] = null;\n\t\t\t})\n\t\t\t.finally(() => {\n\t\t\t\tinFlight.delete(task);\n\t\t\t\tonLoaded();\n\t\t\t});\n\t\tinFlight.add(task);\n\t};\n\n\twhile (nextIndex < files.length || inFlight.size > 0) {\n\t\twhile (nextIndex < files.length && inFlight.size < MAX_CONCURRENT_SESSION_INFO_LOADS) {\n\t\t\tstartNext();\n\t\t}\n\t\tif (inFlight.size > 0) {\n\t\t\tawait Promise.race(inFlight);\n\t\t}\n\t}\n\n\treturn results;\n}\n\nexport class JsonlSessionStore extends InMemorySessionStore {\n\tprivate reference: string | undefined;\n\tprivate flushed = false;\n\n\tisPersisted(): boolean {\n\t\treturn true;\n\t}\n\n\tgetSessionReference(): string | undefined {\n\t\treturn this.reference;\n\t}\n\n\tsetSessionReference(reference: string): void {\n\t\tthis.reference = resolve(reference);\n\t\tthis.flushed = false;\n\t}\n\n\texists(path: string): boolean {\n\t\treturn exists(path);\n\t}\n\n\tensureDir(path: string): void {\n\t\tensureDir(path);\n\t}\n\n\tload(filePath: string): FileEntry[] {\n\t\tif (!exists(filePath)) return [];\n\t\tthis.flushed = true;\n\t\treturn load(filePath);\n\t}\n\n\tappendEntry(entry: SessionEntry): void {\n\t\tsuper.appendEntry(entry);\n\t\tthis.persistAppendedEntry(entry);\n\t}\n\n\tprivate persistAppendedEntry(entry: SessionEntry): void {\n\t\tif (!this.reference) return;\n\n\t\tif (!this.hasAssistantMessage()) {\n\t\t\tthis.flushed = false;\n\t\t\treturn;\n\t\t}\n\n\t\tif (!this.flushed) {\n\t\t\tfor (const fileEntry of this.getFileEntries()) {\n\t\t\t\tappend(this.reference, fileEntry);\n\t\t\t}\n\t\t\tthis.flushed = true;\n\t\t} else {\n\t\t\tappend(this.reference, entry);\n\t\t}\n\t}\n\n\tsaveSnapshot(): void {\n\t\tif (!this.reference) return;\n\t\trewrite(this.reference, this.getFileEntries());\n\t\tthis.flushed = true;\n\t}\n\n\tcommitSnapshot(): void {\n\t\tif (!this.hasAssistantMessage()) {\n\t\t\tthis.flushed = false;\n\t\t\treturn;\n\t\t}\n\t\tthis.saveSnapshot();\n\t}\n}\n"]}
|