refactor(memory): run agent recall, memory tools and the server on the memory contract (P1.5)

- Orchestrator, memory tools, context loader and pattern write-back use a
  MemoryStore and a workspace scope instead of MindDB layers; the knowledge
  graph tools, cognify, memory linker and feedback handler are gone (graph
  hidden in v1, ADR-0002).
- The server opens one mem0 store in <dataDir>/memory/, embedded by the
  in-process model on first use, and extracts facts about the user in the
  background with the model the user chose (memoryExtraction setting).
- Legacy .mind memories are copied into the store once, in the background;
  the legacy files are only read.
- Weaver consolidation, harvest, wiki, memory import, memory lanes, vector
  backfill and index reconciliation are removed; agent runs, loops, sub-agent
  results, connector fetch and the failed-turn capture write through the
  contract. Workspace sessions no longer hold a mind handle.
- FileIndexer and the install audit move to waggle.db; recall date helpers and
  the reranker are copied into core.
- The CLI REPL opens the same memory store.
This commit is contained in:
shumeiko.it
2026-10-24 11:56:26 +02:00
parent f3eb6b6652
commit 784ca770b1
86 changed files with 2520 additions and 8444 deletions

View File

@@ -1,7 +1,7 @@
/**
* Interactive REPL for the Waggle CLI.
*
* Loads WaggleConfig, MindDB, Orchestrator, ModelRouter.
* Loads WaggleConfig, memory, Orchestrator, ModelRouter.
* Uses runAgentLoop() via LiteLLM for all chat interactions.
* Supports slash commands and multi-model chat with tool use.
*/
@@ -10,7 +10,9 @@ import readline from 'node:readline';
import os from 'node:os';
import path from 'node:path';
import chalk from 'chalk';
import { MindDB, WaggleConfig, WaggleDB, createLiteLLMEmbedder } from '@waggle/core';
import { WaggleConfig, WaggleDB, createMemoryEmbedder } from '@waggle/core';
import { PERSONAL_SCOPE } from '@waggle/memory-contract';
import { createMem0MemoryStore } from '@waggle/memory-mem0';
import { Orchestrator, ModelRouter, runAgentLoop, createSystemTools, createPlanTools, createGitTools, Workspace, ensureIdentity, loadSystemPromptWithOverrides, loadSkills, CostTracker, HookRegistry, loadHooksFromConfig, needsConfirmation } from '@waggle/agent';
import { parseCommand, COMMANDS } from './commands.js';
import { renderMarkdown } from './renderer.js';
@@ -18,21 +20,6 @@ import { AdminClient, formatTable } from './commands/admin.js';
import { AuthManager } from './auth.js';
import { detectMode, checkServerHealth } from './mode-detector.js';
/**
* Build an embedder backed by LiteLLM's /embeddings endpoint.
* Falls back to a deterministic mock (text→Float32Array hash) if the API is unavailable,
* so the CLI always works even without a running LiteLLM proxy.
*/
function buildEmbedder(litellmUrl: string, litellmApiKey: string) {
return createLiteLLMEmbedder({
litellmUrl,
litellmApiKey,
model: 'text-embedding',
dimensions: 1024,
fallbackToMock: true,
});
}
export interface ReplOptions {
model?: string;
local?: boolean;
@@ -42,10 +29,6 @@ export interface ReplOptions {
export async function startRepl(options: ReplOptions = {}): Promise<void> {
// Load config
const config = new WaggleConfig();
const mindPath = config.getMindPath();
// Open (or create) .mind database
const db = new MindDB(mindPath);
// Build model router from config
const providers = config.getProviders();
@@ -87,7 +70,6 @@ export async function startRepl(options: ReplOptions = {}): Promise<void> {
if (modeResult.type === 'error') {
console.log(chalk.red(modeResult.error ?? 'Mode detection failed.'));
db.close();
process.exit(1);
}
@@ -97,12 +79,15 @@ export async function startRepl(options: ReplOptions = {}): Promise<void> {
// LiteLLM master key for proxy auth (NOT the provider API key — LiteLLM reads that from its own env)
const litellmApiKey = process.env.LITELLM_API_KEY ?? process.env.LITELLM_MASTER_KEY ?? 'sk-waggle-dev';
// Build embedder backed by LiteLLM (falls back to mock if API unavailable)
const embedder = buildEmbedder(litellmUrl, litellmApiKey);
// Memory: the same store the desktop app uses (ADR-0007), in ~/.waggle/memory.
const memory = await createMem0MemoryStore({
directory: path.join(waggleHome, 'memory'),
embedder: createMemoryEmbedder({ cacheDir: path.join(waggleHome, 'models') }),
});
// Build orchestrator
const waggleDb = WaggleDB.openInDataDir(waggleHome);
const orchestrator = new Orchestrator({ db, waggleDb, embedder });
const orchestrator = new Orchestrator({ memory, waggleDb });
// Ensure identity exists (first-run wizard)
ensureIdentity(orchestrator.getIdentity());
@@ -120,8 +105,7 @@ export async function startRepl(options: ReplOptions = {}): Promise<void> {
const sessionId = workspace.startSession();
// Stats for welcome banner
const frameCount = (db.getDatabase().prepare('SELECT COUNT(*) as cnt FROM memory_frames').get() as { cnt: number }).cnt;
const sessionCount = (db.getDatabase().prepare('SELECT COUNT(*) as cnt FROM sessions').get() as { cnt: number }).cnt;
const frameCount = await memory.count({ scopes: [PERSONAL_SCOPE] });
// Print welcome banner
console.log('');
@@ -129,9 +113,8 @@ export async function startRepl(options: ReplOptions = {}): Promise<void> {
console.log(chalk.dim(' ─────────────────────────────────────'));
console.log(chalk.dim(' Mode: ') + chalk.cyan(modeResult.type));
console.log(chalk.dim(' Model: ') + chalk.cyan(currentModel));
console.log(chalk.dim(' Memory: ') + chalk.cyan(`${frameCount} frames`));
console.log(chalk.dim(' Sessions: ') + chalk.cyan(`${sessionCount}`));
console.log(chalk.dim(' Mind: ') + chalk.cyan(mindPath));
console.log(chalk.dim(' Memory: ') + chalk.cyan(`${frameCount} memories`));
console.log(chalk.dim(' Store: ') + chalk.cyan(path.join(waggleHome, 'memory')));
console.log(chalk.dim(' Workspace:') + chalk.cyan(` ${process.cwd()}`));
if (modeResult.warning) {
console.log(chalk.yellow(' ⚠ ' + modeResult.warning));
@@ -381,11 +364,11 @@ export async function startRepl(options: ReplOptions = {}): Promise<void> {
systemPrompt += userSystemPrompt + '\n\n';
}
systemPrompt += orchestrator.buildSystemPrompt() + `
systemPrompt += await orchestrator.buildSystemPrompt() + `
# Who You Are
You are Waggle — an AI assistant with persistent memory and web access.
Your key strength: you remember past conversations through your .mind memory system.
Your key strength: you remember past conversations through your memory.
# CRITICAL RULES — FOLLOW THESE EXACTLY
@@ -482,7 +465,7 @@ When asked about current events, products, releases, docs, or anything you're no
});
rl.on('close', () => {
db.close();
void memory.close();
waggleDb.close();
process.exit(0);
});
@@ -491,7 +474,7 @@ When asked about current events, products, releases, docs, or anything you're no
process.on('SIGINT', () => {
console.log('');
console.log(chalk.dim('Goodbye!'));
db.close();
void memory.close();
waggleDb.close();
process.exit(0);
});