feat: implement Brain.create
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -32,3 +32,5 @@ report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
|
||||
|
||||
# Finder (MacOS) folder config
|
||||
.DS_Store
|
||||
|
||||
.omo
|
||||
@@ -1,6 +1,10 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { config } from "@/config";
|
||||
import { IdentityDB, type Space } from "identitydb";
|
||||
import { llm } from "@/openrouter";
|
||||
import { loadPrompt } from "@/openrouter/promptLoader";
|
||||
import { logger } from "@/utils/logger";
|
||||
import { factExtractor } from "./factExtractor";
|
||||
import { brainManager, type BrainItem } from "./manager";
|
||||
|
||||
export class Brain {
|
||||
@@ -13,7 +17,54 @@ export class Brain {
|
||||
static async create(
|
||||
displayName: string,
|
||||
seed: string,
|
||||
): Promise<Brain | null> {}
|
||||
): Promise<Brain | null> {
|
||||
try {
|
||||
const personaInitInstruction = await loadPrompt("PERSONA_INIT");
|
||||
const description = await llm.call<string>(llm.models.identity, {
|
||||
instruction: personaInitInstruction,
|
||||
message: seed,
|
||||
});
|
||||
|
||||
const personaSystemInstruction = await loadPrompt(
|
||||
"PERSONA_BASE_SYSTEM_PROMPT",
|
||||
);
|
||||
const baseSystemPrompt = await llm.call<string>(llm.models.identity, {
|
||||
instruction: personaSystemInstruction,
|
||||
message: description,
|
||||
});
|
||||
|
||||
const db = await IdentityDB.connect({
|
||||
client: "sqlite",
|
||||
filename: config.dbPath,
|
||||
});
|
||||
await db.initialize();
|
||||
const brainId = randomUUID();
|
||||
const spaceName = `brain:${brainId}`;
|
||||
const space = await db.upsertSpace({
|
||||
name: spaceName,
|
||||
description: displayName,
|
||||
});
|
||||
|
||||
await db.ingestStatement(description, {
|
||||
extractor: factExtractor,
|
||||
spaceName,
|
||||
});
|
||||
|
||||
const brainbase: BrainItem = {
|
||||
brainId,
|
||||
spaceName,
|
||||
displayName,
|
||||
baseSystemPrompt,
|
||||
};
|
||||
await brainManager.saveBrain(brainId, brainbase);
|
||||
|
||||
return new Brain(db, space, brainbase);
|
||||
} catch (error) {
|
||||
const reason = error instanceof Error ? error.message : String(error);
|
||||
logger.error(`Failed to create brain "${displayName}": ${reason}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static async load(brainId: string): Promise<Brain | null> {
|
||||
const brain = await brainManager.loadBrain(brainId);
|
||||
|
||||
Reference in New Issue
Block a user