pi-studio 0.5.44 → 0.5.45

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/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import type { ExtensionAPI, ExtensionCommandContext, SessionEntry, Theme } from "@mariozechner/pi-coding-agent";
2
+ import { getAgentDir } from "@mariozechner/pi-coding-agent";
2
3
  import { spawn, spawnSync } from "node:child_process";
3
4
  import { randomUUID } from "node:crypto";
4
5
  import { readFileSync, statSync, writeFileSync } from "node:fs";
@@ -107,6 +108,25 @@ interface InitialStudioDocument {
107
108
  label: string;
108
109
  source: StudioSourceKind;
109
110
  path?: string;
111
+ draftId?: string;
112
+ }
113
+
114
+ interface PersistedStudioReviewNote {
115
+ id: string;
116
+ text: string;
117
+ createdAt: number;
118
+ updatedAt: number;
119
+ selectionStart: number;
120
+ selectionEnd: number;
121
+ lineStart: number;
122
+ lineEnd: number;
123
+ selectedText: string;
124
+ }
125
+
126
+ interface StudioPersistentState {
127
+ version: 2;
128
+ scratchpadsByDocument: Record<string, string>;
129
+ reviewNotesByDocument: Record<string, PersistedStudioReviewNote[]>;
110
130
  }
111
131
 
