From 2947d4eb2f2152e7e2b950e9b48d3d5f8cf753e4 Mon Sep 17 00:00:00 2001 From: Ian Macartney Date: Mon, 20 Oct 2025 12:01:21 -0700 Subject: [PATCH] simple way to share component api --- example/convex/_generated/api.d.ts | 199 +---------------------------- src/client/index.ts | 4 +- src/client/types.ts | 34 +++-- src/component/_generated/api.d.ts | 90 +------------ 4 files changed, 25 insertions(+), 302 deletions(-) diff --git a/example/convex/_generated/api.d.ts b/example/convex/_generated/api.d.ts index 0d97802..54b4da1 100644 --- a/example/convex/_generated/api.d.ts +++ b/example/convex/_generated/api.d.ts @@ -8,6 +8,7 @@ * @module */ +import { ComponentApi } from "../../../src/client/types.js"; import type * as admin from "../admin.js"; import type * as example from "../example.js"; import type * as transcription from "../transcription.js"; @@ -43,199 +44,7 @@ export declare const internal: FilterApi< >; export declare const components: { - workflow: { - journal: { - load: FunctionReference< - "query", - "internal", - { workflowId: string }, - { - 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; - }; - stepNumber: number; - workflowId: string; - }>; - logLevel: "DEBUG" | "TRACE" | "INFO" | "REPORT" | "WARN" | "ERROR"; - ok: boolean; - workflow: { - _creationTime: number; - _id: string; - args: any; - generationNumber: number; - logLevel?: any; - name?: string; - onComplete?: { context?: any; fnHandle: string }; - runResult?: - | { kind: "success"; returnValue: any } - | { error: string; kind: "failed" } - | { kind: "canceled" }; - startedAt?: any; - state?: any; - workflowHandle: string; - }; - } - >; - startSteps: FunctionReference< - "mutation", - "internal", - { - generationNumber: number; - steps: Array<{ - retry?: - | 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; - }; - }>; - workflowId: string; - workpoolOptions?: { - defaultRetryBehavior?: { - base: number; - initialBackoffMs: number; - maxAttempts: number; - }; - logLevel?: "DEBUG" | "TRACE" | "INFO" | "REPORT" | "WARN" | "ERROR"; - maxParallelism?: number; - retryActionsByDefault?: boolean; - }; - }, - 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; - }; - stepNumber: number; - workflowId: string; - }> - >; - }; - workflow: { - cancel: FunctionReference< - "mutation", - "internal", - { workflowId: string }, - null - >; - cleanup: FunctionReference< - "mutation", - "internal", - { workflowId: string }, - boolean - >; - complete: FunctionReference< - "mutation", - "internal", - { - generationNumber: number; - runResult: - | { kind: "success"; returnValue: any } - | { error: string; kind: "failed" } - | { kind: "canceled" }; - workflowId: string; - }, - null - >; - create: FunctionReference< - "mutation", - "internal", - { - maxParallelism?: number; - onComplete?: { context?: any; fnHandle: string }; - startAsync?: boolean; - workflowArgs: any; - workflowHandle: string; - workflowName: string; - }, - string - >; - getStatus: FunctionReference< - "query", - "internal", - { workflowId: string }, - { - 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; - }; - stepNumber: number; - workflowId: string; - }>; - logLevel: "DEBUG" | "TRACE" | "INFO" | "REPORT" | "WARN" | "ERROR"; - workflow: { - _creationTime: number; - _id: string; - args: any; - generationNumber: number; - logLevel?: any; - name?: string; - onComplete?: { context?: any; fnHandle: string }; - runResult?: - | { kind: "success"; returnValue: any } - | { error: string; kind: "failed" } - | { kind: "canceled" }; - startedAt?: any; - state?: any; - workflowHandle: string; - }; - } - >; - }; - }; + workflow: ComponentApi< + typeof import("../../../src/component/_generated/api.js").api + >; }; diff --git a/src/client/index.ts b/src/client/index.ts index 2cc5c5b..0b87e0f 100644 --- a/src/client/index.ts +++ b/src/client/index.ts @@ -17,7 +17,7 @@ import type { ObjectType, PropertyValidators, Validator } from "convex/values"; import type { Step } from "../component/schema.js"; import type { OnCompleteArgs, WorkflowId } from "../types.js"; import { safeFunctionName } from "./safeFunctionName.js"; -import type { OpaqueIds, WorkflowComponent, WorkflowStep } from "./types.js"; +import type { WorkflowComponent, WorkflowStep } from "./types.js"; import { workflowMutation } from "./workflowMutation.js"; export { vWorkflowId, type WorkflowId } from "../types.js"; @@ -71,7 +71,7 @@ export type WorkflowDefinition< }; export type WorkflowStatus = - | { type: "inProgress"; running: OpaqueIds[] } + | { type: "inProgress"; running: Step[] } | { type: "completed" } | { type: "canceled" } | { type: "failed"; error: string }; diff --git a/src/client/types.ts b/src/client/types.ts index 9e90065..2f38bae 100644 --- a/src/client/types.ts +++ b/src/client/types.ts @@ -1,4 +1,4 @@ -import type { RetryOption, WorkId } from "@convex-dev/workpool"; +import type { RetryOption } from "@convex-dev/workpool"; import type { Expand, FunctionArgs, @@ -6,10 +6,10 @@ import type { FunctionReturnType, } from "convex/server"; import type { api } from "../component/_generated/api.js"; -import type { GenericId } from "convex/values"; +import type { GenericId, Value } from "convex/values"; import type { WorkflowId } from "../types.js"; -export type WorkflowComponent = UseApi; +export type WorkflowComponent = ComponentApi; export type RunOptions = { /** @@ -83,31 +83,27 @@ export type WorkflowStep = { ): Promise>; }; -export type UseApi = Expand<{ +export type ComponentApi = Expand<{ [mod in keyof API]: API[mod] extends FunctionReference< infer FType, "public", infer FArgs, - infer FReturnType, - infer FComponentPath + infer FReturnType > ? FunctionReference< FType, "internal", - OpaqueIds, - OpaqueIds, - FComponentPath + StringifyIds, + StringifyIds > - : UseApi; + : ComponentApi; }>; -export type OpaqueIds = - T extends GenericId +export type StringifyIds = + T extends GenericId ? string - : T extends WorkId - ? string - : T extends (infer U)[] - ? OpaqueIds[] - : T extends object - ? { [K in keyof T]: OpaqueIds } - : T; + : T extends (infer U)[] + ? StringifyIds[] + : T extends Record + ? { [K in keyof T]: StringifyIds } + : T; diff --git a/src/component/_generated/api.d.ts b/src/component/_generated/api.d.ts index 2caf60c..d5a5484 100644 --- a/src/component/_generated/api.d.ts +++ b/src/component/_generated/api.d.ts @@ -8,6 +8,7 @@ * @module */ +import type { ComponentApi } from "../../client/types.ts"; import type * as journal from "../journal.js"; import type * as logging from "../logging.js"; import type * as model from "../model.js"; @@ -247,90 +248,7 @@ export declare const internal: FilterApi< >; export declare const components: { - workpool: { - lib: { - cancel: FunctionReference< - "mutation", - "internal", - { - id: string; - logLevel: "DEBUG" | "TRACE" | "INFO" | "REPORT" | "WARN" | "ERROR"; - }, - any - >; - cancelAll: FunctionReference< - "mutation", - "internal", - { - before?: number; - logLevel: "DEBUG" | "TRACE" | "INFO" | "REPORT" | "WARN" | "ERROR"; - }, - any - >; - enqueue: FunctionReference< - "mutation", - "internal", - { - config: { - logLevel: "DEBUG" | "TRACE" | "INFO" | "REPORT" | "WARN" | "ERROR"; - maxParallelism: number; - }; - fnArgs: any; - fnHandle: string; - fnName: string; - fnType: "action" | "mutation" | "query"; - onComplete?: { context?: any; fnHandle: string }; - retryBehavior?: { - base: number; - initialBackoffMs: number; - maxAttempts: number; - }; - runAt: number; - }, - string - >; - enqueueBatch: FunctionReference< - "mutation", - "internal", - { - config: { - logLevel: "DEBUG" | "TRACE" | "INFO" | "REPORT" | "WARN" | "ERROR"; - maxParallelism: number; - }; - items: Array<{ - fnArgs: any; - fnHandle: string; - fnName: string; - fnType: "action" | "mutation" | "query"; - onComplete?: { context?: any; fnHandle: string }; - retryBehavior?: { - base: number; - initialBackoffMs: number; - maxAttempts: number; - }; - runAt: number; - }>; - }, - Array - >; - status: FunctionReference< - "query", - "internal", - { id: string }, - | { previousAttempts: number; state: "pending" } - | { previousAttempts: number; state: "running" } - | { state: "finished" } - >; - statusBatch: FunctionReference< - "query", - "internal", - { ids: Array }, - Array< - | { previousAttempts: number; state: "pending" } - | { previousAttempts: number; state: "running" } - | { state: "finished" } - > - >; - }; - }; + workpool: ComponentApi< + typeof import("../../../node_modules/@convex-dev/workpool/dist/component/_generated/api.js").api + >; };