Overview
Concrete factory function that creates a bridge object providing sandboxed skill scripts with controlled access to file system, console, network, and tool registration APIs.
Description
The createSkillAPIBridge function returns a `SkillAPIBridge` object that serves as the host API surface for skill scripts running in the QuickJS VM. It includes a `registerTool` function (for skill-defined tools), an `fs` object bridged to ZenFS with both async and sync methods (`readFile`, `writeFile`, `readdir`, `exists`, `mkdir`, `rm`, `stat`), a `console` object forwarding to host console, a `fetch` function that validates URLs through the SSRF URL guard (`assertSkillFetchUrlAllowed`), and a `downloadFile` function for browser downloads. This is the security boundary: no host API is available unless explicitly provided through this bridge.
Usage
- Create bridge: `const bridge = createSkillAPIBridge({ skillId, onToolRegister })`
- Used internally by `SkillExecutor.executeInVM` and injected into QuickJS via `_injectGlobalAPI`.
Code Reference
Source Location
- Repository: AIPex
- File: packages/browser-runtime/src/lib/vm/skill-api.ts
- Lines: L76-354 (createSkillAPIBridge + SkillAPIBridge interface)
- Support: packages/browser-runtime/src/lib/vm/url-guard.ts (assertSkillFetchUrlAllowed)
Signature
export function createSkillAPIBridge(options: {
skillId: string;
onToolRegister?: (tool: ToolDefinition) => Promise<void>;
}): SkillAPIBridge
export interface SkillAPIBridge {
registerTool(definition: ToolDefinition): Promise<void>;
fs: {
readFile(path: string, encoding?: string): Promise<string | Uint8Array>;
writeFile(path: string, data: string | Uint8Array): Promise<void>;
readdir(path: string): Promise<string[]>;
exists(path: string): Promise<boolean>;
mkdir(path: string, options?: { recursive?: boolean }): Promise<void>;
rm(path: string, options?: { recursive?: boolean }): Promise<void>;
stat(path: string): Promise<FileStats>;
// sync variants: existsSync, readFileSync, writeFileSync, readdirSync, mkdirSync, rmSync, statSync
};
console: { log(...args: any[]): void; error(...args: any[]): void; warn(...args: any[]): void };
fetch(url: string, options?: RequestInit): Promise<any>;
downloadFile(data: string, options?: { filename?: string; encoding?: "base64" | "utf8"; saveAs?: boolean }): Promise<{ success: boolean; downloadId?: number; error?: string }>;
}
interface ToolDefinition {
name: string;
description: string;
parameters?: any;
handler?: string;
}
Import
import { createSkillAPIBridge } from "@aipexstudio/browser-runtime";
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| options.skillId |
string |
Yes |
Skill identifier for namespacing
|
| options.onToolRegister |
(tool) => Promise<void> |
No |
Callback when skill registers a tool
|
Outputs
| Name |
Type |
Description
|
| returns |
SkillAPIBridge |
Bridge object with fs, console, fetch, registerTool, downloadFile
|
Usage Examples
Create Bridge for Skill Execution
import { createSkillAPIBridge } from "@aipexstudio/browser-runtime";
const bridge = createSkillAPIBridge({
skillId: "my-skill",
onToolRegister: async (toolDef) => {
console.log("Skill registered tool:", toolDef.name);
// Register with agent tool registry
},
});
// Bridge is then injected into QuickJS VM:
// quickjs._injectGlobalAPI(vm, bridge, scope);
// Inside VM: fs.readFile(), fetch(), registerTool() all available
SSRF Protection on fetch
// The bridge's fetch validates URLs before making requests:
// bridge.fetch("https://api.example.com/data") // ✅ allowed
// bridge.fetch("http://localhost:8080/admin") // ❌ blocked by URL guard
// bridge.fetch("http://169.254.169.254/latest") // ❌ blocked (SSRF target)
Related Pages
Implements Principle
Requires Environment