robotrock 0.8.1 → 0.8.4

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.
@@ -2,12 +2,189 @@
2
2
  import { task, wait } from "@trigger.dev/sdk";
3
3
 
4
4
  // src/schemas/index.ts
5
+ import { z as z2 } from "zod";
6
+
7
+ // ../core/src/utils/safe-url.ts
8
+ var BLOCKED_HOSTNAMES = /* @__PURE__ */ new Set([
9
+ "localhost",
10
+ "metadata.google.internal",
11
+ "metadata.goog"
12
+ ]);
13
+ function normalizeHostname(hostname) {
14
+ return hostname.toLowerCase().replace(/^\[|\]$/g, "");
15
+ }
16
+ function isBlockedIpv4(host) {
17
+ const match = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(host);
18
+ if (!match) {
19
+ return false;
20
+ }
21
+ const octets = match.slice(1, 5).map((part) => Number(part));
22
+ if (octets.some((octet) => Number.isNaN(octet) || octet < 0 || octet > 255)) {
23
+ return true;
24
+ }
25
+ const [a, b, _c, _d] = octets;
26
+ if (a === void 0 || b === void 0) {
27
+ return true;
28
+ }
29
+ if (a === 127 || a === 0) return true;
30
+ if (a === 10) return true;
31
+ if (a === 169 && b === 254) return true;
32
+ if (a === 192 && b === 168) return true;
33
+ if (a === 172 && b >= 16 && b <= 31) return true;
34
+ if (a === 100 && b >= 64 && b <= 127) return true;
35
+ return false;
36
+ }
37
+ function ipv4FromNumericHost(host) {
38
+ if (/^\d+$/.test(host)) {
39
+ const num = Number(host);
40
+ if (!Number.isFinite(num) || num < 0 || num > 4294967295) {
41
+ return null;
42
+ }
43
+ const a = num >>> 24 & 255;
44
+ const b = num >>> 16 & 255;
45
+ const c = num >>> 8 & 255;
46
+ const d = num & 255;
47
+ return `${a}.${b}.${c}.${d}`;
48
+ }
49
+ const hexMatch = /^0x([0-9a-f]{1,8})$/i.exec(host);
50
+ if (hexMatch) {
51
+ const num = Number.parseInt(hexMatch[1], 16);
52
+ if (!Number.isFinite(num) || num < 0 || num > 4294967295) {
53
+ return null;
54
+ }
55
+ const a = num >>> 24 & 255;
56
+ const b = num >>> 16 & 255;
57
+ const c = num >>> 8 & 255;
58
+ const d = num & 255;
59
+ return `${a}.${b}.${c}.${d}`;
60
+ }
61
+ return null;
62
+ }
63
+ function isBlockedIpv4Mapped(host) {
64
+ const mappedDotted = /^::ffff:(\d{1,3}(?:\.\d{1,3}){3})$/i.exec(host);
65
+ if (mappedDotted) {
66
+ return isBlockedIpv4(mappedDotted[1]);
67
+ }
68
+ const mappedHex = /^::ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/i.exec(host);
69
+ if (mappedHex) {
70
+ const high = Number.parseInt(mappedHex[1], 16);
71
+ const low = Number.parseInt(mappedHex[2], 16);
72
+ if (Number.isFinite(high) && Number.isFinite(low)) {
73
+ const a = high >> 8 & 255;
74
+ const b = high & 255;
75
+ const c = low >> 8 & 255;
76
+ const d = low & 255;
77
+ return isBlockedIpv4(`${a}.${b}.${c}.${d}`);
78
+ }
79
+ }
80
+ return false;
81
+ }
82
+ function isBlockedIpv6(host) {
83
+ if (host === "::1" || host === "0:0:0:0:0:0:0:1") {
84
+ return true;
85
+ }
86
+ if (host.startsWith("fc") || host.startsWith("fd")) {
87
+ return true;
88
+ }
89
+ if (host.startsWith("fe80:")) {
90
+ return true;
91
+ }
92
+ if (isBlockedIpv4Mapped(host)) {
93
+ return true;
94
+ }
95
+ return false;
96
+ }
97
+ function isBlockedHostname(hostname) {
98
+ const host = normalizeHostname(hostname);
99
+ if (!host) {
100
+ return true;
101
+ }
102
+ if (BLOCKED_HOSTNAMES.has(host)) {
103
+ return true;
104
+ }
105
+ if (host.endsWith(".localhost") || host.endsWith(".local")) {
106
+ return true;
107
+ }
108
+ const numericIpv4 = ipv4FromNumericHost(host);
109
+ if (numericIpv4 && isBlockedIpv4(numericIpv4)) {
110
+ return true;
111
+ }
112
+ return isBlockedIpv4(host) || isBlockedIpv6(host);
113
+ }
114
+ function isPublicHttpUrl(urlString) {
115
+ let url;
116
+ try {
117
+ url = new URL(urlString);
118
+ } catch {
119
+ return false;
120
+ }
121
+ if (url.protocol !== "http:" && url.protocol !== "https:") {
122
+ return false;
123
+ }
124
+ if (url.username || url.password) {
125
+ return false;
126
+ }
127
+ return !isBlockedHostname(url.hostname);
128
+ }
129
+ var PUBLIC_HTTP_URL_ERROR = "URL must be a public http:// or https:// address";
130
+
131
+ // ../core/src/schemas/task.ts
5
132
  import { z } from "zod";
