Subagent-Hierarchie:
- Agent Interface erweitert: parentAgentId, depth, model
- claude-bridge.js: Erkennt Task-Tool als Subagent-Start
- events.ts: Listener für subagent-started/stopped
- AgentView.svelte: Baumansicht mit Einrückung + Collapse
ROADMAP erweitert (Phase 5-16):
- Phase 5: Subagent-Hierarchie ✅
- Phase 6-9: Session, UI, Claude-DB, Context
- Phase 10: Sprach-Interface
- Phase 11: Multi-Agent-Modi (Solo/Handlanger/Experten)
- Phase 12: Hook-System
- Phase 13: VSCodium Integration
- Phase 14: Programm-Steuerung (Playwright, D-Bus)
- Phase 15: Schulungsmodus (Mermaid, animierter Code)
- Phase 16: System-Monitor (Debug-Panel)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
312 lines
9.2 KiB
JavaScript
312 lines
9.2 KiB
JavaScript
#!/usr/bin/env node
|
|
// Claude Desktop — Bridge via Claude Agent SDK
|
|
//
|
|
// Nutzt @anthropic-ai/claude-agent-sdk (query-Funktion)
|
|
// OAuth-Auth funktioniert automatisch (Claude Max Abo)
|
|
// Kein CLI-Spawn, kein Overhead — direkte SDK-Aufrufe
|
|
|
|
import { query } from '@anthropic-ai/claude-agent-sdk';
|
|
import { createInterface } from 'node:readline';
|
|
import { randomUUID } from 'node:crypto';
|
|
|
|
// Prozess am Leben halten
|
|
const keepAlive = setInterval(() => {}, 60000);
|
|
process.stdin.resume();
|
|
|
|
// ============ State ============
|
|
|
|
let activeAbort = null;
|
|
let currentAgentId = null;
|
|
let currentModel = process.env.CLAUDE_MODEL || 'opus';
|
|
|
|
// Subagent-Tracking
|
|
// Map: toolUseId → { agentId, parentId, type, task, depth }
|
|
const activeSubagents = new Map();
|
|
|
|
// Verfügbare Modelle
|
|
const AVAILABLE_MODELS = [
|
|
{ id: 'haiku', name: 'Claude Haiku', description: 'Schnell & günstig' },
|
|
{ id: 'sonnet', name: 'Claude Sonnet', description: 'Ausgewogen' },
|
|
{ id: 'opus', name: 'Claude Opus', description: 'Leistungsstark' },
|
|
];
|
|
|
|
// Tools die Subagents spawnen
|
|
const SUBAGENT_TOOLS = ['Task', 'Agent', 'spawn_agent', 'launch_agent'];
|
|
|
|
// Subagent-Typ aus Tool-Input ermitteln
|
|
function getSubagentType(toolName, input) {
|
|
if (input?.subagent_type) return input.subagent_type.toLowerCase();
|
|
if (input?.agent_type) return input.agent_type.toLowerCase();
|
|
|
|
// Fallback basierend auf description/prompt
|
|
const desc = (input?.description || input?.prompt || '').toLowerCase();
|
|
if (desc.includes('explore') || desc.includes('search') || desc.includes('find')) return 'explore';
|
|
if (desc.includes('plan') || desc.includes('design')) return 'plan';
|
|
if (desc.includes('bash') || desc.includes('command') || desc.includes('terminal')) return 'bash';
|
|
if (desc.includes('code') || desc.includes('implement') || desc.includes('write')) return 'code';
|
|
if (desc.includes('test') || desc.includes('verify')) return 'test';
|
|
if (desc.includes('review') || desc.includes('check')) return 'review';
|
|
|
|
return 'explore'; // Default
|
|
}
|
|
|
|
// ============ Kommunikation mit Tauri ============
|
|
|
|
function sendToTauri(msg) {
|
|
process.stdout.write(JSON.stringify(msg) + '\n');
|
|
}
|
|
|
|
function sendEvent(event, payload = {}) {
|
|
sendToTauri({ type: 'event', event, payload });
|
|
}
|
|
|
|
function sendResponse(id, result) {
|
|
sendToTauri({ type: 'response', id, result });
|
|
}
|
|
|
|
function sendError(id, error) {
|
|
sendToTauri({ type: 'response', id, error });
|
|
}
|
|
|
|
// ============ Claude Agent SDK ============
|
|
|
|
async function sendMessage(message, requestId, model = null) {
|
|
// Modell für diese Anfrage (Parameter > State > Default)
|
|
const useModel = model || currentModel;
|
|
|
|
currentAgentId = randomUUID();
|
|
activeAbort = new AbortController();
|
|
|
|
sendEvent('agent-started', {
|
|
id: currentAgentId,
|
|
type: 'Main',
|
|
task: message.substring(0, 100),
|
|
model: useModel,
|
|
});
|
|
|
|
sendResponse(requestId, { agentId: currentAgentId, status: 'gestartet', model: useModel });
|
|
|
|
const startTime = Date.now();
|
|
let fullText = '';
|
|
let usedModel = useModel;
|
|
|
|
try {
|
|
const conversation = query({
|
|
prompt: message,
|
|
options: {
|
|
model: useModel,
|
|
maxTurns: 25,
|
|
abortController: activeAbort,
|
|
},
|
|
});
|
|
|
|
for await (const event of conversation) {
|
|
switch (event.type) {
|
|
case 'assistant':
|
|
// Text aus der Nachricht extrahieren
|
|
if (event.message?.content) {
|
|
for (const block of event.message.content) {
|
|
if (block.type === 'text' && block.text) {
|
|
fullText += block.text;
|
|
sendEvent('text', { text: block.text });
|
|
}
|
|
}
|
|
}
|
|
if (event.message?.model) {
|
|
usedModel = event.message.model;
|
|
}
|
|
break;
|
|
|
|
case 'tool_use': {
|
|
const toolId = event.tool_use_id || randomUUID();
|
|
const toolName = event.name || 'unknown';
|
|
const toolInput = event.input || {};
|
|
|
|
// Prüfen ob dieses Tool einen Subagent startet
|
|
if (SUBAGENT_TOOLS.includes(toolName)) {
|
|
const subagentId = randomUUID();
|
|
const subagentType = getSubagentType(toolName, toolInput);
|
|
const subagentTask = toolInput.description || toolInput.prompt || toolInput.task || 'Subagent-Aufgabe';
|
|
const subagentModel = toolInput.model || useModel;
|
|
|
|
// Tiefe berechnen (Main = 0, erster Sub = 1, etc.)
|
|
// Für jetzt: immer depth 1 (direkter Subagent vom Main)
|
|
const depth = 1;
|
|
|
|
activeSubagents.set(toolId, {
|
|
agentId: subagentId,
|
|
parentId: currentAgentId,
|
|
type: subagentType,
|
|
task: subagentTask,
|
|
depth,
|
|
model: subagentModel,
|
|
});
|
|
|
|
sendEvent('subagent-started', {
|
|
id: subagentId,
|
|
parentAgentId: currentAgentId,
|
|
type: subagentType,
|
|
task: subagentTask.substring(0, 100),
|
|
depth,
|
|
model: subagentModel,
|
|
toolUseId: toolId,
|
|
});
|
|
}
|
|
|
|
sendEvent('tool-start', {
|
|
id: toolId,
|
|
tool: toolName,
|
|
input: toolInput,
|
|
agentId: currentAgentId,
|
|
});
|
|
break;
|
|
}
|
|
|
|
case 'tool_result': {
|
|
const toolId = event.tool_use_id || '';
|
|
|
|
// Prüfen ob dieser Tool-Call ein Subagent war
|
|
if (activeSubagents.has(toolId)) {
|
|
const subagent = activeSubagents.get(toolId);
|
|
sendEvent('subagent-stopped', {
|
|
id: subagent.agentId,
|
|
parentAgentId: subagent.parentId,
|
|
success: !event.is_error,
|
|
toolUseId: toolId,
|
|
});
|
|
activeSubagents.delete(toolId);
|
|
}
|
|
|
|
sendEvent('tool-end', {
|
|
id: toolId,
|
|
success: !event.is_error,
|
|
agentId: currentAgentId,
|
|
});
|
|
break;
|
|
}
|
|
|
|
case 'result':
|
|
// Endergebnis
|
|
sendEvent('result', {
|
|
text: fullText,
|
|
cost: event.total_cost_usd || 0,
|
|
tokens: {
|
|
input: event.usage?.input_tokens || 0,
|
|
output: event.usage?.output_tokens || 0,
|
|
},
|
|
session_id: event.session_id || '',
|
|
duration_ms: Date.now() - startTime,
|
|
model: usedModel,
|
|
});
|
|
break;
|
|
|
|
default:
|
|
// Andere Events still ignorieren
|
|
break;
|
|
}
|
|
}
|
|
} catch (err) {
|
|
if (err.name === 'AbortError') {
|
|
// Abgebrochen — kein Fehler
|
|
} else {
|
|
sendEvent('text', { text: `\n\n**Fehler:** ${err.message || err}` });
|
|
}
|
|
} finally {
|
|
// Alle noch aktiven Subagents stoppen
|
|
for (const [toolId, subagent] of activeSubagents) {
|
|
sendEvent('subagent-stopped', {
|
|
id: subagent.agentId,
|
|
parentAgentId: subagent.parentId,
|
|
success: false, // Vorzeitig beendet
|
|
toolUseId: toolId,
|
|
});
|
|
}
|
|
activeSubagents.clear();
|
|
|
|
sendEvent('agent-stopped', { id: currentAgentId, code: 0 });
|
|
sendEvent('all-stopped');
|
|
currentAgentId = null;
|
|
activeAbort = null;
|
|
}
|
|
}
|
|
|
|
// ============ Befehle von Tauri ============
|
|
|
|
function handleCommand(msg) {
|
|
switch (msg.command) {
|
|
case 'message':
|
|
if (!msg.message) {
|
|
sendError(msg.id, 'Keine Nachricht angegeben');
|
|
return;
|
|
}
|
|
// Modell kann pro Anfrage überschrieben werden
|
|
sendMessage(msg.message, msg.id, msg.model);
|
|
break;
|
|
|
|
case 'stop':
|
|
if (activeAbort) {
|
|
activeAbort.abort();
|
|
}
|
|
sendResponse(msg.id, { status: 'gestoppt' });
|
|
break;
|
|
|
|
case 'set-model':
|
|
if (!msg.model) {
|
|
sendError(msg.id, 'Kein Modell angegeben');
|
|
return;
|
|
}
|
|
const validModels = AVAILABLE_MODELS.map(m => m.id);
|
|
if (!validModels.includes(msg.model)) {
|
|
sendError(msg.id, `Ungültiges Modell: ${msg.model}. Verfügbar: ${validModels.join(', ')}`);
|
|
return;
|
|
}
|
|
currentModel = msg.model;
|
|
sendResponse(msg.id, { model: currentModel, status: 'Modell geändert' });
|
|
sendEvent('model-changed', { model: currentModel });
|
|
break;
|
|
|
|
case 'get-models':
|
|
sendResponse(msg.id, {
|
|
current: currentModel,
|
|
available: AVAILABLE_MODELS,
|
|
});
|
|
break;
|
|
|
|
case 'status':
|
|
sendResponse(msg.id, {
|
|
model: currentModel,
|
|
isProcessing: !!currentAgentId,
|
|
availableModels: AVAILABLE_MODELS,
|
|
});
|
|
break;
|
|
|
|
case 'ping':
|
|
sendResponse(msg.id, { pong: true });
|
|
break;
|
|
|
|
default:
|
|
sendError(msg.id, `Unbekannter Befehl: ${msg.command}`);
|
|
}
|
|
}
|
|
|
|
// ============ Main ============
|
|
|
|
const rl = createInterface({ input: process.stdin });
|
|
rl.on('line', (line) => {
|
|
if (!line.trim()) return;
|
|
try {
|
|
handleCommand(JSON.parse(line));
|
|
} catch (err) {
|
|
process.stderr.write(`Ungültige Eingabe: ${err.message}\n`);
|
|
}
|
|
});
|
|
|
|
rl.on('close', () => {
|
|
process.stderr.write('stdin geschlossen\n');
|
|
});
|
|
|
|
process.on('SIGTERM', () => { clearInterval(keepAlive); process.exit(0); });
|
|
process.on('SIGINT', () => { clearInterval(keepAlive); process.exit(0); });
|
|
|
|
// Bereit
|
|
sendEvent('ready', { version: '1.1.0', pid: process.pid, model: currentModel, availableModels: AVAILABLE_MODELS });
|