@sixb/server 0.1.0 → 0.1.2

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.
Files changed (171) hide show
  1. package/README.md +3 -3
  2. package/dist/auth/application-access.d.ts +2 -2
  3. package/dist/auth/application-access.d.ts.map +1 -1
  4. package/dist/auth/connector-callback-cookie.d.ts +12 -0
  5. package/dist/auth/connector-callback-cookie.d.ts.map +1 -0
  6. package/dist/auth/guard.d.ts +3 -3
  7. package/dist/auth/guard.d.ts.map +1 -1
  8. package/dist/auth/public-routes.d.ts.map +1 -1
  9. package/dist/auth/scope.d.ts +21 -27
  10. package/dist/auth/scope.d.ts.map +1 -1
  11. package/dist/auth/session-cookies.d.ts +2 -2
  12. package/dist/auth/session-cookies.d.ts.map +1 -1
  13. package/dist/connectors/http.d.ts +16 -0
  14. package/dist/connectors/http.d.ts.map +1 -0
  15. package/dist/files/content.d.ts +11 -0
  16. package/dist/files/content.d.ts.map +1 -1
  17. package/dist/index.js +4164 -3541
  18. package/dist/index.js.map +57 -52
  19. package/dist/openapi/tags.d.ts +11 -3
  20. package/dist/openapi/tags.d.ts.map +1 -1
  21. package/dist/registerRoutes.d.ts +6 -2
  22. package/dist/registerRoutes.d.ts.map +1 -1
  23. package/dist/routes/action-runs.d.ts +21 -31
  24. package/dist/routes/action-runs.d.ts.map +1 -1
  25. package/dist/routes/actions.d.ts +9 -8
  26. package/dist/routes/actions.d.ts.map +1 -1
  27. package/dist/routes/agent-api-gateway.d.ts +2 -2
  28. package/dist/routes/agent-api-gateway.d.ts.map +1 -1
  29. package/dist/routes/agents.d.ts +130 -131
  30. package/dist/routes/agents.d.ts.map +1 -1
  31. package/dist/routes/auth.d.ts +90 -90
  32. package/dist/routes/auth.d.ts.map +1 -1
  33. package/dist/routes/connector-connection-runs.d.ts +794 -0
  34. package/dist/routes/connector-connection-runs.d.ts.map +1 -0
  35. package/dist/routes/connector-connections.d.ts +219 -0
  36. package/dist/routes/connector-connections.d.ts.map +1 -0
  37. package/dist/routes/connectors.d.ts +10 -4
  38. package/dist/routes/connectors.d.ts.map +1 -1
  39. package/dist/routes/datasets.d.ts +15 -9
  40. package/dist/routes/datasets.d.ts.map +1 -1
  41. package/dist/routes/events.d.ts +8 -8
  42. package/dist/routes/events.d.ts.map +1 -1
  43. package/dist/routes/files.d.ts +11 -10
  44. package/dist/routes/files.d.ts.map +1 -1
  45. package/dist/routes/links.d.ts +2 -2
  46. package/dist/routes/links.d.ts.map +1 -1
  47. package/dist/routes/logs.d.ts +5 -5
  48. package/dist/routes/logs.d.ts.map +1 -1
  49. package/dist/routes/objects.d.ts +34 -34
  50. package/dist/routes/objects.d.ts.map +1 -1
  51. package/dist/routes/ontology.d.ts +62 -62
  52. package/dist/routes/ontology.d.ts.map +1 -1
  53. package/dist/routes/pipelines.d.ts +28 -34
  54. package/dist/routes/pipelines.d.ts.map +1 -1
  55. package/dist/routes/project.d.ts +2 -2
  56. package/dist/routes/project.d.ts.map +1 -1
  57. package/dist/routes/projections.d.ts +131 -83
  58. package/dist/routes/projections.d.ts.map +1 -1
  59. package/dist/routes/rules.d.ts +6 -6
  60. package/dist/routes/rules.d.ts.map +1 -1
  61. package/dist/routes/status.d.ts +2 -2
  62. package/dist/routes/status.d.ts.map +1 -1
  63. package/dist/routes/syncs.d.ts +27 -30
  64. package/dist/routes/syncs.d.ts.map +1 -1
  65. package/dist/routes/telemetry.d.ts +2 -2
  66. package/dist/routes/telemetry.d.ts.map +1 -1
  67. package/dist/routes/webhook-runs.d.ts +9 -9
  68. package/dist/routes/webhook-runs.d.ts.map +1 -1
  69. package/dist/routes/webhooks.d.ts +2 -2
  70. package/dist/routes/webhooks.d.ts.map +1 -1
  71. package/dist/routes/workflows.d.ts +132 -107
  72. package/dist/routes/workflows.d.ts.map +1 -1
  73. package/dist/routes/ws/agents.d.ts +11 -5
  74. package/dist/routes/ws/agents.d.ts.map +1 -1
  75. package/dist/routes/ws/events.d.ts +8 -8
  76. package/dist/routes/ws/events.d.ts.map +1 -1
  77. package/dist/routes/ws/log-subscription-hub.d.ts +6 -3
  78. package/dist/routes/ws/log-subscription-hub.d.ts.map +1 -1
  79. package/dist/routes/ws/logs.d.ts +10 -10
  80. package/dist/routes/ws/logs.d.ts.map +1 -1
  81. package/dist/schemas/actions.d.ts +97 -202
  82. package/dist/schemas/actions.d.ts.map +1 -1
  83. package/dist/schemas/agents.d.ts +443 -440
  84. package/dist/schemas/agents.d.ts.map +1 -1
  85. package/dist/schemas/ai-usage.d.ts +34 -0
  86. package/dist/schemas/ai-usage.d.ts.map +1 -0
  87. package/dist/schemas/auth.d.ts +168 -168
  88. package/dist/schemas/common.d.ts +37 -0
  89. package/dist/schemas/common.d.ts.map +1 -1
  90. package/dist/schemas/connectors.d.ts +713 -2
  91. package/dist/schemas/connectors.d.ts.map +1 -1
  92. package/dist/schemas/datasets.d.ts +29 -23
  93. package/dist/schemas/datasets.d.ts.map +1 -1
  94. package/dist/schemas/event-payloads.d.ts +6 -6
  95. package/dist/schemas/events.d.ts +34 -34
  96. package/dist/schemas/logs.d.ts +22 -22
  97. package/dist/schemas/objects.d.ts +10 -10
  98. package/dist/schemas/ontology.d.ts +174 -174
  99. package/dist/schemas/pipelines.d.ts +131 -190
  100. package/dist/schemas/pipelines.d.ts.map +1 -1
  101. package/dist/schemas/projections.d.ts +918 -563
  102. package/dist/schemas/projections.d.ts.map +1 -1
  103. package/dist/schemas/rules.d.ts +6 -6
  104. package/dist/schemas/status.d.ts +2 -2
  105. package/dist/schemas/syncs.d.ts +88 -124
  106. package/dist/schemas/syncs.d.ts.map +1 -1
  107. package/dist/schemas/webhook-runs.d.ts +41 -41
  108. package/dist/schemas/webhook-runs.d.ts.map +1 -1
  109. package/dist/schemas/workflows.d.ts +792 -406
  110. package/dist/schemas/workflows.d.ts.map +1 -1
  111. package/dist/server.d.ts +6 -6
  112. package/dist/server.d.ts.map +1 -1
  113. package/dist/utils/http.d.ts +2 -0
  114. package/dist/utils/http.d.ts.map +1 -1
  115. package/dist/utils/ws.d.ts +4 -3
  116. package/dist/utils/ws.d.ts.map +1 -1
  117. package/package.json +8 -5
  118. package/src/auth/access-token-boundary.ts +4 -4
  119. package/src/auth/application-access.ts +8 -9
  120. package/src/auth/connector-callback-cookie.ts +95 -0
  121. package/src/auth/guard.ts +11 -11
  122. package/src/auth/public-routes.ts +13 -0
  123. package/src/auth/scope.ts +34 -30
  124. package/src/auth/session-cookies.ts +3 -3
  125. package/src/connectors/http.ts +110 -0
  126. package/src/files/content.ts +11 -0
  127. package/src/openapi/tags.ts +9 -1
  128. package/src/registerRoutes.ts +34 -23
  129. package/src/routes/action-runs.ts +49 -42
  130. package/src/routes/actions.ts +7 -13
  131. package/src/routes/agent-api-gateway.ts +87 -42
  132. package/src/routes/agents.ts +89 -200
  133. package/src/routes/auth.ts +87 -91
  134. package/src/routes/connector-connection-runs.ts +346 -0
  135. package/src/routes/connector-connections.ts +135 -0
  136. package/src/routes/connectors.ts +24 -12
  137. package/src/routes/datasets.ts +55 -54
  138. package/src/routes/events.ts +5 -5
  139. package/src/routes/files.ts +24 -31
  140. package/src/routes/links.ts +32 -35
  141. package/src/routes/logs.ts +6 -11
  142. package/src/routes/objects.ts +51 -114
  143. package/src/routes/ontology.ts +27 -38
  144. package/src/routes/pipelines.ts +28 -62
  145. package/src/routes/project.ts +3 -3
  146. package/src/routes/projections.ts +26 -38
  147. package/src/routes/rules.ts +11 -25
  148. package/src/routes/status.ts +5 -5
  149. package/src/routes/syncs.ts +19 -45
  150. package/src/routes/telemetry.ts +18 -29
  151. package/src/routes/webhook-runs.ts +5 -5
  152. package/src/routes/webhooks.ts +259 -341
  153. package/src/routes/workflows.ts +260 -259
  154. package/src/routes/ws/agents.ts +196 -66
  155. package/src/routes/ws/events.ts +11 -10
  156. package/src/routes/ws/log-subscription-hub.ts +6 -12
  157. package/src/routes/ws/logs.ts +13 -5
  158. package/src/schemas/actions.ts +58 -25
  159. package/src/schemas/agents.ts +19 -14
  160. package/src/schemas/ai-usage.ts +16 -0
  161. package/src/schemas/common.ts +24 -0
  162. package/src/schemas/connectors.ts +164 -0
  163. package/src/schemas/datasets.ts +3 -2
  164. package/src/schemas/pipelines.ts +24 -40
  165. package/src/schemas/projections.ts +17 -3
  166. package/src/schemas/syncs.ts +16 -10
  167. package/src/schemas/webhook-runs.ts +10 -5
  168. package/src/schemas/workflows.ts +42 -25
  169. package/src/server.ts +65 -26
  170. package/src/utils/http.ts +34 -6
  171. package/src/utils/ws.ts +10 -5
