@narumitw/pi-starship 0.62.0 → 0.64.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.
Files changed (34) hide show
  1. package/README.md +23 -11
  2. package/dist/chunks/{chunk-PWTSHSP3.ts → chunk-A65IOGL6.ts} +6 -7
  3. package/dist/chunks/chunk-A65IOGL6.ts.map +7 -0
  4. package/dist/chunks/{chunk-HPZTMIZS.ts → chunk-DIHVBO5Z.ts} +41 -8
  5. package/dist/chunks/chunk-DIHVBO5Z.ts.map +7 -0
  6. package/dist/chunks/{chunk-43VIENBN.ts → chunk-UIKPE4UC.ts} +7 -7
  7. package/dist/chunks/{chunk-43VIENBN.ts.map → chunk-UIKPE4UC.ts.map} +2 -2
  8. package/dist/chunks/{command-configuration-menu-S2LXCOJG.ts → command-configuration-menu-ZN2VXU4L.ts} +3 -3
  9. package/dist/chunks/{command-settings-chat-O34BEU4X.ts → command-settings-chat-KLUVZXMO.ts} +632 -196
  10. package/dist/chunks/command-settings-chat-KLUVZXMO.ts.map +7 -0
  11. package/dist/chunks/{commands-ZYIJVIEJ.ts → commands-UAUSSHVS.ts} +25 -20
  12. package/dist/chunks/commands-UAUSSHVS.ts.map +7 -0
  13. package/dist/index.ts +116 -59
  14. package/dist/index.ts.map +3 -3
  15. package/package.json +1 -1
  16. package/skills/configure-pi-starship/SKILL.md +3 -1
  17. package/skills/configure-pi-starship/references/configuration.md +14 -12
  18. package/src/command-commit.ts +41 -7
  19. package/src/command-configuration.ts +8 -10
  20. package/src/command-settings-chat.ts +18 -48
  21. package/src/commands.ts +23 -26
  22. package/src/config.ts +6 -6
  23. package/src/pi-starship.ts +155 -88
  24. package/src/settings-chat-display.ts +11 -0
  25. package/src/settings-chat-fullscreen.ts +264 -0
  26. package/src/settings-chat-scroll.ts +17 -0
  27. package/src/settings-chat-tools.ts +207 -0
  28. package/src/settings-chat-transcript.ts +133 -62
  29. package/src/settings-chat-ui.ts +109 -98
  30. package/dist/chunks/chunk-HPZTMIZS.ts.map +0 -7
  31. package/dist/chunks/chunk-PWTSHSP3.ts.map +0 -7
  32. package/dist/chunks/command-settings-chat-O34BEU4X.ts.map +0 -7
  33. package/dist/chunks/commands-ZYIJVIEJ.ts.map +0 -7
  34. /package/dist/chunks/{command-configuration-menu-S2LXCOJG.ts.map → command-configuration-menu-ZN2VXU4L.ts.map} +0 -0
