@sixb/cli 0.2.5 → 0.2.7

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 (67) hide show
  1. package/dist/commands/api.d.ts.map +1 -1
  2. package/dist/commands/app.d.ts.map +1 -1
  3. package/dist/commands/check.d.ts +4 -0
  4. package/dist/commands/check.d.ts.map +1 -1
  5. package/dist/commands/db-migrate.d.ts +2 -0
  6. package/dist/commands/db-migrate.d.ts.map +1 -1
  7. package/dist/commands/deploy.d.ts +0 -20
  8. package/dist/commands/deploy.d.ts.map +1 -1
  9. package/dist/commands/env.d.ts.map +1 -1
  10. package/dist/commands/worker-group.d.ts.map +1 -1
  11. package/dist/commands/worker.d.ts.map +1 -1
  12. package/dist/lib/command-line.d.ts +16 -16
  13. package/dist/lib/command-line.d.ts.map +1 -1
  14. package/dist/lib/deploy-release.d.ts.map +1 -1
  15. package/dist/lib/deploy-source.d.ts +2 -0
  16. package/dist/lib/deploy-source.d.ts.map +1 -1
  17. package/dist/lib/env-config.d.ts.map +1 -1
  18. package/dist/lib/role-lifecycle.d.ts +8 -1
  19. package/dist/lib/role-lifecycle.d.ts.map +1 -1
  20. package/dist/lib/storage-migration.d.ts +7 -0
  21. package/dist/lib/storage-migration.d.ts.map +1 -1
  22. package/dist/lib/worker-registry.d.ts +1 -0
  23. package/dist/lib/worker-registry.d.ts.map +1 -1
  24. package/dist/runner/computer-connection.d.ts.map +1 -1
  25. package/dist/tui/app.d.ts +2 -0
  26. package/dist/tui/app.d.ts.map +1 -1
  27. package/dist/tui/moves.d.ts +62 -0
  28. package/dist/tui/moves.d.ts.map +1 -0
  29. package/dist/tui/parts.d.ts.map +1 -1
  30. package/dist/tui/settings.d.ts +2 -0
  31. package/dist/tui/settings.d.ts.map +1 -1
  32. package/dist/tui/transcript.d.ts +13 -6
  33. package/dist/tui/transcript.d.ts.map +1 -1
  34. package/dist/ui/deploy-screens.d.ts +1 -26
  35. package/dist/ui/deploy-screens.d.ts.map +1 -1
  36. package/dist/ui/screens.d.ts +8 -0
  37. package/dist/ui/screens.d.ts.map +1 -1
  38. package/package.json +21 -21
  39. package/src/commands/agent.ts +1 -1
  40. package/src/commands/api.ts +20 -10
  41. package/src/commands/app.ts +15 -7
  42. package/src/commands/check.ts +87 -49
  43. package/src/commands/db-migrate.ts +25 -1
  44. package/src/commands/deploy.ts +16 -186
  45. package/src/commands/dev-runner.ts +1 -1
  46. package/src/commands/env.ts +15 -5
  47. package/src/commands/worker-group.ts +36 -9
  48. package/src/commands/worker.ts +27 -9
  49. package/src/index.tsx +11 -27
  50. package/src/lib/command-line.ts +19 -53
  51. package/src/lib/deploy-release.ts +3 -4
  52. package/src/lib/deploy-source.ts +15 -0
  53. package/src/lib/env-config.ts +1 -3
  54. package/src/lib/role-lifecycle.ts +48 -8
  55. package/src/lib/storage-migration.ts +33 -1
  56. package/src/lib/worker-registry.ts +4 -1
  57. package/src/runner/computer-connection.ts +7 -1
  58. package/src/tui/app.tsx +183 -38
  59. package/src/tui/moves.ts +221 -0
  60. package/src/tui/parts.ts +7 -0
  61. package/src/tui/settings.ts +5 -0
  62. package/src/tui/transcript.tsx +50 -14
  63. package/src/ui/deploy-screens.ts +11 -112
  64. package/src/ui/screens.ts +23 -0
  65. package/dist/lib/github-ci.d.ts +0 -49
  66. package/dist/lib/github-ci.d.ts.map +0 -1
  67. package/src/lib/github-ci.ts +0 -262
