SDK และ Agent

@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)
});

ผู้ให้บริการ Base44

const sandbox = createSandbox({
  apiKey: process.env.BASE44_API_KEY,
  provider: 'base44',
  agentId: process.env.BASE44_AGENT_ID
});

await sandbox.chat('สวัสดี');
console.log(sandbox.conversationId);

ใช้ใน Cloudflare Worker

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 });
  }
};

ตารางสมาชิกใน SDK

สมาชิกหน้าที่
createSandbox(options)สร้าง sandbox ต้องมี apiKey และต้องมี agentId เมื่อใช้ base44
sandbox.chat(message, { onDelta })ส่งหนึ่งเทิร์น คืน { reply }
sandbox.close()ยกเลิกคำขอที่กำลังทำงาน
SandboxErrorerror ที่มี code และ status

รหัสข้อผิดพลาด

codeความหมาย
turn_failedเทิร์นล้มเหลวจากผู้ให้บริการ
session_failedสร้าง session ไม่สำเร็จ (ดู status)
upstream_unreachableติดต่อผู้ให้บริการไม่ได้
abortedถูกยกเลิกด้วย close()