@@ -0,0 +1,207 @@
1
+ import { resolve } from "node:path";
2
+ import type { AgentEvent, AgentMessage } from "@earendil-works/pi-agent-core";
3
+ import type { AssistantMessage, ToolCall, ToolResultMessage } from "@earendil-works/pi-ai";
4
+ import {
5
+ createEditToolDefinition,
6
+ createReadToolDefinition,
7
+ createWriteToolDefinition,
8
+ ToolExecutionComponent,
9
+ } from "@earendil-works/pi-coding-agent";
10
+ import type { TUI, TuiMouseEvent } from "@earendil-works/pi-tui";
11
+ import { sanitizeTerminalDocument } from "@narumitw/pi-tui-kit/terminal-document";
12
+ import { settingsChatDisplayValue } from "./settings-chat-display.js";
13
+
14
+ type ToolResult = Parameters<ToolExecutionComponent["updateResult"]>[0];
15
+ interface ToolRow {
16
+ name: string;
17
+ args: Record<string, unknown>;
18
+ component?: ToolExecutionComponent;
19
+ complete: boolean;
20
+ started: boolean;
21
+ result?: ToolResult;
22
+ partial: boolean;
23
+ historyResult?: AgentMessage;
24
+ expanded?: boolean;
25
+ }
26
+
27
+ /** Follow Pi's message and tool execution branches with native built-in renderers. */
28
+ export function createSettingsChatTools(tui: TUI, cwd: string, initialExpanded: boolean, editablePath?: string) {
29
+ const rows = new Map<string, ToolRow>();
30
+ const pending = new Set<string>();
31
+ const edit = createEditToolDefinition(cwd);
32
+ const renderEditCall = edit.renderCall;
33
+ if (renderEditCall) {
34
+ edit.renderCall = (args, theme, context) => {
35
+ const path =
36
+ typeof args?.path === "string" ? args.path : args && "file_path" in args ? args.file_path : undefined;
37
+ const allowed =
38
+ editablePath !== undefined && typeof path === "string" && resolve(cwd, path) === resolve(editablePath);
39
+ // Native previews read the target before tool execution. Do not let that
40
+ // display-only read bypass this chat's draft-only edit permission.
41
+ return renderEditCall(args, theme, { ...context, argsComplete: context.argsComplete && allowed });
42
+ };
43
+ }
44
+ const definitions = {
45
+ read: createReadToolDefinition(cwd),
46
+ write: createWriteToolDefinition(cwd),
47
+ edit,
48
+ };
49
+ let disposed = false;
50
+ let expanded = initialExpanded;
51
+ const ensure = (id: string, name: string, args: Record<string, unknown>) => {
52
+ let row = rows.get(id);
53
+ if (!row) {
54
+ row = { name, args, complete: false, started: false, partial: true };
55
+ rows.set(id, row);
56
+ }
57
+ if (!row.component) {
58
+ const toolName = row.name;
59
+ const definition =
60
+ toolName === "read" || toolName === "write" || toolName === "edit" ? definitions[toolName] : undefined;
61
+ const ownedRow = row;
62
+ const toolUi = new Proxy(tui, {
63
+ get(target, property) {
64
+ if (property === "requestRender")
65
+ return () => {
66
+ // Pi's edit-preview read has no cancellation API. Its late completion
67
+ // can invalidate its old component, but must not redraw a closed chat.
68
+ if (!disposed && rows.get(id) === ownedRow && ownedRow.component === component) target.requestRender();
69
+ };
70
+ const value: unknown = Reflect.get(target, property, target);
71
+ return typeof value === "function" ? value.bind(target) : value;
72
+ },
73
+ });
74
+ // Native result clicks call setExpanded(). Keep that local preference
75
+ // when themed components are rebuilt, without reading private Pi state.
76
+ const Component = class extends ToolExecutionComponent {
77
+ override setExpanded(value: boolean) {
78
+ ownedRow.expanded = value;
79
+ super.setExpanded(value);
80
+ }
81
+ };
82
+ const component = new Component(
83
+ sanitizeTerminalDocument(toolName),
84
+ sanitizeTerminalDocument(id),
85
+ settingsChatDisplayValue(row.args),
86
+ { showImages: false, outputPad: 1 },
87
+ definition,
88
+ toolUi,
89
+ sanitizeTerminalDocument(cwd),
90
+ );
91
+ row.component = component;
92
+ component.setExpanded(row.expanded ?? expanded);
93
+ // Historical final results already contain the authoritative edit diff.
94
+ // Do not reread a mutated file to compute another preview after a rebuild.
95
+ if (row.complete && (!row.result || row.partial)) component.setArgsComplete();
96
+ if (row.started) component.markExecutionStarted();
97
+ if (row.result) component.updateResult(settingsChatDisplayValue(row.result), row.partial);
98
+ }
99
+ return row;
100
+ };
101
+ const updateArgs = (call: ToolCall) => {
102
+ const row = ensure(call.id, call.name, call.arguments);
103
+ row.args = call.arguments;
104
+ row.component?.updateArgs(settingsChatDisplayValue(call.arguments));
105
+ return row;
106
+ };
107
+ const completeArgs = (row: ToolRow) => {
108
+ if (row.complete) return;
109
+ row.complete = true;
110
+ row.component?.setArgsComplete();
111
+ };
112
+ const result = (row: ToolRow, value: ToolResult, partial = false) => {
113
+ row.result = value;
114
+ row.partial = partial;
115
+ row.component?.updateResult(settingsChatDisplayValue(value), partial);
116
+ };
117
+ const failure = (message: AssistantMessage): ToolResult => ({
118
+ content: [
119
+ { type: "text", text: message.stopReason === "aborted" ? "Operation aborted" : message.errorMessage || "Error" },
120
+ ],
121
+ isError: true,
122
+ });
123
+ return {
124
+ onEvent(event: AgentEvent) {
125
+ if (disposed) return;
126
+ if (event.type === "message_update" && event.message.role === "assistant") {
127
+ for (const call of event.message.content) {
128
+ if (call.type !== "toolCall") continue;
129
+ updateArgs(call);
130
+ pending.add(call.id);
131
+ }
132
+ }
133
+ if (event.type === "message_end" && event.message.role === "assistant") {
134
+ const failed = event.message.stopReason === "aborted" || event.message.stopReason === "error";
135
+ // A provider can finalize tool calls without an intermediate update.
136
+ for (const call of event.message.content) {
137
+ if (call.type !== "toolCall") continue;
138
+ updateArgs(call);
139
+ pending.add(call.id);
140
+ }
141
+ for (const id of pending) {
142
+ const row = rows.get(id);
143
+ if (!row) continue;
144
+ if (failed) result(row, failure(event.message));
145
+ else completeArgs(row);
146
+ }
147
+ if (failed) pending.clear();
148
+ }
149
+ if (event.type === "tool_execution_start" && !("parentToolCallId" in event && event.parentToolCallId)) {
150
+ const row = ensure(event.toolCallId, event.toolName, event.args);
151
+ pending.add(event.toolCallId);
152
+ row.started = true;
153
+ row.component?.markExecutionStarted();
154
+ }
155
+ if (event.type === "tool_execution_update") {
156
+ const row = pending.has(event.toolCallId) ? rows.get(event.toolCallId) : undefined;
157
+ if (row) result(row, { ...event.partialResult, isError: false }, true);
158
+ }
159
+ if (event.type === "tool_execution_end") {
160
+ const row = pending.has(event.toolCallId) ? rows.get(event.toolCallId) : undefined;
161
+ if (row) result(row, { ...event.result, isError: event.isError, durationMs: event.durationMs });
162
+ pending.delete(event.toolCallId);
163
+ }
164
+ if (event.type === "agent_end") pending.clear();
165
+ },
166
+ render(call: ToolCall, message: AssistantMessage, historyResult: ToolResultMessage | undefined, width: number) {
167
+ const row = ensure(call.id, call.name, call.arguments);
168
+ if (historyResult && row.historyResult !== historyResult) {
169
+ row.historyResult = historyResult;
170
+ result(row, historyResult);
171
+ }
172
+ if (!historyResult && !row.result && (message.stopReason === "aborted" || message.stopReason === "error"))
173
+ result(row, failure(message));
174
+ return row.component?.render(width) ?? [];
175
+ },
176
+ handleMouse(id: string, event: TuiMouseEvent) {
177
+ if (disposed) return undefined;
178
+ return rows.get(id)?.component?.handleMouse(event);
179
+ },
180
+ retain(ids: Set<string>) {
181
+ for (const id of rows.keys())
182
+ if (!ids.has(id)) {
183
+ rows.delete(id);
184
+ pending.delete(id);
185
+ }
186
+ },
187
+ toggle() {
188
+ if (!disposed) {
189
+ expanded = !expanded;
190
+ for (const row of rows.values()) {
191
+ row.expanded = expanded;
192
+ row.component?.setExpanded(expanded);
193
+ }
194
+ tui.requestRender();
195
+ }
196
+ return expanded;
197
+ },
198
+ invalidate() {
199
+ for (const row of rows.values()) row.component = undefined;
200
+ },
201
+ dispose() {
202
+ disposed = true;
203
+ pending.clear();
204
+ rows.clear();
205
+ },
206
+ };
207
+ }
@@ -1,39 +1,64 @@
1
+ import { stripVTControlCharacters } from "node:util";
1
2
  import type { Agent, AgentMessage } from "@earendil-works/pi-agent-core";
