Skip to content

Commit ed4affd

Browse files
added gateway (#27)
* added types for ai agent execution * added session life cycle management * added gateway.ts --------- Co-authored-by: vivek1504 <vivekjadhav2743@gmail.com>
1 parent f00a621 commit ed4affd

3 files changed

Lines changed: 70 additions & 2 deletions

File tree

src/exec/gateway.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { runtimeStore } from "../runtime/store.js";
2+
import { readVsockResponse } from "../runtime/protocol.js";
3+
import { getVmSocket } from "../runtime/transport.js";
4+
import { getSession, createSession, touchSession } from "./session.js";
5+
import { gatewayLogger } from "../utils/logger.js";
6+
import crypto from "crypto";
7+
import type { Vm } from "../types/types.js";
8+
9+
import { createVm } from "../runtime/vm-manager.js";
10+
import { Deque } from "../runtime/deque.js";
11+
12+
export async function ensureSession(sessionId: string): Promise<Vm> {
13+
let session = getSession(sessionId);
14+
if (session?.state === "active") {
15+
const fn = runtimeStore.functions.get(sessionId);
16+
if (fn && fn.vms.length > 0) return fn.vms[0]!;
17+
}
18+
19+
session = session || createSession(sessionId);
20+
21+
let fn = runtimeStore.functions.get(sessionId);
22+
if (!fn) {
23+
fn = {
24+
functionId: sessionId,
25+
queue: new Deque(),
26+
vms: [],
27+
readyVms: new Set(),
28+
weight: 1,
29+
inflightCount: 0,
30+
deficit: 0,
31+
pendingCreations: 0,
32+
};
33+
runtimeStore.functions.set(sessionId, fn);
34+
}
35+
36+
if (fn.vms.length === 0) {
37+
await createVm(sessionId, fn, "__exec__");
38+
}
39+
40+
session.state = "active";
41+
return fn.vms[0]!;
42+
}
43+
44+
export async function sendSessionMessage(
45+
sessionId: string,
46+
message: Record<string, any>,
47+
onStream?: (chunk: any) => void,
48+
timeout: number = 60000,
49+
): Promise<any> {
50+
const vm = await ensureSession(sessionId);
51+
touchSession(sessionId);
52+
53+
const id = message.id || crypto.randomUUID();
54+
const fullMessage = { ...message, id };
55+
56+
const socket = await getVmSocket(vm);
57+
socket.write(JSON.stringify(fullMessage) + "\n");
58+
59+
gatewayLogger.debug(
60+
{ sessionId, messageType: message.type, messageId: id },
61+
"message sent to VM"
62+
);
63+
64+
const result = await readVsockResponse(socket, timeout, onStream);
65+
return { ...result, messageId: id };
66+
}

src/runtime/vm-manager.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type { RuntimeFunction, Vm } from "../types/types.js";
1111
export async function createVm(
1212
functionId: string,
1313
fn: RuntimeFunction,
14+
snapshotId?: string,
1415
): Promise<Vm> {
1516
const instanceId = crypto.randomBytes(4).toString("hex");
1617
const apiSock = `/tmp/firecracker-${functionId}-${instanceId}.socket`;
@@ -38,7 +39,7 @@ export async function createVm(
3839
await waitForFirecrackerApiSocket(apiSock);
3940

4041
const client = createFcClient(apiSock);
41-
await restoreVm(client, functionId, vsock);
42+
await restoreVm(client, snapshotId || functionId, vsock);
4243

4344
const vm: Vm = {
4445
id: instanceId,

src/utils/logger.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ export const vmManagerLogger = logger.child({ module: "vm-manager" });
3333
export const transportLogger = logger.child({ module: "transport" });
3434
export const protocolLogger = logger.child({ module: "protocol" });
3535
export const cleanupLogger = logger.child({ module: "cleanup" });
36-
export const sessionLogger = logger.child({module: "session"})
36+
export const sessionLogger = logger.child({ module: "session" })
37+
export const gatewayLogger = logger.child({module: "gateway"})
3738

3839
export const httpLoggerOptions: Options = {
3940
logger: logger.child({ module: "http" }),

0 commit comments

Comments
 (0)