@parallelworks/code 0.5.0 → 0.6.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.
Files changed (69) hide show
  1. package/README.md +53 -12
  2. package/dist/api.d.ts +67 -5
  3. package/dist/chatStrings.d.ts +3 -0
  4. package/dist/chatStrings.js +4 -1
  5. package/dist/command.d.ts +2 -0
  6. package/dist/command.js +5 -1
  7. package/dist/commandsApi.d.ts +8 -3
  8. package/dist/commandsApi.js +25 -4
  9. package/dist/createPwCodeApi.d.ts +26 -61
  10. package/dist/index.d.ts +6 -5
  11. package/dist/index.js +3 -2
  12. package/dist/locales/en/agentTranscript.js +5 -2
  13. package/dist/locales/en/agents.js +35 -4
  14. package/dist/locales/en.d.ts +33 -0
  15. package/dist/locales/es/agentTranscript.js +5 -2
  16. package/dist/locales/es/agents.js +35 -4
  17. package/dist/locales/ja/agentTranscript.js +5 -2
  18. package/dist/locales/ja/agents.js +30 -4
  19. package/dist/locales/ko/agentTranscript.js +5 -2
  20. package/dist/locales/ko/agents.js +35 -4
  21. package/dist/locales/zh/agentTranscript.js +5 -2
  22. package/dist/locales/zh/agents.js +35 -4
  23. package/dist/machine.d.ts +27 -2
  24. package/dist/machine.js +23 -2
  25. package/dist/notices.js +9 -1
  26. package/dist/pwUpdate.d.ts +52 -0
  27. package/dist/pwUpdate.js +115 -0
  28. package/dist/session.d.ts +1 -1
  29. package/dist/session.js +1 -1
  30. package/dist/sessionActions.js +5 -2
  31. package/dist/sessionEvents.d.ts +10 -3
  32. package/dist/sessionEvents.js +27 -11
  33. package/dist/sessionScreen.d.ts +3 -2
  34. package/dist/sessionScreen.js +8 -2
  35. package/dist/sessionsApi.d.ts +8 -2
  36. package/dist/sessionsApi.js +28 -5
  37. package/dist/startPage.d.ts +6 -3
  38. package/dist/startPage.js +34 -12
  39. package/dist/startSession.d.ts +31 -11
  40. package/dist/startSession.js +126 -44
  41. package/dist/terminalHandoffPanel.js +8 -1
  42. package/dist/transcript.d.ts +26 -3
  43. package/dist/transcript.js +87 -12
  44. package/dist/transcriptRead.d.ts +1 -0
  45. package/dist/transcriptRead.js +2 -1
  46. package/dist/types.d.ts +13 -1
  47. package/dist/useComposer.d.ts +22 -10
  48. package/dist/useComposer.js +75 -25
  49. package/dist/useComposerImages.d.ts +38 -1
  50. package/dist/useComposerImages.js +59 -4
  51. package/dist/useConversation.d.ts +10 -0
  52. package/dist/useConversation.js +14 -11
  53. package/dist/useSubagent.d.ts +20 -5
  54. package/dist/useSubagent.js +39 -17
  55. package/dist/useTranscript.d.ts +27 -1
  56. package/dist/useTranscript.js +217 -45
  57. package/dist/wording.d.ts +1 -1
  58. package/dist/wording.js +14 -2
  59. package/locales/en/agentTranscript.json +5 -2
  60. package/locales/en/agents.json +32 -2
  61. package/locales/es/agentTranscript.json +5 -2
  62. package/locales/es/agents.json +32 -2
  63. package/locales/ja/agentTranscript.json +5 -2
  64. package/locales/ja/agents.json +32 -2
  65. package/locales/ko/agentTranscript.json +5 -2
  66. package/locales/ko/agents.json +32 -2
  67. package/locales/zh/agentTranscript.json +5 -2
  68. package/locales/zh/agents.json +32 -2
  69. package/package.json +3 -3
@@ -6,10 +6,11 @@ import { usePwCodeTranslations } from "./messages.js";
6
6
  import { useTranscriptWords } from "./notices.js";
7
7
  import { pendingApprovals, subagentSessionDetail, turnRunning } from "./transcript.js";
8
8
  import { directionOutcome, transcriptMessages, withdrawalOutcome } from "./useConversation.js";
9
- import { usePwCodeTranscript } from "./useTranscript.js";
9
+ import { canTakeBack, usePwCodeTranscript } from "./useTranscript.js";
10
10
  import { modelLabel } from "./models.js";
11
11
  import { sessionDraftKey, workingActivity } from "./sessionScreen.js";
12
12
  import { pasteRefs } from "./sessionsApi.js";
13
+ import { takeMessageImages, useTakeBackImages } from "./useComposerImages.js";
13
14
  import { composerState } from "./wording.js";
14
15
  import { useCallback, useEffect, useMemo, useState } from "react";
15
16
  // src/useSubagent.ts
