moi-computer 0.12.1-next.3 → 0.12.1-next.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (224) hide show
  1. package/bunfig.toml +1 -5
  2. package/client/api/app-config.ts +1 -2
  3. package/client/api/app-ui-keys.ts +3 -1
  4. package/client/api/http.ts +4 -1
  5. package/client/api/workspace-keys.ts +3 -3
  6. package/client/app/app-provider.tsx +22 -2
  7. package/client/app/app-router.tsx +4 -0
  8. package/client/app/routes/home-route.tsx +14 -0
  9. package/client/app/routes/onboarding-route.tsx +11 -0
  10. package/client/app/routes/settings-route.tsx +1 -1
  11. package/client/app/routes/workspace-route.tsx +21 -19
  12. package/client/components/shared/screen-shell.tsx +23 -0
  13. package/client/components/shared/ultrafast-icon.tsx +61 -0
  14. package/client/components/shared/workspace-icon.tsx +2 -2
  15. package/client/features/account/account-test-utils.tsx +4 -3
  16. package/client/features/app-settings/api.ts +33 -0
  17. package/client/features/app-settings/general-settings.tsx +39 -0
  18. package/client/features/{settings → app-settings}/settings-page.tsx +5 -1
  19. package/client/features/chat/applet-chat-intents.ts +4 -20
  20. package/client/features/chat/chat-send.ts +17 -13
  21. package/client/features/chat/composer/model-order.ts +8 -4
  22. package/client/features/chat/composer/model-picker.tsx +88 -34
  23. package/client/features/chat/composer/use-workspace-composer-state.tsx +11 -3
  24. package/client/features/chat/composer/use-workspace-skill-updates.ts +1 -1
  25. package/client/features/chat/connection/chat-frames.ts +3 -15
  26. package/client/features/chat/messages/chat-empty-state.tsx +1 -4
  27. package/client/features/chat/messages/fork-history.ts +5 -5
  28. package/client/features/chat/messages/turn-view.tsx +3 -12
  29. package/client/features/chat/messages/user-message-bubble.tsx +63 -0
  30. package/client/features/chat/sessions/api.ts +121 -80
  31. package/client/features/chat/sessions/browser-tab-state.ts +14 -12
  32. package/client/features/chat/sessions/chat-selector.tsx +5 -6
  33. package/client/features/chat/sessions/use-selected-session.ts +172 -257
  34. package/client/features/chat/use-chat.ts +14 -19
  35. package/client/features/collab/workspace-users-menu.tsx +1 -1
  36. package/client/features/demo/demo-dialog.tsx +11 -3
  37. package/client/features/dev/dev-index-page.tsx +5 -0
  38. package/client/features/dev/dev-routes.tsx +2 -0
  39. package/client/features/dev/harness-debug-page.tsx +3 -3
  40. package/client/features/dev/onboarding-preview-page.tsx +76 -0
  41. package/client/features/dev/workspace-setup-mocks.ts +16 -14
  42. package/client/features/dev/workspace-setup-preview.tsx +11 -17
  43. package/client/features/harness/api.ts +45 -15
  44. package/client/features/harness/device-login-steps.tsx +9 -3
  45. package/client/features/harness/harness-login-panel.tsx +228 -0
  46. package/client/features/harness/harness-presentation.ts +15 -0
  47. package/client/features/harness/harness-selector.tsx +119 -0
  48. package/client/features/harness/use-harness-login.ts +77 -0
  49. package/client/features/home/home-page.tsx +7 -7
  50. package/client/features/onboarding/agent-setup-step.tsx +57 -0
  51. package/client/features/onboarding/api.ts +42 -0
  52. package/client/features/onboarding/onboarding-idea-disk.tsx +129 -0
  53. package/client/features/onboarding/onboarding-page.tsx +32 -0
  54. package/client/features/onboarding/onboarding-screen.tsx +20 -0
  55. package/client/features/onboarding/onboarding-status.tsx +24 -0
  56. package/client/features/onboarding/workspace-setup-step.tsx +239 -0
  57. package/client/features/overview/default/registry.tsx +2 -2
  58. package/client/features/overview/default/views-widget.tsx +6 -5
  59. package/client/features/overview/overview.tsx +9 -11
  60. package/client/features/overview/widget-canvas.tsx +1 -1
  61. package/client/features/overview/workspace-name.tsx +2 -2
  62. package/client/features/views/api.ts +69 -16
  63. package/client/features/views/pending-view-screen.tsx +30 -9
  64. package/client/features/views/use-view-actions.ts +19 -20
  65. package/client/features/views/view-presentation.ts +2 -2
  66. package/client/features/views/view-submission.ts +28 -41
  67. package/client/features/workspace/add/api.ts +3 -23
  68. package/client/features/workspace/add/create-workspace-dialog.tsx +6 -5
  69. package/client/features/workspace/add/import-workspace-dialog.tsx +3 -3
  70. package/client/features/workspace/add/workspace-agent-options.ts +11 -12
  71. package/client/features/workspace/add/workspace-agent-selector.tsx +3 -132
  72. package/client/features/workspace/api.ts +32 -36
  73. package/client/features/workspace/harness-transition.ts +88 -0
  74. package/client/features/{settings → workspace/settings}/api.ts +1 -34
  75. package/client/features/{settings → workspace/settings}/general-settings.tsx +76 -2
  76. package/client/features/{settings → workspace/settings}/use-env-vars.ts +1 -1
  77. package/client/features/{settings → workspace/settings}/workspace-icon-picker.tsx +3 -3
  78. package/client/features/workspace/setup/workspace-login-state.tsx +37 -156
  79. package/client/features/workspace/setup/workspace-recovery-states.tsx +0 -12
  80. package/client/features/workspace/setup/workspace-setup.tsx +34 -14
  81. package/client/features/workspace/setup/workspace-welcome-state.tsx +2 -2
  82. package/client/features/workspace/use-workspace-navigation.ts +25 -9
  83. package/client/features/workspace/workspace-content.tsx +11 -8
  84. package/client/features/workspace/workspace-frame.tsx +2 -1
  85. package/client/features/workspace/workspace-layout-context.tsx +5 -2
  86. package/client/features/workspace/workspace-menu.tsx +12 -8
  87. package/client/features/workspace/workspace-pending-screen.tsx +10 -10
  88. package/client/features/workspace/workspace-presentation.tsx +5 -13
  89. package/client/features/workspace/workspace-state.ts +169 -0
  90. package/client/features/workspace/workspace-tabs.tsx +22 -2
  91. package/client/index.css +2 -0
  92. package/client/index.tsx +3 -3
  93. package/client/lib/flags.ts +1 -1
  94. package/client/lib/{app-icon-registry.ts → identity-icon-registry.ts} +10 -14
  95. package/client/runtime/workspace-events.ts +21 -4
  96. package/client/runtime/workspace-theme.ts +9 -0
  97. package/client/tailwind-config.ts +43 -0
  98. package/dist/chunk-0mjzg20x.js +2 -0
  99. package/dist/chunk-2y59vrxg.js +5 -0
  100. package/dist/chunk-58dyt9bk.js +2 -0
  101. package/dist/chunk-5aj040e5.css +1 -0
  102. package/dist/{chunk-tqd3pkg8.css → chunk-60q353jq.css} +1 -1
  103. package/dist/chunk-71fn6rmj.js +546 -0
  104. package/dist/chunk-7yg1m8hx.js +2 -0
  105. package/dist/chunk-825hys3h.js +1 -0
  106. package/dist/chunk-9bb8m9rx.js +2 -0
  107. package/dist/{chunk-11cab5dq.css → chunk-cq2r11fg.css} +1 -1
  108. package/dist/chunk-g0tcwqxc.js +1 -0
  109. package/dist/chunk-n0nab26d.js +1 -0
  110. package/dist/chunk-nf2wkvf9.js +14 -0
  111. package/dist/chunk-v88n1bay.js +74 -0
  112. package/dist/index.html +1 -1
  113. package/lib/harness-instructions.ts +1 -1
  114. package/lib/{app-icons.ts → identity-icons.ts} +6 -7
  115. package/lib/onboarding.ts +41 -0
  116. package/lib/speed-mode.ts +5 -0
  117. package/lib/types.ts +71 -62
  118. package/lib/view-build-directives.ts +1 -2
  119. package/lib/workspace-icon.ts +2 -2
  120. package/lib/workspace-layout.ts +1 -1
  121. package/lib/workspace-name.ts +15 -12
  122. package/package.json +9 -8
  123. package/server/README.md +27 -25
  124. package/server/api/harnesses.ts +31 -8
  125. package/server/api/index.ts +163 -126
  126. package/server/api/onboarding.ts +46 -0
  127. package/server/app-config.ts +16 -24
  128. package/server/app-settings.ts +6 -2
  129. package/server/applets/config.ts +5 -5
  130. package/server/applets/views.ts +19 -17
  131. package/server/cli/commands/init.ts +3 -3
  132. package/server/cli/commands/skill.ts +16 -17
  133. package/server/cli/commands/start.ts +8 -34
  134. package/server/cli/server.ts +18 -6
  135. package/server/collab/config.ts +1 -2
  136. package/server/collab/manager.ts +4 -1
  137. package/server/collab/skill/index.ts +1 -1
  138. package/server/control.ts +3 -3
  139. package/server/data-dir.ts +1 -1
  140. package/server/harness/README.md +24 -8
  141. package/server/harness/acp/session.ts +8 -4
  142. package/server/harness/auth.ts +14 -17
  143. package/server/harness/claude-code/NOTES.md +4 -3
  144. package/server/harness/claude-code/index.ts +2 -0
  145. package/server/harness/claude-code/models.ts +2 -20
  146. package/server/harness/claude-code/run-action.ts +123 -0
  147. package/server/harness/claude-code/session-title.ts +5 -7
  148. package/server/harness/claude-code/session.ts +24 -16
  149. package/server/harness/codex/NOTES.md +14 -9
  150. package/server/harness/codex/adapter.ts +14 -17
  151. package/server/harness/codex/index.ts +2 -0
  152. package/server/harness/codex/run-action.ts +93 -0
  153. package/server/harness/codex/session-title.ts +6 -8
  154. package/server/harness/codex/session.ts +11 -11
  155. package/server/harness/command.ts +45 -0
  156. package/server/harness/executable.ts +5 -0
  157. package/server/harness/fork.ts +6 -4
  158. package/server/harness/install.ts +49 -0
  159. package/server/harness/openclaw/session.ts +1 -2
  160. package/server/harness/run-action.ts +85 -0
  161. package/server/harness/session-title.ts +0 -46
  162. package/server/harness/types.ts +26 -2
  163. package/server/onboarding/brief.ts +29 -0
  164. package/server/onboarding/ideas.ts +48 -0
  165. package/server/onboarding/jobs.ts +291 -0
  166. package/server/onboarding/prompt.md +49 -0
  167. package/server/sessions/archive.ts +5 -14
  168. package/server/sessions/config.ts +17 -16
  169. package/server/sessions/lifecycle.ts +15 -13
  170. package/server/sessions/pending-views.ts +79 -108
  171. package/server/sessions/selected.ts +75 -161
  172. package/server/sessions/store.ts +40 -113
  173. package/server/sessions/view-sessions.ts +128 -111
  174. package/server/skills/locations.ts +82 -0
  175. package/server/skills/lock.ts +29 -0
  176. package/server/skills/template.ts +36 -25
  177. package/server/skills/update.ts +52 -5
  178. package/server/skills/version.ts +17 -22
  179. package/server/skills/workspace.ts +121 -0
  180. package/server/web.ts +22 -8
  181. package/server/workspace-state/file.ts +177 -0
  182. package/server/workspace-state/index.ts +162 -0
  183. package/server/workspace-state/legacy-migration.ts +79 -0
  184. package/server/workspace-state/lock.ts +42 -0
  185. package/server/workspace-state/types.ts +16 -0
  186. package/server/workspaces/change-harness.ts +88 -0
  187. package/server/workspaces/harness-transition.ts +29 -0
  188. package/server/workspaces/init.ts +29 -6
  189. package/server/workspaces/layout.ts +3 -0
  190. package/server/workspaces/registry.ts +27 -1
  191. package/{workspace/.claude/skills/moi-workspace → skill}/SKILL.md +7 -5
  192. package/{workspace/.claude/skills/moi-workspace → skill}/references/COLLAB.md +4 -8
  193. package/skill/references/DESIGN.md +148 -0
  194. package/{workspace/.claude/skills/moi-workspace → skill}/references/SCRATCHPAD.md +2 -2
  195. package/{workspace/.claude/skills/moi-workspace → skill}/references/UI-COMPONENTS.md +3 -0
  196. package/ui-components/badge.tsx +1 -1
  197. package/client/accept-hmr-dependencies.ts +0 -22
  198. package/client/features/workspace/setup/workspace-setup-panel.tsx +0 -18
  199. package/dist/chunk-20ynagd4.js +0 -2
  200. package/dist/chunk-42tde9a6.js +0 -1
  201. package/dist/chunk-9w543s0r.js +0 -2
  202. package/dist/chunk-a5372n61.js +0 -546
  203. package/dist/chunk-aja9y1vn.js +0 -68
  204. package/dist/chunk-fvcvrh3d.css +0 -1
  205. package/dist/chunk-gtfjw3nx.js +0 -1
  206. package/dist/chunk-j39y2aky.js +0 -14
  207. package/dist/chunk-pcnahct1.js +0 -5
  208. package/dist/chunk-pja30km3.js +0 -1
  209. package/dist/chunk-rm894bc2.js +0 -2
  210. package/dist/chunk-tevvtfbk.js +0 -10
  211. package/dist/chunk-tgt9zd0r.js +0 -2
  212. package/dist/chunk-tv5k5z00.js +0 -1
  213. package/dist/chunk-zq2ae5dx.js +0 -2
  214. package/lib/experimental.ts +0 -14
  215. package/workspace/.claude/skills/moi-workspace/references/DESIGN.md +0 -157
  216. /package/client/features/{settings → app-settings}/profile-settings.tsx +0 -0
  217. /package/client/features/{settings → app-settings}/settings-section.tsx +0 -0
  218. /package/client/features/{settings → app-settings}/users-settings.tsx +0 -0
  219. /package/client/features/{settings → workspace/settings}/connectors-settings.tsx +0 -0
  220. /package/client/features/{settings → workspace/settings}/environment-settings.tsx +0 -0
  221. /package/client/features/{settings → workspace/settings}/icon-picker-options.ts +0 -0
  222. /package/client/features/{settings → workspace/settings}/settings-layout.tsx +0 -0
  223. /package/client/features/{settings → workspace/settings}/workspace-settings-dialog.tsx +0 -0
  224. /package/{workspace/.claude/skills/moi-workspace → skill}/references/INTENTS.md +0 -0
