@ftrdotdev/pi-tui 0.8.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +225 -0
- package/README.md +5 -0
- package/package.json +18 -15
- package/src/activity-service.test.ts +97 -0
- package/src/activity.test.ts +1 -1
- package/src/activity.ts +38 -0
- package/src/chrome/budget.test.ts +345 -0
- package/src/chrome/budget.ts +220 -0
- package/src/chrome/declarations.ts +303 -0
- package/src/chrome/facts.test.ts +165 -0
- package/src/chrome/facts.ts +267 -0
- package/src/chrome/frame.ts +45 -0
- package/src/chrome/laws.test.ts +231 -0
- package/src/chrome/port.test.ts +164 -0
- package/src/chrome/port.ts +67 -0
- package/src/chrome/service-provider.test.ts +383 -0
- package/src/chrome/service-provider.ts +55 -0
- package/src/commands/git-execution.test.ts +5 -2
- package/src/commands/git-execution.ts +27 -15
- package/src/commands/git-status.test.ts +47 -15
- package/src/commands/git-status.ts +83 -31
- package/src/components/bordered-box.test.ts +12 -15
- package/src/components/bordered-box.ts +11 -7
- package/src/components/define-component.ts +1 -1
- package/src/components/index.ts +7 -0
- package/src/components/row-list.test.ts +310 -0
- package/src/components/row-list.ts +272 -0
- package/src/config.test.ts +118 -3
- package/src/config.ts +46 -40
- package/src/frame/box-frame.test.ts +53 -7
- package/src/harness-policy.test.ts +47 -0
- package/src/icons.test.ts +19 -0
- package/src/icons.ts +50 -31
- package/src/index.test.ts +135 -0
- package/src/index.ts +118 -119
- package/src/layers.test.ts +94 -0
- package/src/layers.ts +92 -0
- package/src/runtime-service.test.ts +218 -0
- package/src/runtime-service.ts +103 -0
- package/src/runtime.test.ts +165 -66
- package/src/runtime.ts +127 -93
- package/src/session-lifecycle-service.test.ts +95 -0
- package/src/session-lifecycle.test.ts +77 -0
- package/src/session-lifecycle.ts +21 -0
- package/src/state.test.ts +132 -3
- package/src/state.ts +43 -13
- package/src/telemetry-service.test.ts +250 -0
- package/src/telemetry.test.ts +322 -0
- package/src/telemetry.ts +130 -23
- package/src/ui/editor-tint.test.ts +58 -45
- package/src/ui/editor-tint.ts +1 -1
- package/src/ui/editor.ts +117 -53
- package/src/ui/footer.test.ts +349 -120
- package/src/ui/footer.ts +147 -124
- package/src/ui/header.test.ts +203 -223
- package/src/ui/header.ts +32 -96
- package/src/ui/preview.test.ts +73 -70
- package/src/ui/preview.ts +48 -33
- package/src/ui/render-helpers.test.ts +82 -13
- package/src/ui/render-helpers.ts +110 -122
- package/src/ui/settings-command.test.ts +70 -11
- package/src/ui/settings-command.ts +30 -45
- package/src/ui/theme-color.test.ts +4 -3
- package/src/usage-totals-service.test.ts +230 -0
- package/src/usage-totals-service.ts +59 -0
- package/src/utils.test.ts +46 -50
- package/src/utils.ts +41 -239
- package/src/vim/editor-host.test.ts +201 -75
- package/src/vim/editor-state.ts +2 -2
- package/src/vim/mode-indicator.test.ts +6 -5
- package/src/vim/mode-indicator.ts +13 -9
- package/src/vim/selection-render.ts +8 -4
- package/src/vim/text-model.test.ts +52 -14
- package/src/vim/text-model.ts +1 -1
- package/src/vim/vim-router.test.ts +3 -0
- package/src/vim/vim-router.ts +1 -1
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { afterEach, assert, it, vi } from "@effect/vitest";
|
|
2
|
+
import { themeDouble } from "@ftrdotdev/pi-test-kit";
|
|
3
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
7
|
+
import { extensionApiDouble, mountCtxDouble } from "../test-support/doubles.ts";
|
|
8
|
+
import tui from "./index.ts";
|
|
9
|
+
|
|
10
|
+
// ---------------------------------------------------------------------------
|
|
11
|
+
// The extension entry (src/index.ts) has no other test: this suite runs the
|
|
12
|
+
// real `main` over the production layer graph and pins the wiring the manual
|
|
13
|
+
// smoke used to be the only evidence for (mount, session resets, unmount).
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
|
|
16
|
+
/** The variable `getAgentDir()` reads, inlined (pi does not re-export it). */
|
|
17
|
+
const AGENT_DIR_ENV = "PI_CODING_AGENT_DIR";
|
|
18
|
+
|
|
19
|
+
type Handler = (event: unknown, ctx: ExtensionContext) => unknown;
|
|
20
|
+
|
|
21
|
+
interface Harness {
|
|
22
|
+
readonly registered: string[];
|
|
23
|
+
fire(event: string): Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const dirs: string[] = [];
|
|
27
|
+
|
|
28
|
+
const tempDir = (prefix: string): string => {
|
|
29
|
+
const dir = mkdtempSync(join(tmpdir(), prefix));
|
|
30
|
+
dirs.push(dir);
|
|
31
|
+
return dir;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
afterEach(() => {
|
|
35
|
+
for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true });
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
/** Registers the extension, then fires its handlers in registration order. */
|
|
39
|
+
const harnessOver = (ctx: ExtensionContext): Harness => {
|
|
40
|
+
const handlers = new Map<string, Handler[]>();
|
|
41
|
+
const registered: string[] = [];
|
|
42
|
+
|
|
43
|
+
tui(
|
|
44
|
+
extensionApiDouble({
|
|
45
|
+
registerTool: (tool) => registered.push(`tool:${tool.name}`),
|
|
46
|
+
registerCommand: (name) => registered.push(`command:${name}`),
|
|
47
|
+
registerEntryRenderer: (type) => registered.push(`renderer:${type}`),
|
|
48
|
+
appendEntry: () => {},
|
|
49
|
+
getThinkingLevel: () => "low",
|
|
50
|
+
on: (event, handler) => {
|
|
51
|
+
const list = handlers.get(event) ?? [];
|
|
52
|
+
list.push(handler as Handler);
|
|
53
|
+
handlers.set(event, list);
|
|
54
|
+
},
|
|
55
|
+
}),
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
registered,
|
|
60
|
+
async fire(event) {
|
|
61
|
+
for (const handler of handlers.get(event) ?? []) await handler({}, ctx);
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
it("the extension mounts the chrome and unmounts on shutdown", async () => {
|
|
67
|
+
// Arrange: an isolated agent dir. The working directory is the package
|
|
68
|
+
// root, which exists for the whole run: the session start's background git
|
|
69
|
+
// and runtime refresh outlive the test, so a removed cwd would fail the
|
|
70
|
+
// spawn after teardown.
|
|
71
|
+
vi.stubEnv(AGENT_DIR_ENV, tempDir("pi-tui-agent-"));
|
|
72
|
+
const cwd = process.cwd();
|
|
73
|
+
const headers: unknown[] = [];
|
|
74
|
+
const footers: unknown[] = [];
|
|
75
|
+
const editors: unknown[] = [];
|
|
76
|
+
const unmounted: string[] = [];
|
|
77
|
+
const ctx = mountCtxDouble({
|
|
78
|
+
cwd,
|
|
79
|
+
model: {
|
|
80
|
+
provider: "anthropic",
|
|
81
|
+
id: "claude-sonnet-4-0",
|
|
82
|
+
contextWindow: 200000,
|
|
83
|
+
reasoning: true,
|
|
84
|
+
},
|
|
85
|
+
hasUI: true,
|
|
86
|
+
mode: "tui",
|
|
87
|
+
getContextUsage: () => undefined,
|
|
88
|
+
sessionManager: { getCwd: () => cwd, getEntries: () => [] },
|
|
89
|
+
ui: {
|
|
90
|
+
theme: themeDouble(),
|
|
91
|
+
notify: () => {},
|
|
92
|
+
setHeader: (factory) => {
|
|
93
|
+
headers.push(factory);
|
|
94
|
+
if (factory === undefined) unmounted.push("header");
|
|
95
|
+
},
|
|
96
|
+
setFooter: (factory) => {
|
|
97
|
+
footers.push(factory);
|
|
98
|
+
if (factory === undefined) unmounted.push("footer");
|
|
99
|
+
},
|
|
100
|
+
setEditorComponent: (factory) => {
|
|
101
|
+
editors.push(factory);
|
|
102
|
+
if (factory === undefined) unmounted.push("editor");
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
});
|
|
106
|
+
const harness = harnessOver(ctx);
|
|
107
|
+
|
|
108
|
+
// Assert: the registrations happen at construction, before any session.
|
|
109
|
+
assert.ok(harness.registered.includes("tool:preview"));
|
|
110
|
+
assert.ok(harness.registered.includes("renderer:preview"));
|
|
111
|
+
assert.ok(harness.registered.includes("command:tui"));
|
|
112
|
+
assert.ok(harness.registered.includes("command:preview"));
|
|
113
|
+
|
|
114
|
+
// Act: a session mounts all three chrome surfaces from the services.
|
|
115
|
+
await harness.fire("session_start");
|
|
116
|
+
assert.strictEqual(headers.length, 1);
|
|
117
|
+
assert.strictEqual(footers.length, 1);
|
|
118
|
+
assert.strictEqual(editors.length, 1);
|
|
119
|
+
assert.strictEqual(typeof headers[0], "function");
|
|
120
|
+
assert.strictEqual(typeof footers[0], "function");
|
|
121
|
+
assert.strictEqual(typeof editors[0], "function");
|
|
122
|
+
|
|
123
|
+
// Act: a run opens and closes through the service-backed tracker.
|
|
124
|
+
await harness.fire("agent_start");
|
|
125
|
+
await harness.fire("agent_end");
|
|
126
|
+
|
|
127
|
+
// Act: shutdown unmounts every surface.
|
|
128
|
+
await harness.fire("session_shutdown");
|
|
129
|
+
assert.deepStrictEqual(unmounted, ["header", "footer", "editor"]);
|
|
130
|
+
|
|
131
|
+
// Act: a later session mounts again, on the same program.
|
|
132
|
+
await harness.fire("session_start");
|
|
133
|
+
assert.strictEqual(headers.length, 3);
|
|
134
|
+
assert.strictEqual(typeof headers[2], "function");
|
|
135
|
+
});
|
package/src/index.ts
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
|
+
// This module is the application entry point: it composes the TUI layers and
|
|
2
|
+
// provides them once here, which is the pattern the rule asks for.
|
|
3
|
+
/* oxlint-disable effecttsgo/strict-effect-provide */
|
|
1
4
|
import { homedir } from "node:os";
|
|
2
5
|
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
3
6
|
import { type TuiConfig, DEFAULT_CONFIG, TuiConfigService } from "./config.ts";
|
|
4
7
|
import { installEditor } from "./ui/editor.ts";
|
|
5
8
|
import { installFooter } from "./ui/footer.ts";
|
|
6
9
|
import { installHeader } from "./ui/header.ts";
|
|
10
|
+
import { resolveGlyphs } from "./icons.ts";
|
|
11
|
+
import { type ChromePort } from "./chrome/port.ts";
|
|
12
|
+
import { makeChromePortBuilder, type TuiServiceHandles } from "./chrome/service-provider.ts";
|
|
7
13
|
import { GitStatus, GitStatusService } from "./commands/git-status.ts";
|
|
8
14
|
import { GitExecutionService } from "./commands/git-execution.ts";
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
15
|
+
import { RuntimeInfoService } from "./runtime-service.ts";
|
|
16
|
+
import { SessionLifecycleService } from "./session-lifecycle.ts";
|
|
11
17
|
import { PreviewService, registerPreview } from "./ui/preview.ts";
|
|
12
18
|
import { registerSettingsCommand } from "./ui/settings-command.ts";
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
} from "./state.ts";
|
|
21
|
-
|
|
22
|
-
import { Effect, Layer } from "effect";
|
|
23
|
-
import * as NodePath from "@effect/platform-node/NodePath";
|
|
24
|
-
import * as NodeFileSystem from "@effect/platform-node/NodeFileSystem";
|
|
25
|
-
import * as NodeChildProcessSpawner from "@effect/platform-node/NodeChildProcessSpawner";
|
|
19
|
+
import { composeNotifyLine, TurnTelemetryService } from "./telemetry.ts";
|
|
20
|
+
import { ActivityService, toTrackerEvent, type TrackerEventSource } from "./activity.ts";
|
|
21
|
+
import { getModelMeta } from "./state.ts";
|
|
22
|
+
import { UsageTotalsService } from "./usage-totals-service.ts";
|
|
23
|
+
import { createTuiSessionHost, tuiServicesLayer, type TuiSessionHost } from "./layers.ts";
|
|
24
|
+
|
|
25
|
+
import { Effect } from "effect";
|
|
26
26
|
|
|
27
27
|
import { VimRouter } from "./vim/vim-router.ts";
|
|
28
28
|
import { EditorTintService } from "./ui/editor-tint.ts";
|
|
@@ -36,28 +36,36 @@ function isTuiContext(ctx: ExtensionContext): boolean {
|
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
const main = Effect.fn("tui/main")(function* (pi: ExtensionAPI) {
|
|
39
|
+
const main = Effect.fn("tui/main")(function* (pi: ExtensionAPI, host: TuiSessionHost) {
|
|
40
40
|
const effectContext = yield* Effect.context<
|
|
41
41
|
GitExecutionService | GitStatusService | PreviewService
|
|
42
42
|
>();
|
|
43
43
|
|
|
44
|
-
const
|
|
45
|
-
const
|
|
46
|
-
const
|
|
44
|
+
const lifecycle = yield* SessionLifecycleService;
|
|
45
|
+
const activity = yield* ActivityService;
|
|
46
|
+
const telemetry = yield* TurnTelemetryService;
|
|
47
|
+
const usage = yield* UsageTotalsService;
|
|
48
|
+
const runtime = yield* RuntimeInfoService;
|
|
47
49
|
|
|
48
50
|
const conf = yield* TuiConfigService;
|
|
49
51
|
const tint = yield* EditorTintService;
|
|
50
52
|
|
|
53
|
+
// The one construction point (D11): resolve the handles here, then build the
|
|
54
|
+
// builder. No module-level cell and no second layer build.
|
|
55
|
+
const handles: TuiServiceHandles = { activity, usage, runtime };
|
|
56
|
+
const buildPort = makeChromePortBuilder(host, handles);
|
|
57
|
+
|
|
51
58
|
let config: TuiConfig = structuredClone(DEFAULT_CONFIG);
|
|
52
59
|
let active = false;
|
|
53
|
-
let lastCtx: ExtensionContext | undefined;
|
|
54
60
|
let requestFooterRender: (() => void) | undefined;
|
|
55
61
|
let workingTimer: ReturnType<typeof setInterval> | undefined;
|
|
56
62
|
let cleanupHeader: (() => void) | undefined;
|
|
57
63
|
let cleanupFooter: (() => void) | undefined;
|
|
58
64
|
let cleanupEditor: (() => void) | undefined;
|
|
65
|
+
/** The single mount provider every chrome surface reads (survives uninstall for the notify line). */
|
|
66
|
+
let mountedPort: ChromePort | undefined;
|
|
59
67
|
|
|
60
|
-
const getThinkingLevel = () => (
|
|
68
|
+
const getThinkingLevel = () => (lifecycle.isCurrent() ? pi.getThinkingLevel() : "off");
|
|
61
69
|
|
|
62
70
|
const applyUi = (ctx: ExtensionContext) => {
|
|
63
71
|
if (!isTuiContext(ctx)) return;
|
|
@@ -66,22 +74,26 @@ const main = Effect.fn("tui/main")(function* (pi: ExtensionAPI) {
|
|
|
66
74
|
return;
|
|
67
75
|
}
|
|
68
76
|
if (!active) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
ctx,
|
|
72
|
-
() =>
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
77
|
+
const port = buildPort(ctx, {
|
|
78
|
+
config: () => config,
|
|
79
|
+
model: () => getModelMeta(ctx, getThinkingLevel),
|
|
80
|
+
extensionStatuses: () => new Map(), // replaced per mount by footerData
|
|
81
|
+
});
|
|
82
|
+
mountedPort = port;
|
|
83
|
+
cleanupHeader = installHeader(ctx, port);
|
|
84
|
+
cleanupFooter = installFooter(ctx, port, {
|
|
85
|
+
setRequestRender: (fn) => {
|
|
86
|
+
requestFooterRender = fn ?? undefined;
|
|
87
|
+
},
|
|
88
|
+
scheduleGitRefresh: () => {
|
|
89
|
+
void scheduleGitRefresh(ctx);
|
|
82
90
|
},
|
|
83
|
-
);
|
|
84
|
-
cleanupEditor = installEditor(pi, ctx, {
|
|
91
|
+
});
|
|
92
|
+
cleanupEditor = installEditor(pi, ctx, {
|
|
93
|
+
getVimEnabled: () => config.vim,
|
|
94
|
+
getGlyphs: () => resolveGlyphs(port.facts.config().icons.mode),
|
|
95
|
+
tint,
|
|
96
|
+
});
|
|
85
97
|
active = true;
|
|
86
98
|
}
|
|
87
99
|
};
|
|
@@ -101,8 +113,8 @@ const main = Effect.fn("tui/main")(function* (pi: ExtensionAPI) {
|
|
|
101
113
|
};
|
|
102
114
|
|
|
103
115
|
const scheduleGitRefresh = async (ctx: ExtensionContext) => {
|
|
104
|
-
if (!
|
|
105
|
-
const generation =
|
|
116
|
+
if (!lifecycle.isCurrent()) return;
|
|
117
|
+
const generation = lifecycle.currentGeneration();
|
|
106
118
|
const cwd = ctx.cwd;
|
|
107
119
|
const git = await Effect.runPromiseWith(effectContext)(
|
|
108
120
|
Effect.gen(function* () {
|
|
@@ -125,23 +137,19 @@ const main = Effect.fn("tui/main")(function* (pi: ExtensionAPI) {
|
|
|
125
137
|
}),
|
|
126
138
|
),
|
|
127
139
|
);
|
|
128
|
-
if (!
|
|
129
|
-
state.git = git;
|
|
140
|
+
if (!lifecycle.isCurrent(generation)) return;
|
|
141
|
+
host.state.git = git;
|
|
130
142
|
requestFooterRender?.();
|
|
131
143
|
};
|
|
132
144
|
|
|
133
145
|
const refreshRuntime = async (ctx: ExtensionContext) => {
|
|
134
|
-
if (!
|
|
135
|
-
|
|
136
|
-
const cwd = ctx.cwd;
|
|
137
|
-
const runtime = await readRuntimeInfo(cwd);
|
|
138
|
-
if (!sessionLifecycle.isCurrent(generation)) return;
|
|
139
|
-
state.runtime = runtime;
|
|
146
|
+
if (!lifecycle.isCurrent()) return;
|
|
147
|
+
await Effect.runPromiseWith(effectContext)(runtime.refresh(ctx.cwd));
|
|
140
148
|
requestFooterRender?.();
|
|
141
149
|
};
|
|
142
150
|
|
|
143
151
|
const refreshInteractiveState = (ctx: ExtensionContext, project = false) => {
|
|
144
|
-
if (!
|
|
152
|
+
if (!lifecycle.isCurrent() || !ctx.hasUI) return;
|
|
145
153
|
if (project) {
|
|
146
154
|
void scheduleGitRefresh(ctx);
|
|
147
155
|
void refreshRuntime(ctx);
|
|
@@ -152,7 +160,7 @@ const main = Effect.fn("tui/main")(function* (pi: ExtensionAPI) {
|
|
|
152
160
|
const startWorkingTimer = () => {
|
|
153
161
|
stopWorkingTimer();
|
|
154
162
|
const tick = () => {
|
|
155
|
-
if (!
|
|
163
|
+
if (!lifecycle.isCurrent() || !active) return;
|
|
156
164
|
requestFooterRender?.();
|
|
157
165
|
};
|
|
158
166
|
tick();
|
|
@@ -164,7 +172,7 @@ const main = Effect.fn("tui/main")(function* (pi: ExtensionAPI) {
|
|
|
164
172
|
};
|
|
165
173
|
|
|
166
174
|
const stopWorkingTimer = () => {
|
|
167
|
-
if (workingTimer) {
|
|
175
|
+
if (workingTimer !== undefined) {
|
|
168
176
|
clearInterval(workingTimer);
|
|
169
177
|
workingTimer = undefined;
|
|
170
178
|
}
|
|
@@ -173,12 +181,13 @@ const main = Effect.fn("tui/main")(function* (pi: ExtensionAPI) {
|
|
|
173
181
|
pi.on("session_start", async (_event, ctx) =>
|
|
174
182
|
Effect.runPromiseWith(effectContext)(
|
|
175
183
|
Effect.gen(function* () {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
state.sessionStartEpoch = Date.now();
|
|
179
|
-
state.lastDoneIn = undefined;
|
|
180
|
-
|
|
181
|
-
|
|
184
|
+
lifecycle.start();
|
|
185
|
+
host.ctx = ctx;
|
|
186
|
+
host.state.sessionStartEpoch = Date.now();
|
|
187
|
+
host.state.lastDoneIn = undefined;
|
|
188
|
+
activity.reset();
|
|
189
|
+
telemetry.reset();
|
|
190
|
+
usage.reset();
|
|
182
191
|
|
|
183
192
|
config = yield* conf.load.pipe(Effect.mapError((e) => ctx.ui.notify(e.message, "error")));
|
|
184
193
|
|
|
@@ -190,36 +199,37 @@ const main = Effect.fn("tui/main")(function* (pi: ExtensionAPI) {
|
|
|
190
199
|
);
|
|
191
200
|
|
|
192
201
|
pi.on("session_shutdown", async (_event, ctx) => {
|
|
193
|
-
|
|
202
|
+
lifecycle.shutdown();
|
|
194
203
|
stopWorkingTimer();
|
|
195
204
|
// Session handoff resets the timer state machine: the next session
|
|
196
205
|
// starts from a clean tracker (matches the session_start reset).
|
|
197
|
-
|
|
198
|
-
|
|
206
|
+
activity.reset();
|
|
207
|
+
telemetry.reset();
|
|
208
|
+
host.state.lastDoneIn = undefined;
|
|
199
209
|
if (active) {
|
|
200
210
|
uninstallUi(ctx);
|
|
201
211
|
}
|
|
202
|
-
|
|
212
|
+
host.ctx = undefined;
|
|
203
213
|
});
|
|
204
214
|
|
|
205
215
|
pi.on("agent_start", (event, _ctx) => {
|
|
206
|
-
|
|
207
|
-
if (!
|
|
208
|
-
|
|
209
|
-
state.lastDoneIn = undefined;
|
|
216
|
+
telemetry.handle(event);
|
|
217
|
+
if (!lifecycle.isCurrent()) return;
|
|
218
|
+
activity.handle({ type: "run_start", now: Date.now() });
|
|
219
|
+
host.state.lastDoneIn = undefined;
|
|
210
220
|
startWorkingTimer();
|
|
211
221
|
});
|
|
212
222
|
|
|
213
223
|
pi.on("agent_end", (_event, _ctx) => {
|
|
214
|
-
if (!
|
|
224
|
+
if (!lifecycle.isCurrent()) return;
|
|
215
225
|
stopWorkingTimer();
|
|
216
226
|
// closeRun folds any span still open at run end (a prompt can outlive
|
|
217
227
|
// the run, like the previous waitingAccum fold), so the frozen
|
|
218
228
|
// breakdown never includes user-decision time. lastDoneIn keeps its
|
|
219
229
|
// active-work semantics: total minus waits.
|
|
220
|
-
const wasOpen =
|
|
221
|
-
const frozen =
|
|
222
|
-
if (wasOpen) state.lastDoneIn = frozen.total - frozen.wait;
|
|
230
|
+
const wasOpen = activity.tracker().isRunOpen();
|
|
231
|
+
const frozen = activity.tracker().closeRun(Date.now());
|
|
232
|
+
if (wasOpen) host.state.lastDoneIn = frozen.total - frozen.wait;
|
|
223
233
|
requestFooterRender?.();
|
|
224
234
|
});
|
|
225
235
|
|
|
@@ -228,19 +238,19 @@ const main = Effect.fn("tui/main")(function* (pi: ExtensionAPI) {
|
|
|
228
238
|
// count toward the footer's working/done time. Nested spans are coalesced
|
|
229
239
|
// by pi; the tracker ignores a wait_start that is already open defensively.
|
|
230
240
|
pi.on("ui_prompt_start", (_event, _ctx) => {
|
|
231
|
-
if (!
|
|
232
|
-
|
|
233
|
-
if (
|
|
241
|
+
if (!lifecycle.isCurrent()) return;
|
|
242
|
+
activity.handle({ type: "wait_start", now: Date.now() });
|
|
243
|
+
if (activity.tracker().isRunOpen()) requestFooterRender?.();
|
|
234
244
|
});
|
|
235
245
|
|
|
236
246
|
pi.on("ui_prompt_end", (_event, _ctx) => {
|
|
237
|
-
if (!
|
|
238
|
-
|
|
247
|
+
if (!lifecycle.isCurrent()) return;
|
|
248
|
+
activity.handle({ type: "wait_end", now: Date.now() });
|
|
239
249
|
requestFooterRender?.();
|
|
240
250
|
});
|
|
241
251
|
|
|
242
252
|
pi.on("turn_start", (event) => {
|
|
243
|
-
|
|
253
|
+
telemetry.handle(event);
|
|
244
254
|
});
|
|
245
255
|
|
|
246
256
|
// Translate pi event fields into tracker descriptors at the boundary and
|
|
@@ -250,37 +260,43 @@ const main = Effect.fn("tui/main")(function* (pi: ExtensionAPI) {
|
|
|
250
260
|
// event outside the current session is harmless.
|
|
251
261
|
const track = (source: TrackerEventSource) => {
|
|
252
262
|
const mapped = toTrackerEvent(source);
|
|
253
|
-
if (mapped)
|
|
263
|
+
if (mapped !== undefined) activity.handle(mapped);
|
|
254
264
|
};
|
|
255
265
|
|
|
256
266
|
pi.on("message_start", (event) => {
|
|
257
|
-
|
|
267
|
+
telemetry.handle(event);
|
|
258
268
|
track({ kind: "message_start", role: event.message.role, now: Date.now() });
|
|
259
269
|
});
|
|
260
270
|
|
|
261
271
|
pi.on("message_update", (event) => {
|
|
262
|
-
|
|
272
|
+
telemetry.handle(event);
|
|
263
273
|
});
|
|
264
274
|
|
|
265
275
|
pi.on("tool_execution_start", (event) => {
|
|
266
|
-
|
|
276
|
+
telemetry.handle(event);
|
|
267
277
|
track({ kind: "tool_execution_start", toolCallId: event.toolCallId, now: Date.now() });
|
|
268
278
|
});
|
|
269
279
|
|
|
270
280
|
pi.on("turn_end", (event) => {
|
|
271
|
-
|
|
281
|
+
telemetry.handle(event);
|
|
272
282
|
});
|
|
273
283
|
|
|
274
284
|
pi.on("agent_settled", (event, ctx) => {
|
|
275
|
-
const
|
|
276
|
-
if (
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
285
|
+
const measured = telemetry.handle(event);
|
|
286
|
+
if (
|
|
287
|
+
measured !== undefined &&
|
|
288
|
+
config.enabled &&
|
|
289
|
+
config.telemetry.enabled &&
|
|
290
|
+
isTuiContext(ctx) &&
|
|
291
|
+
mountedPort !== undefined
|
|
292
|
+
) {
|
|
293
|
+
const message = composeNotifyLine(measured, {
|
|
294
|
+
theme: ctx.ui.theme,
|
|
295
|
+
config: config.telemetry,
|
|
296
|
+
iconMode: mountedPort.facts.config().icons.mode,
|
|
297
|
+
width: mountedPort.facts.notifyWidth(),
|
|
298
|
+
});
|
|
299
|
+
if (message !== "") ctx.ui.notify(message, "info");
|
|
284
300
|
}
|
|
285
301
|
});
|
|
286
302
|
|
|
@@ -293,10 +309,10 @@ const main = Effect.fn("tui/main")(function* (pi: ExtensionAPI) {
|
|
|
293
309
|
});
|
|
294
310
|
|
|
295
311
|
pi.on("message_end", (event, ctx) => {
|
|
296
|
-
|
|
312
|
+
telemetry.handle(event);
|
|
297
313
|
track({ kind: "message_end", role: event.message.role, now: Date.now() });
|
|
298
|
-
if (!
|
|
299
|
-
|
|
314
|
+
if (!lifecycle.isCurrent()) return;
|
|
315
|
+
usage.invalidate();
|
|
300
316
|
refreshInteractiveState(ctx);
|
|
301
317
|
});
|
|
302
318
|
|
|
@@ -306,14 +322,14 @@ const main = Effect.fn("tui/main")(function* (pi: ExtensionAPI) {
|
|
|
306
322
|
});
|
|
307
323
|
|
|
308
324
|
pi.on("session_compact", (_event, ctx) => {
|
|
309
|
-
if (!
|
|
310
|
-
|
|
325
|
+
if (!lifecycle.isCurrent()) return;
|
|
326
|
+
usage.invalidate();
|
|
311
327
|
refreshInteractiveState(ctx);
|
|
312
328
|
});
|
|
313
329
|
|
|
314
330
|
pi.on("session_tree", (_event, ctx) => {
|
|
315
|
-
if (!
|
|
316
|
-
|
|
331
|
+
if (!lifecycle.isCurrent()) return;
|
|
332
|
+
usage.invalidate();
|
|
317
333
|
refreshInteractiveState(ctx);
|
|
318
334
|
});
|
|
319
335
|
|
|
@@ -340,11 +356,11 @@ const main = Effect.fn("tui/main")(function* (pi: ExtensionAPI) {
|
|
|
340
356
|
const wasEnabled = config.enabled;
|
|
341
357
|
yield* conf.save(newConfig);
|
|
342
358
|
config = newConfig;
|
|
343
|
-
if (
|
|
359
|
+
if (host.ctx !== undefined && wasEnabled !== newConfig.enabled) {
|
|
344
360
|
if (newConfig.enabled) {
|
|
345
|
-
applyUi(
|
|
361
|
+
applyUi(host.ctx);
|
|
346
362
|
} else {
|
|
347
|
-
uninstallUi(
|
|
363
|
+
uninstallUi(host.ctx);
|
|
348
364
|
}
|
|
349
365
|
}
|
|
350
366
|
requestFooterRender?.();
|
|
@@ -357,26 +373,9 @@ const main = Effect.fn("tui/main")(function* (pi: ExtensionAPI) {
|
|
|
357
373
|
});
|
|
358
374
|
|
|
359
375
|
export default function (pi: ExtensionAPI) {
|
|
360
|
-
const
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
const
|
|
364
|
-
|
|
365
|
-
// TODO: remove Node homedir dep
|
|
366
|
-
const previewLayer = PreviewService.make(homedir).pipe(
|
|
367
|
-
Layer.provide(Layer.merge(NodePath.layer, NodeFileSystem.layer)),
|
|
368
|
-
);
|
|
369
|
-
const tuiLayer = TuiConfigService.layer.pipe(Layer.provide(NodeFileSystem.layer));
|
|
370
|
-
|
|
371
|
-
const allLayers = Layer.mergeAll(
|
|
372
|
-
tuiLayer,
|
|
373
|
-
EditorTintService.layer,
|
|
374
|
-
previewLayer,
|
|
375
|
-
gitExecLayer,
|
|
376
|
-
gitLayer,
|
|
377
|
-
);
|
|
378
|
-
|
|
379
|
-
const program = main(pi).pipe(Effect.provide(allLayers));
|
|
380
|
-
|
|
381
|
-
Effect.runSync(program);
|
|
376
|
+
const host = createTuiSessionHost();
|
|
377
|
+
// The entry point injects the two primitives no Effect service covers (D19);
|
|
378
|
+
// `getEnv` stays live because `process.env` changes.
|
|
379
|
+
const platform = { homeDir: homedir(), getEnv: (name: string) => process.env[name] };
|
|
380
|
+
Effect.runSync(main(pi, host).pipe(Effect.provide(tuiServicesLayer(host, platform))));
|
|
382
381
|
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// Each test is its own entry point: it builds a fresh layer for one scenario and
|
|
2
|
+
// runs its effects inside it, so providing there is the pattern the rule allows.
|
|
3
|
+
/* oxlint-disable effecttsgo/strict-effect-provide */
|
|
4
|
+
import { assert, it } from "@effect/vitest";
|
|
5
|
+
import { Effect } from "effect";
|
|
6
|
+
import { ActivityService } from "./activity.ts";
|
|
7
|
+
import { createTuiSessionHost, tuiServicesLayer } from "./layers.ts";
|
|
8
|
+
import { RuntimeInfoService } from "./runtime-service.ts";
|
|
9
|
+
import { SessionLifecycleService } from "./session-lifecycle.ts";
|
|
10
|
+
import { TurnTelemetryService } from "./telemetry.ts";
|
|
11
|
+
import { UsageTotalsService } from "./usage-totals-service.ts";
|
|
12
|
+
import { PreviewService } from "./ui/preview.ts";
|
|
13
|
+
|
|
14
|
+
/** The env key one env-based runtime (conda) matches on. */
|
|
15
|
+
const CONDA_ENV = "CONDA_DEFAULT_ENV";
|
|
16
|
+
|
|
17
|
+
/** The two injected primitives, stubbed so no test reads the host environment. */
|
|
18
|
+
const platform = {
|
|
19
|
+
homeDir: "/tmp/pi-tui-test-home",
|
|
20
|
+
getEnv: (name: string) => (name === CONDA_ENV ? "pi-tui-test" : undefined),
|
|
21
|
+
} as const;
|
|
22
|
+
|
|
23
|
+
// ---------------------------------------------------------------------------
|
|
24
|
+
// tuiServicesLayer — the one per-program composition point. Building it must
|
|
25
|
+
// resolve every stateful service from one graph, and the usage cache must see
|
|
26
|
+
// the same session generation the handlers advance (D4/D11, end state 2).
|
|
27
|
+
// ---------------------------------------------------------------------------
|
|
28
|
+
|
|
29
|
+
it.effect("every stateful service resolves from one build", () =>
|
|
30
|
+
Effect.gen(function* () {
|
|
31
|
+
// Arrange: the host `main` receives.
|
|
32
|
+
const host = createTuiSessionHost();
|
|
33
|
+
|
|
34
|
+
// Act: resolve all five tags from the production layer graph.
|
|
35
|
+
const program = Effect.gen(function* () {
|
|
36
|
+
const lifecycle = yield* SessionLifecycleService;
|
|
37
|
+
const activity = yield* ActivityService;
|
|
38
|
+
const telemetry = yield* TurnTelemetryService;
|
|
39
|
+
const usage = yield* UsageTotalsService;
|
|
40
|
+
const runtime = yield* RuntimeInfoService;
|
|
41
|
+
|
|
42
|
+
// Assert: one instance per tag, writing the snapshot the port reads.
|
|
43
|
+
const lifecycleService = yield* SessionLifecycleService;
|
|
44
|
+
assert.strictEqual(lifecycleService, lifecycle);
|
|
45
|
+
activity.handle({ type: "run_start", now: 0 });
|
|
46
|
+
assert.strictEqual(activity.tracker(), host.state.tracker);
|
|
47
|
+
assert.strictEqual(activity.tracker().isRunOpen(), true);
|
|
48
|
+
activity.reset();
|
|
49
|
+
assert.strictEqual(activity.tracker().isRunOpen(), false);
|
|
50
|
+
assert.strictEqual(typeof telemetry.handle, "function");
|
|
51
|
+
assert.strictEqual(runtime.current(), host.state.runtime);
|
|
52
|
+
|
|
53
|
+
// The injected home directory reaches the consumer through the graph.
|
|
54
|
+
const preview = yield* PreviewService;
|
|
55
|
+
const resolved = yield* preview.resolveFileRef("~", platform.homeDir);
|
|
56
|
+
assert.strictEqual(resolved, platform.homeDir);
|
|
57
|
+
|
|
58
|
+
// The injected env read does too: one env-based runtime matches.
|
|
59
|
+
yield* runtime.refresh(platform.homeDir);
|
|
60
|
+
assert.deepStrictEqual(runtime.current(), [{ name: "conda" }]);
|
|
61
|
+
|
|
62
|
+
// The usage cache keys on the lifecycle generation this graph built, so
|
|
63
|
+
// a session start drops the fold: the two tags share one counter (D4).
|
|
64
|
+
const before = usage.totals();
|
|
65
|
+
lifecycle.start();
|
|
66
|
+
const after = usage.totals();
|
|
67
|
+
assert.notStrictEqual(after, before);
|
|
68
|
+
assert.deepStrictEqual(after, before);
|
|
69
|
+
});
|
|
70
|
+
yield* program.pipe(Effect.provide(tuiServicesLayer(host, platform)));
|
|
71
|
+
}),
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
it.effect("two builds do not share service state", () =>
|
|
75
|
+
Effect.gen(function* () {
|
|
76
|
+
// Arrange: two programs, the way a reload builds two graphs.
|
|
77
|
+
const firstHost = createTuiSessionHost();
|
|
78
|
+
const secondHost = createTuiSessionHost();
|
|
79
|
+
|
|
80
|
+
// Act: open a run in the first build only.
|
|
81
|
+
const first = yield* Effect.service(ActivityService).pipe(
|
|
82
|
+
Effect.provide(tuiServicesLayer(firstHost, platform)),
|
|
83
|
+
);
|
|
84
|
+
first.handle({ type: "run_start", now: 0 });
|
|
85
|
+
const second = yield* Effect.service(ActivityService).pipe(
|
|
86
|
+
Effect.provide(tuiServicesLayer(secondHost, platform)),
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
// Assert
|
|
90
|
+
assert.strictEqual(first.tracker().isRunOpen(), true);
|
|
91
|
+
assert.strictEqual(second.tracker().isRunOpen(), false);
|
|
92
|
+
assert.notStrictEqual(first.tracker(), second.tracker());
|
|
93
|
+
}),
|
|
94
|
+
);
|