omega-harness
Agent skills for operating, encoding and communicating in Omega (.om) and the AIEN repositories
Brought by drakestapleton
omega-harness / cli / src / index.tsx
| 1 | #!/usr/bin/env node |
| 2 | import React from 'react'; |
| 3 | import { render } from 'ink'; |
| 4 | import chalk from 'chalk'; |
| 5 | import { CLI_VERSION, SYMBOL, renderBanner } from './brand.js'; |
| 6 | import { |
| 7 | getConfigPath, |
| 8 | loadConfig, |
| 9 | writePersistedConfig, |
| 10 | type ApprovalMode, |
| 11 | type ProviderKind, |
| 12 | } from './config.js'; |
| 13 | import { |
| 14 | buildSystemPrompt, |
| 15 | discoverSkills, |
| 16 | readPinInfo, |
| 17 | } from './harness.js'; |
| 18 | import { probeInkling } from './inkling.js'; |
| 19 | import { rebuildSparkInkling, stackDown, stackUp } from './stack.js'; |
| 20 | import { TOOL_DECLARATIONS } from './tools.js'; |
| 21 | import { OmegaAgent } from './agent.js'; |
| 22 | import { App } from './ui/App.js'; |
| 23 | |
| 24 | interface ParsedArgs { |
| 25 | help: boolean; |
| 26 | version: boolean; |
| 27 | listSkills: boolean; |
| 28 | listTools: boolean; |
| 29 | showPin: boolean; |
| 30 | showStatus: boolean; |
| 31 | resumeSession?: string; |
| 32 | stackCommand?: 'up' | 'down' | 'rebuild'; |
| 33 | launchAfterUp: boolean; |
| 34 | rebuildOnUp: boolean; |
| 35 | sparkHost: string; |
| 36 | provider?: ProviderKind; |
| 37 | endpoint?: string; |
| 38 | prompt?: string; |
| 39 | model?: string; |
| 40 | apiKey?: string; |
| 41 | approvalMode?: ApprovalMode; |
| 42 | skills: string[]; |
| 43 | positional: string[]; |
| 44 | configSubcommand?: { action: 'set' | 'get' | 'path'; key?: string; value?: string }; |
| 45 | } |
| 46 | |
| 47 | function parseArgs(argv: string[]): ParsedArgs { |
| 48 | const out: ParsedArgs = { |
| 49 | help: false, |
| 50 | version: false, |
| 51 | listSkills: false, |
| 52 | listTools: false, |
| 53 | showPin: false, |
| 54 | showStatus: false, |
| 55 | launchAfterUp: false, |
| 56 | rebuildOnUp: false, |
| 57 | sparkHost: process.env.OMEGA_SPARK_HOST || 'spark', |
| 58 | skills: [], |
| 59 | positional: [], |
| 60 | }; |
| 61 | |
| 62 | if (argv[0] === 'config') { |
| 63 | const action = argv[1]; |
| 64 | if (action === 'set' || action === 'get' || action === 'path') { |
| 65 | out.configSubcommand = { action, key: argv[2], value: argv[3] }; |
| 66 | return out; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | let startIdx = 0; |
| 71 | if (argv[0] === 'up' || argv[0] === 'down' || argv[0] === 'rebuild') { |
| 72 | out.stackCommand = argv[0]; |
| 73 | startIdx = 1; |
| 74 | } else if (argv[0] === 'start') { |
| 75 | out.stackCommand = 'up'; |
| 76 | out.launchAfterUp = true; |
| 77 | startIdx = 1; |
| 78 | } |
| 79 | |
| 80 | for (let i = startIdx; i < argv.length; i++) { |
| 81 | const a = argv[i]; |
| 82 | if (a === '-h' || a === '--help') { |
| 83 | out.help = true; |
| 84 | } else if (a === '-v' || a === '--version') { |
| 85 | out.version = true; |
| 86 | } else if (a === '--skills') { |
| 87 | out.listSkills = true; |
| 88 | } else if (a === '--tools') { |
| 89 | out.listTools = true; |
| 90 | } else if (a === '--pin') { |
| 91 | out.showPin = true; |
| 92 | } else if (a === '--status') { |
| 93 | out.showStatus = true; |
| 94 | } else if (a === '--launch') { |
| 95 | out.launchAfterUp = true; |
| 96 | } else if (a === '--rebuild') { |
| 97 | out.rebuildOnUp = true; |
| 98 | } else if (a === '--host' && argv[i + 1]) { |
| 99 | out.sparkHost = argv[++i]; |
| 100 | } else if (a === '-r' || a === '--resume') { |
| 101 | out.resumeSession = argv[i + 1] && !argv[i + 1].startsWith('-') ? argv[++i] : 'latest'; |
| 102 | } else if (a === '-y' || a === '--yolo') { |
| 103 | out.approvalMode = 'yolo'; |
| 104 | } else if (a === '--auto-edit') { |
| 105 | out.approvalMode = 'auto-edit'; |
| 106 | } else if (a === '--omega-only') { |
| 107 | out.approvalMode = 'omega-only'; |
| 108 | } else if (a === '--provider' && argv[i + 1]) { |
| 109 | const p = argv[++i].toLowerCase(); |
| 110 | if (p === 'inkling' || p === 'gemini') out.provider = p; |
| 111 | } else if ((a === '-e' || a === '--endpoint') && argv[i + 1]) { |
| 112 | out.endpoint = argv[++i]; |
| 113 | } else if ((a === '-m' || a === '--model') && argv[i + 1]) { |
| 114 | out.model = argv[++i]; |
| 115 | } else if ((a === '-p' || a === '--prompt') && argv[i + 1]) { |
| 116 | out.prompt = argv[++i]; |
| 117 | } else if (a === '--api-key' && argv[i + 1]) { |
| 118 | out.apiKey = argv[++i]; |
| 119 | } else if ((a === '-s' || a === '--skill') && argv[i + 1]) { |
| 120 | out.skills.push(argv[++i]); |
| 121 | } else if (!a.startsWith('-')) { |
| 122 | out.positional.push(a); |
| 123 | } |
| 124 | } |
| 125 | return out; |
| 126 | } |
| 127 | |
| 128 | function printHelp(): void { |
| 129 | console.log(renderBanner(80).join('\n')); |
| 130 | console.log(''); |
| 131 | console.log( |
| 132 | chalk.cyan.bold(`${SYMBOL} Omega Harness CLI v${CLI_VERSION}`) + |
| 133 | chalk.gray(' — hooked to Inkling-2-Mini (with Gemini SDK fallback)') |
| 134 | ); |
| 135 | console.log(''); |
| 136 | console.log('Usage:'); |
| 137 | console.log(' omega [options] [initial prompt] Launch interactive Ω TUI'); |
| 138 | console.log(' omega start Boot Inkling-2-Mini + Arboris on Spark, tunnel, and launch TUI'); |
| 139 | console.log(' omega up [--rebuild] Start Inkling-2-Mini + Arboris on Spark + SSH tunnel'); |
| 140 | console.log(' omega down Stop Inkling-2-Mini on Spark + close SSH tunnel'); |
| 141 | console.log(' omega rebuild Recompile pure-Omega inkling2-serve on Spark'); |
| 142 | console.log(' omega --status Check Inkling-2-Mini health, KV slots & Arboris hits'); |
| 143 | console.log(' omega -p "<prompt>" Non-interactive headless mode (supports piped stdin)'); |
| 144 | console.log(' omega config set endpoint <url> Save default Inkling HTTP endpoint'); |
| 145 | console.log(' omega config set provider <name> Save default provider (inkling | gemini)'); |
| 146 | console.log(''); |
| 147 | console.log('Options:'); |
| 148 | console.log(' -e, --endpoint <url> Inkling HTTP endpoint (default: http://127.0.0.1:8080 or INKLING_URL)'); |
| 149 | console.log(' --provider <name> Backend provider: inkling (default) | gemini'); |
| 150 | console.log(' -m, --model <id> Model ID (default: inkling-2-mini)'); |
| 151 | console.log(' -p, --prompt <text> Run in non-interactive mode and print response to stdout'); |
| 152 | console.log(' -y, --yolo Auto-approve all tool calls'); |
| 153 | console.log(' --auto-edit Auto-approve file writes and edits'); |
| 154 | console.log(' --omega-only Restrict writes to .om / NOTES.md with auto-omc checks'); |
| 155 | console.log(' -s, --skill <name> Pin an Omega Harness skill into the system prompt (repeatable)'); |
| 156 | console.log(' --status Probe Inkling server health and exit'); |
| 157 | console.log(' --tools List all 27 available agent tools and exit'); |
| 158 | console.log(' --skills List all available Omega Harness skills and exit'); |
| 159 | console.log(' --pin Show omega-harness PIN and local omc status and exit'); |
| 160 | console.log(' --api-key <key> Optional bearer token (Inkling) or Gemini API key'); |
| 161 | console.log(' -v, --version Print version'); |
| 162 | console.log(' -h, --help Show this help'); |
| 163 | } |
| 164 | |
| 165 | async function readStdinIfPiped(): Promise<string> { |
| 166 | if (process.stdin.isTTY) return ''; |
| 167 | const chunks: Buffer[] = []; |
| 168 | for await (const chunk of process.stdin) { |
| 169 | chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)); |
| 170 | } |
| 171 | return Buffer.concat(chunks).toString('utf8').trim(); |
| 172 | } |
| 173 | |
| 174 | async function runHeadless( |
| 175 | prompt: string, |
| 176 | config: ReturnType<typeof loadConfig>, |
| 177 | cwd: string, |
| 178 | skills: string[] |
| 179 | ): Promise<void> { |
| 180 | if (config.provider === 'gemini' && !config.apiKey) { |
| 181 | console.error(`${SYMBOL} error: No Gemini API key configured. Export GEMINI_API_KEY or use --provider inkling.`); |
| 182 | process.exit(1); |
| 183 | } |
| 184 | |
| 185 | const stdinText = await readStdinIfPiped(); |
| 186 | const fullPrompt = stdinText ? `${prompt}\n\n<stdin>\n${stdinText}\n</stdin>` : prompt; |
| 187 | const pinInfo = readPinInfo(config.harnessRoot, config.omcPath); |
| 188 | const systemInstruction = buildSystemPrompt({ |
| 189 | harnessRoot: config.harnessRoot, |
| 190 | cwd, |
| 191 | activeSkills: Array.from(new Set([...config.defaultSkills, ...skills])), |
| 192 | approvalMode: config.approvalMode, |
| 193 | pinInfo, |
| 194 | }); |
| 195 | |
| 196 | const agent = new OmegaAgent(config.apiKey); |
| 197 | await agent.sendTurn(fullPrompt, { |
| 198 | provider: config.provider, |
| 199 | endpoint: config.endpoint, |
| 200 | apiKey: config.apiKey, |
| 201 | model: config.model, |
| 202 | systemInstruction, |
| 203 | toolContext: { |
| 204 | cwd, |
| 205 | harnessRoot: config.harnessRoot, |
| 206 | omcPath: config.omcPath, |
| 207 | approvalMode: config.approvalMode, |
| 208 | }, |
| 209 | callbacks: { |
| 210 | onTextDelta: (chunk) => process.stdout.write(chunk), |
| 211 | onToolStart: (ev) => { |
| 212 | process.stderr.write(chalk.gray(`[${SYMBOL} tool] ${ev.name}(${ev.argsPreview})\n`)); |
| 213 | }, |
| 214 | onToolUpdate: (ev) => { |
| 215 | if (ev.status === 'error' || ev.status === 'denied') { |
| 216 | process.stderr.write(chalk.red(`[${SYMBOL} ${ev.status}] ${ev.name}: ${ev.preview || ''}\n`)); |
| 217 | } |
| 218 | }, |
| 219 | onRequestApproval: async () => { |
| 220 | return config.approvalMode === 'yolo' ? 'allow' : 'deny'; |
| 221 | }, |
| 222 | }, |
| 223 | }); |
| 224 | process.stdout.write('\n'); |
| 225 | } |
| 226 | |
| 227 | async function main(): Promise<void> { |
| 228 | const args = parseArgs(process.argv.slice(2)); |
| 229 | const cwd = process.cwd(); |
| 230 | |
| 231 | if (args.help) { |
| 232 | printHelp(); |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | if (args.version) { |
| 237 | console.log(`${SYMBOL} omega-harness-cli ${CLI_VERSION}`); |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | if (args.configSubcommand) { |
| 242 | const { action, key, value } = args.configSubcommand; |
| 243 | if (action === 'path') { |
| 244 | console.log(getConfigPath()); |
| 245 | return; |
| 246 | } |
| 247 | if (action === 'set' && key && value) { |
| 248 | if ( |
| 249 | key === 'provider' || |
| 250 | key === 'endpoint' || |
| 251 | key === 'apiKey' || |
| 252 | key === 'model' || |
| 253 | key === 'approvalMode' || |
| 254 | key === 'omcPath' |
| 255 | ) { |
| 256 | writePersistedConfig({ [key]: value }); |
| 257 | console.log(`${SYMBOL} saved ${key} in ${getConfigPath()}`); |
| 258 | return; |
| 259 | } |
| 260 | console.error(`Unknown config key: ${key}. Valid keys: provider, endpoint, apiKey, model, approvalMode, omcPath`); |
| 261 | process.exit(1); |
| 262 | } |
| 263 | if (action === 'get' && key) { |
| 264 | const cfg = loadConfig(); |
| 265 | const raw = (cfg as unknown as Record<string, unknown>)[key]; |
| 266 | if (key === 'apiKey' && typeof raw === 'string') { |
| 267 | console.log(`${raw.slice(0, 6)}…${raw.slice(-4)}`); |
| 268 | } else { |
| 269 | console.log(raw ?? '(unset)'); |
| 270 | } |
| 271 | return; |
| 272 | } |
| 273 | console.error('Usage: omega config [path | get <key> | set <key> <value>]'); |
| 274 | process.exit(1); |
| 275 | } |
| 276 | |
| 277 | const config = loadConfig({ |
| 278 | provider: args.provider, |
| 279 | endpoint: args.endpoint, |
| 280 | model: args.model, |
| 281 | apiKey: args.apiKey, |
| 282 | approvalMode: args.approvalMode, |
| 283 | }); |
| 284 | |
| 285 | if (args.stackCommand === 'rebuild') { |
| 286 | const res = rebuildSparkInkling(args.sparkHost); |
| 287 | console.log(res.output); |
| 288 | return; |
| 289 | } |
| 290 | |
| 291 | if (args.stackCommand === 'down') { |
| 292 | stackDown({ sparkHost: args.sparkHost, endpoint: config.endpoint, apiKey: config.apiKey }); |
| 293 | return; |
| 294 | } |
| 295 | |
| 296 | if (args.stackCommand === 'up') { |
| 297 | const ok = await stackUp({ |
| 298 | sparkHost: args.sparkHost, |
| 299 | endpoint: config.endpoint, |
| 300 | apiKey: config.apiKey, |
| 301 | rebuild: args.rebuildOnUp, |
| 302 | }); |
| 303 | if (!ok) process.exit(1); |
| 304 | if (!args.launchAfterUp) return; |
| 305 | } |
| 306 | |
| 307 | if (args.showStatus) { |
| 308 | const h = await probeInkling(config.endpoint, config.apiKey); |
| 309 | console.log(`${SYMBOL} Provider: ${config.provider}`); |
| 310 | console.log(` Inkling URL: ${h.endpoint}`); |
| 311 | console.log(` Status: ${h.online ? chalk.green('ONLINE') : chalk.red(`OFFLINE (${h.error || 'unreachable'})`)}`); |
| 312 | console.log(` Engine: ${h.engine}`); |
| 313 | console.log(` Model: ${h.model || config.model}`); |
| 314 | if (h.contextWindow !== undefined) { |
| 315 | console.log(` Context: ${h.contextWindow.toLocaleString()} tokens/slot`); |
| 316 | } |
| 317 | if (h.kvPrefixHits !== undefined) { |
| 318 | console.log(` KV Cache: ${h.kvPrefixHits} prefix hits`); |
| 319 | } |
| 320 | if (h.slotsMax !== undefined) { |
| 321 | console.log(` KV Slots: ${h.slotsActive ?? 0} / ${h.slotsMax} active`); |
| 322 | } |
| 323 | if (h.arborisTransitions !== undefined) { |
| 324 | const denom = Math.max(1, h.arborisTransitions * 8); |
| 325 | const pct8 = h.arborisTransitions > 0 ? ((100 * (h.arborisTop8Hits ?? 0)) / denom).toFixed(1) : '0.0'; |
| 326 | const pct16 = h.arborisTransitions > 0 ? ((100 * (h.arborisTop16Hits ?? 0)) / denom).toFixed(1) : '0.0'; |
| 327 | console.log(` Arboris: ACTIVE (transitions=${h.arborisTransitions}, top8=${pct8}%, top16=${pct16}%)`); |
| 328 | } |
| 329 | if (!h.online) { |
| 330 | console.log(chalk.gray(' Tunnel Spark: ssh -N -L 8080:127.0.0.1:8080 spark')); |
| 331 | process.exitCode = 1; |
| 332 | } |
| 333 | return; |
| 334 | } |
| 335 | |
| 336 | if (args.listTools) { |
| 337 | console.log(chalk.cyan.bold(`${SYMBOL} Omega Harness Agent Tools (${TOOL_DECLARATIONS.length})`)); |
| 338 | for (const t of TOOL_DECLARATIONS) { |
| 339 | console.log(` ${chalk.bold((t.name || '').padEnd(26))} ${chalk.gray(t.description || '')}`); |
| 340 | } |
| 341 | return; |
| 342 | } |
| 343 | |
| 344 | if (args.listSkills) { |
| 345 | const skills = discoverSkills(config.harnessRoot); |
| 346 | console.log(chalk.cyan.bold(`${SYMBOL} Omega Harness Skills (${skills.length})`)); |
| 347 | for (const s of skills) { |
| 348 | console.log(` ${chalk.bold(s.name.padEnd(22))} ${chalk.gray(s.description)}`); |
| 349 | } |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | if (args.showPin) { |
| 354 | const pin = readPinInfo(config.harnessRoot, config.omcPath); |
| 355 | console.log(`${SYMBOL} Harness root: ${config.harnessRoot}`); |
| 356 | console.log(` PIN omega-mv: ${pin.omegaMvCommit || '(missing)'}`); |
| 357 | console.log(` PIN omc-sha256: ${pin.omcSha256 || '(missing)'}`); |
| 358 | console.log(` Local omc: ${pin.localOmcPath || '(not found)'}`); |
| 359 | console.log(` Local sha256: ${pin.localOmcSha256 || '(n/a)'}`); |
| 360 | console.log(` Match: ${pin.pinMatches === undefined ? 'unknown' : pin.pinMatches ? 'EXACT MATCH' : 'DIFFERENT (local/macOS build)'}`); |
| 361 | return; |
| 362 | } |
| 363 | |
| 364 | if (args.prompt !== undefined) { |
| 365 | await runHeadless(args.prompt, config, cwd, args.skills); |
| 366 | return; |
| 367 | } |
| 368 | |
| 369 | const initialPrompt = args.positional.length > 0 ? args.positional.join(' ') : undefined; |
| 370 | render( |
| 371 | <App |
| 372 | config={config} |
| 373 | cwd={cwd} |
| 374 | initialPrompt={initialPrompt} |
| 375 | initialSkills={args.skills} |
| 376 | resumeSession={args.resumeSession} |
| 377 | /> |
| 378 | ); |
| 379 | } |
| 380 | |
| 381 | main().catch((err) => { |
| 382 | console.error(`${SYMBOL} fatal:`, err instanceof Error ? err.message : err); |
| 383 | process.exit(1); |
| 384 | }); |