| 1 |
/** |
| 2 |
* api.js — Endpoint registry. |
| 3 |
* |
| 4 |
* Each export maps one backend endpoint to a named function. |
| 5 |
* Only concerns: URL, HTTP method, and request body shape. |
| 6 |
* No business logic, no store access, no legacy callbacks. |
| 7 |
* |
| 8 |
* Flow: request.js (HTTP) → api.js (endpoints) → actions/ (logic + stores) → components |
| 9 |
*/ |
| 10 |
|
| 11 |
import { get, post, del, stream, buildHeaders, resolveUrl, readSSEStream, createSignal } from './request.js'; |
| 12 |
|
| 13 |
// ─── Chat stream ────────────────────────────────────────────────────────────── |
| 14 |
|
| 15 |
/** |
| 16 |
* POST /agent/chat/stream |
| 17 |
* Send a user message and consume the SSE response. |
| 18 |
*/ |
| 19 |
export function chatStream(body, onEvent, signal) { |
| 20 |
return stream('/agent/chat/stream', body, onEvent, signal); |
| 21 |
} |
| 22 |
|
| 23 |
// ─── Session ────────────────────────────────────────────────────────────────── |
| 24 |
|
| 25 |
/** POST /agent/session/latest — returns the most recent session metadata (session_id + timestamps). */ |
| 26 |
export function sessionLatest() { |
| 27 |
return post('/agent/session/latest', { |
| 28 |
user_id: window.ZIP_AI_CONFIG?.userId, |
| 29 |
domain: window.ZIP_AI_CONFIG?.domain, |
| 30 |
}); |
| 31 |
} |
| 32 |
|
| 33 |
/** POST /agent/session/history */ |
| 34 |
export function sessionHistory(sessionId, limit, offset) { |
| 35 |
return post('/agent/session/history', { |
| 36 |
session_id: sessionId, |
| 37 |
user_id: window.ZIP_AI_CONFIG?.userId, |
| 38 |
domain: window.ZIP_AI_CONFIG?.domain, |
| 39 |
limit, |
| 40 |
offset, |
| 41 |
}); |
| 42 |
} |
| 43 |
|
| 44 |
/** POST /agent/session/list */ |
| 45 |
export function sessionList() { |
| 46 |
return post('/agent/session/list', { |
| 47 |
user_id: window.ZIP_AI_CONFIG?.userId, |
| 48 |
domain: window.ZIP_AI_CONFIG?.domain, |
| 49 |
}); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* GET /agent/session/{sessionId}/last-turn — Phase 4.2 reload recovery. |
| 54 |
* Returns the most recent turn for the session (regardless of status) so the |
| 55 |
* UI can restore banner + tool-card terminal states after a browser refresh. |
| 56 |
* Returns `{ turn_id: null, snapshot: null }` for sessions with no turns yet. |
| 57 |
*/ |
| 58 |
export function sessionLastTurn(sessionId) { |
| 59 |
return get(`/agent/session/${encodeURIComponent(sessionId)}/last-turn`); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* GET /agent/turn/{turnId} — canonical snapshot read for a specific turn. |
| 64 |
* Used when the UI already knows the turn_id (e.g. resume after explicit link). |
| 65 |
*/ |
| 66 |
export function turnSnapshot(turnId) { |
| 67 |
return get(`/agent/turn/${encodeURIComponent(turnId)}`); |
| 68 |
} |
| 69 |
|
| 70 |
/** POST /agent/session/clear */ |
| 71 |
export function sessionClear(sessionId) { |
| 72 |
return post('/agent/session/clear', { |
| 73 |
session_id: sessionId, |
| 74 |
user_id: window.ZIP_AI_CONFIG?.userId, |
| 75 |
}); |
| 76 |
} |
| 77 |
|
| 78 |
/** POST /agent/session/clear-all */ |
| 79 |
export function sessionClearAll() { |
| 80 |
return post('/agent/session/clear-all', { |
| 81 |
user_id: window.ZIP_AI_CONFIG?.userId, |
| 82 |
}); |
| 83 |
} |
| 84 |
|
| 85 |
/** POST /agent/session/message */ |
| 86 |
export function sessionSaveMessage(sessionId, role, content) { |
| 87 |
return post('/agent/session/message', { |
| 88 |
session_id: sessionId, |
| 89 |
user_id: window.ZIP_AI_CONFIG?.userId, |
| 90 |
domain: window.ZIP_AI_CONFIG?.domain, |
| 91 |
role, |
| 92 |
content, |
| 93 |
}); |
| 94 |
} |
| 95 |
|
| 96 |
// ─── Tool permission gate ───────────────────────────────────────────────────── |
| 97 |
|
| 98 |
/** POST /agent/tool-permission — send allow/deny/always_allow decision back to brain */ |
| 99 |
export function toolPermission(turnId, callId, decision, toolKey = null, sessionId = null) { |
| 100 |
const body = { turn_id: turnId, call_id: callId, decision }; |
| 101 |
if (toolKey) body.tool_key = toolKey; |
| 102 |
if (sessionId) body.session_id = sessionId; |
| 103 |
return post('/agent/tool-permission', body); |
| 104 |
} |
| 105 |
|
| 106 |
// ─── Plan status ────────────────────────────────────────────────────────────── |
| 107 |
|
| 108 |
/** POST /agent/plan/status */ |
| 109 |
export function planStatus() { |
| 110 |
return post('/agent/plan/status', { |
| 111 |
domain: window.ZIP_AI_CONFIG?.domain, |
| 112 |
}); |
| 113 |
} |
| 114 |
|
| 115 |
// ─── Site memory ────────────────────────────────────────────────────────────── |
| 116 |
|
| 117 |
/** GET /memory — returns all memory records for this user+domain */ |
| 118 |
export function memoryGet() { |
| 119 |
const domain = window.ZIP_AI_CONFIG?.domain; |
| 120 |
const query = domain ? `?domain=${encodeURIComponent(domain)}` : ''; |
| 121 |
return get(`/memory${query}`); |
| 122 |
} |
| 123 |
|
| 124 |
/** DELETE /memory */ |
| 125 |
export function memoryDelete() { |
| 126 |
const domain = window.ZIP_AI_CONFIG?.domain; |
| 127 |
const query = domain ? `?domain=${encodeURIComponent(domain)}` : ''; |
| 128 |
return del(`/memory${query}`); |
| 129 |
} |
| 130 |
|
| 131 |
|