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
3 changes: 3 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Linear

This project in Linear is "Factorize"
29 changes: 28 additions & 1 deletion packages/app/src/flow-schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,39 @@ export const flowInputSchema = z.object({
}).strict();
export const flowIdSchema = z.object({ flowId: z.string().min(1) });
export const listRunsSchema = z.object({
flowId: z.string().min(1).optional(), state: runStateSchema.optional(),
jobId: z.string().min(1).optional(), state: runStateSchema.optional(),
limit: z.coerce.number().int().min(1).max(100).default(50), cursor: z.string().min(1).optional(),
});
export const runIdSchema = z.object({ runId: z.string().min(1) });
export const listEventsSchema = z.object({
flowId: z.string().min(1), limit: z.coerce.number().int().min(1).max(100).default(50), cursor: z.string().min(1).optional(),
});

const stringRecordSchema = z.record(z.string(), z.string());
export const triggerSchema = z.discriminatedUnion("kind", [
z.object({ kind: z.literal("manual"), config: z.object({}).strict().default({}) }).strict(),
z.object({ kind: z.literal("schedule"), config: z.record(z.string(), z.unknown()) }).strict(),
z.object({ kind: z.literal("webhook"), config: z.record(z.string(), z.unknown()) }).strict(),
]);
export const jobInputSchema = z.object({
name: z.string().trim().min(1).max(120),
promptTemplate: z.string().min(1).max(50_000),
parameterDefaults: stringRecordSchema.default({}),
concurrencyLimit: z.number().int().min(1).max(50).default(1),
executionTargetId: z.string().min(1),
trigger: triggerSchema,
}).strict().superRefine((value, ctx) => {
if (Object.prototype.hasOwnProperty.call(value.parameterDefaults, "context")) ctx.addIssue({ code: "custom", path: ["parameterDefaults", "context"], message: "context is reserved" });
});
export const jobIdSchema = z.object({ jobId: z.string().min(1) });
export const manualInvocationSchema = z.object({
context: z.string().max(50_000).optional(),
parameters: stringRecordSchema.default({}),
idempotencyKey: z.string().trim().min(1).max(200).optional(),
}).strict().superRefine((value, ctx) => {
if (Object.prototype.hasOwnProperty.call(value.parameters, "context")) ctx.addIssue({ code: "custom", path: ["parameters", "context"], message: "context must use the reserved context field" });
});

export type FlowInput = z.infer<typeof flowInputSchema>;
export type JobInput = z.infer<typeof jobInputSchema>;
export type ManualInvocationInput = z.infer<typeof manualInvocationSchema>;
10 changes: 9 additions & 1 deletion packages/app/src/flow-service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Env, OAuthProps } from "./types";
import type { FlowInput } from "./flow-schemas";
import type { FlowInput, JobInput, ManualInvocationInput } from "./flow-schemas";
import { preparePublicSource, publicSource, resolveContextTemplate } from "./source-lifecycle";
import type { FlowSource } from "./types";

Expand Down Expand Up @@ -76,4 +76,12 @@ export class FlowService {
getRun(runId: string) { return this.call("runs:read", `/v1/runs/${encodeURIComponent(runId)}`); }
listFlowEvents(query: URLSearchParams) { return this.call("runs:read", `/v1/events?${query}`); }
stopRun(runId: string) { return this.call("runs:write", `/v1/runs/${encodeURIComponent(runId)}/stop`, { method: "POST" }); }
listJobs() { return this.call("flows:read", "/v1/jobs"); }
getJob(jobId: string) { return this.call("flows:read", `/v1/jobs/${encodeURIComponent(jobId)}`); }
createJob(input: JobInput) { return this.call("flows:write", "/v1/jobs", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(input) }); }
updateJob(jobId: string, input: JobInput) { return this.call("flows:write", `/v1/jobs/${encodeURIComponent(jobId)}`, { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify(input) }); }
deleteJob(jobId: string) { return this.call("flows:write", `/v1/jobs/${encodeURIComponent(jobId)}`, { method: "DELETE" }); }
setJobEnabled(jobId: string, enabled: boolean) { return this.call("flows:write", `/v1/jobs/${encodeURIComponent(jobId)}/${enabled ? "enable" : "disable"}`, { method: "POST" }); }
invokeJob(jobId: string, input: ManualInvocationInput) { return this.call("runs:write", `/v1/jobs/${encodeURIComponent(jobId)}/invocations`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(input) }); }
listExecutionTargets() { return this.call("flows:read", "/v1/execution-targets"); }
}
24 changes: 22 additions & 2 deletions packages/app/src/protected-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { McpServer, WebStandardStreamableHTTPServerTransport } from "@modelconte
import { createMcpHonoApp } from "@modelcontextprotocol/hono";
import { z } from "zod";
import { FlowService, ServiceError } from "./flow-service";
import { flowIdSchema, flowInputSchema, listEventsSchema, listRunsSchema, runIdSchema } from "./flow-schemas";
import { flowIdSchema, flowInputSchema, jobIdSchema, jobInputSchema, listEventsSchema, listRunsSchema, manualInvocationSchema, runIdSchema } from "./flow-schemas";
import type { Env, OAuthProps } from "./types";

