ai-database 2.1.3 → 2.3.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.
Files changed (260) hide show
  1. package/CHANGELOG.md +35 -1
  2. package/README.md +880 -669
  3. package/dist/actions.d.ts +2 -2
  4. package/dist/actions.d.ts.map +1 -1
  5. package/dist/actions.js +1 -1
  6. package/dist/actions.js.map +1 -1
  7. package/dist/ai-promise-db.d.ts +49 -23
  8. package/dist/ai-promise-db.d.ts.map +1 -1
  9. package/dist/ai-promise-db.js +91 -63
  10. package/dist/ai-promise-db.js.map +1 -1
  11. package/dist/authorization.d.ts.map +1 -1
  12. package/dist/authorization.js +38 -30
  13. package/dist/authorization.js.map +1 -1
  14. package/dist/cascade-orchestrator.d.ts +404 -0
  15. package/dist/cascade-orchestrator.d.ts.map +1 -0
  16. package/dist/cascade-orchestrator.js +828 -0
  17. package/dist/cascade-orchestrator.js.map +1 -0
  18. package/dist/cascade-write-strategy.d.ts +584 -0
  19. package/dist/cascade-write-strategy.d.ts.map +1 -0
  20. package/dist/cascade-write-strategy.js +590 -0
  21. package/dist/cascade-write-strategy.js.map +1 -0
  22. package/dist/ch-adapter.d.ts +358 -0
  23. package/dist/ch-adapter.d.ts.map +1 -0
  24. package/dist/ch-adapter.js +929 -0
  25. package/dist/ch-adapter.js.map +1 -0
  26. package/dist/client/index.d.ts +42 -0
  27. package/dist/client/index.d.ts.map +1 -0
  28. package/dist/client/index.js +43 -0
  29. package/dist/client/index.js.map +1 -0
  30. package/dist/client.d.ts +266 -0
  31. package/dist/client.d.ts.map +1 -0
  32. package/dist/client.js +81 -0
  33. package/dist/client.js.map +1 -0
  34. package/dist/constants.d.ts +64 -1
  35. package/dist/constants.d.ts.map +1 -1
  36. package/dist/constants.js +52 -2
  37. package/dist/constants.js.map +1 -1
  38. package/dist/dataloader.d.ts +99 -0
  39. package/dist/dataloader.d.ts.map +1 -0
  40. package/dist/dataloader.js +225 -0
  41. package/dist/dataloader.js.map +1 -0
  42. package/dist/db-provider-port.d.ts +501 -0
  43. package/dist/db-provider-port.d.ts.map +1 -0
  44. package/dist/db-provider-port.js +113 -0
  45. package/dist/db-provider-port.js.map +1 -0
  46. package/dist/digital-objects-provider.d.ts +49 -0
  47. package/dist/digital-objects-provider.d.ts.map +1 -0
  48. package/dist/digital-objects-provider.js +55 -0
  49. package/dist/digital-objects-provider.js.map +1 -0
  50. package/dist/do-sqlite-adapter.d.ts +402 -0
  51. package/dist/do-sqlite-adapter.d.ts.map +1 -0
  52. package/dist/do-sqlite-adapter.js +745 -0
  53. package/dist/do-sqlite-adapter.js.map +1 -0
  54. package/dist/docs-rels/custom-types.d.ts +134 -0
  55. package/dist/docs-rels/custom-types.d.ts.map +1 -0
  56. package/dist/docs-rels/custom-types.js +70 -0
  57. package/dist/docs-rels/custom-types.js.map +1 -0
  58. package/dist/docs-rels/index.d.ts +16 -0
  59. package/dist/docs-rels/index.d.ts.map +1 -0
  60. package/dist/docs-rels/index.js +16 -0
  61. package/dist/docs-rels/index.js.map +1 -0
  62. package/dist/docs-rels/migrations/index.d.ts +30 -0
  63. package/dist/docs-rels/migrations/index.d.ts.map +1 -0
  64. package/dist/docs-rels/migrations/index.js +128 -0
  65. package/dist/docs-rels/migrations/index.js.map +1 -0
  66. package/dist/docs-rels/schema.d.ts +2961 -0
  67. package/dist/docs-rels/schema.d.ts.map +1 -0
  68. package/dist/docs-rels/schema.js +244 -0
  69. package/dist/docs-rels/schema.js.map +1 -0
  70. package/dist/durable-clickhouse.d.ts.map +1 -1
  71. package/dist/durable-clickhouse.js +16 -13
  72. package/dist/durable-clickhouse.js.map +1 -1
  73. package/dist/durable-promise.d.ts.map +1 -1
  74. package/dist/durable-promise.js +34 -15
  75. package/dist/durable-promise.js.map +1 -1
  76. package/dist/errors.d.ts +127 -0
  77. package/dist/errors.d.ts.map +1 -0
  78. package/dist/errors.js +210 -0
  79. package/dist/errors.js.map +1 -0
  80. package/dist/eventbridge.d.ts +117 -0
  81. package/dist/eventbridge.d.ts.map +1 -0
  82. package/dist/eventbridge.js +238 -0
  83. package/dist/eventbridge.js.map +1 -0
  84. package/dist/events.d.ts +2 -2
  85. package/dist/events.d.ts.map +1 -1
  86. package/dist/events.js +1 -1
  87. package/dist/events.js.map +1 -1
  88. package/dist/execution-queue.d.ts.map +1 -1
  89. package/dist/execution-queue.js +4 -5
  90. package/dist/execution-queue.js.map +1 -1
  91. package/dist/index.d.ts +35 -8
  92. package/dist/index.d.ts.map +1 -1
  93. package/dist/index.js +106 -6
  94. package/dist/index.js.map +1 -1
  95. package/dist/linguistic.d.ts +3 -108
  96. package/dist/linguistic.d.ts.map +1 -1
  97. package/dist/linguistic.js +3 -372
  98. package/dist/linguistic.js.map +1 -1
  99. package/dist/logger.d.ts +132 -0
  100. package/dist/logger.d.ts.map +1 -0
  101. package/dist/logger.js +137 -0
  102. package/dist/logger.js.map +1 -0
  103. package/dist/memory-provider.d.ts +128 -0
  104. package/dist/memory-provider.d.ts.map +1 -1
  105. package/dist/memory-provider.js +592 -257
  106. package/dist/memory-provider.js.map +1 -1
  107. package/dist/pg-adapter.d.ts +424 -0
  108. package/dist/pg-adapter.d.ts.map +1 -0
  109. package/dist/pg-adapter.js +921 -0
  110. package/dist/pg-adapter.js.map +1 -0
  111. package/dist/pipelines-iceberg-emitter.d.ts +327 -0
  112. package/dist/pipelines-iceberg-emitter.d.ts.map +1 -0
  113. package/dist/pipelines-iceberg-emitter.js +351 -0
  114. package/dist/pipelines-iceberg-emitter.js.map +1 -0
  115. package/dist/provider-capabilities.d.ts +146 -0
  116. package/dist/provider-capabilities.d.ts.map +1 -0
  117. package/dist/provider-capabilities.js +214 -0
  118. package/dist/provider-capabilities.js.map +1 -0
  119. package/dist/rdb-provider-adapter.d.ts +195 -0
  120. package/dist/rdb-provider-adapter.d.ts.map +1 -0
  121. package/dist/rdb-provider-adapter.js +291 -0
  122. package/dist/rdb-provider-adapter.js.map +1 -0
  123. package/dist/schema/cascade.d.ts +48 -17
  124. package/dist/schema/cascade.d.ts.map +1 -1
  125. package/dist/schema/cascade.js +477 -278
  126. package/dist/schema/cascade.js.map +1 -1
  127. package/dist/schema/definition-caches.d.ts +24 -0
  128. package/dist/schema/definition-caches.d.ts.map +1 -0
  129. package/dist/schema/definition-caches.js +26 -0
  130. package/dist/schema/definition-caches.js.map +1 -0
  131. package/dist/schema/dependency-graph.d.ts +21 -109
  132. package/dist/schema/dependency-graph.d.ts.map +1 -1
  133. package/dist/schema/dependency-graph.js +25 -333
  134. package/dist/schema/dependency-graph.js.map +1 -1
  135. package/dist/schema/diff.d.ts +103 -0
  136. package/dist/schema/diff.d.ts.map +1 -0
  137. package/dist/schema/diff.js +329 -0
  138. package/dist/schema/diff.js.map +1 -0
  139. package/dist/schema/entity-operations.d.ts +99 -0
  140. package/dist/schema/entity-operations.d.ts.map +1 -0
  141. package/dist/schema/entity-operations.js +818 -0
  142. package/dist/schema/entity-operations.js.map +1 -0
  143. package/dist/schema/index.d.ts +28 -34
  144. package/dist/schema/index.d.ts.map +1 -1
  145. package/dist/schema/index.js +454 -521
  146. package/dist/schema/index.js.map +1 -1
  147. package/dist/schema/migration.d.ts +205 -0
  148. package/dist/schema/migration.d.ts.map +1 -0
  149. package/dist/schema/migration.js +327 -0
  150. package/dist/schema/migration.js.map +1 -0
  151. package/dist/schema/nl-query-generator.d.ts +68 -0
  152. package/dist/schema/nl-query-generator.d.ts.map +1 -0
  153. package/dist/schema/nl-query-generator.js +362 -0
  154. package/dist/schema/nl-query-generator.js.map +1 -0
  155. package/dist/schema/nl-query.d.ts +65 -0
  156. package/dist/schema/nl-query.d.ts.map +1 -0
  157. package/dist/schema/nl-query.js +178 -0
  158. package/dist/schema/nl-query.js.map +1 -0
  159. package/dist/schema/parse.d.ts.map +1 -1
  160. package/dist/schema/parse.js +144 -89
  161. package/dist/schema/parse.js.map +1 -1
  162. package/dist/schema/provider.d.ts +37 -0
  163. package/dist/schema/provider.d.ts.map +1 -1
  164. package/dist/schema/provider.js +15 -7
  165. package/dist/schema/provider.js.map +1 -1
  166. package/dist/schema/resolve.d.ts +46 -5
  167. package/dist/schema/resolve.d.ts.map +1 -1
  168. package/dist/schema/resolve.js +237 -95
  169. package/dist/schema/resolve.js.map +1 -1
  170. package/dist/schema/search-utils.d.ts +76 -0
  171. package/dist/schema/search-utils.d.ts.map +1 -0
  172. package/dist/schema/search-utils.js +86 -0
  173. package/dist/schema/search-utils.js.map +1 -0
  174. package/dist/schema/seed.d.ts +53 -0
  175. package/dist/schema/seed.d.ts.map +1 -0
  176. package/dist/schema/seed.js +94 -0
  177. package/dist/schema/seed.js.map +1 -0
  178. package/dist/schema/semantic.d.ts +10 -0
  179. package/dist/schema/semantic.d.ts.map +1 -1
  180. package/dist/schema/semantic.js +192 -86
  181. package/dist/schema/semantic.js.map +1 -1
  182. package/dist/schema/sub-apis.d.ts +52 -0
  183. package/dist/schema/sub-apis.d.ts.map +1 -0
  184. package/dist/schema/sub-apis.js +216 -0
  185. package/dist/schema/sub-apis.js.map +1 -0
  186. package/dist/schema/system-entities.d.ts +42 -0
  187. package/dist/schema/system-entities.d.ts.map +1 -0
  188. package/dist/schema/system-entities.js +101 -0
  189. package/dist/schema/system-entities.js.map +1 -0
  190. package/dist/schema/types.d.ts +91 -9
  191. package/dist/schema/types.d.ts.map +1 -1
  192. package/dist/schema/union-fallback.d.ts.map +1 -1
  193. package/dist/schema/union-fallback.js +21 -15
  194. package/dist/schema/union-fallback.js.map +1 -1
  195. package/dist/schema/value-generators/ai.d.ts +54 -0
  196. package/dist/schema/value-generators/ai.d.ts.map +1 -0
  197. package/dist/schema/value-generators/ai.js +136 -0
  198. package/dist/schema/value-generators/ai.js.map +1 -0
  199. package/dist/schema/value-generators/index.d.ts +126 -0
  200. package/dist/schema/value-generators/index.d.ts.map +1 -0
  201. package/dist/schema/value-generators/index.js +219 -0
  202. package/dist/schema/value-generators/index.js.map +1 -0
  203. package/dist/schema/value-generators/placeholder.d.ts +52 -0
  204. package/dist/schema/value-generators/placeholder.d.ts.map +1 -0
  205. package/dist/schema/value-generators/placeholder.js +328 -0
  206. package/dist/schema/value-generators/placeholder.js.map +1 -0
  207. package/dist/schema/value-generators/types.d.ts +116 -0
  208. package/dist/schema/value-generators/types.d.ts.map +1 -0
  209. package/dist/schema/value-generators/types.js +11 -0
  210. package/dist/schema/value-generators/types.js.map +1 -0
  211. package/dist/schema/version.d.ts +111 -0
  212. package/dist/schema/version.d.ts.map +1 -0
  213. package/dist/schema/version.js +190 -0
  214. package/dist/schema/version.js.map +1 -0
  215. package/dist/schema.d.ts +1095 -24
  216. package/dist/schema.d.ts.map +1 -1
  217. package/dist/schema.js +2852 -40
  218. package/dist/schema.js.map +1 -1
  219. package/dist/semantic-vectors.d.ts +39 -0
  220. package/dist/semantic-vectors.d.ts.map +1 -0
  221. package/dist/semantic-vectors.js +334 -0
  222. package/dist/semantic-vectors.js.map +1 -0
  223. package/dist/semantic.d.ts +29 -1
  224. package/dist/semantic.d.ts.map +1 -1
  225. package/dist/semantic.js +26 -16
  226. package/dist/semantic.js.map +1 -1
  227. package/dist/telemetry.d.ts +128 -0
  228. package/dist/telemetry.d.ts.map +1 -0
  229. package/dist/telemetry.js +305 -0
  230. package/dist/telemetry.js.map +1 -0
  231. package/dist/tests.d.ts.map +1 -1
  232. package/dist/tests.js +30 -22
  233. package/dist/tests.js.map +1 -1
  234. package/dist/type-guards.d.ts +50 -5
  235. package/dist/type-guards.d.ts.map +1 -1
  236. package/dist/type-guards.js +87 -16
  237. package/dist/type-guards.js.map +1 -1
  238. package/dist/types.d.ts +33 -245
  239. package/dist/types.d.ts.map +1 -1
  240. package/dist/types.js +62 -72
  241. package/dist/types.js.map +1 -1
  242. package/dist/validation.d.ts +2 -5
  243. package/dist/validation.d.ts.map +1 -1
  244. package/dist/validation.js +65 -93
  245. package/dist/validation.js.map +1 -1
  246. package/dist/worker/db-provider.d.ts +168 -0
  247. package/dist/worker/db-provider.d.ts.map +1 -0
  248. package/dist/worker/db-provider.js +277 -0
  249. package/dist/worker/db-provider.js.map +1 -0
  250. package/dist/worker/index.d.ts +35 -0
  251. package/dist/worker/index.d.ts.map +1 -0
  252. package/dist/worker/index.js +37 -0
  253. package/dist/worker/index.js.map +1 -0
  254. package/dist/worker.d.ts +779 -0
  255. package/dist/worker.d.ts.map +1 -0
  256. package/dist/worker.js +2786 -0
  257. package/dist/worker.js.map +1 -0
  258. package/package.json +46 -16
  259. package/src/docs-rels/migrations/0001-init.sql +125 -0
  260. package/LICENSE +0 -21
