From d72c3dde0b910c83bc348520e76028e8ad0d9630 Mon Sep 17 00:00:00 2001 From: p-sw Date: Fri, 5 Jun 2026 22:56:21 +0900 Subject: [PATCH] feat: implement Brain.create --- .gitignore | 2 ++ src/brain/index.ts | 53 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index a14702c..d21fc01 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,5 @@ report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json # Finder (MacOS) folder config .DS_Store + +.omo \ No newline at end of file diff --git a/src/brain/index.ts b/src/brain/index.ts index 46fcd4a..d2c39f8 100644 --- a/src/brain/index.ts +++ b/src/brain/index.ts @@ -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 {} + ): Promise { + try { + const personaInitInstruction = await loadPrompt("PERSONA_INIT"); + const description = await llm.call(llm.models.identity, { + instruction: personaInitInstruction, + message: seed, + }); + + const personaSystemInstruction = await loadPrompt( + "PERSONA_BASE_SYSTEM_PROMPT", + ); + const baseSystemPrompt = await llm.call(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 { const brain = await brainManager.loadBrain(brainId);