pi-suite 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +100 -2
- package/README.zh-CN.md +101 -0
- package/package.json +63 -4
- package/skills/pi-suite-jobs/SKILL.md +76 -0
- package/skills/pi-suite-workflow/SKILL.md +114 -0
- package/src/agents/catalog.ts +80 -0
- package/src/agents/child.ts +79 -0
- package/src/agents/commands.ts +17 -0
- package/src/agents/defaults.ts +78 -0
- package/src/agents/history.ts +112 -0
- package/src/agents/index.ts +211 -0
- package/src/agents/manager.ts +438 -0
- package/src/agents/mentions/clone.ts +51 -0
- package/src/agents/mentions/grammar.ts +64 -0
- package/src/agents/mentions/index.ts +76 -0
- package/src/agents/mentions/provider.ts +79 -0
- package/src/agents/model-editor.ts +95 -0
- package/src/agents/models.ts +43 -0
- package/src/agents/output/schema.ts +101 -0
- package/src/agents/output/tool.ts +98 -0
- package/src/agents/parent.ts +13 -0
- package/src/agents/questions.ts +63 -0
- package/src/agents/records.ts +90 -0
- package/src/agents/registry.ts +20 -0
- package/src/agents/render.ts +85 -0
- package/src/agents/resources/context.ts +48 -0
- package/src/agents/resources/loader.ts +86 -0
- package/src/agents/resources/memory.ts +67 -0
- package/src/agents/resources/packages.ts +96 -0
- package/src/agents/resources/paths.ts +8 -0
- package/src/agents/resources/prompts.ts +11 -0
- package/src/agents/resources/scope.ts +147 -0
- package/src/agents/resources/selectors.ts +186 -0
- package/src/agents/retry.ts +20 -0
- package/src/agents/rpc/handlers.ts +62 -0
- package/src/agents/rpc/index.ts +73 -0
- package/src/agents/rpc/options.ts +44 -0
- package/src/agents/run-groups.ts +16 -0
- package/src/agents/runner.ts +131 -0
- package/src/agents/runtime.ts +40 -0
- package/src/agents/schedules/index.ts +136 -0
- package/src/agents/schedules/menu.ts +38 -0
- package/src/agents/schedules/parse.ts +42 -0
- package/src/agents/schedules/store.ts +78 -0
- package/src/agents/schema.ts +5 -0
- package/src/agents/session.ts +29 -0
- package/src/agents/tools.ts +177 -0
- package/src/agents/types.ts +87 -0
- package/src/agents/ui/index.ts +42 -0
- package/src/agents/ui/runs.ts +122 -0
- package/src/agents/ui/types.ts +91 -0
- package/src/agents/viewer-info.ts +36 -0
- package/src/agents/viewer.ts +160 -0
- package/src/agents/workflow/collisions.ts +124 -0
- package/src/agents/workflow/entry.ts +93 -0
- package/src/agents/workflow/host.ts +119 -0
- package/src/agents/workflow/index.ts +100 -0
- package/src/agents/workflow/journal.ts +147 -0
- package/src/agents/workflow/literal.ts +57 -0
- package/src/agents/workflow/manager.ts +169 -0
- package/src/agents/workflow/meta.ts +323 -0
- package/src/agents/workflow/progress/format.ts +184 -0
- package/src/agents/workflow/progress/index.ts +3 -0
- package/src/agents/workflow/progress/model.ts +224 -0
- package/src/agents/workflow/progress/types.ts +144 -0
- package/src/agents/workflow/render.ts +46 -0
- package/src/agents/workflow/runtime/helpers.ts +236 -0
- package/src/agents/workflow/runtime/index.ts +3 -0
- package/src/agents/workflow/runtime/run.ts +646 -0
- package/src/agents/workflow/runtime/types.ts +273 -0
- package/src/agents/workflow/saved.ts +74 -0
- package/src/agents/workflow/store.ts +23 -0
- package/src/agents/workflow/task.ts +277 -0
- package/src/agents/workflow/tool-description.ts +32 -0
- package/src/agents/workflow/tools.ts +66 -0
- package/src/agents/workflow/ui/menu.ts +144 -0
- package/src/agents/workflow/ui/progress.ts +91 -0
- package/src/agents/workflow/worker/bootstrap.ts +316 -0
- package/src/agents/workflow/worker/flow.ts +218 -0
- package/src/agents/workflow/worker/index.ts +7 -0
- package/src/agents/workflow/worker/scope.ts +215 -0
- package/src/ask/.gitkeep +0 -0
- package/src/ask/form.ts +67 -0
- package/src/ask/index.ts +73 -0
- package/src/ask/keys.ts +32 -0
- package/src/ask/layout.ts +78 -0
- package/src/ask/queue.ts +40 -0
- package/src/ask/render.ts +62 -0
- package/src/ask/result.ts +56 -0
- package/src/ask/schema.ts +67 -0
- package/src/ask/tool-render.ts +49 -0
- package/src/ask/view.ts +406 -0
- package/src/goal/.gitkeep +0 -0
- package/src/goal/activity.ts +50 -0
- package/src/goal/commands.ts +75 -0
- package/src/goal/context.ts +36 -0
- package/src/goal/index.ts +38 -0
- package/src/goal/manager.ts +386 -0
- package/src/goal/presentation.ts +15 -0
- package/src/goal/render.ts +36 -0
- package/src/goal/snapshot.ts +78 -0
- package/src/goal/state.ts +39 -0
- package/src/goal/store.ts +59 -0
- package/src/goal/tools.ts +59 -0
- package/src/goal/wait.ts +39 -0
- package/src/index.ts +20 -0
- package/src/jobs/backends/linux-tree.ts +87 -0
- package/src/jobs/backends/pipe.ts +74 -0
- package/src/jobs/backends/process.ts +79 -0
- package/src/jobs/backends/pty-job.ts +45 -0
- package/src/jobs/backends/pty.ts +502 -0
- package/src/jobs/backends/windows-tree.ts +289 -0
- package/src/jobs/commands.ts +7 -0
- package/src/jobs/control.ts +13 -0
- package/src/jobs/index.ts +331 -0
- package/src/jobs/launch/config.ts +26 -0
- package/src/jobs/launch/name.ts +16 -0
- package/src/jobs/launch/presets.ts +61 -0
- package/src/jobs/log-viewer.ts +147 -0
- package/src/jobs/manager.ts +507 -0
- package/src/jobs/monitors/compile.ts +101 -0
- package/src/jobs/monitors/hands-free.ts +108 -0
- package/src/jobs/monitors/runtime.ts +165 -0
- package/src/jobs/output.ts +54 -0
- package/src/jobs/query.ts +62 -0
- package/src/jobs/render.ts +103 -0
- package/src/jobs/runtime.ts +89 -0
- package/src/jobs/state.ts +25 -0
- package/src/jobs/store.ts +168 -0
- package/src/jobs/terminal/keys.ts +292 -0
- package/src/jobs/terminal/protocol.ts +37 -0
- package/src/jobs/terminal/read.ts +131 -0
- package/src/jobs/terminal/render.ts +70 -0
- package/src/jobs/terminal/screen.ts +92 -0
- package/src/jobs/terminal/session.ts +100 -0
- package/src/jobs/terminal/shell.ts +22 -0
- package/src/jobs/terminal/snapshot.ts +8 -0
- package/src/jobs/terminal/visible-text.ts +53 -0
- package/src/jobs/tools.ts +203 -0
- package/src/jobs/types.ts +60 -0
- package/src/jobs/viewer/component.ts +209 -0
- package/src/jobs/viewer/metadata.ts +9 -0
- package/src/jobs/viewer.ts +149 -0
- package/src/jobs/workspace.ts +31 -0
- package/src/runtime/.gitkeep +0 -0
- package/src/runtime/binding.ts +26 -0
- package/src/runtime/cli-prompt.ts +24 -0
- package/src/runtime/clipboard.ts +16 -0
- package/src/runtime/config-edit.ts +31 -0
- package/src/runtime/config-fields.ts +65 -0
- package/src/runtime/config.ts +111 -0
- package/src/runtime/errors.ts +11 -0
- package/src/runtime/json-store.ts +156 -0
- package/src/runtime/notification-render.ts +35 -0
- package/src/runtime/notifications.ts +98 -0
- package/src/runtime/owner.ts +54 -0
- package/src/runtime/read.ts +200 -0
- package/src/runtime/renderers.ts +50 -0
- package/src/runtime/responses/index.ts +108 -0
- package/src/runtime/responses/payload.ts +78 -0
- package/src/runtime/settings/index.ts +24 -0
- package/src/runtime/settings/view.ts +84 -0
- package/src/runtime/suite.ts +192 -0
- package/src/runtime/text.ts +6 -0
- package/src/runtime/tools.ts +131 -0
- package/src/runtime/wait-render.ts +33 -0
- package/src/runtime/wait.ts +134 -0
- package/src/tasks/commands.ts +41 -0
- package/src/tasks/index.ts +305 -0
- package/src/tasks/model.ts +152 -0
- package/src/tasks/presentation.ts +40 -0
- package/src/tasks/query.ts +38 -0
- package/src/tasks/render.ts +54 -0
- package/src/tasks/schema.ts +25 -0
- package/src/tasks/store.ts +164 -0
- package/src/tasks/tools.ts +69 -0
- package/src/tasks/types.ts +61 -0
- package/src/tasks/viewer.ts +68 -0
- package/src/ui/.gitkeep +0 -0
- package/src/ui/actions.ts +50 -0
- package/src/ui/browser-chrome.ts +175 -0
- package/src/ui/browser.ts +292 -0
- package/src/ui/details.ts +111 -0
- package/src/ui/dock/line.ts +14 -0
- package/src/ui/dock/rows.ts +78 -0
- package/src/ui/dock.ts +224 -0
- package/src/ui/format.ts +25 -0
- package/src/ui/frame.ts +8 -0
- package/src/ui/inline-input.ts +98 -0
- package/src/ui/reading.ts +105 -0
- package/src/ui/rendering/builtin-command.ts +34 -0
- package/src/ui/rendering/command.ts +60 -0
- package/src/ui/rendering/read-page.ts +15 -0
- package/src/ui/rendering/shell/bash.ts +281 -0
- package/src/ui/rendering/shell/index.ts +24 -0
- package/src/ui/rendering/shell/powershell.ts +155 -0
- package/src/ui/rendering/shell/spans.ts +28 -0
- package/src/ui/scroll.ts +111 -0
- package/src/ui/select.ts +67 -0
- package/src/ui/status.ts +67 -0
- package/src/ui/structured-details.ts +41 -0
- package/src/ui/terminal.ts +137 -0
- package/src/ui/ticker.ts +18 -0
- package/src/ui/tool-render.ts +152 -0
- package/src/ui/transcript.ts +140 -0
- package/src/ui/viewer/agent-information.ts +34 -0
- package/src/ui/viewer/editor.ts +18 -0
- package/src/ui/viewer/navigation.ts +25 -0
- package/themes/oh-my-pi.json +95 -0
- package/third-party/UPSTREAM.md +88 -0
- package/third-party/licenses/.gitkeep +0 -0
- package/third-party/licenses/oh-my-pi.txt +23 -0
- package/third-party/licenses/pi-ask-user.txt +21 -0
- package/third-party/licenses/pi-background-jobs.txt +21 -0
- package/third-party/licenses/pi-goal.txt +21 -0
- package/third-party/licenses/pi-interactive-shell.txt +25 -0
- package/third-party/licenses/pi-subagents.txt +21 -0
- package/third-party/licenses/pi-tasks.txt +21 -0
- package/third-party/sources.json +111 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// Presets adapted from pi-interactive-shell 0.18.0 config.ts
|
|
2
|
+
// (MIT, third-party/licenses/pi-interactive-shell.txt), parsed by the shared runtime loader.
|
|
3
|
+
import { Type, type Static } from 'typebox';
|
|
4
|
+
|
|
5
|
+
export const presetSchema = Type.Object({
|
|
6
|
+
command: Type.Optional(Type.String({ minLength: 1, maxLength: 4096, pattern: '\\S' })),
|
|
7
|
+
args: Type.Optional(Type.Array(Type.String({ maxLength: 4096 }), { maxItems: 100 })),
|
|
8
|
+
}, { additionalProperties: false });
|
|
9
|
+
export const presetsSchema = Type.Record(Type.String({ pattern: '^[a-z][a-z0-9-]{0,63}$' }), presetSchema);
|
|
10
|
+
export type Preset = { command: string; args: string[] };
|
|
11
|
+
export type PresetOverrides = Static<typeof presetsSchema>;
|
|
12
|
+
export const defaultPresets: Record<string, Preset> = {
|
|
13
|
+
pi: { command: 'pi', args: [] }, codex: { command: 'codex', args: [] },
|
|
14
|
+
claude: { command: 'claude', args: [] }, cursor: { command: 'agent', args: ['--model', 'composer-2-fast'] },
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/** Merge per preset and field, so project args do not discard a user executable path. */
|
|
18
|
+
export function mergePresets(user?: PresetOverrides, project?: PresetOverrides): Record<string, Preset> {
|
|
19
|
+
const result: Record<string, Preset> = {};
|
|
20
|
+
for (const name of new Set([...Object.keys(defaultPresets), ...Object.keys(user ?? {}), ...Object.keys(project ?? {})])) {
|
|
21
|
+
const preset = { ...defaultPresets[name], ...user?.[name], ...project?.[name] };
|
|
22
|
+
if (!preset.command) throw new Error(`Invalid pi-suite configuration: jobs.agents.${name} needs command`);
|
|
23
|
+
result[name] = { command: preset.command, args: preset.args ?? [] };
|
|
24
|
+
}
|
|
25
|
+
return result;
|
|
26
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { sanitizeText } from '../../runtime/text.ts';
|
|
2
|
+
import type { MonitorInput } from '../monitors/compile.ts';
|
|
3
|
+
|
|
4
|
+
const literal = (text: string) => sanitizeText(text).replace(/[\r\n\t]/g, ' ').trim();
|
|
5
|
+
/** Derive from the caller's command, not the generated polling/watch transport program. */
|
|
6
|
+
export function jobName(source: { name?: string; command?: string; monitor?: MonitorInput }): string {
|
|
7
|
+
const name = literal(source.name ?? '');
|
|
8
|
+
if (name) return name;
|
|
9
|
+
if (source.monitor?.fileWatch) {
|
|
10
|
+
const path = literal(source.monitor.fileWatch.path);
|
|
11
|
+
return path ? `watch ${path}` : 'File monitor (path unavailable)';
|
|
12
|
+
}
|
|
13
|
+
const command = source.command?.split(/\r?\n/).map(literal).find(Boolean);
|
|
14
|
+
// Saved history can lack a usable command. Say what is missing rather than naming it with an ID.
|
|
15
|
+
return command || (source.monitor ? `${source.monitor.strategy ?? 'stream'} monitor (command unavailable)` : 'Command unavailable');
|
|
16
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// CLI argument construction adapted from pi-interactive-shell 0.18.0 spawn.ts (MIT,
|
|
2
|
+
// third-party/licenses/pi-interactive-shell.txt). pi-suite removes worktrees and /spawn;
|
|
3
|
+
// the job tool supplies a structured preset request and uses the existing JobManager.
|
|
4
|
+
import { Type, type Static } from 'typebox';
|
|
5
|
+
import { parseArgs } from '@earendil-works/pi-coding-agent';
|
|
6
|
+
import { fileURLToPath } from 'node:url';
|
|
7
|
+
import { ToolError } from '../../runtime/errors.ts';
|
|
8
|
+
import { CLI_PROMPT_FLAG, CLI_PROMPT_TRIGGER } from '../../runtime/cli-prompt.ts';
|
|
9
|
+
import type { Preset } from './config.ts';
|
|
10
|
+
|
|
11
|
+
export const agentSchema = Type.Object({
|
|
12
|
+
preset: Type.String({ pattern: '^[a-z][a-z0-9-]{0,63}$', description: 'pi, codex, claude, cursor or a configured jobs.agents key' }),
|
|
13
|
+
prompt: Type.String({ minLength: 1, maxLength: 16000, pattern: '\\S', description: 'One literal task prompt; never interpreted as shell code' }),
|
|
14
|
+
fork: Type.Optional(Type.Boolean({ description: 'pi only: fork the current persisted session; omit or false for a fresh session' })),
|
|
15
|
+
}, { additionalProperties: false });
|
|
16
|
+
export type AgentLaunch = Static<typeof agentSchema>;
|
|
17
|
+
|
|
18
|
+
/** Pi always uses Bash, including Git Bash on Windows; preserve $, quotes and backticks. */
|
|
19
|
+
export function shellArgument(value: string): string {
|
|
20
|
+
return /^[A-Za-z0-9_./:-]+$/.test(value) ? value : `'${value.replace(/'/g, `'\\''`)}'`;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Let Pi distinguish options from literal values such as --system-prompt '-e'. */
|
|
24
|
+
function piOptionIndex(args: string[], options: string[]): number {
|
|
25
|
+
// A temporary unknown flag is registered by the parser only at an actual option
|
|
26
|
+
// position. The probe never reaches the child; no duplicate CLI grammar is needed.
|
|
27
|
+
return args.findIndex((arg, index) => options.includes(arg) &&
|
|
28
|
+
parseArgs(args.with(index, '--pi-suite-option-boundary=true')).unknownFlags.get('pi-suite-option-boundary') === 'true');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function agentCommand(agent: AgentLaunch, presets: Record<string, Preset>, sessionFile?: string): string {
|
|
32
|
+
// Own-property lookup prevents names such as constructor from resolving through Object.prototype.
|
|
33
|
+
const preset = Object.hasOwn(presets, agent.preset) ? presets[agent.preset] : undefined;
|
|
34
|
+
if (!preset) throw new ToolError('INVALID_ARGS', `Unknown CLI agent ${agent.preset}; configured presets: ${Object.keys(presets).join(', ')}`);
|
|
35
|
+
if (agent.fork && agent.preset !== 'pi') throw new ToolError('INVALID_ARGS', 'fork is supported only by the pi preset');
|
|
36
|
+
if (agent.fork && !sessionFile) throw new ToolError('INVALID_ARGS', 'Cannot fork an unpersisted session; start a fresh CLI agent instead');
|
|
37
|
+
const args = [...preset.args];
|
|
38
|
+
const end = agent.preset === 'pi' ? piOptionIndex(args, ['--']) : args.indexOf('--');
|
|
39
|
+
if (agent.fork) args.splice(end < 0 ? args.length : end, 0, '--fork', sessionFile!);
|
|
40
|
+
if (agent.preset === 'pi') {
|
|
41
|
+
const parsed = parseArgs(preset.args);
|
|
42
|
+
if (parsed.mode === 'rpc' || parsed.unknownFlags.get('mode') === 'rpc') {
|
|
43
|
+
throw new ToolError('INVALID_ARGS', 'Pi RPC mode does not execute an initial CLI prompt');
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (agent.preset === 'pi' && agent.prompt.startsWith('@')) {
|
|
47
|
+
// Explicit loading also works with --no-extensions. Pi canonicalizes and deduplicates
|
|
48
|
+
// this entry when the same suite is already installed or auto-discovered. Keep a
|
|
49
|
+
// node cli.js wrapper prefix, but restore text before other extensions' input hooks.
|
|
50
|
+
const insertion = piOptionIndex(args, ['-e', '--extension', '--']);
|
|
51
|
+
args.splice(insertion < 0 ? args.length : insertion, 0,
|
|
52
|
+
'-e', fileURLToPath(new URL('../../index.ts', import.meta.url)), `--${CLI_PROMPT_FLAG}=${agent.prompt}`);
|
|
53
|
+
if (end < 0) args.push('--');
|
|
54
|
+
args.push(CLI_PROMPT_TRIGGER);
|
|
55
|
+
} else {
|
|
56
|
+
// End option parsing so a prompt beginning with -- cannot become a CLI switch.
|
|
57
|
+
if (end < 0) args.push('--');
|
|
58
|
+
args.push(agent.prompt);
|
|
59
|
+
}
|
|
60
|
+
return [preset.command, ...args].map(shellArgument).join(' ');
|
|
61
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import type { ExtensionContext, Theme } from '@earendil-works/pi-coding-agent';
|
|
2
|
+
import { Text, type TUI } from '@earendil-works/pi-tui';
|
|
3
|
+
import { DetailsView, type DetailOptions } from '../ui/details.ts';
|
|
4
|
+
import type { DetailAction } from '../ui/actions.ts';
|
|
5
|
+
import { openReading } from '../ui/reading.ts';
|
|
6
|
+
import type { Dock } from '../ui/dock.ts';
|
|
7
|
+
import { fileSource, READ_LIMITS, type ReadPage } from '../runtime/read.ts';
|
|
8
|
+
import type { JobReader } from './control.ts';
|
|
9
|
+
import type { JobSummary, Stream } from './types.ts';
|
|
10
|
+
import { isTerminal } from './state.ts';
|
|
11
|
+
import { readJobLog } from './query.ts';
|
|
12
|
+
import { summaryLine } from './index.ts';
|
|
13
|
+
import type { ViewerNavigation } from '../ui/viewer/navigation.ts';
|
|
14
|
+
import { sanitizeText } from '../runtime/text.ts';
|
|
15
|
+
|
|
16
|
+
const REFRESH_MS = 250;
|
|
17
|
+
/** A bounded byte window over the existing log. UI readers never own or stop a process. */
|
|
18
|
+
export class JobLogView extends DetailsView {
|
|
19
|
+
private page?: ReadPage;
|
|
20
|
+
private totalBytes = 0;
|
|
21
|
+
private error?: string;
|
|
22
|
+
private closed = false;
|
|
23
|
+
private timer?: NodeJS.Timeout;
|
|
24
|
+
private active = true;
|
|
25
|
+
private unsubscribe: () => void;
|
|
26
|
+
private queue = Promise.resolve();
|
|
27
|
+
|
|
28
|
+
constructor(private terminal: TUI, private manager: JobReader, private job: JobSummary,
|
|
29
|
+
private stream: Stream, private live: boolean, done: () => void, theme?: Theme, options: DetailOptions = {}, actions: DetailAction[] = []) {
|
|
30
|
+
super(terminal, () => this.header(), new Text('Loading log…', 0, 0), done, [
|
|
31
|
+
{ key: '[', label: 'Previous bytes', run: () => { void this.previous(); } },
|
|
32
|
+
{ key: ']', label: 'Next bytes', run: () => { void this.next(); } },
|
|
33
|
+
{ key: 'f', label: 'Follow latest', run: () => { void this.follow(); } },
|
|
34
|
+
...actions,
|
|
35
|
+
], undefined, theme, options);
|
|
36
|
+
this.unsubscribe = this.connect();
|
|
37
|
+
}
|
|
38
|
+
private connect(): () => void {
|
|
39
|
+
return this.live ? this.manager.subscribe(() => {
|
|
40
|
+
if (this.closed || !this.active) return;
|
|
41
|
+
const wasFinished = isTerminal(this.job.state);
|
|
42
|
+
this.job = this.manager.peek(this.job.id);
|
|
43
|
+
// Settlement can land between a size poll and its next timer. Read the flushed final
|
|
44
|
+
// output once instead of relying on a timer that now correctly stops for terminal jobs.
|
|
45
|
+
if (!wasFinished && isTerminal(this.job.state)) void this.refresh();
|
|
46
|
+
this.terminal.requestRender();
|
|
47
|
+
}) : () => {};
|
|
48
|
+
}
|
|
49
|
+
get status(): string { return this.header(); }
|
|
50
|
+
activate(): void {
|
|
51
|
+
if (!this.active) {
|
|
52
|
+
this.active = true;
|
|
53
|
+
if (this.live) this.job = this.manager.peek(this.job.id);
|
|
54
|
+
this.unsubscribe = this.connect();
|
|
55
|
+
}
|
|
56
|
+
void this.refresh().then(() => this.arm());
|
|
57
|
+
}
|
|
58
|
+
deactivate(): void { this.active = false; clearTimeout(this.timer); this.unsubscribe(); }
|
|
59
|
+
private header(): string {
|
|
60
|
+
if (this.error) return `${this.job.id} · read failed: ${this.error}`;
|
|
61
|
+
const following = this.reader.following && !this.reader.searching;
|
|
62
|
+
return `${summaryLine(this.job)} · ${this.stream} bytes ${this.page?.offset ?? 0}-${this.page?.nextOffset ?? 0}/${this.totalBytes} · ${following ? 'following' : 'paused'}`;
|
|
63
|
+
}
|
|
64
|
+
private request(action: () => Promise<void>): Promise<void> {
|
|
65
|
+
// Serialize pagination and refresh so a slower earlier read cannot replace a newer window.
|
|
66
|
+
this.queue = this.queue.then(async () => {
|
|
67
|
+
if (this.closed) return;
|
|
68
|
+
await action(); this.error = undefined;
|
|
69
|
+
if (!this.closed) this.terminal.requestRender();
|
|
70
|
+
}).catch(error => {
|
|
71
|
+
this.error = error instanceof Error ? error.message : String(error);
|
|
72
|
+
clearTimeout(this.timer);
|
|
73
|
+
if (!this.closed) this.terminal.requestRender();
|
|
74
|
+
});
|
|
75
|
+
return this.queue;
|
|
76
|
+
}
|
|
77
|
+
private async size(): Promise<number> {
|
|
78
|
+
const source = await fileSource(this.manager.logPath(this.job, this.stream));
|
|
79
|
+
try { return source.size; } finally { await source.close(); }
|
|
80
|
+
}
|
|
81
|
+
private async load(offset: number, mode: 'latest' | 'follow' | 'page', limitBytes: number = READ_LIMITS.maxBytes): Promise<void> {
|
|
82
|
+
const page = await readJobLog(this.manager, this.job, this.stream, { offset, limitBytes });
|
|
83
|
+
if (this.closed) return;
|
|
84
|
+
// Reading is asynchronous: navigation during the read must still pause following.
|
|
85
|
+
if (mode !== 'page' && (!this.reader.following || this.reader.searching)) return;
|
|
86
|
+
this.page = page; this.totalBytes = page.totalBytes;
|
|
87
|
+
if (mode === 'page') this.reader.offset = 0;
|
|
88
|
+
// Program text stays literal; code fences in a log must not become Markdown structure.
|
|
89
|
+
this.reader.setContent(new Text(sanitizeText(page.text) || '(no output)', 0, 0));
|
|
90
|
+
}
|
|
91
|
+
initialize(): Promise<void> { return this.follow(); }
|
|
92
|
+
follow(): Promise<void> {
|
|
93
|
+
this.reader.follow();
|
|
94
|
+
return this.request(async () => {
|
|
95
|
+
const size = await this.size();
|
|
96
|
+
await this.load(Math.max(0, size - READ_LIMITS.maxBytes), 'latest');
|
|
97
|
+
this.arm();
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
previous(): Promise<void> {
|
|
101
|
+
this.reader.pause();
|
|
102
|
+
return this.request(async () => {
|
|
103
|
+
const end = this.page?.offset ?? 0;
|
|
104
|
+
if (end) { const start = Math.max(0, end - READ_LIMITS.maxBytes); await this.load(start, 'page', end - start); }
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
next(): Promise<void> {
|
|
108
|
+
this.reader.pause();
|
|
109
|
+
return this.request(async () => {
|
|
110
|
+
if (this.page && this.page.nextOffset < this.totalBytes) await this.load(this.page.nextOffset, 'page');
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
/** Paused readers observe only the size; their content and search position remain unchanged. */
|
|
114
|
+
refresh(): Promise<void> {
|
|
115
|
+
return this.request(async () => {
|
|
116
|
+
const size = await this.size();
|
|
117
|
+
this.totalBytes = size;
|
|
118
|
+
if (size !== this.page?.totalBytes) this.reader.markNewOutput();
|
|
119
|
+
if (size !== this.page?.totalBytes && this.reader.following && !this.reader.searching) {
|
|
120
|
+
await this.load(Math.max(0, size - READ_LIMITS.maxBytes), 'follow');
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
private arm(): void {
|
|
125
|
+
clearTimeout(this.timer);
|
|
126
|
+
if (this.closed || !this.active || !this.live || isTerminal(this.job.state)) return;
|
|
127
|
+
this.timer = setTimeout(() => { void this.refresh().then(() => { if (!this.error) this.arm(); }); }, REFRESH_MS);
|
|
128
|
+
this.timer.unref();
|
|
129
|
+
}
|
|
130
|
+
protected override readingChanged(): void {
|
|
131
|
+
// Native wheel and scrollbar gestures share the same callback as End. A settled
|
|
132
|
+
// process has no polling timer, so reaching the bottom must fetch its final window.
|
|
133
|
+
if (this.page && this.active && this.reader.following && !this.reader.searching) void this.refresh();
|
|
134
|
+
}
|
|
135
|
+
dispose(): void { super.dispose(); this.closed = true; clearTimeout(this.timer); this.unsubscribe(); }
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export async function showJobLog(ctx: ExtensionContext, dock: Dock, manager: JobReader, job: JobSummary, stream: Stream, live: boolean, navigation?: ViewerNavigation): Promise<void> {
|
|
139
|
+
dock.setDetail(true);
|
|
140
|
+
let view: JobLogView | undefined;
|
|
141
|
+
try {
|
|
142
|
+
await openReading<void>(ctx, async (tui, theme, keys, done) => {
|
|
143
|
+
view = new JobLogView(tui, manager, job, stream, live, done, theme, {navigation, keys});
|
|
144
|
+
await view.initialize(); return view;
|
|
145
|
+
});
|
|
146
|
+
} finally { view?.dispose(); dock.setDetail(false); }
|
|
147
|
+
}
|