exoagent 0.0.1 → 0.0.2

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/sql.mjs ADDED
@@ -0,0 +1,553 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
5
+ var __typeError = (msg) => {
6
+ throw TypeError(msg);
7
+ };
8
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
10
+ var __decoratorStart = (base) => [, , , __create(base?.[__knownSymbol("metadata")] ?? null)];
11
+ var __decoratorStrings = ["class", "method", "getter", "setter", "accessor", "field", "value", "get", "set"];
12
+ var __expectFn = (fn) => fn !== void 0 && typeof fn !== "function" ? __typeError("Function expected") : fn;
13
+ var __decoratorContext = (kind, name, done, metadata, fns) => ({ kind: __decoratorStrings[kind], name, metadata, addInitializer: (fn) => done._ ? __typeError("Already initialized") : fns.push(__expectFn(fn || null)) });
14
+ var __decoratorMetadata = (array, target) => __defNormalProp(target, __knownSymbol("metadata"), array[3]);
15
+ var __runInitializers = (array, flags, self, value) => {
16
+ for (var i = 0, fns = array[flags >> 1], n = fns && fns.length; i < n; i++) flags & 1 ? fns[i].call(self) : value = fns[i].call(self, value);
17
+ return value;
18
+ };
19
+ var __decorateElement = (array, flags, name, decorators, target, extra) => {
20
+ var fn, it, done, ctx, access, k = flags & 7, s = !!(flags & 8), p = !!(flags & 16);
21
+ var j = k > 3 ? array.length + 1 : k ? s ? 1 : 2 : 0, key = __decoratorStrings[k + 5];
22
+ var initializers = k > 3 && (array[j - 1] = []), extraInitializers = array[j] || (array[j] = []);
23
+ var desc = k && (!p && !s && (target = target.prototype), k < 5 && (k > 3 || !p) && __getOwnPropDesc(k < 4 ? target : { get [name]() {
24
+ return __privateGet(this, extra);
25
+ }, set [name](x) {
26
+ return __privateSet(this, extra, x);
27
+ } }, name));
28
+ k ? p && k < 4 && __name(extra, (k > 2 ? "set " : k > 1 ? "get " : "") + name) : __name(target, name);
29
+ for (var i = decorators.length - 1; i >= 0; i--) {
30
+ ctx = __decoratorContext(k, name, done = {}, array[3], extraInitializers);
31
+ if (k) {
32
+ ctx.static = s, ctx.private = p, access = ctx.access = { has: p ? (x) => __privateIn(target, x) : (x) => name in x };
33
+ if (k ^ 3) access.get = p ? (x) => (k ^ 1 ? __privateGet : __privateMethod)(x, target, k ^ 4 ? extra : desc.get) : (x) => x[name];
34
+ if (k > 2) access.set = p ? (x, y) => __privateSet(x, target, y, k ^ 4 ? extra : desc.set) : (x, y) => x[name] = y;
35
+ }
36
+ it = (0, decorators[i])(k ? k < 4 ? p ? extra : desc[key] : k > 4 ? void 0 : { get: desc.get, set: desc.set } : target, ctx), done._ = 1;
37
+ if (k ^ 4 || it === void 0) __expectFn(it) && (k > 4 ? initializers.unshift(it) : k ? p ? extra = it : desc[key] = it : target = it);
38
+ else if (typeof it !== "object" || it === null) __typeError("Object expected");
39
+ else __expectFn(fn = it.get) && (desc.get = fn), __expectFn(fn = it.set) && (desc.set = fn), __expectFn(fn = it.init) && initializers.unshift(fn);
40
+ }
41
+ return k || __decoratorMetadata(array, target), desc && __defProp(target, name, desc), p ? k ^ 4 ? extra : desc : target;
42
+ };
43
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
44
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
45
+ var __privateIn = (member, obj) => Object(obj) !== obj ? __typeError('Cannot use the "in" operator on this value') : member.has(obj);
46
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
47
+ var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
48
+ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
49
+ var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
50
+ var _desc_dec, _asc_dec, ___dec, ___dec2, ___dec3, ___dec4, ___dec5, _NOT_LIKE_dec, _LIKE_dec, ____dec, ____dec2, ___dec6, ____dec3, ___dec7, ____dec4, ___dec8, _isNotNull_dec, _isNull_dec, _not_dec, _and_dec, _or_dec, _a, _init, _execute_dec, _join_dec, _offset_dec, _limit_dec, _orderBy_dec, _where_dec, _select_dec, _b, _db, _limit, _offset, _init2;
51
+ import { sql as sql$1, Kysely } from "kysely";
52
+ import invariant from "tiny-invariant";
53
+ import z from "zod";
54
+ import { R as RpcToolset, t as tool, s as setToolMetadata } from "./rpc-toolset-BnC2BXPq.js";
55
+ const sql = sql$1;
56
+ const buildSql = (parts, separator = sql$1` `) => {
57
+ return sql$1.join(parts.filter((x) => x != null), separator);
58
+ };
59
+ const asSqlExpression = (value) => {
60
+ return value instanceof SqlExpression ? value : new LiteralExpression(value);
61
+ };
62
+ const isSqlExpressionIn = (value) => {
63
+ return typeof value === "number" || typeof value === "string" || typeof value === "boolean" || value === null || value === void 0 || value instanceof SqlExpression;
64
+ };
65
+ const zSqlExpression = z.custom((val) => val instanceof SqlExpression);
66
+ const zNumericSqlExpression = z.union([zSqlExpression, z.number(), z.string()]);
67
+ const zSqlExpressionIn = z.union([zSqlExpression, z.number(), z.string(), z.boolean(), z.null()]);
68
+ class SqlExpression extends (_a = RpcToolset, _or_dec = [tool(zSqlExpression)], _and_dec = [tool(zSqlExpression)], _not_dec = [tool()], _isNull_dec = [tool()], _isNotNull_dec = [tool()], ___dec8 = [tool(zNumericSqlExpression)], ____dec4 = [tool(zNumericSqlExpression)], ___dec7 = [tool(zNumericSqlExpression)], ____dec3 = [tool(zNumericSqlExpression)], ___dec6 = [tool(zSqlExpressionIn)], ____dec2 = [tool(zSqlExpressionIn)], ____dec = [tool(zSqlExpressionIn)], _LIKE_dec = [tool(z.union([zSqlExpression, z.string()]))], _NOT_LIKE_dec = [tool(z.union([zSqlExpression, z.string()]))], ___dec5 = [tool(zNumericSqlExpression)], ___dec4 = [tool(zNumericSqlExpression)], ___dec3 = [tool(zNumericSqlExpression)], ___dec2 = [tool(zNumericSqlExpression)], ___dec = [tool(zNumericSqlExpression)], _asc_dec = [tool()], _desc_dec = [tool()], _a) {
69
+ constructor(precedence = 100) {
70
+ super();
71
+ __runInitializers(_init, 5, this);
72
+ // `= () => ` to ensure the method is a direct property of the class instance,
73
+ // not a method of the class prototype
74
+ __publicField(this, "compile", () => {
75
+ throw new Error("Not implemented");
76
+ });
77
+ this.precedence = precedence;
78
+ }
79
+ or(expr) {
80
+ return new BinaryExpression(this, expr, sql`OR`, 1);
81
+ }
82
+ and(expr) {
83
+ return new BinaryExpression(this, expr, sql`AND`, 2);
84
+ }
85
+ not() {
86
+ return new UnaryExpression(this, sql`NOT`, "prefix", 3);
87
+ }
88
+ isNull() {
89
+ return new UnaryExpression(this, sql`IS NULL`, "postfix", 4);
90
+ }
91
+ isNotNull() {
92
+ return new UnaryExpression(this, sql`IS NOT NULL`, "postfix", 4);
93
+ }
94
+ "<"(expr) {
95
+ return new BinaryExpression(this, asSqlExpression(expr), sql`<`, 5);
96
+ }
97
+ "<="(expr) {
98
+ return new BinaryExpression(this, asSqlExpression(expr), sql`<=`, 5);
99
+ }
100
+ ">"(expr) {
101
+ return new BinaryExpression(this, asSqlExpression(expr), sql`>`, 5);
102
+ }
103
+ ">="(expr) {
104
+ return new BinaryExpression(this, asSqlExpression(expr), sql`>=`, 5);
105
+ }
106
+ "="(expr) {
107
+ return new BinaryExpression(this, asSqlExpression(expr), sql`=`, 5);
108
+ }
109
+ "<>"(expr) {
110
+ return new BinaryExpression(this, asSqlExpression(expr), sql`<>`, 5);
111
+ }
112
+ "!="(expr) {
113
+ return new BinaryExpression(this, asSqlExpression(expr), sql`!=`, 5);
114
+ }
115
+ "LIKE"(expr) {
116
+ return new BinaryExpression(this, asSqlExpression(expr), sql`LIKE`, 6);
117
+ }
118
+ "NOT LIKE"(expr) {
119
+ return new BinaryExpression(this, asSqlExpression(expr), sql`NOT LIKE`, 6);
120
+ }
121
+ "+"(expr) {
122
+ return new BinaryExpression(this, asSqlExpression(expr), sql`+`, 8);
123
+ }
124
+ "-"(expr) {
125
+ return new BinaryExpression(this, asSqlExpression(expr), sql`-`, 8);
126
+ }
127
+ "*"(expr) {
128
+ return new BinaryExpression(this, asSqlExpression(expr), sql`*`, 9);
129
+ }
130
+ "/"(expr) {
131
+ return new BinaryExpression(this, asSqlExpression(expr), sql`/`, 9);
132
+ }
133
+ "%"(expr) {
134
+ return new BinaryExpression(this, asSqlExpression(expr), sql`%`, 9);
135
+ }
136
+ asc() {
137
+ return new OrderByValue(this, sql`ASC`);
138
+ }
139
+ desc() {
140
+ return new OrderByValue(this, sql`DESC`);
141
+ }
142
+ }
143
+ _init = __decoratorStart(_a);
144
+ __decorateElement(_init, 1, "or", _or_dec, SqlExpression);
145
+ __decorateElement(_init, 1, "and", _and_dec, SqlExpression);
146
+ __decorateElement(_init, 1, "not", _not_dec, SqlExpression);
147
+ __decorateElement(_init, 1, "isNull", _isNull_dec, SqlExpression);
148
+ __decorateElement(_init, 1, "isNotNull", _isNotNull_dec, SqlExpression);
149
+ __decorateElement(_init, 1, "<", ___dec8, SqlExpression);
150
+ __decorateElement(_init, 1, "<=", ____dec4, SqlExpression);
151
+ __decorateElement(_init, 1, ">", ___dec7, SqlExpression);
152
+ __decorateElement(_init, 1, ">=", ____dec3, SqlExpression);
153
+ __decorateElement(_init, 1, "=", ___dec6, SqlExpression);
154
+ __decorateElement(_init, 1, "<>", ____dec2, SqlExpression);
155
+ __decorateElement(_init, 1, "!=", ____dec, SqlExpression);
156
+ __decorateElement(_init, 1, "LIKE", _LIKE_dec, SqlExpression);
157
+ __decorateElement(_init, 1, "NOT LIKE", _NOT_LIKE_dec, SqlExpression);
158
+ __decorateElement(_init, 1, "+", ___dec5, SqlExpression);
159
+ __decorateElement(_init, 1, "-", ___dec4, SqlExpression);
160
+ __decorateElement(_init, 1, "*", ___dec3, SqlExpression);
161
+ __decorateElement(_init, 1, "/", ___dec2, SqlExpression);
162
+ __decorateElement(_init, 1, "%", ___dec, SqlExpression);
163
+ __decorateElement(_init, 1, "asc", _asc_dec, SqlExpression);
164
+ __decorateElement(_init, 1, "desc", _desc_dec, SqlExpression);
165
+ __decoratorMetadata(_init, SqlExpression);
166
+ class BinaryExpression extends SqlExpression {
167
+ constructor(left, right, operator, precedence) {
168
+ super(precedence ?? 100);
169
+ this.left = left;
170
+ this.right = right;
171
+ this.operator = operator;
172
+ }
173
+ // `= () => ` to ensure the method is a direct property of the class instance,
174
+ // not a method of the class prototype
175
+ compile = () => {
176
+ const leftCompiled = this.left.compile();
177
+ const rightCompiled = this.right.compile();
178
+ return buildSql([
179
+ this.precedence > this.left.precedence ? sql`(${leftCompiled})` : leftCompiled,
180
+ this.operator,
181
+ this.precedence > this.right.precedence ? sql`(${rightCompiled})` : rightCompiled
182
+ ]);
183
+ };
184
+ }
185
+ class UnaryExpression extends SqlExpression {
186
+ constructor(operand, operator, placement, precedence) {
187
+ super(precedence ?? 100);
188
+ this.operand = operand;
189
+ this.operator = operator;
190
+ this.placement = placement;
191
+ }
192
+ // `= () => ` to ensure the method is a direct property of the class instance,
193
+ // not a method of the class prototype
194
+ compile = () => {
195
+ const operandCompiled = this.operand.compile();
196
+ const wrappedOperand = this.precedence > this.operand.precedence ? sql`(${operandCompiled})` : operandCompiled;
197
+ return this.placement === "postfix" ? buildSql([wrappedOperand, this.operator]) : buildSql([this.operator, wrappedOperand]);
198
+ };
199
+ }
200
+ class ColumnReferenceExpression extends SqlExpression {
201
+ constructor(alias, column) {
202
+ super();
203
+ this.alias = alias;
204
+ this.column = column;
205
+ }
206
+ // `= () => ` to ensure the method is a direct property of the class instance,
207
+ // not a method of the class prototype
208
+ compile = () => {
209
+ return sql.ref(`${this.alias}.${this.column}`);
210
+ };
211
+ }
212
+ class UnboundColumnReferenceExpression extends ColumnReferenceExpression {
213
+ constructor(alias, column) {
214
+ super(alias, column);
215
+ this.alias = alias;
216
+ this.column = column;
217
+ }
218
+ // `= () => ` to ensure the method is a direct property of the class instance,
219
+ // not a method of the class prototype
220
+ compile = () => {
221
+ throw new Error("Unbound column reference cannot be compiled");
222
+ };
223
+ }
224
+ class LiteralExpression extends SqlExpression {
225
+ constructor(value) {
226
+ super();
227
+ this.value = value;
228
+ }
229
+ // `= () => ` to ensure the method is a direct property of the class instance,
230
+ // not a method of the class prototype
231
+ compile = () => {
232
+ return sql`${this.value}`;
233
+ };
234
+ }
235
+ class OrderByValue {
236
+ constructor(value, direction) {
237
+ this.value = value;
238
+ this.direction = direction;
239
+ }
240
+ }
241
+ const safeAliasRegex = /^[a-zA-Z_][a-zA-Z0-9_]*$/;
242
+ const isSafeAlias = (value) => {
243
+ return safeAliasRegex.test(value) && !(value === "constructor" || value === "prototype" || value === "__proto__") && !(value in Object.prototype) && value !== "toJSON";
244
+ };
245
+ const assertSafeAlias = (value) => {
246
+ if (!isSafeAlias(value)) {
247
+ throw new Error(`Alias must be a safe alias: "${value}" does not match the regex ${safeAliasRegex.source}`);
248
+ }
249
+ };
250
+ const asRowLike = (value) => {
251
+ if (value instanceof TableBase) {
252
+ return value;
253
+ }
254
+ return Object.fromEntries(Object.entries(value).map(([key, value2]) => [key, asSqlExpression(value2)]));
255
+ };
256
+ const isRowLikeRawIn = (value) => {
257
+ return typeof value === "object" && value !== null && (Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null) && Object.entries(value).every(([key, value2]) => {
258
+ assertSafeAlias(key);
259
+ return typeof key === "string" && isSqlExpressionIn(value2);
260
+ });
261
+ };
262
+ const isRowLikeIn = (value) => {
263
+ return isRowLikeRawIn(value) || value instanceof TableBase;
264
+ };
265
+ const rowLikeRawEntries = (value) => {
266
+ const entries = [];
267
+ const myKeys = Object.keys(value);
268
+ const protoKeys = [];
269
+ if (value instanceof TableBase) {
270
+ let proto = Object.getPrototypeOf(value);
271
+ while (proto != null && proto !== TableBase.prototype) {
272
+ protoKeys.push(...Object.keys(proto));
273
+ proto = Object.getPrototypeOf(proto);
274
+ }
275
+ }
276
+ for (const key of /* @__PURE__ */ new Set([...myKeys, ...protoKeys])) {
277
+ const v = value[key];
278
+ if (v instanceof SqlExpression) {
279
+ entries.push([key, v]);
280
+ }
281
+ }
282
+ return entries;
283
+ };
284
+ const isTableClass = (value) => {
285
+ return typeof value === "function" && value.prototype instanceof TableBase;
286
+ };
287
+ const isFromItem = (value) => {
288
+ return value instanceof QueryBuilder || isTableClass(value);
289
+ };
290
+ const combinePredicates = (...predicates) => {
291
+ const filtered = predicates.filter((p) => p != null);
292
+ if (filtered.length === 0) {
293
+ return void 0;
294
+ }
295
+ return filtered.reduce((acc, predicate) => acc.and(predicate));
296
+ };
297
+ const namespacedArg = (tables) => {
298
+ return Object.fromEntries(Object.entries(tables).map(([k, v]) => [k, v.fromItem.toRowLike()]));
299
+ };
300
+ const _QueryBuilder = class _QueryBuilder extends (_b = RpcToolset, _select_dec = [tool.callback()], _where_dec = [tool.callback()], _orderBy_dec = [tool.callback()], _limit_dec = [tool(z.number().int().nonnegative())], _offset_dec = [tool(z.number().int().nonnegative())], _join_dec = [tool.unsafeNoValidation()], _execute_dec = [tool()], _b) {
301
+ // set only when created directly from a TableClass
302
+ constructor(params) {
303
+ super();
304
+ __runInitializers(_init2, 5, this);
305
+ __privateAdd(this, _db);
306
+ __publicField(this, "alias");
307
+ __publicField(this, "selectRowLike");
308
+ __publicField(this, "tables");
309
+ __publicField(this, "whereExpression");
310
+ __publicField(this, "orderByExpressions");
311
+ __privateAdd(this, _limit);
312
+ __privateAdd(this, _offset);
313
+ __publicField(this, "arg");
314
+ __publicField(this, "rawTable");
315
+ // Note, we use `= () => ` to ensure the method is a direct property of the class instance,
316
+ // not a method of the class prototype
317
+ __publicField(this, "paramsForCopy", () => {
318
+ return {
319
+ alias: this.alias,
320
+ tables: this.tables,
321
+ selectRowLike: this.selectRowLike,
322
+ whereExpression: this.whereExpression,
323
+ orderByExpressions: this.orderByExpressions,
324
+ limit: __privateGet(this, _limit),
325
+ offset: __privateGet(this, _offset),
326
+ db: __privateGet(this, _db),
327
+ // `rawTable` is set only when created directly from a TableClass
328
+ rawTable: void 0
329
+ };
330
+ });
331
+ __publicField(this, "toRowLike", () => {
332
+ if (this.selectRowLike instanceof TableBase) {
333
+ const tableClass = this.selectRowLike.constructor;
334
+ return tableClass.as(this.alias).toRowLike({ remapColumns: true });
335
+ }
336
+ return Object.fromEntries(Object.keys(this.selectRowLike).map((key) => [key, new ColumnReferenceExpression(this.alias, key)]));
337
+ });
338
+ __publicField(this, "compile", (opts) => {
339
+ const { tables, selectRowLike, whereExpression, orderByExpressions } = this;
340
+ const tablesList = Object.entries(tables);
341
+ const [[_, baseTable], ...rest] = tablesList.filter(([alias, table2]) => {
342
+ invariant(table2.fromItem.alias === alias, `Alias mismatch: ${table2.fromItem.alias} !== ${alias}`);
343
+ return table2.joinType === null;
344
+ });
345
+ const joins = tablesList.filter(([, table2]) => table2.joinType !== null);
346
+ invariant(baseTable, "Base table not found");
347
+ invariant(rest.length === 0, "Implicit joins not supported");
348
+ invariant(Object.keys(selectRowLike).length > 0, "select must return a non-empty row");
349
+ const combinedWhere = combinePredicates(whereExpression, baseTable.fromItem.onExpression);
350
+ const result = buildSql([
351
+ sql`SELECT ${buildSql(rowLikeRawEntries(selectRowLike).map(([key, value]) => sql`${value.compile()} as ${sql.ref(key)}`), sql`, `)}`,
352
+ sql`FROM ${baseTable.fromItem.compile({ isSubquery: true })}`,
353
+ ...joins.map(([, table2]) => buildSql([table2.joinType === "inner" ? sql`JOIN` : table2.joinType === "left" ? sql`LEFT JOIN` : null, table2.isLateral ? sql`LATERAL` : null, table2.fromItem.compile({ isSubquery: true }), table2.on ? sql`ON ${table2.on.compile()}` : null]), sql` `),
354
+ combinedWhere ? sql`WHERE ${combinedWhere.compile()}` : null,
355
+ orderByExpressions && orderByExpressions.length > 0 ? sql`ORDER BY ${buildSql(orderByExpressions.map((expr) => buildSql([expr.value.compile(), expr.direction])), sql`, `)}` : null,
356
+ __privateGet(this, _limit) != null ? sql`LIMIT ${__privateGet(this, _limit)}` : null,
357
+ __privateGet(this, _offset) != null ? sql`OFFSET ${__privateGet(this, _offset)}` : null
358
+ ]);
359
+ return opts?.isSubquery ? sql`(${result}) AS ${sql.ref(this.alias)}` : result;
360
+ });
361
+ __privateSet(this, _db, params.db);
362
+ this.alias = params.alias;
363
+ this.selectRowLike = params.selectRowLike;
364
+ this.tables = params.tables;
365
+ this.whereExpression = params.whereExpression;
366
+ this.orderByExpressions = params.orderByExpressions;
367
+ this.arg = namespacedArg(params.tables);
368
+ __privateSet(this, _limit, params.limit);
369
+ __privateSet(this, _offset, params.offset);
370
+ this.rawTable = params.rawTable;
371
+ }
372
+ select(select) {
373
+ const selectUnwrapped = tool.unwrap(select, isRowLikeIn);
374
+ return selectUnwrapped(this.arg, (result) => {
375
+ return new _QueryBuilder({ ...this.paramsForCopy(), selectRowLike: asRowLike(result) });
376
+ });
377
+ }
378
+ where(where) {
379
+ const whereUnwrapped = tool.unwrap(where, isSqlExpressionIn);
380
+ return whereUnwrapped(this.arg, (result) => {
381
+ return new _QueryBuilder({ ...this.paramsForCopy(), whereExpression: combinePredicates(this.whereExpression, asSqlExpression(result)) });
382
+ });
383
+ }
384
+ orderBy(orderBy) {
385
+ const orderByUnwrapped = tool.unwrap(orderBy, (arg) => {
386
+ if (Array.isArray(arg)) {
387
+ return arg.every((e) => e instanceof SqlExpression || e instanceof OrderByValue);
388
+ }
389
+ return arg instanceof SqlExpression || arg instanceof OrderByValue;
390
+ });
391
+ return orderByUnwrapped(this.arg, (raw) => {
392
+ const rawArray = Array.isArray(raw) ? raw : [raw];
393
+ const exprs = rawArray.map((e) => {
394
+ invariant(e instanceof SqlExpression || e instanceof OrderByValue, "orderBy must return a SqlExpression/OrderByValue or an array of SqlExpressions/OrderByValues");
395
+ return e instanceof SqlExpression ? new OrderByValue(e) : e;
396
+ });
397
+ return new _QueryBuilder({ ...this.paramsForCopy(), orderByExpressions: (this.orderByExpressions ?? []).concat(exprs) });
398
+ });
399
+ }
400
+ limit(limit) {
401
+ invariant(typeof limit === "number" && limit >= 0, "limit must be greater than or equal to 0");
402
+ return new _QueryBuilder({ ...this.paramsForCopy(), limit: Math.min(limit, __privateGet(this, _limit) ?? Infinity) });
403
+ }
404
+ offset(offset) {
405
+ invariant(typeof offset === "number" && offset >= 0, "offset must be greater than or equal to 0");
406
+ return new _QueryBuilder({ ...this.paramsForCopy(), offset: (__privateGet(this, _offset) ?? 0) + offset });
407
+ }
408
+ join(fromItem, on) {
409
+ const fromItemCallbackRaw = isFromItem(fromItem) ? () => fromItem : fromItem;
410
+ const fromItemCallback = tool.unwrap(fromItemCallbackRaw, isFromItem);
411
+ const res = fromItemCallback(this.arg, (fromItemResolved) => {
412
+ if (fromItemResolved instanceof _QueryBuilder && fromItemResolved.rawTable) {
413
+ fromItemResolved = fromItemResolved.rawTable;
414
+ }
415
+ const alias = fromItemResolved.alias;
416
+ if (this.tables[alias]) {
417
+ throw new Error(`Join already exists: ${alias} in ${Object.keys(this.tables)}`);
418
+ }
419
+ const arg = {
420
+ ...this.arg,
421
+ [alias]: fromItemResolved.toRowLike()
422
+ };
423
+ const onCallback = tool.unwrap(on ?? (() => void 0), (raw) => raw === void 0 || isSqlExpressionIn(raw));
424
+ return onCallback(arg, (onRaw) => {
425
+ const onResolved = combinePredicates(onRaw !== void 0 ? asSqlExpression(onRaw) : void 0, fromItemResolved.onExpression);
426
+ invariant(onResolved != null, "Must specify an `on` expression or use `Table.on` to set the on expression");
427
+ invariant(onResolved instanceof SqlExpression, "on must return a SqlExpression");
428
+ const tablesWithAlias = {
429
+ ...this.tables,
430
+ [alias]: {
431
+ fromItem: fromItemResolved,
432
+ on: onResolved,
433
+ joinType: "inner",
434
+ // If `fromItem` is a function that returns a QueryBuilder, it is implicitly a lateral join (depends on the other tables)
435
+ isLateral: !isFromItem(fromItem) && fromItemResolved instanceof _QueryBuilder
436
+ }
437
+ };
438
+ return new _QueryBuilder({ ...this.paramsForCopy(), tables: tablesWithAlias });
439
+ });
440
+ });
441
+ return res;
442
+ }
443
+ async execute() {
444
+ return await __privateGet(this, _db).execute(this.compile());
445
+ }
446
+ };
447
+ _init2 = __decoratorStart(_b);
448
+ _db = new WeakMap();
449
+ _limit = new WeakMap();
450
+ _offset = new WeakMap();
451
+ __decorateElement(_init2, 1, "select", _select_dec, _QueryBuilder);
452
+ __decorateElement(_init2, 1, "where", _where_dec, _QueryBuilder);
453
+ __decorateElement(_init2, 1, "orderBy", _orderBy_dec, _QueryBuilder);
454
+ __decorateElement(_init2, 1, "limit", _limit_dec, _QueryBuilder);
455
+ __decorateElement(_init2, 1, "offset", _offset_dec, _QueryBuilder);
456
+ __decorateElement(_init2, 1, "join", _join_dec, _QueryBuilder);
457
+ __decorateElement(_init2, 1, "execute", _execute_dec, _QueryBuilder);
458
+ __decoratorMetadata(_init2, _QueryBuilder);
459
+ let QueryBuilder = _QueryBuilder;
460
+ class TableBase extends RpcToolset {
461
+ opts;
462
+ // `= () => ` to ensure the method is a direct property of the class instance,
463
+ // not a method of the class prototype
464
+ column = function(columnName) {
465
+ const cls = this.constructor;
466
+ return new UnboundColumnReferenceExpression(cls.alias ?? cls.tableName, columnName);
467
+ };
468
+ }
469
+ const table = (db, name) => {
470
+ const { [name]: tableClass } = { [name]: class extends TableBase {
471
+ static tableName = name;
472
+ static alias = name;
473
+ static onExpression;
474
+ constructor(opts) {
475
+ super();
476
+ this.opts = opts;
477
+ }
478
+ static from() {
479
+ const tables = { [this.alias]: { fromItem: this, joinType: null, isLateral: false } };
480
+ return new QueryBuilder({ db, alias: this.alias, tables, selectRowLike: this.toRowLike(), rawTable: this });
481
+ }
482
+ static as(alias) {
483
+ assertSafeAlias(alias);
484
+ class Ret extends this {
485
+ static alias = alias;
486
+ }
487
+ return Ret;
488
+ }
489
+ static on(on) {
490
+ const Base = this;
491
+ return class extends Base {
492
+ static onExpression = combinePredicates(Base.onExpression, on(Base.toRowLike()));
493
+ };
494
+ }
495
+ // use `function` to bind `this` to the current class instance
496
+ static toRowLike = function(opts) {
497
+ const rowLike = new this(opts);
498
+ for (const key of Object.keys(rowLike)) {
499
+ const value = rowLike[key];
500
+ if (value instanceof UnboundColumnReferenceExpression) {
501
+ const proto = Object.getPrototypeOf(rowLike);
502
+ const getter = function() {
503
+ const cls = this.constructor;
504
+ return new ColumnReferenceExpression(cls.alias ?? cls.tableName, this.opts?.remapColumns ? key : value.column);
505
+ };
506
+ setToolMetadata(getter, { runtimeValidationEnabled: true });
507
+ Object.defineProperty(proto, key, {
508
+ get: getter,
509
+ enumerable: true,
510
+ configurable: true
511
+ });
512
+ delete rowLike[key];
513
+ }
514
+ }
515
+ return rowLike;
516
+ };
517
+ static compile = function() {
518
+ return sql`${sql.ref(this.tableName)} AS ${sql.ref(this.alias ?? this.tableName)}`;
519
+ };
520
+ } };
521
+ return tableClass;
522
+ };
523
+ class Database {
524
+ constructor(dialect, opts) {
525
+ this.dialect = dialect;
526
+ this.opts = opts;
527
+ }
528
+ kysely;
529
+ async execute(query) {
530
+ if (this.kysely == null) {
531
+ this.kysely = new Kysely({ dialect: this.dialect });
532
+ }
533
+ if (this.opts?.logQuery) {
534
+ this.opts.logQuery(query.compile(this.kysely));
535
+ }
536
+ const result = await query.execute(this.kysely);
537
+ if (this.opts?.returnExecutedQuery) {
538
+ const compiled = query.compile(this.kysely);
539
+ return {
540
+ results: result.rows,
541
+ sql: compiled.sql,
542
+ parameters: compiled.parameters
543
+ };
544
+ }
545
+ return { results: result.rows };
546
+ }
547
+ Table(name) {
548
+ return table(this, name);
549
+ }
550
+ }
551
+ export {
552
+ Database
553
+ };
@@ -0,0 +1,11 @@
1
+ import { RpcTransport } from '../packages/capnweb/dist';
2
+ export declare class StreamTransport implements RpcTransport {
3
+ private input;
4
+ private output;
5
+ private bufferReader;
6
+ private writer;
7
+ constructor(input: ReadableStream<Uint8Array>, output: WritableStream<Uint8Array>);
8
+ send(message: string): Promise<void>;
9
+ receive(): Promise<string>;
10
+ abort(reason: unknown): void;
11
+ }
@@ -0,0 +1,16 @@
1
+ import { Tool, ToolExecutionOptions } from 'ai';
2
+ import { RpcToolset } from './rpc-toolset';
3
+ export type WrappableTools = {
4
+ [key: string]: Tool | (() => RpcToolset);
5
+ } | Tool[];
6
+ export declare function generateToolTypes(tools: WrappableTools, name: string): AsyncGenerator<string, void, unknown>;
7
+ export declare const generateToolApi: (tools: WrappableTools, opts: ToolExecutionOptions) => {
8
+ new (code: string): {
9
+ __return_value__: unknown;
10
+ __raw_code__: string;
11
+ __code__(): Promise<string>;
12
+ __return__(result: unknown): void;
13
+ __RPC_TARGET_BRAND: never;
14
+ };
15
+ };
16
+ export type ToolApi = InstanceType<ReturnType<typeof generateToolApi>>;
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "exoagent",
3
3
  "type": "module",
