@sixb/server 0.1.6 → 0.1.8
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/dist/auth/access-token-boundary.d.ts +3 -0
- package/dist/auth/access-token-boundary.d.ts.map +1 -1
- package/dist/auth/public-routes.d.ts.map +1 -1
- package/dist/auth/scope.d.ts +6 -2
- package/dist/auth/scope.d.ts.map +1 -1
- package/dist/auth/shared-access.d.ts +66 -0
- package/dist/auth/shared-access.d.ts.map +1 -0
- package/dist/files/content.d.ts +8 -0
- package/dist/files/content.d.ts.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3402 -1693
- package/dist/index.js.map +35 -28
- package/dist/models/display.d.ts +22 -0
- package/dist/models/display.d.ts.map +1 -0
- package/dist/openapi/security.d.ts +15 -1
- package/dist/openapi/security.d.ts.map +1 -1
- package/dist/openapi/tags.d.ts +6 -2
- package/dist/openapi/tags.d.ts.map +1 -1
- package/dist/registerRoutes.d.ts +2 -0
- package/dist/registerRoutes.d.ts.map +1 -1
- package/dist/routes/action-runs.d.ts +2 -0
- package/dist/routes/action-runs.d.ts.map +1 -1
- package/dist/routes/actions.d.ts +1 -0
- package/dist/routes/actions.d.ts.map +1 -1
- package/dist/routes/agent-api-gateway.d.ts.map +1 -1
- package/dist/routes/agents.d.ts +100 -90
- package/dist/routes/agents.d.ts.map +1 -1
- package/dist/routes/ai-accounting.d.ts.map +1 -1
- package/dist/routes/auth.d.ts +143 -24
- package/dist/routes/auth.d.ts.map +1 -1
- package/dist/routes/connector-connection-runs.d.ts +8 -8
- package/dist/routes/connector-connections.d.ts +4 -4
- package/dist/routes/datasets.d.ts +5 -3
- package/dist/routes/datasets.d.ts.map +1 -1
- package/dist/routes/events.d.ts +3 -3
- package/dist/routes/files.d.ts +1 -0
- package/dist/routes/files.d.ts.map +1 -1
- package/dist/routes/logs.d.ts +1 -1
- package/dist/routes/models.d.ts +18 -1
- package/dist/routes/models.d.ts.map +1 -1
- package/dist/routes/objects.d.ts +3 -1
- package/dist/routes/objects.d.ts.map +1 -1
- package/dist/routes/pipelines.d.ts +6 -1
- package/dist/routes/pipelines.d.ts.map +1 -1
- package/dist/routes/rules.d.ts +1 -1
- package/dist/routes/share-grants.d.ts +264 -0
- package/dist/routes/share-grants.d.ts.map +1 -0
- package/dist/routes/shared-access.d.ts +155 -0
- package/dist/routes/shared-access.d.ts.map +1 -0
- package/dist/routes/syncs.d.ts +3 -0
- package/dist/routes/syncs.d.ts.map +1 -1
- package/dist/routes/workflows.d.ts +58 -55
- package/dist/routes/workflows.d.ts.map +1 -1
- package/dist/routes/ws/logs.d.ts +4 -4
- package/dist/schemas/agents.d.ts +385 -230
- package/dist/schemas/agents.d.ts.map +1 -1
- package/dist/schemas/ai-accounting.d.ts +1847 -295
- package/dist/schemas/ai-accounting.d.ts.map +1 -1
- package/dist/schemas/ai-limits.d.ts +2210 -0
- package/dist/schemas/ai-limits.d.ts.map +1 -0
- package/dist/schemas/auth.d.ts +195 -112
- package/dist/schemas/auth.d.ts.map +1 -1
- package/dist/schemas/datasets.d.ts +28 -22
- package/dist/schemas/datasets.d.ts.map +1 -1
- package/dist/schemas/events.d.ts +6 -6
- package/dist/schemas/files.d.ts +3 -0
- package/dist/schemas/files.d.ts.map +1 -1
- package/dist/schemas/logs.d.ts +6 -6
- package/dist/schemas/models.d.ts +214 -14
- package/dist/schemas/models.d.ts.map +1 -1
- package/dist/schemas/objects.d.ts +4 -4
- package/dist/schemas/pipelines.d.ts +42 -6
- package/dist/schemas/pipelines.d.ts.map +1 -1
- package/dist/schemas/rules.d.ts +6 -6
- package/dist/schemas/share-grants.d.ts +518 -0
- package/dist/schemas/share-grants.d.ts.map +1 -0
- package/dist/schemas/shared-access.d.ts +42 -0
- package/dist/schemas/shared-access.d.ts.map +1 -0
- package/dist/schemas/syncs.d.ts +7 -0
- package/dist/schemas/syncs.d.ts.map +1 -1
- package/dist/schemas/workflows.d.ts +200 -210
- package/dist/schemas/workflows.d.ts.map +1 -1
- package/dist/server.d.ts +5 -0
- package/dist/server.d.ts.map +1 -1
- package/dist/utils/http.d.ts +8 -4
- package/dist/utils/http.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/auth/access-token-boundary.ts +17 -2
- package/src/auth/public-routes.ts +23 -0
- package/src/auth/scope.ts +3 -2
- package/src/auth/shared-access.ts +376 -0
- package/src/files/content.ts +39 -6
- package/src/index.ts +1 -0
- package/src/models/display.ts +91 -0
- package/src/openapi/security.ts +20 -1
- package/src/openapi/tags.ts +4 -0
- package/src/registerRoutes.ts +6 -0
- package/src/routes/actions.ts +5 -10
- package/src/routes/agent-api-gateway.ts +137 -38
- package/src/routes/agents.ts +58 -83
- package/src/routes/ai-accounting.ts +414 -23
- package/src/routes/auth.ts +280 -1
- package/src/routes/models.ts +12 -4
- package/src/routes/share-grants.ts +238 -0
- package/src/routes/shared-access.ts +165 -0
- package/src/routes/workflows.ts +3 -5
- package/src/routes/ws/agents.ts +17 -6
- package/src/schemas/agents.ts +38 -50
- package/src/schemas/ai-accounting.ts +88 -19
- package/src/schemas/ai-limits.ts +116 -0
- package/src/schemas/auth.ts +32 -0
- package/src/schemas/datasets.ts +2 -1
- package/src/schemas/files.ts +1 -0
- package/src/schemas/models.ts +33 -5
- package/src/schemas/share-grants.ts +109 -0
- package/src/schemas/shared-access.ts +31 -0
- package/src/schemas/workflows.ts +0 -2
- package/src/server.ts +91 -1
- package/src/utils/http.ts +33 -2
|
@@ -2,17 +2,38 @@ import type { SixbHostView } from "@sixb/core"
|
|
|
2
2
|
import type {
|
|
3
3
|
AiAccountingAggregate,
|
|
4
4
|
AiAccountingOverview,
|
|
5
|
+
AiLimitPolicy,
|
|
6
|
+
AiLimitPolicyStatus,
|
|
5
7
|
AiModelCallAccountingItem,
|
|
8
|
+
AiModelCallCostRecord,
|
|
9
|
+
AiModelCallGroup,
|
|
6
10
|
} from "@sixb/core/storage"
|
|
11
|
+
import { AiLimitStorageError } from "@sixb/core/storage"
|
|
7
12
|
import type { Elysia } from "elysia"
|
|
13
|
+
import { bearerSecurityRequirement } from "../auth/access-token-boundary"
|
|
14
|
+
import type { RequestAuthState } from "../auth/scope"
|
|
15
|
+
import { requireRequestSixb } from "../auth/scope"
|
|
8
16
|
import { OPENAPI_TAGS } from "../openapi/tags"
|
|
9
17
|
import {
|
|
10
18
|
AiAccountingOverviewQuerySchema,
|
|
11
19
|
AiAccountingOverviewResponseSchema,
|
|
12
20
|
AiModelCallAccountingListQuerySchema,
|
|
13
21
|
AiModelCallAccountingListResponseSchema,
|
|
22
|
+
AiModelCallGroupsQuerySchema,
|
|
23
|
+
AiModelCallGroupsResponseSchema,
|
|
14
24
|
} from "../schemas/ai-accounting"
|
|
15
|
-
import {
|
|
25
|
+
import {
|
|
26
|
+
AiLimitListQuerySchema,
|
|
27
|
+
AiLimitPolicyListResponseSchema,
|
|
28
|
+
AiLimitPolicyParamsSchema,
|
|
29
|
+
AiLimitPolicySchema,
|
|
30
|
+
AiLimitPolicyStatusListResponseSchema,
|
|
31
|
+
AiLimitStatusQuerySchema,
|
|
32
|
+
AiLimitSubjectOptionsResponseSchema,
|
|
33
|
+
CreateAiLimitPolicyBodySchema,
|
|
34
|
+
UpdateAiLimitPolicyBodySchema,
|
|
35
|
+
} from "../schemas/ai-limits"
|
|
36
|
+
import { ErrorResponseSchema, SuccessResponseSchema } from "../schemas/common"
|
|
16
37
|
import { handleRouteError, parseOptionalInt, unconfiguredStorageResponse } from "../utils/http"
|
|
17
38
|
|
|
18
39
|
function serializeAggregate(aggregate: AiAccountingAggregate) {
|
|
@@ -42,7 +63,10 @@ function serializeOverview(overview: AiAccountingOverview) {
|
|
|
42
63
|
...serializeAggregate(model),
|
|
43
64
|
})),
|
|
44
65
|
agents: overview.agents.map((agent) => ({
|
|
45
|
-
|
|
66
|
+
kind: agent.kind,
|
|
67
|
+
...(agent.kind === "workflowAgent"
|
|
68
|
+
? { workflowId: agent.workflowId, agentStepId: agent.agentStepId }
|
|
69
|
+
: {}),
|
|
46
70
|
...serializeAggregate(agent),
|
|
47
71
|
})),
|
|
48
72
|
workflows: overview.workflows.map((workflow) => ({
|
|
@@ -52,6 +76,29 @@ function serializeOverview(overview: AiAccountingOverview) {
|
|
|
52
76
|
})
|
|
53
77
|
}
|
|
54
78
|
|
|
79
|
+
function serializeCost(cost: AiModelCallCostRecord) {
|
|
80
|
+
return {
|
|
81
|
+
...cost,
|
|
82
|
+
source:
|
|
83
|
+
cost.status !== "rated"
|
|
84
|
+
? "unknown"
|
|
85
|
+
: cost.priceSource.sourceId === "provider-reported"
|
|
86
|
+
? "provider"
|
|
87
|
+
: cost.priceSource.sourceId === "model-rate-card"
|
|
88
|
+
? "estimate"
|
|
89
|
+
: "unknown",
|
|
90
|
+
...(cost.priceSource === undefined
|
|
91
|
+
? {}
|
|
92
|
+
: {
|
|
93
|
+
priceSource: {
|
|
94
|
+
...cost.priceSource,
|
|
95
|
+
observedAt: cost.priceSource.observedAt.toISOString(),
|
|
96
|
+
},
|
|
97
|
+
}),
|
|
98
|
+
ratedAt: cost.ratedAt.toISOString(),
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
55
102
|
function serializeModelCall(item: AiModelCallAccountingItem) {
|
|
56
103
|
return AiModelCallAccountingListResponseSchema.shape.items.element.parse({
|
|
57
104
|
usage: {
|
|
@@ -61,6 +108,8 @@ function serializeModelCall(item: AiModelCallAccountingItem) {
|
|
|
61
108
|
callId: item.usage.callId,
|
|
62
109
|
providerId: item.usage.providerId,
|
|
63
110
|
requestedModelId: item.usage.requestedModelId,
|
|
111
|
+
providerIds: item.usage.providerIds ?? null,
|
|
112
|
+
requestedReasoning: item.usage.requestedReasoning,
|
|
64
113
|
responseModelId: item.usage.responseModelId,
|
|
65
114
|
responseId: item.usage.responseId,
|
|
66
115
|
usage: item.usage.usage,
|
|
@@ -68,31 +117,105 @@ function serializeModelCall(item: AiModelCallAccountingItem) {
|
|
|
68
117
|
recordedAt: item.usage.recordedAt.toISOString(),
|
|
69
118
|
},
|
|
70
119
|
attribution: item.attribution,
|
|
71
|
-
cost:
|
|
72
|
-
item.cost === undefined
|
|
73
|
-
? undefined
|
|
74
|
-
: {
|
|
75
|
-
...item.cost,
|
|
76
|
-
priceSource: {
|
|
77
|
-
...item.cost.priceSource,
|
|
78
|
-
observedAt: item.cost.priceSource.observedAt.toISOString(),
|
|
79
|
-
},
|
|
80
|
-
ratedAt: item.cost.ratedAt.toISOString(),
|
|
81
|
-
},
|
|
120
|
+
cost: item.cost === undefined ? undefined : serializeCost(item.cost),
|
|
82
121
|
valuationStatus: item.valuationStatus,
|
|
83
122
|
})
|
|
84
123
|
}
|
|
85
124
|
|
|
125
|
+
function serializeLimitPolicy(policy: AiLimitPolicy) {
|
|
126
|
+
return AiLimitPolicySchema.parse({
|
|
127
|
+
id: policy.id,
|
|
128
|
+
subject: policy.subject,
|
|
129
|
+
limit: policy.limit,
|
|
130
|
+
period: policy.period,
|
|
131
|
+
enabled: policy.enabled,
|
|
132
|
+
createdAt: policy.createdAt.toISOString(),
|
|
133
|
+
updatedAt: policy.updatedAt.toISOString(),
|
|
134
|
+
})
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function serializeLimitStatus(status: AiLimitPolicyStatus) {
|
|
138
|
+
return {
|
|
139
|
+
policy: serializeLimitPolicy(status.policy),
|
|
140
|
+
period: {
|
|
141
|
+
kind: status.period.kind,
|
|
142
|
+
start: status.period.start.toISOString(),
|
|
143
|
+
end: status.period.end.toISOString(),
|
|
144
|
+
resetAt: status.period.resetAt.toISOString(),
|
|
145
|
+
},
|
|
146
|
+
consumption: status.consumption,
|
|
147
|
+
accountingStatus: status.accountingStatus,
|
|
148
|
+
exhausted: status.exhausted,
|
|
149
|
+
orphaned: status.orphaned,
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function includeDisabled(value: "true" | "false" | undefined): boolean {
|
|
154
|
+
return value === "true"
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function handleAiLimitRouteError(error: unknown, set: { status?: number | string }) {
|
|
158
|
+
if (error instanceof AiLimitStorageError) {
|
|
159
|
+
if (error.code === "duplicate_policy") set.status = 409
|
|
160
|
+
else if (error.code === "missing_policy") set.status = 404
|
|
161
|
+
else set.status = 400
|
|
162
|
+
return { error: error.message }
|
|
163
|
+
}
|
|
164
|
+
return handleRouteError(error, set)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
async function serializeGroup(
|
|
168
|
+
group: AiModelCallGroup,
|
|
169
|
+
host: SixbHostView,
|
|
170
|
+
sixb: RequestAuthState["sixb"]
|
|
171
|
+
) {
|
|
172
|
+
// Accounting is project-wide; conversation titles and delegation keys remain private.
|
|
173
|
+
const thread =
|
|
174
|
+
group.attribution?.kind === "agent" && sixb
|
|
175
|
+
? await sixb.agent.threads.getById(group.attribution.threadId)
|
|
176
|
+
: null
|
|
177
|
+
const childIds = thread
|
|
178
|
+
? group.executions.flatMap((execution) =>
|
|
179
|
+
execution.attribution?.kind === "subagent" ? [execution.attribution.subagentRunId] : []
|
|
180
|
+
)
|
|
181
|
+
: []
|
|
182
|
+
const children =
|
|
183
|
+
childIds.length > 0
|
|
184
|
+
? ((await host.storage.agents?.runs.getByIds({ projectId: host.id, ids: childIds })) ?? [])
|
|
185
|
+
: []
|
|
186
|
+
const labels = new Map(
|
|
187
|
+
children.flatMap((run) =>
|
|
188
|
+
run.kind === "subagent" ? [[run.executionId, run.spawnKey] as const] : []
|
|
189
|
+
)
|
|
190
|
+
)
|
|
191
|
+
return {
|
|
192
|
+
...group,
|
|
193
|
+
label: thread?.title,
|
|
194
|
+
canOpenThread: thread !== null,
|
|
195
|
+
firstCallAt: group.firstCallAt.toISOString(),
|
|
196
|
+
lastCallAt: group.lastCallAt.toISOString(),
|
|
197
|
+
executions: group.executions.map((execution) => ({
|
|
198
|
+
...execution,
|
|
199
|
+
label: labels.get(execution.executionId),
|
|
200
|
+
firstCallAt: execution.firstCallAt.toISOString(),
|
|
201
|
+
lastCallAt: execution.lastCallAt.toISOString(),
|
|
202
|
+
})),
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
86
206
|
export function registerAiAccountingRoutes(app: Elysia, host: SixbHostView) {
|
|
87
207
|
app.get(
|
|
88
208
|
"/api/ai/accounting/overview",
|
|
89
|
-
async (
|
|
209
|
+
async (context) => {
|
|
210
|
+
const { query, set } = context
|
|
90
211
|
try {
|
|
212
|
+
const sixb = requireRequestSixb(context)
|
|
213
|
+
sixb.aiUsage.assertObservable()
|
|
91
214
|
const parsed = AiAccountingOverviewQuerySchema.parse(query)
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
215
|
+
if (!sixb.aiUsage.accountingConfigured) {
|
|
216
|
+
return unconfiguredStorageResponse(set, "AI cost storage")
|
|
217
|
+
}
|
|
218
|
+
const overview = await sixb.aiUsage.queryOverview({
|
|
96
219
|
from: new Date(parsed.from),
|
|
97
220
|
to: new Date(parsed.to),
|
|
98
221
|
bucket: parsed.bucket,
|
|
@@ -109,25 +232,30 @@ export function registerAiAccountingRoutes(app: Elysia, host: SixbHostView) {
|
|
|
109
232
|
response: {
|
|
110
233
|
200: AiAccountingOverviewResponseSchema,
|
|
111
234
|
400: ErrorResponseSchema,
|
|
235
|
+
403: ErrorResponseSchema,
|
|
112
236
|
501: ErrorResponseSchema,
|
|
113
237
|
},
|
|
114
238
|
detail: {
|
|
115
239
|
summary: "Get project AI usage and cost analytics",
|
|
116
240
|
tags: [OPENAPI_TAGS.aiAccounting.name],
|
|
117
241
|
operationId: "getAiAccountingOverview",
|
|
242
|
+
security: bearerSecurityRequirement("getAiAccountingOverview"),
|
|
118
243
|
},
|
|
119
244
|
}
|
|
120
245
|
)
|
|
121
246
|
|
|
122
247
|
app.get(
|
|
123
248
|
"/api/ai/model-calls",
|
|
124
|
-
async (
|
|
249
|
+
async (context) => {
|
|
250
|
+
const { query, set } = context
|
|
125
251
|
try {
|
|
252
|
+
const sixb = requireRequestSixb(context)
|
|
253
|
+
sixb.aiUsage.assertObservable()
|
|
126
254
|
const parsed = AiModelCallAccountingListQuerySchema.parse(query)
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
255
|
+
if (!sixb.aiUsage.accountingConfigured) {
|
|
256
|
+
return unconfiguredStorageResponse(set, "AI cost storage")
|
|
257
|
+
}
|
|
258
|
+
const result = await sixb.aiUsage.listModelCalls({
|
|
131
259
|
from: new Date(parsed.from),
|
|
132
260
|
to: new Date(parsed.to),
|
|
133
261
|
providerId: parsed.providerId,
|
|
@@ -152,12 +280,275 @@ export function registerAiAccountingRoutes(app: Elysia, host: SixbHostView) {
|
|
|
152
280
|
response: {
|
|
153
281
|
200: AiModelCallAccountingListResponseSchema,
|
|
154
282
|
400: ErrorResponseSchema,
|
|
283
|
+
403: ErrorResponseSchema,
|
|
155
284
|
501: ErrorResponseSchema,
|
|
156
285
|
},
|
|
157
286
|
detail: {
|
|
158
287
|
summary: "List project AI model-call accounting records",
|
|
159
288
|
tags: [OPENAPI_TAGS.aiAccounting.name],
|
|
160
289
|
operationId: "listAiModelCalls",
|
|
290
|
+
security: bearerSecurityRequirement("listAiModelCalls"),
|
|
291
|
+
},
|
|
292
|
+
}
|
|
293
|
+
)
|
|
294
|
+
|
|
295
|
+
app.get(
|
|
296
|
+
"/api/ai/limits",
|
|
297
|
+
async (context) => {
|
|
298
|
+
const { query, set } = context
|
|
299
|
+
try {
|
|
300
|
+
const sixb = requireRequestSixb(context)
|
|
301
|
+
if (!sixb.aiUsage.limitsConfigured) {
|
|
302
|
+
return unconfiguredStorageResponse(set, "AI limit storage")
|
|
303
|
+
}
|
|
304
|
+
const parsed = AiLimitListQuerySchema.parse(query)
|
|
305
|
+
const items = await sixb.aiUsage.listLimitPolicies({
|
|
306
|
+
includeDisabled: includeDisabled(parsed.includeDisabled),
|
|
307
|
+
})
|
|
308
|
+
return AiLimitPolicyListResponseSchema.parse({
|
|
309
|
+
items: items.map(serializeLimitPolicy),
|
|
310
|
+
capabilities: { manage: sixb.aiUsage.canManageLimits() },
|
|
311
|
+
})
|
|
312
|
+
} catch (error) {
|
|
313
|
+
return handleAiLimitRouteError(error, set)
|
|
314
|
+
}
|
|
315
|
+
},
|
|
316
|
+
{
|
|
317
|
+
query: AiLimitListQuerySchema,
|
|
318
|
+
response: {
|
|
319
|
+
200: AiLimitPolicyListResponseSchema,
|
|
320
|
+
400: ErrorResponseSchema,
|
|
321
|
+
403: ErrorResponseSchema,
|
|
322
|
+
501: ErrorResponseSchema,
|
|
323
|
+
},
|
|
324
|
+
detail: {
|
|
325
|
+
summary: "List project AI usage-limit policies",
|
|
326
|
+
tags: [OPENAPI_TAGS.aiAccounting.name],
|
|
327
|
+
operationId: "listAiLimitPolicies",
|
|
328
|
+
security: bearerSecurityRequirement("listAiLimitPolicies"),
|
|
329
|
+
},
|
|
330
|
+
}
|
|
331
|
+
)
|
|
332
|
+
|
|
333
|
+
app.get(
|
|
334
|
+
"/api/ai/limits/status",
|
|
335
|
+
async (context) => {
|
|
336
|
+
const { query, set } = context
|
|
337
|
+
try {
|
|
338
|
+
const sixb = requireRequestSixb(context)
|
|
339
|
+
sixb.aiUsage.assertObservable()
|
|
340
|
+
if (!sixb.aiUsage.limitsConfigured) {
|
|
341
|
+
return unconfiguredStorageResponse(set, "AI limit storage")
|
|
342
|
+
}
|
|
343
|
+
const parsed = AiLimitStatusQuerySchema.parse(query)
|
|
344
|
+
const items = await sixb.aiUsage.listLimitStatuses({
|
|
345
|
+
includeDisabled: includeDisabled(parsed.includeDisabled),
|
|
346
|
+
})
|
|
347
|
+
return AiLimitPolicyStatusListResponseSchema.parse({
|
|
348
|
+
items: items.map(serializeLimitStatus),
|
|
349
|
+
capabilities: { manage: sixb.aiUsage.canManageLimits() },
|
|
350
|
+
})
|
|
351
|
+
} catch (error) {
|
|
352
|
+
return handleAiLimitRouteError(error, set)
|
|
353
|
+
}
|
|
354
|
+
},
|
|
355
|
+
{
|
|
356
|
+
query: AiLimitStatusQuerySchema,
|
|
357
|
+
response: {
|
|
358
|
+
200: AiLimitPolicyStatusListResponseSchema,
|
|
359
|
+
400: ErrorResponseSchema,
|
|
360
|
+
403: ErrorResponseSchema,
|
|
361
|
+
501: ErrorResponseSchema,
|
|
362
|
+
},
|
|
363
|
+
detail: {
|
|
364
|
+
summary: "Get current project AI usage-limit status",
|
|
365
|
+
tags: [OPENAPI_TAGS.aiAccounting.name],
|
|
366
|
+
operationId: "getAiLimitStatus",
|
|
367
|
+
security: bearerSecurityRequirement("getAiLimitStatus"),
|
|
368
|
+
},
|
|
369
|
+
}
|
|
370
|
+
)
|
|
371
|
+
|
|
372
|
+
app.get(
|
|
373
|
+
"/api/ai/limits/subjects",
|
|
374
|
+
async (context) => {
|
|
375
|
+
const { set } = context
|
|
376
|
+
try {
|
|
377
|
+
const sixb = requireRequestSixb(context)
|
|
378
|
+
return AiLimitSubjectOptionsResponseSchema.parse(
|
|
379
|
+
await sixb.aiUsage.listLimitSubjectOptions()
|
|
380
|
+
)
|
|
381
|
+
} catch (error) {
|
|
382
|
+
return handleAiLimitRouteError(error, set)
|
|
383
|
+
}
|
|
384
|
+
},
|
|
385
|
+
{
|
|
386
|
+
response: {
|
|
387
|
+
200: AiLimitSubjectOptionsResponseSchema,
|
|
388
|
+
400: ErrorResponseSchema,
|
|
389
|
+
403: ErrorResponseSchema,
|
|
390
|
+
},
|
|
391
|
+
detail: {
|
|
392
|
+
summary: "Get selectable AI usage-limit subjects",
|
|
393
|
+
description:
|
|
394
|
+
"Lists registered groups and auth principals available for AI usage-limit policies.",
|
|
395
|
+
tags: [OPENAPI_TAGS.aiAccounting.name],
|
|
396
|
+
operationId: "getAiLimitSubjectOptions",
|
|
397
|
+
security: bearerSecurityRequirement("getAiLimitSubjectOptions"),
|
|
398
|
+
},
|
|
399
|
+
}
|
|
400
|
+
)
|
|
401
|
+
|
|
402
|
+
app.post(
|
|
403
|
+
"/api/ai/limits",
|
|
404
|
+
async (context) => {
|
|
405
|
+
const { body, set } = context
|
|
406
|
+
try {
|
|
407
|
+
const sixb = requireRequestSixb(context)
|
|
408
|
+
sixb.aiUsage.assertManageable()
|
|
409
|
+
if (!sixb.aiUsage.limitsConfigured) {
|
|
410
|
+
return unconfiguredStorageResponse(set, "AI limit storage")
|
|
411
|
+
}
|
|
412
|
+
const parsed = CreateAiLimitPolicyBodySchema.parse(body)
|
|
413
|
+
return serializeLimitPolicy(await sixb.aiUsage.createLimitPolicy(parsed))
|
|
414
|
+
} catch (error) {
|
|
415
|
+
return handleAiLimitRouteError(error, set)
|
|
416
|
+
}
|
|
417
|
+
},
|
|
418
|
+
{
|
|
419
|
+
body: CreateAiLimitPolicyBodySchema,
|
|
420
|
+
response: {
|
|
421
|
+
200: AiLimitPolicySchema,
|
|
422
|
+
400: ErrorResponseSchema,
|
|
423
|
+
403: ErrorResponseSchema,
|
|
424
|
+
409: ErrorResponseSchema,
|
|
425
|
+
501: ErrorResponseSchema,
|
|
426
|
+
},
|
|
427
|
+
detail: {
|
|
428
|
+
summary: "Create an AI usage-limit policy",
|
|
429
|
+
tags: [OPENAPI_TAGS.aiAccounting.name],
|
|
430
|
+
operationId: "createAiLimitPolicy",
|
|
431
|
+
security: bearerSecurityRequirement("createAiLimitPolicy"),
|
|
432
|
+
},
|
|
433
|
+
}
|
|
434
|
+
)
|
|
435
|
+
|
|
436
|
+
app.put(
|
|
437
|
+
"/api/ai/limits/:limitId",
|
|
438
|
+
async (context) => {
|
|
439
|
+
const { params, body, set } = context
|
|
440
|
+
try {
|
|
441
|
+
const sixb = requireRequestSixb(context)
|
|
442
|
+
sixb.aiUsage.assertManageable()
|
|
443
|
+
if (!sixb.aiUsage.limitsConfigured) {
|
|
444
|
+
return unconfiguredStorageResponse(set, "AI limit storage")
|
|
445
|
+
}
|
|
446
|
+
const { limitId } = AiLimitPolicyParamsSchema.parse(params)
|
|
447
|
+
const parsed = UpdateAiLimitPolicyBodySchema.parse(body)
|
|
448
|
+
return serializeLimitPolicy(
|
|
449
|
+
await sixb.aiUsage.updateLimitPolicy({ id: limitId, ...parsed })
|
|
450
|
+
)
|
|
451
|
+
} catch (error) {
|
|
452
|
+
return handleAiLimitRouteError(error, set)
|
|
453
|
+
}
|
|
454
|
+
},
|
|
455
|
+
{
|
|
456
|
+
params: AiLimitPolicyParamsSchema,
|
|
457
|
+
body: UpdateAiLimitPolicyBodySchema,
|
|
458
|
+
response: {
|
|
459
|
+
200: AiLimitPolicySchema,
|
|
460
|
+
400: ErrorResponseSchema,
|
|
461
|
+
403: ErrorResponseSchema,
|
|
462
|
+
404: ErrorResponseSchema,
|
|
463
|
+
501: ErrorResponseSchema,
|
|
464
|
+
},
|
|
465
|
+
detail: {
|
|
466
|
+
summary: "Update an AI usage-limit policy",
|
|
467
|
+
tags: [OPENAPI_TAGS.aiAccounting.name],
|
|
468
|
+
operationId: "updateAiLimitPolicy",
|
|
469
|
+
security: bearerSecurityRequirement("updateAiLimitPolicy"),
|
|
470
|
+
},
|
|
471
|
+
}
|
|
472
|
+
)
|
|
473
|
+
|
|
474
|
+
app.delete(
|
|
475
|
+
"/api/ai/limits/:limitId",
|
|
476
|
+
async (context) => {
|
|
477
|
+
const { params, set } = context
|
|
478
|
+
try {
|
|
479
|
+
const sixb = requireRequestSixb(context)
|
|
480
|
+
sixb.aiUsage.assertManageable()
|
|
481
|
+
if (!sixb.aiUsage.limitsConfigured) {
|
|
482
|
+
return unconfiguredStorageResponse(set, "AI limit storage")
|
|
483
|
+
}
|
|
484
|
+
const { limitId } = AiLimitPolicyParamsSchema.parse(params)
|
|
485
|
+
if (!(await sixb.aiUsage.deleteLimitPolicy(limitId))) {
|
|
486
|
+
set.status = 404
|
|
487
|
+
return { error: "AI usage-limit policy not found." }
|
|
488
|
+
}
|
|
489
|
+
return { success: true }
|
|
490
|
+
} catch (error) {
|
|
491
|
+
return handleAiLimitRouteError(error, set)
|
|
492
|
+
}
|
|
493
|
+
},
|
|
494
|
+
{
|
|
495
|
+
params: AiLimitPolicyParamsSchema,
|
|
496
|
+
response: {
|
|
497
|
+
200: SuccessResponseSchema,
|
|
498
|
+
400: ErrorResponseSchema,
|
|
499
|
+
403: ErrorResponseSchema,
|
|
500
|
+
404: ErrorResponseSchema,
|
|
501
|
+
501: ErrorResponseSchema,
|
|
502
|
+
},
|
|
503
|
+
detail: {
|
|
504
|
+
summary: "Delete an AI usage-limit policy",
|
|
505
|
+
tags: [OPENAPI_TAGS.aiAccounting.name],
|
|
506
|
+
operationId: "deleteAiLimitPolicy",
|
|
507
|
+
security: bearerSecurityRequirement("deleteAiLimitPolicy"),
|
|
508
|
+
},
|
|
509
|
+
}
|
|
510
|
+
)
|
|
511
|
+
|
|
512
|
+
app.get(
|
|
513
|
+
"/api/ai/model-call-groups",
|
|
514
|
+
async (context) => {
|
|
515
|
+
const { query, set } = context
|
|
516
|
+
try {
|
|
517
|
+
const parsed = AiModelCallGroupsQuerySchema.parse(query)
|
|
518
|
+
const sixb = requireRequestSixb(context)
|
|
519
|
+
sixb.aiUsage.assertObservable()
|
|
520
|
+
if (!sixb.aiUsage.accountingConfigured)
|
|
521
|
+
return unconfiguredStorageResponse(set, "AI cost storage")
|
|
522
|
+
const result = await sixb.aiUsage.listModelCallGroups({
|
|
523
|
+
from: new Date(parsed.from),
|
|
524
|
+
to: new Date(parsed.to),
|
|
525
|
+
providerId: parsed.providerId,
|
|
526
|
+
modelId: parsed.modelId,
|
|
527
|
+
valuationStatus: parsed.valuationStatus,
|
|
528
|
+
limit: parseOptionalInt(parsed.limit),
|
|
529
|
+
offset: parseOptionalInt(parsed.offset),
|
|
530
|
+
})
|
|
531
|
+
return AiModelCallGroupsResponseSchema.parse({
|
|
532
|
+
...result,
|
|
533
|
+
items: await Promise.all(result.items.map((group) => serializeGroup(group, host, sixb))),
|
|
534
|
+
})
|
|
535
|
+
} catch (error) {
|
|
536
|
+
return handleRouteError(error, set)
|
|
537
|
+
}
|
|
538
|
+
},
|
|
539
|
+
{
|
|
540
|
+
query: AiModelCallGroupsQuerySchema,
|
|
541
|
+
response: {
|
|
542
|
+
200: AiModelCallGroupsResponseSchema,
|
|
543
|
+
400: ErrorResponseSchema,
|
|
544
|
+
403: ErrorResponseSchema,
|
|
545
|
+
501: ErrorResponseSchema,
|
|
546
|
+
},
|
|
547
|
+
detail: {
|
|
548
|
+
summary: "List AI model calls grouped by initiating execution",
|
|
549
|
+
tags: [OPENAPI_TAGS.aiAccounting.name],
|
|
550
|
+
operationId: "listAiModelCallGroups",
|
|
551
|
+
security: bearerSecurityRequirement("listAiModelCallGroups"),
|
|
161
552
|
},
|
|
162
553
|
}
|
|
163
554
|
)
|