@onyx.dev/onyx-database 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/edge.cjs CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  // src/config/defaults.ts
4
4
  var DEFAULT_BASE_URL = "https://api.onyx.dev";
5
+ var DEFAULT_AI_BASE_URL = "https://ai.onyx.dev";
5
6
  var sanitizeBaseUrl = (u) => u.replace(/\/+$/, "");
6
7
 
7
8
  // src/errors/config-error.ts
@@ -53,6 +54,7 @@ function readEnv(targetId) {
53
54
  if (targetId && envId !== targetId) return {};
54
55
  const res = dropUndefined({
55
56
  baseUrl: pick("ONYX_DATABASE_BASE_URL"),
57
+ aiBaseUrl: pick("ONYX_AI_BASE_URL"),
56
58
  databaseId: envId,
57
59
  apiKey: pick("ONYX_DATABASE_API_KEY"),
58
60
  apiSecret: pick("ONYX_DATABASE_API_SECRET")
@@ -71,11 +73,13 @@ async function resolveConfig(input) {
71
73
  const env = readEnv(input?.databaseId);
72
74
  const merged = {
73
75
  baseUrl: DEFAULT_BASE_URL,
76
+ aiBaseUrl: DEFAULT_AI_BASE_URL,
74
77
  ...dropUndefined(env),
75
78
  ...dropUndefined(input)
76
79
  };
77
80
  dbg("merged (pre-validate):", mask(merged));
78
81
  const baseUrl = sanitizeBaseUrl(merged.baseUrl ?? DEFAULT_BASE_URL);
82
+ const aiBaseUrl = sanitizeBaseUrl(merged.aiBaseUrl ?? DEFAULT_AI_BASE_URL);
79
83
  const databaseId = merged.databaseId ?? "";
80
84
  const apiKey = merged.apiKey ?? "";
81
85
  const apiSecret = merged.apiSecret ?? "";
@@ -100,6 +104,7 @@ async function resolveConfig(input) {
100
104
  }
101
105
  const resolved = {
102
106
  baseUrl,
107
+ aiBaseUrl,
103
108
  databaseId,
104
109
  apiKey,
105
110
  apiSecret,
@@ -1120,6 +1125,7 @@ var OnyxDatabaseImpl = class {
1120
1125
  cfgPromise;
1121
1126
  resolved = null;
1122
1127
  http = null;
1128
+ aiHttp = null;
1123
1129
  streams = /* @__PURE__ */ new Set();
1124
1130
  requestLoggingEnabled;
1125
1131
  responseLoggingEnabled;
@@ -1154,6 +1160,30 @@ var OnyxDatabaseImpl = class {
1154
1160
  databaseId: this.resolved.databaseId
1155
1161
  };
1156
1162
  }
1163
+ async ensureAiClient() {
1164
+ if (!this.resolved) {
1165
+ this.resolved = await this.cfgPromise;
1166
+ }
1167
+ if (!this.aiHttp) {
1168
+ this.aiHttp = new HttpClient({
1169
+ baseUrl: this.resolved.aiBaseUrl,
1170
+ apiKey: this.resolved.apiKey,
1171
+ apiSecret: this.resolved.apiSecret,
1172
+ fetchImpl: this.resolved.fetch,
1173
+ requestLoggingEnabled: this.requestLoggingEnabled,
1174
+ responseLoggingEnabled: this.responseLoggingEnabled,
1175
+ retryEnabled: this.resolved.retryEnabled,
1176
+ maxRetries: this.resolved.maxRetries,
1177
+ retryInitialDelayMs: this.resolved.retryInitialDelayMs
1178
+ });
1179
+ }
1180
+ return {
1181
+ http: this.aiHttp,
1182
+ fetchImpl: this.resolved.fetch,
1183
+ aiBaseUrl: this.resolved.aiBaseUrl,
1184
+ databaseId: this.resolved.databaseId
1185
+ };
1186
+ }
1157
1187
  registerStream(handle) {
1158
1188
  this.streams.add(handle);
1159
1189
  return {
@@ -1166,7 +1196,34 @@ var OnyxDatabaseImpl = class {
1166
1196
  }
1167
1197
  };
1168
1198
  }
1199
+ getRequestLoggingEnabled() {
1200
+ return this.requestLoggingEnabled;
1201
+ }
1202
+ getResponseLoggingEnabled() {
1203
+ return this.responseLoggingEnabled;
1204
+ }
1169
1205
  /** -------- IOnyxDatabase -------- */
1206
+ chat() {
1207
+ return new AiChatClientImpl(
1208
+ () => this.ensureAiClient(),
1209
+ (handle) => this.registerStream(handle),
1210
+ () => this.requestLoggingEnabled,
1211
+ () => this.responseLoggingEnabled
1212
+ );
1213
+ }
1214
+ async getModels() {
1215
+ const { http } = await this.ensureAiClient();
1216
+ return http.request("GET", "/v1/models");
1217
+ }
1218
+ async getModel(modelId) {
1219
+ const { http } = await this.ensureAiClient();
1220
+ const path = `/v1/models/${encodeURIComponent(modelId)}`;
1221
+ return http.request("GET", path);
1222
+ }
1223
+ async requestScriptApproval(input) {
1224
+ const { http } = await this.ensureAiClient();
1225
+ return http.request("POST", "/api/script-approvals", input);
1226
+ }
1170
1227
  from(table) {
1171
1228
  return new QueryBuilderImpl(this, String(table), this.defaultPartition);
1172
1229
  }
@@ -1716,6 +1773,153 @@ var CascadeBuilderImpl = class {
1716
1773
  return this.db.delete(table, primaryKey, opts);
1717
1774
  }
1718
1775
  };
1776
+ var AiChatClientImpl = class {
1777
+ constructor(resolveAiClient, registerStream, requestLoggingEnabled, responseLoggingEnabled) {
1778
+ this.resolveAiClient = resolveAiClient;
1779
+ this.registerStream = registerStream;
1780
+ this.requestLoggingEnabled = requestLoggingEnabled;
1781
+ this.responseLoggingEnabled = responseLoggingEnabled;
1782
+ }
1783
+ normalizeEventData(rawEvent) {
1784
+ const lines = rawEvent.split("\n");
1785
+ const dataLines = [];
1786
+ for (const line of lines) {
1787
+ const trimmed = line.trim();
1788
+ if (!trimmed || trimmed.startsWith(":")) continue;
1789
+ if (trimmed.startsWith("data:")) {
1790
+ dataLines.push(trimmed.slice(5).trim());
1791
+ } else {
1792
+ dataLines.push(trimmed);
1793
+ }
1794
+ }
1795
+ if (!dataLines.length) return null;
1796
+ const joined = dataLines.join("\n").trim();
1797
+ return joined.length > 0 ? joined : null;
1798
+ }
1799
+ logRequest(url, body, headers) {
1800
+ if (!this.requestLoggingEnabled()) return;
1801
+ console.log(`POST ${url}`);
1802
+ if (body != null) {
1803
+ const serialized = typeof body === "string" ? body : JSON.stringify(body);
1804
+ console.log(serialized);
1805
+ }
1806
+ const headerLog = { ...headers };
1807
+ if (headerLog["x-onyx-secret"]) headerLog["x-onyx-secret"] = "[REDACTED]";
1808
+ console.log("Headers:", headerLog);
1809
+ }
1810
+ logResponse(status, statusText, raw) {
1811
+ if (!this.responseLoggingEnabled()) return;
1812
+ const statusLine = `${status} ${statusText}`.trim();
1813
+ console.log(statusLine);
1814
+ if (raw && raw.trim().length > 0) {
1815
+ console.log(raw);
1816
+ }
1817
+ }
1818
+ toOnyxError(status, statusText, raw) {
1819
+ let parsed = raw;
1820
+ try {
1821
+ parsed = parseJsonAllowNaN(raw);
1822
+ } catch {
1823
+ }
1824
+ const message = typeof parsed === "object" && parsed !== null && "error" in parsed && typeof parsed.error === "object" && typeof parsed.error?.message === "string" ? String(parsed.error.message) : `${status} ${statusText}`;
1825
+ return new OnyxHttpError(message, status, statusText, parsed, raw);
1826
+ }
1827
+ async create(request, options) {
1828
+ const body = { ...request, stream: !!request.stream };
1829
+ const { http, fetchImpl, aiBaseUrl, databaseId } = await this.resolveAiClient();
1830
+ const params = new URLSearchParams();
1831
+ const scopedDb = options?.databaseId ?? databaseId;
1832
+ if (scopedDb) params.append("databaseId", scopedDb);
1833
+ const path = `/v1/chat/completions${params.size ? `?${params.toString()}` : ""}`;
1834
+ if (!body.stream) {
1835
+ return http.request("POST", path, body);
1836
+ }
1837
+ const url = `${aiBaseUrl}${path}`;
1838
+ const headers = http.headers({ Accept: "text/event-stream" });
1839
+ this.logRequest(url, body, headers);
1840
+ const res = await fetchImpl(url, {
1841
+ method: "POST",
1842
+ headers,
1843
+ body: JSON.stringify(body)
1844
+ });
1845
+ if (!res.ok) {
1846
+ const raw = await res.text();
1847
+ this.logResponse(res.status, res.statusText, raw);
1848
+ throw this.toOnyxError(res.status, res.statusText, raw);
1849
+ }
1850
+ this.logResponse(res.status, res.statusText);
1851
+ const bodyStream = res.body;
1852
+ const reader = bodyStream && typeof bodyStream.getReader === "function" ? bodyStream.getReader() : null;
1853
+ if (!reader) {
1854
+ throw new OnyxHttpError(
1855
+ "Streaming response body is not readable",
1856
+ res.status,
1857
+ res.statusText,
1858
+ null,
1859
+ ""
1860
+ );
1861
+ }
1862
+ let canceled = false;
1863
+ const handle = {
1864
+ cancel: () => {
1865
+ if (canceled) return;
1866
+ canceled = true;
1867
+ try {
1868
+ reader.cancel?.();
1869
+ } catch {
1870
+ }
1871
+ }
1872
+ };
1873
+ const registered = this.registerStream(handle);
1874
+ const normalizeEvent = (rawEvent) => this.normalizeEventData(rawEvent);
1875
+ const stream = {
1876
+ async *[Symbol.asyncIterator]() {
1877
+ const decoder = new TextDecoder("utf-8");
1878
+ let buffer = "";
1879
+ try {
1880
+ while (!canceled) {
1881
+ const { done, value } = await reader.read();
1882
+ if (done) break;
1883
+ if (value) {
1884
+ buffer += decoder.decode(value, { stream: true });
1885
+ }
1886
+ let splitIndex = buffer.indexOf("\n\n");
1887
+ while (splitIndex !== -1) {
1888
+ const rawEvent = buffer.slice(0, splitIndex);
1889
+ buffer = buffer.slice(splitIndex + 2);
1890
+ const data = normalizeEvent(rawEvent);
1891
+ if (data === "[DONE]") return;
1892
+ if (data) {
1893
+ try {
1894
+ yield parseJsonAllowNaN(data);
1895
+ } catch {
1896
+ }
1897
+ }
1898
+ splitIndex = buffer.indexOf("\n\n");
1899
+ }
1900
+ }
1901
+ buffer += decoder.decode();
1902
+ const remaining = buffer.trim();
1903
+ if (remaining && remaining !== "[DONE]") {
1904
+ const data = normalizeEvent(remaining);
1905
+ if (data && data !== "[DONE]") {
1906
+ try {
1907
+ yield parseJsonAllowNaN(data);
1908
+ } catch {
1909
+ }
1910
+ }
1911
+ }
1912
+ } finally {
1913
+ registered.cancel();
1914
+ }
1915
+ },
1916
+ cancel() {
1917
+ registered.cancel();
1918
+ }
1919
+ };
1920
+ return stream;
1921
+ }
1922
+ };
1719
1923
  function createOnyxFacade(resolveConfig2) {
1720
1924
  let cachedCfg = null;
1721
1925
  function resolveConfigWithCache(config) {