greplica 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/README.md +177 -0
- package/dist/apps/cli/main.d.ts +2 -0
- package/dist/apps/cli/main.js +404 -0
- package/dist/apps/cli/main.js.map +1 -0
- package/dist/apps/cli/repo-context.d.ts +2 -0
- package/dist/apps/cli/repo-context.js +58 -0
- package/dist/apps/cli/repo-context.js.map +1 -0
- package/dist/libs/agent-runner/codex.d.ts +2 -0
- package/dist/libs/agent-runner/codex.js +53 -0
- package/dist/libs/agent-runner/codex.js.map +1 -0
- package/dist/libs/agent-runner/metrics.d.ts +10 -0
- package/dist/libs/agent-runner/metrics.js +72 -0
- package/dist/libs/agent-runner/metrics.js.map +1 -0
- package/dist/libs/agent-runner/types.d.ts +23 -0
- package/dist/libs/agent-runner/types.js +2 -0
- package/dist/libs/agent-runner/types.js.map +1 -0
- package/dist/libs/config/greplica-config.d.ts +24 -0
- package/dist/libs/config/greplica-config.js +121 -0
- package/dist/libs/config/greplica-config.js.map +1 -0
- package/dist/libs/config/greplica-home.d.ts +1 -0
- package/dist/libs/config/greplica-home.js +6 -0
- package/dist/libs/config/greplica-home.js.map +1 -0
- package/dist/libs/env/load-local-env.d.ts +19 -0
- package/dist/libs/env/load-local-env.js +69 -0
- package/dist/libs/env/load-local-env.js.map +1 -0
- package/dist/libs/install/edit-marked-section.d.ts +1 -0
- package/dist/libs/install/edit-marked-section.js +22 -0
- package/dist/libs/install/edit-marked-section.js.map +1 -0
- package/dist/libs/install/install.d.ts +18 -0
- package/dist/libs/install/install.js +80 -0
- package/dist/libs/install/install.js.map +1 -0
- package/dist/libs/install/instruction-blocks.d.ts +1 -0
- package/dist/libs/install/instruction-blocks.js +13 -0
- package/dist/libs/install/instruction-blocks.js.map +1 -0
- package/dist/libs/install/paths.d.ts +12 -0
- package/dist/libs/install/paths.js +31 -0
- package/dist/libs/install/paths.js.map +1 -0
- package/dist/libs/knowledge-graph/claim.d.ts +11 -0
- package/dist/libs/knowledge-graph/claim.js +2 -0
- package/dist/libs/knowledge-graph/claim.js.map +1 -0
- package/dist/libs/knowledge-graph/commit.d.ts +10 -0
- package/dist/libs/knowledge-graph/commit.js +2 -0
- package/dist/libs/knowledge-graph/commit.js.map +1 -0
- package/dist/libs/knowledge-graph/edge.d.ts +13 -0
- package/dist/libs/knowledge-graph/edge.js +17 -0
- package/dist/libs/knowledge-graph/edge.js.map +1 -0
- package/dist/libs/knowledge-graph/folder-export.d.ts +6 -0
- package/dist/libs/knowledge-graph/folder-export.js +337 -0
- package/dist/libs/knowledge-graph/folder-export.js.map +1 -0
- package/dist/libs/knowledge-graph/graph-context/bm25.d.ts +9 -0
- package/dist/libs/knowledge-graph/graph-context/bm25.js +62 -0
- package/dist/libs/knowledge-graph/graph-context/bm25.js.map +1 -0
- package/dist/libs/knowledge-graph/graph-context/config.d.ts +39 -0
- package/dist/libs/knowledge-graph/graph-context/config.js +48 -0
- package/dist/libs/knowledge-graph/graph-context/config.js.map +1 -0
- package/dist/libs/knowledge-graph/graph-context/context-builder.d.ts +18 -0
- package/dist/libs/knowledge-graph/graph-context/context-builder.js +276 -0
- package/dist/libs/knowledge-graph/graph-context/context-builder.js.map +1 -0
- package/dist/libs/knowledge-graph/graph-context/documents.d.ts +20 -0
- package/dist/libs/knowledge-graph/graph-context/documents.js +81 -0
- package/dist/libs/knowledge-graph/graph-context/documents.js.map +1 -0
- package/dist/libs/knowledge-graph/graph-context/embedder.d.ts +6 -0
- package/dist/libs/knowledge-graph/graph-context/embedder.js +8 -0
- package/dist/libs/knowledge-graph/graph-context/embedder.js.map +1 -0
- package/dist/libs/knowledge-graph/graph-context/exact.d.ts +3 -0
- package/dist/libs/knowledge-graph/graph-context/exact.js +37 -0
- package/dist/libs/knowledge-graph/graph-context/exact.js.map +1 -0
- package/dist/libs/knowledge-graph/graph-context/local-embedder.d.ts +13 -0
- package/dist/libs/knowledge-graph/graph-context/local-embedder.js +74 -0
- package/dist/libs/knowledge-graph/graph-context/local-embedder.js.map +1 -0
- package/dist/libs/knowledge-graph/graph-context/openai-embedder.d.ts +14 -0
- package/dist/libs/knowledge-graph/graph-context/openai-embedder.js +59 -0
- package/dist/libs/knowledge-graph/graph-context/openai-embedder.js.map +1 -0
- package/dist/libs/knowledge-graph/graph-context/rank.d.ts +16 -0
- package/dist/libs/knowledge-graph/graph-context/rank.js +52 -0
- package/dist/libs/knowledge-graph/graph-context/rank.js.map +1 -0
- package/dist/libs/knowledge-graph/graph-context/render.d.ts +38 -0
- package/dist/libs/knowledge-graph/graph-context/render.js +114 -0
- package/dist/libs/knowledge-graph/graph-context/render.js.map +1 -0
- package/dist/libs/knowledge-graph/graph-context/types.d.ts +58 -0
- package/dist/libs/knowledge-graph/graph-context/types.js +2 -0
- package/dist/libs/knowledge-graph/graph-context/types.js.map +1 -0
- package/dist/libs/knowledge-graph/graph-context/vector.d.ts +3 -0
- package/dist/libs/knowledge-graph/graph-context/vector.js +25 -0
- package/dist/libs/knowledge-graph/graph-context/vector.js.map +1 -0
- package/dist/libs/knowledge-graph/proposal.d.ts +65 -0
- package/dist/libs/knowledge-graph/proposal.js +135 -0
- package/dist/libs/knowledge-graph/proposal.js.map +1 -0
- package/dist/libs/knowledge-graph/schema.d.ts +32 -0
- package/dist/libs/knowledge-graph/schema.js +2 -0
- package/dist/libs/knowledge-graph/schema.js.map +1 -0
- package/dist/libs/knowledge-graph/scope.d.ts +16 -0
- package/dist/libs/knowledge-graph/scope.js +2 -0
- package/dist/libs/knowledge-graph/scope.js.map +1 -0
- package/dist/libs/knowledge-graph/service.d.ts +54 -0
- package/dist/libs/knowledge-graph/service.js +89 -0
- package/dist/libs/knowledge-graph/service.js.map +1 -0
- package/dist/libs/knowledge-graph/validate-proposal.d.ts +9 -0
- package/dist/libs/knowledge-graph/validate-proposal.js +166 -0
- package/dist/libs/knowledge-graph/validate-proposal.js.map +1 -0
- package/dist/libs/storage/sqlite/db.d.ts +3 -0
- package/dist/libs/storage/sqlite/db.js +16 -0
- package/dist/libs/storage/sqlite/db.js.map +1 -0
- package/dist/libs/storage/sqlite/migrate.d.ts +2 -0
- package/dist/libs/storage/sqlite/migrate.js +56 -0
- package/dist/libs/storage/sqlite/migrate.js.map +1 -0
- package/dist/libs/storage/sqlite/repository.d.ts +95 -0
- package/dist/libs/storage/sqlite/repository.js +310 -0
- package/dist/libs/storage/sqlite/repository.js.map +1 -0
- package/dist/libs/storage/sqlite/schema.d.ts +1 -0
- package/dist/libs/storage/sqlite/schema.js +93 -0
- package/dist/libs/storage/sqlite/schema.js.map +1 -0
- package/package.json +37 -0
- package/skills/greplica-bootstrap/SKILL.md +116 -0
- package/skills/greplica-update-working-memory/SKILL.md +183 -0
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { greplicaHome } from "../../config/greplica-home.js";
|
|
3
|
+
const localModelAliases = {
|
|
4
|
+
"all-mpnet-base-v2": "Xenova/all-mpnet-base-v2",
|
|
5
|
+
"all-MiniLM-L6-v2": "Xenova/all-MiniLM-L6-v2",
|
|
6
|
+
};
|
|
7
|
+
export class LocalEmbedder {
|
|
8
|
+
options;
|
|
9
|
+
extractor;
|
|
10
|
+
modelId;
|
|
11
|
+
cacheDir;
|
|
12
|
+
constructor(options) {
|
|
13
|
+
this.options = options;
|
|
14
|
+
this.modelId = resolveLocalModelId(options.model);
|
|
15
|
+
this.cacheDir = join(greplicaHome(), "models");
|
|
16
|
+
}
|
|
17
|
+
async embed(text) {
|
|
18
|
+
const [embedding] = await this.embedBatch([text]);
|
|
19
|
+
if (!embedding)
|
|
20
|
+
throw new Error("Local embedding model returned no embedding for query.");
|
|
21
|
+
return embedding;
|
|
22
|
+
}
|
|
23
|
+
async embedBatch(texts) {
|
|
24
|
+
const embeddings = [];
|
|
25
|
+
for (let index = 0; index < texts.length; index += this.options.batchSize) {
|
|
26
|
+
embeddings.push(...(await this.embedChunk(texts.slice(index, index + this.options.batchSize))));
|
|
27
|
+
}
|
|
28
|
+
return embeddings;
|
|
29
|
+
}
|
|
30
|
+
async embedChunk(texts) {
|
|
31
|
+
if (texts.length === 0)
|
|
32
|
+
return [];
|
|
33
|
+
const extractor = await this.loadExtractor();
|
|
34
|
+
const output = await extractor(texts, { pooling: "mean", normalize: true });
|
|
35
|
+
return splitTensorRows(output, texts.length, this.options.dimensions, this.modelId);
|
|
36
|
+
}
|
|
37
|
+
async loadExtractor() {
|
|
38
|
+
this.extractor ??= loadFeatureExtractionPipeline(this.modelId, this.cacheDir);
|
|
39
|
+
return this.extractor;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
async function loadFeatureExtractionPipeline(modelId, cacheDir) {
|
|
43
|
+
const { pipeline } = await import("@huggingface/transformers");
|
|
44
|
+
const options = {
|
|
45
|
+
cache_dir: cacheDir,
|
|
46
|
+
dtype: "q8",
|
|
47
|
+
};
|
|
48
|
+
try {
|
|
49
|
+
return await pipeline("feature-extraction", modelId, {
|
|
50
|
+
...options,
|
|
51
|
+
local_files_only: true,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
return pipeline("feature-extraction", modelId, options);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function resolveLocalModelId(model) {
|
|
59
|
+
return localModelAliases[model] ?? model;
|
|
60
|
+
}
|
|
61
|
+
function splitTensorRows(output, expectedRows, expectedDimensions, modelId) {
|
|
62
|
+
const [rows, dimensions] = output.dims;
|
|
63
|
+
if (rows !== expectedRows || dimensions !== expectedDimensions) {
|
|
64
|
+
throw new Error(`Local embedding model ${modelId} returned shape [${output.dims.join(", ")}]; expected [${expectedRows}, ${expectedDimensions}].`);
|
|
65
|
+
}
|
|
66
|
+
const data = output.data;
|
|
67
|
+
const embeddings = [];
|
|
68
|
+
for (let row = 0; row < rows; row += 1) {
|
|
69
|
+
const start = row * dimensions;
|
|
70
|
+
embeddings.push(Array.from(data.slice(start, start + dimensions)));
|
|
71
|
+
}
|
|
72
|
+
return embeddings;
|
|
73
|
+
}
|
|
74
|
+
//# sourceMappingURL=local-embedder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local-embedder.js","sourceRoot":"","sources":["../../../../libs/knowledge-graph/graph-context/local-embedder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,OAAO,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAa7D,MAAM,iBAAiB,GAA2B;IAChD,mBAAmB,EAAE,0BAA0B;IAC/C,kBAAkB,EAAE,yBAAyB;CAC9C,CAAC;AAEF,MAAM,OAAO,aAAa;IAKK;IAJrB,SAAS,CAAiD;IACjD,OAAO,CAAS;IAChB,QAAQ,CAAS;IAElC,YAA6B,OAAwB;QAAxB,YAAO,GAAP,OAAO,CAAiB;QACnD,IAAI,CAAC,OAAO,GAAG,mBAAmB,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;QAClD,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,YAAY,EAAE,EAAE,QAAQ,CAAC,CAAC;IACjD,CAAC;IAED,KAAK,CAAC,KAAK,CAAC,IAAY;QACtB,MAAM,CAAC,SAAS,CAAC,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;QAClD,IAAI,CAAC,SAAS;YAAE,MAAM,IAAI,KAAK,CAAC,wDAAwD,CAAC,CAAC;QAC1F,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,KAAK,CAAC,UAAU,CAAC,KAAe;QAC9B,MAAM,UAAU,GAAe,EAAE,CAAC;QAClC,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,KAAK,CAAC,MAAM,EAAE,KAAK,IAAI,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,CAAC;YAC1E,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,KA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,CAAC,KAAa;IACxC,OAAO,iBAAiB,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC;AAC3C,CAAC;AAED,SAAS,eAAe,CAAC,MAAkB,EAAE,YAAoB,EAAE,kBAA0B,EAAE,OAAe;IAC5G,MAAM,CAAC,IAAI,EAAE,UAAU,CAAC,GAAG,MAAM,CAAC,IAAI,CAAC;IACvC,IAAI,IAAI,KAAK,YAAY,IAAI,UAAU,KAAK,kBAAkB,EAAE,CAAC;QAC/D,MAAM,IAAI,KAAK,CACb,yBAAyB,OAAO,oBAAoB,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,gBAAgB,YAAY,KAAK,kBAAkB,IAAI,CAClI,CAAC;IACJ,CAAC;IAED,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC;IACzB,MAAM,UAAU,GAAe,EAAE,CAAC;IAClC,KAAK,IAAI,GAAG,GAAG,CAAC,EAAE,GAAG,GAAG,IAAI,EAAE,GAAG,IAAI,CAAC,EAAE,CAAC;QACvC,MAAM,KAAK,GAAG,GAAG,GAAG,UAAU,CAAC;QAC/B,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC;IACrE,CAAC;IACD,OAAO,UAAU,CAAC;AACpB,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface OpenAIEmbedderOptions {
|
|
2
|
+
apiKey?: string;
|
|
3
|
+
model: string;
|
|
4
|
+
dimensions: number;
|
|
5
|
+
batchSize: number;
|
|
6
|
+
}
|
|
7
|
+
export declare class OpenAIEmbedder {
|
|
8
|
+
private readonly options;
|
|
9
|
+
private readonly apiKey;
|
|
10
|
+
constructor(options: OpenAIEmbedderOptions);
|
|
11
|
+
embed(text: string): Promise<number[]>;
|
|
12
|
+
embedBatch(texts: string[]): Promise<number[][]>;
|
|
13
|
+
private embedChunk;
|
|
14
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
export class OpenAIEmbedder {
|
|
2
|
+
options;
|
|
3
|
+
apiKey;
|
|
4
|
+
constructor(options) {
|
|
5
|
+
this.options = options;
|
|
6
|
+
const apiKey = (options.apiKey ?? process.env.OPENAI_API_KEY)?.trim();
|
|
7
|
+
if (!apiKey) {
|
|
8
|
+
throw new Error("OPENAI_API_KEY is required for graph context embeddings. Set it in the environment, target-root .env.local, or target-root .env.");
|
|
9
|
+
}
|
|
10
|
+
this.apiKey = apiKey;
|
|
11
|
+
}
|
|
12
|
+
async embed(text) {
|
|
13
|
+
const [embedding] = await this.embedBatch([text]);
|
|
14
|
+
if (!embedding)
|
|
15
|
+
throw new Error("OpenAI returned no embedding for query.");
|
|
16
|
+
return embedding;
|
|
17
|
+
}
|
|
18
|
+
async embedBatch(texts) {
|
|
19
|
+
const embeddings = [];
|
|
20
|
+
for (let index = 0; index < texts.length; index += this.options.batchSize) {
|
|
21
|
+
embeddings.push(...(await this.embedChunk(texts.slice(index, index + this.options.batchSize))));
|
|
22
|
+
}
|
|
23
|
+
return embeddings;
|
|
24
|
+
}
|
|
25
|
+
async embedChunk(texts) {
|
|
26
|
+
if (texts.length === 0)
|
|
27
|
+
return [];
|
|
28
|
+
const response = await fetch("https://api.openai.com/v1/embeddings", {
|
|
29
|
+
method: "POST",
|
|
30
|
+
headers: {
|
|
31
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
32
|
+
"Content-Type": "application/json",
|
|
33
|
+
},
|
|
34
|
+
body: JSON.stringify({
|
|
35
|
+
input: texts,
|
|
36
|
+
model: this.options.model,
|
|
37
|
+
dimensions: this.options.dimensions,
|
|
38
|
+
encoding_format: "float",
|
|
39
|
+
}),
|
|
40
|
+
});
|
|
41
|
+
const payload = (await response.json());
|
|
42
|
+
if (!response.ok) {
|
|
43
|
+
throw new Error(`OpenAI embeddings request failed: ${payload.error?.message ?? response.statusText}`);
|
|
44
|
+
}
|
|
45
|
+
if (!Array.isArray(payload.data)) {
|
|
46
|
+
throw new Error("OpenAI embeddings response did not include data.");
|
|
47
|
+
}
|
|
48
|
+
return payload.data
|
|
49
|
+
.slice()
|
|
50
|
+
.sort((a, b) => a.index - b.index)
|
|
51
|
+
.map((item) => {
|
|
52
|
+
if (item.embedding.length !== this.options.dimensions) {
|
|
53
|
+
throw new Error(`OpenAI returned ${item.embedding.length} dimensions; expected ${this.options.dimensions}.`);
|
|
54
|
+
}
|
|
55
|
+
return item.embedding;
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=openai-embedder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openai-embedder.js","sourceRoot":"","sources":["../../../../libs/knowledge-graph/graph-context/openai-embedder.ts"],"names":[],"mappings":"AAYA,MAAM,OAAO,cAAc;IAGI;IAFZ,MAAM,CAAS;IAEhC,YAA6B,OAA8B;QAA9B,YAAO,GAAP,OAAO,CAAuB;QACzD,MAAM,MAAM,GAAG,CAAC,OAAO,CAAC,MAAM,IAAI,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,EAAE,IAAI,EAAE,CAAC;QACtE,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,MAAM,IAAI,KAAK,CAAC,kIAAkI,CAAC,CAAC;QACtJ,CAAC;QACD,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAED,KAAK,CAAC,KAAK,CAAC,IAAY;QACtB,MAAM,CAAC,SAAS,CAAC,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;QAClD,IAAI,CAAC,SAAS;YAAE,MAAM,IAAI,KAAK,CAAC,yCAAyC,CAAC,CAAC;QAC3E,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,KAAK,CAAC,UAAU,CAAC,KAAe;QAC9B,MAAM,UAAU,GAAe,EAAE,CAAC;QAClC,KAAK,IAAI,KAAK,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AAO,OAAO,CAAC,IAAI;aAChB,KAAK,EAAE;aACP,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC;aACjC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE;YACZ,IAAI,IAAI,CAAC,SAAS,CAAC,MAAM,KAAK,IAAI,CAAC,OAAO,CAAC,UAAU,EAAE,CAAC;gBACtD,MAAM,IAAI,KAAK,CAAC,mBAAmB,IAAI,CAAC,SAAS,CAAC,MAAM,yBAAyB,IAAI,CAAC,OAAO,CAAC,UAAU,GAAG,CAAC,CAAC;YAC/G,CAAC;YACD,OAAO,IAAI,CAAC,SAAS,CAAC;QACxB,CAAC,CAAC,CAAC;IACP,CAAC;CACF"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { GraphContextConfig } from "./config.js";
|
|
2
|
+
import type { ScoreEntry } from "./bm25.js";
|
|
3
|
+
import type { ContextDocument } from "./documents.js";
|
|
4
|
+
import type { ContextSignals } from "./types.js";
|
|
5
|
+
export interface SemanticScoreEntry extends ScoreEntry {
|
|
6
|
+
}
|
|
7
|
+
export interface RankedContextDocument {
|
|
8
|
+
document: ContextDocument;
|
|
9
|
+
score: number;
|
|
10
|
+
signals: ContextSignals;
|
|
11
|
+
}
|
|
12
|
+
export declare function rankContextDocuments(documents: ContextDocument[], semantic: SemanticScoreEntry[], bm25: ScoreEntry[], exact: ScoreEntry[], config: GraphContextConfig): RankedContextDocument[];
|
|
13
|
+
export declare function selectRankedDocuments(ranked: RankedContextDocument[], config: GraphContextConfig, options?: {
|
|
14
|
+
minimumSelected?: number;
|
|
15
|
+
}): RankedContextDocument[];
|
|
16
|
+
export declare function roundScore(value: number): number;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export function rankContextDocuments(documents, semantic, bm25, exact, config) {
|
|
2
|
+
const semanticById = indexScores(semantic);
|
|
3
|
+
const bm25ById = indexScores(bm25);
|
|
4
|
+
const exactById = indexScores(exact);
|
|
5
|
+
const ranked = documents
|
|
6
|
+
.map((document) => {
|
|
7
|
+
const semanticScore = semanticById.get(document.key);
|
|
8
|
+
const bm25Score = bm25ById.get(document.key);
|
|
9
|
+
const exactScore = exactById.get(document.key);
|
|
10
|
+
const semanticValue = semanticScore?.score ?? 0;
|
|
11
|
+
if (semanticValue < config.ranking.semanticThreshold)
|
|
12
|
+
return undefined;
|
|
13
|
+
const weighted = semanticValue * config.ranking.weights.semantic +
|
|
14
|
+
(bm25Score?.score ?? 0) * config.ranking.weights.bm25 +
|
|
15
|
+
(exactScore?.score ?? 0) * config.ranking.weights.exact;
|
|
16
|
+
const divisor = config.ranking.weights.semantic +
|
|
17
|
+
config.ranking.weights.bm25 +
|
|
18
|
+
config.ranking.weights.exact;
|
|
19
|
+
return {
|
|
20
|
+
document,
|
|
21
|
+
score: weighted / divisor,
|
|
22
|
+
signals: {
|
|
23
|
+
semantic_score: semanticValue,
|
|
24
|
+
semantic_rank: semanticScore?.rank ?? null,
|
|
25
|
+
bm25_score: bm25Score?.score ?? 0,
|
|
26
|
+
bm25_rank: bm25Score?.rank ?? null,
|
|
27
|
+
exact_score: exactScore?.score ?? 0,
|
|
28
|
+
exact_rank: exactScore?.rank ?? null,
|
|
29
|
+
graph_score: 0,
|
|
30
|
+
graph_sources: [],
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
})
|
|
34
|
+
.filter((candidate) => candidate !== undefined)
|
|
35
|
+
.sort((a, b) => b.score - a.score || a.document.key.localeCompare(b.document.key));
|
|
36
|
+
const maxScore = ranked[0]?.score ?? 1;
|
|
37
|
+
return ranked.map((candidate) => ({
|
|
38
|
+
...candidate,
|
|
39
|
+
score: maxScore === 0 ? 0 : candidate.score / maxScore,
|
|
40
|
+
}));
|
|
41
|
+
}
|
|
42
|
+
export function selectRankedDocuments(ranked, config, options = {}) {
|
|
43
|
+
return ranked.filter((document, index) => index < (options.minimumSelected ?? 0) ||
|
|
44
|
+
document.score >= config.ranking.selectionThreshold);
|
|
45
|
+
}
|
|
46
|
+
export function roundScore(value) {
|
|
47
|
+
return Math.round(value * 1000) / 1000;
|
|
48
|
+
}
|
|
49
|
+
function indexScores(scores) {
|
|
50
|
+
return new Map(scores.map((score) => [score.id, score]));
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=rank.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rank.js","sourceRoot":"","sources":["../../../../libs/knowledge-graph/graph-context/rank.ts"],"names":[],"mappings":"AAaA,MAAM,UAAU,oBAAoB,CAClC,SAA4B,EAC5B,QAA8B,EAC9B,IAAkB,EAClB,KAAmB,EACnB,MAA0B;IAE1B,MAAM,YAAY,GAAG,WAAW,CAAC,QAAQ,CAAC,CAAC;IAC3C,MAAM,QAAQ,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC;IACnC,MAAM,SAAS,GAAG,WAAW,CAAC,KAAK,CAAC,CAAC;IAErC,MAAM,MAAM,GAAG,SAAS;SACrB,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE;QAChB,MAAM,aAAa,GAAG,YAAY,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;QACrD,MAAM,SAAS,GAAG,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;QAC7C,MAAM,UAAU,GAAG,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;QAC/C,MAAM,aAAa,GAAG,aAAa,EAAE,KAAK,IAAI,CAAC,CAAC;QAEhD,IAAI,aAAa,GAAG,MAAM,CAAC,OAAO,CAAC,iBAAiB;YAAE,OAAO,SAAS,CAAC;QAEvE,MAAM,QAAQ,GACZ,aAAa,GAAG,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,QAAQ;YAC/C,CAAC,SAAS,EAAE,KAAK,IAAI,CAAC,CAAC,GAAG,MAAM,CAAC,OAAO,CAAC,OAA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AAC,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,GAAG,QAAQ;KACvD,CAAC,CAAC,CAAC;AACN,CAAC;AAED,MAAM,UAAU,qBAAqB,CACnC,MAA+B,EAC/B,MAA0B,EAC1B,UAAwC,EAAE;IAE1C,OAAO,MAAM,CAAC,MAAM,CAClB,CAAC,QAAQ,EAAE,KAAK,EAAE,EAAE,CAClB,KAAK,GAAG,CAAC,OAAO,CAAC,eAAe,IAAI,CAAC,CAAC;QACtC,QAAQ,CAAC,KAAK,IAAI,MAAM,CAAC,OAAO,CAAC,kBAAkB,CACtD,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,KAAa;IACtC,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC;AACzC,CAAC;AAED,SAAS,WAAW,CAAC,MAAoB;IACvC,OAAO,IAAI,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC;AAC3D,CAAC"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { Claim } from "../claim.js";
|
|
2
|
+
import type { Source } from "../schema.js";
|
|
3
|
+
import type { ClaimContextResult, GraphContextResult } from "./types.js";
|
|
4
|
+
interface CompactGraphContextResult {
|
|
5
|
+
query: string;
|
|
6
|
+
components: CompactComponentResult[];
|
|
7
|
+
flows: CompactFlowResult[];
|
|
8
|
+
claims: CompactClaimResult[];
|
|
9
|
+
sources: Source[];
|
|
10
|
+
}
|
|
11
|
+
interface CompactComponentResult {
|
|
12
|
+
id: string;
|
|
13
|
+
name: string;
|
|
14
|
+
code_anchor?: string;
|
|
15
|
+
matched_claim_ids: string[];
|
|
16
|
+
}
|
|
17
|
+
interface CompactFlowResult {
|
|
18
|
+
id: string;
|
|
19
|
+
name: string;
|
|
20
|
+
matched_claim_ids: string[];
|
|
21
|
+
}
|
|
22
|
+
interface CompactClaimResult {
|
|
23
|
+
id: string;
|
|
24
|
+
kind: Claim["kind"];
|
|
25
|
+
text: string;
|
|
26
|
+
truth: Claim["truth"];
|
|
27
|
+
intent: Claim["intent"];
|
|
28
|
+
about: ClaimContextResult["about"];
|
|
29
|
+
evidence: Array<{
|
|
30
|
+
source_id: string;
|
|
31
|
+
title?: string;
|
|
32
|
+
ref: string;
|
|
33
|
+
reason?: string;
|
|
34
|
+
}>;
|
|
35
|
+
}
|
|
36
|
+
export declare function renderGraphContextMarkdown(result: GraphContextResult): string;
|
|
37
|
+
export declare function compactGraphContextResult(result: GraphContextResult): CompactGraphContextResult;
|
|
38
|
+
export {};
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
export function renderGraphContextMarkdown(result) {
|
|
2
|
+
const claimById = new Map(result.claims.map((claim) => [claim.object.id, claim]));
|
|
3
|
+
const shownClaimIds = new Set();
|
|
4
|
+
const content = [
|
|
5
|
+
"# Graph Context",
|
|
6
|
+
"",
|
|
7
|
+
`Query: ${result.query}`,
|
|
8
|
+
"",
|
|
9
|
+
"## Components",
|
|
10
|
+
"",
|
|
11
|
+
...renderComponents(result.components),
|
|
12
|
+
"",
|
|
13
|
+
"## Flows",
|
|
14
|
+
"",
|
|
15
|
+
...renderFlows(result.flows, claimById, shownClaimIds),
|
|
16
|
+
];
|
|
17
|
+
const remainingClaims = result.claims.filter((claim) => !shownClaimIds.has(claim.object.id));
|
|
18
|
+
if (remainingClaims.length > 0) {
|
|
19
|
+
content.push("", "## Other Relevant Claims", "", ...renderClaimItems(remainingClaims));
|
|
20
|
+
}
|
|
21
|
+
if (result.sources.length > 0) {
|
|
22
|
+
content.push("", "## Sources", "", ...renderSources(result.sources));
|
|
23
|
+
}
|
|
24
|
+
return lines(...content);
|
|
25
|
+
}
|
|
26
|
+
export function compactGraphContextResult(result) {
|
|
27
|
+
return {
|
|
28
|
+
query: result.query,
|
|
29
|
+
components: result.components.map((component) => ({
|
|
30
|
+
id: component.object.id,
|
|
31
|
+
name: component.object.name,
|
|
32
|
+
code_anchor: component.object.code_anchor,
|
|
33
|
+
matched_claim_ids: component.matched_claim_ids,
|
|
34
|
+
})),
|
|
35
|
+
flows: result.flows.map((flow) => ({
|
|
36
|
+
id: flow.object.id,
|
|
37
|
+
name: flow.object.name,
|
|
38
|
+
matched_claim_ids: flow.matched_claim_ids,
|
|
39
|
+
})),
|
|
40
|
+
claims: result.claims.map((claim) => ({
|
|
41
|
+
id: claim.object.id,
|
|
42
|
+
kind: claim.object.kind,
|
|
43
|
+
text: claim.object.text,
|
|
44
|
+
truth: claim.object.truth,
|
|
45
|
+
intent: claim.object.intent,
|
|
46
|
+
about: claim.about,
|
|
47
|
+
evidence: claim.evidence.map((evidence) => ({
|
|
48
|
+
source_id: evidence.source.id,
|
|
49
|
+
title: evidence.source.title,
|
|
50
|
+
ref: evidence.source.ref,
|
|
51
|
+
reason: evidence.reason.length > 0 ? evidence.reason : undefined,
|
|
52
|
+
})),
|
|
53
|
+
})),
|
|
54
|
+
sources: result.sources,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
function renderComponents(components) {
|
|
58
|
+
if (components.length === 0)
|
|
59
|
+
return ["- None."];
|
|
60
|
+
return components.map((component) => {
|
|
61
|
+
const anchor = component.object.code_anchor === undefined ? "" : `\n Anchor: \`${component.object.code_anchor}\``;
|
|
62
|
+
return `- \`${component.object.id}\` ${component.object.name}${anchor}`;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
function renderFlows(flows, claimById, shownClaimIds) {
|
|
66
|
+
if (flows.length === 0)
|
|
67
|
+
return ["- None."];
|
|
68
|
+
return flows.flatMap((flow) => {
|
|
69
|
+
const claims = claimsForFlow(flow, claimById);
|
|
70
|
+
for (const claim of claims)
|
|
71
|
+
shownClaimIds.add(claim.object.id);
|
|
72
|
+
return [
|
|
73
|
+
`### ${flow.object.name}`,
|
|
74
|
+
"",
|
|
75
|
+
`ID: \`${flow.object.id}\``,
|
|
76
|
+
"",
|
|
77
|
+
"Claims:",
|
|
78
|
+
...renderClaimItems(claims),
|
|
79
|
+
"",
|
|
80
|
+
];
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
function claimsForFlow(flow, claimById) {
|
|
84
|
+
const claims = new Map();
|
|
85
|
+
for (const claimId of flow.matched_claim_ids) {
|
|
86
|
+
const claim = claimById.get(claimId);
|
|
87
|
+
if (claim !== undefined)
|
|
88
|
+
claims.set(claim.object.id, claim);
|
|
89
|
+
}
|
|
90
|
+
for (const claim of claimById.values()) {
|
|
91
|
+
if (claim.about.some((subject) => subject.type === "flow" && subject.id === flow.object.id)) {
|
|
92
|
+
claims.set(claim.object.id, claim);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return [...claims.values()].sort((left, right) => left.object.kind.localeCompare(right.object.kind) || left.object.id.localeCompare(right.object.id));
|
|
96
|
+
}
|
|
97
|
+
function renderClaimItems(claims) {
|
|
98
|
+
if (claims.length === 0)
|
|
99
|
+
return ["- None."];
|
|
100
|
+
return claims.map((claim) => {
|
|
101
|
+
const evidence = claim.evidence.length === 0 ? "" : ` Source: ${claim.evidence.map(evidenceLabel).join("; ")}.`;
|
|
102
|
+
return `- \`${claim.object.id}\` (${claim.object.kind}, ${claim.object.truth}): ${claim.object.text}${evidence}`;
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
function evidenceLabel(evidence) {
|
|
106
|
+
return evidence.source.title ?? evidence.source.ref ?? evidence.source.id;
|
|
107
|
+
}
|
|
108
|
+
function renderSources(sources) {
|
|
109
|
+
return sources.map((source) => `- \`${source.id}\` ${source.title ?? source.ref}`);
|
|
110
|
+
}
|
|
111
|
+
function lines(...values) {
|
|
112
|
+
return `${values.join("\n").replace(/\n{3,}/g, "\n\n").trimEnd()}\n`;
|
|
113
|
+
}
|
|
114
|
+
//# sourceMappingURL=render.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"render.js","sourceRoot":"","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CAAC,CAAC,KAAK,EAAE,EAAE;QAC1B,MAAM,QAAQ,GAAG,KAAK,CAAC,QAAQ,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,YAAY,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC;QAChH,OAAO,OAAO,KAAK,CAAC,MAAM,CAAC,EAAE,OAAO,KAAK,CAAC,MAAM,CAAC,IAAI,KAAK,KAAK,CAAC,MAAM,CAAC,KAAK,MAAM,KAAK,CAAC,MAAM,CAAC,IAAI,GAAG,QAAQ,EAAE,CAAC;IACnH,CAAC,CAAC,CAAC;AACL,CAAC;AAED,SAAS,aAAa,CAAC,QAAgD;IACrE,OAAO,QAAQ,CAAC,MAAM,CAAC,KAAK,IAAI,QAAQ,CAAC,MAAM,CAAC,GAAG,IAAI,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC;AAC5E,CAAC;AAED,SAAS,aAAa,CAAC,OAAiB;IACtC,OAAO,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,OAAO,MAAM,CAAC,EAAE,MAAM,MAAM,CAAC,KAAK,IAAI,MAAM,CAAC,GAAG,EAAE,CAAC,CAAC;AACrF,CAAC;AAED,SAAS,KAAK,CAAC,GAAG,MAAgB;IAChC,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC,OAAO,EAAE,IAAI,CAAC;AACvE,CAAC"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { Claim } from "../claim.js";
|
|
2
|
+
import type { Component, Flow, Source } from "../schema.js";
|
|
3
|
+
export interface EmbeddingStatus {
|
|
4
|
+
checked_objects: number;
|
|
5
|
+
created: number;
|
|
6
|
+
reused: number;
|
|
7
|
+
}
|
|
8
|
+
export interface GraphContextSource {
|
|
9
|
+
id: string;
|
|
10
|
+
edge_kind: string;
|
|
11
|
+
weight: number;
|
|
12
|
+
score: number;
|
|
13
|
+
}
|
|
14
|
+
export interface ContextSignals {
|
|
15
|
+
semantic_score: number;
|
|
16
|
+
semantic_rank: number | null;
|
|
17
|
+
bm25_score: number;
|
|
18
|
+
bm25_rank: number | null;
|
|
19
|
+
exact_score: number;
|
|
20
|
+
exact_rank: number | null;
|
|
21
|
+
graph_score: number;
|
|
22
|
+
graph_sources: GraphContextSource[];
|
|
23
|
+
}
|
|
24
|
+
export interface ClaimEvidenceResult {
|
|
25
|
+
source: Source;
|
|
26
|
+
reason: string;
|
|
27
|
+
}
|
|
28
|
+
export interface ClaimContextResult {
|
|
29
|
+
rank: number;
|
|
30
|
+
score: number;
|
|
31
|
+
signals: ContextSignals;
|
|
32
|
+
object: Claim;
|
|
33
|
+
about: Array<{
|
|
34
|
+
type: "component" | "flow";
|
|
35
|
+
id: string;
|
|
36
|
+
}>;
|
|
37
|
+
evidence: ClaimEvidenceResult[];
|
|
38
|
+
}
|
|
39
|
+
export interface GraphObjectContextResult<TObject> {
|
|
40
|
+
rank: number;
|
|
41
|
+
score: number;
|
|
42
|
+
direct_score: number;
|
|
43
|
+
claim_support_score: number;
|
|
44
|
+
signals: ContextSignals;
|
|
45
|
+
object: TObject;
|
|
46
|
+
matched_claim_ids: string[];
|
|
47
|
+
}
|
|
48
|
+
export type ComponentContextResult = GraphObjectContextResult<Component>;
|
|
49
|
+
export type FlowContextResult = GraphObjectContextResult<Flow>;
|
|
50
|
+
export interface GraphContextResult {
|
|
51
|
+
query: string;
|
|
52
|
+
search_config_version: string;
|
|
53
|
+
embedding_status: EmbeddingStatus;
|
|
54
|
+
claims: ClaimContextResult[];
|
|
55
|
+
components: ComponentContextResult[];
|
|
56
|
+
flows: FlowContextResult[];
|
|
57
|
+
sources: Source[];
|
|
58
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../libs/knowledge-graph/graph-context/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export function float32ArrayToBuffer(vector) {
|
|
2
|
+
return Buffer.from(new Float32Array(vector).buffer);
|
|
3
|
+
}
|
|
4
|
+
export function bufferToFloat32Array(buffer) {
|
|
5
|
+
return new Float32Array(buffer.buffer, buffer.byteOffset, buffer.byteLength / Float32Array.BYTES_PER_ELEMENT);
|
|
6
|
+
}
|
|
7
|
+
export function cosineSimilarity(a, b) {
|
|
8
|
+
if (a.length !== b.length) {
|
|
9
|
+
throw new Error(`Embedding dimension mismatch. Expected ${a.length}, got ${b.length}.`);
|
|
10
|
+
}
|
|
11
|
+
let dot = 0;
|
|
12
|
+
let normA = 0;
|
|
13
|
+
let normB = 0;
|
|
14
|
+
for (let index = 0; index < a.length; index += 1) {
|
|
15
|
+
const av = a[index] ?? 0;
|
|
16
|
+
const bv = b[index] ?? 0;
|
|
17
|
+
dot += av * bv;
|
|
18
|
+
normA += av * av;
|
|
19
|
+
normB += bv * bv;
|
|
20
|
+
}
|
|
21
|
+
if (normA === 0 || normB === 0)
|
|
22
|
+
return 0;
|
|
23
|
+
return dot / (Math.sqrt(normA) * Math.sqrt(normB));
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=vector.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vector.js","sourceRoot":"","sources":["../../../../libs/knowledge-graph/graph-context/vector.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,oBAAoB,CAAC,MAAgB;IACnD,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,YAAY,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC;AACtD,CAAC;AAED,MAAM,UAAU,oBAAoB,CAAC,MAAc;IACjD,OAAO,IAAI,YAAY,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,UAAU,EAAE,MAAM,CAAC,UAAU,GAAG,YAAY,CAAC,iBAAiB,CAAC,CAAC;AAChH,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,CAAoB,EAAE,CAAoB;IACzE,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM,EAAE,CAAC;QAC1B,MAAM,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAC,MAAM,SAAS,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC;IAC1F,CAAC;IAED,IAAI,GAAG,GAAG,CAAC,CAAC;IACZ,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,CAAC,CAAC,MAAM,EAAE,KAAK,IAAI,CAAC,EAAE,CAAC;QACjD,MAAM,EAAE,GAAG,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACzB,MAAM,EAAE,GAAG,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACzB,GAAG,IAAI,EAAE,GAAG,EAAE,CAAC;QACf,KAAK,IAAI,EAAE,GAAG,EAAE,CAAC;QACjB,KAAK,IAAI,EAAE,GAAG,EAAE,CAAC;IACnB,CAAC;IAED,IAAI,KAAK,KAAK,CAAC,IAAI,KAAK,KAAK,CAAC;QAAE,OAAO,CAAC,CAAC;IACzC,OAAO,GAAG,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;AACrD,CAAC"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { Claim } from "./claim.js";
|
|
2
|
+
import type { Edge, EdgeKind, EdgeMetadata } from "./edge.js";
|
|
3
|
+
import type { Component, Flow, GraphObjectType, Source } from "./schema.js";
|
|
4
|
+
export interface MemoryCommitProposal {
|
|
5
|
+
title: string;
|
|
6
|
+
summary?: string;
|
|
7
|
+
creates: {
|
|
8
|
+
components?: Component[];
|
|
9
|
+
flows?: Flow[];
|
|
10
|
+
claims?: Claim[];
|
|
11
|
+
sources?: Source[];
|
|
12
|
+
edges?: Edge[];
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export type CompactComponent = Component & {
|
|
16
|
+
contains?: string | string[];
|
|
17
|
+
supersedes?: string | string[];
|
|
18
|
+
};
|
|
19
|
+
export type CompactFlow = Flow & {
|
|
20
|
+
contains?: string | string[];
|
|
21
|
+
touches?: string | string[];
|
|
22
|
+
supersedes?: string | string[];
|
|
23
|
+
};
|
|
24
|
+
export type CompactClaim = Claim & {
|
|
25
|
+
about?: string | string[];
|
|
26
|
+
evidenced_by?: string | string[];
|
|
27
|
+
supersedes?: string | string[];
|
|
28
|
+
};
|
|
29
|
+
export interface CompactEdge {
|
|
30
|
+
kind: EdgeKind;
|
|
31
|
+
from: string;
|
|
32
|
+
to: string;
|
|
33
|
+
metadata?: EdgeMetadata;
|
|
34
|
+
}
|
|
35
|
+
export interface CompactMemoryProposal {
|
|
36
|
+
title: string;
|
|
37
|
+
summary?: string;
|
|
38
|
+
creates: {
|
|
39
|
+
components?: CompactComponent[];
|
|
40
|
+
flows?: CompactFlow[];
|
|
41
|
+
claims?: CompactClaim[];
|
|
42
|
+
sources?: Source[];
|
|
43
|
+
edges?: CompactEdge[];
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
export type ProposalSubject = {
|
|
47
|
+
type: "component";
|
|
48
|
+
id: string;
|
|
49
|
+
} | {
|
|
50
|
+
type: "flow";
|
|
51
|
+
id: string;
|
|
52
|
+
} | {
|
|
53
|
+
type: "claim";
|
|
54
|
+
id: string;
|
|
55
|
+
} | {
|
|
56
|
+
type: "edge";
|
|
57
|
+
id: string;
|
|
58
|
+
} | {
|
|
59
|
+
type: "source";
|
|
60
|
+
id: string;
|
|
61
|
+
};
|
|
62
|
+
export interface ProposalSubjectLookup {
|
|
63
|
+
subjectType(id: string): GraphObjectType | undefined;
|
|
64
|
+
}
|
|
65
|
+
export declare function normalizeProposal(input: unknown, lookup?: ProposalSubjectLookup): MemoryCommitProposal;
|