Files
gott/wwwroot/js/api.js
T
Meleki 4bf500ad47 feat: Stream-Abbruch per Stop-Button
- Neuer Stop-Button (#stop) neben Send-Button, erscheint nur während Stream
- AbortController in chat.js: abort() schließt SSE-Verbindung → Backend CancellationToken triggered
- apiChat() akzeptiert optionales AbortSignal
- AbortError-Handling: zeigt 'Abgebrochen' statt Fehlermeldung
- Button-Toggle in sendMessage(), stopStreaming(), startStreamingPoll(), Page-Load-Recovery
- CSS: .stop-btn mit --dangerous Farbe
- Diverse kleine UI-Fixes (TODO.md, theme.css box-spacing, settings-section Layout)
2026-03-02 06:52:26 +01:00

200 lines
5.4 KiB
JavaScript

// ── API Calls ──
async function apiChat(messages, sessionId, signal) {
return fetch('/api/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ messages, sessionId }),
signal
});
}
// ── Agent API ──
async function apiGetAgents() {
const resp = await fetch('/api/agents');
return resp.json();
}
async function apiGetAgent(agentId) {
const resp = await fetch(`/api/agents/${agentId}`);
if (!resp.ok) return null;
return resp.json();
}
async function apiCreateAgent(data) {
const resp = await fetch('/api/agents', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
});
return resp.json();
}
async function apiUpdateAgent(agentId, data) {
const resp = await fetch(`/api/agents/${agentId}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
});
return resp.json();
}
async function apiDeleteAgent(agentId) {
return fetch(`/api/agents/${agentId}`, { method: 'DELETE' });
}
async function apiHistory(sessionId, beforeId) {
let url = `/api/history?sessionId=${sessionId}`;
if (beforeId) url += `&beforeId=${beforeId}`;
const resp = await fetch(url);
return resp.json();
}
async function apiHistoryPoll(sessionId, afterId) {
const resp = await fetch(`/api/history/poll?sessionId=${sessionId}&afterId=${afterId}`);
return resp.json();
}
async function apiArchive(sessionId) {
const resp = await fetch(`/api/archive?sessionId=${sessionId}`, { method: 'POST' });
if (resp.status === 204) return null;
return resp.json();
}
async function apiListArchives(sessionId) {
const resp = await fetch(`/api/archives?sessionId=${sessionId}`);
return resp.json();
}
async function apiGetArchive(archiveId) {
const resp = await fetch(`/api/archive/${archiveId}`);
return resp.json();
}
// ── Session API ──
async function apiGetSessions() {
const resp = await fetch('/api/sessions');
return resp.json();
}
async function apiCreateSession(data = {}) {
const resp = await fetch('/api/sessions', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
});
return resp.json();
}
async function apiGetSession(sessionId) {
const resp = await fetch(`/api/sessions/${sessionId}`);
if (!resp.ok) return null;
return resp.json();
}
async function apiDeleteSession(sessionId) {
return fetch(`/api/sessions/${sessionId}`, { method: 'DELETE' });
}
async function apiUpdateSession(sessionId, data) {
const resp = await fetch(`/api/sessions/${sessionId}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
});
return resp.json();
}
async function apiExecCommand(sessionId, command, mode = 'background') {
const resp = await fetch(`/api/sessions/${sessionId}/exec`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ command, mode })
});
if (mode === 'foreground') return resp.json();
return resp;
}
async function apiGetSystemPrompt(sessionId) {
const resp = await fetch(`/api/sessions/${sessionId}/system-prompt`);
if (!resp.ok) throw new Error('System-Prompt konnte nicht geladen werden');
return resp.text();
}
async function apiGetSystemPromptBlocks(sessionId) {
const resp = await fetch(`/api/sessions/${sessionId}/system-prompt-blocks`);
if (!resp.ok) throw new Error('System-Prompt konnte nicht geladen werden');
return resp.json();
}
async function apiIsStreaming(sessionId) {
const resp = await fetch(`/api/sessions/${sessionId}/streaming`);
return resp.json();
}
async function apiGetChain(chainId) {
const resp = await fetch(`/api/chains/${chainId}`);
if (!resp.ok) return null;
return resp.json();
}
async function apiToggleMessageMark(messageId) {
const resp = await fetch(`/api/messages/${messageId}/mark`, { method: 'PUT' });
return resp.json();
}
async function apiGitStatus(sessionId) {
const resp = await fetch(`/api/sessions/${sessionId}/git-status`);
if (!resp.ok) return null;
return resp.json();
}
// ── Skills API ──
async function apiGetSkills() {
const resp = await fetch('/api/skills');
return resp.json();
}
async function apiGetAgentSkills(agentId) {
const resp = await fetch(`/api/agents/${agentId}/skills`);
return resp.json();
}
async function apiSetAgentSkills(agentId, skills) {
const resp = await fetch(`/api/agents/${agentId}/skills`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ skills })
});
return resp.json();
}
async function apiAddAgentSkill(agentId, slug) {
return fetch(`/api/agents/${agentId}/skills/${slug}`, { method: 'POST' });
}
async function apiRemoveAgentSkill(agentId, slug) {
return fetch(`/api/agents/${agentId}/skills/${slug}`, { method: 'DELETE' });
}
async function apiBackupDb() {
const resp = await fetch('/api/backup', { method: 'POST' });
if (!resp.ok) throw new Error('Backup fehlgeschlagen');
const blob = await resp.blob();
const cd = resp.headers.get('content-disposition') || '';
const filename = cd.match(/filename\*=UTF-8''(.+?)(?:;|$)/i)?.[1]
|| cd.match(/filename="?([^";]+)"?/)?.[1]
|| 'gott-backup.tar.gz';
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
a.remove();
URL.revokeObjectURL(url);
}