@clinebot/core 0.0.22 → 0.0.23
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/dist/ClineCore.d.ts +110 -0
- package/dist/ClineCore.d.ts.map +1 -0
- package/dist/account/cline-account-service.d.ts +2 -1
- package/dist/account/cline-account-service.d.ts.map +1 -1
- package/dist/account/index.d.ts +1 -1
- package/dist/account/index.d.ts.map +1 -1
- package/dist/account/rpc.d.ts +3 -1
- package/dist/account/rpc.d.ts.map +1 -1
- package/dist/account/types.d.ts +3 -0
- package/dist/account/types.d.ts.map +1 -1
- package/dist/agents/plugin-loader.d.ts.map +1 -1
- package/dist/agents/plugin-sandbox-bootstrap.js +17 -17
- package/dist/auth/client.d.ts +1 -1
- package/dist/auth/client.d.ts.map +1 -1
- package/dist/auth/cline.d.ts +1 -1
- package/dist/auth/cline.d.ts.map +1 -1
- package/dist/auth/codex.d.ts +1 -1
- package/dist/auth/codex.d.ts.map +1 -1
- package/dist/auth/oca.d.ts +1 -1
- package/dist/auth/oca.d.ts.map +1 -1
- package/dist/auth/utils.d.ts +2 -2
- package/dist/auth/utils.d.ts.map +1 -1
- package/dist/index.d.ts +50 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +949 -0
- package/dist/providers/local-provider-service.d.ts +4 -4
- package/dist/providers/local-provider-service.d.ts.map +1 -1
- package/dist/runtime/runtime-builder.d.ts +1 -0
- package/dist/runtime/runtime-builder.d.ts.map +1 -1
- package/dist/runtime/session-runtime.d.ts +2 -1
- package/dist/runtime/session-runtime.d.ts.map +1 -1
- package/dist/runtime/team-runtime-registry.d.ts +13 -0
- package/dist/runtime/team-runtime-registry.d.ts.map +1 -0
- package/dist/session/default-session-manager.d.ts +2 -2
- package/dist/session/default-session-manager.d.ts.map +1 -1
- package/dist/session/rpc-runtime-ensure.d.ts +53 -0
- package/dist/session/rpc-runtime-ensure.d.ts.map +1 -0
- package/dist/session/session-config-builder.d.ts +2 -3
- package/dist/session/session-config-builder.d.ts.map +1 -1
- package/dist/session/session-host.d.ts +8 -18
- package/dist/session/session-host.d.ts.map +1 -1
- package/dist/session/session-manager.d.ts +1 -1
- package/dist/session/session-manager.d.ts.map +1 -1
- package/dist/session/session-manifest.d.ts +1 -2
- package/dist/session/session-manifest.d.ts.map +1 -1
- package/dist/session/unified-session-persistence-service.d.ts +2 -2
- package/dist/session/unified-session-persistence-service.d.ts.map +1 -1
- package/dist/session/utils/helpers.d.ts +1 -1
- package/dist/session/utils/helpers.d.ts.map +1 -1
- package/dist/session/utils/types.d.ts +1 -1
- package/dist/session/utils/types.d.ts.map +1 -1
- package/dist/storage/provider-settings-legacy-migration.d.ts.map +1 -1
- package/dist/telemetry/OpenTelemetryProvider.d.ts.map +1 -1
- package/dist/telemetry/distinct-id.d.ts +2 -0
- package/dist/telemetry/distinct-id.d.ts.map +1 -0
- package/dist/telemetry/{opentelemetry.d.ts → index.d.ts} +1 -1
- package/dist/telemetry/index.d.ts.map +1 -0
- package/dist/telemetry/index.js +28 -0
- package/dist/tools/constants.d.ts +1 -1
- package/dist/tools/constants.d.ts.map +1 -1
- package/dist/tools/definitions.d.ts +3 -3
- package/dist/tools/definitions.d.ts.map +1 -1
- package/dist/tools/executors/apply-patch.d.ts +1 -1
- package/dist/tools/executors/apply-patch.d.ts.map +1 -1
- package/dist/tools/executors/bash.d.ts +1 -1
- package/dist/tools/executors/bash.d.ts.map +1 -1
- package/dist/tools/executors/editor.d.ts +1 -1
- package/dist/tools/executors/editor.d.ts.map +1 -1
- package/dist/tools/executors/file-read.d.ts +1 -1
- package/dist/tools/executors/file-read.d.ts.map +1 -1
- package/dist/tools/executors/index.d.ts +14 -14
- package/dist/tools/executors/index.d.ts.map +1 -1
- package/dist/tools/executors/search.d.ts +1 -1
- package/dist/tools/executors/search.d.ts.map +1 -1
- package/dist/tools/executors/web-fetch.d.ts +1 -1
- package/dist/tools/executors/web-fetch.d.ts.map +1 -1
- package/dist/tools/helpers.d.ts +1 -1
- package/dist/tools/helpers.d.ts.map +1 -1
- package/dist/tools/index.d.ts +10 -10
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/model-tool-routing.d.ts +1 -1
- package/dist/tools/model-tool-routing.d.ts.map +1 -1
- package/dist/tools/presets.d.ts +1 -1
- package/dist/tools/presets.d.ts.map +1 -1
- package/dist/types/common.d.ts +17 -8
- package/dist/types/common.d.ts.map +1 -1
- package/dist/types/config.d.ts +4 -3
- package/dist/types/config.d.ts.map +1 -1
- package/dist/types/provider-settings.d.ts +1 -1
- package/dist/types/provider-settings.d.ts.map +1 -1
- package/dist/types.d.ts +5 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/version.d.ts +2 -0
- package/dist/version.d.ts.map +1 -0
- package/package.json +44 -38
- package/src/ClineCore.ts +137 -0
- package/src/account/cline-account-service.test.ts +101 -0
- package/src/account/cline-account-service.ts +300 -0
- package/src/account/featurebase-token.test.ts +175 -0
- package/src/account/index.ts +23 -0
- package/src/account/rpc.test.ts +63 -0
- package/src/account/rpc.ts +185 -0
- package/src/account/types.ts +102 -0
- package/src/agents/agent-config-loader.test.ts +236 -0
- package/src/agents/agent-config-loader.ts +108 -0
- package/src/agents/agent-config-parser.ts +198 -0
- package/src/agents/hooks-config-loader.test.ts +20 -0
- package/src/agents/hooks-config-loader.ts +118 -0
- package/src/agents/index.ts +85 -0
- package/src/agents/plugin-config-loader.test.ts +140 -0
- package/src/agents/plugin-config-loader.ts +97 -0
- package/src/agents/plugin-loader.test.ts +210 -0
- package/src/agents/plugin-loader.ts +175 -0
- package/src/agents/plugin-sandbox-bootstrap.ts +448 -0
- package/src/agents/plugin-sandbox.test.ts +296 -0
- package/src/agents/plugin-sandbox.ts +341 -0
- package/src/agents/unified-config-file-watcher.test.ts +196 -0
- package/src/agents/unified-config-file-watcher.ts +483 -0
- package/src/agents/user-instruction-config-loader.test.ts +158 -0
- package/src/agents/user-instruction-config-loader.ts +438 -0
- package/src/auth/client.test.ts +40 -0
- package/src/auth/client.ts +25 -0
- package/src/auth/cline.test.ts +130 -0
- package/src/auth/cline.ts +420 -0
- package/src/auth/codex.test.ts +170 -0
- package/src/auth/codex.ts +491 -0
- package/src/auth/oca.test.ts +215 -0
- package/src/auth/oca.ts +573 -0
- package/src/auth/server.ts +216 -0
- package/src/auth/types.ts +81 -0
- package/src/auth/utils.test.ts +128 -0
- package/src/auth/utils.ts +247 -0
- package/src/chat/chat-schema.ts +82 -0
- package/src/index.ts +479 -0
- package/src/input/file-indexer.d.ts +11 -0
- package/src/input/file-indexer.test.ts +127 -0
- package/src/input/file-indexer.ts +327 -0
- package/src/input/index.ts +7 -0
- package/src/input/mention-enricher.test.ts +85 -0
- package/src/input/mention-enricher.ts +122 -0
- package/src/mcp/config-loader.test.ts +238 -0
- package/src/mcp/config-loader.ts +219 -0
- package/src/mcp/index.ts +26 -0
- package/src/mcp/manager.test.ts +106 -0
- package/src/mcp/manager.ts +262 -0
- package/src/mcp/types.ts +88 -0
- package/src/providers/local-provider-registry.ts +232 -0
- package/src/providers/local-provider-service.test.ts +783 -0
- package/src/providers/local-provider-service.ts +471 -0
- package/src/runtime/commands.test.ts +98 -0
- package/src/runtime/commands.ts +83 -0
- package/src/runtime/hook-file-hooks.test.ts +237 -0
- package/src/runtime/hook-file-hooks.ts +859 -0
- package/src/runtime/index.ts +37 -0
- package/src/runtime/rules.ts +34 -0
- package/src/runtime/runtime-builder.team-persistence.test.ts +245 -0
- package/src/runtime/runtime-builder.test.ts +371 -0
- package/src/runtime/runtime-builder.ts +631 -0
- package/src/runtime/runtime-parity.test.ts +143 -0
- package/src/runtime/sandbox/subprocess-sandbox.ts +231 -0
- package/src/runtime/session-runtime.ts +49 -0
- package/src/runtime/skills.ts +44 -0
- package/src/runtime/team-runtime-registry.ts +46 -0
- package/src/runtime/tool-approval.ts +104 -0
- package/src/runtime/workflows.test.ts +119 -0
- package/src/runtime/workflows.ts +45 -0
- package/src/session/default-session-manager.e2e.test.ts +384 -0
- package/src/session/default-session-manager.test.ts +1931 -0
- package/src/session/default-session-manager.ts +1422 -0
- package/src/session/file-session-service.ts +280 -0
- package/src/session/index.ts +45 -0
- package/src/session/rpc-runtime-ensure.ts +521 -0
- package/src/session/rpc-session-service.ts +107 -0
- package/src/session/rpc-spawn-lease.test.ts +49 -0
- package/src/session/rpc-spawn-lease.ts +122 -0
- package/src/session/runtime-oauth-token-manager.test.ts +137 -0
- package/src/session/runtime-oauth-token-manager.ts +272 -0
- package/src/session/session-agent-events.ts +248 -0
- package/src/session/session-artifacts.ts +106 -0
- package/src/session/session-config-builder.ts +113 -0
- package/src/session/session-graph.ts +92 -0
- package/src/session/session-host.test.ts +89 -0
- package/src/session/session-host.ts +205 -0
- package/src/session/session-manager.ts +69 -0
- package/src/session/session-manifest.ts +29 -0
- package/src/session/session-service.team-persistence.test.ts +48 -0
- package/src/session/session-service.ts +673 -0
- package/src/session/session-team-coordination.ts +229 -0
- package/src/session/session-telemetry.ts +100 -0
- package/src/session/sqlite-rpc-session-backend.ts +303 -0
- package/src/session/unified-session-persistence-service.test.ts +85 -0
- package/src/session/unified-session-persistence-service.ts +994 -0
- package/src/session/utils/helpers.ts +139 -0
- package/src/session/utils/types.ts +57 -0
- package/src/session/utils/usage.ts +32 -0
- package/src/session/workspace-manager.ts +98 -0
- package/src/session/workspace-manifest.ts +100 -0
- package/src/storage/artifact-store.ts +1 -0
- package/src/storage/file-team-store.ts +257 -0
- package/src/storage/index.ts +11 -0
- package/src/storage/provider-settings-legacy-migration.test.ts +424 -0
- package/src/storage/provider-settings-legacy-migration.ts +826 -0
- package/src/storage/provider-settings-manager.test.ts +191 -0
- package/src/storage/provider-settings-manager.ts +152 -0
- package/src/storage/session-store.ts +1 -0
- package/src/storage/sqlite-session-store.ts +275 -0
- package/src/storage/sqlite-team-store.ts +454 -0
- package/src/storage/team-store.ts +40 -0
- package/src/team/index.ts +4 -0
- package/src/team/projections.ts +285 -0
- package/src/telemetry/ITelemetryAdapter.ts +94 -0
- package/src/telemetry/LoggerTelemetryAdapter.test.ts +42 -0
- package/src/telemetry/LoggerTelemetryAdapter.ts +114 -0
- package/src/telemetry/OpenTelemetryAdapter.test.ts +157 -0
- package/src/telemetry/OpenTelemetryAdapter.ts +348 -0
- package/src/telemetry/OpenTelemetryProvider.test.ts +113 -0
- package/src/telemetry/OpenTelemetryProvider.ts +325 -0
- package/src/telemetry/TelemetryService.test.ts +134 -0
- package/src/telemetry/TelemetryService.ts +141 -0
- package/src/telemetry/core-events.ts +400 -0
- package/src/telemetry/distinct-id.test.ts +57 -0
- package/src/telemetry/distinct-id.ts +58 -0
- package/src/telemetry/index.ts +20 -0
- package/src/tools/constants.ts +35 -0
- package/src/tools/definitions.test.ts +704 -0
- package/src/tools/definitions.ts +709 -0
- package/src/tools/executors/apply-patch-parser.ts +520 -0
- package/src/tools/executors/apply-patch.ts +359 -0
- package/src/tools/executors/bash.test.ts +87 -0
- package/src/tools/executors/bash.ts +207 -0
- package/src/tools/executors/editor.test.ts +35 -0
- package/src/tools/executors/editor.ts +219 -0
- package/src/tools/executors/file-read.test.ts +49 -0
- package/src/tools/executors/file-read.ts +110 -0
- package/src/tools/executors/index.ts +87 -0
- package/src/tools/executors/search.ts +278 -0
- package/src/tools/executors/web-fetch.ts +259 -0
- package/src/tools/helpers.ts +130 -0
- package/src/tools/index.ts +169 -0
- package/src/tools/model-tool-routing.test.ts +86 -0
- package/src/tools/model-tool-routing.ts +132 -0
- package/src/tools/presets.test.ts +62 -0
- package/src/tools/presets.ts +168 -0
- package/src/tools/schemas.ts +327 -0
- package/src/tools/types.ts +329 -0
- package/src/types/common.ts +26 -0
- package/src/types/config.ts +86 -0
- package/src/types/events.ts +74 -0
- package/src/types/index.ts +24 -0
- package/src/types/provider-settings.ts +43 -0
- package/src/types/sessions.ts +16 -0
- package/src/types/storage.ts +64 -0
- package/src/types/workspace.ts +7 -0
- package/src/types.ts +132 -0
- package/src/version.ts +3 -0
- package/dist/index.node.d.ts +0 -47
- package/dist/index.node.d.ts.map +0 -1
- package/dist/index.node.js +0 -948
- package/dist/telemetry/opentelemetry.d.ts.map +0 -1
- package/dist/telemetry/opentelemetry.js +0 -27
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
5
|
+
import { createUserInstructionConfigWatcher } from "../agents";
|
|
6
|
+
import {
|
|
7
|
+
listAvailableWorkflowsFromWatcher,
|
|
8
|
+
resolveWorkflowSlashCommandFromWatcher,
|
|
9
|
+
} from "./workflows";
|
|
10
|
+
|
|
11
|
+
describe("runtime workflows helpers", () => {
|
|
12
|
+
const tempRoots: string[] = [];
|
|
13
|
+
|
|
14
|
+
afterEach(async () => {
|
|
15
|
+
await Promise.all(
|
|
16
|
+
tempRoots.map((dir) => rm(dir, { recursive: true, force: true })),
|
|
17
|
+
);
|
|
18
|
+
tempRoots.length = 0;
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("lists only enabled workflows from watcher snapshots", async () => {
|
|
22
|
+
const tempRoot = await mkdtemp(join(tmpdir(), "core-runtime-workflows-"));
|
|
23
|
+
tempRoots.push(tempRoot);
|
|
24
|
+
const workflowsDir = join(tempRoot, "workflows");
|
|
25
|
+
await mkdir(workflowsDir, { recursive: true });
|
|
26
|
+
await writeFile(
|
|
27
|
+
join(workflowsDir, "enabled.md"),
|
|
28
|
+
`---
|
|
29
|
+
name: enabled-workflow
|
|
30
|
+
---
|
|
31
|
+
Run enabled workflow.`,
|
|
32
|
+
);
|
|
33
|
+
await writeFile(
|
|
34
|
+
join(workflowsDir, "disabled.md"),
|
|
35
|
+
`---
|
|
36
|
+
name: disabled-workflow
|
|
37
|
+
disabled: true
|
|
38
|
+
---
|
|
39
|
+
Run disabled workflow.`,
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
const watcher = createUserInstructionConfigWatcher({
|
|
43
|
+
skills: { directories: [] },
|
|
44
|
+
rules: { directories: [] },
|
|
45
|
+
workflows: { directories: [workflowsDir] },
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
await watcher.start();
|
|
50
|
+
expect(listAvailableWorkflowsFromWatcher(watcher)).toEqual([
|
|
51
|
+
{
|
|
52
|
+
id: "enabled-workflow",
|
|
53
|
+
name: "enabled-workflow",
|
|
54
|
+
instructions: "Run enabled workflow.",
|
|
55
|
+
},
|
|
56
|
+
]);
|
|
57
|
+
} finally {
|
|
58
|
+
watcher.stop();
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("expands leading slash commands and preserves trailing user text", async () => {
|
|
63
|
+
const tempRoot = await mkdtemp(join(tmpdir(), "core-runtime-workflows-"));
|
|
64
|
+
tempRoots.push(tempRoot);
|
|
65
|
+
const workflowsDir = join(tempRoot, "workflows");
|
|
66
|
+
await mkdir(workflowsDir, { recursive: true });
|
|
67
|
+
await writeFile(
|
|
68
|
+
join(workflowsDir, "release.md"),
|
|
69
|
+
`---
|
|
70
|
+
name: release
|
|
71
|
+
---
|
|
72
|
+
Run the release workflow.`,
|
|
73
|
+
);
|
|
74
|
+
await writeFile(
|
|
75
|
+
join(workflowsDir, "disabled.md"),
|
|
76
|
+
`---
|
|
77
|
+
name: disabled
|
|
78
|
+
disabled: true
|
|
79
|
+
---
|
|
80
|
+
Do not run this workflow.`,
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
const watcher = createUserInstructionConfigWatcher({
|
|
84
|
+
skills: { directories: [] },
|
|
85
|
+
rules: { directories: [] },
|
|
86
|
+
workflows: { directories: [workflowsDir] },
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
try {
|
|
90
|
+
await watcher.start();
|
|
91
|
+
expect(resolveWorkflowSlashCommandFromWatcher("/release", watcher)).toBe(
|
|
92
|
+
"Run the release workflow.",
|
|
93
|
+
);
|
|
94
|
+
expect(
|
|
95
|
+
resolveWorkflowSlashCommandFromWatcher(" /release ", watcher),
|
|
96
|
+
).toBe(" /release ");
|
|
97
|
+
expect(resolveWorkflowSlashCommandFromWatcher("/disabled", watcher)).toBe(
|
|
98
|
+
"/disabled",
|
|
99
|
+
);
|
|
100
|
+
expect(resolveWorkflowSlashCommandFromWatcher("/missing", watcher)).toBe(
|
|
101
|
+
"/missing",
|
|
102
|
+
);
|
|
103
|
+
expect(
|
|
104
|
+
resolveWorkflowSlashCommandFromWatcher("/release now", watcher),
|
|
105
|
+
).toBe("Run the release workflow. now");
|
|
106
|
+
expect(
|
|
107
|
+
resolveWorkflowSlashCommandFromWatcher(
|
|
108
|
+
"/release use javascript",
|
|
109
|
+
watcher,
|
|
110
|
+
),
|
|
111
|
+
).toBe("Run the release workflow. use javascript");
|
|
112
|
+
expect(
|
|
113
|
+
resolveWorkflowSlashCommandFromWatcher("please run /release", watcher),
|
|
114
|
+
).toBe("please run /release");
|
|
115
|
+
} finally {
|
|
116
|
+
watcher.stop();
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { UserInstructionConfigWatcher } from "../agents";
|
|
2
|
+
import {
|
|
3
|
+
listAvailableRuntimeCommandsForKindFromWatcher,
|
|
4
|
+
resolveRuntimeSlashCommandFromWatcher,
|
|
5
|
+
} from "./commands";
|
|
6
|
+
|
|
7
|
+
export type AvailableWorkflow = {
|
|
8
|
+
id: string;
|
|
9
|
+
name: string;
|
|
10
|
+
instructions: string;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
function matchesLeadingSlashCommand(input: string, name: string): boolean {
|
|
14
|
+
const match = input.match(/^\/(\S+)/);
|
|
15
|
+
return match?.[1] === name;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function listAvailableWorkflowsFromWatcher(
|
|
19
|
+
watcher: UserInstructionConfigWatcher,
|
|
20
|
+
): AvailableWorkflow[] {
|
|
21
|
+
return listAvailableRuntimeCommandsForKindFromWatcher(
|
|
22
|
+
watcher,
|
|
23
|
+
"workflow",
|
|
24
|
+
).map((workflow) => ({
|
|
25
|
+
id: workflow.id,
|
|
26
|
+
name: workflow.name,
|
|
27
|
+
instructions: workflow.instructions,
|
|
28
|
+
}));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Expands a leading slash command (e.g. "/release") to workflow instructions.
|
|
33
|
+
* If the input starts with "/<workflow-name>", that prefix is replaced and the
|
|
34
|
+
* remaining input is preserved unchanged.
|
|
35
|
+
*/
|
|
36
|
+
export function resolveWorkflowSlashCommandFromWatcher(
|
|
37
|
+
input: string,
|
|
38
|
+
watcher: UserInstructionConfigWatcher,
|
|
39
|
+
): string {
|
|
40
|
+
const resolved = resolveRuntimeSlashCommandFromWatcher(input, watcher);
|
|
41
|
+
const matched = listAvailableWorkflowsFromWatcher(watcher).some((workflow) =>
|
|
42
|
+
matchesLeadingSlashCommand(input, workflow.name),
|
|
43
|
+
);
|
|
44
|
+
return matched ? resolved : input;
|
|
45
|
+
}
|
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
import {
|
|
2
|
+
existsSync,
|
|
3
|
+
mkdirSync,
|
|
4
|
+
mkdtempSync,
|
|
5
|
+
readFileSync,
|
|
6
|
+
rmSync,
|
|
7
|
+
unlinkSync,
|
|
8
|
+
writeFileSync,
|
|
9
|
+
} from "node:fs";
|
|
10
|
+
import { tmpdir } from "node:os";
|
|
11
|
+
import { join } from "node:path";
|
|
12
|
+
import type { AgentResult } from "@clinebot/agents";
|
|
13
|
+
import type * as LlmsProviders from "@clinebot/llms/providers";
|
|
14
|
+
import { setClineDir, setHomeDir } from "@clinebot/shared/storage";
|
|
15
|
+
import { nanoid } from "nanoid";
|
|
16
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
17
|
+
import type { SessionSource, SessionStatus } from "../types/common";
|
|
18
|
+
import { DefaultSessionManager } from "./default-session-manager";
|
|
19
|
+
import type { SessionManifest } from "./session-manifest";
|
|
20
|
+
import type { RootSessionArtifacts, SessionRow } from "./session-service";
|
|
21
|
+
|
|
22
|
+
function nowIso(): string {
|
|
23
|
+
return new Date().toISOString();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function createResult(overrides: Partial<AgentResult> = {}): AgentResult {
|
|
27
|
+
return {
|
|
28
|
+
text: "ok",
|
|
29
|
+
iterations: 1,
|
|
30
|
+
finishReason: "completed",
|
|
31
|
+
usage: {
|
|
32
|
+
inputTokens: 1,
|
|
33
|
+
outputTokens: 2,
|
|
34
|
+
totalCost: 0,
|
|
35
|
+
},
|
|
36
|
+
messages: [],
|
|
37
|
+
toolCalls: [],
|
|
38
|
+
durationMs: 1,
|
|
39
|
+
model: {
|
|
40
|
+
id: "mock-model",
|
|
41
|
+
provider: "mock-provider",
|
|
42
|
+
},
|
|
43
|
+
startedAt: new Date("2026-01-01T00:00:00.000Z"),
|
|
44
|
+
endedAt: new Date("2026-01-01T00:00:01.000Z"),
|
|
45
|
+
...overrides,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
class LocalFileSessionService {
|
|
50
|
+
private readonly rows = new Map<string, SessionRow>();
|
|
51
|
+
|
|
52
|
+
constructor(private readonly sessionsDir: string) {}
|
|
53
|
+
|
|
54
|
+
ensureSessionsDir(): string {
|
|
55
|
+
mkdirSync(this.sessionsDir, { recursive: true });
|
|
56
|
+
return this.sessionsDir;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
createRootSessionWithArtifacts(input: {
|
|
60
|
+
sessionId: string;
|
|
61
|
+
source: SessionSource;
|
|
62
|
+
pid: number;
|
|
63
|
+
interactive: boolean;
|
|
64
|
+
provider: string;
|
|
65
|
+
model: string;
|
|
66
|
+
cwd: string;
|
|
67
|
+
workspaceRoot: string;
|
|
68
|
+
teamName?: string;
|
|
69
|
+
enableTools: boolean;
|
|
70
|
+
enableSpawn: boolean;
|
|
71
|
+
enableTeams: boolean;
|
|
72
|
+
prompt?: string;
|
|
73
|
+
startedAt?: string;
|
|
74
|
+
}): RootSessionArtifacts {
|
|
75
|
+
const startedAt = input.startedAt ?? nowIso();
|
|
76
|
+
const sessionId = input.sessionId.trim() || `${Date.now()}_${nanoid(5)}`;
|
|
77
|
+
const sessionPath = join(this.sessionsDir, sessionId);
|
|
78
|
+
mkdirSync(sessionPath, { recursive: true });
|
|
79
|
+
|
|
80
|
+
const manifestPath = join(sessionPath, `${sessionId}.json`);
|
|
81
|
+
const transcriptPath = join(sessionPath, `${sessionId}.log`);
|
|
82
|
+
const hookPath = join(sessionPath, `${sessionId}.hooks.jsonl`);
|
|
83
|
+
const messagesPath = join(sessionPath, `${sessionId}.messages.json`);
|
|
84
|
+
const prompt = input.prompt?.trim() || undefined;
|
|
85
|
+
const manifest: SessionManifest = {
|
|
86
|
+
version: 1,
|
|
87
|
+
session_id: sessionId,
|
|
88
|
+
source: input.source,
|
|
89
|
+
pid: input.pid,
|
|
90
|
+
started_at: startedAt,
|
|
91
|
+
status: "running",
|
|
92
|
+
interactive: input.interactive,
|
|
93
|
+
provider: input.provider,
|
|
94
|
+
model: input.model,
|
|
95
|
+
cwd: input.cwd,
|
|
96
|
+
workspace_root: input.workspaceRoot,
|
|
97
|
+
team_name: input.teamName,
|
|
98
|
+
enable_tools: input.enableTools,
|
|
99
|
+
enable_spawn: input.enableSpawn,
|
|
100
|
+
enable_teams: input.enableTeams,
|
|
101
|
+
prompt,
|
|
102
|
+
messages_path: messagesPath,
|
|
103
|
+
};
|
|
104
|
+
writeFileSync(
|
|
105
|
+
manifestPath,
|
|
106
|
+
`${JSON.stringify(manifest, null, 2)}\n`,
|
|
107
|
+
"utf8",
|
|
108
|
+
);
|
|
109
|
+
writeFileSync(transcriptPath, "", "utf8");
|
|
110
|
+
writeFileSync(hookPath, "", "utf8");
|
|
111
|
+
writeFileSync(
|
|
112
|
+
messagesPath,
|
|
113
|
+
`${JSON.stringify({ version: 1, updated_at: startedAt, messages: [] }, null, 2)}\n`,
|
|
114
|
+
"utf8",
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
this.rows.set(sessionId, {
|
|
118
|
+
sessionId,
|
|
119
|
+
source: input.source,
|
|
120
|
+
pid: input.pid,
|
|
121
|
+
startedAt,
|
|
122
|
+
endedAt: null,
|
|
123
|
+
exitCode: null,
|
|
124
|
+
status: "running",
|
|
125
|
+
statusLock: 0,
|
|
126
|
+
interactive: input.interactive,
|
|
127
|
+
provider: input.provider,
|
|
128
|
+
model: input.model,
|
|
129
|
+
cwd: input.cwd,
|
|
130
|
+
workspaceRoot: input.workspaceRoot,
|
|
131
|
+
teamName: input.teamName ?? null,
|
|
132
|
+
enableTools: input.enableTools,
|
|
133
|
+
enableSpawn: input.enableSpawn,
|
|
134
|
+
enableTeams: input.enableTeams,
|
|
135
|
+
parentSessionId: null,
|
|
136
|
+
parentAgentId: null,
|
|
137
|
+
agentId: null,
|
|
138
|
+
conversationId: null,
|
|
139
|
+
isSubagent: false,
|
|
140
|
+
prompt: prompt ?? null,
|
|
141
|
+
transcriptPath,
|
|
142
|
+
hookPath,
|
|
143
|
+
messagesPath,
|
|
144
|
+
updatedAt: startedAt,
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
return {
|
|
148
|
+
manifestPath,
|
|
149
|
+
transcriptPath,
|
|
150
|
+
hookPath,
|
|
151
|
+
messagesPath,
|
|
152
|
+
manifest,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
persistSessionMessages(
|
|
157
|
+
sessionId: string,
|
|
158
|
+
messages: LlmsProviders.Message[],
|
|
159
|
+
systemPrompt?: string,
|
|
160
|
+
): void {
|
|
161
|
+
const row = this.rows.get(sessionId);
|
|
162
|
+
if (!row?.messagesPath) {
|
|
163
|
+
throw new Error(`session not found: ${sessionId}`);
|
|
164
|
+
}
|
|
165
|
+
const payload: {
|
|
166
|
+
version: number;
|
|
167
|
+
updated_at: string;
|
|
168
|
+
systemPrompt?: string;
|
|
169
|
+
messages: LlmsProviders.Message[];
|
|
170
|
+
} = { version: 1, updated_at: nowIso(), messages };
|
|
171
|
+
if (systemPrompt !== undefined && systemPrompt !== "") {
|
|
172
|
+
payload.systemPrompt = systemPrompt;
|
|
173
|
+
}
|
|
174
|
+
writeFileSync(
|
|
175
|
+
row.messagesPath,
|
|
176
|
+
`${JSON.stringify(payload, null, 2)}\n`,
|
|
177
|
+
"utf8",
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
updateSessionStatus(
|
|
182
|
+
sessionId: string,
|
|
183
|
+
status: SessionStatus,
|
|
184
|
+
exitCode?: number | null,
|
|
185
|
+
): { updated: boolean; endedAt?: string } {
|
|
186
|
+
const row = this.rows.get(sessionId);
|
|
187
|
+
if (!row) {
|
|
188
|
+
return { updated: false };
|
|
189
|
+
}
|
|
190
|
+
const endedAt = nowIso();
|
|
191
|
+
row.status = status;
|
|
192
|
+
row.endedAt = endedAt;
|
|
193
|
+
row.exitCode = typeof exitCode === "number" ? exitCode : null;
|
|
194
|
+
row.updatedAt = endedAt;
|
|
195
|
+
row.statusLock = row.statusLock + 1;
|
|
196
|
+
return { updated: true, endedAt };
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
writeSessionManifest(manifestPath: string, manifest: SessionManifest): void {
|
|
200
|
+
writeFileSync(
|
|
201
|
+
manifestPath,
|
|
202
|
+
`${JSON.stringify(manifest, null, 2)}\n`,
|
|
203
|
+
"utf8",
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
listSessions(limit = 200): SessionRow[] {
|
|
208
|
+
return Array.from(this.rows.values()).slice(0, limit);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
deleteSession(sessionId: string): { deleted: boolean } {
|
|
212
|
+
const row = this.rows.get(sessionId);
|
|
213
|
+
if (!row) {
|
|
214
|
+
return { deleted: false };
|
|
215
|
+
}
|
|
216
|
+
this.rows.delete(sessionId);
|
|
217
|
+
unlinkSync(row.transcriptPath);
|
|
218
|
+
unlinkSync(row.hookPath);
|
|
219
|
+
unlinkSync(row.messagesPath ?? "");
|
|
220
|
+
unlinkSync(join(this.sessionsDir, sessionId, `${sessionId}.json`));
|
|
221
|
+
return { deleted: true };
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
describe("DefaultSessionManager e2e", () => {
|
|
226
|
+
const envSnapshot = {
|
|
227
|
+
HOME: process.env.HOME,
|
|
228
|
+
CLINE_DIR: process.env.CLINE_DIR,
|
|
229
|
+
};
|
|
230
|
+
const tempDirs: string[] = [];
|
|
231
|
+
let isolatedHomeDir = "";
|
|
232
|
+
|
|
233
|
+
beforeEach(() => {
|
|
234
|
+
isolatedHomeDir = mkdtempSync(join(tmpdir(), "core-session-home-"));
|
|
235
|
+
process.env.HOME = isolatedHomeDir;
|
|
236
|
+
process.env.CLINE_DIR = join(isolatedHomeDir, ".cline");
|
|
237
|
+
setHomeDir(isolatedHomeDir);
|
|
238
|
+
setClineDir(process.env.CLINE_DIR);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
afterEach(() => {
|
|
242
|
+
process.env.HOME = envSnapshot.HOME;
|
|
243
|
+
process.env.CLINE_DIR = envSnapshot.CLINE_DIR;
|
|
244
|
+
setHomeDir(envSnapshot.HOME ?? "~");
|
|
245
|
+
setClineDir(envSnapshot.CLINE_DIR ?? join("~", ".cline"));
|
|
246
|
+
for (const dir of tempDirs.splice(0)) {
|
|
247
|
+
rmSync(dir, { recursive: true, force: true });
|
|
248
|
+
}
|
|
249
|
+
rmSync(isolatedHomeDir, { recursive: true, force: true });
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it("runs an interactive lifecycle with real artifact files", async () => {
|
|
253
|
+
const sessionsDir = mkdtempSync(join(tmpdir(), "core-e2e-sessions-"));
|
|
254
|
+
tempDirs.push(sessionsDir);
|
|
255
|
+
|
|
256
|
+
const sessionService = new LocalFileSessionService(sessionsDir);
|
|
257
|
+
const runtimeShutdown = vi.fn();
|
|
258
|
+
const runtimeBuilder = {
|
|
259
|
+
build: vi.fn().mockReturnValue({
|
|
260
|
+
tools: [],
|
|
261
|
+
shutdown: runtimeShutdown,
|
|
262
|
+
}),
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
let messages: LlmsProviders.Message[] = [];
|
|
266
|
+
let turn = 0;
|
|
267
|
+
const run = vi.fn(async (prompt: string) => {
|
|
268
|
+
turn += 1;
|
|
269
|
+
messages = [
|
|
270
|
+
...messages,
|
|
271
|
+
{
|
|
272
|
+
role: "user",
|
|
273
|
+
content: [{ type: "text", text: prompt }],
|
|
274
|
+
},
|
|
275
|
+
{
|
|
276
|
+
role: "assistant",
|
|
277
|
+
content: [{ type: "text", text: `reply:${turn}:${prompt}` }],
|
|
278
|
+
},
|
|
279
|
+
] as LlmsProviders.Message[];
|
|
280
|
+
return createResult({
|
|
281
|
+
text: `reply:${turn}:${prompt}`,
|
|
282
|
+
messages: [...messages],
|
|
283
|
+
});
|
|
284
|
+
});
|
|
285
|
+
const continueFn = vi.fn(async (prompt: string) => {
|
|
286
|
+
turn += 1;
|
|
287
|
+
messages = [
|
|
288
|
+
...messages,
|
|
289
|
+
{
|
|
290
|
+
role: "user",
|
|
291
|
+
content: [{ type: "text", text: prompt }],
|
|
292
|
+
},
|
|
293
|
+
{
|
|
294
|
+
role: "assistant",
|
|
295
|
+
content: [{ type: "text", text: `reply:${turn}:${prompt}` }],
|
|
296
|
+
},
|
|
297
|
+
] as LlmsProviders.Message[];
|
|
298
|
+
return createResult({
|
|
299
|
+
text: `reply:${turn}:${prompt}`,
|
|
300
|
+
messages: [...messages],
|
|
301
|
+
});
|
|
302
|
+
});
|
|
303
|
+
const agentShutdown = vi.fn().mockResolvedValue(undefined);
|
|
304
|
+
|
|
305
|
+
const manager = new DefaultSessionManager({
|
|
306
|
+
distinctId: `test-${nanoid(5)}`,
|
|
307
|
+
sessionService: sessionService as never,
|
|
308
|
+
runtimeBuilder: runtimeBuilder as never,
|
|
309
|
+
createAgent: () =>
|
|
310
|
+
({
|
|
311
|
+
run,
|
|
312
|
+
continue: continueFn,
|
|
313
|
+
abort: vi.fn(),
|
|
314
|
+
restore: vi.fn((baseline: LlmsProviders.Message[]) => {
|
|
315
|
+
messages = [...baseline];
|
|
316
|
+
}),
|
|
317
|
+
updateConnection: vi.fn(),
|
|
318
|
+
shutdown: agentShutdown,
|
|
319
|
+
getMessages: vi.fn(() => [...messages]),
|
|
320
|
+
messages: [],
|
|
321
|
+
}) as never,
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
const started = await manager.start({
|
|
325
|
+
interactive: true,
|
|
326
|
+
config: {
|
|
327
|
+
providerId: "anthropic",
|
|
328
|
+
modelId: "claude-sonnet-4-6",
|
|
329
|
+
apiKey: "test-key",
|
|
330
|
+
cwd: sessionsDir,
|
|
331
|
+
systemPrompt: "You are a test agent",
|
|
332
|
+
enableTools: false,
|
|
333
|
+
enableSpawnAgent: false,
|
|
334
|
+
enableAgentTeams: false,
|
|
335
|
+
},
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
expect(started.sessionId.length).toBeGreaterThan(0);
|
|
339
|
+
expect(existsSync(started.manifestPath)).toBe(false);
|
|
340
|
+
expect(existsSync(started.messagesPath)).toBe(false);
|
|
341
|
+
|
|
342
|
+
const first = await manager.send({
|
|
343
|
+
sessionId: started.sessionId,
|
|
344
|
+
prompt: "first prompt",
|
|
345
|
+
});
|
|
346
|
+
const second = await manager.send({
|
|
347
|
+
sessionId: started.sessionId,
|
|
348
|
+
prompt: "second prompt",
|
|
349
|
+
});
|
|
350
|
+
expect(existsSync(started.manifestPath)).toBe(true);
|
|
351
|
+
expect(existsSync(started.messagesPath)).toBe(true);
|
|
352
|
+
|
|
353
|
+
expect(first?.text).toContain("first prompt");
|
|
354
|
+
expect(second?.text).toContain("second prompt");
|
|
355
|
+
expect(run).toHaveBeenCalledTimes(1);
|
|
356
|
+
expect(continueFn).toHaveBeenCalledTimes(1);
|
|
357
|
+
|
|
358
|
+
const persistedMessages = await manager.readMessages(started.sessionId);
|
|
359
|
+
expect(persistedMessages.length).toBeGreaterThan(0);
|
|
360
|
+
expect(JSON.stringify(persistedMessages)).toContain("second prompt");
|
|
361
|
+
|
|
362
|
+
const listed = await manager.list(20);
|
|
363
|
+
expect(
|
|
364
|
+
listed.some((session) => session.sessionId === started.sessionId),
|
|
365
|
+
).toBe(true);
|
|
366
|
+
|
|
367
|
+
await manager.stop(started.sessionId);
|
|
368
|
+
const stopped = await manager.get(started.sessionId);
|
|
369
|
+
expect(stopped?.status).toBe("cancelled");
|
|
370
|
+
expect(stopped?.exitCode).toBe(0);
|
|
371
|
+
expect(agentShutdown).toHaveBeenCalledTimes(1);
|
|
372
|
+
expect(runtimeShutdown).toHaveBeenCalledTimes(1);
|
|
373
|
+
const parsedManifest = JSON.parse(
|
|
374
|
+
readFileSync(started.manifestPath, "utf8"),
|
|
375
|
+
) as SessionManifest;
|
|
376
|
+
expect(parsedManifest.status).toBe("cancelled");
|
|
377
|
+
|
|
378
|
+
const deleted = await manager.delete(started.sessionId);
|
|
379
|
+
expect(deleted).toBe(true);
|
|
380
|
+
expect(await manager.get(started.sessionId)).toBeUndefined();
|
|
381
|
+
expect(existsSync(started.manifestPath)).toBe(false);
|
|
382
|
+
expect(existsSync(started.messagesPath)).toBe(false);
|
|
383
|
+
});
|
|
384
|
+
});
|