@duckmind/dm-darwin-x64 0.33.1 → 0.33.4

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.
@@ -1,238 +0,0 @@
1
- /**
2
- * ask-ui — dynamic-height bottom-slot overlay for /ask.
3
- *
4
- * Layout (grows with content, bottom-anchored, max = terminal height):
5
- * banner (theme.bg stripe, padded to width) sticky top
6
- * blank
7
- * history — "/ask <q>" (accent prefix + muted text), left-padded 2 cols
8
- * echo — "/ask <q>" (accent prefix + muted text), left-padded 2 cols
9
- * blank
10
- * answer — body wrapped at width-2, left-padded 2 cols
11
- * blank
12
- * footer — key hints (dim) sticky bottom
13
- *
14
- * Natural height = fixed(5: banner, 3 blanks, footer) + 2 (echo + 1 blank before answer)
15
- * + history.length + answerLines.length.
16
- * DM-tui bottom-anchors the overlay so it grows upward with each /ask message.
17
- * If natural height > terminal rows, we clip from the top (older history scrolls off)
18
- * and ↑/↓ scroll the clip window.
19
- *
20
- * Keys (via matchesKey — handles ANSI + Kitty):
21
- * Esc → abort in-flight call + dismiss
22
- * ↑/↓ → scroll (when content exceeds terminal)
23
- * x → clear current-session /ask history
24
- * (f fork key deferred)
25
- */
26
-
27
- import type { ExtensionCommandContext, Theme } from "@mariozechner/pi-coding-agent";
28
- import type { OverlayOptions } from "@mariozechner/pi-tui";
29
- import {
30
- type Component,
31
- Key,
32
- matchesKey,
33
- type TUI,
34
- truncateToWidth,
35
- visibleWidth,
36
- wrapTextWithAnsi,
37
- } from "@mariozechner/pi-tui";
38
- import { type AskTurn, userMessageText } from "./ask.js";
39
-
40
- const ASK_OVERLAY_OPTIONS: OverlayOptions = {
41
- anchor: "bottom-center",
42
- width: "100%",
43
- maxHeight: "85%",
44
- margin: { left: 0, right: 0, bottom: 0 },
45
- };
46
-
47
- const ASK_MAX_HEIGHT_RATIO = 0.85;
48
-
49
- const SIDE_PAD = " "; // 2-col left gutter for history, echo, footer
50
- const ANSWER_PAD = " "; // 4-col left gutter for answer body (double of SIDE_PAD)
51
- const ASK_LITERAL = "/ask";
52
- const PENDING_GLYPH = "…";
53
- const FOOTER_SCROLL = "↑/↓ to scroll";
54
- const FOOTER_CLEAR = "x to clear history";
55
- const FOOTER_DISMISS = "Esc to dismiss";
56
- const FOOTER_SEP = " · ";
57
-
58
- type Mode = "pending" | "answer" | "error";
59
-
60
- export interface ShowAskOverlayParams {
61
- ctx: ExtensionCommandContext;
62
- question: string;
63
- history: AskTurn[];
64
- controller: AbortController;
65
- onClearHistory: () => void;
66
- }
67
-
68
- export interface ShowAskOverlayResult {
69
- overlayPromise: Promise<void>;
70
- controllerReady: Promise<AskOverlayController>;
71
- }
72
-
73
- export class AskOverlayController implements Component {
74
- private mode: Mode = "pending";
75
- private answer = "";
76
- private error = "";
77
- private scrollOffset = 0;
78
- private history: AskTurn[];
79
-
80
- constructor(
81
- private readonly question: string,
82
- history: AskTurn[],
83
- private readonly theme: Theme,
84
- private readonly tui: TUI,
85
- private readonly done: (result?: undefined) => void,
86
- private readonly controller: AbortController,
87
- private readonly onClearHistory: () => void,
88
- ) {
89
- this.history = [...history];
90
- }
91
-
92
- setAnswer(text: string): void {
93
- this.mode = "answer";
94
- this.answer = text;
95
- this.tui.requestRender();
96
- }
97
-
98
- setError(message: string): void {
99
- this.mode = "error";
100
- this.error = message;
101
- this.tui.requestRender();
102
- }
103
-
104
- handleInput(data: string): void {
105
- if (matchesKey(data, Key.escape)) {
106
- this.controller.abort();
107
- this.done();
108
- return;
109
- }
110
- if (matchesKey(data, Key.up)) {
111
- this.scrollOffset = Math.max(0, this.scrollOffset - 1);
112
- this.tui.requestRender();
113
- return;
114
- }
115
- if (matchesKey(data, Key.down)) {
116
- this.scrollOffset = this.scrollOffset + 1;
117
- this.tui.requestRender();
118
- return;
119
- }
120
- if (data === "x") {
121
- this.history = [];
122
- this.onClearHistory();
123
- this.scrollOffset = 0;
124
- this.tui.requestRender();
125
- return;
126
- }
127
- }
128
-
129
- render(width: number): string[] {
130
- const banner = this.renderBanner(width);
131
- const historyLines = this.history.map((h) => this.historyLine(userMessageText(h.userMessage), width));
132
- const echoLine = this.echoLine(this.question, width);
133
- const answerLines = this.renderAnswer(width);
134
- const footerAvail = Math.max(1, width - SIDE_PAD.length);
135
- const footerParts: string[] = [];
136
- if (this.mode !== "pending") footerParts.push(FOOTER_SCROLL);
137
- if (this.history.length > 0) footerParts.push(FOOTER_CLEAR);
138
- footerParts.push(FOOTER_DISMISS);
139
- const footer =
140
- SIDE_PAD + truncateToWidth(this.theme.fg("dim", footerParts.join(FOOTER_SEP)), footerAvail, "…", false);
141
-
142
- // Natural content: banner + blank + history + echo + blank + answer + blank + footer
143
- const natural: string[] = [banner, "", ...historyLines, echoLine, "", ...answerLines, "", footer];
144
-
145
- // Clip to terminal height if we overflow. Bottom-anchor keeps footer+answer visible;
146
- // ↑/↓ scrolls the top (history) up into the clipped region.
147
- const termRows = (this.tui.terminal as { rows?: number }).rows ?? 24;
148
- const maxRows = Math.max(4, Math.floor(termRows * ASK_MAX_HEIGHT_RATIO));
149
- if (natural.length <= maxRows) {
150
- return natural;
151
- }
152
- const excess = natural.length - maxRows;
153
- if (this.scrollOffset > excess) this.scrollOffset = excess;
154
- // scrollOffset=0 shows the BOTTOM (newest). Scrolling up reveals older history.
155
- const start = excess - this.scrollOffset;
156
- return natural.slice(start, start + maxRows);
157
- }
158
-
159
- invalidate(): void {
160
- // no-op — render recomputes from state each cycle
161
- }
162
-
163
- private renderBanner(width: number): string {
164
- const prefix = `${SIDE_PAD}${ASK_LITERAL} `;
165
- const prefixWidth = visibleWidth(prefix);
166
- const qAvail = Math.max(0, width - prefixWidth);
167
- const qTrunc = truncateToWidth(this.question, qAvail, "…", false);
168
- const raw = prefix + qTrunc;
169
- const padded = raw + " ".repeat(Math.max(0, width - visibleWidth(raw)));
170
- return this.theme.bg("customMessageBg", this.theme.fg("customMessageText", padded));
171
- }
172
-
173
- private historyLine(question: string, width: number): string {
174
- const qAvail = Math.max(0, width - SIDE_PAD.length);
175
- const qClean = question.replace(/\s+/g, " ").trim();
176
- const raw = `${ASK_LITERAL} ${qClean}`;
177
- const trunc = truncateToWidth(raw, qAvail, "…", false);
178
- return SIDE_PAD + this.theme.fg("muted", trunc);
179
- }
180
-
181
- private echoLine(question: string, width: number): string {
182
- const bodyAvail = Math.max(1, width - SIDE_PAD.length);
183
- const prefixWidth = visibleWidth(ASK_LITERAL) + 1; // "/ask "
184
- const qAvail = Math.max(0, bodyAvail - prefixWidth);
185
- const qClean = question.replace(/\s+/g, " ").trim();
186
- const qTrunc = truncateToWidth(qClean, qAvail, "…", false);
187
- return `${SIDE_PAD + this.theme.fg("accent", ASK_LITERAL)} ${this.theme.fg("muted", qTrunc)}`;
188
- }
189
-
190
- private renderAnswer(width: number): string[] {
191
- const bodyWidth = Math.max(1, width - ANSWER_PAD.length);
192
- const indent = (lines: string[]) => lines.map((l) => ANSWER_PAD + l);
193
-
194
- if (this.mode === "pending") {
195
- return indent([this.theme.fg("warning", PENDING_GLYPH)]);
196
- }
197
- if (this.mode === "error") {
198
- const out: string[] = [];
199
- for (const ln of this.error.split("\n")) {
200
- const src = ln.length === 0 ? " " : ln;
201
- out.push(...wrapTextWithAnsi(this.theme.fg("error", src), bodyWidth));
202
- }
203
- return indent(out);
204
- }
205
- const out: string[] = [];
206
- for (const ln of this.answer.split("\n")) {
207
- const src = ln.length === 0 ? " " : ln;
208
- out.push(...wrapTextWithAnsi(src, bodyWidth));
209
- }
210
- return indent(out);
211
- }
212
- }
213
-
214
- export function showAskOverlay(params: ShowAskOverlayParams): ShowAskOverlayResult {
215
- let resolveReady!: (controller: AskOverlayController) => void;
216
- const controllerReady = new Promise<AskOverlayController>((resolve) => {
217
- resolveReady = resolve;
218
- });
219
-
220
- const overlayPromise = params.ctx.ui.custom<void>(
221
- (tui, theme, _kb, done) => {
222
- const controller = new AskOverlayController(
223
- params.question,
224
- params.history,
225
- theme,
226
- tui,
227
- done,
228
- params.controller,
229
- params.onClearHistory,
230
- );
231
- resolveReady(controller);
232
- return controller;
233
- },
234
- { overlay: true, overlayOptions: ASK_OVERLAY_OPTIONS },
235
- );
236
-
237
- return { overlayPromise, controllerReady };
238
- }
@@ -1,348 +0,0 @@
1
- /**
2
- * dm-ask — /ask side-question slash command.
3
- *
4
- * Asks the same primary model a one-off side question using the cloned primary
5
- * conversation as context. Answer is rendered ephemerally in a bottom-slot
6
- * overlay (never enters main agent's messages). History persists per-session-file
7
- * via globalThis-keyed storage; process-scoped only (no disk persistence).
8
- */
9
-
10
- import { readFileSync } from "node:fs";
11
- import { fileURLToPath } from "node:url";
12
- import {
13
- type AssistantMessage,
14
- completeSimple,
15
- type Message,
16
- type StopReason,
17
- type UserMessage,
18
- } from "@mariozechner/pi-ai";
19
- import {
20
- convertToLlm,
21
- type ExtensionAPI,
22
- type ExtensionCommandContext,
23
- type ExtensionContext,
24
- type SessionEntry,
25
- } from "@mariozechner/pi-coding-agent";
26
- import { showAskOverlay } from "./ask-ui.js";
27
-
28
- // ---------------------------------------------------------------------------
29
- // Constants — flat named consts, grouped by concern (advisor pattern, b9428e9)
30
- // ---------------------------------------------------------------------------
31
-
32
- // Identity
33
- export const ASK_COMMAND_NAME = "ask";
34
-
35
- // Storage — globalThis-keyed survives module re-import on /new, /fork, /resume.
36
- // Lost on DM process exit (intentional — no disk persistence).
37
- export const ASK_STATE_KEY = Symbol.for("dm-ask");
38
-
39
- // Cross-session pattern hint: how many recent question-strings to inject
40
- export const CROSS_SESSION_HINT_LIMIT = 10;
41
-
42
- // Messages (static)
43
- const MSG_REQUIRES_INTERACTIVE = "/ask requires interactive mode";
44
- const MSG_USAGE = "Usage: /ask <question>";
45
- const MSG_NO_MODEL = "/ask requires an active model";
46
-
47
- // Errors (static)
48
- const ERR_EMPTY_RESPONSE = "/ask returned no text content.";
49
-
50
- // Errors (parameterized)
51
- const errMisconfigured = (label: string, err: string) => `/ask model (${label}) is misconfigured: ${err}`;
52
- const errNoApiKey = (label: string) => `/ask model (${label}) has no API key available.`;
53
- const errCallFailed = (err: string | undefined) => `/ask call failed: ${err ?? "unknown error"}`;
54
- const errCallThrew = (msg: string) => `/ask call threw: ${msg}`;
55
-
56
- // ---------------------------------------------------------------------------
57
- // Types
58
- // ---------------------------------------------------------------------------
59
-
60
- // Real messages — no fabrication. userMessage is built at call time; assistantMessage
61
- // is the unmodified completeSimple response. Stable object references across calls →
62
- // byte-identical prompt prefix on subsequent /ask invocations (cache parity).
63
- export interface AskTurn {
64
- userMessage: UserMessage;
65
- assistantMessage: AssistantMessage;
66
- }
67
-
68
- export interface AskState {
69
- histories: Map<string, AskTurn[]>;
70
- snapshots: Map<string, { messages: Message[] }>;
71
- }
72
-
73
- // ---------------------------------------------------------------------------
74
- // System prompt — loaded once at module init from prompts/ask-system.txt
75
- // ---------------------------------------------------------------------------
76
-
77
- export const ASK_SYSTEM_PROMPT = readFileSync(
78
- fileURLToPath(new URL("./prompts/ask-system.txt", import.meta.url)),
79
- "utf-8",
80
- ).trimEnd();
81
-
82
- // ---------------------------------------------------------------------------
83
- // Storage — globalThis-keyed, survives module re-import on /new, /fork, /resume.
84
- // Standard Node.js `globalThis + Symbol.for()` idiom for cross-import-graph
85
- // singleton state (used by OpenTelemetry, etc.); lost on process exit.
86
- // ---------------------------------------------------------------------------
87
-
88
- function getState(): AskState {
89
- const g = globalThis as unknown as { [k: symbol]: AskState | undefined };
90
- let state = g[ASK_STATE_KEY];
91
- if (!state) {
92
- state = {
93
- histories: new Map(),
94
- snapshots: new Map(),
95
- };
96
- g[ASK_STATE_KEY] = state;
97
- }
98
- return state;
99
- }
100
-
101
- function getSessionFile(ctx: ExtensionContext): string {
102
- return ctx.sessionManager.getSessionFile() ?? `memory:${ctx.sessionManager.getSessionId()}`;
103
- }
104
-
105
- function getSessionHistory(ctx: ExtensionContext): AskTurn[] {
106
- const key = getSessionFile(ctx);
107
- const state = getState();
108
- let turns = state.histories.get(key);
109
- if (!turns) {
110
- turns = [];
111
- state.histories.set(key, turns);
112
- }
113
- return turns;
114
- }
115
-
116
- function pushSessionTurn(ctx: ExtensionContext, turn: AskTurn): void {
117
- getSessionHistory(ctx).push(turn);
118
- }
119
-
120
- export function clearSessionHistory(ctx: ExtensionContext): void {
121
- getState().histories.set(getSessionFile(ctx), []);
122
- }
123
-
124
- function getSnapshot(ctx: ExtensionContext): { messages: Message[] } | undefined {
125
- return getState().snapshots.get(getSessionFile(ctx));
126
- }
127
-
128
- function setSnapshot(ctx: ExtensionContext, snapshot: { messages: Message[] }): void {
129
- getState().snapshots.set(getSessionFile(ctx), snapshot);
130
- }
131
-
132
- export function invalidateSnapshot(ctx: ExtensionContext): void {
133
- getState().snapshots.delete(getSessionFile(ctx));
134
- }
135
-
136
- // Extract text from a UserMessage's content.
137
- export function userMessageText(msg: UserMessage): string {
138
- if (typeof msg.content === "string") return msg.content;
139
- return msg.content
140
- .filter((c): c is { type: "text"; text: string } => c.type === "text")
141
- .map((c) => c.text)
142
- .join("\n");
143
- }
144
-
145
- // Extract text from an AssistantMessage's content (text parts only).
146
- export function assistantMessageText(msg: AssistantMessage): string {
147
- return msg.content
148
- .filter((c): c is { type: "text"; text: string } => c.type === "text")
149
- .map((c) => c.text)
150
- .join("\n");
151
- }
152
-
153
- // Cross-session pattern hint — last N question-strings across ALL sessions.
154
- function getCrossSessionHint(): string {
155
- const allTurns: { q: string; ts: number }[] = [];
156
- for (const turns of getState().histories.values()) {
157
- for (const t of turns) {
158
- allTurns.push({ q: userMessageText(t.userMessage), ts: t.userMessage.timestamp });
159
- }
160
- }
161
- if (allTurns.length === 0) return "";
162
- const recent = allTurns.sort((a, b) => a.ts - b.ts).slice(-CROSS_SESSION_HINT_LIMIT);
163
- const lines = recent.map((t, i) => `${i + 1}. ${t.q.replace(/\s+/g, " ").slice(0, 200)}`);
164
- return `\n\n## Recent /ask questions across sessions (oldest first)\n\n${lines.join("\n")}`;
165
- }
166
-
167
- // ---------------------------------------------------------------------------
168
- // Executor — auth, message threading, completeSimple, four StopReason branches
169
- // Modeled after rpiv-advisor/advisor.ts:225-336
170
- // ---------------------------------------------------------------------------
171
-
172
- export interface AskExecResult {
173
- ok: boolean;
174
- answer?: string;
175
- userMessage?: UserMessage;
176
- assistantMessage?: AssistantMessage;
177
- error?: string;
178
- stopReason?: StopReason;
179
- aborted?: boolean;
180
- }
181
-
182
- function readBranchMessages(ctx: ExtensionContext): Message[] {
183
- const cached = getSnapshot(ctx);
184
- if (cached) return cached.messages;
185
- // Cold start (no message_end fired yet) — fall back to live read
186
- const branch = ctx.sessionManager.getBranch() as SessionEntry[];
187
- const agentMessages = branch
188
- .filter((e): e is SessionEntry & { type: "message" } => e.type === "message")
189
- .map((e) => e.message);
190
- return convertToLlm(agentMessages);
191
- }
192
-
193
- function buildAskMessages(ctx: ExtensionContext, userMessage: UserMessage): Message[] {
194
- const branchMessages = readBranchMessages(ctx);
195
- const history = getSessionHistory(ctx);
196
- // Reusing stored real UserMessage/AssistantMessage object references across calls
197
- // preserves byte-identical prompt prefix (cache parity).
198
- const historyMessages: Message[] = history.flatMap((h) => [h.userMessage, h.assistantMessage]);
199
- return [...branchMessages, ...historyMessages, userMessage];
200
- }
201
-
202
- function buildSystemPrompt(): string {
203
- return ASK_SYSTEM_PROMPT + getCrossSessionHint();
204
- }
205
-
206
- export async function executeAsk(
207
- question: string,
208
- ctx: ExtensionContext,
209
- controller: AbortController,
210
- ): Promise<AskExecResult> {
211
- const model = ctx.model;
212
- if (!model) {
213
- return { ok: false, error: MSG_NO_MODEL };
214
- }
215
- const modelLabel = `${model.provider}:${model.id}`;
216
-
217
- const auth = await ctx.modelRegistry.getApiKeyAndHeaders(model);
218
- if (!auth.ok) {
219
- return { ok: false, error: errMisconfigured(modelLabel, auth.error) };
220
- }
221
- if (!auth.apiKey) {
222
- return { ok: false, error: errNoApiKey(modelLabel) };
223
- }
224
-
225
- const userMessage: UserMessage = {
226
- role: "user",
227
- content: [{ type: "text", text: question }],
228
- timestamp: Date.now(),
229
- };
230
- const messages = buildAskMessages(ctx, userMessage);
231
- const systemPrompt = buildSystemPrompt();
232
-
233
- try {
234
- const response = await completeSimple(
235
- model,
236
- { systemPrompt, messages, tools: [] },
237
- {
238
- apiKey: auth.apiKey,
239
- headers: auth.headers,
240
- signal: controller.signal, // own AbortController, NOT ctx.signal (Decision 8)
241
- },
242
- );
243
-
244
- if (response.stopReason === "aborted") {
245
- return { ok: false, aborted: true, stopReason: response.stopReason };
246
- }
247
- if (response.stopReason === "error") {
248
- return {
249
- ok: false,
250
- error: errCallFailed(response.errorMessage),
251
- stopReason: response.stopReason,
252
- };
253
- }
254
-
255
- const answerText = assistantMessageText(response).trim();
256
- if (!answerText) {
257
- return { ok: false, error: ERR_EMPTY_RESPONSE, stopReason: response.stopReason };
258
- }
259
-
260
- return {
261
- ok: true,
262
- answer: answerText,
263
- userMessage,
264
- assistantMessage: response,
265
- stopReason: response.stopReason,
266
- };
267
- } catch (err) {
268
- const message = err instanceof Error ? err.message : String(err);
269
- if (controller.signal.aborted) {
270
- return { ok: false, aborted: true };
271
- }
272
- return { ok: false, error: errCallThrew(message) };
273
- }
274
- }
275
-
276
- // ---------------------------------------------------------------------------
277
- // Registrars — 3 hooks total: command + message_end snapshot + compact/tree invalidate
278
- // ---------------------------------------------------------------------------
279
-
280
- export function registerMessageEndSnapshot(pi: ExtensionAPI): void {
281
- pi.on("message_end", async (event, ctx) => {
282
- const msg = event.message;
283
- if (msg.role !== "assistant") return;
284
- if ((msg as AssistantMessage).stopReason === "toolUse") return;
285
- const branch = ctx.sessionManager.getBranch() as SessionEntry[];
286
- const agentMessages = branch
287
- .filter((e): e is SessionEntry & { type: "message" } => e.type === "message")
288
- .map((e) => e.message);
289
- setSnapshot(ctx, { messages: convertToLlm(agentMessages) });
290
- });
291
- }
292
-
293
- export function registerInvalidationHooks(pi: ExtensionAPI): void {
294
- pi.on("session_compact", async (_e, ctx) => invalidateSnapshot(ctx));
295
- pi.on("session_tree", async (_e, ctx) => invalidateSnapshot(ctx));
296
- }
297
-
298
- export function registerAskCommand(pi: ExtensionAPI): void {
299
- pi.registerCommand(ASK_COMMAND_NAME, {
300
- description: "Ask a side question without polluting the main conversation",
301
- handler: (args: string, ctx: ExtensionCommandContext) => handleAskCommand(pi, args, ctx),
302
- });
303
- }
304
-
305
- async function handleAskCommand(_pi: ExtensionAPI, args: string, ctx: ExtensionCommandContext): Promise<void> {
306
- if (!ctx.hasUI) {
307
- ctx.ui.notify(MSG_REQUIRES_INTERACTIVE, "error");
308
- return;
309
- }
310
- const question = args.trim();
311
- if (!question) {
312
- ctx.ui.notify(MSG_USAGE, "warning");
313
- return;
314
- }
315
- if (!ctx.model) {
316
- ctx.ui.notify(MSG_NO_MODEL, "error");
317
- return;
318
- }
319
-
320
- const controller = new AbortController();
321
- const historySnapshot = [...getSessionHistory(ctx)];
322
-
323
- const { overlayPromise, controllerReady } = showAskOverlay({
324
- ctx,
325
- question,
326
- history: historySnapshot,
327
- controller,
328
- onClearHistory: () => clearSessionHistory(ctx),
329
- });
330
-
331
- const overlayCtl = await controllerReady;
332
- const result = await executeAsk(question, ctx, controller);
333
-
334
- if (result.ok && result.answer && result.userMessage && result.assistantMessage) {
335
- overlayCtl.setAnswer(result.answer);
336
- pushSessionTurn(ctx, {
337
- userMessage: result.userMessage,
338
- assistantMessage: result.assistantMessage,
339
- });
340
- // No disk persistence — process-scoped only (Decision 4)
341
- } else if (result.aborted) {
342
- // User Esc'd — overlay already dismissed via done(); no further action
343
- } else if (result.error) {
344
- overlayCtl.setError(result.error);
345
- }
346
-
347
- await overlayPromise;
348
- }