6
- import {
7
- isPublicHttpUrl,
8
- PUBLIC_HTTP_URL_ERROR
9
- } from "@robotrock/core/utils";
10
- import { validateContextPublicUrls } from "@robotrock/core/schemas";
133
+
134
+ // ../core/src/schemas/context-urls.ts
135
+ function resolveLinkUrl(value) {
136
+ return value.startsWith("http://") || value.startsWith("https://") ? value : `https://${value}`;
137
+ }
138
+ function widgetType(ui, field) {
139
+ const fieldUi = ui?.[field];
140
+ if (typeof fieldUi !== "object" || fieldUi === null) {
141
+ return void 0;
142
+ }
143
+ const widget = fieldUi["ui:widget"];
144
+ return typeof widget === "string" ? widget : void 0;
145
+ }
146
+ function validateUrlValue(value, widget) {
147
+ if (widget === "image" || widget === "link") {
148
+ if (typeof value !== "string") {
149
+ return PUBLIC_HTTP_URL_ERROR;
150
+ }
151
+ const url = widget === "link" ? resolveLinkUrl(value) : value;
152
+ if (!isPublicHttpUrl(url)) {
153
+ return PUBLIC_HTTP_URL_ERROR;
154
+ }
155
+ return null;
156
+ }
157
+ if (widget === "attachments" && Array.isArray(value)) {
158
+ for (const item of value) {
159
+ if (typeof item !== "object" || item === null) {
160
+ continue;
161
+ }
162
+ const url = item.url;
163
+ if (typeof url === "string" && !isPublicHttpUrl(resolveLinkUrl(url))) {
164
+ return PUBLIC_HTTP_URL_ERROR;
165
+ }
166
+ }
167
+ }
168
+ return null;
169
+ }
170
+ function validateContextPublicUrls(context) {
171
+ if (!context?.data) {
172
+ return null;
173
+ }
174
+ for (const [field, value] of Object.entries(context.data)) {
175
+ const widget = widgetType(context.ui, field);
176
+ if (!widget) {
177
+ continue;
178
+ }
179
+ const error = validateUrlValue(value, widget);
180
+ if (error) {
181
+ return `context.data.${field}: ${error}`;
182
+ }
183
+ }
184
+ return null;
185
+ }
186
+
187
+ // ../core/src/schemas/task.ts
11
188
  var safeUrlSchema = z.string().refine((url) => isPublicHttpUrl(url), {
12
189
  message: PUBLIC_HTTP_URL_ERROR
13
190
  });
@@ -35,6 +212,7 @@ var taskActionSchema = z.object({
35
212
  schema: jsonSchema7Schema.optional(),
36
213
  ui: uiSchemaSchema.optional(),
37
214
  data: z.record(z.string(), z.unknown()).optional(),
215
+ // Optional handlers for this action - if present, must have at least 1
38
216
  handlers: z.array(handlerSchema).min(1).optional()
39
217
  });
