@one.origin/sandbox เรียก agent จากหลังบ้านโดยคีย์ไม่หลุดไปเบราว์เซอร์ ใช้ได้ทั้ง Cloudflare Workers และ Node 20+
npm i @one.origin/sandbox
import { createSandbox } from '@one.origin/sandbox';
const sandbox = createSandbox({ apiKey: process.env.OPENAI_API_KEY });
const { reply } = await sandbox.chat('อธิบาย ONEORIGIN ให้ฟังหน่อย');
console.log(reply);
await sandbox.chat('สรุปสั้นๆ 5 บรรทัด', {
onDelta: (chunk) => process.stdout.write(chunk)
});
const sandbox = createSandbox({
apiKey: process.env.BASE44_API_KEY,
provider: 'base44',
agentId: process.env.BASE44_AGENT_ID
});
await sandbox.chat('สวัสดี');
console.log(sandbox.conversationId);
import { createSandbox } from '@one.origin/sandbox';
export default {
async fetch(request, env) {
if (request.method !== 'POST') {
return Response.json({ ok: false, error: 'method_not_allowed' }, { status: 405 });
}
const { message } = await request.json();
if (!message) {
return Response.json({ ok: false, error: 'message_required' }, { status: 400 });
}
const sandbox = createSandbox({ apiKey: env.OPENAI_API_KEY });
const { reply } = await sandbox.chat(message);
return Response.json({ ok: true, reply });
}
};
| สมาชิก | หน้าที่ |
|---|
createSandbox(options) | สร้าง sandbox ต้องมี apiKey และต้องมี agentId เมื่อใช้ base44 |
sandbox.chat(message, { onDelta }) | ส่งหนึ่งเทิร์น คืน { reply } |
sandbox.close() | ยกเลิกคำขอที่กำลังทำงาน |
SandboxError | error ที่มี code และ status |
| code | ความหมาย |
|---|
turn_failed | เทิร์นล้มเหลวจากผู้ให้บริการ |
session_failed | สร้าง session ไม่สำเร็จ (ดู status) |
upstream_unreachable | ติดต่อผู้ให้บริการไม่ได้ |
aborted | ถูกยกเลิกด้วย close() |