@@ -1,9 +1,14 @@
1
- import type { OntologySource, Sixb } from "@sixb/core"
1
+ import type { SixbHostView } from "@sixb/core"
2
2
  import type { Elysia } from "elysia"
3
3
  import { registerActionRunRoutes } from "./routes/action-runs"
4
4
  import { registerActionRoutes } from "./routes/actions"
5
5
  import { registerAgentApiGatewayRoutes } from "./routes/agent-api-gateway"
6
6
  import { registerAgentRoutes } from "./routes/agents"
7
+ import {
8
+ type ConnectorConnectionRouteOptions,
9
+ registerConnectorConnectionRunRoutes,
10
+ } from "./routes/connector-connection-runs"
11
+ import { registerConnectorConnectionRoutes } from "./routes/connector-connections"
7
12
  import { registerConnectorRoutes } from "./routes/connectors"
8
13
  import { registerDatasetRoutes } from "./routes/datasets"
9
14
  import { registerEventRoutes } from "./routes/events"
@@ -22,28 +27,34 @@ import { registerTelemetryRoutes } from "./routes/telemetry"
22
27
  import { registerWebhookRunRoutes } from "./routes/webhook-runs"
23
28
  import { registerWorkflowRoutes } from "./routes/workflows"
24
29
 
25
- export function registerHttpRoutes(app: Elysia, sixb: Sixb<readonly OntologySource[]>) {
26
- registerAgentApiGatewayRoutes(app, sixb)
27
- registerProjectRoutes(app, sixb)
28
- registerStatusRoutes(app, sixb)
29
- registerConnectorRoutes(app, sixb)
30
- registerDatasetRoutes(app, sixb)
31
- registerSyncRoutes(app, sixb)
32
- registerPipelineRoutes(app, sixb)
33
- registerWorkflowRoutes(app, sixb)
34
- registerRuleRoutes(app, sixb)
35
- registerOntologyRoutes(app, sixb)
36
- registerObjectRoutes(app, sixb)
37
- registerActionRoutes(app, sixb)
38
- registerFileRoutes(app, sixb)
39
- registerActionRunRoutes(app, sixb)
40
- registerAgentRoutes(app, sixb)
41
- registerLinkRoutes(app, sixb)
42
- registerTelemetryRoutes(app, sixb)
43
- registerEventRoutes(app, sixb)
44
- registerLogRoutes(app, sixb)
45
- registerProjectionRoutes(app, sixb)
46
- registerWebhookRunRoutes(app, sixb)
30
+ export interface HttpRouteOptions {
31
+ readonly connectorConnections: ConnectorConnectionRouteOptions
32
+ }
33
+
34
+ export function registerHttpRoutes(app: Elysia, host: SixbHostView, options: HttpRouteOptions) {
35
+ registerAgentApiGatewayRoutes(app, host)
36
+ registerProjectRoutes(app, host)
37
+ registerStatusRoutes(app, host)
38
+ registerConnectorRoutes(app, host)
39
+ registerConnectorConnectionRoutes(app, host)
40
+ registerConnectorConnectionRunRoutes(app, host, options.connectorConnections)
41
+ registerDatasetRoutes(app, host)
42
+ registerSyncRoutes(app, host)
43
+ registerPipelineRoutes(app, host)
44
+ registerWorkflowRoutes(app, host)
45
+ registerRuleRoutes(app, host)
46
+ registerOntologyRoutes(app, host)
47
+ registerObjectRoutes(app, host)
48
+ registerActionRoutes(app, host)
49
+ registerFileRoutes(app, host)
50
+ registerActionRunRoutes(app, host)
51
+ registerAgentRoutes(app, host)
52
+ registerLinkRoutes(app, host)
53
+ registerTelemetryRoutes(app, host)
54
+ registerEventRoutes(app, host)
55
+ registerLogRoutes(app, host)
56
+ registerProjectionRoutes(app, host)
57
+ registerWebhookRunRoutes(app, host)
47
58
 
48
59
  return app
49
60
  }
@@ -1,14 +1,14 @@
1
- import type { OntologySource, Sixb } from "@sixb/core"
2
- import { canViewActionRun } from "@sixb/core/internal/authorization"
1
+ import type { SixbHostView } from "@sixb/core"
3
2
  import type { ActionRunRecord } from "@sixb/core/storage"
4
3
  import type { Elysia } from "elysia"
5
4
  import { z } from "zod"
6
5
  import { bearerSecurityRequirement } from "../auth/access-token-boundary"
7
- import { requestAuthState } from "../auth/scope"
6
+ import { requireRequestSixb } from "../auth/scope"
8
7
  import {
9
8
  createContextualFileContentResponse,
10
9
  fileContentGetResponses,
11
10
  fileContentHeadResponses,
11
+ handleFileContentQueryValidationError,
12
12
  } from "../files/content"
13
13
  import { OPENAPI_TAGS } from "../openapi/tags"
14
14
  import {
@@ -32,7 +32,10 @@ const ActionRunFileContentQuerySchema = FileContentQuerySchema.extend({
32
32
  path: z
33
33
  .string()
34
34
  .min(1)
35
- .regex(/^\/params(?:\/|$)/, "Action run file content paths must start with /params/"),
35
+ .regex(
36
+ /^\/(?:params|writeback\/result)(?:\/|$)/,
37
+ "Action run file content paths must start with /params/ or /writeback/result/"
38
+ ),
36
39
  })
37
40
 
38
41
  function serializeActionRunSummary(
@@ -59,25 +62,35 @@ function serializeActionRunDetail(
59
62
  ...serializeActionRunSummary(run),
60
63
  params: run.params,
61
64
  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
- }
65
+ ? run.writeback.status === "succeeded"
66
+ ? {
67
+ status: "succeeded",
68
+ completedAt: toIsoString(run.writeback.completedAt),
69
+ result: run.writeback.result,
70
+ }
71
+ : {
72
+ status: "failed",
73
+ completedAt: toIsoString(run.writeback.completedAt),
74
+ error: run.writeback.error,
75
+ }
68
76
  : undefined,
69
77
  effects: run.effects
70
- ? {
71
- status: run.effects.status,
72
- completedAt: toIsoString(run.effects.completedAt),
73
- error: run.effects.error,
74
- }
78
+ ? run.effects.status === "succeeded"
79
+ ? {
80
+ status: "succeeded",
81
+ completedAt: toIsoString(run.effects.completedAt),
82
+ }
83
+ : {
84
+ status: "failed",
85
+ completedAt: toIsoString(run.effects.completedAt),
86
+ error: run.effects.error,
87
+ }
75
88
  : undefined,
76
89
  })
77
90
  }
