Files
ForgeBase/Max Avatar/src/main/memory/store.ts
T
williamandClaude Sonnet 5 1c2f002eb3 refactor: move app code into Max Avatar/ subfolder (monorepo prep for backend)
Step 1 of adding a backend proxy server (API keys held server-side instead
of shipped in the installer). Moves everything app-specific into
"Max Avatar/" via git mv (preserves history): src/, tests/, scripts/,
assets/, build/, resources/, package.json/-lock.json, electron-builder.yml,
electron.vite.config.ts, eslint.config.mjs, playwright.config.ts,
vitest.config.mts, sonar-project.properties, tsconfig*.json, Dockerfile,
.editorconfig/.env*/.gitignore/.prettier*.

CLAUDE.md, README.md, docs/, .claude/, .gitea/, .husky/, .vscode/, and the
docker-compose files stay at the repo root, since they'll apply to both the
app and the future backend.

No application code needed changes (all its internal paths are relative to
its own tree). Adjusted what referenced the old root from outside:
- .husky/pre-commit: cd into "Max Avatar" before lint-staged
- .gitea/workflows/*.yml: working-directory + cache-dependency-path +
  artifact paths + SonarQube projectBaseDir
- docker-compose.yml: build context -> ./Max Avatar
- Max Avatar/Dockerfile: node:20-alpine -> node:24-alpine (same node:sqlite
  issue already fixed elsewhere, never applied here before)
- .vscode/launch.json, README.md: updated paths/instructions

Verified from Max Avatar/ after the move: npm ci, typecheck, lint,
test:coverage (473/473, same coverage numbers), build, and dev all work
unchanged.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-31 12:15:30 +01:00

74 lines
2.0 KiB
TypeScript

import { app } from 'electron'
import { DatabaseSync } from 'node:sqlite'
import { join } from 'path'
const MAX_MEMORIES = 150
let db: DatabaseSync | null = null
function getDb(): DatabaseSync {
if (db) return db
const dbPath = join(app.getPath('userData'), 'memory.sqlite')
db = new DatabaseSync(dbPath)
db.exec(`
CREATE TABLE IF NOT EXISTS memories (
id INTEGER PRIMARY KEY AUTOINCREMENT,
text TEXT NOT NULL,
created_at TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS mood (
id INTEGER PRIMARY KEY CHECK (id = 1),
value INTEGER NOT NULL
);
INSERT OR IGNORE INTO mood (id, value) VALUES (1, 0);
`)
return db
}
export function addMemory(text: string): void {
const database = getDb()
database
.prepare('INSERT INTO memories (text, created_at) VALUES (?, ?)')
.run(text, new Date().toISOString())
// Ne garde que les MAX_MEMORIES souvenirs les plus recents.
database
.prepare(
`DELETE FROM memories WHERE id NOT IN (
SELECT id FROM memories ORDER BY id DESC LIMIT ?
)`
)
.run(MAX_MEMORIES)
}
/** Supprime les souvenirs dont le texte contient la recherche. Renvoie true si au moins un a ete retire. */
export function forgetMemory(query: string): boolean {
const result = getDb().prepare('DELETE FROM memories WHERE text LIKE ?').run(`%${query}%`)
return result.changes > 0
}
export function clearAllMemories(): void {
getDb().exec('DELETE FROM memories')
}
export function getMemoriesText(): string {
const rows = getDb().prepare('SELECT text FROM memories ORDER BY id ASC').all() as {
text: string
}[]
if (rows.length === 0) return ''
return rows.map((row) => `- ${row.text}`).join('\n')
}
export function getMood(): number {
const row = getDb().prepare('SELECT value FROM mood WHERE id = 1').get() as
{ value: number } | undefined
return row?.value ?? 0
}
export function adjustMood(delta: number): number {
const next = Math.max(-3, Math.min(3, getMood() + delta))
getDb().prepare('UPDATE mood SET value = ? WHERE id = 1').run(next)
return next
}