@agenticrun/sdk 0.1.0-alpha.0
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/README.md +60 -0
- package/dist/core-BxLvIHye.mjs +1258 -0
- package/dist/index-brUCQYsX.d.mts +837 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.mjs +2 -0
- package/dist/langchain.d.mts +17 -0
- package/dist/langchain.mjs +466 -0
- package/package.json +36 -0
|
@@ -0,0 +1,1258 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
//#region src/core/apiEndpoint.ts
|
|
4
|
+
const defaultApiUrl = "https://api.agenticrun.de/v1";
|
|
5
|
+
const createApiEndpoint = (apiUrl, path) => {
|
|
6
|
+
let endpoint;
|
|
7
|
+
try {
|
|
8
|
+
endpoint = new URL(apiUrl.trim());
|
|
9
|
+
} catch (cause) {
|
|
10
|
+
throw new TypeError("Agentic Run API URL is invalid.", { cause });
|
|
11
|
+
}
|
|
12
|
+
if (endpoint.protocol !== "http:" && endpoint.protocol !== "https:") throw new TypeError("Agentic Run API URL must use HTTP or HTTPS.");
|
|
13
|
+
if (endpoint.search || endpoint.hash) throw new TypeError("Agentic Run API URL must not contain a query or hash.");
|
|
14
|
+
endpoint.pathname = `${endpoint.pathname.replace(/\/+$/, "")}/${path.replace(/^\/+/, "")}`;
|
|
15
|
+
return endpoint.href;
|
|
16
|
+
};
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region ../contracts/src/apiError.ts
|
|
19
|
+
const apiErrorResponseSchema = z.object({ error: z.object({
|
|
20
|
+
code: z.string(),
|
|
21
|
+
message: z.string().optional()
|
|
22
|
+
}).readonly() }).readonly();
|
|
23
|
+
//#endregion
|
|
24
|
+
//#region ../contracts/src/tenantChanges.ts
|
|
25
|
+
const tenantChangeEventSchemas = [
|
|
26
|
+
z.object({
|
|
27
|
+
id: z.uuid(),
|
|
28
|
+
resourceId: z.uuid(),
|
|
29
|
+
resource: z.literal("workspace-key"),
|
|
30
|
+
action: z.enum([
|
|
31
|
+
"created",
|
|
32
|
+
"updated",
|
|
33
|
+
"deleted"
|
|
34
|
+
])
|
|
35
|
+
}),
|
|
36
|
+
z.object({
|
|
37
|
+
id: z.uuid(),
|
|
38
|
+
resourceId: z.uuid(),
|
|
39
|
+
resource: z.literal("provider-key"),
|
|
40
|
+
action: z.enum([
|
|
41
|
+
"created",
|
|
42
|
+
"updated",
|
|
43
|
+
"deleted"
|
|
44
|
+
])
|
|
45
|
+
}),
|
|
46
|
+
z.object({
|
|
47
|
+
id: z.uuid(),
|
|
48
|
+
resourceId: z.uuid(),
|
|
49
|
+
resource: z.literal("tracing-session"),
|
|
50
|
+
action: z.enum(["created", "updated"])
|
|
51
|
+
}),
|
|
52
|
+
z.object({
|
|
53
|
+
id: z.uuid(),
|
|
54
|
+
resourceId: z.uuid(),
|
|
55
|
+
resource: z.literal("agent"),
|
|
56
|
+
action: z.enum(["created", "updated"])
|
|
57
|
+
})
|
|
58
|
+
];
|
|
59
|
+
z.discriminatedUnion("resource", tenantChangeEventSchemas).readonly();
|
|
60
|
+
z.object({
|
|
61
|
+
apiUrl: z.url(),
|
|
62
|
+
logtoAppId: z.string().min(1),
|
|
63
|
+
logtoEndpoint: z.url(),
|
|
64
|
+
logtoPostSignOutRedirectUri: z.url(),
|
|
65
|
+
logtoRedirectUri: z.url()
|
|
66
|
+
});
|
|
67
|
+
//#endregion
|
|
68
|
+
//#region ../modules/src/credentials/providers.contract.ts
|
|
69
|
+
const tokenPriceSchema = z.string().regex(/^(0|[1-9]\d*)(\.\d+)?$/).nullable();
|
|
70
|
+
z.object({
|
|
71
|
+
currency: z.string().regex(/^[A-Z]{3}$/),
|
|
72
|
+
unit: z.literal("million_tokens"),
|
|
73
|
+
rates: z.object({
|
|
74
|
+
input: tokenPriceSchema,
|
|
75
|
+
cacheRead: tokenPriceSchema,
|
|
76
|
+
cacheWrite: tokenPriceSchema,
|
|
77
|
+
output: tokenPriceSchema,
|
|
78
|
+
reasoning: tokenPriceSchema
|
|
79
|
+
}).strict().readonly()
|
|
80
|
+
}).strict().readonly();
|
|
81
|
+
const providerCatalog = [
|
|
82
|
+
{
|
|
83
|
+
id: "openai",
|
|
84
|
+
name: "OpenAI",
|
|
85
|
+
active: true,
|
|
86
|
+
adapter: "openai",
|
|
87
|
+
keyHintLength: 7,
|
|
88
|
+
models: [
|
|
89
|
+
{
|
|
90
|
+
id: "gpt-5.6-sol",
|
|
91
|
+
name: "GPT-5.6 Sol",
|
|
92
|
+
active: true,
|
|
93
|
+
pricing: {
|
|
94
|
+
currency: "USD",
|
|
95
|
+
unit: "million_tokens",
|
|
96
|
+
rates: {
|
|
97
|
+
input: "4.00",
|
|
98
|
+
cacheRead: "0.40",
|
|
99
|
+
cacheWrite: "5.00",
|
|
100
|
+
output: "20.00",
|
|
101
|
+
reasoning: "20.00"
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
id: "gpt-5.6-terra",
|
|
107
|
+
name: "GPT-5.6 Terra",
|
|
108
|
+
active: true,
|
|
109
|
+
pricing: {
|
|
110
|
+
currency: "USD",
|
|
111
|
+
unit: "million_tokens",
|
|
112
|
+
rates: {
|
|
113
|
+
input: "2.00",
|
|
114
|
+
cacheRead: "0.20",
|
|
115
|
+
cacheWrite: "2.50",
|
|
116
|
+
output: "12.00",
|
|
117
|
+
reasoning: "12.00"
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
id: "gpt-5.6-luna",
|
|
123
|
+
name: "GPT-5.6 Luna",
|
|
124
|
+
active: true,
|
|
125
|
+
pricing: {
|
|
126
|
+
currency: "USD",
|
|
127
|
+
unit: "million_tokens",
|
|
128
|
+
rates: {
|
|
129
|
+
input: "0.20",
|
|
130
|
+
cacheRead: "0.02",
|
|
131
|
+
cacheWrite: "0.25",
|
|
132
|
+
output: "1.20",
|
|
133
|
+
reasoning: "1.20"
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
]
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
id: "anthropic",
|
|
141
|
+
name: "Anthropic",
|
|
142
|
+
active: true,
|
|
143
|
+
adapter: "anthropic",
|
|
144
|
+
keyHintLength: 14,
|
|
145
|
+
models: [
|
|
146
|
+
{
|
|
147
|
+
id: "claude-fable-5",
|
|
148
|
+
name: "Claude Fable 5",
|
|
149
|
+
active: true,
|
|
150
|
+
pricing: {
|
|
151
|
+
currency: "USD",
|
|
152
|
+
unit: "million_tokens",
|
|
153
|
+
rates: {
|
|
154
|
+
input: "10",
|
|
155
|
+
cacheRead: "1",
|
|
156
|
+
cacheWrite: null,
|
|
157
|
+
output: "50",
|
|
158
|
+
reasoning: "50"
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
id: "claude-opus-5",
|
|
164
|
+
name: "Claude Opus 5",
|
|
165
|
+
active: true,
|
|
166
|
+
pricing: {
|
|
167
|
+
currency: "USD",
|
|
168
|
+
unit: "million_tokens",
|
|
169
|
+
rates: {
|
|
170
|
+
input: "5",
|
|
171
|
+
cacheRead: "0.50",
|
|
172
|
+
cacheWrite: null,
|
|
173
|
+
output: "25",
|
|
174
|
+
reasoning: "25"
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
},
|
|
178
|
+
{
|
|
179
|
+
id: "claude-sonnet-5",
|
|
180
|
+
name: "Claude Sonnet 5",
|
|
181
|
+
active: true,
|
|
182
|
+
pricing: {
|
|
183
|
+
currency: "USD",
|
|
184
|
+
unit: "million_tokens",
|
|
185
|
+
rates: {
|
|
186
|
+
input: "2",
|
|
187
|
+
cacheRead: "0.20",
|
|
188
|
+
cacheWrite: null,
|
|
189
|
+
output: "10",
|
|
190
|
+
reasoning: "10"
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
id: "claude-haiku-4-5-20251001",
|
|
196
|
+
name: "Claude Haiku 4.5",
|
|
197
|
+
active: true,
|
|
198
|
+
pricing: {
|
|
199
|
+
currency: "USD",
|
|
200
|
+
unit: "million_tokens",
|
|
201
|
+
rates: {
|
|
202
|
+
input: "1",
|
|
203
|
+
cacheRead: "0.10",
|
|
204
|
+
cacheWrite: null,
|
|
205
|
+
output: "5",
|
|
206
|
+
reasoning: "5"
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
]
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
id: "google",
|
|
214
|
+
name: "Google",
|
|
215
|
+
active: true,
|
|
216
|
+
adapter: "google",
|
|
217
|
+
keyHintLength: 8,
|
|
218
|
+
models: [
|
|
219
|
+
{
|
|
220
|
+
id: "gemini-3.7-flash",
|
|
221
|
+
name: "Gemini 3.7 Flash",
|
|
222
|
+
active: true,
|
|
223
|
+
pricing: {
|
|
224
|
+
currency: "USD",
|
|
225
|
+
unit: "million_tokens",
|
|
226
|
+
rates: {
|
|
227
|
+
input: "0.75",
|
|
228
|
+
cacheRead: "0.075",
|
|
229
|
+
cacheWrite: null,
|
|
230
|
+
output: "3.75",
|
|
231
|
+
reasoning: "3.75"
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
},
|
|
235
|
+
{
|
|
236
|
+
id: "gemini-3.6-flash",
|
|
237
|
+
name: "Gemini 3.6 Flash",
|
|
238
|
+
active: true,
|
|
239
|
+
pricing: {
|
|
240
|
+
currency: "USD",
|
|
241
|
+
unit: "million_tokens",
|
|
242
|
+
rates: {
|
|
243
|
+
input: "0.75",
|
|
244
|
+
cacheRead: "0.075",
|
|
245
|
+
cacheWrite: null,
|
|
246
|
+
output: "3.75",
|
|
247
|
+
reasoning: "3.75"
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
},
|
|
251
|
+
{
|
|
252
|
+
id: "gemini-3.5-flash",
|
|
253
|
+
name: "Gemini 3.5 Flash",
|
|
254
|
+
active: true,
|
|
255
|
+
pricing: {
|
|
256
|
+
currency: "USD",
|
|
257
|
+
unit: "million_tokens",
|
|
258
|
+
rates: {
|
|
259
|
+
input: "1.50",
|
|
260
|
+
cacheRead: "0.15",
|
|
261
|
+
cacheWrite: null,
|
|
262
|
+
output: "9.00",
|
|
263
|
+
reasoning: "9.00"
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
},
|
|
267
|
+
{
|
|
268
|
+
id: "gemini-3.5-flash-lite",
|
|
269
|
+
name: "Gemini 3.5 Flash-Lite",
|
|
270
|
+
active: true,
|
|
271
|
+
pricing: {
|
|
272
|
+
currency: "USD",
|
|
273
|
+
unit: "million_tokens",
|
|
274
|
+
rates: {
|
|
275
|
+
input: "0.30",
|
|
276
|
+
cacheRead: "0.03",
|
|
277
|
+
cacheWrite: null,
|
|
278
|
+
output: "2.50",
|
|
279
|
+
reasoning: "2.50"
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
},
|
|
283
|
+
{
|
|
284
|
+
id: "gemini-3.1-flash-lite",
|
|
285
|
+
name: "Gemini 3.1 Flash-Lite",
|
|
286
|
+
active: true,
|
|
287
|
+
pricing: {
|
|
288
|
+
currency: "USD",
|
|
289
|
+
unit: "million_tokens",
|
|
290
|
+
rates: {
|
|
291
|
+
input: null,
|
|
292
|
+
cacheRead: null,
|
|
293
|
+
cacheWrite: null,
|
|
294
|
+
output: "1.50",
|
|
295
|
+
reasoning: "1.50"
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
]
|
|
300
|
+
},
|
|
301
|
+
{
|
|
302
|
+
id: "scaleway",
|
|
303
|
+
name: "Scaleway",
|
|
304
|
+
active: true,
|
|
305
|
+
adapter: "openai-compatible",
|
|
306
|
+
baseUrl: "https://api.scaleway.ai/v1",
|
|
307
|
+
keyHintLength: 4,
|
|
308
|
+
models: [
|
|
309
|
+
{
|
|
310
|
+
id: "glm-5.2",
|
|
311
|
+
name: "GLM 5.2",
|
|
312
|
+
active: true,
|
|
313
|
+
pricing: {
|
|
314
|
+
currency: "EUR",
|
|
315
|
+
unit: "million_tokens",
|
|
316
|
+
rates: {
|
|
317
|
+
input: "1.80",
|
|
318
|
+
cacheRead: null,
|
|
319
|
+
cacheWrite: null,
|
|
320
|
+
output: "5.50",
|
|
321
|
+
reasoning: null
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
},
|
|
325
|
+
{
|
|
326
|
+
id: "deepseek-v4-flash-0731",
|
|
327
|
+
name: "DeepSeek V4 Flash 0731",
|
|
328
|
+
active: true,
|
|
329
|
+
pricing: {
|
|
330
|
+
currency: "EUR",
|
|
331
|
+
unit: "million_tokens",
|
|
332
|
+
rates: {
|
|
333
|
+
input: "0.40",
|
|
334
|
+
cacheRead: "0.08",
|
|
335
|
+
cacheWrite: null,
|
|
336
|
+
output: "0.80",
|
|
337
|
+
reasoning: null
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
},
|
|
341
|
+
{
|
|
342
|
+
id: "qwen3.6-35b-a3b",
|
|
343
|
+
name: "Qwen 3.6 35B A3B",
|
|
344
|
+
active: true,
|
|
345
|
+
pricing: {
|
|
346
|
+
currency: "EUR",
|
|
347
|
+
unit: "million_tokens",
|
|
348
|
+
rates: {
|
|
349
|
+
input: "0.25",
|
|
350
|
+
cacheRead: null,
|
|
351
|
+
cacheWrite: null,
|
|
352
|
+
output: "1.50",
|
|
353
|
+
reasoning: null
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
},
|
|
357
|
+
{
|
|
358
|
+
id: "qwen3.5-397b-a17b",
|
|
359
|
+
name: "Qwen 3.5 397B A17B",
|
|
360
|
+
active: true,
|
|
361
|
+
pricing: {
|
|
362
|
+
currency: "EUR",
|
|
363
|
+
unit: "million_tokens",
|
|
364
|
+
rates: {
|
|
365
|
+
input: "0.60",
|
|
366
|
+
cacheRead: null,
|
|
367
|
+
cacheWrite: null,
|
|
368
|
+
output: "3.60",
|
|
369
|
+
reasoning: null
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
},
|
|
373
|
+
{
|
|
374
|
+
id: "gemma-4-26b-a4b-it",
|
|
375
|
+
name: "Gemma 4 26B A4B IT",
|
|
376
|
+
active: true,
|
|
377
|
+
pricing: {
|
|
378
|
+
currency: "EUR",
|
|
379
|
+
unit: "million_tokens",
|
|
380
|
+
rates: {
|
|
381
|
+
input: "0.25",
|
|
382
|
+
cacheRead: null,
|
|
383
|
+
cacheWrite: null,
|
|
384
|
+
output: "0.50",
|
|
385
|
+
reasoning: null
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
]
|
|
390
|
+
}
|
|
391
|
+
];
|
|
392
|
+
//#endregion
|
|
393
|
+
//#region ../modules/src/credentials/providerKeys/providerKeys.contract.ts
|
|
394
|
+
const providerIds = providerCatalog.map((provider) => provider.id);
|
|
395
|
+
const providerKeyIdSchema$1 = z.uuid();
|
|
396
|
+
const workspaceKeyIdSchema$1 = z.uuid();
|
|
397
|
+
const providerKeyLabelSchema = z.string().trim().min(1).max(100);
|
|
398
|
+
const providerKeyHintSchema = z.string().min(1).max(64);
|
|
399
|
+
const encryptedProviderKeySchema = z.strictObject({
|
|
400
|
+
encryptionVersion: z.literal(1),
|
|
401
|
+
ciphertext: z.string().min(1).max(32768),
|
|
402
|
+
initializationVector: z.string().min(1).max(64),
|
|
403
|
+
wrappedDek: z.string().min(1).max(1024)
|
|
404
|
+
}).readonly();
|
|
405
|
+
const providerIdSchema = z.enum(providerIds);
|
|
406
|
+
const providerKeyMetadataSchema = z.object({
|
|
407
|
+
id: providerKeyIdSchema$1,
|
|
408
|
+
provider: providerIdSchema,
|
|
409
|
+
label: providerKeyLabelSchema.nullable(),
|
|
410
|
+
keyHint: providerKeyHintSchema.nullable(),
|
|
411
|
+
version: z.int().positive(),
|
|
412
|
+
createdAt: z.iso.datetime(),
|
|
413
|
+
updatedAt: z.iso.datetime()
|
|
414
|
+
}).readonly();
|
|
415
|
+
const providerKeyEncryptionContextSchema = z.object({
|
|
416
|
+
organizationId: z.uuid(),
|
|
417
|
+
workspaceKey: z.object({
|
|
418
|
+
kid: workspaceKeyIdSchema$1,
|
|
419
|
+
publicKey: z.string().min(1).max(1024)
|
|
420
|
+
}).readonly()
|
|
421
|
+
}).readonly();
|
|
422
|
+
z.object({
|
|
423
|
+
providerKeys: z.array(providerKeyMetadataSchema).readonly(),
|
|
424
|
+
encryptionContext: providerKeyEncryptionContextSchema.nullable()
|
|
425
|
+
}).readonly();
|
|
426
|
+
z.object({ providerKey: providerKeyMetadataSchema }).readonly();
|
|
427
|
+
z.strictObject({
|
|
428
|
+
id: providerKeyIdSchema$1,
|
|
429
|
+
workspaceKeyKid: workspaceKeyIdSchema$1,
|
|
430
|
+
provider: providerIdSchema,
|
|
431
|
+
label: providerKeyLabelSchema.optional(),
|
|
432
|
+
keyHint: providerKeyHintSchema.optional(),
|
|
433
|
+
encryptedProviderKey: encryptedProviderKeySchema
|
|
434
|
+
}).readonly();
|
|
435
|
+
z.strictObject({
|
|
436
|
+
workspaceKeyKid: workspaceKeyIdSchema$1,
|
|
437
|
+
label: providerKeyLabelSchema.optional(),
|
|
438
|
+
keyHint: providerKeyHintSchema.optional(),
|
|
439
|
+
encryptedProviderKey: encryptedProviderKeySchema,
|
|
440
|
+
version: z.int().positive()
|
|
441
|
+
}).readonly();
|
|
442
|
+
const affectedAgentSchema = z.object({
|
|
443
|
+
id: z.uuid(),
|
|
444
|
+
name: z.string().min(1).max(100)
|
|
445
|
+
}).readonly();
|
|
446
|
+
z.object({ affectedAgents: z.array(affectedAgentSchema).readonly() }).readonly();
|
|
447
|
+
z.object({
|
|
448
|
+
deleted: z.literal(true),
|
|
449
|
+
affectedAgents: z.array(affectedAgentSchema).readonly()
|
|
450
|
+
}).readonly();
|
|
451
|
+
//#endregion
|
|
452
|
+
//#region ../modules/src/agents/agentApiKeys/agentApiKeys.contract.ts
|
|
453
|
+
const agentApiKeyIdSchema = z.uuid();
|
|
454
|
+
const agentApiKeyLabelSchema = z.string().trim().min(1).max(100);
|
|
455
|
+
const agentApiKeyMetadataSchema = z.object({
|
|
456
|
+
id: agentApiKeyIdSchema,
|
|
457
|
+
label: agentApiKeyLabelSchema.nullable(),
|
|
458
|
+
keyPrefix: z.string().min(1).max(32),
|
|
459
|
+
createdAt: z.iso.datetime(),
|
|
460
|
+
lastUsedAt: z.iso.datetime().nullable(),
|
|
461
|
+
revokedAt: z.iso.datetime().nullable()
|
|
462
|
+
}).readonly();
|
|
463
|
+
z.object({ apiKeys: z.array(agentApiKeyMetadataSchema).readonly() }).readonly();
|
|
464
|
+
z.strictObject({ label: agentApiKeyLabelSchema.optional() }).readonly();
|
|
465
|
+
const createdAgentApiKeySchema = z.object({
|
|
466
|
+
metadata: agentApiKeyMetadataSchema,
|
|
467
|
+
secret: z.string().regex(/^ar_agent_[A-Za-z0-9_-]{43}$/)
|
|
468
|
+
}).readonly();
|
|
469
|
+
z.object({ revoked: z.literal(true) }).readonly();
|
|
470
|
+
//#endregion
|
|
471
|
+
//#region ../modules/src/agents/agentConfigurations/agentConfigurations.contract.ts
|
|
472
|
+
const agentIdSchema = z.uuid();
|
|
473
|
+
const providerKeyIdSchema = z.uuid();
|
|
474
|
+
const agentNameSchema = z.string().trim().min(1).max(100);
|
|
475
|
+
const modelIdSchema = z.string().trim().min(1).max(200);
|
|
476
|
+
const agentStatusSchema = z.enum(["active", "archived"]);
|
|
477
|
+
const agentMetadataSchema = z.object({
|
|
478
|
+
id: agentIdSchema,
|
|
479
|
+
name: agentNameSchema,
|
|
480
|
+
providerKeyId: providerKeyIdSchema.nullable(),
|
|
481
|
+
provider: providerIdSchema.nullable(),
|
|
482
|
+
modelId: modelIdSchema,
|
|
483
|
+
status: agentStatusSchema,
|
|
484
|
+
version: z.int().positive(),
|
|
485
|
+
createdAt: z.iso.datetime(),
|
|
486
|
+
updatedAt: z.iso.datetime()
|
|
487
|
+
}).readonly();
|
|
488
|
+
z.object({ agents: z.array(agentMetadataSchema).readonly() }).readonly();
|
|
489
|
+
z.object({ agent: agentMetadataSchema }).readonly();
|
|
490
|
+
z.object({
|
|
491
|
+
agent: agentMetadataSchema,
|
|
492
|
+
apiKey: createdAgentApiKeySchema
|
|
493
|
+
}).readonly();
|
|
494
|
+
z.strictObject({
|
|
495
|
+
name: agentNameSchema,
|
|
496
|
+
providerKeyId: providerKeyIdSchema,
|
|
497
|
+
modelId: modelIdSchema
|
|
498
|
+
}).readonly();
|
|
499
|
+
z.strictObject({
|
|
500
|
+
name: agentNameSchema,
|
|
501
|
+
providerKeyId: providerKeyIdSchema,
|
|
502
|
+
modelId: modelIdSchema,
|
|
503
|
+
version: z.int().positive()
|
|
504
|
+
}).readonly();
|
|
505
|
+
z.strictObject({ version: z.int().positive() }).readonly();
|
|
506
|
+
//#endregion
|
|
507
|
+
//#region ../modules/src/agents/runtimeConfiguration/agentRuntimeConfiguration.contract.ts
|
|
508
|
+
const agentRuntimeConfigurationResponseSchema = z.object({
|
|
509
|
+
agentId: z.uuid(),
|
|
510
|
+
agentConfigurationVersion: z.int().positive(),
|
|
511
|
+
organizationId: z.uuid(),
|
|
512
|
+
providerKeyId: z.uuid(),
|
|
513
|
+
provider: providerIdSchema,
|
|
514
|
+
modelId: z.string().min(1).max(200),
|
|
515
|
+
credential: z.object({
|
|
516
|
+
mode: z.literal("managed"),
|
|
517
|
+
apiKey: z.string().min(1)
|
|
518
|
+
}).readonly()
|
|
519
|
+
}).readonly();
|
|
520
|
+
//#endregion
|
|
521
|
+
//#region src/core/runtimeConfiguration.ts
|
|
522
|
+
const agentApiKeyPattern = /^ar_agent_[A-Za-z0-9_-]{43}$/;
|
|
523
|
+
var AgenticRunApiError = class extends Error {
|
|
524
|
+
code;
|
|
525
|
+
statusCode;
|
|
526
|
+
constructor(statusCode, code, options = {}) {
|
|
527
|
+
super(`Agentic Run API request failed with ${statusCode} (${code}).`, { cause: options.cause });
|
|
528
|
+
this.name = "AgenticRunApiError";
|
|
529
|
+
this.code = code;
|
|
530
|
+
this.statusCode = statusCode;
|
|
531
|
+
}
|
|
532
|
+
};
|
|
533
|
+
const loadAgentRuntimeConfiguration = async ({ agentApiKey, apiUrl = defaultApiUrl, fetch = globalThis.fetch }) => {
|
|
534
|
+
if (!agentApiKeyPattern.test(agentApiKey)) throw new TypeError("Agentic Run Agent API key has an invalid format.");
|
|
535
|
+
const response = await fetch(createApiEndpoint(apiUrl, "agent/configuration"), { headers: { authorization: `Bearer ${agentApiKey}` } });
|
|
536
|
+
const body = await readJson(response);
|
|
537
|
+
if (!response.ok) {
|
|
538
|
+
const error = apiErrorResponseSchema.safeParse(body);
|
|
539
|
+
throw new AgenticRunApiError(response.status, error.success ? error.data.error.code : "request_failed");
|
|
540
|
+
}
|
|
541
|
+
const configuration = agentRuntimeConfigurationResponseSchema.safeParse(body);
|
|
542
|
+
if (!configuration.success) throw new AgenticRunApiError(response.status, "invalid_configuration_response", { cause: configuration.error });
|
|
543
|
+
return configuration.data;
|
|
544
|
+
};
|
|
545
|
+
const readJson = async (response) => {
|
|
546
|
+
try {
|
|
547
|
+
return await response.json();
|
|
548
|
+
} catch (cause) {
|
|
549
|
+
throw new AgenticRunApiError(response.status, "invalid_json_response", { cause });
|
|
550
|
+
}
|
|
551
|
+
};
|
|
552
|
+
//#endregion
|
|
553
|
+
//#region ../modules/src/tracing/tracing.contract.ts
|
|
554
|
+
const sessionStatusSchema = z.enum([
|
|
555
|
+
"idle",
|
|
556
|
+
"running",
|
|
557
|
+
"waiting_for_input",
|
|
558
|
+
"error",
|
|
559
|
+
"closed"
|
|
560
|
+
]);
|
|
561
|
+
const traceStatusSchema = z.enum([
|
|
562
|
+
"running",
|
|
563
|
+
"waiting_for_input",
|
|
564
|
+
"success",
|
|
565
|
+
"error",
|
|
566
|
+
"cancelled"
|
|
567
|
+
]);
|
|
568
|
+
const spanStatusSchema = z.enum([
|
|
569
|
+
"running",
|
|
570
|
+
"success",
|
|
571
|
+
"error",
|
|
572
|
+
"cancelled"
|
|
573
|
+
]);
|
|
574
|
+
const tracingLimits = {
|
|
575
|
+
attributesBytes: 32768,
|
|
576
|
+
attributesKeys: 64,
|
|
577
|
+
attributeKeyCharacters: 128,
|
|
578
|
+
attributeStringBytes: 8192,
|
|
579
|
+
attributeArrayEntries: 100,
|
|
580
|
+
contentBytes: 2097152,
|
|
581
|
+
spanEventsBytes: 262144,
|
|
582
|
+
spanEvents: 1e3,
|
|
583
|
+
spansPerBatch: 500,
|
|
584
|
+
lifecycleEventsPerBatch: 1002,
|
|
585
|
+
batchBytes: 10485760
|
|
586
|
+
};
|
|
587
|
+
const serializedByteLength = (value) => new TextEncoder().encode(JSON.stringify(value)).byteLength;
|
|
588
|
+
const attributeStringSchema = z.string().refine((value) => serializedByteLength(value) <= tracingLimits.attributeStringBytes, "Attribute string is too large");
|
|
589
|
+
const attributeValueSchema = z.union([
|
|
590
|
+
attributeStringSchema,
|
|
591
|
+
z.number(),
|
|
592
|
+
z.boolean(),
|
|
593
|
+
z.null(),
|
|
594
|
+
z.array(attributeStringSchema).max(tracingLimits.attributeArrayEntries),
|
|
595
|
+
z.array(z.number()).max(tracingLimits.attributeArrayEntries),
|
|
596
|
+
z.array(z.boolean()).max(tracingLimits.attributeArrayEntries)
|
|
597
|
+
]);
|
|
598
|
+
const attributeKeySchema = z.string().trim().min(1).max(tracingLimits.attributeKeyCharacters).refine((key) => !key.startsWith("agenticrun."), "`agenticrun.*` attributes are reserved");
|
|
599
|
+
const attributesSchema = z.record(attributeKeySchema, attributeValueSchema).superRefine((attributes, context) => {
|
|
600
|
+
if (Object.keys(attributes).length > tracingLimits.attributesKeys) context.addIssue({
|
|
601
|
+
code: "custom",
|
|
602
|
+
message: `Attributes may contain at most ${tracingLimits.attributesKeys} keys`
|
|
603
|
+
});
|
|
604
|
+
if (serializedByteLength(attributes) > tracingLimits.attributesBytes) context.addIssue({
|
|
605
|
+
code: "custom",
|
|
606
|
+
message: "Attributes object is too large"
|
|
607
|
+
});
|
|
608
|
+
}).readonly();
|
|
609
|
+
const identifierSchema = z.string().trim().min(1).max(512);
|
|
610
|
+
const timestampSchema = z.iso.datetime();
|
|
611
|
+
const contentTextSchema = z.string().max(tracingLimits.contentBytes);
|
|
612
|
+
const textContentSchema = z.strictObject({
|
|
613
|
+
type: z.literal("text"),
|
|
614
|
+
text: contentTextSchema
|
|
615
|
+
});
|
|
616
|
+
const reasoningContentSchema = z.strictObject({
|
|
617
|
+
type: z.literal("reasoning"),
|
|
618
|
+
text: contentTextSchema
|
|
619
|
+
});
|
|
620
|
+
const jsonContentSchema = z.strictObject({
|
|
621
|
+
type: z.literal("json"),
|
|
622
|
+
value: z.json()
|
|
623
|
+
});
|
|
624
|
+
const toolCallContentSchema = z.strictObject({
|
|
625
|
+
type: z.literal("tool-call"),
|
|
626
|
+
toolName: z.string().trim().min(1).max(200),
|
|
627
|
+
callId: identifierSchema,
|
|
628
|
+
arguments: z.json()
|
|
629
|
+
});
|
|
630
|
+
const toolResultContentSchema = z.strictObject({
|
|
631
|
+
type: z.literal("tool-result"),
|
|
632
|
+
callId: identifierSchema,
|
|
633
|
+
result: z.json()
|
|
634
|
+
});
|
|
635
|
+
const messageContentBlockSchema = z.discriminatedUnion("type", [
|
|
636
|
+
textContentSchema,
|
|
637
|
+
reasoningContentSchema,
|
|
638
|
+
jsonContentSchema,
|
|
639
|
+
toolCallContentSchema,
|
|
640
|
+
toolResultContentSchema
|
|
641
|
+
]).readonly();
|
|
642
|
+
const messagesContentSchema = z.strictObject({
|
|
643
|
+
type: z.literal("messages"),
|
|
644
|
+
messages: z.array(z.strictObject({
|
|
645
|
+
id: identifierSchema.optional(),
|
|
646
|
+
role: z.enum([
|
|
647
|
+
"system",
|
|
648
|
+
"developer",
|
|
649
|
+
"user",
|
|
650
|
+
"assistant",
|
|
651
|
+
"tool"
|
|
652
|
+
]),
|
|
653
|
+
name: z.string().trim().min(1).max(200).optional(),
|
|
654
|
+
content: z.array(messageContentBlockSchema).min(1)
|
|
655
|
+
})).min(1)
|
|
656
|
+
});
|
|
657
|
+
const documentsContentSchema = z.strictObject({
|
|
658
|
+
type: z.literal("documents"),
|
|
659
|
+
documents: z.array(z.strictObject({
|
|
660
|
+
id: identifierSchema.optional(),
|
|
661
|
+
content: contentTextSchema,
|
|
662
|
+
attributes: attributesSchema.optional()
|
|
663
|
+
})).min(1)
|
|
664
|
+
});
|
|
665
|
+
const redactedContentSchema = z.strictObject({
|
|
666
|
+
type: z.literal("redacted"),
|
|
667
|
+
reason: z.string().trim().min(1).max(500).optional()
|
|
668
|
+
});
|
|
669
|
+
const omittedContentSchema = z.strictObject({
|
|
670
|
+
type: z.literal("omitted"),
|
|
671
|
+
reason: z.enum([
|
|
672
|
+
"disabled",
|
|
673
|
+
"unsupported",
|
|
674
|
+
"too-large",
|
|
675
|
+
"unavailable"
|
|
676
|
+
]),
|
|
677
|
+
originalBytes: z.int().nonnegative().optional()
|
|
678
|
+
});
|
|
679
|
+
const traceContentSchema = z.discriminatedUnion("type", [
|
|
680
|
+
messagesContentSchema,
|
|
681
|
+
textContentSchema,
|
|
682
|
+
jsonContentSchema,
|
|
683
|
+
toolCallContentSchema,
|
|
684
|
+
toolResultContentSchema,
|
|
685
|
+
documentsContentSchema,
|
|
686
|
+
redactedContentSchema,
|
|
687
|
+
omittedContentSchema
|
|
688
|
+
]).refine((content) => serializedByteLength(content) <= tracingLimits.contentBytes, "Trace content is too large").readonly();
|
|
689
|
+
const spanEventSchema = z.strictObject({
|
|
690
|
+
name: z.string().trim().min(1).max(200),
|
|
691
|
+
time: timestampSchema,
|
|
692
|
+
attributes: attributesSchema.optional()
|
|
693
|
+
}).readonly();
|
|
694
|
+
const spanEventsSchema = z.array(spanEventSchema).max(tracingLimits.spanEvents).refine((events) => serializedByteLength(events) <= tracingLimits.spanEventsBytes, "Span events are too large").readonly();
|
|
695
|
+
const tokenUsageSchema = z.strictObject({
|
|
696
|
+
inputTokensTotal: z.int().nonnegative().optional(),
|
|
697
|
+
inputTokensCacheRead: z.int().nonnegative().optional(),
|
|
698
|
+
inputTokensCacheWrite: z.int().nonnegative().optional(),
|
|
699
|
+
outputTokensTotal: z.int().nonnegative().optional(),
|
|
700
|
+
outputTokensReasoning: z.int().nonnegative().optional(),
|
|
701
|
+
costUsd: z.number().nonnegative().optional()
|
|
702
|
+
}).superRefine((usage, context) => {
|
|
703
|
+
if (usage.inputTokensTotal !== void 0 && usage.inputTokensCacheRead !== void 0 && usage.inputTokensCacheRead > usage.inputTokensTotal) context.addIssue({
|
|
704
|
+
code: "custom",
|
|
705
|
+
path: ["inputTokensCacheRead"],
|
|
706
|
+
message: "Cache-read tokens cannot exceed total input tokens"
|
|
707
|
+
});
|
|
708
|
+
if (usage.inputTokensTotal !== void 0 && usage.inputTokensCacheWrite !== void 0 && usage.inputTokensCacheWrite > usage.inputTokensTotal) context.addIssue({
|
|
709
|
+
code: "custom",
|
|
710
|
+
path: ["inputTokensCacheWrite"],
|
|
711
|
+
message: "Cache-write tokens cannot exceed total input tokens"
|
|
712
|
+
});
|
|
713
|
+
if (usage.inputTokensTotal !== void 0 && usage.inputTokensCacheRead !== void 0 && usage.inputTokensCacheWrite !== void 0 && usage.inputTokensCacheRead + usage.inputTokensCacheWrite > usage.inputTokensTotal) context.addIssue({
|
|
714
|
+
code: "custom",
|
|
715
|
+
path: ["inputTokensCacheWrite"],
|
|
716
|
+
message: "Combined cache tokens cannot exceed total input tokens"
|
|
717
|
+
});
|
|
718
|
+
if (usage.outputTokensTotal !== void 0 && usage.outputTokensReasoning !== void 0 && usage.outputTokensReasoning > usage.outputTokensTotal) context.addIssue({
|
|
719
|
+
code: "custom",
|
|
720
|
+
path: ["outputTokensReasoning"],
|
|
721
|
+
message: "Reasoning tokens cannot exceed total output tokens"
|
|
722
|
+
});
|
|
723
|
+
}).readonly();
|
|
724
|
+
const tracingSessionResolutionSchema = z.strictObject({
|
|
725
|
+
id: z.uuid(),
|
|
726
|
+
externalSessionId: identifierSchema.optional(),
|
|
727
|
+
attributes: attributesSchema.optional()
|
|
728
|
+
}).readonly();
|
|
729
|
+
const traceStartedEventSchema = z.strictObject({
|
|
730
|
+
type: z.literal("trace.started"),
|
|
731
|
+
id: z.uuid(),
|
|
732
|
+
sourceTraceId: identifierSchema.optional(),
|
|
733
|
+
agentConfigurationVersion: z.int().positive(),
|
|
734
|
+
startedAt: timestampSchema,
|
|
735
|
+
attributes: attributesSchema.optional()
|
|
736
|
+
}).readonly();
|
|
737
|
+
const traceCompletedEventSchema = z.strictObject({
|
|
738
|
+
type: z.literal("trace.completed"),
|
|
739
|
+
id: z.uuid(),
|
|
740
|
+
status: z.enum([
|
|
741
|
+
"success",
|
|
742
|
+
"error",
|
|
743
|
+
"cancelled"
|
|
744
|
+
]),
|
|
745
|
+
endedAt: timestampSchema
|
|
746
|
+
}).readonly();
|
|
747
|
+
const spanStartedEventSchema = z.strictObject({
|
|
748
|
+
type: z.literal("span.started"),
|
|
749
|
+
id: z.uuid(),
|
|
750
|
+
traceId: z.uuid(),
|
|
751
|
+
parentSpanId: z.uuid().optional(),
|
|
752
|
+
sourceSpanId: identifierSchema.optional(),
|
|
753
|
+
name: z.string().trim().min(1).max(200),
|
|
754
|
+
spanType: z.enum([
|
|
755
|
+
"agent",
|
|
756
|
+
"workflow",
|
|
757
|
+
"model",
|
|
758
|
+
"tool",
|
|
759
|
+
"retrieval",
|
|
760
|
+
"embedding",
|
|
761
|
+
"custom"
|
|
762
|
+
]),
|
|
763
|
+
startedAt: timestampSchema,
|
|
764
|
+
input: traceContentSchema.optional(),
|
|
765
|
+
attributes: attributesSchema.optional(),
|
|
766
|
+
tags: z.array(z.string().trim().min(1).max(100)).max(50).optional()
|
|
767
|
+
}).readonly();
|
|
768
|
+
const successfulSpanResultSchema = z.strictObject({
|
|
769
|
+
status: z.literal("success"),
|
|
770
|
+
output: traceContentSchema.optional()
|
|
771
|
+
});
|
|
772
|
+
const failedSpanResultSchema = z.strictObject({
|
|
773
|
+
status: z.literal("error"),
|
|
774
|
+
output: traceContentSchema.optional(),
|
|
775
|
+
error: z.strictObject({
|
|
776
|
+
type: z.string().trim().min(1).max(200),
|
|
777
|
+
message: z.string().max(32768)
|
|
778
|
+
})
|
|
779
|
+
});
|
|
780
|
+
const cancelledSpanResultSchema = z.strictObject({
|
|
781
|
+
status: z.literal("cancelled"),
|
|
782
|
+
output: traceContentSchema.optional(),
|
|
783
|
+
error: z.strictObject({
|
|
784
|
+
type: z.string().trim().min(1).max(200),
|
|
785
|
+
message: z.string().max(32768)
|
|
786
|
+
}).optional()
|
|
787
|
+
});
|
|
788
|
+
const spanResultSchema = z.discriminatedUnion("status", [
|
|
789
|
+
successfulSpanResultSchema,
|
|
790
|
+
failedSpanResultSchema,
|
|
791
|
+
cancelledSpanResultSchema
|
|
792
|
+
]).readonly();
|
|
793
|
+
const modelCallSchema = z.strictObject({
|
|
794
|
+
providerId: z.string().trim().min(1).max(200).nullable(),
|
|
795
|
+
modelId: z.string().trim().min(1).max(200).nullable(),
|
|
796
|
+
responseModelId: z.string().trim().min(1).max(200).optional()
|
|
797
|
+
}).readonly();
|
|
798
|
+
const spanCompletedEventSchema = z.strictObject({
|
|
799
|
+
type: z.literal("span.completed"),
|
|
800
|
+
id: z.uuid(),
|
|
801
|
+
traceId: z.uuid(),
|
|
802
|
+
endedAt: timestampSchema,
|
|
803
|
+
firstTokenAt: timestampSchema.optional(),
|
|
804
|
+
result: spanResultSchema,
|
|
805
|
+
events: spanEventsSchema.optional(),
|
|
806
|
+
usage: tokenUsageSchema.optional(),
|
|
807
|
+
model: modelCallSchema.optional(),
|
|
808
|
+
attributes: attributesSchema.optional()
|
|
809
|
+
}).readonly();
|
|
810
|
+
const tracingLifecycleEventSchema = z.discriminatedUnion("type", [
|
|
811
|
+
traceStartedEventSchema,
|
|
812
|
+
traceCompletedEventSchema,
|
|
813
|
+
spanStartedEventSchema,
|
|
814
|
+
spanCompletedEventSchema
|
|
815
|
+
]);
|
|
816
|
+
const tracingIngestionRequestSchema = z.strictObject({
|
|
817
|
+
schemaVersion: z.literal(1),
|
|
818
|
+
session: tracingSessionResolutionSchema,
|
|
819
|
+
events: z.array(tracingLifecycleEventSchema).min(1).max(tracingLimits.lifecycleEventsPerBatch)
|
|
820
|
+
}).superRefine((request, context) => {
|
|
821
|
+
if (new Set(request.events.filter((event) => event.type.startsWith("span.")).map((event) => event.id)).size > tracingLimits.spansPerBatch) context.addIssue({
|
|
822
|
+
code: "custom",
|
|
823
|
+
path: ["events"],
|
|
824
|
+
message: "Batch contains too many spans"
|
|
825
|
+
});
|
|
826
|
+
if (serializedByteLength(request) > tracingLimits.batchBytes) context.addIssue({
|
|
827
|
+
code: "custom",
|
|
828
|
+
message: "Tracing batch is too large"
|
|
829
|
+
});
|
|
830
|
+
}).readonly();
|
|
831
|
+
const tracingIngestionResponseSchema = z.object({
|
|
832
|
+
sessionId: z.uuid(),
|
|
833
|
+
sessionCreated: z.boolean(),
|
|
834
|
+
acceptedEventCount: z.int().positive()
|
|
835
|
+
}).readonly();
|
|
836
|
+
//#endregion
|
|
837
|
+
//#region ../modules/src/tracing/sessionInspection/sessionInspection.contract.ts
|
|
838
|
+
const offset = z.coerce.number().int().min(0).max(2147483647).default(0);
|
|
839
|
+
z.object({ offset });
|
|
840
|
+
z.object({
|
|
841
|
+
traceOffset: offset,
|
|
842
|
+
spanOffset: offset
|
|
843
|
+
});
|
|
844
|
+
const timestamps = {
|
|
845
|
+
createdAt: z.iso.datetime(),
|
|
846
|
+
updatedAt: z.iso.datetime()
|
|
847
|
+
};
|
|
848
|
+
const attributes = z.record(z.string(), z.json());
|
|
849
|
+
const costs = z.array(z.object({
|
|
850
|
+
currency: z.string(),
|
|
851
|
+
total: z.string(),
|
|
852
|
+
input: z.string().nullable(),
|
|
853
|
+
output: z.string().nullable(),
|
|
854
|
+
cacheRead: z.string().nullable(),
|
|
855
|
+
cacheWrite: z.string().nullable(),
|
|
856
|
+
reasoning: z.string().nullable()
|
|
857
|
+
})).nullable().optional();
|
|
858
|
+
const inspectedSessionSchema = z.object({
|
|
859
|
+
id: z.uuid(),
|
|
860
|
+
tenantId: z.uuid(),
|
|
861
|
+
agentId: z.uuid(),
|
|
862
|
+
agentName: z.string(),
|
|
863
|
+
externalId: z.string().nullable(),
|
|
864
|
+
status: sessionStatusSchema,
|
|
865
|
+
endedAt: z.iso.datetime().nullable(),
|
|
866
|
+
attributes,
|
|
867
|
+
...timestamps
|
|
868
|
+
});
|
|
869
|
+
const inspectedContentSchema = z.json().transform((value) => {
|
|
870
|
+
if (value === null) return null;
|
|
871
|
+
const parsed = traceContentSchema.safeParse(value);
|
|
872
|
+
return parsed.success ? parsed.data : {
|
|
873
|
+
type: "json",
|
|
874
|
+
value
|
|
875
|
+
};
|
|
876
|
+
});
|
|
877
|
+
const inspectedSpanSchema = z.object({
|
|
878
|
+
id: z.uuid(),
|
|
879
|
+
tenantId: z.uuid(),
|
|
880
|
+
traceId: z.uuid(),
|
|
881
|
+
parentSpanId: z.uuid().nullable(),
|
|
882
|
+
sourceSpanId: z.string().nullable(),
|
|
883
|
+
name: z.string(),
|
|
884
|
+
spanType: z.enum([
|
|
885
|
+
"agent",
|
|
886
|
+
"workflow",
|
|
887
|
+
"model",
|
|
888
|
+
"tool",
|
|
889
|
+
"retrieval",
|
|
890
|
+
"embedding",
|
|
891
|
+
"custom"
|
|
892
|
+
]),
|
|
893
|
+
status: spanStatusSchema,
|
|
894
|
+
startedAt: z.iso.datetime(),
|
|
895
|
+
endedAt: z.iso.datetime().nullable(),
|
|
896
|
+
firstTokenAt: z.iso.datetime().nullable(),
|
|
897
|
+
input: inspectedContentSchema,
|
|
898
|
+
output: inspectedContentSchema,
|
|
899
|
+
events: z.array(attributes),
|
|
900
|
+
attributes,
|
|
901
|
+
tags: z.array(z.string()),
|
|
902
|
+
errorType: z.string().nullable(),
|
|
903
|
+
errorMessage: z.string().nullable(),
|
|
904
|
+
inputTokensTotal: z.int().nullable(),
|
|
905
|
+
inputTokensCacheRead: z.int().nullable(),
|
|
906
|
+
inputTokensCacheWrite: z.int().nullable(),
|
|
907
|
+
outputTokensTotal: z.int().nullable(),
|
|
908
|
+
outputTokensReasoning: z.int().nullable(),
|
|
909
|
+
costs,
|
|
910
|
+
costUsd: z.string().nullable(),
|
|
911
|
+
...timestamps
|
|
912
|
+
});
|
|
913
|
+
const inspectedTraceSchema = z.object({
|
|
914
|
+
costs,
|
|
915
|
+
id: z.uuid(),
|
|
916
|
+
tenantId: z.uuid(),
|
|
917
|
+
sessionId: z.uuid(),
|
|
918
|
+
sourceTraceId: z.string().nullable(),
|
|
919
|
+
agentConfigurationVersion: z.int(),
|
|
920
|
+
status: traceStatusSchema,
|
|
921
|
+
startedAt: z.iso.datetime(),
|
|
922
|
+
endedAt: z.iso.datetime().nullable(),
|
|
923
|
+
attributes,
|
|
924
|
+
...timestamps,
|
|
925
|
+
spans: z.array(inspectedSpanSchema)
|
|
926
|
+
});
|
|
927
|
+
const sessionSummaryShape = {
|
|
928
|
+
startedAt: z.iso.datetime().nullable(),
|
|
929
|
+
traceCount: z.int().nonnegative(),
|
|
930
|
+
hasErrors: z.boolean(),
|
|
931
|
+
durationMs: z.number().nonnegative().nullable(),
|
|
932
|
+
inputTokens: z.int().nonnegative().nullable(),
|
|
933
|
+
outputTokens: z.int().nonnegative().nullable(),
|
|
934
|
+
costs,
|
|
935
|
+
costUsd: z.string().nullable(),
|
|
936
|
+
inputPreview: z.string().nullable()
|
|
937
|
+
};
|
|
938
|
+
z.object({
|
|
939
|
+
sessions: z.array(inspectedSessionSchema.extend(sessionSummaryShape)),
|
|
940
|
+
nextOffset: z.int().nonnegative().nullable()
|
|
941
|
+
});
|
|
942
|
+
z.object({
|
|
943
|
+
session: inspectedSessionSchema.extend({
|
|
944
|
+
...sessionSummaryShape,
|
|
945
|
+
traces: z.array(inspectedTraceSchema)
|
|
946
|
+
}),
|
|
947
|
+
nextTraceOffset: z.int().nonnegative().nullable(),
|
|
948
|
+
nextSpanOffset: z.int().nonnegative().nullable()
|
|
949
|
+
});
|
|
950
|
+
//#endregion
|
|
951
|
+
//#region src/core/tracing/traceContent.ts
|
|
952
|
+
const sensitiveKeyPattern = /(authorization|cookie|api[-_]?key|token|secret|password)/i;
|
|
953
|
+
const isSensitiveAttributeKey = (key) => sensitiveKeyPattern.test(key);
|
|
954
|
+
const validatedAttributes = (attributes) => {
|
|
955
|
+
const result = attributesSchema.safeParse(attributes);
|
|
956
|
+
return result.success && Object.keys(result.data).length > 0 ? result.data : void 0;
|
|
957
|
+
};
|
|
958
|
+
const safeJson = (value) => {
|
|
959
|
+
const seen = /* @__PURE__ */ new WeakSet();
|
|
960
|
+
const json = JSON.stringify(value ?? null, (key, item) => {
|
|
961
|
+
if (key && sensitiveKeyPattern.test(key)) return "[REDACTED]";
|
|
962
|
+
if (typeof item === "bigint") return item.toString();
|
|
963
|
+
if (typeof item === "function" || typeof item === "symbol" || item === void 0) return null;
|
|
964
|
+
if (typeof item === "object" && item !== null) {
|
|
965
|
+
if (seen.has(item)) return "[Circular]";
|
|
966
|
+
seen.add(item);
|
|
967
|
+
}
|
|
968
|
+
return item;
|
|
969
|
+
});
|
|
970
|
+
return JSON.parse(json ?? "null");
|
|
971
|
+
};
|
|
972
|
+
const validatedContent = (content) => {
|
|
973
|
+
const result = traceContentSchema.safeParse(content);
|
|
974
|
+
return result.success ? result.data : omittedContent(content, "too-large");
|
|
975
|
+
};
|
|
976
|
+
const omittedContent = (value, reason) => ({
|
|
977
|
+
type: "omitted",
|
|
978
|
+
reason,
|
|
979
|
+
originalBytes: new TextEncoder().encode(JSON.stringify(safeJson(value))).byteLength
|
|
980
|
+
});
|
|
981
|
+
//#endregion
|
|
982
|
+
//#region src/core/tracing/traceDelivery.ts
|
|
983
|
+
const createTraceDelivery = ({ session: initialSession, transport, onSessionResolved }) => {
|
|
984
|
+
let session = initialSession;
|
|
985
|
+
let startDelivery = Promise.resolve();
|
|
986
|
+
let completionDelivery;
|
|
987
|
+
const deliver = async (events) => {
|
|
988
|
+
try {
|
|
989
|
+
const response = await transport.send({
|
|
990
|
+
schemaVersion: 1,
|
|
991
|
+
session,
|
|
992
|
+
events
|
|
993
|
+
});
|
|
994
|
+
if (!response) return false;
|
|
995
|
+
session = {
|
|
996
|
+
...session,
|
|
997
|
+
id: response.sessionId
|
|
998
|
+
};
|
|
999
|
+
onSessionResolved(response.sessionId);
|
|
1000
|
+
return true;
|
|
1001
|
+
} catch (error) {
|
|
1002
|
+
console.warn("[Agentic Run] Trace delivery failed", { error: error instanceof Error ? error.name : "UnknownError" });
|
|
1003
|
+
return false;
|
|
1004
|
+
}
|
|
1005
|
+
};
|
|
1006
|
+
return {
|
|
1007
|
+
start(events) {
|
|
1008
|
+
startDelivery = deliver([...events]).then(() => void 0);
|
|
1009
|
+
return startDelivery;
|
|
1010
|
+
},
|
|
1011
|
+
complete(events) {
|
|
1012
|
+
if (!completionDelivery) {
|
|
1013
|
+
const snapshot = [...events];
|
|
1014
|
+
completionDelivery = (async () => {
|
|
1015
|
+
await startDelivery;
|
|
1016
|
+
if (!await deliver(snapshot)) await deliver(snapshot);
|
|
1017
|
+
})();
|
|
1018
|
+
}
|
|
1019
|
+
return completionDelivery;
|
|
1020
|
+
}
|
|
1021
|
+
};
|
|
1022
|
+
};
|
|
1023
|
+
//#endregion
|
|
1024
|
+
//#region src/core/tracing/createTrace.ts
|
|
1025
|
+
const createTrace = (context, options) => {
|
|
1026
|
+
const id = randomUUID();
|
|
1027
|
+
const events = [{
|
|
1028
|
+
type: "trace.started",
|
|
1029
|
+
id,
|
|
1030
|
+
agentConfigurationVersion: context.agentConfigurationVersion,
|
|
1031
|
+
startedAt: now(),
|
|
1032
|
+
...options.sourceTraceId ? { sourceTraceId: options.sourceTraceId } : {},
|
|
1033
|
+
...options.attributes ? { attributes: validatedAttributes(options.attributes) } : {}
|
|
1034
|
+
}];
|
|
1035
|
+
let completed = false;
|
|
1036
|
+
const delivery = createTraceDelivery(context);
|
|
1037
|
+
const startSpan = (input) => {
|
|
1038
|
+
const spanId = randomUUID();
|
|
1039
|
+
let spanCompleted = false;
|
|
1040
|
+
let firstTokenAt;
|
|
1041
|
+
const model = input.spanType === "model" ? { ...context.model } : void 0;
|
|
1042
|
+
const tags = input.tags?.map((tag) => tag.trim().slice(0, 100)).filter(Boolean).slice(0, 50);
|
|
1043
|
+
events.push({
|
|
1044
|
+
type: "span.started",
|
|
1045
|
+
id: spanId,
|
|
1046
|
+
traceId: id,
|
|
1047
|
+
...input.parentSpanId ? { parentSpanId: input.parentSpanId } : {},
|
|
1048
|
+
...input.sourceSpanId ? { sourceSpanId: input.sourceSpanId } : {},
|
|
1049
|
+
name: input.name.trim().slice(0, 200) || input.spanType,
|
|
1050
|
+
spanType: input.spanType,
|
|
1051
|
+
startedAt: now(),
|
|
1052
|
+
...input.input ? { input: validatedContent(input.input) } : {},
|
|
1053
|
+
...input.attributes ? { attributes: validatedAttributes(input.attributes) } : {},
|
|
1054
|
+
...tags?.length ? { tags } : {}
|
|
1055
|
+
});
|
|
1056
|
+
return {
|
|
1057
|
+
id: spanId,
|
|
1058
|
+
markFirstToken() {
|
|
1059
|
+
if (!completed && !spanCompleted && firstTokenAt === void 0) firstTokenAt = now();
|
|
1060
|
+
},
|
|
1061
|
+
complete(result, details = {}) {
|
|
1062
|
+
if (completed || spanCompleted) return;
|
|
1063
|
+
spanCompleted = true;
|
|
1064
|
+
events.push({
|
|
1065
|
+
type: "span.completed",
|
|
1066
|
+
id: spanId,
|
|
1067
|
+
traceId: id,
|
|
1068
|
+
endedAt: now(),
|
|
1069
|
+
result: {
|
|
1070
|
+
...result,
|
|
1071
|
+
...result.output ? { output: validatedContent(result.output) } : {}
|
|
1072
|
+
},
|
|
1073
|
+
...firstTokenAt ? { firstTokenAt } : {},
|
|
1074
|
+
...details.usage ? { usage: { ...details.usage } } : {},
|
|
1075
|
+
...details.attributes ? { attributes: validatedAttributes(details.attributes) } : {},
|
|
1076
|
+
...model ? { model: {
|
|
1077
|
+
...model,
|
|
1078
|
+
...details.responseModelId ? { responseModelId: details.responseModelId } : {}
|
|
1079
|
+
} } : {}
|
|
1080
|
+
});
|
|
1081
|
+
}
|
|
1082
|
+
};
|
|
1083
|
+
};
|
|
1084
|
+
return {
|
|
1085
|
+
id,
|
|
1086
|
+
rootSpan: startSpan(options.rootSpan),
|
|
1087
|
+
started: delivery.start(events),
|
|
1088
|
+
get completed() {
|
|
1089
|
+
return completed;
|
|
1090
|
+
},
|
|
1091
|
+
startSpan(input) {
|
|
1092
|
+
return completed ? void 0 : startSpan(input);
|
|
1093
|
+
},
|
|
1094
|
+
complete(status) {
|
|
1095
|
+
if (!completed) {
|
|
1096
|
+
completed = true;
|
|
1097
|
+
events.push({
|
|
1098
|
+
type: "trace.completed",
|
|
1099
|
+
id,
|
|
1100
|
+
status,
|
|
1101
|
+
endedAt: now()
|
|
1102
|
+
});
|
|
1103
|
+
}
|
|
1104
|
+
return delivery.complete(events);
|
|
1105
|
+
}
|
|
1106
|
+
};
|
|
1107
|
+
};
|
|
1108
|
+
const now = () => (/* @__PURE__ */ new Date()).toISOString();
|
|
1109
|
+
//#endregion
|
|
1110
|
+
//#region src/core/tracing/createTracingSession.ts
|
|
1111
|
+
const createTracingSessionFactory = (context) => (options = {}) => {
|
|
1112
|
+
const sensitiveAttribute = Object.keys(options.attributes ?? {}).find(isSensitiveAttributeKey);
|
|
1113
|
+
if (sensitiveAttribute) throw new TypeError(`Agentic Run tracing attribute "${sensitiveAttribute}" may contain sensitive data.`);
|
|
1114
|
+
let session = tracingSessionResolutionSchema.parse({
|
|
1115
|
+
id: options.id ?? randomUUID(),
|
|
1116
|
+
...options.externalSessionId ? { externalSessionId: options.externalSessionId } : {},
|
|
1117
|
+
...options.attributes ? { attributes: options.attributes } : {}
|
|
1118
|
+
});
|
|
1119
|
+
return {
|
|
1120
|
+
get id() {
|
|
1121
|
+
return session.id;
|
|
1122
|
+
},
|
|
1123
|
+
createTrace(input) {
|
|
1124
|
+
return createTrace({
|
|
1125
|
+
agentConfigurationVersion: context.agentConfigurationVersion,
|
|
1126
|
+
model: {
|
|
1127
|
+
providerId: context.provider,
|
|
1128
|
+
modelId: context.model
|
|
1129
|
+
},
|
|
1130
|
+
session,
|
|
1131
|
+
transport: context.transport,
|
|
1132
|
+
onSessionResolved(id) {
|
|
1133
|
+
session = {
|
|
1134
|
+
...session,
|
|
1135
|
+
id
|
|
1136
|
+
};
|
|
1137
|
+
}
|
|
1138
|
+
}, input);
|
|
1139
|
+
}
|
|
1140
|
+
};
|
|
1141
|
+
};
|
|
1142
|
+
//#endregion
|
|
1143
|
+
//#region src/core/tracing/tracingTransport.ts
|
|
1144
|
+
const createTracingTransport = ({ agentApiKey, apiUrl, fetch, timeoutMs }) => {
|
|
1145
|
+
const endpoint = createApiEndpoint(apiUrl, "agent/tracing");
|
|
1146
|
+
return { async send(request) {
|
|
1147
|
+
try {
|
|
1148
|
+
const payload = tracingIngestionRequestSchema.parse(request);
|
|
1149
|
+
const response = await fetch(endpoint, {
|
|
1150
|
+
method: "POST",
|
|
1151
|
+
headers: {
|
|
1152
|
+
authorization: `Bearer ${agentApiKey}`,
|
|
1153
|
+
"content-type": "application/json"
|
|
1154
|
+
},
|
|
1155
|
+
body: JSON.stringify(payload),
|
|
1156
|
+
signal: AbortSignal.timeout(timeoutMs)
|
|
1157
|
+
});
|
|
1158
|
+
if (!response.ok) {
|
|
1159
|
+
console.warn("[Agentic Run] Trace delivery failed", { statusCode: response.status });
|
|
1160
|
+
return;
|
|
1161
|
+
}
|
|
1162
|
+
const parsed = tracingIngestionResponseSchema.safeParse(await response.json());
|
|
1163
|
+
if (!parsed.success || parsed.data.acceptedEventCount !== payload.events.length) {
|
|
1164
|
+
console.warn("[Agentic Run] Trace delivery returned an invalid response");
|
|
1165
|
+
return;
|
|
1166
|
+
}
|
|
1167
|
+
return parsed.data;
|
|
1168
|
+
} catch (error) {
|
|
1169
|
+
console.warn("[Agentic Run] Trace delivery failed", { error: error instanceof Error ? error.name : "UnknownError" });
|
|
1170
|
+
return;
|
|
1171
|
+
}
|
|
1172
|
+
} };
|
|
1173
|
+
};
|
|
1174
|
+
//#endregion
|
|
1175
|
+
//#region ../env/src/index.ts
|
|
1176
|
+
const readString = (source, key) => {
|
|
1177
|
+
return source[key]?.trim() || void 0;
|
|
1178
|
+
};
|
|
1179
|
+
const validateIntegerConstraints = (key, value, options) => {
|
|
1180
|
+
if (options.min !== void 0 && value < options.min) throw new Error(`${key} must be at least ${options.min}`);
|
|
1181
|
+
if (options.max !== void 0 && value > options.max) throw new Error(`${key} must be at most ${options.max}`);
|
|
1182
|
+
return value;
|
|
1183
|
+
};
|
|
1184
|
+
const readInteger = (key, value, options) => {
|
|
1185
|
+
const integer = typeof value === "number" ? value : Number(value);
|
|
1186
|
+
if (!Number.isSafeInteger(integer) || typeof value === "string" && !/^[+-]?\d+$/.test(value)) throw new Error(`${key} must be an integer`);
|
|
1187
|
+
return validateIntegerConstraints(key, integer, options);
|
|
1188
|
+
};
|
|
1189
|
+
const createEnvironment = (source) => {
|
|
1190
|
+
const environment = (key, options) => {
|
|
1191
|
+
const value = readString(source, key);
|
|
1192
|
+
if (options?.type === "int") {
|
|
1193
|
+
if (value !== void 0) return readInteger(key, value, options);
|
|
1194
|
+
if ("default" in options && options.default !== void 0) return readInteger(key, options.default, options);
|
|
1195
|
+
if (options.optional) return void 0;
|
|
1196
|
+
throw new Error(`${key} is required`);
|
|
1197
|
+
}
|
|
1198
|
+
if (value !== void 0) return value;
|
|
1199
|
+
if (options && "default" in options && options.default !== void 0) {
|
|
1200
|
+
const defaultValue = options.default.trim();
|
|
1201
|
+
if (defaultValue) return defaultValue;
|
|
1202
|
+
throw new Error(`${key} default must not be empty`);
|
|
1203
|
+
}
|
|
1204
|
+
if (options?.optional) return void 0;
|
|
1205
|
+
throw new Error(`${key} is required`);
|
|
1206
|
+
};
|
|
1207
|
+
return environment;
|
|
1208
|
+
};
|
|
1209
|
+
//#endregion
|
|
1210
|
+
//#region ../modules/src/credentials/workspaceKeys/workspaceKeys.contract.ts
|
|
1211
|
+
const workspaceKeyIdSchema = z.uuid();
|
|
1212
|
+
const workspaceKeyMetadataSchema = z.object({
|
|
1213
|
+
kid: workspaceKeyIdSchema,
|
|
1214
|
+
type: z.enum(["google_kms", "customer"])
|
|
1215
|
+
}).readonly();
|
|
1216
|
+
z.object({ workspaceKey: workspaceKeyMetadataSchema.nullable() }).readonly();
|
|
1217
|
+
//#endregion
|
|
1218
|
+
//#region src/core/createAgenticRunClient.ts
|
|
1219
|
+
var AgenticRunConfigurationError = class extends Error {
|
|
1220
|
+
code;
|
|
1221
|
+
constructor(code, message, options = {}) {
|
|
1222
|
+
super(message, { cause: options.cause });
|
|
1223
|
+
this.name = "AgenticRunConfigurationError";
|
|
1224
|
+
this.code = code;
|
|
1225
|
+
}
|
|
1226
|
+
};
|
|
1227
|
+
const createAgenticRunClient = async (options = {}) => {
|
|
1228
|
+
const env = createEnvironment(process.env);
|
|
1229
|
+
const agentApiKey = options.agentApiKey?.trim() ?? env("AGENTICRUN_AGENT_API_KEY");
|
|
1230
|
+
const apiUrl = options.apiUrl?.trim() ?? env("AGENTICRUN_API_URL", { optional: true });
|
|
1231
|
+
const tracingTimeoutMs = options.tracingTimeoutMs ?? 1e3;
|
|
1232
|
+
if (!Number.isInteger(tracingTimeoutMs) || tracingTimeoutMs < 1) throw new TypeError("Agentic Run tracing timeout must be a positive integer.");
|
|
1233
|
+
const configuration = await loadAgentRuntimeConfiguration({
|
|
1234
|
+
agentApiKey,
|
|
1235
|
+
apiUrl,
|
|
1236
|
+
fetch: options.fetch
|
|
1237
|
+
});
|
|
1238
|
+
const provider = providerCatalog.find((candidate) => candidate.id === configuration.provider);
|
|
1239
|
+
if (!provider?.active) throw new AgenticRunConfigurationError("unsupported_provider", `Agentic Run provider "${configuration.provider}" is not supported.`);
|
|
1240
|
+
if (!provider.models.some((model) => model.id === configuration.modelId && model.active)) throw new AgenticRunConfigurationError("unsupported_model", `Agentic Run model "${configuration.modelId}" is not supported for provider "${configuration.provider}".`);
|
|
1241
|
+
return {
|
|
1242
|
+
configuration,
|
|
1243
|
+
provider,
|
|
1244
|
+
createSession: createTracingSessionFactory({
|
|
1245
|
+
agentConfigurationVersion: configuration.agentConfigurationVersion,
|
|
1246
|
+
model: configuration.modelId,
|
|
1247
|
+
provider: configuration.provider,
|
|
1248
|
+
transport: createTracingTransport({
|
|
1249
|
+
agentApiKey,
|
|
1250
|
+
apiUrl: apiUrl ?? "https://api.agenticrun.de/v1",
|
|
1251
|
+
fetch: options.fetch ?? globalThis.fetch,
|
|
1252
|
+
timeoutMs: tracingTimeoutMs
|
|
1253
|
+
})
|
|
1254
|
+
})
|
|
1255
|
+
};
|
|
1256
|
+
};
|
|
1257
|
+
//#endregion
|
|
1258
|
+
export { safeJson as a, tokenUsageSchema as c, loadAgentRuntimeConfiguration as d, omittedContent as i, tracingLimits as l, createAgenticRunClient as n, validatedAttributes as o, isSensitiveAttributeKey as r, validatedContent as s, AgenticRunConfigurationError as t, AgenticRunApiError as u };
|