Skip to content

Commit 441334b

Browse files
committed
feat(core): add shared compute gateway client and template creation types
1 parent 7b37b0c commit 441334b

File tree

4 files changed

+90
-1
lines changed

4 files changed

+90
-1
lines changed

packages/core/package.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@
5252
"./v3/runEngineWorker": "./src/v3/runEngineWorker/index.ts",
5353
"./v3/machines": "./src/v3/machines/index.ts",
5454
"./v3/serverOnly": "./src/v3/serverOnly/index.ts",
55-
"./v3/isomorphic": "./src/v3/isomorphic/index.ts"
55+
"./v3/isomorphic": "./src/v3/isomorphic/index.ts",
56+
"./v3/compute": "./src/v3/compute/index.ts"
5657
},
5758
"sourceDialects": [
5859
"@triggerdotdev/source"
@@ -577,6 +578,17 @@
577578
"types": "./dist/commonjs/v3/isomorphic/index.d.ts",
578579
"default": "./dist/commonjs/v3/isomorphic/index.js"
579580
}
581+
},
582+
"./v3/compute": {
583+
"import": {
584+
"@triggerdotdev/source": "./src/v3/compute/index.ts",
585+
"types": "./dist/esm/v3/compute/index.d.ts",
586+
"default": "./dist/esm/v3/compute/index.js"
587+
},
588+
"require": {
589+
"types": "./dist/commonjs/v3/compute/index.d.ts",
590+
"default": "./dist/commonjs/v3/compute/index.js"
591+
}
580592
}
581593
},
582594
"type": "module",
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import type { TemplateCreateRequest } from "./types.js";
2+
3+
export type ComputeGatewayClientOptions = {
4+
gatewayUrl: string;
5+
authToken?: string;
6+
timeoutMs: number;
7+
};
8+
9+
export class ComputeGatewayClient {
10+
constructor(private opts: ComputeGatewayClientOptions) {}
11+
12+
async createTemplate(
13+
req: TemplateCreateRequest,
14+
options?: { signal?: AbortSignal }
15+
): Promise<{ accepted: boolean; templateId?: string }> {
16+
const url = `${this.opts.gatewayUrl}/api/templates`;
17+
18+
const headers: Record<string, string> = {
19+
"Content-Type": "application/json",
20+
};
21+
if (this.opts.authToken) {
22+
headers["Authorization"] = `Bearer ${this.opts.authToken}`;
23+
}
24+
25+
const signal = options?.signal ?? AbortSignal.timeout(this.opts.timeoutMs);
26+
27+
const response = await fetch(url, {
28+
method: "POST",
29+
headers,
30+
body: JSON.stringify(req),
31+
signal,
32+
});
33+
34+
if (!response.ok) {
35+
const errorBody = await response.text().catch(() => "unknown error");
36+
throw new Error(`Gateway template creation failed (${response.status}): ${errorBody}`);
37+
}
38+
39+
if (response.status === 202) {
40+
return { accepted: true };
41+
}
42+
43+
const result = await response.json();
44+
return { accepted: false, templateId: result.template_id };
45+
}
46+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export { ComputeGatewayClient } from "./gatewayClient.js";
2+
export type { ComputeGatewayClientOptions } from "./gatewayClient.js";
3+
export {
4+
TemplateCreateRequestSchema,
5+
TemplateCallbackPayloadSchema,
6+
} from "./types.js";
7+
export type { TemplateCreateRequest, TemplateCallbackPayload } from "./types.js";
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { z } from "zod";
2+
3+
export const TemplateCreateRequestSchema = z.object({
4+
image: z.string(),
5+
cpu: z.number(),
6+
memory_mb: z.number(),
7+
callback: z
8+
.object({
9+
url: z.string(),
10+
metadata: z.record(z.string()).optional(),
11+
})
12+
.optional(),
13+
});
14+
export type TemplateCreateRequest = z.infer<typeof TemplateCreateRequestSchema>;
15+
16+
export const TemplateCallbackPayloadSchema = z.object({
17+
template_id: z.string().optional(),
18+
image: z.string(),
19+
status: z.enum(["completed", "failed"]),
20+
error: z.string().optional(),
21+
metadata: z.record(z.string()).optional(),
22+
duration_ms: z.number().optional(),
23+
});
24+
export type TemplateCallbackPayload = z.infer<typeof TemplateCallbackPayloadSchema>;

0 commit comments

Comments
 (0)