ai-database 2.1.1 → 2.1.3

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 (63) hide show
  1. package/CHANGELOG.md +13 -1
  2. package/LICENSE +21 -0
  3. package/README.md +667 -1
  4. package/dist/ai-promise-db.d.ts +3 -0
  5. package/dist/ai-promise-db.d.ts.map +1 -1
  6. package/dist/ai-promise-db.js +98 -105
  7. package/dist/ai-promise-db.js.map +1 -1
  8. package/dist/index.d.ts +3 -1
  9. package/dist/index.d.ts.map +1 -1
  10. package/dist/index.js +7 -1
  11. package/dist/index.js.map +1 -1
  12. package/dist/memory-provider.d.ts +1 -0
  13. package/dist/memory-provider.d.ts.map +1 -1
  14. package/dist/memory-provider.js.map +1 -1
  15. package/dist/schema/cascade.d.ts +8 -0
  16. package/dist/schema/cascade.d.ts.map +1 -1
  17. package/dist/schema/cascade.js +25 -6
  18. package/dist/schema/cascade.js.map +1 -1
  19. package/dist/schema/dependency-graph.d.ts +133 -0
  20. package/dist/schema/dependency-graph.d.ts.map +1 -0
  21. package/dist/schema/dependency-graph.js +355 -0
  22. package/dist/schema/dependency-graph.js.map +1 -0
  23. package/dist/schema/generation-context.d.ts +202 -0
  24. package/dist/schema/generation-context.d.ts.map +1 -0
  25. package/dist/schema/generation-context.js +393 -0
  26. package/dist/schema/generation-context.js.map +1 -0
  27. package/dist/schema/index.d.ts +4 -0
  28. package/dist/schema/index.d.ts.map +1 -1
  29. package/dist/schema/index.js +11 -1
  30. package/dist/schema/index.js.map +1 -1
  31. package/dist/schema/parse.d.ts.map +1 -1
  32. package/dist/schema/parse.js +9 -1
  33. package/dist/schema/parse.js.map +1 -1
  34. package/dist/schema/provider.d.ts +1 -0
  35. package/dist/schema/provider.d.ts.map +1 -1
  36. package/dist/schema/resolve.d.ts.map +1 -1
  37. package/dist/schema/resolve.js +103 -28
  38. package/dist/schema/resolve.js.map +1 -1
  39. package/dist/schema/semantic.d.ts +1 -0
  40. package/dist/schema/semantic.d.ts.map +1 -1
  41. package/dist/schema/semantic.js +144 -56
  42. package/dist/schema/semantic.js.map +1 -1
  43. package/dist/schema/union-fallback.d.ts +219 -0
  44. package/dist/schema/union-fallback.d.ts.map +1 -0
  45. package/dist/schema/union-fallback.js +325 -0
  46. package/dist/schema/union-fallback.js.map +1 -0
  47. package/dist/schema/verb-derivation.d.ts +167 -0
  48. package/dist/schema/verb-derivation.d.ts.map +1 -0
  49. package/dist/schema/verb-derivation.js +281 -0
  50. package/dist/schema/verb-derivation.js.map +1 -0
  51. package/dist/schema.d.ts +1 -0
  52. package/dist/schema.d.ts.map +1 -1
  53. package/dist/schema.js +4 -0
  54. package/dist/schema.js.map +1 -1
  55. package/dist/type-guards.d.ts +167 -0
  56. package/dist/type-guards.d.ts.map +1 -0
  57. package/dist/type-guards.js +247 -0
  58. package/dist/type-guards.js.map +1 -0
  59. package/dist/validation.d.ts +168 -0
  60. package/dist/validation.d.ts.map +1 -0
  61. package/dist/validation.js +667 -0
  62. package/dist/validation.js.map +1 -0
  63. package/package.json +13 -13
