Skip to content
Draft
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
5 changes: 5 additions & 0 deletions .changeset/h2-client.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@workflow/world-vercel": patch
---

perf: use HTTP/2 with connection multiplexing for API requests
1 change: 1 addition & 0 deletions packages/world-vercel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@workflow/errors": "workspace:*",
"@workflow/world": "workspace:*",
"cbor-x": "1.6.0",
"undici": "^6.21.0",
"zod": "catalog:"
},
"peerDependencies": {
Expand Down
15 changes: 9 additions & 6 deletions packages/world-vercel/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getVercelOidcToken } from '@vercel/oidc';
import { WorkflowAPIError } from '@workflow/errors';
import { type StructuredError, StructuredErrorSchema } from '@workflow/world';
import { decode, encode } from 'cbor-x';
import { Agent, fetch as undiciFetch } from 'undici';
import type { z } from 'zod';
import {
ErrorType,
Expand All @@ -21,6 +22,8 @@ import {
} from './telemetry.js';
import { version } from './version.js';

export const dispatcher = new Agent({ allowH2: true, pipelining: 10 });

/**
* Hard-coded workflow-server URL override for testing.
* Set this to test against a different workflow-server version.
Expand Down Expand Up @@ -268,12 +271,12 @@ export async function makeRequest<T>({
body = encode(data);
}

const request = new Request(url, {
const response = await undiciFetch(url, {
...options,
body,
headers,
dispatcher,
});
const response = await fetch(request);

span?.setAttributes({
...HttpResponseStatusCode(response.status),
Expand All @@ -289,7 +292,7 @@ export async function makeRequest<T>({
.map(([key, value]: [string, string]) => `-H "${key}: ${value}"`)
.join(' ');
console.error(
`Failed to fetch, reproduce with:\ncurl -X ${request.method} ${stringifiedHeaders} "${url}"`
`Failed to fetch, reproduce with:\ncurl -X ${method} ${stringifiedHeaders} "${url}"`
);
}

Expand All @@ -307,7 +310,7 @@ export async function makeRequest<T>({

const error = new WorkflowAPIError(
errorData.message ||
`${request.method} ${endpoint} -> HTTP ${response.status}: ${response.statusText}`,
`${method} ${endpoint} -> HTTP ${response.status}: ${response.statusText}`,
{ url, status: response.status, code: errorData.code, retryAfter }
);
// Record error attributes per OTEL conventions
Expand All @@ -334,7 +337,7 @@ export async function makeRequest<T>({
} catch (error) {
const contentType = response.headers.get('Content-Type') || 'unknown';
throw new WorkflowAPIError(
`Failed to parse response body for ${request.method} ${endpoint} (Content-Type: ${contentType}):\n\n${error}`,
`Failed to parse response body for ${method} ${endpoint} (Content-Type: ${contentType}):\n\n${error}`,
{ url, cause: error }
);
}
Expand All @@ -344,7 +347,7 @@ export async function makeRequest<T>({
const validationResult = schema.safeParse(parseResult.data);
if (!validationResult.success) {
throw new WorkflowAPIError(
`Schema validation failed for ${request.method} ${endpoint}:\n\n${validationResult.error}\n\nResponse context: ${parseResult.getDebugContext()}`,
`Schema validation failed for ${method} ${endpoint}:\n\n${validationResult.error}\n\nResponse context: ${parseResult.getDebugContext()}`,
{ url, cause: validationResult.error }
);
}
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading