WebOrbiton
v1.5.0

AtheraStudio Code

1,217 lines · 49.2 KB
  1. (function () {
  2. "use strict";
  3. ​
  4. var A = window.Athera;
  5. var ai = A.ai;
  6. var t = ai.t;
  7. ​
  8. var URLS = {
  9. anthropic: "https://api.anthropic.com/v1/messages",
  10. openai: "https://api.openai.com/v1/responses",
  11. openaiChat: "https://api.openai.com/v1/chat/completions",
  12. gemini: "https://generativelanguage.googleapis.com/v1beta/models/",
  13. openrouter: "https://openrouter.ai/api/v1/chat/completions",
  14. groq: "https://api.groq.com/openai/v1/chat/completions"
  15. };
  16. ​
  17. var MAX_RETRIES = 2;
  18. var MAX_RECOVERIES = 5;
  19. var IMAGE_TOKENS = 1600;
  20. var active = new Set();
  21. var seq = 0;
  22. ​
  23. function uid(prefix) {
  24. seq += 1;
  25. return prefix + Date.now().toString(36) + seq.toString(36);
  26. }
  27. ​
  28. function clone(v) {
  29. return v == null ? v : JSON.parse(JSON.stringify(v));
  30. }
  31. ​
  32. function num(v) {
  33. var n = Number(v);
  34. return isFinite(n) && n > 0 ? n : 0;
  35. }
  36. ​
  37. function hash(text) {
  38. var h = 2166136261;
  39. for (var i = 0; i < text.length; i++) {
  40. h ^= text.charCodeAt(i);
  41. h = Math.imul(h, 16777619);
  42. }
  43. return (h >>> 0).toString(36);
  44. }
  45. ​
  46. function dataUrl(part) {
  47. return "data:" + part.mime + ";base64," + part.data;
  48. }
  49. ​
  50. function systemText(req) {
  51. return (req.system || []).map(function (b) { return b.text; }).filter(Boolean).join("\n\n");
  52. }
  53. ​
  54. function resultText(content) {
  55. if (typeof content === "string") return content;
  56. return (content || []).filter(function (p) { return p.type === "text"; }).map(function (p) { return p.text; }).join("\n");
  57. }
  58. ​
  59. function resultImages(content) {
  60. return typeof content === "string" ? [] : (content || []).filter(function (p) { return p.type === "image"; });
  61. }
  62. ​
  63. function emptyUsage() {
  64. return { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, searches: 0 };
  65. }
  66. ​
  67. function mapEffort(effort, levels) {
  68. var order = ["low", "medium", "high", "max"];
  69. var i = Math.max(0, order.indexOf(effort || "medium"));
  70. while (i > 0 && levels.indexOf(order[i]) < 0) i--;
  71. return levels.indexOf(order[i]) >= 0 ? order[i] : levels[0];
  72. }
  73. ​
  74. function wait(ms, signal) {
  75. return new Promise(function (resolve, reject) {
  76. var timer = setTimeout(function () {
  77. if (signal) signal.removeEventListener("abort", onAbort);
  78. resolve();
  79. }, ms);
  80. function onAbort() {
  81. clearTimeout(timer);
  82. reject(new DOMException("Aborted", "AbortError"));
  83. }
  84. if (signal) {
  85. if (signal.aborted) return onAbort();
  86. signal.addEventListener("abort", onAbort, { once: true });
  87. }
  88. });
  89. }
  90. ​
  91. function providerError(provider, key, params, extra) {
  92. var e = new Error(t(key, Object.assign({ provider: ai.providerLabel(provider) }, params || {})));
  93. e.provider = provider;
  94. e.code = key;
  95. return Object.assign(e, extra || {});
  96. }
  97. ​
  98. async function httpError(provider, res) {
  99. var raw = "";
  100. try { raw = await res.text(); } catch (x) { }
  101. var detail = raw.slice(0, 600);
  102. try {
  103. var j = JSON.parse(raw);
  104. var err = Array.isArray(j) ? j[0] && j[0].error : j.error;
  105. detail = (err && (err.message || err.type || (typeof err === "string" ? err : ""))) || j.message || detail;
  106. if (err && err.metadata && err.metadata.raw) detail += " " + String(err.metadata.raw).slice(0, 300);
  107. } catch (x) { }
  108. var status = res.status;
  109. var badKey = status === 400 && /api[ _-]?key (?:not valid|invalid)|invalid api[ _-]?key|API_KEY_INVALID/i.test(detail);
  110. var key = status === 401 || status === 403 || badKey ? "ai.err.auth"
  111. : status === 429 ? "ai.err.rate"
  112. : status === 529 || status === 503 ? "ai.err.overloaded"
  113. : status === 402 ? "ai.err.credit"
  114. : "ai.err.http";
  115. var retryAfter = Number(res.headers.get("retry-after"));
  116. return providerError(provider, key, { status: status, message: detail }, {
  117. status: status,
  118. detail: detail,
  119. retryable: status === 408 || status === 409 || status === 429 || status >= 500,
  120. retryAfter: isFinite(retryAfter) && retryAfter > 0 ? retryAfter * 1000 : 0
  121. });
  122. }
  123. ​
  124. function streamError(provider, message) {
  125. return providerError(provider, "ai.err.stream", { message: String(message || "").slice(0, 600) }, { detail: message });
  126. }
  127. ​
  128. function send(built, signal) {
  129. return fetch(built.url, { method: "POST", headers: built.headers, body: JSON.stringify(built.body), signal: signal, cache: "no-store", credentials: "omit", referrerPolicy: "no-referrer" });
  130. }
  131. ​
  132. async function readSSE(res, onEvent) {
  133. var reader = res.body.getReader();
  134. var decoder = new TextDecoder();
  135. var buf = "";
  136. function dispatch(chunk) {
  137. var name = "message";
  138. var data = [];
  139. chunk.split("\n").forEach(function (line) {
  140. if (!line || line.charAt(0) === ":") return;
  141. var i = line.indexOf(":");
  142. var field = i < 0 ? line : line.slice(0, i);
  143. var value = i < 0 ? "" : line.slice(i + 1).replace(/^ /, "");
  144. if (field === "event") name = value;
  145. else if (field === "data") data.push(value);
  146. });
  147. if (!data.length) return;
  148. var text = data.join("\n");
  149. if (text === "[DONE]") return onEvent("done", null);
  150. var obj;
  151. try { obj = JSON.parse(text); } catch (e) { return; }
  152. onEvent(name, obj);
  153. }
  154. for (;;) {
  155. var r = await reader.read();
  156. if (r.done) break;
  157. buf += decoder.decode(r.value, { stream: true });
  158. var tail = buf.slice(-1) === "\r" ? "\r" : "";
  159. if (tail) buf = buf.slice(0, -1);
  160. buf = buf.replace(/\r\n?/g, "\n");
  161. var idx;
  162. while ((idx = buf.indexOf("\n\n")) >= 0) {
  163. var chunk = buf.slice(0, idx);
  164. buf = buf.slice(idx + 2);
  165. dispatch(chunk);
  166. }
  167. buf += tail;
  168. }
  169. buf += decoder.decode();
  170. if (buf.trim()) dispatch(buf.replace(/\r\n?/g, "\n").trim());
  171. }
  172. ​
  173. function emit(state, name, payload) {
  174. var h = state.handlers || {};
  175. var fn = h[name];
  176. if (!fn) return;
  177. try { fn(payload, state); } catch (e) { console.error(e); }
  178. }
  179. ​
  180. function parseArgs(text) {
  181. if (!text) return { value: {}, ok: true };
  182. try {
  183. var v = JSON.parse(text);
  184. return { value: v && typeof v === "object" && !Array.isArray(v) ? v : {}, ok: true };
  185. } catch (e) {
  186. return { value: {}, ok: false, raw: text };
  187. }
  188. }
  189. ​
  190. function webConfig(req) {
  191. var w = req.web || {};
  192. return {
  193. search: !!w.search,
  194. fetch: !!w.fetch,
  195. allow: (w.allow || []).slice(0, 50),
  196. deny: (w.deny || []).slice(0, 50),
  197. maxUses: Math.max(1, Math.min(10, w.maxUses || 5))
  198. };
  199. }
  200. ​
  201. var CLAUDE5 = /^claude-(?:fable|mythos|opus|sonnet|haiku)-5/;
  202. var CLAUDE_FALLBACK = /^claude-(?:fable-5-1|mythos-5-1|opus-5|opus-5-5|sonnet-5-5)$/;
  203. var CLAUDE_DYNAMIC_WEB = /^claude-(?:opus-5|opus-4-[6-9]|sonnet-5|sonnet-4-6)/;
  204. var CLAUDE_EFFORT = /^claude-(?:fable|mythos|opus-5|opus-4-[5-9]|sonnet-5|sonnet-4-6|haiku-5)/;
  205. var CLAUDE_MAX_EFFORT = /^claude-(?:fable|mythos|opus-5|opus-4-[6-9]|sonnet-5|sonnet-4-6|haiku-5)/;
  206. var CLAUDE_ADAPTIVE_OPT_IN = /^claude-(?:opus-4-[6-9]|sonnet-4-6)/;
  207. ​
  208. function anthropicEcho(blocks) {
  209. var list = clone(blocks || []);
  210. var boundary = -1;
  211. list.forEach(function (b, i) { if (b.type === "fallback") boundary = i; });
  212. if (boundary < 0) return list;
  213. var results = {};
  214. list.forEach(function (b) { if (b.tool_use_id) results[b.tool_use_id] = true; });
  215. return list.filter(function (b, i) {
  216. if (i >= boundary) return true;
  217. if (b.type === "thinking" || b.type === "redacted_thinking" || b.type === "tool_use") return false;
  218. if (b.type === "server_tool_use") return !!results[b.id];
  219. return b.type === "text" || !!b.tool_use_id;
  220. });
  221. }
  222. ​
  223. function anthropicMessages(messages) {
  224. var out = [];
  225. function push(role, blocks) {
  226. if (!blocks.length) return;
  227. var last = out[out.length - 1];
  228. if (last && last.role === role) last.content = last.content.concat(blocks);
  229. else out.push({ role: role, content: blocks });
  230. }
  231. function image(p) {
  232. return { type: "image", source: { type: "base64", media_type: p.mime, data: p.data } };
  233. }
  234. messages.forEach(function (m) {
  235. if (m.role === "assistant") {
  236. if (m.native && m.native.provider === "anthropic") return push("assistant", anthropicEcho(m.native.data));
  237. push("assistant", (m.content || []).map(function (p) {
  238. if (p.type === "text" && p.text) return { type: "text", text: p.text };
  239. if (p.type === "tool_call") return { type: "tool_use", id: p.id, name: p.name, input: p.input || {} };
  240. return null;
  241. }).filter(Boolean));
  242. return;
  243. }
  244. var results = [];
  245. var rest = [];
  246. (m.content || []).forEach(function (p) {
  247. if (p.type === "tool_result") {
  248. var content = typeof p.content === "string" ? p.content : (p.content || []).map(function (c) {
  249. return c.type === "image" ? image(c) : { type: "text", text: c.text || "" };
  250. });
  251. results.push({ type: "tool_result", tool_use_id: p.id, content: content, is_error: !!p.isError });
  252. } else if (p.type === "image") {
  253. rest.push(image(p));
  254. } else if (p.type === "text" && p.text) {
  255. rest.push({ type: "text", text: p.text });
  256. }
  257. });
  258. push("user", results.concat(rest));
  259. });
  260. return out;
  261. }
  262. ​
  263. function markLastCacheable(messages) {
  264. var last = messages[messages.length - 1];
  265. if (!last) return;
  266. for (var i = last.content.length - 1; i >= 0; i--) {
  267. var b = last.content[i];
  268. if (b.type === "text" || b.type === "tool_result" || b.type === "image" || b.type === "tool_use") {
  269. last.content[i] = Object.assign({}, b, { cache_control: { type: "ephemeral" } });
  270. return;
  271. }
  272. }
  273. }
  274. ​
  275. var anthropic = {
  276. id: "anthropic",
  277. ​
  278. build: function (req, key, flags) {
  279. var model = req.model;
  280. var messages = anthropicMessages(req.messages || []);
  281. if (!flags.noMsgCache && req.cache !== false) markLastCacheable(messages);
  282. var system = (req.system || []).filter(function (b) { return b.text; }).map(function (b) { return { type: "text", text: b.text }; });
  283. if (req.cache !== false) {
  284. var stableEnd = -1;
  285. (req.system || []).filter(function (b) { return b.text; }).forEach(function (b, i) { if (b.cache) stableEnd = i; });
  286. if (stableEnd < 0 && system.length) stableEnd = system.length - 1;
  287. if (stableEnd >= 0) system[stableEnd].cache_control = { type: "ephemeral" };
  288. }
  289. var body = { model: model, max_tokens: req.maxTokens || 8192, stream: true, messages: messages };
  290. if (system.length) body.system = system;
  291. var tools = (req.tools || []).map(function (tool) {
  292. var def = { name: tool.name, description: tool.description, input_schema: tool.parameters };
  293. if (!flags.noEager) def.eager_input_streaming = true;
  294. return def;
  295. });
  296. var web = webConfig(req);
  297. var basic = flags.basicWeb || !CLAUDE_DYNAMIC_WEB.test(model);
  298. function domains(def) {
  299. if (web.allow.length) def.allowed_domains = web.allow;
  300. else if (web.deny.length) def.blocked_domains = web.deny;
  301. return def;
  302. }
  303. if (web.search && !flags.noWeb) tools.push(domains({ type: basic ? "web_search_20250305" : "web_search_20260209", name: "web_search", max_uses: web.maxUses }));
  304. if (web.fetch && !flags.noWeb && !flags.noFetch) tools.push(domains({ type: basic ? "web_fetch_20250910" : "web_fetch_20260209", name: "web_fetch", max_uses: web.maxUses }));
  305. if (tools.length) body.tools = tools;
  306. if (req.stop && req.stop.length && !flags.noStop) body.stop_sequences = req.stop.slice(0, 8);
  307. var betas = [];
  308. if (!flags.noThinking) {
  309. if (CLAUDE5.test(model)) {
  310. body.thinking = { type: "adaptive" };
  311. if (req.showThinking) body.thinking.display = "summarized";
  312. if (!flags.noBinding) {
  313. body.thinking.block_binding = { prefix_mismatch_behavior: "drop_block" };
  314. betas.push("thinking-binding-controls-2026-08-01");
  315. }
  316. } else if (req.reasoning && CLAUDE_ADAPTIVE_OPT_IN.test(model)) {
  317. body.thinking = { type: "adaptive" };
  318. }
  319. }
  320. if (!flags.noEffort && CLAUDE_EFFORT.test(model)) {
  321. body.output_config = { effort: mapEffort(req.effort, CLAUDE_MAX_EFFORT.test(model) ? ["low", "medium", "high", "max"] : ["low", "medium", "high"]) };
  322. }
  323. if (!flags.noFallbacks && CLAUDE_FALLBACK.test(model)) {
  324. body.fallbacks = "default";
  325. betas.push("server-side-fallback-2026-07-01");
  326. }
  327. var headers = {
  328. "content-type": "application/json",
  329. "x-api-key": key,
  330. "anthropic-version": "2023-06-01",
  331. "anthropic-dangerous-direct-browser-access": "true"
  332. };
  333. if (betas.length) headers["anthropic-beta"] = betas.join(",");
  334. return { url: URLS.anthropic, headers: headers, body: body };
  335. },
  336. ​
  337. recover: function (msg, flags) {
  338. var m = String(msg);
  339. if (!flags.noBinding && /block_binding|prefix_mismatch|thinking-binding/i.test(m)) return "noBinding";
  340. if (!flags.noFallbacks && /fallback/i.test(m)) return "noFallbacks";
  341. if (!flags.noEager && /eager_input_streaming/i.test(m)) return "noEager";
  342. if (!flags.noEffort && /effort|output_config/i.test(m)) return "noEffort";
  343. if (!flags.basicWeb && /web_(?:search|fetch)_2026/i.test(m)) return "basicWeb";
  344. if (!flags.noFetch && /web_fetch/i.test(m)) return "noFetch";
  345. if (!flags.noWeb && /web_search/i.test(m)) return "noWeb";
  346. if (!flags.noThinking && /thinking/i.test(m)) return "noThinking";
  347. if (!flags.noMsgCache && /cache_control/i.test(m)) return "noMsgCache";
  348. if (!flags.noStop && /stop_sequences/i.test(m)) return "noStop";
  349. return null;
  350. },
  351. ​
  352. start: function (req) {
  353. return { model: req.model, blocks: [], json: {}, usage: emptyUsage(), stop: null, refusal: null };
  354. },
  355. ​
  356. event: function (s, name, d) {
  357. if (!d) return;
  358. var type = d.type || name;
  359. if (type === "error") throw streamError("anthropic", d.error && d.error.message || "error");
  360. if (type === "message_start") {
  361. var msg = d.message || {};
  362. if (msg.model) s.model = msg.model;
  363. readAnthropicUsage(s.usage, msg.usage);
  364. } else if (type === "content_block_start") {
  365. var block = clone(d.content_block || {});
  366. s.blocks[d.index] = block;
  367. if (block.type === "fallback") {
  368. emit(s, "onStatus", { type: "fallback", from: block.from && block.from.model, to: block.to && block.to.model });
  369. } else if (block.type === "tool_use") {
  370. emit(s, "onToolStart", { id: block.id, name: block.name });
  371. }
  372. } else if (type === "content_block_delta") {
  373. var b = s.blocks[d.index];
  374. var delta = d.delta || {};
  375. if (!b) return;
  376. if (delta.type === "text_delta") {
  377. b.text = (b.text || "") + delta.text;
  378. emit(s, "onText", delta.text);
  379. } else if (delta.type === "thinking_delta") {
  380. b.thinking = (b.thinking || "") + delta.thinking;
  381. emit(s, "onThinking", delta.thinking);
  382. } else if (delta.type === "signature_delta") {
  383. b.signature = delta.signature;
  384. } else if (delta.type === "input_json_delta") {
  385. s.json[d.index] = (s.json[d.index] || "") + (delta.partial_json || "");
  386. } else if (delta.type === "citations_delta") {
  387. b.citations = (b.citations || []).concat([delta.citation]);
  388. }
  389. } else if (type === "content_block_stop") {
  390. var done = s.blocks[d.index];
  391. if (!done) return;
  392. if (s.json[d.index] != null) {
  393. var parsed = parseArgs(s.json[d.index]);
  394. done.input = parsed.value;
  395. if (!parsed.ok) done._invalid = parsed.raw;
  396. }
  397. if (done.type === "server_tool_use") {
  398. var input = done.input || {};
  399. emit(s, "onStatus", done.name === "web_fetch" ? { type: "fetch", url: input.url } : { type: "search", query: input.query });
  400. }
  401. } else if (type === "message_delta") {
  402. if (d.delta && d.delta.stop_reason) s.stop = d.delta.stop_reason;
  403. if (d.delta && d.delta.stop_details) s.refusal = d.delta.stop_details;
  404. readAnthropicUsage(s.usage, d.usage);
  405. }
  406. },
  407. ​
  408. finish: function (s) {
  409. var blocks = s.blocks.filter(Boolean);
  410. var boundary = -1;
  411. blocks.forEach(function (b, i) { if (b.type === "fallback") boundary = i; });
  412. var visible = blocks.slice(boundary + 1);
  413. var invalid = {};
  414. var calls = visible.filter(function (b) { return b.type === "tool_use"; }).map(function (b) {
  415. if (b._invalid != null) invalid[b.id] = b._invalid;
  416. return { id: b.id, name: b.name, input: b.input || {}, invalid: b._invalid != null };
  417. });
  418. var text = visible.filter(function (b) { return b.type === "text"; }).map(function (b) { return b.text || ""; }).join("");
  419. var native = blocks.map(function (b) {
  420. var c = Object.assign({}, b);
  421. delete c._invalid;
  422. return c;
  423. });
  424. var stop = { end_turn: "end", stop_sequence: "end", tool_use: "tool", max_tokens: "length", model_context_window_exceeded: "length", pause_turn: "pause", refusal: "refusal" }[s.stop] || (calls.length ? "tool" : "end");
  425. return {
  426. model: s.model,
  427. text: text,
  428. toolCalls: calls,
  429. stopReason: stop,
  430. refusal: s.refusal,
  431. usage: s.usage,
  432. native: native
  433. };
  434. },
  435. ​
  436. partial: function (s) {
  437. return s.usage;
  438. }
  439. };
  440. ​
  441. function readAnthropicUsage(target, u) {
  442. if (!u) return;
  443. if (u.input_tokens != null) target.input = num(u.input_tokens);
  444. if (u.cache_read_input_tokens != null) target.cacheRead = num(u.cache_read_input_tokens);
  445. if (u.cache_creation_input_tokens != null) target.cacheWrite = num(u.cache_creation_input_tokens);
  446. if (u.output_tokens != null) target.output = num(u.output_tokens);
  447. if (u.server_tool_use && u.server_tool_use.web_search_requests != null) target.searches = num(u.server_tool_use.web_search_requests);
  448. }
  449. ​
  450. function openaiInput(messages) {
  451. var input = [];
  452. messages.forEach(function (m) {
  453. if (m.role === "assistant") {
  454. var text = (m.content || []).filter(function (p) { return p.type === "text" && p.text; }).map(function (p) { return p.text; }).join("");
  455. if (text) input.push({ role: "assistant", content: [{ type: "output_text", text: text }] });
  456. (m.content || []).forEach(function (p) {
  457. if (p.type === "tool_call") input.push({ type: "function_call", call_id: p.id, name: p.name, arguments: JSON.stringify(p.input || {}) });
  458. });
  459. return;
  460. }
  461. var parts = [];
  462. var images = [];
  463. (m.content || []).forEach(function (p) {
  464. if (p.type === "tool_result") {
  465. input.push({ type: "function_call_output", call_id: p.id, output: resultText(p.content) || (p.isError ? "error" : "ok") });
  466. resultImages(p.content).forEach(function (img) { images.push(img); });
  467. } else if (p.type === "image") {
  468. parts.push({ type: "input_image", image_url: dataUrl(p) });
  469. } else if (p.type === "text" && p.text) {
  470. parts.push({ type: "input_text", text: p.text });
  471. }
  472. });
  473. images.forEach(function (img) { parts.unshift({ type: "input_image", image_url: dataUrl(img) }); });
  474. if (parts.length) input.push({ role: "user", content: parts });
  475. });
  476. return input;
  477. }
  478. ​
  479. var openai = {
  480. id: "openai",
  481. ​
  482. build: function (req, key, flags) {
  483. if (req.lite) return openaiChat.build(req, key, flags);
  484. var instructions = systemText(req);
  485. var body = {
  486. model: req.model,
  487. input: openaiInput(req.messages || []),
  488. stream: true,
  489. store: false,
  490. max_output_tokens: req.maxTokens || 8192
  491. };
  492. if (instructions) body.instructions = instructions;
  493. if (!flags.noCacheKey && instructions && req.cache !== false) body.prompt_cache_key = req.cacheKey || "athera-" + hash(instructions);
  494. if (!flags.noReasoning) body.reasoning = { effort: mapEffort(req.effort, ["low", "medium", "high"]) };
  495. var tools = (req.tools || []).map(function (tool) {
  496. return { type: "function", name: tool.name, description: tool.description, parameters: tool.parameters, strict: false };
  497. });
  498. var web = webConfig(req);
  499. if ((web.search || web.fetch) && !flags.noWeb) {
  500. var ws = { type: "web_search" };
  501. if (web.allow.length && !flags.noWebFilter) ws.filters = { allowed_domains: web.allow };
  502. tools.push(ws);
  503. }
  504. if (tools.length) body.tools = tools;
  505. return {
  506. url: URLS.openai,
  507. headers: { "content-type": "application/json", authorization: "Bearer " + key },
  508. body: body
  509. };
  510. },
  511. ​
  512. recover: function (msg, flags, req) {
  513. if (req.lite) return openaiChat.recover(msg, flags, req);
  514. var m = String(msg);
  515. if (!flags.noReasoning && /reasoning/i.test(m)) return "noReasoning";
  516. if (!flags.noCacheKey && /prompt_cache_key/i.test(m)) return "noCacheKey";
  517. if (!flags.noWebFilter && /filters|allowed_domains/i.test(m)) return "noWebFilter";
  518. if (!flags.noWeb && /web_search/i.test(m)) return "noWeb";
  519. return null;
  520. },
  521. ​
  522. start: function (req, flags) {
  523. if (req.lite) return openaiChat.start(req, flags);
  524. return { lite: false, model: req.model, text: "", calls: {}, order: [], usage: emptyUsage(), stop: null, refusal: null };
  525. },
  526. ​
  527. event: function (s, name, d) {
  528. if (s.lite) return openaiChat.event(s, name, d);
  529. if (!d) return;
  530. var type = d.type || name;
  531. if (type === "error") throw streamError("openai", d.message || (d.error && d.error.message));
  532. if (type === "response.failed") throw streamError("openai", d.response && d.response.error && d.response.error.message);
  533. if (type === "response.created" && d.response && d.response.model) s.model = d.response.model;
  534. else if (type === "response.output_text.delta") {
  535. s.text += d.delta || "";
  536. emit(s, "onText", d.delta || "");
  537. } else if (type === "response.reasoning_summary_text.delta") {
  538. emit(s, "onThinking", d.delta || "");
  539. } else if (type === "response.refusal.delta") {
  540. s.refusal = { explanation: ((s.refusal && s.refusal.explanation) || "") + (d.delta || "") };
  541. } else if (type === "response.output_item.added") {
  542. var item = d.item || {};
  543. if (item.type === "function_call") {
  544. s.calls[item.id] = { id: item.call_id, name: item.name, args: "" };
  545. s.order.push(item.id);
  546. emit(s, "onToolStart", { id: item.call_id, name: item.name });
  547. }
  548. } else if (type === "response.function_call_arguments.delta") {
  549. if (s.calls[d.item_id]) s.calls[d.item_id].args += d.delta || "";
  550. } else if (type === "response.output_item.done") {
  551. var done = d.item || {};
  552. if (done.type === "function_call" && s.calls[done.id]) {
  553. s.calls[done.id].args = done.arguments || s.calls[done.id].args;
  554. } else if (done.type === "web_search_call") {
  555. s.usage.searches += 1;
  556. var action = done.action || {};
  557. emit(s, "onStatus", action.type === "open_page" ? { type: "fetch", url: action.url } : { type: "search", query: action.query || "" });
  558. }
  559. } else if (type === "response.completed" || type === "response.incomplete") {
  560. var r = d.response || {};
  561. if (r.model) s.model = r.model;
  562. var u = r.usage || {};
  563. var cached = num(u.input_tokens_details && u.input_tokens_details.cached_tokens);
  564. s.usage.input = Math.max(0, num(u.input_tokens) - cached);
  565. s.usage.cacheRead = cached;
  566. s.usage.output = num(u.output_tokens);
  567. if (type === "response.incomplete") s.stop = r.incomplete_details && r.incomplete_details.reason === "content_filter" ? "refusal" : "length";
  568. }
  569. },
  570. ​
  571. finish: function (s) {
  572. if (s.lite) return openaiChat.finish(s);
  573. var calls = s.order.map(function (k) {
  574. var c = s.calls[k];
  575. var parsed = parseArgs(c.args);
  576. return { id: c.id, name: c.name, input: parsed.value, invalid: !parsed.ok };
  577. });
  578. return {
  579. model: s.model,
  580. text: s.text,
  581. toolCalls: calls,
  582. stopReason: s.refusal ? "refusal" : s.stop || (calls.length ? "tool" : "end"),
  583. refusal: s.refusal,
  584. usage: s.usage,
  585. native: null
  586. };
  587. },
  588. ​
  589. partial: function (s) {
  590. return s.usage;
  591. }
  592. };
  593. ​
  594. function chatMessages(req, cacheSystem) {
  595. var out = [];
  596. var sys = (req.system || []).filter(function (b) { return b.text; });
  597. if (sys.length) {
  598. if (cacheSystem) {
  599. out.push({ role: "system", content: sys.map(function (b, i) {
  600. var part = { type: "text", text: b.text };
  601. if (i === sys.length - 1 || b.cache) part.cache_control = { type: "ephemeral" };
  602. return part;
  603. }) });
  604. } else {
  605. out.push({ role: "system", content: sys.map(function (b) { return b.text; }).join("\n\n") });
  606. }
  607. }
  608. (req.messages || []).forEach(function (m) {
  609. if (m.role === "assistant") {
  610. if (m.native && m.native.provider === req.provider && m.native.model === req.model && m.native.data) return out.push(clone(m.native.data));
  611. var text = (m.content || []).filter(function (p) { return p.type === "text" && p.text; }).map(function (p) { return p.text; }).join("");
  612. var calls = (m.content || []).filter(function (p) { return p.type === "tool_call"; }).map(function (p) {
  613. return { id: p.id, type: "function", function: { name: p.name, arguments: JSON.stringify(p.input || {}) } };
  614. });
  615. var msg = { role: "assistant", content: text || null };
  616. if (calls.length) msg.tool_calls = calls;
  617. if (text || calls.length) out.push(msg);
  618. return;
  619. }
  620. var parts = [];
  621. var images = [];
  622. (m.content || []).forEach(function (p) {
  623. if (p.type === "tool_result") {
  624. out.push({ role: "tool", tool_call_id: p.id, content: resultText(p.content) || (p.isError ? "error" : "ok") });
  625. resultImages(p.content).forEach(function (img) { images.push(img); });
  626. } else if (p.type === "image") {
  627. parts.push({ type: "image_url", image_url: { url: dataUrl(p) } });
  628. } else if (p.type === "text" && p.text) {
  629. parts.push({ type: "text", text: p.text });
  630. }
  631. });
  632. images.forEach(function (img) { parts.unshift({ type: "image_url", image_url: { url: dataUrl(img) } }); });
  633. if (!parts.length) return;
  634. var onlyText = parts.every(function (p) { return p.type === "text"; });
  635. out.push({ role: "user", content: onlyText ? parts.map(function (p) { return p.text; }).join("\n\n") : parts });
  636. });
  637. return out;
  638. }
  639. ​
  640. function chatAdapter(cfg) {
  641. var adapter = {
  642. id: cfg.id,
  643. ​
  644. build: function (req, key, flags) {
  645. var model = cfg.model ? cfg.model(req, flags) : req.model;
  646. var body = {
  647. model: model,
  648. messages: chatMessages(req, cfg.cacheSystem && cfg.cacheSystem(req, flags)),
  649. stream: true
  650. };
  651. if (!flags.noStreamOptions) body.stream_options = { include_usage: true };
  652. body[flags.altMax ? cfg.altMax : cfg.maxParam] = req.maxTokens || 8192;
  653. var tools = (req.tools || []).map(function (tool) {
  654. return { type: "function", function: { name: tool.name, description: tool.description, parameters: tool.parameters } };
  655. });
  656. if (tools.length) body.tools = tools;
  657. if (req.stop && req.stop.length && !flags.noStop) body.stop = req.stop.slice(0, 4);
  658. if (cfg.extra) cfg.extra(body, req, flags);
  659. var headers = { "content-type": "application/json", authorization: "Bearer " + key };
  660. if (cfg.headers) Object.assign(headers, cfg.headers());
  661. return { url: cfg.url, headers: headers, body: body };
  662. },
  663. ​
  664. recover: function (msg, flags) {
  665. var m = String(msg);
  666. if (!flags.noReasoning && /reasoning/i.test(m)) return "noReasoning";
  667. if (!flags.noStreamOptions && /stream_options|include_usage/i.test(m)) return "noStreamOptions";
  668. if (!flags.altMax && cfg.altMax && new RegExp(cfg.maxParam).test(m)) return "altMax";
  669. if (!flags.noStop && /\bstop\b/i.test(m)) return "noStop";
  670. if (!flags.noCache && /cache_control/i.test(m)) return "noCache";
  671. if (!flags.noWeb && /online|plugin|web/i.test(m)) return "noWeb";
  672. return null;
  673. },
  674. ​
  675. start: function (req) {
  676. return { lite: true, provider: cfg.id, model: req.model, reqModel: req.model, text: "", reasoning: "", reasoningDetails: null, calls: [], usage: emptyUsage(), cost: null, stop: null, refusal: null };
  677. },
  678. ​
  679. event: function (s, name, d) {
  680. if (!d) return;
  681. if (d.error) throw streamError(cfg.id, d.error.message || d.error);
  682. if (d.model) s.model = d.model;
  683. var choice = d.choices && d.choices[0];
  684. if (choice) {
  685. var delta = choice.delta || {};
  686. if (delta.content) {
  687. s.text += delta.content;
  688. emit(s, "onText", delta.content);
  689. }
  690. var thought = delta.reasoning || delta.reasoning_content;
  691. if (thought) {
  692. s.reasoning += thought;
  693. emit(s, "onThinking", thought);
  694. }
  695. if (delta.reasoning_details) s.reasoningDetails = (s.reasoningDetails || []).concat(delta.reasoning_details);
  696. if (delta.refusal) s.refusal = { explanation: ((s.refusal && s.refusal.explanation) || "") + delta.refusal };
  697. (delta.tool_calls || []).forEach(function (tc) {
  698. var i = tc.index != null ? tc.index : s.calls.length;
  699. var c = s.calls[i];
  700. if (!c) {
  701. c = s.calls[i] = { id: tc.id || uid("call_"), name: "", args: "" };
  702. if (tc.function && tc.function.name) emit(s, "onToolStart", { id: c.id, name: tc.function.name });
  703. }
  704. if (tc.id) c.id = tc.id;
  705. if (tc.function && tc.function.name) c.name += tc.function.name;
  706. if (tc.function && tc.function.arguments) c.args += tc.function.arguments;
  707. });
  708. (delta.executed_tools || []).forEach(function (x) {
  709. if (/search/i.test(x.type || x.name || "")) {
  710. s.usage.searches += 1;
  711. emit(s, "onStatus", { type: "search", query: String(x.arguments || "").slice(0, 200) });
  712. }
  713. });
  714. if (choice.finish_reason) s.stop = choice.finish_reason;
  715. }
  716. var u = d.usage || (d.x_groq && d.x_groq.usage);
  717. if (u) {
  718. var details = u.prompt_tokens_details || {};
  719. var cached = num(details.cached_tokens);
  720. var written = num(details.cache_write_tokens);
  721. s.usage.input = Math.max(0, num(u.prompt_tokens) - cached - written);
  722. s.usage.cacheRead = cached;
  723. s.usage.cacheWrite = written;
  724. s.usage.output = num(u.completion_tokens);
  725. if (typeof u.cost === "number" && isFinite(u.cost)) s.cost = u.cost;
  726. }
  727. },
  728. ​
  729. finish: function (s) {
  730. var calls = s.calls.filter(Boolean).map(function (c) {
  731. var parsed = parseArgs(c.args);
  732. return { id: c.id, name: c.name, input: parsed.value, invalid: !parsed.ok };
  733. });
  734. var native = { role: "assistant", content: s.text || null };
  735. if (calls.length) {
  736. native.tool_calls = s.calls.filter(Boolean).map(function (c) {
  737. return { id: c.id, type: "function", function: { name: c.name, arguments: c.args || "{}" } };
  738. });
  739. }
  740. if (s.reasoningDetails) native.reasoning_details = s.reasoningDetails;
  741. var stop = { stop: "end", tool_calls: "tool", function_call: "tool", length: "length", content_filter: "refusal" }[s.stop] || (calls.length ? "tool" : "end");
  742. return {
  743. model: s.model,
  744. text: s.text,
  745. toolCalls: calls,
  746. stopReason: s.refusal ? "refusal" : stop,
  747. refusal: s.refusal,
  748. usage: s.usage,
  749. cost: s.cost,
  750. native: cfg.keepNative ? native : null
  751. };
  752. },
  753. ​
  754. partial: function (s) {
  755. return s.usage;
  756. }
  757. };
  758. return adapter;
  759. }
  760. ​
  761. var openaiChat = chatAdapter({
  762. id: "openai",
  763. url: URLS.openaiChat,
  764. maxParam: "max_completion_tokens",
  765. altMax: "max_tokens",
  766. extra: function (body, req, flags) {
  767. if (!flags.noReasoning && req.reasoning) body.reasoning_effort = mapEffort(req.effort, ["low", "medium", "high"]);
  768. if (!flags.noCacheKey && req.cache !== false && body.messages[0] && body.messages[0].role === "system") {
  769. body.prompt_cache_key = req.cacheKey || "athera-" + hash(String(body.messages[0].content));
  770. }
  771. }
  772. });
  773. ​
  774. var openrouter = chatAdapter({
  775. id: "openrouter",
  776. url: URLS.openrouter,
  777. maxParam: "max_tokens",
  778. keepNative: true,
  779. cacheSystem: function (req, flags) {
  780. return req.cache !== false && !flags.noCache && /^(anthropic|google)\//.test(req.model);
  781. },
  782. model: function (req, flags) {
  783. var w = webConfig(req);
  784. return (w.search || w.fetch) && !flags.noWeb && !/:online$/.test(req.model) ? req.model + ":online" : req.model;
  785. },
  786. headers: function () {
  787. var h = { "X-Title": "AtheraStudio Code" };
  788. if (/^https?:$/.test(location.protocol)) h["HTTP-Referer"] = location.origin;
  789. return h;
  790. },
  791. extra: function (body, req, flags) {
  792. if (!flags.noReasoning) body.reasoning = { effort: mapEffort(req.effort, ["low", "medium", "high"]), exclude: !req.showThinking };
  793. }
  794. });
  795. ​
  796. var groq = chatAdapter({
  797. id: "groq",
  798. url: URLS.groq,
  799. maxParam: "max_completion_tokens",
  800. altMax: "max_tokens",
  801. extra: function (body, req, flags) {
  802. if (!flags.noReasoning && /gpt-oss|qwen3/i.test(req.model)) body.reasoning_effort = mapEffort(req.effort, ["low", "medium", "high"]);
  803. }
  804. });
  805. ​
  806. var SCHEMA_KEYS = ["type", "description", "properties", "required", "items", "enum", "nullable", "format", "minimum", "maximum", "minItems", "maxItems"];
  807. ​
  808. function geminiSchema(schema) {
  809. if (!schema || typeof schema !== "object") return schema;
  810. var out = {};
  811. SCHEMA_KEYS.forEach(function (k) {
  812. if (schema[k] === undefined) return;
  813. if (k === "properties") {
  814. out.properties = {};
  815. Object.keys(schema.properties).forEach(function (p) { out.properties[p] = geminiSchema(schema.properties[p]); });
  816. } else if (k === "items") {
  817. out.items = geminiSchema(schema.items);
  818. } else if (k === "type" && Array.isArray(schema.type)) {
  819. var types = schema.type.filter(function (x) { return x !== "null"; });
  820. out.type = types[0] || "string";
  821. if (schema.type.indexOf("null") >= 0) out.nullable = true;
  822. } else {
  823. out[k] = clone(schema[k]);
  824. }
  825. });
  826. return out;
  827. }
  828. ​
  829. function geminiModel(model) {
  830. return String(model || "").replace(/^models\//, "");
  831. }
  832. ​
  833. function geminiThinking(model, req) {
  834. if (/gemini-3/i.test(model)) return { thinkingLevel: req.reasoning ? "high" : "low", includeThoughts: !!req.showThinking };
  835. if (/gemini-2\.5-flash/i.test(model)) return { thinkingBudget: req.reasoning ? -1 : 0, includeThoughts: !!req.showThinking };
  836. if (/gemini-2\.5-pro/i.test(model)) return { thinkingBudget: req.reasoning ? -1 : 128, includeThoughts: !!req.showThinking };
  837. return null;
  838. }
  839. ​
  840. function geminiContents(req, flags) {
  841. var out = [];
  842. var gids = {};
  843. function push(role, parts) {
  844. if (!parts.length) return;
  845. var last = out[out.length - 1];
  846. if (last && last.role === role) last.parts = last.parts.concat(parts);
  847. else out.push({ role: role, parts: parts });
  848. }
  849. function inline(p) {
  850. return { inlineData: { mimeType: p.mime, data: p.data } };
  851. }
  852. (req.messages || []).forEach(function (m) {
  853. if (m.role === "assistant") {
  854. (m.content || []).forEach(function (p) { if (p.type === "tool_call" && p.gid) gids[p.id] = true; });
  855. if (m.native && m.native.provider === "gemini" && m.native.model === geminiModel(req.model)) return push("model", clone(m.native.data));
  856. push("model", (m.content || []).map(function (p) {
  857. if (p.type === "text" && p.text) return { text: p.text };
  858. if (p.type === "tool_call") {
  859. var part = { functionCall: { name: p.name, args: p.input || {} } };
  860. if (flags.dummySig) part.thoughtSignature = "skip_thought_signature_validator";
  861. return part;
  862. }
  863. return null;
  864. }).filter(Boolean));
  865. return;
  866. }
  867. var parts = [];
  868. var images = [];
  869. (m.content || []).forEach(function (p) {
  870. if (p.type === "tool_result") {
  871. var fr = { name: p.name || "tool", response: p.isError ? { error: resultText(p.content) } : { result: resultText(p.content) } };
  872. if (gids[p.id]) fr.id = p.id;
  873. parts.push({ functionResponse: fr });
  874. resultImages(p.content).forEach(function (img) { images.push(inline(img)); });
  875. } else if (p.type === "image") {
  876. parts.push(inline(p));
  877. } else if (p.type === "text" && p.text) {
  878. parts.push({ text: p.text });
  879. }
  880. });
  881. push("user", parts.concat(images));
  882. });
  883. return out;
  884. }
  885. ​
  886. var gemini = {
  887. id: "gemini",
  888. ​
  889. build: function (req, key, flags) {
  890. var model = geminiModel(req.model);
  891. var body = { contents: geminiContents(req, flags) };
  892. var sys = systemText(req);
  893. if (sys) body.systemInstruction = { parts: [{ text: sys }] };
  894. var tools = [];
  895. var decls = (req.tools || []).map(function (tool) {
  896. return { name: tool.name, description: tool.description, parameters: geminiSchema(tool.parameters) };
  897. });
  898. if (decls.length) tools.push({ functionDeclarations: decls });
  899. var web = webConfig(req);
  900. if (web.search && !flags.noWeb) tools.push({ google_search: {} });
  901. if (web.fetch && !flags.noWeb && !flags.noFetch) tools.push({ url_context: {} });
  902. if (tools.length) body.tools = tools;
  903. var gen = { maxOutputTokens: req.maxTokens || 8192 };
  904. if (req.stop && req.stop.length && !flags.noStop) gen.stopSequences = req.stop.slice(0, 5);
  905. var thinking = flags.noThinking ? null : geminiThinking(model, req);
  906. if (thinking) gen.thinkingConfig = thinking;
  907. body.generationConfig = gen;
  908. return {
  909. url: URLS.gemini + encodeURIComponent(model) + ":streamGenerateContent?alt=sse",
  910. headers: { "content-type": "application/json", "x-goog-api-key": key },
  911. body: body
  912. };
  913. },
  914. ​
  915. recover: function (msg, flags) {
  916. var m = String(msg);
  917. if (!flags.dummySig && /signature/i.test(m)) return "dummySig";
  918. if (!flags.noThinking && /thinking/i.test(m)) return "noThinking";
  919. if (!flags.noFetch && /url_context|urlContext/i.test(m)) return "noFetch";
  920. if (!flags.noWeb && /google_search|googleSearch|search/i.test(m)) return "noWeb";
  921. if (!flags.noStop && /stop/i.test(m)) return "noStop";
  922. return null;
  923. },
  924. ​
  925. start: function (req) {
  926. return { model: geminiModel(req.model), text: "", parts: [], calls: [], usage: emptyUsage(), stop: null, refusal: null, queries: {} };
  927. },
  928. ​
  929. event: function (s, name, d) {
  930. if (!d) return;
  931. if (d.error) throw streamError("gemini", d.error.message || d.error.status);
  932. if (d.modelVersion) s.model = d.modelVersion;
  933. if (d.promptFeedback && d.promptFeedback.blockReason) s.refusal = { category: d.promptFeedback.blockReason };
  934. var cand = d.candidates && d.candidates[0];
  935. if (cand) {
  936. ((cand.content && cand.content.parts) || []).forEach(function (part) {
  937. if (part.functionCall) {
  938. var fc = part.functionCall;
  939. var call = { id: fc.id || uid("gcall_"), name: fc.name, input: fc.args || {}, gid: !!fc.id };
  940. s.calls.push(call);
  941. emit(s, "onToolStart", { id: call.id, name: call.name });
  942. s.parts.push(clone(part));
  943. return;
  944. }
  945. if (part.thought) {
  946. if (part.text) emit(s, "onThinking", part.text);
  947. s.parts.push(clone(part));
  948. return;
  949. }
  950. if (typeof part.text === "string") {
  951. s.text += part.text;
  952. if (part.text) emit(s, "onText", part.text);
  953. var last = s.parts[s.parts.length - 1];
  954. if (last && typeof last.text === "string" && !last.thought && !last.thoughtSignature && !part.thoughtSignature && !last.functionCall) last.text += part.text;
  955. else s.parts.push(clone(part));
  956. return;
  957. }
  958. s.parts.push(clone(part));
  959. });
  960. if (cand.finishReason) s.stop = cand.finishReason;
  961. var gm = cand.groundingMetadata;
  962. if (gm && gm.webSearchQueries) {
  963. gm.webSearchQueries.forEach(function (q) {
  964. if (s.queries[q]) return;
  965. s.queries[q] = true;
  966. s.usage.searches += 1;
  967. emit(s, "onStatus", { type: "search", query: q });
  968. });
  969. }
  970. }
  971. var u = d.usageMetadata;
  972. if (u) {
  973. var cached = num(u.cachedContentTokenCount);
  974. s.usage.input = Math.max(0, num(u.promptTokenCount) + num(u.toolUsePromptTokenCount) - cached);
  975. s.usage.cacheRead = cached;
  976. s.usage.output = num(u.candidatesTokenCount) + num(u.thoughtsTokenCount);
  977. }
  978. },
  979. ​
  980. finish: function (s) {
  981. var stop = s.calls.length ? "tool"
  982. : s.stop === "MAX_TOKENS" ? "length"
  983. : /SAFETY|RECITATION|BLOCKLIST|PROHIBITED|SPII/.test(s.stop || "") || s.refusal ? "refusal"
  984. : "end";
  985. return {
  986. model: s.model,
  987. text: s.text,
  988. toolCalls: s.calls.map(function (c) { return { id: c.id, name: c.name, input: c.input, gid: c.gid }; }),
  989. stopReason: stop,
  990. refusal: s.refusal || (stop === "refusal" ? { category: s.stop } : null),
  991. usage: s.usage,
  992. native: s.parts
  993. };
  994. },
  995. ​
  996. partial: function (s) {
  997. return s.usage;
  998. }
  999. };
  1000. ​
  1001. var ADAPTERS = { anthropic: anthropic, openai: openai, gemini: gemini, openrouter: openrouter, groq: groq };
  1002. ​
  1003. function estimateInput(req) {
  1004. var chars = systemText(req).length;
  1005. var images = 0;
  1006. (req.messages || []).forEach(function (m) {
  1007. (m.content || []).forEach(function (p) {
  1008. if (p.type === "text") chars += (p.text || "").length;
  1009. else if (p.type === "image") images += 1;
  1010. else if (p.type === "tool_call") chars += JSON.stringify(p.input || {}).length + 40;
  1011. else if (p.type === "tool_result") {
  1012. chars += resultText(p.content).length + 40;
  1013. images += resultImages(p.content).length;
  1014. }
  1015. });
  1016. });
  1017. (req.tools || []).forEach(function (tool) { chars += JSON.stringify(tool).length; });
  1018. return ai.budget.tokensFromChars(chars) + images * IMAGE_TOKENS;
  1019. }
  1020. ​
  1021. function estimateRequest(req, row) {
  1022. var output = Math.min(req.maxTokens || 8192, req.expectedOutput || 1500);
  1023. var web = webConfig(req);
  1024. return ai.budget.estimate(row, {
  1025. input: estimateInput(req),
  1026. output: output,
  1027. searches: web.search ? 1 : 0
  1028. });
  1029. }
  1030. ​
  1031. function estimateOutputFromText(text) {
  1032. return ai.budget.tokens(text || "");
  1033. }
  1034. ​
  1035. function neutralMessage(provider, model, result) {
  1036. var content = [];
  1037. if (result.text) content.push({ type: "text", text: result.text });
  1038. result.toolCalls.forEach(function (c) {
  1039. var part = { type: "tool_call", id: c.id, name: c.name, input: c.input };
  1040. if (c.gid) part.gid = true;
  1041. content.push(part);
  1042. });
  1043. var msg = { role: "assistant", content: content };
  1044. if (result.native) msg.native = { provider: provider, model: model, data: result.native };
  1045. return msg;
  1046. }
  1047. ​
  1048. async function record(req, model, usage, cost) {
  1049. try {
  1050. await ai.budget.record({
  1051. provider: req.provider,
  1052. model: model,
  1053. role: req.role || "",
  1054. kind: req.kind || "chat",
  1055. usage: usage,
  1056. cost: cost
  1057. });
  1058. } catch (e) {
  1059. console.error(e);
  1060. }
  1061. }
  1062. ​
  1063. var providers = (ai.providers = {
  1064. adapters: ADAPTERS,
  1065. ​
  1066. estimateInput: estimateInput,
  1067. ​
  1068. estimate: function (req) {
  1069. var row = req.row || ai.findModel(req.provider, req.model);
  1070. return estimateRequest(req, row);
  1071. },
  1072. ​
  1073. stream: async function (req, handlers) {
  1074. ai.assertNetwork();
  1075. if (ai.redact) req = ai.redact.request(req);
  1076. var adapter = ADAPTERS[req.provider];
  1077. if (!adapter) throw providerError(req.provider, "ai.err.unknown_provider");
  1078. if (!req.model) throw providerError(req.provider, "ai.err.no_model");
  1079. var key;
  1080. try {
  1081. key = await ai.keys.get(req.provider);
  1082. } catch (e) {
  1083. if (e.code === "locked") throw e;
  1084. throw providerError(req.provider, "ai.err.no_key");
  1085. }
  1086. if (!key) throw providerError(req.provider, "ai.err.no_key");
  1087. var row = req.row || ai.findModel(req.provider, req.model);
  1088. if (req.budget !== false) {
  1089. var check = await ai.budget.check(estimateRequest(req, row));
  1090. if (!check.ok) {
  1091. var be = new Error(ai.t(check.reason, check.params));
  1092. be.code = "budget";
  1093. throw be;
  1094. }
  1095. }
  1096. ​
  1097. var controller = new AbortController();
  1098. if (req.signal) {
  1099. if (req.signal.aborted) controller.abort();
  1100. else req.signal.addEventListener("abort", function () { controller.abort(); }, { once: true });
  1101. }
  1102. active.add(controller);
  1103. var flags = {};
  1104. var attempts = 0;
  1105. var recoveries = 0;
  1106. var state = null;
  1107. var started = Date.now();
  1108. try {
  1109. for (;;) {
  1110. var built = adapter.build(req, key, flags);
  1111. var res;
  1112. try {
  1113. res = await send(built, controller.signal);
  1114. } catch (e) {
  1115. if (e.name === "AbortError" || controller.signal.aborted) throw e;
  1116. if (attempts < MAX_RETRIES) {
  1117. attempts++;
  1118. await wait(800 * attempts, controller.signal);
  1119. continue;
  1120. }
  1121. throw providerError(req.provider, "ai.err.network", { message: e.message });
  1122. }
  1123. if (!res.ok) {
  1124. var err = await httpError(req.provider, res);
  1125. if (res.status === 400 && recoveries < MAX_RECOVERIES) {
  1126. var flag = adapter.recover(err.detail, flags, req);
  1127. if (flag) {
  1128. flags[flag] = true;
  1129. recoveries++;
  1130. continue;
  1131. }
  1132. }
  1133. if (err.retryable && attempts < MAX_RETRIES) {
  1134. attempts++;
  1135. await wait(err.retryAfter || 1000 * Math.pow(2, attempts), controller.signal);
  1136. continue;
  1137. }
  1138. throw err;
  1139. }
  1140. state = adapter.start(req, flags);
  1141. state.handlers = handlers || {};
  1142. await readSSE(res, function (name, data) { adapter.event(state, name, data); });
  1143. var out = adapter.finish(state);
  1144. var model = out.model || req.model;
  1145. await record(req, model, out.usage, out.cost);
  1146. state = null;
  1147. return {
  1148. provider: req.provider,
  1149. model: model,
  1150. requestedModel: req.model,
  1151. text: out.text,
  1152. toolCalls: out.toolCalls,
  1153. stopReason: out.stopReason,
  1154. refusal: out.refusal || null,
  1155. usage: out.usage,
  1156. cost: typeof out.cost === "number" ? out.cost : ai.budget.price(ai.findModel(req.provider, model) || row, out.usage).cost,
  1157. costKnown: typeof out.cost === "number" || ai.budget.price(ai.findModel(req.provider, model) || row, out.usage).known,
  1158. ms: Date.now() - started,
  1159. flags: Object.keys(flags),
  1160. message: neutralMessage(req.provider, req.provider === "gemini" ? geminiModel(req.model) : req.model, out)
  1161. };
  1162. }
  1163. } catch (e) {
  1164. var partialCost = 0;
  1165. if (state) {
  1166. var usage = Object.assign(emptyUsage(), adapter.partial(state));
  1167. var produced = state.text || (state.blocks || []).filter(Boolean).map(function (b) { return (b.text || "") + (b.thinking || ""); }).join("");
  1168. if (!usage.output) usage.output = estimateOutputFromText(produced);
  1169. if (!usage.input) usage.input = estimateInput(req);
  1170. await record(req, state.model || req.model, usage, state.cost);
  1171. partialCost = typeof state.cost === "number" ? state.cost : ai.budget.price(ai.findModel(req.provider, state.model || req.model) || row, usage).cost;
  1172. }
  1173. if (e.name === "AbortError" || controller.signal.aborted) {
  1174. var ab = new DOMException(t("ai.err.aborted"), "AbortError");
  1175. ab.partialCost = partialCost;
  1176. throw ab;
  1177. }
  1178. e.partialCost = partialCost;
  1179. throw e;
  1180. } finally {
  1181. active.delete(controller);
  1182. }
  1183. },
  1184. ​
  1185. test: async function (provider) {
  1186. var row = ai.models().find(function (m) { return m.provider === provider && m.model; });
  1187. var model = row ? row.model : ai.get("ghost.provider") === provider ? ai.get("ghost.model") : "";
  1188. if (!model) throw providerError(provider, "ai.err.no_model");
  1189. var result = await providers.stream({
  1190. provider: provider,
  1191. model: model,
  1192. row: row || null,
  1193. kind: "test",
  1194. lite: true,
  1195. cache: false,
  1196. effort: "low",
  1197. maxTokens: 512,
  1198. expectedOutput: 20,
  1199. system: [{ text: "Reply with the single word OK." }],
  1200. messages: [{ role: "user", content: [{ type: "text", text: "OK?" }] }]
  1201. });
  1202. return result;
  1203. },
  1204. ​
  1205. abortAll: function () {
  1206. active.forEach(function (c) { c.abort(); });
  1207. active.clear();
  1208. },
  1209. ​
  1210. busy: function () {
  1211. return active.size > 0;
  1212. }
  1213. });
  1214. ​
  1215. A.on("ai-abort", function () { providers.abortAll(); });
  1216. })();
  1217. ​