package/src/tui/app.tsx CHANGED
@@ -21,6 +21,7 @@ import {
21
21
  listAgentThreadRuns,
22
22
  listAgentThreads,
23
23
  listModels,
24
+ moveAgentThreadToCloud,
24
25
  moveAgentThreadToComputer,
25
26
  postAgentThreadMessage,
26
27
  setAgentThreadApprovalMode,
@@ -44,6 +45,7 @@ import {
44
45
  pickedAnswer,
45
46
  shortPath,
46
47
  } from "./format"
48
+ import { folderName, movedNotice, placeNames } from "./moves"
47
49
  import { Palette, type PaletteItem } from "./palette"
48
50
  import { type LiveRun, liveRun, reduceLive, type ShownMessage, shownMessage } from "./parts"
49
51
  import {
@@ -101,6 +103,8 @@ export function Workspace(props: WorkspaceProps) {
101
103
  } | null>(null)
102
104
  const [quitArmed, setQuitArmed] = useState(false)
103
105
  const draft = useRef<(() => boolean) | null>(null)
106
+ // The open conversation's moves, which take the place of where new ones start in ctrl+p.
107
+ const threadMoves = useRef<readonly PaletteItem[] | null>(null)
104
108
 
105
109
  const setSettings = useCallback((change: (current: Settings) => Settings) => {
106
110
  setSettingsState((current) => {
@@ -389,6 +393,7 @@ export function Workspace(props: WorkspaceProps) {
389
393
  threadId={screen.threadId}
390
394
  approval={approval}
391
395
  onThreadRead={readApproval}
396
+ paletteMoves={threadMoves}
392
397
  />
393
398
  )}
394
399
  {notice || quitArmed ? (
@@ -420,6 +425,7 @@ export function Workspace(props: WorkspaceProps) {
420
425
  models,
421
426
  newConversation: () => setScreen({ kind: "home" }),
422
427
  open: setDialog,
428
+ threadMoves: openThreadId === null ? null : threadMoves.current,
423
429
  setPlace: (place) => setSettings((current) => ({ ...current, place })),
424
430
  toggleKeepAwake: () =>
425
431
  setSettings((current) => {
@@ -754,6 +760,8 @@ function ThreadView(
754
760
  readonly approval: ThreadApproval | null
755
761
  /** The thread as read; `raised` after "always", which raised its mode there. */
756
762
  readonly onThreadRead: (thread: Thread, raised: boolean) => void
763
+ /** Where it lists its moves for the ctrl+p palette while it is open. */
764
+ readonly paletteMoves: { current: readonly PaletteItem[] | null }
757
765
  }
758
766
  ) {
759
767
  const { connection, runner, colors, syntax, settings, models, threadId } = props
@@ -766,26 +774,15 @@ function ThreadView(
766
774
  const [context, setContext] = useState<ContextUse | null>(null)
767
775
  const [live, setLive] = useState<LiveRun | null>(null)
768
776
  const [stopArmed, setStopArmed] = useState(false)
777
+ const [moveArmed, setMoveArmed] = useState(false)
769
778
  const [typed, setTyped] = useState("")
770
779
  const [contextOpen, setContextOpen] = useState(false)
771
780
  const [stopOpen, setStopOpen] = useState(false)
772
- // A conversation adds what only it can show: what it uses of its model's context.
773
- const slashCommands = useMemo(
774
- () => [
775
- ...props.slashCommands,
776
- {
777
- name: "context",
778
- description: "What the conversation uses of the model's context",
779
- run: () => setContextOpen(true),
780
- },
781
- ],
782
- [props.slashCommands]
783
- )
784
- const slash = useSlashMenu(slashCommands, typed)
785
781
  const scroll = useRef<ScrollBoxRenderable>(null)
786
782
 
787
783
  const mine = thread !== null && owns(connection, thread)
788
784
  const onComputer = thread?.runsOn === "computer"
785
+ const names = placeNames({ thread, computerId: props.computerId, mine })
789
786
 
790
787
  // The commands its turns left running here, ticking while they run.
791
788
  const [background, setBackground] = useState<readonly BackgroundCommand[]>(() =>
@@ -871,18 +868,134 @@ function ThreadView(
871
868
  if (activeRunId && runsHere && queued && continueFromWeb)
872
869
  void runner.claimElsewhere(activeRunId)
873
870
  }, [activeRunId, runsHere, queued, continueFromWeb, runner])
871
+
872
+ // A turn holds the conversation where it is: say what frees it, not the server's words.
873
+ const moveRefused = (error: unknown) => {
874
+ const busy =
875
+ typeof error === "object" &&
876
+ error !== null &&
877
+ "code" in error &&
878
+ error.code === "active_run_exists"
879
+ if (!busy) return props.say(errorText(error), "error")
880
+
881
+ props.say(
882
+ approvals.length > 0
883
+ ? "A request waits for your approval: answer it, then press ctrl+g twice to move it."
884
+ : "A reply is running: press esc twice to stop it, then ctrl+g twice to move it.",
885
+ "error"
886
+ )
887
+ }
888
+
874
889
  // Its turns move to this computer, which alone claims them from now on, and to this folder.
875
890
  const moveHere = async () => {
891
+ const moved = movedNotice(names, runs, { id: props.computerId, folder: runner.folder })
876
892
  const { error } = await moveAgentThreadToComputer({
877
893
  client,
878
894
  path: { threadId },
879
895
  body: { computer: { ...(await thisComputer()), folder: runner.folder } },
880
896
  })
881
- if (error) return props.say(errorText(error), "error")
897
+ if (error) return moveRefused(error)
898
+
899
+ void reload()
900
+ props.say(moved)
901
+ }
902
+ // And back to the cloud, from where they can move anywhere again.
903
+ const moveCloud = async () => {
904
+ const moved = movedNotice(names, runs, "cloud")
905
+ const { error } = await moveAgentThreadToCloud({ client, path: { threadId } })
906
+ if (error) return moveRefused(error)
907
+
882
908
  void reload()
883
- props.say("Its turns now run in this folder")
909
+ props.say(moved)
910
+ }
911
+
912
+ const canMoveHere = mine && (!onComputer || !here)
913
+ const canMoveCloud = mine && onComputer
914
+
915
+ // In a conversation, /here and /cloud move it: they take the place of the home screen's, which
916
+ // choose where new conversations start. One that cannot move says why, and the word never
917
+ // reaches the agent.
918
+ const moveCommand = (
919
+ name: string,
920
+ to: string,
921
+ canMove: boolean,
922
+ move: () => Promise<void>
923
+ ): SlashCommand => {
924
+ // Not read yet, it cannot tell where it runs or whose it is: it moves nothing until then.
925
+ if (thread === null) {
926
+ return {
927
+ name,
928
+ description: `Move this conversation to ${to}`,
929
+ run: () => props.say("Reading the conversation…"),
930
+ }
931
+ }
932
+
933
+ const description = canMove
934
+ ? `Move this conversation to ${to}`
935
+ : mine
936
+ ? `This conversation runs in ${to}`
937
+ : "Only its owner can move this conversation"
938
+
939
+ return { name, description, run: () => (canMove ? void move() : props.say(description)) }
940
+ }
941
+ const hereCommand = moveCommand("here", "this folder", canMoveHere, moveHere)
942
+ const cloudCommand = moveCommand("cloud", "the cloud", canMoveCloud, moveCloud)
943
+ const moveCommands: Readonly<Record<string, SlashCommand>> = {
944
+ here: hereCommand,
945
+ cloud: cloudCommand,
946
+ }
947
+
948
+ // ctrl+g moves it where its hint says: to this folder, else back to the cloud. Pressed twice, as
949
+ // esc stops a turn: leaving the cloud retires its paused sub-agents' workspaces there, which a
950
+ // stray key must not do. Not ctrl+f, which the composer reads as a step to the right.
951
+ const keyMove = canMoveHere
952
+ ? { name: "here", to: "this folder", move: moveHere }
953
+ : canMoveCloud
954
+ ? { name: "cloud", to: "the cloud", move: moveCloud }
955
+ : null
956
+ const moveByKey = () => {
957
+ if (!keyMove) return
958
+ if (!moveArmed) {
959
+ setMoveArmed(true)
960
+ setTimeout(() => setMoveArmed(false), 2_000)
961
+ return
962
+ }
963
+
964
+ setMoveArmed(false)
965
+ void keyMove.move()
884
966
  }
885
967
 
968
+ // The ctrl+p palette lists the moves that can happen now, in the slash menu's words. Why the
969
+ // others can't is the slash menu's to say, where each command's name tells them apart.
970
+ const { paletteMoves } = props
971
+ useEffect(() => {
972
+ const possible = [
973
+ ...(canMoveHere ? [hereCommand] : []),
974
+ ...(canMoveCloud ? [cloudCommand] : []),
975
+ ]
976
+ paletteMoves.current = possible.map((command) => ({
977
+ id: command.name,
978
+ group: "Agent",
979
+ label: command.description,
980
+ ...(command.name === keyMove?.name ? { keys: "ctrl+g" } : {}),
981
+ run: command.run,
982
+ }))
983
+ return () => {
984
+ paletteMoves.current = null
985
+ }
986
+ })
987
+
988
+ // A conversation adds what only it can show: its context use.
989
+ const slashCommands = [
990
+ ...props.slashCommands.map((command) => moveCommands[command.name] ?? command),
991
+ {
992
+ name: "context",
993
+ description: "What the conversation uses of the model's context",
994
+ run: () => setContextOpen(true),
995
+ },
996
+ ]
997
+ const slash = useSlashMenu(slashCommands, typed)
998
+
886
999
  useEffect(() => {
887
1000
  void reload()
888
1001
  const socket = createAgentActivitySocket({
@@ -992,9 +1105,7 @@ function ThreadView(
992
1105
  if (key.name === "down" && slash.move(1)) return
993
1106
  if (key.name === "escape") return void stop()
994
1107
  if (key.ctrl && key.name === "x" && runningBackground.length > 0) return setStopOpen(true)
995
- if (key.ctrl && key.name === "f" && onComputer && mine && !here) {
996
- return void moveHere()
997
- }
1108
+ if (key.ctrl && key.name === "g") return moveByKey()
998
1109
  if (key.name === "pageup") scroll.current?.scrollBy(-10)
999
1110
  if (key.name === "pagedown") scroll.current?.scrollBy(10)
1000
1111
  })
@@ -1003,7 +1114,7 @@ function ThreadView(
1003
1114
  if (slash.run(text)) return true
1004
1115
  if (!thread) return false
1005
1116
  if (onComputer && mine && !here) {
1006
- props.say("It works in another folder: press ctrl+f to move it here first.")
1117
+ props.say("It works in another folder: press ctrl+g twice to move it here first.")
1007
1118
  return false
1008
1119
  }
1009
1120
  const { data, error } = await postAgentThreadMessage({
@@ -1067,6 +1178,13 @@ function ThreadView(
1067
1178
  : placeStatus
1068
1179
  const runningSince = runs.find((run) => run.id === activeRunId)?.startedAt
1069
1180
 
1181
+ // The footer names the move ctrl+g makes, in the room its folder leaves: the panel's padding,
1182
+ // the hint itself and `ctrl+p commands` take 46 columns. From another folder the notice above
1183
+ // the composer names it instead.
1184
+ const footerMove =
1185
+ width < 64 || !mine ? null : !onComputer ? "move here" : here ? "move to cloud" : null
1186
+ const footerFolder = footerMove ? Math.max(12, Math.min(36, width - 46)) : 36
1187
+
1070
1188
  return (
1071
1189
  <box flexDirection="column" flexGrow={1} paddingTop={1}>
1072
1190
  <box
@@ -1106,7 +1224,8 @@ function ThreadView(
1106
1224
  live={live}
1107
1225
  approvals={approvals}
1108
1226
  modelNames={modelNames}
1109
- computerRuns={onComputer}
1227
+ thread={thread}
1228
+ names={names}
1110
1229
  width={width}
1111
1230
  approvalChoice={choice}
1112
1231
  {...(onComputer ? { folder: runner.folder } : {})}
@@ -1128,6 +1247,7 @@ function ThreadView(
1128
1247
  onComputer,
1129
1248
  mine,
1130
1249
  here,
1250
+ ...(place ? { folder: folderName(place.folder) } : {}),
1131
1251
  continueFromWeb,
1132
1252
  runningHere: activeRunId !== null && runner.runs(activeRunId),
1133
1253
  })
@@ -1151,9 +1271,9 @@ function ThreadView(
1151
1271
  ? `It runs on ${clip(place.name, 40)}.`
1152
1272
  : `It works in ${shortPath(place.folder, homedir(), 48)}.`}
1153
1273
  </span>
1154
- <span fg={colors.muted}>{" Its turns run there: "}</span>
1155
- <span fg={colors.text}>ctrl+f</span>
1156
- <span fg={colors.muted}> moves it to this folder</span>
1274
+ <span fg={colors.muted}>{" Press "}</span>
1275
+ <span fg={colors.text}>ctrl+g</span>
1276
+ <span fg={colors.muted}> twice to move it to this folder.</span>
1157
1277
  </text>
1158
1278
  ) : null}
1159
1279
  {runningBackground.length > 0 ? (
@@ -1175,10 +1295,27 @@ function ThreadView(
1175
1295
  onSubmit={send}
1176
1296
  />
1177
1297
  <box flexDirection="row" justifyContent="space-between" paddingTop={1}>
1178
- <text fg={colors.faint}>
1179
- {onComputer ? shortPath(runner.folder, homedir(), 40) : "Runs in the cloud"}
1180
- </text>
1181
- <text>
1298
+ {moveArmed && keyMove ? (
1299
+ <text wrapMode="none" flexShrink={1} fg={colors.amber}>
1300
+ {`Press ctrl+g again to move it to ${keyMove.to}`}
1301
+ </text>
1302
+ ) : (
1303
+ <text wrapMode="none" flexShrink={1}>
1304
+ <span fg={colors.faint}>
1305
+ {onComputer
1306
+ ? shortPath(runner.folder, homedir(), footerFolder)
1307
+ : "Runs in the cloud"}
1308
+ </span>
1309
+ {footerMove ? (
1310
+ <>
1311
+ <span fg={colors.faint}>{" · "}</span>
1312
+ <span fg={colors.text}>ctrl+g</span>
1313
+ <span fg={colors.muted}>{` ${footerMove}`}</span>
1314
+ </>
1315
+ ) : null}
1316
+ </text>
1317
+ )}
1318
+ <text flexShrink={0}>
1182
1319
  <span fg={colors.text}>ctrl+p</span>
1183
1320
  <span fg={colors.muted}> commands</span>
1184
1321
  </text>
@@ -1258,6 +1395,8 @@ export function activityLabel(
1258
1395
  readonly onComputer: boolean
1259
1396
  readonly mine: boolean
1260
1397
  readonly here: boolean
1398
+ /** The name of the folder it works in, when it isn't this one. */
1399
+ readonly folder?: string
1261
1400
  readonly continueFromWeb: boolean
1262
1401
  readonly runningHere: boolean
1263
1402
  }
@@ -1266,7 +1405,9 @@ export function activityLabel(
1266
1405
  if (run?.status === "queued") {
1267
1406
  if (!where.onComputer) return "Starting"
1268
1407
  if (!where.mine) return "Waiting for its owner's computer"
1269
- if (!where.here) return "Waiting for sixb in its folder"
1408
+ if (!where.here) {
1409
+ return where.folder ? `Waiting for ${where.folder} to be available` : "Waiting for its folder"
1410
+ }
1270
1411
  return where.continueFromWeb || where.runningHere
1271
1412
  ? "Starting"
1272
1413
  : "Waiting: messages from the web are off"
@@ -1323,6 +1464,8 @@ function commandItems(input: {
1323
1464
  readonly newConversation: () => void
1324
1465
  readonly open: (dialog: Dialog) => void
1325
1466
  readonly setPlace: (place: Settings["place"]) => void
1467
+ /** In a conversation, its moves, in place of where new conversations run. */
1468
+ readonly threadMoves: readonly PaletteItem[] | null
1326
1469
  /** Whether your messages from the web, or another device, run in this folder. */
1327
1470
  readonly toggleContinueFromWeb: () => void
1328
1471
  readonly toggleKeepAwake: () => void
@@ -1369,16 +1512,18 @@ function commandItems(input: {
1369
1512
  keys: "shift+tab",
1370
1513
  run: () => input.open("permissions"),
1371
1514
  },
1372
- {
1373
- id: "place",
1374
- group: "Agent",
1375
- label:
1376
- settings.place === "computer"
1377
- ? "Run new conversations in the cloud"
1378
- : `Run new conversations on ${placeLabel("computer")}`,
1379
- keys: "tab",
1380
- run: () => input.setPlace(settings.place === "computer" ? "cloud" : "computer"),
1381
- },
1515
+ ...(input.threadMoves ?? [
1516
+ {
1517
+ id: "place",
1518
+ group: "Agent",
1519
+ label:
1520
+ settings.place === "computer"
1521
+ ? "Run new conversations in the cloud"
1522
+ : `Run new conversations on ${placeLabel("computer")}`,
1523
+ keys: "tab",
1524
+ run: () => input.setPlace(settings.place === "computer" ? "cloud" : "computer"),
1525
+ },
1526
+ ]),
1382
1527
  {
1383
1528
  id: "continue-from-web",
1384
1529
  group: "Agent",
@@ -0,0 +1,221 @@
1
+ import type { GetAgentThreadResponses, ListAgentThreadRunsResponses } from "@sixb/client"
2
+ import { computerName, computerNameInText } from "./settings"
3
+
4
+ // Where each turn ran, and the moves between them, by the rule the app's transcript follows
5
+ // (`packages/app/src/agent/moves.ts`) and the core tells the model by (`detectMovedMessages`). The
6
+ // app's helper is not on a surface the CLI imports: it belongs to the web chat, with its catalog
7
+ // of strings and its components. This keeps the same rule on every case (retries, a round trip
8
+ // with no turn, forks, a participant, a folder not shown) and the app's English words.
9
+ //
10
+ // Two differences, on purpose. This computer is named as the terminal names it everywhere ("This
11
+ // Mac"), not by its own name. And the terminal knows the names of this computer and of the one
12
+ // the conversation runs on now, not the list of every connected computer the app reads, so a move
13
+ // to another of the person's computers may read without that computer's name.
14
+
15
+ type Run = ListAgentThreadRunsResponses[200]["runs"][number]
16
+ type Thread = GetAgentThreadResponses[200]
17
+
18
+ /**
19
+ * Where a turn runs: the cloud, or a folder on one of its owner's computers. A computer's turn
20
+ * whose folder the reader isn't shown (someone else's conversation, or one from before folders
21
+ * were kept) is "computer".
22
+ */
23
+ export type TurnPlace = "cloud" | "computer" | { readonly id: string; readonly folder: string }
24
+
25
+ /** What places are named with, beyond what the runs hold. */
26
+ export interface PlaceNames {
27
+ /** Each computer's name, by id, where the reader knows it: runs keep only the id. */
28
+ readonly computerNames: ReadonlyMap<string, string>
29
+ /** This computer, named as a label ("This Mac") and lowercased in a sentence ("this Mac"). */
30
+ readonly thisComputerId: string
31
+ /** Whose computer it is, for someone they shared it with, who isn't shown its folders. */
32
+ readonly ownerName: string | null
33
+ }
34
+
35
+ /** The names the terminal knows: this computer's, the conversation's computer's, its owner's. */
36
+ export function placeNames(input: {
37
+ readonly thread: Pick<Thread, "computer" | "owner" | "ownerPrincipal"> | null
38
+ readonly computerId: string
39
+ readonly mine: boolean
40
+ }): PlaceNames {
41
+ const { thread, computerId, mine } = input
42
+
43
+ const computerNames = new Map<string, string>()
44
+ if (thread?.computer) computerNames.set(thread.computer.id, thread.computer.name)
45
+ computerNames.set(computerId, computerName())
46
+
47
+ const ownerName = thread && !mine ? (thread.owner.displayName ?? thread.ownerPrincipal.id) : null
48
+ return { computerNames, thisComputerId: computerId, ownerName }
49
+ }
50
+
51
+ export function runPlace(run: Run): TurnPlace {
52
+ if (run.runsOn !== "computer") return "cloud"
53
+ return run.computer ?? "computer"
54
+ }
55
+
56
+ function threadPlace(thread: Pick<Thread, "runsOn" | "computer">): TurnPlace {
57
+ if (thread.runsOn !== "computer") return "cloud"
58
+ return thread.computer ? { id: thread.computer.id, folder: thread.computer.folder } : "computer"
59
+ }
60
+
61
+ function samePlace(a: TurnPlace, b: TurnPlace): boolean {
62
+ if (typeof a === "string" || typeof b === "string") return a === b
63
+ return a.id === b.id && a.folder === b.folder
64
+ }
65
+
66
+ /** A folder by its name, as a place reads in running text. */
67
+ export function folderName(folder: string): string {
68
+ return folder.split("/").filter(Boolean).at(-1) ?? folder
69
+ }
70
+
71
+ /** A folder's path, a home folder written `~`: for two folders whose names are alike. */
72
+ function shownFolder(folder: string): string {
73
+ return folder.replace(/^\/(?:Users|home)\/[^/]+(?=\/|$)/, "~")
74
+ }
75
+
76
+ /** A folder by its name, on its computer when that's known. */
77
+ function folderPlace(names: PlaceNames, place: { readonly id: string; readonly folder: string }) {
78
+ const computer =
79
+ place.id === names.thisComputerId ? computerNameInText() : names.computerNames.get(place.id)
80
+ const folder = folderName(place.folder)
81
+ return computer ? `${folder} on ${computer}` : folder
82
+ }
83
+
84
+ /** A place under a turn's answer: `Cloud`, or its computer and folder. */
85
+ export function placeName(place: TurnPlace, names: PlaceNames): string {
86
+ if (place === "cloud") return "Cloud"
87
+ if (place === "computer") return names.ownerName ? `${names.ownerName}'s computer` : "Computer"
88
+
89
+ const computer = names.computerNames.get(place.id) ?? "Another computer"
90
+ return `${computer} · ${folderName(place.folder)}`
91
+ }
92
+
93
+ /** Where it went, and which files stay behind: `from` is unknown before any turn. */
94
+ export function movedLine(names: PlaceNames, from: TurnPlace | undefined, to: TurnPlace): string {
95
+ if (to === "cloud") {
96
+ return typeof from === "object"
97
+ ? `Moved to the cloud. The files in ${folderName(from.folder)} stay behind.`
98
+ : "Moved to the cloud. The computer's files stay behind."
99
+ }
100
+
101
+ if (to === "computer") {
102
+ if (from !== "cloud") return "Moved to another folder. Earlier files stay where they were."
103
+
104
+ const computer = names.ownerName ? `${names.ownerName}'s computer` : "the computer"
105
+ return `Moved to ${computer}. The cloud's files stay behind.`
106
+ }
107
+
108
+ if (from === "cloud") return `Moved to ${folderPlace(names, to)}. The cloud's files stay behind.`
109
+
110
+ // Another folder on the same computer: by their names, or their paths where the names are alike.
111
+ if (typeof from === "object" && from.id === to.id) {
112
+ const alike = folderName(from.folder) === folderName(to.folder)
113
+ const shown = alike ? shownFolder : folderName
114
+ const left = shown(from.folder)
115
+ return `Moved from ${left} to ${shown(to.folder)}. The files in ${left} stay behind.`
116
+ }
117
+ return `Moved to ${folderPlace(names, to)}. Earlier files stay where they were.`
118
+ }
119
+
120
+ /** Where a fork's first turn runs: a workspace of its own, apart from its source's. */
121
+ function continuedLine(names: PlaceNames, to: TurnPlace): string {
122
+ const files = "The original conversation's files stay with it."
123
+ if (to === "cloud") return `Continued in the cloud. ${files}`
124
+ if (to === "computer") {
125
+ const computer = names.ownerName ? `${names.ownerName}'s computer` : "the computer"
126
+ return `Continued on ${computer}. ${files}`
127
+ }
128
+ return `Continued in ${folderPlace(names, to)}. ${files}`
129
+ }
130
+
131
+ /**
132
+ * Every move of the conversation, by the shown message it follows: where a turn runs elsewhere
133
+ * than the turn before it, and where it runs now when no turn ran since it moved. Moving back
134
+ * before a turn ran is no move: the workspace is the one the last turn worked in. A fork's first
135
+ * turn starts in a workspace of its own.
136
+ */
137
+ export function transcriptMoves(input: {
138
+ readonly thread: Pick<Thread, "runsOn" | "computer" | "movedAt" | "createdAt"> | null
139
+ /** Oldest first, a resumed turn's trigger included. */
140
+ readonly messages: readonly {
141
+ readonly id: string
142
+ readonly role: "user" | "assistant"
143
+ readonly createdAt: string
144
+ }[]
145
+ /** The messages the transcript draws: a move line follows one of them. */
146
+ readonly shownIds: ReadonlySet<string>
147
+ /** Oldest first. */
148
+ readonly runs: readonly Run[]
149
+ readonly names: PlaceNames
150
+ }): ReadonlyMap<string, string> {
151
+ const { thread, messages, shownIds, runs, names } = input
152
+
153
+ // A retried turn runs again for the same message: its latest run is where it went.
154
+ const placeByTrigger = new Map<string, TurnPlace>()
155
+ for (const run of runs) {
156
+ if (run.kind === "conversation") placeByTrigger.set(run.triggerMessageId, runPlace(run))
157
+ }
158
+
159
+ const moves = new Map<string, string>()
160
+ let previous: TurnPlace | undefined
161
+ let lastShownId: string | null = null
162
+ // A fork's copies keep the time they were written, before the fork was: they ran in its source,
163
+ // so its first turn here starts elsewhere. A message written here with no run, such as one
164
+ // steering a turn, decides nothing.
165
+ let forked = false
166
+
167
+ for (const message of messages) {
168
+ const place = message.role === "user" ? placeByTrigger.get(message.id) : undefined
169
+ const copied = thread !== null && message.createdAt < thread.createdAt
170
+ if (message.role === "user" && copied && previous === undefined) forked = true
171
+
172
+ if (place !== undefined) {
173
+ const line =
174
+ previous === undefined
175
+ ? forked && continuedLine(names, place)
176
+ : !samePlace(previous, place) && movedLine(names, previous, place)
177
+ if (line && lastShownId !== null) moves.set(lastShownId, line)
178
+ previous = place
179
+ }
180
+
181
+ if (shownIds.has(message.id)) lastShownId = message.id
182
+ }
183
+
184
+ if (!thread) return moves
185
+ const current = threadPlace(thread)
186
+
187
+ // A fork no turn ran in yet continues after its last copy. Not by `movedAt`: a fork keeps its
188
+ // source's, which may predate some of the copies, or all of them. A conversation no turn ran in
189
+ // yet, and no fork, starts where it is.
190
+ if (previous === undefined) {
191
+ if (forked && lastShownId !== null) moves.set(lastShownId, continuedLine(names, current))
192
+ return moves
193
+ }
194
+
195
+ // Where it runs now, if no turn ran there since it moved: after the last message from before
196
+ // the move.
197
+ const movedAt = thread.movedAt
198
+ if (movedAt === undefined || samePlace(previous, current)) return moves
199
+
200
+ const afterId = messages
201
+ .filter((message) => message.createdAt <= movedAt && shownIds.has(message.id))
202
+ .at(-1)?.id
203
+ if (afterId) moves.set(afterId, movedLine(names, previous, current))
204
+ return moves
205
+ }
206
+
207
+ /**
208
+ * What a move says once done, by the transcript's rule: from where its last turn ran, the latest
209
+ * run of the latest message. Back there with no turn in between, nothing moved. `to` is the cloud,
210
+ * or the folder this `sixb` is open in.
211
+ */
212
+ export function movedNotice(names: PlaceNames, runs: readonly Run[], to: TurnPlace): string {
213
+ const destination = to === "cloud" ? "the cloud" : "this folder"
214
+ const lastRun = [...runs].reverse().find((run) => run.kind === "conversation")
215
+
216
+ if (!lastRun) return `Moved to ${destination}.`
217
+ if (samePlace(runPlace(lastRun), to)) {
218
+ return `Back in ${destination}. Nothing moved since its last reply.`
219
+ }
220
+ return movedLine(names, runPlace(lastRun), to)
221
+ }
package/src/tui/parts.ts CHANGED
@@ -169,6 +169,13 @@ export function reduceLive(state: LiveRun, event: AgentRunStreamEvent): LiveRun
169
169
  return { ...state, compacting: false, contextFull: false }
170
170
  case "agent.ui.chunk":
171
171
  return reduceChunk(state, event.chunk)
172
+ case "agent.ui.steps": {
173
+ // The turn's finished steps, sent first to a reader who opened it late, replace what it
174
+ // rebuilt so far. What streams next opens a step of its own, even when its start was dropped.
175
+ let next: LiveRun = { ...state, working: true, parts: [], keys: [], step: 0, stepStart: 0 }
176
+ for (const chunk of event.chunks) next = reduceChunk(next, chunk)
177
+ return { ...next, step: next.step + 1, stepStart: next.parts.length }
178
+ }
172
179
  case "agent.message.finalized":
173
180
  return { ...state, finalized: true }
174
181
  case "agent.run.finished":
@@ -68,6 +68,11 @@ export function computerName(): string {
68
68
  return osType() === "Darwin" ? "This Mac" : "This computer"
69
69
  }
70
70
 
71
+ /** The same name inside a sentence, as in "Moved to sixb on this Mac." */
72
+ export function computerNameInText(): string {
73
+ return osType() === "Darwin" ? "this Mac" : "this computer"
74
+ }
75
+
71
76
  export function placeLabel(place: Place): string {
72
77
  return place === "computer" ? computerName() : "Cloud"
73
78
  }