Files
Nathan Colosimo 7a46a81a53 Upgrade to Zod 4.5 and compile schemas (#3902)
Co-authored-by: VaguelySerious <mittgfu@gmail.com>
Co-authored-by: Peter Wielander <peter.wielander@vercel.com>
2026-09-11 09:00:20 -07:00

48 lines
1.0 KiB
TypeScript

import { defineHook, getWritable } from '@workflow/core';
import * as z from 'zod';
export const Hook = defineHook({
schema: z.compile(
z.object({
data: z.string(),
done: z.boolean().optional(),
metadata: z.unknown(),
})
),
});
export async function collectWithHook(token: string, customData: string) {
'use workflow';
const hook = Hook.create({ token, metadata: { customData } });
const collected: Parameters<(typeof Hook)['resume']>[1][] = [];
const wf = getWritable();
await writeEvent(wf, 'hookCreated');
for await (const event of hook) {
await writeEvent(wf, 'hookResumed', event);
collected.push(event);
if (event.done) break;
}
return { collected };
}
async function writeEvent(
writable: WritableStream,
event: string,
payload?: unknown
) {
'use step';
console.log('writing event', event, payload);
const writer = writable.getWriter();
await writer.write(
new TextEncoder().encode(
`${JSON.stringify({
event,
payload,
})}\r\n`
)
);
}