@agentuity/postgres 0.1.41

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.
Files changed (48) hide show
  1. package/AGENTS.md +124 -0
  2. package/README.md +297 -0
  3. package/dist/client.d.ts +224 -0
  4. package/dist/client.d.ts.map +1 -0
  5. package/dist/client.js +670 -0
  6. package/dist/client.js.map +1 -0
  7. package/dist/errors.d.ts +109 -0
  8. package/dist/errors.d.ts.map +1 -0
  9. package/dist/errors.js +115 -0
  10. package/dist/errors.js.map +1 -0
  11. package/dist/index.d.ts +41 -0
  12. package/dist/index.d.ts.map +1 -0
  13. package/dist/index.js +47 -0
  14. package/dist/index.js.map +1 -0
  15. package/dist/patch.d.ts +65 -0
  16. package/dist/patch.d.ts.map +1 -0
  17. package/dist/patch.js +111 -0
  18. package/dist/patch.js.map +1 -0
  19. package/dist/postgres.d.ts +62 -0
  20. package/dist/postgres.d.ts.map +1 -0
  21. package/dist/postgres.js +63 -0
  22. package/dist/postgres.js.map +1 -0
  23. package/dist/reconnect.d.ts +31 -0
  24. package/dist/reconnect.d.ts.map +1 -0
  25. package/dist/reconnect.js +60 -0
  26. package/dist/reconnect.js.map +1 -0
  27. package/dist/registry.d.ts +71 -0
  28. package/dist/registry.d.ts.map +1 -0
  29. package/dist/registry.js +175 -0
  30. package/dist/registry.js.map +1 -0
  31. package/dist/transaction.d.ts +147 -0
  32. package/dist/transaction.d.ts.map +1 -0
  33. package/dist/transaction.js +287 -0
  34. package/dist/transaction.js.map +1 -0
  35. package/dist/types.d.ts +213 -0
  36. package/dist/types.d.ts.map +1 -0
  37. package/dist/types.js +2 -0
  38. package/dist/types.js.map +1 -0
  39. package/package.json +55 -0
  40. package/src/client.ts +776 -0
  41. package/src/errors.ts +154 -0
  42. package/src/index.ts +71 -0
  43. package/src/patch.ts +123 -0
  44. package/src/postgres.ts +65 -0
  45. package/src/reconnect.ts +74 -0
  46. package/src/registry.ts +194 -0
  47. package/src/transaction.ts +312 -0
  48. package/src/types.ts +250 -0
