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
1,021 changes: 872 additions & 149 deletions packages/api-client/src/schema.d.ts

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions packages/api/migrations/0016_projects.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
CREATE TABLE app.projects (
tenant_id uuid NOT NULL REFERENCES app.tenants(id) ON DELETE CASCADE,
id uuid NOT NULL, name text NOT NULL, description text,
created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (tenant_id, id), UNIQUE (tenant_id, name)
);
CREATE INDEX projects_tenant_updated ON app.projects(tenant_id, updated_at);
INSERT INTO app.projects(tenant_id,id,name,description)
SELECT t.id, md5(t.id::text || ':default')::uuid, 'Default', 'Jobs migrated from the legacy jobs collection.'
FROM app.tenants t WHERE EXISTS (SELECT 1 FROM app.jobs j WHERE j.tenant_id=t.id)
ON CONFLICT (tenant_id,name) DO NOTHING;
ALTER TABLE app.jobs ADD COLUMN project_id uuid;
UPDATE app.jobs j SET project_id=p.id FROM app.projects p WHERE p.tenant_id=j.tenant_id AND p.name='Default' AND j.project_id IS NULL;
ALTER TABLE app.jobs ALTER COLUMN project_id SET NOT NULL;
ALTER TABLE app.jobs ADD CONSTRAINT jobs_project_fk FOREIGN KEY (tenant_id,project_id) REFERENCES app.projects(tenant_id,id) ON DELETE RESTRICT;
CREATE INDEX jobs_tenant_project_updated ON app.jobs(tenant_id,project_id,updated_at);
32 changes: 32 additions & 0 deletions packages/api/migrations/0017_execution_targets.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
CREATE TABLE app.execution_targets (
tenant_id uuid NOT NULL REFERENCES app.tenants(id) ON DELETE CASCADE,
id uuid NOT NULL,
name text NOT NULL,
integration_kind text NOT NULL,
integration_id text NOT NULL,
tags jsonb NOT NULL DEFAULT '[]'::jsonb,
agent_kind text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (tenant_id, id),
UNIQUE (tenant_id, name),
UNIQUE (tenant_id, integration_kind, integration_id, name),
CHECK (integration_kind IN ('exe', 'amp'))
);
CREATE INDEX execution_targets_tenant_updated ON app.execution_targets(tenant_id, updated_at, id);

INSERT INTO app.execution_targets(tenant_id, id, name, integration_kind, integration_id, tags, agent_kind)
SELECT c.tenant_id, md5(c.tenant_id::text || ':target:' || c.kind)::uuid,
CASE WHEN c.kind LIKE 'exe:%' THEN 'exe.dev · ' || right(split_part(c.kind, ':', 2), 8) ELSE 'Amp · ' || right(split_part(c.kind, ':', 2), 8) END,
split_part(c.kind, ':', 1), split_part(c.kind, ':', 2), '[]'::jsonb,
CASE WHEN c.kind LIKE 'exe:%' THEN COALESCE((SELECT j.execution_target->>'agentKind' FROM app.jobs j WHERE j.tenant_id=c.tenant_id AND j.execution_target->>'connectionId'=split_part(c.kind, ':', 2) LIMIT 1), 'codex') ELSE 'Amp' END
FROM app.connections c
WHERE c.kind LIKE 'exe:%' OR c.kind LIKE 'amp:%'
ON CONFLICT (tenant_id, name) DO NOTHING;

