diff --git a/docs/content/docs/api-reference/workflow-api/get-world.mdx b/docs/content/docs/api-reference/workflow-api/get-world.mdx new file mode 100644 index 000000000..db3d6c039 --- /dev/null +++ b/docs/content/docs/api-reference/workflow-api/get-world.mdx @@ -0,0 +1,92 @@ +--- +title: getWorld +--- + +Retrieves the World instance for direct access to workflow storage, queuing, and streaming backends. This function returns a `World` which provides low-level access to manage workflow runs, steps, events, and hooks. + +Use this function when you need direct access to the underlying workflow infrastructure, such as listing all runs, querying events, or implementing custom workflow management logic. + +```typescript lineNumbers +import { getWorld } from "workflow/api"; + +const world = getWorld(); +``` + +## API Signature + +### Parameters + +This function does not accept any parameters. + +### Returns + +Returns a `World` object: + + + +## Examples + +### List Workflow Runs + +List all workflow runs with pagination: + +```typescript lineNumbers +import { getWorld } from "workflow/api"; + +export async function GET(req: Request) { + const url = new URL(req.url); + const cursor = url.searchParams.get("cursor") ?? undefined; + + try { + const world = getWorld(); // [!code highlight] + const runs = await world.runs.list({ + pagination: { cursor }, + }); + + return Response.json(runs); + } catch (error) { + return Response.json( + { error: "Failed to list workflow runs" }, + { status: 500 } + ); + } +} +``` + +### Cancel a Workflow Run + +Cancel a running workflow: + +```typescript lineNumbers +import { getWorld } from "workflow/api"; + +export async function POST(req: Request) { + const { runId } = await req.json(); + + if (!runId) { + return Response.json({ error: "No runId provided" }, { status: 400 }); + } + + try { + const world = getWorld(); // [!code highlight] + const run = await world.runs.cancel(runId); // [!code highlight] + + return Response.json({ status: run.status }); + } catch (error) { + return Response.json( + { error: "Failed to cancel workflow run" }, + { status: 500 } + ); + } +} +``` + +## Related Functions + +- [`getRun()`](/docs/api-reference/workflow-api/get-run) - Higher-level API for working with individual runs by ID. +- [`start()`](/docs/api-reference/workflow-api/start) - Start a new workflow run. diff --git a/docs/content/docs/api-reference/workflow-api/index.mdx b/docs/content/docs/api-reference/workflow-api/index.mdx index c1b43bc4d..4e569aa8b 100644 --- a/docs/content/docs/api-reference/workflow-api/index.mdx +++ b/docs/content/docs/api-reference/workflow-api/index.mdx @@ -6,7 +6,7 @@ API reference for runtime functions from the `workflow/api` package. ## Functions -Workflow DevKit provides runtime functions that are used outside of workflow and step functions. These are accessed from the runtime entrypoint (e.g. where `start(workflowFn)` is called): +The API package is for access and introspection of workflow data to inspect runs, start new runs, or access anything else directly accessible by the world. @@ -21,4 +21,7 @@ Workflow DevKit provides runtime functions that are used outside of workflow and Get workflow run status and metadata without waiting for completion. + + Get direct access to workflow storage, queuing, and streaming backends. +