@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,238 @@
|
|
|
1
|
+
import type { OntologySource, Sixb } from "@sixb/core"
|
|
2
|
+
import { canViewActionRun } from "@sixb/core/internal/authorization"
|
|
3
|
+
import type { ActionRunRecord } from "@sixb/core/storage"
|
|
4
|
+
import type { Elysia } from "elysia"
|
|
5
|
+
import { z } from "zod"
|
|
6
|
+
import { bearerSecurityRequirement } from "../auth/access-token-boundary"
|
|
7
|
+
import { requestAuthState } from "../auth/scope"
|
|
8
|
+
import {
|
|
9
|
+
createContextualFileContentResponse,
|
|
10
|
+
fileContentGetResponses,
|
|
11
|
+
fileContentHeadResponses,
|
|
12
|
+
} from "../files/content"
|
|
13
|
+
import { OPENAPI_TAGS } from "../openapi/tags"
|
|
14
|
+
import {
|
|
15
|
+
ActionRunDetailSchema,
|
|
16
|
+
ActionRunIdParamsSchema,
|
|
17
|
+
ActionRunListResponseSchema,
|
|
18
|
+
ActionRunSummarySchema,
|
|
19
|
+
ActionRunsQuerySchema,
|
|
20
|
+
} from "../schemas/actions"
|
|
21
|
+
import { ErrorResponseSchema } from "../schemas/common"
|
|
22
|
+
import { FileContentQuerySchema } from "../schemas/files"
|
|
23
|
+
import {
|
|
24
|
+
handleRouteError,
|
|
25
|
+
parseDate,
|
|
26
|
+
parseOptionalInt,
|
|
27
|
+
toIsoString,
|
|
28
|
+
unconfiguredStorageResponse,
|
|
29
|
+
} from "../utils/http"
|
|
30
|
+
|
|
31
|
+
const ActionRunFileContentQuerySchema = FileContentQuerySchema.extend({
|
|
32
|
+
path: z
|
|
33
|
+
.string()
|
|
34
|
+
.min(1)
|
|
35
|
+
.regex(/^\/params(?:\/|$)/, "Action run file content paths must start with /params/"),
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
function serializeActionRunSummary(
|
|
39
|
+
run: ActionRunRecord
|
|
40
|
+
): ReturnType<typeof ActionRunSummarySchema.parse> {
|
|
41
|
+
return ActionRunSummarySchema.parse({
|
|
42
|
+
id: run.id,
|
|
43
|
+
projectId: run.projectId,
|
|
44
|
+
actionId: run.actionId,
|
|
45
|
+
subject: run.subject,
|
|
46
|
+
status: run.status,
|
|
47
|
+
phase: run.phase,
|
|
48
|
+
queuedAt: toIsoString(run.queuedAt),
|
|
49
|
+
startedAt: run.startedAt ? toIsoString(run.startedAt) : undefined,
|
|
50
|
+
finishedAt: run.finishedAt ? toIsoString(run.finishedAt) : undefined,
|
|
51
|
+
error: run.error,
|
|
52
|
+
})
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function serializeActionRunDetail(
|
|
56
|
+
run: ActionRunRecord
|
|
57
|
+
): ReturnType<typeof ActionRunDetailSchema.parse> {
|
|
58
|
+
return ActionRunDetailSchema.parse({
|
|
59
|
+
...serializeActionRunSummary(run),
|
|
60
|
+
params: run.params,
|
|
61
|
+
writeback: run.writeback
|
|
62
|
+
? {
|
|
63
|
+
status: run.writeback.status,
|
|
64
|
+
completedAt: toIsoString(run.writeback.completedAt),
|
|
65
|
+
...(run.writeback.result !== undefined ? { result: run.writeback.result } : {}),
|
|
66
|
+
error: run.writeback.error,
|
|
67
|
+
}
|
|
68
|
+
: undefined,
|
|
69
|
+
effects: run.effects
|
|
70
|
+
? {
|
|
71
|
+
status: run.effects.status,
|
|
72
|
+
completedAt: toIsoString(run.effects.completedAt),
|
|
73
|
+
error: run.effects.error,
|
|
74
|
+
}
|
|
75
|
+
: undefined,
|
|
76
|
+
})
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async function actionRunFileContentResponse(
|
|
80
|
+
sixb: Sixb<readonly OntologySource[]>,
|
|
81
|
+
context: {
|
|
82
|
+
readonly params: { readonly runId: string }
|
|
83
|
+
readonly query: unknown
|
|
84
|
+
readonly request: Request
|
|
85
|
+
readonly set: { status?: number | string }
|
|
86
|
+
},
|
|
87
|
+
options: { readonly head?: boolean } = {}
|
|
88
|
+
) {
|
|
89
|
+
const { authz } = requestAuthState(context)
|
|
90
|
+
|
|
91
|
+
const storage = sixb.storage.actionRuns
|
|
92
|
+
if (!storage) {
|
|
93
|
+
return unconfiguredStorageResponse(context.set, "Action run storage")
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return createContextualFileContentResponse({
|
|
97
|
+
blobStorage: sixb.blobStorage,
|
|
98
|
+
query: context.query,
|
|
99
|
+
querySchema: ActionRunFileContentQuerySchema,
|
|
100
|
+
request: context.request,
|
|
101
|
+
set: context.set,
|
|
102
|
+
head: options.head,
|
|
103
|
+
resolveRoot: async () => {
|
|
104
|
+
const run = await storage.getById({ projectId: sixb.id, id: context.params.runId })
|
|
105
|
+
if (!run || !canViewActionRun(authz, run)) {
|
|
106
|
+
return null
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return serializeActionRunDetail(run)
|
|
110
|
+
},
|
|
111
|
+
})
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function registerActionRunRoutes(app: Elysia, sixb: Sixb<readonly OntologySource[]>) {
|
|
115
|
+
return app
|
|
116
|
+
.get(
|
|
117
|
+
"/api/action-runs",
|
|
118
|
+
async (context) => {
|
|
119
|
+
const { query, set } = context
|
|
120
|
+
const { authz } = requestAuthState(context)
|
|
121
|
+
try {
|
|
122
|
+
const storage = sixb.storage.actionRuns
|
|
123
|
+
if (!storage) {
|
|
124
|
+
return unconfiguredStorageResponse(set, "Action run storage")
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const parsed = ActionRunsQuerySchema.parse(query)
|
|
128
|
+
const limit = parseOptionalInt(parsed.limit)
|
|
129
|
+
const offset = parseOptionalInt(parsed.offset)
|
|
130
|
+
const actionIds = authz ? [...authz.grants["apply:action"]] : undefined
|
|
131
|
+
const objectTypeIds = authz ? [...authz.grants["view:object"]] : undefined
|
|
132
|
+
const result = await storage.list({
|
|
133
|
+
projectId: sixb.id,
|
|
134
|
+
actionId: parsed.actionId,
|
|
135
|
+
actionIds,
|
|
136
|
+
objectTypeId: parsed.objectTypeId,
|
|
137
|
+
objectTypeIds,
|
|
138
|
+
primaryId: parsed.primaryId,
|
|
139
|
+
statuses: parsed.status ? [parsed.status] : undefined,
|
|
140
|
+
startedAfter: parseDate(parsed.startedAfter),
|
|
141
|
+
startedBefore: parseDate(parsed.startedBefore),
|
|
142
|
+
limit,
|
|
143
|
+
offset,
|
|
144
|
+
order: parsed.order,
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
return ActionRunListResponseSchema.parse({
|
|
148
|
+
runs: result.runs.map(serializeActionRunSummary),
|
|
149
|
+
hasMore: result.hasMore,
|
|
150
|
+
total: result.total,
|
|
151
|
+
})
|
|
152
|
+
} catch (error) {
|
|
153
|
+
return handleRouteError(error, set)
|
|
154
|
+
}
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
query: ActionRunsQuerySchema,
|
|
158
|
+
response: {
|
|
159
|
+
200: ActionRunListResponseSchema,
|
|
160
|
+
400: ErrorResponseSchema,
|
|
161
|
+
501: ErrorResponseSchema,
|
|
162
|
+
},
|
|
163
|
+
detail: {
|
|
164
|
+
summary: "List action run history",
|
|
165
|
+
tags: [OPENAPI_TAGS.actionRuns.name],
|
|
166
|
+
operationId: "listActionRuns",
|
|
167
|
+
},
|
|
168
|
+
}
|
|
169
|
+
)
|
|
170
|
+
.get(
|
|
171
|
+
"/api/action-runs/:runId",
|
|
172
|
+
async (context) => {
|
|
173
|
+
const { params, set } = context
|
|
174
|
+
const { authz } = requestAuthState(context)
|
|
175
|
+
try {
|
|
176
|
+
const storage = sixb.storage.actionRuns
|
|
177
|
+
if (!storage) {
|
|
178
|
+
return unconfiguredStorageResponse(set, "Action run storage")
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const run = await storage.getById({ projectId: sixb.id, id: params.runId })
|
|
182
|
+
if (!run || !canViewActionRun(authz, run)) {
|
|
183
|
+
set.status = 404
|
|
184
|
+
return { error: "Action run not found" }
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return serializeActionRunDetail(run)
|
|
188
|
+
} catch (error) {
|
|
189
|
+
return handleRouteError(error, set)
|
|
190
|
+
}
|
|
191
|
+
},
|
|
192
|
+
{
|
|
193
|
+
params: ActionRunIdParamsSchema,
|
|
194
|
+
response: {
|
|
195
|
+
200: ActionRunDetailSchema,
|
|
196
|
+
400: ErrorResponseSchema,
|
|
197
|
+
404: ErrorResponseSchema,
|
|
198
|
+
501: ErrorResponseSchema,
|
|
199
|
+
},
|
|
200
|
+
detail: {
|
|
201
|
+
summary: "Get action run detail",
|
|
202
|
+
tags: [OPENAPI_TAGS.actionRuns.name],
|
|
203
|
+
operationId: "getActionRun",
|
|
204
|
+
security: bearerSecurityRequirement("getActionRun"),
|
|
205
|
+
},
|
|
206
|
+
}
|
|
207
|
+
)
|
|
208
|
+
.get(
|
|
209
|
+
"/api/action-runs/:runId/files/content",
|
|
210
|
+
(context) => actionRunFileContentResponse(sixb, context),
|
|
211
|
+
{
|
|
212
|
+
params: ActionRunIdParamsSchema,
|
|
213
|
+
query: FileContentQuerySchema,
|
|
214
|
+
detail: {
|
|
215
|
+
summary: "Get action run file content",
|
|
216
|
+
tags: ["Actions"],
|
|
217
|
+
operationId: "getActionRunFileContent",
|
|
218
|
+
security: bearerSecurityRequirement("getActionRunFileContent"),
|
|
219
|
+
responses: fileContentGetResponses({ optionalStorage: true }),
|
|
220
|
+
},
|
|
221
|
+
}
|
|
222
|
+
)
|
|
223
|
+
.head(
|
|
224
|
+
"/api/action-runs/:runId/files/content",
|
|
225
|
+
(context) => actionRunFileContentResponse(sixb, context, { head: true }),
|
|
226
|
+
{
|
|
227
|
+
params: ActionRunIdParamsSchema,
|
|
228
|
+
query: FileContentQuerySchema,
|
|
229
|
+
detail: {
|
|
230
|
+
summary: "Head action run file content",
|
|
231
|
+
tags: ["Actions"],
|
|
232
|
+
operationId: "headActionRunFileContent",
|
|
233
|
+
security: bearerSecurityRequirement("headActionRunFileContent"),
|
|
234
|
+
responses: fileContentHeadResponses({ optionalStorage: true }),
|
|
235
|
+
},
|
|
236
|
+
}
|
|
237
|
+
)
|
|
238
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import type { ActionDefinition, OntologySource, Sixb } from "@sixb/core"
|
|
2
|
+
import type { Elysia } from "elysia"
|
|
3
|
+
import { bearerSecurityRequirement } from "../auth/access-token-boundary"
|
|
4
|
+
import { requestAuthState } from "../auth/scope"
|
|
5
|
+
import { OPENAPI_TAGS } from "../openapi/tags"
|
|
6
|
+
import {
|
|
7
|
+
ActionCatalogItemSchema,
|
|
8
|
+
ActionIdParamsSchema,
|
|
9
|
+
RequestActionBodySchema,
|
|
10
|
+
RequestActionResponseSchema,
|
|
11
|
+
} from "../schemas/actions"
|
|
12
|
+
import { ErrorResponseSchema } from "../schemas/common"
|
|
13
|
+
import { handleRouteError } from "../utils/http"
|
|
14
|
+
|
|
15
|
+
function serializeAction(
|
|
16
|
+
action: ActionDefinition
|
|
17
|
+
): ReturnType<typeof ActionCatalogItemSchema.parse> {
|
|
18
|
+
return ActionCatalogItemSchema.parse({
|
|
19
|
+
id: action.id,
|
|
20
|
+
name: action.id,
|
|
21
|
+
description: action.description,
|
|
22
|
+
...(action.binding.kind === "object" ? { objectTypeId: action.binding.objectType.id } : {}),
|
|
23
|
+
params: Object.entries(action.params).map(([id, config]) => ({
|
|
24
|
+
id,
|
|
25
|
+
name: id,
|
|
26
|
+
schema: config.schema,
|
|
27
|
+
required: config.required ?? false,
|
|
28
|
+
nullable: config.nullable,
|
|
29
|
+
description: config.description,
|
|
30
|
+
semanticType: config.semanticType,
|
|
31
|
+
})),
|
|
32
|
+
phases: {
|
|
33
|
+
validate: action.phases.validate.length > 0,
|
|
34
|
+
writeback: action.phases.writeback !== undefined,
|
|
35
|
+
edits: action.phases.edits !== undefined,
|
|
36
|
+
effects: action.phases.effects !== undefined,
|
|
37
|
+
},
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function registerActionRoutes(app: Elysia, sixb: Sixb<readonly OntologySource[]>) {
|
|
42
|
+
return app
|
|
43
|
+
.get(
|
|
44
|
+
"/api/actions",
|
|
45
|
+
async (context) => {
|
|
46
|
+
const { scoped } = requestAuthState(context)
|
|
47
|
+
const actions = scoped ? scoped.listActions() : sixb.listActions()
|
|
48
|
+
return actions.map(serializeAction)
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
response: { 200: ActionCatalogItemSchema.array() },
|
|
52
|
+
detail: {
|
|
53
|
+
summary: "List registered actions",
|
|
54
|
+
tags: [OPENAPI_TAGS.actions.name],
|
|
55
|
+
operationId: "listActions",
|
|
56
|
+
security: bearerSecurityRequirement("listActions"),
|
|
57
|
+
},
|
|
58
|
+
}
|
|
59
|
+
)
|
|
60
|
+
.get(
|
|
61
|
+
"/api/actions/:actionId",
|
|
62
|
+
async (context) => {
|
|
63
|
+
const { params, set } = context
|
|
64
|
+
const { scoped } = requestAuthState(context)
|
|
65
|
+
const action = scoped
|
|
66
|
+
? scoped.getActionById(params.actionId)
|
|
67
|
+
: sixb.getActionById(params.actionId)
|
|
68
|
+
if (!action) {
|
|
69
|
+
set.status = 404
|
|
70
|
+
return { error: "Action not found" }
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return serializeAction(action)
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
params: ActionIdParamsSchema,
|
|
77
|
+
response: { 200: ActionCatalogItemSchema, 404: ErrorResponseSchema },
|
|
78
|
+
detail: {
|
|
79
|
+
summary: "Get action metadata",
|
|
80
|
+
tags: [OPENAPI_TAGS.actions.name],
|
|
81
|
+
operationId: "getAction",
|
|
82
|
+
security: bearerSecurityRequirement("getAction"),
|
|
83
|
+
},
|
|
84
|
+
}
|
|
85
|
+
)
|
|
86
|
+
.post(
|
|
87
|
+
"/api/actions/:actionId",
|
|
88
|
+
async (context) => {
|
|
89
|
+
const { params, body, set } = context
|
|
90
|
+
const { scoped } = requestAuthState(context)
|
|
91
|
+
try {
|
|
92
|
+
const parsedBody = RequestActionBodySchema.parse(body)
|
|
93
|
+
const input = {
|
|
94
|
+
actionId: params.actionId,
|
|
95
|
+
subject: parsedBody.subject,
|
|
96
|
+
params: parsedBody.params,
|
|
97
|
+
runId: parsedBody.runId,
|
|
98
|
+
}
|
|
99
|
+
const result = scoped
|
|
100
|
+
? await scoped.requestAction(input)
|
|
101
|
+
: await sixb.actions.request(input)
|
|
102
|
+
|
|
103
|
+
set.status = 202
|
|
104
|
+
return RequestActionResponseSchema.parse(result)
|
|
105
|
+
} catch (error) {
|
|
106
|
+
return handleRouteError(error, set)
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
params: ActionIdParamsSchema,
|
|
111
|
+
body: RequestActionBodySchema,
|
|
112
|
+
response: {
|
|
113
|
+
202: RequestActionResponseSchema,
|
|
114
|
+
400: ErrorResponseSchema,
|
|
115
|
+
403: ErrorResponseSchema,
|
|
116
|
+
404: ErrorResponseSchema,
|
|
117
|
+
},
|
|
118
|
+
detail: {
|
|
119
|
+
summary: "Request an action",
|
|
120
|
+
tags: [OPENAPI_TAGS.actions.name],
|
|
121
|
+
operationId: "requestAction",
|
|
122
|
+
security: bearerSecurityRequirement("requestAction"),
|
|
123
|
+
},
|
|
124
|
+
}
|
|
125
|
+
)
|
|
126
|
+
}
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import { type OntologySource, resolveAuthorizationContext, type Sixb } from "@sixb/core"
|
|
2
|
+
import {
|
|
3
|
+
AGENT_API_GATEWAY_PREFIX,
|
|
4
|
+
AGENT_API_ROUTES,
|
|
5
|
+
isAllowedAgentApiRequest,
|
|
6
|
+
isValidAgentApiGatewayCapability,
|
|
7
|
+
} from "@sixb/core/internal/agents"
|
|
8
|
+
import { normalizeRoutePath, pathSegmentsFor } from "@sixb/core/internal/http"
|
|
9
|
+
import type { Elysia } from "elysia"
|
|
10
|
+
import { registerInternalRequestAuthState } from "../auth/scope"
|
|
11
|
+
|
|
12
|
+
const MAX_AGENT_API_BODY_BYTES = 1_000_000
|
|
13
|
+
const HOP_BY_HOP_RESPONSE_HEADERS = new Set([
|
|
14
|
+
"connection",
|
|
15
|
+
"keep-alive",
|
|
16
|
+
"proxy-authenticate",
|
|
17
|
+
"proxy-authorization",
|
|
18
|
+
"te",
|
|
19
|
+
"trailer",
|
|
20
|
+
"transfer-encoding",
|
|
21
|
+
"upgrade",
|
|
22
|
+
])
|
|
23
|
+
|
|
24
|
+
export function registerAgentApiGatewayRoutes(app: Elysia, sixb: Sixb<readonly OntologySource[]>) {
|
|
25
|
+
for (const route of AGENT_API_ROUTES) {
|
|
26
|
+
const path = `${AGENT_API_GATEWAY_PREFIX}/:agentGatewayRunId/:agentGatewayCapability${route.path}`
|
|
27
|
+
const handler = async (context: AgentApiGatewayRouteContext) =>
|
|
28
|
+
handleAgentApiGatewayRequest({
|
|
29
|
+
sixb,
|
|
30
|
+
app,
|
|
31
|
+
request: context.request,
|
|
32
|
+
agentRunId: context.params.agentGatewayRunId ?? "",
|
|
33
|
+
capability: context.params.agentGatewayCapability ?? "",
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
if (route.method === "GET") {
|
|
37
|
+
app.get(path, handler, { detail: { hide: true } })
|
|
38
|
+
continue
|
|
39
|
+
}
|
|
40
|
+
if (route.method === "POST") {
|
|
41
|
+
app.post(path, handler, { detail: { hide: true } })
|
|
42
|
+
continue
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
throw new Error(`[SixbServer] Unsupported agent API gateway method '${route.method}'.`)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
interface AgentApiGatewayRouteContext {
|
|
50
|
+
readonly request: Request
|
|
51
|
+
readonly params: Readonly<Record<string, string | undefined>>
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async function handleAgentApiGatewayRequest(input: {
|
|
55
|
+
readonly sixb: Sixb<readonly OntologySource[]>
|
|
56
|
+
readonly app: Elysia
|
|
57
|
+
readonly request: Request
|
|
58
|
+
readonly agentRunId: string
|
|
59
|
+
readonly capability: string
|
|
60
|
+
}): Promise<Response> {
|
|
61
|
+
const sourceUrl = new URL(input.request.url)
|
|
62
|
+
const upstreamPath = gatewayUpstreamPath(sourceUrl.pathname)
|
|
63
|
+
if (!isAllowedAgentApiRequest(input.request.method, upstreamPath)) {
|
|
64
|
+
return jsonError(
|
|
65
|
+
403,
|
|
66
|
+
`Agent API gateway does not allow ${input.request.method} ${upstreamPath}.`
|
|
67
|
+
)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const authState = await resolveAgentRunAuthState(input.sixb, {
|
|
71
|
+
runId: input.agentRunId,
|
|
72
|
+
capability: input.capability,
|
|
73
|
+
})
|
|
74
|
+
if (authState instanceof Response) {
|
|
75
|
+
return authState
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
let body: ArrayBuffer | undefined
|
|
79
|
+
try {
|
|
80
|
+
body = await readRequestBody(input.request)
|
|
81
|
+
} catch (error) {
|
|
82
|
+
if (error instanceof PayloadTooLargeError) {
|
|
83
|
+
return jsonError(413, error.message)
|
|
84
|
+
}
|
|
85
|
+
throw error
|
|
86
|
+
}
|
|
87
|
+
const internalUrl = new URL(`${upstreamPath}${sourceUrl.search}`, input.request.url)
|
|
88
|
+
const internalRequest = new Request(internalUrl, {
|
|
89
|
+
method: input.request.method,
|
|
90
|
+
headers: forwardedRequestHeaders(input.request.headers),
|
|
91
|
+
body,
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
registerInternalRequestAuthState(internalRequest, authState)
|
|
95
|
+
|
|
96
|
+
const response = await input.app.fetch(internalRequest)
|
|
97
|
+
return new Response(response.body, {
|
|
98
|
+
status: response.status,
|
|
99
|
+
statusText: response.statusText,
|
|
100
|
+
headers: forwardedResponseHeaders(response.headers),
|
|
101
|
+
})
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async function resolveAgentRunAuthState(
|
|
105
|
+
sixb: Sixb<readonly OntologySource[]>,
|
|
106
|
+
input: { readonly runId: string; readonly capability: string }
|
|
107
|
+
) {
|
|
108
|
+
const agentStorage = sixb.storage.agents
|
|
109
|
+
const workflowRuns = sixb.storage.workflowRuns
|
|
110
|
+
if (!agentStorage && !workflowRuns) {
|
|
111
|
+
// 501, not 403: nothing about the caller is being refused — neither storage role
|
|
112
|
+
// that could hold an agent run is configured, so no request can ever succeed here.
|
|
113
|
+
return jsonError(501, "Agent API gateway is not configured for this runtime.")
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const conversationalRun = await agentStorage?.runs.getById({
|
|
117
|
+
projectId: sixb.id,
|
|
118
|
+
id: input.runId,
|
|
119
|
+
})
|
|
120
|
+
const workflowRun = conversationalRun
|
|
121
|
+
? null
|
|
122
|
+
: await workflowRuns?.agentNodes.getByNodeRunId({
|
|
123
|
+
projectId: sixb.id,
|
|
124
|
+
nodeRunId: input.runId,
|
|
125
|
+
})
|
|
126
|
+
const run = conversationalRun ?? workflowRun
|
|
127
|
+
if (
|
|
128
|
+
!run ||
|
|
129
|
+
run.status !== "running" ||
|
|
130
|
+
!run.execution ||
|
|
131
|
+
run.execution.queueLeaseExpiresAt.getTime() <= Date.now() ||
|
|
132
|
+
!isValidAgentApiGatewayCapability({
|
|
133
|
+
projectId: sixb.id,
|
|
134
|
+
runId: input.runId,
|
|
135
|
+
executionToken: run.execution.token,
|
|
136
|
+
capability: input.capability,
|
|
137
|
+
})
|
|
138
|
+
) {
|
|
139
|
+
return jsonError(403, "Agent API gateway capability is not valid for this run.")
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (!sixb.auth.isEnabled()) {
|
|
143
|
+
return {
|
|
144
|
+
authz: null,
|
|
145
|
+
scoped: null,
|
|
146
|
+
...(conversationalRun ? { agentRun: conversationalRun } : {}),
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Two different failures shared one status. Missing auth storage is a runtime the
|
|
151
|
+
// operator did not configure; a run without an execution principal is a run that
|
|
152
|
+
// cannot prove an identity. Only the first is a 501.
|
|
153
|
+
const auth = sixb.storage.auth
|
|
154
|
+
if (!auth) {
|
|
155
|
+
return jsonError(501, "Agent API gateway is not configured for authenticated agent access.")
|
|
156
|
+
}
|
|
157
|
+
if (!run.executionPrincipal) {
|
|
158
|
+
return jsonError(403, "Agent run has no execution identity.")
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const serviceAccount = await auth.serviceAccounts.getById({
|
|
162
|
+
projectId: sixb.id,
|
|
163
|
+
id: run.executionPrincipal.id,
|
|
164
|
+
})
|
|
165
|
+
if (!serviceAccount || serviceAccount.status !== "active") {
|
|
166
|
+
return jsonError(403, "Agent execution identity is not active.")
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const memberships = await auth.serviceAccountGroupMemberships.listForServiceAccount({
|
|
170
|
+
projectId: sixb.id,
|
|
171
|
+
serviceAccountId: serviceAccount.id,
|
|
172
|
+
})
|
|
173
|
+
const authz = resolveAuthorizationContext({
|
|
174
|
+
principal: run.executionPrincipal,
|
|
175
|
+
groupIds: memberships.map((membership) => membership.groupId),
|
|
176
|
+
roles: sixb.security.listResolvedRoles(),
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
return {
|
|
180
|
+
authz,
|
|
181
|
+
scoped: sixb.as(authz),
|
|
182
|
+
...(conversationalRun ? { agentRun: conversationalRun } : {}),
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function gatewayUpstreamPath(pathname: string): string {
|
|
187
|
+
const prefixLength = pathSegmentsFor(AGENT_API_GATEWAY_PREFIX).length
|
|
188
|
+
const segments = pathSegmentsFor(pathname)
|
|
189
|
+
const upstreamSegments = segments.slice(prefixLength + 2)
|
|
190
|
+
return normalizeRoutePath(`/${upstreamSegments.join("/")}`)
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function forwardedRequestHeaders(source: Headers): Headers {
|
|
194
|
+
const headers = new Headers()
|
|
195
|
+
copyHeader(source, headers, "accept")
|
|
196
|
+
copyHeader(source, headers, "content-type")
|
|
197
|
+
return headers
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function forwardedResponseHeaders(source: Headers): Headers {
|
|
201
|
+
const headers = new Headers()
|
|
202
|
+
for (const [key, value] of source) {
|
|
203
|
+
const normalized = key.toLowerCase()
|
|
204
|
+
if (
|
|
205
|
+
HOP_BY_HOP_RESPONSE_HEADERS.has(normalized) ||
|
|
206
|
+
normalized === "content-length" ||
|
|
207
|
+
normalized === "set-cookie"
|
|
208
|
+
) {
|
|
209
|
+
continue
|
|
210
|
+
}
|
|
211
|
+
headers.append(key, value)
|
|
212
|
+
}
|
|
213
|
+
return headers
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function copyHeader(source: Headers, target: Headers, name: string): void {
|
|
217
|
+
const value = source.get(name)
|
|
218
|
+
if (value !== null) {
|
|
219
|
+
target.set(name, value)
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
async function readRequestBody(request: Request): Promise<ArrayBuffer | undefined> {
|
|
224
|
+
if (!request.body || request.method === "GET" || request.method === "HEAD") {
|
|
225
|
+
return undefined
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const reader = request.body.getReader()
|
|
229
|
+
const chunks: Uint8Array[] = []
|
|
230
|
+
let total = 0
|
|
231
|
+
|
|
232
|
+
while (true) {
|
|
233
|
+
const { done, value } = await reader.read()
|
|
234
|
+
if (done) break
|
|
235
|
+
total += value.byteLength
|
|
236
|
+
if (total > MAX_AGENT_API_BODY_BYTES) {
|
|
237
|
+
throw new PayloadTooLargeError("Agent API gateway request body exceeds 1MB.")
|
|
238
|
+
}
|
|
239
|
+
chunks.push(value)
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return concatChunks(chunks, total)
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function concatChunks(chunks: readonly Uint8Array[], total: number): ArrayBuffer {
|
|
246
|
+
const buffer = new ArrayBuffer(total)
|
|
247
|
+
const result = new Uint8Array(buffer)
|
|
248
|
+
let offset = 0
|
|
249
|
+
for (const chunk of chunks) {
|
|
250
|
+
result.set(chunk, offset)
|
|
251
|
+
offset += chunk.byteLength
|
|
252
|
+
}
|
|
253
|
+
return buffer
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function jsonError(status: number, message: string): Response {
|
|
257
|
+
return Response.json({ error: message }, { status })
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
class PayloadTooLargeError extends Error {}
|