@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.
package/dist/index.cjs CHANGED
@@ -3,6 +3,7 @@
3
3
  // src/config/defaults.ts
4
4
  var DEFAULT_BASE_URL = "https://api.onyx.dev";
5
5
  var DEFAULT_AI_BASE_URL = "https://ai.onyx.dev";
6
+ var DEFAULT_AI_MODEL = "onyx";
6
7
  var sanitizeBaseUrl = (u) => u.replace(/\/+$/, "");
7
8
 
8
9
  // src/errors/config-error.ts
@@ -62,6 +63,7 @@ function readEnv(targetId) {
62
63
  const res = dropUndefined({
63
64
  baseUrl: pick("ONYX_DATABASE_BASE_URL"),
64
65
  aiBaseUrl: pick("ONYX_AI_BASE_URL"),
66
+ defaultModel: pick("ONYX_DEFAULT_MODEL"),
65
67
  databaseId: envId,
66
68
  apiKey: pick("ONYX_DATABASE_API_KEY"),
67
69
  apiSecret: pick("ONYX_DATABASE_API_SECRET")
@@ -199,6 +201,7 @@ async function resolveConfig(input) {
199
201
  const merged = {
200
202
  baseUrl: DEFAULT_BASE_URL,
201
203
  aiBaseUrl: DEFAULT_AI_BASE_URL,
204
+ defaultModel: DEFAULT_AI_MODEL,
202
205
  ...dropUndefined(home),
203
206
  ...dropUndefined(project),
204
207
  ...dropUndefined(cfgPath),
@@ -208,6 +211,7 @@ async function resolveConfig(input) {
208
211
  dbg("merged (pre-validate):", mask(merged));
209
212
  const baseUrl = sanitizeBaseUrl(merged.baseUrl ?? DEFAULT_BASE_URL);
210
213
  const aiBaseUrl = sanitizeBaseUrl(merged.aiBaseUrl ?? DEFAULT_AI_BASE_URL);
214
+ const defaultModel = typeof merged.defaultModel === "string" && merged.defaultModel.trim() ? merged.defaultModel.trim() : DEFAULT_AI_MODEL;
211
215
  const databaseId = merged.databaseId ?? "";
212
216
  const apiKey = merged.apiKey ?? "";
213
217
  const apiSecret = merged.apiSecret ?? "";
@@ -244,6 +248,7 @@ async function resolveConfig(input) {
244
248
  const resolved = {
245
249
  baseUrl,
246
250
  aiBaseUrl,
251
+ defaultModel,
247
252
  databaseId,
248
253
  apiKey,
249
254
  apiSecret,
@@ -255,6 +260,7 @@ async function resolveConfig(input) {
255
260
  const source = {
256
261
  databaseId: input?.databaseId ? "explicit config" : env.databaseId ? "env" : cfgPath.databaseId ? "env ONYX_CONFIG_PATH" : project.databaseId ? "project file" : home.databaseId ? "home profile" : "unknown",
257
262
  aiBaseUrl: input?.aiBaseUrl ? "explicit config" : env.aiBaseUrl ? "env" : cfgPath.aiBaseUrl ? "env ONYX_CONFIG_PATH" : project.aiBaseUrl ? "project file" : home.aiBaseUrl ? "home profile" : "default",
263
+ defaultModel: input?.defaultModel ? "explicit config" : env.defaultModel ? "env" : cfgPath.defaultModel ? "env ONYX_CONFIG_PATH" : project.defaultModel ? "project file" : home.defaultModel ? "home profile" : "default",
258
264
  apiKey: input?.apiKey ? "explicit config" : env.apiKey ? "env" : cfgPath.apiKey ? "env ONYX_CONFIG_PATH" : project.apiKey ? "project file" : home.apiKey ? "home profile" : "unknown",
259
265
  apiSecret: input?.apiSecret ? "explicit config" : env.apiSecret ? "env" : cfgPath.apiSecret ? "env ONYX_CONFIG_PATH" : project.apiSecret ? "project file" : home.apiSecret ? "home profile" : "unknown"
260
266
  };
@@ -1277,58 +1283,64 @@ var OnyxDatabaseImpl = class {
1277
1283
  requestLoggingEnabled;
1278
1284
  responseLoggingEnabled;
1279
1285
  defaultPartition;
1286
+ ai;
1280
1287
  constructor(config, resolveConfigWithCache) {
1281
1288
  this.requestLoggingEnabled = !!config?.requestLoggingEnabled;
1282
1289
  this.responseLoggingEnabled = !!config?.responseLoggingEnabled;
1283
1290
  this.defaultPartition = config?.partition;
1284
1291
  this.cfgPromise = resolveConfigWithCache(config);
1292
+ this.ai = this.createAiFacade();
1285
1293
  }
1286
- async ensureClient() {
1294
+ async resolveConfig() {
1287
1295
  if (!this.resolved) {
1288
1296
  this.resolved = await this.cfgPromise;
1289
1297
  }
1298
+ return this.resolved;
1299
+ }
1300
+ async ensureClient() {
1301
+ const cfg = await this.resolveConfig();
1290
1302
  if (!this.http) {
1291
1303
  this.http = new HttpClient({
1292
- baseUrl: this.resolved.baseUrl,
1293
- apiKey: this.resolved.apiKey,
1294
- apiSecret: this.resolved.apiSecret,
1295
- fetchImpl: this.resolved.fetch,
1304
+ baseUrl: cfg.baseUrl,
1305
+ apiKey: cfg.apiKey,
1306
+ apiSecret: cfg.apiSecret,
1307
+ fetchImpl: cfg.fetch,
1296
1308
  requestLoggingEnabled: this.requestLoggingEnabled,
1297
1309
  responseLoggingEnabled: this.responseLoggingEnabled,
1298
- retryEnabled: this.resolved.retryEnabled,
1299
- maxRetries: this.resolved.maxRetries,
1300
- retryInitialDelayMs: this.resolved.retryInitialDelayMs
1310
+ retryEnabled: cfg.retryEnabled,
1311
+ maxRetries: cfg.maxRetries,
1312
+ retryInitialDelayMs: cfg.retryInitialDelayMs
1301
1313
  });
1302
1314
  }
1303
1315
  return {
1304
1316
  http: this.http,
1305
- fetchImpl: this.resolved.fetch,
1306
- baseUrl: this.resolved.baseUrl,
1307
- databaseId: this.resolved.databaseId
1317
+ fetchImpl: cfg.fetch,
1318
+ baseUrl: cfg.baseUrl,
1319
+ databaseId: cfg.databaseId,
1320
+ defaultModel: cfg.defaultModel ?? DEFAULT_AI_MODEL
1308
1321
  };
1309
1322
  }
1310
1323
  async ensureAiClient() {
1311
- if (!this.resolved) {
1312
- this.resolved = await this.cfgPromise;
1313
- }
1324
+ const cfg = await this.resolveConfig();
1314
1325
  if (!this.aiHttp) {
1315
1326
  this.aiHttp = new HttpClient({
1316
- baseUrl: this.resolved.aiBaseUrl,
1317
- apiKey: this.resolved.apiKey,
1318
- apiSecret: this.resolved.apiSecret,
1319
- fetchImpl: this.resolved.fetch,
1327
+ baseUrl: cfg.aiBaseUrl,
1328
+ apiKey: cfg.apiKey,
1329
+ apiSecret: cfg.apiSecret,
1330
+ fetchImpl: cfg.fetch,
1320
1331
  requestLoggingEnabled: this.requestLoggingEnabled,
1321
1332
  responseLoggingEnabled: this.responseLoggingEnabled,
1322
- retryEnabled: this.resolved.retryEnabled,
1323
- maxRetries: this.resolved.maxRetries,
1324
- retryInitialDelayMs: this.resolved.retryInitialDelayMs
1333
+ retryEnabled: cfg.retryEnabled,
1334
+ maxRetries: cfg.maxRetries,
1335
+ retryInitialDelayMs: cfg.retryInitialDelayMs
1325
1336
  });
1326
1337
  }
1327
1338
  return {
1328
1339
  http: this.aiHttp,
1329
- fetchImpl: this.resolved.fetch,
1330
- aiBaseUrl: this.resolved.aiBaseUrl,
1331
- databaseId: this.resolved.databaseId
1340
+ fetchImpl: cfg.fetch,
1341
+ aiBaseUrl: cfg.aiBaseUrl,
1342
+ databaseId: cfg.databaseId,
1343
+ defaultModel: cfg.defaultModel ?? DEFAULT_AI_MODEL
1332
1344
  };
1333
1345
  }
1334
1346
  registerStream(handle) {
@@ -1343,6 +1355,21 @@ var OnyxDatabaseImpl = class {
1343
1355
  }
1344
1356
  };
1345
1357
  }
1358
+ createAiFacade() {
1359
+ const chat = ((contentOrRequest, options) => {
1360
+ if (typeof contentOrRequest === "string") {
1361
+ return this.chatWithContent(contentOrRequest, options);
1362
+ }
1363
+ return this.getAiChatClient().create(contentOrRequest, options);
1364
+ });
1365
+ return {
1366
+ chat,
1367
+ chatClient: () => this.getAiChatClient(),
1368
+ getModels: () => this.getModels(),
1369
+ getModel: (modelId) => this.getModel(modelId),
1370
+ requestScriptApproval: (input) => this.requestScriptApproval(input)
1371
+ };
1372
+ }
1346
1373
  getRequestLoggingEnabled() {
1347
1374
  return this.requestLoggingEnabled;
1348
1375
  }
@@ -1350,7 +1377,7 @@ var OnyxDatabaseImpl = class {
1350
1377
  return this.responseLoggingEnabled;
1351
1378
  }
1352
1379
  /** -------- IOnyxDatabase -------- */
1353
- chat() {
1380
+ getAiChatClient() {
1354
1381
  return new AiChatClientImpl(
1355
1382
  () => this.ensureAiClient(),
1356
1383
  (handle) => this.registerStream(handle),
@@ -1358,6 +1385,32 @@ var OnyxDatabaseImpl = class {
1358
1385
  () => this.responseLoggingEnabled
1359
1386
  );
1360
1387
  }
1388
+ async chatWithContent(content, options) {
1389
+ const { defaultModel } = await this.ensureAiClient();
1390
+ const stream = options?.stream ?? false;
1391
+ const request = {
1392
+ model: options?.model ?? defaultModel,
1393
+ messages: [{ role: options?.role ?? "user", content }],
1394
+ stream
1395
+ };
1396
+ if (options && "temperature" in options) {
1397
+ request.temperature = options.temperature ?? null;
1398
+ }
1399
+ const result = await this.getAiChatClient().create(request, options);
1400
+ if (stream) return result;
1401
+ if (options?.raw) return result;
1402
+ const first = result.choices?.[0]?.message?.content;
1403
+ if (typeof first === "string" && first.trim().length > 0) {
1404
+ return first;
1405
+ }
1406
+ throw new Error("Chat completion response is missing message content");
1407
+ }
1408
+ chat(content, options) {
1409
+ if (typeof content === "string") {
1410
+ return this.chatWithContent(content, options);
1411
+ }
1412
+ return this.getAiChatClient();
1413
+ }
1361
1414
  async getModels() {
1362
1415
  const { http } = await this.ensureAiClient();
1363
1416
  return http.request("GET", "/v1/models");