kibi-mcp 0.1.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/bin/kibi-mcp +59 -0
- package/dist/env.js +99 -0
- package/dist/mcpcat.js +129 -0
- package/dist/server.js +673 -0
- package/dist/tools/branch.js +208 -0
- package/dist/tools/check.js +349 -0
- package/dist/tools/context.js +280 -0
- package/dist/tools/coverage-report.js +91 -0
- package/dist/tools/delete.js +100 -0
- package/dist/tools/derive.js +311 -0
- package/dist/tools/impact.js +70 -0
- package/dist/tools/list-types.js +75 -0
- package/dist/tools/prolog-list.js +176 -0
- package/dist/tools/query-relationships.js +176 -0
- package/dist/tools/query.js +364 -0
- package/dist/tools/suggest-shared-facts.js +138 -0
- package/dist/tools/symbols.js +219 -0
- package/dist/tools/upsert.js +228 -0
- package/dist/tools-config.js +448 -0
- package/dist/workspace.js +126 -0
- package/package.json +43 -0
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Kibi — repo-local, per-branch, queryable long-term memory for software projects
|
|
3
|
+
Copyright (C) 2026 Piotr Franczyk
|
|
4
|
+
|
|
5
|
+
This program is free software: you can redistribute it and/or modify
|
|
6
|
+
it under the terms of the GNU Affero General Public License as published by
|
|
7
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
(at your option) any later version.
|
|
9
|
+
|
|
10
|
+
This program is distributed in the hope that it will be useful,
|
|
11
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
GNU Affero General Public License for more details.
|
|
14
|
+
|
|
15
|
+
You should have received a copy of the GNU Affero General Public License
|
|
16
|
+
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
17
|
+
*/
|
|
18
|
+
const VALID_REL_TYPES = [
|
|
19
|
+
"depends_on",
|
|
20
|
+
"specified_by",
|
|
21
|
+
"verified_by",
|
|
22
|
+
"validates",
|
|
23
|
+
"implements",
|
|
24
|
+
"covered_by",
|
|
25
|
+
"constrained_by",
|
|
26
|
+
"constrains",
|
|
27
|
+
"requires_property",
|
|
28
|
+
"guards",
|
|
29
|
+
"publishes",
|
|
30
|
+
"consumes",
|
|
31
|
+
"supersedes",
|
|
32
|
+
"relates_to",
|
|
33
|
+
];
|
|
34
|
+
/**
|
|
35
|
+
* Handle kb_query_relationships tool calls.
|
|
36
|
+
* Queries the kb_relationship/3 predicate which has arity (Type, From, To).
|
|
37
|
+
*
|
|
38
|
+
* Note: kb_relationship/3 requires RelType to be bound (atom_concat/3 in Prolog
|
|
39
|
+
* does not work with an unbound first argument). When no type filter is given,
|
|
40
|
+
* we iterate over all known type values.
|
|
41
|
+
*/
|
|
42
|
+
export async function handleKbQueryRelationships(prolog, args) {
|
|
43
|
+
const { from, to, type } = args;
|
|
44
|
+
if (type && !VALID_REL_TYPES.includes(type)) {
|
|
45
|
+
throw new Error(`Invalid relationship type '${type}'. Valid types: ${VALID_REL_TYPES.join(", ")}`);
|
|
46
|
+
}
|
|
47
|
+
// When type is specified we run one query; otherwise iterate all known types
|
|
48
|
+
// (kb_relationship/3 requires the type to be bound due to atom_concat/3 in Prolog).
|
|
49
|
+
const typesToQuery = type ? [type] : VALID_REL_TYPES;
|
|
50
|
+
const allRelationships = [];
|
|
51
|
+
for (const relType of typesToQuery) {
|
|
52
|
+
// We collect what we actually need based on which args are bound.
|
|
53
|
+
// When both from and to are specified, we just need to check existence.
|
|
54
|
+
// Otherwise collect the unbound sides.
|
|
55
|
+
let goal;
|
|
56
|
+
if (from && to) {
|
|
57
|
+
// Check if the specific triple exists
|
|
58
|
+
goal = `(kb_relationship('${relType}', '${from}', '${to}') -> Results = [['${from}','${to}']] ; Results = [])`;
|
|
59
|
+
}
|
|
60
|
+
else if (from) {
|
|
61
|
+
goal = `findall(To, kb_relationship('${relType}', '${from}', To), Results)`;
|
|
62
|
+
}
|
|
63
|
+
else if (to) {
|
|
64
|
+
goal = `findall(From, kb_relationship('${relType}', From, '${to}'), Results)`;
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
goal = `findall([From,To], kb_relationship('${relType}', From, To), Results)`;
|
|
68
|
+
}
|
|
69
|
+
const queryResult = await prolog.query(goal);
|
|
70
|
+
if (!queryResult.success) {
|
|
71
|
+
throw new Error(queryResult.error || "Relationship query failed");
|
|
72
|
+
}
|
|
73
|
+
if (queryResult.bindings.Results) {
|
|
74
|
+
const raw = queryResult.bindings.Results;
|
|
75
|
+
if (from && to) {
|
|
76
|
+
// Results is either [[from,to]] or []
|
|
77
|
+
const pairs = parsePairResults(raw);
|
|
78
|
+
for (const [pairFrom, pairTo] of pairs) {
|
|
79
|
+
allRelationships.push({ relType, from: pairFrom, to: pairTo });
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
else if (from) {
|
|
83
|
+
// Results is [To, To, ...]
|
|
84
|
+
const ids = parseIdList(raw);
|
|
85
|
+
for (const toId of ids) {
|
|
86
|
+
allRelationships.push({ relType, from, to: toId });
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
else if (to) {
|
|
90
|
+
// Results is [From, From, ...]
|
|
91
|
+
const ids = parseIdList(raw);
|
|
92
|
+
for (const fromId of ids) {
|
|
93
|
+
allRelationships.push({ relType, from: fromId, to });
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
// Results is [[From,To], ...]
|
|
98
|
+
const pairs = parsePairResults(raw);
|
|
99
|
+
for (const [pairFrom, pairTo] of pairs) {
|
|
100
|
+
allRelationships.push({ relType, from: pairFrom, to: pairTo });
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
const text = allRelationships.length === 0
|
|
106
|
+
? "No relationships found."
|
|
107
|
+
: `Found ${allRelationships.length} relationship(s): ${allRelationships
|
|
108
|
+
.map((r) => `${r.from} -[${r.relType}]-> ${r.to}`)
|
|
109
|
+
.join(", ")}`;
|
|
110
|
+
return {
|
|
111
|
+
content: [{ type: "text", text }],
|
|
112
|
+
structuredContent: {
|
|
113
|
+
relationships: allRelationships,
|
|
114
|
+
count: allRelationships.length,
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Parse a flat Prolog list of atoms "[A,B,C]" into a string array.
|
|
120
|
+
*/
|
|
121
|
+
function parseIdList(raw) {
|
|
122
|
+
const cleaned = raw.trim();
|
|
123
|
+
if (cleaned === "[]" || cleaned === "")
|
|
124
|
+
return [];
|
|
125
|
+
const inner = cleaned.replace(/^\[/, "").replace(/\]$/, "");
|
|
126
|
+
return inner
|
|
127
|
+
.split(",")
|
|
128
|
+
.map((s) => s.trim().replace(/^'|'$/g, "").replace(/^"|"$/g, ""))
|
|
129
|
+
.filter(Boolean);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Parse Prolog findall result "[[From,To],...]" into [from, to] pairs.
|
|
133
|
+
*/
|
|
134
|
+
function parsePairResults(raw) {
|
|
135
|
+
const cleaned = raw.trim();
|
|
136
|
+
if (cleaned === "[]" || cleaned === "")
|
|
137
|
+
return [];
|
|
138
|
+
const inner = cleaned.replace(/^\[/, "").replace(/\]$/, "");
|
|
139
|
+
const pairs = [];
|
|
140
|
+
let depth = 0;
|
|
141
|
+
let current = "";
|
|
142
|
+
for (let i = 0; i < inner.length; i++) {
|
|
143
|
+
const ch = inner[i];
|
|
144
|
+
if (ch === "[") {
|
|
145
|
+
depth++;
|
|
146
|
+
current += ch;
|
|
147
|
+
}
|
|
148
|
+
else if (ch === "]") {
|
|
149
|
+
depth--;
|
|
150
|
+
current += ch;
|
|
151
|
+
if (depth === 0) {
|
|
152
|
+
const pair = parsePair(current.trim());
|
|
153
|
+
if (pair)
|
|
154
|
+
pairs.push(pair);
|
|
155
|
+
current = "";
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
else if (ch === "," && depth === 0) {
|
|
159
|
+
// top-level separator between pairs — skip
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
current += ch;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return pairs;
|
|
166
|
+
}
|
|
167
|
+
function parsePair(pairStr) {
|
|
168
|
+
// expect "[From,To]"
|
|
169
|
+
const inner = pairStr.replace(/^\[/, "").replace(/\]$/, "").trim();
|
|
170
|
+
const parts = inner
|
|
171
|
+
.split(",")
|
|
172
|
+
.map((s) => s.trim().replace(/^'|'$/g, "").replace(/^"|"$/g, ""));
|
|
173
|
+
if (parts.length < 2)
|
|
174
|
+
return null;
|
|
175
|
+
return [parts[0], parts[1]];
|
|
176
|
+
}
|
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Kibi — repo-local, per-branch, queryable long-term memory for software projects
|
|
3
|
+
Copyright (C) 2026 Piotr Franczyk
|
|
4
|
+
|
|
5
|
+
This program is free software: you can redistribute it and/or modify
|
|
6
|
+
it under the terms of the GNU Affero General Public License as published by
|
|
7
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
(at your option) any later version.
|
|
9
|
+
|
|
10
|
+
This program is distributed in the hope that it will be useful,
|
|
11
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
GNU Affero General Public License for more details.
|
|
14
|
+
|
|
15
|
+
You should have received a copy of the GNU Affero General Public License
|
|
16
|
+
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
17
|
+
*/
|
|
18
|
+
export const VALID_ENTITY_TYPES = [
|
|
19
|
+
"req",
|
|
20
|
+
"scenario",
|
|
21
|
+
"test",
|
|
22
|
+
"adr",
|
|
23
|
+
"flag",
|
|
24
|
+
"event",
|
|
25
|
+
"symbol",
|
|
26
|
+
"fact",
|
|
27
|
+
];
|
|
28
|
+
/**
|
|
29
|
+
* Handle kb.query tool calls
|
|
30
|
+
* Reuses query logic from CLI command
|
|
31
|
+
*/
|
|
32
|
+
export async function handleKbQuery(prolog, args) {
|
|
33
|
+
const { type, id, tags, sourceFile, limit = 100, offset = 0 } = args;
|
|
34
|
+
try {
|
|
35
|
+
let results = [];
|
|
36
|
+
// Validate type if provided
|
|
37
|
+
if (type) {
|
|
38
|
+
if (!VALID_ENTITY_TYPES.includes(type)) {
|
|
39
|
+
throw new Error(`Invalid type '${type}'. Valid types: ${VALID_ENTITY_TYPES.join(", ")}. Use a single type value, or omit this parameter to query all entities.`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
// Build Prolog query
|
|
43
|
+
let goal;
|
|
44
|
+
if (sourceFile) {
|
|
45
|
+
const safeSource = sourceFile.replace(/'/g, "\\'");
|
|
46
|
+
if (type) {
|
|
47
|
+
goal = `findall([Id,'${type}',Props], (kb_entities_by_source('${safeSource}', SourceIds), member(Id, SourceIds), kb_entity(Id, '${type}', Props)), Results)`;
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
goal = `findall([Id,Type,Props], (kb_entities_by_source('${safeSource}', SourceIds), member(Id, SourceIds), kb_entity(Id, Type, Props)), Results)`;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
else if (id && type) {
|
|
54
|
+
goal = `kb_entity('${id}', '${type}', Props), Id = '${id}', Type = '${type}', Result = [Id, Type, Props]`;
|
|
55
|
+
}
|
|
56
|
+
else if (id) {
|
|
57
|
+
goal = `findall(['${id}',Type,Props], kb_entity('${id}', Type, Props), Results)`;
|
|
58
|
+
}
|
|
59
|
+
else if (tags && tags.length > 0) {
|
|
60
|
+
const tagList = `[${tags.map((t) => `'${t}'`).join(",")}]`;
|
|
61
|
+
if (type) {
|
|
62
|
+
goal = `findall([Id,'${type}',Props], (kb_entity(Id, '${type}', Props), memberchk(tags=Tags, Props), member(Tag, Tags), member(Tag, ${tagList})), Results)`;
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
goal = `findall([Id,Type,Props], (kb_entity(Id, Type, Props), memberchk(tags=Tags, Props), member(Tag, Tags), member(Tag, ${tagList})), Results)`;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
else if (type) {
|
|
69
|
+
goal = `findall([Id,'${type}',Props], kb_entity(Id, '${type}', Props), Results)`;
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
goal = "findall([Id,Type,Props], kb_entity(Id, Type, Props), Results)";
|
|
73
|
+
}
|
|
74
|
+
const queryResult = await prolog.query(goal);
|
|
75
|
+
if (queryResult.success) {
|
|
76
|
+
if (id && type) {
|
|
77
|
+
// Single entity query
|
|
78
|
+
if (queryResult.bindings.Result) {
|
|
79
|
+
const entity = parseEntityFromBinding(queryResult.bindings.Result);
|
|
80
|
+
results = [entity];
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
// Multiple entities query
|
|
85
|
+
if (queryResult.bindings.Results) {
|
|
86
|
+
const entitiesData = parseListOfLists(queryResult.bindings.Results);
|
|
87
|
+
for (const data of entitiesData) {
|
|
88
|
+
const entity = parseEntityFromList(data);
|
|
89
|
+
results.push(entity);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
throw new Error(queryResult.error || "Query failed with unknown error");
|
|
96
|
+
}
|
|
97
|
+
// Apply pagination
|
|
98
|
+
const paginated = results.slice(offset, offset + limit);
|
|
99
|
+
// Build human-readable text with entity IDs and titles
|
|
100
|
+
let text;
|
|
101
|
+
if (results.length === 0) {
|
|
102
|
+
text = `No entities found${type ? ` of type '${type}'` : ""}.`;
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
const details = paginated
|
|
106
|
+
.map((e) => {
|
|
107
|
+
const id = e.id.replace(/^file:\/\/.*\//, "");
|
|
108
|
+
const title = e.title;
|
|
109
|
+
const status = e.status;
|
|
110
|
+
return `${id} (${title}, status=${status})`;
|
|
111
|
+
})
|
|
112
|
+
.join(", ");
|
|
113
|
+
text = `Found ${results.length} entities${type ? ` of type '${type}'` : ""}. Showing ${paginated.length} (offset ${offset}, limit ${limit}): ${details}`;
|
|
114
|
+
}
|
|
115
|
+
// Return MCP structured response
|
|
116
|
+
return {
|
|
117
|
+
content: [
|
|
118
|
+
{
|
|
119
|
+
type: "text",
|
|
120
|
+
text,
|
|
121
|
+
},
|
|
122
|
+
],
|
|
123
|
+
structuredContent: {
|
|
124
|
+
entities: paginated,
|
|
125
|
+
count: results.length,
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
131
|
+
throw new Error(`Query execution failed: ${message}`);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Parse a Prolog list of lists into a JavaScript array.
|
|
136
|
+
* Input: "[[a,b,c],[d,e,f]]"
|
|
137
|
+
* Output: [["a", "b", "c"], ["d", "e", "f"]]
|
|
138
|
+
*/
|
|
139
|
+
export function parseListOfLists(listStr) {
|
|
140
|
+
const cleaned = listStr.trim().replace(/^\[/, "").replace(/\]$/, "");
|
|
141
|
+
if (cleaned === "") {
|
|
142
|
+
return [];
|
|
143
|
+
}
|
|
144
|
+
const results = [];
|
|
145
|
+
let depth = 0;
|
|
146
|
+
let current = "";
|
|
147
|
+
let currentList = [];
|
|
148
|
+
for (let i = 0; i < cleaned.length; i++) {
|
|
149
|
+
const char = cleaned[i];
|
|
150
|
+
if (char === "[") {
|
|
151
|
+
depth++;
|
|
152
|
+
if (depth > 1)
|
|
153
|
+
current += char;
|
|
154
|
+
}
|
|
155
|
+
else if (char === "]") {
|
|
156
|
+
depth--;
|
|
157
|
+
if (depth === 0) {
|
|
158
|
+
if (current) {
|
|
159
|
+
currentList.push(current.trim());
|
|
160
|
+
current = "";
|
|
161
|
+
}
|
|
162
|
+
if (currentList.length > 0) {
|
|
163
|
+
results.push(currentList);
|
|
164
|
+
currentList = [];
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
current += char;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
else if (char === "," && depth === 1) {
|
|
172
|
+
if (current) {
|
|
173
|
+
currentList.push(current.trim());
|
|
174
|
+
current = "";
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
else if (char === "," && depth === 0) {
|
|
178
|
+
// Skip comma between lists
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
current += char;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return results;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Parse a single entity from Prolog binding format.
|
|
188
|
+
* Input: "[abc123, req, [id=abc123, title=\"Test\", ...]]"
|
|
189
|
+
*/
|
|
190
|
+
export function parseEntityFromBinding(bindingStr) {
|
|
191
|
+
const cleaned = bindingStr.trim().replace(/^\[/, "").replace(/\]$/, "");
|
|
192
|
+
const parts = splitTopLevel(cleaned, ",");
|
|
193
|
+
if (parts.length < 3) {
|
|
194
|
+
return {};
|
|
195
|
+
}
|
|
196
|
+
const id = parts[0].trim();
|
|
197
|
+
const type = parts[1].trim();
|
|
198
|
+
const propsStr = parts.slice(2).join(",").trim();
|
|
199
|
+
const props = parsePropertyList(propsStr);
|
|
200
|
+
return { ...props, id: normalizeEntityId(stripOuterQuotes(id)), type };
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Parse entity from array returned by parseListOfLists.
|
|
204
|
+
* Input: ["abc123", "req", "[id=abc123, title=\"Test\", ...]"]
|
|
205
|
+
*/
|
|
206
|
+
export function parseEntityFromList(data) {
|
|
207
|
+
if (data.length < 3) {
|
|
208
|
+
return {};
|
|
209
|
+
}
|
|
210
|
+
const id = data[0].trim();
|
|
211
|
+
const type = data[1].trim();
|
|
212
|
+
const propsStr = data[2].trim();
|
|
213
|
+
const props = parsePropertyList(propsStr);
|
|
214
|
+
return { ...props, id: normalizeEntityId(stripOuterQuotes(id)), type };
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Parse Prolog property list into JavaScript object.
|
|
218
|
+
*/
|
|
219
|
+
export function parsePropertyList(propsStr) {
|
|
220
|
+
const props = {};
|
|
221
|
+
let cleaned = propsStr.trim();
|
|
222
|
+
if (cleaned.startsWith("[")) {
|
|
223
|
+
cleaned = cleaned.substring(1);
|
|
224
|
+
}
|
|
225
|
+
if (cleaned.endsWith("]")) {
|
|
226
|
+
cleaned = cleaned.substring(0, cleaned.length - 1);
|
|
227
|
+
}
|
|
228
|
+
const pairs = splitTopLevel(cleaned, ",");
|
|
229
|
+
for (const pair of pairs) {
|
|
230
|
+
const eqIndex = pair.indexOf("=");
|
|
231
|
+
if (eqIndex === -1)
|
|
232
|
+
continue;
|
|
233
|
+
const key = pair.substring(0, eqIndex).trim();
|
|
234
|
+
const value = pair.substring(eqIndex + 1).trim();
|
|
235
|
+
if (key === "..." || value === "..." || value === "...|...") {
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
const parsed = parsePrologValue(value);
|
|
239
|
+
props[key] = parsed;
|
|
240
|
+
}
|
|
241
|
+
return props;
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Parse a single Prolog value, handling typed literals and URIs.
|
|
245
|
+
*/
|
|
246
|
+
export function parsePrologValue(valueInput) {
|
|
247
|
+
const value = valueInput.trim();
|
|
248
|
+
// Handle typed literal: ^^("value", type)
|
|
249
|
+
if (value.startsWith("^^(")) {
|
|
250
|
+
const innerStart = value.indexOf("(") + 1;
|
|
251
|
+
let depth = 1;
|
|
252
|
+
let innerEnd = innerStart;
|
|
253
|
+
for (let i = innerStart; i < value.length; i++) {
|
|
254
|
+
if (value[i] === "(")
|
|
255
|
+
depth++;
|
|
256
|
+
if (value[i] === ")") {
|
|
257
|
+
depth--;
|
|
258
|
+
if (depth === 0) {
|
|
259
|
+
innerEnd = i;
|
|
260
|
+
break;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
const innerContent = value.substring(innerStart, innerEnd);
|
|
265
|
+
const parts = splitTopLevel(innerContent, ",");
|
|
266
|
+
if (parts.length >= 2) {
|
|
267
|
+
let literalValue = parts[0].trim();
|
|
268
|
+
if (literalValue.startsWith('"') && literalValue.endsWith('"')) {
|
|
269
|
+
literalValue = literalValue.substring(1, literalValue.length - 1);
|
|
270
|
+
}
|
|
271
|
+
// Handle array notation
|
|
272
|
+
if (literalValue.startsWith("[") && literalValue.endsWith("]")) {
|
|
273
|
+
const listContent = literalValue.substring(1, literalValue.length - 1);
|
|
274
|
+
if (listContent === "") {
|
|
275
|
+
return [];
|
|
276
|
+
}
|
|
277
|
+
return splitTopLevel(listContent, ",").map((item) => item.trim());
|
|
278
|
+
}
|
|
279
|
+
return literalValue;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
// Handle URI
|
|
283
|
+
if (value.startsWith("file:///")) {
|
|
284
|
+
const lastSlash = value.lastIndexOf("/");
|
|
285
|
+
if (lastSlash !== -1) {
|
|
286
|
+
return value.substring(lastSlash + 1);
|
|
287
|
+
}
|
|
288
|
+
return value;
|
|
289
|
+
}
|
|
290
|
+
// Handle quoted string
|
|
291
|
+
if (value.startsWith('"') && value.endsWith('"')) {
|
|
292
|
+
return value.substring(1, value.length - 1);
|
|
293
|
+
}
|
|
294
|
+
// Handle quoted atom
|
|
295
|
+
if (value.startsWith("'") && value.endsWith("'")) {
|
|
296
|
+
return value.substring(1, value.length - 1);
|
|
297
|
+
}
|
|
298
|
+
// Handle list
|
|
299
|
+
if (value.startsWith("[") && value.endsWith("]")) {
|
|
300
|
+
const listContent = value.substring(1, value.length - 1);
|
|
301
|
+
if (listContent === "") {
|
|
302
|
+
return [];
|
|
303
|
+
}
|
|
304
|
+
const items = splitTopLevel(listContent, ",").map((item) => {
|
|
305
|
+
return parsePrologValue(item.trim());
|
|
306
|
+
});
|
|
307
|
+
return items;
|
|
308
|
+
}
|
|
309
|
+
return value;
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Split a string by delimiter at the top level (not inside brackets or quotes).
|
|
313
|
+
*/
|
|
314
|
+
export function splitTopLevel(str, delimiter) {
|
|
315
|
+
const results = [];
|
|
316
|
+
let current = "";
|
|
317
|
+
let depth = 0;
|
|
318
|
+
let inQuotes = false;
|
|
319
|
+
for (let i = 0; i < str.length; i++) {
|
|
320
|
+
const char = str[i];
|
|
321
|
+
const prevChar = i > 0 ? str[i - 1] : "";
|
|
322
|
+
if (char === '"' && prevChar !== "\\") {
|
|
323
|
+
inQuotes = !inQuotes;
|
|
324
|
+
current += char;
|
|
325
|
+
}
|
|
326
|
+
else if (!inQuotes && (char === "[" || char === "(")) {
|
|
327
|
+
depth++;
|
|
328
|
+
current += char;
|
|
329
|
+
}
|
|
330
|
+
else if (!inQuotes && (char === "]" || char === ")")) {
|
|
331
|
+
depth--;
|
|
332
|
+
current += char;
|
|
333
|
+
}
|
|
334
|
+
else if (!inQuotes && depth === 0 && char === delimiter) {
|
|
335
|
+
if (current) {
|
|
336
|
+
results.push(current);
|
|
337
|
+
current = "";
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
else {
|
|
341
|
+
current += char;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
if (current) {
|
|
345
|
+
results.push(current);
|
|
346
|
+
}
|
|
347
|
+
return results;
|
|
348
|
+
}
|
|
349
|
+
function stripOuterQuotes(value) {
|
|
350
|
+
if (value.startsWith("'") && value.endsWith("'")) {
|
|
351
|
+
return value.slice(1, -1);
|
|
352
|
+
}
|
|
353
|
+
if (value.startsWith('"') && value.endsWith('"')) {
|
|
354
|
+
return value.slice(1, -1);
|
|
355
|
+
}
|
|
356
|
+
return value;
|
|
357
|
+
}
|
|
358
|
+
function normalizeEntityId(value) {
|
|
359
|
+
if (!value.startsWith("file:///")) {
|
|
360
|
+
return value;
|
|
361
|
+
}
|
|
362
|
+
const idx = value.lastIndexOf("/");
|
|
363
|
+
return idx === -1 ? value : value.slice(idx + 1);
|
|
364
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Kibi — repo-local, per-branch, queryable long-term memory for software projects
|
|
3
|
+
Copyright (C) 2026 Piotr Franczyk
|
|
4
|
+
|
|
5
|
+
This program is free software: you can redistribute it and/or modify
|
|
6
|
+
it under the terms of the GNU Affero General Public License as published by
|
|
7
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
(at your option) any later version.
|
|
9
|
+
|
|
10
|
+
This program is distributed in the hope that it will be useful,
|
|
11
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
GNU Affero General Public License for more details.
|
|
14
|
+
|
|
15
|
+
You should have received a copy of the GNU Affero General Public License
|
|
16
|
+
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
17
|
+
*/
|
|
18
|
+
import { parseAtomList } from "./prolog-list.js";
|
|
19
|
+
/**
|
|
20
|
+
* Handle analyze_shared_facts tool calls
|
|
21
|
+
* Analyzes requirements to suggest shared domain facts for extraction
|
|
22
|
+
*/
|
|
23
|
+
export async function handleSuggestSharedFacts(prolog, args) {
|
|
24
|
+
const minFreq = args.min_frequency ?? 2;
|
|
25
|
+
try {
|
|
26
|
+
// Query all requirements with their text properties
|
|
27
|
+
const reqsResult = await prolog.query("findall([Id,Title], (kb_entity(Id, req, Props), memberchk(title=Title, Props)), Reqs)");
|
|
28
|
+
if (!reqsResult.success || !reqsResult.bindings.Reqs) {
|
|
29
|
+
return {
|
|
30
|
+
content: [{ type: "text", text: "No requirements found in KB" }],
|
|
31
|
+
structuredContent: { suggestions: [], count: 0 },
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
const reqsList = parseAtomList(reqsResult.bindings.Reqs);
|
|
35
|
+
const requirements = [];
|
|
36
|
+
// Parse the list-of-lists format from Prolog
|
|
37
|
+
const reqMatch = reqsList.join("").matchAll(/\[([^,]+),([^\]]+)\]/g);
|
|
38
|
+
if (reqMatch) {
|
|
39
|
+
for (const match of reqMatch) {
|
|
40
|
+
const id = match[1].trim().replace(/^'|'$/g, "");
|
|
41
|
+
const title = match[2].trim().replace(/^'|'$/g, "");
|
|
42
|
+
requirements.push({ id, title, description: title });
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
// Query all existing facts for context
|
|
46
|
+
const factsResult = await prolog.query("findall([Id,Title], (kb_entity(Id, fact, Props), memberchk(title=Title, Props)), Facts)");
|
|
47
|
+
if (!factsResult.success || !factsResult.bindings.Facts) {
|
|
48
|
+
return {
|
|
49
|
+
content: [{ type: "text", text: "No facts found in KB" }],
|
|
50
|
+
structuredContent: { suggestions: [], count: 0 },
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
const factsList = parseAtomList(factsResult.bindings.Facts);
|
|
54
|
+
const existingFacts = new Set();
|
|
55
|
+
const factMatch = factsList.join("").matchAll(/\[([^,]+),([^\]]+)\]/g);
|
|
56
|
+
if (factMatch) {
|
|
57
|
+
for (const match of factMatch) {
|
|
58
|
+
const title = match[2].trim().replace(/^'|'$/g, "");
|
|
59
|
+
existingFacts.add(title.toLowerCase());
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
// Extract and analyze domain concepts from requirements
|
|
63
|
+
const suggestions = analyzeSharedConcepts(requirements, existingFacts, minFreq);
|
|
64
|
+
return {
|
|
65
|
+
content: [
|
|
66
|
+
{
|
|
67
|
+
type: "text",
|
|
68
|
+
text: `Found ${suggestions.length} potential shared fact(s) to consider creating.`,
|
|
69
|
+
},
|
|
70
|
+
],
|
|
71
|
+
structuredContent: {
|
|
72
|
+
suggestions,
|
|
73
|
+
count: suggestions.length,
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
79
|
+
throw new Error(`Shared facts analysis failed: ${message}`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Lightweight heuristic to identify shared domain concepts
|
|
84
|
+
* Focuses on:
|
|
85
|
+
* - Capitalized terms (possible domain concepts)
|
|
86
|
+
* - Repeated phrases across multiple requirements
|
|
87
|
+
* - Excludes existing facts
|
|
88
|
+
*/
|
|
89
|
+
function analyzeSharedConcepts(requirements, existingFacts, minFreq) {
|
|
90
|
+
const conceptCounts = new Map();
|
|
91
|
+
for (const req of requirements) {
|
|
92
|
+
const originalText = `${req.title} ${req.description || ""}`;
|
|
93
|
+
const text = originalText.toLowerCase();
|
|
94
|
+
// Extract capitalized terms (potential domain concepts)
|
|
95
|
+
// Pattern: words starting with capital letters that aren't at sentence start
|
|
96
|
+
const capitalizedTerms = originalText.matchAll(/\b([A-Z][a-z]+)\b/g);
|
|
97
|
+
// Extract repeated phrases (2+ words)
|
|
98
|
+
// Extract repeated phrases (2+ words)
|
|
99
|
+
const words = text.split(/\s+/).filter(w => w.length > 3);
|
|
100
|
+
for (let i = 0; i < words.length - 1; i++) {
|
|
101
|
+
const phrase = `${words[i]} ${words[i + 1]}`;
|
|
102
|
+
if (!conceptCounts.has(phrase)) {
|
|
103
|
+
conceptCounts.set(phrase, new Set());
|
|
104
|
+
}
|
|
105
|
+
conceptCounts.get(phrase).add(req.id);
|
|
106
|
+
}
|
|
107
|
+
// Also track individual capitalized terms
|
|
108
|
+
for (const match of capitalizedTerms) {
|
|
109
|
+
const lowerTerm = match[1].toLowerCase(); // Get the captured group
|
|
110
|
+
if (!conceptCounts.has(lowerTerm)) {
|
|
111
|
+
conceptCounts.set(lowerTerm, new Set());
|
|
112
|
+
}
|
|
113
|
+
conceptCounts.get(lowerTerm).add(req.id);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
// Generate suggestions
|
|
117
|
+
const suggestions = [];
|
|
118
|
+
for (const [concept, reqIds] of conceptCounts) {
|
|
119
|
+
if (reqIds.size >= minFreq) {
|
|
120
|
+
// Skip if this concept already exists as a fact
|
|
121
|
+
if (!existingFacts.has(concept)) {
|
|
122
|
+
suggestions.push({
|
|
123
|
+
concept: capitalizeConcept(concept),
|
|
124
|
+
mentions: reqIds.size,
|
|
125
|
+
requirements: Array.from(reqIds),
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
// Sort by frequency (most mentioned first)
|
|
131
|
+
return suggestions.sort((a, b) => b.mentions - a.mentions);
|
|
132
|
+
}
|
|
133
|
+
function capitalizeConcept(concept) {
|
|
134
|
+
return concept
|
|
135
|
+
.split(/\s+/)
|
|
136
|
+
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
|
|
137
|
+
.join(" ");
|
|
138
|
+
}
|