llmspy 0.3.0 → 0.5.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/README.md CHANGED
@@ -110,7 +110,7 @@ With **Node.js 26 or later** (`node --version`), from any folder:
110
110
  npx llmspy
111
111
  ```
112
112
 
113
- This serves the proxy and the UI on `http://127.0.0.1:8080`, in the foreground. Stop it with Ctrl+C, or with `npx llmspy stop` from any terminal. To keep the `llmspy` command to hand, install it once with `npm install -g llmspy`; `llmspy --help` lists its commands and settings. `npx` runs the version it has cached, and `npx llmspy@latest` the newest.
113
+ This serves the proxy and the UI on `http://127.0.0.1:8080`, in the foreground. Stop it with Ctrl+C, or with `npx llmspy stop` from any terminal. To run it in the background instead, use `npx llmspy --background` (`-b`): it returns once LLMSpy is ready, its output goes to `~/.llmspy/llmspy.log`, and `npx llmspy stop` stops it. To keep the `llmspy` command to hand, install it once with `npm install -g llmspy`; `llmspy --help` lists its commands and settings. `npx` runs the version it has cached, and `npx llmspy@latest` the newest.
114
114
 
115
115
  With Docker, from a clone of the repo:
116
116
 
@@ -150,7 +150,7 @@ claude
150
150
 
151
151
  Each request then appears in the UI at `http://127.0.0.1:8080`. The app keeps its own API key, which LLMSpy passes on to the provider.
152
152
 
153
- For coding agents and scripts, [llms.txt](llms.txt) is a compact reference: how to run LLMSpy, point an app at it, read the captured traffic as JSON or one call as Markdown, and wait for a call that is still running.
153
+ For coding agents and scripts, [llms.txt](llms.txt) is a compact reference: how to run LLMSpy, point an app at it, read the captured traffic as JSON or one call as Markdown, and wait for a call that is still running. A running LLMSpy serves it too, for its own version, at `http://127.0.0.1:8080/llms.txt`.
154
154
 
155
155
  ### Keep apps apart with projects
156
156
 
