@abelxiaoxing/cadence 2.0.36 → 2.0.38
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/README.de.md +4 -4
- package/README.es.md +4 -4
- package/README.fr.md +4 -4
- package/README.ja.md +4 -4
- package/README.ko.md +4 -4
- package/README.md +11 -9
- package/README.pt-BR.md +4 -4
- package/README.ru.md +4 -4
- package/README.zh-CN.md +12 -10
- package/THIRD_PARTY_NOTICES.md +9 -1
- package/agents/design-explorer.md +4 -2
- package/agents/diagnosis-worker.md +5 -2
- package/agents/implementation-worker.md +19 -12
- package/docs/conversation-mode.md +46 -18
- package/docs/reuse-and-distribution.md +1 -1
- package/docs/service-management.md +35 -9
- package/package.json +16 -8
- package/src/agent-browser/labeled-rule.ts +74 -0
- package/src/agent-browser/session-navigator.ts +214 -0
- package/src/agent-browser/source.ts +174 -0
- package/src/agent-browser/transcript-content.ts +442 -0
- package/src/agent-browser/transcript-pane.ts +324 -0
- package/src/agent-panel-cadence.ts +147 -628
- package/src/agent-panel-model.ts +34 -267
- package/src/agent-panel.ts +139 -437
- package/src/cadence-instructions.ts +8 -7
- package/src/cadence-mode.ts +30 -9
- package/src/change-tools.mjs +268 -0
- package/src/coordinator-tools.mjs +44 -16
- package/src/git-environment.d.mts +2 -0
- package/src/git-environment.mjs +24 -0
- package/src/image-content.mjs +7 -28
- package/src/index.ts +1 -1
- package/src/native-build.ts +1 -1
- package/src/native-workflow-history.ts +20 -3
- package/src/operator-cli.mjs +18 -11
- package/src/research-scan.mjs +393 -0
- package/src/research-tools.mjs +406 -342
- package/src/sdk-worker.mjs +42 -55
- package/src/service/contracts/core.ts +0 -1
- package/src/service/contracts/workbench.ts +21 -19
- package/src/service/contracts/workflow.ts +283 -16
- package/src/service/coordination.ts +7 -0
- package/src/service-cli.mjs +652 -149
- package/src/service-host.mjs +2745 -1137
- package/src/worker-capture.mjs +24 -1
- package/src/worker-public.mjs +19 -2
- package/src/worker-redaction.mjs +11 -0
- package/src/worker-toolkit.mjs +157 -0
- package/src/worker-workspace.d.mts +13 -0
- package/src/worker-workspace.mjs +48 -0
- package/src/workspace.ts +4 -24
- package/web/dist/assets/cadence.css +1 -1
- package/web/dist/assets/cadence.js +18 -17
- package/web/dist/manifest.json +7 -7
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
/** The upstream browser's readonly source seam, adapted to Cadence's bus.
|
|
2
|
+
* No manager, SDK session, filesystem path or execution operation crosses it. */
|
|
3
|
+
import type {
|
|
4
|
+
AgentSession,
|
|
5
|
+
Theme,
|
|
6
|
+
ToolDefinition,
|
|
7
|
+
} from "@earendil-works/pi-coding-agent";
|
|
8
|
+
import {
|
|
9
|
+
described,
|
|
10
|
+
factsText,
|
|
11
|
+
type SubagentState,
|
|
12
|
+
type SubagentStore,
|
|
13
|
+
} from "../agent-panel-model.ts";
|
|
14
|
+
|
|
15
|
+
export type SessionMessage = AgentSession["messages"][number];
|
|
16
|
+
export interface ModelIdentity {
|
|
17
|
+
provider: string;
|
|
18
|
+
id: string;
|
|
19
|
+
}
|
|
20
|
+
export interface EntryHeading {
|
|
21
|
+
name: string;
|
|
22
|
+
modeLabel: string | undefined;
|
|
23
|
+
description: string;
|
|
24
|
+
}
|
|
25
|
+
export interface TranscriptSource {
|
|
26
|
+
getMessages(): readonly SessionMessage[];
|
|
27
|
+
inFlight(): Extract<SessionMessage, { role: "assistant" }> | undefined;
|
|
28
|
+
subscribe(onChange: () => void): (() => void) | undefined;
|
|
29
|
+
streaming(): string | undefined;
|
|
30
|
+
notice(): string | undefined;
|
|
31
|
+
getToolDefinition(name: string): ToolDefinition | undefined;
|
|
32
|
+
sessionModel(): {
|
|
33
|
+
model: ModelIdentity | undefined;
|
|
34
|
+
thinkingLevel: Parameters<Theme["getThinkingBorderColor"]>[0] | undefined;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export interface NavigationEntry {
|
|
38
|
+
id: string;
|
|
39
|
+
label: string;
|
|
40
|
+
heading: EntryHeading;
|
|
41
|
+
source: TranscriptSource;
|
|
42
|
+
}
|
|
43
|
+
export interface BrowserSource {
|
|
44
|
+
entries(): NavigationEntry[];
|
|
45
|
+
subscribe(changed: () => void): () => void;
|
|
46
|
+
view(id: string | null): void;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Keep the producer's message identities so the upstream content cache can
|
|
50
|
+
* append settled blocks without rebuilding earlier messages. */
|
|
51
|
+
function renderable(message: object): boolean {
|
|
52
|
+
const m = message as { role?: unknown; content?: unknown };
|
|
53
|
+
if (m.role === "user")
|
|
54
|
+
return (
|
|
55
|
+
typeof m.content === "string" ||
|
|
56
|
+
(Array.isArray(m.content) &&
|
|
57
|
+
m.content.every((block) => block && typeof block === "object"))
|
|
58
|
+
);
|
|
59
|
+
if (m.role === "toolResult") return Array.isArray(m.content);
|
|
60
|
+
if (m.role !== "assistant" || !Array.isArray(m.content)) return false;
|
|
61
|
+
return m.content.every(
|
|
62
|
+
(block) =>
|
|
63
|
+
block &&
|
|
64
|
+
typeof block === "object" &&
|
|
65
|
+
((block.type === "text" && typeof block.text === "string") ||
|
|
66
|
+
(block.type === "thinking" && typeof block.thinking === "string") ||
|
|
67
|
+
(block.type === "toolCall" &&
|
|
68
|
+
typeof block.name === "string" &&
|
|
69
|
+
typeof block.id === "string")),
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function transcriptSource(
|
|
74
|
+
store: SubagentStore,
|
|
75
|
+
agent: SubagentState,
|
|
76
|
+
): TranscriptSource {
|
|
77
|
+
let revision = -1;
|
|
78
|
+
let messages: SessionMessage[] = [];
|
|
79
|
+
let partial: Extract<SessionMessage, { role: "assistant" }> | undefined;
|
|
80
|
+
const refresh = () => {
|
|
81
|
+
if (revision === agent.revision) return;
|
|
82
|
+
revision = agent.revision;
|
|
83
|
+
const visible = agent.messages.filter(renderable) as SessionMessage[];
|
|
84
|
+
const tail = visible.at(-1);
|
|
85
|
+
// The feed replaces a streaming snapshot rather than emitting SDK deltas.
|
|
86
|
+
// Keep it out of settled history so the upstream cache only repaints the
|
|
87
|
+
// live tail. Clone it because a producer may mutate that snapshot in place.
|
|
88
|
+
partial =
|
|
89
|
+
agent.streaming &&
|
|
90
|
+
tail === agent.messages.at(-1) &&
|
|
91
|
+
tail?.role === "assistant"
|
|
92
|
+
? { ...tail }
|
|
93
|
+
: undefined;
|
|
94
|
+
messages = partial ? visible.slice(0, -1) : visible;
|
|
95
|
+
if (agent.streaming && !partial && tail)
|
|
96
|
+
messages[messages.length - 1] = { ...tail };
|
|
97
|
+
};
|
|
98
|
+
return {
|
|
99
|
+
getMessages() {
|
|
100
|
+
refresh();
|
|
101
|
+
return messages;
|
|
102
|
+
},
|
|
103
|
+
inFlight() {
|
|
104
|
+
refresh();
|
|
105
|
+
return partial;
|
|
106
|
+
},
|
|
107
|
+
subscribe: (changed) => {
|
|
108
|
+
let seenRevision = agent.revision;
|
|
109
|
+
let seenStatus = agent.status;
|
|
110
|
+
let seenActivity = agent.activity;
|
|
111
|
+
return store.subscribe(() => {
|
|
112
|
+
if (
|
|
113
|
+
seenRevision === agent.revision &&
|
|
114
|
+
seenStatus === agent.status &&
|
|
115
|
+
seenActivity === agent.activity
|
|
116
|
+
)
|
|
117
|
+
return;
|
|
118
|
+
seenRevision = agent.revision;
|
|
119
|
+
seenStatus = agent.status;
|
|
120
|
+
seenActivity = agent.activity;
|
|
121
|
+
changed();
|
|
122
|
+
});
|
|
123
|
+
},
|
|
124
|
+
streaming: () =>
|
|
125
|
+
agent.status === "running" ? agent.activity || "Working" : undefined,
|
|
126
|
+
notice: () =>
|
|
127
|
+
agent.omitted
|
|
128
|
+
? `… ${agent.omitted} earlier messages not retained; see /cadence-history`
|
|
129
|
+
: !agent.messages.length
|
|
130
|
+
? "No retained messages observed yet"
|
|
131
|
+
: undefined,
|
|
132
|
+
getToolDefinition: () => undefined,
|
|
133
|
+
// The feed records no model or thinking evidence: do not borrow the parent's.
|
|
134
|
+
sessionModel: () => ({ model: undefined, thinkingLevel: undefined }),
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function cadenceBrowserSource(
|
|
139
|
+
store: SubagentStore,
|
|
140
|
+
view: (id: string | null) => void,
|
|
141
|
+
now: () => number,
|
|
142
|
+
): BrowserSource {
|
|
143
|
+
const sources = new Map<string, TranscriptSource>();
|
|
144
|
+
return {
|
|
145
|
+
entries: () => {
|
|
146
|
+
for (const id of sources.keys())
|
|
147
|
+
if (store.get(id)?.status !== "running") sources.delete(id);
|
|
148
|
+
return store
|
|
149
|
+
.ordered()
|
|
150
|
+
.filter((agent) => agent.status === "running")
|
|
151
|
+
.map((agent) => {
|
|
152
|
+
let source = sources.get(agent.id);
|
|
153
|
+
if (!source) {
|
|
154
|
+
source = transcriptSource(store, agent);
|
|
155
|
+
sources.set(agent.id, source);
|
|
156
|
+
}
|
|
157
|
+
return {
|
|
158
|
+
id: agent.id,
|
|
159
|
+
label: `${described(agent)} · ${agent.status} · ${factsText(agent, now())}`,
|
|
160
|
+
heading: {
|
|
161
|
+
name: agent.label,
|
|
162
|
+
modeLabel: agent.kind,
|
|
163
|
+
get description() {
|
|
164
|
+
return agent.status;
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
source,
|
|
168
|
+
};
|
|
169
|
+
});
|
|
170
|
+
},
|
|
171
|
+
subscribe: (changed) => store.subscribe(changed),
|
|
172
|
+
view,
|
|
173
|
+
};
|
|
174
|
+
}
|
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
/** Adapted from gotgenes/pi-packages @ 52854c12d590142e61a4f6a6947be9084035300d.
|
|
2
|
+
* MIT; see licenses/pi-subagents-MIT.txt and provenance/agent-browser-reuse.json. */
|
|
3
|
+
/**
|
|
4
|
+
* transcript-content.ts — the rows the `/subagents:sessions` overlay paints.
|
|
5
|
+
*
|
|
6
|
+
* Owns the transcript's content model: the `SessionMessage` → Pi-component
|
|
7
|
+
* mapping (mirroring Pi's own interactive-mode `renderSessionContext`), and the
|
|
8
|
+
* rendered rows those components produce at a given width. The overlay
|
|
9
|
+
* (`session-navigator.ts`) owns scroll state, chrome, and key handling, and
|
|
10
|
+
* asks this collaborator only for rows — it never reaches into the components.
|
|
11
|
+
*
|
|
12
|
+
* Lives in the SDK/TUI layer rather than the pure `session-navigation.ts` core
|
|
13
|
+
* because Pi's per-entry components require a `TUI`, `cwd`, and markdown theme.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import {
|
|
17
|
+
AssistantMessageComponent,
|
|
18
|
+
BashExecutionComponent,
|
|
19
|
+
BranchSummaryMessageComponent,
|
|
20
|
+
CompactionSummaryMessageComponent,
|
|
21
|
+
parseSkillBlock,
|
|
22
|
+
SkillInvocationMessageComponent,
|
|
23
|
+
ToolExecutionComponent,
|
|
24
|
+
UserMessageComponent,
|
|
25
|
+
} from "@earendil-works/pi-coding-agent";
|
|
26
|
+
import {
|
|
27
|
+
Container,
|
|
28
|
+
type MarkdownTheme,
|
|
29
|
+
Spacer,
|
|
30
|
+
type TUI,
|
|
31
|
+
truncateToWidth,
|
|
32
|
+
} from "@earendil-works/pi-tui";
|
|
33
|
+
import type { SessionMessage, TranscriptSource } from "./source.ts";
|
|
34
|
+
|
|
35
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
36
|
+
|
|
37
|
+
/** The SDK/TUI environment Pi's per-entry components need, plus the transcript's source. */
|
|
38
|
+
export interface TranscriptContentOptions {
|
|
39
|
+
tui: TUI;
|
|
40
|
+
cwd: string;
|
|
41
|
+
markdownTheme: MarkdownTheme;
|
|
42
|
+
source: TranscriptSource;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The transcript's renderable rows: settled history rendered through Pi's
|
|
47
|
+
* per-entry components, followed by the running agent's activity row.
|
|
48
|
+
*
|
|
49
|
+
* Settled rows are rendered once per width and cached, so a paint or a scroll
|
|
50
|
+
* costs a slice rather than a walk of the whole component tree. The activity
|
|
51
|
+
* row is recomputed per call — it is two rows, and it tracks live state the
|
|
52
|
+
* source updates without a rebuild.
|
|
53
|
+
*
|
|
54
|
+
* Settled history is held as one block of components per message, so a message
|
|
55
|
+
* that settles appends a block and a tool result touches only its own — neither
|
|
56
|
+
* discards the rest of the transcript's rendering. This is sound because the
|
|
57
|
+
* agent core pushes a finished message into its message array before notifying
|
|
58
|
+
* listeners, so at event time the settled prefix is already visible.
|
|
59
|
+
*
|
|
60
|
+
* The message currently being streamed is held as its own component, updated
|
|
61
|
+
* per delta exactly as Pi's interactive mode does, so a token never touches
|
|
62
|
+
* settled history. The two provably cannot overlap: the agent core keeps the
|
|
63
|
+
* in-flight message outside its message array until it settles.
|
|
64
|
+
*/
|
|
65
|
+
export class TranscriptContent {
|
|
66
|
+
private readonly options: TranscriptContentOptions;
|
|
67
|
+
private expanded = false;
|
|
68
|
+
/** Settled history, one block per message that produced components. */
|
|
69
|
+
private blocks: SettledBlock[] = [];
|
|
70
|
+
/** How many source messages have been consumed into `blocks`. */
|
|
71
|
+
private consumedCount = 0;
|
|
72
|
+
/** The last consumed message; a mismatch means history was rewritten. */
|
|
73
|
+
private lastConsumed: SessionMessage | undefined;
|
|
74
|
+
/** Whether any consumed block holds components, driving user-message spacing. */
|
|
75
|
+
private hasVisibleContent = false;
|
|
76
|
+
/** In-flight tool components by tool-call id, pairing a later result to its block. */
|
|
77
|
+
private readonly pendingTools = new Map<string, PendingTool>();
|
|
78
|
+
/** Width `blocks` and `settledRows` were rendered at. */
|
|
79
|
+
private settledWidth: number | undefined;
|
|
80
|
+
/** Concatenation of every block's rows, or undefined when the cache is cold. */
|
|
81
|
+
private settledRows: readonly string[] | undefined;
|
|
82
|
+
/** The message being streamed right now, rendered below settled history. */
|
|
83
|
+
private inFlight: AssistantMessageComponent | undefined;
|
|
84
|
+
/** Width `inFlightRows` was rendered at. */
|
|
85
|
+
private inFlightWidth: number | undefined;
|
|
86
|
+
private inFlightRows: readonly string[] | undefined;
|
|
87
|
+
|
|
88
|
+
constructor(options: TranscriptContentOptions) {
|
|
89
|
+
this.options = options;
|
|
90
|
+
this.apply();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Total content rows at `width`: settled history plus the live tail. */
|
|
94
|
+
lineCount(width: number): number {
|
|
95
|
+
if (width <= 0) return 0;
|
|
96
|
+
return this.settled(width).length + this.tail(width).length;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Rows `[start, start + count)` at `width`, clamped to what exists. */
|
|
100
|
+
slice(width: number, start: number, count: number): string[] {
|
|
101
|
+
if (width <= 0) return [];
|
|
102
|
+
const settled = this.settled(width);
|
|
103
|
+
const end = start + count;
|
|
104
|
+
const rows = settled.slice(start, Math.min(end, settled.length));
|
|
105
|
+
if (end > settled.length) {
|
|
106
|
+
const tail = this.tail(width);
|
|
107
|
+
rows.push(
|
|
108
|
+
...tail.slice(
|
|
109
|
+
Math.max(0, start - settled.length),
|
|
110
|
+
end - settled.length,
|
|
111
|
+
),
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
return rows;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** The source adapts Cadence snapshots to the upstream settled/live split;
|
|
118
|
+
* no synthetic SDK event or running SDK session is required by this view. */
|
|
119
|
+
apply(): void {
|
|
120
|
+
this.consumeSettled();
|
|
121
|
+
const partial = this.options.source.inFlight();
|
|
122
|
+
if (partial) this.updateInFlight(partial);
|
|
123
|
+
else this.clearInFlight();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
toggleExpanded(): void {
|
|
127
|
+
this.expanded = !this.expanded;
|
|
128
|
+
this.reset();
|
|
129
|
+
this.apply();
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Drop cached rendering held by the mounted components and by this object. */
|
|
133
|
+
invalidate(): void {
|
|
134
|
+
for (const block of this.blocks) {
|
|
135
|
+
block.container.invalidate();
|
|
136
|
+
block.rows = undefined;
|
|
137
|
+
}
|
|
138
|
+
this.settledRows = undefined;
|
|
139
|
+
this.inFlight?.invalidate();
|
|
140
|
+
this.inFlightRows = undefined;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// ---- Private ----
|
|
144
|
+
|
|
145
|
+
/** Settled rows at `width`; each block renders once per width and content change. */
|
|
146
|
+
private settled(width: number): readonly string[] {
|
|
147
|
+
if (this.settledWidth !== width) {
|
|
148
|
+
this.settledWidth = width;
|
|
149
|
+
for (const block of this.blocks) block.rows = undefined;
|
|
150
|
+
this.settledRows = undefined;
|
|
151
|
+
}
|
|
152
|
+
if (this.settledRows) return this.settledRows;
|
|
153
|
+
const rows: string[] = [];
|
|
154
|
+
for (const block of this.blocks) {
|
|
155
|
+
block.rows ??= block.container
|
|
156
|
+
.render(width)
|
|
157
|
+
.map((row) => truncateToWidth(stripPromptMarks(row), width));
|
|
158
|
+
rows.push(...block.rows);
|
|
159
|
+
}
|
|
160
|
+
this.settledRows = rows;
|
|
161
|
+
return rows;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Rows below settled history: the in-flight message, then the activity row. */
|
|
165
|
+
private tail(width: number): readonly string[] {
|
|
166
|
+
const rows = [...this.inFlightRendered(width)];
|
|
167
|
+
const notice = this.options.source.notice();
|
|
168
|
+
if (notice) rows.push(truncateToWidth(notice, width));
|
|
169
|
+
const streaming = this.options.source.streaming();
|
|
170
|
+
if (streaming) {
|
|
171
|
+
const activity = `◍ ${streaming}`;
|
|
172
|
+
rows.push("", truncateToWidth(activity, width));
|
|
173
|
+
}
|
|
174
|
+
return rows;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** The in-flight message's rows, re-rendered independently of settled history. */
|
|
178
|
+
private inFlightRendered(width: number): readonly string[] {
|
|
179
|
+
if (!this.inFlight) return [];
|
|
180
|
+
if (this.inFlightWidth !== width) {
|
|
181
|
+
this.inFlightWidth = width;
|
|
182
|
+
this.inFlightRows = undefined;
|
|
183
|
+
}
|
|
184
|
+
this.inFlightRows ??= this.inFlight
|
|
185
|
+
.render(width)
|
|
186
|
+
.map((row) => truncateToWidth(stripPromptMarks(row), width));
|
|
187
|
+
return this.inFlightRows;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
private updateInFlight(message: AssistantSessionMessage): void {
|
|
191
|
+
if (this.inFlight) this.inFlight.updateContent(message);
|
|
192
|
+
else
|
|
193
|
+
this.inFlight = new AssistantMessageComponent(
|
|
194
|
+
message,
|
|
195
|
+
true,
|
|
196
|
+
this.options.markdownTheme,
|
|
197
|
+
);
|
|
198
|
+
this.inFlightRows = undefined;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
private clearInFlight(): void {
|
|
202
|
+
this.inFlight = undefined;
|
|
203
|
+
this.inFlightRows = undefined;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** Discard all settled state, so the next consume rebuilds from scratch. */
|
|
207
|
+
private reset(): void {
|
|
208
|
+
this.blocks = [];
|
|
209
|
+
this.consumedCount = 0;
|
|
210
|
+
this.lastConsumed = undefined;
|
|
211
|
+
this.hasVisibleContent = false;
|
|
212
|
+
this.pendingTools.clear();
|
|
213
|
+
this.settledRows = undefined;
|
|
214
|
+
this.clearInFlight();
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Append blocks for every message that settled since the last check. When the
|
|
219
|
+
* consumed prefix no longer mirrors the source — history replaced wholesale by
|
|
220
|
+
* compaction or branching — start over rather than appending onto stale blocks.
|
|
221
|
+
*/
|
|
222
|
+
private consumeSettled(): void {
|
|
223
|
+
const messages = this.options.source.getMessages();
|
|
224
|
+
if (this.hasRewrittenHistory(messages)) this.reset();
|
|
225
|
+
if (messages.length === this.consumedCount) return;
|
|
226
|
+
for (let i = this.consumedCount; i < messages.length; i++)
|
|
227
|
+
this.consumeMessage(messages[i]);
|
|
228
|
+
this.consumedCount = messages.length;
|
|
229
|
+
this.lastConsumed = messages.at(-1);
|
|
230
|
+
this.settledRows = undefined;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
private hasRewrittenHistory(messages: readonly SessionMessage[]): boolean {
|
|
234
|
+
if (messages.length < this.consumedCount) return true;
|
|
235
|
+
return (
|
|
236
|
+
this.consumedCount > 0 &&
|
|
237
|
+
messages[this.consumedCount - 1] !== this.lastConsumed
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Map one settled message onto its own block of Pi's per-entry components,
|
|
243
|
+
* mirroring Pi's own interactive-mode `renderSessionContext` mapping.
|
|
244
|
+
* `custom`-role messages produce no block — rendering them needs the child
|
|
245
|
+
* session's message-renderer registry, which the navigator does not hold.
|
|
246
|
+
*/
|
|
247
|
+
private consumeMessage(message: SessionMessage): void {
|
|
248
|
+
switch (message.role) {
|
|
249
|
+
case "assistant":
|
|
250
|
+
this.consumeAssistant(message);
|
|
251
|
+
break;
|
|
252
|
+
case "toolResult":
|
|
253
|
+
this.consumeToolResult(message);
|
|
254
|
+
break;
|
|
255
|
+
case "user":
|
|
256
|
+
this.consumeUser(message);
|
|
257
|
+
break;
|
|
258
|
+
case "bashExecution":
|
|
259
|
+
this.consumeBashExecution(message);
|
|
260
|
+
break;
|
|
261
|
+
case "compactionSummary":
|
|
262
|
+
this.consumeSummary(
|
|
263
|
+
new CompactionSummaryMessageComponent(
|
|
264
|
+
message,
|
|
265
|
+
this.options.markdownTheme,
|
|
266
|
+
),
|
|
267
|
+
);
|
|
268
|
+
break;
|
|
269
|
+
case "branchSummary":
|
|
270
|
+
this.consumeSummary(
|
|
271
|
+
new BranchSummaryMessageComponent(
|
|
272
|
+
message,
|
|
273
|
+
this.options.markdownTheme,
|
|
274
|
+
),
|
|
275
|
+
);
|
|
276
|
+
break;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
private consumeAssistant(message: AssistantSessionMessage): void {
|
|
281
|
+
const block = this.appendBlock();
|
|
282
|
+
block.container.addChild(
|
|
283
|
+
new AssistantMessageComponent(message, true, this.options.markdownTheme),
|
|
284
|
+
);
|
|
285
|
+
for (const content of message.content) {
|
|
286
|
+
if (content.type !== "toolCall") continue;
|
|
287
|
+
const tool = new ToolExecutionComponent(
|
|
288
|
+
content.name,
|
|
289
|
+
content.id,
|
|
290
|
+
content.arguments,
|
|
291
|
+
{ showImages: false },
|
|
292
|
+
this.options.source.getToolDefinition(content.name),
|
|
293
|
+
this.options.tui,
|
|
294
|
+
this.options.cwd,
|
|
295
|
+
);
|
|
296
|
+
tool.setExpanded(this.expanded);
|
|
297
|
+
// Retained edits must never preview a diff against today's checkout.
|
|
298
|
+
if (content.name !== "edit") tool.setArgsComplete();
|
|
299
|
+
block.container.addChild(tool);
|
|
300
|
+
this.pendingTools.set(content.id, { component: tool, block });
|
|
301
|
+
}
|
|
302
|
+
this.hasVisibleContent = true;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/** A tool result mutates the block holding its call; no other block changes. */
|
|
306
|
+
private consumeToolResult(
|
|
307
|
+
message: Extract<SessionMessage, { role: "toolResult" }>,
|
|
308
|
+
): void {
|
|
309
|
+
let pending = this.pendingTools.get(message.toolCallId);
|
|
310
|
+
if (!pending) {
|
|
311
|
+
// The bounded feed can retain a result after its call fell out of view.
|
|
312
|
+
const block = this.appendBlock();
|
|
313
|
+
const component = new ToolExecutionComponent(
|
|
314
|
+
message.toolName,
|
|
315
|
+
message.toolCallId,
|
|
316
|
+
{},
|
|
317
|
+
{ showImages: false },
|
|
318
|
+
undefined,
|
|
319
|
+
this.options.tui,
|
|
320
|
+
this.options.cwd,
|
|
321
|
+
);
|
|
322
|
+
component.setExpanded(this.expanded);
|
|
323
|
+
block.container.addChild(component);
|
|
324
|
+
pending = { component, block };
|
|
325
|
+
}
|
|
326
|
+
pending.component.updateResult(message);
|
|
327
|
+
pending.block.rows = undefined;
|
|
328
|
+
this.settledRows = undefined;
|
|
329
|
+
this.pendingTools.delete(message.toolCallId);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
private consumeUser(
|
|
333
|
+
message: Extract<SessionMessage, { role: "user" }>,
|
|
334
|
+
): void {
|
|
335
|
+
const block = this.appendBlock();
|
|
336
|
+
addUserComponents(
|
|
337
|
+
block.container,
|
|
338
|
+
message.content,
|
|
339
|
+
this.options.markdownTheme,
|
|
340
|
+
this.hasVisibleContent,
|
|
341
|
+
);
|
|
342
|
+
if (block.container.children.length > 0) this.hasVisibleContent = true;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
private consumeBashExecution(
|
|
346
|
+
message: Extract<SessionMessage, { role: "bashExecution" }>,
|
|
347
|
+
): void {
|
|
348
|
+
const block = this.appendBlock();
|
|
349
|
+
const bash = new BashExecutionComponent(
|
|
350
|
+
message.command,
|
|
351
|
+
this.options.tui,
|
|
352
|
+
message.excludeFromContext,
|
|
353
|
+
);
|
|
354
|
+
if (message.output) bash.appendOutput(message.output);
|
|
355
|
+
bash.setComplete(
|
|
356
|
+
message.exitCode,
|
|
357
|
+
message.cancelled,
|
|
358
|
+
undefined,
|
|
359
|
+
message.fullOutputPath,
|
|
360
|
+
);
|
|
361
|
+
block.container.addChild(bash);
|
|
362
|
+
this.hasVisibleContent = true;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/** Compaction and branch summaries share the same spacer-plus-expanded shape. */
|
|
366
|
+
private consumeSummary(
|
|
367
|
+
summary: CompactionSummaryMessageComponent | BranchSummaryMessageComponent,
|
|
368
|
+
): void {
|
|
369
|
+
const block = this.appendBlock();
|
|
370
|
+
block.container.addChild(new Spacer(1));
|
|
371
|
+
summary.setExpanded(true);
|
|
372
|
+
block.container.addChild(summary);
|
|
373
|
+
this.hasVisibleContent = true;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
private appendBlock(): SettledBlock {
|
|
377
|
+
const block: SettledBlock = { container: new Container(), rows: undefined };
|
|
378
|
+
this.blocks.push(block);
|
|
379
|
+
return block;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/** One settled message's components plus its rows at the current width. */
|
|
384
|
+
interface SettledBlock {
|
|
385
|
+
readonly container: Container;
|
|
386
|
+
rows: readonly string[] | undefined;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/** A tool call awaiting its result, and the block whose rows it will invalidate. */
|
|
390
|
+
interface PendingTool {
|
|
391
|
+
readonly component: ToolExecutionComponent;
|
|
392
|
+
readonly block: SettledBlock;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/** The assistant variant of a session message, as Pi's components consume it. */
|
|
396
|
+
type AssistantSessionMessage = Extract<SessionMessage, { role: "assistant" }>;
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Render a user message (skill block + text) into the block, mirroring Pi.
|
|
400
|
+
* Whether a leading spacer is needed is a whole-transcript property, so the
|
|
401
|
+
* caller — which alone knows what precedes this block — supplies it.
|
|
402
|
+
*/
|
|
403
|
+
function addUserComponents(
|
|
404
|
+
container: Container,
|
|
405
|
+
content: string | readonly { type: string; text?: string }[],
|
|
406
|
+
markdownTheme: MarkdownTheme,
|
|
407
|
+
hasPrecedingContent: boolean,
|
|
408
|
+
): void {
|
|
409
|
+
const text = userMessageText(content);
|
|
410
|
+
if (!text) return;
|
|
411
|
+
if (hasPrecedingContent) container.addChild(new Spacer(1));
|
|
412
|
+
|
|
413
|
+
const skillBlock = parseSkillBlock(text);
|
|
414
|
+
if (!skillBlock) {
|
|
415
|
+
container.addChild(new UserMessageComponent(text, markdownTheme));
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
const skill = new SkillInvocationMessageComponent(skillBlock, markdownTheme);
|
|
419
|
+
skill.setExpanded(true);
|
|
420
|
+
container.addChild(skill);
|
|
421
|
+
if (skillBlock.userMessage) {
|
|
422
|
+
container.addChild(new Spacer(1));
|
|
423
|
+
container.addChild(
|
|
424
|
+
new UserMessageComponent(skillBlock.userMessage, markdownTheme),
|
|
425
|
+
);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/** Concatenate the text blocks of a user message's content (mirrors Pi). */
|
|
430
|
+
function userMessageText(
|
|
431
|
+
content: string | readonly { type: string; text?: string }[],
|
|
432
|
+
): string {
|
|
433
|
+
if (typeof content === "string") return content;
|
|
434
|
+
return content
|
|
435
|
+
.filter((block) => block.type === "text")
|
|
436
|
+
.map((block) => block.text ?? "")
|
|
437
|
+
.join("");
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
// biome-ignore lint/suspicious/noControlCharactersInRegex: remove unmatched shell-integration marks
|
|
441
|
+
const OSC_133 = /\x1b\]133;[^\x07\x1b]*(?:\x07|\x1b\\)/g;
|
|
442
|
+
const stripPromptMarks = (line: string) => line.replace(OSC_133, "");
|