spool-memory 0.1.0

Local-first developer memory system — persistent, structured knowledge for AI coding tools
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
import * as React from 'react'
import {
  useInfiniteQuery,
  useMutation,
  useQuery,
  useQueryClient,
} from '@tanstack/react-query'
import { useConfig } from '@/state/config'
import { ipc } from '@/lib/api/desktop'
import { asEnvelope } from '@/lib/error'
import { qk } from '@/lib/queryKeys'
import type {
  DesktopErrorEnvelope,
  DesktopImportSessionResponse,
  DesktopSessionDetailResponse,
  DesktopSessionItem,
  DesktopSessionMessage,
  ImportCandidateDto,
} from '@/lib/types/desktop'
import {
  Card,
  CardContent,
  CardDescription,
  CardHeader,
  CardTitle,
} from '@/components/ui/card'
import { Button } from '@/components/ui/button'
import { Badge } from '@/components/ui/badge'
import { Separator } from '@/components/ui/separator'
import {
  Dialog,
  DialogClose,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from '@/components/ui/dialog'
import { ErrorBanner } from '@/components/error-banner'
import {
  Download,
  Filter,
  Loader2,
  MessageSquare,
  Play,
  Sparkles,
  Trash2,
} from 'lucide-react'

const PER_PAGE = 20
const MESSAGE_PAGE_SIZE = 50

const PROVIDER_OPTIONS = [
  { value: '', label: '全部' },
  { value: 'claude', label: 'Claude Code' },
  { value: 'codex', label: 'Codex' },
  { value: 'gemini', label: 'Gemini CLI' },
] as const

export function SessionsPage({ onImported }: { onImported?: () => void } = {}) {
  const cfg = useConfig()
  const qc = useQueryClient()
  const configPath = cfg.configPath

  const [providerFilter, setProviderFilter] = React.useState('')
  const [selectedId, setSelectedId] = React.useState<string | null>(null)
  const [preview, setPreview] = React.useState<DesktopImportSessionResponse | null>(null)
  const [actionError, setActionError] = React.useState<DesktopErrorEnvelope | null>(null)
  // Local detail state to support appending paginated messages without abandoning
  // the cached query data.
  const [detailMessages, setDetailMessages] = React.useState<DesktopSessionMessage[]>([])
  const [detailMessagesShown, setDetailMessagesShown] = React.useState(0)
  const [detailHasMore, setDetailHasMore] = React.useState(false)

  const listSentinelRef = React.useRef<HTMLDivElement>(null)
  const messageSentinelRef = React.useRef<HTMLDivElement>(null)

  const sessionsQuery = useInfiniteQuery({
    queryKey: qk.sessionsList(configPath, providerFilter),
    queryFn: ({ pageParam }) =>
      ipc.browseSessions({
        config_path: configPath,
        page: pageParam as number,
        per_page: PER_PAGE,
        provider: providerFilter || null,
      }),
    initialPageParam: 1,
    getNextPageParam: (lastPage) =>
      lastPage.has_more ? lastPage.page + 1 : undefined,
    enabled: configPath.trim().length > 0,
  })

  const allSessions: DesktopSessionItem[] = React.useMemo(
    () => sessionsQuery.data?.pages.flatMap((p) => p.sessions) ?? [],
    [sessionsQuery.data]
  )
  const totalCount = sessionsQuery.data?.pages[0]?.total ?? 0
  const hasMore = sessionsQuery.hasNextPage
  const loadingList = sessionsQuery.isFetching

  const sessionDetailQuery = useQuery({
    queryKey: selectedId
      ? qk.sessionDetail(configPath, selectedId)
      : ['sessionDetail', '__none__'],
    queryFn: () =>
      ipc.getSession({
        config_path: configPath,
        session_id: selectedId as string,
        message_offset: null,
        message_limit: MESSAGE_PAGE_SIZE,
      }),
    enabled: Boolean(selectedId) && configPath.trim().length > 0,
  })

  // Sync local pagination state when initial detail loads / changes.
  React.useEffect(() => {
    const data = sessionDetailQuery.data
    if (!data) {
      setDetailMessages([])
      setDetailMessagesShown(0)
      setDetailHasMore(false)
      return
    }
    setDetailMessages(data.messages)
    setDetailMessagesShown(data.showing_recent_messages)
    setDetailHasMore(data.has_more_messages)
  }, [sessionDetailQuery.data])

  const loadMoreMessagesMutation = useMutation({
    mutationFn: async () => {
      if (!selectedId) throw new Error('no session selected')
      return ipc.getSession({
        config_path: configPath,
        session_id: selectedId,
        message_offset: detailMessages.length,
        message_limit: MESSAGE_PAGE_SIZE,
      })
    },
    onSuccess: (res) => {
      if (!res) return
      setDetailMessages((prev) => [...prev, ...res.messages])
      setDetailMessagesShown((prev) => prev + res.messages.length)
      setDetailHasMore(res.has_more_messages)
    },
  })

  const continueMutation = useMutation({
    mutationFn: () =>
      ipc.continueSession({ config_path: configPath, session_id: selectedId as string }),
    onSuccess: () => {
      qc.invalidateQueries({ queryKey: qk.sessionsList(configPath, providerFilter) })
    },
  })

  const deleteMutation = useMutation({
    mutationFn: () =>
      ipc.deleteSession({ config_path: configPath, session_id: selectedId as string }),
    onSuccess: () => {
      setSelectedId(null)
      qc.invalidateQueries({ queryKey: qk.sessionsList(configPath, providerFilter) })
    },
  })

  const injectMutation = useMutation({
    mutationFn: async () => {
      const session = sessionDetailQuery.data?.session
      if (!session) throw new Error('no session detail')
      const cwd = session.cwd || ''
      const res = await ipc.runContext({
        config_path: configPath,
        vault_root_override: cfg.vaultRoot.trim() || null,
        cwd,
        task: session.title || 'session context',
        files: [],
        target: 'claude' as never,
        format: 'prompt' as never,
      })
      if (res.rendered && navigator.clipboard) {
        await navigator.clipboard.writeText(res.rendered)
      }
      return res
    },
  })

  const previewImportMutation = useMutation({
    mutationFn: async () => {
      const detail = sessionDetailQuery.data
      if (!selectedId || !detail) throw new Error('no session selected')
      return ipc.importSession({
        config_path: configPath,
        provider: detail.session.provider as 'claude' | 'codex',
        session_id: selectedId,
        apply: false,
        actor: 'desktop-ui',
      })
    },
    onSuccess: (res) => {
      setPreview(res)
    },
  })

  const confirmImportMutation = useMutation({
    mutationFn: async () => {
      const detail = sessionDetailQuery.data
      if (!selectedId || !detail || !preview) throw new Error('no preview')
      return ipc.importSession({
        config_path: configPath,
        provider: detail.session.provider as 'claude' | 'codex',
        session_id: selectedId,
        apply: true,
        actor: 'desktop-ui',
      })
    },
    onSuccess: () => {
      setPreview(null)
      qc.invalidateQueries({ queryKey: qk.workbench(configPath) })
      onImported?.()
    },
  })

  // IntersectionObserver for session list infinite scroll
  React.useEffect(() => {
    const sentinel = listSentinelRef.current
    if (!sentinel) return
    const observer = new IntersectionObserver(
      (entries) => {
        if (entries[0].isIntersecting && hasMore && !loadingList) {
          void sessionsQuery.fetchNextPage()
        }
      },
      { threshold: 0.1 }
    )
    observer.observe(sentinel)
    return () => observer.disconnect()
  }, [hasMore, loadingList, sessionsQuery])

  function handleProviderChange(value: string) {
    setProviderFilter(value)
  }

  const acting: 'continue' | 'delete' | 'import' | 'inject' | null =
    continueMutation.isPending
      ? 'continue'
      : deleteMutation.isPending
        ? 'delete'
        : previewImportMutation.isPending
          ? 'import'
          : injectMutation.isPending
            ? 'inject'
            : null

  const queryError =
    sessionsQuery.error ??
    sessionDetailQuery.error ??
    continueMutation.error ??
    deleteMutation.error ??
    injectMutation.error ??
    previewImportMutation.error ??
    confirmImportMutation.error ??
    loadMoreMessagesMutation.error ??
    null
  const errorEnvelope = actionError ?? (queryError ? asEnvelope(queryError) : null)

  React.useEffect(() => {
    setActionError(null)
  }, [selectedId])

  const detail = sessionDetailQuery.data ?? null
  const loadingDetail = sessionDetailQuery.isFetching
  const loadingMore = loadMoreMessagesMutation.isPending
  const applying = confirmImportMutation.isPending

  return (
    <div className="grid gap-4 lg:grid-cols-[400px_1fr]">
      <Card className="h-[calc(100vh-13rem)] overflow-hidden flex flex-col">
        <CardHeader className="pb-3 shrink-0">
          <div className="flex items-center justify-between">
            <CardTitle className="text-base flex items-center gap-2">
              会话列表
              {loadingList && (
                <Loader2 className="h-3.5 w-3.5 animate-spin text-muted-foreground" />
              )}
            </CardTitle>
          </div>
          <CardDescription>
            {totalCount > 0
              ? `共 ${totalCount} 条 · 已加载 ${allSessions.length} 条`
              : loadingList
                ? '加载中...'
                : '暂无会话'}
          </CardDescription>
          <div className="flex items-center gap-1 pt-1">
            <Filter className="h-3 w-3 text-muted-foreground" />
            <select
              className="h-7 rounded-md border bg-background px-2 text-xs"
              value={providerFilter}
              onChange={(e) => handleProviderChange(e.target.value)}
              aria-label="Provider filter"
            >
              {PROVIDER_OPTIONS.map((opt) => (
                <option key={opt.value} value={opt.value}>
                  {opt.label}
                </option>
              ))}
            </select>
          </div>
        </CardHeader>
        <CardContent className="overflow-y-auto px-3 pb-3 flex-1">
          {allSessions.length === 0 && !loadingList && (
            <div className="px-3 py-8 text-center">
              <p className="text-sm text-muted-foreground">暂无会话</p>
              <p className="mt-2 text-xs text-muted-foreground">
                在 AI 客户端开启会话后会自动出现
              </p>
            </div>
          )}
          <div className="space-y-1">
            {allSessions.map((s) => (
              <SessionRow
                key={`${s.provider}:${s.session_id}`}
                item={s}
                selected={s.session_id === selectedId}
                onSelect={() => setSelectedId(s.session_id)}
              />
            ))}
          </div>
          {hasMore && (
            <div ref={listSentinelRef} className="flex justify-center py-3">
              {loadingList && <Loader2 className="h-4 w-4 animate-spin text-muted-foreground" />}
            </div>
          )}
        </CardContent>
      </Card>

      <div className="space-y-4">
        {errorEnvelope && <ErrorBanner envelope={errorEnvelope} />}
        {!selectedId && !errorEnvelope && (
          <Card>
            <CardContent className="p-10 text-center text-sm text-muted-foreground">
              选择左侧的会话查看对话记录与关联的记忆记录。
            </CardContent>
          </Card>
        )}
        {selectedId && detail && (
          <SessionDetail
            detail={detail}
            messages={detailMessages}
            messagesShown={detailMessagesShown}
            hasMoreMessages={detailHasMore}
            loading={loadingDetail}
            loadingMore={loadingMore}
            acting={acting}
            onContinue={() => continueMutation.mutate()}
            onDelete={() => deleteMutation.mutate()}
            onImport={() => previewImportMutation.mutate()}
            onInjectContext={() => injectMutation.mutate()}
            onLoadMore={() => loadMoreMessagesMutation.mutate()}
            messageSentinelRef={messageSentinelRef}
          />
        )}
      </div>

      <Dialog
        open={preview !== null}
        onOpenChange={(open) => {
          if (!open) setPreview(null)
        }}
      >
        <DialogContent>
          <DialogHeader>
            <DialogTitle>导入预览 · {preview?.session_ref}</DialogTitle>
            <DialogDescription>
              扫描 {preview?.total_messages} 条消息,识别 {preview?.candidate_count} 条候选记忆。
              确认后以 candidate 状态写入 ledger,需在"记忆审核"tab 再做接受/归档。
            </DialogDescription>
          </DialogHeader>
          <div className="max-h-[50vh] space-y-2 overflow-y-auto">
            {preview?.candidates.length === 0 && (
              <p className="py-8 text-center text-sm text-muted-foreground">
                未识别到候选。可以关闭本窗口换条会话再试。
              </p>
            )}
            {preview?.candidates.map((c, idx) => (
              <CandidateRow key={`${c.source_ref}-${idx}`} candidate={c} />
            ))}
          </div>
          <DialogFooter>
            <DialogClose asChild>
              <Button variant="outline" disabled={applying}>
                取消
              </Button>
            </DialogClose>
            <Button
              onClick={() => confirmImportMutation.mutate()}
              disabled={applying || !preview || preview.candidate_count === 0}
            >
              {applying && <Loader2 className="mr-1 h-3.5 w-3.5 animate-spin" />}
              确认导入 {preview?.candidate_count ?? 0} 条候选
            </Button>
          </DialogFooter>
        </DialogContent>
      </Dialog>
    </div>
  )
}

function CandidateRow({ candidate }: { candidate: ImportCandidateDto }) {
  return (
    <div className="rounded-md border px-3 py-2 text-sm">
      <div className="mb-1 flex items-center gap-2 text-xs">
        <Badge variant="default">{candidate.signal}</Badge>
        <Badge variant="outline">{candidate.memory_type}</Badge>
        <Badge variant="muted">{candidate.scope}</Badge>
      </div>
      <p className="font-medium">{candidate.title}</p>
      <p className="mt-1 text-xs leading-relaxed text-muted-foreground line-clamp-3">
        {candidate.summary}
      </p>
    </div>
  )
}

function SessionRow({
  item,
  selected,
  onSelect,
}: {
  item: DesktopSessionItem
  selected: boolean
  onSelect: () => void
}) {
  return (
    <button
      type="button"
      onClick={onSelect}
      className={`w-full rounded-md border px-3 py-2 text-left transition-colors ${
        selected ? 'border-primary bg-accent' : 'border-transparent hover:bg-accent/60'
      }`}
    >
      <div className="flex items-center justify-between gap-2">
        <span className="line-clamp-1 text-sm font-medium">{item.title}</span>
        <Badge variant="outline" className="shrink-0 text-[10px]">
          {item.provider}
        </Badge>
      </div>
      {item.summary && (
        <p className="mt-1 line-clamp-2 text-xs text-muted-foreground">{item.summary}</p>
      )}
      <div className="mt-1 flex items-center gap-2 text-xs text-muted-foreground">
        <MessageSquare className="h-3 w-3" />
        <span>{item.record_count} 条记忆</span>
        {item.pending_review_count > 0 && (
          <Badge variant="secondary" className="text-[10px]">
            待审 {item.pending_review_count}
          </Badge>
        )}
        <span className="ml-auto truncate">{item.updated_at.slice(0, 19)}</span>
      </div>
      {item.project_path && (
        <p className="mt-0.5 truncate text-[10px] text-muted-foreground/70 font-mono">
          {item.project_path}
        </p>
      )}
    </button>
  )
}

function SessionDetail({
  detail,
  messages,
  messagesShown,
  hasMoreMessages,
  loading,
  loadingMore,
  acting,
  onContinue,
  onDelete,
  onImport,
  onInjectContext,
  onLoadMore,
  messageSentinelRef,
}: {
  detail: DesktopSessionDetailResponse
  messages: DesktopSessionMessage[]
  messagesShown: number
  hasMoreMessages: boolean
  loading: boolean
  loadingMore: boolean
  acting: 'continue' | 'delete' | 'import' | 'inject' | null
  onContinue: () => void
  onDelete: () => void
  onImport: () => void
  onInjectContext: () => void
  onLoadMore: () => void
  messageSentinelRef: React.RefObject<HTMLDivElement | null>
}) {
  const { session } = detail

  // Auto-load more messages when sentinel is visible
  React.useEffect(() => {
    const sentinel = messageSentinelRef.current
    if (!sentinel || !hasMoreMessages) return
    const observer = new IntersectionObserver(
      (entries) => {
        if (entries[0].isIntersecting && hasMoreMessages && !loadingMore) {
          onLoadMore()
        }
      },
      { threshold: 0.1 }
    )
    observer.observe(sentinel)
    return () => observer.disconnect()
  }, [hasMoreMessages, loadingMore, onLoadMore, messageSentinelRef])

  return (
    <>
      <Card>
        <CardHeader className="pb-3">
          <div className="flex items-start justify-between gap-3">
            <div>
              <CardTitle className="text-base">{session.title}</CardTitle>
              <CardDescription className="flex flex-wrap items-center gap-2 pt-1 text-xs">
                <Badge variant="outline">{session.provider}</Badge>
                <span>{session.session_id}</span>
                <span className="text-muted-foreground">{session.updated_at}</span>
              </CardDescription>
            </div>
            {loading && <Loader2 className="h-4 w-4 animate-spin text-muted-foreground" />}
          </div>
        </CardHeader>
        <CardContent className="space-y-3">
          {session.summary && (
            <p className="text-sm leading-relaxed text-muted-foreground">{session.summary}</p>
          )}
          {session.cwd && (
            <p className="text-xs">
              cwd: <span className="font-mono">{session.cwd}</span>
            </p>
          )}
          {session.project_path && session.project_path !== session.cwd && (
            <p className="text-xs">
              project: <span className="font-mono">{session.project_path}</span>
            </p>
          )}
          {session.source_path && (
            <p className="text-xs">
              source: <span className="font-mono">{session.source_path}</span>
            </p>
          )}
          <Separator />
          <div className="flex flex-wrap gap-2">
            <Button size="sm" onClick={onContinue} disabled={acting !== null}>
              {acting === 'continue' ? (
                <Loader2 className="h-3.5 w-3.5 animate-spin" />
              ) : (
                <Play className="h-3.5 w-3.5" />
              )}
              继续会话
            </Button>
            <Button size="sm" variant="outline" onClick={onInjectContext} disabled={acting !== null}>
              {acting === 'inject' ? (
                <Loader2 className="h-3.5 w-3.5 animate-spin" />
              ) : (
                <Sparkles className="h-3.5 w-3.5" />
              )}
              生成上下文
            </Button>
            <Button
              size="sm"
              variant="secondary"
              onClick={onImport}
              disabled={acting !== null}
            >
              {acting === 'import' ? (
                <Loader2 className="h-3.5 w-3.5 animate-spin" />
              ) : (
                <Download className="h-3.5 w-3.5" />
              )}
              导入为候选
            </Button>
            <Button
              size="sm"
              variant="destructive"
              onClick={onDelete}
              disabled={acting !== null}
            >
              {acting === 'delete' ? (
                <Loader2 className="h-3.5 w-3.5 animate-spin" />
              ) : (
                <Trash2 className="h-3.5 w-3.5" />
              )}
              删除
            </Button>
          </div>
        </CardContent>
      </Card>

      <Card>
        <CardHeader className="pb-3">
          <CardTitle className="text-base">消息</CardTitle>
          <CardDescription>
            共 {detail.total_messages} 条 · 已加载 {messagesShown} 条
            {hasMoreMessages && '(滚动加载更多)'}
          </CardDescription>
        </CardHeader>
        <CardContent className="space-y-3">
          {messages.map((m, idx) => (
            <MessageCard key={idx} message={m} />
          ))}
          {hasMoreMessages && (
            <div ref={messageSentinelRef as React.LegacyRef<HTMLDivElement>} className="flex justify-center py-3">
              {loadingMore && <Loader2 className="h-4 w-4 animate-spin text-muted-foreground" />}
            </div>
          )}
        </CardContent>
      </Card>
    </>
  )
}

function MessageCard({ message }: { message: DesktopSessionMessage }) {
  const isUser = message.role === 'user'
  return (
    <div
      className={`rounded-md border px-3 py-2 text-sm ${
        isUser ? 'bg-accent/40' : 'bg-muted/20'
      }`}
    >
      <div className="mb-1 flex items-center gap-2 text-xs text-muted-foreground">
        <Badge variant={isUser ? 'default' : 'outline'}>{message.role}</Badge>
        <span>{message.timestamp}</span>
        {message.truncated && <Badge variant="muted">truncated</Badge>}
      </div>
      <p className="whitespace-pre-wrap leading-relaxed">{message.content}</p>
    </div>
  )
}