78
91
 
79
92
  async function actionRunFileContentResponse(
80
- sixb: Sixb<readonly OntologySource[]>,
93
+ host: SixbHostView,
81
94
  context: {
82
95
  readonly params: { readonly runId: string }
83
96
  readonly query: unknown
@@ -86,40 +99,36 @@ async function actionRunFileContentResponse(
86
99
  },
87
100
  options: { readonly head?: boolean } = {}
88
101
  ) {
89
- const { authz } = requestAuthState(context)
102
+ const sixb = requireRequestSixb(context)
90
103
 
91
- const storage = sixb.storage.actionRuns
104
+ const storage = host.storage.actionRuns
92
105
  if (!storage) {
93
106
  return unconfiguredStorageResponse(context.set, "Action run storage")
94
107
  }
95
108
 
96
109
  return createContextualFileContentResponse({
97
- blobStorage: sixb.blobStorage,
110
+ blobStorage: host.blobStorage,
98
111
  query: context.query,
99
112
  querySchema: ActionRunFileContentQuerySchema,
100
113
  request: context.request,
101
114
  set: context.set,
102
115
  head: options.head,
103
116
  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)
117
+ const run = await sixb.actions.runs.getById(context.params.runId)
118
+ return run ? serializeActionRunDetail(run) : null
110
119
  },
111
120
  })