40
218
  var uiFieldSchemaSchema = z.object({
@@ -50,6 +228,7 @@ var contextDataSchema = z.object({
50
228
  ui: contextUiSchema
51
229
  }).optional();
52
230
  var taskContextObjectSchema = z.object({
231
+ /** Source application id; omitted tasks are grouped in the default inbox. */
53
232
  app: z.string().min(1).optional(),
54
233
  type: z.string().min(1),
55
234
  name: z.string().min(1),
@@ -114,17 +293,47 @@ var agentCostSchema = z.object({
114
293
  usd: z.number().nonnegative().optional()
115
294
  });
116
295
  var AGENT_INFO_MAX_KEYS = 32;
296
+ var AGENT_TOOL_CALLS_MAX_KEYS = 64;
297
+ var AGENT_OTEL_SPANS_MAX = 32;
298
+ var agentToolCallsSchema = z.record(z.string().min(1), nonNegativeInt);
299
+ var agentOtelSpanStatusSchema = z.enum(["ok", "error"]);
300
+ var agentOtelSpanSummarySchema = z.object({
301
+ name: z.string().min(1),
302
+ durationMs: z.number().nonnegative(),
303
+ status: agentOtelSpanStatusSchema
304
+ });
305
+ var agentOtelSchema = z.object({
306
+ traceId: z.string().min(1),
307
+ spanId: z.string().min(1).optional(),
308
+ rootDurationMs: z.number().nonnegative().optional(),
309
+ spans: z.array(agentOtelSpanSummarySchema).max(AGENT_OTEL_SPANS_MAX).optional()
310
+ });
117
311
  var agentTelemetrySchema = z.object({
312
+ /** Agent or app version (semver, git SHA, deploy tag — caller-defined). */
118
313
  version: z.string().min(1).optional(),
314
+ /** Total tool invocations. When omitted but `toolCalls` is set, the sum of counts is used. */
119
315
  toolCallCount: nonNegativeInt.optional(),
316
+ /** Per-tool invocation counts keyed by tool name. */
317
+ toolCalls: agentToolCallsSchema.optional(),
120
318
  cost: agentCostSchema.optional(),
121
- info: z.record(z.string(), z.unknown()).optional()
319
+ /** Free-form metadata for experiments (model, prompt variant, etc.). */
320
+ info: z.record(z.string(), z.unknown()).optional(),
321
+ /** Structured OpenTelemetry span snapshot for feedback analysis. */
322
+ otel: agentOtelSchema.optional()
122
323
  }).refine(
123
324
  (data) => {
124
325
  const keys = data.info ? Object.keys(data.info) : [];
125
326
  return keys.length <= AGENT_INFO_MAX_KEYS;
126
327
  },
127
328
  { message: `agent.info may contain at most ${AGENT_INFO_MAX_KEYS} keys` }
329
+ ).refine(
330
+ (data) => {
331
+ const keys = data.toolCalls ? Object.keys(data.toolCalls) : [];
332
+ return keys.length <= AGENT_TOOL_CALLS_MAX_KEYS;
333
+ },
334
+ {
335
+ message: `agent.toolCalls may contain at most ${AGENT_TOOL_CALLS_MAX_KEYS} tool names`
336
+ }
128
337
  );
129
338
  var createTaskBodySchema = taskContextObjectSchema.extend({
130
339
  assignTo: assignToSchema.optional(),
@@ -143,9 +352,174 @@ var createTaskBodySchema = taskContextObjectSchema.extend({
143
352
  * the inbox status bar and the thread update log.
144
353
  */
145
354
  update: threadUpdateInputSchema.optional(),
355
+ /** Agent telemetry (version, cost, tool calls) — not shown in inbox UI. */
146
356
  agent: agentTelemetrySchema.optional()
147
357
  }).superRefine(refineContextPublicUrls);
148
- var threadUpdateBodySchema = threadUpdateInputSchema;
358
+
359
+ // src/schemas/index.ts
360
+ var safeUrlSchema2 = z2.string().refine((url) => isPublicHttpUrl(url), {
361
+ message: PUBLIC_HTTP_URL_ERROR
362
+ });
363
+ var jsonSchema7Schema2 = z2.custom(
364
+ (val) => typeof val === "object" && val !== null,
365
+ { message: "Must be a valid JSON Schema object" }
366
+ );
367
+ var uiSchemaSchema2 = z2.custom((val) => typeof val === "object" && val !== null, {
368
+ message: "Must be a valid UiSchema object"
369
+ });
370
+ var webhookHandlerSchema2 = z2.object({
371
+ type: z2.literal("webhook"),
372
+ url: safeUrlSchema2,
373
+ headers: z2.record(z2.string(), z2.string())
374
+ });
375
+ var triggerHandlerSchema2 = webhookHandlerSchema2.extend({
376
+ type: z2.literal("trigger"),
377
+ tokenId: z2.string().min(1)
378
+ });
379
+ var handlerSchema2 = z2.discriminatedUnion("type", [webhookHandlerSchema2, triggerHandlerSchema2]);
380
+ var taskActionSchema2 = z2.object({
381
+ id: z2.string().min(1),
382
+ title: z2.string().min(1),
383
+ description: z2.string().optional(),
384
+ schema: jsonSchema7Schema2.optional(),
385
+ ui: uiSchemaSchema2.optional(),
386
+ data: z2.record(z2.string(), z2.unknown()).optional(),
387
+ handlers: z2.array(handlerSchema2).min(1).optional()
388
+ });
389
+ var uiFieldSchemaSchema2 = z2.object({
390
+ "ui:widget": z2.string().optional(),
391
+ "ui:title": z2.string().optional(),
392
+ "ui:description": z2.string().optional(),
393
+ "ui:options": z2.record(z2.string(), z2.unknown()).optional(),
394
+ items: z2.lazy(() => z2.record(z2.string(), uiFieldSchemaSchema2)).optional()
395
+ }).passthrough();
396
+ var contextUiSchema2 = z2.record(z2.string(), uiFieldSchemaSchema2).optional();
397
+ var contextDataSchema2 = z2.object({
398
+ data: z2.record(z2.string(), z2.unknown()),
399
+ ui: contextUiSchema2
400
+ }).optional();
401
+ var taskContextObjectSchema2 = z2.object({
402
+ app: z2.string().min(1).optional(),
403
+ type: z2.string().min(1),
404
+ name: z2.string().min(1),
405
+ description: z2.string().optional(),
406
+ validUntil: z2.string().optional(),
407
+ context: contextDataSchema2,
408
+ version: z2.literal(2).optional(),
409
+ actions: z2.array(taskActionSchema2).min(1, "At least one action is required")
410
+ });
411
+ function refineContextPublicUrls2(data, ctx) {
412
+ const error = validateContextPublicUrls(data.context);
413
+ if (error) {
414
+ ctx.addIssue({
415
+ code: z2.ZodIssueCode.custom,
416
+ message: error,
417
+ path: ["context"]
418
+ });
419
+ }
420
+ }
421
+ var taskContextSchema2 = taskContextObjectSchema2.superRefine(
422
+ refineContextPublicUrls2
423
+ );
424
+ var assignToSchema2 = z2.object({
425
+ users: z2.array(z2.string().email()).optional(),
426
+ groups: z2.array(z2.string().min(1)).optional()
427
+ }).refine(
428
+ (data) => {
429
+ const groups = data.groups ?? [];
430
+ if (groups.includes("all") && groups.length > 1) {
431
+ return false;
432
+ }
433
+ return true;
434
+ },
435
+ { message: 'Cannot combine "all" with other group slugs' }
436
+ );
437
+ var threadUpdateMessageSchema2 = z2.string().min(1).max(500);
438
+ var threadUpdateStatuses2 = [
439
+ "info",
440
+ "queued",
441
+ "running",
442
+ "waiting",
443
+ "succeeded",
444
+ "failed",
445
+ "cancelled"
446
+ ];
447
+ var threadUpdateStatusSchema2 = z2.enum(threadUpdateStatuses2);
448
+ var threadUpdateInputSchema2 = z2.object({
449
+ message: threadUpdateMessageSchema2,
450
+ status: threadUpdateStatusSchema2.optional()
451
+ });
452
+ var taskPriorities2 = ["low", "normal", "high", "urgent"];
453
+ var taskPrioritySchema2 = z2.enum(taskPriorities2);
454
+ var nonNegativeInt2 = z2.number().int().nonnegative();
455
+ var agentCostTokensSchema2 = z2.object({
456
+ input: nonNegativeInt2.optional(),
457
+ output: nonNegativeInt2.optional(),
458
+ total: nonNegativeInt2.optional()
459
+ });
460
+ var agentCostSchema2 = z2.object({
461
+ tokens: agentCostTokensSchema2.optional(),
462
+ eur: z2.number().nonnegative().optional(),
463
+ usd: z2.number().nonnegative().optional()
464
+ });
465
+ var AGENT_INFO_MAX_KEYS2 = 32;
466
+ var AGENT_TOOL_CALLS_MAX_KEYS2 = 64;
467
+ var AGENT_OTEL_SPANS_MAX2 = 32;
468
+ var agentToolCallsSchema2 = z2.record(z2.string().min(1), nonNegativeInt2);
469
+ var agentOtelSpanStatusSchema2 = z2.enum(["ok", "error"]);
470
+ var agentOtelSpanSummarySchema2 = z2.object({
471
+ name: z2.string().min(1),
472
+ durationMs: z2.number().nonnegative(),
473
+ status: agentOtelSpanStatusSchema2
474
+ });
475
+ var agentOtelSchema2 = z2.object({
476
+ traceId: z2.string().min(1),
477
+ spanId: z2.string().min(1).optional(),
478
+ rootDurationMs: z2.number().nonnegative().optional(),
479
+ spans: z2.array(agentOtelSpanSummarySchema2).max(AGENT_OTEL_SPANS_MAX2).optional()
480
+ });
481
+ var agentTelemetrySchema2 = z2.object({
482
+ version: z2.string().min(1).optional(),
483
+ toolCallCount: nonNegativeInt2.optional(),
484
+ toolCalls: agentToolCallsSchema2.optional(),
485
+ cost: agentCostSchema2.optional(),
486
+ info: z2.record(z2.string(), z2.unknown()).optional(),
487
+ otel: agentOtelSchema2.optional()
488
+ }).refine(
489
+ (data) => {
490
+ const keys = data.info ? Object.keys(data.info) : [];
491
+ return keys.length <= AGENT_INFO_MAX_KEYS2;
492
+ },
493
+ { message: `agent.info may contain at most ${AGENT_INFO_MAX_KEYS2} keys` }
494
+ ).refine(
495
+ (data) => {
496
+ const keys = data.toolCalls ? Object.keys(data.toolCalls) : [];
497
+ return keys.length <= AGENT_TOOL_CALLS_MAX_KEYS2;
498
+ },
499
+ {
500
+ message: `agent.toolCalls may contain at most ${AGENT_TOOL_CALLS_MAX_KEYS2} tool names`
501
+ }
502
+ );
503
+ var createTaskBodySchema2 = taskContextObjectSchema2.extend({
504
+ assignTo: assignToSchema2.optional(),
505
+ /**
506
+ * Groups related tasks together. When omitted, the server generates one and
507
+ * returns it so the caller can reuse it on later tasks in the same thread.
508
+ */
509
+ threadId: z2.string().min(1).optional(),
510
+ /**
511
+ * Optional thread priority. When set, applies to the whole thread and
512
+ * overwrites any previous priority. Omit on later tasks to leave unchanged.
513
+ */
514
+ priority: taskPrioritySchema2.optional(),
515
+ /**
516
+ * Optional initial status update logged against the task's thread. Shows in
517
+ * the inbox status bar and the thread update log.
518
+ */
519
+ update: threadUpdateInputSchema2.optional(),
520
+ agent: agentTelemetrySchema2.optional()
521
+ }).superRefine(refineContextPublicUrls2);
522
+ var threadUpdateBodySchema = threadUpdateInputSchema2;
149
523
 
150
524
  // src/approval-result.ts
151
525
  var TaskTimeoutError = class extends Error {
@@ -259,7 +633,7 @@ var RobotRock = class {
259
633
  ...task2.update !== void 0 ? { update: task2.update } : {},
260
634
  ...task2.agent !== void 0 ? { agent: task2.agent } : {}
261
635
  };
262
- const validation = createTaskBodySchema.safeParse(bodyPayload);
636
+ const validation = createTaskBodySchema2.safeParse(bodyPayload);
263
637
  if (!validation.success) {
264
638
  throw new RobotRockError(
265
639
  `Invalid task: ${validation.error.issues[0]?.message}`,