@sixb/server 0.1.26 → 0.2.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 +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2495 -1845
- package/dist/index.js.map +29 -27
- package/dist/routes/actions.d.ts +1 -1
- package/dist/routes/agent-approvals.d.ts +11 -5
- package/dist/routes/agent-approvals.d.ts.map +1 -1
- package/dist/routes/agent-children.d.ts +2 -2
- package/dist/routes/agent-computers.d.ts +214 -10
- package/dist/routes/agent-computers.d.ts.map +1 -1
- package/dist/routes/agent-runners.d.ts +1 -1
- package/dist/routes/agent-runners.d.ts.map +1 -1
- package/dist/routes/agents.d.ts +230 -24
- package/dist/routes/agents.d.ts.map +1 -1
- package/dist/routes/ai-accounting.d.ts.map +1 -1
- package/dist/routes/auth.d.ts +9 -0
- package/dist/routes/auth.d.ts.map +1 -1
- package/dist/routes/datasets.d.ts +93 -17
- package/dist/routes/datasets.d.ts.map +1 -1
- package/dist/routes/events.d.ts +1 -1
- package/dist/routes/links.d.ts +121 -81
- package/dist/routes/links.d.ts.map +1 -1
- package/dist/routes/logs.d.ts +3 -3
- package/dist/routes/objects.d.ts +149 -58
- package/dist/routes/objects.d.ts.map +1 -1
- package/dist/routes/observed-agent-threads.d.ts +23 -5
- package/dist/routes/observed-agent-threads.d.ts.map +1 -1
- package/dist/routes/pipelines.d.ts +2 -2
- package/dist/routes/projections.d.ts +122 -122
- package/dist/routes/rules.d.ts +2 -2
- package/dist/routes/telemetry.d.ts +0 -50
- package/dist/routes/telemetry.d.ts.map +1 -1
- package/dist/routes/webhook-runs.d.ts +1 -1
- package/dist/routes/workflows.d.ts +21 -10
- package/dist/routes/workflows.d.ts.map +1 -1
- package/dist/routes/ws/agents.d.ts +4 -1
- package/dist/routes/ws/agents.d.ts.map +1 -1
- package/dist/routes/ws/logs.d.ts +10 -10
- package/dist/schemas/actions.d.ts +3 -3
- package/dist/schemas/activity.d.ts +4 -4
- package/dist/schemas/agent-approvals.d.ts +21 -21
- package/dist/schemas/agents.d.ts +760 -107
- package/dist/schemas/agents.d.ts.map +1 -1
- package/dist/schemas/ai-accounting.d.ts +1114 -6
- package/dist/schemas/ai-accounting.d.ts.map +1 -1
- package/dist/schemas/auth.d.ts +47 -0
- package/dist/schemas/auth.d.ts.map +1 -1
- package/dist/schemas/datasets.d.ts +303 -60
- package/dist/schemas/datasets.d.ts.map +1 -1
- package/dist/schemas/event-payloads.d.ts +6 -6
- package/dist/schemas/events.d.ts +8 -8
- package/dist/schemas/lineage.d.ts +1272 -0
- package/dist/schemas/lineage.d.ts.map +1 -0
- package/dist/schemas/links.d.ts +4 -27
- package/dist/schemas/links.d.ts.map +1 -1
- package/dist/schemas/logs.d.ts +22 -22
- package/dist/schemas/objects.d.ts +7 -26
- package/dist/schemas/objects.d.ts.map +1 -1
- package/dist/schemas/pipelines.d.ts +12 -12
- package/dist/schemas/projections.d.ts +733 -733
- package/dist/schemas/rules.d.ts +4 -4
- package/dist/schemas/telemetry.d.ts +0 -13
- package/dist/schemas/telemetry.d.ts.map +1 -1
- package/dist/schemas/webhook-runs.d.ts +2 -2
- package/dist/schemas/workflows.d.ts +133 -44
- package/dist/schemas/workflows.d.ts.map +1 -1
- package/dist/server.d.ts +4 -0
- package/dist/server.d.ts.map +1 -1
- package/dist/utils/request-body.d.ts +5 -0
- package/dist/utils/request-body.d.ts.map +1 -1
- package/dist/utils/request-timing.d.ts +18 -0
- package/dist/utils/request-timing.d.ts.map +1 -0
- package/package.json +7 -7
- package/src/index.ts +1 -0
- package/src/routes/agent-approvals.ts +2 -2
- package/src/routes/agent-computers.ts +309 -87
- package/src/routes/agent-runners.ts +30 -6
- package/src/routes/agents.ts +187 -25
- package/src/routes/ai-accounting.ts +149 -12
- package/src/routes/auth.ts +24 -2
- package/src/routes/datasets.ts +55 -5
- package/src/routes/links.ts +43 -76
- package/src/routes/objects.ts +49 -42
- package/src/routes/observed-agent-threads.ts +2 -2
- package/src/routes/telemetry.ts +1 -42
- package/src/routes/ws/agents.ts +10 -1
- package/src/schemas/agents.ts +121 -6
- package/src/schemas/ai-accounting.ts +78 -15
- package/src/schemas/auth.ts +19 -0
- package/src/schemas/datasets.ts +36 -0
- package/src/schemas/lineage.ts +115 -0
- package/src/schemas/links.ts +3 -11
- package/src/schemas/objects.ts +6 -10
- package/src/schemas/telemetry.ts +0 -6
- package/src/server.ts +18 -2
- package/src/utils/request-body.ts +10 -4
- package/src/utils/request-timing.ts +136 -0
package/src/server.ts
CHANGED
|
@@ -62,6 +62,11 @@ import { registerWebhookRoutes } from "./routes/webhooks"
|
|
|
62
62
|
import { registerWebSocketRoutes } from "./routes/ws"
|
|
63
63
|
import { ObjectQueryOpenApiSchemas } from "./schemas/objects"
|
|
64
64
|
import { limitRequestBody, RequestBodyTooLargeError } from "./utils/request-body"
|
|
65
|
+
import {
|
|
66
|
+
markRequestAuthorized,
|
|
67
|
+
type SixbRequestTimingOptions,
|
|
68
|
+
timeRequest,
|
|
69
|
+
} from "./utils/request-timing"
|
|
65
70
|
|
|
66
71
|
// Clients send only subscription messages; Bun's default would buffer up to 16 MB for one.
|
|
67
72
|
export const MAX_WEBSOCKET_MESSAGE_BYTES = 64 * 1024
|
|
@@ -81,6 +86,8 @@ export interface SixbServerOptions {
|
|
|
81
86
|
* `"private"` for loopback, private, and link-local networks. Defaults to `["private"]`.
|
|
82
87
|
*/
|
|
83
88
|
trustedProxies?: readonly string[]
|
|
89
|
+
/** Where requests spend their time: a `Server-Timing` header and a log of slow requests. */
|
|
90
|
+
timing?: SixbRequestTimingOptions
|
|
84
91
|
}
|
|
85
92
|
|
|
86
93
|
export function createSixbServer(options: SixbServerOptions): SixbServer {
|
|
@@ -98,6 +105,7 @@ export class SixbServer {
|
|
|
98
105
|
private readonly authExperience?: SixbAuthExperienceOptions
|
|
99
106
|
private readonly sharedAccessOptions: SixbSharedAccessOptions
|
|
100
107
|
private readonly resolveClientAddress: ClientAddressResolver
|
|
108
|
+
private readonly timing: SixbRequestTimingOptions
|
|
101
109
|
private app: SixbApp | null = null
|
|
102
110
|
private bunServer: ReturnType<typeof Bun.serve> | null = null
|
|
103
111
|
private maintenance: OntologyMaintenanceHandle | null = null
|
|
@@ -108,6 +116,7 @@ export class SixbServer {
|
|
|
108
116
|
this.port = options.port ?? 3000
|
|
109
117
|
this.hostname = options.hostname ?? "0.0.0.0"
|
|
110
118
|
this.quiet = options.quiet ?? false
|
|
119
|
+
this.timing = options.timing ?? {}
|
|
111
120
|
this.apiBrowserPolicy = resolveApiBrowserPolicy(options.browser)
|
|
112
121
|
// URLs the API hands to third parties, such as file download URLs, name this origin.
|
|
113
122
|
if (this.apiBrowserPolicy.publicOrigin) {
|
|
@@ -191,6 +200,7 @@ export class SixbServer {
|
|
|
191
200
|
host: this.hostname,
|
|
192
201
|
port: this.port,
|
|
193
202
|
resolveClientAddress: this.resolveClientAddress,
|
|
203
|
+
timing: this.timing,
|
|
194
204
|
})
|
|
195
205
|
} catch (error) {
|
|
196
206
|
this.app = null
|
|
@@ -377,7 +387,12 @@ export function createSixbApi(server: SixbServer) {
|
|
|
377
387
|
}),
|
|
378
388
|
}
|
|
379
389
|
})
|
|
380
|
-
.onBeforeHandle(({ auth }) =>
|
|
390
|
+
.onBeforeHandle(({ auth, route }) => {
|
|
391
|
+
// A share or an Agent's run token speaks for someone who has not signed in here.
|
|
392
|
+
const signedIn = auth.kind === "allow" && auth.session?.authenticated === true
|
|
393
|
+
markRequestAuthorized(route, signedIn || (auth.kind === "allow" && !guard.isAuthEnabled()))
|
|
394
|
+
return auth.kind === "deny" ? auth.response : undefined
|
|
395
|
+
})
|
|
381
396
|
.mapResponse(({ auth, request, responseValue, set, sharedAccessCookies }) => {
|
|
382
397
|
if (sharedAccessCookies) {
|
|
383
398
|
if (sharedAccessCookies.setCookies.length > 0) {
|
|
@@ -557,6 +572,7 @@ function startApiServer(
|
|
|
557
572
|
readonly host: string
|
|
558
573
|
readonly port: number
|
|
559
574
|
readonly resolveClientAddress: ClientAddressResolver
|
|
575
|
+
readonly timing: SixbRequestTimingOptions
|
|
560
576
|
}
|
|
561
577
|
) {
|
|
562
578
|
const bunServer = Bun.serve({
|
|
@@ -573,7 +589,7 @@ function startApiServer(
|
|
|
573
589
|
const forwardedFor = request.headers.get("x-forwarded-for")
|
|
574
590
|
setRequestClientAddress(request, options.resolveClientAddress(socketAddress, forwardedFor))
|
|
575
591
|
}
|
|
576
|
-
return app.fetch(request)
|
|
592
|
+
return timeRequest(request, options.timing, () => app.fetch(request))
|
|
577
593
|
},
|
|
578
594
|
websocket: getElysiaWsHandler(app),
|
|
579
595
|
} as Parameters<typeof Bun.serve>[0])
|
|
@@ -54,6 +54,15 @@ export function limitRequestBody(request: Request): Request {
|
|
|
54
54
|
return limited
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
+
/**
|
|
58
|
+
* Lifts the server's cap for a request whose handler reads the body itself, within its own limit,
|
|
59
|
+
* once it has authenticated the caller: a runner's run session (see serveAgentRunSession).
|
|
60
|
+
*/
|
|
61
|
+
export function liftRequestBodyCap(request: Request): void {
|
|
62
|
+
const cap = requestBodyCaps.get(request)
|
|
63
|
+
if (cap) cap.lifted = true
|
|
64
|
+
}
|
|
65
|
+
|
|
57
66
|
/**
|
|
58
67
|
* Reads a request body fully into memory, failing once it passes `limitBytes`. That limit
|
|
59
68
|
* replaces the server's default cap for this request, so a route reading more than the default
|
|
@@ -66,10 +75,7 @@ export async function readRequestBodyWithLimit(
|
|
|
66
75
|
tooLargeMessage?: string
|
|
67
76
|
): Promise<Uint8Array<ArrayBuffer>> {
|
|
68
77
|
// The loop below enforces `limitBytes` in place of the server's cap.
|
|
69
|
-
|
|
70
|
-
if (cap) {
|
|
71
|
-
cap.lifted = true
|
|
72
|
-
}
|
|
78
|
+
liftRequestBodyCap(request)
|
|
73
79
|
|
|
74
80
|
const contentLength = request.headers.get("content-length")
|
|
75
81
|
if (contentLength !== null) {
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks"
|
|
2
|
+
import { runWithStorageTimer, StorageTimer } from "@sixb/core/internal/storage-operation-scope"
|
|
3
|
+
|
|
4
|
+
const DEFAULT_SLOW_REQUEST_MILLIS = 1_000
|
|
5
|
+
|
|
6
|
+
export interface SixbRequestTimingOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Send a `Server-Timing` header on responses to signed-in requests, which browsers show under
|
|
9
|
+
* a request's Timing tab. Off by default, since timings reveal how the server spends its time.
|
|
10
|
+
*/
|
|
11
|
+
readonly header?: boolean
|
|
12
|
+
/** Log requests that take at least this long. Defaults to 1000; 0 turns the log off. */
|
|
13
|
+
readonly slowRequestMillis?: number
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Read `SIXB_SERVER_TIMING` and `SIXB_SLOW_REQUEST_MS`. */
|
|
17
|
+
export function requestTimingOptionsFromEnv(
|
|
18
|
+
env: Readonly<Record<string, string | undefined>> = process.env
|
|
19
|
+
): SixbRequestTimingOptions {
|
|
20
|
+
const slowRequestMillis = Number(env.SIXB_SLOW_REQUEST_MS)
|
|
21
|
+
return {
|
|
22
|
+
header: env.SIXB_SERVER_TIMING === "1" || env.SIXB_SERVER_TIMING === "true",
|
|
23
|
+
...(env.SIXB_SLOW_REQUEST_MS && slowRequestMillis >= 0 ? { slowRequestMillis } : {}),
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
class RequestTiming {
|
|
28
|
+
readonly startedAt = performance.now()
|
|
29
|
+
readonly storage = new StorageTimer()
|
|
30
|
+
authorized?: {
|
|
31
|
+
readonly at: number
|
|
32
|
+
readonly storageMillis: number
|
|
33
|
+
readonly storageCalls: number
|
|
34
|
+
readonly route: string
|
|
35
|
+
readonly showTimings: boolean
|
|
36
|
+
}
|
|
37
|
+
subject?: string
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const currentRequest = new AsyncLocalStorage<RequestTiming>()
|
|
41
|
+
|
|
42
|
+
/** The request has been authenticated and is about to run its route. */
|
|
43
|
+
export function markRequestAuthorized(route: string, showTimings: boolean): void {
|
|
44
|
+
const timing = currentRequest.getStore()
|
|
45
|
+
if (!timing) return
|
|
46
|
+
timing.authorized = {
|
|
47
|
+
at: performance.now(),
|
|
48
|
+
storageMillis: timing.storage.busyMillis,
|
|
49
|
+
storageCalls: timing.storage.calls,
|
|
50
|
+
route,
|
|
51
|
+
showTimings,
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Name what a shared route is working on, so a slow request can be told from its neighbors. */
|
|
56
|
+
export function noteRequestSubject(subject: string | undefined): void {
|
|
57
|
+
const timing = currentRequest.getStore()
|
|
58
|
+
if (timing) timing.subject = subject
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function measure(timing: RequestTiming) {
|
|
62
|
+
const totalMillis = performance.now() - timing.startedAt
|
|
63
|
+
const authMillis = timing.authorized ? timing.authorized.at - timing.startedAt : 0
|
|
64
|
+
const storageMillis = timing.storage.busyMillis - (timing.authorized?.storageMillis ?? 0)
|
|
65
|
+
return {
|
|
66
|
+
totalMillis,
|
|
67
|
+
authMillis,
|
|
68
|
+
storageMillis,
|
|
69
|
+
storageCalls: timing.storage.calls - (timing.authorized?.storageCalls ?? 0),
|
|
70
|
+
// Validating, checking permissions and building the response.
|
|
71
|
+
appMillis: Math.max(0, totalMillis - authMillis - storageMillis),
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
type RequestMeasure = ReturnType<typeof measure>
|
|
76
|
+
|
|
77
|
+
const storageCalls = (calls: number) => `${calls} ${calls === 1 ? "call" : "calls"}`
|
|
78
|
+
|
|
79
|
+
function serverTimingHeader(measured: RequestMeasure): string {
|
|
80
|
+
const metric = (name: string, millis: number, description?: string) =>
|
|
81
|
+
`${name};dur=${millis.toFixed(1)}${description ? `;desc="${description}"` : ""}`
|
|
82
|
+
return [
|
|
83
|
+
metric("auth", measured.authMillis),
|
|
84
|
+
metric("storage", measured.storageMillis, storageCalls(measured.storageCalls)),
|
|
85
|
+
metric("app", measured.appMillis),
|
|
86
|
+
metric("total", measured.totalMillis),
|
|
87
|
+
].join(", ")
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function logSlowRequest(
|
|
91
|
+
request: Request,
|
|
92
|
+
response: Response,
|
|
93
|
+
timing: RequestTiming,
|
|
94
|
+
measured: RequestMeasure
|
|
95
|
+
): void {
|
|
96
|
+
// The route's pattern, not its URL: a path or query string can hold a token or a person's data.
|
|
97
|
+
const route = timing.authorized?.route ?? "(public route)"
|
|
98
|
+
const subject = timing.subject ? ` (${timing.subject})` : ""
|
|
99
|
+
const round = Math.round
|
|
100
|
+
console.warn(
|
|
101
|
+
`[SixbServer] Slow request: ${request.method} ${route}${subject} ${response.status} in ` +
|
|
102
|
+
`${round(measured.totalMillis)} ms: auth ${round(measured.authMillis)}, ` +
|
|
103
|
+
`storage ${round(measured.storageMillis)} in ${storageCalls(measured.storageCalls)}, ` +
|
|
104
|
+
`app ${round(measured.appMillis)}`
|
|
105
|
+
)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Run one request, then say where its time went. Time after the response starts is not counted. */
|
|
109
|
+
export async function timeRequest<TResult>(
|
|
110
|
+
request: Request,
|
|
111
|
+
options: SixbRequestTimingOptions,
|
|
112
|
+
handle: () => TResult | Promise<TResult>
|
|
113
|
+
): Promise<TResult | Response> {
|
|
114
|
+
const timing = new RequestTiming()
|
|
115
|
+
const result = await currentRequest.run(timing, () => runWithStorageTimer(timing.storage, handle))
|
|
116
|
+
// A WebSocket upgrade answers with nothing, and lasts as long as the socket.
|
|
117
|
+
if (!(result instanceof Response)) return result
|
|
118
|
+
|
|
119
|
+
const measured = measure(timing)
|
|
120
|
+
const slowRequestMillis = options.slowRequestMillis ?? DEFAULT_SLOW_REQUEST_MILLIS
|
|
121
|
+
if (slowRequestMillis > 0 && measured.totalMillis >= slowRequestMillis) {
|
|
122
|
+
logSlowRequest(request, result, timing, measured)
|
|
123
|
+
}
|
|
124
|
+
if (!options.header || !timing.authorized?.showTimings) return result
|
|
125
|
+
|
|
126
|
+
const headers = new Headers(result.headers)
|
|
127
|
+
headers.set("server-timing", serverTimingHeader(measured))
|
|
128
|
+
// The app and API are usually on different hosts, and a page only reads timings it is allowed to.
|
|
129
|
+
const origin = request.headers.get("origin")
|
|
130
|
+
if (origin) headers.set("timing-allow-origin", origin)
|
|
131
|
+
return new Response(result.body, {
|
|
132
|
+
status: result.status,
|
|
133
|
+
statusText: result.statusText,
|
|
134
|
+
headers,
|
|
135
|
+
})
|
|
136
|
+
}
|