112
121
  }
113
122
 
114
- export function registerActionRunRoutes(app: Elysia, sixb: Sixb<readonly OntologySource[]>) {
123
+ export function registerActionRunRoutes(app: Elysia, host: SixbHostView) {
115
124
  return app
116
125
  .get(
117
126
  "/api/action-runs",
118
127
  async (context) => {
119
128
  const { query, set } = context
120
- const { authz } = requestAuthState(context)
129
+ const sixb = requireRequestSixb(context)
121
130
  try {
122
- const storage = sixb.storage.actionRuns
131
+ const storage = host.storage.actionRuns
123
132
  if (!storage) {
124
133
  return unconfiguredStorageResponse(set, "Action run storage")
125
134
  }
@@ -127,14 +136,9 @@ export function registerActionRunRoutes(app: Elysia, sixb: Sixb<readonly Ontolog
127
136
  const parsed = ActionRunsQuerySchema.parse(query)
128
137
  const limit = parseOptionalInt(parsed.limit)
129
138
  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,
139
+ const result = await sixb.actions.runs.list({
134
140
  actionId: parsed.actionId,
135
- actionIds,
136
141
  objectTypeId: parsed.objectTypeId,
137
- objectTypeIds,
138
142
  primaryId: parsed.primaryId,
139
143
  statuses: parsed.status ? [parsed.status] : undefined,
140
144
  startedAfter: parseDate(parsed.startedAfter),
@@ -164,6 +168,7 @@ export function registerActionRunRoutes(app: Elysia, sixb: Sixb<readonly Ontolog
164
168
  summary: "List action run history",
165
169
  tags: [OPENAPI_TAGS.actionRuns.name],
166
170
  operationId: "listActionRuns",
171
+ security: bearerSecurityRequirement("listActionRuns"),
167
172
  },
168
173
  }
169
174
  )
@@ -171,15 +176,15 @@ export function registerActionRunRoutes(app: Elysia, sixb: Sixb<readonly Ontolog
171
176
  "/api/action-runs/:runId",
172
177
  async (context) => {
173
178
  const { params, set } = context
174
- const { authz } = requestAuthState(context)
179
+ const sixb = requireRequestSixb(context)
175
180
  try {
176
- const storage = sixb.storage.actionRuns
181
+ const storage = host.storage.actionRuns
177
182
  if (!storage) {
178
183
  return unconfiguredStorageResponse(set, "Action run storage")
179
184
  }
180
185
 
181
- const run = await storage.getById({ projectId: sixb.id, id: params.runId })
182
- if (!run || !canViewActionRun(authz, run)) {
186
+ const run = await sixb.actions.runs.getById(params.runId)
187
+ if (!run) {
183
188
  set.status = 404
184
189
  return { error: "Action run not found" }
185
190
  }
@@ -207,10 +212,11 @@ export function registerActionRunRoutes(app: Elysia, sixb: Sixb<readonly Ontolog
207
212
  )
208
213
  .get(
209
214
  "/api/action-runs/:runId/files/content",
210
- (context) => actionRunFileContentResponse(sixb, context),
215
+ (context) => actionRunFileContentResponse(host, context),
211
216
  {
212
217
  params: ActionRunIdParamsSchema,
213
- query: FileContentQuerySchema,
218
+ query: ActionRunFileContentQuerySchema,
219
+ error: handleFileContentQueryValidationError,
214
220
  detail: {
215
221
  summary: "Get action run file content",
216
222
  tags: ["Actions"],
@@ -222,10 +228,11 @@ export function registerActionRunRoutes(app: Elysia, sixb: Sixb<readonly Ontolog
222
228
  )
223
229
  .head(
224
230
  "/api/action-runs/:runId/files/content",
225
- (context) => actionRunFileContentResponse(sixb, context, { head: true }),
231
+ (context) => actionRunFileContentResponse(host, context, { head: true }),
226
232
  {
227
233
  params: ActionRunIdParamsSchema,
228
- query: FileContentQuerySchema,
234
+ query: ActionRunFileContentQuerySchema,
235
+ error: handleFileContentQueryValidationError,
229
236
  detail: {
230
237
  summary: "Head action run file content",
231
238
  tags: ["Actions"],
@@ -1,7 +1,7 @@
1
- import type { ActionDefinition, OntologySource, Sixb } from "@sixb/core"
1
+ import type { ActionDefinition, SixbHostView } from "@sixb/core"
2
2
  import type { Elysia } from "elysia"
3
3
  import { bearerSecurityRequirement } from "../auth/access-token-boundary"
4
- import { requestAuthState } from "../auth/scope"
4
+ import { requireRequestSixb } from "../auth/scope"
5
5
  import { OPENAPI_TAGS } from "../openapi/tags"
6
6
  import {
7
7
  ActionCatalogItemSchema,
@@ -38,13 +38,12 @@ function serializeAction(
38
38
  })
39
39
  }
40
40
 
41
- export function registerActionRoutes(app: Elysia, sixb: Sixb<readonly OntologySource[]>) {
41
+ export function registerActionRoutes(app: Elysia, _host: SixbHostView) {
42
42
  return app
43
43
  .get(
44
44
  "/api/actions",
45
45
  async (context) => {
46
- const { scoped } = requestAuthState(context)
47
- const actions = scoped ? scoped.listActions() : sixb.listActions()
46
+ const actions = requireRequestSixb(context).actions.list()
48
47
  return actions.map(serializeAction)
49
48
  },
50
49
  {
@@ -61,10 +60,7 @@ export function registerActionRoutes(app: Elysia, sixb: Sixb<readonly OntologySo
61
60
  "/api/actions/:actionId",
62
61
  async (context) => {
63
62
  const { params, set } = context
64
- const { scoped } = requestAuthState(context)
65
- const action = scoped
66
- ? scoped.getActionById(params.actionId)
67
- : sixb.getActionById(params.actionId)
63
+ const action = requireRequestSixb(context).actions.getById(params.actionId)
68
64
  if (!action) {
69
65
  set.status = 404
70
66
  return { error: "Action not found" }
@@ -87,7 +83,7 @@ export function registerActionRoutes(app: Elysia, sixb: Sixb<readonly OntologySo
87
83
  "/api/actions/:actionId",
88
84
  async (context) => {
89
85
  const { params, body, set } = context
90
- const { scoped } = requestAuthState(context)
86
+ const sixb = requireRequestSixb(context)
91
87
  try {
92
88
  const parsedBody = RequestActionBodySchema.parse(body)
93
89
  const input = {
@@ -96,9 +92,7 @@ export function registerActionRoutes(app: Elysia, sixb: Sixb<readonly OntologySo
96
92
  params: parsedBody.params,
97
93
  runId: parsedBody.runId,
98
94
  }
99
- const result = scoped
100
- ? await scoped.requestAction(input)
101
- : await sixb.actions.request(input)
95
+ const result = await sixb.actions.request(input)
102
96
 
103
97
  set.status = 202
104
98
  return RequestActionResponseSchema.parse(result)
@@ -1,13 +1,16 @@
1
- import { type OntologySource, resolveAuthorizationContext, type Sixb } from "@sixb/core"
1
+ import type { AuthorizationContext, SixbHostView } from "@sixb/core"
2
+ import { assertAgentExecutionRecord } from "@sixb/core/internal/agent-execution"
2
3
  import {
3
4
  AGENT_API_GATEWAY_PREFIX,
4
5
  AGENT_API_ROUTES,
5
6
  isAllowedAgentApiRequest,
6
7
  isValidAgentApiGatewayCapability,
8
+ resolveAgentExecutionAuthorization,
7
9
  } from "@sixb/core/internal/agents"
8
10
  import { normalizeRoutePath, pathSegmentsFor } from "@sixb/core/internal/http"
9
11
  import type { Elysia } from "elysia"
10
12
  import { registerInternalRequestAuthState } from "../auth/scope"
13
+ import { DEFAULT_SIMPLE_FILE_UPLOAD_BODY_BYTES } from "./files"
11
14
 
12
15
  const MAX_AGENT_API_BODY_BYTES = 1_000_000
13
16
  const HOP_BY_HOP_RESPONSE_HEADERS = new Set([
@@ -21,12 +24,12 @@ const HOP_BY_HOP_RESPONSE_HEADERS = new Set([
21
24
  "upgrade",
22
25
  ])
23
26
 
24
- export function registerAgentApiGatewayRoutes(app: Elysia, sixb: Sixb<readonly OntologySource[]>) {
27
+ export function registerAgentApiGatewayRoutes(app: Elysia, host: SixbHostView) {
25
28
  for (const route of AGENT_API_ROUTES) {
26
29
  const path = `${AGENT_API_GATEWAY_PREFIX}/:agentGatewayRunId/:agentGatewayCapability${route.path}`
27
30
  const handler = async (context: AgentApiGatewayRouteContext) =>
28
31
  handleAgentApiGatewayRequest({
29
- sixb,
32
+ host,
30
33
  app,
31
34
  request: context.request,
32
35
  agentRunId: context.params.agentGatewayRunId ?? "",
@@ -52,7 +55,7 @@ interface AgentApiGatewayRouteContext {
52
55
  }
53
56
 
54
57
  async function handleAgentApiGatewayRequest(input: {
55
- readonly sixb: Sixb<readonly OntologySource[]>
58
+ readonly host: SixbHostView
56
59
  readonly app: Elysia
57
60
  readonly request: Request
58
61
  readonly agentRunId: string
@@ -67,17 +70,29 @@ async function handleAgentApiGatewayRequest(input: {
67
70
  )
68
71
  }
69
72
 
70
- const authState = await resolveAgentRunAuthState(input.sixb, {
73
+ const authState = await resolveAgentRunAuthState(input.host, {
71
74
  runId: input.agentRunId,
72
75
  capability: input.capability,
73
76
  })
74
77
  if (authState instanceof Response) {
75
78
  return authState
76
79
  }
80
+ if (
81
+ authState.agentExecution.kind === "workflow" &&
82
+ input.request.method === "POST" &&
83
+ matchesWorkflowRunStart(upstreamPath)
84
+ ) {
85
+ return jsonError(409, "Workflow agent nodes cannot start another workflow run.")
86
+ }
77
87
 
78
88
  let body: ArrayBuffer | undefined
79
89
  try {
80
- body = await readRequestBody(input.request)
90
+ body = await readRequestBody(
91
+ input.request,
92
+ isSimpleFileUpload(input.request.method, upstreamPath)
93
+ ? DEFAULT_SIMPLE_FILE_UPLOAD_BODY_BYTES
94
+ : MAX_AGENT_API_BODY_BYTES
95
+ )
81
96
  } catch (error) {
82
97
  if (error instanceof PayloadTooLargeError) {
83
98
  return jsonError(413, error.message)
@@ -102,11 +117,11 @@ async function handleAgentApiGatewayRequest(input: {
102
117
  }
103
118
 
104
119
  async function resolveAgentRunAuthState(
105
- sixb: Sixb<readonly OntologySource[]>,
120
+ host: SixbHostView,
106
121
  input: { readonly runId: string; readonly capability: string }
107
122
  ) {
108
- const agentStorage = sixb.storage.agents
109
- const workflowRuns = sixb.storage.workflowRuns
123
+ const agentStorage = host.storage.agents
124
+ const workflowRuns = host.storage.workflowRuns
110
125
  if (!agentStorage && !workflowRuns) {
111
126
  // 501, not 403: nothing about the caller is being refused — neither storage role
112
127
  // that could hold an agent run is configured, so no request can ever succeed here.
@@ -114,23 +129,29 @@ async function resolveAgentRunAuthState(
114
129
  }
115
130
 
116
131
  const conversationalRun = await agentStorage?.runs.getById({
117
- projectId: sixb.id,
132
+ projectId: host.id,
118
133
  id: input.runId,
119
134
  })
120
135
  const workflowRun = conversationalRun
121
136
  ? null
122
137
  : await workflowRuns?.agentNodes.getByNodeRunId({
123
- projectId: sixb.id,
138
+ projectId: host.id,
124
139
  nodeRunId: input.runId,
125
140
  })
126
141
  const run = conversationalRun ?? workflowRun
142
+ const agentExecution = conversationalRun
143
+ ? ({ kind: "conversation", runId: conversationalRun.id } as const)
144
+ : workflowRun
145
+ ? ({ kind: "workflow", nodeRunId: workflowRun.nodeRunId } as const)
146
+ : null
127
147
  if (
128
148
  !run ||
149
+ !agentExecution ||
129
150
  run.status !== "running" ||
130
151
  !run.execution ||
131
152
  run.execution.queueLeaseExpiresAt.getTime() <= Date.now() ||
132
153
  !isValidAgentApiGatewayCapability({
133
- projectId: sixb.id,
154
+ projectId: host.id,
134
155
  runId: input.runId,
135
156
  executionToken: run.execution.token,
136
157
  capability: input.capability,
@@ -139,46 +160,49 @@ async function resolveAgentRunAuthState(
139
160
  return jsonError(403, "Agent API gateway capability is not valid for this run.")
140
161
  }
141
162
 
142
- if (!sixb.auth.isEnabled()) {
163
+ if (!host.auth.isEnabled()) {
143
164
  return {
144
- authz: null,
145
- scoped: null,
165
+ authorization: { type: "disabled" as const },
166
+ agentExecution,
146
167
  ...(conversationalRun ? { agentRun: conversationalRun } : {}),
147
168
  }
148
169
  }
149
170
 
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
171
+ const auth = host.storage.auth
154
172
  if (!auth) {
155
173
  return jsonError(501, "Agent API gateway is not configured for authenticated agent access.")
156
174
  }
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,
175
+ const execution = await host.storage.executions.getById({
176
+ projectId: host.id,
177
+ id: run.executionId,
164
178
  })
165
- if (!serviceAccount || serviceAccount.status !== "active") {
166
- return jsonError(403, "Agent execution identity is not active.")
179
+ if (!execution) {
180
+ return jsonError(403, "Agent run execution is not available.")
167
181
  }
168
182
 
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
- })
183
+ let authz: AuthorizationContext
184
+ try {
185
+ const resolved = await resolveAgentExecutionAuthorization({
186
+ auth,
187
+ projectId: host.id,
188
+ agentId: run.agentId,
189
+ authorizationRef: execution.authorizationRef,
190
+ security: host.definitions.security,
191
+ })
192
+ assertAgentExecutionRecord({
193
+ execution,
194
+ agentId: run.agentId,
195
+ runId: input.runId,
196
+ authorization: resolved.context,
197
+ })
198
+ authz = resolved.context
199
+ } catch {
200
+ return jsonError(403, "Agent run execution authority is not valid.")
201
+ }
178
202
 
179
203
  return {
180
- authz,
181
- scoped: sixb.as(authz),
204
+ authorization: { type: "principal" as const, context: authz },
205
+ agentExecution,
182
206
  ...(conversationalRun ? { agentRun: conversationalRun } : {}),
183
207
  }
184
208
  }
@@ -220,7 +244,10 @@ function copyHeader(source: Headers, target: Headers, name: string): void {
220
244
  }
221
245
  }
222
246
 
223
- async function readRequestBody(request: Request): Promise<ArrayBuffer | undefined> {
247
+ async function readRequestBody(
248
+ request: Request,
249
+ maxBytes: number
250
+ ): Promise<ArrayBuffer | undefined> {
224
251
  if (!request.body || request.method === "GET" || request.method === "HEAD") {
225
252
  return undefined
226
253
  }
@@ -233,8 +260,12 @@ async function readRequestBody(request: Request): Promise<ArrayBuffer | undefine
233
260
  const { done, value } = await reader.read()
234
261
  if (done) break
235
262
  total += value.byteLength
236
- if (total > MAX_AGENT_API_BODY_BYTES) {
237
- throw new PayloadTooLargeError("Agent API gateway request body exceeds 1MB.")
263
+ if (total > maxBytes) {
264
+ throw new PayloadTooLargeError(
265
+ maxBytes === MAX_AGENT_API_BODY_BYTES
266
+ ? "Agent API gateway request body exceeds 1MB."
267
+ : `Agent API gateway file upload request body exceeds the ${maxBytes} byte limit.`
268
+ )
238
269
  }
239
270
  chunks.push(value)
240
271
  }
@@ -242,6 +273,20 @@ async function readRequestBody(request: Request): Promise<ArrayBuffer | undefine
242
273
  return concatChunks(chunks, total)
243
274
  }
244
275
 
276
+ function isSimpleFileUpload(method: string, pathname: string): boolean {
277
+ return method === "POST" && normalizeRoutePath(pathname) === "/api/files"
278
+ }
279
+
280
+ function matchesWorkflowRunStart(pathname: string): boolean {
281
+ const segments = pathSegmentsFor(pathname)
282
+ return (
283
+ segments.length === 4 &&
284
+ segments[0] === "api" &&
285
+ segments[1] === "workflows" &&
286
+ segments[3] === "runs"
287
+ )
288
+ }
289
+
245
290
  function concatChunks(chunks: readonly Uint8Array[], total: number): ArrayBuffer {
246
291
  const buffer = new ArrayBuffer(total)
247
292
  const result = new Uint8Array(buffer)