4
- "version": "0.0.1",
5
- "description": " The OS kernel to safely unleash your agents",
4
+ "version": "0.0.2",
5
+ "description": "The OS kernel to safely unleash your agents",
6
6
  "author": "Ryan Rasti <https://github.com/ryanrasti>",
7
7
  "license": "MIT",
8
8
  "homepage": "https://github.com/ryanrasti/exoagent#readme",
@@ -20,37 +20,49 @@
20
20
  ],
21
21
  "sideEffects": false,
22
22
  "exports": {
23
- ".": "./dist/index.mjs",
23
+ ".": {
24
+ "types": "./dist/index.d.ts",
25
+ "import": "./dist/index.mjs"
26
+ },
27
+ "./sql": {
28
+ "types": "./dist/sql.d.ts",
29
+ "import": "./dist/sql.mjs"
30
+ },
24
31
  "./package.json": "./package.json"
25
32
  },
26
33
  "main": "./dist/index.mjs",
27
34
  "module": "./dist/index.mjs",
28
- "types": "./dist/index.d.mts",
35
+ "types": "./dist/index.d.ts",
29
36
  "files": [
30
37
  "dist"
31
38
  ],
32
39
  "scripts": {
33
40
  "clean": "rm -rf dist",
34
- "build": "npm run clean && npm run build:dts && npm run build:main && npm run build:runtime-bundle && npm run build:test-deps",
35
- "build:main": "esbuild src/index.ts --bundle --format=esm --target=es2022 --outfile=dist/index.mjs --packages=external",
36
- "build:dts": "tsdown --dts-only",
37
- "build:runtime-bundle": "esbuild src/code-mode-runtime.ts --bundle --format=esm --target=es2022 --outfile=dist/code-mode-runtime.mjs --packages=external",
38
- "build:test-deps": "npm run build:runtime-bundle && tsdown --config tsdown.test-deps.config.ts",
41
+ "build": "npm run clean && npm run build:runtime && npm run build:main && npm run build:capnweb && npm run build:test-deps",
42
+ "build:main": "vite build",
43
+ "build:runtime": "esbuild src/code-mode-runtime.ts --bundle --format=esm --target=es2022 --outfile=dist/code-mode-runtime.mjs --packages=external",
44
+ "build:capnweb": "npm pack ./packages/capnweb --pack-destination dist && mkdir -p dist/capnweb && tar -xzf dist/capnweb-*.tgz --strip-components=2 -C dist/capnweb package/dist && rm dist/capnweb-*.tgz",
45
+ "build:test-deps": "tsdown --config tsdown.test-deps.config.ts",
39
46
  "dev": "tsdown --watch",
40
47
  "lint": "eslint",
41
48
  "prepublishOnly": "npm run build",
42
49
  "release": "bumpp",
43
50
  "start": "tsx src/index.ts",
51
+ "pretest": "npm run build:runtime",
44
52
  "test": "vitest --run",
45
53
  "typecheck": "tsc",
46
54
  "ci:capnweb": "cd packages/capnweb && npm ci && npm run build",
47
- "ci": "npm run ci:capnweb && npm ci && npm run lint && npm run typecheck && npm run build && npm run test"
55
+ "ci:examples": "npm --prefix ./examples run ci",
56
+ "ci": "npm run ci:capnweb && npm ci && npm run lint && npm run typecheck && npm run build && npm run test && npm run ci:examples"
57
+ },
58
+ "peerDependencies": {
59
+ "ai": "^6.0.0"
48
60
  },
49
61
  "dependencies": {
50
- "ai": "^6.0.3",
51
62
  "camelcase": "^9.0.0",
63
+ "capnweb": "file:packages/capnweb",
52
64
  "json-schema": "^0.4.0",
53
- "json-schema-to-typescript": "^15.0.4",
65
+ "json-schema-to-typescript": "^15.0.0",
54
66
  "kysely": "^0.28.9",
55
67
  "tiny-invariant": "^1.3.3",
56
68
  "zod": "^4.2.1",
@@ -74,6 +86,8 @@
74
86
  "tsx": "4.21.0",
75
87
  "typescript": "5.9.3",
76
88
  "vite": "7.2.7",
89
+ "vite-plugin-dts": "^4.5.4",
90
+ "vite-plugin-externalize-deps": "^0.10.0",
77
91
  "vitest": "4.0.15",
78
92
  "vitest-package-exports": "0.1.1",
79
93
  "yaml": "2.8.2"