pi-suite 0.3.6 → 0.3.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-suite",
3
- "version": "0.3.6",
3
+ "version": "0.3.7",
4
4
  "description": "Custom modular Pi extension suite",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -90,7 +90,7 @@ export async function showRuns(ctx: ExtensionContext, deps: SubagentsDeps, state
90
90
  },
91
91
  actions: (item, done) => {
92
92
  const runs = displayed.get(item.value)!, [record] = runs;
93
- const readOnly = readonly(record), controls = agentControls(ctx, agents, record.id, record.runId, readOnly);
93
+ const readOnly = readonly(record), controls = agentControls(ctx, agents, record.id, () => record.runId, readOnly);
94
94
  const run = (value: string) => () => { action = value; done(item.value); };
95
95
  const pending = record.state === 'queued';
96
96
  return [
@@ -16,6 +16,7 @@ import { isActive as jobActive } from '../jobs/state.ts';
16
16
  import { ViewerNavigation } from '../ui/viewer/navigation.ts';
17
17
  import { agentInformation } from '../ui/viewer/agent-information.ts';
18
18
  import { WORKING_INTERVAL_MS, workingLine } from '../ui/working.ts';
19
+ import { PresentationTicker } from '../ui/ticker.ts';
19
20
  import { agentDetails } from './viewer-info.ts';
20
21
  import { showDetails } from '../ui/details.ts';
21
22
  import { displayName as jobName, jobCompactStatus, jobLabel, jobStatus } from '../jobs/index.ts';
@@ -55,12 +56,12 @@ export async function showAgentJobs(ctx: ExtensionContext, deps: AgentJobViewer,
55
56
  } } finally { release(); }
56
57
  }
57
58
 
