@lune_99/pi-ask-question 0.1.0 → 0.2.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/CHANGELOG.md +38 -2
- package/README.md +31 -22
- package/package.json +6 -6
- package/src/commands/question.ts +17 -7
- package/src/core/question-delivery.ts +81 -29
- package/src/core/question-format.ts +36 -11
- package/src/core/questionManager.ts +302 -78
- package/src/index.ts +64 -35
- package/src/tools/ask-user-questions.ts +142 -56
- package/src/tools/withdraw-user-questions.ts +169 -0
- package/src/tui/dock-line.ts +42 -0
- package/src/tui/panel/clipboard-paste.ts +124 -0
- package/src/tui/panel/question-panel-controller.ts +13 -12
- package/src/tui/panel/question-panel-renderer.ts +180 -126
- package/src/tui/panel/question-panel.ts +259 -97
- package/src/tui/preview/preview-box.ts +22 -15
- package/src/tui/preview/preview-layout.ts +7 -4
- package/src/tui/preview/preview-renderer.ts +6 -1
- package/src/tui/question-answer-message.ts +6 -4
- package/src/tui/question-dock-contribution.ts +63 -23
- package/src/tui/question-dock.ts +58 -21
- package/docs/question-answer-row.png +0 -0
- package/docs/question-answer-row.svg +0 -16
- package/docs/question-dock.png +0 -0
- package/docs/question-dock.svg +0 -8
- package/docs/question-panel.svg +0 -58
- package/src/core/question-notification.ts +0 -34
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
2
|
|
|
3
|
-
import type {
|
|
4
|
-
ExtensionAPI,
|
|
5
|
-
ExtensionContext,
|
|
6
|
-
} from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
7
4
|
|
|
8
5
|
import type {
|
|
6
|
+
AnswerMessageQuestion,
|
|
9
7
|
AskQuestion,
|
|
10
8
|
AskQuestionAnswer,
|
|
11
9
|
AskQuestionDraft,
|
|
@@ -39,12 +37,33 @@ export type QuestionManagerEvent =
|
|
|
39
37
|
| { type: "request-shown"; id: string }
|
|
40
38
|
| { type: "request-answered"; id: string }
|
|
41
39
|
| { type: "request-skipped"; id: string }
|
|
40
|
+
| { type: "request-withdrawn"; id: string; reason: string }
|
|
41
|
+
| { type: "request-wait-started"; id: string }
|
|
42
|
+
| { type: "request-wait-ended"; id: string }
|
|
42
43
|
| { type: "request-delivered"; id: string }
|
|
43
44
|
| { type: "requests-restored" }
|
|
44
45
|
| { type: "requests-cleared" };
|
|
45
46
|
|
|
47
|
+
/** How a blocking tool call's wait ended; an interrupted request stays pending. */
|
|
48
|
+
export type QuestionWaitOutcome =
|
|
49
|
+
| { status: "settled"; request: Readonly<AskQuestionRequest> }
|
|
50
|
+
| { status: "interrupted" };
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Result of a withdrawal. Only a pending request can be withdrawn: a settled one already has an
|
|
54
|
+
* answer on its way to the model, and a waited-on one belongs to the paused tool call.
|
|
55
|
+
*/
|
|
56
|
+
export type QuestionWithdrawOutcome =
|
|
57
|
+
| { status: "withdrawn"; request: Readonly<AskQuestionRequest> }
|
|
58
|
+
| { status: "settled"; request: Readonly<AskQuestionRequest> }
|
|
59
|
+
| { status: "waiting" }
|
|
60
|
+
| { status: "unknown" };
|
|
61
|
+
|
|
46
62
|
export type QuestionManagerListener = (event: QuestionManagerEvent) => void;
|
|
47
|
-
export type QuestionManagerListenerErrorHandler = (
|
|
63
|
+
export type QuestionManagerListenerErrorHandler = (
|
|
64
|
+
error: unknown,
|
|
65
|
+
event: QuestionManagerEvent,
|
|
66
|
+
) => void;
|
|
48
67
|
|
|
49
68
|
export class QuestionManager {
|
|
50
69
|
private readonly pending = new Map<string, AskQuestionRequest>();
|
|
@@ -56,12 +75,34 @@ export class QuestionManager {
|
|
|
56
75
|
* out of the pending queue - until the branch really carries its message.
|
|
57
76
|
*/
|
|
58
77
|
private readonly outbox = new Map<string, AskQuestionRequest>();
|
|
59
|
-
|
|
78
|
+
/**
|
|
79
|
+
* Blocking tool calls by the request they wait on. They live in memory only: after a restart
|
|
80
|
+
* pi closes the orphaned call, and the restored request is delivered as a message.
|
|
81
|
+
*/
|
|
82
|
+
private readonly waiters = new Map<string, (outcome: QuestionWaitOutcome) => void>();
|
|
83
|
+
private readonly listeners = new Map<
|
|
84
|
+
QuestionManagerListener,
|
|
85
|
+
QuestionManagerListenerErrorHandler | undefined
|
|
86
|
+
>();
|
|
60
87
|
private shownSeqCounter = 0;
|
|
88
|
+
/**
|
|
89
|
+
* JSON of the state the active branch already records, or `undefined` when unknown.
|
|
90
|
+
*
|
|
91
|
+
* Every `appendEntry` adds a session line and a tree node, and quit and `/tree` persist
|
|
92
|
+
* whether or not anything changed, so a snapshot equal to this one is not written again.
|
|
93
|
+
*/
|
|
94
|
+
private persistedSnapshot: string | undefined;
|
|
61
95
|
|
|
62
|
-
|
|
96
|
+
/**
|
|
97
|
+
* `reservedIds` are ids the new request must not take, such as those of the answer messages on
|
|
98
|
+
* the branch: reconciliation matches a message by id and would release the new request unsent.
|
|
99
|
+
*/
|
|
100
|
+
create(
|
|
101
|
+
questions: readonly AskQuestion[],
|
|
102
|
+
reservedIds: ReadonlySet<string> = new Set(),
|
|
103
|
+
): AskQuestionRequest {
|
|
63
104
|
const request: AskQuestionRequest = {
|
|
64
|
-
id:
|
|
105
|
+
id: this.newRequestId(reservedIds),
|
|
65
106
|
createdAt: Date.now(),
|
|
66
107
|
questions: questions.map(copyQuestion),
|
|
67
108
|
status: "pending",
|
|
@@ -80,23 +121,25 @@ export class QuestionManager {
|
|
|
80
121
|
|
|
81
122
|
/** Pending requests oldest first; this is the order they were created in. */
|
|
82
123
|
getPendingRequests(): readonly AskQuestionRequest[] {
|
|
83
|
-
return Array.from(this.pending.values())
|
|
84
|
-
|
|
124
|
+
return Array.from(this.pending.values()).sort(
|
|
125
|
+
(left, right) => left.createdAt - right.createdAt,
|
|
126
|
+
);
|
|
85
127
|
}
|
|
86
128
|
|
|
87
129
|
/**
|
|
88
130
|
* The pending request the panel should show next.
|
|
89
131
|
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
132
|
+
* A request a blocking tool call waits on comes first, because the agent cannot continue
|
|
133
|
+
* without it. After that, least recently shown first, with a request that was never shown
|
|
134
|
+
* ahead of all of them: a question that just arrived has to surface immediately, while one the
|
|
135
|
+
* user dismissed with Esc must not pop back up in front of it. Requests that were never shown
|
|
136
|
+
* fall back to the order they were created in.
|
|
94
137
|
*/
|
|
95
138
|
nextPendingRequest(): AskQuestionRequest | undefined {
|
|
96
139
|
let next: AskQuestionRequest | undefined;
|
|
97
140
|
|
|
98
141
|
for (const request of this.getPendingRequests()) {
|
|
99
|
-
if (next === undefined ||
|
|
142
|
+
if (next === undefined || this.showsBefore(request, next)) {
|
|
100
143
|
next = request;
|
|
101
144
|
}
|
|
102
145
|
}
|
|
@@ -104,6 +147,48 @@ export class QuestionManager {
|
|
|
104
147
|
return next;
|
|
105
148
|
}
|
|
106
149
|
|
|
150
|
+
/** Whether a blocking tool call waits on the pending request `id`. */
|
|
151
|
+
isWaiting(id: string): boolean {
|
|
152
|
+
return this.waiters.has(id);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Hold the answer of pending request `id` for a blocking tool call.
|
|
157
|
+
*
|
|
158
|
+
* The promise resolves with the settled request, which then skips the outbox: the tool result
|
|
159
|
+
* carries the answer. When `signal` aborts first, or the state is cleared, it resolves as
|
|
160
|
+
* interrupted and the request stays pending, so a later answer is delivered as a message.
|
|
161
|
+
*/
|
|
162
|
+
waitForSettle(id: string, signal: AbortSignal | undefined): Promise<QuestionWaitOutcome> {
|
|
163
|
+
if (!this.pending.has(id) || this.waiters.has(id)) {
|
|
164
|
+
throw new Error(`Question request ${id} is not pending or already has a waiter`);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (signal?.aborted) {
|
|
168
|
+
return Promise.resolve({ status: "interrupted" });
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const onAbort = (): void => {
|
|
172
|
+
if (this.releaseWaiter(id, { status: "interrupted" })) {
|
|
173
|
+
this.emit({ type: "request-wait-ended", id });
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
const outcome = new Promise<QuestionWaitOutcome>((resolve) => {
|
|
178
|
+
this.waiters.set(id, (result) => {
|
|
179
|
+
signal?.removeEventListener("abort", onAbort);
|
|
180
|
+
resolve(result);
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
// pi waits for the run to become idle before an abort completes, and it has no tool
|
|
185
|
+
// timeout, so a wait that ignored the signal would hang Esc.
|
|
186
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
187
|
+
this.emit({ type: "request-wait-started", id });
|
|
188
|
+
|
|
189
|
+
return outcome;
|
|
190
|
+
}
|
|
191
|
+
|
|
107
192
|
/**
|
|
108
193
|
* Record that the panel is showing `id`, which moves it behind the requests it passed.
|
|
109
194
|
*
|
|
@@ -138,15 +223,7 @@ export class QuestionManager {
|
|
|
138
223
|
|
|
139
224
|
request.status = "answered";
|
|
140
225
|
request.answers = answers.map(copyAnswer);
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
// Settling is one move: out of the pending queue, into the durable outbox. The
|
|
144
|
-
// request must not be asked again, but its payload has to survive until pi's
|
|
145
|
-
// session really carries the answer message. The draft stays with the panel: keeping
|
|
146
|
-
// it would duplicate a written answer in the outbox and the snapshot.
|
|
147
|
-
request.draft = emptyDraft(request.questions.length);
|
|
148
|
-
this.pending.delete(id);
|
|
149
|
-
this.outbox.set(id, request);
|
|
226
|
+
this.settle(request);
|
|
150
227
|
|
|
151
228
|
this.emit({ type: "request-answered", id });
|
|
152
229
|
|
|
@@ -160,24 +237,48 @@ export class QuestionManager {
|
|
|
160
237
|
return false;
|
|
161
238
|
}
|
|
162
239
|
|
|
163
|
-
request.status = "skipped";
|
|
164
|
-
request.settledAt = Date.now();
|
|
165
|
-
|
|
166
240
|
// A skip is delivered as a custom message too, so it takes the same durable path
|
|
167
241
|
// as an answer: dropping it from the outbox would lose the model's only notice.
|
|
168
|
-
request.
|
|
169
|
-
this.
|
|
170
|
-
this.outbox.set(id, request);
|
|
242
|
+
request.status = "skipped";
|
|
243
|
+
this.settle(request);
|
|
171
244
|
|
|
172
245
|
this.emit({ type: "request-skipped", id });
|
|
173
246
|
|
|
174
247
|
return true;
|
|
175
248
|
}
|
|
176
249
|
|
|
250
|
+
/**
|
|
251
|
+
* Remove pending request `id` without an answer; no message is sent, the withdrawing tool
|
|
252
|
+
* result is the record. Like submit and skip, only the first of them counts.
|
|
253
|
+
*/
|
|
254
|
+
withdraw(id: string, reason: string): QuestionWithdrawOutcome {
|
|
255
|
+
const settled = this.outbox.get(id);
|
|
256
|
+
|
|
257
|
+
if (settled) {
|
|
258
|
+
return { status: "settled", request: settled };
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
const request = this.pending.get(id);
|
|
262
|
+
|
|
263
|
+
if (!request) {
|
|
264
|
+
return { status: "unknown" };
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (this.waiters.has(id)) {
|
|
268
|
+
return { status: "waiting" };
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
this.pending.delete(id);
|
|
272
|
+
this.emit({ type: "request-withdrawn", id, reason });
|
|
273
|
+
|
|
274
|
+
return { status: "withdrawn", request };
|
|
275
|
+
}
|
|
276
|
+
|
|
177
277
|
/** Settled requests whose answer message is not confirmed in the session yet. */
|
|
178
278
|
getUndeliveredRequests(): readonly Readonly<AskQuestionRequest>[] {
|
|
179
|
-
return Array.from(this.outbox.values())
|
|
180
|
-
|
|
279
|
+
return Array.from(this.outbox.values()).sort(
|
|
280
|
+
(left, right) => left.createdAt - right.createdAt,
|
|
281
|
+
);
|
|
181
282
|
}
|
|
182
283
|
|
|
183
284
|
/**
|
|
@@ -198,6 +299,14 @@ export class QuestionManager {
|
|
|
198
299
|
|
|
199
300
|
/** Drop every request, pending or not; a session starts from its own branch. */
|
|
200
301
|
clearAll(): void {
|
|
302
|
+
this.persistedSnapshot = undefined;
|
|
303
|
+
|
|
304
|
+
// A waiting tool call must not outlive the state it waits on; the branch being restored
|
|
305
|
+
// brings the request back as pending, to be answered by message.
|
|
306
|
+
for (const id of [...this.waiters.keys()]) {
|
|
307
|
+
this.releaseWaiter(id, { status: "interrupted" });
|
|
308
|
+
}
|
|
309
|
+
|
|
201
310
|
if (this.pending.size === 0 && this.outbox.size === 0) {
|
|
202
311
|
return;
|
|
203
312
|
}
|
|
@@ -207,9 +316,21 @@ export class QuestionManager {
|
|
|
207
316
|
this.emit({ type: "requests-cleared" });
|
|
208
317
|
}
|
|
209
318
|
|
|
210
|
-
/**
|
|
319
|
+
/**
|
|
320
|
+
* Persist the pending requests (with drafts) and the delivery outbox into the session of `pi`,
|
|
321
|
+
* unless the active branch already records this exact state.
|
|
322
|
+
*/
|
|
211
323
|
persist(pi: ExtensionAPI): void {
|
|
212
|
-
|
|
324
|
+
const snapshot = this.snapshot();
|
|
325
|
+
const serialized = JSON.stringify(snapshot);
|
|
326
|
+
|
|
327
|
+
if (serialized === this.persistedSnapshot) {
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
pi.appendEntry(QUESTION_STATE_ENTRY, snapshot);
|
|
332
|
+
// Only a write that succeeded counts; a failed one is attempted again next time.
|
|
333
|
+
this.persistedSnapshot = serialized;
|
|
213
334
|
}
|
|
214
335
|
|
|
215
336
|
snapshot(): AskQuestionStateSnapshot {
|
|
@@ -221,24 +342,17 @@ export class QuestionManager {
|
|
|
221
342
|
|
|
222
343
|
/** Restore the newest snapshot on the active branch; the newest state always wins. */
|
|
223
344
|
restore(ctx: ExtensionContext): void {
|
|
224
|
-
const
|
|
345
|
+
const wasEmpty = this.pending.size === 0 && this.outbox.size === 0;
|
|
346
|
+
const snapshot = newestSnapshot(ctx);
|
|
347
|
+
const restored = snapshot !== undefined && this.restoreSnapshot(snapshot);
|
|
225
348
|
|
|
226
|
-
|
|
227
|
-
|
|
349
|
+
// The branch record is known only when no live request was merged into it. It is set
|
|
350
|
+
// before the event, so the persistence listener does not write the branch's own state
|
|
351
|
+
// back onto it.
|
|
352
|
+
this.persistedSnapshot = wasEmpty ? JSON.stringify(this.snapshot()) : undefined;
|
|
228
353
|
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
const snapshot = entry.data as Partial<AskQuestionStateSnapshot> | undefined;
|
|
234
|
-
|
|
235
|
-
// The newest state entry wins, but only when it matches the schema this build
|
|
236
|
-
// writes; a malformed snapshot is not guessed at and not patched over.
|
|
237
|
-
if (snapshot && Array.isArray(snapshot.requests) && Array.isArray(snapshot.outbox)) {
|
|
238
|
-
this.restoreSnapshot(snapshot as AskQuestionStateSnapshot);
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
return;
|
|
354
|
+
if (restored) {
|
|
355
|
+
this.emit({ type: "requests-restored" });
|
|
242
356
|
}
|
|
243
357
|
}
|
|
244
358
|
|
|
@@ -247,7 +361,10 @@ export class QuestionManager {
|
|
|
247
361
|
* the listener that writes the snapshot is also the one whose failure would silently
|
|
248
362
|
* lose it, and the others must keep running either way.
|
|
249
363
|
*/
|
|
250
|
-
subscribe(
|
|
364
|
+
subscribe(
|
|
365
|
+
listener: QuestionManagerListener,
|
|
366
|
+
onError?: QuestionManagerListenerErrorHandler,
|
|
367
|
+
): () => void {
|
|
251
368
|
this.listeners.set(listener, onError);
|
|
252
369
|
|
|
253
370
|
return () => {
|
|
@@ -255,7 +372,68 @@ export class QuestionManager {
|
|
|
255
372
|
};
|
|
256
373
|
}
|
|
257
374
|
|
|
258
|
-
|
|
375
|
+
/**
|
|
376
|
+
* Settling is one move: out of the pending queue, into the durable outbox. The request must
|
|
377
|
+
* not be asked again, but its payload has to survive until pi's session really carries the
|
|
378
|
+
* answer message.
|
|
379
|
+
*
|
|
380
|
+
* Only the delivery payload goes along. The draft stays with the panel - keeping it would
|
|
381
|
+
* duplicate a written answer - and `displayText` and the previews are panel input that every
|
|
382
|
+
* later snapshot would otherwise copy until the delivery is confirmed.
|
|
383
|
+
*/
|
|
384
|
+
private settle(request: AskQuestionRequest): void {
|
|
385
|
+
request.settledAt = Date.now();
|
|
386
|
+
request.questions = request.questions.map(toAnswerMessageQuestion);
|
|
387
|
+
request.draft = emptyDraft(request.questions.length);
|
|
388
|
+
this.pending.delete(request.id);
|
|
389
|
+
|
|
390
|
+
// A waiting tool call returns the answer as its result; an outbox entry would deliver it a
|
|
391
|
+
// second time as a message.
|
|
392
|
+
if (!this.releaseWaiter(request.id, { status: "settled", request })) {
|
|
393
|
+
this.outbox.set(request.id, request);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/** Resolve and drop the waiter of `id`; returns whether there was one. */
|
|
398
|
+
private releaseWaiter(id: string, outcome: QuestionWaitOutcome): boolean {
|
|
399
|
+
const resolve = this.waiters.get(id);
|
|
400
|
+
|
|
401
|
+
if (!resolve) {
|
|
402
|
+
return false;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
this.waiters.delete(id);
|
|
406
|
+
resolve(outcome);
|
|
407
|
+
|
|
408
|
+
return true;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* Eight hex digits are short enough for the model to copy into `withdraw_user_questions`.
|
|
413
|
+
* Requests restored from older sessions keep their UUIDs.
|
|
414
|
+
*/
|
|
415
|
+
private newRequestId(reservedIds: ReadonlySet<string>): string {
|
|
416
|
+
for (;;) {
|
|
417
|
+
const id = randomBytes(4).toString("hex");
|
|
418
|
+
|
|
419
|
+
if (!this.hasRequest(id) && !reservedIds.has(id)) {
|
|
420
|
+
return id;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
private showsBefore(left: AskQuestionRequest, right: AskQuestionRequest): boolean {
|
|
426
|
+
const leftWaits = this.waiters.has(left.id);
|
|
427
|
+
|
|
428
|
+
if (leftWaits !== this.waiters.has(right.id)) {
|
|
429
|
+
return leftWaits;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
return shownOrder(left) < shownOrder(right);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/** Returns whether anything was restored; the caller emits the event. */
|
|
436
|
+
private restoreSnapshot(snapshot: AskQuestionStateSnapshot): boolean {
|
|
259
437
|
let restored = false;
|
|
260
438
|
|
|
261
439
|
for (const saved of snapshot.requests) {
|
|
@@ -287,9 +465,7 @@ export class QuestionManager {
|
|
|
287
465
|
restored = true;
|
|
288
466
|
}
|
|
289
467
|
|
|
290
|
-
|
|
291
|
-
this.emit({ type: "requests-restored" });
|
|
292
|
-
}
|
|
468
|
+
return restored;
|
|
293
469
|
}
|
|
294
470
|
|
|
295
471
|
/** Whether either queue already holds `id`; a snapshot never replaces live state. */
|
|
@@ -333,18 +509,30 @@ function copyQuestion(question: AskQuestion): AskQuestion {
|
|
|
333
509
|
};
|
|
334
510
|
}
|
|
335
511
|
|
|
512
|
+
/** The fields an answer message reads: the label, the prompt and the option names. */
|
|
513
|
+
export function toAnswerMessageQuestion(question: AskQuestion): AnswerMessageQuestion {
|
|
514
|
+
return {
|
|
515
|
+
header: question.header,
|
|
516
|
+
question: question.question,
|
|
517
|
+
options: question.options?.map((option) => ({ label: option.label })),
|
|
518
|
+
};
|
|
519
|
+
}
|
|
520
|
+
|
|
336
521
|
function copyAnswer(answer: AskQuestionAnswer): AskQuestionAnswer {
|
|
337
522
|
return createAnswer(
|
|
338
523
|
Array.isArray(answer.selectedIndexes) ? [...answer.selectedIndexes] : [],
|
|
339
|
-
typeof answer.customText === "string" && answer.customText.length > 0
|
|
524
|
+
typeof answer.customText === "string" && answer.customText.length > 0
|
|
525
|
+
? answer.customText
|
|
526
|
+
: undefined,
|
|
340
527
|
);
|
|
341
528
|
}
|
|
342
529
|
|
|
343
530
|
/** Custom text is a separate kind of answer, so an unset one is omitted instead of `undefined`. */
|
|
344
|
-
function createAnswer(
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
531
|
+
function createAnswer(
|
|
532
|
+
selectedIndexes: number[],
|
|
533
|
+
customText: string | undefined,
|
|
534
|
+
): AskQuestionAnswer {
|
|
535
|
+
return customText === undefined ? { selectedIndexes } : { selectedIndexes, customText };
|
|
348
536
|
}
|
|
349
537
|
|
|
350
538
|
/** A stored answer can come from an older or hand-edited session entry, so it is validated. */
|
|
@@ -357,12 +545,14 @@ function normalizeAnswer(value: unknown): AskQuestionAnswer | undefined {
|
|
|
357
545
|
|
|
358
546
|
const selectedIndexes = Array.isArray(candidate.selectedIndexes)
|
|
359
547
|
? candidate.selectedIndexes.filter(
|
|
360
|
-
|
|
361
|
-
|
|
548
|
+
(index): index is number =>
|
|
549
|
+
typeof index === "number" && Number.isInteger(index) && index >= 0,
|
|
550
|
+
)
|
|
362
551
|
: [];
|
|
363
|
-
const customText =
|
|
364
|
-
|
|
365
|
-
|
|
552
|
+
const customText =
|
|
553
|
+
typeof candidate.customText === "string" && candidate.customText.length > 0
|
|
554
|
+
? candidate.customText
|
|
555
|
+
: undefined;
|
|
366
556
|
|
|
367
557
|
if (selectedIndexes.length === 0 && customText === undefined) {
|
|
368
558
|
return undefined;
|
|
@@ -384,7 +574,10 @@ function normalizePendingRequest(saved: AskQuestionRequest): AskQuestionRequest
|
|
|
384
574
|
return {
|
|
385
575
|
id: saved.id,
|
|
386
576
|
createdAt: typeof saved.createdAt === "number" ? saved.createdAt : Date.now(),
|
|
387
|
-
shownSeq:
|
|
577
|
+
shownSeq:
|
|
578
|
+
typeof saved.shownSeq === "number" && Number.isFinite(saved.shownSeq)
|
|
579
|
+
? saved.shownSeq
|
|
580
|
+
: undefined,
|
|
388
581
|
questions: saved.questions.map(copyQuestion),
|
|
389
582
|
status: "pending",
|
|
390
583
|
draft: normalizeDraft(saved.draft, saved.questions.length),
|
|
@@ -394,9 +587,9 @@ function normalizePendingRequest(saved: AskQuestionRequest): AskQuestionRequest
|
|
|
394
587
|
/**
|
|
395
588
|
* A settled outbox entry read back from a snapshot.
|
|
396
589
|
*
|
|
397
|
-
* Only the delivery payload is kept: a settled request never reopens, so its draft
|
|
398
|
-
*
|
|
399
|
-
* is not guessed at.
|
|
590
|
+
* Only the delivery payload is kept: a settled request never reopens, so neither its draft nor
|
|
591
|
+
* its panel-only question fields are restored. An answered entry without valid answers cannot
|
|
592
|
+
* be delivered and is not guessed at.
|
|
400
593
|
*/
|
|
401
594
|
function normalizeSettledRequest(saved: AskQuestionRequest): AskQuestionRequest | undefined {
|
|
402
595
|
if (saved.status !== "answered" && saved.status !== "skipped") {
|
|
@@ -406,7 +599,9 @@ function normalizeSettledRequest(saved: AskQuestionRequest): AskQuestionRequest
|
|
|
406
599
|
const request: AskQuestionRequest = {
|
|
407
600
|
id: saved.id,
|
|
408
601
|
createdAt: typeof saved.createdAt === "number" ? saved.createdAt : Date.now(),
|
|
409
|
-
questions: saved.questions.map(
|
|
602
|
+
questions: saved.questions.map((question) =>
|
|
603
|
+
toAnswerMessageQuestion(copyQuestion(question)),
|
|
604
|
+
),
|
|
410
605
|
status: saved.status,
|
|
411
606
|
draft: emptyDraft(saved.questions.length),
|
|
412
607
|
settledAt: typeof saved.settledAt === "number" ? saved.settledAt : Date.now(),
|
|
@@ -433,7 +628,9 @@ function normalizeAnswers(saved: unknown): AskQuestionAnswer[] | undefined {
|
|
|
433
628
|
|
|
434
629
|
const answers = saved.map((answer) => normalizeAnswer(answer));
|
|
435
630
|
|
|
436
|
-
return answers.every((answer): answer is AskQuestionAnswer => answer !== undefined)
|
|
631
|
+
return answers.every((answer): answer is AskQuestionAnswer => answer !== undefined)
|
|
632
|
+
? answers
|
|
633
|
+
: undefined;
|
|
437
634
|
}
|
|
438
635
|
|
|
439
636
|
/** Requests that were never shown sort before every shown one. */
|
|
@@ -441,7 +638,10 @@ function shownOrder(request: AskQuestionRequest): number {
|
|
|
441
638
|
return request.shownSeq ?? -1;
|
|
442
639
|
}
|
|
443
640
|
|
|
444
|
-
function normalizeDraft(
|
|
641
|
+
function normalizeDraft(
|
|
642
|
+
draft: AskQuestionDraft | undefined,
|
|
643
|
+
questionCount: number,
|
|
644
|
+
): AskQuestionDraft {
|
|
445
645
|
const normalized = emptyDraft(questionCount);
|
|
446
646
|
|
|
447
647
|
if (!draft) {
|
|
@@ -450,7 +650,9 @@ function normalizeDraft(draft: AskQuestionDraft | undefined, questionCount: numb
|
|
|
450
650
|
|
|
451
651
|
normalized.currentIndex = clampIndex(draft.currentIndex, questionCount);
|
|
452
652
|
normalized.optionIndex = Math.max(0, draft.optionIndex ?? 0);
|
|
453
|
-
normalized.answers = normalized.answers.map((_, index) =>
|
|
653
|
+
normalized.answers = normalized.answers.map((_, index) =>
|
|
654
|
+
normalizeAnswer(draft.answers?.[index]),
|
|
655
|
+
);
|
|
454
656
|
normalized.customDrafts = normalized.customDrafts.map((_, index) => {
|
|
455
657
|
const text = draft.customDrafts?.[index];
|
|
456
658
|
|
|
@@ -469,6 +671,32 @@ function clampIndex(index: number, questionCount: number): number {
|
|
|
469
671
|
return Math.min(questionCount, Math.max(0, Math.trunc(index)));
|
|
470
672
|
}
|
|
471
673
|
|
|
674
|
+
/**
|
|
675
|
+
* The newest state entry on the active branch, when it matches the schema this build writes.
|
|
676
|
+
*
|
|
677
|
+
* Only the newest entry counts: a malformed one is not guessed at and not patched over with an
|
|
678
|
+
* older one.
|
|
679
|
+
*/
|
|
680
|
+
function newestSnapshot(ctx: ExtensionContext): AskQuestionStateSnapshot | undefined {
|
|
681
|
+
const branch = ctx.sessionManager.getBranch();
|
|
682
|
+
|
|
683
|
+
for (let index = branch.length - 1; index >= 0; index--) {
|
|
684
|
+
const entry = branch[index];
|
|
685
|
+
|
|
686
|
+
if (!entry || entry.type !== "custom" || entry.customType !== QUESTION_STATE_ENTRY) {
|
|
687
|
+
continue;
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
const snapshot = entry.data as Partial<AskQuestionStateSnapshot> | undefined;
|
|
691
|
+
|
|
692
|
+
return snapshot && Array.isArray(snapshot.requests) && Array.isArray(snapshot.outbox)
|
|
693
|
+
? (snapshot as AskQuestionStateSnapshot)
|
|
694
|
+
: undefined;
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
return undefined;
|
|
698
|
+
}
|
|
699
|
+
|
|
472
700
|
function isStoredRequest(value: unknown): value is AskQuestionRequest {
|
|
473
701
|
if (!value || typeof value !== "object") {
|
|
474
702
|
return false;
|
|
@@ -478,7 +706,3 @@ function isStoredRequest(value: unknown): value is AskQuestionRequest {
|
|
|
478
706
|
|
|
479
707
|
return typeof request.id === "string" && Array.isArray(request.questions);
|
|
480
708
|
}
|
|
481
|
-
|
|
482
|
-
// Shared singleton: its state outlives extension reloads, which is why the extension
|
|
483
|
-
// clears it on session start before restoring from the branch.
|
|
484
|
-
export const questionManager = new QuestionManager();
|