2
3
  import type { AssistantMessage } from "@earendil-works/pi-ai";
3
- import {
4
- AssistantMessageComponent,
5
- createEditToolDefinition,
6
- createReadToolDefinition,
7
- createWriteToolDefinition,
8
- ToolExecutionComponent,
9
- UserMessageComponent,
10
- } from "@earendil-works/pi-coding-agent";
11
- import type { Component, TUI } from "@earendil-works/pi-tui";
4
+ import { AssistantMessageComponent, UserMessageComponent } from "@earendil-works/pi-coding-agent";
5
+ import type { Component, TUI, TuiMouseEvent } from "@earendil-works/pi-tui";
12
6
  import { sanitizeTerminalDocument } from "@narumitw/pi-tui-kit/terminal-document";
13
7
 
14
- /** Display-only copies keep terminal controls out of native renderers without changing agent history. */
15
- function displayValue<T>(value: T): T {
16
- if (typeof value === "string") return sanitizeTerminalDocument(value) as T;
17
- if (Array.isArray(value)) return value.map(displayValue) as T;
18
- if (value && typeof value === "object") {
19
- return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, displayValue(item)])) as T;
20
- }
21
- return value;
8
+ import { settingsChatDisplayValue as displayValue } from "./settings-chat-display.js";
9
+ import { createSettingsChatTools } from "./settings-chat-tools.js";
10
+
11
+ export interface SettingsChatAnchor {
12
+ message: AgentMessage | symbol;
13
+ row: number;
14
+ width: number;
15
+ offset: number;
16
+ text: string;
22
17
  }
