withruntime 0.34.0 → 0.37.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/BLAXEL.md +207 -204
- package/COMPATIBILITY.md +367 -337
- package/DAYTONA.md +180 -173
- package/E2B.md +246 -230
- package/README.md +182 -142
- package/README.src.md +182 -142
- package/VERCEL.md +173 -168
- package/agent-platforms-inventory.json +387 -0
- package/dist/agents-cli.d.ts +7 -1
- package/dist/agents-cli.js +219 -2
- package/dist/ci-cli.d.ts +7 -1
- package/dist/ci-cli.js +25 -1
- package/dist/cli-words.js +1 -0
- package/dist/cli.d.ts +1 -1
- package/dist/cli.js +25 -3
- package/dist/errors.d.ts +1 -1
- package/dist/errors.js +4 -1
- package/dist/functions/index.d.ts +10 -0
- package/dist/functions/index.js +11 -0
- package/dist/index.d.ts +3 -3
- package/dist/inngest/index.d.ts +96 -0
- package/dist/inngest/index.js +183 -0
- package/dist/jobs-code/cron.d.ts +12 -0
- package/dist/jobs-code/cron.js +93 -0
- package/dist/jobs-code/registry.d.ts +31 -0
- package/dist/jobs-code/registry.js +30 -0
- package/dist/jobs-code/runner.d.ts +1 -0
- package/dist/jobs-code/runner.js +74 -0
- package/dist/jobs-deploy.d.ts +22 -0
- package/dist/jobs-deploy.js +247 -0
- package/dist/jobs-import.d.ts +15 -0
- package/dist/jobs-import.js +231 -0
- package/dist/products/agents.d.ts +161 -1
- package/dist/products/agents.js +40 -0
- package/dist/products/ci.d.ts +19 -1
- package/dist/products/functions.d.ts +2 -0
- package/dist/products/functions.js +5 -0
- package/dist/products/index.d.ts +2 -0
- package/dist/products/index.js +3 -0
- package/dist/products/jobs.d.ts +77 -0
- package/dist/products/jobs.js +5 -0
- package/dist/transport.d.ts +5 -1
- package/dist/transport.js +7 -1
- package/dist/trigger/index.d.ts +202 -0
- package/dist/trigger/index.js +244 -0
- package/dist/types.d.ts +15 -3
- package/package.json +19 -3
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
export declare class AbortTaskRunError extends Error {
|
|
2
|
+
}
|
|
3
|
+
export declare class CompatibilityError extends Error {
|
|
4
|
+
readonly code = "not_supported";
|
|
5
|
+
}
|
|
6
|
+
/** Trigger.dev's machine presets as vCPU and MiB: a job's sandbox is at
|
|
7
|
+
* least 1 vCPU and 128 MiB, so the smaller ones round up to it. */
|
|
8
|
+
export declare const MACHINES: Record<string, {
|
|
9
|
+
vcpu: number;
|
|
10
|
+
memoryMiB: number;
|
|
11
|
+
}>;
|
|
12
|
+
/** Month and day names as the numbers Runtime's cron takes. */
|
|
13
|
+
export declare function numericCron(cron: string): string;
|
|
14
|
+
type Machine = keyof typeof MACHINES | {
|
|
15
|
+
preset?: keyof typeof MACHINES;
|
|
16
|
+
};
|
|
17
|
+
type Retry = {
|
|
18
|
+
maxAttempts?: number;
|
|
19
|
+
[key: string]: unknown;
|
|
20
|
+
};
|
|
21
|
+
export type Context = {
|
|
22
|
+
task: {
|
|
23
|
+
id: string;
|
|
24
|
+
filePath: string;
|
|
25
|
+
};
|
|
26
|
+
run: {
|
|
27
|
+
id: string;
|
|
28
|
+
tags: string[];
|
|
29
|
+
isTest: boolean;
|
|
30
|
+
createdAt: Date;
|
|
31
|
+
startedAt: Date;
|
|
32
|
+
};
|
|
33
|
+
attempt: {
|
|
34
|
+
number: number;
|
|
35
|
+
startedAt: Date;
|
|
36
|
+
};
|
|
37
|
+
environment: {
|
|
38
|
+
type: "PRODUCTION";
|
|
39
|
+
slug: "prod";
|
|
40
|
+
};
|
|
41
|
+
machine: {
|
|
42
|
+
name: string;
|
|
43
|
+
cpu: number;
|
|
44
|
+
memory: number;
|
|
45
|
+
};
|
|
46
|
+
project: {
|
|
47
|
+
ref: string;
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
type RunParams = {
|
|
51
|
+
ctx: Context;
|
|
52
|
+
signal: AbortSignal;
|
|
53
|
+
init?: unknown;
|
|
54
|
+
};
|
|
55
|
+
type TaskOptions<P, O> = {
|
|
56
|
+
id: string;
|
|
57
|
+
run: (payload: P, params: RunParams) => Promise<O> | O;
|
|
58
|
+
machine?: Machine;
|
|
59
|
+
maxDuration?: number;
|
|
60
|
+
retry?: Retry;
|
|
61
|
+
queue?: {
|
|
62
|
+
name?: string;
|
|
63
|
+
concurrencyLimit?: number;
|
|
64
|
+
};
|
|
65
|
+
description?: string;
|
|
66
|
+
init?: (payload: P, params: RunParams) => unknown;
|
|
67
|
+
cleanup?: (payload: P, params: RunParams) => unknown;
|
|
68
|
+
onStart?: (payload: P, params: RunParams) => unknown;
|
|
69
|
+
onSuccess?: (payload: P, output: O, params: RunParams) => unknown;
|
|
70
|
+
onFailure?: (payload: P, error: unknown, params: RunParams) => unknown;
|
|
71
|
+
onComplete?: (payload: P, result: unknown, params: RunParams) => unknown;
|
|
72
|
+
[hook: string]: unknown;
|
|
73
|
+
};
|
|
74
|
+
export type ScheduledTaskPayload = {
|
|
75
|
+
timestamp: Date;
|
|
76
|
+
lastTimestamp: Date | undefined;
|
|
77
|
+
timezone: string;
|
|
78
|
+
scheduleId: string;
|
|
79
|
+
externalId: string | undefined;
|
|
80
|
+
upcoming: Date[];
|
|
81
|
+
};
|
|
82
|
+
type Cron = string | {
|
|
83
|
+
pattern: string;
|
|
84
|
+
timezone?: string;
|
|
85
|
+
environments?: string[];
|
|
86
|
+
};
|
|
87
|
+
export type Task<P = unknown, O = unknown> = {
|
|
88
|
+
id: string;
|
|
89
|
+
trigger(payload?: P): Promise<{
|
|
90
|
+
id: string;
|
|
91
|
+
}>;
|
|
92
|
+
triggerAndWait(payload?: P): never;
|
|
93
|
+
batchTrigger(items: unknown[]): never;
|
|
94
|
+
batchTriggerAndWait(items: unknown[]): never;
|
|
95
|
+
/** For tests: the task's run function. */
|
|
96
|
+
run(payload: P, params: RunParams): Promise<O> | O;
|
|
97
|
+
};
|
|
98
|
+
/** A task run on demand: `tasks.trigger(id, payload)` runs it once. */
|
|
99
|
+
export declare function task<P = unknown, O = unknown>(options: TaskOptions<P, O>): Task<P, O>;
|
|
100
|
+
export declare const schemaTask: <P = unknown, O = unknown>(options: TaskOptions<P, O> & {
|
|
101
|
+
schema?: {
|
|
102
|
+
parse?: (v: unknown) => P;
|
|
103
|
+
};
|
|
104
|
+
}) => Task<P, O>;
|
|
105
|
+
export declare const schedules: {
|
|
106
|
+
/** A task on a cron: a Runtime job with that schedule. */
|
|
107
|
+
task<O = unknown>(options: TaskOptions<ScheduledTaskPayload, O> & {
|
|
108
|
+
cron?: Cron;
|
|
109
|
+
}): Task<ScheduledTaskPayload, O>;
|
|
110
|
+
create: () => never;
|
|
111
|
+
update: () => never;
|
|
112
|
+
del: () => never;
|
|
113
|
+
deactivate: () => never;
|
|
114
|
+
activate: () => never;
|
|
115
|
+
list: () => never;
|
|
116
|
+
retrieve: () => never;
|
|
117
|
+
};
|
|
118
|
+
/** The job `runtime jobs deploy` made for task `id`. A backend calling
|
|
119
|
+
* tasks.trigger, or a task triggering another in its run, has usually not
|
|
120
|
+
* loaded trigger.config.ts, so it may not know the project's name: the job
|
|
121
|
+
* is found by what it runs (`run <file> trigger <id>`, the deploy's
|
|
122
|
+
* command), and the project's name only picks between projects that both
|
|
123
|
+
* have a task with this id. */
|
|
124
|
+
export declare function taskJob<J extends {
|
|
125
|
+
name: string;
|
|
126
|
+
state: string;
|
|
127
|
+
definition: {
|
|
128
|
+
command: {
|
|
129
|
+
argv: string[];
|
|
130
|
+
};
|
|
131
|
+
};
|
|
132
|
+
}>(id: string, jobs: J[]): J;
|
|
133
|
+
export declare const tasks: {
|
|
134
|
+
/** Runs the task once now, in a fresh sandbox of its image, with this
|
|
135
|
+
* payload (at most 64 KiB as JSON). Needs RUNTIME_API_KEY where it is
|
|
136
|
+
* called, and the task deployed with `runtime jobs deploy`. */
|
|
137
|
+
trigger(id: string, payload?: unknown): Promise<{
|
|
138
|
+
id: string;
|
|
139
|
+
}>;
|
|
140
|
+
triggerAndWait: () => never;
|
|
141
|
+
batchTrigger: () => never;
|
|
142
|
+
};
|
|
143
|
+
type Attributes = Record<string, unknown>;
|
|
144
|
+
export declare const logger: {
|
|
145
|
+
debug: (message: string, attributes?: Attributes) => void;
|
|
146
|
+
log: (message: string, attributes?: Attributes) => void;
|
|
147
|
+
info: (message: string, attributes?: Attributes) => void;
|
|
148
|
+
warn: (message: string, attributes?: Attributes) => void;
|
|
149
|
+
error: (message: string, attributes?: Attributes) => void;
|
|
150
|
+
trace: <T>(_name: string, fn: (span: unknown) => Promise<T>) => Promise<T>;
|
|
151
|
+
};
|
|
152
|
+
type Duration = {
|
|
153
|
+
seconds?: number;
|
|
154
|
+
minutes?: number;
|
|
155
|
+
hours?: number;
|
|
156
|
+
days?: number;
|
|
157
|
+
};
|
|
158
|
+
/** Waits in the run, which counts toward its maxDuration. */
|
|
159
|
+
export declare const wait: {
|
|
160
|
+
for: (d: Duration) => Promise<void>;
|
|
161
|
+
until: ({ date }: {
|
|
162
|
+
date: Date;
|
|
163
|
+
}) => Promise<void>;
|
|
164
|
+
forToken: () => never;
|
|
165
|
+
createToken: () => never;
|
|
166
|
+
};
|
|
167
|
+
/** trigger.config.ts's defineConfig: its `project` names the jobs. */
|
|
168
|
+
export declare function defineConfig<T extends {
|
|
169
|
+
project?: string;
|
|
170
|
+
dirs?: string[];
|
|
171
|
+
maxDuration?: number;
|
|
172
|
+
machine?: string;
|
|
173
|
+
retries?: {
|
|
174
|
+
default?: {
|
|
175
|
+
maxAttempts?: number;
|
|
176
|
+
};
|
|
177
|
+
};
|
|
178
|
+
}>(config: T): T;
|
|
179
|
+
export declare const configure: () => undefined;
|
|
180
|
+
export declare const auth: {
|
|
181
|
+
configure: () => undefined;
|
|
182
|
+
withAuth: <T>(_a: unknown, fn: () => T) => T;
|
|
183
|
+
};
|
|
184
|
+
export declare const metadata: {
|
|
185
|
+
set: () => /*elided*/ any;
|
|
186
|
+
get: () => undefined;
|
|
187
|
+
current: () => {};
|
|
188
|
+
flush: () => Promise<undefined>;
|
|
189
|
+
};
|
|
190
|
+
export declare const runs: {
|
|
191
|
+
retrieve: () => never;
|
|
192
|
+
list: () => never;
|
|
193
|
+
cancel: () => never;
|
|
194
|
+
};
|
|
195
|
+
export declare const queue: (options: {
|
|
196
|
+
name: string;
|
|
197
|
+
concurrencyLimit?: number;
|
|
198
|
+
}) => {
|
|
199
|
+
name: string;
|
|
200
|
+
concurrencyLimit?: number;
|
|
201
|
+
};
|
|
202
|
+
export {};
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
/* Trigger.dev's task SDK, run on Runtime Jobs: change `@trigger.dev/sdk` (or
|
|
2
|
+
`@trigger.dev/sdk/v3`) to `withruntime/trigger`, then `npx withruntime jobs
|
|
3
|
+
deploy`. Each `schedules.task` becomes a Runtime job on its cron; each
|
|
4
|
+
`task` becomes a job that `tasks.trigger(id, payload)` (or the task's own
|
|
5
|
+
`trigger`) runs once in a fresh sandbox with that payload. A run is a
|
|
6
|
+
sandbox of the task's machine for up to its maxDuration, retried up to its
|
|
7
|
+
retry.maxAttempts.
|
|
8
|
+
|
|
9
|
+
What has no equivalent is refused by name when called, never ignored:
|
|
10
|
+
triggerAndWait and batches (a run cannot wait on another here), queues'
|
|
11
|
+
concurrency keys, imperative schedules (use runtime.jobs.reschedule), and
|
|
12
|
+
waitpoint tokens. */
|
|
13
|
+
import { cronTimes } from "../jobs-code/cron.js";
|
|
14
|
+
import { jobNameOf, registerTask } from "../jobs-code/registry.js";
|
|
15
|
+
export class AbortTaskRunError extends Error {
|
|
16
|
+
}
|
|
17
|
+
export class CompatibilityError extends Error {
|
|
18
|
+
code = "not_supported";
|
|
19
|
+
}
|
|
20
|
+
const refuse = (what, instead) => {
|
|
21
|
+
throw new CompatibilityError(`${what} has no equivalent on Runtime Jobs: ${instead}`);
|
|
22
|
+
};
|
|
23
|
+
/** Trigger.dev's machine presets as vCPU and MiB: a job's sandbox is at
|
|
24
|
+
* least 1 vCPU and 128 MiB, so the smaller ones round up to it. */
|
|
25
|
+
export const MACHINES = {
|
|
26
|
+
micro: { vcpu: 1, memoryMiB: 256 },
|
|
27
|
+
"small-1x": { vcpu: 1, memoryMiB: 512 },
|
|
28
|
+
"small-2x": { vcpu: 1, memoryMiB: 1024 },
|
|
29
|
+
"medium-1x": { vcpu: 1, memoryMiB: 2048 },
|
|
30
|
+
"medium-2x": { vcpu: 2, memoryMiB: 4096 },
|
|
31
|
+
"large-1x": { vcpu: 4, memoryMiB: 8192 },
|
|
32
|
+
"large-2x": { vcpu: 8, memoryMiB: 16_384 },
|
|
33
|
+
};
|
|
34
|
+
const DAYS = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"];
|
|
35
|
+
const MONTHS = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"];
|
|
36
|
+
/** Month and day names as the numbers Runtime's cron takes. */
|
|
37
|
+
export function numericCron(cron) {
|
|
38
|
+
const f = cron.trim().split(/\s+/);
|
|
39
|
+
if (f.length !== 5)
|
|
40
|
+
throw new Error(`"${cron}" is not five cron fields`);
|
|
41
|
+
const names = (field, list, first) => field.toUpperCase().replace(/[A-Z]{3}/g, (n) => {
|
|
42
|
+
const i = list.indexOf(n);
|
|
43
|
+
if (i < 0)
|
|
44
|
+
throw new Error(`"${cron}" uses ${n}, which is not a name cron knows`);
|
|
45
|
+
return String(i + first);
|
|
46
|
+
});
|
|
47
|
+
f[3] = names(f[3], MONTHS, 1);
|
|
48
|
+
f[4] = names(f[4], DAYS, 0);
|
|
49
|
+
return f.join(" ");
|
|
50
|
+
}
|
|
51
|
+
const machineOf = (machine) => {
|
|
52
|
+
const name = typeof machine === "string" ? machine : (machine?.preset ?? "small-1x");
|
|
53
|
+
const found = MACHINES[name];
|
|
54
|
+
if (!found)
|
|
55
|
+
throw new Error(`machine "${name}" is not one of ${Object.keys(MACHINES).join(", ")}`);
|
|
56
|
+
return { name, ...found };
|
|
57
|
+
};
|
|
58
|
+
/** The project's name, set by `defineConfig` or the deploy. */
|
|
59
|
+
const project = () => process.env.RUNTIME_JOBS_PROJECT ??
|
|
60
|
+
globalThis[Symbol.for("withruntime.trigger.project")] ??
|
|
61
|
+
"trigger";
|
|
62
|
+
function declare(options, schedule, payloadOf) {
|
|
63
|
+
const machine = machineOf(options.machine);
|
|
64
|
+
const explicit = options.machine !== undefined;
|
|
65
|
+
const invoke = async ({ payload, attempt, runId }) => {
|
|
66
|
+
const now = new Date();
|
|
67
|
+
const ctx = {
|
|
68
|
+
task: { id: options.id, filePath: process.argv[3] ?? "" },
|
|
69
|
+
run: { id: runId, tags: [], isTest: false, createdAt: now, startedAt: now },
|
|
70
|
+
attempt: { number: attempt, startedAt: now },
|
|
71
|
+
environment: { type: "PRODUCTION", slug: "prod" },
|
|
72
|
+
machine: { name: machine.name, cpu: machine.vcpu, memory: machine.memoryMiB / 1024 },
|
|
73
|
+
project: { ref: project() },
|
|
74
|
+
};
|
|
75
|
+
const controller = new AbortController();
|
|
76
|
+
const params = { ctx, signal: controller.signal };
|
|
77
|
+
const input = payloadOf(payload);
|
|
78
|
+
params.init = await options.init?.(input, params);
|
|
79
|
+
await options.onStart?.(input, params);
|
|
80
|
+
try {
|
|
81
|
+
const output = await options.run(input, params);
|
|
82
|
+
await options.onSuccess?.(input, output, params);
|
|
83
|
+
await options.onComplete?.(input, { ok: true, data: output }, params);
|
|
84
|
+
return output;
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
await options.onFailure?.(input, error, params);
|
|
88
|
+
await options.onComplete?.(input, { ok: false, error }, params);
|
|
89
|
+
throw error;
|
|
90
|
+
}
|
|
91
|
+
finally {
|
|
92
|
+
await options.cleanup?.(input, params);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
registerTask({
|
|
96
|
+
kind: "trigger",
|
|
97
|
+
id: options.id,
|
|
98
|
+
schedule,
|
|
99
|
+
...(explicit ? { compute: { vcpu: machine.vcpu, memoryMiB: machine.memoryMiB } } : {}),
|
|
100
|
+
...(options.maxDuration ? { maxDuration: options.maxDuration } : {}),
|
|
101
|
+
...(options.retry?.maxAttempts ? { retry: { maxAttempts: options.retry.maxAttempts } } : {}),
|
|
102
|
+
invoke,
|
|
103
|
+
});
|
|
104
|
+
return {
|
|
105
|
+
id: options.id,
|
|
106
|
+
trigger: (payload) => tasks.trigger(options.id, payload),
|
|
107
|
+
triggerAndWait: () => refuse("triggerAndWait", "a run cannot wait for another; trigger it and read its run later"),
|
|
108
|
+
batchTrigger: () => refuse("batchTrigger", "call trigger once per payload"),
|
|
109
|
+
batchTriggerAndWait: () => refuse("batchTriggerAndWait", "call trigger once per payload"),
|
|
110
|
+
run: options.run,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
/** A task run on demand: `tasks.trigger(id, payload)` runs it once. */
|
|
114
|
+
export function task(options) {
|
|
115
|
+
return declare(options, null, (given) => given);
|
|
116
|
+
}
|
|
117
|
+
export const schemaTask = (options) => declare(options, null, (given) => options.schema?.parse ? options.schema.parse(given) : given);
|
|
118
|
+
export const schedules = {
|
|
119
|
+
/** A task on a cron: a Runtime job with that schedule. */
|
|
120
|
+
task(options) {
|
|
121
|
+
if (!options.cron)
|
|
122
|
+
refuse(`schedules.task "${options.id}" without a cron (schedules attached later with schedules.create)`, "give it a cron, or change a job's schedule with `runtime jobs reschedule`");
|
|
123
|
+
const cron = typeof options.cron === "string" ? { pattern: options.cron } : options.cron;
|
|
124
|
+
const timezone = cron.timezone ?? "UTC";
|
|
125
|
+
const expression = numericCron(cron.pattern);
|
|
126
|
+
return declare(options, { cron: expression, timezone }, () => {
|
|
127
|
+
const now = Date.now();
|
|
128
|
+
const [timestamp, lastTimestamp] = cronTimes(expression, timezone, now, -1, 2);
|
|
129
|
+
return {
|
|
130
|
+
timestamp: timestamp ?? new Date(now),
|
|
131
|
+
lastTimestamp,
|
|
132
|
+
timezone,
|
|
133
|
+
scheduleId: jobNameOf(project(), options.id),
|
|
134
|
+
externalId: undefined,
|
|
135
|
+
upcoming: cronTimes(expression, timezone, now + 60_000, 1, 10),
|
|
136
|
+
};
|
|
137
|
+
});
|
|
138
|
+
},
|
|
139
|
+
create: () => refuse("schedules.create", "a task's schedule is its cron; change it with runtime.jobs.reschedule"),
|
|
140
|
+
update: () => refuse("schedules.update", "use runtime.jobs.reschedule"),
|
|
141
|
+
del: () => refuse("schedules.del", "use runtime.jobs.cancel"),
|
|
142
|
+
deactivate: () => refuse("schedules.deactivate", "use runtime.jobs.pause"),
|
|
143
|
+
activate: () => refuse("schedules.activate", "use runtime.jobs.resume"),
|
|
144
|
+
list: () => refuse("schedules.list", "use runtime.jobs.list"),
|
|
145
|
+
retrieve: () => refuse("schedules.retrieve", "use runtime.jobs.get"),
|
|
146
|
+
};
|
|
147
|
+
/** The job `runtime jobs deploy` made for task `id`. A backend calling
|
|
148
|
+
* tasks.trigger, or a task triggering another in its run, has usually not
|
|
149
|
+
* loaded trigger.config.ts, so it may not know the project's name: the job
|
|
150
|
+
* is found by what it runs (`run <file> trigger <id>`, the deploy's
|
|
151
|
+
* command), and the project's name only picks between projects that both
|
|
152
|
+
* have a task with this id. */
|
|
153
|
+
export function taskJob(id, jobs) {
|
|
154
|
+
const deployed = jobs.filter((job) => {
|
|
155
|
+
const argv = job.definition.command.argv;
|
|
156
|
+
return ((job.state === "active" || job.state === "paused") &&
|
|
157
|
+
argv.length === 6 &&
|
|
158
|
+
argv[1] === ".runtime/jobs-runner.ts" &&
|
|
159
|
+
argv[2] === "run" &&
|
|
160
|
+
argv[4] === "trigger" &&
|
|
161
|
+
argv[5] === id);
|
|
162
|
+
});
|
|
163
|
+
if (deployed.length === 1)
|
|
164
|
+
return deployed[0];
|
|
165
|
+
const named = deployed.find((job) => job.name === jobNameOf(project(), id));
|
|
166
|
+
if (named)
|
|
167
|
+
return named;
|
|
168
|
+
if (!deployed.length)
|
|
169
|
+
throw new Error(`No job runs task "${id}": deploy it first with \`npx withruntime jobs deploy\`.`);
|
|
170
|
+
throw new Error(`Jobs ${deployed.map((job) => job.name).join(", ")} all run a task "${id}", one per project: set RUNTIME_JOBS_PROJECT to the project's name (trigger.config.ts's \`project\`) where tasks.trigger is called.`);
|
|
171
|
+
}
|
|
172
|
+
export const tasks = {
|
|
173
|
+
/** Runs the task once now, in a fresh sandbox of its image, with this
|
|
174
|
+
* payload (at most 64 KiB as JSON). Needs RUNTIME_API_KEY where it is
|
|
175
|
+
* called, and the task deployed with `runtime jobs deploy`. */
|
|
176
|
+
async trigger(id, payload) {
|
|
177
|
+
const { Runtime } = await import("../client.js");
|
|
178
|
+
const runtime = new Runtime();
|
|
179
|
+
const template = taskJob(id, await Array.fromAsync(await runtime.jobs.list({ limit: 100 })));
|
|
180
|
+
const name = template.name;
|
|
181
|
+
const body = Buffer.from(JSON.stringify(payload ?? null)).toString("base64");
|
|
182
|
+
if (body.length > 65_536)
|
|
183
|
+
throw new Error(`The payload is over 64 KiB as JSON.`);
|
|
184
|
+
const definition = template.definition;
|
|
185
|
+
const argv = [...definition.command.argv.slice(0, 6), body];
|
|
186
|
+
const run = await runtime.jobs.create({
|
|
187
|
+
name: `${name.slice(0, 60)}-${Date.now().toString(36)}`,
|
|
188
|
+
schedule: { at: Date.now() },
|
|
189
|
+
command: argv,
|
|
190
|
+
...(definition.image ? { image: definition.image } : {}),
|
|
191
|
+
compute: definition.compute,
|
|
192
|
+
timeoutSeconds: definition.timeoutSeconds,
|
|
193
|
+
retry: definition.retry,
|
|
194
|
+
...(definition.secrets?.length ? { secrets: definition.secrets } : {}),
|
|
195
|
+
});
|
|
196
|
+
return { id: run.id };
|
|
197
|
+
},
|
|
198
|
+
triggerAndWait: () => refuse("tasks.triggerAndWait", "a run cannot wait for another; trigger it and read its run later"),
|
|
199
|
+
batchTrigger: () => refuse("tasks.batchTrigger", "call tasks.trigger once per payload"),
|
|
200
|
+
};
|
|
201
|
+
const line = (level) => (message, attributes) => console[level === "error" ? "error" : level === "warn" ? "warn" : "log"](attributes ? `${message} ${JSON.stringify(attributes)}` : message);
|
|
202
|
+
export const logger = {
|
|
203
|
+
debug: line("debug"),
|
|
204
|
+
log: line("log"),
|
|
205
|
+
info: line("info"),
|
|
206
|
+
warn: line("warn"),
|
|
207
|
+
error: line("error"),
|
|
208
|
+
trace: async (_name, fn) => fn({}),
|
|
209
|
+
};
|
|
210
|
+
const sleep = (ms) => new Promise((done) => setTimeout(done, Math.max(0, ms)));
|
|
211
|
+
/** Waits in the run, which counts toward its maxDuration. */
|
|
212
|
+
export const wait = {
|
|
213
|
+
for: (d) => sleep(((d.seconds ?? 0) + (d.minutes ?? 0) * 60 + (d.hours ?? 0) * 3600 + (d.days ?? 0) * 86_400) *
|
|
214
|
+
1000),
|
|
215
|
+
until: ({ date }) => sleep(date.getTime() - Date.now()),
|
|
216
|
+
forToken: () => refuse("wait.forToken", "waitpoint tokens are not supported"),
|
|
217
|
+
createToken: () => refuse("wait.createToken", "waitpoint tokens are not supported"),
|
|
218
|
+
};
|
|
219
|
+
/** trigger.config.ts's defineConfig: its `project` names the jobs. */
|
|
220
|
+
export function defineConfig(config) {
|
|
221
|
+
globalThis[Symbol.for("withruntime.trigger.config")] = {
|
|
222
|
+
maxDuration: config.maxDuration,
|
|
223
|
+
machine: config.machine,
|
|
224
|
+
maxAttempts: config.retries?.default?.maxAttempts,
|
|
225
|
+
};
|
|
226
|
+
if (config.project)
|
|
227
|
+
globalThis[Symbol.for("withruntime.trigger.project")] =
|
|
228
|
+
config.project;
|
|
229
|
+
return config;
|
|
230
|
+
}
|
|
231
|
+
export const configure = () => undefined;
|
|
232
|
+
export const auth = { configure: () => undefined, withAuth: (_a, fn) => fn() };
|
|
233
|
+
export const metadata = {
|
|
234
|
+
set: () => metadata,
|
|
235
|
+
get: () => undefined,
|
|
236
|
+
current: () => ({}),
|
|
237
|
+
flush: async () => undefined,
|
|
238
|
+
};
|
|
239
|
+
export const runs = {
|
|
240
|
+
retrieve: () => refuse("runs.retrieve", "read a run with runtime.jobs.run(id)"),
|
|
241
|
+
list: () => refuse("runs.list", "list a job's runs with runtime.jobs.runs(id)"),
|
|
242
|
+
cancel: () => refuse("runs.cancel", "use runtime.jobs.cancel"),
|
|
243
|
+
};
|
|
244
|
+
export const queue = (options) => options;
|
package/dist/types.d.ts
CHANGED
|
@@ -28,6 +28,9 @@ export type SandboxInfo = {
|
|
|
28
28
|
idlePauseUnusedOnly?: boolean;
|
|
29
29
|
/** A request (exec, files, terminal, a visit to a shared port) wakes it when paused. */
|
|
30
30
|
autoWake?: boolean;
|
|
31
|
+
/** Who its commands, processes and terminals run as when one names no
|
|
32
|
+
* user: "sandbox" (uid 1000) or "root". Absent from APIs before 25 October 2026. */
|
|
33
|
+
user?: "sandbox" | "root";
|
|
31
34
|
/** Runs until stopped while credit lasts, never paused for idleness or a
|
|
32
35
|
* time limit, and its disk is kept after a stop. */
|
|
33
36
|
persistent?: boolean;
|
|
@@ -139,6 +142,11 @@ export type CreateSandbox = {
|
|
|
139
142
|
idlePauseSeconds?: number;
|
|
140
143
|
/** A request to a paused sandbox wakes it. Default true. */
|
|
141
144
|
autoWake?: boolean;
|
|
145
|
+
/** "root": its commands, processes and terminals run as root, through the
|
|
146
|
+
* sandbox's passwordless sudo, unless one says `user: "sandbox"`. Default
|
|
147
|
+
* "sandbox"; a sandbox made from a snapshot runs as its source did. Files
|
|
148
|
+
* written through `files` stay the sandbox user's. */
|
|
149
|
+
user?: "sandbox" | "root";
|
|
142
150
|
/** Keep it running until you stop it, while credit lasts, with no idle
|
|
143
151
|
* pause unless idlePauseSeconds asks for one. Its disk is billed as any
|
|
144
152
|
* sandbox's, and kept after a stop, for restart(), as any sandbox's is.
|
|
@@ -224,9 +232,10 @@ export type CommandResult = {
|
|
|
224
232
|
};
|
|
225
233
|
export type ExecOptions = {
|
|
226
234
|
cwd?: string;
|
|
227
|
-
/** Who runs it: "sandbox" (
|
|
228
|
-
*
|
|
229
|
-
*
|
|
235
|
+
/** Who runs it: "sandbox" (uid 1000, HOME=/workspace) or "root", through
|
|
236
|
+
* the sandbox's passwordless sudo with the same environment and PATH and
|
|
237
|
+
* HOME=/root: no need to prefix it with sudo. Default: the sandbox's own
|
|
238
|
+
* `user`, the sandbox user unless it was made with "root". */
|
|
230
239
|
user?: "sandbox" | "root";
|
|
231
240
|
/** Merged over the sandbox's environment. Put secrets here, never in the command. */
|
|
232
241
|
env?: Record<string, string>;
|
|
@@ -487,6 +496,9 @@ export type SandboxSettings = {
|
|
|
487
496
|
env?: Record<string, string | null>;
|
|
488
497
|
/** A request to a paused sandbox wakes it. */
|
|
489
498
|
autoWake?: boolean;
|
|
499
|
+
/** Who later commands, processes and terminals run as when one names no
|
|
500
|
+
* user; running ones keep theirs. */
|
|
501
|
+
user?: "sandbox" | "root";
|
|
490
502
|
/** Pause after this many seconds with no activity, counted from now; 0
|
|
491
503
|
* never, otherwise 60 to 86400 (a shorter time is raised to 60). */
|
|
492
504
|
idlePauseSeconds?: number;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "withruntime",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.37.0",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"mcpName": "com.withruntime/runtime",
|
|
6
6
|
"type": "module",
|
|
@@ -91,6 +91,11 @@
|
|
|
91
91
|
"import": "./dist/apps/index.js",
|
|
92
92
|
"default": "./dist/apps/index.js"
|
|
93
93
|
},
|
|
94
|
+
"./functions": {
|
|
95
|
+
"types": "./dist/functions/index.d.ts",
|
|
96
|
+
"import": "./dist/functions/index.js",
|
|
97
|
+
"default": "./dist/functions/index.js"
|
|
98
|
+
},
|
|
94
99
|
"./e2b": {
|
|
95
100
|
"types": "./dist/e2b/index.d.ts",
|
|
96
101
|
"import": "./dist/e2b/index.js",
|
|
@@ -176,6 +181,16 @@
|
|
|
176
181
|
"import": "./dist/modal/index.js",
|
|
177
182
|
"default": "./dist/modal/index.js"
|
|
178
183
|
},
|
|
184
|
+
"./trigger": {
|
|
185
|
+
"types": "./dist/trigger/index.d.ts",
|
|
186
|
+
"import": "./dist/trigger/index.js",
|
|
187
|
+
"default": "./dist/trigger/index.js"
|
|
188
|
+
},
|
|
189
|
+
"./inngest": {
|
|
190
|
+
"types": "./dist/inngest/index.d.ts",
|
|
191
|
+
"import": "./dist/inngest/index.js",
|
|
192
|
+
"default": "./dist/inngest/index.js"
|
|
193
|
+
},
|
|
179
194
|
"./cloudflare": {
|
|
180
195
|
"workerd": {
|
|
181
196
|
"types": "./dist/cloudflare/worker.d.ts",
|
|
@@ -200,7 +215,8 @@
|
|
|
200
215
|
"BLAXEL.md",
|
|
201
216
|
"LICENSE",
|
|
202
217
|
"COMPATIBILITY.md",
|
|
203
|
-
"compatibility-lock.json"
|
|
218
|
+
"compatibility-lock.json",
|
|
219
|
+
"agent-platforms-inventory.json"
|
|
204
220
|
],
|
|
205
221
|
"engines": {
|
|
206
222
|
"node": ">=22.12"
|
|
@@ -208,7 +224,7 @@
|
|
|
208
224
|
"scripts": {
|
|
209
225
|
"build": "node ../typescript-config/tsc5.mjs -p tsconfig.build.json",
|
|
210
226
|
"check-types": "tsgo --noEmit && tsgo -p tests/compat/consumers",
|
|
211
|
-
"lint": "oxlint -c ../eslint-config/oxlint.json --ignore-pattern 'dist/**' --ignore-pattern 'tests/fixtures/modal/**' --ignore-pattern 'tests/compat/consumers/**'",
|
|
227
|
+
"lint": "oxlint -c ../eslint-config/oxlint.json --ignore-pattern 'dist/**' --ignore-pattern 'tests/fixtures/modal/**' --ignore-pattern 'tests/fixtures/jobs-dropins/**' --ignore-pattern 'tests/compat/consumers/**'",
|
|
212
228
|
"test": "bun test"
|
|
213
229
|
},
|
|
214
230
|
"devDependencies": {
|