Skip to content
Open
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
2 changes: 0 additions & 2 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,5 @@ jobs:
- run: npm i
- run: npm run build
- run: npx pkg-pr-new publish
- run: cd example && npm i && cd ..
- run: npm run typecheck
- run: cd example && npm run lint && cd ..
- run: npm test
2 changes: 1 addition & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default [
...typescriptEslint.configs["recommended"].rules,
...pluginJs.configs.recommended.rules,
"@typescript-eslint/no-floating-promises": "error",
"@typescript-eslint/no-explicit-any": "warn",
"@typescript-eslint/no-explicit-any": "off",
// allow (_arg: number) => {} and const _foo = 1;
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": [
Expand Down
225 changes: 164 additions & 61 deletions example/convex/_generated/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import type * as admin from "../admin.js";
import type * as example from "../example.js";
import type * as transcription from "../transcription.js";
import type * as userConfirmation from "../userConfirmation.js";

import type {
ApiFromModules,
Expand All @@ -30,6 +31,7 @@ declare const fullApi: ApiFromModules<{
admin: typeof admin;
example: typeof example;
transcription: typeof transcription;
userConfirmation: typeof userConfirmation;
}>;
declare const fullApiWithMounts: typeof fullApi;

Expand All @@ -44,30 +46,80 @@ export declare const internal: FilterApi<

export declare const components: {
workflow: {
event: {
create: FunctionReference<
"mutation",
"internal",
{ name: string; workflowId: string },
string
>;
send: FunctionReference<
"mutation",
"internal",
{
eventId?: string;
name?: string;
result:
| { kind: "success"; returnValue: any }
| { error: string; kind: "failed" }
| { kind: "canceled" };
workflowId: string;
workpoolOptions?: {
defaultRetryBehavior?: {
base: number;
initialBackoffMs: number;
maxAttempts: number;
};
logLevel?: "DEBUG" | "TRACE" | "INFO" | "REPORT" | "WARN" | "ERROR";
maxParallelism?: number;
retryActionsByDefault?: boolean;
};
},
string
>;
};
journal: {
load: FunctionReference<
"query",
"internal",
{ workflowId: string },
{ shortCircuit?: boolean; workflowId: string },
{
blocked?: boolean;
journalEntries: Array<{
_creationTime: number;
_id: string;
step: {
args: any;
argsSize: number;
completedAt?: number;
functionType: "query" | "mutation" | "action";
handle: string;
inProgress: boolean;
name: string;
runResult?:
| { kind: "success"; returnValue: any }
| { error: string; kind: "failed" }
| { kind: "canceled" };
startedAt: number;
workId?: string;
};
step:
| {
args: any;
argsSize: number;
completedAt?: number;
functionType: "query" | "mutation" | "action";
handle: string;
inProgress: boolean;
kind?: "function";
name: string;
runResult?:
| { kind: "success"; returnValue: any }
| { error: string; kind: "failed" }
| { kind: "canceled" };
startedAt: number;
workId?: string;
}
| {
args: { eventId?: string };
argsSize: number;
completedAt?: number;
eventId?: string;
inProgress: boolean;
kind: "event";
name: string;
runResult?:
| { kind: "success"; returnValue: any }
| { error: string; kind: "failed" }
| { kind: "canceled" };
startedAt: number;
workId?: string;
};
stepNumber: number;
workflowId: string;
}>;
Expand Down Expand Up @@ -101,21 +153,38 @@ export declare const components: {
| boolean
| { base: number; initialBackoffMs: number; maxAttempts: number };
schedulerOptions?: { runAt?: number } | { runAfter?: number };
step: {
args: any;
argsSize: number;
completedAt?: number;
functionType: "query" | "mutation" | "action";
handle: string;
inProgress: boolean;
name: string;
runResult?:
| { kind: "success"; returnValue: any }
| { error: string; kind: "failed" }
| { kind: "canceled" };
startedAt: number;
workId?: string;
};
step:
| {
args: any;
argsSize: number;
completedAt?: number;
functionType: "query" | "mutation" | "action";
handle: string;
inProgress: boolean;
kind?: "function";
name: string;
runResult?:
| { kind: "success"; returnValue: any }
| { error: string; kind: "failed" }
| { kind: "canceled" };
startedAt: number;
workId?: string;
}
| {
args: { eventId?: string };
argsSize: number;
completedAt?: number;
eventId?: string;
inProgress: boolean;
kind: "event";
name: string;
runResult?:
| { kind: "success"; returnValue: any }
| { error: string; kind: "failed" }
| { kind: "canceled" };
startedAt: number;
workId?: string;
};
}>;
workflowId: string;
workpoolOptions?: {
Expand All @@ -132,21 +201,38 @@ export declare const components: {
Array<{
_creationTime: number;
_id: string;
step: {
args: any;
argsSize: number;
completedAt?: number;
functionType: "query" | "mutation" | "action";
handle: string;
inProgress: boolean;
name: string;
runResult?:
| { kind: "success"; returnValue: any }
| { error: string; kind: "failed" }
| { kind: "canceled" };
startedAt: number;
workId?: string;
};
step:
| {
args: any;
argsSize: number;
completedAt?: number;
functionType: "query" | "mutation" | "action";
handle: string;
inProgress: boolean;
kind?: "function";
name: string;
runResult?:
| { kind: "success"; returnValue: any }
| { error: string; kind: "failed" }
| { kind: "canceled" };
startedAt: number;
workId?: string;
}
| {
args: { eventId?: string };
argsSize: number;
completedAt?: number;
eventId?: string;
inProgress: boolean;
kind: "event";
name: string;
runResult?:
| { kind: "success"; returnValue: any }
| { error: string; kind: "failed" }
| { kind: "canceled" };
startedAt: number;
workId?: string;
};
stepNumber: number;
workflowId: string;
}>
Expand Down Expand Up @@ -199,21 +285,38 @@ export declare const components: {
inProgress: Array<{
_creationTime: number;
_id: string;
step: {
args: any;
argsSize: number;
completedAt?: number;
functionType: "query" | "mutation" | "action";
handle: string;
inProgress: boolean;
name: string;
runResult?:
| { kind: "success"; returnValue: any }
| { error: string; kind: "failed" }
| { kind: "canceled" };
startedAt: number;
workId?: string;
};
step:
| {
args: any;
argsSize: number;
completedAt?: number;
functionType: "query" | "mutation" | "action";
handle: string;
inProgress: boolean;
kind?: "function";
name: string;
runResult?:
| { kind: "success"; returnValue: any }
| { error: string; kind: "failed" }
| { kind: "canceled" };
startedAt: number;
workId?: string;
}
| {
args: { eventId?: string };
argsSize: number;
completedAt?: number;
eventId?: string;
inProgress: boolean;
kind: "event";
name: string;
runResult?:
| { kind: "success"; returnValue: any }
| { error: string; kind: "failed" }
| { kind: "canceled" };
startedAt: number;
workId?: string;
};
stepNumber: number;
workflowId: string;
}>;
Expand Down
50 changes: 50 additions & 0 deletions example/convex/userConfirmation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { defineEvent, vWorkflowId } from "@convex-dev/workflow";
import { v } from "convex/values";
import { internal } from "./_generated/api";
import { internalAction, mutation } from "./_generated/server";
import { workflow } from "./example";

const approvalEvent = defineEvent({
name: "approval",
validator: v.union(
v.object({ approved: v.literal(true), choice: v.number() }),
v.object({ approved: v.literal(false), reason: v.string() }),
),
});

export const confirmationWorkflow = workflow.define({
args: { prompt: v.string() },
returns: v.string(),
handler: async (step, args): Promise<string> => {
const proposals = await step.runAction(
internal.userConfirmation.generateProposals,
{ prompt: args.prompt },
{ retry: true },
);
const approval = await step.awaitEvent(approvalEvent);
if (!approval.approved) {
return "rejected: " + approval.reason;
}
const choice = proposals[approval.choice];
return choice;
},
});

export const generateProposals = internalAction({
args: { prompt: v.string() },
handler: async (ctx, args) => {
// imagine this is a call to an LLM
return ["proposal1", "proposal2", "proposal3"];
},
});

export const chooseProposal = mutation({
args: { workflowId: vWorkflowId, choice: v.number() },
handler: async (ctx, args) => {
await workflow.sendEvent(ctx, args.workflowId, approvalEvent, {
approved: true,
choice: args.choice,
});
return true;
},
});
35 changes: 35 additions & 0 deletions src/client/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { EventId, EventSpec, VEventId } from "../types.js";
import { v, type Infer, type Validator } from "convex/values";

/**
* Define a named event with a validator.
* @param spec - The event spec.
* @returns Utility functions to specify type-safe events and results.
*/
export function defineEvent<
Name extends string,
V extends Validator<any, any, any>,
>(spec: {
name: Name;
validator?: V;
}): EventSpec<Name, Infer<V>> & {
/**
* A validator for the named event ID.
*/
vEventId: VEventId<Name>;
/**
* Use this to provide an ID to `awaitEvent` or `sendEvent`.
*/
withId: (id: EventId<Name>) => EventSpec<Name, Infer<V>>;
} {
return {
...spec,
withId: (id: EventId<Name>) => ({ ...spec, id }),
vEventId: v.string() as VEventId<Name>,
};
}

export type TypedRunResult<T> =
| { kind: "success"; returnValue: T }
| { kind: "failed"; error: string }
| { kind: "canceled" };
Loading
Loading