@onyx.dev/onyx-database 1.2.0 → 2.0.1

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
@@ -1,8 +1,13 @@
1
1
  'use strict';
2
2
 
3
+ // package.json
4
+ var name = "@onyx.dev/onyx-database";
5
+ var version = "2.0.1";
6
+
3
7
  // src/config/defaults.ts
4
8
  var DEFAULT_BASE_URL = "https://api.onyx.dev";
5
9
  var DEFAULT_AI_BASE_URL = "https://ai.onyx.dev";
10
+ var DEFAULT_AI_MODEL = "onyx";
6
11
  var sanitizeBaseUrl = (u) => u.replace(/\/+$/, "");
7
12
 
8
13
  // src/errors/config-error.ts
@@ -62,6 +67,7 @@ function readEnv(targetId) {
62
67
  const res = dropUndefined({
63
68
  baseUrl: pick("ONYX_DATABASE_BASE_URL"),
64
69
  aiBaseUrl: pick("ONYX_AI_BASE_URL"),
70
+ defaultModel: pick("ONYX_DEFAULT_MODEL"),
65
71
  databaseId: envId,
66
72
  apiKey: pick("ONYX_DATABASE_API_KEY"),
67
73
  apiSecret: pick("ONYX_DATABASE_API_SECRET")
@@ -199,6 +205,7 @@ async function resolveConfig(input) {
199
205
  const merged = {
200
206
  baseUrl: DEFAULT_BASE_URL,
201
207
  aiBaseUrl: DEFAULT_AI_BASE_URL,
208
+ defaultModel: DEFAULT_AI_MODEL,
202
209
  ...dropUndefined(home),
203
210
  ...dropUndefined(project),
204
211
  ...dropUndefined(cfgPath),
@@ -208,6 +215,7 @@ async function resolveConfig(input) {
208
215
  dbg("merged (pre-validate):", mask(merged));
209
216
  const baseUrl = sanitizeBaseUrl(merged.baseUrl ?? DEFAULT_BASE_URL);
210
217
  const aiBaseUrl = sanitizeBaseUrl(merged.aiBaseUrl ?? DEFAULT_AI_BASE_URL);
218
+ const defaultModel = typeof merged.defaultModel === "string" && merged.defaultModel.trim() ? merged.defaultModel.trim() : DEFAULT_AI_MODEL;
211
219
  const databaseId = merged.databaseId ?? "";
212
220
  const apiKey = merged.apiKey ?? "";
213
221
  const apiSecret = merged.apiSecret ?? "";
@@ -244,6 +252,7 @@ async function resolveConfig(input) {
244
252
  const resolved = {
245
253
  baseUrl,
246
254
  aiBaseUrl,
255
+ defaultModel,
247
256
  databaseId,
248
257
  apiKey,
249
258
  apiSecret,
@@ -255,6 +264,7 @@ async function resolveConfig(input) {
255
264
  const source = {
256
265
  databaseId: input?.databaseId ? "explicit config" : env.databaseId ? "env" : cfgPath.databaseId ? "env ONYX_CONFIG_PATH" : project.databaseId ? "project file" : home.databaseId ? "home profile" : "unknown",
257
266
  aiBaseUrl: input?.aiBaseUrl ? "explicit config" : env.aiBaseUrl ? "env" : cfgPath.aiBaseUrl ? "env ONYX_CONFIG_PATH" : project.aiBaseUrl ? "project file" : home.aiBaseUrl ? "home profile" : "default",
267
+ defaultModel: input?.defaultModel ? "explicit config" : env.defaultModel ? "env" : cfgPath.defaultModel ? "env ONYX_CONFIG_PATH" : project.defaultModel ? "project file" : home.defaultModel ? "home profile" : "default",
258
268
  apiKey: input?.apiKey ? "explicit config" : env.apiKey ? "env" : cfgPath.apiKey ? "env ONYX_CONFIG_PATH" : project.apiKey ? "project file" : home.apiKey ? "home profile" : "unknown",
259
269
  apiSecret: input?.apiSecret ? "explicit config" : env.apiSecret ? "env" : cfgPath.apiSecret ? "env ONYX_CONFIG_PATH" : project.apiSecret ? "project file" : home.apiSecret ? "home profile" : "unknown"
260
270
  };
@@ -945,8 +955,8 @@ var CascadeRelationshipBuilder = class {
945
955
  * builder.graph('programs');
946
956
  * ```
947
957
  */
948
- graph(name) {
949
- this.graphName = name;
958
+ graph(name2) {
959
+ this.graphName = name2;
950
960
  return this;
951
961
  }
952
962
  /**
@@ -1033,24 +1043,24 @@ function diffAttributes(apiAttrs, localAttrs) {
1033
1043
  const added = [];
1034
1044
  const removed = [];
1035
1045
  const changed = [];
1036
- for (const [name, local] of localMap.entries()) {
1037
- if (!apiMap.has(name)) {
1046
+ for (const [name2, local] of localMap.entries()) {
1047
+ if (!apiMap.has(name2)) {
1038
1048
  added.push(local);
1039
1049
  continue;
1040
1050
  }
1041
- const api = apiMap.get(name);
1051
+ const api = apiMap.get(name2);
1042
1052
  const apiNull = Boolean(api.isNullable);
1043
1053
  const localNull = Boolean(local.isNullable);
1044
1054
  if (api.type !== local.type || apiNull !== localNull) {
1045
1055
  changed.push({
1046
- name,
1056
+ name: name2,
1047
1057
  from: { type: api.type, isNullable: apiNull },
1048
1058
  to: { type: local.type, isNullable: localNull }
1049
1059
  });
1050
1060
  }
1051
1061
  }
1052
- for (const name of apiMap.keys()) {
1053
- if (!localMap.has(name)) removed.push(name);
1062
+ for (const name2 of apiMap.keys()) {
1063
+ if (!localMap.has(name2)) removed.push(name2);
1054
1064
  }
1055
1065
  added.sort((a, b) => a.name.localeCompare(b.name));
1056
1066
  removed.sort();
@@ -1064,22 +1074,22 @@ function diffIndexes(apiIndexes, localIndexes) {
1064
1074
  const added = [];
1065
1075
  const removed = [];
1066
1076
  const changed = [];
1067
- for (const [name, local] of localMap.entries()) {
1068
- if (!apiMap.has(name)) {
1077
+ for (const [name2, local] of localMap.entries()) {
1078
+ if (!apiMap.has(name2)) {
1069
1079
  added.push(local);
1070
1080
  continue;
1071
1081
  }
1072
- const api = apiMap.get(name);
1082
+ const api = apiMap.get(name2);
1073
1083
  const apiType = api.type ?? "DEFAULT";
1074
1084
  const localType = local.type ?? "DEFAULT";
1075
1085
  const apiScore = api.minimumScore;
1076
1086
  const localScore = local.minimumScore;
1077
1087
  if (apiType !== localType || apiScore !== localScore) {
1078
- changed.push({ name, from: api, to: local });
1088
+ changed.push({ name: name2, from: api, to: local });
1079
1089
  }
1080
1090
  }
1081
- for (const name of apiMap.keys()) {
1082
- if (!localMap.has(name)) removed.push(name);
1091
+ for (const name2 of apiMap.keys()) {
1092
+ if (!localMap.has(name2)) removed.push(name2);
1083
1093
  }
1084
1094
  added.sort((a, b) => a.name.localeCompare(b.name));
1085
1095
  removed.sort();
@@ -1093,18 +1103,18 @@ function diffResolvers(apiResolvers, localResolvers) {
1093
1103
  const added = [];
1094
1104
  const removed = [];
1095
1105
  const changed = [];
1096
- for (const [name, local] of localMap.entries()) {
1097
- if (!apiMap.has(name)) {
1106
+ for (const [name2, local] of localMap.entries()) {
1107
+ if (!apiMap.has(name2)) {
1098
1108
  added.push(local);
1099
1109
  continue;
1100
1110
  }
1101
- const api = apiMap.get(name);
1111
+ const api = apiMap.get(name2);
1102
1112
  if (api.resolver !== local.resolver) {
1103
- changed.push({ name, from: api, to: local });
1113
+ changed.push({ name: name2, from: api, to: local });
1104
1114
  }
1105
1115
  }
1106
- for (const name of apiMap.keys()) {
1107
- if (!localMap.has(name)) removed.push(name);
1116
+ for (const name2 of apiMap.keys()) {
1117
+ if (!localMap.has(name2)) removed.push(name2);
1108
1118
  }
1109
1119
  added.sort((a, b) => a.name.localeCompare(b.name));
1110
1120
  removed.sort();
@@ -1118,18 +1128,18 @@ function diffTriggers(apiTriggers, localTriggers) {
1118
1128
  const added = [];
1119
1129
  const removed = [];
1120
1130
  const changed = [];
1121
- for (const [name, local] of localMap.entries()) {
1122
- if (!apiMap.has(name)) {
1131
+ for (const [name2, local] of localMap.entries()) {
1132
+ if (!apiMap.has(name2)) {
1123
1133
  added.push(local);
1124
1134
  continue;
1125
1135
  }
1126
- const api = apiMap.get(name);
1136
+ const api = apiMap.get(name2);
1127
1137
  if (api.event !== local.event || api.trigger !== local.trigger) {
1128
- changed.push({ name, from: api, to: local });
1138
+ changed.push({ name: name2, from: api, to: local });
1129
1139
  }
1130
1140
  }
1131
- for (const name of apiMap.keys()) {
1132
- if (!localMap.has(name)) removed.push(name);
1141
+ for (const name2 of apiMap.keys()) {
1142
+ if (!localMap.has(name2)) removed.push(name2);
1133
1143
  }
1134
1144
  added.sort((a, b) => a.name.localeCompare(b.name));
1135
1145
  removed.sort();
@@ -1145,13 +1155,13 @@ function computeSchemaDiff(apiSchema, localSchema) {
1145
1155
  const newTables = [];
1146
1156
  const removedTables = [];
1147
1157
  const changedTables = [];
1148
- for (const [name, localEntity] of localMap.entries()) {
1149
- if (!apiMap.has(name)) {
1150
- newTables.push(name);
1158
+ for (const [name2, localEntity] of localMap.entries()) {
1159
+ if (!apiMap.has(name2)) {
1160
+ newTables.push(name2);
1151
1161
  continue;
1152
1162
  }
1153
- const apiEntity = apiMap.get(name);
1154
- const tableDiff = { name };
1163
+ const apiEntity = apiMap.get(name2);
1164
+ const tableDiff = { name: name2 };
1155
1165
  const partitionFrom = normalizePartition(apiEntity.partition);
1156
1166
  const partitionTo = normalizePartition(localEntity.partition);
1157
1167
  if (partitionFrom !== partitionTo) {
@@ -1176,8 +1186,8 @@ function computeSchemaDiff(apiSchema, localSchema) {
1176
1186
  changedTables.push(tableDiff);
1177
1187
  }
1178
1188
  }
1179
- for (const name of apiMap.keys()) {
1180
- if (!localMap.has(name)) removedTables.push(name);
1189
+ for (const name2 of apiMap.keys()) {
1190
+ if (!localMap.has(name2)) removedTables.push(name2);
1181
1191
  }
1182
1192
  newTables.sort();
1183
1193
  removedTables.sort();
@@ -1277,58 +1287,64 @@ var OnyxDatabaseImpl = class {
1277
1287
  requestLoggingEnabled;
1278
1288
  responseLoggingEnabled;
1279
1289
  defaultPartition;
1290
+ ai;
1280
1291
  constructor(config, resolveConfigWithCache) {
1281
1292
  this.requestLoggingEnabled = !!config?.requestLoggingEnabled;
1282
1293
  this.responseLoggingEnabled = !!config?.responseLoggingEnabled;
1283
1294
  this.defaultPartition = config?.partition;
1284
1295
  this.cfgPromise = resolveConfigWithCache(config);
1296
+ this.ai = this.createAiFacade();
1285
1297
  }
1286
- async ensureClient() {
1298
+ async resolveConfig() {
1287
1299
  if (!this.resolved) {
1288
1300
  this.resolved = await this.cfgPromise;
1289
1301
  }
1302
+ return this.resolved;
1303
+ }
1304
+ async ensureClient() {
1305
+ const cfg = await this.resolveConfig();
1290
1306
  if (!this.http) {
1291
1307
  this.http = new HttpClient({
1292
- baseUrl: this.resolved.baseUrl,
1293
- apiKey: this.resolved.apiKey,
1294
- apiSecret: this.resolved.apiSecret,
1295
- fetchImpl: this.resolved.fetch,
1308
+ baseUrl: cfg.baseUrl,
1309
+ apiKey: cfg.apiKey,
1310
+ apiSecret: cfg.apiSecret,
1311
+ fetchImpl: cfg.fetch,
1296
1312
  requestLoggingEnabled: this.requestLoggingEnabled,
1297
1313
  responseLoggingEnabled: this.responseLoggingEnabled,
1298
- retryEnabled: this.resolved.retryEnabled,
1299
- maxRetries: this.resolved.maxRetries,
1300
- retryInitialDelayMs: this.resolved.retryInitialDelayMs
1314
+ retryEnabled: cfg.retryEnabled,
1315
+ maxRetries: cfg.maxRetries,
1316
+ retryInitialDelayMs: cfg.retryInitialDelayMs
1301
1317
  });
1302
1318
  }
1303
1319
  return {
1304
1320
  http: this.http,
1305
- fetchImpl: this.resolved.fetch,
1306
- baseUrl: this.resolved.baseUrl,
1307
- databaseId: this.resolved.databaseId
1321
+ fetchImpl: cfg.fetch,
1322
+ baseUrl: cfg.baseUrl,
1323
+ databaseId: cfg.databaseId,
1324
+ defaultModel: cfg.defaultModel ?? DEFAULT_AI_MODEL
1308
1325
  };
1309
1326
  }
1310
1327
  async ensureAiClient() {
1311
- if (!this.resolved) {
1312
- this.resolved = await this.cfgPromise;
1313
- }
1328
+ const cfg = await this.resolveConfig();
1314
1329
  if (!this.aiHttp) {
1315
1330
  this.aiHttp = new HttpClient({
1316
- baseUrl: this.resolved.aiBaseUrl,
1317
- apiKey: this.resolved.apiKey,
1318
- apiSecret: this.resolved.apiSecret,
1319
- fetchImpl: this.resolved.fetch,
1331
+ baseUrl: cfg.aiBaseUrl,
1332
+ apiKey: cfg.apiKey,
1333
+ apiSecret: cfg.apiSecret,
1334
+ fetchImpl: cfg.fetch,
1320
1335
  requestLoggingEnabled: this.requestLoggingEnabled,
1321
1336
  responseLoggingEnabled: this.responseLoggingEnabled,
1322
- retryEnabled: this.resolved.retryEnabled,
1323
- maxRetries: this.resolved.maxRetries,
1324
- retryInitialDelayMs: this.resolved.retryInitialDelayMs
1337
+ retryEnabled: cfg.retryEnabled,
1338
+ maxRetries: cfg.maxRetries,
1339
+ retryInitialDelayMs: cfg.retryInitialDelayMs
1325
1340
  });
1326
1341
  }
1327
1342
  return {
1328
1343
  http: this.aiHttp,
1329
- fetchImpl: this.resolved.fetch,
1330
- aiBaseUrl: this.resolved.aiBaseUrl,
1331
- databaseId: this.resolved.databaseId
1344
+ fetchImpl: cfg.fetch,
1345
+ aiBaseUrl: cfg.aiBaseUrl,
1346
+ databaseId: cfg.databaseId,
1347
+ defaultModel: cfg.defaultModel ?? DEFAULT_AI_MODEL
1332
1348
  };
1333
1349
  }
1334
1350
  registerStream(handle) {
@@ -1343,6 +1359,21 @@ var OnyxDatabaseImpl = class {
1343
1359
  }
1344
1360
  };
1345
1361
  }
1362
+ createAiFacade() {
1363
+ const chat = ((contentOrRequest, options) => {
1364
+ if (typeof contentOrRequest === "string") {
1365
+ return this.chatWithContent(contentOrRequest, options);
1366
+ }
1367
+ return this.getAiChatClient().create(contentOrRequest, options);
1368
+ });
1369
+ return {
1370
+ chat,
1371
+ chatClient: () => this.getAiChatClient(),
1372
+ getModels: () => this.getModels(),
1373
+ getModel: (modelId) => this.getModel(modelId),
1374
+ requestScriptApproval: (input) => this.requestScriptApproval(input)
1375
+ };
1376
+ }
1346
1377
  getRequestLoggingEnabled() {
1347
1378
  return this.requestLoggingEnabled;
1348
1379
  }
@@ -1350,7 +1381,7 @@ var OnyxDatabaseImpl = class {
1350
1381
  return this.responseLoggingEnabled;
1351
1382
  }
1352
1383
  /** -------- IOnyxDatabase -------- */
1353
- chat() {
1384
+ getAiChatClient() {
1354
1385
  return new AiChatClientImpl(
1355
1386
  () => this.ensureAiClient(),
1356
1387
  (handle) => this.registerStream(handle),
@@ -1358,6 +1389,32 @@ var OnyxDatabaseImpl = class {
1358
1389
  () => this.responseLoggingEnabled
1359
1390
  );
1360
1391
  }
1392
+ async chatWithContent(content, options) {
1393
+ const { defaultModel } = await this.ensureAiClient();
1394
+ const stream = options?.stream ?? false;
1395
+ const request = {
1396
+ model: options?.model ?? defaultModel,
1397
+ messages: [{ role: options?.role ?? "user", content }],
1398
+ stream
1399
+ };
1400
+ if (options && "temperature" in options) {
1401
+ request.temperature = options.temperature ?? null;
1402
+ }
1403
+ const result = await this.getAiChatClient().create(request, options);
1404
+ if (stream) return result;
1405
+ if (options?.raw) return result;
1406
+ const first = result.choices?.[0]?.message?.content;
1407
+ if (typeof first === "string" && first.trim().length > 0) {
1408
+ return first;
1409
+ }
1410
+ throw new Error("Chat completion response is missing message content");
1411
+ }
1412
+ chat(content, options) {
1413
+ if (typeof content === "string") {
1414
+ return this.chatWithContent(content, options);
1415
+ }
1416
+ return this.getAiChatClient();
1417
+ }
1361
1418
  async getModels() {
1362
1419
  const { http } = await this.ensureAiClient();
1363
1420
  return http.request("GET", "/v1/models");
@@ -2274,12 +2331,9 @@ var upper = (attribute) => `upper(${attribute})`;
2274
2331
  var lower = (attribute) => `lower(${attribute})`;
2275
2332
  var substring = (attribute, from, length) => `substring(${attribute},${from},${length})`;
2276
2333
  var replace = (attribute, pattern, repl) => `replace(${attribute}, '${pattern.replace(/'/g, "\\'")}', '${repl.replace(/'/g, "\\'")}')`;
2334
+ var format = (attribute, formatter) => `format(${attribute}, '${formatter.replace(/'/g, "\\'")}')`;
2277
2335
  var percentile = (attribute, p) => `percentile(${attribute}, ${p})`;
2278
2336
 
2279
- // src/index.ts
2280
- var sdkName = "@onyx.dev/onyx-database";
2281
- var sdkVersion = "0.1.0";
2282
-
2283
2337
  exports.QueryResults = QueryResults;
2284
2338
  exports.asc = asc;
2285
2339
  exports.avg = avg;
@@ -2289,6 +2343,7 @@ exports.containsIgnoreCase = containsIgnoreCase;
2289
2343
  exports.count = count;
2290
2344
  exports.desc = desc;
2291
2345
  exports.eq = eq;
2346
+ exports.format = format;
2292
2347
  exports.gt = gt;
2293
2348
  exports.gte = gte;
2294
2349
  exports.inOp = inOp;
@@ -2313,8 +2368,8 @@ exports.notWithin = notWithin;
2313
2368
  exports.onyx = onyx;
2314
2369
  exports.percentile = percentile;
2315
2370
  exports.replace = replace;
2316
- exports.sdkName = sdkName;
2317
- exports.sdkVersion = sdkVersion;
2371
+ exports.sdkName = name;
2372
+ exports.sdkVersion = version;
2318
2373
  exports.search = search;
2319
2374
  exports.startsWith = startsWith;
2320
2375
  exports.std = std;