@@ -1,37 +1,22 @@
1
- import { mkdir, rename } from 'node:fs/promises'
2
- import { join } from 'path'
1
+ import type { WorkspaceState, WorkspaceTabId } from '@/lib/types'
2
+ import { readWorkspaceState, updateWorkspaceState, type WorkspaceRef } from '../workspace-state'
3
3
 
4
- import { DATA_DIR } from '../data-dir'
5
- import type { WorkspaceSessionSelection, WorkspaceTabId } from '@/lib/types'
6
- import { isWorkspaceTabId } from '@/lib/workspace-tabs'
7
-
8
- type Selection = WorkspaceSessionSelection
9
- type Store = Record<string, Selection>
10
- const emptySelection = (): Selection => ({ selected: {}, pinned: null })
11
-
12
- export type SelectedSessionUpdate = {
13
- changed: boolean
14
- sessionId: string | null
15
- }
16
-
17
- export const DEFAULT_SELECTED_SESSION_PATH = join(DATA_DIR, 'selected-sessions.json')
18
-
19
- let storePath = DEFAULT_SELECTED_SESSION_PATH
4
+ // Temporary ids the harnesses renamed, per workspace id. A request can still
5
+ // carry the old id after the rename landed.
20
6
  const renamedSessions = new Map<string, Map<string, string>>()
