mirror of
https://github.com/Jeuners/agenttwo-tools.git
synced 2026-09-11 07:52:33 +02:00
feat: Modellauswahl, globales Gedächtnis und Session-Selbstheilung
- lokales Ollama-Modell frei wählbar (Dropdown aus /api/ollama/models, /api/model akzeptiert ?name=, Chat-Option model mit Regex-Validierung) - Gedächtnis jetzt global über alle Chats: Ankerpunkte werden sessionübergreifend eingespielt (eigene Session bevorzugt), recall und Gedächtnis-Panel sehen alle Anker - Frontend heilt tote Session-IDs: unbekannte ID verfällt beim Laden, Client springt auf die vom Server genutzte Session — Nachrichten landen nicht mehr in schleichend neuen Chats - Origin-Allowlist um Port 5173 ergänzt (alter Dev-Server blockierte sonst alles mit 403) - Rollen-Label und Composer-Platzhalter zeigen das echte Modell statt hartkodiertem qwen3
This commit is contained in:
parent
9bce768f95
commit
c8b74892ef
12 changed files with 212 additions and 95 deletions
|
|
@ -92,21 +92,23 @@ app.get("/api/health", async () => ({ ok: true }));
|
|||
|
||||
app.get("/api/tools", async () => ({ ok: true, tools: toolNames() }));
|
||||
|
||||
app.get("/api/model", async () => {
|
||||
app.get("/api/model", async (req, reply) => {
|
||||
const q = (req.query as { name?: string }).name;
|
||||
const name = q && /^[\w.:-]{2,80}$/.test(q) ? q : MODEL;
|
||||
try {
|
||||
const res = await fetch(`${OLLAMA_URL}/api/show`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ name: MODEL }),
|
||||
body: JSON.stringify({ name }),
|
||||
});
|
||||
if (!res.ok) return { ok: false, error: `Ollama HTTP ${res.status}` };
|
||||
if (!res.ok) return reply.code(404).send({ ok: false, error: `Ollama HTTP ${res.status}` });
|
||||
const data = (await res.json()) as {
|
||||
details?: { parameter_size?: string; quantization_level?: string };
|
||||
capabilities?: string[];
|
||||
};
|
||||
return {
|
||||
ok: true,
|
||||
model: MODEL,
|
||||
model: name,
|
||||
parameterSize: data.details?.parameter_size,
|
||||
quantization: data.details?.quantization_level,
|
||||
capabilities: data.capabilities ?? [],
|
||||
|
|
@ -139,7 +141,7 @@ app.delete("/api/sessions/:id", async (req) => {
|
|||
app.get("/api/sessions/:id/memory", async (req, reply) => {
|
||||
const { id } = req.params as { id: string };
|
||||
if (!dbmod.getSession(id)) return reply.code(404).send({ error: "not found" });
|
||||
return { state: mem.getMemoryState(id), anchors: mem.listAnchors(id) };
|
||||
return { state: mem.getMemoryState(id), anchors: mem.listAnchorsAll() };
|
||||
});
|
||||
|
||||
app.post("/api/sessions/:id/dream", async (req, reply) => {
|
||||
|
|
@ -235,6 +237,29 @@ app.get("/api/openrouter/models", async () => {
|
|||
}
|
||||
});
|
||||
|
||||
// --- Ollama models ---
|
||||
app.get("/api/ollama/models", async () => {
|
||||
try {
|
||||
const res = await fetch(`${OLLAMA_URL}/api/tags`);
|
||||
if (!res.ok) return { ok: false, error: `Ollama HTTP ${res.status}` };
|
||||
const data = (await res.json()) as {
|
||||
models?: { name: string; size?: number; details?: { parameter_size?: string; quantization_level?: string } }[];
|
||||
};
|
||||
return {
|
||||
ok: true,
|
||||
models: (data.models ?? []).map((m) => ({
|
||||
name: m.name,
|
||||
sizeGB: m.size ? Math.round(m.size / 1e8) / 10 : undefined,
|
||||
parameterSize: m.details?.parameter_size,
|
||||
quantization: m.details?.quantization_level,
|
||||
})),
|
||||
};
|
||||
} catch (err) {
|
||||
app.log.error({ err }, "Ollama-Modellliste fehlgeschlagen");
|
||||
return { ok: false, error: "Ollama nicht erreichbar" };
|
||||
}
|
||||
});
|
||||
|
||||
// --- WebSocket ---
|
||||
interface ChatOptionsPayload {
|
||||
think: boolean;
|
||||
|
|
@ -243,6 +268,7 @@ interface ChatOptionsPayload {
|
|||
provider?: string;
|
||||
openrouterModel?: string;
|
||||
tools?: boolean;
|
||||
model?: string;
|
||||
memorySteps?: number;
|
||||
memoryAnchors?: boolean;
|
||||
dreamAuto?: boolean;
|
||||
|
|
@ -257,7 +283,10 @@ function parseOptions(raw: unknown): OllamaOptions & {
|
|||
} {
|
||||
const o = (raw ?? {}) as Partial<ChatOptionsPayload>;
|
||||
return {
|
||||
model: MODEL,
|
||||
model:
|
||||
typeof o.model === "string" && /^[\w.:-]{2,80}$/.test(o.model)
|
||||
? o.model
|
||||
: MODEL,
|
||||
think: o.think !== false,
|
||||
tools: o.tools !== false,
|
||||
temperature: clamp(Number(o.temperature ?? 0.7), 0, 2),
|
||||
|
|
|
|||
|
|
@ -139,6 +139,15 @@ export function listAnchors(sessionId: string): AnchorRow[] {
|
|||
.all(sessionId) as unknown as AnchorRow[];
|
||||
}
|
||||
|
||||
/** Ankerpunkte über alle Sessions — das Gedächtnis ist bewusst global. */
|
||||
export function listAnchorsAll(): AnchorRow[] {
|
||||
return db
|
||||
.prepare(
|
||||
"SELECT * FROM anchors ORDER BY pinned DESC, importance DESC, updated_at DESC LIMIT 100",
|
||||
)
|
||||
.all() as unknown as AnchorRow[];
|
||||
}
|
||||
|
||||
function countAnchors(sessionId: string): number {
|
||||
return Number(
|
||||
(
|
||||
|
|
@ -354,25 +363,27 @@ export function anchorContextBlock(
|
|||
sessionId: string,
|
||||
maxChars = 1200,
|
||||
): string | null {
|
||||
const anchors = listAnchors(sessionId).slice(0, 20);
|
||||
const all = [...listAnchorsAll()];
|
||||
const score = (a: AnchorRow) =>
|
||||
(a.pinned ? 2 : 0) + a.importance + (a.session_id === sessionId ? 0.5 : 0);
|
||||
all.sort((x, y) => score(y) - score(x));
|
||||
const lines: string[] = [];
|
||||
let chars = 0;
|
||||
for (const a of anchors) {
|
||||
for (const a of all) {
|
||||
const line = `- [${KIND_LABEL[a.kind] ?? a.kind}] ${a.text}`;
|
||||
if (chars + line.length > maxChars) break;
|
||||
lines.push(line);
|
||||
chars += line.length;
|
||||
}
|
||||
if (lines.length === 0) return null;
|
||||
return `Bekannte Ankerpunkte aus diesem und früheren Gesprächen (Gedächtnis — als Kontext nutzen, nicht wörtlich zitieren):\n${lines.join("\n")}`;
|
||||
return `Bekannte Ankerpunkte über den Nutzer aus früheren und aktuellen Gesprächen (Gedächtnis — als verlässliches Wissen behandeln, nicht wörtlich zitieren):\n${lines.join("\n")}`;
|
||||
}
|
||||
|
||||
export function queryAnchors(
|
||||
sessionId: string,
|
||||
query: string,
|
||||
limit = 12,
|
||||
): AnchorRow[] {
|
||||
const anchors = listAnchors(sessionId);
|
||||
const anchors = listAnchorsAll();
|
||||
const q = query.trim();
|
||||
if (!q) return anchors.slice(0, limit);
|
||||
return anchors
|
||||
|
|
|
|||
|
|
@ -8,6 +8,8 @@
|
|||
*/
|
||||
|
||||
const DEFAULT_ORIGINS = [
|
||||
"http://localhost:5173",
|
||||
"http://127.0.0.1:5173",
|
||||
"http://localhost:5174",
|
||||
"http://127.0.0.1:5174",
|
||||
"http://localhost:8788",
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ export const recallTool: Tool = {
|
|||
},
|
||||
async run(args, ctx) {
|
||||
if (!ctx.sessionId) throw new ToolError("Keine Sitzung für das Gedächtnis bekannt");
|
||||
const anchors = queryAnchors(ctx.sessionId, String(args.query ?? ""));
|
||||
const anchors = queryAnchors(String(args.query ?? ""));
|
||||
return {
|
||||
count: anchors.length,
|
||||
anchors: anchors.map((a) => ({
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue