omega-harness
Agent skills for operating, encoding and communicating in Omega (.om) and the AIEN repositories
Brought by drakestapleton
omega-harness / cli / src / state.ts
| 1 | import fs from 'node:fs'; |
| 2 | import path from 'node:path'; |
| 3 | import { getConfigDir } from './config.js'; |
| 4 | |
| 5 | export interface TodoItem { |
| 6 | id: string; |
| 7 | title: string; |
| 8 | status: 'pending' | 'in_progress' | 'completed'; |
| 9 | } |
| 10 | |
| 11 | export interface SubagentRecord { |
| 12 | id: string; |
| 13 | role: string; |
| 14 | task: string; |
| 15 | mode: 'read-only' | 'full'; |
| 16 | status: 'running' | 'completed' | 'failed'; |
| 17 | startedAt: number; |
| 18 | finishedAt?: number; |
| 19 | toolCalls: number; |
| 20 | result?: string; |
| 21 | error?: string; |
| 22 | } |
| 23 | |
| 24 | export type SubagentRunner = (req: { |
| 25 | id: string; |
| 26 | role: string; |
| 27 | task: string; |
| 28 | mode: 'read-only' | 'full'; |
| 29 | existingId?: string; |
| 30 | }) => Promise<{ text: string; toolCalls: number }>; |
| 31 | |
| 32 | export class SessionState { |
| 33 | public todos: TodoItem[] = []; |
| 34 | public subagents = new Map<string, SubagentRecord>(); |
| 35 | private subagentSeq = 0; |
| 36 | public runSubagent?: SubagentRunner; |
| 37 | |
| 38 | setTodos(items: TodoItem[]): TodoItem[] { |
| 39 | this.todos = items.map((it, idx) => ({ |
| 40 | id: it.id || String(idx + 1), |
| 41 | title: String(it.title || '').trim(), |
| 42 | status: |
| 43 | it.status === 'in_progress' || it.status === 'completed' |
| 44 | ? it.status |
| 45 | : 'pending', |
| 46 | })); |
| 47 | return this.todos; |
| 48 | } |
| 49 | |
| 50 | formatTodos(): string { |
| 51 | if (this.todos.length === 0) return '(no tasks recorded)'; |
| 52 | return this.todos |
| 53 | .map((t) => { |
| 54 | const box = t.status === 'completed' ? '[x]' : t.status === 'in_progress' ? '[~]' : '[ ]'; |
| 55 | return `${box} (${t.id}) ${t.title}`; |
| 56 | }) |
| 57 | .join('\n'); |
| 58 | } |
| 59 | |
| 60 | nextSubagentId(role: string): string { |
| 61 | this.subagentSeq += 1; |
| 62 | const slug = role |
| 63 | .toLowerCase() |
| 64 | .replace(/[^a-z0-9]+/g, '-') |
| 65 | .replace(/^-|-$/g, '') |
| 66 | .slice(0, 18) || 'worker'; |
| 67 | return `sub-${this.subagentSeq}-${slug}`; |
| 68 | } |
| 69 | |
| 70 | registerSubagent(rec: SubagentRecord): void { |
| 71 | this.subagents.set(rec.id, rec); |
| 72 | } |
| 73 | |
| 74 | listSubagents(): SubagentRecord[] { |
| 75 | return Array.from(this.subagents.values()); |
| 76 | } |
| 77 | |
| 78 | formatSubagents(): string { |
| 79 | const list = this.listSubagents(); |
| 80 | if (list.length === 0) return '(no subagents spawned in this session)'; |
| 81 | return list |
| 82 | .map((s) => { |
| 83 | const dur = s.finishedAt ? `${((s.finishedAt - s.startedAt) / 1000).toFixed(1)}s` : 'running'; |
| 84 | const summary = (s.result || s.error || '').replace(/\s+/g, ' ').slice(0, 100); |
| 85 | return `- ${s.id} [${s.role}] (${s.status}, ${s.mode}, ${s.toolCalls} tools, ${dur})${summary ? `: ${summary}` : ''}`; |
| 86 | }) |
| 87 | .join('\n'); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | function getMemoryPath(): string { |
| 92 | return path.join(getConfigDir(), 'memory.json'); |
| 93 | } |
| 94 | |
| 95 | export function readMemories(): Record<string, { value: string; updatedAt: string }> { |
| 96 | try { |
| 97 | const raw = fs.readFileSync(getMemoryPath(), 'utf8'); |
| 98 | const parsed = JSON.parse(raw); |
| 99 | if (parsed && typeof parsed === 'object') { |
| 100 | return parsed as Record<string, { value: string; updatedAt: string }>; |
| 101 | } |
| 102 | } catch { |
| 103 | // missing or corrupt |
| 104 | } |
| 105 | return {}; |
| 106 | } |
| 107 | |
| 108 | export function saveMemory(key: string, value: string): void { |
| 109 | const dir = getConfigDir(); |
| 110 | fs.mkdirSync(dir, { recursive: true, mode: 0o700 }); |
| 111 | const mem = readMemories(); |
| 112 | mem[key] = { value, updatedAt: new Date().toISOString() }; |
| 113 | fs.writeFileSync(getMemoryPath(), JSON.stringify(mem, null, 2) + '\n', { mode: 0o600 }); |
| 114 | } |
| 115 | |
| 116 | export function formatMemories(filterKey?: string): string { |
| 117 | const mem = readMemories(); |
| 118 | if (filterKey) { |
| 119 | const hit = mem[filterKey]; |
| 120 | if (!hit) return `No memory found for key "${filterKey}".`; |
| 121 | return `${filterKey}: ${hit.value} (updated ${hit.updatedAt})`; |
| 122 | } |
| 123 | const entries = Object.entries(mem); |
| 124 | if (entries.length === 0) return '(no persistent memories saved)'; |
| 125 | return entries.map(([k, v]) => `- ${k}: ${v.value}`).join('\n'); |
| 126 | } |