@kinqs/brainrouter-mcp-server 0.3.4
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/.env.example +144 -0
- package/README.md +56 -0
- package/agents/README.md +120 -0
- package/agents/code-reviewer.md +97 -0
- package/agents/security-auditor.md +101 -0
- package/agents/test-engineer.md +95 -0
- package/dist/__tests__/agent_mode.test.d.ts +1 -0
- package/dist/__tests__/api-routes.test.d.ts +1 -0
- package/dist/__tests__/api-routes.test.js +170 -0
- package/dist/__tests__/crypto.test.d.ts +1 -0
- package/dist/__tests__/crypto.test.js +28 -0
- package/dist/__tests__/host-integrations.test.d.ts +1 -0
- package/dist/__tests__/host-integrations.test.js +82 -0
- package/dist/__tests__/integration.test.d.ts +1 -0
- package/dist/__tests__/integration.test.js +50 -0
- package/dist/__tests__/loader.test.d.ts +1 -0
- package/dist/__tests__/loader.test.js +89 -0
- package/dist/__tests__/neural-spark.test.d.ts +1 -0
- package/dist/__tests__/neural-spark.test.js +112 -0
- package/dist/__tests__/pagination.test.d.ts +1 -0
- package/dist/__tests__/pagination.test.js +23 -0
- package/dist/__tests__/redaction.test.d.ts +1 -0
- package/dist/__tests__/redaction.test.js +17 -0
- package/dist/__tests__/registry.test.d.ts +1 -0
- package/dist/__tests__/registry.test.js +56 -0
- package/dist/__tests__/retry.test.d.ts +1 -0
- package/dist/__tests__/retry.test.js +30 -0
- package/dist/__tests__/skill-activation.test.d.ts +1 -0
- package/dist/__tests__/skill-activation.test.js +112 -0
- package/dist/__tests__/working-memory.test.d.ts +1 -0
- package/dist/__tests__/working-memory.test.js +200 -0
- package/dist/__tests__/workspace-paths.test.d.ts +1 -0
- package/dist/__tests__/workspace-paths.test.js +56 -0
- package/dist/__tests__/writer.test.d.ts +1 -0
- package/dist/__tests__/writer.test.js +94 -0
- package/dist/api/auth/crypto.d.ts +4 -0
- package/dist/api/auth/crypto.js +54 -0
- package/dist/api/middleware/auth.d.ts +12 -0
- package/dist/api/middleware/auth.js +90 -0
- package/dist/api/pagination.d.ts +18 -0
- package/dist/api/pagination.js +32 -0
- package/dist/api/routes/auth.d.ts +1 -0
- package/dist/api/routes/auth.js +130 -0
- package/dist/api/routes/chat-completions.d.ts +7 -0
- package/dist/api/routes/chat-completions.js +474 -0
- package/dist/api/routes/contradictions.d.ts +1 -0
- package/dist/api/routes/contradictions.js +28 -0
- package/dist/api/routes/evidence.d.ts +1 -0
- package/dist/api/routes/evidence.js +59 -0
- package/dist/api/routes/governance.d.ts +1 -0
- package/dist/api/routes/governance.js +95 -0
- package/dist/api/routes/graph.d.ts +1 -0
- package/dist/api/routes/graph.js +25 -0
- package/dist/api/routes/hooks.d.ts +1 -0
- package/dist/api/routes/hooks.js +88 -0
- package/dist/api/routes/memories.d.ts +1 -0
- package/dist/api/routes/memories.js +92 -0
- package/dist/api/routes/persona.d.ts +1 -0
- package/dist/api/routes/persona.js +9 -0
- package/dist/api/routes/scenes.d.ts +1 -0
- package/dist/api/routes/scenes.js +35 -0
- package/dist/api/routes/skills.d.ts +1 -0
- package/dist/api/routes/skills.js +14 -0
- package/dist/api/routes/stats.d.ts +1 -0
- package/dist/api/routes/stats.js +8 -0
- package/dist/api/routes/users.d.ts +1 -0
- package/dist/api/routes/users.js +82 -0
- package/dist/api/routes/working.d.ts +1 -0
- package/dist/api/routes/working.js +88 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +492 -0
- package/dist/integrations/claude-code.d.ts +12 -0
- package/dist/integrations/claude-code.js +35 -0
- package/dist/integrations/codex.d.ts +12 -0
- package/dist/integrations/codex.js +34 -0
- package/dist/integrations/generic-mcp.d.ts +52 -0
- package/dist/integrations/generic-mcp.js +118 -0
- package/dist/loader.d.ts +29 -0
- package/dist/loader.js +200 -0
- package/dist/memory/capture.d.ts +35 -0
- package/dist/memory/capture.js +230 -0
- package/dist/memory/config.d.ts +2 -0
- package/dist/memory/config.js +3 -0
- package/dist/memory/engine.d.ts +203 -0
- package/dist/memory/engine.js +626 -0
- package/dist/memory/llm-semaphore.d.ts +41 -0
- package/dist/memory/llm-semaphore.js +81 -0
- package/dist/memory/memory-type-config.d.ts +11 -0
- package/dist/memory/memory-type-config.js +65 -0
- package/dist/memory/pipeline/cognitive-contradiction.d.ts +7 -0
- package/dist/memory/pipeline/cognitive-contradiction.js +59 -0
- package/dist/memory/pipeline/cognitive-dedup.d.ts +23 -0
- package/dist/memory/pipeline/cognitive-dedup.js +38 -0
- package/dist/memory/pipeline/cognitive-extractor.d.ts +21 -0
- package/dist/memory/pipeline/cognitive-extractor.js +183 -0
- package/dist/memory/pipeline/contextual-focus-builder.d.ts +13 -0
- package/dist/memory/pipeline/contextual-focus-builder.js +135 -0
- package/dist/memory/pipeline/focus-direction-shift.d.ts +10 -0
- package/dist/memory/pipeline/focus-direction-shift.js +27 -0
- package/dist/memory/pipeline/graph-builder.d.ts +11 -0
- package/dist/memory/pipeline/graph-builder.js +88 -0
- package/dist/memory/pipeline/graph-recall.d.ts +13 -0
- package/dist/memory/pipeline/graph-recall.js +55 -0
- package/dist/memory/pipeline/identity-distiller.d.ts +15 -0
- package/dist/memory/pipeline/identity-distiller.js +40 -0
- package/dist/memory/pipeline/l1-contradiction.d.ts +7 -0
- package/dist/memory/pipeline/l1-contradiction.js +66 -0
- package/dist/memory/pipeline/l1-dedup.d.ts +23 -0
- package/dist/memory/pipeline/l1-dedup.js +39 -0
- package/dist/memory/pipeline/l1-extractor.d.ts +21 -0
- package/dist/memory/pipeline/l1-extractor.js +180 -0
- package/dist/memory/pipeline/l2-direction-shift.d.ts +10 -0
- package/dist/memory/pipeline/l2-direction-shift.js +27 -0
- package/dist/memory/pipeline/l2-scene.d.ts +15 -0
- package/dist/memory/pipeline/l2-scene.js +140 -0
- package/dist/memory/pipeline/l3-distiller.d.ts +15 -0
- package/dist/memory/pipeline/l3-distiller.js +40 -0
- package/dist/memory/pipeline/neural-spark.d.ts +27 -0
- package/dist/memory/pipeline/neural-spark.js +78 -0
- package/dist/memory/pipeline/skill-prewarm.d.ts +63 -0
- package/dist/memory/pipeline/skill-prewarm.js +127 -0
- package/dist/memory/pipeline/task-queue.d.ts +54 -0
- package/dist/memory/pipeline/task-queue.js +117 -0
- package/dist/memory/prompts/cognitive-contradiction.d.ts +1 -0
- package/dist/memory/prompts/cognitive-contradiction.js +25 -0
- package/dist/memory/prompts/cognitive-extraction.d.ts +10 -0
- package/dist/memory/prompts/cognitive-extraction.js +114 -0
- package/dist/memory/prompts/core-identity.d.ts +6 -0
- package/dist/memory/prompts/core-identity.js +60 -0
- package/dist/memory/prompts/focus-direction-shift.d.ts +5 -0
- package/dist/memory/prompts/focus-direction-shift.js +32 -0
- package/dist/memory/prompts/focus-scene-cluster.d.ts +2 -0
- package/dist/memory/prompts/focus-scene-cluster.js +33 -0
- package/dist/memory/prompts/focus-scene.d.ts +7 -0
- package/dist/memory/prompts/focus-scene.js +40 -0
- package/dist/memory/prompts/graph-extraction-batch.d.ts +14 -0
- package/dist/memory/prompts/graph-extraction-batch.js +54 -0
- package/dist/memory/prompts/graph-extraction.d.ts +2 -0
- package/dist/memory/prompts/graph-extraction.js +53 -0
- package/dist/memory/prompts/l1-contradiction-batch.d.ts +16 -0
- package/dist/memory/prompts/l1-contradiction-batch.js +47 -0
- package/dist/memory/prompts/l1-contradiction.d.ts +1 -0
- package/dist/memory/prompts/l1-contradiction.js +25 -0
- package/dist/memory/prompts/l1-extraction.d.ts +10 -0
- package/dist/memory/prompts/l1-extraction.js +114 -0
- package/dist/memory/prompts/l2-direction-shift.d.ts +5 -0
- package/dist/memory/prompts/l2-direction-shift.js +32 -0
- package/dist/memory/prompts/l2-scene-cluster.d.ts +2 -0
- package/dist/memory/prompts/l2-scene-cluster.js +33 -0
- package/dist/memory/prompts/l2-scene.d.ts +7 -0
- package/dist/memory/prompts/l2-scene.js +40 -0
- package/dist/memory/prompts/l3-persona.d.ts +6 -0
- package/dist/memory/prompts/l3-persona.js +60 -0
- package/dist/memory/recall.d.ts +47 -0
- package/dist/memory/recall.js +427 -0
- package/dist/memory/redaction.d.ts +1 -0
- package/dist/memory/redaction.js +24 -0
- package/dist/memory/retry.d.ts +13 -0
- package/dist/memory/retry.js +53 -0
- package/dist/memory/scheduler.d.ts +9 -0
- package/dist/memory/scheduler.js +16 -0
- package/dist/memory/skill-hints-loader.d.ts +30 -0
- package/dist/memory/skill-hints-loader.js +100 -0
- package/dist/memory/store/embedding.d.ts +16 -0
- package/dist/memory/store/embedding.js +68 -0
- package/dist/memory/store/reranker.d.ts +24 -0
- package/dist/memory/store/reranker.js +83 -0
- package/dist/memory/store/sqlite.d.ts +167 -0
- package/dist/memory/store/sqlite.js +1816 -0
- package/dist/memory/store/types.d.ts +101 -0
- package/dist/memory/store/types.js +1 -0
- package/dist/memory/types.d.ts +207 -0
- package/dist/memory/types.js +7 -0
- package/dist/memory/validation.d.ts +441 -0
- package/dist/memory/validation.js +129 -0
- package/dist/memory/working/canvas.d.ts +5 -0
- package/dist/memory/working/canvas.js +43 -0
- package/dist/memory/working/offload.d.ts +71 -0
- package/dist/memory/working/offload.js +211 -0
- package/dist/memory/working/step-log.d.ts +16 -0
- package/dist/memory/working/step-log.js +35 -0
- package/dist/registry.d.ts +34 -0
- package/dist/registry.js +305 -0
- package/dist/resolver.d.ts +17 -0
- package/dist/resolver.js +126 -0
- package/dist/scripts/validate-foreign-workspace-path.d.ts +1 -0
- package/dist/scripts/validate-foreign-workspace-path.js +39 -0
- package/dist/tools/agent_memory_tools.d.ts +485 -0
- package/dist/tools/agent_memory_tools.js +793 -0
- package/dist/tools/create_skill.d.ts +46 -0
- package/dist/tools/create_skill.js +46 -0
- package/dist/tools/get_doc.d.ts +21 -0
- package/dist/tools/get_doc.js +24 -0
- package/dist/tools/get_persona.d.ts +15 -0
- package/dist/tools/get_persona.js +20 -0
- package/dist/tools/get_reference.d.ts +15 -0
- package/dist/tools/get_reference.js +20 -0
- package/dist/tools/get_skill.d.ts +34 -0
- package/dist/tools/get_skill.js +65 -0
- package/dist/tools/get_template_doc.d.ts +21 -0
- package/dist/tools/get_template_doc.js +24 -0
- package/dist/tools/list_docs.d.ts +15 -0
- package/dist/tools/list_docs.js +16 -0
- package/dist/tools/list_skills.d.ts +18 -0
- package/dist/tools/list_skills.js +17 -0
- package/dist/tools/list_template_docs.d.ts +15 -0
- package/dist/tools/list_template_docs.js +16 -0
- package/dist/tools/memory-engineering.d.ts +225 -0
- package/dist/tools/memory-engineering.js +284 -0
- package/dist/tools/memory-explain.d.ts +34 -0
- package/dist/tools/memory-explain.js +109 -0
- package/dist/tools/memory-governance.d.ts +171 -0
- package/dist/tools/memory-governance.js +224 -0
- package/dist/tools/memory-hooks.d.ts +67 -0
- package/dist/tools/memory-hooks.js +102 -0
- package/dist/tools/memory-working.d.ts +98 -0
- package/dist/tools/memory-working.js +101 -0
- package/dist/tools/memory_capture_turn.d.ts +66 -0
- package/dist/tools/memory_capture_turn.js +85 -0
- package/dist/tools/memory_consolidate.d.ts +55 -0
- package/dist/tools/memory_consolidate.js +176 -0
- package/dist/tools/memory_contradictions.d.ts +53 -0
- package/dist/tools/memory_contradictions.js +52 -0
- package/dist/tools/memory_graph_query.d.ts +51 -0
- package/dist/tools/memory_graph_query.js +35 -0
- package/dist/tools/memory_mark_cited.d.ts +43 -0
- package/dist/tools/memory_mark_cited.js +63 -0
- package/dist/tools/memory_recall.d.ts +77 -0
- package/dist/tools/memory_recall.js +81 -0
- package/dist/tools/memory_register_skill_hints.d.ts +49 -0
- package/dist/tools/memory_register_skill_hints.js +55 -0
- package/dist/tools/memory_resolve_session.d.ts +24 -0
- package/dist/tools/memory_resolve_session.js +133 -0
- package/dist/tools/memory_search.d.ts +146 -0
- package/dist/tools/memory_search.js +84 -0
- package/dist/tools/search_skills.d.ts +18 -0
- package/dist/tools/search_skills.js +17 -0
- package/dist/tools/update_doc.d.ts +24 -0
- package/dist/tools/update_doc.js +35 -0
- package/dist/tools/update_skill.d.ts +30 -0
- package/dist/tools/update_skill.js +80 -0
- package/dist/types.d.ts +81 -0
- package/dist/types.js +4 -0
- package/dist/writer.d.ts +30 -0
- package/dist/writer.js +220 -0
- package/docs/TEMPLATE ONLY +1 -0
- package/docs/api/API.md +64 -0
- package/docs/api/security/SECURITY.md +58 -0
- package/docs/deployment/DockerDeployment.md +30 -0
- package/docs/design/Design.md +59 -0
- package/docs/design/themes/apple.md +101 -0
- package/docs/design/themes/dieter-grid.md +100 -0
- package/docs/design/themes/gallery-white.md +100 -0
- package/docs/design/themes/pinterest.md +101 -0
- package/docs/design/themes/realty-open-house.md +101 -0
- package/docs/design/themes/vodafone.md +101 -0
- package/docs/hooks/Hooks.md +30 -0
- package/docs/schema/Schema.md +35 -0
- package/docs/strategy/ScalingStrategy.md +19 -0
- package/package.json +88 -0
- package/references/accessibility-checklist.md +160 -0
- package/references/orchestration-patterns.md +370 -0
- package/references/performance-checklist.md +153 -0
- package/references/security-checklist.md +134 -0
- package/references/testing-patterns.md +236 -0
- package/skills/agent/adr-skill/SKILL.md +299 -0
- package/skills/agent/agentic-engineering-workflow/SKILL.md +95 -0
- package/skills/agent/bootstrap-skill/SKILL.md +103 -0
- package/skills/agent/context-engineering/SKILL.md +307 -0
- package/skills/agent/debugging-and-error-recovery/SKILL.md +308 -0
- package/skills/agent/developer-growth-analysis/SKILL.md +328 -0
- package/skills/agent/doubt-driven-skill/SKILL.md +249 -0
- package/skills/agent/handover-skill/SKILL.md +112 -0
- package/skills/agent/idea-refine-skill/SKILL.md +185 -0
- package/skills/agent/idea-refine-skill/examples.md +238 -0
- package/skills/agent/idea-refine-skill/frameworks.md +99 -0
- package/skills/agent/idea-refine-skill/refinement-criteria.md +113 -0
- package/skills/agent/interview-skill/SKILL.md +226 -0
- package/skills/agent/planning-skill/SKILL.md +270 -0
- package/skills/agent/skill-authoring/SKILL.md +189 -0
- package/skills/agent/source-driven-skill/SKILL.md +197 -0
- package/skills/agent/spec-driven-skill/SKILL.md +221 -0
- package/skills/agent/sync-skill/SKILL.md +92 -0
- package/skills/agent/using-agent-skills/SKILL.md +189 -0
- package/skills/api/a11y-skill/SKILL.md +88 -0
- package/skills/api/api-skill/SKILL.md +123 -0
- package/skills/api/auth-skill/SKILL.md +80 -0
- package/skills/api/debug-skill/SKILL.md +535 -0
- package/skills/api/performance-skill/SKILL.md +100 -0
- package/skills/api/testing-skill/SKILL.md +100 -0
- package/skills/codebase/code-review-and-quality/SKILL.md +228 -0
- package/skills/codebase/code-simplification/SKILL.md +352 -0
- package/skills/codebase/code-structure-cleanup/SKILL.md +142 -0
- package/skills/codebase/concerns-skill/SKILL.md +89 -0
- package/skills/codebase/conventions-skill/SKILL.md +95 -0
- package/skills/codebase/doc-management-skill/SKILL.md +47 -0
- package/skills/codebase/git-workflow-skill/SKILL.md +312 -0
- package/skills/communication/1-3-1-rule/SKILL.md +120 -0
- package/skills/design/brutalist-skill/SKILL.md +131 -0
- package/skills/design/concept-diagrams/SKILL.md +387 -0
- package/skills/design/concept-diagrams/examples/apartment-floor-plan-conversion.md +244 -0
- package/skills/design/concept-diagrams/examples/automated-password-reset-flow.md +276 -0
- package/skills/design/concept-diagrams/examples/autonomous-llm-research-agent-flow.md +240 -0
- package/skills/design/concept-diagrams/examples/banana-journey-tree-to-smoothie.md +161 -0
- package/skills/design/concept-diagrams/examples/commercial-aircraft-structure.md +209 -0
- package/skills/design/concept-diagrams/examples/cpu-ooo-microarchitecture.md +236 -0
- package/skills/design/concept-diagrams/examples/electricity-grid-flow.md +182 -0
- package/skills/design/concept-diagrams/examples/feature-film-production-pipeline.md +172 -0
- package/skills/design/concept-diagrams/examples/hospital-emergency-department-flow.md +165 -0
- package/skills/design/concept-diagrams/examples/ml-benchmark-grouped-bar-chart.md +114 -0
- package/skills/design/concept-diagrams/examples/place-order-uml-sequence.md +325 -0
- package/skills/design/concept-diagrams/examples/smart-city-infrastructure.md +173 -0
- package/skills/design/concept-diagrams/examples/smartphone-layer-anatomy.md +154 -0
- package/skills/design/concept-diagrams/examples/sn2-reaction-mechanism.md +247 -0
- package/skills/design/concept-diagrams/examples/wind-turbine-structure.md +338 -0
- package/skills/design/concept-diagrams/references/dashboard-patterns.md +43 -0
- package/skills/design/concept-diagrams/references/infrastructure-patterns.md +144 -0
- package/skills/design/concept-diagrams/references/physical-shape-cookbook.md +42 -0
- package/skills/design/concept-diagrams/templates/template.html +174 -0
- package/skills/design/gpt-tasteskill/SKILL.md +114 -0
- package/skills/design/minimalist-skill/SKILL.md +116 -0
- package/skills/design/output-skill/SKILL.md +87 -0
- package/skills/design/redesign-skill/SKILL.md +213 -0
- package/skills/design/soft-skill/SKILL.md +132 -0
- package/skills/design/stitch-skill/EXAMPLE.md +121 -0
- package/skills/design/stitch-skill/SKILL.md +222 -0
- package/skills/design/taste-skill/SKILL.md +269 -0
- package/skills/devops/ci-cd-skill/SKILL.md +402 -0
- package/skills/devops/docker-skill/SKILL.md +297 -0
- package/skills/devops/domain-skill/SKILL.md +234 -0
- package/skills/lifecycle/changelog-generator/SKILL.md +135 -0
- package/skills/lifecycle/incremental-skill/SKILL.md +257 -0
- package/skills/lifecycle/migration-skill/SKILL.md +218 -0
- package/skills/lifecycle/shipping-skill/SKILL.md +321 -0
- package/skills/memory/agent-memory/SKILL.md +122 -0
- package/skills/qa/browser-testing-skill/SKILL.md +314 -0
- package/skills/ux/adversarial-ux-skill/SKILL.md +168 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { EmbeddingServiceConfig } from "@kinqs/brainrouter-types";
|
|
2
|
+
export declare class EmbeddingService {
|
|
3
|
+
private readonly endpoint;
|
|
4
|
+
private readonly apiKey;
|
|
5
|
+
private readonly model;
|
|
6
|
+
private readonly dimensions;
|
|
7
|
+
private readonly ready;
|
|
8
|
+
constructor(config: EmbeddingServiceConfig);
|
|
9
|
+
getDimensions(): number;
|
|
10
|
+
isReady(): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Get an embedding for a single text.
|
|
13
|
+
* Throws if not ready, so always check isReady() first.
|
|
14
|
+
*/
|
|
15
|
+
embed(text: string): Promise<Float32Array>;
|
|
16
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { fetchWithExternalRetry } from "../retry.js";
|
|
2
|
+
import { acquireLLMSlot } from "../llm-semaphore.js";
|
|
3
|
+
export class EmbeddingService {
|
|
4
|
+
endpoint;
|
|
5
|
+
apiKey;
|
|
6
|
+
model;
|
|
7
|
+
dimensions;
|
|
8
|
+
ready;
|
|
9
|
+
constructor(config) {
|
|
10
|
+
this.endpoint = config.endpoint ?? "https://api.openai.com/v1/embeddings";
|
|
11
|
+
this.apiKey = config.apiKey ?? "";
|
|
12
|
+
this.model = config.model ?? "text-embedding-3-small";
|
|
13
|
+
this.dimensions = config.dimensions ?? 768;
|
|
14
|
+
// Graceful fallback: If no API key is provided, we disable the embedding service.
|
|
15
|
+
this.ready = !!this.apiKey;
|
|
16
|
+
if (!this.ready) {
|
|
17
|
+
console.error("[BrainRouter] Embedding API key not set. Vector search will be disabled. Falling back to FTS-only mode.");
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
getDimensions() {
|
|
21
|
+
return this.dimensions;
|
|
22
|
+
}
|
|
23
|
+
isReady() {
|
|
24
|
+
return this.ready;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Get an embedding for a single text.
|
|
28
|
+
* Throws if not ready, so always check isReady() first.
|
|
29
|
+
*/
|
|
30
|
+
async embed(text) {
|
|
31
|
+
if (!this.ready) {
|
|
32
|
+
throw new Error("EmbeddingService is not ready (missing API key)");
|
|
33
|
+
}
|
|
34
|
+
// Same backend as ModelLLMRunner — go through the shared semaphore so
|
|
35
|
+
// embedding requests count against the concurrency cap. Otherwise a burst
|
|
36
|
+
// of new cognitive records can fire N embeddings + N LLM calls in
|
|
37
|
+
// parallel and overwhelm LM Studio just like before.
|
|
38
|
+
const release = await acquireLLMSlot();
|
|
39
|
+
try {
|
|
40
|
+
const res = await fetchWithExternalRetry(this.endpoint, {
|
|
41
|
+
method: "POST",
|
|
42
|
+
headers: {
|
|
43
|
+
"Content-Type": "application/json",
|
|
44
|
+
"Authorization": `Bearer ${this.apiKey}`,
|
|
45
|
+
},
|
|
46
|
+
body: JSON.stringify({
|
|
47
|
+
input: text,
|
|
48
|
+
model: this.model,
|
|
49
|
+
}),
|
|
50
|
+
}, {
|
|
51
|
+
label: "Embedding API",
|
|
52
|
+
});
|
|
53
|
+
if (!res.ok) {
|
|
54
|
+
const err = await res.text().catch(() => "(no body)");
|
|
55
|
+
throw new Error(`Embedding API failed: HTTP ${res.status} ${res.statusText} - ${err}`);
|
|
56
|
+
}
|
|
57
|
+
const data = await res.json();
|
|
58
|
+
if (!data.data || !data.data[0] || !Array.isArray(data.data[0].embedding)) {
|
|
59
|
+
throw new Error("Invalid embedding response format");
|
|
60
|
+
}
|
|
61
|
+
const vec = data.data[0].embedding;
|
|
62
|
+
return new Float32Array(vec);
|
|
63
|
+
}
|
|
64
|
+
finally {
|
|
65
|
+
release();
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { RerankerServiceConfig } from "@kinqs/brainrouter-types";
|
|
2
|
+
export interface RankedResult {
|
|
3
|
+
index: number;
|
|
4
|
+
relevanceScore: number;
|
|
5
|
+
}
|
|
6
|
+
export declare class RerankerService {
|
|
7
|
+
private readonly endpoint;
|
|
8
|
+
private readonly apiKey;
|
|
9
|
+
private readonly model;
|
|
10
|
+
private readonly topN;
|
|
11
|
+
private readonly ready;
|
|
12
|
+
constructor(config: RerankerServiceConfig);
|
|
13
|
+
isReady(): boolean;
|
|
14
|
+
getTopN(): number;
|
|
15
|
+
/**
|
|
16
|
+
* Reranks documents against a query using Cohere/vLLM /v1/rerank API.
|
|
17
|
+
* Throws if not ready, so always check isReady() first.
|
|
18
|
+
*/
|
|
19
|
+
rerank(params: {
|
|
20
|
+
query: string;
|
|
21
|
+
documents: string[];
|
|
22
|
+
topN?: number;
|
|
23
|
+
}): Promise<RankedResult[]>;
|
|
24
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { fetchWithExternalRetry } from "../retry.js";
|
|
2
|
+
export class RerankerService {
|
|
3
|
+
endpoint;
|
|
4
|
+
apiKey;
|
|
5
|
+
model;
|
|
6
|
+
topN;
|
|
7
|
+
ready;
|
|
8
|
+
constructor(config) {
|
|
9
|
+
this.endpoint = config.endpoint ?? "https://api.cohere.com/v1/rerank";
|
|
10
|
+
this.apiKey = config.apiKey ?? "";
|
|
11
|
+
this.model = config.model ?? "rerank-english-v3.0";
|
|
12
|
+
this.topN = config.topN ?? 5;
|
|
13
|
+
// Graceful fallback: If no API key is provided, disable the reranker service.
|
|
14
|
+
this.ready = !!this.apiKey;
|
|
15
|
+
if (!this.ready) {
|
|
16
|
+
console.error("[BrainRouter] Reranker API key not set. Stage 3 reranking will be disabled. Falling back to RRF-only mode.");
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
isReady() {
|
|
20
|
+
return this.ready;
|
|
21
|
+
}
|
|
22
|
+
getTopN() {
|
|
23
|
+
return this.topN;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Reranks documents against a query using Cohere/vLLM /v1/rerank API.
|
|
27
|
+
* Throws if not ready, so always check isReady() first.
|
|
28
|
+
*/
|
|
29
|
+
async rerank(params) {
|
|
30
|
+
if (!this.ready) {
|
|
31
|
+
throw new Error("RerankerService is not ready (missing API key)");
|
|
32
|
+
}
|
|
33
|
+
if (params.documents.length === 0) {
|
|
34
|
+
return [];
|
|
35
|
+
}
|
|
36
|
+
const requestTopN = params.topN ?? this.topN;
|
|
37
|
+
// Graceful truncation: Ensure inputs do not exceed typical 512-token context length (approx 2000 chars).
|
|
38
|
+
// To be absolutely safe and prevent HTTP 400 Bad Request errors from local rerankers
|
|
39
|
+
// with strict 512-token limits, we limit the query to 200 characters (~50 tokens)
|
|
40
|
+
// and each document to 700 characters (~180 tokens).
|
|
41
|
+
const safeDocuments = params.documents.map(doc => doc.length > 700 ? doc.substring(0, 700) + "..." : doc);
|
|
42
|
+
const safeQuery = params.query.length > 200
|
|
43
|
+
? params.query.substring(0, 200) + "..."
|
|
44
|
+
: params.query;
|
|
45
|
+
const res = await fetchWithExternalRetry(this.endpoint, {
|
|
46
|
+
method: "POST",
|
|
47
|
+
headers: {
|
|
48
|
+
"Content-Type": "application/json",
|
|
49
|
+
"Authorization": `Bearer ${this.apiKey}`,
|
|
50
|
+
},
|
|
51
|
+
body: JSON.stringify({
|
|
52
|
+
query: safeQuery,
|
|
53
|
+
documents: safeDocuments,
|
|
54
|
+
model: this.model,
|
|
55
|
+
top_n: requestTopN,
|
|
56
|
+
}),
|
|
57
|
+
}, {
|
|
58
|
+
label: "Reranker API",
|
|
59
|
+
});
|
|
60
|
+
if (!res.ok) {
|
|
61
|
+
const err = await res.text().catch(() => "(no body)");
|
|
62
|
+
throw new Error(`Reranker API failed: HTTP ${res.status} ${res.statusText} - ${err}`);
|
|
63
|
+
}
|
|
64
|
+
const data = await res.json();
|
|
65
|
+
// Example vLLM response:
|
|
66
|
+
// {
|
|
67
|
+
// 'id': 'score-940bec41fb803c3f',
|
|
68
|
+
// 'model': 'BAAI/bge-reranker-v2-m3',
|
|
69
|
+
// 'results': [
|
|
70
|
+
// {'index': 0, 'document': {'text': '...'}, 'relevance_score': 0.997682},
|
|
71
|
+
// {'index': 1, 'document': {'text': '...'}, 'relevance_score': 0.000016}
|
|
72
|
+
// ]
|
|
73
|
+
// }
|
|
74
|
+
if (!data.results || !Array.isArray(data.results)) {
|
|
75
|
+
throw new Error("Invalid reranker response format: missing 'results' array");
|
|
76
|
+
}
|
|
77
|
+
const rankedResults = data.results.map((r) => ({
|
|
78
|
+
index: r.index,
|
|
79
|
+
relevanceScore: r.relevance_score
|
|
80
|
+
}));
|
|
81
|
+
return rankedResults;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import type { ContradictionRecord, CursorPaginationOptions, EvidenceListFilters, ExtractionStatus, ImportResult, SensoryRecord, CognitiveRecord, CognitiveFtsResult, MemoryEvidence, MemoryExport, MemoryImport, MemoryListFilters, MemoryListItem, MemoryOperation, MemoryStatus, OperationLogFilters, VectorSearchResult, SkillActivationRecord, SkillHintsRecord, ContextualFocusRecord, CoreIdentityRecord, SchedulerState, GraphNode, GraphEdge, StalledExtractionBacklog, UserRecord } from "@kinqs/brainrouter-types";
|
|
2
|
+
import type { IMemoryStore } from "@kinqs/brainrouter-types";
|
|
3
|
+
export declare class SqliteMemoryStore implements IMemoryStore {
|
|
4
|
+
private db;
|
|
5
|
+
private stmtSensoryUpsertMeta;
|
|
6
|
+
private stmtSensoryQueryAfter;
|
|
7
|
+
private stmtCognitiveUpsertMeta;
|
|
8
|
+
private stmtCognitiveGetMeta;
|
|
9
|
+
private stmtCognitiveFtsInsert;
|
|
10
|
+
private stmtCognitiveFtsSearch;
|
|
11
|
+
private stmtCognitiveVecInsert?;
|
|
12
|
+
private stmtCognitiveVecDelete?;
|
|
13
|
+
private vecLoaded;
|
|
14
|
+
private vecDimensions;
|
|
15
|
+
constructor(dbPath: string);
|
|
16
|
+
init(): void;
|
|
17
|
+
initVec(dimensions: number): void;
|
|
18
|
+
isVecAvailable(): boolean;
|
|
19
|
+
reembedStaleRecords(embedder: (text: string) => Promise<Float32Array>): Promise<number>;
|
|
20
|
+
getSqliteVersion(): string;
|
|
21
|
+
private initSchema;
|
|
22
|
+
upsertSensory(record: SensoryRecord): void;
|
|
23
|
+
getRecentSensoryMessages(userId: string, sessionKey: string, limit: number, afterIsoTime?: string): SensoryRecord[];
|
|
24
|
+
getUnextractedSensoryCount(userId: string, sessionKey: string): number;
|
|
25
|
+
markSensoryExtracted(userId: string, sessionKey: string, recordIds: string[], extractedAt?: string): void;
|
|
26
|
+
upsertCognitiveBatch(entries: Array<{
|
|
27
|
+
record: CognitiveRecord;
|
|
28
|
+
embedding?: Float32Array;
|
|
29
|
+
}>, options?: {
|
|
30
|
+
skipAudit?: boolean;
|
|
31
|
+
}): void;
|
|
32
|
+
upsertCognitive(record: CognitiveRecord, options?: {
|
|
33
|
+
skipAudit?: boolean;
|
|
34
|
+
}): void;
|
|
35
|
+
invalidateCognitiveRecord(userId: string, recordId: string, supersededById: string): void;
|
|
36
|
+
getMemoryById(userId: string, recordId: string): CognitiveRecord | null;
|
|
37
|
+
getMemoriesByFilePath(userId: string, filePath: string, limit: number): CognitiveRecord[];
|
|
38
|
+
updateCognitiveConfidence(userId: string, recordId: string, confidence: number, status: MemoryStatus): void;
|
|
39
|
+
insertEvidence(ev: MemoryEvidence): void;
|
|
40
|
+
getEvidenceByRecord(userId: string, recordId: string): MemoryEvidence[];
|
|
41
|
+
listEvidence(userId: string, filters?: EvidenceListFilters, pagination?: CursorPaginationOptions<{
|
|
42
|
+
observedAt: string;
|
|
43
|
+
id: string;
|
|
44
|
+
}>): MemoryEvidence[];
|
|
45
|
+
insertOperation(op: MemoryOperation): void;
|
|
46
|
+
getOperationLog(userId: string, options?: CursorPaginationOptions<{
|
|
47
|
+
createdAt: string;
|
|
48
|
+
id: string;
|
|
49
|
+
}>, filters?: OperationLogFilters): MemoryOperation[];
|
|
50
|
+
exportMemories(userId: string): MemoryExport;
|
|
51
|
+
importMemories(userId: string, data: MemoryImport): ImportResult;
|
|
52
|
+
hardDeleteMemory(userId: string, recordId: string, reason: string): void;
|
|
53
|
+
searchCognitiveFts(userId: string, query: string, limit: number): CognitiveFtsResult[];
|
|
54
|
+
searchCognitiveFtsAsOf(userId: string, query: string, limit: number, asOf: string): CognitiveFtsResult[];
|
|
55
|
+
upsertCognitiveVec(recordId: string, embedding: Float32Array): void;
|
|
56
|
+
searchCognitiveVec(userId: string, queryEmbedding: Float32Array, limit: number): VectorSearchResult[];
|
|
57
|
+
upsertContradiction(data: {
|
|
58
|
+
id: string;
|
|
59
|
+
userId: string;
|
|
60
|
+
recordIdA: string;
|
|
61
|
+
recordIdB: string;
|
|
62
|
+
reason: string;
|
|
63
|
+
confidence: number;
|
|
64
|
+
}): void;
|
|
65
|
+
getPendingContradictions(userId: string, pagination?: CursorPaginationOptions<{
|
|
66
|
+
confidence: number;
|
|
67
|
+
id: string;
|
|
68
|
+
}>): ContradictionRecord[];
|
|
69
|
+
resolveContradiction(id: string, userId: string, status: 'resolved' | 'dismissed'): void;
|
|
70
|
+
upsertSkillHints(skillName: string, hints: string, sourceFile?: string): void;
|
|
71
|
+
listSkillHints(): SkillHintsRecord[];
|
|
72
|
+
upsertContextualFocus(record: ContextualFocusRecord): void;
|
|
73
|
+
getTopContextualFocus(userId: string, limit?: number, cursor?: {
|
|
74
|
+
heatScore: number;
|
|
75
|
+
id: string;
|
|
76
|
+
}): ContextualFocusRecord[];
|
|
77
|
+
decayContextualFocusHeatScores(userId: string, decayFactor?: number): void;
|
|
78
|
+
boostContextualFocusHeatScore(userId: string, sceneName: string, boost?: number): void;
|
|
79
|
+
getCognitivesByFocus(userId: string, sceneName: string, limit?: number): any[];
|
|
80
|
+
getContextualFocusCount(userId: string): number;
|
|
81
|
+
getColdContextualFocus(userId: string, limit: number): ContextualFocusRecord[];
|
|
82
|
+
deleteContextualFocus(userId: string, sceneIds: string[]): void;
|
|
83
|
+
getContextualFocusByName(userId: string, sceneName: string): ContextualFocusRecord | null;
|
|
84
|
+
getDistinctSceneNames(userId: string): string[];
|
|
85
|
+
renameFocusInCognitiveRecords(userId: string, oldName: string, canonicalName: string): void;
|
|
86
|
+
upsertCoreIdentity(record: CoreIdentityRecord): void;
|
|
87
|
+
getCoreIdentity(userId: string): CoreIdentityRecord | null;
|
|
88
|
+
getIdentityAndInstructionCognitives(userId: string, limit?: number): any[];
|
|
89
|
+
getSchedulerState(userId: string): SchedulerState;
|
|
90
|
+
incrementSchedulerCognitiveCount(userId: string, count: number): void;
|
|
91
|
+
resetSchedulerFocusCount(userId: string): void;
|
|
92
|
+
resetSchedulerIdentityCount(userId: string): void;
|
|
93
|
+
recordExtractionFailure(userId: string, message: string): void;
|
|
94
|
+
resetExtractionFailures(userId: string): void;
|
|
95
|
+
getExtractionStatus(userId: string): ExtractionStatus;
|
|
96
|
+
sweepUnextractedBacklog(options: {
|
|
97
|
+
olderThanMs: number;
|
|
98
|
+
minUnextracted?: number;
|
|
99
|
+
maxFailures?: number;
|
|
100
|
+
limit?: number;
|
|
101
|
+
}): StalledExtractionBacklog[];
|
|
102
|
+
getAllGraphNodes(userId: string): GraphNode[];
|
|
103
|
+
upsertGraphNode(node: GraphNode): void;
|
|
104
|
+
upsertGraphEdge(edge: GraphEdge): void;
|
|
105
|
+
getGraphNodeByEntity(userId: string, entity: string): GraphNode | null;
|
|
106
|
+
getGraphNeighbors(userId: string, entityId: string, skillTag?: string, maxHops?: number): {
|
|
107
|
+
nodes: GraphNode[];
|
|
108
|
+
edges: GraphEdge[];
|
|
109
|
+
};
|
|
110
|
+
markCited(userId: string, recordIds: string[]): void;
|
|
111
|
+
incrementNeverCited(userId: string, recordIds: string[]): {
|
|
112
|
+
recordId: string;
|
|
113
|
+
neverCitedCount: number;
|
|
114
|
+
}[];
|
|
115
|
+
archiveCognitiveRecord(userId: string, recordId: string): void;
|
|
116
|
+
getRecentSkillContextCognitives(userId: string, limit: number): {
|
|
117
|
+
skillTag: string;
|
|
118
|
+
createdTime: string;
|
|
119
|
+
}[];
|
|
120
|
+
getSkillHints(skillName: string): string | null;
|
|
121
|
+
getSkillActivations(userId: string): SkillActivationRecord[];
|
|
122
|
+
upsertSkillActivations(userId: string, activations: SkillActivationRecord[]): void;
|
|
123
|
+
createUser(userId: string, apiKey: string, displayName?: string, isAdmin?: boolean): UserRecord;
|
|
124
|
+
getUserByApiKey(apiKey: string): UserRecord | null;
|
|
125
|
+
getUserByEmail(email: string): UserRecord | null;
|
|
126
|
+
getUserById(userId: string): UserRecord | null;
|
|
127
|
+
updateUserPassword(userId: string, passwordHash: string): void;
|
|
128
|
+
updateUserEmail(userId: string, email: string): void;
|
|
129
|
+
updateUserDisplayName(userId: string, displayName: string): void;
|
|
130
|
+
updateUserStatus(userId: string, status: "active" | "disabled"): void;
|
|
131
|
+
updateUserApiKey(userId: string, apiKey: string): void;
|
|
132
|
+
listUsers(pagination?: CursorPaginationOptions<{
|
|
133
|
+
createdAt: string;
|
|
134
|
+
userId: string;
|
|
135
|
+
}>): UserRecord[];
|
|
136
|
+
deleteUser(userId: string): void;
|
|
137
|
+
listMemories(userId: string, filters?: MemoryListFilters, pagination?: CursorPaginationOptions<{
|
|
138
|
+
createdTime: string;
|
|
139
|
+
recordId: string;
|
|
140
|
+
}>): MemoryListItem[];
|
|
141
|
+
getMemoryStats(userId: string): {
|
|
142
|
+
total: number;
|
|
143
|
+
archived: number;
|
|
144
|
+
byType: Record<string, number>;
|
|
145
|
+
citationRate: number;
|
|
146
|
+
lastRecallAt: string | null;
|
|
147
|
+
extraction: ExtractionStatus;
|
|
148
|
+
};
|
|
149
|
+
private replaceFileIndex;
|
|
150
|
+
upsertConnection(userId: string, sourceId: string, targetId: string, weight: number): void;
|
|
151
|
+
getConnectionsForSource(userId: string, sourceId: string): Array<{
|
|
152
|
+
targetId: string;
|
|
153
|
+
weight: number;
|
|
154
|
+
}>;
|
|
155
|
+
strengthenConnectionsBatch(userId: string, pairs: Array<{
|
|
156
|
+
source: string;
|
|
157
|
+
target: string;
|
|
158
|
+
}>, delta: number): void;
|
|
159
|
+
decayConnections(userId: string, decayFactor: number): void;
|
|
160
|
+
pruneConnections(userId: string, threshold: number): void;
|
|
161
|
+
getAllConnections(userId: string): Array<{
|
|
162
|
+
sourceId: string;
|
|
163
|
+
targetId: string;
|
|
164
|
+
weight: number;
|
|
165
|
+
lastActivatedAt: string;
|
|
166
|
+
}>;
|
|
167
|
+
}
|