@@ -18,7 +19,7 @@ var nextStep = (_id) => "nextStep";
18
19
  * current from the session's feed, and a composer that talks to it. Commands
19
20
  * and `!` stay with the main conversation's composer. The host reads the
20
21
  * subagent with `api.useSubagentDetail`. */
21
- function usePwCodeSubagent({ api, machine, sessionId, subId, subagent, listed, usage, subscribe, loadPalette, describeError, sessionLock, modelNames, onSent }) {
22
+ function usePwCodeSubagent({ api, machine, sessionId, subId, subagent, listed, usage, subscribe, loadPalette, describeError, sessionLock, modelNames, images, onSent }) {
22
23
  const t = usePwCodeTranslations("agents");
23
24
  const typeLabel = useAgentTypeLabel();
24
25
  const { data, error, refresh } = subagent;
@@ -26,11 +27,12 @@ function usePwCodeSubagent({ api, machine, sessionId, subId, subagent, listed, u
26
27
  const name = known ? typeLabel(known.agentType) : "";
27
28
  const detail = useMemo(() => data ? subagentSessionDetail(data) : void 0, [data]);
28
29
  const [notice, setNotice] = useState(null);
29
- const postMessage = useCallback(async (text, sent) => {
30
+ const postMessage = useCallback(async (text, sent, images) => {
30
31
  try {
31
32
  const { data: accepted, error: refusal } = await api.messageSubagent(machine, sessionId, subId, {
32
33
  text,
33
- ...pasteRefs(sent)
34
+ ...pasteRefs(sent),
35
+ ...images?.length ? { images } : {}
34
36
  });
35
37
  return refusal ? { error: describeError(refusal) } : { turnId: accepted?.turnId };
36
38
  } catch (thrown) {
@@ -43,9 +45,10 @@ function usePwCodeSubagent({ api, machine, sessionId, subId, subagent, listed, u
43
45
  sessionId,
44
46
  subId
45
47
  ]);
46
- const postDirection = useCallback((text, sent) => directionOutcome(() => api.steerSubagent(machine, sessionId, subId, {
48
+ const postDirection = useCallback((text, sent, images) => directionOutcome(() => api.steerSubagent(machine, sessionId, subId, {
47
49
  text,
48
- ...pasteRefs(sent)
50
+ ...pasteRefs(sent),
51
+ ...images?.length ? { images } : {}
49
52
  }), t("detail.steerUncertain"), describeError), [
50
53
  api,
51
54
  describeError,
@@ -67,7 +70,7 @@ function usePwCodeSubagent({ api, machine, sessionId, subId, subagent, listed, u
67
70
  }, [refresh]);
68
71
  const words = useTranscriptWords();
69
72
  const imageUrl = useCallback((ref) => sessionImageUrl(machine, sessionId, ref), [machine, sessionId]);
70
- const { transcript, sending, onEvent, sendMessage: sendToTranscript, resolveApproval, canRemoveQueued, removeQueued: removeFromTranscript, takeBackPending } = usePwCodeTranscript({
73
+ const { transcript, sending, onEvent, sendMessage: sendToTranscript, resolveApproval, canRemoveQueued, removeQueued: removeFromTranscript, takeBackPending, handedBack, handedBackShown, dropShownHandedBack } = usePwCodeTranscript({
71
74
  subagent: subId,
72
75
  detail,
73
76
  send: postMessage,
@@ -82,20 +85,26 @@ function usePwCodeSubagent({ api, machine, sessionId, subId, subagent, listed, u
82
85
  const refusal = await removeFromTranscript(id);
83
86
  if (refusal) setNotice(refusal);
84
87
  }, [removeFromTranscript]);
88
+ const takenBack = useTakeBackImages(images?.restore);
89
+ const backWithImages = takenBack.back;
85
90
  const takeBack = useCallback(async () => {
86
91
  const { taken, refusal } = await takeBackPending();
87
92
  if (refusal) setNotice(refusal);
88
- return taken.map(({ text, pastes }) => ({
89
- text,
90
- pastes
91
- }));
92
- }, [takeBackPending]);
93
+ return backWithImages(taken);
94
+ }, [backWithImages, takeBackPending]);
95
+ const takeImages = images?.take;
93
96
  const send = useCallback(async (typed, sent) => {
94
97
  if (shellCommand(typed) !== null || typed.trimStart().startsWith("/") && slashAction(typed, await loadPalette().catch(() => null)).kind !== "message") {
95
98
  setNotice(t("switcher.commandsOnMain"));
96
99
  return false;
97
100
  }
98
- const refusal = await sendToTranscript(typed, { pastes: sent });
101
+ const { text, images: refs, previews, settle } = await takeMessageImages(typed, takeImages);
102
+ const refusal = await sendToTranscript(text, {
103
+ pastes: sent,
104
+ images: refs,
105
+ previews
106
+ });
107
+ settle(!refusal);
99
108
  setNotice(refusal);
100
109
  if (!refusal) onSent();
101
110
  return !refusal;
@@ -103,7 +112,8 @@ function usePwCodeSubagent({ api, machine, sessionId, subId, subagent, listed, u
103
112
  loadPalette,
104
113
  onSent,
105
114
  sendToTranscript,
106
- t
115
+ t,
116
+ takeImages
107
117
  ]);
108
118
  const [stopping, setStopping] = useState(false);
109
119
  const stop = useCallback(async () => {
@@ -185,9 +195,21 @@ function usePwCodeSubagent({ api, machine, sessionId, subId, subagent, listed, u
185
195
  removeQueued,
186
196
  /** The composer's `recall`: ↑ in it, or Edit under the queue, takes back what waits. */
187
197
  recall: {
188
- available: messages.queued.some((m) => canRemoveQueued(m.id)),
189
- take: takeBack
198
+ available: canTakeBack(handedBack, messages.queued, canRemoveQueued),
199
+ take: takeBack,
200
+ placed: takenBack.placed
190
201
  },
202
+ /** Grows as a turn gives back a message the model never read; the host
203
+ * takes it into its composer then, as `recall` does on ↑. */
204
+ handedBack,
205
+ /** A message waiting to come back is one a failed turn left drawn where
206
+ * it was sent: take back only into an empty composer
207
+ * (`recall({ ifEmpty: true })` on the session view's handle), and call
208
+ * `dropShownHandedBack` when nothing came back; the rest then comes back
209
+ * as `handedBack` drops. */
210
+ handedBackShown,
211
+ /** Lets go of what failed turns gave back, which stays in the transcript and in the composer's history. */
212
+ dropShownHandedBack,
191
213
  showAuthor: messages.all.some((m) => m.author),
192
214
  /** The subagent has yet to load for the first time. */
193
215
  loading: !data && !error,
@@ -207,7 +229,7 @@ function usePwCodeSubagent({ api, machine, sessionId, subId, subagent, listed, u
207
229
  answerApproval,
208
230
  composer: {
209
231
  ...composer,
210
- disabled: sending || composer.locked || !data
232
+ disabled: sending || !!images?.taking || composer.locked || !data
211
233
  }
212
234
  };
213
235
  }
@@ -26,6 +26,9 @@ export interface PwCodeTakenBack {
26
26
  pastes?: MessagePaste[] | undefined;
27
27
  images?: PwCodeImageRef[] | undefined;
28
28
  previews?: PwCodeImage[] | undefined;
29
+ /** Still drawn where it was sent, as a failed turn leaves it: a composer
30
+ * that filled meanwhile can let it go, as the terminal does. */
31
+ shown?: boolean | undefined;
29
32
  }
30
33
  interface SendOptions {
31
34
  steer?: boolean;
@@ -36,6 +39,11 @@ interface SendOptions {
36
39
  /** Drawn on the message until its echo brings the daemon's; a note can wait a whole tool call for that. */
37
40
  previews?: PwCodeImage[] | undefined;
38
41
  }
42
+ /** Whether a composer's ↑ has anything to take back: a message a turn gave
43
+ * back, or one still waiting that the machine would give up. */
44
+ export declare function canTakeBack(handedBack: number, queued: readonly {
45
+ id: string;
46
+ }[], canRemoveQueued: (id: string) => boolean): boolean;
39
47
  /** Holds the live transcript for one session. The host still owns the socket
40
48
  * and the fetch: it feeds events to `onEvent` and posts through `send`. */
41
49
  export declare function usePwCodeTranscript({ sessionId, subagent, detail, send, steer, withdraw, withdrawDirection, onEffect, goalNotice, compactionNotice, recapNotice, words, imageUrl, }: {
@@ -66,17 +74,35 @@ export declare function usePwCodeTranscript({ sessionId, subagent, detail, send,
66
74
  sendMessage: (text: string, options?: SendOptions) => Promise<string | null>;
67
75
  resolveApproval: (approvalId: string) => void;
68
76
  noteShellRun: (command: string, output: string) => void;
77
+ /** Shows a command as typed while the machine runs it, until the call it
78
+ * returns: then, `filed`, the record the machine filed of it takes its
79
+ * place at the next read; otherwise it goes at once. */
80
+ showCommand: (command: string) => (filed: boolean) => void;
69
81
  /** Pending messages the daemon named a turn or note for can be taken back
70
82
  * until they reach the model. */
71
83
  canRemoveQueued: (id: string) => boolean;
72
84
  /** Resolves to the refusal, or null once the message is gone. */
73
85
  removeQueued: (id: string) => Promise<string | null>;
74
86
  /** Takes back every pending message the machine still holds, with the first
75
- * refusal; one the model read meanwhile stays sent. */
87
+ * refusal; one the model read meanwhile stays sent. What a turn gave back
88
+ * comes first. */
76
89
  takeBackPending: () => Promise<{
77
90
  taken: PwCodeTakenBack[];
78
91
  refusal: string | null;
79
92
  }>;
93
+ /** Messages this window sent that a turn failed or stopped on before the
94
+ * model read them, so the conversation left them out: as the terminal
95
+ * does, they go back to the composer. `takeBackPending` returns them; the
96
+ * host takes them into its composer as this grows. */
97
+ handedBack: number;
98
+ /** A message waiting to come back is still drawn where it was sent, as a
99
+ * failed turn leaves it: as the terminal refills only an empty line, it
100
+ * goes back only into an empty composer. */
101
+ handedBackShown: boolean;
102
+ /** Lets go of the messages still drawn where they were sent, for a
103
+ * composer that held something: they stay in the transcript, and in its
104
+ * history. A take running meanwhile collects them instead. */
105
+ dropShownHandedBack: () => void;
80
106
  /** Where a reconnect resumes from: the last seq applied, read outside render. */
81
107
  cursor: () => number;
82
108
  };
@@ -1,15 +1,57 @@
1
- import { appendOwnMessage, appendShellRun, appendSteeredMessage, appendTurnMessage, applyStreamEvent, dropMessage, initialTranscript, markQueued, pendingMessages, resolveApprovalEverywhere, subagentFrame, turnRunning } from "./transcript.js";
1
+ import { appendCommand, appendOwnMessage, appendShellRun, appendSteeredMessage, appendTurnMessage, applyStreamEvent, dropMessage, initialTranscript, markQueued, pendingMessages, queueOwnMessage, resolveApprovalEverywhere, stoppedTurn, subagentFrame, turnRunning } from "./transcript.js";
2
2
  import { useCallback, useEffect, useRef, useState } from "react";
3
3
  // src/useTranscript.ts
4
4
  var MAX_REPLAY_FRAMES = 5e3;
5
+ var MAX_TURN_OUTCOMES = 50;
6
+ function noteOutcome(outcomes, turnId, outcome) {
7
+ if (outcomes.get(turnId) === "settled") return;
8
+ outcomes.delete(turnId);
9
+ outcomes.set(turnId, outcome);
10
+ const oldest = outcomes.keys().next().value;
11
+ if (outcomes.size > MAX_TURN_OUTCOMES && oldest !== void 0) outcomes.delete(oldest);
12
+ }
13
+ /** What a frame says of its turn's prompt, given the turn as drawn before it.
14
+ * As in the terminal, a turn that replied before it failed, as a skill does
15
+ * before its prompt counts as read, gives nothing back. `readsReported` says
16
+ * the machine sends prompt_read at all. */
17
+ function turnOutcome(ev, before, readsReported) {
18
+ switch (ev.type) {
19
+ case "prompt_read":
20
+ case "done": return "settled";
21
+ case "error": {
22
+ const replied = !(before.streaming?.parts ?? []).every((p) => p.kind === "notice" || p.kind === "warning" || p.kind === "error");
23
+ if (stoppedTurn(ev)) return replied && !readsReported ? "settled" : "stopped";
24
+ return replied ? "settled" : "failed";
25
+ }
26
+ default: return null;
27
+ }
28
+ }
29
+ /** The message a stop closed the turn with, when it says only that: the
30
+ * machine records no stop for a prompt it hands back. */
31
+ function stopLineOf(before, after) {
32
+ const closing = after.messages.at(-1);
33
+ return !!closing && closing.role === "assistant" && !before.messages.some((m) => m.id === closing.id) && (closing.parts ?? []).every((p) => p.kind === "notice") ? closing.id : void 0;
34
+ }
35
+ /** Takes a prompt its turn left out of the conversation off the transcript,
36
+ * with the stop line that closed it. */
37
+ function withoutPrompt(state, messageId, stopLine) {
38
+ const dropped = dropMessage(state, messageId);
39
+ return stopLine ? dropMessage(dropped, stopLine) : dropped;
40
+ }
41
+ /** The message a user_message from another window drew for the turn it opened. */
42
+ function othersPrompt(ev, before, after, own) {
43
+ if (ev.type !== "user_message" || !ev.turnId || own.echoOf || own.turnIds.has(ev.turnId) || !!ev.content && own.texts.has(ev.content)) return;
44
+ const placed = after.messages.at(-1);
45
+ return placed?.role === "user" && !before.messages.some((m) => m.id === placed.id) ? placed.id : void 0;
46
+ }
5
47
  function keepForSeed(frames, ev) {
6
48
  frames.push(ev);
7
49
  if (frames.length > MAX_REPLAY_FRAMES) frames.shift();
8
50
  }
9
51
  /** Matches the way the echo is recognised: by turn once the send has named it,
10
- * by text before then. */
52
+ * by text before then. A schedule's prompt is no send's echo, whatever its words. */
11
53
  function echoedSend(unechoed, ev) {
12
- if (ev.type !== "user_message" && ev.type !== "user_note") return;
54
+ if (ev.type !== "user_message" && ev.type !== "user_note" || ev.scheduled) return;
13
55
  const steered = ev.type === "user_note";
14
56
  for (const [localId, sent] of unechoed) {
15
57
  if (!!sent.steered !== steered) continue;
@@ -32,6 +74,11 @@ function forgetPlaced(unechoed, detail) {
32
74
  if (!sent.steered && (reached || sent.text === last)) unechoed.delete(localId);
33
75
  }
34
76
  }
77
+ /** Whether a composer's ↑ has anything to take back: a message a turn gave
78
+ * back, or one still waiting that the machine would give up. */
79
+ function canTakeBack(handedBack, queued, canRemoveQueued) {
80
+ return handedBack > 0 || queued.some((m) => canRemoveQueued(m.id));
81
+ }
35
82
  /** Holds the live transcript for one session. The host still owns the socket
36
83
  * and the fetch: it feeds events to `onEvent` and posts through `send`. */
37
84
  function usePwCodeTranscript({ sessionId, subagent, detail, send, steer, withdraw, withdrawDirection, onEffect, goalNotice, compactionNotice, recapNotice, words, imageUrl }) {
@@ -42,7 +89,27 @@ function usePwCodeTranscript({ sessionId, subagent, detail, send, steer, withdra
42
89
  const ownTexts = useRef(/* @__PURE__ */ new Set());
43
90
  const ownSeq = useRef(0);
44
91
  const unechoed = useRef(/* @__PURE__ */ new Map());
92
+ const commands = useRef(/* @__PURE__ */ new Map());
93
+ const heldRefetch = useRef(false);
45
94
  const appliedSinceSeed = useRef([]);
95
+ const unreadTurns = useRef(/* @__PURE__ */ new Map());
96
+ const turnOutcomes = useRef(/* @__PURE__ */ new Map());
97
+ const othersUnread = useRef(/* @__PURE__ */ new Map());
98
+ const readsReported = useRef(false);
99
+ const feedGaps = useRef(0);
100
+ const handedBackRef = useRef([]);
101
+ const [handedBack, setHandedBack] = useState({
102
+ count: 0,
103
+ shown: false
104
+ });
105
+ const publishHandedBack = useCallback(() => {
106
+ const waiting = handedBackRef.current;
107
+ setHandedBack({
108
+ count: waiting.length,
109
+ shown: waiting.some((m) => m.shown)
110
+ });
111
+ }, []);
112
+ const takingBack = useRef(false);
46
113
  const sendRef = useRef(send);
47
114
  const steerRef = useRef(steer);
48
115
  const withdrawRef = useRef(withdraw);
@@ -96,8 +163,9 @@ function usePwCodeTranscript({ sessionId, subagent, detail, send, steer, withdra
96
163
  for (const [localId, sent] of unechoed.current) {
97
164
  const { text, pastes, previews, steered } = sent;
98
165
  if (steered) next = appendSteeredMessage(next, text, localId, pastes, previews);
99
- else if (!subagent) next = appendOwnMessage(next, text, localId, pastes, previews);
166
+ else if (!subagent) next = (commands.current.size > 0 ? queueOwnMessage : appendOwnMessage)(next, text, localId, pastes, previews);
100
167
  }
168
+ for (const [localId, command] of commands.current) next = appendCommand(next, command, localId);
101
169
  const cursor = Math.max(next.cursor, transcriptRef.current?.cursor ?? 0);
102
170
  next = cursor === next.cursor ? next : {
103
171
  ...next,
@@ -113,7 +181,58 @@ function usePwCodeTranscript({ sessionId, subagent, detail, send, steer, withdra
113
181
  transcriptRef.current = next;
114
182
  setTranscript(next);
115
183
  }, []);
184
+ /** Gives back a message its turn left out of the conversation. The machine
185
+ * still shows one its turn failed on where it was sent, as the terminal
186
+ * does; it keeps nothing of one a stop handed back, or never echoed. */
187
+ const handBack = useCallback((turnId, { localId, sent }, outcome, stopLine) => {
188
+ noteOutcome(turnOutcomes.current, turnId, "settled");
189
+ const { text, typed, pastes, images, previews } = sent;
190
+ const shown = outcome === "failed" && (!!subagent || !unechoed.current.has(localId));
191
+ handedBackRef.current.push({
192
+ text: typed ?? text,
193
+ pastes,
194
+ images,
195
+ previews,
196
+ shown
197
+ });
198
+ publishHandedBack();
199
+ if (!shown) {
200
+ unechoed.current.delete(localId);
201
+ apply((state) => withoutPrompt(state, localId, stopLine));
202
+ }
203
+ }, [
204
+ apply,
205
+ subagent,
206
+ publishHandedBack
207
+ ]);
208
+ const dropShownHandedBack = useCallback(() => {
209
+ if (takingBack.current) return;
210
+ handedBackRef.current = handedBackRef.current.filter((m) => !m.shown);
211
+ publishHandedBack();
212
+ }, [publishHandedBack]);
213
+ /** Follows a turn this window sent until the model reads its prompt, or
214
+ * gives its message back at once when the turn already ended without. */
215
+ const trackUnread = useCallback((turnId, unread) => {
216
+ if (unread.sent.steered || unreadTurns.current.has(turnId)) return;
217
+ const outcome = turnOutcomes.current.get(turnId);
218
+ if (outcome === "settled") return;
219
+ if (outcome) {
220
+ handBack(turnId, unread, outcome);
221
+ return;
222
+ }
223
+ unreadTurns.current.set(turnId, unread);
224
+ }, [handBack]);
225
+ const settleTurn = useCallback((turnId, outcome, stopLine) => {
226
+ const unread = unreadTurns.current.get(turnId);
227
+ unreadTurns.current.delete(turnId);
228
+ const othersMessage = othersUnread.current.get(turnId);
229
+ othersUnread.current.delete(turnId);
230
+ if (unread && outcome !== "settled") handBack(turnId, unread, outcome, stopLine);
231
+ else noteOutcome(turnOutcomes.current, turnId, outcome);
232
+ if (othersMessage && outcome === "stopped") apply((state) => withoutPrompt(state, othersMessage, stopLine));
233
+ }, [apply, handBack]);
116
234
  const onEvent = useCallback((frame) => {
235
+ if (frame.type === "prompt_read") readsReported.current = true;
117
236
  const ev = subagent ? subagentFrame(frame, subagent) : frame;
118
237
  if (ev === "refetch") {
119
238
  onEffectRef.current({ type: "refetch" });
@@ -139,25 +258,39 @@ function usePwCodeTranscript({ sessionId, subagent, detail, send, steer, withdra
139
258
  words: wordsRef.current,
140
259
  imageUrl: imageUrlRef.current
141
260
  });
142
- if (ev.type === "replay_truncated") appliedSinceSeed.current = [];
143
- else if (state.cursor > current.cursor) {
261
+ if (ev.type === "replay_truncated") {
262
+ appliedSinceSeed.current = [];
263
+ unreadTurns.current.clear();
264
+ othersUnread.current.clear();
265
+ feedGaps.current += 1;
266
+ } else if (state.cursor > current.cursor) {
144
267
  keepForSeed(appliedSinceSeed.current, ev);
145
268
  if (echoOf) unechoed.current.delete(echoOf);
146
269
  forgetSettled(unechoed.current, current, state);
147
270
  }
148
271
  apply(() => state);
149
- for (const effect of effects) onEffectRef.current(effect);
272
+ const othersMessage = subagent ? void 0 : othersPrompt(ev, current, state, {
273
+ echoOf,
274
+ turnIds: ownTurnIds.current,
275
+ texts: ownTexts.current
276
+ });
277
+ if (ev.turnId && othersMessage) othersUnread.current.set(ev.turnId, othersMessage);
278
+ const outcome = ev.turnId && state.cursor > current.cursor ? turnOutcome(ev, current, readsReported.current) : null;
279
+ if (ev.turnId && outcome) settleTurn(ev.turnId, outcome, outcome === "stopped" ? stopLineOf(current, state) : void 0);
280
+ for (const effect of effects) if (effect.type === "refetch" && commands.current.size > 0) heldRefetch.current = true;
281
+ else onEffectRef.current(effect);
150
282
  if (subagent && frame.type === "subagent_user_note") onEffectRef.current({ type: "refetch" });
151
283
  }, [
152
284
  apply,
153
285
  sessionId,
154
- subagent
286
+ subagent,
287
+ settleTurn
155
288
  ]);
156
289
  /** True once the running turn took the text, or the refusal if it may have.
157
290
  * Commands and shell escapes change the next turn, so like the TUI they wait. */
158
291
  const trySteer = useCallback(async (text, { typed, pastes, images, previews }) => {
159
292
  const steerTo = steerRef.current;
160
- if (!steerTo || !turnRunning(transcriptRef.current) || /^[/!]/.test(text.trimStart())) return false;
293
+ if (!steerTo || !turnRunning(transcriptRef.current) || commands.current.size > 0 || /^[/!]/.test(text.trimStart())) return false;
161
294
  ownSeq.current += 1;
162
295
  const localId = `own-${ownSeq.current}`;
163
296
  unechoed.current.set(localId, {
@@ -197,8 +330,9 @@ function usePwCodeTranscript({ sessionId, subagent, detail, send, steer, withdra
197
330
  images,
198
331
  previews
199
332
  });
200
- const append = subagent ? appendTurnMessage : appendOwnMessage;
333
+ const append = subagent ? appendTurnMessage : commands.current.size > 0 ? queueOwnMessage : appendOwnMessage;
201
334
  apply((state) => append(state, text, localId, pastes, previews));
335
+ const gapsBefore = feedGaps.current;
202
336
  const result = await sendRef.current(text, pastes, images);
203
337
  setSending(false);
204
338
  if ("error" in result) {
@@ -214,15 +348,40 @@ function usePwCodeTranscript({ sessionId, subagent, detail, send, steer, withdra
214
348
  ownTexts.current.delete(text);
215
349
  const pending = unechoed.current.get(localId);
216
350
  if (pending) pending.turnId = result.turnId;
351
+ if (feedGaps.current === gapsBefore) trackUnread(result.turnId, {
352
+ localId,
353
+ sent: pending ?? {
354
+ text,
355
+ typed,
356
+ pastes,
357
+ images,
358
+ previews
359
+ }
360
+ });
217
361
  }
218
362
  return null;
219
363
  }, [
220
364
  apply,
221
365
  trySteer,
222
- subagent
366
+ subagent,
367
+ trackUnread
223
368
  ]);
224
369
  const resolveApproval = useCallback((approvalId) => apply((state) => resolveApprovalEverywhere(state, approvalId)), [apply]);
225
370
  const noteShellRun = useCallback((command, output) => apply((state) => appendShellRun(state, command, output)), [apply]);
371
+ const showCommand = useCallback((command) => {
372
+ ownSeq.current += 1;
373
+ const localId = `own-${ownSeq.current}`;
374
+ commands.current.set(localId, command);
375
+ apply((state) => appendCommand(state, command, localId));
376
+ return (filed) => {
377
+ commands.current.delete(localId);
378
+ if (!filed) apply((state) => dropMessage(state, localId));
379
+ if (heldRefetch.current && commands.current.size === 0) {
380
+ heldRefetch.current = false;
381
+ onEffectRef.current({ type: "refetch" });
382
+ }
383
+ };
384
+ }, [apply]);
226
385
  /** Takes back a message queued as a turn of its own, or one steered into
227
386
  * the turn that the model has not been handed yet. */
228
387
  const withdrawal = (id) => {
@@ -246,7 +405,10 @@ function usePwCodeTranscript({ sessionId, subagent, detail, send, steer, withdra
246
405
  const result = await withdrawIt();
247
406
  if (result.error) return { refusal: result.error };
248
407
  unechoed.current.delete(id);
249
- if (sent.turnId) ownTurnIds.current.delete(sent.turnId);
408
+ if (sent.turnId) {
409
+ ownTurnIds.current.delete(sent.turnId);
410
+ unreadTurns.current.delete(sent.turnId);
411
+ }
250
412
  apply((state) => dropMessage(state, id));
251
413
  return { sent };
252
414
  }, [apply]);
@@ -254,7 +416,42 @@ function usePwCodeTranscript({ sessionId, subagent, detail, send, steer, withdra
254
416
  const outcome = await withdrawMessage(id);
255
417
  return outcome && "refusal" in outcome ? outcome.refusal : null;
256
418
  }, [withdrawMessage]);
257
- const takingBack = useRef(false);
419
+ /** Takes back every pending message the machine still holds, as ↑ does in the
420
+ * terminal: steered ones first, then turns of their own, each oldest first.
421
+ * One runs at a time: another meanwhile takes nothing. */
422
+ const takeBackPending = useCallback(async () => {
423
+ if (takingBack.current) return {
424
+ taken: [],
425
+ refusal: null
426
+ };
427
+ takingBack.current = true;
428
+ try {
429
+ const state = transcriptRef.current;
430
+ const ids = state ? [...state.steered, ...state.queued].map((m) => m.id) : [];
431
+ const outcomes = [];
432
+ for (const id of ids.toReversed()) outcomes.unshift(await withdrawMessage(id));
433
+ const taken = handedBackRef.current;
434
+ handedBackRef.current = [];
435
+ if (taken.length > 0) publishHandedBack();
436
+ let refusal = null;
437
+ for (const outcome of outcomes) if (outcome && "sent" in outcome) {
438
+ const { text, typed, pastes, images, previews } = outcome.sent;
439
+ taken.push({
440
+ text: typed ?? text,
441
+ pastes,
442
+ images,
443
+ previews
444
+ });
445
+ } else if (outcome) refusal ??= outcome.refusal;
446
+ return {
447
+ taken,
448
+ refusal
449
+ };
450
+ } finally {
451
+ takingBack.current = false;
452
+ }
453
+ }, [withdrawMessage, publishHandedBack]);
454
+ const cursor = useCallback(() => transcriptRef.current?.cursor ?? 0, []);
258
455
  return {
259
456
  transcript,
260
457
  sending,
@@ -262,39 +459,14 @@ function usePwCodeTranscript({ sessionId, subagent, detail, send, steer, withdra
262
459
  sendMessage,
263
460
  resolveApproval,
264
461
  noteShellRun,
462
+ showCommand,
265
463
  canRemoveQueued,
266
464
  removeQueued,
267
- takeBackPending: useCallback(async () => {
268
- if (takingBack.current) return {
269
- taken: [],
270
- refusal: null
271
- };
272
- takingBack.current = true;
273
- try {
274
- const state = transcriptRef.current;
275
- const ids = state ? [...state.steered, ...state.queued].map((m) => m.id) : [];
276
- const outcomes = [];
277
- for (const id of ids.toReversed()) outcomes.unshift(await withdrawMessage(id));
278
- const taken = [];
279
- let refusal = null;
280
- for (const outcome of outcomes) if (outcome && "sent" in outcome) {
281
- const { text, typed, pastes, images, previews } = outcome.sent;
282
- taken.push({
283
- text: typed ?? text,
284
- pastes,
285
- images,
286
- previews
287
- });
288
- } else if (outcome) refusal ??= outcome.refusal;
289
- return {
290
- taken,
291
- refusal
292
- };
293
- } finally {
294
- takingBack.current = false;
295
- }
296
- }, [withdrawMessage]),
297
- cursor: useCallback(() => transcriptRef.current?.cursor ?? 0, [])
465
+ takeBackPending,
466
+ handedBack: handedBack.count,
467
+ handedBackShown: handedBack.shown,
468
+ dropShownHandedBack,
469
+ cursor
298
470
  };
299
471
  }
300
- export { usePwCodeTranscript };
472
+ export { canTakeBack, usePwCodeTranscript };
package/dist/wording.d.ts CHANGED
@@ -20,7 +20,7 @@ export declare function sessionStatusText(s: {
20
20
  }, t: Translate, resumeClock?: (at: string) => string): string;
21
21
  /** A dropped machine outranks the session's own state. */
22
22
  export declare function sessionStateText(s: Parameters<typeof sessionStatusText>[0], machine: Pick<PwCodeMachine, 'status'> | undefined, t: Translate, resumeClock?: (at: string) => string): string;
23
- export declare function machineHint(machine: Pick<PwCodeMachine, 'status' | 'error' | 'agentVersion'>, t: Translate): {
23
+ export declare function machineHint(machine: Pick<PwCodeMachine, 'status' | 'error' | 'agentVersion'> & Partial<Pick<PwCodeMachine, 'kind'>>, t: Translate): {
24
24
  text: string;
25
25
  detail?: string;
26
26
  } | null;
package/dist/wording.js CHANGED
@@ -32,7 +32,8 @@ var DOWN_STATUSES = [
32
32
  "no_daemon",
33
33
  "no_agent",
34
34
  "unreachable",
35
- "unsupported_agent"
35
+ "unsupported_agent",
36
+ "starting"
36
37
  ];
37
38
  function isDownStatus(status) {
38
39
  return DOWN_STATUSES.includes(status);
@@ -42,8 +43,19 @@ function sessionStateText(s, machine, t, resumeClock) {
42
43
  if (!isMachineDown(machine)) return sessionStatusText(s, t, resumeClock);
43
44
  return t(`machine.${isDownStatus(machine.status) ? machine.status : "unreachable"}`);
44
45
  }
46
+ /** Why a machine's pw is too old to read sessions from. A computer's own pw
47
+ * dials in, with no pw agent between. */
48
+ function tooOldHint(machine, t) {
49
+ const version = machine.agentVersion;
50
+ if (machine.kind === "local") return version ? t("machine.unsupportedComputerHintVersion", { version }) : t("machine.unsupportedComputerHint");
51
+ return version ? t("machine.unsupportedHintVersion", { version }) : t("machine.unsupportedHint");
52
+ }
45
53
  function machineHint(machine, t) {
46
54
  const detail = machine.error ? { detail: machine.error } : {};
55
+ if (machine.kind === "workspace" && machine.status !== "connected" && machine.status !== "no_daemon") return {
56
+ text: t(machine.status === "unsupported_agent" ? "machine.workspaceOutdatedHint" : machine.status === "starting" ? "machine.workspaceStartingHint" : "machine.workspaceDownHint"),
57
+ ...detail
58
+ };
47
59
  switch (machine.status) {
48
60
  case "connected": return null;
49
61
  case "no_daemon": return {
@@ -55,7 +67,7 @@ function machineHint(machine, t) {
55
67
  ...detail
56
68
  };
57
69
  case "unsupported_agent": return {
58
- text: machine.agentVersion ? t("machine.unsupportedHintVersion", { version: machine.agentVersion }) : t("machine.unsupportedHint"),
70
+ text: tooOldHint(machine, t),
59
71
  ...detail
60
72
  };
61
73
  default: return {
@@ -62,7 +62,8 @@
62
62
  "noPreview": "No preview for this option",
63
63
  "previewOf": "Preview: {label}",
64
64
  "notesLabel": "Notes",
65
- "notesPlaceholder": "Add notes on this design…",
65
+ "notesPlaceholder": "Add notes…",
66
+ "addNotes": "Add notes",
66
67
  "notesOnly": "(notes only)",
67
68
  "todoPending": "To do",
68
69
  "todoInProgress": "In progress",
@@ -151,7 +152,9 @@
151
152
  "input": {
152
153
  "stopGenerating": "Stop generating (Escape)",
153
154
  "loadingCommands": "Loading commands…",
154
- "emoji": "Emoji"
155
+ "emoji": "Emoji",
156
+ "shellMode": "shell mode",
157
+ "shellHint": "Enter runs it on the session's machine, and its output is added to the conversation"
155
158
  },
156
159
  "thread": {
157
160
  "jumpToPrompt": "Go to message: {text}"