@luxass/pi-btw 0.0.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/README.md +23 -0
- package/package.json +38 -0
- package/src/index.ts +147 -0
- package/src/overlay.ts +179 -0
- package/src/session.ts +95 -0
package/README.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# @luxass/pi-btw
|
|
2
|
+
|
|
3
|
+
Ask [Pi](https://github.com/badlogic/pi-mono) a side question in a popover without derailing the main conversation.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pi install npm:@luxass/pi-btw
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Use
|
|
12
|
+
|
|
13
|
+
Run `/btw` to open the side thread, or `/btw <text>` to open it and ask right away. If a thread already exists, `/btw` asks whether to continue it or start fresh.
|
|
14
|
+
|
|
15
|
+
In the popover, **Enter** asks, **Esc** stops a running answer or closes the popover, and **↑↓** / **PgUp PgDn** scroll. Closing a non-empty thread asks whether to keep it or inject a summary into the main chat.
|
|
16
|
+
|
|
17
|
+
The side thread sees the main conversation up to the moment you ask, using your current model and thinking level. Its tools are read-only (`read`, `grep`, `find`, `ls`), so it can look things up but not change files. Because the side request starts with the same prefix as the main chat, providers with prompt caching can reuse the main conversation's cache.
|
|
18
|
+
|
|
19
|
+
Injecting asks the model for a summary, sends it as a user message, and clears the side thread. If the main agent is busy, the summary is queued as a follow-up.
|
|
20
|
+
|
|
21
|
+
The side thread is stored in the session log, follows branch navigation, and never enters the main agent's context unless you inject it.
|
|
22
|
+
|
|
23
|
+
Models from providers registered by other extensions are not available in the side thread; it resolves models from Pi's `auth.json` and `models.json`.
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@luxass/pi-btw",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Side questions in a Pi popover",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"pi-package"
|
|
7
|
+
],
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/luxass/pi-extensions",
|
|
11
|
+
"directory": "packages/btw"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"src",
|
|
15
|
+
"README.md"
|
|
16
|
+
],
|
|
17
|
+
"type": "module",
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
21
|
+
"peerDependencies": {
|
|
22
|
+
"@earendil-works/pi-ai": "*",
|
|
23
|
+
"@earendil-works/pi-coding-agent": "*",
|
|
24
|
+
"@earendil-works/pi-tui": "*"
|
|
25
|
+
},
|
|
26
|
+
"pi": {
|
|
27
|
+
"extensions": [
|
|
28
|
+
"./src/index.ts"
|
|
29
|
+
]
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"lint": "oxlint --deny-warnings .",
|
|
33
|
+
"lint:fix": "oxlint --fix --deny-warnings .",
|
|
34
|
+
"fmt": "oxfmt .",
|
|
35
|
+
"fmt:check": "oxfmt --check .",
|
|
36
|
+
"typecheck": "tsc -p tsconfig.json"
|
|
37
|
+
}
|
|
38
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AgentSession,
|
|
3
|
+
AgentSessionEvent,
|
|
4
|
+
ExtensionAPI,
|
|
5
|
+
ExtensionCommandContext,
|
|
6
|
+
ExtensionContext,
|
|
7
|
+
} from "@earendil-works/pi-coding-agent";
|
|
8
|
+
|
|
9
|
+
import { createOverlay } from "./overlay";
|
|
10
|
+
import { answerText, createSideSession, summarize, type BtwState, type Exchange } from "./session";
|
|
11
|
+
|
|
12
|
+
const EXCHANGE_ENTRY = "btw-exchange";
|
|
13
|
+
const RESET_ENTRY = "btw-reset";
|
|
14
|
+
|
|
15
|
+
const CONTINUE = "Continue side thread";
|
|
16
|
+
const FRESH = "Start fresh";
|
|
17
|
+
const KEEP = "Keep side thread";
|
|
18
|
+
const INJECT = "Inject summary into main chat";
|
|
19
|
+
|
|
20
|
+
export default function btw(pi: ExtensionAPI) {
|
|
21
|
+
const state: BtwState = { thread: [], busy: false };
|
|
22
|
+
|
|
23
|
+
function restore(ctx: ExtensionContext): void {
|
|
24
|
+
state.thread = [];
|
|
25
|
+
for (const entry of ctx.sessionManager.getBranch()) {
|
|
26
|
+
if (entry.type !== "custom") continue;
|
|
27
|
+
if (entry.customType === RESET_ENTRY) state.thread = [];
|
|
28
|
+
if (entry.customType === EXCHANGE_ENTRY) state.thread.push(entry.data as Exchange);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function reset(): void {
|
|
33
|
+
if (state.thread.length === 0) return;
|
|
34
|
+
state.thread = [];
|
|
35
|
+
pi.appendEntry(RESET_ENTRY);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function track(event: AgentSessionEvent): void {
|
|
39
|
+
const { pending } = state;
|
|
40
|
+
if (pending == null) return;
|
|
41
|
+
if (event.type === "message_update" && event.message.role === "assistant") {
|
|
42
|
+
pending.text = answerText(event.message);
|
|
43
|
+
} else if (event.type === "tool_execution_start") {
|
|
44
|
+
const { path, pattern } = event.args as { path?: string; pattern?: string };
|
|
45
|
+
pending.tools.push({
|
|
46
|
+
id: event.toolCallId,
|
|
47
|
+
name: event.toolName,
|
|
48
|
+
detail: pattern ?? path ?? "",
|
|
49
|
+
status: "running",
|
|
50
|
+
});
|
|
51
|
+
} else if (event.type === "tool_execution_end") {
|
|
52
|
+
const row = pending.tools.find(({ id }) => id === event.toolCallId);
|
|
53
|
+
if (row) row.status = event.isError ? "error" : "done";
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async function open(ctx: ExtensionCommandContext, initial: string): Promise<void> {
|
|
58
|
+
// Lives only while the overlay is open, so each opening sees the latest main conversation.
|
|
59
|
+
let side: AgentSession | undefined;
|
|
60
|
+
|
|
61
|
+
await ctx.ui.custom<void>(
|
|
62
|
+
(tui, theme, keybindings, done) => {
|
|
63
|
+
async function ask(question: string): Promise<void> {
|
|
64
|
+
const pending: NonNullable<BtwState["pending"]> = { question, text: "", tools: [] };
|
|
65
|
+
state.pending = pending;
|
|
66
|
+
state.busy = true;
|
|
67
|
+
tui.requestRender();
|
|
68
|
+
try {
|
|
69
|
+
if (side == null) {
|
|
70
|
+
side = await createSideSession(ctx, pi.getThinkingLevel(), state.thread);
|
|
71
|
+
side.subscribe((event) => {
|
|
72
|
+
track(event);
|
|
73
|
+
tui.requestRender();
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
await side.prompt(question);
|
|
77
|
+
const answer = side.messages.at(-1);
|
|
78
|
+
if (answer?.role !== "assistant") throw new Error("No answer");
|
|
79
|
+
if (answer.stopReason === "aborted") throw new Error("Stopped");
|
|
80
|
+
if (answer.stopReason === "error")
|
|
81
|
+
throw new Error(answer.errorMessage ?? "Request failed");
|
|
82
|
+
|
|
83
|
+
const exchange: Exchange = { question, answer };
|
|
84
|
+
state.thread.push(exchange);
|
|
85
|
+
pi.appendEntry(EXCHANGE_ENTRY, exchange);
|
|
86
|
+
state.pending = undefined;
|
|
87
|
+
} catch (error) {
|
|
88
|
+
pending.error = error instanceof Error ? error.message : String(error);
|
|
89
|
+
} finally {
|
|
90
|
+
state.busy = false;
|
|
91
|
+
tui.requestRender();
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (initial) void ask(initial);
|
|
96
|
+
return createOverlay({
|
|
97
|
+
tui,
|
|
98
|
+
theme,
|
|
99
|
+
keybindings,
|
|
100
|
+
title: ctx.model?.id ?? "no model",
|
|
101
|
+
state,
|
|
102
|
+
onAsk: (question) => void ask(question),
|
|
103
|
+
onEscape: () => {
|
|
104
|
+
if (state.busy) void side?.abort();
|
|
105
|
+
else done();
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
overlay: true,
|
|
111
|
+
overlayOptions: { width: "80%", minWidth: 60, maxHeight: "80%", anchor: "center" },
|
|
112
|
+
},
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
side?.dispose();
|
|
116
|
+
state.pending = undefined;
|
|
117
|
+
|
|
118
|
+
if (state.thread.length === 0) return;
|
|
119
|
+
if ((await ctx.ui.select("Close btw", [KEEP, INJECT])) !== INJECT) return;
|
|
120
|
+
if (ctx.model == null) throw new Error("No model selected");
|
|
121
|
+
ctx.ui.notify("Summarizing side thread…");
|
|
122
|
+
const summary = await summarize(ctx, ctx.model, state.thread);
|
|
123
|
+
pi.sendUserMessage(`Summary of a side conversation:\n\n${summary}`, { deliverAs: "followUp" });
|
|
124
|
+
reset();
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
pi.registerCommand("btw", {
|
|
128
|
+
description: "ask a side question without derailing the main conversation",
|
|
129
|
+
handler: async (args, ctx) => {
|
|
130
|
+
const question = args.trim();
|
|
131
|
+
if (!question && state.thread.length > 0) {
|
|
132
|
+
const choice = await ctx.ui.select("btw", [CONTINUE, FRESH]);
|
|
133
|
+
if (choice == null) return;
|
|
134
|
+
if (choice === FRESH) reset();
|
|
135
|
+
}
|
|
136
|
+
await open(ctx, question);
|
|
137
|
+
},
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
pi.on("session_start", (_event, ctx) => {
|
|
141
|
+
restore(ctx);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
pi.on("session_tree", (_event, ctx) => {
|
|
145
|
+
restore(ctx);
|
|
146
|
+
});
|
|
147
|
+
}
|
package/src/overlay.ts
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import type { AssistantMessage } from "@earendil-works/pi-ai";
|
|
2
|
+
import { getMarkdownTheme, type Theme } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import {
|
|
4
|
+
Input,
|
|
5
|
+
Markdown,
|
|
6
|
+
truncateToWidth,
|
|
7
|
+
visibleWidth,
|
|
8
|
+
wrapTextWithAnsi,
|
|
9
|
+
type Component,
|
|
10
|
+
type Focusable,
|
|
11
|
+
type KeybindingsManager,
|
|
12
|
+
type TUI,
|
|
13
|
+
} from "@earendil-works/pi-tui";
|
|
14
|
+
|
|
15
|
+
import { answerText, type BtwState, type ToolRow } from "./session";
|
|
16
|
+
|
|
17
|
+
const TOOL_ICONS = { running: "⚙", done: "✓", error: "✗" };
|
|
18
|
+
const TOOL_COLORS = { running: "dim", done: "success", error: "error" } as const;
|
|
19
|
+
|
|
20
|
+
interface OverlayOptions {
|
|
21
|
+
tui: TUI;
|
|
22
|
+
theme: Theme;
|
|
23
|
+
keybindings: KeybindingsManager;
|
|
24
|
+
title: string;
|
|
25
|
+
state: BtwState;
|
|
26
|
+
onAsk: (question: string) => void;
|
|
27
|
+
onEscape: () => void;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function createOverlay({
|
|
31
|
+
tui,
|
|
32
|
+
theme,
|
|
33
|
+
keybindings,
|
|
34
|
+
title,
|
|
35
|
+
state,
|
|
36
|
+
onAsk,
|
|
37
|
+
onEscape,
|
|
38
|
+
}: OverlayOptions): Component & Focusable {
|
|
39
|
+
const input = new Input();
|
|
40
|
+
let answers = new WeakMap<AssistantMessage, Markdown>();
|
|
41
|
+
// Lines scrolled up from the bottom; 0 follows new output.
|
|
42
|
+
let scroll = 0;
|
|
43
|
+
let page = 1;
|
|
44
|
+
|
|
45
|
+
input.onSubmit = (value) => {
|
|
46
|
+
const question = value.trim();
|
|
47
|
+
if (!question || state.busy) return;
|
|
48
|
+
input.setValue("");
|
|
49
|
+
scroll = 0;
|
|
50
|
+
onAsk(question);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const border = (text: string) => theme.fg("borderAccent", text);
|
|
54
|
+
|
|
55
|
+
function markdown(answer: AssistantMessage, width: number): string[] {
|
|
56
|
+
let rendered = answers.get(answer);
|
|
57
|
+
if (rendered == null) {
|
|
58
|
+
rendered = new Markdown(answerText(answer), 0, 0, getMarkdownTheme());
|
|
59
|
+
answers.set(answer, rendered);
|
|
60
|
+
}
|
|
61
|
+
return rendered.render(width);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function question(text: string, width: number): string[] {
|
|
65
|
+
return wrapTextWithAnsi(theme.fg("userMessageText", text), width - 2).map((line) =>
|
|
66
|
+
theme.bg("userMessageBg", ` ${line}${" ".repeat(width - 2 - visibleWidth(line))} `),
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function answer(lines: string[]): string[] {
|
|
71
|
+
return lines.map((line) => theme.fg("borderAccent", "▎ ") + line);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function tool({ name, detail, status }: ToolRow, width: number): string {
|
|
75
|
+
const line = `${theme.fg(TOOL_COLORS[status], TOOL_ICONS[status])} ${theme.fg("toolTitle", name)} ${theme.fg("dim", detail)}`;
|
|
76
|
+
return truncateToWidth(line, width, "…");
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function transcript(width: number): string[] {
|
|
80
|
+
const lines: string[] = [];
|
|
81
|
+
for (const exchange of state.thread) {
|
|
82
|
+
lines.push(
|
|
83
|
+
...question(exchange.question, width),
|
|
84
|
+
"",
|
|
85
|
+
...answer(markdown(exchange.answer, width - 2)),
|
|
86
|
+
"",
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const { pending } = state;
|
|
91
|
+
if (pending != null) {
|
|
92
|
+
lines.push(
|
|
93
|
+
...question(pending.question, width),
|
|
94
|
+
"",
|
|
95
|
+
...pending.tools.map((row) => tool(row, width)),
|
|
96
|
+
);
|
|
97
|
+
if (pending.text)
|
|
98
|
+
lines.push(...new Markdown(pending.text, 0, 0, getMarkdownTheme()).render(width));
|
|
99
|
+
if (pending.error != null)
|
|
100
|
+
lines.push(...wrapTextWithAnsi(theme.fg("error", pending.error), width));
|
|
101
|
+
if (state.busy) lines.push("", theme.fg("dim", "answering… esc to stop"));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (lines.length > 0) return lines;
|
|
105
|
+
return wrapTextWithAnsi(
|
|
106
|
+
theme.fg(
|
|
107
|
+
"dim",
|
|
108
|
+
"Ask a side question. The main conversation is included as context. When you close, you can keep this thread or inject a summary into the main chat.",
|
|
109
|
+
),
|
|
110
|
+
width,
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function row(content: string, width: number): string {
|
|
115
|
+
const fitted = truncateToWidth(content, width, "");
|
|
116
|
+
return `${border("│")} ${fitted}${" ".repeat(Math.max(0, width - visibleWidth(fitted)))} ${border("│")}`;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function edge(left: string, right: string, label: string, info: string, width: number): string {
|
|
120
|
+
const fill = Math.max(0, width - visibleWidth(label) - visibleWidth(info) - 2);
|
|
121
|
+
const line = border(`${left}─`) + label + border("─".repeat(fill)) + info + border(`─${right}`);
|
|
122
|
+
return truncateToWidth(line, width + 2, "");
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return {
|
|
126
|
+
get focused() {
|
|
127
|
+
return input.focused;
|
|
128
|
+
},
|
|
129
|
+
set focused(value: boolean) {
|
|
130
|
+
input.focused = value;
|
|
131
|
+
},
|
|
132
|
+
|
|
133
|
+
render(width) {
|
|
134
|
+
const inner = width - 2;
|
|
135
|
+
const content = inner - 2;
|
|
136
|
+
const height = Math.max(4, Math.floor(tui.terminal.rows * 0.8) - 4);
|
|
137
|
+
page = height;
|
|
138
|
+
|
|
139
|
+
const lines = transcript(content);
|
|
140
|
+
scroll = Math.min(scroll, Math.max(0, lines.length - height));
|
|
141
|
+
const end = lines.length - scroll;
|
|
142
|
+
const visible = lines.slice(Math.max(0, end - height), end);
|
|
143
|
+
while (visible.length < height) visible.push("");
|
|
144
|
+
const position = lines.length > height ? ` ${end}/${lines.length} ` : "";
|
|
145
|
+
|
|
146
|
+
return [
|
|
147
|
+
edge("┌", "┐", theme.fg("accent", ` btw · ${title} `), "", inner),
|
|
148
|
+
...visible.map((line) => row(line, content)),
|
|
149
|
+
border(`├${"─".repeat(inner)}┤`),
|
|
150
|
+
row(input.render(content)[0] ?? "", content),
|
|
151
|
+
edge(
|
|
152
|
+
"└",
|
|
153
|
+
"┘",
|
|
154
|
+
theme.fg("dim", " enter ask · esc close · ↑↓ scroll "),
|
|
155
|
+
theme.fg("dim", position),
|
|
156
|
+
inner,
|
|
157
|
+
),
|
|
158
|
+
];
|
|
159
|
+
},
|
|
160
|
+
|
|
161
|
+
handleInput(data) {
|
|
162
|
+
if (keybindings.matches(data, "tui.select.cancel")) {
|
|
163
|
+
onEscape();
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
if (keybindings.matches(data, "tui.select.up")) scroll += 1;
|
|
167
|
+
else if (keybindings.matches(data, "tui.select.down")) scroll = Math.max(0, scroll - 1);
|
|
168
|
+
else if (keybindings.matches(data, "tui.select.pageUp")) scroll += page;
|
|
169
|
+
else if (keybindings.matches(data, "tui.select.pageDown"))
|
|
170
|
+
scroll = Math.max(0, scroll - page);
|
|
171
|
+
else input.handleInput(data);
|
|
172
|
+
tui.requestRender();
|
|
173
|
+
},
|
|
174
|
+
|
|
175
|
+
invalidate() {
|
|
176
|
+
answers = new WeakMap();
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
}
|
package/src/session.ts
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type { AssistantMessage } from "@earendil-works/pi-ai";
|
|
2
|
+
import {
|
|
3
|
+
createAgentSession,
|
|
4
|
+
DefaultResourceLoader,
|
|
5
|
+
getAgentDir,
|
|
6
|
+
SessionManager,
|
|
7
|
+
type AgentSession,
|
|
8
|
+
type ExtensionAPI,
|
|
9
|
+
type ExtensionContext,
|
|
10
|
+
} from "@earendil-works/pi-coding-agent";
|
|
11
|
+
|
|
12
|
+
const SIDE_PROMPT =
|
|
13
|
+
"The user is asking a side question (a 'btw') about the conversation above. The main agent keeps its own thread; you only answer here. Answer directly and briefly. Your tools are read-only.";
|
|
14
|
+
|
|
15
|
+
const SUMMARY_PROMPT =
|
|
16
|
+
"Summarize this side conversation for the main agent. Keep decisions, findings, open questions, and next steps. Output only the summary.";
|
|
17
|
+
|
|
18
|
+
export interface Exchange {
|
|
19
|
+
question: string;
|
|
20
|
+
answer: AssistantMessage;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface ToolRow {
|
|
24
|
+
id: string;
|
|
25
|
+
name: string;
|
|
26
|
+
detail: string;
|
|
27
|
+
status: "running" | "done" | "error";
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface BtwState {
|
|
31
|
+
thread: Exchange[];
|
|
32
|
+
pending?: { question: string; text: string; tools: ToolRow[]; error?: string };
|
|
33
|
+
busy: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function answerText(message: AssistantMessage): string {
|
|
37
|
+
return message.content
|
|
38
|
+
.flatMap((part) => (part.type === "text" ? [part.text] : []))
|
|
39
|
+
.join("\n")
|
|
40
|
+
.trim();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export async function createSideSession(
|
|
44
|
+
ctx: ExtensionContext,
|
|
45
|
+
thinkingLevel: ReturnType<ExtensionAPI["getThinkingLevel"]>,
|
|
46
|
+
thread: Exchange[],
|
|
47
|
+
): Promise<AgentSession> {
|
|
48
|
+
const resourceLoader = new DefaultResourceLoader({
|
|
49
|
+
cwd: ctx.cwd,
|
|
50
|
+
agentDir: getAgentDir(),
|
|
51
|
+
noExtensions: true,
|
|
52
|
+
appendSystemPrompt: [SIDE_PROMPT],
|
|
53
|
+
});
|
|
54
|
+
await resourceLoader.reload();
|
|
55
|
+
|
|
56
|
+
// Seeding with the main branch keeps its system prompt in front, so the request shares the
|
|
57
|
+
// main chat's prefix and Pi only appends a patch for SIDE_PROMPT and the read-only tools.
|
|
58
|
+
const sessionManager = SessionManager.inMemory(
|
|
59
|
+
ctx.cwd,
|
|
60
|
+
undefined,
|
|
61
|
+
ctx.sessionManager.getBranch(),
|
|
62
|
+
);
|
|
63
|
+
for (const { question, answer } of thread) {
|
|
64
|
+
sessionManager.appendMessage({ role: "user", content: question, timestamp: answer.timestamp });
|
|
65
|
+
sessionManager.appendMessage(answer);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const { session } = await createAgentSession({
|
|
69
|
+
cwd: ctx.cwd,
|
|
70
|
+
model: ctx.model,
|
|
71
|
+
thinkingLevel,
|
|
72
|
+
tools: ["read", "grep", "find", "ls"],
|
|
73
|
+
resourceLoader,
|
|
74
|
+
sessionManager,
|
|
75
|
+
});
|
|
76
|
+
return session;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export async function summarize(
|
|
80
|
+
ctx: ExtensionContext,
|
|
81
|
+
model: NonNullable<ExtensionContext["model"]>,
|
|
82
|
+
thread: Exchange[],
|
|
83
|
+
): Promise<string> {
|
|
84
|
+
const transcript = thread
|
|
85
|
+
.map(({ question, answer }) => `User: ${question}\n\nAssistant: ${answerText(answer)}`)
|
|
86
|
+
.join("\n\n---\n\n");
|
|
87
|
+
const response = await ctx.modelRegistry.complete(model, {
|
|
88
|
+
systemPrompt: SUMMARY_PROMPT,
|
|
89
|
+
messages: [{ role: "user", content: transcript, timestamp: Date.now() }],
|
|
90
|
+
});
|
|
91
|
+
if (response.stopReason === "error" || response.stopReason === "aborted") {
|
|
92
|
+
throw new Error(response.errorMessage ?? "Summary failed");
|
|
93
|
+
}
|
|
94
|
+
return answerText(response);
|
|
95
|
+
}
|