feat: implemented supporters
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
DB_PATH=
|
DB_PATH=./brainbox.db
|
||||||
|
BRAINDB_PATH=./braindb.json
|
||||||
|
|
||||||
OPENROUTER_API_KEY=
|
OPENROUTER_API_KEY=
|
||||||
|
|||||||
16
src/brain/factExtractor.ts
Normal file
16
src/brain/factExtractor.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import { llm } from "@/openrouter";
|
||||||
|
import { extractedFactSchema } from "@/openrouter/schema";
|
||||||
|
import { type ExtractedFact, LlmFactExtractor } from "identitydb";
|
||||||
|
|
||||||
|
export const factExtractor = new LlmFactExtractor({
|
||||||
|
model: {
|
||||||
|
async generateText({ instruction, input }) {
|
||||||
|
return await llm.call<ExtractedFact[]>(llm.models.identity, {
|
||||||
|
instruction,
|
||||||
|
message: input,
|
||||||
|
jsonSchemaName: "fact-extractor",
|
||||||
|
jsonSchema: extractedFactSchema,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
32
src/brain/index.ts
Normal file
32
src/brain/index.ts
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
import { config } from "@/config";
|
||||||
|
import { IdentityDB, type Space } from "identitydb";
|
||||||
|
import { llm } from "@/openrouter";
|
||||||
|
import { brainManager, type BrainItem } from "./manager";
|
||||||
|
|
||||||
|
export class Brain {
|
||||||
|
constructor(
|
||||||
|
public db: IdentityDB,
|
||||||
|
public space: Space,
|
||||||
|
public brainbase: BrainItem,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
static async create(
|
||||||
|
displayName: string,
|
||||||
|
seed: string,
|
||||||
|
): Promise<Brain | null> {}
|
||||||
|
|
||||||
|
static async load(brainId: string): Promise<Brain | null> {
|
||||||
|
const brain = await brainManager.loadBrain(brainId);
|
||||||
|
if (!brain) return null;
|
||||||
|
|
||||||
|
const db = await IdentityDB.connect({
|
||||||
|
client: "sqlite",
|
||||||
|
filename: config.dbPath,
|
||||||
|
});
|
||||||
|
|
||||||
|
const space = await db.getSpaceByName(brain.spaceName);
|
||||||
|
if (!space) return null;
|
||||||
|
|
||||||
|
return new Brain(db, space, brain);
|
||||||
|
}
|
||||||
|
}
|
||||||
55
src/brain/manager.ts
Normal file
55
src/brain/manager.ts
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
import { config } from "@/config";
|
||||||
|
import { readFile, writeFile } from "fs/promises";
|
||||||
|
|
||||||
|
export interface BrainItem {
|
||||||
|
brainId: string;
|
||||||
|
spaceName: string;
|
||||||
|
displayName: string;
|
||||||
|
baseSystemPrompt: string;
|
||||||
|
}
|
||||||
|
export type BrainDB = Record<string, BrainItem>;
|
||||||
|
|
||||||
|
export class BrainDBManager {
|
||||||
|
private get db() {
|
||||||
|
return readFile(config.braindbPath, { encoding: "utf-8" }).then(
|
||||||
|
(content) => {
|
||||||
|
return JSON.parse(content) as BrainDB;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async writeDb(db: BrainDB) {
|
||||||
|
await writeFile(config.braindbPath, JSON.stringify(db), {
|
||||||
|
encoding: "utf-8",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async loadBrain(brainId: string): Promise<BrainItem | undefined> {
|
||||||
|
const brainOrNot = (await this.db)[brainId];
|
||||||
|
return brainOrNot;
|
||||||
|
}
|
||||||
|
|
||||||
|
async saveBrain(brainId: string, brain: BrainItem) {
|
||||||
|
const db = await this.db;
|
||||||
|
db[brainId] = brain;
|
||||||
|
await this.writeDb(db);
|
||||||
|
}
|
||||||
|
|
||||||
|
async listBrain() {
|
||||||
|
return Object.entries(await this.db).map(
|
||||||
|
([_, { brainId, displayName }]) => ({ brainId, displayName }),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async deleteBrain(brainId: string) {
|
||||||
|
const db = await this.db;
|
||||||
|
delete db[brainId];
|
||||||
|
await this.writeDb(db);
|
||||||
|
}
|
||||||
|
|
||||||
|
async isBrainAvailable(brainId: string) {
|
||||||
|
return brainId in (await this.db);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const brainManager = new BrainDBManager();
|
||||||
@@ -1,15 +1,22 @@
|
|||||||
import "dotenv/config";
|
import "dotenv/config";
|
||||||
|
import { join } from "path";
|
||||||
|
|
||||||
export interface Config {
|
export interface Config {
|
||||||
openrouterApiKey: string;
|
openrouterApiKey: string;
|
||||||
dbPath: string;
|
dbPath: string;
|
||||||
|
braindbPath: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const openrouterApiKey = process.env["OPENROUTER_API_KEY"];
|
const openrouterApiKey = process.env["OPENROUTER_API_KEY"];
|
||||||
if (!openrouterApiKey) throw new Error("OPENROUTER_API_KEY is missing");
|
if (!openrouterApiKey) throw new Error("OPENROUTER_API_KEY is missing");
|
||||||
const dbPath = process.env["DB_PATH"] ?? "sqlite.db";
|
const dbPath = join(process.cwd(), process.env["DB_PATH"] ?? "brainbox.db");
|
||||||
|
const braindbPath = join(
|
||||||
|
process.cwd(),
|
||||||
|
process.env["BRAINDB_PATH"] ?? "brainbox.json",
|
||||||
|
);
|
||||||
|
|
||||||
export const config: Config = {
|
export const config: Config = {
|
||||||
openrouterApiKey,
|
openrouterApiKey,
|
||||||
dbPath,
|
dbPath,
|
||||||
|
braindbPath,
|
||||||
};
|
};
|
||||||
|
|||||||
16
src/openrouter/promptLoader.ts
Normal file
16
src/openrouter/promptLoader.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import { readFile } from "fs/promises";
|
||||||
|
import path from "path";
|
||||||
|
|
||||||
|
const prompts = ["PERSONA_INIT", "PERSONA_BASE_SYSTEM_PROMPT"] as const;
|
||||||
|
export type PromptKey = (typeof prompts)[number];
|
||||||
|
|
||||||
|
function fileName(promptKey: PromptKey): string {
|
||||||
|
return promptKey.toLowerCase() + ".md";
|
||||||
|
}
|
||||||
|
|
||||||
|
const PROMPTS_DIR = path.resolve(import.meta.dir, "../../prompts");
|
||||||
|
|
||||||
|
export async function loadPrompt(promptKey: PromptKey): Promise<string> {
|
||||||
|
const filePath = path.join(PROMPTS_DIR, fileName(promptKey));
|
||||||
|
return readFile(filePath, "utf-8");
|
||||||
|
}
|
||||||
37
src/openrouter/schema.ts
Normal file
37
src/openrouter/schema.ts
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
export const extractedFactSchema = {
|
||||||
|
type: "array",
|
||||||
|
items: {
|
||||||
|
type: "object",
|
||||||
|
additionalProperties: false,
|
||||||
|
properties: {
|
||||||
|
statement: { type: "string" },
|
||||||
|
summary: { type: "string" },
|
||||||
|
source: { type: "string" },
|
||||||
|
confidence: { type: "number" },
|
||||||
|
metadata: { type: "object", additionalProperties: false },
|
||||||
|
topics: {
|
||||||
|
type: "array",
|
||||||
|
items: {
|
||||||
|
type: "object",
|
||||||
|
additionalProperties: false,
|
||||||
|
properties: {
|
||||||
|
name: { type: "string" },
|
||||||
|
category: {
|
||||||
|
type: "string",
|
||||||
|
enum: ["entity", "concept", "temporal", "custom"],
|
||||||
|
},
|
||||||
|
granularity: {
|
||||||
|
type: "string",
|
||||||
|
enum: ["abstract", "concrete", "mixed"],
|
||||||
|
},
|
||||||
|
role: { type: "string" },
|
||||||
|
description: { type: "string" },
|
||||||
|
metadata: { type: "object", additionalProperties: false },
|
||||||
|
},
|
||||||
|
required: ["name", "category", "granularity", "role", "description"],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
required: ["statement", "summary", "source", "confidence", "topics"],
|
||||||
|
},
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user