@@ -0,0 +1,287 @@
1
+ import { TransactionError } from './errors';
2
+ /**
3
+ * Represents a PostgreSQL transaction with support for savepoints.
4
+ *
5
+ * Transactions are created via `PostgresClient.begin()` and support
6
+ * tagged template literal syntax for queries.
7
+ */
8
+ export class Transaction {
9
+ _sql;
10
+ _connection;
11
+ _committed = false;
12
+ _rolledBack = false;
13
+ _savepointCounter = 0;
14
+ constructor(sql, connection) {
15
+ this._sql = sql;
16
+ this._connection = connection;
17
+ }
18
+ /**
19
+ * Whether the transaction has been committed.
20
+ */
21
+ get committed() {
22
+ return this._committed;
23
+ }
24
+ /**
25
+ * Whether the transaction has been rolled back.
26
+ */
27
+ get rolledBack() {
28
+ return this._rolledBack;
29
+ }
30
+ /**
31
+ * Whether the transaction is still active (not committed or rolled back).
32
+ */
33
+ get active() {
34
+ return !this._committed && !this._rolledBack;
35
+ }
36
+ /**
37
+ * Execute a query within this transaction using tagged template literal syntax.
38
+ *
39
+ * @example
40
+ * ```typescript
41
+ * const tx = await client.begin();
42
+ * const result = await tx`SELECT * FROM users WHERE id = ${userId}`;
43
+ * await tx.commit();
44
+ * ```
45
+ */
46
+ query(strings, ...values) {
47
+ this._ensureActive('query');
48
+ return this._sql(strings, ...values);
49
+ }
50
+ /**
51
+ * Create a savepoint within this transaction.
52
+ *
53
+ * @param name - Optional name for the savepoint. If not provided, a unique name is generated.
54
+ * If provided, must be a valid SQL identifier (alphanumeric and underscores only,
55
+ * starting with a letter or underscore).
56
+ * @returns A Savepoint object that can be used to rollback to this point.
57
+ * @throws {TransactionError} If the provided name is not a valid SQL identifier.
58
+ *
59
+ * @example
60
+ * ```typescript
61
+ * const tx = await client.begin();
62
+ * await tx`INSERT INTO users (name) VALUES ('Alice')`;
63
+ *
64
+ * const sp = await tx.savepoint();
65
+ * await tx`INSERT INTO users (name) VALUES ('Bob')`;
66
+ *
67
+ * // Oops, rollback Bob but keep Alice
68
+ * await sp.rollback();
69
+ *
70
+ * await tx.commit(); // Only Alice is committed
71
+ * ```
72
+ */
73
+ async savepoint(name) {
74
+ this._ensureActive('savepoint');
75
+ const savepointName = name ?? `sp_${++this._savepointCounter}`;
76
+ // Validate savepoint name to prevent SQL injection
77
+ // Must be a valid SQL identifier: starts with letter or underscore, contains only alphanumeric and underscores
78
+ const validIdentifierPattern = /^[A-Za-z_][A-Za-z0-9_]*$/;
79
+ if (!validIdentifierPattern.test(savepointName)) {
80
+ throw new TransactionError({
81
+ message: `Invalid savepoint name "${savepointName}": must be a valid SQL identifier (alphanumeric and underscores only, starting with a letter or underscore)`,
82
+ phase: 'savepoint',
83
+ });
84
+ }
85
+ try {
86
+ await this._sql `SAVEPOINT ${this._sql.unsafe(savepointName)}`;
87
+ return new Savepoint(this._sql, savepointName);
88
+ }
89
+ catch (error) {
90
+ throw new TransactionError({
91
+ message: `Failed to create savepoint: ${error instanceof Error ? error.message : String(error)}`,
92
+ phase: 'savepoint',
93
+ cause: error,
94
+ });
95
+ }
96
+ }
97
+ /**
98
+ * Commit the transaction.
99
+ *
100
+ * @throws {TransactionError} If the transaction is not active or commit fails.
101
+ */
102
+ async commit() {
103
+ this._ensureActive('commit');
104
+ try {
105
+ await this._sql `COMMIT`;
106
+ this._committed = true;
107
+ }
108
+ catch (error) {
109
+ throw new TransactionError({
110
+ message: `Failed to commit transaction: ${error instanceof Error ? error.message : String(error)}`,
111
+ phase: 'commit',
112
+ cause: error,
113
+ });
114
+ }
115
+ }
116
+ /**
117
+ * Rollback the transaction.
118
+ *
119
+ * @throws {TransactionError} If the transaction is not active or rollback fails.
120
+ */
121
+ async rollback() {
122
+ this._ensureActive('rollback');
123
+ try {
124
+ await this._sql `ROLLBACK`;
125
+ this._rolledBack = true;
126
+ }
127
+ catch (error) {
128
+ throw new TransactionError({
129
+ message: `Failed to rollback transaction: ${error instanceof Error ? error.message : String(error)}`,
130
+ phase: 'rollback',
131
+ cause: error,
132
+ });
133
+ }
134
+ }
135
+ /**
136
+ * Ensures the transaction is still active.
137
+ */
138
+ _ensureActive(operation) {
139
+ if (this._committed) {
140
+ throw new TransactionError({
141
+ message: `Cannot ${operation}: transaction has been committed`,
142
+ phase: operation,
143
+ });
144
+ }
145
+ if (this._rolledBack) {
146
+ throw new TransactionError({
147
+ message: `Cannot ${operation}: transaction has been rolled back`,
148
+ phase: operation,
149
+ });
150
+ }
151
+ }
152
+ }
153
+ /**
154
+ * Represents a savepoint within a transaction.
155
+ */
156
+ export class Savepoint {
157
+ _sql;
158
+ _name;
159
+ _released = false;
160
+ _rolledBack = false;
161
+ constructor(sql, name) {
162
+ this._sql = sql;
163
+ this._name = name;
164
+ }
165
+ /**
166
+ * The name of this savepoint.
167
+ */
168
+ get name() {
169
+ return this._name;
170
+ }
171
+ /**
172
+ * Whether the savepoint has been released.
173
+ */
174
+ get released() {
175
+ return this._released;
176
+ }
177
+ /**
178
+ * Whether the savepoint has been rolled back to.
179
+ */
180
+ get rolledBack() {
181
+ return this._rolledBack;
182
+ }
183
+ /**
184
+ * Rollback to this savepoint.
185
+ * All changes made after this savepoint was created will be undone.
186
+ *
187
+ * @throws {TransactionError} If the savepoint has been released or already rolled back.
188
+ */
189
+ async rollback() {
190
+ if (this._released) {
191
+ throw new TransactionError({
192
+ message: `Cannot rollback: savepoint "${this._name}" has been released`,
193
+ phase: 'savepoint',
194
+ });
195
+ }
196
+ if (this._rolledBack) {
197
+ throw new TransactionError({
198
+ message: `Cannot rollback: savepoint "${this._name}" has already been rolled back`,
199
+ phase: 'savepoint',
200
+ });
201
+ }
202
+ try {
203
+ await this._sql `ROLLBACK TO SAVEPOINT ${this._sql.unsafe(this._name)}`;
204
+ this._rolledBack = true;
205
+ }
206
+ catch (error) {
207
+ throw new TransactionError({
208
+ message: `Failed to rollback to savepoint: ${error instanceof Error ? error.message : String(error)}`,
209
+ phase: 'savepoint',
210
+ cause: error,
211
+ });
212
+ }
213
+ }
214
+ /**
215
+ * Release this savepoint.
216
+ * The savepoint is destroyed but changes are kept.
217
+ */
218
+ async release() {
219
+ if (this._released) {
220
+ return; // Already released, no-op
221
+ }
222
+ try {
223
+ await this._sql `RELEASE SAVEPOINT ${this._sql.unsafe(this._name)}`;
224
+ this._released = true;
225
+ }
226
+ catch (error) {
227
+ throw new TransactionError({
228
+ message: `Failed to release savepoint: ${error instanceof Error ? error.message : String(error)}`,
229
+ phase: 'savepoint',
230
+ cause: error,
231
+ });
232
+ }
233
+ }
234
+ }
235
+ /**
236
+ * Represents a reserved (exclusive) connection from the pool.
237
+ *
238
+ * Reserved connections are created via `PostgresClient.reserve()` and support
239
+ * tagged template literal syntax for queries.
240
+ */
241
+ export class ReservedConnection {
242
+ _sql;
243
+ _released = false;
244
+ constructor(sql) {
245
+ this._sql = sql;
246
+ }
247
+ /**
248
+ * Whether the connection has been released back to the pool.
249
+ */
250
+ get released() {
251
+ return this._released;
252
+ }
253
+ /**
254
+ * Execute a query on this reserved connection using tagged template literal syntax.
255
+ *
256
+ * @example
257
+ * ```typescript
258
+ * const conn = await client.reserve();
259
+ * try {
260
+ * await conn`SET LOCAL timezone = 'UTC'`;
261
+ * const result = await conn`SELECT NOW()`;
262
+ * } finally {
263
+ * conn.release();
264
+ * }
265
+ * ```
266
+ */
267
+ query(strings, ...values) {
268
+ if (this._released) {
269
+ throw new TransactionError({
270
+ message: 'Cannot query: connection has been released',
271
+ });
272
+ }
273
+ return this._sql(strings, ...values);
274
+ }
275
+ /**
276
+ * Release the connection back to the pool.
277
+ */
278
+ release() {
279
+ if (this._released) {
280
+ return; // Already released, no-op
281
+ }
282
+ this._released = true;
283
+ // The underlying SQL connection will be returned to the pool
284
+ // when it goes out of scope or is explicitly closed
285
+ }
286
+ }
287
+ //# sourceMappingURL=transaction.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transaction.js","sourceRoot":"","sources":["../src/transaction.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAE5C;;;;;GAKG;AACH,MAAM,OAAO,WAAW;I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AE;gBACjG,KAAK,EAAE,WAAW;gBAClB,KAAK,EAAE,KAAK;aACZ,CAAC,CAAC;QACJ,CAAC;IACF,CAAC;CACD;AAED;;;;;GAKG;AACH,MAAM,OAAO,kBAAkB;IACtB,IAAI,CAAM;IACV,SAAS,GAAG,KAAK,CAAC;IAE1B,YAAY,GAAQ;QACnB,IAAI,CAAC,IAAI,GAAG,GAAG,CAAC;IACjB,CAAC;IAED;;OAEG;IACH,IAAI,QAAQ;QACX,OAAO,IAAI,CAAC,SAAS,CAAC;IACvB,CAAC;IAED;;;;;;;;;;;;;OAaG;IACH,KAAK,CAAC,OAA6B,EAAE,GAAG,MAAiB;QACxD,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC;YACpB,MAAM,IAAI,gBAAgB,CAAC;gBAC1B,OAAO,EAAE,4CAA4C;aACrD,CAAC,CAAC;QACJ,CAAC;QACD,OAAO,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,MAAM,CAAC,CAAC;IACtC,CAAC;IAED;;OAEG;IACH,OAAO;QACN,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC;YACpB,OAAO,CAAC,0BAA0B;QACnC,CAAC;QACD,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;QACtB,6DAA6D;QAC7D,oDAAoD;IACrD,CAAC;CACD"}
@@ -0,0 +1,213 @@
1
+ /**
2
+ * TLS configuration options for PostgreSQL connections.
3
+ */
4
+ export interface TLSConfig {
5
+ /**
6
+ * Whether to require TLS for the connection.
7
+ * - `true`: Require TLS (fail if not available)
8
+ * - `false`: Disable TLS
9
+ * - `'prefer'`: Use TLS if available, fall back to unencrypted
10
+ */
11
+ require?: boolean | 'prefer';
12
+ /**
13
+ * Whether to reject unauthorized certificates.
14
+ * Set to `false` to allow self-signed certificates.
15
+ */
16
+ rejectUnauthorized?: boolean;
17
+ /**
18
+ * CA certificate(s) for verifying the server certificate.
19
+ */
20
+ ca?: string | Buffer | (string | Buffer)[];
21
+ /**
22
+ * Client certificate for mutual TLS authentication.
23
+ */
24
+ cert?: string | Buffer;
25
+ /**
26
+ * Client private key for mutual TLS authentication.
27
+ */
28
+ key?: string | Buffer;
29
+ }
30
+ /**
31
+ * Configuration for automatic reconnection behavior.
32
+ */
33
+ export interface ReconnectConfig {
34
+ /**
35
+ * Maximum number of reconnection attempts before giving up.
36
+ * @default 10
37
+ */
38
+ maxAttempts?: number;
39
+ /**
40
+ * Initial delay in milliseconds before the first reconnection attempt.
41
+ * @default 100
42
+ */
43
+ initialDelayMs?: number;
44
+ /**
45
+ * Maximum delay in milliseconds between reconnection attempts.
46
+ * @default 30000
47
+ */
48
+ maxDelayMs?: number;
49
+ /**
50
+ * Multiplier for exponential backoff.
51
+ * @default 2
52
+ */
53
+ multiplier?: number;
54
+ /**
55
+ * Maximum random jitter in milliseconds to add to the delay.
56
+ * Helps prevent thundering herd problems.
57
+ * @default 1000
58
+ */
59
+ jitterMs?: number;
60
+ /**
61
+ * Whether to automatically reconnect on connection loss.
62
+ * @default true
63
+ */
64
+ enabled?: boolean;
65
+ }
66
+ /**
67
+ * Statistics about the connection state and reconnection history.
68
+ */
69
+ export interface ConnectionStats {
70
+ /**
71
+ * Whether the client is currently connected.
72
+ */
73
+ connected: boolean;
74
+ /**
75
+ * Whether a reconnection attempt is in progress.
76
+ */
77
+ reconnecting: boolean;
78
+ /**
79
+ * Total number of successful connections (including reconnections).
80
+ */
81
+ totalConnections: number;
82
+ /**
83
+ * Total number of reconnection attempts.
84
+ */
85
+ reconnectAttempts: number;
86
+ /**
87
+ * Total number of failed reconnection attempts.
88
+ */
89
+ failedReconnects: number;
90
+ /**
91
+ * Timestamp of the last successful connection.
92
+ */
93
+ lastConnectedAt: Date | null;
94
+ /**
95
+ * Timestamp of the last disconnection.
96
+ */
97
+ lastDisconnectedAt: Date | null;
98
+ /**
99
+ * Timestamp of the last reconnection attempt.
100
+ */
101
+ lastReconnectAttemptAt: Date | null;
102
+ }
103
+ /**
104
+ * PostgreSQL connection configuration options.
105
+ * Extends Bun.SQL options with reconnection configuration.
106
+ */
107
+ export interface PostgresConfig {
108
+ /**
109
+ * PostgreSQL connection URL.
110
+ * If not provided, uses `process.env.DATABASE_URL`.
111
+ */
112
+ url?: string;
113
+ /**
114
+ * Database hostname.
115
+ */
116
+ hostname?: string;
117
+ /**
118
+ * Database port.
119
+ * @default 5432
120
+ */
121
+ port?: number;
122
+ /**
123
+ * Database username.
124
+ */
125
+ username?: string;
126
+ /**
127
+ * Database password.
128
+ */
129
+ password?: string;
130
+ /**
131
+ * Database name.
132
+ */
133
+ database?: string;
134
+ /**
135
+ * TLS configuration.
136
+ */
137
+ tls?: TLSConfig | boolean;
138
+ /**
139
+ * Maximum number of connections in the pool.
140
+ * @default 10
141
+ */
142
+ max?: number;
143
+ /**
144
+ * Connection timeout in milliseconds.
145
+ * @default 30000
146
+ */
147
+ connectionTimeout?: number;
148
+ /**
149
+ * Idle timeout in milliseconds.
150
+ * @default 0 (no timeout)
151
+ */
152
+ idleTimeout?: number;
153
+ /**
154
+ * Reconnection configuration.
155
+ */
156
+ reconnect?: ReconnectConfig;
157
+ /**
158
+ * Whether to establish a connection immediately on client creation.
159
+ * If true, the client will execute a test query (SELECT 1) to verify
160
+ * the connection is working. If false (default), the connection is
161
+ * established lazily on first query.
162
+ *
163
+ * Note: Even with preconnect=false, the underlying Bun.SQL client is
164
+ * created immediately, but the actual TCP connection is deferred.
165
+ *
166
+ * @default false
167
+ */
168
+ preconnect?: boolean;
169
+ /**
170
+ * Callback invoked when the connection is closed.
171
+ */
172
+ onclose?: (error?: Error) => void;
173
+ /**
174
+ * Callback invoked when reconnection starts.
175
+ */
176
+ onreconnect?: (attempt: number) => void;
177
+ /**
178
+ * Callback invoked when reconnection succeeds.
179
+ */
180
+ onreconnected?: () => void;
181
+ /**
182
+ * Callback invoked when reconnection fails permanently.
183
+ */
184
+ onreconnectfailed?: (error: Error) => void;
185
+ }
186
+ /**
187
+ * Options for creating a transaction.
188
+ */
189
+ export interface TransactionOptions {
190
+ /**
191
+ * Transaction isolation level.
192
+ */
193
+ isolationLevel?: 'read uncommitted' | 'read committed' | 'repeatable read' | 'serializable';
194
+ /**
195
+ * Whether the transaction is read-only.
196
+ */
197
+ readOnly?: boolean;
198
+ /**
199
+ * Whether the transaction is deferrable.
200
+ * Only applicable for serializable read-only transactions.
201
+ */
202
+ deferrable?: boolean;
203
+ }
204
+ /**
205
+ * Options for reserving a connection.
206
+ */
207
+ export interface ReserveOptions {
208
+ /**
209
+ * Timeout in milliseconds for acquiring a connection from the pool.
210
+ */
211
+ timeout?: number;
212
+ }
213
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,SAAS;IACzB;;;;;OAKG;IACH,OAAO,CAAC,EAAE,OAAO,GAAG,QAAQ,CAAC;IAE7B;;;OAGG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAE7B;;OAEG;IACH,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAAC;IAE3C;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IAEvB;;OAEG;IACH,GAAG,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;CACtB;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC/B;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IAErB;;;OAGG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;IAExB;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB;;;;OAIG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB;;;OAGG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,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EG;IACH,aAAa,CAAC,EAAE,MAAM,IAAI,CAAC;IAE3B;;OAEG;IACH,iBAAiB,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;CAC3C;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB;IAClC;;OAEG;IACH,cAAc,CAAC,EAAE,kBAAkB,GAAG,gBAAgB,GAAG,iBAAiB,GAAG,cAAc,CAAC;IAE5F;;OAEG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IAEnB;;;OAGG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;CACrB;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC9B;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;CACjB"}
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@agentuity/postgres",
3
+ "version": "0.1.41",
4
+ "license": "Apache-2.0",
5
+ "author": "Agentuity employees and contributors",
6
+ "type": "module",
7
+ "description": "Resilient PostgreSQL client with automatic reconnection for Agentuity projects",
8
+ "main": "./dist/index.js",
9
+ "module": "./dist/index.js",
10
+ "types": "./dist/index.d.ts",
11
+ "files": [
12
+ "AGENTS.md",
13
+ "README.md",
14
+ "src",
15
+ "dist"
16
+ ],
17
+ "exports": {
18
+ ".": "./dist/index.js"
19
+ },
20
+ "typesVersions": {
21
+ "*": {
22
+ ".": [
23
+ "./dist/index.d.ts"
24
+ ]
25
+ }
26
+ },
27
+ "scripts": {
28
+ "clean": "rm -rf dist tsconfig.tsbuildinfo",
29
+ "build": "bunx tsc --build --force",
30
+ "typecheck": "bunx tsc --noEmit",
31
+ "prepublishOnly": "bun run clean && bun run build"
32
+ },
33
+ "dependencies": {
34
+ "@agentuity/core": "0.1.41"
35
+ },
36
+ "peerDependencies": {
37
+ "@agentuity/runtime": "0.1.41"
38
+ },
39
+ "peerDependenciesMeta": {
40
+ "@agentuity/runtime": {
41
+ "optional": true
42
+ }
43
+ },
44
+ "devDependencies": {
45
+ "@agentuity/runtime": "0.1.41",
46
+ "@agentuity/test-utils": "0.1.41",
47
+ "@types/bun": "latest",
48
+ "bun-types": "latest",
49
+ "typescript": "^5.9.0"
50
+ },
51
+ "publishConfig": {
52
+ "access": "public"
53
+ },
54
+ "sideEffects": false
55
+ }