@@ -0,0 +1 @@
1
+ {"version":3,"file":"union-fallback.d.ts","sourceRoot":"","sources":["../../src/schema/union-fallback.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,8CAA8C;IAC9C,GAAG,EAAE,MAAM,CAAA;IACX,6BAA6B;IAC7B,MAAM,EAAE,MAAM,CAAA;IACd,4BAA4B;IAC5B,KAAK,EAAE,MAAM,CAAA;IACb,4BAA4B;IAC5B,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CACvB;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,iCAAiC;IACjC,IAAI,EAAE,MAAM,CAAA;IACZ,oBAAoB;IACpB,OAAO,EAAE,MAAM,CAAA;IACf,qBAAqB;IACrB,KAAK,CAAC,EAAE,KAAK,CAAA;CACd;AAED;;GAEG;AACH,MAAM,WAA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;;;;;;;GAsBG;AACH,wBAAgB,eAAe,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,EAAE,CAoB1D;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,oBAAoB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAmB7E;AAwCD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,wBAAsB,gBAAgB,CACpC,KAAK,EAAE,SAAS,MAAM,EAAE,EACxB,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,qBAAqB,GAC7B,OAAO,CAAC,iBAAiB,CAAC,CAuB5B;AAsJD;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,sBAAsB,CACpC,QAAQ,EAAE;IACR,cAAc,EAAE,CACd,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,EACb,OAAO,CAAC,EAAE;QAAE,QAAQ,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,KAC5C,OAAO,CAAC,KAAK,CAAC;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAC;QAAC,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;KAAE,CAAC,CAAC,CAAA;CAC7E,GACA,aAAa,CAYf"}
@@ -0,0 +1,325 @@
1
+ /**
2
+ * Union Type Fallback Search
3
+ *
4
+ * Implements fallback search behavior for union types in backward fuzzy (<~) operators.
5
+ * When searching `<~Type1|Type2|Type3`, searches types in order and falls back to
6
+ * the next type if no match is found or threshold not met.
7
+ *
8
+ * Supports two search modes:
9
+ * - `ordered`: Search types sequentially, stop on first match (default)
10
+ * - `parallel`: Search all types concurrently, return best match
11
+ *
12
+ * @packageDocumentation
13
+ */
14
+ /**
15
+ * Parse union type string into array of individual types
16
+ *
17
+ * Handles various formats:
18
+ * - `Type1|Type2|Type3` - Standard pipe-separated union
19
+ * - `Type1 | Type2 | Type3` - With spaces around pipes
20
+ * - `Type1|Type2(0.8)|Type3` - With threshold syntax (stripped from result)
21
+ *
22
+ * @param typeSpec - The union type specification string
23
+ * @returns Array of individual type names in declaration order
24
+ *
25
+ * @example
26
+ * ```ts
27
+ * parseUnionTypes('Document|Video|Expert')
28
+ * // => ['Document', 'Video', 'Expert']
29
+ *
30
+ * parseUnionTypes('Type1 | Type2(0.8) | Type3')
31
+ * // => ['Type1', 'Type2', 'Type3']
32
+ *
33
+ * parseUnionTypes('SingleType')
34
+ * // => ['SingleType']
35
+ * ```
36
+ */
37
+ export function parseUnionTypes(typeSpec) {
38
+ if (!typeSpec || typeSpec.trim() === '') {
39
+ return [];
40
+ }
41
+ // Split by pipe and process each type
42
+ const types = typeSpec.split('|').map((t) => {
43
+ let type = t.trim();
44
+ // Strip threshold syntax: Type(0.8) -> Type
45
+ const thresholdMatch = type.match(/^([A-Za-z][A-Za-z0-9_]*)\s*\([^)]*\)$/);
46
+ if (thresholdMatch) {
47
+ type = thresholdMatch[1];
48
+ }
49
+ return type;
50
+ });
51
+ // Filter out empty strings (from cases like "Type1||Type2")
52
+ return types.filter((t) => t.length > 0);
53
+ }
54
+ /**
55
+ * Parse per-type thresholds from union type specification
56
+ *
57
+ * Extracts threshold values from syntax like `Type1|Type2(0.8)|Type3(0.6)`
58
+ *
59
+ * @param typeSpec - The union type specification string
60
+ * @returns Record mapping type names to their thresholds
61
+ *
62
+ * @example
63
+ * ```ts
64
+ * parseUnionThresholds('Type1|Type2(0.8)|Type3(0.6)')
65
+ * // => { Type2: 0.8, Type3: 0.6 }
66
+ * ```
67
+ */
68
+ export function parseUnionThresholds(typeSpec) {
69
+ const thresholds = {};
70
+ if (!typeSpec)
71
+ return thresholds;
72
+ const parts = typeSpec.split('|');
73
+ for (const part of parts) {
74
+ const match = part.trim().match(/^([A-Za-z][A-Za-z0-9_]*)\s*\(([0-9.]+)\)$/);
75
+ if (match) {
76
+ const [, typeName, thresholdStr] = match;
77
+ const threshold = parseFloat(thresholdStr);
78
+ if (!isNaN(threshold) && threshold >= 0 && threshold <= 1) {
79
+ thresholds[typeName] = threshold;
80
+ }
81
+ }
82
+ }
83
+ return thresholds;
84
+ }
85
+ /**
86
+ * Get the effective threshold for a type
87
+ *
88
+ * @param type - The type name
89
+ * @param options - Search options containing global and per-type thresholds
90
+ * @returns The threshold to use for this type
91
+ */
92
+ function getThresholdForType(type, options) {
93
+ // Per-type threshold takes precedence
94
+ if (options.thresholds && type in options.thresholds) {
95
+ return options.thresholds[type];
96
+ }
97
+ // Fall back to global threshold, default to 0 (accept all)
98
+ return options.threshold ?? 0;
99
+ }
100
+ /**
101
+ * Filter matches by threshold
102
+ */
103
+ function filterByThreshold(matches, threshold, includeBelowThreshold) {
104
+ const above = [];
105
+ const below = [];
106
+ for (const match of matches) {
107
+ if (match.$score >= threshold) {
108
+ above.push(match);
109
+ }
110
+ else if (includeBelowThreshold) {
111
+ below.push(match);
112
+ }
113
+ }
114
+ return { above, below };
115
+ }
116
+ /**
117
+ * Search union types with fallback behavior
118
+ *
119
+ * Searches multiple types either sequentially (ordered) or concurrently (parallel),
120
+ * with support for per-type thresholds and graceful error handling.
121
+ *
122
+ * @param types - Array of type names to search (in priority order)
123
+ * @param query - The search query
124
+ * @param options - Search options including mode, thresholds, and searcher
125
+ * @returns Search result with matches and metadata
126
+ *
127
+ * @example Ordered mode (stops on first match)
128
+ * ```ts
129
+ * const result = await searchUnionTypes(
130
+ * ['Document', 'Video', 'Expert'],
131
+ * 'machine learning tutorial',
132
+ * {
133
+ * mode: 'ordered',
134
+ * threshold: 0.75,
135
+ * searcher: async (type, query) => {
136
+ * return provider.semanticSearch(type, query)
137
+ * }
138
+ * }
139
+ * )
140
+ * ```
141
+ *
142
+ * @example Parallel mode (searches all, returns best)
143
+ * ```ts
144
+ * const result = await searchUnionTypes(
145
+ * ['Document', 'Video', 'Expert'],
146
+ * 'machine learning tutorial',
147
+ * {
148
+ * mode: 'parallel',
149
+ * returnAll: true,
150
+ * searcher: async (type, query) => {
151
+ * return provider.semanticSearch(type, query)
152
+ * }
153
+ * }
154
+ * )
155
+ * ```
156
+ */
157
+ export async function searchUnionTypes(types, query, options) {
158
+ // Initialize result
159
+ const result = {
160
+ matches: [],
161
+ searchedTypes: [],
162
+ searchOrder: [],
163
+ fallbackTriggered: false,
164
+ allTypesExhausted: false,
165
+ belowThresholdMatches: options.includeBelowThreshold ? [] : undefined,
166
+ errors: [],
167
+ };
168
+ // Handle empty types array
169
+ if (!types || types.length === 0) {
170
+ result.allTypesExhausted = true;
171
+ return result;
172
+ }
173
+ if (options.mode === 'parallel') {
174
+ return searchParallel(types, query, options, result);
175
+ }
176
+ else {
177
+ return searchOrdered(types, query, options, result);
178
+ }
179
+ }
180
+ /**
181
+ * Search types in order, stopping on first match
182
+ */
183
+ async function searchOrdered(types, query, options, result) {
184
+ const { searcher, includeBelowThreshold, onError = 'throw' } = options;
185
+ for (let i = 0; i < types.length; i++) {
186
+ const type = types[i];
187
+ const threshold = getThresholdForType(type, options);
188
+ result.searchedTypes.push(type);
189
+ result.searchOrder.push(type);
190
+ if (i > 0) {
191
+ result.fallbackTriggered = true;
192
+ }
193
+ try {
194
+ const matches = await searcher(type, query, {
195
+ threshold,
196
+ limit: options.limit,
197
+ });
198
+ // Filter by threshold
199
+ const { above, below } = filterByThreshold(matches, threshold, includeBelowThreshold);
200
+ // Collect below-threshold matches for debugging
201
+ if (includeBelowThreshold && below.length > 0) {
202
+ result.belowThresholdMatches.push(...below);
203
+ }
204
+ // If we found matches above threshold, stop searching
205
+ if (above.length > 0) {
206
+ result.matches = above;
207
+ result.matchedType = type;
208
+ result.confidence = Math.max(...above.map((m) => m.$score));
209
+ return result;
210
+ }
211
+ }
212
+ catch (error) {
213
+ if (onError === 'throw') {
214
+ throw error;
215
+ }
216
+ // Continue mode: record error and try next type
217
+ result.errors.push({
218
+ type,
219
+ message: error instanceof Error ? error.message : String(error),
220
+ error: error instanceof Error ? error : undefined,
221
+ });
222
+ }
223
+ }
224
+ // No matches found in any type
225
+ result.allTypesExhausted = true;
226
+ return result;
227
+ }
228
+ /**
229
+ * Search all types in parallel, return best match(es)
230
+ */
231
+ async function searchParallel(types, query, options, result) {
232
+ const { searcher, returnAll = false, includeBelowThreshold, onError = 'continue' } = options;
233
+ // Mark all types as searched
234
+ result.searchedTypes = [...types];
235
+ result.searchOrder = [...types];
236
+ // Search all types in parallel
237
+ const searchPromises = types.map(async (type) => {
238
+ const threshold = getThresholdForType(type, options);
239
+ try {
240
+ const matches = await searcher(type, query, {
241
+ threshold,
242
+ limit: options.limit,
243
+ });
244
+ return { type, matches, error: null };
245
+ }
246
+ catch (error) {
247
+ return {
248
+ type,
249
+ matches: [],
250
+ error: error instanceof Error ? error : new Error(String(error)),
251
+ };
252
+ }
253
+ });
254
+ const results = await Promise.all(searchPromises);
255
+ // Collect all matches and errors
256
+ const allMatches = [];
257
+ const allBelowThreshold = [];
258
+ for (const { type, matches, error } of results) {
259
+ if (error) {
260
+ if (onError === 'throw') {
261
+ throw error;
262
+ }
263
+ result.errors.push({
264
+ type,
265
+ message: error.message,
266
+ error,
267
+ });
268
+ continue;
269
+ }
270
+ const threshold = getThresholdForType(type, options);
271
+ const { above, below } = filterByThreshold(matches, threshold, includeBelowThreshold);
272
+ allMatches.push(...above);
273
+ if (includeBelowThreshold) {
274
+ allBelowThreshold.push(...below);
275
+ }
276
+ }
277
+ // Sort by score descending
278
+ allMatches.sort((a, b) => b.$score - a.$score);
279
+ result.belowThresholdMatches = allBelowThreshold;
280
+ if (allMatches.length === 0) {
281
+ result.allTypesExhausted = true;
282
+ return result;
283
+ }
284
+ // Return all matches or just the best one
285
+ if (returnAll) {
286
+ result.matches = allMatches;
287
+ }
288
+ else {
289
+ result.matches = [allMatches[0]];
290
+ }
291
+ result.matchedType = result.matches[0].$type;
292
+ result.confidence = result.matches[0].$score;
293
+ // Fallback triggered if the best match isn't from the first type
294
+ result.fallbackTriggered = result.matchedType !== types[0];
295
+ return result;
296
+ }
297
+ /**
298
+ * Create a searcher function that wraps a semantic search provider
299
+ *
300
+ * @param provider - The database provider with semanticSearch method
301
+ * @returns A searcher function compatible with searchUnionTypes
302
+ *
303
+ * @example
304
+ * ```ts
305
+ * const searcher = createProviderSearcher(provider)
306
+ * const result = await searchUnionTypes(
307
+ * ['Document', 'Video'],
308
+ * 'tutorial',
309
+ * { mode: 'ordered', searcher }
310
+ * )
311
+ * ```
312
+ */
313
+ export function createProviderSearcher(provider) {
314
+ return async (type, query, options) => {
315
+ const results = await provider.semanticSearch(type, query, {
316
+ minScore: options?.threshold,
317
+ limit: options?.limit ?? 10,
318
+ });
319
+ return results.map((r) => ({
320
+ ...r,
321
+ $type: type,
322
+ }));
323
+ };
324
+ }
325
+ //# sourceMappingURL=union-fallback.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"union-fallback.js","sourceRoot":"","sources":["../../src/schema/union-fallback.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAiHH;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,UAAU,eAAe,CAAC,QAAgB;IAC9C,IAAI,CAAC,QAAQ,IAAI,QAAQ,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC;QACxC,OAAO,EAAE,CAAA;IACX,CAAC;IAED,sCAAsC;IACtC,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE;QAC1C,IAAI,IAAI,GAAG,CAAC,CAAC,IAAI,EAAE,CAAA;QAEnB,4CAA4C;QAC5C,MAAM,cAAc,GAAG,IAAI,CAAC,KAAK,CAAC,uCAAuC,CAAC,CAAA;QAC1E,IAAI,cAAc,EAAE,CAAC;YACnB,IAAI,GAAG,cAAc,CAAC,CAAC,CAAE,CAAA;QAC3B,CAAC;QAED,OAAO,IAAI,CAAA;IACb,CAAC,CAAC,CAAA;IAEF,4DAA4D;IAC5D,OAAO,KAAK,CA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AAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;YACzB,GAAG,CAAC;YACJ,KAAK,EAAE,IAAI;SACZ,CAAC,CAAiB,CAAA;IACrB,CAAC,CAAA;AACH,CAAC"}
@@ -0,0 +1,167 @@
1
+ /**
2
+ * Verb Derivation for Backward Cascade Resolution
3
+ *
4
+ * Derives reverse verbs for backward relationships:
5
+ * - manages -> managedBy
6
+ * - owns -> ownedBy
7
+ * - creates -> createdBy
8
+ * - parent_of <-> child_of (bidirectional)
9
+ *
10
+ * Used by the cascade resolver to determine the reverse relationship
11
+ * when traversing relationships in the opposite direction.
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * import { deriveReverseVerb, fieldNameToVerb, isPassiveVerb } from './verb-derivation.js'
16
+ *
17
+ * deriveReverseVerb('manages') // 'managedBy'
18
+ * deriveReverseVerb('parent_of') // 'child_of'
19
+ * deriveReverseVerb('managedBy') // 'manages'
20
+ *
21
+ * fieldNameToVerb('manager') // 'manages'
22
+ * fieldNameToVerb('owner') // 'owns'
23
+ *
24
+ * isPassiveVerb('managedBy') // true
25
+ * isPassiveVerb('manages') // false
26
+ * ```
27
+ *
28
+ * @packageDocumentation
29
+ */
30
+ /**
31
+ * Forward verbs to their reverse forms.
32
+ * Common active verbs mapped to their passive counterparts.
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * FORWARD_TO_REVERSE.manages // 'managedBy'
37
+ * FORWARD_TO_REVERSE.owns // 'ownedBy'
38
+ * FORWARD_TO_REVERSE.creates // 'createdBy'
39
+ * ```
40
+ */
41
+ export declare const FORWARD_TO_REVERSE: Readonly<Record<string, string>>;
42
+ /**
43
+ * Known verb pairs for bidirectional relationships.
44
+ * These are symmetric - each maps to the other.
45
+ *
46
+ * @example
47
+ * ```ts
48
+ * BIDIRECTIONAL_PAIRS.parent_of // 'child_of'
49
+ * BIDIRECTIONAL_PAIRS.child_of // 'parent_of'
50
+ * ```
51
+ */
52
+ export declare const BIDIRECTIONAL_PAIRS: Readonly<Record<string, string>>;
53
+ /**
54
+ * Derives the reverse verb for a given forward verb.
55
+ *
56
+ * Resolution order:
57
+ * 1. Check bidirectional pairs first (parent_of <-> child_of)
58
+ * 2. Check known forward verbs (manages -> managedBy)
59
+ * 3. Check if already a reversed verb (managedBy -> manages)
60
+ * 4. Apply standard transformation for verbs ending in 's'
61
+ * 5. Add 'By' suffix for unknown verbs
62
+ *
63
+ * @param verb - The verb to derive the reverse of
64
+ * @returns The reverse verb
65
+ *
66
+ * @example
67
+ * ```ts
68
+ * deriveReverseVerb('manages') // 'managedBy'
69
+ * deriveReverseVerb('owns') // 'ownedBy'
70
+ * deriveReverseVerb('parent_of') // 'child_of'
71
+ * deriveReverseVerb('managedBy') // 'manages' (already reversed)
72
+ * deriveReverseVerb('customAction') // 'customActionBy' (unknown verb)
73
+ * ```
74
+ */
75
+ export declare function deriveReverseVerb(verb: string): string;
76
+ /**
77
+ * Derives a verb from a field name.
78
+ *
79
+ * Common field names like "manager", "owner", "creator" are mapped
80
+ * to their corresponding verbs. Unknown field names are returned as-is.
81
+ *
82
+ * @param fieldName - The field name to derive a verb from
83
+ * @returns The derived verb or the field name if no mapping exists
84
+ *
85
+ * @example
86
+ * ```ts
87
+ * fieldNameToVerb('manager') // 'manages'
88
+ * fieldNameToVerb('owner') // 'owns'
89
+ * fieldNameToVerb('creator') // 'creates'
90
+ * fieldNameToVerb('customField') // 'customField' (no mapping)
91
+ * ```
92
+ */
93
+ export declare function fieldNameToVerb(fieldName: string): string;
94
+ /**
95
+ * Checks if a verb is in passive form.
96
+ *
97
+ * A verb is considered passive if it ends with:
98
+ * - 'By' (managedBy, ownedBy, createdBy)
99
+ * - 'To' (relatedTo, linkedTo, connectedTo)
100
+ * - 'Of' (parent_of, child_of, partOf)
101
+ *
102
+ * @param verb - The verb to check
103
+ * @returns True if the verb is passive, false otherwise
104
+ *
105
+ * @example
106
+ * ```ts
107
+ * isPassiveVerb('managedBy') // true
108
+ * isPassiveVerb('relatedTo') // true
109
+ * isPassiveVerb('parent_of') // true
110
+ * isPassiveVerb('manages') // false
111
+ * ```
112
+ */
113
+ export declare function isPassiveVerb(verb: string): boolean;
114
+ /**
115
+ * Register a custom verb pair for forward/reverse derivation.
116
+ *
117
+ * This allows extending the verb derivation system with domain-specific
118
+ * verb mappings at runtime.
119
+ *
120
+ * @param forward - The forward (active) verb
121
+ * @param reverse - The reverse (passive) verb
122
+ *
123
+ * @example
124
+ * ```ts
125
+ * registerVerbPair('sponsors', 'sponsoredBy')
126
+ * deriveReverseVerb('sponsors') // 'sponsoredBy'
127
+ * deriveReverseVerb('sponsoredBy') // 'sponsors'
128
+ * ```
129
+ */
130
+ export declare function registerVerbPair(forward: string, reverse: string): void;
131
+ /**
132
+ * Register a bidirectional verb pair.
133
+ *
134
+ * Bidirectional pairs are symmetric relationships where each verb
135
+ * maps to the other. For self-referential relationships, both
136
+ * arguments can be the same verb.
137
+ *
138
+ * @param verbA - The first verb
139
+ * @param verbB - The second verb (can be same as verbA for symmetric)
140
+ *
141
+ * @example
142
+ * ```ts
143
+ * // Asymmetric bidirectional
144
+ * registerBidirectionalPair('mentor_of', 'mentee_of')
145
+ * deriveReverseVerb('mentor_of') // 'mentee_of'
146
+ * deriveReverseVerb('mentee_of') // 'mentor_of'
147
+ *
148
+ * // Symmetric (self-referential)
149
+ * registerBidirectionalPair('relatedTo', 'relatedTo')
150
+ * deriveReverseVerb('relatedTo') // 'relatedTo'
151
+ * ```
152
+ */
153
+ export declare function registerBidirectionalPair(verbA: string, verbB: string): void;
154
+ /**
155
+ * Register a field name to verb mapping.
156
+ *
157
+ * @param fieldName - The field name
158
+ * @param verb - The verb it derives to
159
+ *
160
+ * @example
161
+ * ```ts
162
+ * registerFieldVerb('coordinator', 'coordinates')
163
+ * fieldNameToVerb('coordinator') // 'coordinates'
164
+ * ```
165
+ */
166
+ export declare function registerFieldVerb(fieldName: string, verb: string): void;
167
+ //# sourceMappingURL=verb-derivation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"verb-derivation.d.ts","sourceRoot":"","sources":["../../src/schema/verb-derivation.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAiCH;;;;;;;;;;GAUG;AACH,eAAO,MAAM,kBAAkB,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAqB,CAAA;AAcrF;;;;;;;;;GASG;AACH,eAAO,MAAM,mBAAmB,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAuB,CAAA;AAwBxF;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAwCtD;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,eAAe,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAEzD;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAYnD;AAMD;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI,CAGvE;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,yBAAyB,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAG5E;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,iBAAiB,CAAC,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAEvE"}