SPB Git forge

spb/doc-api

Public
2commits 1branches 0releases
15.7 MBsize
maindefault branch
13 days agolast push
Python 88.3% TypeScript 7.6% Shell 4.1%
1.7 KB · 24 lines typescript
Raw Blame History
1// STATUS: LIVE_VERIFIED — executed 2026-09-18 (exit 0 in 4.1s)2// Conversations API with the Node SDK: create -> items.create -> response with conversation -> items.list -> delete.3import OpenAI from "openai";4import fs from "node:fs";56const client = new OpenAI();7const log = (method: string, path: string, cost: number, note: string) =>8  fs.appendFileSync("reports/live-requests.jsonl", JSON.stringify({ ts: new Date().toISOString().replace(/\.\d+Z$/, "Z"), provider: "openai", method, path, status: 200, est_cost_usd: cost, note }) + "\n");910const conv = await client.conversations.create({ metadata: { atlas: "example" } });11log("POST", "/v1/conversations", 0, "example conversations/lifecycle.ts");12await client.conversations.items.create(conv.id, { items: [{ type: "message", role: "user", content: [{ type: "input_text", text: "Reply with OK." }] }] });13log("POST", "/v1/conversations/{id}/items", 0, "example lifecycle.ts");14const response = await client.responses.create({ model: "gpt-5.4-nano", conversation: conv.id, input: "Reply with OK once more.", max_output_tokens: 32 });15log("POST", "/v1/responses", 0.00002, "example lifecycle.ts");16console.log(response.conversation?.id === conv.id, response.output_text);17const items = await client.conversations.items.list(conv.id, { order: "asc" });18log("GET", "/v1/conversations/{id}/items", 0, "example lifecycle.ts");19console.log(items.data.map((i) => [i.type, (i as any).role]));20const deleted = await client.conversations.delete(conv.id);21log("DELETE", "/v1/conversations/{id}", 0, "example lifecycle.ts");22console.log(deleted.object, deleted.deleted);23if (!deleted.deleted || items.data.length !== 3) throw new Error("unexpected");24