21
7
 
22
- export function setSelectedSessionPath(path: string): void {
23
- storePath = path
8
+ export function forgetSessionRenames(): void {
24
9
  renamedSessions.clear()
25
10
  }
26
11
 
27
- function rememberSessionRename(workspacePath: string, from: string, to: string): void {
28
- const renames = renamedSessions.get(workspacePath) ?? new Map<string, string>()
12
+ export function rememberSessionRename(workspaceId: string, from: string, to: string): void {
13
+ const renames = renamedSessions.get(workspaceId) ?? new Map<string, string>()
29
14
  renames.set(from, to)
30
- renamedSessions.set(workspacePath, renames)
15
+ renamedSessions.set(workspaceId, renames)
31
16
  }
32
17
 
33
- export function resolveRenamedSession(workspacePath: string, sessionId: string): string {
34
- const renames = renamedSessions.get(workspacePath)
18
+ export function resolveRenamedSession(workspaceId: string, sessionId: string): string {
19
+ const renames = renamedSessions.get(workspaceId)
35
20
  const seen = new Set<string>()
36
21
  while (renames?.has(sessionId) && !seen.has(sessionId)) {
37
22
  seen.add(sessionId)
@@ -40,160 +25,89 @@ export function resolveRenamedSession(workspacePath: string, sessionId: string):
40
25
  return sessionId
41
26
  }
42
27
 
43
- async function readStore(): Promise<Store> {
44
- try {
45
- const parsed: unknown = JSON.parse(await Bun.file(storePath).text())
46
- if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return {}
47
-
48
- const store: Store = {}
49
- for (const [workspacePath, value] of Object.entries(parsed)) {
50
- if (value === null || typeof value === 'string') {
51
- store[workspacePath] = { selected: value === null ? {} : { overview: value }, pinned: null }
52
- } else if (value && typeof value === 'object' && 'selected' in value) {
53
- const entry = value as Selection
54
- store[workspacePath] = {
55
- selected: Object.fromEntries(
56
- Object.entries(entry.selected ?? {}).filter(([, id]) => typeof id === 'string')
57
- ),
58
- pinned: typeof entry.pinned === 'string' ? entry.pinned : null
59
- }
60
- }
61
- }
62
- return store
63
- } catch {
64
- return {}
65
- }
66
- }
67
-
68
- async function writeStore(store: Store): Promise<void> {
69
- await mkdir(join(storePath, '..'), { recursive: true })
70
- const temporaryPath = `${storePath}.${process.pid}.tmp`
71
- await Bun.write(temporaryPath, JSON.stringify(store, null, 2))
72
- await rename(temporaryPath, storePath)
28
+ export async function getPinnedSession(ws: WorkspaceRef): Promise<string | undefined> {
29
+ return (await readWorkspaceState(ws)).pinnedSessionId
73
30
  }
74
31
 
75
- let writeChain: Promise<unknown> = Promise.resolve()
76
-
77
- function locked<T>(operation: () => Promise<T>): Promise<T> {
78
- const run = writeChain.then(operation, operation)
79
- writeChain = run.catch(() => {})
80
- return run
32
+ export async function pinSession(ws: WorkspaceRef, sessionId: string): Promise<void> {
33
+ await updateWorkspaceState(ws, state => {
34
+ state.pinnedSessionId = resolveRenamedSession(ws.id, sessionId)
35
+ })
81
36
  }
82
37
 
83
- export async function getSelectedSession(
84
- workspacePath: string,
85
- tabId: WorkspaceTabId = 'overview'
86
- ): Promise<string | undefined> {
87
- const store = await readStore()
88
- return store[workspacePath]?.selected[tabId] ?? undefined
38
+ // Unpins only when `sessionId` is still the pinned chat.
39
+ export async function unpinSession(ws: WorkspaceRef, sessionId: string): Promise<void> {
40
+ await updateWorkspaceState(ws, state => {
41
+ if (state.pinnedSessionId === resolveRenamedSession(ws.id, sessionId))
42
+ delete state.pinnedSessionId
43
+ })
89
44
  }
90
45
 
91
- export async function getWorkspaceSessionSelection(
92
- workspacePath: string
93
- ): Promise<WorkspaceSessionSelection> {
94
- await writeChain
95
- return (await readStore())[workspacePath] ?? emptySelection()
46
+ // Show `sessionId` in `tabId` inside a state transaction. With `ifMatch`, only
47
+ // when the tab still shows that chat (null: no chat), so a stale client
48
+ // cannot undo a newer choice. Returns whether the tab changed.
49
+ export function selectSessionForTab(
50
+ state: WorkspaceState,
51
+ workspaceId: string,
52
+ sessionId: string,
53
+ tabId: WorkspaceTabId,
54
+ ifMatch?: string | null
55
+ ): boolean {
56
+ const current = state.selected[tabId] ?? null
57
+ const expected = ifMatch == null ? ifMatch : resolveRenamedSession(workspaceId, ifMatch)
58
+ if (expected !== undefined && current !== expected) return false
59
+ const next = resolveRenamedSession(workspaceId, sessionId)
60
+ if (current === next) return false
61
+ state.selected[tabId] = next
62
+ return true
96
63
  }
97
64
 
98
- export async function getPinnedSession(workspacePath: string): Promise<string | null> {
99
- return (await readStore())[workspacePath]?.pinned ?? null
65
+ export async function saveSelectedSession(
66
+ ws: WorkspaceRef,
67
+ sessionId: string,
68
+ tabId: WorkspaceTabId = 'overview',
69
+ ifMatch?: string | null
70
+ ): Promise<boolean> {
71
+ return updateWorkspaceState(ws, state =>
72
+ selectSessionForTab(state, ws.id, sessionId, tabId, ifMatch)
73
+ )
100
74
  }
101
75
 
102
- export async function pinSession(workspacePath: string, sessionId: string | null): Promise<void> {
103
- await locked(async () => {
104
- const store = await readStore()
105
- const selection = (store[workspacePath] ??= emptySelection())
106
- selection.pinned = sessionId === null ? null : resolveRenamedSession(workspacePath, sessionId)
107
- await writeStore(store)
76
+ // Clears `tabId` only while it still shows `sessionId`; the tab then opens a
77
+ // new chat.
78
+ export async function deselectSession(
79
+ ws: WorkspaceRef,
80
+ sessionId: string,
81
+ tabId: WorkspaceTabId
82
+ ): Promise<void> {
83
+ await updateWorkspaceState(ws, state => {
84
+ if (state.selected[tabId] === resolveRenamedSession(ws.id, sessionId))
85
+ delete state.selected[tabId]
108
86
  })
109
87
  }
110
88
 
111
- export async function dropSessionTab(workspacePath: string, tabId: WorkspaceTabId): Promise<void> {
112
- await locked(async () => {
113
- const store = await readStore()
114
- const selection = store[workspacePath]
115
- if (!selection || !(tabId in selection.selected)) return
116
- delete selection.selected[tabId]
117
- await writeStore(store)
89
+ export async function dropSessionTab(ws: WorkspaceRef, tabId: WorkspaceTabId): Promise<void> {
90
+ await updateWorkspaceState(ws, state => {
91
+ delete state.selected[tabId]
118
92
  })
119
93
  }
120
94
 
121
- export async function saveSelectedSession(
122
- workspacePath: string,
123
- sessionId: string | null,
124
- previousSessionId?: string | null,
125
- tabId: WorkspaceTabId = 'overview'
126
- ): Promise<SelectedSessionUpdate> {
127
- return locked(async () => {
128
- const store = await readStore()
129
- const selection = (store[workspacePath] ??= emptySelection())
130
- const current = selection.selected[tabId]
131
- const resolvedSessionId =
132
- sessionId === null ? null : resolveRenamedSession(workspacePath, sessionId)
133
- const resolvedPreviousSessionId =
134
- previousSessionId == null
135
- ? previousSessionId
136
- : resolveRenamedSession(workspacePath, previousSessionId)
137
-
138
- if (
139
- resolvedPreviousSessionId !== undefined &&
140
- (current ?? null) !== resolvedPreviousSessionId
141
- ) {
142
- return { changed: false, sessionId: current ?? null }
143
- }
144
- if ((current ?? null) === resolvedSessionId)
145
- return { changed: false, sessionId: resolvedSessionId }
146
-
147
- if (resolvedSessionId === null) delete selection.selected[tabId]
148
- else selection.selected[tabId] = resolvedSessionId
149
- await writeStore(store)
150
- return { changed: true, sessionId: resolvedSessionId }
151
- })
95
+ // Follow a temporary id's rename in every tab and the pin.
96
+ export function renameSessionSelection(state: WorkspaceState, from: string, to: string): void {
97
+ for (const [tabId, sessionId] of Object.entries(state.selected) as [WorkspaceTabId, string][]) {
98
+ if (sessionId === from) state.selected[tabId] = to
99
+ }
100
+ if (state.pinnedSessionId === from) state.pinnedSessionId = to
152
101
  }
153
102
 
154
- export async function renameSelectedSession(
155
- workspacePath: string,
156
- from: string,
157
- to: string
158
- ): Promise<SelectedSessionUpdate> {
159
- if (from === to) return { changed: false, sessionId: to }
160
- return locked(async () => {
161
- rememberSessionRename(workspacePath, from, to)
162
- const store = await readStore()
163
- const selection = (store[workspacePath] ??= emptySelection())
164
- let changed = false
165
- for (const tabId of Object.keys(selection.selected).filter(isWorkspaceTabId)) {
166
- if (selection.selected[tabId] !== from) continue
167
- selection.selected[tabId] = to
168
- changed = true
169
- }
170
- if (selection.pinned === from) {
171
- selection.pinned = to
172
- changed = true
173
- }
174
- if (changed) await writeStore(store)
175
- return { changed, sessionId: selection.selected.overview ?? null }
176
- })
103
+ // Remove an archived chat from every tab and the pin.
104
+ export function clearSessionSelection(state: WorkspaceState, sessionId: string): void {
105
+ for (const [tabId, selected] of Object.entries(state.selected) as [WorkspaceTabId, string][]) {
106
+ if (selected === sessionId) delete state.selected[tabId]
107
+ }
108
+ if (state.pinnedSessionId === sessionId) delete state.pinnedSessionId
177
109
  }
178
110
 
179
- export async function clearSelectedSession(
180
- workspacePath: string,
181
- sessionId: string
182
- ): Promise<SelectedSessionUpdate> {
183
- return locked(async () => {
184
- const store = await readStore()
185
- const selection = (store[workspacePath] ??= emptySelection())
186
- let changed = false
187
- for (const tabId of Object.keys(selection.selected).filter(isWorkspaceTabId)) {
188
- if (selection.selected[tabId] !== sessionId) continue
189
- delete selection.selected[tabId]
190
- changed = true
191
- }
192
- if (selection.pinned === sessionId) {
193
- selection.pinned = null
194
- changed = true
195
- }
196
- if (changed) await writeStore(store)
197
- return { changed, sessionId: selection.selected.overview ?? null }
198
- })
111
+ export async function clearSelectedSession(ws: WorkspaceRef, sessionId: string): Promise<void> {
112
+ await updateWorkspaceState(ws, state => clearSessionSelection(state, sessionId))
199
113
  }
@@ -1,127 +1,54 @@
1
- import { mkdir, rename } from 'node:fs/promises'
2
- import { dirname, join } from 'node:path'
3
- import type {
4
- SessionConfig,
5
- SessionRecord,
6
- SessionRecordPatch,
7
- SessionInfo,
8
- SessionSummary
9
- } from '@/lib/types'
10
- import { DATA_DIR } from '../data-dir'
1
+ import type { WorkspaceSessionState, WorkspaceState, WorkspaceTabId } from '@/lib/types'
2
+ import { readWorkspaceState, updateWorkspaceState, type WorkspaceRef } from '../workspace-state'
11
3
 
12
- type Store = Record<string, Record<string, SessionRecord>>
13
- export const DEFAULT_SESSION_STORE_PATH = join(DATA_DIR, 'sessions.json')
14
- let storePath = DEFAULT_SESSION_STORE_PATH
15
- let writes: Promise<unknown> = Promise.resolve()
16
-
17
- export function setSessionStorePath(path: string) {
18
- storePath = path
19
- }
20
-
21
- async function readFile<T>(path: string): Promise<T | undefined> {
22
- const file = Bun.file(path)
23
- return (await file.exists()) ? file.json() : undefined
24
- }
25
-
26
- async function read(): Promise<Store> {
27
- const stored = await readFile<Store>(storePath)
28
- if (stored) return stored
29
- // Import old files until the first unified write; leave the originals intact.
30
- const directory = dirname(storePath)
31
- const configs = await readFile<Record<string, Record<string, SessionConfig>>>(
32
- join(directory, 'session-config.json')
33
- )
34
- const store: Store = {}
35
- for (const [workspace, sessions] of Object.entries(configs ?? {})) {
36
- const records = (store[workspace] ??= {})
37
- for (const [id, config] of Object.entries(sessions)) records[id] = { ...records[id], config }
38
- }
39
- return store
40
- }
41
-
42
- export async function getSessionRecords(workspacePath: string) {
43
- await writes
44
- return (await read())[workspacePath] ?? {}
4
+ export async function getSessionEntries(
5
+ ws: WorkspaceRef
6
+ ): Promise<Record<string, WorkspaceSessionState>> {
7
+ return (await readWorkspaceState(ws)).sessions
45
8
  }
46
9
 
47
- export async function getSessionRecord(
48
- workspacePath: string,
10
+ export async function getSessionEntry(
11
+ ws: WorkspaceRef,
49
12
  sessionId: string
50
- ): Promise<SessionRecord> {
51
- return (await getSessionRecords(workspacePath))[sessionId] ?? {}
13
+ ): Promise<WorkspaceSessionState> {
14
+ return (await readWorkspaceState(ws)).sessions[sessionId] ?? {}
52
15
  }
53
16
 
54
- export async function updateSessionRecords<T>(
55
- workspacePath: string,
56
- change: (sessions: Record<string, SessionRecord>) => T
57
- ) {
58
- const run = writes.then(async () => {
59
- const store = await read()
60
- const sessions = (store[workspacePath] ??= {})
61
- const result = change(sessions)
62
- if (result === false) return result
63
- for (const [id, record] of Object.entries(sessions)) {
64
- if (Object.keys(record).length === 0) delete sessions[id]
65
- }
66
- if (Object.keys(sessions).length === 0) delete store[workspacePath]
67
- await mkdir(dirname(storePath), { recursive: true })
68
- const temp = `${storePath}.${process.pid}.tmp`
69
- await Bun.write(temp, JSON.stringify(store, null, 2))
70
- await rename(temp, storePath)
71
- return result
72
- })
73
- writes = run.catch(() => {})
74
- return run
75
- }
76
-
77
- export async function renameSessionRecord(workspacePath: string, from: string, to: string) {
78
- if (from === to) return
79
- await updateSessionRecords(workspacePath, sessions => {
80
- if (sessions[from]) {
81
- const source = sessions[from]
82
- const target = sessions[to]
83
- sessions[to] = {
84
- ...source,
85
- ...target,
86
- ...(source.config || target?.config
87
- ? { config: { ...source.config, ...target?.config } }
88
- : {})
89
- }
90
- delete sessions[from]
91
- }
92
- for (const record of Object.values(sessions)) {
93
- if (record.forkedFromSessionId === from) record.forkedFromSessionId = to
94
- }
95
- })
17
+ // Merge `patch` into a session entry inside a state transaction. An undefined
18
+ // field clears it.
19
+ export function patchSessionEntry(
20
+ state: WorkspaceState,
21
+ sessionId: string,
22
+ patch: Partial<WorkspaceSessionState>
23
+ ): void {
24
+ state.sessions[sessionId] = { ...state.sessions[sessionId], ...patch }
96
25
  }
97
26
 
98
- export async function withSessionRecords(
99
- workspacePath: string,
100
- sessions: SessionSummary[]
101
- ): Promise<SessionInfo[]> {
102
- const records = await getSessionRecords(workspacePath)
103
- return sessions.map(session => {
104
- const { config: _, ...metadata } = records[session.sessionId] ?? {}
105
- return { ...session, ...metadata }
106
- })
27
+ export function setSessionTab(state: WorkspaceState, sessionId: string, tabId: WorkspaceTabId) {
28
+ patchSessionEntry(state, sessionId, { tabId: tabId === 'overview' ? undefined : tabId })
107
29
  }
108
30
 
109
- export async function patchSessionRecord(
110
- workspacePath: string,
31
+ export async function updateSessionEntry(
32
+ ws: WorkspaceRef,
111
33
  sessionId: string,
112
- metadata: SessionRecordPatch
113
- ) {
114
- await updateSessionRecords(workspacePath, sessions => {
115
- const next = { ...sessions[sessionId], ...metadata }
116
- assertSessionRecord(next)
117
- sessions[sessionId] = next
118
- })
34
+ patch: Partial<WorkspaceSessionState>
35
+ ): Promise<void> {
36
+ await updateWorkspaceState(ws, state => patchSessionEntry(state, sessionId, patch))
119
37
  }
120
38
 
121
- function assertSessionRecord(record: Partial<SessionRecord>): asserts record is SessionRecord {
122
- if (
123
- record.forkedFromSessionId === undefined &&
124
- (record.forkedThroughMessageId !== undefined || record.forkedNoticeIds !== undefined)
125
- )
126
- throw new Error('Fork history requires a source session')
39
+ // A temporary id became the harness's real one. Settings saved under either id
40
+ // survive, the real id's winning, and forks of the old id follow it.
41
+ export function renameSessionEntry(state: WorkspaceState, from: string, to: string): void {
42
+ if (from === to) return
43
+ const source = state.sessions[from]
44
+ if (source) {
45
+ const target = state.sessions[to]
46
+ const config =
47
+ source.config || target?.config ? { ...source.config, ...target?.config } : undefined
48
+ state.sessions[to] = { ...source, ...target, ...(config ? { config } : {}) }
49
+ delete state.sessions[from]
50
+ }
51
+ for (const session of Object.values(state.sessions)) {
52
+ if (session.fork?.fromSessionId === from) session.fork.fromSessionId = to
53
+ }
127
54
  }