@ordinatio/entities 1.0.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.
- package/LICENSE +21 -0
- package/README.md +328 -0
- package/dist/chunk-QXBSEBJW.mjs +750 -0
- package/dist/index.mjs +2265 -0
- package/dist/observer-YUTEGG4S.mjs +10 -0
- package/package.json +54 -0
|
@@ -0,0 +1,750 @@
|
|
|
1
|
+
// src/errors.ts
|
|
2
|
+
var KNOWLEDGE_ERRORS = {
|
|
3
|
+
KNOWLEDGE_300: {
|
|
4
|
+
code: "KNOWLEDGE_300",
|
|
5
|
+
file: "knowledge/field-definitions.ts",
|
|
6
|
+
function: "getFieldDefinitions",
|
|
7
|
+
severity: "high",
|
|
8
|
+
recoverable: true,
|
|
9
|
+
description: "Failed to list field definitions.",
|
|
10
|
+
diagnosis: [
|
|
11
|
+
{ step: "Check DB connectivity", check: "Verify DATABASE_URL is reachable" },
|
|
12
|
+
{ step: "Check schema", check: "Run pnpm db:push to ensure EntityFieldDefinition table exists" }
|
|
13
|
+
]
|
|
14
|
+
},
|
|
15
|
+
KNOWLEDGE_301: {
|
|
16
|
+
code: "KNOWLEDGE_301",
|
|
17
|
+
file: "knowledge/field-definitions.ts",
|
|
18
|
+
function: "createFieldDefinition",
|
|
19
|
+
severity: "high",
|
|
20
|
+
recoverable: true,
|
|
21
|
+
description: "Failed to create field definition.",
|
|
22
|
+
diagnosis: [
|
|
23
|
+
{ step: "Check uniqueness", check: "entityType + key must be unique" },
|
|
24
|
+
{ step: "Check DB connectivity", check: "Verify DATABASE_URL is reachable" },
|
|
25
|
+
{ step: "Check validation", check: "Review Zod validation errors in response body" }
|
|
26
|
+
]
|
|
27
|
+
},
|
|
28
|
+
KNOWLEDGE_302: {
|
|
29
|
+
code: "KNOWLEDGE_302",
|
|
30
|
+
file: "knowledge/field-definitions.ts",
|
|
31
|
+
function: "updateFieldDefinition",
|
|
32
|
+
severity: "high",
|
|
33
|
+
recoverable: true,
|
|
34
|
+
description: "Failed to update field definition.",
|
|
35
|
+
diagnosis: [
|
|
36
|
+
{ step: "Check field exists", check: "Verify field ID is valid" },
|
|
37
|
+
{ step: "Check DB connectivity", check: "Verify DATABASE_URL is reachable" }
|
|
38
|
+
]
|
|
39
|
+
},
|
|
40
|
+
KNOWLEDGE_303: {
|
|
41
|
+
code: "KNOWLEDGE_303",
|
|
42
|
+
file: "knowledge/field-definitions.ts",
|
|
43
|
+
function: "getFieldDefinitionById",
|
|
44
|
+
severity: "low",
|
|
45
|
+
recoverable: true,
|
|
46
|
+
description: "Field definition not found.",
|
|
47
|
+
diagnosis: [
|
|
48
|
+
{ step: "Verify field ID", check: "Check that field exists in EntityFieldDefinition table" }
|
|
49
|
+
]
|
|
50
|
+
},
|
|
51
|
+
KNOWLEDGE_304: {
|
|
52
|
+
code: "KNOWLEDGE_304",
|
|
53
|
+
file: "knowledge/field-definitions.ts",
|
|
54
|
+
function: "deactivateFieldDefinition",
|
|
55
|
+
severity: "high",
|
|
56
|
+
recoverable: true,
|
|
57
|
+
description: "Failed to deactivate field definition.",
|
|
58
|
+
diagnosis: [
|
|
59
|
+
{ step: "Check field exists", check: "Field may have been deleted already" },
|
|
60
|
+
{ step: "Check DB connectivity", check: "Verify DATABASE_URL is reachable" }
|
|
61
|
+
]
|
|
62
|
+
},
|
|
63
|
+
KNOWLEDGE_305: {
|
|
64
|
+
code: "KNOWLEDGE_305",
|
|
65
|
+
file: "knowledge/search.ts",
|
|
66
|
+
function: "searchByFields",
|
|
67
|
+
severity: "high",
|
|
68
|
+
recoverable: true,
|
|
69
|
+
description: "Failed to search entities by field values.",
|
|
70
|
+
diagnosis: [
|
|
71
|
+
{ step: "Check filter format", check: "Filters must be { fieldKey: value } pairs" },
|
|
72
|
+
{ step: "Check DB connectivity", check: "Verify DATABASE_URL is reachable" }
|
|
73
|
+
]
|
|
74
|
+
},
|
|
75
|
+
KNOWLEDGE_310: {
|
|
76
|
+
code: "KNOWLEDGE_310",
|
|
77
|
+
file: "knowledge/ledger.ts",
|
|
78
|
+
function: "getEntityFields",
|
|
79
|
+
severity: "high",
|
|
80
|
+
recoverable: true,
|
|
81
|
+
description: "Failed to read entity field values.",
|
|
82
|
+
diagnosis: [
|
|
83
|
+
{ step: "Check entity exists", check: "Verify entityType and entityId are valid" },
|
|
84
|
+
{ step: "Check DB connectivity", check: "Verify DATABASE_URL is reachable" }
|
|
85
|
+
]
|
|
86
|
+
},
|
|
87
|
+
KNOWLEDGE_311: {
|
|
88
|
+
code: "KNOWLEDGE_311",
|
|
89
|
+
file: "knowledge/ledger.ts",
|
|
90
|
+
function: "setEntityFields",
|
|
91
|
+
severity: "high",
|
|
92
|
+
recoverable: true,
|
|
93
|
+
description: "Failed to write entity field values.",
|
|
94
|
+
diagnosis: [
|
|
95
|
+
{ step: "Check field definitions", check: "All field keys must match approved field definitions" },
|
|
96
|
+
{ step: "Check value types", check: "Values must match field dataType (text, number, date, etc.)" },
|
|
97
|
+
{ step: "Check DB connectivity", check: "Verify DATABASE_URL is reachable" }
|
|
98
|
+
]
|
|
99
|
+
},
|
|
100
|
+
KNOWLEDGE_312: {
|
|
101
|
+
code: "KNOWLEDGE_312",
|
|
102
|
+
file: "knowledge/ledger.ts",
|
|
103
|
+
function: "getFieldHistory",
|
|
104
|
+
severity: "medium",
|
|
105
|
+
recoverable: true,
|
|
106
|
+
description: "Failed to load field value history.",
|
|
107
|
+
diagnosis: [
|
|
108
|
+
{ step: "Check entity exists", check: "Verify entityType and entityId are valid" },
|
|
109
|
+
{ step: "Check DB connectivity", check: "Verify DATABASE_URL is reachable" }
|
|
110
|
+
]
|
|
111
|
+
},
|
|
112
|
+
KNOWLEDGE_313: {
|
|
113
|
+
code: "KNOWLEDGE_313",
|
|
114
|
+
file: "knowledge/ledger.ts",
|
|
115
|
+
function: "setEntityFields",
|
|
116
|
+
severity: "medium",
|
|
117
|
+
recoverable: true,
|
|
118
|
+
description: "Unknown field key \u2014 no approved field definition found.",
|
|
119
|
+
diagnosis: [
|
|
120
|
+
{ step: "Check field key", check: "Verify key matches an approved EntityFieldDefinition" },
|
|
121
|
+
{ step: "Check entity type", check: "Field must be defined for this entity type" }
|
|
122
|
+
]
|
|
123
|
+
},
|
|
124
|
+
KNOWLEDGE_314: {
|
|
125
|
+
code: "KNOWLEDGE_314",
|
|
126
|
+
file: "knowledge/ledger.ts",
|
|
127
|
+
function: "setEntityFields",
|
|
128
|
+
severity: "low",
|
|
129
|
+
recoverable: true,
|
|
130
|
+
description: "Failed to supersede old ledger entry during value update.",
|
|
131
|
+
diagnosis: [
|
|
132
|
+
{ step: "Check DB connectivity", check: "Transaction may have failed mid-write" },
|
|
133
|
+
{ step: "Retry operation", check: "Supersede + insert should be atomic" }
|
|
134
|
+
]
|
|
135
|
+
},
|
|
136
|
+
KNOWLEDGE_320: {
|
|
137
|
+
code: "KNOWLEDGE_320",
|
|
138
|
+
file: "knowledge/search.ts",
|
|
139
|
+
function: "logSearchQuery",
|
|
140
|
+
severity: "low",
|
|
141
|
+
recoverable: true,
|
|
142
|
+
description: "Failed to log search query.",
|
|
143
|
+
diagnosis: [
|
|
144
|
+
{ step: "Check DB connectivity", check: "Query logging is best-effort \u2014 does not block search" }
|
|
145
|
+
]
|
|
146
|
+
},
|
|
147
|
+
KNOWLEDGE_321: {
|
|
148
|
+
code: "KNOWLEDGE_321",
|
|
149
|
+
file: "knowledge/search.ts",
|
|
150
|
+
function: "logSearchQuery",
|
|
151
|
+
severity: "low",
|
|
152
|
+
recoverable: true,
|
|
153
|
+
description: "Failed to process search query log request.",
|
|
154
|
+
diagnosis: [
|
|
155
|
+
{ step: "Check request body", check: "Must include query and source fields" }
|
|
156
|
+
]
|
|
157
|
+
},
|
|
158
|
+
KNOWLEDGE_330: {
|
|
159
|
+
code: "KNOWLEDGE_330",
|
|
160
|
+
file: "knowledge/batch.ts",
|
|
161
|
+
function: "runKnowledgeBatch",
|
|
162
|
+
severity: "high",
|
|
163
|
+
recoverable: true,
|
|
164
|
+
description: "Knowledge batch job failed during execution.",
|
|
165
|
+
diagnosis: [
|
|
166
|
+
{ step: "Check DB connectivity", check: "Batch job needs read/write access to multiple tables" },
|
|
167
|
+
{ step: "Check logs", check: "Review worker logs for detailed error trace" }
|
|
168
|
+
]
|
|
169
|
+
},
|
|
170
|
+
KNOWLEDGE_331: {
|
|
171
|
+
code: "KNOWLEDGE_331",
|
|
172
|
+
file: "knowledge/batch.ts",
|
|
173
|
+
function: "extractFieldsFromNotes",
|
|
174
|
+
severity: "medium",
|
|
175
|
+
recoverable: true,
|
|
176
|
+
description: "Failed to extract structured fields from notes.",
|
|
177
|
+
diagnosis: [
|
|
178
|
+
{ step: "Check note content", check: "Notes may be empty or contain no extractable data" },
|
|
179
|
+
{ step: "Check field hints", check: "Verify extractionHint values on field definitions" }
|
|
180
|
+
]
|
|
181
|
+
},
|
|
182
|
+
KNOWLEDGE_332: {
|
|
183
|
+
code: "KNOWLEDGE_332",
|
|
184
|
+
file: "knowledge/batch.ts",
|
|
185
|
+
function: "buildTemporalPatterns",
|
|
186
|
+
severity: "medium",
|
|
187
|
+
recoverable: true,
|
|
188
|
+
description: "Failed to build temporal search patterns.",
|
|
189
|
+
diagnosis: [
|
|
190
|
+
{ step: "Check query logs", check: "SearchQueryLog may have no unprocessed entries" },
|
|
191
|
+
{ step: "Check DB connectivity", check: "Pattern upsert may have failed" }
|
|
192
|
+
]
|
|
193
|
+
},
|
|
194
|
+
// --- Time-Travel (340-341) ---
|
|
195
|
+
KNOWLEDGE_340: {
|
|
196
|
+
code: "KNOWLEDGE_340",
|
|
197
|
+
file: "knowledge/time-travel.ts",
|
|
198
|
+
function: "getKnowledgeAt",
|
|
199
|
+
severity: "high",
|
|
200
|
+
recoverable: true,
|
|
201
|
+
description: "Failed to reconstruct knowledge state at point in time.",
|
|
202
|
+
diagnosis: [
|
|
203
|
+
{ step: "Check timestamp", check: "Ensure timestamp is a valid Date object" },
|
|
204
|
+
{ step: "Check DB connectivity", check: "Verify DATABASE_URL is reachable" }
|
|
205
|
+
]
|
|
206
|
+
},
|
|
207
|
+
KNOWLEDGE_341: {
|
|
208
|
+
code: "KNOWLEDGE_341",
|
|
209
|
+
file: "knowledge/time-travel.ts",
|
|
210
|
+
function: "getKnowledgeTimeline",
|
|
211
|
+
severity: "medium",
|
|
212
|
+
recoverable: true,
|
|
213
|
+
description: "Invalid date range for knowledge timeline query.",
|
|
214
|
+
diagnosis: [
|
|
215
|
+
{ step: "Check date order", check: "from must be before to" },
|
|
216
|
+
{ step: "Check date format", check: "Both from and to must be valid Date objects" }
|
|
217
|
+
]
|
|
218
|
+
},
|
|
219
|
+
// --- Decay (342) ---
|
|
220
|
+
KNOWLEDGE_342: {
|
|
221
|
+
code: "KNOWLEDGE_342",
|
|
222
|
+
file: "knowledge/decay.ts",
|
|
223
|
+
function: "getStaleFields",
|
|
224
|
+
severity: "medium",
|
|
225
|
+
recoverable: true,
|
|
226
|
+
description: "Failed to compute stale fields for entity.",
|
|
227
|
+
diagnosis: [
|
|
228
|
+
{ step: "Check entity exists", check: "Verify entityType and entityId are valid" },
|
|
229
|
+
{ step: "Check DB connectivity", check: "Verify DATABASE_URL is reachable" }
|
|
230
|
+
]
|
|
231
|
+
},
|
|
232
|
+
// --- Branching (343-344) ---
|
|
233
|
+
KNOWLEDGE_343: {
|
|
234
|
+
code: "KNOWLEDGE_343",
|
|
235
|
+
file: "knowledge/branching.ts",
|
|
236
|
+
function: "setEntityFieldsWithBranching",
|
|
237
|
+
severity: "high",
|
|
238
|
+
recoverable: true,
|
|
239
|
+
description: "Failed to write entity fields with branching validation.",
|
|
240
|
+
diagnosis: [
|
|
241
|
+
{ step: "Check field definitions", check: "All field keys must match approved field definitions" },
|
|
242
|
+
{ step: "Check DB connectivity", check: "Verify DATABASE_URL is reachable" }
|
|
243
|
+
]
|
|
244
|
+
},
|
|
245
|
+
KNOWLEDGE_344: {
|
|
246
|
+
code: "KNOWLEDGE_344",
|
|
247
|
+
file: "knowledge/branching.ts",
|
|
248
|
+
function: "setEntityFieldsWithBranching",
|
|
249
|
+
severity: "medium",
|
|
250
|
+
recoverable: true,
|
|
251
|
+
description: "Validation callback failed during branching check.",
|
|
252
|
+
diagnosis: [
|
|
253
|
+
{ step: "Check callback", check: "Ensure requestValidation callback is implemented correctly" },
|
|
254
|
+
{ step: "Check connectivity", check: "Callback may depend on external service" }
|
|
255
|
+
]
|
|
256
|
+
},
|
|
257
|
+
// --- Shadow Graph (345) ---
|
|
258
|
+
KNOWLEDGE_345: {
|
|
259
|
+
code: "KNOWLEDGE_345",
|
|
260
|
+
file: "knowledge/shadow-graph.ts",
|
|
261
|
+
function: "findRelatedEntities",
|
|
262
|
+
severity: "medium",
|
|
263
|
+
recoverable: true,
|
|
264
|
+
description: "Failed to discover entity relationships via shared fields.",
|
|
265
|
+
diagnosis: [
|
|
266
|
+
{ step: "Check entity exists", check: "Verify entityType and entityId are valid" },
|
|
267
|
+
{ step: "Check DB connectivity", check: "Verify DATABASE_URL is reachable" }
|
|
268
|
+
]
|
|
269
|
+
},
|
|
270
|
+
// --- Reflection (346-347) ---
|
|
271
|
+
KNOWLEDGE_346: {
|
|
272
|
+
code: "KNOWLEDGE_346",
|
|
273
|
+
file: "knowledge/reflection.ts",
|
|
274
|
+
function: "detectConflicts",
|
|
275
|
+
severity: "medium",
|
|
276
|
+
recoverable: true,
|
|
277
|
+
description: "Failed to detect contradictions in entity knowledge.",
|
|
278
|
+
diagnosis: [
|
|
279
|
+
{ step: "Check entity exists", check: "Verify entityType and entityId are valid" },
|
|
280
|
+
{ step: "Check DB connectivity", check: "Verify DATABASE_URL is reachable" }
|
|
281
|
+
]
|
|
282
|
+
},
|
|
283
|
+
KNOWLEDGE_347: {
|
|
284
|
+
code: "KNOWLEDGE_347",
|
|
285
|
+
file: "knowledge/reflection.ts",
|
|
286
|
+
function: "scanForConflicts",
|
|
287
|
+
severity: "medium",
|
|
288
|
+
recoverable: true,
|
|
289
|
+
description: "Failed to scan entities for contradictions.",
|
|
290
|
+
diagnosis: [
|
|
291
|
+
{ step: "Check entity type", check: "Verify entityType is valid" },
|
|
292
|
+
{ step: "Check DB connectivity", check: "Verify DATABASE_URL is reachable" }
|
|
293
|
+
]
|
|
294
|
+
},
|
|
295
|
+
KNOWLEDGE_348: {
|
|
296
|
+
code: "KNOWLEDGE_348",
|
|
297
|
+
file: "knowledge/scoring.ts",
|
|
298
|
+
function: "computeEntityTruthScores",
|
|
299
|
+
severity: "medium",
|
|
300
|
+
recoverable: true,
|
|
301
|
+
description: "Failed to compute truth scores for entity fields.",
|
|
302
|
+
diagnosis: [
|
|
303
|
+
{ step: "Check entity exists", check: "Verify entityType and entityId are valid" },
|
|
304
|
+
{ step: "Check DB connectivity", check: "Verify DATABASE_URL is reachable" }
|
|
305
|
+
]
|
|
306
|
+
},
|
|
307
|
+
// --- Observer (350-352) ---
|
|
308
|
+
KNOWLEDGE_350: {
|
|
309
|
+
code: "KNOWLEDGE_350",
|
|
310
|
+
file: "knowledge/observer.ts",
|
|
311
|
+
function: "checkConstraints",
|
|
312
|
+
severity: "medium",
|
|
313
|
+
recoverable: true,
|
|
314
|
+
description: "Failed to evaluate field constraints for entity.",
|
|
315
|
+
diagnosis: [
|
|
316
|
+
{ step: "Check constraints JSON", check: "Ensure constraints on field definitions are valid JSON arrays" },
|
|
317
|
+
{ step: "Check DB connectivity", check: "Verify DATABASE_URL is reachable" }
|
|
318
|
+
]
|
|
319
|
+
},
|
|
320
|
+
KNOWLEDGE_351: {
|
|
321
|
+
code: "KNOWLEDGE_351",
|
|
322
|
+
file: "knowledge/observer.ts",
|
|
323
|
+
function: "fireObservers",
|
|
324
|
+
severity: "low",
|
|
325
|
+
recoverable: true,
|
|
326
|
+
description: "Observer callback failed during constraint violation notification.",
|
|
327
|
+
diagnosis: [
|
|
328
|
+
{ step: "Check callback", check: "Ensure onConstraintViolation callback is implemented correctly" },
|
|
329
|
+
{ step: "Non-blocking", check: "Observer failures do not block writes" }
|
|
330
|
+
]
|
|
331
|
+
},
|
|
332
|
+
KNOWLEDGE_352: {
|
|
333
|
+
code: "KNOWLEDGE_352",
|
|
334
|
+
file: "knowledge/observer.ts",
|
|
335
|
+
function: "evaluateConstraint",
|
|
336
|
+
severity: "low",
|
|
337
|
+
recoverable: true,
|
|
338
|
+
description: "Invalid constraint definition on field.",
|
|
339
|
+
diagnosis: [
|
|
340
|
+
{ step: "Check operator", check: "Operator must be one of: not_in, in, not_equal, less_than, greater_than, regex" },
|
|
341
|
+
{ step: "Check values", check: "Ensure values/value/pattern match the operator" }
|
|
342
|
+
]
|
|
343
|
+
},
|
|
344
|
+
// --- Ghost Fields (355-356) ---
|
|
345
|
+
KNOWLEDGE_355: {
|
|
346
|
+
code: "KNOWLEDGE_355",
|
|
347
|
+
file: "knowledge/ghost-fields.ts",
|
|
348
|
+
function: "predictGhostFields",
|
|
349
|
+
severity: "medium",
|
|
350
|
+
recoverable: true,
|
|
351
|
+
description: "Failed to predict ghost fields from co-occurrence patterns.",
|
|
352
|
+
diagnosis: [
|
|
353
|
+
{ step: "Check entity exists", check: "Verify entityType and entityId are valid" },
|
|
354
|
+
{ step: "Check data volume", check: "Co-occurrence requires sufficient data (minOccurrences)" }
|
|
355
|
+
]
|
|
356
|
+
},
|
|
357
|
+
KNOWLEDGE_356: {
|
|
358
|
+
code: "KNOWLEDGE_356",
|
|
359
|
+
file: "knowledge/ghost-fields.ts",
|
|
360
|
+
function: "writeGhostFields",
|
|
361
|
+
severity: "medium",
|
|
362
|
+
recoverable: true,
|
|
363
|
+
description: "Failed to write predicted ghost field values.",
|
|
364
|
+
diagnosis: [
|
|
365
|
+
{ step: "Check field definitions", check: "Predicted field keys must match approved definitions" },
|
|
366
|
+
{ step: "Check DB connectivity", check: "Verify DATABASE_URL is reachable" }
|
|
367
|
+
]
|
|
368
|
+
},
|
|
369
|
+
// --- Health (360-361) ---
|
|
370
|
+
KNOWLEDGE_360: {
|
|
371
|
+
code: "KNOWLEDGE_360",
|
|
372
|
+
file: "knowledge/health.ts",
|
|
373
|
+
function: "computeEntityHealth",
|
|
374
|
+
severity: "medium",
|
|
375
|
+
recoverable: true,
|
|
376
|
+
description: "Failed to compute entity health report.",
|
|
377
|
+
diagnosis: [
|
|
378
|
+
{ step: "Check entity exists", check: "Verify entityType and entityId are valid" },
|
|
379
|
+
{ step: "Check DB connectivity", check: "Verify DATABASE_URL is reachable" }
|
|
380
|
+
]
|
|
381
|
+
},
|
|
382
|
+
KNOWLEDGE_361: {
|
|
383
|
+
code: "KNOWLEDGE_361",
|
|
384
|
+
file: "knowledge/health.ts",
|
|
385
|
+
function: "getEntityTypeHealth",
|
|
386
|
+
severity: "medium",
|
|
387
|
+
recoverable: true,
|
|
388
|
+
description: "Failed to compute entity type health summary.",
|
|
389
|
+
diagnosis: [
|
|
390
|
+
{ step: "Check entity type", check: "Verify entityType is valid" },
|
|
391
|
+
{ step: "Check DB connectivity", check: "Verify DATABASE_URL is reachable" }
|
|
392
|
+
]
|
|
393
|
+
}
|
|
394
|
+
};
|
|
395
|
+
var AGENTKNOW_ERRORS = {
|
|
396
|
+
AGENTKNOW_400: {
|
|
397
|
+
code: "AGENTKNOW_400",
|
|
398
|
+
file: "agent/knowledge.ts",
|
|
399
|
+
function: "queryKnowledge",
|
|
400
|
+
httpStatus: 400,
|
|
401
|
+
severity: "warning",
|
|
402
|
+
recoverable: true,
|
|
403
|
+
description: "Knowledge query failed \u2014 invalid parameters",
|
|
404
|
+
diagnosis: [
|
|
405
|
+
'Check that entity is a non-empty string (e.g., "order-item")',
|
|
406
|
+
"If field is provided, it must be a non-empty string",
|
|
407
|
+
"If search is provided, it must be at least 1 character"
|
|
408
|
+
]
|
|
409
|
+
},
|
|
410
|
+
AGENTKNOW_401: {
|
|
411
|
+
code: "AGENTKNOW_401",
|
|
412
|
+
file: "agent/knowledge.ts",
|
|
413
|
+
function: "queryKnowledge",
|
|
414
|
+
httpStatus: 500,
|
|
415
|
+
severity: "error",
|
|
416
|
+
recoverable: true,
|
|
417
|
+
description: "Knowledge query failed \u2014 database error",
|
|
418
|
+
diagnosis: [
|
|
419
|
+
"Check database connectivity",
|
|
420
|
+
"Verify AgentKnowledge table exists (run pnpm db:push)",
|
|
421
|
+
"Check Prisma logs for query errors"
|
|
422
|
+
]
|
|
423
|
+
},
|
|
424
|
+
AGENTKNOW_402: {
|
|
425
|
+
code: "AGENTKNOW_402",
|
|
426
|
+
file: "agent/knowledge.ts",
|
|
427
|
+
function: "createKnowledgeEntry",
|
|
428
|
+
httpStatus: 400,
|
|
429
|
+
severity: "warning",
|
|
430
|
+
recoverable: true,
|
|
431
|
+
description: "Knowledge entry creation failed \u2014 validation error",
|
|
432
|
+
diagnosis: [
|
|
433
|
+
"entity, field, value, and label are all required",
|
|
434
|
+
'entity must be a recognized entity type (e.g., "order-item")',
|
|
435
|
+
"aliases must be an array of strings if provided",
|
|
436
|
+
"Check Zod validation errors in context.validationErrors"
|
|
437
|
+
]
|
|
438
|
+
},
|
|
439
|
+
AGENTKNOW_403: {
|
|
440
|
+
code: "AGENTKNOW_403",
|
|
441
|
+
file: "agent/knowledge.ts",
|
|
442
|
+
function: "createKnowledgeEntry",
|
|
443
|
+
httpStatus: 409,
|
|
444
|
+
severity: "warning",
|
|
445
|
+
recoverable: true,
|
|
446
|
+
description: "Knowledge entry already exists for this entity/field/value combination",
|
|
447
|
+
diagnosis: [
|
|
448
|
+
"An entry with the same entity + field + value already exists",
|
|
449
|
+
"Use updateKnowledgeEntry to modify existing entries",
|
|
450
|
+
"Or use a different value if this is a genuinely new option"
|
|
451
|
+
]
|
|
452
|
+
},
|
|
453
|
+
AGENTKNOW_404: {
|
|
454
|
+
code: "AGENTKNOW_404",
|
|
455
|
+
file: "agent/knowledge.ts",
|
|
456
|
+
function: "updateKnowledgeEntry",
|
|
457
|
+
httpStatus: 404,
|
|
458
|
+
severity: "warning",
|
|
459
|
+
recoverable: true,
|
|
460
|
+
description: "Knowledge entry not found",
|
|
461
|
+
diagnosis: [
|
|
462
|
+
"Verify the entry ID is correct",
|
|
463
|
+
"The entry may have been deleted",
|
|
464
|
+
"Use queryKnowledge to find valid entry IDs"
|
|
465
|
+
]
|
|
466
|
+
},
|
|
467
|
+
AGENTKNOW_405: {
|
|
468
|
+
code: "AGENTKNOW_405",
|
|
469
|
+
file: "agent/knowledge.ts",
|
|
470
|
+
function: "deleteKnowledgeEntry",
|
|
471
|
+
httpStatus: 404,
|
|
472
|
+
severity: "warning",
|
|
473
|
+
recoverable: true,
|
|
474
|
+
description: "Cannot delete knowledge entry \u2014 not found",
|
|
475
|
+
diagnosis: [
|
|
476
|
+
"Verify the entry ID is correct",
|
|
477
|
+
"The entry may have already been deleted"
|
|
478
|
+
]
|
|
479
|
+
},
|
|
480
|
+
AGENTKNOW_406: {
|
|
481
|
+
code: "AGENTKNOW_406",
|
|
482
|
+
file: "agent/preferences.ts",
|
|
483
|
+
function: "getPreferences",
|
|
484
|
+
httpStatus: 400,
|
|
485
|
+
severity: "warning",
|
|
486
|
+
recoverable: true,
|
|
487
|
+
description: "Preference query failed \u2014 invalid parameters",
|
|
488
|
+
diagnosis: [
|
|
489
|
+
"entity must be a non-empty string",
|
|
490
|
+
"userId is optional \u2014 omit for org-wide preferences"
|
|
491
|
+
]
|
|
492
|
+
},
|
|
493
|
+
AGENTKNOW_407: {
|
|
494
|
+
code: "AGENTKNOW_407",
|
|
495
|
+
file: "agent/preferences.ts",
|
|
496
|
+
function: "setPreference",
|
|
497
|
+
httpStatus: 400,
|
|
498
|
+
severity: "warning",
|
|
499
|
+
recoverable: true,
|
|
500
|
+
description: "Preference creation failed \u2014 validation error",
|
|
501
|
+
diagnosis: [
|
|
502
|
+
"entity, field, value, and label are all required",
|
|
503
|
+
"conditions must be a valid JSON object if provided",
|
|
504
|
+
"Check Zod validation errors in context.validationErrors"
|
|
505
|
+
]
|
|
506
|
+
},
|
|
507
|
+
AGENTKNOW_408: {
|
|
508
|
+
code: "AGENTKNOW_408",
|
|
509
|
+
file: "agent/preferences.ts",
|
|
510
|
+
function: "deletePreference",
|
|
511
|
+
httpStatus: 404,
|
|
512
|
+
severity: "warning",
|
|
513
|
+
recoverable: true,
|
|
514
|
+
description: "Preference not found",
|
|
515
|
+
diagnosis: [
|
|
516
|
+
"Verify the preference ID is correct",
|
|
517
|
+
"The preference may have already been deleted"
|
|
518
|
+
]
|
|
519
|
+
},
|
|
520
|
+
AGENTKNOW_409: {
|
|
521
|
+
code: "AGENTKNOW_409",
|
|
522
|
+
file: "agent/knowledge.ts",
|
|
523
|
+
function: "ensureKnowledgeDefaults",
|
|
524
|
+
httpStatus: 500,
|
|
525
|
+
severity: "error",
|
|
526
|
+
recoverable: true,
|
|
527
|
+
description: "Seed data insertion failed",
|
|
528
|
+
diagnosis: [
|
|
529
|
+
"Check database connectivity",
|
|
530
|
+
"Verify AgentKnowledge table exists (run pnpm db:push)",
|
|
531
|
+
"Check for unique constraint violations in seed data",
|
|
532
|
+
"Inspect Prisma error details for specific row failures"
|
|
533
|
+
]
|
|
534
|
+
},
|
|
535
|
+
AGENTKNOW_410: {
|
|
536
|
+
code: "AGENTKNOW_410",
|
|
537
|
+
file: "agent/knowledge.ts",
|
|
538
|
+
function: "resetKnowledgeDefaults",
|
|
539
|
+
httpStatus: 500,
|
|
540
|
+
severity: "error",
|
|
541
|
+
recoverable: true,
|
|
542
|
+
description: "Knowledge reset failed \u2014 could not delete and re-seed",
|
|
543
|
+
diagnosis: [
|
|
544
|
+
"Check database connectivity",
|
|
545
|
+
"Verify transaction support is working",
|
|
546
|
+
'This deletes ALL entries with source="seed" and re-inserts defaults'
|
|
547
|
+
]
|
|
548
|
+
},
|
|
549
|
+
AGENTKNOW_411: {
|
|
550
|
+
code: "AGENTKNOW_411",
|
|
551
|
+
file: "api/agent/knowledge/route.ts",
|
|
552
|
+
function: "GET",
|
|
553
|
+
httpStatus: 500,
|
|
554
|
+
severity: "error",
|
|
555
|
+
recoverable: true,
|
|
556
|
+
description: "Failed to query agent knowledge entries.",
|
|
557
|
+
diagnosis: [
|
|
558
|
+
"Check database connectivity",
|
|
559
|
+
"Verify AgentKnowledge table exists",
|
|
560
|
+
"May be a service-level error not caught by specific AGENTKNOW_400/401 codes"
|
|
561
|
+
]
|
|
562
|
+
},
|
|
563
|
+
AGENTKNOW_412: {
|
|
564
|
+
code: "AGENTKNOW_412",
|
|
565
|
+
file: "api/agent/knowledge/route.ts",
|
|
566
|
+
function: "POST",
|
|
567
|
+
httpStatus: 500,
|
|
568
|
+
severity: "error",
|
|
569
|
+
recoverable: true,
|
|
570
|
+
description: "Failed to create agent knowledge entry.",
|
|
571
|
+
diagnosis: [
|
|
572
|
+
"Check database connectivity",
|
|
573
|
+
"Verify AgentKnowledge table exists",
|
|
574
|
+
"May be a service-level error not caught by specific AGENTKNOW_402/403 codes"
|
|
575
|
+
]
|
|
576
|
+
}
|
|
577
|
+
};
|
|
578
|
+
function knowledgeError(code, context) {
|
|
579
|
+
const def = KNOWLEDGE_ERRORS[code];
|
|
580
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[-:]/g, "").replace(/\.\d+Z/, "");
|
|
581
|
+
return {
|
|
582
|
+
code: def.code,
|
|
583
|
+
ref: `${def.code}-${timestamp}`,
|
|
584
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
585
|
+
module: "KNOWLEDGE",
|
|
586
|
+
description: def.description,
|
|
587
|
+
severity: def.severity,
|
|
588
|
+
recoverable: def.recoverable,
|
|
589
|
+
diagnosis: [...def.diagnosis],
|
|
590
|
+
context: context || {}
|
|
591
|
+
};
|
|
592
|
+
}
|
|
593
|
+
function agentKnowledgeError(code, context) {
|
|
594
|
+
const entry = AGENTKNOW_ERRORS[code];
|
|
595
|
+
const ts = (/* @__PURE__ */ new Date()).toISOString().replace(/[-:]/g, "").replace(/\.\d+Z$/, "");
|
|
596
|
+
return {
|
|
597
|
+
code: entry.code,
|
|
598
|
+
ref: `${entry.code}-${ts}`,
|
|
599
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
600
|
+
module: "AGENT_KNOWLEDGE",
|
|
601
|
+
description: entry.description,
|
|
602
|
+
severity: entry.severity,
|
|
603
|
+
recoverable: entry.recoverable,
|
|
604
|
+
diagnosis: [...entry.diagnosis],
|
|
605
|
+
context: context || {}
|
|
606
|
+
};
|
|
607
|
+
}
|
|
608
|
+
var ContactNotFoundError = class extends Error {
|
|
609
|
+
constructor(id) {
|
|
610
|
+
super(`Contact not found: ${id}`);
|
|
611
|
+
this.name = "ContactNotFoundError";
|
|
612
|
+
}
|
|
613
|
+
};
|
|
614
|
+
var ContactExistsError = class extends Error {
|
|
615
|
+
constructor(email) {
|
|
616
|
+
super(`Contact already exists with email: ${email}`);
|
|
617
|
+
this.name = "ContactExistsError";
|
|
618
|
+
}
|
|
619
|
+
};
|
|
620
|
+
var ContactAlreadyConvertedError = class extends Error {
|
|
621
|
+
constructor(id) {
|
|
622
|
+
super(`Contact ${id} has already been converted to a client`);
|
|
623
|
+
this.name = "ContactAlreadyConvertedError";
|
|
624
|
+
}
|
|
625
|
+
};
|
|
626
|
+
|
|
627
|
+
// src/knowledge/observer.ts
|
|
628
|
+
function evaluateConstraint(constraint, entityFields) {
|
|
629
|
+
const targetValue = entityFields[constraint.targetField];
|
|
630
|
+
if (targetValue === void 0 || targetValue === null) return null;
|
|
631
|
+
let violated = false;
|
|
632
|
+
switch (constraint.operator) {
|
|
633
|
+
case "not_in":
|
|
634
|
+
violated = Array.isArray(constraint.values) && constraint.values.includes(targetValue);
|
|
635
|
+
break;
|
|
636
|
+
case "in":
|
|
637
|
+
violated = Array.isArray(constraint.values) && !constraint.values.includes(targetValue);
|
|
638
|
+
break;
|
|
639
|
+
case "not_equal":
|
|
640
|
+
violated = targetValue === constraint.value;
|
|
641
|
+
break;
|
|
642
|
+
case "less_than":
|
|
643
|
+
violated = typeof targetValue === "number" && typeof constraint.value === "number" && targetValue >= constraint.value;
|
|
644
|
+
break;
|
|
645
|
+
case "greater_than":
|
|
646
|
+
violated = typeof targetValue === "number" && typeof constraint.value === "number" && targetValue <= constraint.value;
|
|
647
|
+
break;
|
|
648
|
+
case "regex":
|
|
649
|
+
if (typeof targetValue === "string" && constraint.pattern) {
|
|
650
|
+
try {
|
|
651
|
+
violated = !new RegExp(constraint.pattern).test(targetValue);
|
|
652
|
+
} catch (error) {
|
|
653
|
+
const err = knowledgeError("KNOWLEDGE_352", { pattern: constraint.pattern, error: String(error) });
|
|
654
|
+
console.error(`[${err.ref}] ${err.description}`, error);
|
|
655
|
+
return null;
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
break;
|
|
659
|
+
}
|
|
660
|
+
if (!violated) return null;
|
|
661
|
+
return {
|
|
662
|
+
fieldKey: constraint.targetField,
|
|
663
|
+
constraintField: constraint.targetField,
|
|
664
|
+
operator: constraint.operator,
|
|
665
|
+
actualValue: targetValue,
|
|
666
|
+
constraintValue: constraint.values ?? constraint.value ?? constraint.pattern,
|
|
667
|
+
message: constraint.message,
|
|
668
|
+
severity: constraint.severity
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
async function checkConstraints(db, entityType, entityId) {
|
|
672
|
+
try {
|
|
673
|
+
const allFieldDefs = await db.entityFieldDefinition.findMany({
|
|
674
|
+
where: {
|
|
675
|
+
entityType,
|
|
676
|
+
isActive: true
|
|
677
|
+
}
|
|
678
|
+
});
|
|
679
|
+
const fieldDefs = allFieldDefs.filter(
|
|
680
|
+
(fd) => fd.constraints != null
|
|
681
|
+
);
|
|
682
|
+
if (fieldDefs.length === 0) return [];
|
|
683
|
+
const entries = await db.knowledgeLedgerEntry.findMany({
|
|
684
|
+
where: { entityType, entityId, supersededAt: null },
|
|
685
|
+
include: { field: true }
|
|
686
|
+
});
|
|
687
|
+
const entityFields = {};
|
|
688
|
+
for (const entry of entries) {
|
|
689
|
+
entityFields[entry.field.key] = entry.value;
|
|
690
|
+
}
|
|
691
|
+
const violations = [];
|
|
692
|
+
for (const fieldDef of fieldDefs) {
|
|
693
|
+
const constraints = fieldDef.constraints;
|
|
694
|
+
if (!constraints || !Array.isArray(constraints)) continue;
|
|
695
|
+
for (const constraint of constraints) {
|
|
696
|
+
const violation = evaluateConstraint(constraint, entityFields);
|
|
697
|
+
if (violation) {
|
|
698
|
+
violations.push(violation);
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
return violations;
|
|
703
|
+
} catch (error) {
|
|
704
|
+
const err = knowledgeError("KNOWLEDGE_350", { entityType, entityId, error: String(error) });
|
|
705
|
+
console.error(`[${err.ref}] ${err.description}`, error);
|
|
706
|
+
throw error;
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
async function fireObservers(db, entityType, entityId, writtenFields, callbacks) {
|
|
710
|
+
try {
|
|
711
|
+
const violations = await checkConstraints(db, entityType, entityId);
|
|
712
|
+
if (violations.length > 0 && callbacks?.onConstraintViolation) {
|
|
713
|
+
try {
|
|
714
|
+
await callbacks.onConstraintViolation(entityType, entityId, violations);
|
|
715
|
+
} catch (error) {
|
|
716
|
+
const err = knowledgeError("KNOWLEDGE_351", { entityType, entityId, error: String(error) });
|
|
717
|
+
console.error(`[${err.ref}] ${err.description}`, error);
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
if (violations.length > 0) {
|
|
721
|
+
try {
|
|
722
|
+
await callbacks?.emitEvent?.("KNOWLEDGE_DISSONANCE", {
|
|
723
|
+
entityType,
|
|
724
|
+
entityId,
|
|
725
|
+
violations,
|
|
726
|
+
triggeredBy: writtenFields
|
|
727
|
+
});
|
|
728
|
+
} catch {
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
return violations;
|
|
732
|
+
} catch (error) {
|
|
733
|
+
const err = knowledgeError("KNOWLEDGE_350", { entityType, entityId, error: String(error) });
|
|
734
|
+
console.error(`[${err.ref}] ${err.description}`, error);
|
|
735
|
+
return [];
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
export {
|
|
740
|
+
KNOWLEDGE_ERRORS,
|
|
741
|
+
AGENTKNOW_ERRORS,
|
|
742
|
+
knowledgeError,
|
|
743
|
+
agentKnowledgeError,
|
|
744
|
+
ContactNotFoundError,
|
|
745
|
+
ContactExistsError,
|
|
746
|
+
ContactAlreadyConvertedError,
|
|
747
|
+
evaluateConstraint,
|
|
748
|
+
checkConstraints,
|
|
749
|
+
fireObservers
|
|
750
|
+
};
|