@sixb/server 0.1.1 → 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 (166) 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 +13 -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/index.js +3293 -2704
  16. package/dist/index.js.map +55 -50
  17. package/dist/openapi/tags.d.ts +11 -3
  18. package/dist/openapi/tags.d.ts.map +1 -1
  19. package/dist/registerRoutes.d.ts +6 -2
  20. package/dist/registerRoutes.d.ts.map +1 -1
  21. package/dist/routes/action-runs.d.ts +21 -31
  22. package/dist/routes/action-runs.d.ts.map +1 -1
  23. package/dist/routes/actions.d.ts +9 -8
  24. package/dist/routes/actions.d.ts.map +1 -1
  25. package/dist/routes/agent-api-gateway.d.ts +2 -2
  26. package/dist/routes/agent-api-gateway.d.ts.map +1 -1
  27. package/dist/routes/agents.d.ts +130 -131
  28. package/dist/routes/agents.d.ts.map +1 -1
  29. package/dist/routes/auth.d.ts +84 -84
  30. package/dist/routes/auth.d.ts.map +1 -1
  31. package/dist/routes/connector-connection-runs.d.ts +794 -0
  32. package/dist/routes/connector-connection-runs.d.ts.map +1 -0
  33. package/dist/routes/connector-connections.d.ts +219 -0
  34. package/dist/routes/connector-connections.d.ts.map +1 -0
  35. package/dist/routes/connectors.d.ts +10 -4
  36. package/dist/routes/connectors.d.ts.map +1 -1
  37. package/dist/routes/datasets.d.ts +8 -4
  38. package/dist/routes/datasets.d.ts.map +1 -1
  39. package/dist/routes/events.d.ts +8 -8
  40. package/dist/routes/events.d.ts.map +1 -1
  41. package/dist/routes/files.d.ts +11 -10
  42. package/dist/routes/files.d.ts.map +1 -1
  43. package/dist/routes/links.d.ts +2 -2
  44. package/dist/routes/links.d.ts.map +1 -1
  45. package/dist/routes/logs.d.ts +5 -5
  46. package/dist/routes/logs.d.ts.map +1 -1
  47. package/dist/routes/objects.d.ts +34 -34
  48. package/dist/routes/objects.d.ts.map +1 -1
  49. package/dist/routes/ontology.d.ts +50 -50
  50. package/dist/routes/ontology.d.ts.map +1 -1
  51. package/dist/routes/pipelines.d.ts +24 -34
  52. package/dist/routes/pipelines.d.ts.map +1 -1
  53. package/dist/routes/project.d.ts +2 -2
  54. package/dist/routes/project.d.ts.map +1 -1
  55. package/dist/routes/projections.d.ts +131 -83
  56. package/dist/routes/projections.d.ts.map +1 -1
  57. package/dist/routes/rules.d.ts +6 -6
  58. package/dist/routes/rules.d.ts.map +1 -1
  59. package/dist/routes/status.d.ts +2 -2
  60. package/dist/routes/status.d.ts.map +1 -1
  61. package/dist/routes/syncs.d.ts +20 -25
  62. package/dist/routes/syncs.d.ts.map +1 -1
  63. package/dist/routes/telemetry.d.ts +2 -2
  64. package/dist/routes/telemetry.d.ts.map +1 -1
  65. package/dist/routes/webhook-runs.d.ts +9 -9
  66. package/dist/routes/webhook-runs.d.ts.map +1 -1
  67. package/dist/routes/webhooks.d.ts +2 -2
  68. package/dist/routes/webhooks.d.ts.map +1 -1
  69. package/dist/routes/workflows.d.ts +74 -48
  70. package/dist/routes/workflows.d.ts.map +1 -1
  71. package/dist/routes/ws/agents.d.ts +11 -5
  72. package/dist/routes/ws/agents.d.ts.map +1 -1
  73. package/dist/routes/ws/events.d.ts +8 -8
  74. package/dist/routes/ws/events.d.ts.map +1 -1
  75. package/dist/routes/ws/log-subscription-hub.d.ts +6 -3
  76. package/dist/routes/ws/log-subscription-hub.d.ts.map +1 -1
  77. package/dist/routes/ws/logs.d.ts +10 -10
  78. package/dist/routes/ws/logs.d.ts.map +1 -1
  79. package/dist/schemas/actions.d.ts +97 -202
  80. package/dist/schemas/actions.d.ts.map +1 -1
  81. package/dist/schemas/agents.d.ts +442 -439
  82. package/dist/schemas/agents.d.ts.map +1 -1
  83. package/dist/schemas/ai-usage.d.ts +34 -0
  84. package/dist/schemas/ai-usage.d.ts.map +1 -0
  85. package/dist/schemas/auth.d.ts +142 -142
  86. package/dist/schemas/common.d.ts +37 -0
  87. package/dist/schemas/common.d.ts.map +1 -1
  88. package/dist/schemas/connectors.d.ts +713 -2
  89. package/dist/schemas/connectors.d.ts.map +1 -1
  90. package/dist/schemas/datasets.d.ts +2 -2
  91. package/dist/schemas/event-payloads.d.ts +6 -6
  92. package/dist/schemas/events.d.ts +34 -34
  93. package/dist/schemas/logs.d.ts +22 -22
  94. package/dist/schemas/objects.d.ts +10 -10
  95. package/dist/schemas/ontology.d.ts +118 -118
  96. package/dist/schemas/pipelines.d.ts +95 -190
  97. package/dist/schemas/pipelines.d.ts.map +1 -1
  98. package/dist/schemas/projections.d.ts +918 -563
  99. package/dist/schemas/projections.d.ts.map +1 -1
  100. package/dist/schemas/rules.d.ts +6 -6
  101. package/dist/schemas/status.d.ts +2 -2
  102. package/dist/schemas/syncs.d.ts +65 -108
  103. package/dist/schemas/syncs.d.ts.map +1 -1
  104. package/dist/schemas/webhook-runs.d.ts +41 -41
  105. package/dist/schemas/webhook-runs.d.ts.map +1 -1
  106. package/dist/schemas/workflows.d.ts +516 -183
  107. package/dist/schemas/workflows.d.ts.map +1 -1
  108. package/dist/server.d.ts +6 -6
  109. package/dist/server.d.ts.map +1 -1
  110. package/dist/utils/http.d.ts +2 -0
  111. package/dist/utils/http.d.ts.map +1 -1
  112. package/dist/utils/ws.d.ts +4 -3
  113. package/dist/utils/ws.d.ts.map +1 -1
  114. package/package.json +4 -4
  115. package/src/auth/access-token-boundary.ts +4 -4
  116. package/src/auth/application-access.ts +8 -9
  117. package/src/auth/connector-callback-cookie.ts +95 -0
  118. package/src/auth/guard.ts +11 -11
  119. package/src/auth/public-routes.ts +13 -0
  120. package/src/auth/scope.ts +26 -36
  121. package/src/auth/session-cookies.ts +3 -3
  122. package/src/connectors/http.ts +110 -0
  123. package/src/openapi/tags.ts +9 -1
  124. package/src/registerRoutes.ts +34 -23
  125. package/src/routes/action-runs.ts +39 -39
  126. package/src/routes/actions.ts +7 -13
  127. package/src/routes/agent-api-gateway.ts +41 -38
  128. package/src/routes/agents.ts +89 -200
  129. package/src/routes/auth.ts +87 -91
  130. package/src/routes/connector-connection-runs.ts +346 -0
  131. package/src/routes/connector-connections.ts +135 -0
  132. package/src/routes/connectors.ts +24 -12
  133. package/src/routes/datasets.ts +55 -54
  134. package/src/routes/events.ts +5 -5
  135. package/src/routes/files.ts +22 -30
  136. package/src/routes/links.ts +31 -35
  137. package/src/routes/logs.ts +6 -11
  138. package/src/routes/objects.ts +51 -114
  139. package/src/routes/ontology.ts +27 -38
  140. package/src/routes/pipelines.ts +28 -62
  141. package/src/routes/project.ts +3 -3
  142. package/src/routes/projections.ts +26 -38
  143. package/src/routes/rules.ts +11 -25
  144. package/src/routes/status.ts +5 -5
  145. package/src/routes/syncs.ts +19 -45
  146. package/src/routes/telemetry.ts +18 -29
  147. package/src/routes/webhook-runs.ts +5 -5
  148. package/src/routes/webhooks.ts +259 -341
  149. package/src/routes/workflows.ts +226 -248
  150. package/src/routes/ws/agents.ts +196 -66
  151. package/src/routes/ws/events.ts +11 -10
  152. package/src/routes/ws/log-subscription-hub.ts +6 -12
  153. package/src/routes/ws/logs.ts +13 -5
  154. package/src/schemas/actions.ts +58 -25
  155. package/src/schemas/agents.ts +19 -14
  156. package/src/schemas/ai-usage.ts +16 -0
  157. package/src/schemas/common.ts +24 -0
  158. package/src/schemas/connectors.ts +164 -0
  159. package/src/schemas/pipelines.ts +23 -15
  160. package/src/schemas/projections.ts +17 -3
  161. package/src/schemas/syncs.ts +13 -8
  162. package/src/schemas/webhook-runs.ts +10 -5
  163. package/src/schemas/workflows.ts +16 -6
  164. package/src/server.ts +65 -26
  165. package/src/utils/http.ts +34 -6
  166. 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,10 +1,9 @@
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,
@@ -63,25 +62,35 @@ function serializeActionRunDetail(
63
62
  ...serializeActionRunSummary(run),
64
63
  params: run.params,
65
64
  writeback: run.writeback
66
- ? {
67
- status: run.writeback.status,
68
- completedAt: toIsoString(run.writeback.completedAt),
69
- ...(run.writeback.result !== undefined ? { result: run.writeback.result } : {}),
70
- error: run.writeback.error,
71
- }
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
+ }
72
76
  : undefined,
