Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135import { z } from "zod";import { runSkillScript } from "../../worker/skill-script-runner";
export default { async fetch(request: Request, env: { LOADER: WorkerLoader }) { const value = await request.json<{ source: string; path?: string; input?: unknown; cancelAfter?: number; preabort?: boolean; tools?: boolean; workspace?: "none" | "read" | "read-write"; waitAfter?: number; }>(); const controller = new AbortController(); if (value.preabort) controller.abort(); let active = true; let calls = 0; let attempts = 0; let hostCallbacks = 0; const loaded: unknown[] = []; const files = new Map([["approved.txt", "Workspace contents"]]); const check = (path?: string) => { attempts++; if (!active || controller.signal.aborted) throw new Error("Fixture authority revoked"); if (path && path !== "approved.txt") throw new Error("Path not granted"); calls++; }; const observe = (code: WorkerLoaderWorkerCode) => { loaded.push({ globalOutbound: code.globalOutbound === null ? "blocked" : "other", env: code.env ?? null, limits: code.limits, modules: Object.keys(code.modules), }); return code; }; const loader: WorkerLoader = { load: (code) => env.LOADER.load(observe(code)), get: (name, code) => env.LOADER.get(name, async () => observe(await code())), }; const schema = z.strictObject({ value: z.string().max(100) }); const timer = value.cancelAfter === undefined ? undefined : setTimeout(() => controller.abort(), value.cancelAfter); const started = Date.now(); const result = await runSkillScript({ loader, skill: { name: "script-guide", description: "A native script fixture", body: "Use the script", version: "1.0.0", }, source: value.source, path: value.path ?? "scripts/run.js", input: value.input, signal: controller.signal, resources: [ { path: "references/guide.txt", kind: "reference", content: "Bundled reference", }, { path: "scripts/other.js", kind: "script", content: "throw new Error('Never loaded')", }, { path: "scripts/run.js", kind: "script", content: value.source }, { path: "permissions.json", kind: "file", content: "{}" }, ], workspaceAccess: value.workspace ?? "none", workspace: { readFile: async (path) => { hostCallbacks++; check(path); return files.get(path) ?? null; }, writeFile: async (path, content) => { hostCallbacks++; check(path); files.set(path, content); }, readDir: async () => { hostCallbacks++; throw new Error("Directory discovery not granted"); }, glob: async () => { hostCallbacks++; throw new Error("Glob discovery not granted"); }, stat: async (path) => { hostCallbacks++; check(path); return files.has(path) ? { type: "file", size: files.get(path)!.length } : null; }, }, tools: value.tools ? { echo: { inputSchema: schema, execute: async (input) => { hostCallbacks++; const parsed = schema.parse(input); check(); return parsed.value; }, }, } : undefined, }); active = false; if (timer !== undefined) clearTimeout(timer); const elapsed = Date.now() - started; if (value.waitAfter) await new Promise((resolve) => setTimeout(resolve, value.waitAfter)); return Response.json({ result, elapsed, calls, attempts, hostCallbacks, files: Object.fromEntries(files), loaded, }); },};