@sixb/server 0.1.6 → 0.1.7

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 (74) hide show
  1. package/dist/auth/public-routes.d.ts.map +1 -1
  2. package/dist/auth/scope.d.ts +6 -2
  3. package/dist/auth/scope.d.ts.map +1 -1
  4. package/dist/files/content.d.ts +8 -0
  5. package/dist/files/content.d.ts.map +1 -1
  6. package/dist/index.js +2492 -1575
  7. package/dist/index.js.map +22 -20
  8. package/dist/models/display.d.ts +22 -0
  9. package/dist/models/display.d.ts.map +1 -0
  10. package/dist/routes/action-runs.d.ts +2 -0
  11. package/dist/routes/action-runs.d.ts.map +1 -1
  12. package/dist/routes/actions.d.ts +1 -0
  13. package/dist/routes/actions.d.ts.map +1 -1
  14. package/dist/routes/agent-api-gateway.d.ts.map +1 -1
  15. package/dist/routes/agents.d.ts +99 -89
  16. package/dist/routes/agents.d.ts.map +1 -1
  17. package/dist/routes/ai-accounting.d.ts.map +1 -1
  18. package/dist/routes/auth.d.ts +134 -15
  19. package/dist/routes/auth.d.ts.map +1 -1
  20. package/dist/routes/events.d.ts +3 -3
  21. package/dist/routes/files.d.ts +1 -0
  22. package/dist/routes/files.d.ts.map +1 -1
  23. package/dist/routes/models.d.ts +18 -1
  24. package/dist/routes/models.d.ts.map +1 -1
  25. package/dist/routes/objects.d.ts +3 -1
  26. package/dist/routes/objects.d.ts.map +1 -1
  27. package/dist/routes/pipelines.d.ts +2 -1
  28. package/dist/routes/pipelines.d.ts.map +1 -1
  29. package/dist/routes/rules.d.ts +1 -1
  30. package/dist/routes/syncs.d.ts +1 -0
  31. package/dist/routes/syncs.d.ts.map +1 -1
  32. package/dist/routes/workflows.d.ts +57 -54
  33. package/dist/routes/workflows.d.ts.map +1 -1
  34. package/dist/routes/ws/logs.d.ts +4 -4
  35. package/dist/schemas/agents.d.ts +378 -215
  36. package/dist/schemas/agents.d.ts.map +1 -1
  37. package/dist/schemas/ai-accounting.d.ts +1847 -295
  38. package/dist/schemas/ai-accounting.d.ts.map +1 -1
  39. package/dist/schemas/ai-limits.d.ts +2210 -0
  40. package/dist/schemas/ai-limits.d.ts.map +1 -0
  41. package/dist/schemas/auth.d.ts +155 -72
  42. package/dist/schemas/auth.d.ts.map +1 -1
  43. package/dist/schemas/events.d.ts +6 -6
  44. package/dist/schemas/files.d.ts +3 -0
  45. package/dist/schemas/files.d.ts.map +1 -1
  46. package/dist/schemas/models.d.ts +214 -14
  47. package/dist/schemas/models.d.ts.map +1 -1
  48. package/dist/schemas/objects.d.ts +4 -4
  49. package/dist/schemas/pipelines.d.ts +6 -6
  50. package/dist/schemas/rules.d.ts +6 -6
  51. package/dist/schemas/workflows.d.ts +196 -206
  52. package/dist/schemas/workflows.d.ts.map +1 -1
  53. package/dist/utils/http.d.ts +8 -4
  54. package/dist/utils/http.d.ts.map +1 -1
  55. package/package.json +4 -4
  56. package/src/auth/public-routes.ts +18 -0
  57. package/src/auth/scope.ts +3 -2
  58. package/src/files/content.ts +39 -6
  59. package/src/models/display.ts +91 -0
  60. package/src/routes/agent-api-gateway.ts +137 -38
  61. package/src/routes/agents.ts +58 -83
  62. package/src/routes/ai-accounting.ts +414 -23
  63. package/src/routes/auth.ts +280 -1
  64. package/src/routes/models.ts +12 -4
  65. package/src/routes/workflows.ts +3 -5
  66. package/src/routes/ws/agents.ts +17 -6
  67. package/src/schemas/agents.ts +40 -49
  68. package/src/schemas/ai-accounting.ts +88 -19
  69. package/src/schemas/ai-limits.ts +116 -0
  70. package/src/schemas/auth.ts +32 -0
  71. package/src/schemas/files.ts +1 -0
  72. package/src/schemas/models.ts +33 -5
  73. package/src/schemas/workflows.ts +0 -2
  74. package/src/utils/http.ts +33 -2
