-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.ts
More file actions
169 lines (150 loc) · 4.08 KB
/
Copy pathworker.ts
File metadata and controls
169 lines (150 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import type { ExecutionContext } from '@hono/hono';
import { buildApp } from './app_core.ts';
export { AppWriterAgent } from './app_writer/agent.ts';
import {
type AppGenerationWorkflowParams,
runAppGenerationFileWorkflowStep,
runAppGenerationFinishWorkflowStep,
runAppGenerationInitializationWorkflowStep,
runAppGenerationPlanningWorkflowStep,
} from './app_writer/workflow_runner.ts';
import { resolveWorkerServices, type WorkerBindings } from './app_worker_services.ts';
import { createObjectEnvReader } from './platform/env.ts';
interface WorkflowEntrypointRuntimeClass {
new (): object;
}
declare const WorkflowEntrypoint: WorkflowEntrypointRuntimeClass | undefined;
interface WorkflowEvent<Params> {
readonly payload: Readonly<Params>;
readonly timestamp: Date;
readonly instanceId: string;
}
interface WorkflowStep {
do<Result>(name: string, callback: () => Result | Promise<Result>): Promise<Result>;
do<Result>(
name: string,
config: WorkflowStepConfig,
callback: () => Result | Promise<Result>,
): Promise<Result>;
}
interface WorkflowStepConfig {
retries?: {
limit: number;
delay: string | number;
backoff?: 'constant' | 'linear' | 'exponential';
};
timeout?: string | number;
}
interface WorkerExecutionContext {
waitUntil?(promise: Promise<unknown>): void;
passThroughOnException?(): void;
}
const WorkflowEntrypointBase: WorkflowEntrypointRuntimeClass =
typeof WorkflowEntrypoint === 'function' ? WorkflowEntrypoint : Object;
export default {
fetch(
request: Request,
env: WorkerBindings = {},
executionContext?: WorkerExecutionContext,
): Response | Promise<Response> {
const app = buildApp(resolveWorkerServices(env, createObjectEnvReader(env)));
return app.fetch(request, env, toHonoExecutionContext(executionContext));
},
};
export class AppGenerationWorkflow extends WorkflowEntrypointBase {
declare readonly env: WorkerBindings;
async run(
event: WorkflowEvent<AppGenerationWorkflowParams>,
step: WorkflowStep,
): Promise<unknown> {
const initialized = await step.do(
'initialize app writer workspace',
{
retries: {
limit: 1,
delay: '30 seconds',
backoff: 'exponential',
},
timeout: '2 minutes',
},
() =>
runAppGenerationInitializationWorkflowStep({
bindings: this.env,
params: event.payload,
}),
);
if (!initialized.ok) {
return initialized.result;
}
const planned = await step.do(
'plan app writer generation',
{
retries: {
limit: 1,
delay: '30 seconds',
backoff: 'exponential',
},
timeout: '5 minutes',
},
() =>
runAppGenerationPlanningWorkflowStep({
bindings: this.env,
initialized: initialized.value,
}),
);
if (!planned.ok) {
return planned.result;
}
const generated = await step.do(
'write app writer scaffold files',
{
retries: {
limit: 1,
delay: '30 seconds',
backoff: 'exponential',
},
timeout: '45 minutes',
},
() =>
runAppGenerationFileWorkflowStep({
bindings: this.env,
planned: planned.value,
}),
);
if (!generated.ok) {
return generated.result;
}
return await step.do(
'validate repair and save app writer package',
{
retries: {
limit: 1,
delay: '30 seconds',
backoff: 'exponential',
},
timeout: '15 minutes',
},
() =>
runAppGenerationFinishWorkflowStep({
bindings: this.env,
generated: generated.value,
}),
);
}
}
function toHonoExecutionContext(
executionContext?: WorkerExecutionContext,
): ExecutionContext | undefined {
if (executionContext === undefined) {
return undefined;
}
return {
props: {},
waitUntil(promise) {
executionContext.waitUntil?.(promise);
},
passThroughOnException() {
executionContext.passThroughOnException?.();
},
};
}