Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions packages/api-client/src/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2873,10 +2873,18 @@ export interface operations {
cwd: string;
agentKind: string;
integrationId?: string;
tags?: string[];
model?: string;
effort?: string;
models?: string[];
modelsRefreshedAt?: string | null;
efforts?: string[];
capabilities: string[];
referencingProjects?: {
id: string;
name: string;
}[];
referencingJobCount?: number;
}[];
};
};
Expand Down Expand Up @@ -2906,6 +2914,14 @@ export interface operations {
integrationKind: "exe" | "amp";
integrationId: string;
agentKind: string;
tags?: string[];
/** @default */
model?: string;
/** @default */
effort?: string;
models?: string[];
/** Format: date-time */
expectedUpdatedAt?: string;
};
};
};
Expand All @@ -2925,10 +2941,18 @@ export interface operations {
cwd: string;
agentKind: string;
integrationId?: string;
tags?: string[];
model?: string;
effort?: string;
models?: string[];
modelsRefreshedAt?: string | null;
efforts?: string[];
capabilities: string[];
referencingProjects?: {
id: string;
name: string;
}[];
referencingJobCount?: number;
};
};
};
Expand Down Expand Up @@ -2961,6 +2985,14 @@ export interface operations {
integrationKind: "exe" | "amp";
integrationId: string;
agentKind: string;
tags?: string[];
/** @default */
model?: string;
/** @default */
effort?: string;
models?: string[];
/** Format: date-time */
expectedUpdatedAt?: string;
};
};
};
Expand All @@ -2980,10 +3012,18 @@ export interface operations {
cwd: string;
agentKind: string;
integrationId?: string;
tags?: string[];
model?: string;
effort?: string;
models?: string[];
modelsRefreshedAt?: string | null;
efforts?: string[];
capabilities: string[];
referencingProjects?: {
id: string;
name: string;
}[];
referencingJobCount?: number;
};
};
};
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/api-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const tailBody = z.object({
.meta({ writeOnly: true }),
generateSecret: z.boolean().optional(),
});
const executionTargetBody = z.object({ name: z.string().trim().max(120).default(""), integrationKind: z.enum(["exe", "amp"]), integrationId: z.string().min(1), agentKind: z.string().min(1).max(40) });
const executionTargetBody = z.object({ name: z.string().trim().max(120).default(""), integrationKind: z.enum(["exe", "amp"]), integrationId: z.string().min(1), agentKind: z.string().min(1).max(40), tags: z.array(z.string().regex(/^[A-Za-z0-9][A-Za-z0-9._:-]{0,63}$/)).max(20).optional(), model: z.string().trim().max(120).default(""), effort: z.string().trim().max(40).default(""), models: z.array(z.string().trim().min(1).max(120)).max(100).optional(), expectedUpdatedAt: z.string().datetime().optional() });
const executionTargetUpdateBody = executionTargetBody;
const deliveryQuery = z.object({
provider: z.string().optional(),
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/editor-contracts.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { z } from "zod";
export const executionTargetResponse = z.object({
id: z.string(), kind: z.enum(["exe-vm", "amp"]), name: z.string(), workspace: z.string(), cwd: z.string(), agentKind: z.string(), integrationId: z.string().optional(),
models: z.array(z.string()).optional(), modelsRefreshedAt: z.string().nullable().optional(), efforts: z.array(z.string()).optional(), capabilities: z.array(z.string()),
tags: z.array(z.string()).optional(), model: z.string().optional(), effort: z.string().optional(), models: z.array(z.string()).optional(), modelsRefreshedAt: z.string().nullable().optional(), efforts: z.array(z.string()).optional(), capabilities: z.array(z.string()), referencingProjects: z.array(z.object({ id: z.string(), name: z.string() })).optional(), referencingJobCount: z.number().int().optional(),
});
export const executionTargetResource = z.object({ id: z.string(), name: z.string(), integrationKind: z.enum(["exe", "amp"]), integrationId: z.string(), agentKind: z.string(), createdAt: z.string(), updatedAt: z.string() });
export const executionTargetResource = z.object({ id: z.string(), name: z.string(), integrationKind: z.enum(["exe", "amp"]), integrationId: z.string(), agentKind: z.string(), tags: z.array(z.string()), models: z.array(z.string()), efforts: z.array(z.string()), createdAt: z.string(), updatedAt: z.string(), referencingProjects: z.array(z.object({ id: z.string(), name: z.string() })), referencingJobCount: z.number().int() });
export const triggerAvailabilityResponse = z.object({ manual: z.literal(true), schedule: z.literal(true), jobLifecycle: z.literal(true), linear: z.boolean(), clickup: z.boolean(), github: z.boolean(), cloudflareTail: z.boolean() });
export const namedOption = z.object({ id: z.string(), name: z.string() });
export const providerOptions = z.object({ statuses: z.array(namedOption), users: z.array(namedOption), labels: z.array(namedOption) });
Expand Down
6 changes: 3 additions & 3 deletions packages/api/src/flow-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,12 @@ export class ApiService {
}
async listExecutionTargets() {
await this.authorize("jobs:read"); const records = await this.targetRecords().list(); const targets: any[] = [];
for (const target of records) { const value = await this.connections().get<any>(`${target.integrationKind}:${target.integrationId}`); if (!value) continue; targets.push({ id: target.id, kind: target.integrationKind === "amp" ? "amp" : "exe-vm", name: target.name, integrationId: target.integrationId, workspace: target.integrationKind === "amp" ? value.project : "ephemeral", cwd: target.integrationKind === "amp" ? "Cloud orb" : "/home/exedev/workspace", agentKind: target.agentKind, models: target.models ?? [], modelsRefreshedAt: target.modelsRefreshedAt ?? null, efforts: target.agentKind === "codex" ? ["minimal", "low", "medium", "high", "xhigh"] : [], capabilities: target.integrationKind === "amp" ? ["stop"] : ["recovery", "stop"] }); }
for (const target of records) { const value = await this.connections().get<any>(`${target.integrationKind}:${target.integrationId}`); if (!value) continue; targets.push({ id: target.id, kind: target.integrationKind === "amp" ? "amp" : "exe-vm", name: target.name, integrationId: target.integrationId, tags: target.tags, model: target.model, effort: target.effort, workspace: target.integrationKind === "amp" ? value.project : "ephemeral", cwd: target.integrationKind === "amp" ? "Cloud orb" : "/home/exedev/workspace", agentKind: target.agentKind, models: target.models ?? [], modelsRefreshedAt: target.modelsRefreshedAt ?? null, efforts: target.efforts, capabilities: target.integrationKind === "amp" ? ["stop"] : ["recovery", "stop"], referencingProjects: target.referencingProjects, referencingJobCount: target.referencingJobCount }); }
return publicValue(z.array(executionTargetResponse), targets);
}
async listExecutionTargetResources() { await this.authorize("integrations:read"); return publicValue(z.array((await import("./editor-contracts")).executionTargetResource), await this.targetRecords().list()); }
async createExecutionTarget(input: { name: string; integrationKind: "exe" | "amp"; integrationId: string; agentKind: string }) { await this.authorize("integrations:write"); const value = await this.connections().get<any>(`${input.integrationKind}:${input.integrationId}`); if (!value) throw new ServiceError(404, "not_found", "Integration not found"); if (input.integrationKind === "exe" && !["codex", "claude", "pi"].includes(input.agentKind)) throw new ServiceError(400, "invalid_request", "Unsupported agent kind"); return this.targetRecords().create(input); }
async updateExecutionTarget(id: string, input: { name: string; integrationKind: "exe" | "amp"; integrationId: string; agentKind: string; expectedUpdatedAt?: string }) { await this.authorize("integrations:write"); const value = await this.connections().get<any>(`${input.integrationKind}:${input.integrationId}`); if (!value) throw new ServiceError(404, "not_found", "Integration not found"); if (input.integrationKind === "exe" && !["codex", "claude", "pi"].includes(input.agentKind)) throw new ServiceError(400, "invalid_request", "Unsupported agent kind"); const result = await this.targetRecords().update(id, input, input.expectedUpdatedAt); if (!result) throw new ServiceError(409, "stale_target", "This target changed. Reload it before saving."); return result; }
async createExecutionTarget(input: { name: string; integrationKind: "exe" | "amp"; integrationId: string; agentKind: string; tags?: string[]; model?: string; effort?: string; models?: string[] }) { await this.authorize("integrations:write"); const value = await this.connections().get<any>(`${input.integrationKind}:${input.integrationId}`); if (!value) throw new ServiceError(404, "not_found", "Integration not found"); if (!input.name.trim()) throw new ServiceError(400, "invalid_request", "Enter a display name before saving the target."); if (input.integrationKind === "exe" && !["codex", "claude", "pi"].includes(input.agentKind)) throw new ServiceError(400, "invalid_request", "Choose a supported exe.dev agent."); const tags: string[] = (input.tags ?? value.tags ?? []).map(String); if (tags.length > 20 || tags.some((tag: string) => !/^[A-Za-z0-9][A-Za-z0-9._:-]{0,63}$/.test(tag))) throw new ServiceError(400, "invalid_request", "VM tags must start with a letter or number and contain only letters, numbers, dots, underscores, colons, or hyphens."); return this.targetRecords().create({ ...input, tags, model: input.model ?? "", effort: input.effort ?? "", models: input.models ?? value.models ?? [] }); }
async updateExecutionTarget(id: string, input: { name: string; integrationKind: "exe" | "amp"; integrationId: string; agentKind: string; tags?: string[]; model?: string; effort?: string; models?: string[]; expectedUpdatedAt?: string }) { await this.authorize("integrations:write"); const value = await this.connections().get<any>(`${input.integrationKind}:${input.integrationId}`); if (!value) throw new ServiceError(404, "not_found", "Integration not found"); if (!input.name.trim()) throw new ServiceError(400, "invalid_request", "Enter a display name before saving the target."); if (input.integrationKind === "exe" && !["codex", "claude", "pi"].includes(input.agentKind)) throw new ServiceError(400, "invalid_request", "Choose a supported exe.dev agent."); const result = await this.targetRecords().update(id, { ...input, tags: input.tags ?? [], model: input.model ?? "", effort: input.effort ?? "", models: input.models ?? value.models ?? [] }, input.expectedUpdatedAt); if (!result) throw new ServiceError(409, "stale_target", "This target changed. Reload it before saving."); return result; }
async deleteExecutionTarget(id: string) { await this.authorize("integrations:write"); const result = await this.targetRecords().remove(id); if (result === "missing") throw new ServiceError(404, "not_found", "Execution target not found"); if (result === "referenced") throw new ServiceError(409, "conflict", "This target is used by a job"); if (result === "active") throw new ServiceError(409, "conflict", "This target is used by an active or queued run"); return { id, deleted: true }; }
async diagnoseExeIntegration(connectionId: string) { await this.authorize("integrations:write"); const value = await new IntegrationService(databaseFor(this.env), this.auth.tenantId, this.env.CREDENTIAL_ENCRYPTION_KEY).diagnoseExe(connectionId); if (!value) throw new ServiceError(404,"not_found","Integration not found"); return value; }
async diagnoseExecutionTarget(id: string) { await this.authorize("integrations:write"); const target = await this.targetRecords().get(id); if (!target) throw new ServiceError(404, "not_found", "Execution target not found"); if (target.integrationKind !== "exe") throw new ServiceError(400, "invalid_request", "Diagnostics are only available for exe.dev targets"); const value = await new IntegrationService(databaseFor(this.env), this.auth.tenantId, this.env.CREDENTIAL_ENCRYPTION_KEY).diagnoseExeTarget(id, target.integrationId, [], target.agentKind as "codex" | "claude" | "pi"); if (!value) throw new ServiceError(404, "not_found", "Integration not found"); return value; }
Expand Down
13 changes: 7 additions & 6 deletions packages/api/src/postgres/execution-target-repository.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import type { Database } from "./database";

export type ExecutionTargetRecord = { id: string; name: string; integrationKind: "exe" | "amp"; integrationId: string; agentKind: string; models?: string[]; modelsRefreshedAt?: string | null; createdAt: string; updatedAt: string };
export type ExecutionTargetRecord = { id: string; name: string; integrationKind: "exe" | "amp"; integrationId: string; agentKind: string; tags: string[]; model: string; effort: string; models: string[]; modelsRefreshedAt?: string | null; efforts: string[]; referencingProjects: { id: string; name: string }[]; referencingJobCount: number; createdAt: string; updatedAt: string };

const map = (row: any): ExecutionTargetRecord => ({ id: row.id, name: row.name, integrationKind: row.integration_kind, integrationId: row.integration_id, agentKind: row.agent_kind, models: row.models ?? [], modelsRefreshedAt: row.models_refreshed_at?.toISOString() ?? null, createdAt: row.created_at.toISOString(), updatedAt: row.updated_at.toISOString() });
const map = (row: any): ExecutionTargetRecord => ({ id: row.id, name: row.name, integrationKind: row.integration_kind, integrationId: row.integration_id, agentKind: row.agent_kind, tags: row.tags ?? [], model: row.model ?? "", effort: row.effort ?? "", models: row.models ?? [], modelsRefreshedAt: row.models_refreshed_at?.toISOString() ?? null, efforts: row.agent_kind === "codex" ? ["minimal", "low", "medium", "high", "xhigh"] : [], referencingProjects: row.referencing_projects ?? [], referencingJobCount: Number(row.referencing_job_count ?? 0), createdAt: row.created_at.toISOString(), updatedAt: row.updated_at.toISOString() });

export class ExecutionTargetRepository {
constructor(private database: Database, private tenantId: string) {}
async list() { const result = await this.database.pool.query("SELECT * FROM app.execution_targets WHERE tenant_id=$1 ORDER BY name,id", [this.tenantId]); return result.rows.map(map); }
async get(id: string) { const result = await this.database.pool.query("SELECT * FROM app.execution_targets WHERE tenant_id=$1 AND id=$2", [this.tenantId, id]); return result.rows[0] ? map(result.rows[0]) : null; }
async create(input: Omit<ExecutionTargetRecord, "id" | "createdAt" | "updatedAt">) { const result = await this.database.pool.query("INSERT INTO app.execution_targets(tenant_id,id,name,integration_kind,integration_id,agent_kind) VALUES ($1,gen_random_uuid(),$2,$3,$4,$5) RETURNING *", [this.tenantId, input.name.trim(), input.integrationKind, input.integrationId, input.agentKind]); return map(result.rows[0]); }
async update(id: string, input: Pick<ExecutionTargetRecord, "name" | "agentKind" | "integrationKind" | "integrationId">, expectedUpdatedAt?: string) { const result = await this.database.pool.query("UPDATE app.execution_targets SET name=$3,integration_kind=$4,integration_id=$5,agent_kind=$6,updated_at=now() WHERE tenant_id=$1 AND id=$2 AND ($7::timestamptz IS NULL OR updated_at=$7) RETURNING *", [this.tenantId, id, input.name.trim(), input.integrationKind, input.integrationId, input.agentKind, expectedUpdatedAt ?? null]); return result.rows[0] ? map(result.rows[0]) : null; }
private query = `SELECT t.*, count(j.id)::int AS referencing_job_count, COALESCE(jsonb_agg(DISTINCT jsonb_build_object('id', p.id, 'name', p.name)) FILTER (WHERE p.id IS NOT NULL), '[]'::jsonb) AS referencing_projects FROM app.execution_targets t LEFT JOIN app.jobs j ON j.tenant_id=t.tenant_id AND j.execution_target->>'targetId'=t.id::text LEFT JOIN app.projects p ON p.tenant_id=j.tenant_id AND p.id=j.project_id WHERE t.tenant_id=$1 GROUP BY t.tenant_id,t.id ORDER BY t.name,t.id`;
async list() { const result = await this.database.pool.query(this.query, [this.tenantId]); return result.rows.map(map); }
async get(id: string) { const result = await this.database.pool.query(this.query.replace("WHERE t.tenant_id=$1", "WHERE t.tenant_id=$1 AND t.id=$2"), [this.tenantId, id]); return result.rows[0] ? map(result.rows[0]) : null; }
async create(input: Omit<ExecutionTargetRecord, "id" | "createdAt" | "updatedAt" | "referencingProjects" | "referencingJobCount" | "efforts">) { const result = await this.database.pool.query("INSERT INTO app.execution_targets(tenant_id,id,name,integration_kind,integration_id,agent_kind,tags,models,model,effort) VALUES ($1,gen_random_uuid(),$2,$3,$4,$5,$6,$7,$8,$9,$10) RETURNING *", [this.tenantId, input.name.trim(), input.integrationKind, input.integrationId, input.agentKind, JSON.stringify(input.tags), JSON.stringify(input.models), input.model, input.effort]); return map({ ...result.rows[0], referencing_projects: [], referencing_job_count: 0 }); }
async update(id: string, input: Pick<ExecutionTargetRecord, "name" | "agentKind" | "integrationKind" | "integrationId" | "tags" | "models" | "model" | "effort">, expectedUpdatedAt?: string) { const result = await this.database.pool.query("UPDATE app.execution_targets SET name=$3,integration_kind=$4,integration_id=$5,agent_kind=$6,tags=$7,models=$8,model=$9,effort=$10,updated_at=now() WHERE tenant_id=$1 AND id=$2 AND ($11::timestamptz IS NULL OR updated_at=$11) RETURNING *", [this.tenantId, id, input.name.trim(), input.integrationKind, input.integrationId, input.agentKind, JSON.stringify(input.tags), JSON.stringify(input.models), input.model, input.effort, expectedUpdatedAt ?? null]); if (!result.rows[0]) return null; return this.get(id); }
async remove(id: string) { return this.database.transaction(async client => { const target = await client.query("SELECT 1 FROM app.execution_targets WHERE tenant_id=$1 AND id=$2 FOR UPDATE", [this.tenantId, id]); if (!target.rowCount) return "missing" as const; const jobs = await client.query("SELECT 1 FROM app.jobs WHERE tenant_id=$1 AND execution_target->>'targetId'=$2 LIMIT 1", [this.tenantId, id]); if (jobs.rowCount) return "referenced" as const; const runs = await client.query("SELECT 1 FROM app.runs WHERE tenant_id=$1 AND execution_target->>'targetId'=$2 AND state NOT IN ('succeeded','failed','stopped') LIMIT 1", [this.tenantId, id]); if (runs.rowCount) return "active" as const; await client.query("DELETE FROM app.execution_targets WHERE tenant_id=$1 AND id=$2", [this.tenantId, id]); return "deleted" as const; }); }
}
Loading
Loading