Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

1.8 KB
Raw Blame
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';

import { CLI_CONFIG_DIR_NAME } from '../constants/identity';

export interface TaskEntry {
  agentId?: string;
  /**
   * Heterogeneous agent type. Covers remote platform agents (`hermes`,
   * `openclaw`) and local CLI agents (`devin`, `claude-code`, `codex`, …)
   * dispatched through `lh connect` device gateway.
   */
  agentType: string;
  operationId: string;
  parentOperationId?: string;
  pid: number;
  startedAt: string;
  taskId: string;
  topicId: string;
  /**
   * Workspace that owns the dispatched topic. Persisted so the cancel-time
   * notify still scopes to the right workspace after the daemon restarts.
   */
  workspaceId?: string;
}

function getRegistryPath(): string {
  return path.join(os.homedir(), CLI_CONFIG_DIR_NAME, 'task-registry.json');
}

function readRegistry(): Record<string, TaskEntry> {
  try {
    return JSON.parse(fs.readFileSync(getRegistryPath(), 'utf8')) as Record<string, TaskEntry>;
  } catch {
    return {};
  }
}

function writeRegistry(entries: Record<string, TaskEntry>): void {
  const dir = path.dirname(getRegistryPath());
  fs.mkdirSync(dir, { mode: 0o700, recursive: true });
  fs.writeFileSync(getRegistryPath(), JSON.stringify(entries, null, 2), { mode: 0o600 });
}

export function saveTask(entry: TaskEntry): void {
  const registry = readRegistry();
  registry[entry.taskId] = entry;
  writeRegistry(registry);
}

export function getTask(taskId: string): TaskEntry | undefined {
  return readRegistry()[taskId];
}

export function removeTask(taskId: string): void {
  const registry = readRegistry();
  delete registry[taskId];
  writeRegistry(registry);
}

export function listTasks(): TaskEntry[] {
  return Object.values(readRegistry());
}