The logger, injection scanner, ingress guard, WorkspaceManager, embedders, recall helpers and execution traces came from @waggle/hive-mind-core (Apache-2.0); awareness, identity and parts of WaggleDB were adapted from it. They keep Apache-2.0: an SPDX header with the original copyright line and the source file, packages/core/LICENSE.md listing them, LICENSE-APACHE with the full text, and "MIT AND Apache-2.0" in package.json. A test keeps the list and the headers in step. No code changes. Logs UF-069: the hive-mind-core LICENSE is an abridged Apache text.
82 lines
2.6 KiB
TypeScript
82 lines
2.6 KiB
TypeScript
// SPDX-License-Identifier: Apache-2.0
|
|
// Copyright 2026 Marko Marković · Egzakta Group · waggle-os.ai
|
|
// Parts copied from @waggle/hive-mind-core src/mind/db.ts and changed for @waggle/core.
|
|
/**
|
|
* waggle.db — Waggle's own data, kept apart from memory (ADR-0007).
|
|
*
|
|
* Holds what the app needs to run: automations, notifications and held
|
|
* actions, skill hashes, thumbs feedback, cost tracking (execution traces and
|
|
* budget reservations), the user profile card and awareness items. Facts the
|
|
* agent learns about the user live in the memory store, not here.
|
|
*
|
|
* Stores that belong here take a `WaggleDB`, not a generic SQLite handle, so
|
|
* passing a memory database by mistake is a type error.
|
|
*/
|
|
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import Database from 'better-sqlite3';
|
|
import type { Database as DatabaseType } from 'better-sqlite3';
|
|
|
|
export const WAGGLE_DB_FILE = 'waggle.db';
|
|
|
|
const BUSY_TIMEOUT_MS = 10_000;
|
|
|
|
const META_TABLE_SQL = `
|
|
CREATE TABLE IF NOT EXISTS waggle_meta (
|
|
key TEXT PRIMARY KEY,
|
|
value TEXT NOT NULL
|
|
)`;
|
|
|
|
export class WaggleDB {
|
|
private readonly db: DatabaseType;
|
|
|
|
/** Opens (and creates if needed) the database at `dbPath`; `':memory:'` is accepted. */
|
|
constructor(readonly dbPath: string) {
|
|
if (dbPath !== ':memory:') {
|
|
fs.mkdirSync(path.dirname(dbPath), { recursive: true });
|
|
}
|
|
this.db = new Database(dbPath);
|
|
this.db.pragma('journal_mode = WAL');
|
|
this.db.pragma('foreign_keys = ON');
|
|
this.db.pragma(`busy_timeout = ${BUSY_TIMEOUT_MS}`);
|
|
this.db.exec(META_TABLE_SQL);
|
|
}
|
|
|
|
/** Opens `<dataDir>/waggle.db`. */
|
|
static openInDataDir(dataDir: string): WaggleDB {
|
|
return new WaggleDB(path.join(dataDir, WAGGLE_DB_FILE));
|
|
}
|
|
|
|
getDatabase(): DatabaseType {
|
|
return this.db;
|
|
}
|
|
|
|
getMeta(key: string): string | null {
|
|
const row = this.db.prepare('SELECT value FROM waggle_meta WHERE key = ?').get(key) as
|
|
| { value: string }
|
|
| undefined;
|
|
return row?.value ?? null;
|
|
}
|
|
|
|
setMeta(key: string, value: string): void {
|
|
this.db
|
|
.prepare('INSERT INTO waggle_meta (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value')
|
|
.run(key, value);
|
|
}
|
|
|
|
close(): void {
|
|
if (this.db.open) this.db.close();
|
|
}
|
|
}
|
|
|
|
/** Data scope of a profile or awareness item: the personal mind or one workspace. */
|
|
export type DataScope = 'personal' | `workspace:${string}`;
|
|
|
|
export const PERSONAL_DATA_SCOPE: DataScope = 'personal';
|
|
|
|
export function workspaceDataScope(workspaceId: string): DataScope {
|
|
if (!workspaceId.trim()) throw new TypeError('workspaceId must be a non-empty string');
|
|
return `workspace:${workspaceId}`;
|
|
}
|