Skip to content

Where agent teams gather. Cross-platform team chat where AI agents are first-class teammates — with cloud or bring-your-own (Claude Code / Codex) brains.

TypeScript

fix(composer): keep the draft policy out of the server compile graph

yetone2cd19ab10a40
diff --git a/src/stores/app.ts b/src/stores/app.ts
index 164a0bc..429d143 100644
--- a/src/stores/app.ts
+++ b/src/stores/app.ts
@@ -1,10 +1,7 @@
import { create } from 'zustand'
-import type { ApiAttachment } from '@/api/client'
import type { ViewKey } from '@/types'
-import {
- applyDraftUpdate, loadComposerDrafts, saveComposerDrafts,
- type ComposerDraft,
-} from './composerDrafts'
+import { applyDraftUpdate, type ComposerDraft } from './composerDrafts'
+import { loadComposerDrafts, saveComposerDrafts } from './composerDraftsStorage'
export type { ComposerDraft }
@@ -57,7 +54,8 @@ interface AppState {
* → chat; desktop loses the whole ChatPane the moment `view` leaves
* `conversations`, because DesktopApp mounts views conditionally. Component
* state cannot survive either, so a half-typed message can't live there.
- * The store is also mirrored to localStorage (see ./composerDrafts), so a
+ * The store is also mirrored to localStorage (see
+ * ./composerDraftsStorage), so a
* reload or an app restart doesn't lose it either. Cleared on send, and on
* sign-out so drafts don't cross accounts on a shared device.
*/
diff --git a/src/stores/composerDrafts.ts b/src/stores/composerDrafts.ts
index 77ef596..d55fb5c 100644
--- a/src/stores/composerDrafts.ts
+++ b/src/stores/composerDrafts.ts
@@ -11,13 +11,25 @@
* Kept in its own module so the policy — what a draft is, how many we keep,
* how big the mirror may get — is testable without a store or a DOM.
*/
-import type { ApiAttachment } from '@/api/client'
+/** Structurally identical to `ApiAttachment` (src/api/client.ts), declared
+ * here rather than imported so this module stays free of the `@/` alias and
+ * of anything that drags the DOM in — that is what lets the policy be unit
+ * tested from the server test runner. `assertDraftAttachmentMatchesApi` in
+ * ./composerDraftsStorage fails the client build if the two ever drift. */
+export interface DraftAttachment {
+ url: string
+ name: string
+ kind: 'img' | 'pdf' | 'file' | 'fig'
+ mime?: string
+ size?: number
+ key?: string
+}
export interface ComposerDraft {
text: string
/** An upload the user already paid for. Dropping it on navigation loses a
* file that is sitting in storage, so it rides along with the text. */
- attachment: ApiAttachment | null
+ attachment: DraftAttachment | null
}
/** Keyed by composer SCOPE, not conversation: the desktop thread drawer is a
@@ -110,7 +122,7 @@ export function parseStoredDrafts(raw: string | null): ComposerDrafts {
return evict(out)
}
-function parseAttachment(value: unknown): ApiAttachment | null {
+function parseAttachment(value: unknown): DraftAttachment | null {
if (!value || typeof value !== 'object') return null
const a = value as Record<string, unknown>
if (typeof a.url !== 'string' || typeof a.name !== 'string') return null
@@ -140,59 +152,3 @@ export function serializeDrafts(drafts: ComposerDrafts): string | null {
working = copy
}
}
-
-export function loadComposerDrafts(): ComposerDrafts {
- if (typeof localStorage === 'undefined') return {}
- try {
- return parseStoredDrafts(localStorage.getItem(STORAGE_KEY))
- } catch {
- return {}
- }
-}
-
-let flushTimer: ReturnType<typeof setTimeout> | null = null
-let pending: ComposerDrafts | null = null
-
-function writeNow(): void {
- if (pending === null) return
- const drafts = pending
- pending = null
- if (flushTimer) { clearTimeout(flushTimer); flushTimer = null }
- try {
- if (Object.keys(drafts).length === 0) { localStorage.removeItem(STORAGE_KEY); return }
- const json = serializeDrafts(drafts)
- if (json === null) localStorage.removeItem(STORAGE_KEY)
- else localStorage.setItem(STORAGE_KEY, json)
- } catch {
- /* private mode, quota, disabled storage — a lost mirror must never break typing */
- }
-}
-
-/**
- * Mirror drafts to localStorage on a trailing debounce: typing fires this on
- * every keystroke, and a synchronous localStorage write per keystroke is a
- * jank source on long drafts. The `pagehide` flush below is what makes the
- * debounce safe — otherwise closing the window inside the window would lose
- * the very keystrokes this exists to protect.
- */
-export function saveComposerDrafts(drafts: ComposerDrafts): void {
- if (typeof localStorage === 'undefined') return
- pending = drafts
- if (flushTimer) return
- flushTimer = setTimeout(() => { flushTimer = null; writeNow() }, 400)
-}
-
-/** Write immediately — used by the unload hooks and by tests. */
-export function flushComposerDrafts(): void {
- writeNow()
-}
-
-if (typeof window !== 'undefined') {
- // `pagehide` fires on tab close, navigation and (unlike `beforeunload`)
- // the iOS back/forward cache path. `visibilitychange` covers backgrounding
- // the app without closing it, which is the common mobile case.
- window.addEventListener('pagehide', flushComposerDrafts)
- document.addEventListener('visibilitychange', () => {
- if (document.visibilityState === 'hidden') flushComposerDrafts()
- })
-}
diff --git a/src/stores/composerDraftsStorage.ts b/src/stores/composerDraftsStorage.ts
new file mode 100644
index 0000000..61f66dd
--- /dev/null
+++ b/src/stores/composerDraftsStorage.ts
@@ -0,0 +1,81 @@
+/**
+ * The browser half of composer-draft persistence: mirror the store's drafts
+ * into localStorage so a reload or an app restart doesn't lose a half-typed
+ * message.
+ *
+ * Split from ./composerDrafts on purpose. That module holds the policy — what
+ * a draft is, how many we keep, what a stored blob is allowed to decode to —
+ * and stays free of the DOM and of the `@/` alias so the server test runner
+ * can exercise it directly. Everything that needs a `window` lives here.
+ */
+import type { ApiAttachment } from '@/api/client'
+import {
+ STORAGE_KEY, parseStoredDrafts, serializeDrafts,
+ type ComposerDrafts, type DraftAttachment,
+} from './composerDrafts'
+
+/** The policy module re-declares the attachment shape rather than importing
+ * it. These two assignments are the drift alarm: if `ApiAttachment` gains a
+ * required field, or the two shapes stop lining up, the client build fails
+ * here instead of silently dropping data on the way to or from storage. */
+const _draftAttachmentIsApiAttachment: DraftAttachment = null as unknown as ApiAttachment
+const _apiAttachmentIsDraftAttachment: ApiAttachment = null as unknown as DraftAttachment
+void _draftAttachmentIsApiAttachment
+void _apiAttachmentIsDraftAttachment
+
+export function loadComposerDrafts(): ComposerDrafts {
+ if (typeof localStorage === 'undefined') return {}
+ try {
+ return parseStoredDrafts(localStorage.getItem(STORAGE_KEY))
+ } catch {
+ return {}
+ }
+}
+
+let flushTimer: ReturnType<typeof setTimeout> | null = null
+let pending: ComposerDrafts | null = null
+
+function writeNow(): void {
+ if (pending === null) return
+ const drafts = pending
+ pending = null
+ if (flushTimer) { clearTimeout(flushTimer); flushTimer = null }
+ try {
+ if (Object.keys(drafts).length === 0) { localStorage.removeItem(STORAGE_KEY); return }
+ const json = serializeDrafts(drafts)
+ if (json === null) localStorage.removeItem(STORAGE_KEY)
+ else localStorage.setItem(STORAGE_KEY, json)
+ } catch {
+ /* private mode, quota, disabled storage — a lost mirror must never break typing */
+ }
+}
+
+/**
+ * Mirror drafts on a trailing debounce: typing calls this on every keystroke,
+ * and a synchronous localStorage write per keystroke is a jank source on a
+ * long draft. The unload flush below is what makes the debounce safe —
+ * otherwise closing the window inside the window would lose exactly the
+ * keystrokes this exists to protect.
+ */
+export function saveComposerDrafts(drafts: ComposerDrafts): void {
+ if (typeof localStorage === 'undefined') return
+ pending = drafts
+ if (flushTimer) return
+ flushTimer = setTimeout(() => { flushTimer = null; writeNow() }, 400)
+}
+
+/** Write immediately — used by the unload hooks and by tests. */
+export function flushComposerDrafts(): void {
+ writeNow()
+}
+
+if (typeof window !== 'undefined') {
+ // `pagehide` covers tab close and navigation, including the back/forward
+ // cache path where `beforeunload` doesn't fire. `visibilitychange` covers
+ // backgrounding the app without closing it, which is the common case on
+ // mobile and when the desktop window is hidden rather than quit.
+ window.addEventListener('pagehide', flushComposerDrafts)
+ document.addEventListener('visibilitychange', () => {
+ if (document.visibilityState === 'hidden') flushComposerDrafts()
+ })
+}