@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.
@@ -10,6 +10,7 @@ var process__default = /*#__PURE__*/_interopDefault(process);
10
10
  // src/config/defaults.ts
11
11
  var DEFAULT_BASE_URL = "https://api.onyx.dev";
12
12
  var DEFAULT_AI_BASE_URL = "https://ai.onyx.dev";
13
+ var DEFAULT_AI_MODEL = "onyx";
13
14
  var sanitizeBaseUrl = (u) => u.replace(/\/+$/, "");
14
15
 
15
16
  // src/errors/config-error.ts
@@ -69,6 +70,7 @@ function readEnv(targetId) {
69
70
  const res = dropUndefined({
70
71
  baseUrl: pick("ONYX_DATABASE_BASE_URL"),
71
72
  aiBaseUrl: pick("ONYX_AI_BASE_URL"),
73
+ defaultModel: pick("ONYX_DEFAULT_MODEL"),
72
74
  databaseId: envId,
73
75
  apiKey: pick("ONYX_DATABASE_API_KEY"),
74
76
  apiSecret: pick("ONYX_DATABASE_API_SECRET")
@@ -206,6 +208,7 @@ async function resolveConfig(input) {
206
208
  const merged = {
207
209
  baseUrl: DEFAULT_BASE_URL,
208
210
  aiBaseUrl: DEFAULT_AI_BASE_URL,
211
+ defaultModel: DEFAULT_AI_MODEL,
209
212
  ...dropUndefined(home),
210
213
  ...dropUndefined(project),
211
214
  ...dropUndefined(cfgPath),
@@ -215,6 +218,7 @@ async function resolveConfig(input) {
215
218
  dbg("merged (pre-validate):", mask(merged));
216
219
  const baseUrl = sanitizeBaseUrl(merged.baseUrl ?? DEFAULT_BASE_URL);
217
220
  const aiBaseUrl = sanitizeBaseUrl(merged.aiBaseUrl ?? DEFAULT_AI_BASE_URL);
221
+ const defaultModel = typeof merged.defaultModel === "string" && merged.defaultModel.trim() ? merged.defaultModel.trim() : DEFAULT_AI_MODEL;
218
222
  const databaseId = merged.databaseId ?? "";
219
223
  const apiKey = merged.apiKey ?? "";
220
224
  const apiSecret = merged.apiSecret ?? "";
@@ -251,6 +255,7 @@ async function resolveConfig(input) {
251
255
  const resolved = {
252
256
  baseUrl,
253
257
  aiBaseUrl,
258
+ defaultModel,
254
259
  databaseId,
255
260
  apiKey,
256
261
  apiSecret,
@@ -262,6 +267,7 @@ async function resolveConfig(input) {
262
267
  const source = {
263
268
  databaseId: input?.databaseId ? "explicit config" : env.databaseId ? "env" : cfgPath.databaseId ? "env ONYX_CONFIG_PATH" : project.databaseId ? "project file" : home.databaseId ? "home profile" : "unknown",
264
269
  aiBaseUrl: input?.aiBaseUrl ? "explicit config" : env.aiBaseUrl ? "env" : cfgPath.aiBaseUrl ? "env ONYX_CONFIG_PATH" : project.aiBaseUrl ? "project file" : home.aiBaseUrl ? "home profile" : "default",
270
+ defaultModel: input?.defaultModel ? "explicit config" : env.defaultModel ? "env" : cfgPath.defaultModel ? "env ONYX_CONFIG_PATH" : project.defaultModel ? "project file" : home.defaultModel ? "home profile" : "default",
265
271
  apiKey: input?.apiKey ? "explicit config" : env.apiKey ? "env" : cfgPath.apiKey ? "env ONYX_CONFIG_PATH" : project.apiKey ? "project file" : home.apiKey ? "home profile" : "unknown",
266
272
  apiSecret: input?.apiSecret ? "explicit config" : env.apiSecret ? "env" : cfgPath.apiSecret ? "env ONYX_CONFIG_PATH" : project.apiSecret ? "project file" : home.apiSecret ? "home profile" : "unknown"
267
273
  };
@@ -1374,58 +1380,64 @@ var OnyxDatabaseImpl = class {
1374
1380
  requestLoggingEnabled;
1375
1381
  responseLoggingEnabled;
1376
1382
  defaultPartition;
1383
+ ai;
1377
1384
  constructor(config, resolveConfigWithCache) {
1378
1385
  this.requestLoggingEnabled = !!config?.requestLoggingEnabled;
1379
1386
  this.responseLoggingEnabled = !!config?.responseLoggingEnabled;
1380
1387
  this.defaultPartition = config?.partition;
1381
1388
  this.cfgPromise = resolveConfigWithCache(config);
1389
+ this.ai = this.createAiFacade();
1382
1390
  }
1383
- async ensureClient() {
1391
+ async resolveConfig() {
1384
1392
  if (!this.resolved) {
1385
1393
  this.resolved = await this.cfgPromise;
1386
1394
  }
1395
+ return this.resolved;
1396
+ }
1397
+ async ensureClient() {
1398
+ const cfg = await this.resolveConfig();
1387
1399
  if (!this.http) {
1388
1400
  this.http = new HttpClient({
1389
- baseUrl: this.resolved.baseUrl,
1390
- apiKey: this.resolved.apiKey,
1391
- apiSecret: this.resolved.apiSecret,
1392
- fetchImpl: this.resolved.fetch,
1401
+ baseUrl: cfg.baseUrl,
1402
+ apiKey: cfg.apiKey,
1403
+ apiSecret: cfg.apiSecret,
1404
+ fetchImpl: cfg.fetch,
1393
1405
  requestLoggingEnabled: this.requestLoggingEnabled,
1394
1406
  responseLoggingEnabled: this.responseLoggingEnabled,
1395
- retryEnabled: this.resolved.retryEnabled,
1396
- maxRetries: this.resolved.maxRetries,
1397
- retryInitialDelayMs: this.resolved.retryInitialDelayMs
1407
+ retryEnabled: cfg.retryEnabled,
1408
+ maxRetries: cfg.maxRetries,
1409
+ retryInitialDelayMs: cfg.retryInitialDelayMs
1398
1410
  });
1399
1411
  }
1400
1412
  return {
1401
1413
  http: this.http,
1402
- fetchImpl: this.resolved.fetch,
1403
- baseUrl: this.resolved.baseUrl,
1404
- databaseId: this.resolved.databaseId
1414
+ fetchImpl: cfg.fetch,
1415
+ baseUrl: cfg.baseUrl,
1416
+ databaseId: cfg.databaseId,
1417
+ defaultModel: cfg.defaultModel ?? DEFAULT_AI_MODEL
1405
1418
  };
1406
1419
  }
1407
1420
  async ensureAiClient() {
1408
- if (!this.resolved) {
1409
- this.resolved = await this.cfgPromise;
1410
- }
1421
+ const cfg = await this.resolveConfig();
1411
1422
  if (!this.aiHttp) {
1412
1423
  this.aiHttp = new HttpClient({
1413
- baseUrl: this.resolved.aiBaseUrl,
1414
- apiKey: this.resolved.apiKey,
1415
- apiSecret: this.resolved.apiSecret,
1416
- fetchImpl: this.resolved.fetch,
1424
+ baseUrl: cfg.aiBaseUrl,
1425
+ apiKey: cfg.apiKey,
1426
+ apiSecret: cfg.apiSecret,
1427
+ fetchImpl: cfg.fetch,
1417
1428
  requestLoggingEnabled: this.requestLoggingEnabled,
1418
1429
  responseLoggingEnabled: this.responseLoggingEnabled,
1419
- retryEnabled: this.resolved.retryEnabled,
1420
- maxRetries: this.resolved.maxRetries,
1421
- retryInitialDelayMs: this.resolved.retryInitialDelayMs
1430
+ retryEnabled: cfg.retryEnabled,
1431
+ maxRetries: cfg.maxRetries,
1432
+ retryInitialDelayMs: cfg.retryInitialDelayMs
1422
1433
  });
1423
1434
  }
1424
1435
  return {
1425
1436
  http: this.aiHttp,
1426
- fetchImpl: this.resolved.fetch,
1427
- aiBaseUrl: this.resolved.aiBaseUrl,
1428
- databaseId: this.resolved.databaseId
1437
+ fetchImpl: cfg.fetch,
1438
+ aiBaseUrl: cfg.aiBaseUrl,
1439
+ databaseId: cfg.databaseId,
1440
+ defaultModel: cfg.defaultModel ?? DEFAULT_AI_MODEL
1429
1441
  };
1430
1442
  }
1431
1443
  registerStream(handle) {
@@ -1440,6 +1452,21 @@ var OnyxDatabaseImpl = class {
1440
1452
  }
1441
1453
  };
1442
1454
  }
1455
+ createAiFacade() {
1456
+ const chat = ((contentOrRequest, options) => {
1457
+ if (typeof contentOrRequest === "string") {
1458
+ return this.chatWithContent(contentOrRequest, options);
1459
+ }
1460
+ return this.getAiChatClient().create(contentOrRequest, options);
1461
+ });
1462
+ return {
1463
+ chat,
1464
+ chatClient: () => this.getAiChatClient(),
1465
+ getModels: () => this.getModels(),
1466
+ getModel: (modelId) => this.getModel(modelId),
1467
+ requestScriptApproval: (input) => this.requestScriptApproval(input)
1468
+ };
1469
+ }
1443
1470
  getRequestLoggingEnabled() {
1444
1471
  return this.requestLoggingEnabled;
1445
1472
  }
@@ -1447,7 +1474,7 @@ var OnyxDatabaseImpl = class {
1447
1474
  return this.responseLoggingEnabled;
1448
1475
  }
1449
1476
  /** -------- IOnyxDatabase -------- */
1450
- chat() {
1477
+ getAiChatClient() {
1451
1478
  return new AiChatClientImpl(
1452
1479
  () => this.ensureAiClient(),
1453
1480
  (handle) => this.registerStream(handle),
@@ -1455,6 +1482,32 @@ var OnyxDatabaseImpl = class {
1455
1482
  () => this.responseLoggingEnabled
1456
1483
  );
1457
1484
  }
1485
+ async chatWithContent(content, options) {
1486
+ const { defaultModel } = await this.ensureAiClient();
1487
+ const stream = options?.stream ?? false;
1488
+ const request = {
1489
+ model: options?.model ?? defaultModel,
1490
+ messages: [{ role: options?.role ?? "user", content }],
1491
+ stream
1492
+ };
1493
+ if (options && "temperature" in options) {
1494
+ request.temperature = options.temperature ?? null;
1495
+ }
1496
+ const result = await this.getAiChatClient().create(request, options);
1497
+ if (stream) return result;
1498
+ if (options?.raw) return result;
1499
+ const first = result.choices?.[0]?.message?.content;
1500
+ if (typeof first === "string" && first.trim().length > 0) {
1501
+ return first;
1502
+ }
1503
+ throw new Error("Chat completion response is missing message content");
1504
+ }
1505
+ chat(content, options) {
1506
+ if (typeof content === "string") {
1507
+ return this.chatWithContent(content, options);
1508
+ }
1509
+ return this.getAiChatClient();
1510
+ }
1458
1511
  async getModels() {
1459
1512
  const { http } = await this.ensureAiClient();
1460
1513
  return http.request("GET", "/v1/models");