@sixb/server 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +123 -0
- package/dist/auth/access-token-boundary.d.ts +23 -0
- package/dist/auth/access-token-boundary.d.ts.map +1 -0
- package/dist/auth/application-access.d.ts +4 -0
- package/dist/auth/application-access.d.ts.map +1 -0
- package/dist/auth/browser-origin.d.ts +62 -0
- package/dist/auth/browser-origin.d.ts.map +1 -0
- package/dist/auth/csrf.d.ts +2 -0
- package/dist/auth/csrf.d.ts.map +1 -0
- package/dist/auth/guard.d.ts +35 -0
- package/dist/auth/guard.d.ts.map +1 -0
- package/dist/auth/public-routes.d.ts +8 -0
- package/dist/auth/public-routes.d.ts.map +1 -0
- package/dist/auth/responses.d.ts +11 -0
- package/dist/auth/responses.d.ts.map +1 -0
- package/dist/auth/return-to.d.ts +3 -0
- package/dist/auth/return-to.d.ts.map +1 -0
- package/dist/auth/scope.d.ts +43 -0
- package/dist/auth/scope.d.ts.map +1 -0
- package/dist/auth/session-activity.d.ts +4 -0
- package/dist/auth/session-activity.d.ts.map +1 -0
- package/dist/auth/session-cookies.d.ts +12 -0
- package/dist/auth/session-cookies.d.ts.map +1 -0
- package/dist/files/content.d.ts +141 -0
- package/dist/files/content.d.ts.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11522 -0
- package/dist/index.js.map +78 -0
- package/dist/openapi/security.d.ts +26 -0
- package/dist/openapi/security.d.ts.map +1 -0
- package/dist/openapi/tags.d.ts +151 -0
- package/dist/openapi/tags.d.ts.map +1 -0
- package/dist/registerRoutes.d.ts +31 -0
- package/dist/registerRoutes.d.ts.map +1 -0
- package/dist/routes/action-runs.d.ts +244 -0
- package/dist/routes/action-runs.d.ts.map +1 -0
- package/dist/routes/actions.d.ts +183 -0
- package/dist/routes/actions.d.ts.map +1 -0
- package/dist/routes/agent-api-gateway.d.ts +4 -0
- package/dist/routes/agent-api-gateway.d.ts.map +1 -0
- package/dist/routes/agents.d.ts +1061 -0
- package/dist/routes/agents.d.ts.map +1 -0
- package/dist/routes/auth.d.ts +1366 -0
- package/dist/routes/auth.d.ts.map +1 -0
- package/dist/routes/connectors.d.ts +108 -0
- package/dist/routes/connectors.d.ts.map +1 -0
- package/dist/routes/datasets.d.ts +339 -0
- package/dist/routes/datasets.d.ts.map +1 -0
- package/dist/routes/events.d.ts +116 -0
- package/dist/routes/events.d.ts.map +1 -0
- package/dist/routes/files.d.ts +340 -0
- package/dist/routes/files.d.ts.map +1 -0
- package/dist/routes/links.d.ts +175 -0
- package/dist/routes/links.d.ts.map +1 -0
- package/dist/routes/logs.d.ts +88 -0
- package/dist/routes/logs.d.ts.map +1 -0
- package/dist/routes/objects.d.ts +450 -0
- package/dist/routes/objects.d.ts.map +1 -0
- package/dist/routes/ontology.d.ts +240 -0
- package/dist/routes/ontology.d.ts.map +1 -0
- package/dist/routes/pipelines.d.ts +399 -0
- package/dist/routes/pipelines.d.ts.map +1 -0
- package/dist/routes/project.d.ts +56 -0
- package/dist/routes/project.d.ts.map +1 -0
- package/dist/routes/projections.d.ts +916 -0
- package/dist/routes/projections.d.ts.map +1 -0
- package/dist/routes/rules.d.ts +165 -0
- package/dist/routes/rules.d.ts.map +1 -0
- package/dist/routes/status.d.ts +81 -0
- package/dist/routes/status.d.ts.map +1 -0
- package/dist/routes/syncs.d.ts +294 -0
- package/dist/routes/syncs.d.ts.map +1 -0
- package/dist/routes/telemetry.d.ts +241 -0
- package/dist/routes/telemetry.d.ts.map +1 -0
- package/dist/routes/webhook-runs.d.ts +89 -0
- package/dist/routes/webhook-runs.d.ts.map +1 -0
- package/dist/routes/webhooks.d.ts +60 -0
- package/dist/routes/webhooks.d.ts.map +1 -0
- package/dist/routes/workflows.d.ts +973 -0
- package/dist/routes/workflows.d.ts.map +1 -0
- package/dist/routes/ws/agents.d.ts +99 -0
- package/dist/routes/ws/agents.d.ts.map +1 -0
- package/dist/routes/ws/events.d.ts +89 -0
- package/dist/routes/ws/events.d.ts.map +1 -0
- package/dist/routes/ws/index.d.ts +31 -0
- package/dist/routes/ws/index.d.ts.map +1 -0
- package/dist/routes/ws/log-subscription-hub.d.ts +47 -0
- package/dist/routes/ws/log-subscription-hub.d.ts.map +1 -0
- package/dist/routes/ws/logs.d.ts +110 -0
- package/dist/routes/ws/logs.d.ts.map +1 -0
- package/dist/schemas/actions.d.ts +644 -0
- package/dist/schemas/actions.d.ts.map +1 -0
- package/dist/schemas/agents.d.ts +3157 -0
- package/dist/schemas/agents.d.ts.map +1 -0
- package/dist/schemas/auth.d.ts +2023 -0
- package/dist/schemas/auth.d.ts.map +1 -0
- package/dist/schemas/common.d.ts +30 -0
- package/dist/schemas/common.d.ts.map +1 -0
- package/dist/schemas/connectors.d.ts +82 -0
- package/dist/schemas/connectors.d.ts.map +1 -0
- package/dist/schemas/datasets.d.ts +744 -0
- package/dist/schemas/datasets.d.ts.map +1 -0
- package/dist/schemas/event-payloads.d.ts +477 -0
- package/dist/schemas/event-payloads.d.ts.map +1 -0
- package/dist/schemas/events.d.ts +665 -0
- package/dist/schemas/events.d.ts.map +1 -0
- package/dist/schemas/files.d.ts +241 -0
- package/dist/schemas/files.d.ts.map +1 -0
- package/dist/schemas/links.d.ts +90 -0
- package/dist/schemas/links.d.ts.map +1 -0
- package/dist/schemas/logs.d.ts +231 -0
- package/dist/schemas/logs.d.ts.map +1 -0
- package/dist/schemas/objects.d.ts +288 -0
- package/dist/schemas/objects.d.ts.map +1 -0
- package/dist/schemas/ontology.d.ts +778 -0
- package/dist/schemas/ontology.d.ts.map +1 -0
- package/dist/schemas/pipelines.d.ts +1373 -0
- package/dist/schemas/pipelines.d.ts.map +1 -0
- package/dist/schemas/project.d.ts +9 -0
- package/dist/schemas/project.d.ts.map +1 -0
- package/dist/schemas/projections.d.ts +6992 -0
- package/dist/schemas/projections.d.ts.map +1 -0
- package/dist/schemas/rules.d.ts +204 -0
- package/dist/schemas/rules.d.ts.map +1 -0
- package/dist/schemas/status.d.ts +189 -0
- package/dist/schemas/status.d.ts.map +1 -0
- package/dist/schemas/syncs.d.ts +538 -0
- package/dist/schemas/syncs.d.ts.map +1 -0
- package/dist/schemas/telemetry.d.ts +265 -0
- package/dist/schemas/telemetry.d.ts.map +1 -0
- package/dist/schemas/webhook-runs.d.ts +169 -0
- package/dist/schemas/webhook-runs.d.ts.map +1 -0
- package/dist/schemas/workflows.d.ts +1720 -0
- package/dist/schemas/workflows.d.ts.map +1 -0
- package/dist/server.d.ts +64 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/tsconfig.json +5 -0
- package/dist/utils/http.d.ts +23 -0
- package/dist/utils/http.d.ts.map +1 -0
- package/dist/utils/request-body.d.ts +15 -0
- package/dist/utils/request-body.d.ts.map +1 -0
- package/dist/utils/ws.d.ts +10 -0
- package/dist/utils/ws.d.ts.map +1 -0
- package/package.json +68 -0
- package/src/auth/access-token-boundary.ts +68 -0
- package/src/auth/application-access.ts +16 -0
- package/src/auth/browser-origin.ts +367 -0
- package/src/auth/csrf.ts +1 -0
- package/src/auth/guard.ts +169 -0
- package/src/auth/public-routes.ts +87 -0
- package/src/auth/responses.ts +54 -0
- package/src/auth/return-to.ts +21 -0
- package/src/auth/scope.ts +64 -0
- package/src/auth/session-activity.ts +19 -0
- package/src/auth/session-cookies.ts +43 -0
- package/src/files/content.ts +419 -0
- package/src/index.ts +2 -0
- package/src/openapi/security.ts +37 -0
- package/src/openapi/tags.ts +151 -0
- package/src/registerRoutes.ts +49 -0
- package/src/routes/action-runs.ts +238 -0
- package/src/routes/actions.ts +126 -0
- package/src/routes/agent-api-gateway.ts +260 -0
- package/src/routes/agents.ts +943 -0
- package/src/routes/auth.ts +2233 -0
- package/src/routes/connectors.ts +69 -0
- package/src/routes/datasets.ts +402 -0
- package/src/routes/events.ts +55 -0
- package/src/routes/files.ts +675 -0
- package/src/routes/links.ts +143 -0
- package/src/routes/logs.ts +65 -0
- package/src/routes/objects.ts +838 -0
- package/src/routes/ontology.ts +138 -0
- package/src/routes/pipelines.ts +332 -0
- package/src/routes/project.ts +16 -0
- package/src/routes/projections.ts +251 -0
- package/src/routes/rules.ts +124 -0
- package/src/routes/status.ts +53 -0
- package/src/routes/syncs.ts +243 -0
- package/src/routes/telemetry.ts +207 -0
- package/src/routes/webhook-runs.ts +81 -0
- package/src/routes/webhooks.ts +508 -0
- package/src/routes/workflows.ts +1333 -0
- package/src/routes/ws/agents.ts +301 -0
- package/src/routes/ws/events.ts +278 -0
- package/src/routes/ws/index.ts +12 -0
- package/src/routes/ws/log-subscription-hub.ts +375 -0
- package/src/routes/ws/logs.ts +117 -0
- package/src/schemas/actions.ts +123 -0
- package/src/schemas/agents.ts +293 -0
- package/src/schemas/auth.ts +317 -0
- package/src/schemas/common.ts +60 -0
- package/src/schemas/connectors.ts +21 -0
- package/src/schemas/datasets.ts +107 -0
- package/src/schemas/event-payloads.ts +79 -0
- package/src/schemas/events.ts +117 -0
- package/src/schemas/files.ts +82 -0
- package/src/schemas/links.ts +38 -0
- package/src/schemas/logs.ts +76 -0
- package/src/schemas/objects.ts +759 -0
- package/src/schemas/ontology.ts +77 -0
- package/src/schemas/pipelines.ts +150 -0
- package/src/schemas/project.ts +5 -0
- package/src/schemas/projections.ts +144 -0
- package/src/schemas/rules.ts +98 -0
- package/src/schemas/status.ts +51 -0
- package/src/schemas/syncs.ts +83 -0
- package/src/schemas/telemetry.ts +59 -0
- package/src/schemas/webhook-runs.ts +40 -0
- package/src/schemas/workflows.ts +229 -0
- package/src/server.ts +383 -0
- package/src/utils/http.ts +108 -0
- package/src/utils/request-body.ts +62 -0
- package/src/utils/ws.ts +59 -0
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
LogLevel,
|
|
3
|
+
LogRunRef,
|
|
4
|
+
OntologySource,
|
|
5
|
+
Sixb,
|
|
6
|
+
SixbRunKind,
|
|
7
|
+
StoredLogLine,
|
|
8
|
+
} from "@sixb/core"
|
|
9
|
+
import { BrokerCursorExpiredError } from "@sixb/core/broker"
|
|
10
|
+
|
|
11
|
+
const MAX_CLIENT_RECORDS = 1_000
|
|
12
|
+
const MAX_CLIENT_BYTES = 1_048_576
|
|
13
|
+
const MAX_SOCKET_BUFFERED_BYTES = 1_048_576
|
|
14
|
+
const MAX_BATCH_RECORDS = 100
|
|
15
|
+
const FLUSH_DELAY_MS = 10
|
|
16
|
+
const READ_PAGE_SIZE = 500
|
|
17
|
+
|
|
18
|
+
interface LogSocket {
|
|
19
|
+
send(message: string): unknown
|
|
20
|
+
close(code?: number, reason?: string): unknown
|
|
21
|
+
readonly raw?: unknown
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface LogSubscriptionFilter {
|
|
25
|
+
readonly kinds?: readonly SixbRunKind[]
|
|
26
|
+
readonly levels?: readonly LogLevel[]
|
|
27
|
+
readonly run?: LogRunRef
|
|
28
|
+
readonly afterCursor?: string
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
interface ClientState {
|
|
32
|
+
readonly ws: LogSocket
|
|
33
|
+
readonly filter: LogSubscriptionFilter
|
|
34
|
+
readonly queue: StoredLogLine[]
|
|
35
|
+
readonly queuedCursors: Set<string>
|
|
36
|
+
readonly pendingLive: StoredLogLine[]
|
|
37
|
+
readonly pendingCursors: Set<string>
|
|
38
|
+
readonly replayedPendingCursors: Set<string>
|
|
39
|
+
queuedBytes: number
|
|
40
|
+
pendingBytes: number
|
|
41
|
+
flushTimer: ReturnType<typeof setTimeout> | null
|
|
42
|
+
readonly queueProgressWaiters: Set<() => void>
|
|
43
|
+
catchingUp: boolean
|
|
44
|
+
closed: boolean
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* One broker subscription per server/project, multiplexed to all connected
|
|
49
|
+
* clients. Each client has a bounded queue so a slow socket cannot retain an
|
|
50
|
+
* unbounded portion of the process heap.
|
|
51
|
+
*/
|
|
52
|
+
export class LogSubscriptionHub {
|
|
53
|
+
private readonly clients = new Map<object, ClientState>()
|
|
54
|
+
private readonly subscriptionGenerations = new WeakMap<object, number>()
|
|
55
|
+
private startPromise: Promise<void> | null = null
|
|
56
|
+
private unsubscribeBroker: (() => void) | null = null
|
|
57
|
+
private closed = false
|
|
58
|
+
|
|
59
|
+
constructor(private readonly sixb: Sixb<readonly OntologySource[]>) {}
|
|
60
|
+
|
|
61
|
+
async subscribe(
|
|
62
|
+
key: object,
|
|
63
|
+
ws: LogSocket,
|
|
64
|
+
filter: LogSubscriptionFilter,
|
|
65
|
+
onSubscribed: () => void
|
|
66
|
+
): Promise<void> {
|
|
67
|
+
if (this.closed) throw new Error("Log subscription hub is closed")
|
|
68
|
+
const generation = this.nextSubscriptionGeneration(key)
|
|
69
|
+
this.removeClient(key)
|
|
70
|
+
|
|
71
|
+
try {
|
|
72
|
+
await this.ensureStarted()
|
|
73
|
+
} catch (error) {
|
|
74
|
+
if (this.closed || !this.isCurrentSubscription(key, generation)) return
|
|
75
|
+
throw error
|
|
76
|
+
}
|
|
77
|
+
if (this.closed || !this.isCurrentSubscription(key, generation)) return
|
|
78
|
+
|
|
79
|
+
const state: ClientState = {
|
|
80
|
+
ws,
|
|
81
|
+
filter,
|
|
82
|
+
queue: [],
|
|
83
|
+
queuedCursors: new Set(),
|
|
84
|
+
pendingLive: [],
|
|
85
|
+
pendingCursors: new Set(),
|
|
86
|
+
replayedPendingCursors: new Set(),
|
|
87
|
+
queuedBytes: 0,
|
|
88
|
+
pendingBytes: 0,
|
|
89
|
+
flushTimer: null,
|
|
90
|
+
queueProgressWaiters: new Set(),
|
|
91
|
+
catchingUp: filter.afterCursor !== undefined,
|
|
92
|
+
closed: false,
|
|
93
|
+
}
|
|
94
|
+
this.clients.set(key, state)
|
|
95
|
+
onSubscribed()
|
|
96
|
+
|
|
97
|
+
if (state.catchingUp) {
|
|
98
|
+
void this.catchUp(key, state)
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
unsubscribe(key: object): void {
|
|
103
|
+
this.nextSubscriptionGeneration(key)
|
|
104
|
+
this.removeClient(key)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
private removeClient(key: object): void {
|
|
108
|
+
const state = this.clients.get(key)
|
|
109
|
+
if (!state) return
|
|
110
|
+
state.closed = true
|
|
111
|
+
if (state.flushTimer) clearTimeout(state.flushTimer)
|
|
112
|
+
this.notifyQueueProgress(state)
|
|
113
|
+
this.clients.delete(key)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
private nextSubscriptionGeneration(key: object): number {
|
|
117
|
+
const generation = (this.subscriptionGenerations.get(key) ?? 0) + 1
|
|
118
|
+
this.subscriptionGenerations.set(key, generation)
|
|
119
|
+
return generation
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
private isCurrentSubscription(key: object, generation: number): boolean {
|
|
123
|
+
return this.subscriptionGenerations.get(key) === generation
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async close(): Promise<void> {
|
|
127
|
+
this.closed = true
|
|
128
|
+
this.unsubscribeBroker?.()
|
|
129
|
+
this.unsubscribeBroker = null
|
|
130
|
+
for (const key of this.clients.keys()) this.unsubscribe(key)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
private async ensureStarted(): Promise<void> {
|
|
134
|
+
if (this.closed) throw new Error("Log subscription hub is closed")
|
|
135
|
+
if (!this.startPromise) {
|
|
136
|
+
this.startPromise = this.sixb.logs
|
|
137
|
+
.subscribe({ from: "latest" }, (lines) => this.deliverLive(lines))
|
|
138
|
+
.then((unsubscribe) => {
|
|
139
|
+
if (this.closed) unsubscribe()
|
|
140
|
+
else this.unsubscribeBroker = unsubscribe
|
|
141
|
+
})
|
|
142
|
+
.catch((error) => {
|
|
143
|
+
this.startPromise = null
|
|
144
|
+
throw error
|
|
145
|
+
})
|
|
146
|
+
}
|
|
147
|
+
await this.startPromise
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
private deliverLive(lines: readonly StoredLogLine[]): void {
|
|
151
|
+
for (const state of this.clients.values()) {
|
|
152
|
+
for (const line of lines) {
|
|
153
|
+
if (!matches(line, state.filter)) continue
|
|
154
|
+
if (state.catchingUp) this.enqueuePendingLive(state, line)
|
|
155
|
+
else this.enqueue(state, line)
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
private async catchUp(key: object, state: ClientState): Promise<void> {
|
|
161
|
+
let afterCursor = state.filter.afterCursor
|
|
162
|
+
try {
|
|
163
|
+
let hasMore = true
|
|
164
|
+
while (hasMore) {
|
|
165
|
+
const page = await this.sixb.logs.read({
|
|
166
|
+
afterCursor,
|
|
167
|
+
limit: READ_PAGE_SIZE,
|
|
168
|
+
kinds: state.filter.kinds,
|
|
169
|
+
levels: state.filter.levels,
|
|
170
|
+
run: state.filter.run,
|
|
171
|
+
})
|
|
172
|
+
if (state.closed || this.clients.get(key) !== state) return
|
|
173
|
+
|
|
174
|
+
for (const line of page.lines) {
|
|
175
|
+
if (state.pendingCursors.has(line.cursor)) {
|
|
176
|
+
state.replayedPendingCursors.add(line.cursor)
|
|
177
|
+
}
|
|
178
|
+
if (!(await this.enqueueReplay(state, line))) return
|
|
179
|
+
}
|
|
180
|
+
if (state.closed) return
|
|
181
|
+
afterCursor = page.cursor ?? afterCursor
|
|
182
|
+
hasMore = page.hasMore
|
|
183
|
+
if (hasMore && !page.cursor) {
|
|
184
|
+
throw new Error("Log broker returned hasMore without a cursor")
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
state.catchingUp = false
|
|
189
|
+
const pendingLive = [...state.pendingLive]
|
|
190
|
+
const replayedPendingCursors = new Set(state.replayedPendingCursors)
|
|
191
|
+
state.pendingLive.length = 0
|
|
192
|
+
state.pendingCursors.clear()
|
|
193
|
+
state.replayedPendingCursors.clear()
|
|
194
|
+
state.pendingBytes = 0
|
|
195
|
+
for (const line of pendingLive) {
|
|
196
|
+
if (!replayedPendingCursors.has(line.cursor)) this.enqueue(state, line)
|
|
197
|
+
}
|
|
198
|
+
} catch (error) {
|
|
199
|
+
if (state.closed || this.clients.get(key) !== state) return
|
|
200
|
+
state.catchingUp = false
|
|
201
|
+
state.pendingLive.length = 0
|
|
202
|
+
state.pendingCursors.clear()
|
|
203
|
+
state.replayedPendingCursors.clear()
|
|
204
|
+
state.pendingBytes = 0
|
|
205
|
+
|
|
206
|
+
if (error instanceof BrokerCursorExpiredError) {
|
|
207
|
+
try {
|
|
208
|
+
const latest = await this.sixb.logs.tail({ limit: 1 })
|
|
209
|
+
this.send(state, {
|
|
210
|
+
type: "reset",
|
|
211
|
+
reason: "cursor_expired",
|
|
212
|
+
cursor: latest.lines.at(-1)?.cursor ?? latest.cursor,
|
|
213
|
+
})
|
|
214
|
+
} catch (resetError) {
|
|
215
|
+
this.fail(
|
|
216
|
+
state,
|
|
217
|
+
resetError instanceof Error ? resetError.message : String(resetError),
|
|
218
|
+
1011
|
|
219
|
+
)
|
|
220
|
+
}
|
|
221
|
+
return
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
this.fail(state, error instanceof Error ? error.message : String(error), 1011)
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
private enqueue(state: ClientState, line: StoredLogLine): void {
|
|
229
|
+
if (state.closed || state.queuedCursors.has(line.cursor)) return
|
|
230
|
+
const bytes = encodedBytes(line)
|
|
231
|
+
if (!hasQueueCapacity(state, bytes)) {
|
|
232
|
+
this.fail(state, "Log stream client is too slow; reconnect from the last cursor.", 1013)
|
|
233
|
+
return
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
this.pushQueuedLine(state, line, bytes)
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
private async enqueueReplay(state: ClientState, line: StoredLogLine): Promise<boolean> {
|
|
240
|
+
if (state.closed) return false
|
|
241
|
+
if (state.queuedCursors.has(line.cursor)) return true
|
|
242
|
+
const bytes = encodedBytes(line)
|
|
243
|
+
|
|
244
|
+
while (!hasQueueCapacity(state, bytes)) {
|
|
245
|
+
// Replay is an internal producer and can pause while already-queued replay
|
|
246
|
+
// batches drain. Pending live records cannot drain until replay finishes,
|
|
247
|
+
// so a queue containing only pending live records has no forward progress.
|
|
248
|
+
if (state.queue.length === 0 || socketBufferedAmount(state.ws) > MAX_SOCKET_BUFFERED_BYTES) {
|
|
249
|
+
this.fail(state, "Log stream client is too slow; reconnect from the last cursor.", 1013)
|
|
250
|
+
return false
|
|
251
|
+
}
|
|
252
|
+
this.scheduleFlush(state)
|
|
253
|
+
await this.waitForQueueProgress(state)
|
|
254
|
+
if (state.closed) return false
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
this.pushQueuedLine(state, line, bytes)
|
|
258
|
+
return !state.closed
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
private pushQueuedLine(state: ClientState, line: StoredLogLine, bytes: number): void {
|
|
262
|
+
state.queue.push(line)
|
|
263
|
+
state.queuedCursors.add(line.cursor)
|
|
264
|
+
state.queuedBytes += bytes
|
|
265
|
+
this.scheduleFlush(state)
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
private enqueuePendingLive(state: ClientState, line: StoredLogLine): void {
|
|
269
|
+
if (state.closed || state.pendingCursors.has(line.cursor)) return
|
|
270
|
+
const bytes = encodedBytes(line)
|
|
271
|
+
if (!hasQueueCapacity(state, bytes)) {
|
|
272
|
+
this.fail(state, "Log stream client is too slow; reconnect from the last cursor.", 1013)
|
|
273
|
+
return
|
|
274
|
+
}
|
|
275
|
+
state.pendingLive.push(line)
|
|
276
|
+
state.pendingCursors.add(line.cursor)
|
|
277
|
+
state.pendingBytes += bytes
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
private scheduleFlush(state: ClientState): void {
|
|
281
|
+
if (state.closed || state.flushTimer) return
|
|
282
|
+
state.flushTimer = setTimeout(() => {
|
|
283
|
+
state.flushTimer = null
|
|
284
|
+
this.flush(state)
|
|
285
|
+
}, FLUSH_DELAY_MS)
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
private flush(state: ClientState): void {
|
|
289
|
+
if (state.closed || state.queue.length === 0) return
|
|
290
|
+
if (socketBufferedAmount(state.ws) > MAX_SOCKET_BUFFERED_BYTES) {
|
|
291
|
+
this.scheduleFlush(state)
|
|
292
|
+
return
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const lines = state.queue.splice(0, MAX_BATCH_RECORDS)
|
|
296
|
+
for (const line of lines) {
|
|
297
|
+
state.queuedCursors.delete(line.cursor)
|
|
298
|
+
state.queuedBytes -= encodedBytes(line)
|
|
299
|
+
}
|
|
300
|
+
this.notifyQueueProgress(state)
|
|
301
|
+
this.send(state, { type: "logs", logs: lines })
|
|
302
|
+
if (state.queue.length > 0) this.scheduleFlush(state)
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
private waitForQueueProgress(state: ClientState): Promise<void> {
|
|
306
|
+
return new Promise((resolve) => state.queueProgressWaiters.add(resolve))
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
private notifyQueueProgress(state: ClientState): void {
|
|
310
|
+
const waiters = [...state.queueProgressWaiters]
|
|
311
|
+
state.queueProgressWaiters.clear()
|
|
312
|
+
for (const resolve of waiters) resolve()
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
private send(state: ClientState, payload: unknown): void {
|
|
316
|
+
if (state.closed) return
|
|
317
|
+
try {
|
|
318
|
+
state.ws.send(JSON.stringify(payload))
|
|
319
|
+
} catch (error) {
|
|
320
|
+
this.fail(state, error instanceof Error ? error.message : String(error), 1011)
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
private fail(state: ClientState, message: string, closeCode: number): void {
|
|
325
|
+
if (state.closed) return
|
|
326
|
+
try {
|
|
327
|
+
state.ws.send(JSON.stringify({ type: "error", message }))
|
|
328
|
+
} catch {
|
|
329
|
+
// The close below is still required when the error frame cannot be sent.
|
|
330
|
+
}
|
|
331
|
+
state.closed = true
|
|
332
|
+
if (state.flushTimer) clearTimeout(state.flushTimer)
|
|
333
|
+
this.notifyQueueProgress(state)
|
|
334
|
+
for (const [key, candidate] of this.clients) {
|
|
335
|
+
if (candidate === state) this.clients.delete(key)
|
|
336
|
+
}
|
|
337
|
+
try {
|
|
338
|
+
state.ws.close(
|
|
339
|
+
closeCode,
|
|
340
|
+
closeCode === 1013 ? "Log stream backpressure" : "Log stream failure"
|
|
341
|
+
)
|
|
342
|
+
} catch {
|
|
343
|
+
// Socket is already gone.
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
function hasQueueCapacity(state: ClientState, bytes: number): boolean {
|
|
349
|
+
return (
|
|
350
|
+
state.queue.length + state.pendingLive.length < MAX_CLIENT_RECORDS &&
|
|
351
|
+
state.queuedBytes + state.pendingBytes + bytes <= MAX_CLIENT_BYTES
|
|
352
|
+
)
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
function matches(line: StoredLogLine, filter: LogSubscriptionFilter): boolean {
|
|
356
|
+
if (filter.run) {
|
|
357
|
+
if (line.context.run.kind !== filter.run.kind || line.context.run.id !== filter.run.id) {
|
|
358
|
+
return false
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
if (filter.kinds && !filter.kinds.includes(line.context.run.kind)) return false
|
|
362
|
+
if (filter.levels && !filter.levels.includes(line.level)) return false
|
|
363
|
+
return true
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
function encodedBytes(value: unknown): number {
|
|
367
|
+
return new TextEncoder().encode(JSON.stringify(value)).byteLength
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
function socketBufferedAmount(ws: LogSocket): number {
|
|
371
|
+
const raw = ws.raw
|
|
372
|
+
if (!raw || typeof raw !== "object" || !("bufferedAmount" in raw)) return 0
|
|
373
|
+
const bufferedAmount = (raw as { bufferedAmount?: unknown }).bufferedAmount
|
|
374
|
+
return typeof bufferedAmount === "number" ? bufferedAmount : 0
|
|
375
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { isAllowed, type LogLevel } from "@sixb/core"
|
|
2
|
+
import type { Elysia } from "elysia"
|
|
3
|
+
import { z } from "zod"
|
|
4
|
+
import { LOG_LEVELS, LogRunIdSchema, SIXB_RUN_KINDS } from "../../schemas/logs"
|
|
5
|
+
import type { SixbServer } from "../../server"
|
|
6
|
+
import { decodeWsMessage, safeSend, wsAuthz, wsStateKey } from "../../utils/ws"
|
|
7
|
+
import { LogSubscriptionHub } from "./log-subscription-hub"
|
|
8
|
+
|
|
9
|
+
const SubscribeSchema = z
|
|
10
|
+
.object({
|
|
11
|
+
type: z.literal("subscribe"),
|
|
12
|
+
kinds: z.array(z.enum(SIXB_RUN_KINDS)).min(1).optional(),
|
|
13
|
+
levels: z.array(z.enum(LOG_LEVELS)).min(1).optional(),
|
|
14
|
+
run: z
|
|
15
|
+
.object({
|
|
16
|
+
kind: z.enum(SIXB_RUN_KINDS),
|
|
17
|
+
id: LogRunIdSchema,
|
|
18
|
+
})
|
|
19
|
+
.optional(),
|
|
20
|
+
afterCursor: z.string().optional(),
|
|
21
|
+
})
|
|
22
|
+
.superRefine((value, context) => {
|
|
23
|
+
if (value.run && value.kinds && !value.kinds.includes(value.run.kind)) {
|
|
24
|
+
context.addIssue({
|
|
25
|
+
code: z.ZodIssueCode.custom,
|
|
26
|
+
path: ["run"],
|
|
27
|
+
message: "run.kind must be included in kinds",
|
|
28
|
+
})
|
|
29
|
+
}
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
const UnsubscribeSchema = z.object({ type: z.literal("unsubscribe") })
|
|
33
|
+
const SubscriptionMessageSchema = z.union([SubscribeSchema, UnsubscribeSchema])
|
|
34
|
+
|
|
35
|
+
export function parseLogSubscriptionMessage(
|
|
36
|
+
payload: unknown
|
|
37
|
+
): { ok: true; data: z.infer<typeof SubscriptionMessageSchema> } | { ok: false; error: string } {
|
|
38
|
+
if (!payload || typeof payload !== "object") {
|
|
39
|
+
return { ok: false, error: "Message must be a JSON object." }
|
|
40
|
+
}
|
|
41
|
+
const parsed = SubscriptionMessageSchema.safeParse(payload)
|
|
42
|
+
if (!parsed.success) {
|
|
43
|
+
return {
|
|
44
|
+
ok: false,
|
|
45
|
+
error: parsed.error.issues[0]?.message ?? "Invalid websocket message.",
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return { ok: true, data: parsed.data }
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function registerLogStreamRoutes(app: Elysia, server: SixbServer) {
|
|
52
|
+
const hub = new LogSubscriptionHub(server.getSixb())
|
|
53
|
+
|
|
54
|
+
app.onStop(() => hub.close())
|
|
55
|
+
return app.ws("/ws/logs", {
|
|
56
|
+
open(ws) {
|
|
57
|
+
if (!isAllowed(wsAuthz(ws), { kind: "logs.observe" })) {
|
|
58
|
+
ws.close(1008, "Missing required capability 'observe:logs'.")
|
|
59
|
+
return
|
|
60
|
+
}
|
|
61
|
+
safeSend(ws, { type: "connected", channel: "logs" })
|
|
62
|
+
},
|
|
63
|
+
|
|
64
|
+
async message(ws, message) {
|
|
65
|
+
if (!isAllowed(wsAuthz(ws), { kind: "logs.observe" })) {
|
|
66
|
+
ws.close(1008, "Missing required capability 'observe:logs'.")
|
|
67
|
+
return
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const parsed = parseLogSubscriptionMessage(await decodeWsMessage(message))
|
|
71
|
+
if (!parsed.ok) {
|
|
72
|
+
safeSend(ws, { type: "error", message: parsed.error })
|
|
73
|
+
return
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const key = wsStateKey(ws)
|
|
77
|
+
if (parsed.data.type === "unsubscribe") {
|
|
78
|
+
hub.unsubscribe(key)
|
|
79
|
+
safeSend(ws, { type: "unsubscribed" })
|
|
80
|
+
return
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const subscription = parsed.data
|
|
84
|
+
const levels = subscription.levels as readonly LogLevel[] | undefined
|
|
85
|
+
try {
|
|
86
|
+
await hub.subscribe(
|
|
87
|
+
key,
|
|
88
|
+
ws,
|
|
89
|
+
{
|
|
90
|
+
kinds: subscription.kinds,
|
|
91
|
+
levels,
|
|
92
|
+
run: subscription.run,
|
|
93
|
+
afterCursor: subscription.afterCursor,
|
|
94
|
+
},
|
|
95
|
+
() =>
|
|
96
|
+
safeSend(ws, {
|
|
97
|
+
type: "subscribed",
|
|
98
|
+
kinds: subscription.kinds ?? null,
|
|
99
|
+
levels: levels ?? null,
|
|
100
|
+
run: subscription.run ?? null,
|
|
101
|
+
afterCursor: subscription.afterCursor ?? null,
|
|
102
|
+
})
|
|
103
|
+
)
|
|
104
|
+
} catch (error) {
|
|
105
|
+
safeSend(ws, {
|
|
106
|
+
type: "error",
|
|
107
|
+
message: error instanceof Error ? error.message : String(error),
|
|
108
|
+
})
|
|
109
|
+
ws.close(1011, "Log stream setup failed")
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
|
|
113
|
+
close(ws) {
|
|
114
|
+
hub.unsubscribe(wsStateKey(ws))
|
|
115
|
+
},
|
|
116
|
+
})
|
|
117
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { z } from "zod"
|
|
2
|
+
import { ActionParamSchema } from "./ontology"
|
|
3
|
+
|
|
4
|
+
export const ActionIdParamsSchema = z.object({
|
|
5
|
+
actionId: z.string().min(1),
|
|
6
|
+
})
|
|
7
|
+
|
|
8
|
+
export const ActionRunIdParamsSchema = z.object({
|
|
9
|
+
runId: z.string().min(1),
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
export const ActionSubjectSchema = z.discriminatedUnion("kind", [
|
|
13
|
+
z.object({
|
|
14
|
+
kind: z.literal("none"),
|
|
15
|
+
}),
|
|
16
|
+
z.object({
|
|
17
|
+
kind: z.literal("object"),
|
|
18
|
+
objectTypeId: z.string().min(1),
|
|
19
|
+
primaryId: z.string().min(1),
|
|
20
|
+
}),
|
|
21
|
+
])
|
|
22
|
+
|
|
23
|
+
export const RequestActionBodySchema = z.object({
|
|
24
|
+
subject: ActionSubjectSchema.optional(),
|
|
25
|
+
params: z.record(z.unknown()).optional(),
|
|
26
|
+
runId: z.string().min(1).optional(),
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
export const RequestActionResponseSchema = z.object({
|
|
30
|
+
runId: z.string(),
|
|
31
|
+
queuedAt: z.string(),
|
|
32
|
+
jobId: z.string().optional(),
|
|
33
|
+
created: z.boolean(),
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
export const ActionCatalogItemSchema = z.object({
|
|
37
|
+
id: z.string(),
|
|
38
|
+
name: z.string(),
|
|
39
|
+
description: z.string().optional(),
|
|
40
|
+
objectTypeId: z.string().optional(),
|
|
41
|
+
params: z.array(ActionParamSchema),
|
|
42
|
+
phases: z.object({
|
|
43
|
+
validate: z.boolean(),
|
|
44
|
+
writeback: z.boolean(),
|
|
45
|
+
edits: z.boolean(),
|
|
46
|
+
effects: z.boolean(),
|
|
47
|
+
}),
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
export const ActionRunStatusSchema = z.enum([
|
|
51
|
+
"queued",
|
|
52
|
+
"running",
|
|
53
|
+
"succeeded",
|
|
54
|
+
"failed",
|
|
55
|
+
"cancelled",
|
|
56
|
+
])
|
|
57
|
+
|
|
58
|
+
export const ActionRunPhaseSchema = z.enum([
|
|
59
|
+
"request",
|
|
60
|
+
"enqueue",
|
|
61
|
+
"validation",
|
|
62
|
+
"writeback",
|
|
63
|
+
"edits",
|
|
64
|
+
"commit",
|
|
65
|
+
"effects",
|
|
66
|
+
"cancelled",
|
|
67
|
+
])
|
|
68
|
+
|
|
69
|
+
export const ActionRunsQuerySchema = z.object({
|
|
70
|
+
actionId: z.string().optional(),
|
|
71
|
+
status: ActionRunStatusSchema.optional(),
|
|
72
|
+
objectTypeId: z.string().optional(),
|
|
73
|
+
primaryId: z.string().optional(),
|
|
74
|
+
startedAfter: z.string().optional(),
|
|
75
|
+
startedBefore: z.string().optional(),
|
|
76
|
+
limit: z.string().optional(),
|
|
77
|
+
offset: z.string().optional(),
|
|
78
|
+
order: z.enum(["asc", "desc"]).optional(),
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
export const ActionRunFailureSchema = z.object({
|
|
82
|
+
name: z.string().optional(),
|
|
83
|
+
message: z.string(),
|
|
84
|
+
phase: ActionRunPhaseSchema.optional(),
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
export const ActionRunSummarySchema = z.object({
|
|
88
|
+
id: z.string(),
|
|
89
|
+
projectId: z.string(),
|
|
90
|
+
actionId: z.string(),
|
|
91
|
+
subject: ActionSubjectSchema,
|
|
92
|
+
status: ActionRunStatusSchema,
|
|
93
|
+
phase: ActionRunPhaseSchema.optional(),
|
|
94
|
+
queuedAt: z.string(),
|
|
95
|
+
startedAt: z.string().optional(),
|
|
96
|
+
finishedAt: z.string().optional(),
|
|
97
|
+
error: ActionRunFailureSchema.optional(),
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
const ActionRunWritebackRecordSchema = z.object({
|
|
101
|
+
status: z.enum(["succeeded", "failed"]),
|
|
102
|
+
completedAt: z.string(),
|
|
103
|
+
result: z.unknown().optional(),
|
|
104
|
+
error: ActionRunFailureSchema.optional(),
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
const ActionRunEffectsRecordSchema = z.object({
|
|
108
|
+
status: z.enum(["succeeded", "failed"]),
|
|
109
|
+
completedAt: z.string(),
|
|
110
|
+
error: ActionRunFailureSchema.optional(),
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
export const ActionRunDetailSchema = ActionRunSummarySchema.extend({
|
|
114
|
+
params: z.record(z.unknown()),
|
|
115
|
+
writeback: ActionRunWritebackRecordSchema.optional(),
|
|
116
|
+
effects: ActionRunEffectsRecordSchema.optional(),
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
export const ActionRunListResponseSchema = z.object({
|
|
120
|
+
runs: z.array(ActionRunSummarySchema),
|
|
121
|
+
hasMore: z.boolean(),
|
|
122
|
+
total: z.number(),
|
|
123
|
+
})
|