信頼できない JavaScript / TypeScript を、アプリ本体や Node.js・ネットワークへ直接触れさせずに実行する。生成プログラムはワーカースレッド上の新規 QuickJS コンテキストで評価され、外との接点はアプリが渡す hostFunctions のみ。

ts
import { run } from 'run';

const result = await run({
  source: `
    const orders = await store.listOrders("customer_123");
    const total = orders.reduce((sum, order) => sum + order.amount, 0);
    return { count: orders.length, total };
  `,
  hostFunctions: {
    store: {
      listOrders: async (customerId: string) => {
        return database.orders.findMany({ customerId });
      },
    },
  },
});
  • 機密操作では hostFunctions が interrupt。結果の署名トークンで再開し、完了済み呼び出しは再実行しない
  • createRunner()timeoutMsmemoryLimitBytes などを共有設定。呼び出しごとに新規 QuickJS コンテキスト
  • 対象ランタイム: Node.js 22.13+ と Bun。導入は pnpm add run
  • AI SDK の code mode の実行層。OS・パッケージ導入・プロセス隔離が必要なら Vercel Sandbox 側

#参考文献