@llm4ts/shell 4.5.0 → 4.7.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/dist/Cli.d.ts +11 -1
- package/dist/Cli.d.ts.map +1 -1
- package/dist/Cli.js +25 -2
- package/dist/Cli.js.map +1 -1
- package/dist/WebWatch.d.ts +28 -9
- package/dist/WebWatch.d.ts.map +1 -1
- package/dist/WebWatch.js +57 -11
- package/dist/WebWatch.js.map +1 -1
- package/package.json +4 -4
- package/src/Cli.ts +42 -1
- package/src/WebWatch.ts +100 -32
- package/web/app.js +19 -19
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@llm4ts/shell",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.7.0",
|
|
4
4
|
"description": "Interactive shell and CLI for llm4ts: flow discovery, run-a-flow, and view",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -46,9 +46,9 @@
|
|
|
46
46
|
"dependencies": {
|
|
47
47
|
"@effect/platform-node": "4.0.0",
|
|
48
48
|
"@effect/platform-node-shared": "4.0.0",
|
|
49
|
-
"@llm4ts/
|
|
50
|
-
"@llm4ts/
|
|
51
|
-
"@llm4ts/
|
|
49
|
+
"@llm4ts/flow": "4.7.0",
|
|
50
|
+
"@llm4ts/runner": "4.7.0",
|
|
51
|
+
"@llm4ts/core": "4.7.0"
|
|
52
52
|
},
|
|
53
53
|
"peerDependencies": {
|
|
54
54
|
"effect": "4.0.0"
|
package/src/Cli.ts
CHANGED
|
@@ -625,6 +625,36 @@ const costsCommand = Command.make(
|
|
|
625
625
|
)
|
|
626
626
|
)
|
|
627
627
|
|
|
628
|
+
export interface WebBindFlags {
|
|
629
|
+
readonly web: boolean
|
|
630
|
+
readonly port: Option.Option<number>
|
|
631
|
+
readonly host: Option.Option<string>
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
/** `--port` and `--host` for `watch --web`; both need `--web`, a port fits 0–65535 (0: a free one). */
|
|
635
|
+
export const webBindFrom = (
|
|
636
|
+
flags: WebBindFlags
|
|
637
|
+
): Effect.Effect<{ readonly port?: number; readonly host?: string }, ShellUsageError> =>
|
|
638
|
+
Effect.gen(function* () {
|
|
639
|
+
const port = Option.getOrUndefined(flags.port)
|
|
640
|
+
const host = Option.getOrUndefined(flags.host)?.trim()
|
|
641
|
+
if (!flags.web && (port !== undefined || host !== undefined)) {
|
|
642
|
+
return yield* new ShellUsageError({ message: "--port and --host need --web" })
|
|
643
|
+
}
|
|
644
|
+
if (port !== undefined && (!Number.isInteger(port) || port < 0 || port > 65_535)) {
|
|
645
|
+
return yield* new ShellUsageError({ message: "--port must be between 0 and 65535" })
|
|
646
|
+
}
|
|
647
|
+
if (host !== undefined && (host === "" || /[\s/]/.test(host))) {
|
|
648
|
+
return yield* new ShellUsageError({
|
|
649
|
+
message: "--host must be an address or a host name, such as 127.0.0.1 or 0.0.0.0"
|
|
650
|
+
})
|
|
651
|
+
}
|
|
652
|
+
return {
|
|
653
|
+
...(port === undefined ? {} : { port }),
|
|
654
|
+
...(host === undefined ? {} : { host })
|
|
655
|
+
}
|
|
656
|
+
})
|
|
657
|
+
|
|
628
658
|
export interface WatchFlags {
|
|
629
659
|
readonly trace: Option.Option<string>
|
|
630
660
|
readonly repo: Option.Option<string>
|
|
@@ -696,18 +726,29 @@ const watchCommand = Command.make(
|
|
|
696
726
|
Flag.withDescription(
|
|
697
727
|
"Watch in a browser instead: serve the tree on a local URL until Ctrl-C (ADR 0051)"
|
|
698
728
|
)
|
|
729
|
+
),
|
|
730
|
+
port: Flag.Int("port").pipe(
|
|
731
|
+
Flag.optional,
|
|
732
|
+
Flag.withDescription("With --web: the port to listen on (default: a free one)")
|
|
733
|
+
),
|
|
734
|
+
host: Flag.String("host").pipe(
|
|
735
|
+
Flag.optional,
|
|
736
|
+
Flag.withDescription(
|
|
737
|
+
"With --web: the address to listen on (default 127.0.0.1; 0.0.0.0 shows the run to the network)"
|
|
738
|
+
)
|
|
699
739
|
)
|
|
700
740
|
},
|
|
701
741
|
(config) =>
|
|
702
742
|
Effect.gen(function* () {
|
|
703
743
|
const options = yield* watchOptionsFrom(config)
|
|
744
|
+
const bind = yield* webBindFrom(config)
|
|
704
745
|
const idleAfterMs = idleAfterFrom(yield* verbEnvironment(options.repo))
|
|
705
746
|
if (config.web) {
|
|
706
747
|
if (options.tail !== undefined) {
|
|
707
748
|
return yield* new ShellUsageError({ message: "watch --web does not take --tail" })
|
|
708
749
|
}
|
|
709
750
|
return yield* runWebWatch(
|
|
710
|
-
{ ...options, idleAfterMs },
|
|
751
|
+
{ ...options, ...bind, idleAfterMs },
|
|
711
752
|
nodeWebWatchDependencies(nodeTraceSources)
|
|
712
753
|
)
|
|
713
754
|
}
|
package/src/WebWatch.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// @internal: `llm4ts watch --web`, served by the shell's own CLI
|
|
2
2
|
import { readFileSync } from "node:fs"
|
|
3
3
|
import { createServer, type Server } from "node:http"
|
|
4
|
+
import { isIP, isIPv4 } from "node:net"
|
|
4
5
|
import { basename } from "node:path"
|
|
5
6
|
import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer"
|
|
6
7
|
import * as Clock from "effect/Clock"
|
|
@@ -15,6 +16,7 @@ import * as HttpServer from "effect/http/HttpServer"
|
|
|
15
16
|
import type { ServeError } from "effect/http/HttpServerError"
|
|
16
17
|
import * as HttpServerRequest from "effect/http/HttpServerRequest"
|
|
17
18
|
import * as HttpServerResponse from "effect/http/HttpServerResponse"
|
|
19
|
+
import { formatUrlHostString } from "effect/net/NetAddress"
|
|
18
20
|
import type * as NetAddress from "effect/net/NetAddress"
|
|
19
21
|
import type { FlowError } from "@llm4ts/flow/FlowError"
|
|
20
22
|
import { readTrace } from "@llm4ts/flow/Replay"
|
|
@@ -68,9 +70,9 @@ export type WebWatchOptions = Pick<
|
|
|
68
70
|
WatchOptions,
|
|
69
71
|
"repo" | "trace" | "epic" | "replay" | "speed" | "idleAfterMs"
|
|
70
72
|
> & {
|
|
71
|
-
/**
|
|
73
|
+
/** `127.0.0.1` by default; the page shows the run to whoever can reach it (`--host`). */
|
|
72
74
|
readonly host?: string
|
|
73
|
-
/** A free port when omitted. */
|
|
75
|
+
/** A free port when omitted (`--port`). */
|
|
74
76
|
readonly port?: number
|
|
75
77
|
}
|
|
76
78
|
|
|
@@ -150,39 +152,73 @@ const sse = (event: string, data: string): string => `event: ${event}\ndata: ${d
|
|
|
150
152
|
const frameEvent = (frame: WebFrame): string => sse("frame", encodeFrame(frame))
|
|
151
153
|
const endEvent = sse("end", "{}")
|
|
152
154
|
|
|
155
|
+
const loopbackNames: ReadonlySet<string> = new Set(["127.0.0.1", "localhost", "::1"])
|
|
156
|
+
const wildcardHosts: ReadonlySet<string> = new Set(["0.0.0.0", "::"])
|
|
157
|
+
|
|
158
|
+
/** A host as written, lower case and without IPv6 brackets: `[::1]` is `::1`. */
|
|
159
|
+
const bareHost = (host: string): string => host.toLowerCase().replace(/^\[(.*)\]$/, "$1")
|
|
160
|
+
|
|
161
|
+
/** The address `--host` binds by default. */
|
|
162
|
+
export const defaultWebHost = "127.0.0.1"
|
|
163
|
+
|
|
164
|
+
/** Whether a bind address is this machine only (`127.0.0.0/8`, `::1`, `localhost`). */
|
|
165
|
+
export const isLoopbackHost = (host: string): boolean => {
|
|
166
|
+
const bare = bareHost(host)
|
|
167
|
+
return loopbackNames.has(bare) || (isIPv4(bare) && bare.startsWith("127."))
|
|
168
|
+
}
|
|
169
|
+
|
|
153
170
|
/**
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
*
|
|
171
|
+
* Whether a request's `Host` names this server: its port, and either a
|
|
172
|
+
* loopback name, the address it was bound to, or, bound to every interface
|
|
173
|
+
* (`0.0.0.0`, `::`), any IP address. A DNS name it was not bound to is
|
|
174
|
+
* refused, so a page on another origin cannot read the run by rebinding
|
|
175
|
+
* its own name to this machine.
|
|
157
176
|
*/
|
|
158
|
-
export const
|
|
159
|
-
|
|
177
|
+
export const hostAllowed = (
|
|
178
|
+
header: string | undefined,
|
|
179
|
+
port: number,
|
|
180
|
+
bound: string = defaultWebHost
|
|
181
|
+
): boolean => {
|
|
182
|
+
const match = header === undefined ? null : /^(\[[^\]]+\]|[^:[\]]+):(\d+)$/.exec(header)
|
|
183
|
+
if (match === null || Number(match[2]) !== port) {
|
|
184
|
+
return false
|
|
185
|
+
}
|
|
186
|
+
const name = bareHost(match[1] ?? "")
|
|
187
|
+
const bind = bareHost(bound)
|
|
188
|
+
return loopbackNames.has(name) || name === bind || (wildcardHosts.has(bind) && isIP(name) !== 0)
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** What `watch --web` adds to its banner when it binds beyond this machine; empty on loopback. */
|
|
192
|
+
export const exposureNotice = (host: string): string =>
|
|
193
|
+
isLoopbackHost(host)
|
|
194
|
+
? ""
|
|
195
|
+
: `Listening on ${host}, not loopback: anyone who can reach this port sees the run.\n`
|
|
160
196
|
|
|
161
197
|
const portOf = (address: NetAddress.SocketAddress): number | undefined =>
|
|
162
198
|
"port" in address ? address.port : undefined
|
|
163
199
|
|
|
164
200
|
/** Refuses a foreign `Host` (403); stamps every answer as not to be sniffed or cached. */
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
201
|
+
const guardFor = (bound: string) =>
|
|
202
|
+
HttpRouter.middleware(
|
|
203
|
+
Effect.gen(function* () {
|
|
204
|
+
const server = yield* HttpServer.HttpServer
|
|
205
|
+
const port = portOf(server.address)
|
|
206
|
+
return (app) =>
|
|
207
|
+
Effect.gen(function* () {
|
|
208
|
+
const request = yield* HttpServerRequest.HttpServerRequest
|
|
209
|
+
const host = request.headers["host"]
|
|
210
|
+
const response =
|
|
211
|
+
port !== undefined && hostAllowed(host, port, bound)
|
|
212
|
+
? yield* app
|
|
213
|
+
: HttpServerResponse.text("forbidden host", { status: 403 })
|
|
214
|
+
return HttpServerResponse.setHeaders(response, {
|
|
215
|
+
"x-content-type-options": "nosniff",
|
|
216
|
+
"cache-control": "no-store"
|
|
217
|
+
})
|
|
181
218
|
})
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
)
|
|
219
|
+
}),
|
|
220
|
+
{ global: true }
|
|
221
|
+
)
|
|
186
222
|
|
|
187
223
|
/**
|
|
188
224
|
* The routes: the page, `/api/run`, `/api/frame?count=` (a replay's scrub),
|
|
@@ -401,21 +437,46 @@ export const webWatchApp = Effect.fn("@llm4ts/shell/WebWatch.app")(function* (
|
|
|
401
437
|
|
|
402
438
|
return Layer.mergeAll(
|
|
403
439
|
HttpRouter.addAll([...pages, favicon, run, frame, stream, transcript]),
|
|
404
|
-
|
|
440
|
+
guardFor(options.host ?? defaultWebHost)
|
|
405
441
|
)
|
|
406
442
|
})
|
|
407
443
|
|
|
444
|
+
/** The server could not listen on the address and port asked for. */
|
|
445
|
+
export class WebListenError extends Schema.TaggedError<WebListenError>()("WebListen", {
|
|
446
|
+
message: Schema.String
|
|
447
|
+
}) {}
|
|
448
|
+
|
|
449
|
+
const listenReasons: Readonly<Record<string, string>> = {
|
|
450
|
+
EADDRINUSE: "the port is in use (pick another --port, or 0 for a free one)",
|
|
451
|
+
EADDRNOTAVAIL: "this machine has no such address",
|
|
452
|
+
EACCES: "not allowed to listen there",
|
|
453
|
+
ENOTFOUND: "no such host"
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/** Why listening failed, in words; the cause's own message for a code it does not know. */
|
|
457
|
+
export const listenFailure = (host: string, port: number | undefined, cause: unknown): string => {
|
|
458
|
+
const code =
|
|
459
|
+
typeof cause === "object" && cause !== null && "code" in cause ? String(cause.code) : ""
|
|
460
|
+
const message = cause instanceof Error ? cause.message : String(cause)
|
|
461
|
+
return `watch --web cannot listen on ${formatUrlHostString(host)}:${port ?? 0}: ${listenReasons[code] ?? message}`
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/** The URL the banner says to open: a wildcard bind (`0.0.0.0`, `::`) is opened on loopback. */
|
|
465
|
+
export const openAddress = (formatted: string): string =>
|
|
466
|
+
formatted.replace("//0.0.0.0:", "//127.0.0.1:").replace("//[::]:", "//[::1]:")
|
|
467
|
+
|
|
408
468
|
/** Serves the page until the reader detaches; prints where to open it. */
|
|
409
469
|
export const runWebWatch = Effect.fn("@llm4ts/shell/WebWatch.run")(function* (
|
|
410
470
|
options: WebWatchOptions,
|
|
411
471
|
dependencies: WebWatchDependencies
|
|
412
|
-
): Effect.fn.Return<void, FlowError | WatchTargetMissing |
|
|
472
|
+
): Effect.fn.Return<void, FlowError | WatchTargetMissing | WebListenError> {
|
|
413
473
|
const app = yield* webWatchApp(options, dependencies)
|
|
414
|
-
const host = options.host ??
|
|
474
|
+
const host = options.host ?? defaultWebHost
|
|
475
|
+
const exposed = exposureNotice(host)
|
|
415
476
|
const announce = Layer.effectDiscard(
|
|
416
477
|
HttpServer.addressFormattedWith((address) =>
|
|
417
478
|
dependencies.write(
|
|
418
|
-
`llm4ts watch --web${options.replay === true ? " --replay" : ""} · open ${address}/\
|
|
479
|
+
`llm4ts watch --web${options.replay === true ? " --replay" : ""} · open ${openAddress(address)}/\n${exposed}Ctrl-C stops the server and detaches.\n`
|
|
419
480
|
)
|
|
420
481
|
)
|
|
421
482
|
)
|
|
@@ -447,7 +508,14 @@ export const runWebWatch = Effect.fn("@llm4ts/shell/WebWatch.run")(function* (
|
|
|
447
508
|
server?.closeAllConnections()
|
|
448
509
|
})
|
|
449
510
|
)
|
|
450
|
-
yield* Effect.raceFirst(
|
|
511
|
+
yield* Effect.raceFirst(
|
|
512
|
+
Effect.mapError(
|
|
513
|
+
served,
|
|
514
|
+
(error: ServeError) =>
|
|
515
|
+
new WebListenError({ message: listenFailure(host, options.port, error.cause) })
|
|
516
|
+
),
|
|
517
|
+
detach
|
|
518
|
+
)
|
|
451
519
|
yield* dependencies.write("detached\n")
|
|
452
520
|
})
|
|
453
521
|
|