mirror of
https://github.com/harivansh-afk/sandbox-agent.git
synced 2026-04-16 04:02:01 +00:00
Configure lefthook formatter checks (#231)
* Add lefthook formatter checks * Fix SDK mode hydration * Stabilize SDK mode integration test
This commit is contained in:
parent
0471214d65
commit
d2346bafb3
282 changed files with 5840 additions and 8399 deletions
|
|
@ -2,65 +2,61 @@ import type { Sandbox } from "@cloudflare/sandbox";
|
|||
import { SandboxAgent } from "sandbox-agent";
|
||||
|
||||
export type PromptRequest = {
|
||||
agent?: string;
|
||||
prompt?: string;
|
||||
agent?: string;
|
||||
prompt?: string;
|
||||
};
|
||||
|
||||
export async function runPromptEndpointStream(
|
||||
sandbox: Sandbox,
|
||||
request: PromptRequest,
|
||||
port: number,
|
||||
emit: (event: { type: string; [key: string]: unknown }) => Promise<void> | void,
|
||||
sandbox: Sandbox,
|
||||
request: PromptRequest,
|
||||
port: number,
|
||||
emit: (event: { type: string; [key: string]: unknown }) => Promise<void> | void,
|
||||
): Promise<void> {
|
||||
const client = await SandboxAgent.connect({
|
||||
fetch: (req, init) =>
|
||||
sandbox.containerFetch(
|
||||
req,
|
||||
{
|
||||
...(init ?? {}),
|
||||
// Cloudflare containerFetch may drop long-lived update streams when
|
||||
// a forwarded AbortSignal is cancelled; clear it for this path.
|
||||
signal: undefined,
|
||||
},
|
||||
port,
|
||||
),
|
||||
});
|
||||
const client = await SandboxAgent.connect({
|
||||
fetch: (req, init) =>
|
||||
sandbox.containerFetch(
|
||||
req,
|
||||
{
|
||||
...(init ?? {}),
|
||||
// Cloudflare containerFetch may drop long-lived update streams when
|
||||
// a forwarded AbortSignal is cancelled; clear it for this path.
|
||||
signal: undefined,
|
||||
},
|
||||
port,
|
||||
),
|
||||
});
|
||||
|
||||
let unsubscribe: (() => void) | undefined;
|
||||
try {
|
||||
const session = await client.createSession({
|
||||
agent: request.agent ?? "codex",
|
||||
});
|
||||
let unsubscribe: (() => void) | undefined;
|
||||
try {
|
||||
const session = await client.createSession({
|
||||
agent: request.agent ?? "codex",
|
||||
});
|
||||
|
||||
const promptText =
|
||||
request.prompt?.trim() || "Reply with a short confirmation.";
|
||||
await emit({
|
||||
type: "session.created",
|
||||
sessionId: session.id,
|
||||
agent: session.agent,
|
||||
prompt: promptText,
|
||||
});
|
||||
const promptText = request.prompt?.trim() || "Reply with a short confirmation.";
|
||||
await emit({
|
||||
type: "session.created",
|
||||
sessionId: session.id,
|
||||
agent: session.agent,
|
||||
prompt: promptText,
|
||||
});
|
||||
|
||||
let pendingWrites: Promise<void> = Promise.resolve();
|
||||
unsubscribe = session.onEvent((event) => {
|
||||
pendingWrites = pendingWrites
|
||||
.then(async () => {
|
||||
await emit({ type: "session.event", event });
|
||||
})
|
||||
.catch(() => {});
|
||||
});
|
||||
let pendingWrites: Promise<void> = Promise.resolve();
|
||||
unsubscribe = session.onEvent((event) => {
|
||||
pendingWrites = pendingWrites
|
||||
.then(async () => {
|
||||
await emit({ type: "session.event", event });
|
||||
})
|
||||
.catch(() => {});
|
||||
});
|
||||
|
||||
const response = await session.prompt([{ type: "text", text: promptText }]);
|
||||
await pendingWrites;
|
||||
await emit({ type: "prompt.response", response });
|
||||
await emit({ type: "prompt.completed" });
|
||||
} finally {
|
||||
if (unsubscribe) {
|
||||
unsubscribe();
|
||||
}
|
||||
await Promise.race([
|
||||
client.dispose(),
|
||||
new Promise((resolve) => setTimeout(resolve, 250)),
|
||||
]);
|
||||
}
|
||||
const response = await session.prompt([{ type: "text", text: promptText }]);
|
||||
await pendingWrites;
|
||||
await emit({ type: "prompt.response", response });
|
||||
await emit({ type: "prompt.completed" });
|
||||
} finally {
|
||||
if (unsubscribe) {
|
||||
unsubscribe();
|
||||
}
|
||||
await Promise.race([client.dispose(), new Promise((resolve) => setTimeout(resolve, 250))]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue