Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

6.1 KB
Raw Blame
import { create } from 'zustand'
import { persist } from 'zustand/middleware'
import { safeJSONStorage } from '../lib/storage-quota'

/**
 * The plan a long run is working through, written by the model itself.
 *
 * On a task that takes twenty tool calls the transcript scrolls away and the
 * user is left guessing whether the agent is on step 2 or step 9, and whether
 * it still remembers what it set out to do. The audit filed this as C4: the
 * model had no way to state a plan and no way to show progress against it.
 *
 * The model owns the list through the `todo_write` tool and replaces it whole
 * on every call. Whole-list replacement rather than per-item patching is
 * deliberate: a small local model cannot be trusted to hold stable item ids
 * across twenty turns, and a half-applied patch would show a plan that never
 * existed.
 *
 * Keyed by conversation so the Code tab and every chat keep their own plan, and
 * persisted so closing the app mid-task does not lose it.
 */

export type TodoStatus = 'pending' | 'in_progress' | 'completed'

export interface TodoItem {
  content: string
  status: TodoStatus
}

export interface ArchivedPlan {
  /** The assistant message the plan hangs under in the transcript. */
  anchorId: string
  todos: TodoItem[]
}

/** Older plans of one chat that are kept; the oldest go first. */
const MAX_ARCHIVED = 30

interface TodoState {
  /** conversation id -> that run's plan */
  byConversation: Record<string, TodoItem[]>
  /** Wall-clock of the last write per conversation, for the "just updated" cue. */
  updatedAt: Record<string, number>

  /** conversation id -> earlier plans, each kept at the answer it belonged to
   *  (lib/plan-pin.ts). The pinned plan leaves the chat once the user writes
   *  again; the transcript keeps it as a folded "Plan" row. */
  archived: Record<string, ArchivedPlan[]>
  /** conversation id -> the pinned plan is unfolded. Open unless the user
   *  folded it (David, 14.11.2026); kept per chat, so a running step never
   *  folds or unfolds it again. */
  planOpen: Record<string, boolean>
  setPlanOpen: (conversationId: string, open: boolean) => void

  setTodos: (conversationId: string, todos: TodoItem[]) => void
  archivePlan: (conversationId: string, anchorId: string, todos: TodoItem[]) => void
  /** Everything of a chat, its archive included: for a deleted chat. */
  forgetConversation: (conversationId: string) => void
  getTodos: (conversationId: string | null | undefined) => TodoItem[]
  clearTodos: (conversationId: string) => void
}

/** Guards against a model that returns 40 steps or a novel per step. */
const MAX_ITEMS = 40
const MAX_CONTENT = 200

export function normalizeTodos(raw: unknown): TodoItem[] {
  if (!Array.isArray(raw)) return []
  const out: TodoItem[] = []
  for (const entry of raw.slice(0, MAX_ITEMS)) {
    if (!entry || typeof entry !== 'object') continue
    const e = entry as Record<string, unknown>
    const content = typeof e.content === 'string' ? e.content.trim() : ''
    if (!content) continue
    // Anything unrecognised counts as not started: a typo must never mark work
    // done that was never done.
    const status: TodoStatus =
      e.status === 'completed' || e.status === 'in_progress' ? e.status : 'pending'
    out.push({ content: content.slice(0, MAX_CONTENT), status })
  }
  return out
}

export const useTodoStore = create<TodoState>()(
  persist(
    (set, get) => ({
      byConversation: {},
      updatedAt: {},
      archived: {},
      planOpen: {},

      setPlanOpen: (conversationId, open) =>
        set((s) => ({ planOpen: { ...s.planOpen, [conversationId]: open } })),

      setTodos: (conversationId, todos) =>
        set((s) => ({
          byConversation: { ...s.byConversation, [conversationId]: todos },
          updatedAt: { ...s.updatedAt, [conversationId]: Date.now() },
        })),

      archivePlan: (conversationId, anchorId, todos) =>
        set((s) => {
          if (todos.length === 0) return {}
          const kept = (s.archived[conversationId] ?? []).filter((a) => a.anchorId !== anchorId)
          return { archived: { ...s.archived, [conversationId]: [...kept, { anchorId, todos }].slice(-MAX_ARCHIVED) } }
        }),

      forgetConversation: (conversationId) =>
        set((s) => {
          const byConversation = { ...s.byConversation }
          const updatedAt = { ...s.updatedAt }
          const archived = { ...s.archived }
          const planOpen = { ...s.planOpen }
          delete byConversation[conversationId]
          delete updatedAt[conversationId]
          delete archived[conversationId]
          delete planOpen[conversationId]
          return { byConversation, updatedAt, archived, planOpen }
        }),

      getTodos: (conversationId) =>
        (conversationId && get().byConversation[conversationId]) || [],

      clearTodos: (conversationId) =>
        set((s) => {
          const byConversation = { ...s.byConversation }
          const updatedAt = { ...s.updatedAt }
          delete byConversation[conversationId]
          delete updatedAt[conversationId]
          return { byConversation, updatedAt }
        }),
    }),
    {
      name: 'locally-uncensored-todos',
      storage: safeJSONStorage(),
    },
  ),
)

/** Non-hook access for the tool executor, which runs outside React. */
export function writeTodos(conversationId: string, raw: unknown): TodoItem[] {
  const todos = normalizeTodos(raw)
  useTodoStore.getState().setTodos(conversationId, todos)
  return todos
}

/** One-line summary the tool hands back to the model, so it can see its own
 *  progress in the transcript without us replaying the whole list. */
export function summarizeTodos(todos: TodoItem[]): string {
  if (todos.length === 0) return 'Plan cleared.'
  const done = todos.filter((t) => t.status === 'completed').length
  const current = todos.find((t) => t.status === 'in_progress')
  const lines = todos.map((t) => {
    const mark = t.status === 'completed' ? '[x]' : t.status === 'in_progress' ? '[>]' : '[ ]'
    return `${mark} ${t.content}`
  })
  const head = `Plan updated (${done}/${todos.length} done${current ? `, now: ${current.content}` : ''}).`
  return `${head}\n${lines.join('\n')}`
}