-
Notifications
You must be signed in to change notification settings - Fork 244
Refactor web-shared to separate UI components from server concerns #927
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
6e37181
Merge branch 'main' of github.com:vercel/workflow into karthik/expose…
karthikscale3 6264999
Refactor web-shared to separate UI components from server concerns
karthikscale3 f2bf157
add support for streams
karthikscale3 3126f91
Merge branch 'main' of github.com:vercel/workflow into karthik/expose…
karthikscale3 2667ebd
Merge branch 'main' of github.com:vercel/workflow into karthik/expose…
karthikscale3 3c953df
update lock file
karthikscale3 077c1e6
Merge branch 'main' of github.com:vercel/workflow into karthik/expose…
karthikscale3 0dd91f4
Migrate helpers from web-shared to core
karthikscale3 d3c3036
Migrate helpers from web-shared to core
karthikscale3 55732aa
Migrate helpers from web-shared to core
karthikscale3 2177860
Migrate helpers from web-shared to core
karthikscale3 b0953cc
Merge branch 'main' of github.com:vercel/workflow into karthik/expose…
karthikscale3 1803ee2
Merge branch 'main' of github.com:vercel/workflow into karthik/expose…
karthikscale3 bdae698
Fix claude's comments
karthikscale3 deddc66
Fix server actions bug for web
karthikscale3 7268d48
fix copilot bugs
karthikscale3 a767477
Merge branch 'main' of github.com:vercel/workflow into karthik/expose…
karthikscale3 84e167e
Merge branch 'main' of github.com:vercel/workflow into karthik/expose…
karthikscale3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| "@workflow/web-shared": patch | ||
| "@workflow/core": patch | ||
| "@workflow/web": patch | ||
| --- | ||
|
|
||
| Added subpatch exports for runtime modules to allow direct imports in core. Refactored web-shared to be a thin package that exported UI components and world-actions. Updated web package to consume the UI components and world-actions from web-shared. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './dist/runtime.d.ts'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './dist/runtime.js'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,255 @@ | ||
| import { hydrateWorkflowArguments } from '../serialization.js'; | ||
| import { | ||
| type Event, | ||
| isLegacySpecVersion, | ||
| SPEC_VERSION_LEGACY, | ||
| type World, | ||
| } from '@workflow/world'; | ||
| import { getWorkflowQueueName } from './helpers.js'; | ||
| import { start } from './start.js'; | ||
|
|
||
| export interface RecreateRunOptions { | ||
| deploymentId?: string; | ||
| specVersion?: number; | ||
| } | ||
|
|
||
| export interface StopSleepResult { | ||
| /** Number of pending sleeps that were stopped */ | ||
| stoppedCount: number; | ||
| } | ||
|
|
||
| export interface ReadStreamOptions { | ||
| /** | ||
| * The index to start reading from. Defaults to 0. | ||
| */ | ||
| startIndex?: number; | ||
| } | ||
|
|
||
| export interface StopSleepOptions { | ||
| /** | ||
| * Optional list of specific correlation IDs to target. | ||
| * If provided, only these sleep calls will be interrupted. | ||
| * If not provided, all pending sleep calls will be interrupted. | ||
| */ | ||
| correlationIds?: string[]; | ||
| } | ||
|
|
||
| const normalizeWorkflowArgs = (args: unknown): unknown[] => { | ||
| return Array.isArray(args) ? args : [args]; | ||
| }; | ||
|
|
||
| /** | ||
| * Start a new workflow run based on an existing run. | ||
| */ | ||
| export async function recreateRunFromExisting( | ||
|
karthikscale3 marked this conversation as resolved.
|
||
| world: World, | ||
| runId: string, | ||
| options: RecreateRunOptions = {} | ||
| ): Promise<string> { | ||
| try { | ||
| const run = await world.runs.get(runId, { resolveData: 'all' }); | ||
| const workflowArgs = normalizeWorkflowArgs( | ||
| hydrateWorkflowArguments(run.input, globalThis) | ||
| ); | ||
| const specVersion = | ||
| options.specVersion ?? run.specVersion ?? SPEC_VERSION_LEGACY; | ||
| const deploymentId = options.deploymentId ?? run.deploymentId; | ||
|
|
||
| const newRun = await start( | ||
| { workflowId: run.workflowName }, | ||
| workflowArgs as unknown[], | ||
| { | ||
| deploymentId, | ||
| world, | ||
| specVersion, | ||
| } | ||
| ); | ||
| return newRun.runId; | ||
| } catch (err) { | ||
| throw new Error( | ||
| `Failed to recreate run from ${runId}: ${err instanceof Error ? err.message : String(err)}`, | ||
| { cause: err } | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Cancel a workflow run. | ||
| */ | ||
| export async function cancelRun(world: World, runId: string): Promise<void> { | ||
| try { | ||
| const run = await world.runs.get(runId, { resolveData: 'none' }); | ||
| const specVersion = run.specVersion ?? SPEC_VERSION_LEGACY; | ||
| const compatMode = isLegacySpecVersion(specVersion); | ||
| const eventData = { | ||
| eventType: 'run_cancelled' as const, | ||
| specVersion, | ||
| }; | ||
| await world.events.create(runId, eventData, { v1Compat: compatMode }); | ||
| } catch (err) { | ||
| throw new Error( | ||
| `Failed to cancel run ${runId}: ${err instanceof Error ? err.message : String(err)}`, | ||
| { cause: err } | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Re-enqueue a workflow run. | ||
| */ | ||
| export async function reenqueueRun(world: World, runId: string): Promise<void> { | ||
| try { | ||
| const run = await world.runs.get(runId, { resolveData: 'none' }); | ||
| await world.queue( | ||
| getWorkflowQueueName(run.workflowName), | ||
| { | ||
| runId, | ||
| }, | ||
| { | ||
| deploymentId: run.deploymentId, | ||
| } | ||
| ); | ||
| } catch (err) { | ||
| throw new Error( | ||
| `Failed to re-enqueue run ${runId}: ${err instanceof Error ? err.message : String(err)}`, | ||
| { cause: err } | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Wake up a workflow run by interrupting pending sleep() calls. | ||
| */ | ||
| export async function wakeUpRun( | ||
| world: World, | ||
| runId: string, | ||
| options?: StopSleepOptions | ||
| ): Promise<StopSleepResult> { | ||
| try { | ||
| const run = await world.runs.get(runId, { resolveData: 'none' }); | ||
| const compatMode = isLegacySpecVersion(run.specVersion); | ||
|
|
||
| // Paginate through all events to ensure we don't miss any sleeps | ||
| // in long-running workflows with more than 1000 events. | ||
| const allEvents: Event[] = []; | ||
| let cursor: string | null = null; | ||
| do { | ||
| const eventsResult = await world.events.list({ | ||
| runId, | ||
| pagination: { limit: 1000, ...(cursor ? { cursor } : {}) }, | ||
| resolveData: 'none', | ||
| }); | ||
| allEvents.push(...eventsResult.data); | ||
| cursor = eventsResult.hasMore ? eventsResult.cursor : null; | ||
| } while (cursor); | ||
|
|
||
| const waitCreatedEvents = allEvents.filter( | ||
| (event: Event) => event.eventType === 'wait_created' | ||
| ); | ||
| const waitCompletedCorrelationIds = new Set( | ||
| allEvents | ||
| .filter((event: Event) => event.eventType === 'wait_completed') | ||
| .map((event: Event) => event.correlationId) | ||
| ); | ||
|
|
||
| let pendingWaits = waitCreatedEvents.filter( | ||
| (event: Event) => !waitCompletedCorrelationIds.has(event.correlationId) | ||
| ); | ||
|
|
||
| if (options?.correlationIds && options.correlationIds.length > 0) { | ||
| const targetCorrelationIds = new Set(options.correlationIds); | ||
| pendingWaits = pendingWaits.filter( | ||
| (event: Event) => | ||
| event.correlationId && targetCorrelationIds.has(event.correlationId) | ||
| ); | ||
| } | ||
|
|
||
| const errors: Error[] = []; | ||
| let stoppedCount = 0; | ||
|
|
||
| for (const waitEvent of pendingWaits) { | ||
| if (!waitEvent.correlationId) continue; | ||
| const eventData = compatMode | ||
| ? { | ||
| eventType: 'wait_completed' as const, | ||
| correlationId: waitEvent.correlationId, | ||
| } | ||
| : { | ||
| eventType: 'wait_completed' as const, | ||
| correlationId: waitEvent.correlationId, | ||
| specVersion: run.specVersion, | ||
| }; | ||
| try { | ||
| await world.events.create(runId, eventData, { v1Compat: compatMode }); | ||
| stoppedCount++; | ||
| } catch (err) { | ||
| errors.push(err instanceof Error ? err : new Error(String(err))); | ||
| } | ||
| } | ||
|
|
||
| if (stoppedCount > 0) { | ||
| await world.queue( | ||
| getWorkflowQueueName(run.workflowName), | ||
| { | ||
| runId, | ||
| }, | ||
| { | ||
| deploymentId: run.deploymentId, | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| if (errors.length > 0) { | ||
| throw new AggregateError( | ||
| errors, | ||
| `Failed to complete ${errors.length}/${pendingWaits.length} pending wait(s) for run ${runId}` | ||
| ); | ||
| } | ||
|
|
||
| return { stoppedCount }; | ||
| } catch (err) { | ||
| if (err instanceof AggregateError) { | ||
| throw err; | ||
| } | ||
| throw new Error( | ||
| `Failed to wake up run ${runId}: ${err instanceof Error ? err.message : String(err)}`, | ||
| { cause: err } | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Read from a stream by stream ID. | ||
| * Returns a ReadableStream of Uint8Array chunks. | ||
| */ | ||
| export async function readStream( | ||
| world: World, | ||
| streamId: string, | ||
| options?: ReadStreamOptions | ||
| ): Promise<ReadableStream<Uint8Array>> { | ||
| try { | ||
| return await world.readFromStream(streamId, options?.startIndex); | ||
| } catch (err) { | ||
| throw new Error( | ||
| `Failed to read stream ${streamId}: ${err instanceof Error ? err.message : String(err)}`, | ||
| { cause: err } | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * List all stream IDs for a workflow run. | ||
| */ | ||
| export async function listStreams( | ||
| world: World, | ||
| runId: string | ||
| ): Promise<string[]> { | ||
| try { | ||
| return await world.listStreamsByRunId(runId); | ||
| } catch (err) { | ||
| throw new Error( | ||
| `Failed to list streams for run ${runId}: ${err instanceof Error ? err.message : String(err)}`, | ||
| { cause: err } | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice