Version: 3.0 (Tool-Based Architecture)
Datum: 31. Januar 2026
Status: ✅ PRODUCTION READY
Das agent/ Modul implementiert ein Tool-Based AI System mit OpenAI Function Calling. Das alte 3-Phasen System (Intent Detection → Content Proposal → Structure Generation) wurde vollständig ersetzt.
Architektur:
src/lib/agent/
├── index.ts ← Zentrale Exports (Public API)
├── types.ts ← TypeScript Interfaces (Tool-Types)
├── llmRequest.ts ← LLM API Wrapper (OpenAI/Ollama)
└── tools/ ← Tool-Based AI System
├── index.ts ← Tool Exports
├── toolDefinitions.ts ← OpenAI Function Calling Schemas
├── toolExecutor.ts ← Tool Dispatcher & Execution
└── toolSystemPrompt.ts ← Kontextbewusster System Prompt
Statt manuell LLM-Responses zu parsen, verwendet das System OpenAI Function Calling:
┌─────────────────────────────────────────────────────────────────┐
│ User: "Erstelle eine Karte zu Fake News" │
└─────────────────────────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────────────────────┐
│ LLM + Tools: Wählt automatisch `add_card` Tool │
│ Response: { tool_calls: [{ name: "add_card", args: {...} }] } │
└─────────────────────────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────────────────────┐
│ Tool Executor: Führt `add_card` mit BoardStore aus │
│ → Karte wird erstellt, User erhält Feedback │
└─────────────────────────────────────────────────────────────────┘
Vorteile:
- ✅ Keine manuelle JSON-Validierung nötig
- ✅ LLM wählt automatisch das richtige Tool
- ✅ Robuster als Text-Parsing
- ✅ Erweiterbar (neue Tools = neue Funktionen)
| Tool | Beschreibung |
|---|---|
populate_board |
Befüllt Board komplett mit Spalten & Karten |
update_board |
Aktualisiert Board-Metadaten (Beschreibung, Tags) |
| Tool | Beschreibung |
|---|---|
add_column |
Fügt neue Spalte hinzu |
update_column |
Benennt Spalte um |
delete_column |
Löscht Spalte inkl. Karten |
| Tool | Beschreibung |
|---|---|
add_card |
Erstellt einzelne Karte in Spalte |
update_card |
Aktualisiert Karten-Inhalt, Labels, Links, Bild |
move_card |
Verschiebt Karte zwischen Spalten |
delete_card |
Löscht Karte |
| Tool | Beschreibung |
|---|---|
add_comment |
Fügt Kommentar zu Karte hinzu |
| Tool | Beschreibung |
|---|---|
respond |
Antwortet ohne Board-Aktion (Fragen, Erklärungen) |
ask_clarification |
Fragt nach mehr Details |
// Zentrale Imports
import { llmRequest } from '$lib/agent';
import type { ToolDefinition, ToolCall, ToolResult } from '$lib/agent';
// Tool-spezifische Imports
import {
toolDefinitions,
getToolDefinitions,
buildToolSystemPrompt,
executeToolCall,
executeToolCalls
} from '$lib/agent/tools';// OpenAI Function Calling Format
interface ToolDefinition {
type: 'function';
function: {
name: string;
description: string;
parameters: { type: 'object'; properties: Record<string, any>; required: string[] };
};
}
// Tool-Call aus LLM Response
interface ToolCall {
id: string;
type: 'function';
function: { name: string; arguments: string }; // JSON string
}
// Ergebnis einer Tool-Ausführung
interface ToolResult {
tool_call_id: string;
tool_name: string;
success: boolean;
result?: any;
error?: string;
}import { buildToolSystemPrompt } from '$lib/agent/tools';
const systemPrompt = buildToolSystemPrompt(
{
name: boardStore.board.name,
description: boardStore.board.description,
columns: boardStore.uiData
},
selectedCard // Optional: aktuell selektierte Karte
);import { getToolDefinitions } from '$lib/agent/tools';
const response = await fetch(endpoint, {
method: 'POST',
body: JSON.stringify({
model: 'gpt-4',
messages: [
{ role: 'system', content: systemPrompt },
{ role: 'user', content: userMessage }
],
tools: getToolDefinitions(), // ← Alle Tool-Schemas
tool_choice: 'auto'
})
});import { executeToolCalls } from '$lib/agent/tools';
const toolCalls = response.choices[0].message.tool_calls;
if (toolCalls) {
const results = await executeToolCalls(toolCalls, {
boardOperations, // BoardOperations instance
nostrIntegration, // NostrIntegration instance
board: boardStore.board
});
// Ergebnisse verarbeiten
for (const result of results) {
if (result.success) {
console.log(`✅ ${result.tool_name}: ${result.result}`);
} else {
console.error(`❌ ${result.tool_name}: ${result.error}`);
}
}
}llmRequest.ts bietet einen generischen LLM-Wrapper:
import { llmRequest } from '$lib/agent';
// Text-Response
const text = await llmRequest({
systemPrompt: 'Du bist ein hilfreicher Assistent.',
userMessage: 'Was ist 2+2?',
returnType: 'text'
});
// JSON-Response (mit Auto-Extraktion)
const json = await llmRequest<{ answer: number }>({
systemPrompt: 'Antworte nur mit JSON.',
userMessage: 'Was ist 2+2?',
returnType: 'json'
});
// → Unterstützt: OpenAI, OpenRouter, Ollama (auto-detect)Features:
- ✅ Auto-Detection: OpenAI vs Ollama Endpoint
- ✅ JSON Auto-Extraktion (aus Markdown, Text)
- ✅ Reasoning-Model Support (DeepSeek, o1)
- ✅ Konfiguration via
settingsStore
Der toolExecutor.ts enthält:
LLMs (besonders Ollama) erzeugen manchmal fehlerhaftes JSON. Der Executor repariert:
- Echte Newlines in Strings →
\n - Doppelt escapte Characters →
\\n→\n - Fehlende schließende Klammern
- Extra Text nach JSON
interface ExecutionContext {
boardOperations: BoardOperations;
nostrIntegration: NostrIntegration;
board: Board;
}Jedes Tool wird an die entsprechende BoardOperations Methode dispatcht:
// add_card → BoardOperations.createCard()
// update_card → BoardOperations.editCard()
// move_card → BoardOperations.moveCard()
// etc.Die LLM-Einstellungen kommen aus settingsStore:
| Setting | Beschreibung |
|---|---|
llmBaseUrl |
API-Endpoint (OpenAI, OpenRouter, Ollama) |
llmApiKey |
API-Key (nicht nötig für Ollama) |
llmModel |
Model-Name (z.B. gpt-4, llama3) |
Das alte 3-Phasen System wurde archiviert:
archive/MIGRATION-AIACTIONGENERATOR.md- Historische Migrationarchive/TWO-PHASE-AI-RESPONSE.md- Altes 2-Phasen System
Diese Dateien sind nicht mehr relevant für die aktuelle Implementierung.
{
type: 'function',
function: {
name: 'my_new_tool',
description: 'Beschreibung für das LLM',
parameters: {
type: 'object',
properties: {
param1: { type: 'string', description: '...' }
},
required: ['param1']
}
}
}case 'my_new_tool': {
const { param1 } = args;
// Implementierung...
return { success: true, result: 'Ergebnis' };
}Aktuell (v3.0): ✅ Tool-Based AI System
- OpenAI Function Calling
- Board/Column/Card Tools
- JSON Repair für LLM-Fehler
- Multi-Provider Support (OpenAI, Ollama)
Geplant (feature/ai-mcp Branch):
- OER-Suche Tools (
search_oer,add_cards_from_oer) - URL-Import Tool (
import_from_url) - MCP Server Integration
Zuletzt aktualisiert: 31. Januar 2026
Status: ✅ Production Ready