@@ -1,8 +1,15 @@
1
1
  import { z } from "zod"
2
2
  import { AiUsageSummarySchema } from "./ai-usage"
3
+ import { ModelReasoningSchema } from "./models"
3
4
 
4
5
  const IntegerStringSchema = z.string().regex(/^\d+$/)
5
6
  const IsoDateSchema = z.string().datetime({ offset: true })
7
+ const CurrencySchema = z.string().regex(/^[A-Z]{3}$/)
8
+
9
+ export const AiMoneySchema = z.object({
10
+ currency: CurrencySchema,
11
+ amountNanos: IntegerStringSchema,
12
+ })
6
13
 
7
14
  export const AiAccountingBucketSchema = z.enum(["hour", "day", "week"])
8
15
  export const AiValuationStatusSchema = z.enum(["rated", "unpriceable", "unvalued"])
@@ -25,11 +32,6 @@ export const AiModelCallAccountingListQuerySchema = AiAccountingRangeQuerySchema
25
32
  offset: IntegerStringSchema.optional(),
26
33
  })
27
34
 
28
- export const AiMoneySchema = z.object({
29
- currency: z.string().regex(/^[A-Z]{3}$/),
30
- amountNanos: IntegerStringSchema,
31
- })
32
-
33
35
  export const AiCostSummarySchema = z.object({
34
36
  amounts: z.array(AiMoneySchema),
35
37
  ratedCallCount: z.number().int().nonnegative(),
@@ -60,9 +62,14 @@ export const AiAccountingOverviewResponseSchema = z.object({
60
62
  })
61
63
  ),
62
64
  agents: z.array(
63
- AiAccountingAggregateSchema.extend({
64
- agentId: z.string(),
65
- })
65
+ z.discriminatedUnion("kind", [
66
+ AiAccountingAggregateSchema.extend({ kind: z.literal("agent") }),
67
+ AiAccountingAggregateSchema.extend({
68
+ kind: z.literal("workflowAgent"),
69
+ workflowId: z.string(),
70
+ agentStepId: z.string(),
71
+ }),
72
+ ])
66
73
  ),
67
74
  workflows: z.array(
68
75
  AiAccountingAggregateSchema.extend({
@@ -77,6 +84,7 @@ export const AiPricingContextSchema = z.object({
77
84
  region: z.string().optional(),
78
85
  inferenceGeo: z.string().optional(),
79
86
  routedProviderId: z.string().optional(),
87
+ routedModelId: z.string().optional(),
80
88
  deploymentId: z.string().optional(),
81
89
  inferenceProfileId: z.string().optional(),
82
90
  cacheWriteTtlSeconds: z.number().int().positive().optional(),
@@ -89,6 +97,8 @@ const AiCostComponentSchema = z.object({
89
97
  "tokens.input.uncached",
90
98
  "tokens.input.cacheRead",
91
99
  "tokens.input.cacheWrite",
100
+ "tokens.input.cacheWrite5m",
101
+ "tokens.input.cacheWrite1h",
92
102
  "tokens.output.total",
93
103
  "tokens.output.text",
94
104
  "tokens.output.reasoning",
@@ -107,13 +117,36 @@ const AiPriceSourceSchema = z.object({
107
117
  sourceId: z.string(),
108
118
  sourceEntryId: z.string(),
109
119
  sourceVersion: z.string(),
110
- sourceUrl: z.string().url(),
120
+ sourceUrl: z.string().url().optional(),
111
121
  observedAt: IsoDateSchema,
112
122
  })
113
123
 
124
+ const AiCostSourceSchema = z.enum(["estimate", "provider", "unknown"])
125
+ const AiUnpriceableReasonSchema = z.enum([
126
+ "missingBillingIdentity",
127
+ "missingRateCard",
128
+ "missingUsageMeter",
129
+ "unsupportedPricingDimension",
130
+ "invalidUsageForFormula",
131
+ ])
132
+ const AiCostEstimateSchema = z.discriminatedUnion("status", [
133
+ z.object({
134
+ status: z.literal("rated"),
135
+ money: AiMoneySchema,
136
+ components: z.array(AiCostComponentSchema),
137
+ }),
138
+ z.object({
139
+ status: z.literal("unpriceable"),
140
+ reason: AiUnpriceableReasonSchema,
141
+ missingMeters: z.array(AiCostComponentSchema.shape.meter).optional(),
142
+ }),
143
+ ])
144
+
114
145
  const AiModelCallCostSchema = z.discriminatedUnion("status", [
115
146
  z.object({
116
147
  status: z.literal("rated"),
148
+ source: AiCostSourceSchema,
149
+ estimate: AiCostEstimateSchema.optional(),
117
150
  billingIdentity: AiBillingIdentitySchema,
118
151
  pricingContext: AiPricingContextSchema,
119
152
  priceSource: AiPriceSourceSchema,
@@ -123,16 +156,12 @@ const AiModelCallCostSchema = z.discriminatedUnion("status", [
123
156
  }),
124
157
  z.object({
125
158
  status: z.literal("unpriceable"),
159
+ source: AiCostSourceSchema,
160
+ estimate: AiCostEstimateSchema.optional(),
126
161
  billingIdentity: AiBillingIdentitySchema.optional(),
127
162
  pricingContext: AiPricingContextSchema,
128
- priceSource: AiPriceSourceSchema,
129
- reason: z.enum([
130
- "missingBillingIdentity",
131
- "missingCatalogEntry",
132
- "missingUsageMeter",
133
- "unsupportedPricingDimension",
134
- "invalidUsageForFormula",
135
- ]),
163
+ priceSource: AiPriceSourceSchema.optional(),
164
+ reason: AiUnpriceableReasonSchema,
136
165
  missingMeters: z.array(AiCostComponentSchema.shape.meter).optional(),
137
166
  ratedAt: IsoDateSchema,
138
167
  }),
@@ -145,8 +174,16 @@ const AiModelCallUsageRecordSchema = z.object({
145
174
  callId: z.string(),
146
175
  providerId: z.string(),
147
176
  requestedModelId: z.string(),
177
+ requestedReasoning: ModelReasoningSchema.optional(),
148
178
  responseModelId: z.string().optional(),
149
179
  responseId: z.string(),
180
+ providerIds: z
181
+ .object({
182
+ requestId: z.string().optional(),
183
+ responseId: z.string().optional(),
184
+ generationId: z.string().optional(),
185
+ })
186
+ .nullable(),
150
187
  usage: AiUsageSummarySchema,
151
188
  occurredAt: IsoDateSchema,
152
189
  recordedAt: IsoDateSchema,
@@ -158,17 +195,21 @@ export const AiModelCallAccountingItemSchema = z.object({
158
195
  .discriminatedUnion("kind", [
159
196
  z.object({
160
197
  kind: z.literal("agent"),
161
- agentId: z.string(),
162
198
  agentRunId: z.string(),
163
199
  threadId: z.string(),
164
200
  }),
165
201
  z.object({
166
202
  kind: z.literal("workflowAgent"),
167
- agentId: z.string(),
203
+ agentStepId: z.string(),
168
204
  nodeRunId: z.string(),
169
205
  workflowId: z.string(),
170
206
  workflowRunId: z.string(),
171
207
  }),
208
+ z.object({
209
+ kind: z.literal("subagent"),
210
+ subagentRunId: z.string(),
211
+ parentRunId: z.string(),
212
+ }),
172
213
  ])
173
214
  .optional(),
174
215
  cost: AiModelCallCostSchema.optional(),
@@ -180,3 +221,31 @@ export const AiModelCallAccountingListResponseSchema = z.object({
180
221
  hasMore: z.boolean(),
181
222
  total: z.number().int().nonnegative(),
182
223
  })
224
+
225
+ export const AiModelCallGroupsQuerySchema = AiModelCallAccountingListQuerySchema.omit({
226
+ executionId: true,
227
+ })
228
+
229
+ const AiModelCallExecutionSummarySchema = z.object({
230
+ executionId: z.string(),
231
+ attribution: AiModelCallAccountingItemSchema.shape.attribution,
232
+ modelCallCount: z.number().int().nonnegative(),
233
+ totalTokens: z.number().int().nonnegative().optional(),
234
+ costs: AiCostSummarySchema,
235
+ firstCallAt: IsoDateSchema,
236
+ lastCallAt: IsoDateSchema,
237
+ models: z.array(AiBillingIdentitySchema),
238
+ /** Present only when the caller can read the initiating conversation. */
239
+ label: z.string().optional(),
240
+ })
241
+
242
+ export const AiModelCallGroupsResponseSchema = z.object({
243
+ items: z.array(
244
+ AiModelCallExecutionSummarySchema.omit({ models: true }).extend({
245
+ executions: z.array(AiModelCallExecutionSummarySchema),
246
+ canOpenThread: z.boolean(),
247
+ })
248
+ ),
249
+ total: z.number().int().nonnegative(),
250
+ hasMore: z.boolean(),
251
+ })
@@ -0,0 +1,116 @@
1
+ import { z } from "zod"
2
+ import { AiMoneySchema } from "./ai-accounting"
3
+
4
+ const IsoDateSchema = z.string().datetime({ offset: true })
5
+
6
+ export const AiLimitSubjectSchema = z.discriminatedUnion("type", [
7
+ z.object({ type: z.literal("project") }),
8
+ z.object({ type: z.literal("group"), id: z.string().trim().min(1) }),
9
+ z.object({ type: z.literal("user"), id: z.string().trim().min(1) }),
10
+ z.object({ type: z.literal("serviceAccount"), id: z.string().trim().min(1) }),
11
+ ])
12
+
13
+ export const AiLimitQuantitySchema = z.discriminatedUnion("meter", [
14
+ z.object({
15
+ meter: z.literal("tokens.total"),
16
+ amount: z.number().int().nonnegative().safe(),
17
+ }),
18
+ z.object({
19
+ meter: z.literal("cost.catalogEstimated"),
20
+ amount: AiMoneySchema.extend({ currency: z.literal("USD") }),
21
+ }),
22
+ ])
23
+
24
+ export const AiLimitPolicySchema = z.object({
25
+ id: z.string(),
26
+ subject: AiLimitSubjectSchema,
27
+ limit: AiLimitQuantitySchema,
28
+ period: z.literal("calendarMonth"),
29
+ enabled: z.boolean(),
30
+ createdAt: IsoDateSchema,
31
+ updatedAt: IsoDateSchema,
32
+ })
33
+
34
+ export const AiLimitConsumptionSchema = z.object({
35
+ actual: AiLimitQuantitySchema,
36
+ reserved: AiLimitQuantitySchema,
37
+ unknown: AiLimitQuantitySchema,
38
+ remaining: AiLimitQuantitySchema,
39
+ })
40
+
41
+ export const AiLimitPolicyStatusSchema = z.object({
42
+ policy: AiLimitPolicySchema,
43
+ period: z.object({
44
+ kind: z.literal("calendarMonth"),
45
+ start: IsoDateSchema,
46
+ end: IsoDateSchema,
47
+ resetAt: IsoDateSchema,
48
+ }),
49
+ consumption: AiLimitConsumptionSchema,
50
+ accountingStatus: z.enum(["complete", "unavailable"]),
51
+ exhausted: z.boolean(),
52
+ orphaned: z.boolean(),
53
+ })
54
+
55
+ export const AiLimitCapabilitiesSchema = z.object({ manage: z.boolean() })
56
+
57
+ export const AiLimitSubjectOptionsResponseSchema = z.object({
58
+ groups: z.array(
59
+ z.object({
60
+ id: z.string(),
61
+ label: z.string().optional(),
62
+ description: z.string().optional(),
63
+ })
64
+ ),
65
+ users: z.array(
66
+ z.object({
67
+ id: z.string(),
68
+ email: z.string(),
69
+ displayName: z.string().optional(),
70
+ status: z.enum(["active", "suspended"]),
71
+ })
72
+ ),
73
+ serviceAccounts: z.array(
74
+ z.object({
75
+ id: z.string(),
76
+ name: z.string(),
77
+ description: z.string().optional(),
78
+ status: z.enum(["active", "suspended"]),
79
+ })
80
+ ),
81
+ })
82
+
83
+ export const AiLimitListQuerySchema = z.object({
84
+ includeDisabled: z.enum(["true", "false"]).optional(),
85
+ })
86
+
87
+ export const AiLimitStatusQuerySchema = AiLimitListQuerySchema
88
+
89
+ export const AiLimitPolicyListResponseSchema = z.object({
90
+ items: z.array(AiLimitPolicySchema),
91
+ capabilities: AiLimitCapabilitiesSchema,
92
+ })
93
+
94
+ export const AiLimitPolicyStatusListResponseSchema = z.object({
95
+ items: z.array(AiLimitPolicyStatusSchema),
96
+ capabilities: AiLimitCapabilitiesSchema,
97
+ })
98
+
99
+ export const CreateAiLimitPolicyBodySchema = z.object({
100
+ subject: AiLimitSubjectSchema,
101
+ limit: AiLimitQuantitySchema,
102
+ enabled: z.boolean().optional(),
103
+ })
104
+
105
+ export const UpdateAiLimitPolicyBodySchema = z
106
+ .object({
107
+ limit: AiLimitQuantitySchema.optional(),
108
+ enabled: z.boolean().optional(),
109
+ })
110
+ .refine((value) => value.limit !== undefined || value.enabled !== undefined, {
111
+ message: "An AI limit update must change limit or enabled.",
112
+ })
113
+
114
+ export const AiLimitPolicyParamsSchema = z.object({
115
+ limitId: z.string().trim().min(1),
116
+ })
@@ -295,6 +295,38 @@ export const AuthSignOutResponseSchema = z.object({
295
295
  success: z.boolean(),
296
296
  })
297
297
 
298
+ export const CreateDeviceAuthorizationBodySchema = z.object({
299
+ clientName: z.string().trim().min(1).max(100),
300
+ tokenName: z.string().trim().min(1).max(100),
301
+ expiresIn: z.literal("90d").optional(),
302
+ })
303
+
304
+ export const CreateDeviceAuthorizationResponseSchema = z.object({
305
+ deviceCode: z.string(),
306
+ userCode: z.string(),
307
+ verificationUri: z.string(),
308
+ verificationUriComplete: z.string(),
309
+ expiresAt: z.string(),
310
+ interval: z.number(),
311
+ })
312
+
313
+ export const ExchangeDeviceAuthorizationBodySchema = z.object({
314
+ deviceCode: z.string().min(1),
315
+ })
316
+
317
+ export const ExchangeDeviceAuthorizationResponseSchema = z.union([
318
+ z.object({ status: z.literal("pending") }),
319
+ z.object({ status: z.literal("denied") }),
320
+ z.object({ status: z.literal("expired") }),
321
+ z.object({ status: z.literal("approved"), accessToken: z.string() }),
322
+ ])
323
+
324
+ export const DeviceAuthorizationDecisionBodySchema = z.object({
325
+ userCode: z.string().trim().min(1),
326
+ decision: z.enum(["approve", "deny"]),
327
+ csrfToken: z.string().min(1),
328
+ })
329
+
298
330
  export const AuthSessionSummarySchema = z.object({
299
331
  id: z.string(),
300
332
  audience: z.enum(["atlas", "app"]),
@@ -79,4 +79,5 @@ export const CompleteFileUploadBodySchema = z.object({
79
79
  export const FileContentQuerySchema = z.object({
80
80
  path: z.string().min(1),
81
81
  disposition: z.enum(["inline", "attachment"]).optional(),
82
+ v: z.string().optional().describe("Client cache key; the route still resolves the current file"),
82
83
  })
@@ -1,15 +1,43 @@
1
+ import { MODEL_REASONING_LEVELS } from "@sixb/core/models"
1
2
  import { z } from "zod"
2
3
 
3
- export const LanguageModelSchema = z.object({
4
+ export const LanguageModelRefSchema = z.object({
5
+ provider: z.string().trim().min(1),
6
+ modelId: z.string().trim().min(1),
7
+ })
8
+
9
+ export const LanguageModelSchema = LanguageModelRefSchema.extend({
4
10
  /**
5
- * The AI SDK binding and model id the project configured. Together they identify the entry;
6
- * the runtime's internal reference is not part of this contract.
11
+ * The provider binding and model id the project configured. Together they identify the entry;
12
+ * there is no separate Sixb model id.
7
13
  */
8
- provider: z.string(),
9
- modelId: z.string(),
10
14
  isDefault: z.boolean(),
15
+ name: z.string().min(1),
16
+ description: z.string().min(1).optional(),
17
+ publisher: z.object({
18
+ id: z.string().min(1),
19
+ name: z.string().min(1),
20
+ }),
21
+ via: z.string().min(1).optional(),
22
+ capabilities: z.object({
23
+ input: z.array(z.enum(["text", "image", "audio", "video", "pdf"])),
24
+ output: z.array(z.enum(["text", "image", "audio", "video", "pdf"])),
25
+ attachments: z.boolean().optional(),
26
+ reasoning: z.boolean().optional(),
27
+ tools: z.boolean().optional(),
28
+ structuredOutput: z.boolean().optional(),
29
+ contextWindowTokens: z.number().int().positive().optional(),
30
+ }),
31
+ reasoningLevels: z.array(z.enum(MODEL_REASONING_LEVELS)),
11
32
  })
12
33
 
13
34
  export const ModelCatalogSchema = z.object({
14
35
  language: z.array(LanguageModelSchema),
15
36
  })
37
+
38
+ export const ModelReasoningLevelSchema = z.enum(MODEL_REASONING_LEVELS)
39
+
40
+ export const ModelReasoningSchema = z.union([
41
+ ModelReasoningLevelSchema,
42
+ z.object({ budgetTokens: z.number().int().nonnegative() }).strict(),
43
+ ])
@@ -119,7 +119,6 @@ const WorkflowNodeSchema = z.union([
119
119
  type: z.literal("agent"),
120
120
  id: z.string(),
121
121
  key: z.string(),
122
- agentId: z.string(),
123
122
  input: WorkflowIOSnapshotSchema,
124
123
  output: WorkflowIOSnapshotSchema,
125
124
  }),
@@ -143,7 +142,6 @@ export const WorkflowRunDetailSchema = WorkflowRunSummarySchema.extend({
143
142
  })
144
143
 
145
144
  export const WorkflowAgentNodeExecutionSummarySchema = z.object({
146
- agentId: z.string(),
147
145
  status: z.enum(["queued", "running", "succeeded", "failed", "cancelled"]),
148
146
  attempt: z.number().int().nonnegative(),
149
147
  modelId: z.string().optional(),
package/src/utils/http.ts CHANGED
@@ -2,6 +2,7 @@ import {
2
2
  AuthorizationError,
3
3
  OntologyNotFoundError,
4
4
  OntologyValidationError,
5
+ type ReadonlyJsonValue,
5
6
  type SixbErrorCode,
6
7
  } from "@sixb/core"
7
8
  import { isSixbError } from "@sixb/core/internal/errors"
@@ -28,6 +29,13 @@ const HTTP_STATUS_BY_ERROR_CODE: Partial<Record<SixbErrorCode, number>> = {
28
29
  "connector.revocation_pending": 409,
29
30
  "dataset.not_found": 404,
30
31
  "dataset.version_not_found": 404,
32
+ "ai.usage_limit_exceeded": 429,
33
+ "ai.usage_limit_unavailable": 429,
34
+ }
35
+
36
+ interface RouteResponseSet {
37
+ status?: number | string
38
+ headers?: unknown
31
39
  }
32
40
 
33
41
  export function toIsoString(value: Date): string {
@@ -79,14 +87,22 @@ export function unconfiguredStorageResponse(
79
87
 
80
88
  export function handleRouteError(
81
89
  error: unknown,
82
- set: { status?: number | string }
90
+ set: RouteResponseSet
83
91
  ): {
84
92
  error: string
85
93
  code?: SixbErrorCode
94
+ details?: ReadonlyJsonValue
86
95
  } {
87
96
  if (isSixbError(error)) {
88
97
  set.status = HTTP_STATUS_BY_ERROR_CODE[error.code] ?? 500
89
- return { error: error.message, code: error.code }
98
+ if (error.code === "ai.usage_limit_exceeded") setRetryAfterFromDetails(set, error.details)
99
+ const exposesDetails =
100
+ error.code === "ai.usage_limit_exceeded" || error.code === "ai.usage_limit_unavailable"
101
+ return {
102
+ error: error.message,
103
+ code: error.code,
104
+ ...(!exposesDetails || error.details === undefined ? {} : { details: error.details }),
105
+ }
90
106
  }
91
107
 
92
108
  if (error instanceof AuthorizationError) {
@@ -123,6 +139,21 @@ export function handleRouteError(
123
139
  return { error: message }
124
140
  }
125
141
 
142
+ function setRetryAfterFromDetails(set: RouteResponseSet, details: ReadonlyJsonValue | undefined) {
143
+ if (typeof details !== "object" || details === null || Array.isArray(details)) return
144
+ const resetAt = (details as Readonly<Record<string, ReadonlyJsonValue>>).resetAt
145
+ if (typeof resetAt !== "string") return
146
+ const resetTime = Date.parse(resetAt)
147
+ if (!Number.isFinite(resetTime)) return
148
+ const seconds = String(Math.max(0, Math.ceil((resetTime - Date.now()) / 1_000)))
149
+ const headers =
150
+ typeof set.headers === "object" && set.headers !== null
151
+ ? (set.headers as Record<string, unknown>)
152
+ : {}
153
+ headers["Retry-After"] = seconds
154
+ set.headers = headers
155
+ }
156
+
126
157
  function fileUploadSessionErrorStatus(reason: FileUploadSessionErrorReason): number {
127
158
  switch (reason) {
128
159
  case "not_found":