23
18
 
24
19
  /** Reuse Pi's public message components, not another extension's renderer or private settings. */
25
- export function createSettingsChatTranscript(agent: Agent, tui: TUI, cwd: string) {
20
+ export function createSettingsChatTranscript(
21
+ agent: Agent,
22
+ tui: TUI,
23
+ cwd: string,
24
+ initialExpanded = false,
25
+ editablePath?: string,
26
+ ) {
26
27
  const messages = new Map<AgentMessage, Component>();
27
- const tools = new Map<string, { component: ToolExecutionComponent; result?: AgentMessage }>();
28
- const definitions = {
29
- read: createReadToolDefinition(cwd),
30
- write: createWriteToolDefinition(cwd),
31
- edit: createEditToolDefinition(cwd),
32
- };
28
+ const tools = createSettingsChatTools(tui, cwd, initialExpanded, editablePath);
29
+ // Agent emits a new object for each partial update and another at finalization.
30
+ // Keep one response identity across those events, including frames with no render.
31
+ const identities = new WeakMap<AgentMessage, symbol>();
32
+ let streamingIdentity: symbol | undefined;
33
+ const initial = agent.state.streamingMessage;
34
+ if (initial?.role === "assistant") {
35
+ streamingIdentity = Symbol();
36
+ identities.set(initial, streamingIdentity);
37
+ }
38
+ const unsubscribe = agent.subscribe((event) => {
39
+ if (disposed) return;
40
+ tools.onEvent(event);
41
+ if (event.type === "agent_end") streamingIdentity = undefined;
42
+ if (
43
+ (event.type === "message_start" || event.type === "message_update" || event.type === "message_end") &&
44
+ event.message.role === "assistant"
45
+ ) {
46
+ if (event.type === "message_start") streamingIdentity = Symbol();
47
+ streamingIdentity ??= Symbol();
48
+ identities.set(event.message, streamingIdentity);
49
+ if (event.type === "message_end") streamingIdentity = undefined;
50
+ }
51
+ });
52
+ let spans: Array<{ message: AgentMessage | symbol; start: number; end: number; offsets: number[]; text: string }> =
53
+ [];
54
+ let toolSpans: Array<{ id: string; start: number; end: number }> = [];
55
+ let renderedWidth = 0;
33
56
  let streaming: AssistantMessageComponent | undefined;
34
57
  let disposed = false;
35
58
  return {
36
59
  render(width: number): string[] {
60
+ spans = [];
61
+ toolSpans = [];
37
62
  if (disposed || width <= 0) return [];
38
63
  const results = new Map(
39
64
  agent.state.messages.flatMap((message) =>
@@ -46,51 +71,37 @@ export function createSettingsChatTranscript(agent: Agent, tui: TUI, cwd: string
46
71
  const activeMessages = new Set<AgentMessage>();
47
72
  const activeTools = new Set<string>();
48
73
  const lines: string[] = [];
74
+ renderedWidth = width;
75
+ const recordSpan = (message: AgentMessage | symbol, start: number) => {
76
+ let offset = 0;
77
+ let text = "";
78
+ const offsets = lines.slice(start).map((line) => {
79
+ const before = offset;
80
+ // Display-only offsets ignore padding and wrap-boundary whitespace.
81
+ const content = stripVTControlCharacters(line).replace(/\s/gu, "");
82
+ text += content;
83
+ offset += content.length;
84
+ return before;
85
+ });
86
+ spans.push({ message, start, end: lines.length, offsets, text });
87
+ };
49
88
  const appendAssistant = (message: AssistantMessage, component: AssistantMessageComponent) => {
50
89
  lines.push(...component.render(width));
51
90
  for (const call of message.content) {
52
91
  if (call.type !== "toolCall") continue;
53
92
  activeTools.add(call.id);
54
- let tool = tools.get(call.id);
55
- if (!tool) {
56
- const definition =
57
- call.name === "read" || call.name === "write" || call.name === "edit"
58
- ? definitions[call.name]
59
- : undefined;
60
- const component = new ToolExecutionComponent(
61
- sanitizeTerminalDocument(call.name),
62
- sanitizeTerminalDocument(call.id),
63
- displayValue(call.arguments),
64
- { showImages: false, outputPad: 1 },
65
- definition,
66
- tui,
67
- sanitizeTerminalDocument(cwd),
68
- );
69
- // Expanded output avoids advertising a tool-expand shortcut that this view does not own.
70
- component.setExpanded(true);
71
- component.setArgsComplete();
72
- component.markExecutionStarted();
73
- tool = { component };
74
- tools.set(call.id, tool);
75
- }
76
- const result = results.get(call.id);
77
- if (result && tool.result !== result) {
78
- tool.component.updateResult({
79
- content: displayValue(result.content),
80
- details: displayValue(result.details),
81
- isError: result.isError,
82
- durationMs: result.durationMs,
83
- });
84
- tool.result = result;
85
- }
86
- lines.push(...tool.component.render(width));
93
+ const start = lines.length;
94
+ lines.push(...tools.render(call, message, results.get(call.id), width));
95
+ toolSpans.push({ id: call.id, start, end: lines.length });
87
96
  }
88
97
  };
89
98
  for (const message of visible) {
90
99
  activeMessages.add(message);
100
+ let start = lines.length;
91
101
  let component = messages.get(message);
92
102
  if (message.role === "user") {
93
103
  if (lines.length) lines.push("");
104
+ start = lines.length;
94
105
  if (!component) {
95
106
  const text =
96
107
  typeof message.content === "string"
@@ -110,30 +121,90 @@ export function createSettingsChatTranscript(agent: Agent, tui: TUI, cwd: string
110
121
  }
111
122
  appendAssistant(message, component as AssistantMessageComponent);
112
123
  }
124
+ recordSpan(identities.get(message) ?? message, start);
113
125
  }
114
126
  const partial = agent.state.streamingMessage;
115
127
  if (partial?.role === "assistant") {
116
128
  streaming ??= new AssistantMessageComponent(undefined, true);
117
129
  streaming.updateContent(displayValue(partial), true);
118
- // Tool calls are rendered after finalization, not from incomplete streaming arguments.
119
- lines.push(...streaming.render(width));
130
+ const start = lines.length;
131
+ appendAssistant(partial, streaming);
132
+ recordSpan(identities.get(partial) ?? partial, start);
120
133
  } else streaming = undefined;
121
134
  for (const message of messages.keys()) if (!activeMessages.has(message)) messages.delete(message);
122
- for (const id of tools.keys()) if (!activeTools.has(id)) tools.delete(id);
135
+ tools.retain(activeTools);
123
136
  return lines;
124
137
  },
138
+ handleMouse(event: TuiMouseEvent) {
139
+ if (disposed) return undefined;
140
+ const span = toolSpans.find((item) => event.y >= item.start && event.y < item.end);
141
+ return span
142
+ ? tools.handleMouse(span.id, { ...event, y: event.y - span.start, height: span.end - span.start })
143
+ : undefined;
144
+ },
145
+ anchorAt(row: number): SettingsChatAnchor | undefined {
146
+ const span = spans.find((item) => row < item.end);
147
+ return span
148
+ ? {
149
+ message: span.message,
150
+ row: row - span.start,
151
+ width: renderedWidth,
152
+ offset: span.offsets[Math.max(0, row - span.start)] ?? 0,
153
+ text: span.text.slice(
154
+ span.offsets[Math.max(0, row - span.start)] ?? 0,
155
+ span.offsets[Math.max(0, row - span.start) + 1],
156
+ ),
157
+ }
158
+ : undefined;
159
+ },
160
+ rowAt(anchor: SettingsChatAnchor): number {
161
+ const span = spans.find((item) => item.message === anchor.message);
162
+ // If the anchored message was evicted, show the oldest retained content.
163
+ if (!span) return 0;
164
+ let row = anchor.row;
165
+ const currentText = span.text.slice(span.offsets[Math.max(0, row)], span.offsets[Math.max(0, row) + 1]);
166
+ if (anchor.width !== renderedWidth || currentText !== anchor.text) {
167
+ // Restore retained content after reflow or tool expansion/collapse.
168
+ // Match the row's content, not the accumulated length of earlier rows:
169
+ // Markdown rules and other decoration can change width independently.
170
+ let offset = anchor.offset;
171
+ if (anchor.text) {
172
+ let match = span.text.indexOf(anchor.text);
173
+ let best = -1;
174
+ while (match >= 0) {
175
+ if (best < 0 || Math.abs(match - anchor.offset) < Math.abs(best - anchor.offset)) best = match;
176
+ match = span.text.indexOf(anchor.text, match + 1);
177
+ }
178
+ if (best >= 0) offset = best;
179
+ }
180
+ row = 0;
181
+ for (let index = 0; index < span.offsets.length; index++) {
182
+ if (span.offsets[index] > offset) break;
183
+ row = index;
184
+ }
185
+ }
186
+ return Math.max(0, span.start + Math.min(row, span.end - span.start - 1));
187
+ },
188
+ toggleTools() {
189
+ return tools.toggle();
190
+ },
125
191
  invalidate() {
126
192
  if (disposed) return;
127
193
  // Native Markdown themes can retain concrete colors. Recreate display components
128
194
  // rather than retaining colors captured before a host theme change.
129
195
  messages.clear();
130
- tools.clear();
196
+ tools.invalidate();
131
197
  streaming = undefined;
132
198
  },
133
199
  dispose() {
200
+ if (disposed) return;
134
201
  disposed = true;
202
+ unsubscribe();
203
+ streamingIdentity = undefined;
204
+ spans = [];
205
+ toolSpans = [];
135
206
  messages.clear();
136
- tools.clear();
207
+ tools.dispose();
137
208
  streaming = undefined;
138
209
  },
139
210
  };