Files
Codewalkers/apps/server/db/repositories/agent-repository.ts
Lukas May 28521e1c20 chore: merge main into cw/small-change-flow
Integrates main branch changes (headquarters dashboard, task retry count,
agent prompt persistence, remote sync improvements) with the initiative's
errand agent feature. Both features coexist in the merged result.

Key resolutions:
- Schema: take main's errands table (nullable projectId, no conflictFiles,
  with errandsRelations); migrate to 0035_faulty_human_fly
- Router: keep both errandProcedures and headquartersProcedures
- Errand prompt: take main's simpler version (no question-asking flow)
- Manager: take main's status check (running|idle only, no waiting_for_input)
- Tests: update to match removed conflictFiles field and undefined vs null
2026-03-06 16:48:12 +01:00

121 lines
3.0 KiB
TypeScript

/**
* Agent Repository Port Interface
*
* Port for Agent aggregate operations.
* Implementations (Drizzle, etc.) are adapters.
*/
import type { Agent } from '../schema.js';
import type { AgentMode } from '../../agent/types.js';
/**
* Agent status values.
*/
export type AgentStatus = 'idle' | 'running' | 'waiting_for_input' | 'stopped' | 'crashed';
/**
* Data for creating a new agent.
* Omits system-managed fields and makes optional fields explicit.
*/
export interface CreateAgentData {
name: string;
worktreeId: string;
taskId?: string | null;
initiativeId?: string | null;
sessionId?: string | null;
status?: AgentStatus;
mode?: AgentMode; // Defaults to 'execute' if not provided
provider?: string; // Defaults to 'claude' if not provided
accountId?: string | null;
}
/**
* Data for updating an existing agent.
* All fields optional. System-managed fields (id, createdAt, updatedAt) are excluded.
*/
export interface UpdateAgentData {
name?: string;
worktreeId?: string;
taskId?: string | null;
initiativeId?: string | null;
sessionId?: string | null;
status?: AgentStatus;
mode?: AgentMode;
provider?: string;
accountId?: string | null;
pid?: number | null;
exitCode?: number | null;
prompt?: string | null;
outputFilePath?: string | null;
result?: string | null;
pendingQuestions?: string | null;
userDismissedAt?: Date | null;
updatedAt?: Date;
}
/**
* Agent Repository Port
*
* Defines operations for the Agent aggregate.
* Enables agent state persistence for session resumption and listing.
*/
export interface AgentRepository {
/**
* Create a new agent.
* Generates id and sets timestamps automatically.
* Name must be unique.
*/
create(agent: CreateAgentData): Promise<Agent>;
/**
* Find an agent by its ID.
* Returns null if not found.
*/
findById(id: string): Promise<Agent | null>;
/**
* Find an agent by its human-readable name.
* Returns null if not found.
*/
findByName(name: string): Promise<Agent | null>;
/**
* Find an agent by its associated task.
* Returns null if no agent is assigned to that task.
*/
findByTaskId(taskId: string): Promise<Agent | null>;
/**
* Find an agent by its Claude CLI session ID.
* Used for session resumption.
* Returns null if not found.
*/
findBySessionId(sessionId: string): Promise<Agent | null>;
/**
* Find all agents.
* Returns empty array if none exist.
*/
findAll(): Promise<Agent[]>;
/**
* Find agents by status.
* Returns empty array if no agents have that status.
*/
findByStatus(status: AgentStatus): Promise<Agent[]>;
/**
* Update an agent with partial data.
* Only provided fields are updated, others remain unchanged.
* Throws if agent not found.
* Updates updatedAt timestamp automatically.
*/
update(id: string, data: UpdateAgentData): Promise<Agent>;
/**
* Delete an agent.
* Throws if agent not found.
*/
delete(id: string): Promise<void>;
}