UPDATE app.jobs j
SET execution_target = j.execution_target || jsonb_build_object('targetId', t.id)
FROM app.execution_targets t
WHERE t.tenant_id=j.tenant_id
AND t.integration_id=j.execution_target->>'connectionId'
AND t.integration_kind=CASE WHEN j.execution_target->>'agentKind'='Amp' THEN 'amp' ELSE 'exe' END;
4 changes: 4 additions & 0 deletions packages/api/migrations/0018_run_target_snapshot.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER TABLE app.runs ADD COLUMN execution_target jsonb;
UPDATE app.runs r SET execution_target = j.execution_target || jsonb_build_object('model', j.model, 'effort', j.effort)
FROM app.jobs j WHERE j.tenant_id=r.tenant_id AND j.id=r.job_id AND r.execution_target IS NULL;
ALTER TABLE app.runs ALTER COLUMN execution_target SET NOT NULL;
19 changes: 19 additions & 0 deletions packages/api/src/api-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { exeConnection, integrationStatus, authorizedClient, accessToken, create
import { executionTargetResponse, triggerAvailabilityResponse, namedOption, providerOptions, githubInstallation, githubRepository, tailIntegration } from "./editor-contracts";
import { runSummaryResponse, runPageResponse, runStatusResponse, revisionTracePage, revisionTraceQuery } from "./run-read-contracts";
import { jobPageQuery, jobSummaryPage, jobSelectorPage, jobResponse, jobUpdateInput } from "./job-contracts";
import { projectInput, projectResponse } from "./project-contracts";
import { z } from "zod";
import type { ApiService } from "./flow-service";
import {
Expand Down Expand Up @@ -73,6 +74,8 @@ const tailBody = z.object({
.meta({ writeOnly: true }),
generateSecret: z.boolean().optional(),
});
const executionTargetBody = z.object({ name: z.string().trim().min(1).max(120), integrationKind: z.enum(["exe", "amp"]), integrationId: z.string().min(1), tags: z.array(z.string().regex(/^[A-Za-z0-9][A-Za-z0-9._:-]{0,63}$/)).max(20).default([]), agentKind: z.string().min(1).max(40) });
const executionTargetUpdateBody = executionTargetBody;
const deliveryQuery = z.object({
provider: z.string().optional(),
deliveryId: z.string().optional(),
Expand Down Expand Up @@ -236,6 +239,10 @@ export const API_ROUTES: RouteContract[] = [
},
(service) => service.listExecutionTargets(),
),
route("POST", "/api/v1/execution-targets", "flows:write", { summary: "Create execution target", responses: { "201": { description: "Created", content: { "application/json": { schema: jsonSchema(executionTargetResponse, "output") } } } } }, (service, { body }) => service.createExecutionTarget(body), { body: executionTargetBody }),
route("PUT", "/api/v1/execution-targets/{targetId}", "flows:write", { summary: "Update execution target", responses: { "200": { description: "Updated", content: { "application/json": { schema: jsonSchema(executionTargetResponse, "output") } } } } }, (service, { params, body }) => service.updateExecutionTarget(params.targetId, body), { body: executionTargetUpdateBody }),
route("DELETE", "/api/v1/execution-targets/{targetId}", "flows:write", { summary: "Delete execution target", responses: { "200": { description: "Deleted" } } }, (service, { params }) => service.deleteExecutionTarget(params.targetId)),
route("POST", "/api/v1/execution-targets/{targetId}/diagnostics", "flows:write", { summary: "Diagnose an execution target", responses: { "200": { description: "Target-scoped credential-safe diagnostic result", content: { "application/json": { schema: jsonSchema(z.object({ ok: z.boolean(), targetId: z.string(), integrationId: z.string(), agentKind: z.string(), tags: z.array(z.string()), permissions: z.object({ ok: z.boolean(), missing: z.array(z.string()), tags: z.array(z.string()) }), agent: z.object({ ok: z.boolean(), models: z.array(z.string()).optional(), error: z.string().optional() }).nullable() }), "output") } } } } }, (service, { params }) => service.diagnoseExecutionTarget(params.targetId)),
route(
"GET",
"/api/v1/job-trigger-availability",
Expand Down Expand Up @@ -635,6 +642,18 @@ export const API_ROUTES: RouteContract[] = [
summary: "Search bounded lifecycle job options",
responses: { "200": { description: "Selector page; same ordering/filter semantics as job-summaries", content: { "application/json": { schema: jsonSchema(jobSelectorPage, "output") } } } },
}, (service, { query }) => service.listJobSummaries(query, true), { query: jobPageQuery }),
route(
"GET",
"/api/v1/projects",
"flows:read",
{ summary: "List projects", responses: { "200": { description: "Projects", content: { "application/json": { schema: jsonSchema(z.array(projectResponse), "output") } } } } },
(service) => service.listProjects(),
),
route("POST", "/api/v1/projects", "flows:write", { summary: "Create project", responses: { "201": { description: "Created", content: { "application/json": { schema: jsonSchema(projectResponse, "output") } } } } }, (service, { body }) => service.createProject(body), { body: projectInput }),
route("GET", "/api/v1/projects/{projectId}", "flows:read", { summary: "Get project", responses: { "200": { description: "Project", content: { "application/json": { schema: jsonSchema(projectResponse, "output") } } } } }, (service, { params }) => service.getProject(params.projectId)),
route("PUT", "/api/v1/projects/{projectId}", "flows:write", { summary: "Update project", responses: { "200": { description: "Updated", content: { "application/json": { schema: jsonSchema(projectResponse, "output") } } } } }, (service, { params, body }) => service.updateProject(params.projectId, body), { body: projectInput }),
route("DELETE", "/api/v1/projects/{projectId}", "flows:write", { summary: "Delete empty project", responses: { "200": { description: "Deleted" } } }, (service, { params }) => service.deleteProject(params.projectId)),
route("POST", "/api/v1/jobs/{jobId}/project", "flows:write", { summary: "Move a job to a project", responses: { "200": { description: "Moved", content: { "application/json": { schema: jsonSchema(jobResponse, "output") } } } } }, (service, { params, body }) => service.moveJob(params.jobId, body.projectId, body.expectedUpdatedAt), { body: z.object({ projectId: z.guid(), expectedUpdatedAt: z.iso.datetime().optional() }) }),
route(
"GET",
"/api/v1/jobs",
Expand Down
3 changes: 2 additions & 1 deletion packages/api/src/editor-contracts.ts
Original file line number Diff line number Diff line change
@@ -1,8 +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(),
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(), tags: z.array(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()),
});
export const executionTargetResource = z.object({ id: z.string(), name: z.string(), integrationKind: z.enum(["exe", "amp"]), integrationId: z.string(), tags: z.array(z.string()), agentKind: z.string(), createdAt: z.string(), updatedAt: z.string() });
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
3 changes: 2 additions & 1 deletion packages/api/src/flow-schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const matchRuleSchema = z.object({
targetId: z.string().min(1),
});
export const listRunsSchema = z.object({
jobId: z.string().min(1).optional(), state: z.union([runStateSchema, z.array(runStateSchema).min(1).max(10)]).optional(),
projectId: z.string().min(1).optional(), jobId: z.string().min(1).optional(), state: z.union([runStateSchema, z.array(runStateSchema).min(1).max(10)]).optional(),
contextQuery: z.string().min(1).max(50_000).optional(),
sort: z.enum(["job", "run", "status", "created", "agent"]).optional(),
direction: z.enum(["asc", "desc"]).optional(),
Expand All @@ -34,6 +34,7 @@ export const triggerSchema = z.discriminatedUnion("kind", [
}).strict() }).strict(),
]);
export const jobInputSchema = z.object({
projectId: z.guid().optional(),
name: z.string().trim().min(1).max(120),
slug: z.string().regex(/^[a-z][a-z0-9_-]{0,29}$/),
promptTemplate: z.string().min(1).max(50_000),
Expand Down
Loading
Loading