@sixb/server 0.1.1 → 0.1.3
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 +3 -3
- package/dist/auth/application-access.d.ts +2 -2
- package/dist/auth/application-access.d.ts.map +1 -1
- package/dist/auth/connector-callback-cookie.d.ts +12 -0
- package/dist/auth/connector-callback-cookie.d.ts.map +1 -0
- package/dist/auth/experience.d.ts +33 -0
- package/dist/auth/experience.d.ts.map +1 -0
- package/dist/auth/guard.d.ts +3 -3
- package/dist/auth/guard.d.ts.map +1 -1
- package/dist/auth/public-routes.d.ts.map +1 -1
- package/dist/auth/scope.d.ts +13 -27
- package/dist/auth/scope.d.ts.map +1 -1
- package/dist/auth/session-cookies.d.ts +2 -2
- package/dist/auth/session-cookies.d.ts.map +1 -1
- package/dist/connectors/http.d.ts +16 -0
- package/dist/connectors/http.d.ts.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3903 -2760
- package/dist/index.js.map +58 -50
- package/dist/openapi/tags.d.ts +15 -3
- package/dist/openapi/tags.d.ts.map +1 -1
- package/dist/registerRoutes.d.ts +6 -2
- package/dist/registerRoutes.d.ts.map +1 -1
- package/dist/routes/action-runs.d.ts +21 -31
- package/dist/routes/action-runs.d.ts.map +1 -1
- package/dist/routes/actions.d.ts +12 -11
- package/dist/routes/actions.d.ts.map +1 -1
- package/dist/routes/agent-api-gateway.d.ts +2 -2
- package/dist/routes/agent-api-gateway.d.ts.map +1 -1
- package/dist/routes/agents.d.ts +230 -159
- package/dist/routes/agents.d.ts.map +1 -1
- package/dist/routes/ai-accounting.d.ts +31 -0
- package/dist/routes/ai-accounting.d.ts.map +1 -0
- package/dist/routes/auth.d.ts +149 -92
- package/dist/routes/auth.d.ts.map +1 -1
- package/dist/routes/connector-connection-runs.d.ts +794 -0
- package/dist/routes/connector-connection-runs.d.ts.map +1 -0
- package/dist/routes/connector-connections.d.ts +219 -0
- package/dist/routes/connector-connections.d.ts.map +1 -0
- package/dist/routes/connectors.d.ts +10 -4
- package/dist/routes/connectors.d.ts.map +1 -1
- package/dist/routes/datasets.d.ts +16 -12
- package/dist/routes/datasets.d.ts.map +1 -1
- package/dist/routes/events.d.ts +9 -9
- package/dist/routes/events.d.ts.map +1 -1
- package/dist/routes/files.d.ts +19 -18
- package/dist/routes/files.d.ts.map +1 -1
- package/dist/routes/links.d.ts +11 -11
- package/dist/routes/links.d.ts.map +1 -1
- package/dist/routes/logs.d.ts +5 -5
- package/dist/routes/logs.d.ts.map +1 -1
- package/dist/routes/objects.d.ts +37 -37
- package/dist/routes/objects.d.ts.map +1 -1
- package/dist/routes/ontology.d.ts +50 -50
- package/dist/routes/ontology.d.ts.map +1 -1
- package/dist/routes/pipelines.d.ts +28 -38
- package/dist/routes/pipelines.d.ts.map +1 -1
- package/dist/routes/project.d.ts +2 -2
- package/dist/routes/project.d.ts.map +1 -1
- package/dist/routes/projections.d.ts +145 -85
- package/dist/routes/projections.d.ts.map +1 -1
- package/dist/routes/rules.d.ts +6 -6
- package/dist/routes/rules.d.ts.map +1 -1
- package/dist/routes/status.d.ts +2 -2
- package/dist/routes/status.d.ts.map +1 -1
- package/dist/routes/syncs.d.ts +23 -28
- package/dist/routes/syncs.d.ts.map +1 -1
- package/dist/routes/telemetry.d.ts +9 -9
- package/dist/routes/telemetry.d.ts.map +1 -1
- package/dist/routes/webhook-runs.d.ts +9 -9
- package/dist/routes/webhook-runs.d.ts.map +1 -1
- package/dist/routes/webhooks.d.ts +2 -2
- package/dist/routes/webhooks.d.ts.map +1 -1
- package/dist/routes/workflows.d.ts +177 -66
- package/dist/routes/workflows.d.ts.map +1 -1
- package/dist/routes/ws/agents.d.ts +11 -5
- package/dist/routes/ws/agents.d.ts.map +1 -1
- package/dist/routes/ws/events.d.ts +8 -8
- package/dist/routes/ws/events.d.ts.map +1 -1
- package/dist/routes/ws/log-subscription-hub.d.ts +6 -3
- package/dist/routes/ws/log-subscription-hub.d.ts.map +1 -1
- package/dist/routes/ws/logs.d.ts +10 -10
- package/dist/routes/ws/logs.d.ts.map +1 -1
- package/dist/schemas/actions.d.ts +89 -194
- package/dist/schemas/actions.d.ts.map +1 -1
- package/dist/schemas/agents.d.ts +791 -471
- package/dist/schemas/agents.d.ts.map +1 -1
- package/dist/schemas/ai-accounting.d.ts +2393 -0
- package/dist/schemas/ai-accounting.d.ts.map +1 -0
- package/dist/schemas/ai-usage.d.ts +34 -0
- package/dist/schemas/ai-usage.d.ts.map +1 -0
- package/dist/schemas/auth.d.ts +102 -102
- package/dist/schemas/common.d.ts +37 -0
- package/dist/schemas/common.d.ts.map +1 -1
- package/dist/schemas/connectors.d.ts +713 -2
- package/dist/schemas/connectors.d.ts.map +1 -1
- package/dist/schemas/datasets.d.ts +2 -2
- package/dist/schemas/event-payloads.d.ts +12 -12
- package/dist/schemas/events.d.ts +40 -40
- package/dist/schemas/links.d.ts +2 -2
- package/dist/schemas/logs.d.ts +22 -22
- package/dist/schemas/objects.d.ts +10 -10
- package/dist/schemas/ontology.d.ts +118 -118
- package/dist/schemas/pipelines.d.ts +89 -184
- package/dist/schemas/pipelines.d.ts.map +1 -1
- package/dist/schemas/projections.d.ts +1030 -563
- package/dist/schemas/projections.d.ts.map +1 -1
- package/dist/schemas/rules.d.ts +6 -6
- package/dist/schemas/syncs.d.ts +65 -108
- package/dist/schemas/syncs.d.ts.map +1 -1
- package/dist/schemas/webhook-runs.d.ts +41 -41
- package/dist/schemas/webhook-runs.d.ts.map +1 -1
- package/dist/schemas/workflows.d.ts +1189 -185
- package/dist/schemas/workflows.d.ts.map +1 -1
- package/dist/server.d.ts +11 -6
- package/dist/server.d.ts.map +1 -1
- package/dist/utils/http.d.ts +2 -0
- package/dist/utils/http.d.ts.map +1 -1
- package/dist/utils/ws.d.ts +4 -3
- package/dist/utils/ws.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/auth/access-token-boundary.ts +4 -4
- package/src/auth/application-access.ts +8 -9
- package/src/auth/connector-callback-cookie.ts +95 -0
- package/src/auth/experience.ts +190 -0
- package/src/auth/guard.ts +11 -11
- package/src/auth/public-routes.ts +20 -0
- package/src/auth/scope.ts +26 -36
- package/src/auth/session-cookies.ts +3 -3
- package/src/connectors/http.ts +110 -0
- package/src/index.ts +1 -0
- package/src/openapi/tags.ts +13 -1
- package/src/registerRoutes.ts +36 -23
- package/src/routes/action-runs.ts +39 -39
- package/src/routes/actions.ts +7 -13
- package/src/routes/agent-api-gateway.ts +41 -38
- package/src/routes/agents.ts +90 -200
- package/src/routes/ai-accounting.ts +166 -0
- package/src/routes/auth.ts +274 -122
- package/src/routes/connector-connection-runs.ts +346 -0
- package/src/routes/connector-connections.ts +135 -0
- package/src/routes/connectors.ts +24 -12
- package/src/routes/datasets.ts +55 -54
- package/src/routes/events.ts +5 -5
- package/src/routes/files.ts +22 -30
- package/src/routes/links.ts +31 -35
- package/src/routes/logs.ts +6 -11
- package/src/routes/objects.ts +51 -114
- package/src/routes/ontology.ts +27 -38
- package/src/routes/pipelines.ts +28 -62
- package/src/routes/project.ts +3 -3
- package/src/routes/projections.ts +44 -42
- package/src/routes/rules.ts +11 -25
- package/src/routes/status.ts +5 -5
- package/src/routes/syncs.ts +19 -45
- package/src/routes/telemetry.ts +18 -29
- package/src/routes/webhook-runs.ts +5 -5
- package/src/routes/webhooks.ts +259 -341
- package/src/routes/workflows.ts +242 -248
- package/src/routes/ws/agents.ts +196 -66
- package/src/routes/ws/events.ts +11 -10
- package/src/routes/ws/log-subscription-hub.ts +6 -12
- package/src/routes/ws/logs.ts +13 -5
- package/src/schemas/actions.ts +58 -25
- package/src/schemas/agents.ts +23 -23
- package/src/schemas/ai-accounting.ts +182 -0
- package/src/schemas/ai-usage.ts +16 -0
- package/src/schemas/common.ts +24 -0
- package/src/schemas/connectors.ts +164 -0
- package/src/schemas/pipelines.ts +23 -15
- package/src/schemas/projections.ts +23 -3
- package/src/schemas/syncs.ts +13 -8
- package/src/schemas/webhook-runs.ts +10 -5
- package/src/schemas/workflows.ts +23 -8
- package/src/server.ts +75 -26
- package/src/utils/http.ts +34 -6
- package/src/utils/ws.ts +10 -5
package/src/routes/ws/agents.ts
CHANGED
|
@@ -1,13 +1,22 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import type { OntologySource } from "@sixb/core"
|
|
2
|
+
import {
|
|
3
|
+
AGENT_ACTIVITY_STREAM_ID,
|
|
4
|
+
type AgentRunActivityEvent,
|
|
5
|
+
agentActivityStreamDefinition,
|
|
6
|
+
agentRunStreamDefinition,
|
|
7
|
+
agentRunStreamId,
|
|
8
|
+
isAgentRunActivityEvent,
|
|
9
|
+
} from "@sixb/core/agents/streams"
|
|
10
|
+
import { BrokerCursorExpiredError, type BrokerRecord } from "@sixb/core/broker"
|
|
11
|
+
import type { Sixb } from "@sixb/core/internal/request-execution"
|
|
4
12
|
import type { Elysia } from "elysia"
|
|
5
13
|
import { z } from "zod"
|
|
6
14
|
import type { SixbServer } from "../../server"
|
|
7
|
-
import { decodeWsMessage, safeSend,
|
|
15
|
+
import { decodeWsMessage, safeSend, wsRequestSixb, wsStateKey } from "../../utils/ws"
|
|
8
16
|
import { serializeAgentRun } from "../agents"
|
|
9
17
|
|
|
10
18
|
interface AgentStreamSubscriptionState {
|
|
19
|
+
channel: "run" | "activity" | null
|
|
11
20
|
runId: string | null
|
|
12
21
|
unsubscribe: (() => void) | null
|
|
13
22
|
}
|
|
@@ -18,6 +27,10 @@ const SubscribeSchema = z.object({
|
|
|
18
27
|
afterCursor: z.string().min(1).optional(),
|
|
19
28
|
})
|
|
20
29
|
|
|
30
|
+
const SubscribeActivitySchema = z.object({
|
|
31
|
+
type: z.literal("subscribe.activity"),
|
|
32
|
+
})
|
|
33
|
+
|
|
21
34
|
const ReplaySchema = z.object({
|
|
22
35
|
type: z.literal("replay"),
|
|
23
36
|
runId: z.string().min(1),
|
|
@@ -30,7 +43,12 @@ const UnsubscribeSchema = z.object({
|
|
|
30
43
|
runId: z.string().min(1).optional(),
|
|
31
44
|
})
|
|
32
45
|
|
|
33
|
-
const AgentStreamMessageSchema = z.union([
|
|
46
|
+
const AgentStreamMessageSchema = z.union([
|
|
47
|
+
SubscribeSchema,
|
|
48
|
+
SubscribeActivitySchema,
|
|
49
|
+
ReplaySchema,
|
|
50
|
+
UnsubscribeSchema,
|
|
51
|
+
])
|
|
34
52
|
|
|
35
53
|
type SubscribeMessage = z.infer<typeof SubscribeSchema>
|
|
36
54
|
type ReplayMessage = z.infer<typeof ReplaySchema>
|
|
@@ -62,42 +80,29 @@ export function parseAgentStreamMessage(payload: unknown):
|
|
|
62
80
|
|
|
63
81
|
export async function canAccessAgentRunStream(
|
|
64
82
|
sixb: Sixb<readonly OntologySource[]>,
|
|
65
|
-
|
|
66
|
-
readonly runId: string
|
|
67
|
-
readonly authz: AuthorizationContext | null
|
|
68
|
-
}
|
|
83
|
+
runId: string
|
|
69
84
|
): Promise<{ ok: true } | { ok: false; message: string }> {
|
|
70
|
-
const
|
|
71
|
-
if (!storage) {
|
|
72
|
-
return { ok: false, message: "Agent storage is not configured." }
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const run = await storage.runs.getById({ projectId: sixb.id, id: input.runId })
|
|
85
|
+
const run = await sixb.agents.runs.getById(runId)
|
|
76
86
|
if (!run) {
|
|
77
87
|
return { ok: false, message: "Agent run not found." }
|
|
78
88
|
}
|
|
79
|
-
|
|
80
|
-
// Auth-disabled development runtimes are privileged, matching the existing HTTP agent routes.
|
|
81
|
-
if (!input.authz) {
|
|
82
|
-
return { ok: true }
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// Reuse the one owner + `run:agent` rule instead of re-implementing it here: the scoped surface
|
|
86
|
-
// returns null for a thread the caller may not read (absent, not owner, or ungranted).
|
|
87
|
-
const thread = await sixb.as(input.authz).getThread(run.threadId)
|
|
88
|
-
if (!thread) {
|
|
89
|
-
return { ok: false, message: "Agent run not found." }
|
|
90
|
-
}
|
|
91
|
-
|
|
92
89
|
return { ok: true }
|
|
93
90
|
}
|
|
94
91
|
|
|
92
|
+
export async function canAccessAgentThreadActivity(
|
|
93
|
+
sixb: Sixb<readonly OntologySource[]>,
|
|
94
|
+
event: AgentRunActivityEvent
|
|
95
|
+
): Promise<boolean> {
|
|
96
|
+
const thread = await sixb.agents.threads.getById(event.threadId)
|
|
97
|
+
return thread?.agentId === event.agentId
|
|
98
|
+
}
|
|
99
|
+
|
|
95
100
|
export function registerAgentStreamRoutes(app: Elysia, server: SixbServer) {
|
|
96
101
|
const states = new WeakMap<object, AgentStreamSubscriptionState>()
|
|
97
102
|
|
|
98
103
|
return app.ws("/ws/agents", {
|
|
99
104
|
open(ws) {
|
|
100
|
-
states.set(wsStateKey(ws), { runId: null, unsubscribe: null })
|
|
105
|
+
states.set(wsStateKey(ws), { channel: null, runId: null, unsubscribe: null })
|
|
101
106
|
safeSend(ws, { type: "connected", channel: "agents" })
|
|
102
107
|
},
|
|
103
108
|
|
|
@@ -119,6 +124,11 @@ export function registerAgentStreamRoutes(app: Elysia, server: SixbServer) {
|
|
|
119
124
|
return
|
|
120
125
|
}
|
|
121
126
|
|
|
127
|
+
if (parsed.data.type === "subscribe.activity") {
|
|
128
|
+
await subscribeAgentActivity(states, server, ws)
|
|
129
|
+
return
|
|
130
|
+
}
|
|
131
|
+
|
|
122
132
|
await subscribeAgentStream(states, server, ws, parsed.data)
|
|
123
133
|
},
|
|
124
134
|
|
|
@@ -135,11 +145,13 @@ async function subscribeAgentStream(
|
|
|
135
145
|
ws: { send: (message: string) => void },
|
|
136
146
|
message: SubscribeMessage
|
|
137
147
|
): Promise<void> {
|
|
138
|
-
const
|
|
139
|
-
const
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
148
|
+
const host = server.getHost()
|
|
149
|
+
const sixb = wsRequestSixb(ws)
|
|
150
|
+
if (!sixb) {
|
|
151
|
+
safeSend(ws, { type: "error", message: "Execution scope is not available." })
|
|
152
|
+
return
|
|
153
|
+
}
|
|
154
|
+
const access = await canAccessAgentRunStream(sixb, message.runId)
|
|
143
155
|
if (!access.ok) {
|
|
144
156
|
safeSend(ws, { type: "error", message: access.message })
|
|
145
157
|
return
|
|
@@ -147,8 +159,8 @@ async function subscribeAgentStream(
|
|
|
147
159
|
|
|
148
160
|
const streamId = agentRunStreamId(message.runId)
|
|
149
161
|
try {
|
|
150
|
-
await
|
|
151
|
-
projectId:
|
|
162
|
+
await host.broker.ensureStream({
|
|
163
|
+
projectId: host.id,
|
|
152
164
|
stream: agentRunStreamDefinition(message.runId),
|
|
153
165
|
})
|
|
154
166
|
} catch (error) {
|
|
@@ -156,41 +168,73 @@ async function subscribeAgentStream(
|
|
|
156
168
|
return
|
|
157
169
|
}
|
|
158
170
|
|
|
159
|
-
const state = states.get(wsStateKey(ws)) ?? {
|
|
171
|
+
const state = states.get(wsStateKey(ws)) ?? {
|
|
172
|
+
channel: null,
|
|
173
|
+
runId: null,
|
|
174
|
+
unsubscribe: null,
|
|
175
|
+
}
|
|
160
176
|
stopSubscription(state)
|
|
161
177
|
states.set(wsStateKey(ws), state)
|
|
162
178
|
|
|
163
179
|
const buffered: BrokerRecord[] = []
|
|
164
180
|
let live = false
|
|
181
|
+
const receive = (records: readonly BrokerRecord[]): void => {
|
|
182
|
+
if (!live) {
|
|
183
|
+
buffered.push(...records)
|
|
184
|
+
return
|
|
185
|
+
}
|
|
186
|
+
sendRecords(ws, records)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Set when the requested cursor had already been trimmed, so the client learns its resume point
|
|
190
|
+
// is gone instead of silently receiving a stream that starts after the gap.
|
|
191
|
+
let resumedFromEarliest = false
|
|
165
192
|
try {
|
|
166
|
-
state.unsubscribe = await
|
|
193
|
+
state.unsubscribe = await host.broker.subscribe(
|
|
167
194
|
{
|
|
168
|
-
projectId:
|
|
195
|
+
projectId: host.id,
|
|
169
196
|
streamId,
|
|
170
197
|
...(message.afterCursor === undefined
|
|
171
198
|
? { from: "earliest" as const }
|
|
172
199
|
: { afterCursor: message.afterCursor }),
|
|
173
200
|
},
|
|
174
|
-
|
|
175
|
-
if (!live) {
|
|
176
|
-
buffered.push(...records)
|
|
177
|
-
return
|
|
178
|
-
}
|
|
179
|
-
sendRecords(ws, records)
|
|
180
|
-
}
|
|
201
|
+
receive
|
|
181
202
|
)
|
|
203
|
+
state.channel = "run"
|
|
182
204
|
state.runId = message.runId
|
|
183
205
|
} catch (error) {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
206
|
+
// Retention can outrun a paused tab: an agent run stream keeps 5000 records and a streamed
|
|
207
|
+
// turn writes one per chunk. Replaying everything still retained beats leaving the socket
|
|
208
|
+
// open with a cursor the broker will reject forever.
|
|
209
|
+
if (error instanceof BrokerCursorExpiredError) {
|
|
210
|
+
try {
|
|
211
|
+
state.unsubscribe = await host.broker.subscribe(
|
|
212
|
+
{ projectId: host.id, streamId, from: "earliest" },
|
|
213
|
+
receive
|
|
214
|
+
)
|
|
215
|
+
state.channel = "run"
|
|
216
|
+
state.runId = message.runId
|
|
217
|
+
resumedFromEarliest = true
|
|
218
|
+
} catch (retryError) {
|
|
219
|
+
state.unsubscribe = null
|
|
220
|
+
state.channel = null
|
|
221
|
+
state.runId = null
|
|
222
|
+
safeSend(ws, { type: "error", message: errorMessage(retryError) })
|
|
223
|
+
return
|
|
224
|
+
}
|
|
225
|
+
} else {
|
|
226
|
+
state.unsubscribe = null
|
|
227
|
+
state.channel = null
|
|
228
|
+
state.runId = null
|
|
229
|
+
safeSend(ws, { type: "error", message: errorMessage(error) })
|
|
230
|
+
return
|
|
231
|
+
}
|
|
188
232
|
}
|
|
189
233
|
|
|
190
234
|
safeSend(ws, {
|
|
191
235
|
type: "subscribed",
|
|
192
236
|
runId: message.runId,
|
|
193
|
-
afterCursor: message.afterCursor ?? null,
|
|
237
|
+
afterCursor: resumedFromEarliest ? null : (message.afterCursor ?? null),
|
|
194
238
|
})
|
|
195
239
|
sendRecords(ws, buffered.splice(0))
|
|
196
240
|
if (!(await sendRunSnapshot(sixb, ws, message.runId))) {
|
|
@@ -201,16 +245,95 @@ async function subscribeAgentStream(
|
|
|
201
245
|
sendRecords(ws, buffered.splice(0))
|
|
202
246
|
}
|
|
203
247
|
|
|
248
|
+
async function subscribeAgentActivity(
|
|
249
|
+
states: WeakMap<object, AgentStreamSubscriptionState>,
|
|
250
|
+
server: SixbServer,
|
|
251
|
+
ws: { send: (message: string) => void }
|
|
252
|
+
): Promise<void> {
|
|
253
|
+
const host = server.getHost()
|
|
254
|
+
const sixb = wsRequestSixb(ws)
|
|
255
|
+
if (!sixb) {
|
|
256
|
+
safeSend(ws, { type: "error", message: "Execution scope is not available." })
|
|
257
|
+
return
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
try {
|
|
261
|
+
await host.broker.ensureStream({
|
|
262
|
+
projectId: host.id,
|
|
263
|
+
stream: agentActivityStreamDefinition(),
|
|
264
|
+
})
|
|
265
|
+
} catch (error) {
|
|
266
|
+
safeSend(ws, { type: "error", message: errorMessage(error) })
|
|
267
|
+
return
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const state = states.get(wsStateKey(ws)) ?? {
|
|
271
|
+
channel: null,
|
|
272
|
+
runId: null,
|
|
273
|
+
unsubscribe: null,
|
|
274
|
+
}
|
|
275
|
+
stopSubscription(state)
|
|
276
|
+
states.set(wsStateKey(ws), state)
|
|
277
|
+
|
|
278
|
+
try {
|
|
279
|
+
state.unsubscribe = await host.broker.subscribe(
|
|
280
|
+
{
|
|
281
|
+
projectId: host.id,
|
|
282
|
+
streamId: AGENT_ACTIVITY_STREAM_ID,
|
|
283
|
+
from: "latest",
|
|
284
|
+
},
|
|
285
|
+
(records) => {
|
|
286
|
+
void sendVisibleActivityRecords(sixb, ws, records).catch((error) => {
|
|
287
|
+
console.error("[SixbServer] Agent activity visibility check failed:", error)
|
|
288
|
+
safeSend(ws, {
|
|
289
|
+
type: "error",
|
|
290
|
+
message: "Could not verify agent activity visibility.",
|
|
291
|
+
})
|
|
292
|
+
})
|
|
293
|
+
}
|
|
294
|
+
)
|
|
295
|
+
state.channel = "activity"
|
|
296
|
+
} catch (error) {
|
|
297
|
+
state.unsubscribe = null
|
|
298
|
+
state.channel = null
|
|
299
|
+
safeSend(ws, { type: "error", message: errorMessage(error) })
|
|
300
|
+
return
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// The client reconciles its durable thread query on every subscription, closing the race between
|
|
304
|
+
// the initial HTTP snapshot and this live subscription and recovering changes missed offline.
|
|
305
|
+
safeSend(ws, { type: "subscribed.activity" })
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
async function sendVisibleActivityRecords(
|
|
309
|
+
sixb: Sixb<readonly OntologySource[]>,
|
|
310
|
+
ws: { send: (message: string) => void },
|
|
311
|
+
records: readonly BrokerRecord[]
|
|
312
|
+
): Promise<void> {
|
|
313
|
+
const events: AgentRunActivityEvent[] = []
|
|
314
|
+
for (const record of records) {
|
|
315
|
+
if (isAgentRunActivityEvent(record.payload)) events.push(record.payload)
|
|
316
|
+
}
|
|
317
|
+
const visible = await Promise.all(
|
|
318
|
+
events.map((event) => canAccessAgentThreadActivity(sixb, event))
|
|
319
|
+
)
|
|
320
|
+
for (const [index, event] of events.entries()) {
|
|
321
|
+
if (visible[index]) safeSend(ws, { type: "activity", event })
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
204
325
|
async function replayAgentStream(
|
|
205
326
|
server: SixbServer,
|
|
206
327
|
ws: { send: (message: string) => void },
|
|
207
328
|
message: ReplayMessage
|
|
208
329
|
): Promise<void> {
|
|
209
|
-
const
|
|
210
|
-
const
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
330
|
+
const host = server.getHost()
|
|
331
|
+
const sixb = wsRequestSixb(ws)
|
|
332
|
+
if (!sixb) {
|
|
333
|
+
safeSend(ws, { type: "error", message: "Execution scope is not available." })
|
|
334
|
+
return
|
|
335
|
+
}
|
|
336
|
+
const access = await canAccessAgentRunStream(sixb, message.runId)
|
|
214
337
|
if (!access.ok) {
|
|
215
338
|
safeSend(ws, { type: "error", message: access.message })
|
|
216
339
|
return
|
|
@@ -218,12 +341,12 @@ async function replayAgentStream(
|
|
|
218
341
|
|
|
219
342
|
const streamId = agentRunStreamId(message.runId)
|
|
220
343
|
try {
|
|
221
|
-
await
|
|
222
|
-
projectId:
|
|
344
|
+
await host.broker.ensureStream({
|
|
345
|
+
projectId: host.id,
|
|
223
346
|
stream: agentRunStreamDefinition(message.runId),
|
|
224
347
|
})
|
|
225
|
-
const page = await
|
|
226
|
-
projectId:
|
|
348
|
+
const page = await host.broker.read({
|
|
349
|
+
projectId: host.id,
|
|
227
350
|
streamId,
|
|
228
351
|
afterCursor: message.afterCursor,
|
|
229
352
|
limit: message.limit,
|
|
@@ -248,15 +371,18 @@ function unsubscribeAgentStream(
|
|
|
248
371
|
): void {
|
|
249
372
|
const state = states.get(wsStateKey(ws))
|
|
250
373
|
const previousRunId = state?.runId ?? null
|
|
374
|
+
const previousChannel = state?.channel ?? null
|
|
251
375
|
|
|
252
376
|
if (!message.runId || previousRunId === null || message.runId === previousRunId) {
|
|
253
377
|
stopSubscription(state)
|
|
254
378
|
}
|
|
255
379
|
|
|
256
|
-
safeSend(
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
380
|
+
safeSend(
|
|
381
|
+
ws,
|
|
382
|
+
previousChannel === "activity"
|
|
383
|
+
? { type: "unsubscribed.activity" }
|
|
384
|
+
: { type: "unsubscribed", runId: message.runId ?? previousRunId }
|
|
385
|
+
)
|
|
260
386
|
}
|
|
261
387
|
|
|
262
388
|
function stopSubscription(state: AgentStreamSubscriptionState | undefined): void {
|
|
@@ -265,6 +391,7 @@ function stopSubscription(state: AgentStreamSubscriptionState | undefined): void
|
|
|
265
391
|
}
|
|
266
392
|
state.unsubscribe?.()
|
|
267
393
|
state.unsubscribe = null
|
|
394
|
+
state.channel = null
|
|
268
395
|
state.runId = null
|
|
269
396
|
}
|
|
270
397
|
|
|
@@ -274,12 +401,15 @@ async function sendRunSnapshot(
|
|
|
274
401
|
runId: string
|
|
275
402
|
): Promise<boolean> {
|
|
276
403
|
try {
|
|
277
|
-
const run = await sixb.
|
|
404
|
+
const run = await sixb.agents.runs.getById(runId)
|
|
278
405
|
if (!run) {
|
|
279
406
|
safeSend(ws, { type: "error", message: "Agent run not found." })
|
|
280
407
|
return false
|
|
281
408
|
}
|
|
282
|
-
safeSend(ws, {
|
|
409
|
+
safeSend(ws, {
|
|
410
|
+
type: "run.snapshot",
|
|
411
|
+
run: serializeAgentRun(run),
|
|
412
|
+
})
|
|
283
413
|
return true
|
|
284
414
|
} catch (error) {
|
|
285
415
|
safeSend(ws, { type: "error", message: errorMessage(error) })
|
package/src/routes/ws/events.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import type { DomainEvent } from "@sixb/core"
|
|
2
2
|
import { scopeKeysForEvent } from "@sixb/core/events/scope"
|
|
3
|
-
import { canViewEvent } from "@sixb/core/internal/authorization"
|
|
4
3
|
import type { Elysia } from "elysia"
|
|
5
4
|
import { z } from "zod"
|
|
6
5
|
import { EVENT_TOPICS, EVENT_TYPES } from "../../schemas/events"
|
|
7
6
|
import type { SixbServer } from "../../server"
|
|
8
|
-
import { decodeWsMessage, safeSend,
|
|
7
|
+
import { decodeWsMessage, safeSend, wsRequestSixb, wsStateKey } from "../../utils/ws"
|
|
9
8
|
|
|
10
9
|
interface EventSubscriptionState {
|
|
11
10
|
topics?: DomainEvent["topic"][]
|
|
@@ -65,7 +64,7 @@ export function parseSubscriptionMessage(payload: unknown):
|
|
|
65
64
|
}
|
|
66
65
|
|
|
67
66
|
async function resolveLatestCursor(server: SixbServer): Promise<string | undefined> {
|
|
68
|
-
return server.
|
|
67
|
+
return server.getHost().events.latestCursor()
|
|
69
68
|
}
|
|
70
69
|
|
|
71
70
|
function createDefaultState(): EventSubscriptionState {
|
|
@@ -126,7 +125,11 @@ export function registerEventStreamRoutes(app: Elysia, server: SixbServer) {
|
|
|
126
125
|
return
|
|
127
126
|
}
|
|
128
127
|
|
|
129
|
-
const
|
|
128
|
+
const sixb = wsRequestSixb(ws)
|
|
129
|
+
if (!sixb) {
|
|
130
|
+
safeSend(ws, { type: "error", message: "Execution scope is not available." })
|
|
131
|
+
return
|
|
132
|
+
}
|
|
130
133
|
|
|
131
134
|
const tick = async () => {
|
|
132
135
|
if (state.polling) {
|
|
@@ -135,7 +138,7 @@ export function registerEventStreamRoutes(app: Elysia, server: SixbServer) {
|
|
|
135
138
|
|
|
136
139
|
state.polling = true
|
|
137
140
|
try {
|
|
138
|
-
const p = server.
|
|
141
|
+
const p = server.getHost()
|
|
139
142
|
const events = await p.events.read({
|
|
140
143
|
afterCursor: state.afterCursor,
|
|
141
144
|
limit: state.limit,
|
|
@@ -143,15 +146,13 @@ export function registerEventStreamRoutes(app: Elysia, server: SixbServer) {
|
|
|
143
146
|
types: state.types,
|
|
144
147
|
})
|
|
145
148
|
|
|
146
|
-
//
|
|
147
|
-
//
|
|
148
|
-
// optional scope narrows the stream further. The cursor advances over
|
|
149
|
-
// every event read, not just the ones sent.
|
|
149
|
+
// The execution facade owns per-event visibility. The cursor still advances over every
|
|
150
|
+
// record read, not only the visible ones, so an invisible tail cannot stall polling.
|
|
150
151
|
for (const event of events) {
|
|
151
152
|
if (!eventMatchesScope(event, state)) {
|
|
152
153
|
continue
|
|
153
154
|
}
|
|
154
|
-
if (
|
|
155
|
+
if (sixb.events.canRead(event)) {
|
|
155
156
|
safeSend(ws, { type: "event", event })
|
|
156
157
|
}
|
|
157
158
|
}
|
|
@@ -1,12 +1,6 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
LogLevel,
|
|
3
|
-
LogRunRef,
|
|
4
|
-
OntologySource,
|
|
5
|
-
Sixb,
|
|
6
|
-
SixbRunKind,
|
|
7
|
-
StoredLogLine,
|
|
8
|
-
} from "@sixb/core"
|
|
1
|
+
import type { LogLevel, LogRunRef, SixbRunKind, StoredLogLine } from "@sixb/core"
|
|
9
2
|
import { BrokerCursorExpiredError } from "@sixb/core/broker"
|
|
3
|
+
import type { LoggingService } from "@sixb/core/internal/logging"
|
|
10
4
|
|
|
11
5
|
const MAX_CLIENT_RECORDS = 1_000
|
|
12
6
|
const MAX_CLIENT_BYTES = 1_048_576
|
|
@@ -56,7 +50,7 @@ export class LogSubscriptionHub {
|
|
|
56
50
|
private unsubscribeBroker: (() => void) | null = null
|
|
57
51
|
private closed = false
|
|
58
52
|
|
|
59
|
-
constructor(private readonly
|
|
53
|
+
constructor(private readonly host: { readonly logging: LoggingService }) {}
|
|
60
54
|
|
|
61
55
|
async subscribe(
|
|
62
56
|
key: object,
|
|
@@ -133,7 +127,7 @@ export class LogSubscriptionHub {
|
|
|
133
127
|
private async ensureStarted(): Promise<void> {
|
|
134
128
|
if (this.closed) throw new Error("Log subscription hub is closed")
|
|
135
129
|
if (!this.startPromise) {
|
|
136
|
-
this.startPromise = this.
|
|
130
|
+
this.startPromise = this.host.logging
|
|
137
131
|
.subscribe({ from: "latest" }, (lines) => this.deliverLive(lines))
|
|
138
132
|
.then((unsubscribe) => {
|
|
139
133
|
if (this.closed) unsubscribe()
|
|
@@ -162,7 +156,7 @@ export class LogSubscriptionHub {
|
|
|
162
156
|
try {
|
|
163
157
|
let hasMore = true
|
|
164
158
|
while (hasMore) {
|
|
165
|
-
const page = await this.
|
|
159
|
+
const page = await this.host.logging.read({
|
|
166
160
|
afterCursor,
|
|
167
161
|
limit: READ_PAGE_SIZE,
|
|
168
162
|
kinds: state.filter.kinds,
|
|
@@ -205,7 +199,7 @@ export class LogSubscriptionHub {
|
|
|
205
199
|
|
|
206
200
|
if (error instanceof BrokerCursorExpiredError) {
|
|
207
201
|
try {
|
|
208
|
-
const latest = await this.
|
|
202
|
+
const latest = await this.host.logging.tail({ limit: 1 })
|
|
209
203
|
this.send(state, {
|
|
210
204
|
type: "reset",
|
|
211
205
|
reason: "cursor_expired",
|
package/src/routes/ws/logs.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { LogLevel } from "@sixb/core"
|
|
2
2
|
import type { Elysia } from "elysia"
|
|
3
3
|
import { z } from "zod"
|
|
4
4
|
import { LOG_LEVELS, LogRunIdSchema, SIXB_RUN_KINDS } from "../../schemas/logs"
|
|
5
5
|
import type { SixbServer } from "../../server"
|
|
6
|
-
import { decodeWsMessage, safeSend,
|
|
6
|
+
import { decodeWsMessage, safeSend, wsRequestSixb, wsStateKey } from "../../utils/ws"
|
|
7
7
|
import { LogSubscriptionHub } from "./log-subscription-hub"
|
|
8
8
|
|
|
9
9
|
const SubscribeSchema = z
|
|
@@ -49,12 +49,16 @@ export function parseLogSubscriptionMessage(
|
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
export function registerLogStreamRoutes(app: Elysia, server: SixbServer) {
|
|
52
|
-
const hub = new LogSubscriptionHub(server.
|
|
52
|
+
const hub = new LogSubscriptionHub(server.getHost())
|
|
53
53
|
|
|
54
54
|
app.onStop(() => hub.close())
|
|
55
55
|
return app.ws("/ws/logs", {
|
|
56
56
|
open(ws) {
|
|
57
|
-
|
|
57
|
+
try {
|
|
58
|
+
const sixb = wsRequestSixb(ws)
|
|
59
|
+
if (!sixb) throw new Error("Execution scope is not available.")
|
|
60
|
+
sixb.logs.assertObservable()
|
|
61
|
+
} catch {
|
|
58
62
|
ws.close(1008, "Missing required capability 'observe:logs'.")
|
|
59
63
|
return
|
|
60
64
|
}
|
|
@@ -62,7 +66,11 @@ export function registerLogStreamRoutes(app: Elysia, server: SixbServer) {
|
|
|
62
66
|
},
|
|
63
67
|
|
|
64
68
|
async message(ws, message) {
|
|
65
|
-
|
|
69
|
+
try {
|
|
70
|
+
const sixb = wsRequestSixb(ws)
|
|
71
|
+
if (!sixb) throw new Error("Execution scope is not available.")
|
|
72
|
+
sixb.logs.assertObservable()
|
|
73
|
+
} catch {
|
|
66
74
|
ws.close(1008, "Missing required capability 'observe:logs'.")
|
|
67
75
|
return
|
|
68
76
|
}
|
package/src/schemas/actions.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ACTION_RUN_FAILURE_CODES,
|
|
3
|
+
ACTION_RUN_PHASES,
|
|
4
|
+
type ActionRunFailure,
|
|
5
|
+
} from "@sixb/core/storage"
|
|
1
6
|
import { z } from "zod"
|
|
7
|
+
import { JsonValueSchema, sixbFailureSchema } from "./common"
|
|
2
8
|
import { ActionParamSchema } from "./ontology"
|
|
3
9
|
|
|
4
10
|
export const ActionIdParamsSchema = z.object({
|
|
@@ -55,16 +61,7 @@ export const ActionRunStatusSchema = z.enum([
|
|
|
55
61
|
"cancelled",
|
|
56
62
|
])
|
|
57
63
|
|
|
58
|
-
export const ActionRunPhaseSchema = z.enum(
|
|
59
|
-
"request",
|
|
60
|
-
"enqueue",
|
|
61
|
-
"validation",
|
|
62
|
-
"writeback",
|
|
63
|
-
"edits",
|
|
64
|
-
"commit",
|
|
65
|
-
"effects",
|
|
66
|
-
"cancelled",
|
|
67
|
-
])
|
|
64
|
+
export const ActionRunPhaseSchema = z.enum(ACTION_RUN_PHASES)
|
|
68
65
|
|
|
69
66
|
export const ActionRunsQuerySchema = z.object({
|
|
70
67
|
actionId: z.string().optional(),
|
|
@@ -78,10 +75,34 @@ export const ActionRunsQuerySchema = z.object({
|
|
|
78
75
|
order: z.enum(["asc", "desc"]).optional(),
|
|
79
76
|
})
|
|
80
77
|
|
|
81
|
-
export const ActionRunFailureSchema =
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
78
|
+
export const ActionRunFailureSchema: z.ZodType<ActionRunFailure> = sixbFailureSchema(
|
|
79
|
+
ACTION_RUN_FAILURE_CODES
|
|
80
|
+
).extend({
|
|
81
|
+
details: z.object({
|
|
82
|
+
actionId: z.string(),
|
|
83
|
+
runId: z.string(),
|
|
84
|
+
phase: ActionRunPhaseSchema,
|
|
85
|
+
}),
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
const ActionRunWritebackFailureSchema: z.ZodType<ActionRunFailure<"writeback">> = sixbFailureSchema(
|
|
89
|
+
ACTION_RUN_FAILURE_CODES
|
|
90
|
+
).extend({
|
|
91
|
+
details: z.object({
|
|
92
|
+
actionId: z.string(),
|
|
93
|
+
runId: z.string(),
|
|
94
|
+
phase: z.literal("writeback"),
|
|
95
|
+
}),
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
const ActionRunEffectsFailureSchema: z.ZodType<ActionRunFailure<"effects">> = sixbFailureSchema(
|
|
99
|
+
ACTION_RUN_FAILURE_CODES
|
|
100
|
+
).extend({
|
|
101
|
+
details: z.object({
|
|
102
|
+
actionId: z.string(),
|
|
103
|
+
runId: z.string(),
|
|
104
|
+
phase: z.literal("effects"),
|
|
105
|
+
}),
|
|
85
106
|
})
|
|
86
107
|
|
|
87
108
|
export const ActionRunSummarySchema = z.object({
|
|
@@ -97,18 +118,30 @@ export const ActionRunSummarySchema = z.object({
|
|
|
97
118
|
error: ActionRunFailureSchema.optional(),
|
|
98
119
|
})
|
|
99
120
|
|
|
100
|
-
const ActionRunWritebackRecordSchema = z.
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
})
|
|
121
|
+
const ActionRunWritebackRecordSchema = z.discriminatedUnion("status", [
|
|
122
|
+
z.object({
|
|
123
|
+
status: z.literal("succeeded"),
|
|
124
|
+
completedAt: z.string(),
|
|
125
|
+
result: JsonValueSchema,
|
|
126
|
+
}),
|
|
127
|
+
z.object({
|
|
128
|
+
status: z.literal("failed"),
|
|
129
|
+
completedAt: z.string(),
|
|
130
|
+
error: ActionRunWritebackFailureSchema,
|
|
131
|
+
}),
|
|
132
|
+
])
|
|
106
133
|
|
|
107
|
-
const ActionRunEffectsRecordSchema = z.
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
})
|
|
134
|
+
const ActionRunEffectsRecordSchema = z.discriminatedUnion("status", [
|
|
135
|
+
z.object({
|
|
136
|
+
status: z.literal("succeeded"),
|
|
137
|
+
completedAt: z.string(),
|
|
138
|
+
}),
|
|
139
|
+
z.object({
|
|
140
|
+
status: z.literal("failed"),
|
|
141
|
+
completedAt: z.string(),
|
|
142
|
+
error: ActionRunEffectsFailureSchema,
|
|
143
|
+
}),
|
|
144
|
+
])
|
|
112
145
|
|
|
113
146
|
export const ActionRunDetailSchema = ActionRunSummarySchema.extend({
|
|
114
147
|
params: z.record(z.unknown()),
|