112
132
  interface HelloMessage {
@@ -217,6 +237,172 @@ const CMUX_STUDIO_STATUS_KEY = "pi_studio";
217
237
  const CMUX_STUDIO_STATUS_COLOR_DARK = "#5ea1ff";
218
238
  const CMUX_STUDIO_STATUS_COLOR_LIGHT = "#0047ab";
219
239
  const STUDIO_PROMPT_METADATA_CUSTOM_TYPE = "pi-studio/direct-prompt";
240
+ const STUDIO_DEFAULT_SCRATCHPAD_DOCUMENT_KEY = "doc:blank:blank";
241
+ const STUDIO_PERSISTENT_STATE_DIR = join(getAgentDir(), "pi-studio");
242
+ const STUDIO_PERSISTENT_STATE_PATH = join(STUDIO_PERSISTENT_STATE_DIR, "local-state.json");
243
+
244
+ let studioPersistentStateCache: StudioPersistentState | null = null;
245
+ let studioPersistentStateQueue: Promise<void> = Promise.resolve();
246
+
247
+ function createEmptyStudioPersistentState(): StudioPersistentState {
248
+ return {
249
+ version: 2,
250
+ scratchpadsByDocument: {},
251
+ reviewNotesByDocument: {},
252
+ };
253
+ }
254
+
255
+ function normalizePersistedStudioReviewNote(value: unknown): PersistedStudioReviewNote | null {
256
+ if (!value || typeof value !== "object") return null;
257
+ const candidate = value as Partial<PersistedStudioReviewNote>;
258
+ if (typeof candidate.id !== "string" || !candidate.id.trim()) return null;
259
+ if (typeof candidate.text !== "string") return null;
260
+ const createdAt = typeof candidate.createdAt === "number" && Number.isFinite(candidate.createdAt)
261
+ ? candidate.createdAt
262
+ : Date.now();
263
+ const updatedAt = typeof candidate.updatedAt === "number" && Number.isFinite(candidate.updatedAt)
264
+ ? candidate.updatedAt
265
+ : createdAt;
266
+ const selectionStart = typeof candidate.selectionStart === "number" && Number.isFinite(candidate.selectionStart)
267
+ ? Math.max(0, Math.floor(candidate.selectionStart))
268
+ : 0;
269
+ const selectionEnd = typeof candidate.selectionEnd === "number" && Number.isFinite(candidate.selectionEnd)
270
+ ? Math.max(selectionStart, Math.floor(candidate.selectionEnd))
271
+ : selectionStart;
272
+ const lineStart = typeof candidate.lineStart === "number" && Number.isFinite(candidate.lineStart)
273
+ ? Math.max(1, Math.floor(candidate.lineStart))
274
+ : 1;
275
+ const lineEnd = typeof candidate.lineEnd === "number" && Number.isFinite(candidate.lineEnd)
276
+ ? Math.max(lineStart, Math.floor(candidate.lineEnd))
277
+ : lineStart;
278
+ return {
279
+ id: candidate.id,
280
+ text: candidate.text,
281
+ createdAt,
282
+ updatedAt,
283
+ selectionStart,
284
+ selectionEnd,
285
+ lineStart,
286
+ lineEnd,
287
+ selectedText: typeof candidate.selectedText === "string" ? candidate.selectedText : "",
288
+ };
289
+ }
290
+
291
+ function normalizeStudioPersistentState(value: unknown): StudioPersistentState {
292
+ const fallback = createEmptyStudioPersistentState();
293
+ if (!value || typeof value !== "object") return fallback;
294
+ const candidate = value as Partial<StudioPersistentState> & {
295
+ reviewNotesByDocument?: unknown;
296
+ scratchpadsByDocument?: unknown;
297
+ scratchpadText?: unknown;
298
+ };
299
+ const reviewNotesByDocument: Record<string, PersistedStudioReviewNote[]> = {};
300
+ if (candidate.reviewNotesByDocument && typeof candidate.reviewNotesByDocument === "object") {
301
+ for (const [documentKey, rawNotes] of Object.entries(candidate.reviewNotesByDocument as Record<string, unknown>)) {
302
+ if (typeof documentKey !== "string" || !documentKey.trim() || !Array.isArray(rawNotes)) continue;
303
+ const normalizedNotes = rawNotes
304
+ .map((note) => normalizePersistedStudioReviewNote(note))
305
+ .filter((note): note is PersistedStudioReviewNote => Boolean(note));
306
+ if (normalizedNotes.length > 0) {
307
+ reviewNotesByDocument[documentKey] = normalizedNotes;
308
+ }
309
+ }
310
+ }
311
+ const scratchpadsByDocument: Record<string, string> = {};
312
+ if (candidate.scratchpadsByDocument && typeof candidate.scratchpadsByDocument === "object") {
313
+ for (const [documentKey, rawText] of Object.entries(candidate.scratchpadsByDocument as Record<string, unknown>)) {
314
+ if (typeof documentKey !== "string" || !documentKey.trim() || typeof rawText !== "string") continue;
315
+ scratchpadsByDocument[documentKey] = rawText;
316
+ }
317
+ } else if (typeof candidate.scratchpadText === "string" && candidate.scratchpadText.length > 0) {
318
+ scratchpadsByDocument[STUDIO_DEFAULT_SCRATCHPAD_DOCUMENT_KEY] = candidate.scratchpadText;
319
+ }
320
+ return {
321
+ version: 2,
322
+ scratchpadsByDocument,
323
+ reviewNotesByDocument,
324
+ };
325
+ }
326
+
327
+ async function loadStudioPersistentState(): Promise<StudioPersistentState> {
328
+ if (studioPersistentStateCache) return studioPersistentStateCache;
329
+ try {
330
+ const raw = await readFile(STUDIO_PERSISTENT_STATE_PATH, "utf-8");
331
+ studioPersistentStateCache = normalizeStudioPersistentState(JSON.parse(raw));
332
+ } catch (error) {
333
+ if (!(error && typeof error === "object" && "code" in error && (error as { code?: unknown }).code === "ENOENT")) {
334
+ // Ignore parse/read errors and fall back to a fresh local state blob.
335
+ }
336
+ studioPersistentStateCache = createEmptyStudioPersistentState();
337
+ }
338
+ return studioPersistentStateCache;
339
+ }
340
+
341
+ async function saveStudioPersistentState(state: StudioPersistentState): Promise<void> {
342
+ await mkdir(STUDIO_PERSISTENT_STATE_DIR, { recursive: true });
343
+ await writeFile(STUDIO_PERSISTENT_STATE_PATH, `${JSON.stringify(state, null, 2)}\n`, "utf-8");
344
+ studioPersistentStateCache = state;
345
+ }
346
+
347
+ async function mutateStudioPersistentState(mutator: (state: StudioPersistentState) => void): Promise<void> {
348
+ const run = studioPersistentStateQueue.catch(() => undefined).then(async () => {
349
+ const state = normalizeStudioPersistentState(await loadStudioPersistentState());
350
+ mutator(state);
351
+ await saveStudioPersistentState(state);
352
+ });
353
+ studioPersistentStateQueue = run.then(() => undefined, () => undefined);
354
+ await run;
355
+ }
356
+
357
+ async function readPersistedStudioScratchpadText(documentKey: string): Promise<string> {
358
+ const key = String(documentKey ?? "").trim();
359
+ if (!key) return "";
360
+ const state = await loadStudioPersistentState();
361
+ const value = state.scratchpadsByDocument[key];
362
+ return typeof value === "string" ? value : "";
363
+ }
364
+
365
+ async function writePersistedStudioScratchpadText(documentKey: string, text: string): Promise<void> {
366
+ const key = String(documentKey ?? "").trim();
367
+ if (!key) return;
368
+ await mutateStudioPersistentState((state) => {
369
+ const normalized = String(text ?? "");
370
+ if (normalized.length === 0) {
371
+ delete state.scratchpadsByDocument[key];
372
+ return;
373
+ }
374
+ state.scratchpadsByDocument[key] = normalized;
375
+ });
376
+ }
377
+
378
+ function clonePersistedStudioReviewNotes(notes: PersistedStudioReviewNote[]): PersistedStudioReviewNote[] {
379
+ return notes.map((note) => ({ ...note }));
380
+ }
381
+
382
+ async function readPersistedStudioReviewNotes(documentKey: string): Promise<PersistedStudioReviewNote[]> {
383
+ const key = String(documentKey ?? "").trim();
384
+ if (!key) return [];
385
+ const state = await loadStudioPersistentState();
386
+ const notes = state.reviewNotesByDocument[key];
387
+ return Array.isArray(notes) ? clonePersistedStudioReviewNotes(notes) : [];
388
+ }
389
+
390
+ async function writePersistedStudioReviewNotes(documentKey: string, notes: PersistedStudioReviewNote[]): Promise<void> {
391
+ const key = String(documentKey ?? "").trim();
392
+ if (!key) return;
393
+ const normalizedNotes = Array.isArray(notes)
394
+ ? notes
395
+ .map((note) => normalizePersistedStudioReviewNote(note))
396
+ .filter((note): note is PersistedStudioReviewNote => Boolean(note))
397
+ : [];
398
+ await mutateStudioPersistentState((state) => {
399
+ if (normalizedNotes.length === 0) {
400
+ delete state.reviewNotesByDocument[key];
401
+ return;
402
+ }
403
+ state.reviewNotesByDocument[key] = clonePersistedStudioReviewNotes(normalizedNotes);
404
+ });
405
+ }
220
406
 
221
407
  function scaleStudioPdfLength(length: string, factor: number): string | null {
222
408
  const match = String(length ?? "").trim().match(/^(\d+(?:\.\d+)?)(pt|bp|mm|cm|in|pc)$/i);
@@ -951,6 +1137,10 @@ function createSessionToken(): string {
951
1137
  return randomUUID();
952
1138
  }
953
1139
 
1140
+ function createStudioDraftId(): string {
1141
+ return `draft_${randomUUID().replace(/[^a-zA-Z0-9_-]/g, "_")}`;
1142
+ }
1143
+
954
1144
  function rawDataToString(data: RawData): string {
955
1145
  if (typeof data === "string") return data;
956
1146
  if (data instanceof Buffer) return data.toString("utf-8");
@@ -5608,12 +5798,65 @@ function normalizeStudioUiMode(raw: string | null | undefined): StudioUiMode {
5608
5798
  return raw === "editor-only" ? "editor-only" : "full";
5609
5799
  }
5610
5800
 
5611
- function buildStudioUrl(port: number, token: string, mode: StudioUiMode = "full"): string {
5801
+ function buildStudioUrl(
5802
+ port: number,
5803
+ token: string,
5804
+ mode: StudioUiMode = "full",
5805
+ doc?: InitialStudioDocument | null,
5806
+ ): string {
5612
5807
  const params = new URLSearchParams({ token });
5613
5808
  if (mode !== "full") params.set("mode", mode);
5809
+ if (doc?.source) params.set("docSource", doc.source);
5810
+ if (doc?.label) params.set("docLabel", doc.label);
5811
+ if (doc?.path) params.set("docPath", doc.path);
5812
+ if (doc?.draftId) params.set("draftId", doc.draftId);
5614
5813
  return `http://127.0.0.1:${port}/?${params.toString()}`;
5615
5814
  }
5616
5815
 
5816
+ function resolveRequestedStudioDocumentFromUrl(
5817
+ requestUrl: URL,
5818
+ fallback: InitialStudioDocument | null,
5819
+ studioCwd: string,
5820
+ latestResponse?: LastStudioResponse | null,
5821
+ ): InitialStudioDocument | null {
5822
+ const requestedPath = (requestUrl.searchParams.get("docPath") ?? "").trim();
5823
+ const requestedSourceRaw = (requestUrl.searchParams.get("docSource") ?? "").trim();
5824
+ const requestedLabel = (requestUrl.searchParams.get("docLabel") ?? "").trim();
5825
+ const requestedDraftId = (requestUrl.searchParams.get("draftId") ?? "").trim();
5826
+
5827
+ if (requestedPath) {
5828
+ const file = readStudioFile(requestedPath, studioCwd);
5829
+ if (file.ok !== false) {
5830
+ return {
5831
+ text: file.text,
5832
+ label: requestedLabel || file.label,
5833
+ source: "file",
5834
+ path: file.resolvedPath,
5835
+ };
5836
+ }
5837
+ }
5838
+
5839
+ if (requestedSourceRaw === "last-response") {
5840
+ return {
5841
+ text: latestResponse?.markdown ?? (fallback?.source === "last-response" ? fallback.text : ""),
5842
+ label: requestedLabel || "last model response",
5843
+ source: "last-response",
5844
+ draftId: requestedDraftId || undefined,
5845
+ };
5846
+ }
5847
+
5848
+ if (requestedSourceRaw || requestedLabel || requestedDraftId) {
5849
+ return {
5850
+ text: fallback?.source === "blank" ? fallback.text : "",
5851
+ label: requestedLabel || requestedSourceRaw || "blank",
5852
+ source: "blank",
5853
+ draftId: requestedDraftId || undefined,
5854
+ };
5855
+ }
5856
+
5857
+ return fallback;
5858
+ }
5859
+
5617
5860
  function formatModelLabel(model: { provider?: string; id?: string } | undefined): string {
5618
5861
  const provider = typeof model?.provider === "string" ? model.provider.trim() : "";
5619
5862
  const id = typeof model?.id === "string" ? model.id.trim() : "";
@@ -5751,6 +5994,7 @@ function buildStudioHtml(
5751
5994
  const initialSource = initialDocument?.source ?? "blank";
5752
5995
  const initialLabel = escapeHtmlForInline(initialDocument?.label ?? "blank");
5753
5996
  const initialPath = escapeHtmlForInline(initialDocument?.path ?? "");
5997
+ const initialDraftId = escapeHtmlForInline(initialDocument?.draftId ?? "");
5754
5998
  const initialModel = escapeHtmlForInline(initialModelLabel ?? "none");
5755
5999
  const initialTerminal = escapeHtmlForInline(initialTerminalLabel ?? "unknown");
5756
6000
  const initialContextTokens =
@@ -5819,7 +6063,7 @@ ${cssVarsBlock}
5819
6063
  </style>
5820
6064
  <link rel="stylesheet" href="${stylesheetHref}" />
5821
6065
  </head>
5822
- <body data-initial-source="${initialSource}" data-initial-label="${initialLabel}" data-initial-path="${initialPath}" data-model-label="${initialModel}" data-terminal-label="${initialTerminal}" data-context-tokens="${initialContextTokens}" data-context-window="${initialContextWindow}" data-context-percent="${initialContextPercent}" data-studio-mode="${studioMode}">
6066
+ <body data-initial-source="${initialSource}" data-initial-label="${initialLabel}" data-initial-path="${initialPath}" data-initial-draft-id="${initialDraftId}" data-model-label="${initialModel}" data-terminal-label="${initialTerminal}" data-context-tokens="${initialContextTokens}" data-context-window="${initialContextWindow}" data-context-percent="${initialContextPercent}" data-studio-mode="${studioMode}">
5823
6067
  <header>
5824
6068
  <h1><span class="app-logo" aria-hidden="true">π</span> Studio <span class="app-subtitle">${appSubtitle}</span></h1>
5825
6069
  <div class="controls">
@@ -5843,13 +6087,14 @@ ${cssVarsBlock}
5843
6087
  </div>
5844
6088
  <div class="section-header-actions">
5845
6089
  <button id="leftFocusBtn" class="pane-focus-btn" type="button" title="Show only the editor pane. Shortcut: F10 or Cmd/Ctrl+Esc.">Focus pane</button>
5846
- <button id="scratchpadBtn" type="button" title="Open a local persistent scratchpad for quick notes. Scratchpad text is never run, critiqued, or exported unless you explicitly insert it into the editor.">Scratchpad</button>
6090
+ <button id="reviewNotesBtn" type="button" title="Toggle local comments beside the current editor document or draft. Comments stay outside the document text and can later be converted into [an: ...] annotations.">Comments</button>
6091
+ <button id="scratchpadBtn" type="button" title="Open a local persistent scratchpad for the current editor document or draft. Scratchpad text is never run, critiqued, or exported unless you explicitly insert it into the editor.">Scratchpad</button>
5847
6092
  </div>
5848
6093
  </div>
5849
6094
  <div class="source-wrap">
5850
6095
  <div class="source-meta">
5851
6096
  <div class="badge-row">
5852
- <span id="sourceBadge" class="source-badge">Editor origin: ${initialLabel}</span>
6097
+ <button id="sourceBadge" type="button" class="source-badge source-badge-button">Editor origin: ${initialLabel}</button>
5853
6098
  <button id="resourceDirBtn" type="button" class="resource-dir-btn" hidden title="Set working directory for resolving relative paths in preview">Set working dir</button>
5854
6099
  <span id="resourceDirLabel" class="source-badge resource-dir-label" hidden title="Click to change working directory"></span>
5855
6100
  <span id="resourceDirInputWrap" class="resource-dir-input-wrap">
@@ -5867,9 +6112,9 @@ ${cssVarsBlock}
5867
6112
  </div>
5868
6113
  <div class="source-actions-row">
5869
6114
  <button id="insertHeaderBtn" type="button" title="Insert annotated-reply protocol header (source metadata, [an: ...] syntax hint, precedence note, and end marker).">Annotation header</button>
5870
- <select id="annotationModeSelect" aria-label="Annotation visibility mode" title="On: keep and send [an: ...] markers. Hidden: keep markers in editor, hide in preview, and strip before Run/Critique.">
5871
- <option value="on" selected>Annotations: On</option>
5872
- <option value="off">Annotations: Hidden</option>
6115
+ <select id="annotationModeSelect" aria-label="Inline annotation visibility mode" title="On: keep and send [an: ...] markers. Hide: keep markers in the editor, hide them in preview, and strip before Run/Critique.">
6116
+ <option value="on" selected>Inline annotations: On</option>
6117
+ <option value="off">Inline annotations: Hide</option>
5873
6118
  </select>
5874
6119
  <button id="stripAnnotationsBtn" type="button" title="Destructively remove all [an: ...] markers from editor text.">Strip annotations…</button>
5875
6120
  <button id="saveAnnotatedBtn" type="button" title="Save full editor content (including [an: ...] markers) as a .annotated.md file.">Save .annotated.md</button>
@@ -5910,21 +6155,51 @@ ${cssVarsBlock}
5910
6155
  <option value="yaml">Syntax highlight: YAML</option>
5911
6156
  </select>
5912
6157
  <select id="lineNumbersSelect" aria-label="Editor line numbers">
5913
- <option value="off" selected>Line numbers: Off</option>
5914
- <option value="on">Line numbers: On</option>
6158
+ <option value="off">Line numbers: Off</option>
6159
+ <option value="on" selected>Line numbers: On</option>
5915
6160
  </select>
5916
6161
  </div>
5917
6162
  </div>
5918
6163
  </div>
5919
- <div id="sourceEditorWrap" class="editor-highlight-wrap">
5920
- <div id="lineNumberGutter" class="editor-line-number-gutter" hidden aria-hidden="true">
5921
- <div id="lineNumberGutterContent" class="editor-line-number-gutter-content"></div>
6164
+ <div class="source-body">
6165
+ <div class="source-primary">
6166
+ <div id="sourceEditorWrap" class="editor-highlight-wrap">
6167
+ <div id="reviewNoteGutter" class="editor-review-note-gutter" hidden aria-hidden="true">
6168
+ <div id="reviewNoteGutterContent" class="editor-review-note-gutter-content"></div>
6169
+ </div>
6170
+ <div id="lineNumberGutter" class="editor-line-number-gutter" hidden aria-hidden="true">
6171
+ <div id="lineNumberGutterContent" class="editor-line-number-gutter-content"></div>
6172
+ </div>
6173
+ <div id="lineNumberMeasure" class="editor-line-number-measure" aria-hidden="true"></div>
6174
+ <pre id="sourceHighlight" class="editor-highlight" aria-hidden="true"></pre>
6175
+ <textarea id="sourceText" placeholder="Paste or edit text here.">${initialText}</textarea>
6176
+ </div>
6177
+ <div id="sourcePreview" class="panel-scroll rendered-markdown" hidden><pre class="plain-markdown"></pre></div>
5922
6178
  </div>
5923
- <div id="lineNumberMeasure" class="editor-line-number-measure" aria-hidden="true"></div>
5924
- <pre id="sourceHighlight" class="editor-highlight" aria-hidden="true"></pre>
5925
- <textarea id="sourceText" placeholder="Paste or edit text here.">${initialText}</textarea>
6179
+ <aside id="reviewNotesOverlay" class="review-notes-dock-wrap" hidden>
6180
+ <div id="reviewNotesDialog" class="review-notes-dock" role="complementary" aria-labelledby="reviewNotesTitle">
6181
+ <div class="scratchpad-header">
6182
+ <div>
6183
+ <h2 id="reviewNotesTitle">Comments</h2>
6184
+ <p class="scratchpad-description">Local comments for editor text. Stay out of the text, anchored to selections or lines, and can be converted into inline <span class="review-notes-inline-token">[an: ...]</span> annotations.</p>
6185
+ </div>
6186
+ <button id="reviewNotesCloseBtn" type="button" class="scratchpad-close-btn" aria-label="Hide comments" title="Hide comments">✕</button>
6187
+ </div>
6188
+ <div class="review-notes-toolbar">
6189
+ <span id="reviewNotesMeta" class="scratchpad-meta">No comments</span>
6190
+ </div>
6191
+ <div id="reviewNotesEmptyState" class="review-notes-empty">No comments yet for this document. Select text (or just place the caret on a line) in <strong>Editor (Raw)</strong>, then choose <em>New comment from selection</em>.</div>
6192
+ <div id="reviewNotesList" class="review-notes-list" aria-live="polite"></div>
6193
+ <div class="review-notes-dock-footer">
6194
+ <div class="scratchpad-actions">
6195
+ <button id="reviewNotesAddBtn" type="button" title="Create a new local comment from the current editor selection, or from the current line if nothing is selected.">New comment from selection</button>
6196
+ <button id="reviewNotesInlineAllBtn" type="button" title="Toggle inline annotations for all non-empty comments.">All inline: Off</button>
6197
+ <button id="reviewNotesDoneBtn" type="button" title="Hide the comments rail.">Hide</button>
6198
+ </div>
6199
+ </div>
6200
+ </div>
6201
+ </aside>
5926
6202
  </div>
5927
- <div id="sourcePreview" class="panel-scroll rendered-markdown" hidden><pre class="plain-markdown"></pre></div>
5928
6203
  </div>
5929
6204
  </section>
5930
6205
 
@@ -5989,7 +6264,7 @@ ${cssVarsBlock}
5989
6264
  <div class="scratchpad-header">
5990
6265
  <div>
5991
6266
  <h2 id="scratchpadTitle">Scratchpad</h2>
5992
- <p class="scratchpad-description">Local persistent notes for thoughts you want to park while working. Closing the scratchpad does not clear it: notes persist locally until you edit or clear them. Scratchpad text is not run, critiqued, sent, or exported unless you explicitly insert it into the editor.</p>
6267
+ <p class="scratchpad-description">Local persistent notes for thoughts you want to park while working on the current Studio document or draft. Closing the scratchpad does not clear it: notes persist locally for this document identity until you edit or clear them. File-backed documents reliably come back across Pi restarts; unsaved drafts stay with their own draft instance until you save them or discard them. Scratchpad text is not run, critiqued, sent, or exported unless you explicitly insert it into the editor.</p>
5993
6268
  </div>
5994
6269
  <button id="scratchpadCloseBtn" type="button" class="scratchpad-close-btn" aria-label="Keep current scratchpad text and close scratchpad" title="Keep current scratchpad text and close scratchpad">✕</button>
5995
6270
  </div>
@@ -7431,6 +7706,120 @@ export default function (pi: ExtensionAPI) {
7431
7706
  res.end(prepared.pdf);
7432
7707
  };
7433
7708
 
7709
+ const handleScratchpadStateRequest = async (req: IncomingMessage, res: ServerResponse, requestUrl: URL) => {
7710
+ const method = (req.method ?? "GET").toUpperCase();
7711
+ if (method === "GET") {
7712
+ const documentKey = (requestUrl.searchParams.get("documentKey") ?? "").trim();
7713
+ if (!documentKey) {
7714
+ respondJson(res, 400, { ok: false, error: "Missing documentKey query parameter." });
7715
+ return;
7716
+ }
7717
+ respondJson(res, 200, { ok: true, text: await readPersistedStudioScratchpadText(documentKey) });
7718
+ return;
7719
+ }
7720
+ if (method !== "POST") {
7721
+ res.setHeader("Allow", "GET, POST");
7722
+ respondJson(res, 405, { ok: false, error: "Method not allowed. Use GET or POST." });
7723
+ return;
7724
+ }
7725
+
7726
+ let rawBody = "";
7727
+ try {
7728
+ rawBody = await readRequestBody(req, REQUEST_BODY_MAX_BYTES);
7729
+ } catch (error) {
7730
+ const message = error instanceof Error ? error.message : String(error);
7731
+ const status = message.includes("exceeds") ? 413 : 400;
7732
+ respondJson(res, status, { ok: false, error: message });
7733
+ return;
7734
+ }
7735
+
7736
+ let parsedBody: unknown;
7737
+ try {
7738
+ parsedBody = rawBody ? JSON.parse(rawBody) : {};
7739
+ } catch {
7740
+ respondJson(res, 400, { ok: false, error: "Invalid JSON body." });
7741
+ return;
7742
+ }
7743
+
7744
+ const documentKey =
7745
+ parsedBody && typeof parsedBody === "object" && typeof (parsedBody as { documentKey?: unknown }).documentKey === "string"
7746
+ ? (parsedBody as { documentKey: string }).documentKey.trim()
7747
+ : "";
7748
+ if (!documentKey) {
7749
+ respondJson(res, 400, { ok: false, error: "Missing documentKey in request body." });
7750
+ return;
7751
+ }
7752
+
7753
+ const text =
7754
+ parsedBody && typeof parsedBody === "object" && typeof (parsedBody as { text?: unknown }).text === "string"
7755
+ ? (parsedBody as { text: string }).text
7756
+ : null;
7757
+ if (text === null) {
7758
+ respondJson(res, 400, { ok: false, error: "Missing scratchpad text in request body." });
7759
+ return;
7760
+ }
7761
+
7762
+ await writePersistedStudioScratchpadText(documentKey, text);
7763
+ respondJson(res, 200, { ok: true });
7764
+ };
7765
+
7766
+ const handleReviewNotesRequest = async (req: IncomingMessage, res: ServerResponse, requestUrl: URL) => {
7767
+ const method = (req.method ?? "GET").toUpperCase();
7768
+ if (method === "GET") {
7769
+ const documentKey = (requestUrl.searchParams.get("documentKey") ?? "").trim();
7770
+ if (!documentKey) {
7771
+ respondJson(res, 400, { ok: false, error: "Missing documentKey query parameter." });
7772
+ return;
7773
+ }
7774
+ respondJson(res, 200, { ok: true, notes: await readPersistedStudioReviewNotes(documentKey) });
7775
+ return;
7776
+ }
7777
+ if (method !== "POST") {
7778
+ res.setHeader("Allow", "GET, POST");
7779
+ respondJson(res, 405, { ok: false, error: "Method not allowed. Use GET or POST." });
7780
+ return;
7781
+ }
7782
+
7783
+ let rawBody = "";
7784
+ try {
7785
+ rawBody = await readRequestBody(req, REQUEST_BODY_MAX_BYTES);
7786
+ } catch (error) {
7787
+ const message = error instanceof Error ? error.message : String(error);
7788
+ const status = message.includes("exceeds") ? 413 : 400;
7789
+ respondJson(res, status, { ok: false, error: message });
7790
+ return;
7791
+ }
7792
+
7793
+ let parsedBody: unknown;
7794
+ try {
7795
+ parsedBody = rawBody ? JSON.parse(rawBody) : {};
7796
+ } catch {
7797
+ respondJson(res, 400, { ok: false, error: "Invalid JSON body." });
7798
+ return;
7799
+ }
7800
+
7801
+ const documentKey =
7802
+ parsedBody && typeof parsedBody === "object" && typeof (parsedBody as { documentKey?: unknown }).documentKey === "string"
7803
+ ? (parsedBody as { documentKey: string }).documentKey.trim()
7804
+ : "";
7805
+ if (!documentKey) {
7806
+ respondJson(res, 400, { ok: false, error: "Missing documentKey in request body." });
7807
+ return;
7808
+ }
7809
+
7810
+ const notes =
7811
+ parsedBody && typeof parsedBody === "object" && Array.isArray((parsedBody as { notes?: unknown }).notes)
7812
+ ? (parsedBody as { notes: PersistedStudioReviewNote[] }).notes
7813
+ : null;
7814
+ if (!notes) {
7815
+ respondJson(res, 400, { ok: false, error: "Missing notes array in request body." });
7816
+ return;
7817
+ }
7818
+
7819
+ await writePersistedStudioReviewNotes(documentKey, notes);
7820
+ respondJson(res, 200, { ok: true });
7821
+ };
7822
+
7434
7823
  const handleRenderPreviewRequest = async (req: IncomingMessage, res: ServerResponse) => {
7435
7824
  let rawBody = "";
7436
7825
  try {
@@ -7673,6 +8062,36 @@ export default function (pi: ExtensionAPI) {
7673
8062
  return;
7674
8063
  }
7675
8064
 
8065
+ if (requestUrl.pathname === "/scratchpad-state") {
8066
+ const token = requestUrl.searchParams.get("token") ?? "";
8067
+ if (token !== serverState.token) {
8068
+ respondJson(res, 403, { ok: false, error: "Invalid or expired studio token. Re-run /studio." });
8069
+ return;
8070
+ }
8071
+ void handleScratchpadStateRequest(req, res, requestUrl).catch((error) => {
8072
+ respondJson(res, 500, {
8073
+ ok: false,
8074
+ error: `Scratchpad persistence failed: ${error instanceof Error ? error.message : String(error)}`,
8075
+ });
8076
+ });
8077
+ return;
8078
+ }
8079
+
8080
+ if (requestUrl.pathname === "/review-notes") {
8081
+ const token = requestUrl.searchParams.get("token") ?? "";
8082
+ if (token !== serverState.token) {
8083
+ respondJson(res, 403, { ok: false, error: "Invalid or expired studio token. Re-run /studio." });
8084
+ return;
8085
+ }
8086
+ void handleReviewNotesRequest(req, res, requestUrl).catch((error) => {
8087
+ respondJson(res, 500, {
8088
+ ok: false,
8089
+ error: `Review-note persistence failed: ${error instanceof Error ? error.message : String(error)}`,
8090
+ });
8091
+ });
8092
+ return;
8093
+ }
8094
+
7676
8095
  if (requestUrl.pathname === "/render-preview") {
7677
8096
  const token = requestUrl.searchParams.get("token") ?? "";
7678
8097
  if (token !== serverState.token) {
@@ -7749,7 +8168,8 @@ export default function (pi: ExtensionAPI) {
7749
8168
  });
7750
8169
  refreshContextUsage();
7751
8170
  const studioMode = normalizeStudioUiMode(requestUrl.searchParams.get("mode"));
7752
- res.end(buildStudioHtml(initialStudioDocument, serverState.token, lastCommandCtx?.ui.theme, currentModelLabel, terminalSessionLabel, contextUsageSnapshot, studioMode));
8171
+ const requestInitialDocument = resolveRequestedStudioDocumentFromUrl(requestUrl, initialStudioDocument, studioCwd, lastStudioResponse);
8172
+ res.end(buildStudioHtml(requestInitialDocument, serverState.token, lastCommandCtx?.ui.theme, currentModelLabel, terminalSessionLabel, contextUsageSnapshot, studioMode));
7753
8173
  };
7754
8174
 
7755
8175
  const ensureServer = async (): Promise<StudioServerState> => {
@@ -8229,12 +8649,14 @@ export default function (pi: ExtensionAPI) {
8229
8649
  text: latestAssistant,
8230
8650
  label: "last model response",
8231
8651
  source: "last-response",
8652
+ draftId: createStudioDraftId(),
8232
8653
  };
8233
8654
  }
8234
8655
  return {
8235
8656
  text: "",
8236
8657
  label: "blank",
8237
8658
  source: "blank",
8659
+ draftId: createStudioDraftId(),
8238
8660
  };
8239
8661
  }
8240
8662
 
@@ -8243,6 +8665,7 @@ export default function (pi: ExtensionAPI) {
8243
8665
  text: "",
8244
8666
  label: "blank",
8245
8667
  source: "blank",
8668
+ draftId: createStudioDraftId(),
8246
8669
  };
8247
8670
  }
8248
8671
 
@@ -8253,12 +8676,14 @@ export default function (pi: ExtensionAPI) {
8253
8676
  text: "",
8254
8677
  label: "blank",
8255
8678
  source: "blank",
8679
+ draftId: createStudioDraftId(),
8256
8680
  };
8257
8681
  }
8258
8682
  return {
8259
8683
  text: latestAssistant,
8260
8684
  label: "last model response",
8261
8685
  source: "last-response",
8686
+ draftId: createStudioDraftId(),
8262
8687
  };
8263
8688
  }
8264
8689
 
@@ -8331,7 +8756,7 @@ export default function (pi: ExtensionAPI) {
8331
8756
  initialStudioDocument = selected;
8332
8757
 
8333
8758
  const state = await ensureServer();
8334
- const url = buildStudioUrl(state.port, state.token, mode);
8759
+ const url = buildStudioUrl(state.port, state.token, mode, selected);
8335
8760
  const openedLabel = mode === "editor-only" ? "pi Studio editor-only view" : "pi Studio";
8336
8761
 
8337
8762
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-studio",
3
- "version": "0.5.44",
3
+ "version": "0.5.45",
4
4
  "description": "Two-pane browser workspace for pi with prompt/response editing, annotations, critiques, prompt/response history, and live Markdown/LaTeX/code preview",
5
5
  "type": "module",
6
6
  "license": "MIT",