@onyx.dev/onyx-database 1.2.0 → 2.0.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.
@@ -12,6 +12,7 @@ var process__default = /*#__PURE__*/_interopDefault(process);
12
12
  // src/config/defaults.ts
13
13
  var DEFAULT_BASE_URL = "https://api.onyx.dev";
14
14
  var DEFAULT_AI_BASE_URL = "https://ai.onyx.dev";
15
+ var DEFAULT_AI_MODEL = "onyx";
15
16
  var sanitizeBaseUrl = (u) => u.replace(/\/+$/, "");
16
17
 
17
18
  // src/errors/config-error.ts
@@ -71,6 +72,7 @@ function readEnv(targetId) {
71
72
  const res = dropUndefined({
72
73
  baseUrl: pick("ONYX_DATABASE_BASE_URL"),
73
74
  aiBaseUrl: pick("ONYX_AI_BASE_URL"),
75
+ defaultModel: pick("ONYX_DEFAULT_MODEL"),
74
76
  databaseId: envId,
75
77
  apiKey: pick("ONYX_DATABASE_API_KEY"),
76
78
  apiSecret: pick("ONYX_DATABASE_API_SECRET")
@@ -208,6 +210,7 @@ async function resolveConfig(input) {
208
210
  const merged = {
209
211
  baseUrl: DEFAULT_BASE_URL,
210
212
  aiBaseUrl: DEFAULT_AI_BASE_URL,
213
+ defaultModel: DEFAULT_AI_MODEL,
211
214
  ...dropUndefined(home),
212
215
  ...dropUndefined(project),
213
216
  ...dropUndefined(cfgPath),
@@ -217,6 +220,7 @@ async function resolveConfig(input) {
217
220
  dbg("merged (pre-validate):", mask(merged));
218
221
  const baseUrl = sanitizeBaseUrl(merged.baseUrl ?? DEFAULT_BASE_URL);
219
222
  const aiBaseUrl = sanitizeBaseUrl(merged.aiBaseUrl ?? DEFAULT_AI_BASE_URL);
223
+ const defaultModel = typeof merged.defaultModel === "string" && merged.defaultModel.trim() ? merged.defaultModel.trim() : DEFAULT_AI_MODEL;
220
224
  const databaseId = merged.databaseId ?? "";
221
225
  const apiKey = merged.apiKey ?? "";
222
226
  const apiSecret = merged.apiSecret ?? "";
@@ -253,6 +257,7 @@ async function resolveConfig(input) {
253
257
  const resolved = {
254
258
  baseUrl,
255
259
  aiBaseUrl,
260
+ defaultModel,
256
261
  databaseId,
257
262
  apiKey,
258
263
  apiSecret,
@@ -264,6 +269,7 @@ async function resolveConfig(input) {
264
269
  const source = {
265
270
  databaseId: input?.databaseId ? "explicit config" : env.databaseId ? "env" : cfgPath.databaseId ? "env ONYX_CONFIG_PATH" : project.databaseId ? "project file" : home.databaseId ? "home profile" : "unknown",
266
271
  aiBaseUrl: input?.aiBaseUrl ? "explicit config" : env.aiBaseUrl ? "env" : cfgPath.aiBaseUrl ? "env ONYX_CONFIG_PATH" : project.aiBaseUrl ? "project file" : home.aiBaseUrl ? "home profile" : "default",
272
+ defaultModel: input?.defaultModel ? "explicit config" : env.defaultModel ? "env" : cfgPath.defaultModel ? "env ONYX_CONFIG_PATH" : project.defaultModel ? "project file" : home.defaultModel ? "home profile" : "default",
267
273
  apiKey: input?.apiKey ? "explicit config" : env.apiKey ? "env" : cfgPath.apiKey ? "env ONYX_CONFIG_PATH" : project.apiKey ? "project file" : home.apiKey ? "home profile" : "unknown",
268
274
  apiSecret: input?.apiSecret ? "explicit config" : env.apiSecret ? "env" : cfgPath.apiSecret ? "env ONYX_CONFIG_PATH" : project.apiSecret ? "project file" : home.apiSecret ? "home profile" : "unknown"
269
275
  };
@@ -284,6 +290,7 @@ async function resolveConfigWithSource(input) {
284
290
  const sources = {
285
291
  baseUrl: input?.baseUrl ? "explicit config" : env.baseUrl ? "env" : cfgPath.baseUrl ? "env ONYX_CONFIG_PATH" : project.baseUrl ? "project file" : home.baseUrl ? "home profile" : "default",
286
292
  aiBaseUrl: input?.aiBaseUrl ? "explicit config" : env.aiBaseUrl ? "env" : cfgPath.aiBaseUrl ? "env ONYX_CONFIG_PATH" : project.aiBaseUrl ? "project file" : home.aiBaseUrl ? "home profile" : "default",
293
+ defaultModel: input?.defaultModel ? "explicit config" : env.defaultModel ? "env" : cfgPath.defaultModel ? "env ONYX_CONFIG_PATH" : project.defaultModel ? "project file" : home.defaultModel ? "home profile" : "default",
287
294
  databaseId: input?.databaseId ? "explicit config" : env.databaseId ? "env" : cfgPath.databaseId ? "env ONYX_CONFIG_PATH" : project.databaseId ? "project file" : home.databaseId ? "home profile" : "unknown",
288
295
  apiKey: input?.apiKey ? "explicit config" : env.apiKey ? "env" : cfgPath.apiKey ? "env ONYX_CONFIG_PATH" : project.apiKey ? "project file" : home.apiKey ? "home profile" : "unknown",
289
296
  apiSecret: input?.apiSecret ? "explicit config" : env.apiSecret ? "env" : cfgPath.apiSecret ? "env ONYX_CONFIG_PATH" : project.apiSecret ? "project file" : home.apiSecret ? "home profile" : "unknown",
@@ -1411,58 +1418,64 @@ var OnyxDatabaseImpl = class {
1411
1418
  requestLoggingEnabled;
1412
1419
  responseLoggingEnabled;
1413
1420
  defaultPartition;
1421
+ ai;
1414
1422
  constructor(config, resolveConfigWithCache) {
1415
1423
  this.requestLoggingEnabled = !!config?.requestLoggingEnabled;
1416
1424
  this.responseLoggingEnabled = !!config?.responseLoggingEnabled;
1417
1425
  this.defaultPartition = config?.partition;
1418
1426
  this.cfgPromise = resolveConfigWithCache(config);
1427
+ this.ai = this.createAiFacade();
1419
1428
  }
1420
- async ensureClient() {
1429
+ async resolveConfig() {
1421
1430
  if (!this.resolved) {
1422
1431
  this.resolved = await this.cfgPromise;
1423
1432
  }
1433
+ return this.resolved;
1434
+ }
1435
+ async ensureClient() {
1436
+ const cfg = await this.resolveConfig();
1424
1437
  if (!this.http) {
1425
1438
  this.http = new HttpClient({
1426
- baseUrl: this.resolved.baseUrl,
1427
- apiKey: this.resolved.apiKey,
1428
- apiSecret: this.resolved.apiSecret,
1429
- fetchImpl: this.resolved.fetch,
1439
+ baseUrl: cfg.baseUrl,
1440
+ apiKey: cfg.apiKey,
1441
+ apiSecret: cfg.apiSecret,
1442
+ fetchImpl: cfg.fetch,
1430
1443
  requestLoggingEnabled: this.requestLoggingEnabled,
1431
1444
  responseLoggingEnabled: this.responseLoggingEnabled,
1432
- retryEnabled: this.resolved.retryEnabled,
1433
- maxRetries: this.resolved.maxRetries,
1434
- retryInitialDelayMs: this.resolved.retryInitialDelayMs
1445
+ retryEnabled: cfg.retryEnabled,
1446
+ maxRetries: cfg.maxRetries,
1447
+ retryInitialDelayMs: cfg.retryInitialDelayMs
1435
1448
  });
1436
1449
  }
1437
1450
  return {
1438
1451
  http: this.http,
1439
- fetchImpl: this.resolved.fetch,
1440
- baseUrl: this.resolved.baseUrl,
1441
- databaseId: this.resolved.databaseId
1452
+ fetchImpl: cfg.fetch,
1453
+ baseUrl: cfg.baseUrl,
1454
+ databaseId: cfg.databaseId,
1455
+ defaultModel: cfg.defaultModel ?? DEFAULT_AI_MODEL
1442
1456
  };
1443
1457
  }
1444
1458
  async ensureAiClient() {
1445
- if (!this.resolved) {
1446
- this.resolved = await this.cfgPromise;
1447
- }
1459
+ const cfg = await this.resolveConfig();
1448
1460
  if (!this.aiHttp) {
1449
1461
  this.aiHttp = new HttpClient({
1450
- baseUrl: this.resolved.aiBaseUrl,
1451
- apiKey: this.resolved.apiKey,
1452
- apiSecret: this.resolved.apiSecret,
1453
- fetchImpl: this.resolved.fetch,
1462
+ baseUrl: cfg.aiBaseUrl,
1463
+ apiKey: cfg.apiKey,
1464
+ apiSecret: cfg.apiSecret,
1465
+ fetchImpl: cfg.fetch,
1454
1466
  requestLoggingEnabled: this.requestLoggingEnabled,
1455
1467
  responseLoggingEnabled: this.responseLoggingEnabled,
1456
- retryEnabled: this.resolved.retryEnabled,
1457
- maxRetries: this.resolved.maxRetries,
1458
- retryInitialDelayMs: this.resolved.retryInitialDelayMs
1468
+ retryEnabled: cfg.retryEnabled,
1469
+ maxRetries: cfg.maxRetries,
1470
+ retryInitialDelayMs: cfg.retryInitialDelayMs
1459
1471
  });
1460
1472
  }
1461
1473
  return {
1462
1474
  http: this.aiHttp,
1463
- fetchImpl: this.resolved.fetch,
1464
- aiBaseUrl: this.resolved.aiBaseUrl,
1465
- databaseId: this.resolved.databaseId
1475
+ fetchImpl: cfg.fetch,
1476
+ aiBaseUrl: cfg.aiBaseUrl,
1477
+ databaseId: cfg.databaseId,
1478
+ defaultModel: cfg.defaultModel ?? DEFAULT_AI_MODEL
1466
1479
  };
1467
1480
  }
1468
1481
  registerStream(handle) {
@@ -1477,6 +1490,21 @@ var OnyxDatabaseImpl = class {
1477
1490
  }
1478
1491
  };
1479
1492
  }
1493
+ createAiFacade() {
1494
+ const chat = ((contentOrRequest, options) => {
1495
+ if (typeof contentOrRequest === "string") {
1496
+ return this.chatWithContent(contentOrRequest, options);
1497
+ }
1498
+ return this.getAiChatClient().create(contentOrRequest, options);
1499
+ });
1500
+ return {
1501
+ chat,
1502
+ chatClient: () => this.getAiChatClient(),
1503
+ getModels: () => this.getModels(),
1504
+ getModel: (modelId) => this.getModel(modelId),
1505
+ requestScriptApproval: (input) => this.requestScriptApproval(input)
1506
+ };
1507
+ }
1480
1508
  getRequestLoggingEnabled() {
1481
1509
  return this.requestLoggingEnabled;
1482
1510
  }
@@ -1484,7 +1512,7 @@ var OnyxDatabaseImpl = class {
1484
1512
  return this.responseLoggingEnabled;
1485
1513
  }
1486
1514
  /** -------- IOnyxDatabase -------- */
1487
- chat() {
1515
+ getAiChatClient() {
1488
1516
  return new AiChatClientImpl(
1489
1517
  () => this.ensureAiClient(),
1490
1518
  (handle) => this.registerStream(handle),
@@ -1492,6 +1520,32 @@ var OnyxDatabaseImpl = class {
1492
1520
  () => this.responseLoggingEnabled
1493
1521
  );
1494
1522
  }
1523
+ async chatWithContent(content, options) {
1524
+ const { defaultModel } = await this.ensureAiClient();
1525
+ const stream = options?.stream ?? false;
1526
+ const request = {
1527
+ model: options?.model ?? defaultModel,
1528
+ messages: [{ role: options?.role ?? "user", content }],
1529
+ stream
1530
+ };
1531
+ if (options && "temperature" in options) {
1532
+ request.temperature = options.temperature ?? null;
1533
+ }
1534
+ const result = await this.getAiChatClient().create(request, options);
1535
+ if (stream) return result;
1536
+ if (options?.raw) return result;
1537
+ const first = result.choices?.[0]?.message?.content;
1538
+ if (typeof first === "string" && first.trim().length > 0) {
1539
+ return first;
1540
+ }
1541
+ throw new Error("Chat completion response is missing message content");
1542
+ }
1543
+ chat(content, options) {
1544
+ if (typeof content === "string") {
1545
+ return this.chatWithContent(content, options);
1546
+ }
1547
+ return this.getAiChatClient();
1548
+ }
1495
1549
  async getModels() {
1496
1550
  const { http } = await this.ensureAiClient();
1497
1551
  return http.request("GET", "/v1/models");