@@ -171,6 +171,7 @@ The `llmspy` command, from npm (`npx llmspy`, or `llmspy` once installed with `n
171
171
  | Command | What it does |
172
172
  | ------------------ | ------------------------------------------------------------------- |
173
173
  | `llmspy` | Start LLMSpy in the foreground |
174
+ | `llmspy -b` | Start LLMSpy in the background (`--background`) |
174
175
  | `llmspy stop` | Stop LLMSpy started with `llmspy` or `npm start`, from any terminal |
175
176
  | `llmspy --help` | List the commands and the settings |
176
177
  | `llmspy --version` | Print the version |
package/llms.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  # LLMSpy
2
2
 
3
- > A local reverse proxy that records the LLM API calls an app makes and shows each one in a web UI: the prompt, the system message, the tools, the reply and the token counts. An app is pointed at it by its base URL setting (`ANTHROPIC_BASE_URL`, `OPENAI_BASE_URL`); requests are forwarded unchanged and a copy is kept. This file is the reference for AI agents and scripts; it ships in the repository and in the npm package `llmspy`, and `README.md` is the human guide.
3
+ > A local reverse proxy that records the LLM API calls an app makes and shows each one in a web UI: the prompt, the system message, the tools, the reply and the token counts. An app is pointed at it by its base URL setting (`ANTHROPIC_BASE_URL`, `OPENAI_BASE_URL`); requests are forwarded unchanged and a copy is kept. This file is the reference for AI agents and scripts; it ships in the repository and in the npm package `llmspy`, a running LLMSpy serves it at `/llms.txt` (`http://127.0.0.1:8080/llms.txt` by default) for the version that is running, and `README.md` is the human guide.
4
4
 
5
5
  ## What it does and does not do
6
6
 
@@ -18,14 +18,16 @@ Running without Docker needs Node.js 26 or later (`engines` in `package.json`);
18
18
 
19
19
  | How | Start | Stop |
20
20
  |---|---|---|
21
- | npm package | `npx --yes llmspy` (or `llmspy`, after `npm install -g llmspy`) | `npx --yes llmspy stop` (`llmspy stop`), from any terminal |
21
+ | npm package, in the background (for agents and scripts) | `npx --yes llmspy --background` (or `llmspy --background`, after `npm install -g llmspy`) | `npx --yes llmspy stop` (`llmspy stop`), from any terminal |
22
+ | npm package, in the terminal | `npx --yes llmspy` (`llmspy`) | Ctrl+C, or `llmspy stop` from any terminal |
22
23
  | Docker (runs in the background, restarts with Docker) | `npm run start:docker` (`docker compose up -d --build`) | `npm run stop:docker` (`docker compose down`) |
23
24
  | Built, local | `npm run build`, then `npm start` | `npm run stop`, from any terminal |
24
25
  | Development, with reload | `npm run dev` (UI also on `http://localhost:3000`) | Ctrl+C in its terminal |
25
26
 
26
- - Ready when `GET http://127.0.0.1:8080/ping` answers `200`. `llmspy`, `npm start` and `npm run dev` stay in the foreground: run them in the background and poll `/ping`. Pass `--yes` to `npx`, or the first run waits for an answer to "Ok to proceed?" before installing the package, which blocks a run in the background.
27
+ - `llmspy --background` (`-b`) returns once LLMSpy is listening: it prints the same lines a foreground start does (the UI address and the base URLs), the pid, the log file and the command that stops it, and exits `0`. The background LLMSpy writes its output to `~/.llmspy/llmspy.log`; a start that succeeds moves the last log to `~/.llmspy/llmspy.log.old`, and one that fails leaves its output in `~/.llmspy/llmspy.log.new`. If an LLMSpy started with `llmspy`, `npm start` or `npm run dev` is already running (its pid file names a process that answers `/ping`), it says so and exits `0` without starting another. Anything else on the port, Docker's LLMSpy included, makes the start fail: if LLMSpy exits while starting (a port in use, a bad setting), or is not listening within 60 seconds (then it is stopped), it prints the end of the log and exits `1`.
28
+ - `llmspy` with no argument, `npm start` and `npm run dev` stay in the foreground; ready when `GET http://127.0.0.1:8080/ping` answers `200`. To run one of them from an agent, run it in the background and poll `/ping`. Pass `--yes` to `npx`, or the first run waits for an answer to "Ok to proceed?" before installing the package, which blocks a run in the background.
27
29
  - All of them use port 8080. Stop one before starting another; a second start on a taken port fails with `EADDRINUSE` and exits `1`.
28
- - `llmspy`, `npm start` and `npm run dev` write `~/.llmspy/llmspy.pid` (`{"pid", "url"}`) once they listen. `llmspy stop` (in a clone, `npm run stop` runs the same) stops that process only if `/ping` at that URL reports the same pid, so a stale or unreadable file is removed and nothing else is killed. It exits `0` when LLMSpy is stopped or was not running, and `1` when it did not stop within 5 seconds or the pid file could not be read. It does not stop Docker. For `npm run dev` it stops the proxy, but the watcher starts it again on the next file change: stop `npm run dev` with Ctrl+C.
30
+ - `llmspy` (also in the background), `npm start` and `npm run dev` write `~/.llmspy/llmspy.pid` (`{"pid", "url"}`) once they listen. `llmspy stop` (in a clone, `npm run stop` runs the same) stops that process only if `/ping` at that URL reports the same pid, so a stale or unreadable file is removed and nothing else is killed. It exits `0` when LLMSpy is stopped or was not running, and `1` when it did not stop within 5 seconds or the pid file could not be read. It does not stop Docker. For `npm run dev` it stops the proxy, but the watcher starts it again on the next file change: stop `npm run dev` with Ctrl+C.
29
31
  - `llmspy --help` lists the commands and settings, `llmspy --version` prints the version. Any other argument exits `1`; on Node.js older than 26 `llmspy` says so and exits `1`.
30
32
  - On start LLMSpy prints the database file and how long calls are kept (`Calls kept 7 days in …`), then the base URL of each upstream. A bad `LLMSPY_UPSTREAMS` or `LLMSPY_RETENTION_DAYS` prints the reason and exits `1`.
31
33
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "llmspy",
3
- "version": "0.3.0",
3
+ "version": "0.5.0",
4
4
  "description": "Local developer tool for intercepting and displaying LLM API traffic",
5
5
  "license": "MIT",
6
6
  "author": "piyook",
@@ -0,0 +1,197 @@
1
+ // `llmspy --background`: starts LLMSpy in the background and returns once it
2
+ // is listening. The background process is a plain `llmspy`, with its output in
3
+ // a log file; it loads ~/.llmspy/.env itself, so what it says about that is in
4
+ // the log too. It says it is listening with a message on an IPC channel
5
+ // (ready.ts), so readiness does not depend on the pid file, which LLMSpy may
6
+ // fail to write. `llmspy stop` stops it, as it would a foreground one.
7
+ import { spawn } from 'node:child_process';
8
+ import { closeSync, mkdirSync, openSync, readFileSync, renameSync, } from 'node:fs';
9
+ import { homedir } from 'node:os';
10
+ import * as path from 'node:path';
11
+ import { checkPidFile, PID_FILE } from './pidfile.js';
12
+ import { BACKGROUND_ENV, READY } from './ready.js';
13
+ /** The background LLMSpy's output. */
14
+ const LOG_FILE = path.join(homedir(), '.llmspy', 'llmspy.log');
15
+ /**
16
+ * A new run writes here, and the file becomes LOG_FILE (the last one moving
17
+ * to LOG_FILE.old) only once it is listening: a start that fails leaves the
18
+ * log of an LLMSpy that may still be running alone.
19
+ */
20
+ const NEW_LOG_FILE = `${LOG_FILE}.new`;
21
+ /** Generous: startup prunes and checks the database before it listens. */
22
+ const TIMEOUT_MS = 60_000;
23
+ /** How long a process that timed out gets to end once it is told to. */
24
+ const STOP_MS = 3000;
25
+ /** Lines of the log shown when the background LLMSpy did not start. */
26
+ const TAIL_LINES = 20;
27
+ /** The Node flags passed on to the background process: loaders, such as tsx's. */
28
+ const LOADER_FLAGS = new Set([
29
+ '--import',
30
+ '--require',
31
+ '-r',
32
+ '--loader',
33
+ '--experimental-loader',
34
+ ]);
35
+ /** Waits for `child` to say it is ready, to exit, to fail to start, or for `timeoutMs`. */
36
+ export function waitUntilReady(child, timeoutMs = TIMEOUT_MS) {
37
+ return new Promise((resolve) => {
38
+ const done = (outcome) => {
39
+ clearTimeout(timer);
40
+ child.off('message', onMessage);
41
+ child.off('exit', onExit);
42
+ child.off('error', onError);
43
+ resolve(outcome);
44
+ };
45
+ const onMessage = (message) => {
46
+ if (message?.type === READY)
47
+ done({ kind: 'ready' });
48
+ };
49
+ const onExit = () => done({ kind: 'exited' });
50
+ const onError = (error) => done({ kind: 'failed', error });
51
+ const timer = setTimeout(() => done({ kind: 'timeout' }), timeoutMs);
52
+ child.on('message', onMessage);
53
+ child.on('exit', onExit);
54
+ child.on('error', onError);
55
+ });
56
+ }
57
+ /** Resolves true once `child` has exited, false if it has not within `ms`. */
58
+ export function waitForExit(child, ms = STOP_MS) {
59
+ if (child.exitCode !== null || child.signalCode !== null)
60
+ return Promise.resolve(true);
61
+ return new Promise((resolve) => {
62
+ const onExit = () => {
63
+ clearTimeout(timer);
64
+ resolve(true);
65
+ };
66
+ const timer = setTimeout(() => {
67
+ child.off('exit', onExit);
68
+ resolve(false);
69
+ }, ms);
70
+ child.once('exit', onExit);
71
+ });
72
+ }
73
+ /** The loader flags in `execArgv` (`--import x` or `--import=x`), and nothing else, such as `--inspect`. */
74
+ export function loaderArgs(execArgv) {
75
+ const kept = [];
76
+ for (let i = 0; i < execArgv.length; i++) {
77
+ const arg = execArgv[i];
78
+ const [flag] = arg.split('=', 1);
79
+ if (!LOADER_FLAGS.has(flag))
80
+ continue;
81
+ if (arg.includes('='))
82
+ kept.push(arg);
83
+ else if (i + 1 < execArgv.length)
84
+ kept.push(arg, execArgv[++i]);
85
+ }
86
+ return kept;
87
+ }
88
+ /** The command that stops LLMSpy, as this one was run: npm sets npm_command for npx. */
89
+ export function stopCommand(env = process.env) {
90
+ return env.npm_command === 'exec' ? 'npx llmspy stop' : 'llmspy stop';
91
+ }
92
+ /** The new run's log becomes LOG_FILE; resolves with where its output now is. */
93
+ function keepNewLog() {
94
+ try {
95
+ renameSync(LOG_FILE, `${LOG_FILE}.old`);
96
+ }
97
+ catch {
98
+ // No log yet, or it can't be moved: the new one replaces it below.
99
+ }
100
+ try {
101
+ renameSync(NEW_LOG_FILE, LOG_FILE);
102
+ return LOG_FILE;
103
+ }
104
+ catch {
105
+ return NEW_LOG_FILE;
106
+ }
107
+ }
108
+ function readLog(file) {
109
+ try {
110
+ return readFileSync(file, 'utf8');
111
+ }
112
+ catch {
113
+ return '';
114
+ }
115
+ }
116
+ /** Starts LLMSpy in the background, and resolves with the exit code. */
117
+ export async function startInBackground() {
118
+ let state;
119
+ try {
120
+ state = await checkPidFile();
121
+ }
122
+ catch (error) {
123
+ console.error(`Could not read ${PID_FILE}: ${error.message}`);
124
+ return 1;
125
+ }
126
+ if (state.kind === 'running') {
127
+ console.log(`LLMSpy is already running at ${state.url} (pid ${state.pid}).`);
128
+ return 0;
129
+ }
130
+ const { outcome, pid } = await launch();
131
+ if (outcome.kind === 'ready') {
132
+ const log = keepNewLog();
133
+ process.stdout.write(readLog(log));
134
+ console.log(`\nRunning in the background (pid ${pid}). Log: ${log}\nStop it with: ${stopCommand()}`);
135
+ return 0;
136
+ }
137
+ return reportFailure(outcome, pid);
138
+ }
139
+ /** Starts a plain `llmspy` detached, its output in NEW_LOG_FILE, and waits for it. */
140
+ async function launch() {
141
+ mkdirSync(path.dirname(LOG_FILE), { recursive: true });
142
+ const log = openSync(NEW_LOG_FILE, 'w');
143
+ let child;
144
+ try {
145
+ child = spawn(process.execPath, [...loaderArgs(process.execArgv), process.argv[1]], {
146
+ detached: true,
147
+ stdio: ['ignore', log, log, 'ipc'],
148
+ windowsHide: true,
149
+ env: { ...process.env, [BACKGROUND_ENV]: '1' },
150
+ });
151
+ }
152
+ finally {
153
+ closeSync(log);
154
+ }
155
+ // Kept for the whole launch: a kill or disconnect that fails after the
156
+ // wait must not crash the launcher.
157
+ child.on('error', () => { });
158
+ const waited = await waitUntilReady(child);
159
+ let outcome;
160
+ if (waited.kind === 'timeout') {
161
+ child.kill();
162
+ outcome = { kind: 'timeout', stopped: await waitForExit(child) };
163
+ }
164
+ else {
165
+ outcome = waited;
166
+ }
167
+ if (child.connected)
168
+ child.disconnect();
169
+ child.unref();
170
+ return { outcome, pid: child.pid };
171
+ }
172
+ /** Why the background LLMSpy (`pid`) did not start. */
173
+ export function failureMessage(outcome, pid) {
174
+ const within = `LLMSpy did not start within ${TIMEOUT_MS / 1000} s`;
175
+ switch (outcome.kind) {
176
+ case 'failed':
177
+ return `Could not start LLMSpy: ${outcome.error.message}`;
178
+ case 'timeout':
179
+ return outcome.stopped
180
+ ? `${within}, so it was stopped.`
181
+ : `${within} and may still be running (pid ${pid}).`;
182
+ case 'exited':
183
+ return 'LLMSpy did not start.';
184
+ }
185
+ }
186
+ /** The last `count` lines of `text`, without its trailing newline. */
187
+ export function lastLines(text, count = TAIL_LINES) {
188
+ return text.trimEnd().split('\n').slice(-count).join('\n');
189
+ }
190
+ /** Says why the background LLMSpy did not start, with the end of its log, and returns the exit code. */
191
+ function reportFailure(outcome, pid) {
192
+ console.error(failureMessage(outcome, pid));
193
+ const tail = lastLines(readLog(NEW_LOG_FILE));
194
+ if (tail)
195
+ console.error(`From ${NEW_LOG_FILE}:\n${tail}`);
196
+ return 1;
197
+ }
package/proxy/dist/cli.js CHANGED
@@ -11,6 +11,11 @@ if (tooOld) {
11
11
  }
12
12
  const args = process.argv.slice(2);
13
13
  switch (parseCommand(args)) {
14
+ case 'background': {
15
+ const { startInBackground } = await import('./background.js');
16
+ process.exitCode = await startInBackground();
17
+ break;
18
+ }
14
19
  case 'start': {
15
20
  const { ENV_FILE, loadEnvFile } = await import('./envfile.js');
16
21
  try {
@@ -10,6 +10,9 @@ export function parseCommand(args) {
10
10
  if (args.length > 1)
11
11
  return undefined;
12
12
  switch (args[0]) {
13
+ case '--background':
14
+ case '-b':
15
+ return 'background';
13
16
  case 'stop':
14
17
  return 'stop';
15
18
  case 'help':
@@ -34,10 +37,12 @@ export function helpText(version) {
34
37
  return `LLMSpy ${version}: shows LLM API traffic live, in a web UI on your machine.
35
38
 
36
39
  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
40
+ llmspy start LLMSpy in this terminal (Ctrl+C stops it)
41
+ llmspy --background start it in the background instead, with its output in
42
+ ~/.llmspy/llmspy.log; also -b
43
+ llmspy stop stop the LLMSpy running on this machine
44
+ llmspy --version print the version
45
+ llmspy --help show this
41
46
 
42
47
  Settings are environment variables, set in the shell or in ~/.llmspy/.env
43
48
  (the shell wins):
@@ -1,17 +1,31 @@
1
- import { existsSync, mkdirSync } from 'node:fs';
1
+ import { existsSync, mkdirSync, readFileSync } from 'node:fs';
2
2
  import { homedir } from 'node:os';
3
3
  import * as path from 'node:path';
4
4
  import { hostForUrl } from './guard.js';
5
5
  import { writePidFile } from './pidfile.js';
6
6
  import { ProxyServer } from './proxy.js';
7
+ import { notifyReady } from './ready.js';
7
8
  import { parseRetentionDays, RequestStore } from './store.js';
8
9
  import { parseUpstreams } from './upstreams.js';
9
10
  import { readVersion } from './version.js';
10
11
  // Built layout: proxy/dist/index.js next to ui/dist/ (also in the image and
11
12
  // the npm package).
12
13
  const uiDir = path.resolve(import.meta.dirname, '../../ui/dist');
14
+ // llms.txt is at the root of the repo, the image and the npm package.
15
+ const llmsTxtFile = path.resolve(import.meta.dirname, '../../llms.txt');
13
16
  const dbFile = path.join(homedir(), '.llmspy', 'llmspy.db');
14
17
  const HOUR_MS = 60 * 60 * 1000;
18
+ /** The text of llms.txt, or undefined when it is not there (then `/llms.txt` is a 404). */
19
+ function readLlmsTxt() {
20
+ try {
21
+ return readFileSync(llmsTxtFile, 'utf8');
22
+ }
23
+ catch (error) {
24
+ if (error.code === 'ENOENT')
25
+ return undefined;
26
+ throw error;
27
+ }
28
+ }
15
29
  let config;
16
30
  let store;
17
31
  let retentionDays;
@@ -21,6 +35,7 @@ try {
21
35
  host: process.env.LLMSPY_HOST ?? '127.0.0.1',
22
36
  upstreams: parseUpstreams(process.env.LLMSPY_UPSTREAMS),
23
37
  uiDir,
38
+ llmsTxt: readLlmsTxt(),
24
39
  version: readVersion(),
25
40
  };
26
41
  retentionDays = parseRetentionDays(process.env.LLMSPY_RETENTION_DAYS);
@@ -64,6 +79,7 @@ async function start() {
64
79
  console.log('\nFor example:');
65
80
  console.log(` ANTHROPIC_BASE_URL=${base}/anthropic`);
66
81
  console.log(` OPENAI_BASE_URL=${base}/openai/v1`);
82
+ notifyReady();
67
83
  }
68
84
  catch (error) {
69
85
  console.error('Failed to start LLMSpy:', error);
@@ -44,6 +44,30 @@ export function readPidFile(file = PID_FILE) {
44
44
  return 'invalid';
45
45
  }
46
46
  }
47
+ /** The pid that LLMSpy at `url` answers `/ping` with, or undefined if nothing does. */
48
+ export async function pingPid(url) {
49
+ try {
50
+ const res = await fetch(`${url}/ping`, {
51
+ signal: AbortSignal.timeout(2000),
52
+ });
53
+ return (await res.json()).pid;
54
+ }
55
+ catch {
56
+ return undefined;
57
+ }
58
+ }
59
+ /**
60
+ * What `file` says, checked against `/ping` at its URL: `running` only when
61
+ * LLMSpy there answers with the recorded pid, `stale` when nothing does or
62
+ * another process does. Read errors other than a missing file are thrown.
63
+ */
64
+ export async function checkPidFile(file = PID_FILE) {
65
+ const record = readPidFile(file);
66
+ if (typeof record !== 'object')
67
+ return { kind: record };
68
+ const answer = await pingPid(record.url);
69
+ return { kind: answer === record.pid ? 'running' : 'stale', ...record };
70
+ }
47
71
  /** Removes the file only if it is still this process's, so a newer LLMSpy's record is left alone. */
48
72
  export function removePidFile(file = PID_FILE) {
49
73
  try {
@@ -51,6 +51,12 @@ export class ProxyServer {
51
51
  this.handlePing(res);
52
52
  return;
53
53
  }
54
+ if (path === '/llms.txt' &&
55
+ this.config.llmsTxt !== undefined &&
56
+ (req.method === 'GET' || req.method === 'HEAD')) {
57
+ this.handleLlmsTxt(req, res, this.config.llmsTxt);
58
+ return;
59
+ }
54
60
  if (path === '/events') {
55
61
  this.handleSSE(req, res);
56
62
  return;
@@ -269,6 +275,16 @@ export class ProxyServer {
269
275
  res.writeHead(400, { 'content-type': 'text/plain; charset=utf-8' });
270
276
  res.end(`LLMSpy: ${message}`);
271
277
  }
278
+ /** `GET /llms.txt`: the reference for agents. Not guarded: it holds no captured data. */
279
+ handleLlmsTxt(req, res, text) {
280
+ res.writeHead(200, {
281
+ 'content-type': 'text/plain; charset=utf-8',
282
+ 'content-length': Buffer.byteLength(text),
283
+ 'cache-control': 'no-cache',
284
+ 'x-content-type-options': 'nosniff',
285
+ });
286
+ res.end(req.method === 'HEAD' ? undefined : text);
287
+ }
272
288
  handlePing(res) {
273
289
  res.writeHead(200, { 'Content-Type': 'application/json' });
274
290
  res.end(JSON.stringify({
@@ -0,0 +1,14 @@
1
+ // Between `llmspy --background` (background.ts) and the LLMSpy it starts: the
2
+ // background process says it is listening with one message on the IPC channel.
3
+ // Kept apart from background.ts so the server does not load the launcher.
4
+ /** Set for the background process, which then reports on the IPC channel. */
5
+ export const BACKGROUND_ENV = 'LLMSPY_BACKGROUND';
6
+ export const READY = 'llmspy:ready';
7
+ /** Run by LLMSpy once it listens: tells `llmspy --background` it is ready. */
8
+ export function notifyReady() {
9
+ if (process.env[BACKGROUND_ENV] !== '1' || !process.connected)
10
+ return;
11
+ // The launcher may have gone (Ctrl+C, a command timeout): with a callback,
12
+ // a send that fails is ignored rather than crashing LLMSpy.
13
+ process.send?.({ type: READY }, undefined, undefined, () => { });
14
+ }
@@ -2,49 +2,34 @@
2
2
  // `npm start` or `npm run dev` on this machine, found from the pid file it
3
3
  // writes on startup. Not for Docker (use `npm run stop:docker`).
4
4
  import { rmSync } from 'node:fs';
5
- import { PID_FILE, readPidFile } from './pidfile.js';
5
+ import { checkPidFile, PID_FILE, pingPid } from './pidfile.js';
6
6
  const POLL_MS = 100;
7
7
  const POLLS = 50;
8
- /** The pid that answers at `url`, or undefined if nothing does. */
9
- async function pingPid(url) {
10
- try {
11
- const res = await fetch(`${url}/ping`, {
12
- signal: AbortSignal.timeout(2000),
13
- });
14
- return (await res.json()).pid;
15
- }
16
- catch {
17
- return undefined;
18
- }
19
- }
8
+ const NOT_RUNNING = {
9
+ none: 'no pid file',
10
+ invalid: 'removed an unreadable pid file',
11
+ stale: 'removed a stale pid file',
12
+ };
20
13
  /** Stops the LLMSpy recorded in `file`, and resolves with the exit code. */
21
14
  export async function stop(file = PID_FILE) {
22
- let record;
15
+ let state;
23
16
  try {
24
- record = readPidFile(file);
17
+ state = await checkPidFile(file);
25
18
  }
26
19
  catch (error) {
27
20
  console.error(`Could not read ${file}: ${error.message}`);
28
21
  return 1;
29
22
  }
30
- if (record === 'none') {
31
- console.log('LLMSpy is not running (no pid file).');
32
- return 0;
33
- }
34
- if (record === 'invalid') {
35
- rmSync(file, { force: true });
36
- console.log('LLMSpy is not running (removed an unreadable pid file).');
37
- return 0;
38
- }
39
- const { pid, url } = record;
40
23
  // A pid file left by a process that died without cleaning up may name a
41
24
  // pid the system has since given to something else: only stop it if
42
25
  // LLMSpy at the recorded URL says it is that process.
43
- if ((await pingPid(url)) !== pid) {
44
- rmSync(file, { force: true });
45
- console.log('LLMSpy is not running (removed a stale pid file).');
26
+ if (state.kind !== 'running') {
27
+ if (state.kind !== 'none')
28
+ rmSync(file, { force: true });
29
+ console.log(`LLMSpy is not running (${NOT_RUNNING[state.kind]}).`);
46
30
  return 0;
47
31
  }
32
+ const { pid, url } = state;
48
33
  try {
49
34
  process.kill(pid, 'SIGTERM');
50
35
  }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "llmspy-proxy",
3
- "version": "0.3.0",
3
+ "version": "0.5.0",
4
4
  "private": true,
5
5
  "type": "module"
6
6
  }
@@ -18,6 +18,7 @@
18
18
  shows its number of matches.</li> <li>The chips under Today show only the calls of the last 5 to 90
19
19
  minutes.</li> <li>The red Failed chip shows only the calls that went wrong: an error,
20
20
  a status of 400 or above, or an error inside the reply.</li> <li>The cards at the top count the calls in the list: how many, how many
21
- failed, their tokens and their latency.</li> <li>API keys are masked. Prompts and replies are shown in full.</li> <li>Calls are kept in a file on your machine, so they survive a restart:
21
+ failed, their tokens and their latency.</li> <li>Coding agents and scripts can read the captured calls too; point them
22
+ at the reference: <code class="help-code"> </code></li> <li>API keys are masked. Prompts and replies are shown in full.</li> <li>Calls are kept in a file on your machine, so they survive a restart:
22
23
  the last 7 days unless <code>LLMSPY_RETENTION_DAYS</code> says
23
- otherwise. Clear All empties the project you are viewing.</li></ul> <p class="help-more">More in the <a target="_blank" rel="noreferrer">README</a>.</p></div>`);function Zs(e,t){let n=M(()=>`${location.protocol}//${t.address??`127.0.0.1:8080`}`);Js(e,{id:`help-popover`,label:`Help`,buttonClass:`icon-btn`,buttonLabel:`Help`,trigger:e=>{J(e,Ys())},children:(e,t)=>{var r=Xs(),i=z(I(r),2),a=I(i),o=z(I(a)),s=R(o),c=R(z(o,2));we(),D(a),we(4),D(i);var l=z(i,4),u=z(I(l),2),d=R(z(I(u),3));we(3),D(u),we(12),D(l);var f=z(l,2);$(z(I(f)),`href`,`https://github.com/piyook/llmspy#readme`),we(),D(f),D(r),B(()=>{Y(s,`ANTHROPIC_BASE_URL=${U(n)??``}/anthropic`),Y(c,`OPENAI_BASE_URL=${U(n)??``}/openai/v1`),Y(d,`ANTHROPIC_BASE_URL=${U(n)??``}/@my-app/anthropic`)}),J(e,r)},$$slots:{trigger:!0,default:!0}})}var Qs=[{value:`system`,label:`System`},{value:`light`,label:`Light`},{value:`dark`,label:`Dark`}],$s=`llmspy-theme`;function ec(){try{let e=localStorage.getItem($s);if(e===`light`||e===`dark`)return e}catch{}return`system`}function tc(e){let t=document.documentElement;e===`system`?delete t.dataset.theme:t.dataset.theme=e;try{e===`system`?localStorage.removeItem($s):localStorage.setItem($s,e)}catch{}}var nc=xr(`<svg viewBox="0 0 24 24" width="28" height="28" fill="none" stroke="currentColor" aria-hidden="true"><circle cx="12" cy="12" r="6.5" stroke-width="2"></circle><circle cx="12" cy="12" r="2.5" stroke-width="2"></circle><path stroke-width="3" d="M19 12h3M12 19v3M5 12H2M12 5V2M16.95 16.95l2.12 2.12M7.05 16.95l-2.12 2.12M7.05 7.05 4.93 4.93M16.95 7.05l2.12-2.12"></path></svg>`),rc=K(`<label><input type="radio" name="theme"/> </label>`),ic=K(`<p class="setting-version"> </p>`),ac=K(`<fieldset class="setting"><legend>Theme</legend> <div class="setting-options"></div></fieldset> <!>`,1);function oc(e,t){k(t,!0);let n=[],r=P(Wt(ec()));Js(e,{id:`settings-popover`,label:`Settings`,buttonClass:`icon-btn`,buttonLabel:`Settings`,trigger:e=>{J(e,nc())},children:(e,i)=>{var a=ac(),o=L(a),s=z(I(o),2);Z(s,21,()=>Qs,e=>e.value,(e,t)=>{var i=rc(),a=I(i);ri(a);var o,s=z(a);D(i),B(()=>{o!==(o=U(t).value)&&(a.value=(a.__value=o)??``),Y(s,` ${U(t).label??``}`)}),W(`change`,a,()=>tc(U(r))),li(n,[],a,()=>(U(t).value,U(r)),e=>F(r,e)),J(e,i)}),D(s),D(o);var c=z(o,2),l=e=>{var n=ic(),r=R(n);B(()=>Y(r,`LLMSpy ${t.version??``}`)),J(e,n)};X(c,e=>{t.version&&e(l)}),J(e,a)},$$slots:{trigger:!0,default:!0}}),A()}G([`change`]);var sc=xr(`<path class="wave wave-far" d="M26 4.5a10 10 0 0 1 0 15"></path><path class="wave wave-mid" d="M22.5 7a7 7 0 0 1 0 10"></path><path class="wave wave-near" d="M19.5 9.5a3.5 3.5 0 0 1 0 5"></path>`,1),cc=xr(`<line x1="3" y1="4" x2="18" y2="21"></line>`),lc=K(`<span role="status"><svg viewBox="0 0 30 24" width="45" height="36" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M4 9.5A6.5 6.5 0 0 1 17 9.5C17 14 12.5 15 12.5 18.5A3.5 3.5 0 0 1 5.5 18.5"></path><path d="M13.5 9.5A3 3 0 0 0 7.5 9.5v1a2 2 0 0 1 0 4"></path><!></svg> <span class="visually-hidden"> </span></span>`);function uc(e,t){let n=M(()=>t.connected?`Listening${t.address?` on ${t.address}`:``}`:`Not connected to the proxy`);var r=lc();let i;var a=I(r),o=z(I(a),2),s=e=>{var t=sc();we(2),J(e,t)},c=e=>{J(e,cc())};X(o,e=>{t.connected?e(s):e(c,-1)}),D(a);var l=R(z(a,2),!0);D(r),B(()=>{i=Q(r,1,`status-icon`,null,i,{connected:t.connected}),$(r,`title`,U(n)),Y(l,U(n))}),J(e,r)}var dc=K(`<p class="popover-error">Not connected to the proxy.</p>`),fc=K(`<p class="popover-error">Couldn't load the list from the proxy.</p>`),pc=K(`<p class="popover-hint">Loading...</p>`),mc=K(`<li class="upstream"><span class="upstream-name"> </span> <code class="upstream-url"> </code> <button type="button" class="small-btn"> </button> <span class="upstream-target"> </span></li>`),hc=K(`<ul class="upstream-list"></ul>`),gc=K(`<p class="popover-hint">Point an app's base URL at one of these.</p> <!>`,1);function _c(e,t){k(t,!0);let n=P(void 0),r=P(!1),i=P(void 0),a;function o(e){return`${location.protocol}//${t.address}/${e}`}async function s(){F(r,!1);try{let e=await fetch(`/api/upstreams`);if(!e.ok)throw Error(`HTTP ${e.status}`);F(n,await e.json(),!0)}catch{F(n,void 0),F(r,!0)}}async function c(e){let t=`Copied`;try{await navigator.clipboard.writeText(o(e))}catch{t=`Failed`}F(i,{name:e,label:t},!0),clearTimeout(a),a=setTimeout(()=>F(i,void 0),1500)}Js(e,{id:`upstream-popover`,label:`Base URLs`,align:`center`,onopen:s,trigger:e=>{we(),J(e,Sr(`Base URLs`))},children:(e,a)=>{var s=gc(),l=z(L(s),2),u=e=>{J(e,dc())},d=e=>{J(e,fc())},f=e=>{J(e,pc())},p=e=>{var t=hc();Z(t,21,()=>U(n),e=>e.name,(e,t)=>{var n=mc(),r=I(n),a=R(r,!0),s=z(r,2),l=R(s,!0),u=z(s,2),d=R(u,!0),f=R(z(u,2));D(n),B(e=>{Y(a,U(t).name),Y(l,e),$(u,`aria-label`,`Copy the ${U(t).name} base URL`),Y(d,U(i)?.name===U(t).name?U(i).label:`Copy`),Y(f,`to ${U(t).target??``}`)},[()=>o(U(t).name)]),W(`click`,u,()=>c(U(t).name)),J(e,n)}),D(t),J(e,t)};X(l,e=>{t.address?U(r)?e(d,1):U(n)?e(p,-1):e(f,2):e(u)}),J(e,s)},$$slots:{trigger:!0,default:!0}}),A()}G([`click`]);var vc=K(`<div class="header"><div class="title"><h1>LLMSpy</h1> <!></div> <div class="controls"><!> <input type="search" placeholder="Search requests..." aria-label="Search requests" class="search"/> <button type="button" class="clear-btn">Clear All</button> <!> <!></div></div>`);function yc(e,t){k(t,!0);let n=vi(t,`search`,15,``);var r=vc(),i=I(r);uc(z(I(i),2),{get connected(){return t.connected},get address(){return t.address}}),D(i);var a=z(i,2),o=I(a);{let e=M(()=>t.connected?t.address:void 0);_c(o,{get address(){return U(e)}})}var s=z(o,2);ri(s);var c=z(s,2),l=z(c,2);{let e=M(()=>t.connected?t.address:void 0);Zs(l,{get address(){return U(e)}})}oc(z(l,2),{get version(){return t.version}}),D(a),D(r),si(s,n),W(`click`,c,function(...e){t.onclear?.apply(this,e)}),J(e,r),A()}G([`click`]);var bc=K(`<p class="pick-hint" role="status">Click the call to compare with. <button type="button" class="small-btn">Cancel</button></p>`),xc=K(`<div class="day"><!> <!> <!></div>`),Sc=K(`<div class="no-requests">Loading…</div>`),Cc=K(`<div class="no-requests">Select a request to see it here.</div>`),wc=K(`<!> <!> <!> <div class="panes"><section aria-label="Captured requests"><!> <!> <!></section> <section class="detail" aria-label="Selected request"><!></section></div>`,1);function Tc(e,t){k(t,!0);let n=new bi,r=P(``),i=P(void 0),a=P(!1),o=P(void 0),s=P(void 0);async function c(){try{let e=await(await fetch(`/ping`)).json();typeof e.port==`number`&&F(o,`${location.hostname}:${e.port}`),typeof e.version==`string`&&F(s,e.version,!0)}catch{}}let l=M(()=>hs(n.values())),u=P(Wt(ms)),d=M(()=>U(l).includes(U(u))?U(u):ms),f=Date.now(),p=new bi,m=e=>vs(n.values(),e,p.get(e)??f);function h(e){if(e===U(d))return;let t=Date.now();p.set(U(d),t),p.set(e,t),F(u,e,!0),F(i,void 0),F(y,void 0),F(b,!1)}let g=M(()=>U(i)?n.get(U(i)):void 0),_=bs(()=>U(g)),v=P(`conversation`),y=P(void 0),b=P(!1),x=M(()=>{if(U(g))return(U(y)?n.get(U(y)):void 0)??ta(n.values(),U(g))}),S=M(()=>U(g)!==void 0&&U(v)===`compare`),ee=bs(()=>U(S)?U(x):void 0),te=M(()=>!_.current||U(S)&&U(x)!==void 0&&!ee.current);function ne(e){U(b)?(F(b,!1),e!==U(i)&&F(y,e,!0)):(F(i,e,!0),F(y,void 0))}function re(e){e.key===`Escape`&&F(b,!1)}let ie=P(void 0),ae=M(()=>U(ie)?.filter(({id:e})=>n.has(e))),oe=M(()=>U(ae)&&new Set(U(ae).map(({id:e})=>e))),se=M(()=>U(ae)&&gs(U(ae))),ce=M(()=>[...n.values()].filter(e=>Ho(e)===`done`).length);pn(()=>{let e=U(r);if(U(ce),!e){F(ie,void 0);return}let t=!1,n=setTimeout(async()=>{try{let n=await fetch(`/api/requests?${new URLSearchParams({q:e})}`);if(!n.ok)return;let r=await n.json();t||F(ie,r)}catch(e){console.error(`Failed to search requests:`,e)}},250);return()=>{t=!0,clearTimeout(n)}});let le=M(()=>U(l).flatMap(e=>{let t=U(se)?.get(e)??0;return e!==U(d)&&t>0?[[e,t]]:[]})),ue=P(!1),de=M(()=>[...n.values()].filter(e=>zo(e,{project:U(d),matches:U(r)?U(oe):void 0})).sort(Go)),fe=M(()=>U(ue)?Bo(U(de)):U(de)),pe=P(Wt(Date.now())),me=M(()=>cs(U(pe))),he=new bi;pn(()=>{let e=setInterval(()=>{let e=Date.now(),t=cs(U(pe));cs(e)!==t&&!he.has(t)&&he.set(t,!0),F(pe,e,!0)},6e4);return()=>clearInterval(e)});let ge=M(()=>ps(U(fe))),_e=e=>!!U(r)||U(ue)||(he.get(e)??e===U(me)),C=P(void 0),ve=M(()=>U(C)!==void 0&&!U(r)),ye=P(Wt(Date.now()));pn(()=>{if(!U(ve))return;F(ye,Date.now(),!0);let e=setInterval(()=>F(ye,Date.now(),!0),5e3);return()=>clearInterval(e)});let be=e=>e===U(me)&&!U(r)&&_e(e),xe=e=>U(ve)&&U(C)!==void 0&&e.key===U(me)?fs(e.calls,U(C),U(ye)):e.calls,w=M(()=>U(ge).flatMap(xe)),Se=M(()=>xs(Si(U(w)))),T=M(()=>Vo(ps(U(de)).flatMap(xe)));yi(()=>{let e,t;function r(){e=new EventSource(`/events`),e.onopen=()=>{F(a,!0),n.clear(),c()},e.onmessage=e=>{let t=JSON.parse(e.data);n.set(t.id,t)},e.addEventListener(`clear`,e=>{let{project:t}=JSON.parse(e.data);we(typeof t==`string`?t:void 0)}),e.addEventListener(`delete`,e=>{let{id:t}=JSON.parse(e.data);typeof t==`string`&&E(t)}),e.onerror=()=>{F(a,!1),e?.readyState===EventSource.CLOSED&&(t=setTimeout(r,3e3))}}return r(),()=>{clearTimeout(t),e?.close()}});function E(e){n.delete(e),U(i)===e&&(F(i,void 0),F(b,!1)),U(y)===e&&F(y,void 0)}async function Ce(e){try{let t=await fetch(`/api/requests/${e}`,{method:`DELETE`});(t.ok||t.status===404)&&E(e)}catch(e){console.error(`Failed to delete request:`,e)}}function we(e){for(let t of[...n.values()])(e===void 0||t.project===e)&&E(t.id)}async function Te(){let e=U(d);try{await fetch(`/api/clear?project=${encodeURIComponent(e)}`,{method:`POST`}),we(e)}catch(e){console.error(`Failed to clear requests:`,e)}}var Ee=wc();fr(`keydown`,qt,re);var De=L(Ee);yc(De,{get connected(){return U(a)},get address(){return U(o)},get version(){return U(s)},onclear:Te,get search(){return U(r)},set search(e){F(r,e,!0)}});var Oe=z(De,2);{let e=M(()=>U(r)?U(se):void 0);Es(Oe,{get projects(){return U(l)},get current(){return U(d)},unseen:m,get matches(){return U(e)},onchoose:h})}var ke=z(Oe,2);{let e=M(()=>U(w).length>0);Gs(ke,{get shown(){return U(e)},get cards(){return U(Se)}})}var Ae=z(ke,2),je=I(Ae);let Me;var Ne=I(je),Pe=e=>{var t=bc(),n=z(I(t));D(t),W(`click`,n,()=>F(b,!1)),J(e,t)};X(Ne,e=>{U(b)&&e(Pe)});var Fe=z(Ne,2);ss(Fe,{get pressed(){return U(ue)},get count(){return U(T)},ontoggle:()=>F(ue,!U(ue))}),Z(z(Fe,2),17,()=>U(ge),e=>e.key,(e,t)=>{let n=M(()=>_e(U(t).key)),r=M(()=>xe(U(t)));var a=xc(),o=I(a);{let e=M(()=>us(U(t).key,U(pe)));Ei(o,{get label(){return U(e)},get count(){return U(r).length},get total(){return U(t).calls.length},get open(){return U(n)},ontoggle:()=>he.set(U(t).key,!U(n))})}var s=z(o,2);{let e=M(()=>be(U(t).key));As(s,{get shown(){return U(e)},get minutes(){return U(C)},get count(){return U(r).length},onchoose:e=>F(C,e,!0)})}var c=z(s,2),l=e=>{var t=q();Z(L(t),17,()=>U(r),e=>e.id,(e,t)=>{{let n=M(()=>U(t).id===U(i)),r=M(()=>U(S)&&U(t).id===U(x)?.id);Vs(e,{get request(){return U(t)},get selected(){return U(n)},get comparing(){return U(S)},get compared(){return U(r)},onselect:()=>ne(U(t).id),ondelete:()=>Ce(U(t).id)})}}),J(e,t)};X(c,e=>{U(n)&&e(l)}),D(a),J(e,a)},e=>{as(e,{get project(){return U(d)},get search(){return U(r)},get failedOnly(){return U(ue)},get elsewhere(){return U(le)},onchoose:h})}),D(je);var Ie=z(je,2),Le=I(Ie),Re=e=>{J(e,Sc())},ze=e=>{ts(e,{get request(){return _.current},get other(){return ee.current},get picking(){return U(b)},onpick:()=>F(b,!U(b)),get chosen(){return U(v)},set chosen(e){F(v,e,!0)}})},Be=e=>{J(e,Cc())};X(Le,e=>{U(g)&&U(te)?e(Re):_.current?e(ze,1):e(Be,-1)}),D(Ie),D(Ae),B(()=>Me=Q(je,1,`list`,null,Me,{picking:U(b)})),J(e,Ee),A()}G([`click`]);var Ec=document.getElementById(`app`);if(!Ec)throw Error(`#app element not found in index.html`);Dr(Tc,{target:Ec});
24
+ otherwise. Clear All empties the project you are viewing.</li></ul> <p class="help-more">More in the <a target="_blank" rel="noreferrer">README</a>.</p></div>`);function Zs(e,t){let n=M(()=>`${location.protocol}//${t.address??`127.0.0.1:8080`}`);Js(e,{id:`help-popover`,label:`Help`,buttonClass:`icon-btn`,buttonLabel:`Help`,trigger:e=>{J(e,Ys())},children:(e,t)=>{var r=Xs(),i=z(I(r),2),a=I(i),o=z(I(a)),s=R(o),c=R(z(o,2));we(),D(a),we(4),D(i);var l=z(i,4),u=z(I(l),2),d=R(z(I(u),3));we(3),D(u);var f=z(u,10),p=R(z(I(f)));D(f),we(4),D(l);var m=z(l,2);$(z(I(m)),`href`,`https://github.com/piyook/llmspy#readme`),we(),D(m),D(r),B(()=>{Y(s,`ANTHROPIC_BASE_URL=${U(n)??``}/anthropic`),Y(c,`OPENAI_BASE_URL=${U(n)??``}/openai/v1`),Y(d,`ANTHROPIC_BASE_URL=${U(n)??``}/@my-app/anthropic`),Y(p,`${U(n)??``}/llms.txt`)}),J(e,r)},$$slots:{trigger:!0,default:!0}})}var Qs=[{value:`system`,label:`System`},{value:`light`,label:`Light`},{value:`dark`,label:`Dark`}],$s=`llmspy-theme`;function ec(){try{let e=localStorage.getItem($s);if(e===`light`||e===`dark`)return e}catch{}return`system`}function tc(e){let t=document.documentElement;e===`system`?delete t.dataset.theme:t.dataset.theme=e;try{e===`system`?localStorage.removeItem($s):localStorage.setItem($s,e)}catch{}}var nc=xr(`<svg viewBox="0 0 24 24" width="28" height="28" fill="none" stroke="currentColor" aria-hidden="true"><circle cx="12" cy="12" r="6.5" stroke-width="2"></circle><circle cx="12" cy="12" r="2.5" stroke-width="2"></circle><path stroke-width="3" d="M19 12h3M12 19v3M5 12H2M12 5V2M16.95 16.95l2.12 2.12M7.05 16.95l-2.12 2.12M7.05 7.05 4.93 4.93M16.95 7.05l2.12-2.12"></path></svg>`),rc=K(`<label><input type="radio" name="theme"/> </label>`),ic=K(`<p class="setting-version"> </p>`),ac=K(`<fieldset class="setting"><legend>Theme</legend> <div class="setting-options"></div></fieldset> <!>`,1);function oc(e,t){k(t,!0);let n=[],r=P(Wt(ec()));Js(e,{id:`settings-popover`,label:`Settings`,buttonClass:`icon-btn`,buttonLabel:`Settings`,trigger:e=>{J(e,nc())},children:(e,i)=>{var a=ac(),o=L(a),s=z(I(o),2);Z(s,21,()=>Qs,e=>e.value,(e,t)=>{var i=rc(),a=I(i);ri(a);var o,s=z(a);D(i),B(()=>{o!==(o=U(t).value)&&(a.value=(a.__value=o)??``),Y(s,` ${U(t).label??``}`)}),W(`change`,a,()=>tc(U(r))),li(n,[],a,()=>(U(t).value,U(r)),e=>F(r,e)),J(e,i)}),D(s),D(o);var c=z(o,2),l=e=>{var n=ic(),r=R(n);B(()=>Y(r,`LLMSpy ${t.version??``}`)),J(e,n)};X(c,e=>{t.version&&e(l)}),J(e,a)},$$slots:{trigger:!0,default:!0}}),A()}G([`change`]);var sc=xr(`<path class="wave wave-far" d="M26 4.5a10 10 0 0 1 0 15"></path><path class="wave wave-mid" d="M22.5 7a7 7 0 0 1 0 10"></path><path class="wave wave-near" d="M19.5 9.5a3.5 3.5 0 0 1 0 5"></path>`,1),cc=xr(`<line x1="3" y1="4" x2="18" y2="21"></line>`),lc=K(`<span role="status"><svg viewBox="0 0 30 24" width="45" height="36" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M4 9.5A6.5 6.5 0 0 1 17 9.5C17 14 12.5 15 12.5 18.5A3.5 3.5 0 0 1 5.5 18.5"></path><path d="M13.5 9.5A3 3 0 0 0 7.5 9.5v1a2 2 0 0 1 0 4"></path><!></svg> <span class="visually-hidden"> </span></span>`);function uc(e,t){let n=M(()=>t.connected?`Listening${t.address?` on ${t.address}`:``}`:`Not connected to the proxy`);var r=lc();let i;var a=I(r),o=z(I(a),2),s=e=>{var t=sc();we(2),J(e,t)},c=e=>{J(e,cc())};X(o,e=>{t.connected?e(s):e(c,-1)}),D(a);var l=R(z(a,2),!0);D(r),B(()=>{i=Q(r,1,`status-icon`,null,i,{connected:t.connected}),$(r,`title`,U(n)),Y(l,U(n))}),J(e,r)}var dc=K(`<p class="popover-error">Not connected to the proxy.</p>`),fc=K(`<p class="popover-error">Couldn't load the list from the proxy.</p>`),pc=K(`<p class="popover-hint">Loading...</p>`),mc=K(`<li class="upstream"><span class="upstream-name"> </span> <code class="upstream-url"> </code> <button type="button" class="small-btn"> </button> <span class="upstream-target"> </span></li>`),hc=K(`<ul class="upstream-list"></ul>`),gc=K(`<p class="popover-hint">Point an app's base URL at one of these.</p> <!>`,1);function _c(e,t){k(t,!0);let n=P(void 0),r=P(!1),i=P(void 0),a;function o(e){return`${location.protocol}//${t.address}/${e}`}async function s(){F(r,!1);try{let e=await fetch(`/api/upstreams`);if(!e.ok)throw Error(`HTTP ${e.status}`);F(n,await e.json(),!0)}catch{F(n,void 0),F(r,!0)}}async function c(e){let t=`Copied`;try{await navigator.clipboard.writeText(o(e))}catch{t=`Failed`}F(i,{name:e,label:t},!0),clearTimeout(a),a=setTimeout(()=>F(i,void 0),1500)}Js(e,{id:`upstream-popover`,label:`Base URLs`,align:`center`,onopen:s,trigger:e=>{we(),J(e,Sr(`Base URLs`))},children:(e,a)=>{var s=gc(),l=z(L(s),2),u=e=>{J(e,dc())},d=e=>{J(e,fc())},f=e=>{J(e,pc())},p=e=>{var t=hc();Z(t,21,()=>U(n),e=>e.name,(e,t)=>{var n=mc(),r=I(n),a=R(r,!0),s=z(r,2),l=R(s,!0),u=z(s,2),d=R(u,!0),f=R(z(u,2));D(n),B(e=>{Y(a,U(t).name),Y(l,e),$(u,`aria-label`,`Copy the ${U(t).name} base URL`),Y(d,U(i)?.name===U(t).name?U(i).label:`Copy`),Y(f,`to ${U(t).target??``}`)},[()=>o(U(t).name)]),W(`click`,u,()=>c(U(t).name)),J(e,n)}),D(t),J(e,t)};X(l,e=>{t.address?U(r)?e(d,1):U(n)?e(p,-1):e(f,2):e(u)}),J(e,s)},$$slots:{trigger:!0,default:!0}}),A()}G([`click`]);var vc=K(`<div class="header"><div class="title"><h1>LLMSpy</h1> <!></div> <div class="controls"><!> <input type="search" placeholder="Search requests..." aria-label="Search requests" class="search"/> <button type="button" class="clear-btn">Clear All</button> <!> <!></div></div>`);function yc(e,t){k(t,!0);let n=vi(t,`search`,15,``);var r=vc(),i=I(r);uc(z(I(i),2),{get connected(){return t.connected},get address(){return t.address}}),D(i);var a=z(i,2),o=I(a);{let e=M(()=>t.connected?t.address:void 0);_c(o,{get address(){return U(e)}})}var s=z(o,2);ri(s);var c=z(s,2),l=z(c,2);{let e=M(()=>t.connected?t.address:void 0);Zs(l,{get address(){return U(e)}})}oc(z(l,2),{get version(){return t.version}}),D(a),D(r),si(s,n),W(`click`,c,function(...e){t.onclear?.apply(this,e)}),J(e,r),A()}G([`click`]);var bc=K(`<p class="pick-hint" role="status">Click the call to compare with. <button type="button" class="small-btn">Cancel</button></p>`),xc=K(`<div class="day"><!> <!> <!></div>`),Sc=K(`<div class="no-requests">Loading…</div>`),Cc=K(`<div class="no-requests">Select a request to see it here.</div>`),wc=K(`<!> <!> <!> <div class="panes"><section aria-label="Captured requests"><!> <!> <!></section> <section class="detail" aria-label="Selected request"><!></section></div>`,1);function Tc(e,t){k(t,!0);let n=new bi,r=P(``),i=P(void 0),a=P(!1),o=P(void 0),s=P(void 0);async function c(){try{let e=await(await fetch(`/ping`)).json();typeof e.port==`number`&&F(o,`${location.hostname}:${e.port}`),typeof e.version==`string`&&F(s,e.version,!0)}catch{}}let l=M(()=>hs(n.values())),u=P(Wt(ms)),d=M(()=>U(l).includes(U(u))?U(u):ms),f=Date.now(),p=new bi,m=e=>vs(n.values(),e,p.get(e)??f);function h(e){if(e===U(d))return;let t=Date.now();p.set(U(d),t),p.set(e,t),F(u,e,!0),F(i,void 0),F(y,void 0),F(b,!1)}let g=M(()=>U(i)?n.get(U(i)):void 0),_=bs(()=>U(g)),v=P(`conversation`),y=P(void 0),b=P(!1),x=M(()=>{if(U(g))return(U(y)?n.get(U(y)):void 0)??ta(n.values(),U(g))}),S=M(()=>U(g)!==void 0&&U(v)===`compare`),ee=bs(()=>U(S)?U(x):void 0),te=M(()=>!_.current||U(S)&&U(x)!==void 0&&!ee.current);function ne(e){U(b)?(F(b,!1),e!==U(i)&&F(y,e,!0)):(F(i,e,!0),F(y,void 0))}function re(e){e.key===`Escape`&&F(b,!1)}let ie=P(void 0),ae=M(()=>U(ie)?.filter(({id:e})=>n.has(e))),oe=M(()=>U(ae)&&new Set(U(ae).map(({id:e})=>e))),se=M(()=>U(ae)&&gs(U(ae))),ce=M(()=>[...n.values()].filter(e=>Ho(e)===`done`).length);pn(()=>{let e=U(r);if(U(ce),!e){F(ie,void 0);return}let t=!1,n=setTimeout(async()=>{try{let n=await fetch(`/api/requests?${new URLSearchParams({q:e})}`);if(!n.ok)return;let r=await n.json();t||F(ie,r)}catch(e){console.error(`Failed to search requests:`,e)}},250);return()=>{t=!0,clearTimeout(n)}});let le=M(()=>U(l).flatMap(e=>{let t=U(se)?.get(e)??0;return e!==U(d)&&t>0?[[e,t]]:[]})),ue=P(!1),de=M(()=>[...n.values()].filter(e=>zo(e,{project:U(d),matches:U(r)?U(oe):void 0})).sort(Go)),fe=M(()=>U(ue)?Bo(U(de)):U(de)),pe=P(Wt(Date.now())),me=M(()=>cs(U(pe))),he=new bi;pn(()=>{let e=setInterval(()=>{let e=Date.now(),t=cs(U(pe));cs(e)!==t&&!he.has(t)&&he.set(t,!0),F(pe,e,!0)},6e4);return()=>clearInterval(e)});let ge=M(()=>ps(U(fe))),_e=e=>!!U(r)||U(ue)||(he.get(e)??e===U(me)),C=P(void 0),ve=M(()=>U(C)!==void 0&&!U(r)),ye=P(Wt(Date.now()));pn(()=>{if(!U(ve))return;F(ye,Date.now(),!0);let e=setInterval(()=>F(ye,Date.now(),!0),5e3);return()=>clearInterval(e)});let be=e=>e===U(me)&&!U(r)&&_e(e),xe=e=>U(ve)&&U(C)!==void 0&&e.key===U(me)?fs(e.calls,U(C),U(ye)):e.calls,w=M(()=>U(ge).flatMap(xe)),Se=M(()=>xs(Si(U(w)))),T=M(()=>Vo(ps(U(de)).flatMap(xe)));yi(()=>{let e,t;function r(){e=new EventSource(`/events`),e.onopen=()=>{F(a,!0),n.clear(),c()},e.onmessage=e=>{let t=JSON.parse(e.data);n.set(t.id,t)},e.addEventListener(`clear`,e=>{let{project:t}=JSON.parse(e.data);we(typeof t==`string`?t:void 0)}),e.addEventListener(`delete`,e=>{let{id:t}=JSON.parse(e.data);typeof t==`string`&&E(t)}),e.onerror=()=>{F(a,!1),e?.readyState===EventSource.CLOSED&&(t=setTimeout(r,3e3))}}return r(),()=>{clearTimeout(t),e?.close()}});function E(e){n.delete(e),U(i)===e&&(F(i,void 0),F(b,!1)),U(y)===e&&F(y,void 0)}async function Ce(e){try{let t=await fetch(`/api/requests/${e}`,{method:`DELETE`});(t.ok||t.status===404)&&E(e)}catch(e){console.error(`Failed to delete request:`,e)}}function we(e){for(let t of[...n.values()])(e===void 0||t.project===e)&&E(t.id)}async function Te(){let e=U(d);try{await fetch(`/api/clear?project=${encodeURIComponent(e)}`,{method:`POST`}),we(e)}catch(e){console.error(`Failed to clear requests:`,e)}}var Ee=wc();fr(`keydown`,qt,re);var De=L(Ee);yc(De,{get connected(){return U(a)},get address(){return U(o)},get version(){return U(s)},onclear:Te,get search(){return U(r)},set search(e){F(r,e,!0)}});var Oe=z(De,2);{let e=M(()=>U(r)?U(se):void 0);Es(Oe,{get projects(){return U(l)},get current(){return U(d)},unseen:m,get matches(){return U(e)},onchoose:h})}var ke=z(Oe,2);{let e=M(()=>U(w).length>0);Gs(ke,{get shown(){return U(e)},get cards(){return U(Se)}})}var Ae=z(ke,2),je=I(Ae);let Me;var Ne=I(je),Pe=e=>{var t=bc(),n=z(I(t));D(t),W(`click`,n,()=>F(b,!1)),J(e,t)};X(Ne,e=>{U(b)&&e(Pe)});var Fe=z(Ne,2);ss(Fe,{get pressed(){return U(ue)},get count(){return U(T)},ontoggle:()=>F(ue,!U(ue))}),Z(z(Fe,2),17,()=>U(ge),e=>e.key,(e,t)=>{let n=M(()=>_e(U(t).key)),r=M(()=>xe(U(t)));var a=xc(),o=I(a);{let e=M(()=>us(U(t).key,U(pe)));Ei(o,{get label(){return U(e)},get count(){return U(r).length},get total(){return U(t).calls.length},get open(){return U(n)},ontoggle:()=>he.set(U(t).key,!U(n))})}var s=z(o,2);{let e=M(()=>be(U(t).key));As(s,{get shown(){return U(e)},get minutes(){return U(C)},get count(){return U(r).length},onchoose:e=>F(C,e,!0)})}var c=z(s,2),l=e=>{var t=q();Z(L(t),17,()=>U(r),e=>e.id,(e,t)=>{{let n=M(()=>U(t).id===U(i)),r=M(()=>U(S)&&U(t).id===U(x)?.id);Vs(e,{get request(){return U(t)},get selected(){return U(n)},get comparing(){return U(S)},get compared(){return U(r)},onselect:()=>ne(U(t).id),ondelete:()=>Ce(U(t).id)})}}),J(e,t)};X(c,e=>{U(n)&&e(l)}),D(a),J(e,a)},e=>{as(e,{get project(){return U(d)},get search(){return U(r)},get failedOnly(){return U(ue)},get elsewhere(){return U(le)},onchoose:h})}),D(je);var Ie=z(je,2),Le=I(Ie),Re=e=>{J(e,Sc())},ze=e=>{ts(e,{get request(){return _.current},get other(){return ee.current},get picking(){return U(b)},onpick:()=>F(b,!U(b)),get chosen(){return U(v)},set chosen(e){F(v,e,!0)}})},Be=e=>{J(e,Cc())};X(Le,e=>{U(g)&&U(te)?e(Re):_.current?e(ze,1):e(Be,-1)}),D(Ie),D(Ae),B(()=>Me=Q(je,1,`list`,null,Me,{picking:U(b)})),J(e,Ee),A()}G([`click`]);var Ec=document.getElementById(`app`);if(!Ec)throw Error(`#app element not found in index.html`);Dr(Tc,{target:Ec});
@@ -12,7 +12,7 @@
12
12
  if (theme === 'light' || theme === 'dark') document.documentElement.dataset.theme = theme;
13
13
  } catch {}
14
14
  </script>
15
- <script type="module" crossorigin src="/assets/index-CyJmqpbn.js"></script>
15
+ <script type="module" crossorigin src="/assets/index-B9XIilZJ.js"></script>
16
16
  <link rel="stylesheet" crossorigin href="/assets/index-moM-ONoJ.css">
17
17
  </head>
18
18
  <body>