Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

add xspace-agent/xspace-agent/providers/openai-chat.js

oversteppingc3fb124c79c0
diff --git a/xspace-agent/xspace-agent/providers/openai-chat.js b/xspace-agent/xspace-agent/providers/openai-chat.js
new file mode 100644
index 000000000..4d8204b49
--- /dev/null
+++ b/xspace-agent/xspace-agent/providers/openai-chat.js
@@ -0,0 +1,82 @@
+// SPDX-License-Identifier: Apache-2.0
+// Copyright 2026 nirholas (https://github.com/nirholas/xspace-agent) [§32]
+
+// DEPRECATED: Use packages/core (xspace-agent) instead.
+// This file is kept for backward compatibility with server.js.
+// Will be removed in v1.0.
+
+const axios = require("axios")
+
+const OPENAI_API_KEY = process.env.OPENAI_API_KEY
+const MODEL = process.env.OPENAI_MODEL || "gpt-4o-mini"
+const MAX_HISTORY = 20
+
+const history = { 0: [], 1: [] }
+
+function addToHistory(agentId, role, content) {
+ if (!history[agentId]) history[agentId] = []
+ history[agentId].push({ role, content })
+ if (history[agentId].length > MAX_HISTORY) {
+ history[agentId] = history[agentId].slice(-MAX_HISTORY)
+ }
+}
+
+async function* streamResponse(agentId, userText, systemPrompt) {
+ addToHistory(agentId, "user", userText)
+
+ const messages = [
+ { role: "system", content: systemPrompt },
+ ...history[agentId]
+ ]
+
+ const response = await axios.post(
+ "https://api.openai.com/v1/chat/completions",
+ {
+ model: MODEL,
+ messages,
+ max_tokens: 300,
+ stream: true
+ },
+ {
+ headers: {
+ Authorization: `Bearer ${OPENAI_API_KEY}`,
+ "Content-Type": "application/json"
+ },
+ responseType: "stream"
+ }
+ )
+
+ let fullResponse = ""
+
+ for await (const chunk of response.data) {
+ const lines = chunk.toString().split("\n").filter(l => l.startsWith("data: "))
+ for (const line of lines) {
+ const data = line.slice(6).trim()
+ if (data === "[DONE]") break
+ try {
+ const parsed = JSON.parse(data)
+ const delta = parsed.choices?.[0]?.delta?.content
+ if (delta) {
+ fullResponse += delta
+ yield delta
+ }
+ } catch (e) {
+ // skip malformed chunks
+ }
+ }
+ }
+
+ addToHistory(agentId, "assistant", fullResponse)
+}
+
+function clearHistory(agentId) {
+ history[agentId] = []
+}
+
+module.exports = {
+ type: "socket",
+ streamResponse,
+ clearHistory
+}
+
+