@@ -0,0 +1,779 @@
1
+ /**
2
+ * Worker Export - WorkerEntrypoint for RPC access to AI Database
3
+ *
4
+ * Exposes database operations via Cloudflare RPC.
5
+ * Works both in Cloudflare Workers and standalone (with MemoryProvider).
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * // wrangler.jsonc
10
+ * {
11
+ * "services": [
12
+ * { "binding": "AI_DATABASE", "service": "ai-database" }
13
+ * ]
14
+ * }
15
+ *
16
+ * // worker.ts - consuming service
17
+ * export default {
18
+ * async fetch(request: Request, env: Env) {
19
+ * const db = env.AI_DATABASE.connect('my-namespace')
20
+ * const post = await db.create('Post', { title: 'Hello' })
21
+ * return Response.json(post)
22
+ * }
23
+ * }
24
+ * ```
25
+ *
26
+ * @packageDocumentation
27
+ */
28
+ /**
29
+ * ExecutionContext interface - matches @cloudflare/workers-types
30
+ */
31
+ export interface MockExecutionContext {
32
+ waitUntil(promise: Promise<unknown>): void;
33
+ passThroughOnException(): void;
34
+ }
35
+ /**
36
+ * SqlStorage interface - matches DurableObjectStorage.sql from @cloudflare/workers-types
37
+ */
38
+ export interface SqlStorage {
39
+ exec(query: string, ...params: unknown[]): SqlStorageResult;
40
+ }
41
+ /**
42
+ * SqlStorageResult interface
43
+ */
44
+ export interface SqlStorageResult {
45
+ toArray(): unknown[];
46
+ }
47
+ /**
48
+ * DurableObjectStorage interface - subset needed for our use
49
+ */
50
+ export interface MockDurableObjectStorage {
51
+ sql: SqlStorage;
52
+ }
53
+ /**
54
+ * DurableObjectId interface
55
+ */
56
+ export interface MockDurableObjectId {
57
+ toString(): string;
58
+ equals(other: MockDurableObjectId): boolean;
59
+ readonly name?: string;
60
+ }
61
+ /**
62
+ * DurableObjectState interface - matches @cloudflare/workers-types
63
+ */
64
+ export interface MockDurableObjectState {
65
+ waitUntil(promise: Promise<unknown>): void;
66
+ readonly id: MockDurableObjectId;
67
+ readonly storage: MockDurableObjectStorage;
68
+ }
69
+ /**
70
+ * Database row interfaces for type-safe SQL operations
71
+ */
72
+ export interface DataRow {
73
+ id: string;
74
+ type: string;
75
+ data: string;
76
+ created_at: string;
77
+ updated_at: string;
78
+ }
79
+ export interface RelRow {
80
+ from_id: string;
81
+ relation: string;
82
+ to_id: string;
83
+ metadata: string | null;
84
+ created_at: string;
85
+ }
86
+ export interface EventRow {
87
+ id: string;
88
+ event: string;
89
+ actor: string | null;
90
+ object: string | null;
91
+ data: string | null;
92
+ result: string | null;
93
+ previous_data: string | null;
94
+ timestamp: string;
95
+ }
96
+ export interface EmbeddingRow {
97
+ id: string;
98
+ entity_type: string;
99
+ entity_id: string;
100
+ model: string;
101
+ vector: string;
102
+ content_hash: string | null;
103
+ created_at: string;
104
+ updated_at: string;
105
+ }
106
+ export interface SubscriptionRow {
107
+ id: string;
108
+ pattern: string;
109
+ webhook: string;
110
+ created_at: string;
111
+ }
112
+ export interface MetaRow {
113
+ key: string;
114
+ value: string;
115
+ }
116
+ export interface CountRow {
117
+ count: number;
118
+ }
119
+ /**
120
+ * Environment with AI binding for Cloudflare AI
121
+ */
122
+ export interface EnvWithAI {
123
+ AI?: {
124
+ run(model: string, options: {
125
+ text: string[];
126
+ }): Promise<{
127
+ data: number[][];
128
+ }>;
129
+ };
130
+ }
131
+ /**
132
+ * Deserialized entity types
133
+ */
134
+ export interface DeserializedDataRow {
135
+ id: string;
136
+ type: string;
137
+ data: Record<string, unknown>;
138
+ created_at: string;
139
+ updated_at: string;
140
+ }
141
+ export interface DeserializedRelRow {
142
+ from_id: string;
143
+ relation: string;
144
+ to_id: string;
145
+ metadata: Record<string, unknown> | null;
146
+ created_at: string;
147
+ }
148
+ export interface DeserializedEventRow {
149
+ id: string;
150
+ event: string;
151
+ actor: string | null;
152
+ object: string | null;
153
+ data: unknown;
154
+ result: string | null;
155
+ previousData: unknown;
156
+ timestamp: string;
157
+ }
158
+ /**
159
+ * Event handler type
160
+ */
161
+ export type DatabaseEventHandler = (event: DeserializedEventRow) => void | Promise<void>;
162
+ /**
163
+ * Action status type
164
+ */
165
+ export type ActionStatus = 'pending' | 'in_progress' | 'completed' | 'failed';
166
+ /**
167
+ * Action interface
168
+ */
169
+ export interface Action {
170
+ id: string;
171
+ action: string;
172
+ actor: string;
173
+ object?: string;
174
+ status: ActionStatus;
175
+ progress?: number;
176
+ total?: number;
177
+ data?: unknown;
178
+ result?: unknown;
179
+ error?: string;
180
+ createdAt: string;
181
+ updatedAt: string;
182
+ }
183
+ /**
184
+ * Artifact interface
185
+ */
186
+ export interface Artifact {
187
+ url: string;
188
+ type: string;
189
+ content: unknown;
190
+ sourceHash: string;
191
+ metadata?: Record<string, unknown>;
192
+ createdAt: string;
193
+ updatedAt: string;
194
+ }
195
+ /**
196
+ * Request body types
197
+ */
198
+ export interface CreateDataBody {
199
+ id?: string;
200
+ type: string;
201
+ data: Record<string, unknown>;
202
+ }
203
+ export interface UpdateDataBody {
204
+ data: Record<string, unknown>;
205
+ }
206
+ export interface CreateRelBody {
207
+ from_id: string;
208
+ relation: string;
209
+ to_id: string;
210
+ metadata?: Record<string, unknown>;
211
+ }
212
+ export interface CreateEventBody {
213
+ event: string;
214
+ actor?: string;
215
+ object?: string;
216
+ data?: unknown;
217
+ }
218
+ export interface SearchBody {
219
+ type?: string;
220
+ query: string;
221
+ limit?: number;
222
+ fields?: string[];
223
+ minScore?: number;
224
+ }
225
+ export interface SemanticSearchBody {
226
+ type: string;
227
+ query: string;
228
+ limit?: number;
229
+ threshold?: number;
230
+ }
231
+ export interface HybridSearchBody {
232
+ type: string;
233
+ query: string;
234
+ limit?: number;
235
+ ftsWeight?: number;
236
+ semanticWeight?: number;
237
+ }
238
+ export interface TraverseFilterBody {
239
+ from_id: string;
240
+ relation: string;
241
+ direction?: 'outgoing' | 'incoming';
242
+ maxDepth?: number;
243
+ filter?: Record<string, unknown>;
244
+ }
245
+ export interface ReplayEventsBody {
246
+ object: string;
247
+ until?: string;
248
+ }
249
+ export interface SubscribeBody {
250
+ pattern: string;
251
+ webhook: string;
252
+ }
253
+ export interface EmbeddingsConfigBody {
254
+ model?: string;
255
+ }
256
+ export interface EmbeddingsGenerateBody {
257
+ type: string;
258
+ }
259
+ export interface EmbeddingsBatchBody {
260
+ type: string;
261
+ batchSize?: number;
262
+ }
263
+ export interface UpdateRelMetadataBody {
264
+ from_id: string;
265
+ relation: string;
266
+ to_id: string;
267
+ metadata: Record<string, unknown>;
268
+ }
269
+ export interface QueryListBody {
270
+ type: string;
271
+ where?: Record<string, unknown>;
272
+ orderBy?: string;
273
+ order?: 'asc' | 'desc';
274
+ limit?: number;
275
+ offset?: number;
276
+ }
277
+ export interface QueryFindBody {
278
+ type: string;
279
+ where?: Record<string, unknown>;
280
+ }
281
+ export interface PipelineConfigBody {
282
+ retryEnabled?: boolean;
283
+ batchSize?: number;
284
+ }
285
+ export interface PipelineTestErrorBody {
286
+ simulateError?: boolean;
287
+ }
288
+ declare class MockRpcTarget {
289
+ }
290
+ declare class MockWorkerEntrypoint<T = unknown> {
291
+ protected env: T;
292
+ protected ctx: MockExecutionContext;
293
+ }
294
+ declare class MockDurableObject {
295
+ protected ctx: MockDurableObjectState;
296
+ protected env: unknown;
297
+ constructor(_state: MockDurableObjectState, _env: unknown);
298
+ }
299
+ declare let WorkerEntrypoint: typeof MockWorkerEntrypoint;
300
+ declare let RpcTarget: typeof MockRpcTarget;
301
+ declare let DurableObjectBase: typeof MockDurableObject;
302
+ import type { MemoryProviderOptions, EmbeddingProvider } from './memory-provider.js';
303
+ import type { ListOptions, SearchOptions, SemanticSearchOptions, HybridSearchOptions, EmbeddingsConfig } from './schema.js';
304
+ /**
305
+ * Environment bindings for the worker
306
+ */
307
+ export interface Env {
308
+ }
309
+ /**
310
+ * Entity result type with standard fields
311
+ */
312
+ export interface EntityResult {
313
+ $id: string;
314
+ $type: string;
315
+ [key: string]: unknown;
316
+ }
317
+ /**
318
+ * Search result with score
319
+ */
320
+ export interface SearchResult extends EntityResult {
321
+ $score?: number;
322
+ }
323
+ /**
324
+ * Semantic search result with scores
325
+ */
326
+ export interface SemanticSearchResult extends EntityResult {
327
+ $score: number;
328
+ }
329
+ /**
330
+ * Hybrid search result with RRF and component scores
331
+ */
332
+ export interface HybridSearchResult extends SemanticSearchResult {
333
+ $rrfScore: number;
334
+ $ftsRank: number;
335
+ $semanticRank: number;
336
+ }
337
+ /**
338
+ * DatabaseServiceCore - RpcTarget wrapper around MemoryProvider
339
+ *
340
+ * Exposes all required methods as RPC-callable methods.
341
+ * This is the core service class that can be instantiated directly.
342
+ */
343
+ export declare class DatabaseServiceCore extends RpcTarget {
344
+ private provider;
345
+ constructor(namespace?: string, options?: MemoryProviderOptions);
346
+ /**
347
+ * Set embeddings configuration for auto-generation
348
+ */
349
+ setEmbeddingsConfig(config: EmbeddingsConfig): void;
350
+ /**
351
+ * Enable or disable ai-functions for embeddings
352
+ */
353
+ setUseAiFunctions(enabled: boolean): void;
354
+ /**
355
+ * Set a custom embedding provider
356
+ */
357
+ setEmbeddingProvider(provider: EmbeddingProvider | undefined): void;
358
+ /**
359
+ * Get an entity by type and ID
360
+ */
361
+ get(type: string, id: string): Promise<EntityResult | null>;
362
+ /**
363
+ * List entities by type
364
+ */
365
+ list(type: string, options?: ListOptions): Promise<EntityResult[]>;
366
+ /**
367
+ * Create an entity
368
+ */
369
+ create(type: string, data: Record<string, unknown>, id?: string): Promise<EntityResult>;
370
+ /**
371
+ * Update an entity
372
+ */
373
+ update(type: string, id: string, data: Record<string, unknown>): Promise<EntityResult>;
374
+ /**
375
+ * Delete an entity
376
+ */
377
+ delete(type: string, id: string): Promise<boolean>;
378
+ /**
379
+ * Full-text search
380
+ */
381
+ search(type: string, query: string, options?: SearchOptions): Promise<SearchResult[]>;
382
+ /**
383
+ * Semantic search using vector similarity
384
+ */
385
+ semanticSearch(type: string, query: string, options?: SemanticSearchOptions): Promise<SemanticSearchResult[]>;
386
+ /**
387
+ * Hybrid search combining FTS and semantic
388
+ */
389
+ hybridSearch(type: string, query: string, options?: HybridSearchOptions): Promise<HybridSearchResult[]>;
390
+ /**
391
+ * Get related entities
392
+ */
393
+ related(type: string, id: string, relation: string): Promise<EntityResult[]>;
394
+ /**
395
+ * Create a relationship between two entities
396
+ */
397
+ relate(fromType: string, fromId: string, relation: string, toType: string, toId: string, metadata?: {
398
+ matchMode?: 'exact' | 'fuzzy';
399
+ similarity?: number;
400
+ matchedType?: string;
401
+ }): Promise<void>;
402
+ /**
403
+ * Remove a relationship between two entities
404
+ */
405
+ unrelate(fromType: string, fromId: string, relation: string, toType: string, toId: string): Promise<void>;
406
+ /**
407
+ * Subscribe to events matching a pattern
408
+ * @returns Unsubscribe function ID (use unsubscribe() to remove)
409
+ */
410
+ on(pattern: string, handler: DatabaseEventHandler): string;
411
+ private _eventHandlers;
412
+ /**
413
+ * Unsubscribe from events
414
+ */
415
+ unsubscribe(handlerId: string): void;
416
+ /**
417
+ * Emit an event
418
+ */
419
+ emit(eventOrOptions: string | {
420
+ event: string;
421
+ actor: string;
422
+ object?: string;
423
+ data?: unknown;
424
+ }, data?: unknown): Promise<DeserializedEventRow | null>;
425
+ /**
426
+ * List events
427
+ */
428
+ listEvents(options?: {
429
+ event?: string;
430
+ actor?: string;
431
+ object?: string;
432
+ since?: Date;
433
+ until?: Date;
434
+ limit?: number;
435
+ }): Promise<DeserializedEventRow[]>;
436
+ /**
437
+ * Create a new action
438
+ */
439
+ createAction(options: {
440
+ action: string;
441
+ actor: string;
442
+ object?: string;
443
+ data?: unknown;
444
+ total?: number;
445
+ }): Promise<Action | null>;
446
+ /**
447
+ * Get an action by ID
448
+ */
449
+ getAction(id: string): Promise<Action | null>;
450
+ /**
451
+ * Update an action
452
+ */
453
+ updateAction(id: string, updates: {
454
+ status?: string;
455
+ progress?: number;
456
+ result?: unknown;
457
+ error?: string;
458
+ }): Promise<Action | null>;
459
+ /**
460
+ * List actions
461
+ */
462
+ listActions(options?: {
463
+ status?: string;
464
+ action?: string;
465
+ actor?: string;
466
+ object?: string;
467
+ since?: Date;
468
+ until?: Date;
469
+ limit?: number;
470
+ }): Promise<Action[]>;
471
+ /**
472
+ * Get an artifact
473
+ */
474
+ getArtifact(url: string, type: string): Promise<Artifact | null>;
475
+ /**
476
+ * Set an artifact
477
+ */
478
+ setArtifact(url: string, type: string, data: {
479
+ content: unknown;
480
+ sourceHash: string;
481
+ metadata?: Record<string, unknown>;
482
+ }): Promise<void>;
483
+ /**
484
+ * Delete an artifact
485
+ */
486
+ deleteArtifact(url: string, type?: string): Promise<void>;
487
+ /**
488
+ * List artifacts for a URL
489
+ */
490
+ listArtifacts(url: string): Promise<Artifact[]>;
491
+ /**
492
+ * Clear all data in the provider (useful for testing)
493
+ */
494
+ clear(): void;
495
+ }
496
+ /**
497
+ * DatabaseDO - Durable Object using SQLite for the core schema layer.
498
+ *
499
+ * Provides two tables:
500
+ * - `_data`: id TEXT PRIMARY KEY, type TEXT, data TEXT (JSON), created_at TEXT, updated_at TEXT
501
+ * - `_rels`: from_id TEXT, relation TEXT, to_id TEXT, metadata TEXT (JSON),
502
+ * PRIMARY KEY(from_id, relation, to_id)
503
+ *
504
+ * Handles HTTP requests for CRUD operations on data records and relationships,
505
+ * plus graph traversal queries.
506
+ */
507
+ export declare class DatabaseDO extends DurableObjectBase {
508
+ private sql;
509
+ private initialized;
510
+ private pipelineBuffer;
511
+ private pipelineConfig;
512
+ private pipelineStats;
513
+ private embeddingsConfig;
514
+ private embeddingsCacheStats;
515
+ private batchJobs;
516
+ constructor(state: MockDurableObjectState, env: unknown);
517
+ private ensureSchema;
518
+ fetch(request: Request): Promise<Response>;
519
+ private handleListData;
520
+ private handleInsertData;
521
+ private handleGetData;
522
+ private handleUpdateData;
523
+ private handleDeleteData;
524
+ /**
525
+ * Cascade delete related entities up to a given depth
526
+ */
527
+ private cascadeDelete;
528
+ private handleQueryRels;
529
+ private handleCreateRel;
530
+ private handleDeleteRel;
531
+ private handleTraverse;
532
+ /**
533
+ * Handle bidirectional traversal (both incoming and outgoing)
534
+ */
535
+ private handleBidirectionalTraverse;
536
+ /**
537
+ * Handle forward (outgoing) traversal with depth control
538
+ */
539
+ private handleForwardTraverse;
540
+ /**
541
+ * Handle reverse (incoming) traversal
542
+ */
543
+ private handleReverseTraverse;
544
+ /**
545
+ * Fetch entities by IDs with optional type filter and metadata
546
+ */
547
+ private fetchEntitiesByIds;
548
+ /**
549
+ * Handle POST /traverse/filter - filter traversal by metadata
550
+ */
551
+ private handleTraverseFilter;
552
+ /**
553
+ * Check if metadata matches a filter with operator support
554
+ */
555
+ private matchesMetadataFilter;
556
+ /**
557
+ * Handle PATCH /rels - update relationship metadata
558
+ */
559
+ private handleUpdateRel;
560
+ private handleGetIndexes;
561
+ private handleGetVersion;
562
+ /**
563
+ * Validate a field name to prevent SQL injection.
564
+ * Only allows alphanumeric characters, underscores, and dots for nested fields.
565
+ */
566
+ private isValidFieldName;
567
+ /**
568
+ * Build SQL WHERE clause from a where object.
569
+ * Returns [clause, params] tuple where clause includes the WHERE keyword.
570
+ */
571
+ /**
572
+ * Convert a value for SQLite JSON comparison.
573
+ * Booleans need to be converted to 1/0 because SQLite stores JSON booleans as integers.
574
+ */
575
+ private toSqliteValue;
576
+ private buildWhereClause;
577
+ /**
578
+ * Build ORDER BY clause from orderBy and order parameters.
579
+ */
580
+ private buildOrderByClause;
581
+ /**
582
+ * Handle GET /query/list - simple query via URL params
583
+ */
584
+ private handleQueryList;
585
+ /**
586
+ * Handle POST /query/list - complex query via JSON body
587
+ */
588
+ private handleQueryListPost;
589
+ /**
590
+ * Handle POST /query/find - find first matching record
591
+ */
592
+ private handleQueryFind;
593
+ /**
594
+ * Escape special characters in LIKE patterns
595
+ */
596
+ private escapeLikePattern;
597
+ /**
598
+ * Calculate a simple relevance score based on term matches
599
+ */
600
+ private calculateRelevanceScore;
601
+ /**
602
+ * Handle POST /query/search - full-text search
603
+ */
604
+ private handleQuerySearch;
605
+ /**
606
+ * Emit an event to the _events table and pipeline
607
+ */
608
+ private emitEvent;
609
+ /**
610
+ * Handle GET /events - list events with filtering
611
+ */
612
+ private handleListEvents;
613
+ /**
614
+ * Handle POST /events - create custom event
615
+ */
616
+ private handleCreateEvent;
617
+ /**
618
+ * Handle POST /events/replay - replay events
619
+ */
620
+ private handleReplayEvents;
621
+ /**
622
+ * Handle POST /events/rebuild - rebuild entity from events
623
+ */
624
+ private handleRebuildEntity;
625
+ /**
626
+ * Handle POST /events/subscribe - create subscription
627
+ */
628
+ private handleSubscribe;
629
+ /**
630
+ * Handle GET /events/subscriptions - list subscriptions
631
+ */
632
+ private handleListSubscriptions;
633
+ /**
634
+ * Handle DELETE /events/subscriptions/:id - unsubscribe
635
+ */
636
+ private handleUnsubscribe;
637
+ /**
638
+ * Handle GET /events/subscriptions/:id/deliveries - list deliveries
639
+ */
640
+ private handleListDeliveries;
641
+ /**
642
+ * Handle GET /pipeline/status - get pipeline status
643
+ */
644
+ private handlePipelineStatus;
645
+ /**
646
+ * Handle POST /pipeline/flush - flush pipeline buffer to R2
647
+ */
648
+ private handlePipelineFlush;
649
+ /**
650
+ * Flush the pipeline buffer to R2 storage
651
+ */
652
+ private flushPipeline;
653
+ /**
654
+ * Handle GET /pipeline/r2/list - list R2 objects
655
+ */
656
+ private handlePipelineR2List;
657
+ /**
658
+ * Handle POST /pipeline/config - configure pipeline
659
+ */
660
+ private handlePipelineConfig;
661
+ /**
662
+ * Handle POST /pipeline/test-error - test error handling
663
+ */
664
+ private handlePipelineTestError;
665
+ /**
666
+ * Deserialize an event row from the database
667
+ */
668
+ private deserializeEventRow;
669
+ private deserializeDataRow;
670
+ private deserializeRelRow;
671
+ /**
672
+ * Configure embeddings model
673
+ */
674
+ private handleConfigureEmbeddings;
675
+ /**
676
+ * List all embeddings with optional type filter
677
+ * Generates embeddings lazily for entities that don't have them yet
678
+ */
679
+ private handleListEmbeddings;
680
+ /**
681
+ * Get embedding cache stats
682
+ */
683
+ private handleEmbeddingsStats;
684
+ /**
685
+ * Warm up embedding cache for a type
686
+ */
687
+ private handleEmbeddingsWarmup;
688
+ /**
689
+ * Generate embeddings for all entities of a type
690
+ */
691
+ private handleEmbeddingsGenerate;
692
+ /**
693
+ * Batch process embeddings for specific entities
694
+ */
695
+ private handleEmbeddingsBatch;
696
+ /**
697
+ * Start a batch embedding job
698
+ */
699
+ private handleEmbeddingsBatchStart;
700
+ /**
701
+ * Process a batch job asynchronously
702
+ */
703
+ private processBatchJob;
704
+ /**
705
+ * Get batch job status
706
+ */
707
+ private handleEmbeddingsBatchStatus;
708
+ /**
709
+ * Get embedding for a specific entity
710
+ */
711
+ private handleGetEmbedding;
712
+ /**
713
+ * Semantic search using vector similarity
714
+ */
715
+ private handleSemanticSearch;
716
+ /**
717
+ * Hybrid search combining FTS and semantic
718
+ */
719
+ private handleHybridSearch;
720
+ /**
721
+ * Generate embedding for an entity and store it
722
+ */
723
+ private generateEmbeddingForEntity;
724
+ /**
725
+ * Generate embedding vector for text using AI binding or fallback
726
+ */
727
+ private generateEmbedding;
728
+ /**
729
+ * Generate a deterministic embedding based on text content
730
+ * This is used as a fallback when no AI binding is available
731
+ */
732
+ private generateDeterministicEmbedding;
733
+ /**
734
+ * Extract text content from entity data for embedding
735
+ */
736
+ private extractEmbeddableText;
737
+ /**
738
+ * Calculate cosine similarity between two vectors
739
+ */
740
+ private cosineSimilarity;
741
+ /**
742
+ * Simple hash function for deterministic randomness
743
+ */
744
+ private simpleHash;
745
+ /**
746
+ * Generate deterministic pseudo-random number from seed
747
+ */
748
+ private seededRandom;
749
+ /**
750
+ * Hash content for change detection
751
+ */
752
+ private hashContent;
753
+ }
754
+ /**
755
+ * DatabaseService - WorkerEntrypoint for RPC access
756
+ *
757
+ * Provides `connect(namespace)` method that returns an RpcTarget service
758
+ * with all database operations.
759
+ *
760
+ * When used standalone (in tests), uses an in-memory provider with namespace isolation.
761
+ */
762
+ export declare class DatabaseService extends WorkerEntrypoint<Env> {
763
+ /**
764
+ * Connect to a namespace and get an RPC-enabled service
765
+ *
766
+ * @param namespace - The namespace to connect to (defaults to 'default')
767
+ * @param options - Optional provider configuration
768
+ * @returns DatabaseServiceCore instance for RPC calls
769
+ */
770
+ connect(namespace?: string, options?: MemoryProviderOptions): DatabaseServiceCore;
771
+ /**
772
+ * Handle fetch requests - required by vitest-pool-workers for service binding tests
773
+ * Returns a simple JSON response for health checks or routes to the appropriate service
774
+ */
775
+ fetch(request: Request): Promise<Response>;
776
+ }
777
+ export default DatabaseService;
778
+ export { DatabaseService as DatabaseWorker };
779
+ //# sourceMappingURL=worker.d.ts.map