@agentbadge/database 0.6.21 → 0.6.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/database.d.ts +6 -0
- package/dist/database.js +7 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +2 -0
- package/dist/prisma/contract.json +4453 -3034
- package/dist/repositories/interaction-repository.js +21 -17
- package/dist/repositories/knowledge-repository.d.ts +83 -0
- package/dist/repositories/knowledge-repository.js +181 -0
- package/dist/repositories/knowledge-taxonomy-repository.d.ts +106 -0
- package/dist/repositories/knowledge-taxonomy-repository.js +285 -0
- package/package.json +1 -1
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import { param } from "@prisma/orm-postgres/relational-core/expression";
|
|
2
|
+
const txt = (v) => param(v ?? null, { codecId: "pg/text@1" });
|
|
3
|
+
function isUniqueViolation(err) {
|
|
4
|
+
const m = err instanceof Error ? err.message : String(err);
|
|
5
|
+
return /unique|duplicate/i.test(m);
|
|
6
|
+
}
|
|
7
|
+
export const KNOWLEDGE_EVIDENCE_CAP = 50;
|
|
8
|
+
export const KNOWLEDGE_GAP_SAMPLES_CAP = 10;
|
|
9
|
+
export class KnowledgeTaxonomyRepository {
|
|
10
|
+
db;
|
|
11
|
+
constructor(db) {
|
|
12
|
+
this.db = db;
|
|
13
|
+
}
|
|
14
|
+
async upsertCapability(input) {
|
|
15
|
+
const plan = this.db.raw.sql `
|
|
16
|
+
INSERT INTO "knowledgeCapability"
|
|
17
|
+
("id","name","kind","description","source","verified","createdAt","updatedAt")
|
|
18
|
+
VALUES (${txt(input.id)}, ${txt(input.name)}, ${txt(input.kind)},
|
|
19
|
+
${txt(input.description)}, ${txt(input.source)}, ${param(input.verified, { codecId: "pg/bool@1" })},
|
|
20
|
+
now(), now())
|
|
21
|
+
ON CONFLICT ("name") DO UPDATE SET
|
|
22
|
+
"kind" = EXCLUDED."kind",
|
|
23
|
+
"description" = EXCLUDED."description",
|
|
24
|
+
"verified" = EXCLUDED."verified",
|
|
25
|
+
"updatedAt" = now()
|
|
26
|
+
RETURNING "id","name","kind","description","source","verified","createdAt","updatedAt",
|
|
27
|
+
(xmax = 0) AS "created"`
|
|
28
|
+
.returnsRow({
|
|
29
|
+
id: "pg/text@1",
|
|
30
|
+
name: "pg/text@1",
|
|
31
|
+
kind: "pg/text@1",
|
|
32
|
+
description: "pg/text@1",
|
|
33
|
+
source: "pg/text@1",
|
|
34
|
+
verified: "pg/bool@1",
|
|
35
|
+
createdAt: "pg/timestamptz-string@1",
|
|
36
|
+
updatedAt: "pg/timestamptz-string@1",
|
|
37
|
+
created: "pg/bool@1",
|
|
38
|
+
})
|
|
39
|
+
.build();
|
|
40
|
+
const rows = await this.db.runtime().query(plan);
|
|
41
|
+
for await (const r of rows) {
|
|
42
|
+
return {
|
|
43
|
+
row: {
|
|
44
|
+
id: r.id,
|
|
45
|
+
name: r.name,
|
|
46
|
+
kind: r.kind,
|
|
47
|
+
description: r.description,
|
|
48
|
+
source: r.source,
|
|
49
|
+
verified: r.verified,
|
|
50
|
+
createdAt: r.createdAt,
|
|
51
|
+
updatedAt: r.updatedAt,
|
|
52
|
+
},
|
|
53
|
+
created: r.created,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
throw new Error("upsertCapability: empty result");
|
|
57
|
+
}
|
|
58
|
+
async getCapability(id) {
|
|
59
|
+
return this.db.orm.public.KnowledgeCapability.where({ id }).first();
|
|
60
|
+
}
|
|
61
|
+
async getCapabilityByName(name) {
|
|
62
|
+
return this.db.orm.public.KnowledgeCapability.where({ name }).first();
|
|
63
|
+
}
|
|
64
|
+
async listCapabilities(opts) {
|
|
65
|
+
let q = this.db.orm.public.KnowledgeCapability.orderBy((r) => r.name.asc());
|
|
66
|
+
if (opts?.verified !== undefined)
|
|
67
|
+
q = q.where({ verified: opts.verified });
|
|
68
|
+
if (opts?.kind !== undefined)
|
|
69
|
+
q = q.where({ kind: opts.kind });
|
|
70
|
+
return q.limit(opts?.limit ?? 1000).all();
|
|
71
|
+
}
|
|
72
|
+
async linkCapability(itemId, capabilityId) {
|
|
73
|
+
const plan = this.db.raw.sql `
|
|
74
|
+
INSERT INTO "knowledgeItemCapability" ("itemId","capabilityId")
|
|
75
|
+
VALUES (${txt(itemId)}, ${txt(capabilityId)})
|
|
76
|
+
ON CONFLICT ("itemId","capabilityId") DO NOTHING`
|
|
77
|
+
.affectedCount()
|
|
78
|
+
.build();
|
|
79
|
+
const stats = await this.db.runtime().execute(plan);
|
|
80
|
+
return { created: stats.affectedRows > 0 };
|
|
81
|
+
}
|
|
82
|
+
async unlinkCapability(itemId, capabilityId) {
|
|
83
|
+
const plan = this.db.raw.sql `
|
|
84
|
+
DELETE FROM "knowledgeItemCapability"
|
|
85
|
+
WHERE "itemId" = ${txt(itemId)} AND "capabilityId" = ${txt(capabilityId)}`
|
|
86
|
+
.affectedCount()
|
|
87
|
+
.build();
|
|
88
|
+
const stats = await this.db.runtime().execute(plan);
|
|
89
|
+
return { removed: stats.affectedRows > 0 };
|
|
90
|
+
}
|
|
91
|
+
async capabilitiesForItem(itemId) {
|
|
92
|
+
const plan = this.db.raw.sql `
|
|
93
|
+
SELECT cp."id","name","kind","description","source","verified","createdAt","updatedAt"
|
|
94
|
+
FROM "knowledgeCapability" cp
|
|
95
|
+
JOIN "knowledgeItemCapability" ic ON ic."capabilityId" = cp."id"
|
|
96
|
+
WHERE ic."itemId" = ${txt(itemId)}
|
|
97
|
+
ORDER BY cp."name"`
|
|
98
|
+
.returnsRow({
|
|
99
|
+
id: "pg/text@1",
|
|
100
|
+
name: "pg/text@1",
|
|
101
|
+
kind: "pg/text@1",
|
|
102
|
+
description: "pg/text@1",
|
|
103
|
+
source: "pg/text@1",
|
|
104
|
+
verified: "pg/bool@1",
|
|
105
|
+
createdAt: "pg/timestamptz-string@1",
|
|
106
|
+
updatedAt: "pg/timestamptz-string@1",
|
|
107
|
+
})
|
|
108
|
+
.build();
|
|
109
|
+
const rows = await this.db.runtime().query(plan);
|
|
110
|
+
const out = [];
|
|
111
|
+
for await (const r of rows)
|
|
112
|
+
out.push(r);
|
|
113
|
+
return out;
|
|
114
|
+
}
|
|
115
|
+
async itemsForCapability(name) {
|
|
116
|
+
const plan = this.db.raw.sql `
|
|
117
|
+
SELECT ic."itemId" AS "itemId"
|
|
118
|
+
FROM "knowledgeItemCapability" ic
|
|
119
|
+
JOIN "knowledgeCapability" cp ON cp."id" = ic."capabilityId"
|
|
120
|
+
WHERE cp."name" = ${txt(name)}`
|
|
121
|
+
.returnsRow({ itemId: "pg/text@1" })
|
|
122
|
+
.build();
|
|
123
|
+
const rows = await this.db.runtime().query(plan);
|
|
124
|
+
const out = [];
|
|
125
|
+
for await (const r of rows)
|
|
126
|
+
out.push(r.itemId);
|
|
127
|
+
return out;
|
|
128
|
+
}
|
|
129
|
+
async addRelation(input) {
|
|
130
|
+
try {
|
|
131
|
+
const row = await this.db.orm.public.KnowledgeRelation.create({
|
|
132
|
+
id: input.id,
|
|
133
|
+
fromItemId: input.fromItemId,
|
|
134
|
+
toItemId: input.toItemId,
|
|
135
|
+
type: input.type,
|
|
136
|
+
createdBy: input.createdBy,
|
|
137
|
+
});
|
|
138
|
+
return { row, created: true };
|
|
139
|
+
}
|
|
140
|
+
catch (err) {
|
|
141
|
+
if (isUniqueViolation(err))
|
|
142
|
+
return { row: null, created: false };
|
|
143
|
+
throw err;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
async removeRelation(id) {
|
|
147
|
+
const plan = this.db.raw.sql `
|
|
148
|
+
DELETE FROM "knowledgeRelation" WHERE "id" = ${txt(id)}`
|
|
149
|
+
.affectedCount()
|
|
150
|
+
.build();
|
|
151
|
+
const stats = await this.db.runtime().execute(plan);
|
|
152
|
+
return { removed: stats.affectedRows > 0 };
|
|
153
|
+
}
|
|
154
|
+
async relationsFor(itemId) {
|
|
155
|
+
const orm = this.db.orm.public.KnowledgeRelation;
|
|
156
|
+
const outgoing = await orm.where({ fromItemId: itemId }).all();
|
|
157
|
+
const incoming = await orm.where({ toItemId: itemId }).all();
|
|
158
|
+
return { outgoing, incoming };
|
|
159
|
+
}
|
|
160
|
+
async addEvidence(input, cap = KNOWLEDGE_EVIDENCE_CAP) {
|
|
161
|
+
return this.db.transaction(async (tx) => {
|
|
162
|
+
const v = await tx.orm.public.KnowledgeVersion.where({
|
|
163
|
+
id: input.versionId,
|
|
164
|
+
}).first();
|
|
165
|
+
if (!v)
|
|
166
|
+
return { ok: false, reason: "missing_version" };
|
|
167
|
+
const plan = this.db.raw.sql `
|
|
168
|
+
SELECT count(*)::int AS "n" FROM "knowledgeEvidence"
|
|
169
|
+
WHERE "versionId" = ${txt(input.versionId)}`
|
|
170
|
+
.returnsRow({ n: "pg/int4@1" })
|
|
171
|
+
.build();
|
|
172
|
+
const counted = await tx.query(plan);
|
|
173
|
+
let n = 0;
|
|
174
|
+
for await (const r of counted)
|
|
175
|
+
n = r.n;
|
|
176
|
+
if (n >= cap)
|
|
177
|
+
return { ok: false, reason: "cap_exceeded" };
|
|
178
|
+
const row = await tx.orm.public.KnowledgeEvidence.create({
|
|
179
|
+
id: input.id,
|
|
180
|
+
versionId: input.versionId,
|
|
181
|
+
type: input.type,
|
|
182
|
+
uri: input.uri,
|
|
183
|
+
label: input.label ?? null,
|
|
184
|
+
addedBy: input.addedBy,
|
|
185
|
+
});
|
|
186
|
+
return { ok: true, row };
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
async listEvidence(versionId) {
|
|
190
|
+
return this.db.orm.public.KnowledgeEvidence.where({ versionId })
|
|
191
|
+
.orderBy((r) => r.createdAt.asc())
|
|
192
|
+
.all();
|
|
193
|
+
}
|
|
194
|
+
async removeEvidence(id) {
|
|
195
|
+
const plan = this.db.raw.sql `
|
|
196
|
+
DELETE FROM "knowledgeEvidence" WHERE "id" = ${txt(id)}`
|
|
197
|
+
.affectedCount()
|
|
198
|
+
.build();
|
|
199
|
+
const stats = await this.db.runtime().execute(plan);
|
|
200
|
+
return { removed: stats.affectedRows > 0 };
|
|
201
|
+
}
|
|
202
|
+
async itemsCiting(uri) {
|
|
203
|
+
return this.db.orm.public.KnowledgeEvidence.where({ uri }).all();
|
|
204
|
+
}
|
|
205
|
+
async recordGap(input, cap = KNOWLEDGE_GAP_SAMPLES_CAP) {
|
|
206
|
+
const samples = JSON.stringify([input.sampleQuery]);
|
|
207
|
+
const plan = this.db.raw.sql `
|
|
208
|
+
INSERT INTO "knowledgeGap"
|
|
209
|
+
("id","normalizedQuery","sampleQueries","count","capabilityRef","intent","source","sourceRef","status","linkedItemId","firstSeenAt","lastSeenAt")
|
|
210
|
+
VALUES (${txt(input.id)}, ${txt(input.normalizedQuery)},
|
|
211
|
+
to_jsonb(ARRAY[${txt(input.sampleQuery)}::text])::json, 1,
|
|
212
|
+
${txt(input.capabilityRef)}, ${txt(input.intent)},
|
|
213
|
+
${txt(input.source)}, ${txt(input.sourceRef)},
|
|
214
|
+
'OPEN', NULL, now(), now())
|
|
215
|
+
ON CONFLICT ("normalizedQuery") DO UPDATE SET
|
|
216
|
+
"count" = "knowledgeGap"."count" + 1,
|
|
217
|
+
"lastSeenAt" = now(),
|
|
218
|
+
"capabilityRef" = COALESCE("knowledgeGap"."capabilityRef", EXCLUDED."capabilityRef"),
|
|
219
|
+
"intent" = COALESCE("knowledgeGap"."intent", EXCLUDED."intent"),
|
|
220
|
+
"sampleQueries" = CASE
|
|
221
|
+
WHEN jsonb_array_length("knowledgeGap"."sampleQueries"::jsonb) < ${param(cap, { codecId: "pg/int4@1" })}
|
|
222
|
+
AND NOT ("knowledgeGap"."sampleQueries"::jsonb ? ${txt(input.sampleQuery)})
|
|
223
|
+
THEN ("knowledgeGap"."sampleQueries"::jsonb || to_jsonb(ARRAY[${txt(input.sampleQuery)}::text]))::json
|
|
224
|
+
ELSE "knowledgeGap"."sampleQueries"
|
|
225
|
+
END
|
|
226
|
+
RETURNING "id","normalizedQuery","sampleQueries","count","capabilityRef",
|
|
227
|
+
"intent","source","sourceRef","status","linkedItemId","firstSeenAt","lastSeenAt",
|
|
228
|
+
(xmax = 0) AS "created"`
|
|
229
|
+
.returnsRow({
|
|
230
|
+
id: "pg/text@1",
|
|
231
|
+
normalizedQuery: "pg/text@1",
|
|
232
|
+
sampleQueries: "pg/json@1",
|
|
233
|
+
count: "pg/int4@1",
|
|
234
|
+
capabilityRef: "pg/text@1",
|
|
235
|
+
intent: "pg/text@1",
|
|
236
|
+
source: "pg/text@1",
|
|
237
|
+
sourceRef: "pg/text@1",
|
|
238
|
+
status: "pg/text@1",
|
|
239
|
+
linkedItemId: "pg/text@1",
|
|
240
|
+
firstSeenAt: "pg/timestamptz-string@1",
|
|
241
|
+
lastSeenAt: "pg/timestamptz-string@1",
|
|
242
|
+
created: "pg/bool@1",
|
|
243
|
+
})
|
|
244
|
+
.build();
|
|
245
|
+
const rows = await this.db.runtime().query(plan);
|
|
246
|
+
for await (const r of rows) {
|
|
247
|
+
const { created, ...rest } = r;
|
|
248
|
+
return {
|
|
249
|
+
row: { ...rest, sampleQueries: rest.sampleQueries },
|
|
250
|
+
created,
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
throw new Error("recordGap: empty result");
|
|
254
|
+
}
|
|
255
|
+
async getGap(id) {
|
|
256
|
+
return this.db.orm.public.KnowledgeGap.where({ id }).first();
|
|
257
|
+
}
|
|
258
|
+
async listGaps(opts) {
|
|
259
|
+
const base = this.db.orm.public.KnowledgeGap;
|
|
260
|
+
const ordered = opts?.orderBy === "lastSeen"
|
|
261
|
+
? base.orderBy((r) => r.lastSeenAt.desc())
|
|
262
|
+
: base.orderBy((r) => r.count.desc());
|
|
263
|
+
const q = opts?.status !== undefined ? ordered.where({ status: opts.status }) : ordered;
|
|
264
|
+
return q.limit(opts?.limit ?? 500).all();
|
|
265
|
+
}
|
|
266
|
+
async transitionGap(id, input) {
|
|
267
|
+
const cur = await this.getGap(id);
|
|
268
|
+
if (!cur)
|
|
269
|
+
return { gap: null, transitioned: false, reason: "missing" };
|
|
270
|
+
const plan = input.linkedItemId !== undefined
|
|
271
|
+
? this.db.raw
|
|
272
|
+
.sql `UPDATE "knowledgeGap" SET "status" = ${txt(input.to)}, "linkedItemId" = ${txt(input.linkedItemId)} WHERE "id" = ${txt(id)} AND "status" = ${txt(input.from)}`
|
|
273
|
+
.affectedCount()
|
|
274
|
+
.build()
|
|
275
|
+
: this.db.raw
|
|
276
|
+
.sql `UPDATE "knowledgeGap" SET "status" = ${txt(input.to)} WHERE "id" = ${txt(id)} AND "status" = ${txt(input.from)}`
|
|
277
|
+
.affectedCount()
|
|
278
|
+
.build();
|
|
279
|
+
const stats = await this.db.runtime().execute(plan);
|
|
280
|
+
if (stats.affectedRows === 0) {
|
|
281
|
+
return { gap: cur, transitioned: false, reason: "status_mismatch" };
|
|
282
|
+
}
|
|
283
|
+
return { gap: await this.getGap(id), transitioned: true };
|
|
284
|
+
}
|
|
285
|
+
}
|
package/package.json
CHANGED