Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

5.3 KB
Raw Blame
export type NativeChatTaskStatus = 'pending' | 'in_progress' | 'completed'
export type NativeChatTask = {
  content: string
  status: NativeChatTaskStatus
  activeForm?: string
}
export type NativeChatTaskList = { tasks: NativeChatTask[]; explanation?: string }
export type NativeChatTaskChange = {
  kind: 'added' | 'removed' | 'started' | 'completed' | 'pending' | 'updated'
  task: NativeChatTask
}
export type NativeChatTaskListTool = 'todowrite' | 'update_plan'

export function nativeChatTaskListTool(name: string): NativeChatTaskListTool | null {
  const normalized = name.trim().toLowerCase()
  return normalized === 'todowrite' || normalized === 'update_plan' ? normalized : null
}

function record(value: unknown): Record<string, unknown> | null {
  return value !== null && typeof value === 'object' && !Array.isArray(value)
    ? (value as Record<string, unknown>)
    : null
}

function nonemptyString(value: unknown): string | undefined {
  return typeof value === 'string' && value.trim() ? value.trim() : undefined
}

export function normalizeNativeChatTaskList(
  name: string,
  input: unknown
): NativeChatTaskList | null {
  const tool = nativeChatTaskListTool(name)
  if (!tool) {
    return null
  }
  if (typeof input === 'string') {
    try {
      input = JSON.parse(input)
    } catch {
      return null
    }
  }
  const value = record(input)
  const entries = tool === 'todowrite' ? value?.todos : value?.plan
  if (!Array.isArray(entries)) {
    return null
  }
  const tasks: NativeChatTask[] = []
  for (const entry of entries) {
    const item = record(entry)
    const content = nonemptyString(tool === 'todowrite' ? item?.content : item?.step)
    if (!item || !content) {
      continue
    }
    const status =
      item.status === 'in_progress' || (tool === 'update_plan' && item.status === 'inProgress')
        ? 'in_progress'
        : item.status === 'completed'
          ? 'completed'
          : 'pending'
    const activeForm = tool === 'todowrite' ? nonemptyString(item.activeForm) : undefined
    tasks.push({ content, status, ...(activeForm ? { activeForm } : {}) })
  }
  if (entries.length > 0 && tasks.length === 0) {
    return null
  }
  const explanation = tool === 'update_plan' ? nonemptyString(value?.explanation) : undefined
  return { tasks, ...(explanation ? { explanation } : {}) }
}

/** Shared task copy for desktop translations and direct mobile rendering. */
export const NATIVE_CHAT_TASK_LIST_COPY = {
  title: 'Tasks',
  completed: 'Completed',
  inProgress: 'In progress',
  pending: 'Pending',
  empty: 'No tasks',
  progress: '{{completed}} of {{total}} tasks completed',
  added: 'Added {{task}}',
  removed: 'Removed {{task}}',
  started: 'Started {{task}}',
  finished: 'Completed {{task}}',
  reset: 'Marked pending: {{task}}',
  updated: 'Updated {{task}}',
  unchanged: 'Tasks unchanged',
  showAll: 'Full task list'
} as const

export type NativeChatTaskChangeCopyKey =
  | 'added'
  | 'removed'
  | 'started'
  | 'finished'
  | 'reset'
  | 'updated'

/** Which sentence a change reads as, and the task it names. */
export function nativeChatTaskChangeWords(change: NativeChatTaskChange): {
  key: NativeChatTaskChangeCopyKey
  task: string
} {
  switch (change.kind) {
    case 'added':
    case 'removed':
    case 'started':
      return { key: change.kind, task: change.task.content }
    case 'completed':
      return { key: 'finished', task: change.task.content }
    case 'pending':
      return { key: 'reset', task: change.task.content }
    case 'updated':
      return { key: 'updated', task: nativeChatTaskLabel(change.task) }
  }
}

export function nativeChatCompletedTaskCount(list: NativeChatTaskList): number {
  return list.tasks.filter((task) => task.status === 'completed').length
}

export function nativeChatTaskLabel(task: NativeChatTask): string {
  return task.status === 'in_progress' && task.activeForm ? task.activeForm : task.content
}

/** Content plus occurrence is the only identity the providers give these entries. */
export function diffNativeChatTaskLists(
  previous: NativeChatTaskList,
  current: NativeChatTaskList
): NativeChatTaskChange[] {
  const byContent = new Map<string, NativeChatTask[]>()
  for (const task of previous.tasks) {
    const matches = byContent.get(task.content)
    if (matches) {
      matches.push(task)
    } else {
      byContent.set(task.content, [task])
    }
  }
  const occurrences = new Map<string, number>()
  const consumed = new Set<NativeChatTask>()
  const changes: NativeChatTaskChange[] = []
  for (const task of current.tasks) {
    const occurrence = occurrences.get(task.content) ?? 0
    occurrences.set(task.content, occurrence + 1)
    const before = byContent.get(task.content)?.[occurrence]
    if (!before) {
      changes.push({ kind: 'added', task })
      continue
    }
    consumed.add(before)
    if (before.status !== task.status) {
      changes.push({
        kind:
          task.status === 'completed'
            ? 'completed'
            : task.status === 'in_progress'
              ? 'started'
              : 'pending',
        task
      })
    } else if (before.activeForm !== task.activeForm) {
      changes.push({ kind: 'updated', task })
    }
  }
  for (const task of previous.tasks) {
    if (!consumed.has(task)) {
      changes.push({ kind: 'removed', task })
    }
  }
  return changes
}