function errorResponse(error: unknown): Response {
Expand Down Expand Up @@ -48,6 +48,17 @@ export class ProtectedApiHandler extends WorkerEntrypoint<Env, OAuthProps> {
if (request.method === "GET" && path === "/api/v1/projects") return Response.json(await service.listProjects());
if (request.method === "GET" && path === "/api/v1/flow-options") return Response.json(await service.listFlowOptions());
if (request.method === "GET" && path === "/api/v1/exe-connections") return Response.json(await service.listExeConnections());
if (request.method === "GET" && path === "/api/v1/execution-targets") return Response.json(await service.listExecutionTargets());
if (request.method === "GET" && path === "/api/v1/jobs") return Response.json(await service.listJobs());
if (request.method === "POST" && path === "/api/v1/jobs") return Response.json(await service.createJob(jobInputSchema.parse(await request.json())), { status: 201 });
const invocationMatch = path.match(/^\/api\/v1\/jobs\/([^/]+)\/invocations$/);
if (invocationMatch && request.method === "POST") return Response.json(await service.invokeJob(decodeURIComponent(invocationMatch[1]), manualInvocationSchema.parse(await request.json())), { status: 202 });
const enabledMatch = path.match(/^\/api\/v1\/jobs\/([^/]+)\/(enable|disable)$/);
if (enabledMatch && request.method === "POST") return Response.json(await service.setJobEnabled(decodeURIComponent(enabledMatch[1]), enabledMatch[2] === "enable"));
const jobMatch = path.match(/^\/api\/v1\/jobs\/([^/]+)$/);
if (jobMatch && request.method === "GET") return Response.json(await service.getJob(decodeURIComponent(jobMatch[1])));
if (jobMatch && request.method === "PUT") return Response.json(await service.updateJob(decodeURIComponent(jobMatch[1]), jobInputSchema.parse(await request.json())));
if (jobMatch && request.method === "DELETE") return Response.json(await service.deleteJob(decodeURIComponent(jobMatch[1])));
if (request.method === "GET" && path === "/api/v1/runs") { const q = listRunsSchema.parse(queryInput(url)); return Response.json(await service.listRuns(queryOf(q))); }
const runMatch = path.match(/^\/api\/v1\/runs\/([^/]+)$/);
if (runMatch && request.method === "GET") return Response.json(await service.getRun(decodeURIComponent(runMatch[1])));
Expand All @@ -72,10 +83,19 @@ export class ProtectedApiHandler extends WorkerEntrypoint<Env, OAuthProps> {
tool("list_projects", "List Linear projects", z.object({}), () => service.listProjects());
tool("list_flow_options", "List match-rule options", z.object({}), () => service.listFlowOptions());
tool("list_exe_connections", "List safe metadata for saved exe.dev connections. Select a connectionId before creating or updating a flow; API tokens are never returned.", z.object({}), () => service.listExeConnections());
tool("list_runs", "List and filter flow runs", listRunsSchema, (input: any) => service.listRuns(queryOf(input)));
tool("list_runs", "List and filter job runs", listRunsSchema, (input: any) => service.listRuns(queryOf(input)));
tool("get_run", "Get a run and its current output", runIdSchema, ({ runId }: any) => service.getRun(runId));
tool("list_flow_events", "List webhook activity for a flow", listEventsSchema, (input: any) => service.listFlowEvents(queryOf(input)));
tool("stop_run", "Stop an active run", runIdSchema, ({ runId }: any) => service.stopRun(runId));
tool("list_jobs", "List jobs", z.object({}), () => service.listJobs());
tool("get_job", "Get a job", jobIdSchema, ({ jobId }: any) => service.getJob(jobId));
tool("create_job", "Create a job using an executionTargetId returned by list_execution_targets. Credentials are never accepted or returned.", jobInputSchema, (input: any) => service.createJob(input));
tool("update_job", "Replace a job configuration. Credentials are never accepted or returned.", jobInputSchema.extend({ jobId: z.string().min(1) }), ({ jobId, ...input }: any) => service.updateJob(jobId, input));
tool("delete_job", "Delete a job and its queued invocation history", jobIdSchema, ({ jobId }: any) => service.deleteJob(jobId));
tool("enable_job", "Enable a job", jobIdSchema, ({ jobId }: any) => service.setJobEnabled(jobId, true));
tool("disable_job", "Disable a job", jobIdSchema, ({ jobId }: any) => service.setJobEnabled(jobId, false));
tool("invoke_job", "Manually invoke any enabled job, overriding string parameters and optional reserved context", manualInvocationSchema.extend({ jobId: z.string().min(1) }), ({ jobId, ...input }: any) => service.invokeJob(jobId, input));
tool("list_execution_targets", "List non-secret execution target metadata and capabilities", z.object({}), () => service.listExecutionTargets());
const transport = new WebStandardStreamableHTTPServerTransport({ sessionIdGenerator: undefined });
await server.connect(transport);
const appHostname = new URL(this.env.APP_ORIGIN).hostname;
Expand Down
Loading