// server/modules/orchestration/a2a/task-store.ts
// A2A task persistence backed by shared JsonStore for atomic writes and corruption recovery.
// Exposes a Map-compatible surface (get/set/delete/size/entries/values) so routes.ts
// can treat the store as a typed Map while still benefiting from durable JSON storage.
import os from 'os';
import path from 'path';
import { JsonStore } from '../../../database/json-store.js';
const filePath = path.join(os.homedir(), '.pixcode', 'a2a-tasks.json');
export class A2ATaskStore {
    store;
    constructor(_options) {
        this.store = new JsonStore(filePath, { a2a_tasks: [] });
    }
    // ── Map-compatible API ─────────────────────────────────────────────────────
    get(id) {
        return this.store.findWhere('a2a_tasks', (t) => t.id === id) ?? undefined;
    }
    set(task) {
        const existing = this.get(task.id);
        if (existing) {
            this.store.updateWhere('a2a_tasks', (t) => t.id === task.id, task);
        }
        else {
            this.store.insert('a2a_tasks', task, { autoId: false });
        }
    }
    delete(id) {
        this.store.deleteWhere('a2a_tasks', (t) => t.id === id);
    }
    get size() {
        return this.store.filterWhere('a2a_tasks', () => true).length;
    }
    *entries() {
        const tasks = this.store.filterWhere('a2a_tasks', () => true);
        for (const task of tasks) {
            yield [task.id, task];
        }
    }
    *values() {
        const tasks = this.store.filterWhere('a2a_tasks', () => true);
        for (const task of tasks) {
            yield task;
        }
    }
    // ── Query helpers ──────────────────────────────────────────────────────────
    list(options = {}) {
        const { state, contextId, adapterId, limit = 50 } = options;
        let tasks = this.store.filterWhere('a2a_tasks', (t) => {
            if (state && t.state !== state)
                return false;
            if (contextId && t.contextId !== contextId)
                return false;
            if (adapterId && t.metadata?.adapterId !== adapterId)
                return false;
            return true;
        });
        if (limit > 0)
            tasks = tasks.slice(0, limit);
        return tasks;
    }
    summarize(task) {
        return {
            id: task.id,
            contextId: task.contextId,
            state: task.state,
            adapterId: task.metadata?.adapterId,
            adapterSelector: task.metadata?.adapterSelector,
            error: task.error,
            createdAt: task.createdAt,
            updatedAt: task.updatedAt,
            messageCount: task.history?.length ?? 0,
            artifactCount: task.artifacts?.length ?? 0,
            lastMessage: task.history?.at(-1),
        };
    }
    // ── Legacy named methods (kept for any future internal callers) ────────────
    getTask(id) {
        return this.get(id);
    }
    getTaskByIndex(index) {
        const tasks = this.store.filterWhere('a2a_tasks', () => true);
        return tasks[index] ?? undefined;
    }
    getTasks() {
        return this.store.filterWhere('a2a_tasks', () => true);
    }
    createTask(task) {
        this.store.insert('a2a_tasks', task, { autoId: false });
    }
    updateTask(id, updates) {
        const existing = this.get(id);
        if (!existing)
            return;
        const updated = { ...existing, ...updates, id: existing.id };
        this.store.updateWhere('a2a_tasks', (t) => t.id === id, updated);
    }
    updateTaskState(id, state) {
        this.updateTask(id, { status: { state } });
    }
    deleteTask(id) {
        this.delete(id);
    }
    appendHistory(id, entry) {
        const task = this.get(id);
        if (!task)
            return;
        const history = [...(task.history ?? []), entry];
        this.updateTask(id, { history });
    }
    addArtifact(id, artifact) {
        const task = this.get(id);
        if (!task)
            return;
        const artifacts = [...(task.artifacts ?? []), artifact];
        this.updateTask(id, { artifacts });
    }
    addMessage(id, message) {
        const task = this.get(id);
        if (!task)
            return;
        const history = [...(task.history ?? []), message];
        this.updateTask(id, { history });
    }
    updateMessage(id, messageId, updates) {
        const task = this.get(id);
        if (!task?.history)
            return;
        const history = task.history.map((m) => m.messageId === messageId ? { ...m, ...updates } : m);
        this.updateTask(id, { history });
    }
    getMessage(id, messageId) {
        const task = this.get(id);
        return task?.history?.find((m) => m.messageId === messageId);
    }
    getMessages(id) {
        const task = this.get(id);
        return task?.history ?? [];
    }
    clearMessages(id) {
        this.updateTask(id, { history: [] });
    }
}
export const a2aTaskStore = new A2ATaskStore();
//# sourceMappingURL=task-store.js.map