llmspy 0.0.0-stage → 0.4.0

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/package.json CHANGED
@@ -1,6 +1,31 @@
1
1
  {
2
- "name": "llmspy",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
2
+ "name": "llmspy",
3
+ "version": "0.4.0",
4
+ "description": "Local developer tool for intercepting and displaying LLM API traffic",
5
+ "license": "MIT",
6
+ "author": "piyook",
7
+ "homepage": "https://github.com/piyook/llmspy#readme",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/piyook/llmspy.git"
11
+ },
12
+ "bugs": {
13
+ "url": "https://github.com/piyook/llmspy/issues"
14
+ },
15
+ "keywords": [
16
+ "llm",
17
+ "proxy",
18
+ "anthropic",
19
+ "openai",
20
+ "claude",
21
+ "debugging",
22
+ "traffic",
23
+ "devtools"
24
+ ],
25
+ "engines": {
26
+ "node": ">=26"
27
+ },
28
+ "bin": {
29
+ "llmspy": "proxy/dist/cli.js"
30
+ }
31
+ }
@@ -0,0 +1,230 @@
1
+ import { randomUUID } from 'node:crypto';
2
+ import * as http from 'node:http';
3
+ import * as https from 'node:https';
4
+ import * as zlib from 'node:zlib';
5
+ import { isEventStream } from './parsers/sse.js';
6
+ /** Headers that apply to one connection and must not be forwarded (RFC 9110 7.6.1). */
7
+ const HOP_BY_HOP = new Set([
8
+ 'connection',
9
+ 'keep-alive',
10
+ 'proxy-authenticate',
11
+ 'proxy-authorization',
12
+ 'proxy-connection',
13
+ 'te',
14
+ 'trailer',
15
+ 'transfer-encoding',
16
+ 'upgrade',
17
+ ]);
18
+ const SECRET_HEADERS = new Set(['authorization', 'x-api-key']);
19
+ export function stripHopByHop(headers) {
20
+ const listed = String(headers.connection ?? '')
21
+ .split(',')
22
+ .map((token) => token.trim().toLowerCase())
23
+ .filter(Boolean);
24
+ const result = {};
25
+ for (const [name, value] of Object.entries(headers)) {
26
+ if (value === undefined)
27
+ continue;
28
+ if (HOP_BY_HOP.has(name) || listed.includes(name))
29
+ continue;
30
+ result[name] = value;
31
+ }
32
+ return result;
33
+ }
34
+ /**
35
+ * Masks a secret, keeping an auth scheme such as `Bearer ` and the last
36
+ * four characters when the secret is long enough for that to be safe.
37
+ */
38
+ export function maskSecret(value) {
39
+ const space = value.indexOf(' ');
40
+ const scheme = space > 0 ? value.slice(0, space + 1) : '';
41
+ const secret = value.slice(scheme.length);
42
+ const tail = secret.length > 8 ? secret.slice(-4) : '';
43
+ return `${scheme}••••${tail}`;
44
+ }
45
+ export function maskHeaders(headers) {
46
+ const result = {};
47
+ for (const [name, value] of Object.entries(headers)) {
48
+ if (!SECRET_HEADERS.has(name)) {
49
+ result[name] = value;
50
+ }
51
+ else {
52
+ result[name] = Array.isArray(value)
53
+ ? value.map(maskSecret)
54
+ : maskSecret(value);
55
+ }
56
+ }
57
+ return result;
58
+ }
59
+ function decompress(data, encoding) {
60
+ // Flush rather than fail at the end, so a stream cut short still shows
61
+ // what arrived.
62
+ switch (encoding) {
63
+ case 'gzip':
64
+ case 'x-gzip':
65
+ return zlib.gunzipSync(data, {
66
+ finishFlush: zlib.constants.Z_SYNC_FLUSH,
67
+ });
68
+ case 'deflate':
69
+ return zlib.inflateSync(data, {
70
+ finishFlush: zlib.constants.Z_SYNC_FLUSH,
71
+ });
72
+ case 'br':
73
+ return zlib.brotliDecompressSync(data, {
74
+ finishFlush: zlib.constants.BROTLI_OPERATION_FLUSH,
75
+ });
76
+ case 'zstd':
77
+ return zlib.zstdDecompressSync(data);
78
+ default:
79
+ throw new Error(`unsupported content-encoding "${encoding}"`);
80
+ }
81
+ }
82
+ /** Decompresses a body per its `content-encoding` and decodes it as UTF-8. */
83
+ export function decodeBody(raw, contentEncoding) {
84
+ if (raw.length === 0)
85
+ return '';
86
+ const encodings = String(contentEncoding ?? '')
87
+ .split(',')
88
+ .map((encoding) => encoding.trim().toLowerCase())
89
+ .filter((encoding) => encoding && encoding !== 'identity');
90
+ try {
91
+ let data = raw;
92
+ // Encodings are listed in the order they were applied; undo them in reverse.
93
+ for (const encoding of encodings.reverse()) {
94
+ data = decompress(data, encoding);
95
+ }
96
+ return data.toString('utf8');
97
+ }
98
+ catch (error) {
99
+ return `[LLMSpy could not decode this body: ${error.message}]`;
100
+ }
101
+ }
102
+ /** How often, at most, a streamed reply is passed on while it arrives. */
103
+ const LIVE_INTERVAL_MS = 500;
104
+ /**
105
+ * Whether the reply so far is worth passing on while it arrives: only an
106
+ * uncompressed event stream. A JSON body cut short can not be read, and a
107
+ * compressed one would have to be decompressed from the start each time.
108
+ */
109
+ export function isLiveStream(headers) {
110
+ const encoding = String(headers['content-encoding'] ?? '')
111
+ .trim()
112
+ .toLowerCase();
113
+ return (isEventStream(headers['content-type']) &&
114
+ (encoding === '' || encoding === 'identity'));
115
+ }
116
+ /**
117
+ * Forwards one request to `target` and streams the response back unchanged,
118
+ * chunk by chunk, keeping a copy of both bodies. Each handler gets the same
119
+ * record, updated as the exchange goes on.
120
+ *
121
+ * It does not care how the request arrived, so any front end (base-URL
122
+ * routes now, an intercepting proxy later) can hand requests to it.
123
+ */
124
+ export function forwardAndCapture(req, res, { project, provider, target }, { onUpdate, onDone }) {
125
+ const start = Date.now();
126
+ const captured = {
127
+ id: randomUUID(),
128
+ timestamp: new Date(start).toISOString(),
129
+ project,
130
+ provider,
131
+ host: target.host,
132
+ method: req.method ?? 'GET',
133
+ path: target.pathname + target.search,
134
+ headers: maskHeaders(stripHopByHop(req.headers)),
135
+ body: '',
136
+ timings: { start },
137
+ };
138
+ const requestChunks = [];
139
+ const responseChunks = [];
140
+ let upstreamRes;
141
+ let finished = false;
142
+ let liveTimer;
143
+ const finish = (error) => {
144
+ if (finished)
145
+ return;
146
+ finished = true;
147
+ clearTimeout(liveTimer);
148
+ const end = Date.now();
149
+ captured.timings.end = end;
150
+ captured.latency = end - start;
151
+ captured.body = decodeBody(Buffer.concat(requestChunks), req.headers['content-encoding']);
152
+ if (upstreamRes) {
153
+ captured.response = {
154
+ status: upstreamRes.statusCode ?? 0,
155
+ headers: stripHopByHop(upstreamRes.headers),
156
+ body: decodeBody(Buffer.concat(responseChunks), upstreamRes.headers['content-encoding']),
157
+ };
158
+ }
159
+ if (error)
160
+ captured.error = error;
161
+ onDone(captured);
162
+ };
163
+ const headers = stripHopByHop(req.headers);
164
+ headers.host = target.host;
165
+ const transport = target.protocol === 'https:' ? https : http;
166
+ const upstreamReq = transport.request(target, {
167
+ method: req.method,
168
+ headers,
169
+ });
170
+ req.on('data', (chunk) => requestChunks.push(chunk));
171
+ req.on('end', () => {
172
+ if (finished)
173
+ return;
174
+ captured.body = decodeBody(Buffer.concat(requestChunks), req.headers['content-encoding']);
175
+ onUpdate(captured);
176
+ });
177
+ req.pipe(upstreamReq);
178
+ upstreamReq.on('response', (response) => {
179
+ upstreamRes = response;
180
+ captured.timings.firstByte = Date.now();
181
+ res.writeHead(response.statusCode ?? 502, stripHopByHop(response.headers));
182
+ const live = {
183
+ status: response.statusCode ?? 0,
184
+ headers: stripHopByHop(response.headers),
185
+ body: '',
186
+ };
187
+ captured.response = live;
188
+ onUpdate(captured);
189
+ // The reply so far, on a timer, so a chunk is passed through to the
190
+ // client before any of this work is done.
191
+ const sendLive = () => {
192
+ liveTimer = undefined;
193
+ if (finished)
194
+ return;
195
+ live.body = Buffer.concat(responseChunks).toString('utf8');
196
+ onUpdate(captured);
197
+ };
198
+ const isLive = isLiveStream(response.headers);
199
+ response.on('data', (chunk) => {
200
+ responseChunks.push(chunk);
201
+ if (!isLive)
202
+ return;
203
+ captured.timings.lastData = Date.now();
204
+ if (!liveTimer)
205
+ liveTimer = setTimeout(sendLive, LIVE_INTERVAL_MS);
206
+ });
207
+ response.on('error', (error) => {
208
+ finish(`Upstream response failed: ${error.message}`);
209
+ res.destroy();
210
+ });
211
+ response.pipe(res);
212
+ });
213
+ upstreamReq.on('error', (error) => {
214
+ finish(`Upstream request failed: ${error.message}`);
215
+ if (res.headersSent) {
216
+ res.destroy();
217
+ }
218
+ else {
219
+ res.writeHead(502, { 'content-type': 'text/plain; charset=utf-8' });
220
+ res.end(`LLMSpy could not reach ${target.origin}: ${error.message}`);
221
+ }
222
+ });
223
+ res.on('finish', () => finish());
224
+ res.on('close', () => {
225
+ if (res.writableFinished)
226
+ return;
227
+ finish('Client closed the connection before the response finished');
228
+ upstreamReq.destroy();
229
+ });
230
+ }
@@ -0,0 +1,42 @@
1
+ #!/usr/bin/env node
2
+ // The `llmspy` command. The Node version is checked before anything that
3
+ // needs a recent Node is loaded, so an old one gets a clear message rather
4
+ // than a stack trace.
5
+ import { helpText, nodeVersionError, parseCommand } from './command.js';
6
+ import { readVersion } from './version.js';
7
+ const tooOld = nodeVersionError(process.versions.node);
8
+ if (tooOld) {
9
+ console.error(tooOld);
10
+ process.exit(1);
11
+ }
12
+ const args = process.argv.slice(2);
13
+ switch (parseCommand(args)) {
14
+ case 'start': {
15
+ const { ENV_FILE, loadEnvFile } = await import('./envfile.js');
16
+ try {
17
+ if (loadEnvFile())
18
+ console.log(`Settings read from ${ENV_FILE}`);
19
+ }
20
+ catch (error) {
21
+ console.error(`Could not read ${ENV_FILE}: ${error.message}`);
22
+ process.exit(1);
23
+ }
24
+ await import('./index.js');
25
+ break;
26
+ }
27
+ case 'stop': {
28
+ const { stop } = await import('./stop.js');
29
+ process.exitCode = await stop();
30
+ break;
31
+ }
32
+ case 'help':
33
+ console.log(helpText(readVersion()));
34
+ break;
35
+ case 'version':
36
+ console.log(readVersion());
37
+ break;
38
+ default:
39
+ console.error(`Unknown command: ${args.join(' ')}\n`);
40
+ console.error(helpText(readVersion()));
41
+ process.exitCode = 1;
42
+ }
@@ -0,0 +1,50 @@
1
+ // What the `llmspy` command was asked to do. Kept apart from cli.ts so it can
2
+ // be tested, and free of anything newer than old Node versions can load, as
3
+ // the CLI runs it before the version check has passed.
4
+ /** The oldest Node major LLMSpy runs on: node:sqlite loads with no warning from 26. */
5
+ const MIN_NODE_MAJOR = 26;
6
+ /** The command in `args` (argv after the script), or undefined if it is not one. */
7
+ export function parseCommand(args) {
8
+ if (args.length === 0)
9
+ return 'start';
10
+ if (args.length > 1)
11
+ return undefined;
12
+ switch (args[0]) {
13
+ case 'stop':
14
+ return 'stop';
15
+ case 'help':
16
+ case '--help':
17
+ case '-h':
18
+ return 'help';
19
+ case '--version':
20
+ case '-v':
21
+ return 'version';
22
+ default:
23
+ return undefined;
24
+ }
25
+ }
26
+ /** Why LLMSpy can't run on this Node `version`, or undefined if it can. */
27
+ export function nodeVersionError(version) {
28
+ const major = Number.parseInt(version, 10);
29
+ if (major >= MIN_NODE_MAJOR)
30
+ return undefined;
31
+ return `LLMSpy needs Node.js ${MIN_NODE_MAJOR} or later; this is Node.js ${version}.`;
32
+ }
33
+ export function helpText(version) {
34
+ return `LLMSpy ${version}: shows LLM API traffic live, in a web UI on your machine.
35
+
36
+ Usage:
37
+ llmspy start LLMSpy in the foreground (Ctrl+C stops it)
38
+ llmspy stop stop the LLMSpy running on this machine
39
+ llmspy --version print the version
40
+ llmspy --help show this
41
+
42
+ Settings are environment variables, set in the shell or in ~/.llmspy/.env
43
+ (the shell wins):
44
+ PROXY_PORT port for the proxy and the UI (8080)
45
+ LLMSPY_HOST interface to listen on (127.0.0.1)
46
+ LLMSPY_UPSTREAMS extra upstreams, as name=url,...
47
+ LLMSPY_RETENTION_DAYS days to keep captured calls, 0 for ever (7)
48
+
49
+ Docs: https://github.com/piyook/llmspy`;
50
+ }
@@ -0,0 +1,102 @@
1
+ const HEADINGS = {
2
+ user: 'User',
3
+ assistant: 'Assistant',
4
+ tool: 'Tool',
5
+ };
6
+ /**
7
+ * One call's conversation as Markdown, in full, for an agent to read: the
8
+ * system prompt, tools, messages sent and the reply. A body that could not
9
+ * be read is named under Error, with whatever else was read still shown.
10
+ * `undefined` when the call was not read as an LLM API call.
11
+ */
12
+ export function toMarkdown(captured) {
13
+ const { llm } = captured;
14
+ if (!llm)
15
+ return undefined;
16
+ const sections = [header(captured, llm)];
17
+ if (llm.system)
18
+ sections.push(`## System\n\n${llm.system}`);
19
+ if (llm.tools?.length) {
20
+ const lines = llm.tools.map(({ name, description }) => description ? `- \`${name}\`: ${description}` : `- \`${name}\``);
21
+ sections.push(`## Tools\n\n${lines.join('\n')}`);
22
+ }
23
+ for (const message of llm.messages) {
24
+ sections.push(messageSection(HEADINGS[message.role], message.parts));
25
+ }
26
+ if (llm.output) {
27
+ sections.push(messageSection('Assistant (reply)', llm.output.parts));
28
+ }
29
+ const errors = errorLines(captured, llm);
30
+ if (errors.length)
31
+ sections.push(`## Error\n\n${errors.join('\n\n')}`);
32
+ return `${sections.join('\n\n')}\n`;
33
+ }
34
+ function header(captured, llm) {
35
+ const facts = [captured.timestamp, `project ${captured.project}`];
36
+ facts.push(status(captured));
37
+ if (captured.latency !== undefined)
38
+ facts.push(`${captured.latency} ms`);
39
+ if (llm.usage) {
40
+ const { input = '?', output = '?' } = llm.usage;
41
+ facts.push(`${input} in / ${output} out tokens`);
42
+ }
43
+ if (llm.stopReason)
44
+ facts.push(`stop ${llm.stopReason}`);
45
+ return `# ${llm.model ?? 'unknown model'} · ${llm.format}\n\n${facts.join(' · ')}`;
46
+ }
47
+ function status(captured) {
48
+ if (captured.timings.end === undefined)
49
+ return 'running';
50
+ if (captured.response)
51
+ return `status ${captured.response.status}`;
52
+ return 'no response';
53
+ }
54
+ function errorLines(captured, llm) {
55
+ const lines = [];
56
+ if (llm.error) {
57
+ const type = llm.error.type ? `${llm.error.type}: ` : '';
58
+ lines.push(`${type}${llm.error.message}`);
59
+ }
60
+ if (captured.error)
61
+ lines.push(captured.error);
62
+ if (llm.parseError)
63
+ lines.push(`Could not read ${llm.parseError}`);
64
+ return lines;
65
+ }
66
+ function messageSection(heading, parts) {
67
+ const body = parts.map(renderPart).join('\n\n');
68
+ return body ? `## ${heading}\n\n${body}` : `## ${heading}`;
69
+ }
70
+ function renderPart(part) {
71
+ switch (part.type) {
72
+ case 'text':
73
+ return part.text;
74
+ case 'thinking':
75
+ return quote(`*Thinking*\n\n${part.text}`);
76
+ case 'tool_call':
77
+ return `**Tool call** \`${part.name}\` (${part.id})\n\n${fence(json(part.input), 'json')}`;
78
+ case 'tool_result': {
79
+ const failed = part.isError ? ', error' : '';
80
+ return `**Tool result** (${part.id}${failed})\n\n${fence(part.content)}`;
81
+ }
82
+ case 'image':
83
+ return '[image]';
84
+ case 'other':
85
+ return fence(json(part.raw), 'json');
86
+ }
87
+ }
88
+ function quote(text) {
89
+ return text
90
+ .split('\n')
91
+ .map((line) => (line ? `> ${line}` : '>'))
92
+ .join('\n');
93
+ }
94
+ function json(value) {
95
+ return JSON.stringify(value, null, 2) ?? 'undefined';
96
+ }
97
+ /** A code fence longer than any run of backticks in the content, so it cannot close early. */
98
+ function fence(content, lang = '') {
99
+ const longest = Math.max(0, ...(content.match(/`+/g) ?? []).map((run) => run.length));
100
+ const ticks = '`'.repeat(Math.max(3, longest + 1));
101
+ return `${ticks}${lang}\n${content}\n${ticks}`;
102
+ }
@@ -0,0 +1,22 @@
1
+ import { homedir } from 'node:os';
2
+ import * as path from 'node:path';
3
+ /**
4
+ * Settings for the `llmspy` command, kept next to the database. Not a `.env`
5
+ * in the current folder: that is usually an app's, with its API keys.
6
+ */
7
+ export const ENV_FILE = path.join(homedir(), '.llmspy', '.env');
8
+ /**
9
+ * Reads `file` into process.env if it exists, and says whether it did. A
10
+ * variable already set (in the shell) is left as it is, so it wins.
11
+ */
12
+ export function loadEnvFile(file = ENV_FILE) {
13
+ try {
14
+ process.loadEnvFile(file);
15
+ return true;
16
+ }
17
+ catch (error) {
18
+ if (error.code === 'ENOENT')
19
+ return false;
20
+ throw error;
21
+ }
22
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Whether a call went wrong: the capture's own error (the upstream could not
3
+ * be reached, the stream broke off, LLMSpy stopped while it ran), an HTTP
4
+ * status of 400 or above, or an error the provider sent inside the reply.
5
+ * The UI imports this file too, so it imports types only.
6
+ */
7
+ export function isFailed({ error, response, llm, }) {
8
+ return (error !== undefined ||
9
+ (response?.status ?? 0) >= 400 ||
10
+ llm?.error !== undefined);
11
+ }
@@ -0,0 +1,45 @@
1
+ const LOOPBACK = new Set(['localhost', '127.0.0.1', '[::1]']);
2
+ const WILDCARD = new Set(['0.0.0.0', '::']);
3
+ /** `host` as it is written in a URL: an IPv6 address in brackets. */
4
+ export function hostForUrl(host) {
5
+ return host.includes(':') && !host.startsWith('[') ? `[${host}]` : host;
6
+ }
7
+ function parseHost(value) {
8
+ try {
9
+ return new URL(`http://${value}`);
10
+ }
11
+ catch {
12
+ return undefined;
13
+ }
14
+ }
15
+ /**
16
+ * Whether a request may use the UI's API. Captured prompts are not masked, so
17
+ * only the UI itself may read them:
18
+ * - `Host` must be a loopback name (or the address LLMSpy was told to listen
19
+ * on), which defeats DNS rebinding: a rebound page still sends its own name.
20
+ * - `Origin`, when there is one, must be the same origin as `Host`, so another
21
+ * site open in the browser cannot read or clear captures.
22
+ * Clients that are not browsers (curl, scripts, the Docker healthcheck) send
23
+ * no `Origin` and pass.
24
+ */
25
+ export function isAllowedUiRequest(headers, listenHost) {
26
+ const host = headers.host ? parseHost(headers.host) : undefined;
27
+ if (!host)
28
+ return false;
29
+ const allowed = LOOPBACK.has(host.hostname) ||
30
+ (!WILDCARD.has(listenHost) &&
31
+ host.hostname === parseHost(hostForUrl(listenHost))?.hostname);
32
+ if (!allowed)
33
+ return false;
34
+ const { origin } = headers;
35
+ if (origin === undefined)
36
+ return true;
37
+ try {
38
+ const url = new URL(origin);
39
+ return url.protocol === 'http:' && url.host === host.host;
40
+ }
41
+ catch {
42
+ // Includes `Origin: null` from sandboxed frames and file pages.
43
+ return false;
44
+ }
45
+ }
@@ -0,0 +1,95 @@
1
+ import { existsSync, mkdirSync, readFileSync } from 'node:fs';
2
+ import { homedir } from 'node:os';
3
+ import * as path from 'node:path';
4
+ import { hostForUrl } from './guard.js';
5
+ import { writePidFile } from './pidfile.js';
6
+ import { ProxyServer } from './proxy.js';
7
+ import { parseRetentionDays, RequestStore } from './store.js';
8
+ import { parseUpstreams } from './upstreams.js';
9
+ import { readVersion } from './version.js';
10
+ // Built layout: proxy/dist/index.js next to ui/dist/ (also in the image and
11
+ // the npm package).
12
+ const uiDir = path.resolve(import.meta.dirname, '../../ui/dist');
13
+ // llms.txt is at the root of the repo, the image and the npm package.
14
+ const llmsTxtFile = path.resolve(import.meta.dirname, '../../llms.txt');
15
+ const dbFile = path.join(homedir(), '.llmspy', 'llmspy.db');
16
+ const HOUR_MS = 60 * 60 * 1000;
17
+ /** The text of llms.txt, or undefined when it is not there (then `/llms.txt` is a 404). */
18
+ function readLlmsTxt() {
19
+ try {
20
+ return readFileSync(llmsTxtFile, 'utf8');
21
+ }
22
+ catch (error) {
23
+ if (error.code === 'ENOENT')
24
+ return undefined;
25
+ throw error;
26
+ }
27
+ }
28
+ let config;
29
+ let store;
30
+ let retentionDays;
31
+ try {
32
+ config = {
33
+ port: Number.parseInt(process.env.PROXY_PORT ?? '8080', 10),
34
+ host: process.env.LLMSPY_HOST ?? '127.0.0.1',
35
+ upstreams: parseUpstreams(process.env.LLMSPY_UPSTREAMS),
36
+ uiDir,
37
+ llmsTxt: readLlmsTxt(),
38
+ version: readVersion(),
39
+ };
40
+ retentionDays = parseRetentionDays(process.env.LLMSPY_RETENTION_DAYS);
41
+ mkdirSync(path.dirname(dbFile), { recursive: true });
42
+ store = new RequestStore({ path: dbFile, retentionDays });
43
+ }
44
+ catch (error) {
45
+ console.error(error.message);
46
+ process.exit(1);
47
+ }
48
+ const keptFor = retentionDays === 0
49
+ ? 'until cleared'
50
+ : `${retentionDays} day${retentionDays === 1 ? '' : 's'}`;
51
+ store.prune();
52
+ setInterval(() => store.prune(), HOUR_MS).unref();
53
+ const proxy = new ProxyServer(config, store);
54
+ async function start() {
55
+ try {
56
+ const port = await proxy.start();
57
+ // In a container LLMSpy listens on all interfaces, but it is reached
58
+ // through the port published on the host's loopback.
59
+ const shownHost = ['0.0.0.0', '::'].includes(config.host)
60
+ ? '127.0.0.1'
61
+ : config.host;
62
+ const base = `http://${hostForUrl(shownHost)}:${port}`;
63
+ // Written only once the port is ours, so a second start that fails leaves the first one's record.
64
+ writePidFile(base);
65
+ const width = Math.max(...[...config.upstreams.keys()].map((n) => n.length));
66
+ console.log(`LLMSpy ${config.version} listening on ${hostForUrl(config.host)}:${port}`);
67
+ console.log(`Calls kept ${keptFor} in ${dbFile}`);
68
+ if (existsSync(path.join(uiDir, 'index.html'))) {
69
+ console.log(`UI: ${base}/\n`);
70
+ }
71
+ else {
72
+ console.log('UI: not built here (npm run build); `npm run dev` serves it on http://localhost:3000\n');
73
+ }
74
+ console.log('Point an app at one of these base URLs:');
75
+ for (const [name, url] of config.upstreams) {
76
+ console.log(` ${name.padEnd(width)} ${base}/${name} -> ${url.href}`);
77
+ }
78
+ console.log('\nFor example:');
79
+ console.log(` ANTHROPIC_BASE_URL=${base}/anthropic`);
80
+ console.log(` OPENAI_BASE_URL=${base}/openai/v1`);
81
+ }
82
+ catch (error) {
83
+ console.error('Failed to start LLMSpy:', error);
84
+ process.exit(1);
85
+ }
86
+ }
87
+ // SIGTERM is what `docker stop` sends; as PID 1, Node would otherwise ignore it.
88
+ for (const signal of ['SIGINT', 'SIGTERM']) {
89
+ process.on(signal, async () => {
90
+ await proxy.stop();
91
+ store.close();
92
+ process.exit(0);
93
+ });
94
+ }
95
+ start();