58
- export function agentControls(ctx: ExtensionContext, agents: Pick<AgentsModule, 'manager' | 'scope'>, id: string, runId: string, readonly: boolean) {
59
- const get = () => agents.manager.get(id, runId);
59
+ export function agentControls(ctx: ExtensionContext, agents: Pick<AgentsModule, 'manager' | 'scope'>, id: string, runId: () => string, readonly: boolean) {
60
+ const get = () => agents.manager.get(id, runId());
60
61
  const canStop = () => { const run = get().run; return Boolean(!readonly && run && !run.settled && active(run.record.state) && run.record.state !== 'stopping'); };
61
62
  const canMessage = () => {
62
63
  const { record, run } = get(), scope = agents.scope(ctx);
63
- if (readonly || agents.manager.get(id).record.runId !== runId) return false;
64
+ if (readonly || agents.manager.get(id).record.runId !== record.runId) return false;
64
65
  if (!run || run.settled) return !active(record.state) && !agents.manager.isStopping(id) && agents.manager.visible(record, scope);
65
66
  // The public state can still say running while the SDK session is closing.
66
67
  if (!active(run.record.state) || run.record.state === 'queued' || run.record.state === 'stopping') return false;
@@ -70,15 +71,15 @@ export function agentControls(ctx: ExtensionContext, agents: Pick<AgentsModule,
70
71
  }
71
72
 
72
73
  export function agentMessage(ctx: ExtensionContext, agents: Pick<AgentsModule, 'manager' | 'scope' | 'admitSend'>, id: string, runId: string,
73
- readonly: boolean, config: () => SuiteConfig, sent?: () => void): InlineRequest {
74
- const {get, canMessage} = agentControls(ctx, agents, id, runId, readonly);
74
+ readonly: boolean, config: () => SuiteConfig, sent?: (result: Awaited<ReturnType<AgentsModule['manager']['send']>>) => void): InlineRequest {
75
+ const {get, canMessage} = agentControls(ctx, agents, id, () => runId, readonly);
75
76
  const {record, run} = get();
76
77
  const prompt = run?.question ? 'Answer parent question: ' : active(record.state) ? `Message ${record.name}: ` : `Resume ${record.name}: `;
77
78
  return {multiline: true, prompt, submit: async message => {
78
79
  // The composer can outlive a run; never send an old reader's input to its successor.
79
80
  if (!canMessage()) throw new Error('This run no longer accepts messages');
80
81
  const result = await agents.admitSend(id).send(id, message, agents.scope(ctx), config());
81
- sent?.();
82
+ sent?.(result);
82
83
  return `${record.name}: ${result.action}`;
83
84
  }};
84
85
  }
@@ -98,28 +99,43 @@ export const showAgent = (...args: Parameters<typeof readAgent>): Promise<void>
98
99
  withReadingScreen(args[0], () => readAgent(...args));
99
100
 
100
101
  async function readAgent(ctx: ExtensionContext, agents: Pick<AgentsModule, 'manager' | 'scope' | 'renderer' | 'customRendererLookup' | 'admitSend'>, dock: Dock, id: string, runId: string, config: () => SuiteConfig, readonly = false, jobs?: AgentJobViewer, navigation = new ViewerNavigation()): Promise<void> {
101
- const release = agents.manager.retain(runId);
102
+ const { manager } = agents;
103
+ // Only a reader opened on the latest run follows successors; history stays pinned.
104
+ const follow = manager.get(id).record.runId === runId;
105
+ let release = manager.retain(runId);
102
106
  let content: Transcript | undefined, reader: ScrollView | undefined;
103
- const { get, canStop, canMessage } = agentControls(ctx, agents, id, runId, readonly);
107
+ let revision: ReturnType<typeof transcript> = [], transcriptError: string | undefined;
108
+ const { get, canStop, canMessage } = agentControls(ctx, agents, id, () => runId, readonly);
109
+ const syncRun = () => {
110
+ if (!follow) return;
111
+ const latest = manager.get(id).record.runId;
112
+ if (latest === runId) return;
113
+ const releaseNext = manager.retain(latest);
114
+ release(); release = releaseNext; runId = latest;
115
+ transcriptError = undefined;
116
+ };
117
+ const suiteLookup = agents.customRendererLookup();
118
+ // Look up the current child lazily: a successor may still be binding its extensions.
119
+ const lookup = {
120
+ getMessageRenderer: (type: string) => (get().run?.customRenderers ?? suiteLookup).getMessageRenderer(type),
121
+ getEntryRenderer: (type: string) => (get().run?.customRenderers ?? suiteLookup).getEntryRenderer(type),
122
+ };
123
+ let refresh: (() => void) | undefined;
124
+ // Keep the run lease current even while a nested page owns the screen.
125
+ const unsubscribe = manager.subscribe(() => { syncRun(); refresh?.(); });
104
126
  try { for (; !navigation.closed;) {
105
- const { run } = get();
106
- const suiteLookup = agents.customRendererLookup();
107
- // A reader can open while the child is still binding its extensions.
108
- const lookup = {
109
- getMessageRenderer: (type: string) => (run?.customRenderers ?? suiteLookup).getMessageRenderer(type),
110
- getEntryRenderer: (type: string) => (run?.customRenderers ?? suiteLookup).getEntryRenderer(type),
111
- };
127
+ syncRun();
112
128
  dock.setDetail(true);
113
129
  let action: 'model' | 'stop' | 'jobs' | 'details' | undefined;
114
130
  try {
115
131
  await openReading<void>(ctx, (tui, theme, keys, done) => {
116
- content ??= new Transcript(tui, get().record.cwd, name => run?.renderers?.get(name) ?? agents.renderer(name), { theme, ...lookup });
117
- content.setEntries(transcript(get().record, get().run));
118
- const view = new DetailsView(tui, () => '', content, done, [
119
- { key: 'm', label: 'Message', available: canMessage, input: () => agentMessage(ctx, agents, id, runId, readonly, config) },
132
+ const body = content ??= new Transcript(tui, get().record.cwd, name => get().run?.renderers?.get(name) ?? agents.renderer(name), { theme, ...lookup });
133
+ const view = new DetailsView(tui, () => '', body, done, [
134
+ { key: 'm', label: 'Message', available: canMessage, input: () => agentMessage(ctx, agents, id, runId, readonly, config,
135
+ ({action}) => { if (action === 'resumed') view.reader.follow(); }) },
120
136
  { key: 'd', label: 'Details', run: () => { action = 'details'; done(); } },
121
- { key: 'o', label: 'Tools', run: () => { content!.toggleTools(); view.invalidate(); } },
122
- { key: 'h', label: 'Thinking', run: () => { content!.toggleThinking(); view.invalidate(); } },
137
+ { key: 'o', label: 'Tools', run: () => { body.toggleTools(); view.invalidate(); } },
138
+ { key: 'h', label: 'Thinking', run: () => { body.toggleThinking(); view.invalidate(); } },
123
139
  { key: 'j', label: 'Jobs', available: () => jobs !== undefined, run: () => { action = 'jobs'; done(); } },
124
140
  { key: 'c', label: 'Model / thinking', available: () => !readonly, run: () => { action = 'model'; done(); } },
125
141
  { label: 'Background', key: 'b', available: () => !readonly && agents.manager.canSendToBackground(id, runId), run: () => { agents.manager.sendToBackground(id, runId); } },
@@ -128,27 +144,31 @@ async function readAgent(ctx: ExtensionContext, agents: Pick<AgentsModule, 'mana
128
144
  bottom: width => agentInformation(get().record, Boolean(get().run?.question), readonly, view.reader, width, theme),
129
145
  status: () => ['starting', 'running'].includes(get().record.state) ? workingLine(theme) : undefined});
130
146
  if (!reader) view.reader.follow();
131
- view.reader.setContent(content);
132
147
  reader = view.reader;
133
- let revision = transcript(get().record, get().run);
134
- const refresh = () => {
135
- const { record, run } = get(), entries = transcript(record, run);
148
+ const clock = new PresentationTicker(() => true, () => tui.requestRender(), WORKING_INTERVAL_MS);
149
+ refresh = () => {
150
+ const { record, run } = get();
151
+ let entries: ReturnType<typeof transcript> | undefined;
152
+ try { entries = transcript(record, run); transcriptError = undefined; }
153
+ catch (error) {
154
+ // An unreadable saved transcript must not break synchronous manager notifications.
155
+ const message = error instanceof Error ? error.message : String(error);
156
+ if (transcriptError !== message) ctx.ui.notify(message, 'error');
157
+ transcriptError = message;
158
+ }
136
159
  // Several tools can stream concurrently. Compare every immutable entry identity,
137
160
  // since an unchanged last tool result says nothing about an earlier live tail.
138
- if (entries.length !== revision.length || entries.some((entry, index) => entry !== revision[index])) {
161
+ if (entries && (entries.length !== revision.length || entries.some((entry, index) => entry !== revision[index]))) {
139
162
  revision = entries;
140
163
  view.reader.markNewOutput();
141
- content!.setEntries(entries); view.reader.setContent(content!);
164
+ body.setEntries(entries); view.reader.setContent(body);
142
165
  }
166
+ clock.update(active(record.state));
143
167
  tui.requestRender();
144
168
  };
145
- const unsubscribe = agents.manager.subscribe(refresh);
146
- const clock = active(get().record.state) ? setInterval(() => {
147
- if (!active(get().record.state)) clearInterval(clock);
148
- tui.requestRender();
149
- }, WORKING_INTERVAL_MS).unref() : undefined;
169
+ refresh();
150
170
  const dispose = view.dispose.bind(view);
151
- return Object.assign(view, { dispose: () => {dispose(); unsubscribe(); clearInterval(clock);} });
171
+ return Object.assign(view, { dispose: () => {refresh = undefined; clock.stop(); dispose();} });
152
172
  });
153
173
  } finally { dock.setDetail(false); }
154
174
  if (!action || navigation.closed) return;
@@ -158,5 +178,5 @@ async function readAgent(ctx: ExtensionContext, agents: Pick<AgentsModule, 'mana
158
178
  if (action === 'details') await showDetails(ctx, dock, 'Subagents ▸ Details', agentDetails(get().record, {readonly, canMessage: canMessage(), canStop: canStop()}), 'text', navigation);
159
179
  else if (action !== 'stop' || canStop()) await performAgentAction(action, ctx, agents, id, runId, readonly, config, jobs, navigation);
160
180
  } catch (error) { ctx.ui.notify(error instanceof Error ? error.message : String(error), 'error'); }
161
- } } finally { release(); }
181
+ } } finally { unsubscribe(); release(); }
162
182
  }
package/src/ui/ticker.ts CHANGED
@@ -2,14 +2,14 @@
2
2
  export class PresentationTicker {
3
3
  private timer?: NodeJS.Timeout;
4
4
 
5
- constructor(private visible: () => boolean, private publish: () => void) {}
5
+ constructor(private visible: () => boolean, private publish: () => void, private intervalMs = 1000) {}
6
6
 
7
7
  update(active: boolean): void {
8
8
  if (!active || !this.visible()) { this.stop(); return; }
9
9
  this.timer ??= setInterval(() => {
10
10
  if (this.visible()) this.publish();
11
11
  else this.stop();
12
- }, 1000).unref();
12
+ }, this.intervalMs).unref();
13
13
  }
14
14
 
15
15
  stop(): void {