[ PROMPT_NODE_24500 ]
Workflows 设计模式
[ SKILL_DOCUMENTATION ]
# 工作流模式
## 图像处理流水线
typescript
export class ImageProcessingWorkflow extends WorkflowEntrypoint {
async run(event, step) {
const imageData = await step.do('fetch', async () => (await this.env.BUCKET.get(event.params.imageKey)).arrayBuffer());
const description = await step.do('generate description', async () =>
await this.env.AI.run('@cf/llava-hf/llava-1.5-7b-hf', {image: Array.from(new Uint8Array(imageData)), prompt: 'Describe this image', max_tokens: 50})
);
await step.waitForEvent('await approval', { event: 'approved', timeout: '24h' });
await step.do('publish', async () => await this.env.BUCKET.put(`public/${event.params.imageKey}`, imageData));
}
}
## 用户生命周期
typescript
export class UserLifecycleWorkflow extends WorkflowEntrypoint {
async run(event, step) {
await step.do('welcome email', async () => await sendEmail(event.params.email, 'Welcome!'));
await step.sleep('trial period', '7 days');
const hasConverted = await step.do('check conversion', async () => {
const user = await this.env.DB.prepare('SELECT subscription_status FROM users WHERE id = ?').bind(event.params.userId).first();
return user.subscription_status === 'active';
});
if (!hasConverted) await step.do('trial expiration email', async () => await sendEmail(event.params.email, 'Trial ending'));
}
}
## 数据流水线
typescript
export class DataPipelineWorkflow extends WorkflowEntrypoint {
async run(event, step) {
const rawData = await step.do('extract', {retries: { limit: 10, delay: '30s', backoff: 'exponential' }}, async () => {
const res = await fetch(event.params.sourceUrl);
if (!res.ok) throw new Error('Fetch failed');
return res.json();
});
const transformed = await step.do('transform', async () =>
rawData.map(item => ({ id: item.id, normalized: normalizeData(item) }))
);
const dataRef = await step.do('store', async () => {
const key = `processed/${Date.now()}.json`;
await this.env.BUCKET.put(key, JSON.stringify(transformed));
return { key };
});
await step.do('load', async () => {
const data = await (await this.env.BUCKET.get(dataRef.key)).json();
for (let i = 0; i
this.env.DB.prepare('INSERT INTO records VALUES (?, ?)').bind(item.id, it