@odysx/codemind 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core/provider/index.d.ts +52 -10
- package/dist/core/provider/index.js +99 -12
- package/package.json +6 -1
- package/dist/commands/agents.d.ts.map +0 -1
- package/dist/commands/agents.js.map +0 -1
- package/dist/commands/auth.d.ts.map +0 -1
- package/dist/commands/auth.js.map +0 -1
- package/dist/commands/chat.d.ts.map +0 -1
- package/dist/commands/chat.js.map +0 -1
- package/dist/commands/keys.d.ts.map +0 -1
- package/dist/commands/keys.js.map +0 -1
- package/dist/commands/local.d.ts.map +0 -1
- package/dist/commands/local.js.map +0 -1
- package/dist/commands/mcp.d.ts.map +0 -1
- package/dist/commands/mcp.js.map +0 -1
- package/dist/commands/prompts.d.ts.map +0 -1
- package/dist/commands/prompts.js.map +0 -1
- package/dist/commands/skills.d.ts.map +0 -1
- package/dist/commands/skills.js.map +0 -1
- package/dist/core/auth/index.d.ts.map +0 -1
- package/dist/core/auth/index.js.map +0 -1
- package/dist/core/config/index.d.ts.map +0 -1
- package/dist/core/config/index.js.map +0 -1
- package/dist/core/history.d.ts.map +0 -1
- package/dist/core/history.js.map +0 -1
- package/dist/core/markdown-renderer.d.ts.map +0 -1
- package/dist/core/markdown-renderer.js.map +0 -1
- package/dist/core/permissions/index.d.ts.map +0 -1
- package/dist/core/permissions/index.js.map +0 -1
- package/dist/core/provider/external.d.ts.map +0 -1
- package/dist/core/provider/external.js.map +0 -1
- package/dist/core/provider/index.d.ts.map +0 -1
- package/dist/core/provider/index.js.map +0 -1
- package/dist/core/registry/index.d.ts.map +0 -1
- package/dist/core/registry/index.js.map +0 -1
- package/dist/core/schema/types.d.ts.map +0 -1
- package/dist/core/schema/types.js.map +0 -1
- package/dist/core/terminal.d.ts.map +0 -1
- package/dist/core/terminal.js.map +0 -1
- package/dist/core/workspace/index.d.ts.map +0 -1
- package/dist/core/workspace/index.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/ui/app.d.ts.map +0 -1
- package/dist/ui/app.js.map +0 -1
- package/dist/ui/commands/cases/catalog.d.ts.map +0 -1
- package/dist/ui/commands/cases/catalog.js.map +0 -1
- package/dist/ui/commands/cases/misc.d.ts.map +0 -1
- package/dist/ui/commands/cases/misc.js.map +0 -1
- package/dist/ui/commands/cases/tasks.d.ts.map +0 -1
- package/dist/ui/commands/cases/tasks.js.map +0 -1
- package/dist/ui/commands/runCommand.d.ts.map +0 -1
- package/dist/ui/commands/runCommand.js.map +0 -1
- package/dist/ui/components/AgentPicker.d.ts.map +0 -1
- package/dist/ui/components/AgentPicker.js.map +0 -1
- package/dist/ui/components/AuthPicker.d.ts.map +0 -1
- package/dist/ui/components/AuthPicker.js.map +0 -1
- package/dist/ui/components/Banner.d.ts.map +0 -1
- package/dist/ui/components/Banner.js.map +0 -1
- package/dist/ui/components/CommandMenu.d.ts.map +0 -1
- package/dist/ui/components/CommandMenu.js.map +0 -1
- package/dist/ui/components/ErrorBoundary.d.ts.map +0 -1
- package/dist/ui/components/ErrorBoundary.js.map +0 -1
- package/dist/ui/components/InputBar.d.ts.map +0 -1
- package/dist/ui/components/InputBar.js.map +0 -1
- package/dist/ui/components/KeysPanel.d.ts.map +0 -1
- package/dist/ui/components/KeysPanel.js.map +0 -1
- package/dist/ui/components/MessageView.d.ts.map +0 -1
- package/dist/ui/components/MessageView.js.map +0 -1
- package/dist/ui/components/PermissionPrompt.d.ts.map +0 -1
- package/dist/ui/components/PermissionPrompt.js.map +0 -1
- package/dist/ui/components/Pickers.d.ts.map +0 -1
- package/dist/ui/components/Pickers.js.map +0 -1
- package/dist/ui/components/SearchOverlay.d.ts.map +0 -1
- package/dist/ui/components/SearchOverlay.js.map +0 -1
- package/dist/ui/components/SessionPicker.d.ts.map +0 -1
- package/dist/ui/components/SessionPicker.js.map +0 -1
- package/dist/ui/components/WorkingIndicator.d.ts.map +0 -1
- package/dist/ui/components/WorkingIndicator.js.map +0 -1
- package/dist/ui/controls/ApiKeyEntry.d.ts.map +0 -1
- package/dist/ui/controls/ApiKeyEntry.js.map +0 -1
- package/dist/ui/controls/MultiSelect.d.ts.map +0 -1
- package/dist/ui/controls/MultiSelect.js.map +0 -1
- package/dist/ui/hooks/useAgentPicker.d.ts.map +0 -1
- package/dist/ui/hooks/useAgentPicker.js.map +0 -1
- package/dist/ui/hooks/useAuthFlow.d.ts.map +0 -1
- package/dist/ui/hooks/useAuthFlow.js.map +0 -1
- package/dist/ui/hooks/useKeysFlow.d.ts.map +0 -1
- package/dist/ui/hooks/useKeysFlow.js.map +0 -1
- package/dist/ui/hooks/useSessionPicker.d.ts.map +0 -1
- package/dist/ui/hooks/useSessionPicker.js.map +0 -1
- package/dist/ui/hooks/useTerminalSize.d.ts.map +0 -1
- package/dist/ui/hooks/useTerminalSize.js.map +0 -1
- package/dist/ui/input/keyRouter.d.ts.map +0 -1
- package/dist/ui/input/keyRouter.js.map +0 -1
- package/dist/ui/layouts/ScreenLayout.d.ts.map +0 -1
- package/dist/ui/layouts/ScreenLayout.js.map +0 -1
- package/dist/ui/shared/types.d.ts.map +0 -1
- package/dist/ui/shared/types.js.map +0 -1
- package/dist/ui/theme.d.ts.map +0 -1
- package/dist/ui/theme.js.map +0 -1
- package/dist/ui/utils/markdown.d.ts.map +0 -1
- package/dist/ui/utils/markdown.js.map +0 -1
- package/dist/ui/utils/mentions.d.ts.map +0 -1
- package/dist/ui/utils/mentions.js.map +0 -1
- package/dist/ui/utils/terminal-text.d.ts.map +0 -1
- package/dist/ui/utils/terminal-text.js.map +0 -1
- package/dist/ui/utils/text.d.ts.map +0 -1
- package/dist/ui/utils/text.js.map +0 -1
- package/eslint.config.js +0 -40
- package/graphify-out/GRAPH_REPORT.md +0 -140
- package/graphify-out/cache/1236b1806943f8eb5c64fece9fb448149370f4a1b52b32f0977ad528d7911291.json +0 -1
- package/graphify-out/cache/15cbf641dcc0fd641c18da872214918203a1f64baa7ac8430e4702ef32d18b02.json +0 -1
- package/graphify-out/cache/1abc52745bb46f6cc5a528530e2bc1f368c26e348dff85cdde5cad6f275d7177.json +0 -1
- package/graphify-out/cache/1c7815c37c13177e98895e272285f6151a8d0807478fc843ec452634b3e9912d.json +0 -1
- package/graphify-out/cache/1ec38c39f044f329c78642b61d6394fb8e15d0014335f32947d1f5c7b88c02bc.json +0 -1
- package/graphify-out/cache/20c9b57ff76dbb9d57cbd00659b0a42d2b28dd2abacf1ef4143cc67a5b6fe7f4.json +0 -1
- package/graphify-out/cache/285e11876884eb619e779d540cd957b33e70f14c67ecc26c9b9755aa9cedd717.json +0 -1
- package/graphify-out/cache/2c0a9c49ab51f4e0f07543b4e941bf802b7db31fa607c4cee8e595575be68fe7.json +0 -1
- package/graphify-out/cache/3025e6c2756c79ccaadd51d611f25a8b6630a6a11ce1b4e6e261b3e033dc194f.json +0 -1
- package/graphify-out/cache/4801a686ddfa0a52824a4fc3d65443cbb0ab9f0b030af67d02fccb520c584a08.json +0 -1
- package/graphify-out/cache/4f8fd96f38aa7d0293b4fab60c4581c4f3e68f4533c664c9cd78d1c53bea1cba.json +0 -1
- package/graphify-out/cache/5928e562b0f5700184b810e2403f4aa685e2b93c3c584c10460562b514c40fdd.json +0 -1
- package/graphify-out/cache/66132486efb0a4cfaeea766448f1f15bb33635808714052e7e24e5b00fbc984f.json +0 -1
- package/graphify-out/cache/66ab27669e7ec685130063662af9f30018c267f7639b4af4b0504a98282c383a.json +0 -1
- package/graphify-out/cache/6ff6b3dd433b5ffe96e166263a203dd622397e419dc00bb676824988b7defeab.json +0 -1
- package/graphify-out/cache/7a00a49932177a6215e2590b1ddfd6f1d8494792f693068e9d427d16b165d370.json +0 -1
- package/graphify-out/cache/7b681bc5182f40f707f1c0b16b78af38efffa62a1df315d10878f4de888408ef.json +0 -1
- package/graphify-out/cache/81196d44e50ac0b276d826a531ed10c425a528b298e3fe18e8d5a3eec996e9d5.json +0 -1
- package/graphify-out/cache/8934e9b32dea6e66fdc34fa1681b287ff1335c389917e0fc6ee2fc573e23e87b.json +0 -1
- package/graphify-out/cache/975f6c99807f86b79c80ca40c783618e2080a927eaebe6ea3db37b869f048851.json +0 -1
- package/graphify-out/cache/9ab98f08626fd8190462595e38de828293ad184db8df5e1177b05a8fe290faa1.json +0 -1
- package/graphify-out/cache/9c434a6796d7932b6e87a908c155c09cb2b8d097fc55f141f0302b6600f94749.json +0 -1
- package/graphify-out/cache/ac5cf4c489dc417c1f6ea728d0b6ba12ac7087bc6c2e736697ff3024b4d4e534.json +0 -1
- package/graphify-out/cache/ae5083b5c1dca3e5c6af485e7f6eb8a08ff8b382cdecf92ab5e0902053442afe.json +0 -1
- package/graphify-out/cache/b274057fc2e0118cf2746d1d8060841122bb2a2b0305566fc3f32d3a51e08bcc.json +0 -1
- package/graphify-out/cache/b39beef31da682387f030c0d149f6cd5b3e66effbf936cc8430db15360759dda.json +0 -1
- package/graphify-out/cache/bf3c6a36bb72820bc11cbf4a5c49067d1f62013d4686c019a4dbd9951b1e980e.json +0 -1
- package/graphify-out/cache/c034e156028d1a58b89bce659726e9a4065ddc4d2b560d6bce7a206fdff8dad3.json +0 -1
- package/graphify-out/cache/c619b5bbe8d1f427fbd7816d1e9fcfaf90d7201b020ae6b578166aeb9cb20683.json +0 -1
- package/graphify-out/cache/c8db77a9da0b0f601acfa50a620f31155cdda0d0cc0f2285adcea19e77194ee6.json +0 -1
- package/graphify-out/cache/ca664791ec7741a2077d9e7c10384142d30e7a3b77b388df719c315da608d57d.json +0 -1
- package/graphify-out/cache/cba43a116186bd55c8a63368dea4eee4a8123b2bec0a42df6efb3254f89f9a7a.json +0 -1
- package/graphify-out/cache/cd51cbb3dad1cf34417b9761fe2eb184a9f57d23dc86e45630e63bfa2ecafddc.json +0 -1
- package/graphify-out/cache/d06738cde193b34a136b94104220c17e0212b814b3e4d7ff399105635cae1a03.json +0 -1
- package/graphify-out/cache/d093c19f0169c0398eeb1360895b1c16ecc50abc59f4be8eec3f6fd27c89a6e5.json +0 -1
- package/graphify-out/cache/da8f75c438531d232fe114d5cbf2374576ce842a5fb87c7e5a88b78fdb334e5e.json +0 -1
- package/graphify-out/cache/e35798fa04ddede548a6953bdea506bd7d0788dfed4e8a8ea6ae77b1b60ef5e0.json +0 -1
- package/graphify-out/cache/e5a38c254719000487098782125e15e2e982c07d66b99d359ed1781f30f9fc8c.json +0 -1
- package/graphify-out/cache/e6e4c4a5a7200ee0154585b5d6538f699b0a746dbded3164f8391f06ce7cb212.json +0 -1
- package/graphify-out/cache/ea8d0838df6309b673b031f616b253af957898ee7289d25a541fa6183b732e4e.json +0 -1
- package/graphify-out/cache/ebaee92b0e881efd7a08f3143e2c1803310396b4651d4f77532003c699cc6836.json +0 -1
- package/graphify-out/cache/edf8543318a9e60d56d04704078da6c37202ddd6fdfc83144fe2b94fe19b80ef.json +0 -1
- package/graphify-out/cache/efca19d3668035f11126fc9eccbeab290abfa94babf3f9a8706de81d17aba263.json +0 -1
- package/graphify-out/cache/f1480bb642025ebd6d99986cdd8a7d5726897ec038300e4b095c9c938435dfad.json +0 -1
- package/graphify-out/cache/f459f21277f03865f732affb175b1b092954dae5967f27b3cd1864466e3e24f4.json +0 -1
- package/graphify-out/cache/f899e37e864010b30f4c196e86082d7bd022a0df032005e0407883ebcec36514.json +0 -1
- package/graphify-out/cache/fb3cbe07fbcd97c536cff9c6bc343c35f22d49613f05c53ad30ef67a75c5d93e.json +0 -1
- package/graphify-out/cache/fd3dc682b1ca2f5ae03b50d6b0e045ae50f5701c0a21c5a388d77ef482939d3a.json +0 -1
- package/graphify-out/graph.html +0 -257
- package/graphify-out/graph.json +0 -4396
- package/scripts/e2e-external.mjs +0 -65
- package/scripts/live-agents.mjs +0 -6
- package/scripts/live-stream.mjs +0 -51
- package/src/commands/agents.ts +0 -91
- package/src/commands/auth.ts +0 -39
- package/src/commands/chat.ts +0 -198
- package/src/commands/keys.ts +0 -83
- package/src/commands/local.ts +0 -302
- package/src/commands/mcp.ts +0 -74
- package/src/commands/prompts.ts +0 -58
- package/src/commands/skills.ts +0 -43
- package/src/core/auth/index.ts +0 -101
- package/src/core/config/index.ts +0 -152
- package/src/core/history.ts +0 -79
- package/src/core/markdown-renderer.ts +0 -420
- package/src/core/permissions/index.ts +0 -99
- package/src/core/provider/external.ts +0 -312
- package/src/core/provider/index.ts +0 -517
- package/src/core/registry/index.ts +0 -250
- package/src/core/schema/types.ts +0 -106
- package/src/core/terminal.ts +0 -124
- package/src/core/workspace/index.ts +0 -216
- package/src/index.ts +0 -401
- package/src/ui/app.tsx +0 -644
- package/src/ui/commands/cases/catalog.ts +0 -76
- package/src/ui/commands/cases/misc.ts +0 -293
- package/src/ui/commands/cases/tasks.ts +0 -115
- package/src/ui/commands/runCommand.ts +0 -178
- package/src/ui/components/Banner.tsx +0 -63
- package/src/ui/components/CommandMenu.tsx +0 -63
- package/src/ui/components/ErrorBoundary.tsx +0 -75
- package/src/ui/components/InputBar.tsx +0 -227
- package/src/ui/components/KeysPanel.tsx +0 -115
- package/src/ui/components/MessageView.tsx +0 -226
- package/src/ui/components/Pickers.tsx +0 -234
- package/src/ui/components/SearchOverlay.tsx +0 -65
- package/src/ui/components/WorkingIndicator.tsx +0 -35
- package/src/ui/controls/ApiKeyEntry.tsx +0 -29
- package/src/ui/controls/MultiSelect.tsx +0 -55
- package/src/ui/hooks/useAgentPicker.ts +0 -64
- package/src/ui/hooks/useAuthFlow.ts +0 -47
- package/src/ui/hooks/useKeysFlow.ts +0 -157
- package/src/ui/hooks/useSessionPicker.ts +0 -84
- package/src/ui/hooks/useTerminalSize.ts +0 -36
- package/src/ui/input/keyRouter.ts +0 -627
- package/src/ui/layouts/ScreenLayout.tsx +0 -202
- package/src/ui/shared/types.ts +0 -160
- package/src/ui/theme.ts +0 -53
- package/src/ui/utils/markdown.ts +0 -255
- package/src/ui/utils/mentions.ts +0 -40
- package/src/ui/utils/terminal-text.ts +0 -130
- package/src/ui/utils/text.ts +0 -174
- package/test/commands.test.mjs +0 -127
- package/test/config.test.mjs +0 -123
- package/test/history.test.mjs +0 -42
- package/test/keyrouter.test.mjs +0 -324
- package/test/layout.test.mjs +0 -271
- package/test/markdown.test.mjs +0 -92
- package/test/mentions.test.mjs +0 -51
- package/test/permissions.test.mjs +0 -130
- package/test/provider.test.mjs +0 -287
- package/test/terminal-text.test.mjs +0 -116
- package/test/text.test.mjs +0 -134
- package/test/workspace.test.mjs +0 -101
- package/tsconfig.json +0 -32
|
@@ -1,517 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* API provider — HTTP client for the OdysX backend.
|
|
3
|
-
*
|
|
4
|
-
* Handles: auth headers, JSON parsing, SSE streaming, error mapping.
|
|
5
|
-
* Mirrors the frontend's `apiMap` pattern.
|
|
6
|
-
*
|
|
7
|
-
* Every request is bounded by a timeout. Callers that pass their own signal
|
|
8
|
-
* (Esc to interrupt) still get their abort honoured, and user aborts stay
|
|
9
|
-
* distinguishable from timeouts so the REPL can report them differently.
|
|
10
|
-
*/
|
|
11
|
-
import { getConfig } from "../config/index.js";
|
|
12
|
-
|
|
13
|
-
export interface ApiOptions {
|
|
14
|
-
method?: string;
|
|
15
|
-
body?: unknown;
|
|
16
|
-
headers?: Record<string, string>;
|
|
17
|
-
stream?: boolean;
|
|
18
|
-
signal?: AbortSignal;
|
|
19
|
-
/** Override the default request timeout (ms). */
|
|
20
|
-
timeoutMs?: number;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export interface ApiResponse<T = unknown> {
|
|
24
|
-
ok: boolean;
|
|
25
|
-
status: number;
|
|
26
|
-
data: T;
|
|
27
|
-
error?: string;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/** SSE event from streaming endpoint */
|
|
31
|
-
export interface StreamEvent {
|
|
32
|
-
type: "token" | "done" | "error";
|
|
33
|
-
data: string;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/** Default ceiling for a non-streaming API call. */
|
|
37
|
-
export const DEFAULT_TIMEOUT_MS = 30_000;
|
|
38
|
-
/** Streaming turns may legitimately run long, but never forever. */
|
|
39
|
-
export const STREAM_IDLE_TIMEOUT_MS = 120_000;
|
|
40
|
-
|
|
41
|
-
/** Build full URL from path */
|
|
42
|
-
function buildUrl(path: string, baseUrl?: string): string {
|
|
43
|
-
const base = baseUrl ?? getConfig().apiUrl;
|
|
44
|
-
return `${base}${path.replace(/^\//, "/")}`;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/** Build auth headers */
|
|
48
|
-
async function authHeaders(): Promise<Record<string, string>> {
|
|
49
|
-
const config = getConfig();
|
|
50
|
-
const headers: Record<string, string> = {
|
|
51
|
-
"Content-Type": "application/json",
|
|
52
|
-
Accept: "application/json",
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
if (config.apiKey) {
|
|
56
|
-
headers["X-Api-Key"] = config.apiKey;
|
|
57
|
-
} else if (config.token) {
|
|
58
|
-
headers["Authorization"] = `Bearer ${config.token}`;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
return headers;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Combine a caller signal with a timeout. Abort reasons are reported through
|
|
66
|
-
* `timedOut()` so callers can tell "user pressed Esc" from "server hung".
|
|
67
|
-
*/
|
|
68
|
-
function withTimeout(
|
|
69
|
-
external: AbortSignal | undefined,
|
|
70
|
-
ms: number,
|
|
71
|
-
): { signal: AbortSignal; clear: () => void; timedOut: () => boolean } {
|
|
72
|
-
const controller = new AbortController();
|
|
73
|
-
let fired = false;
|
|
74
|
-
const timer = setTimeout(() => {
|
|
75
|
-
fired = true;
|
|
76
|
-
controller.abort();
|
|
77
|
-
}, ms);
|
|
78
|
-
// Do not keep the event loop alive just for the timer.
|
|
79
|
-
timer.unref?.();
|
|
80
|
-
|
|
81
|
-
// The listener must be removed on success too. A long-lived caller signal
|
|
82
|
-
// (a shared controller, a batch runner) accumulated one listener per
|
|
83
|
-
// request; Node warns at eleven, and the warning is written to stderr in
|
|
84
|
-
// the middle of the Ink frame.
|
|
85
|
-
let detach: (() => void) | undefined;
|
|
86
|
-
if (external) {
|
|
87
|
-
if (external.aborted) {
|
|
88
|
-
controller.abort();
|
|
89
|
-
} else {
|
|
90
|
-
const onAbort = (): void => controller.abort();
|
|
91
|
-
external.addEventListener("abort", onAbort, { once: true });
|
|
92
|
-
detach = () => external.removeEventListener("abort", onAbort);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
return {
|
|
96
|
-
signal: controller.signal,
|
|
97
|
-
clear: () => {
|
|
98
|
-
clearTimeout(timer);
|
|
99
|
-
detach?.();
|
|
100
|
-
detach = undefined;
|
|
101
|
-
},
|
|
102
|
-
timedOut: () => fired,
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Keep a stream alive for as long as it keeps producing, but abort if it goes
|
|
108
|
-
* silent for `ms`. This is deliberately *not* `AbortSignal.timeout`, which is a
|
|
109
|
-
* single deadline from the start: a reply that streams for longer than the
|
|
110
|
-
* timeout would be killed mid-sentence even though data was flowing.
|
|
111
|
-
*
|
|
112
|
-
* `touch()` resets the countdown; call it for every chunk received.
|
|
113
|
-
*/
|
|
114
|
-
export interface IdleTimeout {
|
|
115
|
-
signal: AbortSignal;
|
|
116
|
-
touch: () => void;
|
|
117
|
-
clear: () => void;
|
|
118
|
-
timedOut: () => boolean;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
export function idleTimeout(
|
|
122
|
-
ms: number,
|
|
123
|
-
external?: AbortSignal,
|
|
124
|
-
): IdleTimeout {
|
|
125
|
-
const controller = new AbortController();
|
|
126
|
-
let fired = false;
|
|
127
|
-
let timer: ReturnType<typeof setTimeout> | null = null;
|
|
128
|
-
|
|
129
|
-
const arm = (): void => {
|
|
130
|
-
if (fired) return;
|
|
131
|
-
timer = setTimeout(() => {
|
|
132
|
-
fired = true;
|
|
133
|
-
controller.abort();
|
|
134
|
-
}, ms);
|
|
135
|
-
timer.unref?.();
|
|
136
|
-
};
|
|
137
|
-
arm();
|
|
138
|
-
|
|
139
|
-
let detach: (() => void) | undefined;
|
|
140
|
-
if (external) {
|
|
141
|
-
if (external.aborted) {
|
|
142
|
-
// Already cancelled by the caller — do not arm a timer that can never
|
|
143
|
-
// fire, and surface the abort immediately.
|
|
144
|
-
fired = false;
|
|
145
|
-
if (timer) clearTimeout(timer);
|
|
146
|
-
controller.abort(external.reason);
|
|
147
|
-
} else {
|
|
148
|
-
const onAbort = (): void => controller.abort(external.reason);
|
|
149
|
-
external.addEventListener("abort", onAbort, { once: true });
|
|
150
|
-
// Removed on clear() as well: a long-lived caller signal otherwise
|
|
151
|
-
// accumulates a listener per request until Node warns at eleven.
|
|
152
|
-
detach = () => external.removeEventListener("abort", onAbort);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
return {
|
|
157
|
-
signal: controller.signal,
|
|
158
|
-
touch: () => {
|
|
159
|
-
if (fired) return;
|
|
160
|
-
if (timer) clearTimeout(timer);
|
|
161
|
-
arm();
|
|
162
|
-
},
|
|
163
|
-
clear: () => {
|
|
164
|
-
if (timer) clearTimeout(timer);
|
|
165
|
-
detach?.();
|
|
166
|
-
detach = undefined;
|
|
167
|
-
timer = null;
|
|
168
|
-
},
|
|
169
|
-
timedOut: () => fired,
|
|
170
|
-
};
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/** Human-readable error message from a non-2xx JSON or text body. */
|
|
174
|
-
function errorMessage(data: unknown, status: number, statusText: string): string {
|
|
175
|
-
const rec = data as Record<string, unknown> | null;
|
|
176
|
-
if (rec && typeof rec === "object") {
|
|
177
|
-
const detail = rec.detail ?? rec.message ?? rec.error;
|
|
178
|
-
if (typeof detail === "string" && detail.trim()) return detail;
|
|
179
|
-
// FastAPI validation errors arrive as an array of {loc, msg}.
|
|
180
|
-
if (Array.isArray(detail) && detail.length > 0) {
|
|
181
|
-
const first = detail[0] as { msg?: string; loc?: unknown[] };
|
|
182
|
-
if (first?.msg) return `${first.msg}${first.loc ? ` (${first.loc.join(".")})` : ""}`;
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
return statusText ? `HTTP ${status} ${statusText}` : `HTTP ${status}`;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* Parse a response body without ever throwing on malformed JSON.
|
|
190
|
-
*
|
|
191
|
-
* `kind` reports what actually arrived so callers can refuse to treat an HTML
|
|
192
|
-
* login page as a successful API payload:
|
|
193
|
-
* json — parsed JSON object/array/number
|
|
194
|
-
* empty — no body at all
|
|
195
|
-
* text — non-JSON content type, or a JSON content type with broken JSON
|
|
196
|
-
*/
|
|
197
|
-
async function readBody(
|
|
198
|
-
res: Response,
|
|
199
|
-
): Promise<{ data: unknown; raw: string; kind: "json" | "text" | "empty" }> {
|
|
200
|
-
const contentType = res.headers.get("content-type") ?? "";
|
|
201
|
-
const raw = await res.text();
|
|
202
|
-
if (!raw) return { data: null, raw, kind: "empty" };
|
|
203
|
-
if (contentType.includes("application/json") || contentType.includes("+json")) {
|
|
204
|
-
try {
|
|
205
|
-
return { data: JSON.parse(raw), raw, kind: "json" };
|
|
206
|
-
} catch {
|
|
207
|
-
return { data: raw, raw, kind: "text" };
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
return { data: raw, raw, kind: "text" };
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* Encode a value for use as a single URL path segment.
|
|
215
|
-
*
|
|
216
|
-
* Ids come straight from the command line. Interpolated raw, `x/../../y`
|
|
217
|
-
* would be normalised by undici into a *different* endpoint and sent with the
|
|
218
|
-
* user's credentials; `?`/`#` would truncate the path. Always encode.
|
|
219
|
-
*/
|
|
220
|
-
export const urlSegment = (value: string): string => encodeURIComponent(value);
|
|
221
|
-
|
|
222
|
-
/** Generic API call */
|
|
223
|
-
export async function apiCall<T = unknown>(
|
|
224
|
-
path: string,
|
|
225
|
-
options: ApiOptions = {},
|
|
226
|
-
): Promise<ApiResponse<T>> {
|
|
227
|
-
const url = buildUrl(path);
|
|
228
|
-
const authHdrs = await authHeaders();
|
|
229
|
-
const timeout = withTimeout(options.signal, options.timeoutMs ?? DEFAULT_TIMEOUT_MS);
|
|
230
|
-
|
|
231
|
-
try {
|
|
232
|
-
const res = await fetch(url, {
|
|
233
|
-
method: options.method ?? "GET",
|
|
234
|
-
headers: { ...authHdrs, ...options.headers },
|
|
235
|
-
body: options.body !== undefined ? JSON.stringify(options.body) : undefined,
|
|
236
|
-
signal: timeout.signal,
|
|
237
|
-
});
|
|
238
|
-
|
|
239
|
-
const { data, kind } = await readBody(res);
|
|
240
|
-
|
|
241
|
-
if (!res.ok) {
|
|
242
|
-
return {
|
|
243
|
-
ok: false,
|
|
244
|
-
status: res.status,
|
|
245
|
-
data: data as T,
|
|
246
|
-
error: errorMessage(data, res.status, res.statusText),
|
|
247
|
-
};
|
|
248
|
-
}
|
|
249
|
-
// A 2xx that isn't JSON is not a success this client can use: a proxy
|
|
250
|
-
// login page or a captive portal returns 200 text/html, and returning it
|
|
251
|
-
// as `data` made callers read properties off a string (`data.api_key` →
|
|
252
|
-
// undefined, printed as if it were the created key).
|
|
253
|
-
if (kind !== "json" && kind !== "empty") {
|
|
254
|
-
return {
|
|
255
|
-
ok: false,
|
|
256
|
-
status: res.status,
|
|
257
|
-
data: data as T,
|
|
258
|
-
error: `Unexpected non-JSON response from ${url} (${res.headers.get("content-type") ?? "no content type"})`,
|
|
259
|
-
};
|
|
260
|
-
}
|
|
261
|
-
return { ok: true, status: res.status, data: data as T };
|
|
262
|
-
} catch (err) {
|
|
263
|
-
if (timeout.timedOut()) {
|
|
264
|
-
return {
|
|
265
|
-
ok: false,
|
|
266
|
-
status: 0,
|
|
267
|
-
data: null as T,
|
|
268
|
-
error: `Request timed out after ${Math.round((options.timeoutMs ?? DEFAULT_TIMEOUT_MS) / 1000)}s — ${url}`,
|
|
269
|
-
};
|
|
270
|
-
}
|
|
271
|
-
const msg = err instanceof Error ? err.message : String(err);
|
|
272
|
-
// undici reports the real failure in err.cause.code; the message is often
|
|
273
|
-
// just "fetch failed", which told the user nothing actionable.
|
|
274
|
-
const code = (err as { cause?: { code?: string } })?.cause?.code ?? "";
|
|
275
|
-
if (
|
|
276
|
-
msg.includes("fetch failed") || msg.includes("ECONNREFUSED") ||
|
|
277
|
-
msg.includes("ENOTFOUND") || NETWORK_ERROR_CODES.has(code)
|
|
278
|
-
) {
|
|
279
|
-
const detail = code && !msg.includes(code) ? ` (${code})` : "";
|
|
280
|
-
return {
|
|
281
|
-
ok: false,
|
|
282
|
-
status: 0,
|
|
283
|
-
data: null as T,
|
|
284
|
-
error: `Cannot connect to ${getConfig().apiUrl}${detail}. Is the server running?`,
|
|
285
|
-
};
|
|
286
|
-
}
|
|
287
|
-
if (err instanceof Error && err.name === "AbortError") {
|
|
288
|
-
return { ok: false, status: 0, data: null as T, error: "Request cancelled" };
|
|
289
|
-
}
|
|
290
|
-
return { ok: false, status: 0, data: null as T, error: msg };
|
|
291
|
-
} finally {
|
|
292
|
-
timeout.clear();
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
/** Socket-level failures that mean "the server is unreachable", not a bug. */
|
|
297
|
-
const NETWORK_ERROR_CODES = new Set([
|
|
298
|
-
"ECONNREFUSED", "ECONNRESET", "ENOTFOUND", "EAI_AGAIN", "ETIMEDOUT",
|
|
299
|
-
"EHOSTUNREACH", "ENETUNREACH", "CERT_HAS_EXPIRED", "DEPTH_ZERO_SELF_SIGNED_CERT",
|
|
300
|
-
"UNABLE_TO_VERIFY_LEAF_SIGNATURE", "UND_ERR_CONNECT_TIMEOUT",
|
|
301
|
-
]);
|
|
302
|
-
|
|
303
|
-
/* ── SSE ─────────────────────────────────────────────────────────────────── */
|
|
304
|
-
|
|
305
|
-
/**
|
|
306
|
-
* Incremental SSE reader shared by the platform stream and the external
|
|
307
|
-
* providers. It tolerates the framing differences seen in the wild:
|
|
308
|
-
* - `data:` with or without a space
|
|
309
|
-
* - CRLF and LF line endings
|
|
310
|
-
* - a final event with no trailing newline
|
|
311
|
-
* - comments / `event:` lines (ignored)
|
|
312
|
-
* `onData` receives the raw payload string of every `data:` line.
|
|
313
|
-
* `onActivity` fires on every chunk read, so a caller can keep an idle
|
|
314
|
-
* timeout armed only while the stream is genuinely silent.
|
|
315
|
-
*/
|
|
316
|
-
export async function readSSE(
|
|
317
|
-
res: Response,
|
|
318
|
-
onData: (data: string) => void,
|
|
319
|
-
signal?: AbortSignal,
|
|
320
|
-
onActivity?: () => void,
|
|
321
|
-
): Promise<void> {
|
|
322
|
-
const reader = res.body?.getReader();
|
|
323
|
-
if (!reader) throw new Error("No response body");
|
|
324
|
-
|
|
325
|
-
const decoder = new TextDecoder();
|
|
326
|
-
let buffer = "";
|
|
327
|
-
|
|
328
|
-
const dispatch = (line: string): void => {
|
|
329
|
-
const trimmed = line.endsWith("\r") ? line.slice(0, -1) : line;
|
|
330
|
-
if (!trimmed.startsWith("data:")) return;
|
|
331
|
-
const payload = trimmed.slice(5).replace(/^ /, "");
|
|
332
|
-
if (payload !== "") onData(payload);
|
|
333
|
-
};
|
|
334
|
-
|
|
335
|
-
try {
|
|
336
|
-
while (true) {
|
|
337
|
-
if (signal?.aborted) {
|
|
338
|
-
await reader.cancel().catch(() => {});
|
|
339
|
-
return;
|
|
340
|
-
}
|
|
341
|
-
const { done, value } = await reader.read();
|
|
342
|
-
if (done) break;
|
|
343
|
-
onActivity?.();
|
|
344
|
-
|
|
345
|
-
buffer += decoder.decode(value, { stream: true });
|
|
346
|
-
const lines = buffer.split("\n");
|
|
347
|
-
buffer = lines.pop() ?? "";
|
|
348
|
-
for (const line of lines) dispatch(line);
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
// Flush whatever the decoder and the line buffer still hold: a final
|
|
352
|
-
// `data: [DONE]` without a newline is common behind proxies.
|
|
353
|
-
buffer += decoder.decode();
|
|
354
|
-
if (buffer.trim()) dispatch(buffer);
|
|
355
|
-
} catch (err) {
|
|
356
|
-
// A throwing onData (an `[ERROR]` frame, a render failure) must not leave
|
|
357
|
-
// the HTTP response streaming in the background: cancel the body before
|
|
358
|
-
// the error continues up the stack.
|
|
359
|
-
await reader.cancel().catch(() => {});
|
|
360
|
-
throw err;
|
|
361
|
-
} finally {
|
|
362
|
-
reader.releaseLock();
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
/** Stream SSE from the chat endpoint. Calls `onToken` for each token.
|
|
367
|
-
* `onUsage` (when given) receives approximate token counts computed from
|
|
368
|
-
* the stream (~4 chars ≈ 1 token, matching the server-side accounting). */
|
|
369
|
-
export async function streamChat(
|
|
370
|
-
path: string,
|
|
371
|
-
body: unknown,
|
|
372
|
-
onToken: (token: string) => void,
|
|
373
|
-
onDone?: () => void,
|
|
374
|
-
signal?: AbortSignal,
|
|
375
|
-
onUsage?: (usage: { promptTokens: number; completionTokens: number }) => void,
|
|
376
|
-
): Promise<void> {
|
|
377
|
-
const url = buildUrl(path);
|
|
378
|
-
const authHdrs = await authHeaders();
|
|
379
|
-
const timeout = idleTimeout(STREAM_IDLE_TIMEOUT_MS, signal);
|
|
380
|
-
|
|
381
|
-
let completionChars = 0;
|
|
382
|
-
// A completed stream reaches both the `[DONE]` frame and the code after
|
|
383
|
-
// readSSE returns. Firing usage/done in both places doubled every session's
|
|
384
|
-
// token count and called `onDone` twice (which double-prompts interactive
|
|
385
|
-
// chat). The guard makes "finish" idempotent whichever path gets there first.
|
|
386
|
-
let finished = false;
|
|
387
|
-
const finish = (): void => {
|
|
388
|
-
if (finished) return;
|
|
389
|
-
finished = true;
|
|
390
|
-
onUsage?.({
|
|
391
|
-
promptTokens: Math.max(1, Math.ceil(
|
|
392
|
-
((body as { message?: string })?.message?.length ?? 0) / 4,
|
|
393
|
-
)),
|
|
394
|
-
completionTokens: Math.max(1, Math.ceil(completionChars / 4)),
|
|
395
|
-
});
|
|
396
|
-
onDone?.();
|
|
397
|
-
};
|
|
398
|
-
|
|
399
|
-
try {
|
|
400
|
-
const res = await fetch(url, {
|
|
401
|
-
method: "POST",
|
|
402
|
-
headers: { ...authHdrs, "Content-Type": "application/json" },
|
|
403
|
-
body: JSON.stringify(body),
|
|
404
|
-
signal: timeout.signal,
|
|
405
|
-
});
|
|
406
|
-
|
|
407
|
-
if (!res.ok) {
|
|
408
|
-
const { data } = await readBody(res);
|
|
409
|
-
throw new Error(errorMessage(data, res.status, res.statusText));
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
await readSSE(res, (data) => {
|
|
413
|
-
if (data === "[DONE]") {
|
|
414
|
-
finish();
|
|
415
|
-
return;
|
|
416
|
-
}
|
|
417
|
-
if (data.startsWith("[ERROR]")) {
|
|
418
|
-
throw new Error(data.slice(7).trim() || "Stream error");
|
|
419
|
-
}
|
|
420
|
-
completionChars += data.length;
|
|
421
|
-
onToken(data);
|
|
422
|
-
}, timeout.signal, timeout.touch);
|
|
423
|
-
|
|
424
|
-
// Some servers close without a `[DONE]` frame: settle the turn here too.
|
|
425
|
-
finish();
|
|
426
|
-
} catch (err) {
|
|
427
|
-
if (timeout.timedOut()) {
|
|
428
|
-
throw new Error(
|
|
429
|
-
`Stream timed out after ${Math.round(STREAM_IDLE_TIMEOUT_MS / 1000)}s with no response — ${url}`,
|
|
430
|
-
);
|
|
431
|
-
}
|
|
432
|
-
throw err;
|
|
433
|
-
} finally {
|
|
434
|
-
timeout.clear();
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
/**
|
|
439
|
-
* One chat turn for every CLI front-end.
|
|
440
|
-
*
|
|
441
|
-
* Three chat paths used to disagree: the Ink REPL handled BYOK and fell back
|
|
442
|
-
* from master's unsupported SSE to POST /llm/chat, while `odysx chat` and the
|
|
443
|
-
* piped REPL streamed blindly and failed out of the box. Keeping the choice
|
|
444
|
-
* here means a new front-end cannot forget either behaviour.
|
|
445
|
-
*
|
|
446
|
-
* `history` is only used for external providers (which need the whole
|
|
447
|
-
* conversation); the platform owns session history server-side.
|
|
448
|
-
*/
|
|
449
|
-
export async function streamTurn(
|
|
450
|
-
message: string,
|
|
451
|
-
opts: {
|
|
452
|
-
agentId?: string;
|
|
453
|
-
sessionId: string;
|
|
454
|
-
history?: Array<{ role: "user" | "assistant"; content: string }>;
|
|
455
|
-
system?: string;
|
|
456
|
-
onToken: (token: string) => void;
|
|
457
|
-
onDone?: () => void;
|
|
458
|
-
onUsage?: (usage: { promptTokens: number; completionTokens: number }) => void;
|
|
459
|
-
signal?: AbortSignal;
|
|
460
|
-
},
|
|
461
|
-
): Promise<void> {
|
|
462
|
-
// Dynamic import: external.ts imports readSSE/idleTimeout from this module.
|
|
463
|
-
const { getActiveProvider, streamExternalChat } = await import("./external.js");
|
|
464
|
-
const active = getActiveProvider();
|
|
465
|
-
if (active) {
|
|
466
|
-
const wire = [
|
|
467
|
-
...(opts.history ?? []),
|
|
468
|
-
{ role: "user" as const, content: message },
|
|
469
|
-
];
|
|
470
|
-
await streamExternalChat(wire, {
|
|
471
|
-
system: opts.system,
|
|
472
|
-
onToken: opts.onToken,
|
|
473
|
-
signal: opts.signal,
|
|
474
|
-
});
|
|
475
|
-
opts.onUsage?.({
|
|
476
|
-
promptTokens: Math.max(1, Math.ceil(message.length / 4)),
|
|
477
|
-
completionTokens: 0, // external providers don't report usage here
|
|
478
|
-
});
|
|
479
|
-
opts.onDone?.();
|
|
480
|
-
return;
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
const body = {
|
|
484
|
-
agent_id: opts.agentId ?? "master",
|
|
485
|
-
message,
|
|
486
|
-
session_id: opts.sessionId,
|
|
487
|
-
};
|
|
488
|
-
try {
|
|
489
|
-
await streamChat(
|
|
490
|
-
"/api/v1/llm/chat/stream",
|
|
491
|
-
body,
|
|
492
|
-
opts.onToken,
|
|
493
|
-
opts.onDone,
|
|
494
|
-
opts.signal,
|
|
495
|
-
opts.onUsage,
|
|
496
|
-
);
|
|
497
|
-
} catch (err) {
|
|
498
|
-
const text = err instanceof Error ? err.message : String(err);
|
|
499
|
-
if (!/HTTP 400/.test(text) || !/Streaming is not supported/i.test(text)) throw err;
|
|
500
|
-
// Master (and other non-SSE agents) answer over the blocking endpoint.
|
|
501
|
-
const res = await apiCall<{
|
|
502
|
-
answer?: string;
|
|
503
|
-
usage?: { prompt_tokens?: number; completion_tokens?: number };
|
|
504
|
-
}>("/api/v1/llm/chat", {
|
|
505
|
-
method: "POST",
|
|
506
|
-
body,
|
|
507
|
-
signal: opts.signal,
|
|
508
|
-
});
|
|
509
|
-
if (!res.ok) throw new Error(res.error ?? `HTTP ${res.status}`);
|
|
510
|
-
opts.onUsage?.({
|
|
511
|
-
promptTokens: res.data?.usage?.prompt_tokens ?? 0,
|
|
512
|
-
completionTokens: res.data?.usage?.completion_tokens ?? 0,
|
|
513
|
-
});
|
|
514
|
-
opts.onToken(res.data?.answer ?? "(empty reply)");
|
|
515
|
-
opts.onDone?.();
|
|
516
|
-
}
|
|
517
|
-
}
|