73
77
  effects: run.effects
74
- ? {
75
- status: run.effects.status,
76
- completedAt: toIsoString(run.effects.completedAt),
77
- error: run.effects.error,
78
- }
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
+ }
79
88
  : undefined,
80
89
  })
81
90
  }
82
91
 
83
92
  async function actionRunFileContentResponse(
84
- sixb: Sixb<readonly OntologySource[]>,
93
+ host: SixbHostView,
85
94
  context: {
86
95
  readonly params: { readonly runId: string }
87
96
  readonly query: unknown
@@ -90,40 +99,36 @@ async function actionRunFileContentResponse(
90
99
  },
91
100
  options: { readonly head?: boolean } = {}
92
101
  ) {
93
- const { authz } = requestAuthState(context)
102
+ const sixb = requireRequestSixb(context)
94
103
 
95
- const storage = sixb.storage.actionRuns
104
+ const storage = host.storage.actionRuns
96
105
  if (!storage) {
97
106
  return unconfiguredStorageResponse(context.set, "Action run storage")
98
107
  }
99
108
 
100
109
  return createContextualFileContentResponse({
101
- blobStorage: sixb.blobStorage,
110
+ blobStorage: host.blobStorage,
102
111
  query: context.query,
103
112
  querySchema: ActionRunFileContentQuerySchema,
104
113
  request: context.request,
105
114
  set: context.set,
106
115
  head: options.head,
107
116
  resolveRoot: async () => {
108
- const run = await storage.getById({ projectId: sixb.id, id: context.params.runId })
109
- if (!run || !canViewActionRun(authz, run)) {
110
- return null
111
- }
112
-
113
- return serializeActionRunDetail(run)
117
+ const run = await sixb.actions.runs.getById(context.params.runId)
118
+ return run ? serializeActionRunDetail(run) : null
114
119
  },
115
120
  })
116
121
  }
117
122
 
118
- export function registerActionRunRoutes(app: Elysia, sixb: Sixb<readonly OntologySource[]>) {
123
+ export function registerActionRunRoutes(app: Elysia, host: SixbHostView) {
119
124
  return app
120
125
  .get(
121
126
  "/api/action-runs",
122
127
  async (context) => {
123
128
  const { query, set } = context
124
- const { authz } = requestAuthState(context)
129
+ const sixb = requireRequestSixb(context)
125
130
  try {
126
- const storage = sixb.storage.actionRuns
131
+ const storage = host.storage.actionRuns
127
132
  if (!storage) {
128
133
  return unconfiguredStorageResponse(set, "Action run storage")
129
134
  }
@@ -131,14 +136,9 @@ export function registerActionRunRoutes(app: Elysia, sixb: Sixb<readonly Ontolog
131
136
  const parsed = ActionRunsQuerySchema.parse(query)
132
137
  const limit = parseOptionalInt(parsed.limit)
133
138
  const offset = parseOptionalInt(parsed.offset)
134
- const actionIds = authz ? [...authz.grants["apply:action"]] : undefined
135
- const objectTypeIds = authz ? [...authz.grants["view:object"]] : undefined
136
- const result = await storage.list({
137
- projectId: sixb.id,
139
+ const result = await sixb.actions.runs.list({
138
140
  actionId: parsed.actionId,
139
- actionIds,
140
141
  objectTypeId: parsed.objectTypeId,
141
- objectTypeIds,
142
142
  primaryId: parsed.primaryId,
143
143
  statuses: parsed.status ? [parsed.status] : undefined,
144
144
  startedAfter: parseDate(parsed.startedAfter),
@@ -176,15 +176,15 @@ export function registerActionRunRoutes(app: Elysia, sixb: Sixb<readonly Ontolog
176
176
  "/api/action-runs/:runId",
177
177
  async (context) => {
178
178
  const { params, set } = context
179
- const { authz } = requestAuthState(context)
179
+ const sixb = requireRequestSixb(context)
180
180
  try {
181
- const storage = sixb.storage.actionRuns
181
+ const storage = host.storage.actionRuns
182
182
  if (!storage) {
183
183
  return unconfiguredStorageResponse(set, "Action run storage")
184
184
  }
185
185
 
186
- const run = await storage.getById({ projectId: sixb.id, id: params.runId })
187
- if (!run || !canViewActionRun(authz, run)) {
186
+ const run = await sixb.actions.runs.getById(params.runId)
187
+ if (!run) {
188
188
  set.status = 404
189
189
  return { error: "Action run not found" }
190
190
  }
@@ -212,7 +212,7 @@ export function registerActionRunRoutes(app: Elysia, sixb: Sixb<readonly Ontolog
212
212
  )
213
213
  .get(
214
214
  "/api/action-runs/:runId/files/content",
215
- (context) => actionRunFileContentResponse(sixb, context),
215
+ (context) => actionRunFileContentResponse(host, context),
216
216
  {
217
217
  params: ActionRunIdParamsSchema,
218
218
  query: ActionRunFileContentQuerySchema,
@@ -228,7 +228,7 @@ export function registerActionRunRoutes(app: Elysia, sixb: Sixb<readonly Ontolog
228
228
  )
229
229
  .head(
230
230
  "/api/action-runs/:runId/files/content",
231
- (context) => actionRunFileContentResponse(sixb, context, { head: true }),
231
+ (context) => actionRunFileContentResponse(host, context, { head: true }),
232
232
  {
233
233
  params: ActionRunIdParamsSchema,
234
234
  query: ActionRunFileContentQuerySchema,
@@ -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,9 +1,11 @@
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"
@@ -22,12 +24,12 @@ const HOP_BY_HOP_RESPONSE_HEADERS = new Set([
22
24
  "upgrade",
23
25
  ])
24
26
 
25
- export function registerAgentApiGatewayRoutes(app: Elysia, sixb: Sixb<readonly OntologySource[]>) {
27
+ export function registerAgentApiGatewayRoutes(app: Elysia, host: SixbHostView) {
26
28
  for (const route of AGENT_API_ROUTES) {
27
29
  const path = `${AGENT_API_GATEWAY_PREFIX}/:agentGatewayRunId/:agentGatewayCapability${route.path}`
28
30
  const handler = async (context: AgentApiGatewayRouteContext) =>
29
31
  handleAgentApiGatewayRequest({
30
- sixb,
32
+ host,
31
33
  app,
32
34
  request: context.request,
33
35
  agentRunId: context.params.agentGatewayRunId ?? "",
@@ -53,7 +55,7 @@ interface AgentApiGatewayRouteContext {
53
55
  }
54
56
 
55
57
  async function handleAgentApiGatewayRequest(input: {
56
- readonly sixb: Sixb<readonly OntologySource[]>
58
+ readonly host: SixbHostView
57
59
  readonly app: Elysia
58
60
  readonly request: Request
59
61
  readonly agentRunId: string
@@ -68,7 +70,7 @@ async function handleAgentApiGatewayRequest(input: {
68
70
  )
69
71
  }
70
72
 
71
- const authState = await resolveAgentRunAuthState(input.sixb, {
73
+ const authState = await resolveAgentRunAuthState(input.host, {
72
74
  runId: input.agentRunId,
73
75
  capability: input.capability,
74
76
  })
@@ -115,11 +117,11 @@ async function handleAgentApiGatewayRequest(input: {
115
117
  }
116
118
 
117
119
  async function resolveAgentRunAuthState(
118
- sixb: Sixb<readonly OntologySource[]>,
120
+ host: SixbHostView,
119
121
  input: { readonly runId: string; readonly capability: string }
120
122
  ) {
121
- const agentStorage = sixb.storage.agents
122
- const workflowRuns = sixb.storage.workflowRuns
123
+ const agentStorage = host.storage.agents
124
+ const workflowRuns = host.storage.workflowRuns
123
125
  if (!agentStorage && !workflowRuns) {
124
126
  // 501, not 403: nothing about the caller is being refused — neither storage role
125
127
  // that could hold an agent run is configured, so no request can ever succeed here.
@@ -127,13 +129,13 @@ async function resolveAgentRunAuthState(
127
129
  }
128
130
 
129
131
  const conversationalRun = await agentStorage?.runs.getById({
130
- projectId: sixb.id,
132
+ projectId: host.id,
131
133
  id: input.runId,
132
134
  })
133
135
  const workflowRun = conversationalRun
134
136
  ? null
135
137
  : await workflowRuns?.agentNodes.getByNodeRunId({
136
- projectId: sixb.id,
138
+ projectId: host.id,
137
139
  nodeRunId: input.runId,
138
140
  })
139
141
  const run = conversationalRun ?? workflowRun
@@ -149,7 +151,7 @@ async function resolveAgentRunAuthState(
149
151
  !run.execution ||
150
152
  run.execution.queueLeaseExpiresAt.getTime() <= Date.now() ||
151
153
  !isValidAgentApiGatewayCapability({
152
- projectId: sixb.id,
154
+ projectId: host.id,
153
155
  runId: input.runId,
154
156
  executionToken: run.execution.token,
155
157
  capability: input.capability,
@@ -158,47 +160,48 @@ async function resolveAgentRunAuthState(
158
160
  return jsonError(403, "Agent API gateway capability is not valid for this run.")
159
161
  }
160
162
 
161
- if (!sixb.auth.isEnabled()) {
163
+ if (!host.auth.isEnabled()) {
162
164
  return {
163
- authz: null,
164
- scoped: null,
165
+ authorization: { type: "disabled" as const },
165
166
  agentExecution,
166
167
  ...(conversationalRun ? { agentRun: conversationalRun } : {}),
167
168
  }
168
169
  }
169
170
 
170
- // Two different failures shared one status. Missing auth storage is a runtime the
171
- // operator did not configure; a run without an execution principal is a run that
172
- // cannot prove an identity. Only the first is a 501.
173
- const auth = sixb.storage.auth
171
+ const auth = host.storage.auth
174
172
  if (!auth) {
175
173
  return jsonError(501, "Agent API gateway is not configured for authenticated agent access.")
176
174
  }
177
- if (!run.executionPrincipal) {
178
- return jsonError(403, "Agent run has no execution identity.")
179
- }
180
-
181
- const serviceAccount = await auth.serviceAccounts.getById({
182
- projectId: sixb.id,
183
- id: run.executionPrincipal.id,
175
+ const execution = await host.storage.executions.getById({
176
+ projectId: host.id,
177
+ id: run.executionId,
184
178
  })
185
- if (!serviceAccount || serviceAccount.status !== "active") {
186
- return jsonError(403, "Agent execution identity is not active.")
179
+ if (!execution) {
180
+ return jsonError(403, "Agent run execution is not available.")
187
181
  }
188
182
 
189
- const memberships = await auth.serviceAccountGroupMemberships.listForServiceAccount({
190
- projectId: sixb.id,
191
- serviceAccountId: serviceAccount.id,
192
- })
193
- const authz = resolveAuthorizationContext({
194
- principal: run.executionPrincipal,
195
- groupIds: memberships.map((membership) => membership.groupId),
196
- roles: sixb.security.listResolvedRoles(),
197
- })
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
+ }
198
202
 
199
203
  return {
200
- authz,
201
- scoped: sixb.as(authz),
204
+ authorization: { type: "principal" as const, context: authz },
202
205
  agentExecution,
203
206
  ...(conversationalRun ? { agentRun: conversationalRun } : {}),
204
207
  }