@jive-sh/workflow-platform 0.7.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +66 -435
- package/component/_generated/api.ts +72 -0
- package/component/_generated/component.ts +621 -0
- package/component/_generated/dataModel.ts +60 -0
- package/component/_generated/server.ts +169 -0
- package/component/convex.config.ts +3 -0
- package/component/engine.ts +755 -0
- package/component/errors.ts +66 -0
- package/component/events.ts +17 -0
- package/component/notes.ts +13 -0
- package/component/places.ts +136 -0
- package/component/retention.ts +205 -0
- package/component/runEngine.ts +25 -0
- package/component/schema.ts +132 -0
- package/component/steps.ts +188 -0
- package/component/store.ts +512 -0
- package/component/tasks.ts +289 -0
- package/component/worker.ts +109 -0
- package/component/workflows.ts +83 -0
- package/dist/definition/agent-runner.js +9 -0
- package/dist/definition/agent-runner.js.map +1 -0
- package/dist/definition/file-store.js +9 -0
- package/dist/definition/file-store.js.map +1 -0
- package/dist/definition/program-build.js +10 -0
- package/dist/definition/program-build.js.map +1 -0
- package/dist/definition/step.js +1 -0
- package/dist/definition/step.js.map +1 -0
- package/dist/definition/task-schema.js +5 -0
- package/dist/definition/task-schema.js.map +1 -0
- package/dist/definition/tasks.js +9 -0
- package/dist/definition/tasks.js.map +1 -0
- package/dist/definition/workflow.js +111 -0
- package/dist/definition/workflow.js.map +1 -0
- package/dist/graph.js +301 -9
- package/dist/graph.js.map +1 -1
- package/dist/index.js +7 -196
- package/dist/index.js.map +1 -1
- package/package.json +9 -4
- package/src/definition/agent-runner.ts +59 -0
- package/src/definition/file-store.ts +20 -0
- package/src/definition/program-build.ts +30 -0
- package/src/definition/step.ts +106 -0
- package/src/definition/task-schema.ts +15 -0
- package/src/definition/tasks.ts +43 -0
- package/src/definition/workflow.ts +255 -0
- package/src/graph.ts +341 -19
- package/src/index.ts +8 -238
- package/worker/directory-file-store.ts +42 -0
- package/worker/index.ts +12 -0
- package/worker/platform-client.ts +138 -0
- package/worker/step-meter.ts +80 -0
- package/worker/worker-tasks.ts +27 -0
- package/worker/worker.test.ts +200 -0
- package/worker/worker.ts +240 -0
- package/dist/budget-states.js +0 -30
- package/dist/budget-states.js.map +0 -1
- package/dist/cpu-limit/limiter.js +0 -839
- package/dist/cpu-limit/limiter.js.map +0 -1
- package/dist/cpu-limit/use.js +0 -139
- package/dist/cpu-limit/use.js.map +0 -1
- package/dist/dashboard/agent-usage.js +0 -25
- package/dist/dashboard/agent-usage.js.map +0 -1
- package/dist/dashboard/dashboard.css +0 -148
- package/dist/dashboard/drilldown.js +0 -47
- package/dist/dashboard/drilldown.js.map +0 -1
- package/dist/dashboard/queue-lines.js +0 -44
- package/dist/dashboard/queue-lines.js.map +0 -1
- package/dist/dashboard/queue-table.js +0 -16
- package/dist/dashboard/queue-table.js.map +0 -1
- package/dist/dashboard/run-doing-words.js +0 -29
- package/dist/dashboard/run-doing-words.js.map +0 -1
- package/dist/dashboard/setting-words.js +0 -18
- package/dist/dashboard/setting-words.js.map +0 -1
- package/dist/dashboard/spending-words.js +0 -24
- package/dist/dashboard/spending-words.js.map +0 -1
- package/dist/failure.js +0 -35
- package/dist/failure.js.map +0 -1
- package/dist/keep-running.js +0 -79
- package/dist/keep-running.js.map +0 -1
- package/dist/outside-failure.js +0 -31
- package/dist/outside-failure.js.map +0 -1
- package/dist/pick-wakes.js +0 -125
- package/dist/pick-wakes.js.map +0 -1
- package/dist/project.js +0 -1
- package/dist/project.js.map +0 -1
- package/dist/queue-counts.js +0 -51
- package/dist/queue-counts.js.map +0 -1
- package/dist/reasons.js +0 -33
- package/dist/reasons.js.map +0 -1
- package/dist/retention.js +0 -59
- package/dist/retention.js.map +0 -1
- package/dist/settings.js +0 -88
- package/dist/settings.js.map +0 -1
- package/dist/spending-limit.js +0 -82
- package/dist/spending-limit.js.map +0 -1
- package/dist/step-files.js +0 -200
- package/dist/step-files.js.map +0 -1
- package/dist/step-meter.js +0 -97
- package/dist/step-meter.js.map +0 -1
- package/dist/step-times.js +0 -114
- package/dist/step-times.js.map +0 -1
- package/dist/step-usage.js +0 -19
- package/dist/step-usage.js.map +0 -1
- package/dist/steps.js +0 -1205
- package/dist/steps.js.map +0 -1
- package/dist/storage/agent-claims.js +0 -3
- package/dist/storage/agent-claims.js.map +0 -1
- package/dist/storage/budgets.js +0 -121
- package/dist/storage/budgets.js.map +0 -1
- package/dist/storage/engine-records.js +0 -91
- package/dist/storage/engine-records.js.map +0 -1
- package/dist/storage/engine-storage.js +0 -67
- package/dist/storage/engine-storage.js.map +0 -1
- package/dist/storage/input-records.js +0 -12
- package/dist/storage/input-records.js.map +0 -1
- package/dist/storage/move-store.js +0 -251
- package/dist/storage/move-store.js.map +0 -1
- package/dist/storage/sqlite-store.js +0 -901
- package/dist/storage/sqlite-store.js.map +0 -1
- package/dist/storage/step-records.js +0 -11
- package/dist/storage/step-records.js.map +0 -1
- package/dist/storage/storage-error.js +0 -19
- package/dist/storage/storage-error.js.map +0 -1
- package/dist/storage/task-store.js +0 -30
- package/dist/storage/task-store.js.map +0 -1
- package/dist/system-workflows.js +0 -54
- package/dist/system-workflows.js.map +0 -1
- package/dist/tasks.js +0 -500
- package/dist/tasks.js.map +0 -1
- package/dist/views/format.js +0 -37
- package/dist/views/format.js.map +0 -1
- package/dist/views/runs-now.js +0 -117
- package/dist/views/runs-now.js.map +0 -1
- package/dist/views/step-funnel.js +0 -98
- package/dist/views/step-funnel.js.map +0 -1
- package/dist/views/step-graph.js +0 -39
- package/dist/views/step-graph.js.map +0 -1
- package/dist/views/workflows-view.js +0 -236
- package/dist/views/workflows-view.js.map +0 -1
- package/src/budget-states.ts +0 -54
- package/src/cpu-limit/limiter.ts +0 -935
- package/src/cpu-limit/use.ts +0 -166
- package/src/dashboard/agent-usage-line.tsx +0 -28
- package/src/dashboard/agent-usage.ts +0 -48
- package/src/dashboard/dashboard.css +0 -148
- package/src/dashboard/drilldown.ts +0 -70
- package/src/dashboard/index.tsx +0 -4
- package/src/dashboard/limits-and-runs.tsx +0 -191
- package/src/dashboard/machine-controls.tsx +0 -53
- package/src/dashboard/queue-bar.tsx +0 -62
- package/src/dashboard/queue-chart.tsx +0 -149
- package/src/dashboard/queue-lines.ts +0 -87
- package/src/dashboard/queue-table.ts +0 -37
- package/src/dashboard/run-doing-words.ts +0 -41
- package/src/dashboard/setting-words.ts +0 -30
- package/src/dashboard/spending-limit-control.tsx +0 -47
- package/src/dashboard/spending-words.ts +0 -32
- package/src/dashboard/time-distribution.tsx +0 -61
- package/src/dashboard/workflow-dag.tsx +0 -322
- package/src/dashboard/workflow-drilldown.tsx +0 -31
- package/src/dashboard/workflows-dashboard.tsx +0 -532
- package/src/failure.ts +0 -36
- package/src/keep-running.ts +0 -85
- package/src/outside-failure.ts +0 -39
- package/src/pick-wakes.ts +0 -182
- package/src/project.ts +0 -127
- package/src/queue-counts.ts +0 -64
- package/src/reasons.ts +0 -30
- package/src/retention.ts +0 -90
- package/src/settings.ts +0 -171
- package/src/spending-limit.ts +0 -139
- package/src/step-files.ts +0 -208
- package/src/step-meter.ts +0 -120
- package/src/step-times.ts +0 -151
- package/src/step-usage.ts +0 -23
- package/src/steps.ts +0 -1616
- package/src/storage/agent-claims.ts +0 -63
- package/src/storage/budgets.ts +0 -152
- package/src/storage/engine-records.ts +0 -119
- package/src/storage/engine-storage.ts +0 -82
- package/src/storage/input-records.ts +0 -43
- package/src/storage/move-store.ts +0 -265
- package/src/storage/sqlite-store.ts +0 -1022
- package/src/storage/step-records.ts +0 -358
- package/src/storage/storage-error.ts +0 -35
- package/src/storage/task-store.ts +0 -235
- package/src/system-workflows.ts +0 -70
- package/src/tasks.ts +0 -586
- package/src/views/format.ts +0 -40
- package/src/views/runs-now.ts +0 -188
- package/src/views/step-funnel.ts +0 -163
- package/src/views/step-graph.ts +0 -77
- package/src/views/workflows-view.ts +0 -484
- package/src/workflow-dag.test.tsx +0 -89
- package/src/workflow-page.test.tsx +0 -170
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import { v } from "convex/values"
|
|
2
|
+
import { Option } from "effect"
|
|
3
|
+
import {
|
|
4
|
+
createsTasks,
|
|
5
|
+
inputPlacesOf,
|
|
6
|
+
parentRefValidator,
|
|
7
|
+
placeAtStartOf,
|
|
8
|
+
placeRefValidator,
|
|
9
|
+
skippedOtherValidator,
|
|
10
|
+
stepSpendValidator,
|
|
11
|
+
stepUsedValidator,
|
|
12
|
+
taskFileValidator,
|
|
13
|
+
taskNoteValidator,
|
|
14
|
+
} from "../src/graph"
|
|
15
|
+
import { mutation, query } from "./_generated/server"
|
|
16
|
+
import * as Engine from "./engine"
|
|
17
|
+
import { runEngine } from "./runEngine"
|
|
18
|
+
import { notesOf } from "./notes"
|
|
19
|
+
|
|
20
|
+
/** A task arrives: through the workflow's task event, or through a named request event. Fields are already encoded by the task schema. */
|
|
21
|
+
export const create = mutation({
|
|
22
|
+
args: {
|
|
23
|
+
workflow: v.string(),
|
|
24
|
+
event: v.optional(v.string()),
|
|
25
|
+
fields: v.any(),
|
|
26
|
+
files: v.optional(v.array(taskFileValidator)),
|
|
27
|
+
parent: v.optional(parentRefValidator),
|
|
28
|
+
},
|
|
29
|
+
returns: v.id("tasks"),
|
|
30
|
+
handler: (ctx, args) =>
|
|
31
|
+
runEngine(
|
|
32
|
+
ctx,
|
|
33
|
+
Engine.createTask({
|
|
34
|
+
workflow: args.workflow,
|
|
35
|
+
event: Option.fromNullishOr(args.event),
|
|
36
|
+
fields: args.fields,
|
|
37
|
+
files: args.files ?? [],
|
|
38
|
+
parent: Option.fromNullishOr(args.parent),
|
|
39
|
+
}),
|
|
40
|
+
),
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
/** Replaces a task's fields (and files, when given); every change event watching a field that changed starts a flow. */
|
|
44
|
+
export const setFields = mutation({
|
|
45
|
+
args: { task: v.id("tasks"), fields: v.any(), files: v.optional(v.array(taskFileValidator)) },
|
|
46
|
+
returns: v.null(),
|
|
47
|
+
handler: async (ctx, args) => {
|
|
48
|
+
await runEngine(ctx, Engine.setTaskFields({ task: args.task, fields: args.fields, files: Option.fromNullishOr(args.files) }))
|
|
49
|
+
return null
|
|
50
|
+
},
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
const stepRunValidator = v.object({
|
|
54
|
+
step: v.string(),
|
|
55
|
+
status: v.string(),
|
|
56
|
+
worker: v.optional(v.string()),
|
|
57
|
+
startedAt: v.number(),
|
|
58
|
+
endedAt: v.optional(v.number()),
|
|
59
|
+
endState: v.optional(v.string()),
|
|
60
|
+
error: v.optional(v.string()),
|
|
61
|
+
call: v.number(),
|
|
62
|
+
version: v.string(),
|
|
63
|
+
spend: stepSpendValidator,
|
|
64
|
+
used: stepUsedValidator,
|
|
65
|
+
agent: v.optional(v.string()),
|
|
66
|
+
files: v.array(v.string()),
|
|
67
|
+
note: v.optional(v.string()),
|
|
68
|
+
transcripts: v.array(taskFileValidator),
|
|
69
|
+
skippedOthers: v.optional(v.array(skippedOtherValidator)),
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
/** A task as it is now: its fields, files, every place its flows are, and the record of every step it went through. */
|
|
73
|
+
export const get = query({
|
|
74
|
+
// Any string, so a task id that came from outside, such as another task's run writing to it, reads as no task.
|
|
75
|
+
args: { task: v.string() },
|
|
76
|
+
returns: v.union(
|
|
77
|
+
v.null(),
|
|
78
|
+
v.object({
|
|
79
|
+
id: v.id("tasks"),
|
|
80
|
+
workflow: v.string(),
|
|
81
|
+
fields: v.any(),
|
|
82
|
+
files: v.array(taskFileValidator),
|
|
83
|
+
endedAs: v.optional(v.string()),
|
|
84
|
+
parent: v.optional(parentRefValidator),
|
|
85
|
+
places: v.array(v.object({ startedBy: v.string(), place: v.string() })),
|
|
86
|
+
stepRuns: v.array(stepRunValidator),
|
|
87
|
+
}),
|
|
88
|
+
),
|
|
89
|
+
handler: async (ctx, args) => {
|
|
90
|
+
const task = await (async () => {
|
|
91
|
+
const id = ctx.db.normalizeId("tasks", args.task)
|
|
92
|
+
return id === null ? null : await ctx.db.get(id)
|
|
93
|
+
})()
|
|
94
|
+
if (task === null) return null
|
|
95
|
+
const flows = await ctx.db.query("flows").withIndex("by_task_started_by", (q) => q.eq("task", task._id)).collect()
|
|
96
|
+
const runs = await ctx.db.query("stepRuns").withIndex("by_task", (q) => q.eq("task", task._id)).collect()
|
|
97
|
+
return {
|
|
98
|
+
id: task._id,
|
|
99
|
+
workflow: task.workflow,
|
|
100
|
+
fields: task.fields,
|
|
101
|
+
files: task.files,
|
|
102
|
+
...(task.endedAs !== undefined ? { endedAs: task.endedAs } : {}),
|
|
103
|
+
...(task.parent !== undefined ? { parent: task.parent } : {}),
|
|
104
|
+
places: flows.map((flow) => ({ startedBy: flow.startedBy, place: flow.place })),
|
|
105
|
+
stepRuns: runs.map((run) => ({
|
|
106
|
+
step: run.step,
|
|
107
|
+
status: run.status,
|
|
108
|
+
call: run.call,
|
|
109
|
+
version: run.version,
|
|
110
|
+
spend: run.spend,
|
|
111
|
+
used: run.used,
|
|
112
|
+
files: run.files,
|
|
113
|
+
transcripts: run.transcripts,
|
|
114
|
+
...(run.agent !== undefined ? { agent: run.agent } : {}),
|
|
115
|
+
startedAt: run.startedAt,
|
|
116
|
+
...(run.worker !== undefined ? { worker: run.worker } : {}),
|
|
117
|
+
...(run.endedAt !== undefined ? { endedAt: run.endedAt } : {}),
|
|
118
|
+
...(run.endState !== undefined ? { endState: run.endState } : {}),
|
|
119
|
+
...(run.error !== undefined ? { error: run.error } : {}),
|
|
120
|
+
...(run.note !== undefined ? { note: run.note } : {}),
|
|
121
|
+
...(run.skippedOthers !== undefined ? { skippedOthers: run.skippedOthers } : {}),
|
|
122
|
+
})),
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
/** The tasks a flow of this task waits on in a sub-workflow step, by list entry; their fields live in the sub-workflow's component. */
|
|
128
|
+
export const subWorkflowTasks = query({
|
|
129
|
+
args: { task: v.id("tasks") },
|
|
130
|
+
returns: v.array(v.object({ entryIndex: v.number(), workflow: v.string(), task: v.optional(v.string()), endedAs: v.optional(v.string()) })),
|
|
131
|
+
handler: async (ctx, args) => {
|
|
132
|
+
const flows = await ctx.db.query("flows").withIndex("by_task_started_by", (q) => q.eq("task", args.task)).collect()
|
|
133
|
+
const rows = []
|
|
134
|
+
for (const flow of flows) {
|
|
135
|
+
rows.push(...(await ctx.db.query("subWorkflowTasks").withIndex("by_flow_entry", (q) => q.eq("flow", flow._id)).collect()))
|
|
136
|
+
}
|
|
137
|
+
return rows.map((row) => ({
|
|
138
|
+
entryIndex: row.entryIndex,
|
|
139
|
+
workflow: row.workflow,
|
|
140
|
+
...(row.task !== undefined ? { task: row.task } : {}),
|
|
141
|
+
...(row.endedAs !== undefined ? { endedAs: row.endedAs } : {}),
|
|
142
|
+
}))
|
|
143
|
+
},
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
/** The workforce created the sub-workflow task for one entry of a flow waiting here. */
|
|
147
|
+
export const recordSubWorkflowTask = mutation({
|
|
148
|
+
args: { flow: v.id("flows"), entryIndex: v.number(), task: v.string() },
|
|
149
|
+
returns: v.null(),
|
|
150
|
+
handler: async (ctx, args) => {
|
|
151
|
+
await runEngine(ctx, Engine.recordSubWorkflowTask(args))
|
|
152
|
+
return null
|
|
153
|
+
},
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
/** The workforce relays that a sub-workflow task of a flow waiting here ended, with that task's fields. */
|
|
157
|
+
export const subWorkflowTaskEnded = mutation({
|
|
158
|
+
args: { flow: v.id("flows"), entryIndex: v.number(), endState: v.string(), fields: v.any() },
|
|
159
|
+
returns: v.null(),
|
|
160
|
+
handler: async (ctx, args) => {
|
|
161
|
+
await runEngine(ctx, Engine.subWorkflowTaskEnded(args))
|
|
162
|
+
return null
|
|
163
|
+
},
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Sends a task back through a task or request event of its workflow, with new fields when given and `note` kept as
|
|
168
|
+
* why: from the end state it stopped at onto that event's line. Fields are already encoded by the task schema.
|
|
169
|
+
*/
|
|
170
|
+
export const requeue = mutation({
|
|
171
|
+
args: { task: v.id("tasks"), event: v.string(), fields: v.optional(v.any()), note: v.string() },
|
|
172
|
+
returns: v.null(),
|
|
173
|
+
handler: async (ctx, args) => {
|
|
174
|
+
await runEngine(ctx, Engine.requeue({ task: args.task, event: args.event, fields: Option.fromNullishOr(args.fields), note: args.note }))
|
|
175
|
+
return null
|
|
176
|
+
},
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
/** Everything said about a task, oldest first: its step runs' notes and the reasons calls sent it back. */
|
|
180
|
+
export const notes = query({
|
|
181
|
+
args: { task: v.id("tasks") },
|
|
182
|
+
returns: v.array(taskNoteValidator),
|
|
183
|
+
handler: async (ctx, args) => {
|
|
184
|
+
const task = await ctx.db.get(args.task)
|
|
185
|
+
if (task === null) return []
|
|
186
|
+
const runs = await ctx.db.query("stepRuns").withIndex("by_task", (q) => q.eq("task", task._id)).collect()
|
|
187
|
+
const left = await ctx.db.query("taskNotes").withIndex("by_task", (q) => q.eq("task", task._id)).collect()
|
|
188
|
+
return notesOf({ task, runs, left })
|
|
189
|
+
},
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
/** Where a task is, as `find` reports it: the place of the flow that brought it in. */
|
|
193
|
+
const taskPlaceValidator = v.union(v.object({ line: v.string() }), v.object({ step: v.string() }), v.object({ end: v.string() }))
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Tasks of the workflow whose fields equal every value of `where` (compared as stored), narrowed by `at` to those
|
|
197
|
+
* waiting in front of a step, in a step, at an end state or on one line; at most `limit`. Each comes with its fields,
|
|
198
|
+
* files, notes and where it is now. The workforce checks that the asking step may read this workflow.
|
|
199
|
+
*/
|
|
200
|
+
export const find = query({
|
|
201
|
+
args: { workflow: v.string(), where: v.optional(v.any()), at: v.optional(placeRefValidator), limit: v.number() },
|
|
202
|
+
returns: v.array(
|
|
203
|
+
v.object({ taskId: v.id("tasks"), fields: v.any(), files: v.array(taskFileValidator), notes: v.array(taskNoteValidator), at: taskPlaceValidator }),
|
|
204
|
+
),
|
|
205
|
+
handler: async (ctx, args) => {
|
|
206
|
+
const workflow = await ctx.db.query("workflows").withIndex("by_name", (q) => q.eq("name", args.workflow)).unique()
|
|
207
|
+
if (workflow === null) return []
|
|
208
|
+
const graph = workflow.graph
|
|
209
|
+
const arrivalEvents = new Set(graph.events.filter(createsTasks).map((event) => event.id))
|
|
210
|
+
const where: Record<string, unknown> = typeof args.where === "object" && args.where !== null ? args.where : {}
|
|
211
|
+
const matches = (fields: Record<string, unknown>) => Object.entries(where).every(([field, value]) => JSON.stringify(fields[field]) === JSON.stringify(value))
|
|
212
|
+
const at = args.at
|
|
213
|
+
const places =
|
|
214
|
+
at === undefined
|
|
215
|
+
? undefined
|
|
216
|
+
: "inFrontOf" in at
|
|
217
|
+
? inputPlacesOf(graph, at.inFrontOf)
|
|
218
|
+
: "step" in at
|
|
219
|
+
? [`step:${at.step}`]
|
|
220
|
+
: "end" in at
|
|
221
|
+
? [`end:${at.end}`]
|
|
222
|
+
: graph.lines.filter((line) => line.id === at.line).map(placeAtStartOf)
|
|
223
|
+
const candidates =
|
|
224
|
+
places === undefined
|
|
225
|
+
? await ctx.db.query("tasks").withIndex("by_workflow", (q) => q.eq("workflow", args.workflow)).collect()
|
|
226
|
+
: await (async () => {
|
|
227
|
+
const tasks = []
|
|
228
|
+
for (const place of places) {
|
|
229
|
+
const flows = await ctx.db.query("flows").withIndex("by_workflow_place_entered", (q) => q.eq("workflow", args.workflow).eq("place", place)).collect()
|
|
230
|
+
for (const flow of flows.filter((candidate) => arrivalEvents.has(candidate.startedBy))) {
|
|
231
|
+
const task = await ctx.db.get(flow.task)
|
|
232
|
+
if (task !== null) tasks.push(task)
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return tasks
|
|
236
|
+
})()
|
|
237
|
+
const found = []
|
|
238
|
+
for (const task of candidates) {
|
|
239
|
+
if (found.length >= args.limit) break
|
|
240
|
+
if (!matches(task.fields)) continue
|
|
241
|
+
const flows = await ctx.db.query("flows").withIndex("by_task_started_by", (q) => q.eq("task", task._id)).collect()
|
|
242
|
+
const arrival = flows.find((flow) => arrivalEvents.has(flow.startedBy))
|
|
243
|
+
if (arrival === undefined) continue
|
|
244
|
+
const place = arrival.place
|
|
245
|
+
const runs = await ctx.db.query("stepRuns").withIndex("by_task", (q) => q.eq("task", task._id)).collect()
|
|
246
|
+
const left = await ctx.db.query("taskNotes").withIndex("by_task", (q) => q.eq("task", task._id)).collect()
|
|
247
|
+
found.push({
|
|
248
|
+
taskId: task._id,
|
|
249
|
+
fields: task.fields,
|
|
250
|
+
files: task.files,
|
|
251
|
+
notes: notesOf({ task, runs, left }),
|
|
252
|
+
at: place.startsWith("step:") ? { step: place.slice(5) } : place.startsWith("end:") ? { end: place.slice(4) } : { line: place.slice(5) },
|
|
253
|
+
})
|
|
254
|
+
}
|
|
255
|
+
return found
|
|
256
|
+
},
|
|
257
|
+
})
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Another task's run writes to one of this workflow's tasks, inside the workforce mutation completing that run. Returns
|
|
261
|
+
* why the write was skipped, or null once it is applied (`Engine.writeOther`). Fields are already encoded and checked.
|
|
262
|
+
*/
|
|
263
|
+
export const writeOther = mutation({
|
|
264
|
+
args: { task: v.string(), fields: v.optional(v.any()), files: v.optional(v.array(taskFileValidator)), note: v.string(), by: v.string() },
|
|
265
|
+
returns: v.union(v.null(), v.string()),
|
|
266
|
+
handler: async (ctx, args) => {
|
|
267
|
+
const task = ctx.db.normalizeId("tasks", args.task)
|
|
268
|
+
if (task === null) return "there is no such task"
|
|
269
|
+
const skipped = await runEngine(
|
|
270
|
+
ctx,
|
|
271
|
+
Engine.writeOther({ task, fields: Option.fromNullishOr(args.fields), files: Option.fromNullishOr(args.files), note: args.note, by: args.by }),
|
|
272
|
+
)
|
|
273
|
+
return Option.getOrNull(skipped)
|
|
274
|
+
},
|
|
275
|
+
})
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Moves these tasks to the front of the queues they wait in, in this order, with `note` as why (`Engine.moveToFront`):
|
|
279
|
+
* those in a step, ended, or of another workflow are named as not waiting.
|
|
280
|
+
*/
|
|
281
|
+
export const moveToFront = mutation({
|
|
282
|
+
args: { workflow: v.string(), tasks: v.array(v.string()), note: v.string() },
|
|
283
|
+
returns: v.object({ moved: v.array(v.string()), notWaiting: v.array(v.string()) }),
|
|
284
|
+
handler: async (ctx, args) => {
|
|
285
|
+
const tasks = args.tasks.map((id) => ({ id, task: Option.fromNullishOr(ctx.db.normalizeId("tasks", id)) }))
|
|
286
|
+
const result = await runEngine(ctx, Engine.moveToFront({ workflow: args.workflow, tasks, note: args.note }))
|
|
287
|
+
return { moved: [...result.moved], notWaiting: [...result.notWaiting] }
|
|
288
|
+
},
|
|
289
|
+
})
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { v } from "convex/values"
|
|
2
|
+
import { Option } from "effect"
|
|
3
|
+
import { skippedOtherValidator, stepMeasurementsValidator, taskFileValidator, taskNoteValidator } from "../src/graph"
|
|
4
|
+
import { internalMutation, mutation, query } from "./_generated/server"
|
|
5
|
+
import * as Engine from "./engine"
|
|
6
|
+
import { runEngine } from "./runEngine"
|
|
7
|
+
|
|
8
|
+
/** Takes the task that has waited longest in front of any of the given steps whose workflow budget has room, or null. */
|
|
9
|
+
export const take = mutation({
|
|
10
|
+
args: {
|
|
11
|
+
worker: v.string(),
|
|
12
|
+
steps: v.array(v.object({ workflow: v.string(), step: v.string() })),
|
|
13
|
+
leaseMs: v.number(),
|
|
14
|
+
},
|
|
15
|
+
returns: v.union(
|
|
16
|
+
v.null(),
|
|
17
|
+
v.object({
|
|
18
|
+
run: v.id("stepRuns"),
|
|
19
|
+
workflow: v.string(),
|
|
20
|
+
step: v.string(),
|
|
21
|
+
task: v.object({ id: v.id("tasks"), fields: v.any(), files: v.array(taskFileValidator), notes: v.array(taskNoteValidator) }),
|
|
22
|
+
lastFingerprint: v.optional(v.string()),
|
|
23
|
+
}),
|
|
24
|
+
),
|
|
25
|
+
handler: async (ctx, args) => {
|
|
26
|
+
const maybeTaken = await runEngine(ctx, Engine.take(args))
|
|
27
|
+
return Option.getOrNull(maybeTaken)
|
|
28
|
+
},
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
/** Reports a step's end state, with the task's new fields and files when the step changed them. */
|
|
32
|
+
export const complete = mutation({
|
|
33
|
+
args: {
|
|
34
|
+
run: v.id("stepRuns"),
|
|
35
|
+
workflow: v.string(),
|
|
36
|
+
endState: v.string(),
|
|
37
|
+
fields: v.optional(v.any()),
|
|
38
|
+
files: v.optional(v.array(taskFileValidator)),
|
|
39
|
+
measurements: v.optional(stepMeasurementsValidator),
|
|
40
|
+
note: v.optional(v.string()),
|
|
41
|
+
/** From a step that syncs a declared item list: every item on it, as encoded task fields. */
|
|
42
|
+
listed: v.optional(v.array(v.any())),
|
|
43
|
+
/** Writes to other tasks the workforce could not apply, each with why. */
|
|
44
|
+
skippedOthers: v.optional(v.array(skippedOtherValidator)),
|
|
45
|
+
},
|
|
46
|
+
returns: v.null(),
|
|
47
|
+
handler: async (ctx, args) => {
|
|
48
|
+
await runEngine(
|
|
49
|
+
ctx,
|
|
50
|
+
Engine.complete({
|
|
51
|
+
run: args.run,
|
|
52
|
+
workflow: args.workflow,
|
|
53
|
+
endState: args.endState,
|
|
54
|
+
fields: Option.fromNullishOr(args.fields),
|
|
55
|
+
files: Option.fromNullishOr(args.files),
|
|
56
|
+
measurements: Option.fromNullishOr(args.measurements),
|
|
57
|
+
note: Option.fromNullishOr(args.note),
|
|
58
|
+
listed: Option.fromNullishOr(args.listed),
|
|
59
|
+
skippedOthers: args.skippedOthers ?? [],
|
|
60
|
+
}),
|
|
61
|
+
)
|
|
62
|
+
return null
|
|
63
|
+
},
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
export const fail = mutation({
|
|
67
|
+
args: { run: v.id("stepRuns"), error: v.string(), measurements: v.optional(stepMeasurementsValidator) },
|
|
68
|
+
returns: v.null(),
|
|
69
|
+
handler: async (ctx, args) => {
|
|
70
|
+
await runEngine(ctx, Engine.fail({ run: args.run, error: args.error, measurements: Option.fromNullishOr(args.measurements) }))
|
|
71
|
+
return null
|
|
72
|
+
},
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
export const heartbeat = mutation({
|
|
76
|
+
args: { run: v.id("stepRuns"), leaseMs: v.number() },
|
|
77
|
+
returns: v.null(),
|
|
78
|
+
handler: async (ctx, args) => {
|
|
79
|
+
await runEngine(ctx, Engine.heartbeat(args))
|
|
80
|
+
return null
|
|
81
|
+
},
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
export const expireLease = internalMutation({
|
|
85
|
+
args: { run: v.id("stepRuns") },
|
|
86
|
+
returns: v.null(),
|
|
87
|
+
handler: async (ctx, args) => {
|
|
88
|
+
await runEngine(ctx, Engine.expireLease(args))
|
|
89
|
+
return null
|
|
90
|
+
},
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
/** When the task `take` would give entered its queue, or null; read inside the workforce's `take` transaction. */
|
|
94
|
+
export const oldestTakeable = mutation({
|
|
95
|
+
args: { steps: v.array(v.object({ workflow: v.string(), step: v.string() })) },
|
|
96
|
+
returns: v.union(v.null(), v.number()),
|
|
97
|
+
handler: async (ctx, args) => Option.getOrNull(await runEngine(ctx, Engine.oldestTakeable(args))),
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
/** Which step a run is of, its task, and whether it is still running; the workforce reads it to check the step's access. */
|
|
101
|
+
export const runOf = query({
|
|
102
|
+
args: { run: v.string() },
|
|
103
|
+
returns: v.union(v.null(), v.object({ workflow: v.string(), step: v.string(), task: v.id("tasks"), status: v.string() })),
|
|
104
|
+
handler: async (ctx, args) => {
|
|
105
|
+
const id = ctx.db.normalizeId("stepRuns", args.run)
|
|
106
|
+
const run = id === null ? null : await ctx.db.get(id)
|
|
107
|
+
return run === null ? null : { workflow: run.workflow, step: run.step, task: run.task, status: run.status }
|
|
108
|
+
},
|
|
109
|
+
})
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { v } from "convex/values"
|
|
2
|
+
import { Option } from "effect"
|
|
3
|
+
import { budgetsValidator, graphValidator, workforceHooksValidator } from "../src/graph"
|
|
4
|
+
import { mutation, query } from "./_generated/server"
|
|
5
|
+
import * as Engine from "./engine"
|
|
6
|
+
import { runEngine } from "./runEngine"
|
|
7
|
+
|
|
8
|
+
/** Registers a workflow, or replaces its graph; tasks already in it keep their places. Its budgets stay as they were. */
|
|
9
|
+
export const register = mutation({
|
|
10
|
+
args: { graph: graphValidator, hooks: v.optional(workforceHooksValidator) },
|
|
11
|
+
returns: v.null(),
|
|
12
|
+
handler: async (ctx, args) => {
|
|
13
|
+
await runEngine(ctx, Engine.register({ graph: args.graph, hooks: Option.fromNullishOr(args.hooks) }))
|
|
14
|
+
return null
|
|
15
|
+
},
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
export const setBudgets = mutation({
|
|
19
|
+
args: { workflow: v.string(), budgets: budgetsValidator },
|
|
20
|
+
returns: v.null(),
|
|
21
|
+
handler: async (ctx, args) => {
|
|
22
|
+
await runEngine(ctx, Engine.setBudgets(args))
|
|
23
|
+
return null
|
|
24
|
+
},
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
/** The graph a workflow runs now, with its `version` (the one every new step run records) and its budgets; null if not registered. */
|
|
28
|
+
export const get = query({
|
|
29
|
+
args: { workflow: v.string() },
|
|
30
|
+
returns: v.union(v.null(), v.object({ graph: graphValidator, version: v.string(), budgets: budgetsValidator })),
|
|
31
|
+
handler: async (ctx, args) => {
|
|
32
|
+
const workflow = await ctx.db.query("workflows").withIndex("by_name", (q) => q.eq("name", args.workflow)).unique()
|
|
33
|
+
return workflow === null ? null : { graph: workflow.graph, version: workflow.version, budgets: workflow.budgets }
|
|
34
|
+
},
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The number of tasks at every place of a workflow: on each line, in each step, at each end state. A line out of an
|
|
39
|
+
* end state counts the tasks waiting at that end state, since they are its queue.
|
|
40
|
+
*/
|
|
41
|
+
export const counts = query({
|
|
42
|
+
args: { workflow: v.string() },
|
|
43
|
+
returns: v.object({
|
|
44
|
+
lines: v.array(v.object({ line: v.string(), tasks: v.number() })),
|
|
45
|
+
steps: v.array(v.object({ step: v.string(), tasks: v.number() })),
|
|
46
|
+
ends: v.array(v.object({ end: v.string(), tasks: v.number() })),
|
|
47
|
+
budgets: v.object({ agents: v.object({ inUse: v.number(), limit: v.number() }), cpu: v.object({ inUse: v.number(), limit: v.number() }) }),
|
|
48
|
+
}),
|
|
49
|
+
handler: async (ctx, args) => {
|
|
50
|
+
const workflow = await ctx.db.query("workflows").withIndex("by_name", (q) => q.eq("name", args.workflow)).unique()
|
|
51
|
+
if (workflow === null) throw new Error(`workflow ${args.workflow} is not registered`)
|
|
52
|
+
const flows = await ctx.db.query("flows").withIndex("by_workflow_place_entered", (q) => q.eq("workflow", args.workflow)).collect()
|
|
53
|
+
const tasksAt = (place: string) => flows.filter((flow) => flow.place === place).length
|
|
54
|
+
const running = (budget: "agents" | "cpu") =>
|
|
55
|
+
ctx.db
|
|
56
|
+
.query("stepRuns")
|
|
57
|
+
.withIndex("by_workflow_budget_status", (q) => q.eq("workflow", args.workflow).eq("budget", budget).eq("status", "running"))
|
|
58
|
+
.collect()
|
|
59
|
+
return {
|
|
60
|
+
lines: workflow.graph.lines.map((line) => ({
|
|
61
|
+
line: line.id,
|
|
62
|
+
tasks: "end" in line.from ? tasksAt(`end:${line.from.end}`) : "end" in line.to ? 0 : tasksAt(`line:${line.id}`),
|
|
63
|
+
})),
|
|
64
|
+
steps: workflow.graph.steps.map((step) => ({ step: step.id, tasks: tasksAt(`step:${step.id}`) })),
|
|
65
|
+
ends: workflow.graph.endStates.map((end) => ({ end, tasks: tasksAt(`end:${end}`) })),
|
|
66
|
+
budgets: {
|
|
67
|
+
agents: { inUse: (await running("agents")).length, limit: workflow.budgets.agents },
|
|
68
|
+
cpu: { inUse: (await running("cpu")).length, limit: workflow.budgets.cpu },
|
|
69
|
+
},
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
/** Deletes a workflow with all its tasks and records. Tasks of its sub-workflows stay in their own workflows. */
|
|
76
|
+
export const remove = mutation({
|
|
77
|
+
args: { workflow: v.string() },
|
|
78
|
+
returns: v.null(),
|
|
79
|
+
handler: async (ctx, args) => {
|
|
80
|
+
await runEngine(ctx, Engine.remove(args))
|
|
81
|
+
return null
|
|
82
|
+
},
|
|
83
|
+
})
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Context, Data } from "effect";
|
|
2
|
+
export class AgentRunFailed extends Data.TaggedError("AgentRunFailed") {
|
|
3
|
+
get message() {
|
|
4
|
+
return `the agent run failed: ${String(this.cause)}`;
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export class AgentRunner extends Context.Service()("AgentRunner") {
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=agent-runner.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent-runner.js","sourceRoot":"","sources":["../../../../src/definition/agent-runner.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAe,MAAM,QAAQ,CAAA;AAGnD,MAAM,OAAO,cAAe,SAAQ,IAAI,CAAC,WAAW,CAAC,gBAAgB,CAA8B;IACjG,IAAa,OAAO;QAClB,OAAO,yBAAyB,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAA;IACtD,CAAC;CACF;AAmDD,MAAM,OAAO,WAAY,SAAQ,OAAO,CAAC,OAAO,EAA6B,CAAC,aAAa,CAAC;CAAG"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Context, Data } from "effect";
|
|
2
|
+
export class FileStoreError extends Data.TaggedError("FileStoreError") {
|
|
3
|
+
get message() {
|
|
4
|
+
return `file ${this.file} could not be stored or read: ${String(this.cause)}`;
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export class FileStore extends Context.Service()("FileStore") {
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=file-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file-store.js","sourceRoot":"","sources":["../../../../src/definition/file-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAe,MAAM,QAAQ,CAAA;AAGnD,MAAM,OAAO,cAAe,SAAQ,IAAI,CAAC,WAAW,CAAC,gBAAgB,CAAqD;IACxH,IAAa,OAAO;QAClB,OAAO,QAAQ,IAAI,CAAC,IAAI,iCAAiC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAA;IAC/E,CAAC;CACF;AAWD,MAAM,OAAO,SAAU,SAAQ,OAAO,CAAC,OAAO,EAAyB,CAAC,WAAW,CAAC;CAAG"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Context, Data } from "effect";
|
|
2
|
+
/** The build could not run at all, such as when the toolchain is missing. A build that runs and fails is a result. */
|
|
3
|
+
export class ProgramBuildFailed extends Data.TaggedError("ProgramBuildFailed") {
|
|
4
|
+
get message() {
|
|
5
|
+
return `the program build failed: ${String(this.cause)}`;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export class ProgramBuild extends Context.Service()("ProgramBuild") {
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=program-build.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"program-build.js","sourceRoot":"","sources":["../../../../src/definition/program-build.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,IAAI,EAA4B,MAAM,QAAQ,CAAA;AAEhE,sHAAsH;AACtH,MAAM,OAAO,kBAAmB,SAAQ,IAAI,CAAC,WAAW,CAAC,oBAAoB,CAA8B;IACzG,IAAa,OAAO;QAClB,OAAO,6BAA6B,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAA;IAC1D,CAAC;CACF;AAsBD,MAAM,OAAO,YAAa,SAAQ,OAAO,CAAC,OAAO,EAA+B,CAAC,cAAc,CAAC;CAAG"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=step.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"step.js","sourceRoot":"","sources":["../../../../src/definition/step.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"task-schema.js","sourceRoot":"","sources":["../../../../src/definition/task-schema.ts"],"names":[],"mappings":"AAWA,MAAM,CAAC,MAAM,IAAI,GAAG,CAA8B,KAA0E,EAAsB,EAAE,CAAC,CAAC;IACpJ,MAAM,EAAE,KAAK,CAAC,MAAM;IACpB,KAAK,EAAE,KAAK,CAAC,KAAK,IAAI,EAAE;CACzB,CAAC,CAAA"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Context, Data } from "effect";
|
|
2
|
+
export class TasksCallFailed extends Data.TaggedError("TasksCallFailed") {
|
|
3
|
+
get message() {
|
|
4
|
+
return `reading tasks failed: ${String(this.cause)}`;
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export class Tasks extends Context.Service()("Tasks") {
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=tasks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tasks.js","sourceRoot":"","sources":["../../../../src/definition/tasks.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,IAAI,EAAe,MAAM,QAAQ,CAAA;AAGnD,MAAM,OAAO,eAAgB,SAAQ,IAAI,CAAC,WAAW,CAAC,iBAAiB,CAA8B;IACnG,IAAa,OAAO;QAClB,OAAO,yBAAyB,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAA;IACtD,CAAC;CACF;AAmCD,MAAM,OAAO,KAAM,SAAQ,OAAO,CAAC,OAAO,EAAiB,CAAC,OAAO,CAAC;CAAG"}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { Cause, Effect, Exit, Option, Schema } from "effect";
|
|
2
|
+
import { everySubWorkflowTaskEnded, lineIdOf, } from "../graph";
|
|
3
|
+
/** A step's declared access as its runnable form keeps it: nothing declared is nothing granted. */
|
|
4
|
+
const accessOf = (access) => ({
|
|
5
|
+
services: [...(access?.services ?? [])],
|
|
6
|
+
...(access?.tasks !== undefined ? { tasks: { read: [...access.tasks.read], write: [...access.tasks.write] } } : {}),
|
|
7
|
+
});
|
|
8
|
+
const stepBuilder = () => ({
|
|
9
|
+
syncList: (input) => ({
|
|
10
|
+
kind: "code",
|
|
11
|
+
endStates: ["synced"],
|
|
12
|
+
access: accessOf(input.access),
|
|
13
|
+
syncs: input.list,
|
|
14
|
+
run: function* (task) {
|
|
15
|
+
return { endState: "synced", listed: yield* input.read(task) };
|
|
16
|
+
},
|
|
17
|
+
}),
|
|
18
|
+
inputsChanged: (input) => ({
|
|
19
|
+
kind: "code",
|
|
20
|
+
endStates: ["changed", "unchanged"],
|
|
21
|
+
access: accessOf(input.access),
|
|
22
|
+
inputsSettledBy: [...input.settledBy],
|
|
23
|
+
run: function* (task) {
|
|
24
|
+
const fingerprint = yield* input.read(task);
|
|
25
|
+
const unchanged = Option.isSome(task.lastFingerprint) && task.lastFingerprint.value === fingerprint;
|
|
26
|
+
return { endState: unchanged ? "unchanged" : "changed", fingerprint };
|
|
27
|
+
},
|
|
28
|
+
}),
|
|
29
|
+
code: (input) => ({
|
|
30
|
+
kind: "code",
|
|
31
|
+
endStates: input.endStates,
|
|
32
|
+
access: accessOf(input.access),
|
|
33
|
+
run: input.run,
|
|
34
|
+
...(input.retries !== undefined ? { retries: input.retries } : {}),
|
|
35
|
+
...(input.retention !== undefined ? { retention: input.retention } : {}),
|
|
36
|
+
}),
|
|
37
|
+
agent: (input) => ({
|
|
38
|
+
kind: "agent",
|
|
39
|
+
endStates: input.endStates,
|
|
40
|
+
access: accessOf(input.access),
|
|
41
|
+
run: input.run,
|
|
42
|
+
...(input.retries !== undefined ? { retries: input.retries } : {}),
|
|
43
|
+
...(input.retention !== undefined ? { retention: input.retention } : {}),
|
|
44
|
+
}),
|
|
45
|
+
workflow: (input) => ({ kind: "workflow", endStates: input.workflow.endStates, workflow: { name: input.workflow.name } }),
|
|
46
|
+
each: (input) => ({
|
|
47
|
+
kind: "workflow",
|
|
48
|
+
endStates: [everySubWorkflowTaskEnded],
|
|
49
|
+
workflow: { name: input.workflow.name, each: input.each, ...(input.collect !== undefined ? { collect: input.collect } : {}) },
|
|
50
|
+
}),
|
|
51
|
+
});
|
|
52
|
+
/** A timer's turn task: what the task schema decodes from `{}`, encoded as Convex stores it, or why it cannot be made. */
|
|
53
|
+
const turnFieldsOf = (fields) => {
|
|
54
|
+
const decoded = Schema.decodeUnknownExit(fields)({});
|
|
55
|
+
if (Exit.isFailure(decoded))
|
|
56
|
+
return { fieldsProblem: `its task schema cannot make a task from {}, so some field has no default: ${Cause.pretty(decoded.cause)}` };
|
|
57
|
+
const encoded = Schema.encodeUnknownExit(fields)(decoded.value);
|
|
58
|
+
if (Exit.isFailure(encoded))
|
|
59
|
+
return { fieldsProblem: `the task its schema makes from {} cannot be encoded: ${Cause.pretty(encoded.cause)}` };
|
|
60
|
+
return { fields: encoded.value };
|
|
61
|
+
};
|
|
62
|
+
export const make = (input) => {
|
|
63
|
+
const steps = input.steps(stepBuilder());
|
|
64
|
+
const events = Object.entries(input.events).map(([id, event]) => event.kind === "timer"
|
|
65
|
+
? { id, kind: "timer", everyMs: event.everyMs, ...turnFieldsOf(input.task.fields) }
|
|
66
|
+
: event.kind === "change"
|
|
67
|
+
? { id, kind: "change", field: event.field, ...(event.resumes !== undefined ? { resumes: event.resumes } : {}) }
|
|
68
|
+
: event.kind === "list"
|
|
69
|
+
? { id, kind: "list", key: event.key, leftAs: event.leftAs }
|
|
70
|
+
: { id, kind: event.kind });
|
|
71
|
+
const graphSteps = Object.entries(steps).map(([id, step]) => {
|
|
72
|
+
if (step.kind === "workflow")
|
|
73
|
+
return { id, kind: "workflow", endStates: [...step.endStates], workflow: step.workflow };
|
|
74
|
+
const access = {
|
|
75
|
+
services: step.access.services.map((service) => service.key),
|
|
76
|
+
...(step.access.tasks !== undefined ? { tasks: { read: [...step.access.tasks.read], write: [...step.access.tasks.write] } } : {}),
|
|
77
|
+
};
|
|
78
|
+
return {
|
|
79
|
+
id,
|
|
80
|
+
kind: step.kind,
|
|
81
|
+
endStates: [...step.endStates],
|
|
82
|
+
access,
|
|
83
|
+
...(step.retries !== undefined ? { retries: { ...step.retries } } : {}),
|
|
84
|
+
...(step.inputsSettledBy !== undefined ? { inputsSettledBy: [...step.inputsSettledBy] } : {}),
|
|
85
|
+
...(step.syncs !== undefined ? { syncs: step.syncs } : {}),
|
|
86
|
+
...(step.retention !== undefined ? { retention: { ...step.retention } } : {}),
|
|
87
|
+
};
|
|
88
|
+
});
|
|
89
|
+
const runnableSteps = {};
|
|
90
|
+
for (const [id, step] of Object.entries(steps)) {
|
|
91
|
+
if (step.kind !== "workflow")
|
|
92
|
+
runnableSteps[id] = step;
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
name: input.name,
|
|
96
|
+
task: input.task,
|
|
97
|
+
endStates: input.endStates,
|
|
98
|
+
runnableSteps,
|
|
99
|
+
graph: {
|
|
100
|
+
name: input.name,
|
|
101
|
+
endStates: [...input.endStates],
|
|
102
|
+
events,
|
|
103
|
+
steps: graphSteps,
|
|
104
|
+
lines: input.lines.map((line) => ({ id: lineIdOf(line), from: line.from, to: line.to })),
|
|
105
|
+
budgets: input.budgets,
|
|
106
|
+
...(input.retention !== undefined ? { retention: { ...input.retention } } : {}),
|
|
107
|
+
...(input.inProgress !== undefined ? { inProgress: { ...input.inProgress } } : {}),
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
};
|
|
111
|
+
//# sourceMappingURL=workflow.js.map
|