|
1 | 1 | import { vResultValidator } from "@convex-dev/workpool"; |
2 | 2 | import { assert } from "convex-helpers"; |
3 | | -import type { FunctionHandle } from "convex/server"; |
| 3 | +import { |
| 4 | + paginationOptsValidator, |
| 5 | + type FunctionHandle, |
| 6 | + type PaginationResult, |
| 7 | +} from "convex/server"; |
4 | 8 | import { type Infer, v } from "convex/values"; |
5 | 9 | import { mutation, type MutationCtx, query } from "./_generated/server.js"; |
6 | 10 | import { type Logger, logLevel } from "./logging.js"; |
7 | 11 | import { getWorkflow } from "./model.js"; |
8 | 12 | import { getWorkpool } from "./pool.js"; |
9 | | -import { journalDocument, vOnComplete, workflowDocument } from "./schema.js"; |
| 13 | +import schema, { |
| 14 | + journalDocument, |
| 15 | + vOnComplete, |
| 16 | + workflowDocument, |
| 17 | + type JournalEntry, |
| 18 | +} from "./schema.js"; |
10 | 19 | import { getDefaultLogger } from "./utils.js"; |
11 | | -import type { WorkflowId, OnCompleteArgs } from "../types.js"; |
| 20 | +import { |
| 21 | + type WorkflowId, |
| 22 | + type OnCompleteArgs, |
| 23 | + type WorkflowStep, |
| 24 | + type EventId, |
| 25 | + vPaginationResult, |
| 26 | + vWorkflowStep, |
| 27 | +} from "../types.js"; |
12 | 28 | import { api, internal } from "./_generated/api.js"; |
13 | 29 | import { formatErrorWithStack } from "../shared.js"; |
14 | 30 | import type { SchedulerOptions } from "../client/types.js"; |
| 31 | +import type { Id } from "./_generated/dataModel.js"; |
| 32 | +import { paginator } from "convex-helpers/server/pagination"; |
15 | 33 |
|
16 | 34 | const createArgs = v.object({ |
17 | 35 | workflowName: v.string(), |
@@ -95,6 +113,60 @@ export const getStatus = query({ |
95 | 113 | }, |
96 | 114 | }); |
97 | 115 |
|
| 116 | +function publicWorkflowId(workflowId: Id<"workflows">): WorkflowId { |
| 117 | + return workflowId as any; |
| 118 | +} |
| 119 | + |
| 120 | +function publicStep(step: JournalEntry): WorkflowStep { |
| 121 | + return { |
| 122 | + workflowId: publicWorkflowId(step.workflowId), |
| 123 | + name: step.step.name, |
| 124 | + stepId: step._id, |
| 125 | + stepNumber: step.stepNumber, |
| 126 | + |
| 127 | + args: step.step.args, |
| 128 | + runResult: step.step.runResult, |
| 129 | + |
| 130 | + startedAt: step.step.startedAt, |
| 131 | + completedAt: step.step.completedAt, |
| 132 | + |
| 133 | + ...(step.step.kind === "event" |
| 134 | + ? { |
| 135 | + kind: "event", |
| 136 | + eventId: step.step.eventId as unknown as EventId, |
| 137 | + } |
| 138 | + : step.step.kind === "workflow" |
| 139 | + ? { |
| 140 | + kind: "workflow", |
| 141 | + nestedWorkflowId: publicWorkflowId(step.step.workflowId!), |
| 142 | + } |
| 143 | + : { |
| 144 | + kind: "function", |
| 145 | + workId: step.step.workId!, |
| 146 | + }), |
| 147 | + } satisfies WorkflowStep; |
| 148 | +} |
| 149 | + |
| 150 | +export const listSteps = query({ |
| 151 | + args: { |
| 152 | + workflowId: v.id("workflows"), |
| 153 | + order: v.union(v.literal("asc"), v.literal("desc")), |
| 154 | + paginationOpts: paginationOptsValidator, |
| 155 | + }, |
| 156 | + returns: vPaginationResult(vWorkflowStep), |
| 157 | + handler: async (ctx, args) => { |
| 158 | + const result = await paginator(ctx.db, schema) |
| 159 | + .query("steps") |
| 160 | + .withIndex("workflow", (q) => q.eq("workflowId", args.workflowId)) |
| 161 | + .order(args.order) |
| 162 | + .paginate(args.paginationOpts); |
| 163 | + return { |
| 164 | + ...result, |
| 165 | + page: result.page.map(publicStep), |
| 166 | + } as PaginationResult<Infer<typeof vWorkflowStep>>; |
| 167 | + }, |
| 168 | +}); |
| 169 | + |
98 | 170 | export const cancel = mutation({ |
99 | 171 | args: { |
100 | 172 | workflowId: v.id("workflows"), |
|
0 commit comments