mirror of
https://github.com/Jeuners/agenttwo-tools.git
synced 2026-09-09 15:02:31 +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) => ({
|
||||
|
|
|
|||
74
web/dist/assets/index-BvIjR1bZ.js
vendored
74
web/dist/assets/index-BvIjR1bZ.js
vendored
File diff suppressed because one or more lines are too long
74
web/dist/assets/index-leRe8gQm.js
vendored
Normal file
74
web/dist/assets/index-leRe8gQm.js
vendored
Normal file
File diff suppressed because one or more lines are too long
2
web/dist/index.html
vendored
2
web/dist/index.html
vendored
|
|
@ -6,7 +6,7 @@
|
|||
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
|
||||
<meta name="theme-color" content="#0a0f0c" />
|
||||
<title>agenttwo-tools — qwen3 mit Vision</title>
|
||||
<script type="module" crossorigin src="/assets/index-BvIjR1bZ.js"></script>
|
||||
<script type="module" crossorigin src="/assets/index-leRe8gQm.js"></script>
|
||||
<link rel="stylesheet" crossorigin href="/assets/index-CKaNlN-w.css">
|
||||
</head>
|
||||
<body>
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import { Sidebar } from "./components/Sidebar";
|
|||
import { ChatMessage } from "./components/ChatMessage";
|
||||
import { Composer } from "./components/Composer";
|
||||
import { MemoryPanel } from "./components/MemoryPanel";
|
||||
import type { OpenRouterModel } from "./types";
|
||||
import type { OpenRouterModel, OllamaModel } from "./types";
|
||||
|
||||
const VOICE_KEY = "oxagenttwo.voiceMode";
|
||||
|
||||
|
|
@ -19,6 +19,7 @@ export default function App() {
|
|||
() => localStorage.getItem(VOICE_KEY) === "1",
|
||||
);
|
||||
const [orModels, setOrModels] = useState<OpenRouterModel[]>([]);
|
||||
const [ollamaModels, setOllamaModels] = useState<OllamaModel[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
if (
|
||||
|
|
@ -33,7 +34,19 @@ export default function App() {
|
|||
})
|
||||
.catch(() => {});
|
||||
}
|
||||
}, [settingsOpen, chat.options.provider, orModels.length]);
|
||||
if (
|
||||
settingsOpen &&
|
||||
chat.options.provider === "ollama" &&
|
||||
ollamaModels.length === 0
|
||||
) {
|
||||
fetch("/api/ollama/models")
|
||||
.then((r) => r.json())
|
||||
.then((d: { ok: boolean; models?: OllamaModel[] }) => {
|
||||
if (d.ok && d.models) setOllamaModels(d.models);
|
||||
})
|
||||
.catch(() => {});
|
||||
}
|
||||
}, [settingsOpen, chat.options.provider, orModels.length, ollamaModels.length]);
|
||||
|
||||
const voiceModeRef = useRef(voiceMode);
|
||||
const streamingRef = useRef(false);
|
||||
|
|
@ -139,6 +152,10 @@ export default function App() {
|
|||
.catch(() => setToolNames([]));
|
||||
}, []);
|
||||
|
||||
const modelLabel = (chat.options.provider === "openrouter"
|
||||
? chat.options.openrouterModel.split("/").pop()
|
||||
: chat.options.model.replace(/:latest$/, "")) ?? "qwen3";
|
||||
|
||||
const handleSend = useCallback(
|
||||
(text: string, images: string[] = []) => {
|
||||
voice.cancelSpeech();
|
||||
|
|
@ -263,6 +280,36 @@ export default function App() {
|
|||
</datalist>
|
||||
</label>
|
||||
)}
|
||||
{chat.options.provider === "ollama" && (
|
||||
<label className="setting-row">
|
||||
<span>Lokales Modell (Ollama)</span>
|
||||
<select
|
||||
className="provider-select"
|
||||
value={
|
||||
ollamaModels.some((m) => m.name === chat.options.model)
|
||||
? chat.options.model
|
||||
: ""
|
||||
}
|
||||
onChange={(e) =>
|
||||
chat.setOptions({ model: e.target.value })
|
||||
}
|
||||
>
|
||||
{!ollamaModels.some((m) => m.name === chat.options.model) && (
|
||||
<option value="">{chat.options.model} (nicht installiert)</option>
|
||||
)}
|
||||
{ollamaModels.map((m) => (
|
||||
<option key={m.name} value={m.name}>
|
||||
{m.name}
|
||||
{m.parameterSize ? ` · ${m.parameterSize}` : ""}
|
||||
{m.sizeGB ? ` · ${m.sizeGB} GB` : ""}
|
||||
</option>
|
||||
))}
|
||||
{ollamaModels.length === 0 && (
|
||||
<option value={chat.options.model}>{chat.options.model}</option>
|
||||
)}
|
||||
</select>
|
||||
</label>
|
||||
)}
|
||||
{chat.options.provider === "ollama" && (
|
||||
<label className="setting-row checkbox">
|
||||
<input
|
||||
|
|
@ -378,7 +425,12 @@ export default function App() {
|
|||
</div>
|
||||
)}
|
||||
{chat.messages.map((m) => (
|
||||
<ChatMessage key={m.id} message={m} toolEvents={chat.toolEvents[m.id]} />
|
||||
<ChatMessage
|
||||
key={m.id}
|
||||
message={m}
|
||||
toolEvents={chat.toolEvents[m.id]}
|
||||
modelLabel={modelLabel}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
|
||||
|
|
@ -395,6 +447,7 @@ export default function App() {
|
|||
recording={voice.recording}
|
||||
transcribing={voice.transcribing}
|
||||
injectedText={injectedText}
|
||||
modelLabel={modelLabel}
|
||||
onInjected={() => setInjectedText(null)}
|
||||
onSend={handleSend}
|
||||
onAbort={handleAbort}
|
||||
|
|
|
|||
|
|
@ -30,9 +30,11 @@ function imagesOf(message: Message): string[] {
|
|||
export function ChatMessage({
|
||||
message,
|
||||
toolEvents = [],
|
||||
modelLabel = "qwen3",
|
||||
}: {
|
||||
message: Message;
|
||||
toolEvents?: ToolEvent[];
|
||||
modelLabel?: string;
|
||||
}) {
|
||||
const [showThinking, setShowThinking] = useState(false);
|
||||
const isUser = message.role === "user";
|
||||
|
|
@ -41,7 +43,7 @@ export function ChatMessage({
|
|||
return (
|
||||
<div className={`msg ${isUser ? "msg-user" : "msg-assistant"}`}>
|
||||
<div className="msg-role">
|
||||
{isUser ? "du" : "qwen3"}
|
||||
{isUser ? "du" : modelLabel}
|
||||
</div>
|
||||
|
||||
{!isUser && message.thinking && message.thinking.length > 0 && (
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ interface Props {
|
|||
recording: boolean;
|
||||
transcribing: boolean;
|
||||
injectedText: string | null;
|
||||
modelLabel?: string;
|
||||
onInjected: () => void;
|
||||
onSend: (text: string, images: string[]) => void;
|
||||
onAbort: () => void;
|
||||
|
|
@ -49,6 +50,7 @@ export function Composer({
|
|||
recording,
|
||||
transcribing,
|
||||
injectedText,
|
||||
modelLabel = "qwen3",
|
||||
onInjected,
|
||||
onSend,
|
||||
onAbort,
|
||||
|
|
@ -194,7 +196,7 @@ export function Composer({
|
|||
? "Ich höre zu … (zum Beenden nochmal auf das Mikro klicken)"
|
||||
: disabled
|
||||
? "Keine Session aktiv — neuen Chat starten"
|
||||
: "Nachricht an qwen3 … (Enter = senden, Bild einfügen mit ⌘V)"
|
||||
: `Nachricht an ${modelLabel} … (Enter = senden, Bild einfügen mit ⌘V)`
|
||||
}
|
||||
rows={1}
|
||||
disabled={disabled}
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ export interface ToolEvent {
|
|||
}
|
||||
|
||||
export interface ChatOptions {
|
||||
model: string;
|
||||
think: boolean;
|
||||
tools: boolean;
|
||||
temperature: number;
|
||||
|
|
@ -35,6 +36,13 @@ export interface ChatOptions {
|
|||
dreamAuto: boolean;
|
||||
}
|
||||
|
||||
export interface OllamaModel {
|
||||
name: string;
|
||||
sizeGB?: number;
|
||||
parameterSize?: string;
|
||||
quantization?: string;
|
||||
}
|
||||
|
||||
export interface Anchor {
|
||||
id: number;
|
||||
session_id: string;
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ const SYSTEM_KEY = "oxagenttwo.systemPrompt";
|
|||
|
||||
function loadOptions(): ChatOptions {
|
||||
const defaults: ChatOptions = {
|
||||
model: "qwen3.5:latest",
|
||||
think: true,
|
||||
tools: true,
|
||||
temperature: 0.7,
|
||||
|
|
@ -65,6 +66,9 @@ export function useChat() {
|
|||
setMessages((prev) =>
|
||||
prev.some((x) => x.id === m.id) ? prev : [...prev, m],
|
||||
);
|
||||
// Legt der Server bei unbekannter sessionId einen neuen Chat an,
|
||||
// springt der Client mit — sonst landen Nachrichten "im Leeren".
|
||||
setActiveId((cur) => (cur === m.session_id ? cur : m.session_id));
|
||||
} else if (t === "assistant-start") {
|
||||
const m = data.message as Message;
|
||||
setMessages((prev) => [...prev, { ...m, content: "", thinking: "" }]);
|
||||
|
|
@ -127,16 +131,22 @@ export function useChat() {
|
|||
const res = await fetch("/api/sessions");
|
||||
const list = (await res.json()) as Session[];
|
||||
setSessions(list);
|
||||
setActiveId((cur) => cur ?? list[0]?.id ?? null);
|
||||
// Tote IDs (z. B. gelöschte Sessions) verfallen und fallen auf die neueste zurück.
|
||||
setActiveId((cur) =>
|
||||
cur && list.some((s) => s.id === cur) ? cur : (list[0]?.id ?? null),
|
||||
);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
void refreshSessions();
|
||||
fetch("/api/model")
|
||||
}, [refreshSessions]);
|
||||
|
||||
useEffect(() => {
|
||||
fetch(`/api/model?name=${encodeURIComponent(options.model)}`)
|
||||
.then((r) => r.json())
|
||||
.then(setModelInfo)
|
||||
.catch(() => setModelInfo({ ok: false, error: "Ollama nicht erreichbar" }));
|
||||
}, [refreshSessions]);
|
||||
}, [options.model]);
|
||||
|
||||
// load messages on session switch
|
||||
useEffect(() => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue