robotrock 0.8.1 → 0.8.3

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,10 +293,17 @@ 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 agentToolCallsSchema = z.record(z.string().min(1), nonNegativeInt);
117
298
  var agentTelemetrySchema = z.object({
299
+ /** Agent or app version (semver, git SHA, deploy tag — caller-defined). */
118
300
  version: z.string().min(1).optional(),
301
+ /** Total tool invocations. When omitted but `toolCalls` is set, the sum of counts is used. */
119
302
  toolCallCount: nonNegativeInt.optional(),
303
+ /** Per-tool invocation counts keyed by tool name. */
304
+ toolCalls: agentToolCallsSchema.optional(),
120
305
  cost: agentCostSchema.optional(),
306
+ /** Free-form metadata for experiments (model, prompt variant, etc.). */
121
307
  info: z.record(z.string(), z.unknown()).optional()
122
308
  }).refine(
123
309
  (data) => {
@@ -125,6 +311,14 @@ var agentTelemetrySchema = z.object({
125
311
  return keys.length <= AGENT_INFO_MAX_KEYS;
126
312
  },
127
313
  { message: `agent.info may contain at most ${AGENT_INFO_MAX_KEYS} keys` }
314
+ ).refine(
315
+ (data) => {
316
+ const keys = data.toolCalls ? Object.keys(data.toolCalls) : [];
317
+ return keys.length <= AGENT_TOOL_CALLS_MAX_KEYS;
318
+ },
319
+ {
320
+ message: `agent.toolCalls may contain at most ${AGENT_TOOL_CALLS_MAX_KEYS} tool names`
321
+ }
128
322
  );
129
323
  var createTaskBodySchema = taskContextObjectSchema.extend({
130
324
  assignTo: assignToSchema.optional(),
@@ -143,9 +337,160 @@ var createTaskBodySchema = taskContextObjectSchema.extend({
143
337
  * the inbox status bar and the thread update log.
144
338
  */
145
339
  update: threadUpdateInputSchema.optional(),
340
+ /** Agent telemetry (version, cost, tool calls) — not shown in inbox UI. */
146
341
  agent: agentTelemetrySchema.optional()
147
342
  }).superRefine(refineContextPublicUrls);
148
- var threadUpdateBodySchema = threadUpdateInputSchema;
343
+
344
+ // src/schemas/index.ts
345
+ var safeUrlSchema2 = z2.string().refine((url) => isPublicHttpUrl(url), {
346
+ message: PUBLIC_HTTP_URL_ERROR
347
+ });
348
+ var jsonSchema7Schema2 = z2.custom(
349
+ (val) => typeof val === "object" && val !== null,
350
+ { message: "Must be a valid JSON Schema object" }
351
+ );
352
+ var uiSchemaSchema2 = z2.custom((val) => typeof val === "object" && val !== null, {
353
+ message: "Must be a valid UiSchema object"
354
+ });
355
+ var webhookHandlerSchema2 = z2.object({
356
+ type: z2.literal("webhook"),
357
+ url: safeUrlSchema2,
358
+ headers: z2.record(z2.string(), z2.string())
359
+ });
360
+ var triggerHandlerSchema2 = webhookHandlerSchema2.extend({
361
+ type: z2.literal("trigger"),
362
+ tokenId: z2.string().min(1)
363
+ });
364
+ var handlerSchema2 = z2.discriminatedUnion("type", [webhookHandlerSchema2, triggerHandlerSchema2]);
365
+ var taskActionSchema2 = z2.object({
366
+ id: z2.string().min(1),
367
+ title: z2.string().min(1),
368
+ description: z2.string().optional(),
369
+ schema: jsonSchema7Schema2.optional(),
370
+ ui: uiSchemaSchema2.optional(),
371
+ data: z2.record(z2.string(), z2.unknown()).optional(),
372
+ handlers: z2.array(handlerSchema2).min(1).optional()
373
+ });
374
+ var uiFieldSchemaSchema2 = z2.object({
375
+ "ui:widget": z2.string().optional(),
376
+ "ui:title": z2.string().optional(),
377
+ "ui:description": z2.string().optional(),
378
+ "ui:options": z2.record(z2.string(), z2.unknown()).optional(),
379
+ items: z2.lazy(() => z2.record(z2.string(), uiFieldSchemaSchema2)).optional()
380
+ }).passthrough();
381
+ var contextUiSchema2 = z2.record(z2.string(), uiFieldSchemaSchema2).optional();
382
+ var contextDataSchema2 = z2.object({
383
+ data: z2.record(z2.string(), z2.unknown()),
384
+ ui: contextUiSchema2
385
+ }).optional();
386
+ var taskContextObjectSchema2 = z2.object({
387
+ app: z2.string().min(1).optional(),
388
+ type: z2.string().min(1),
389
+ name: z2.string().min(1),
390
+ description: z2.string().optional(),
391
+ validUntil: z2.string().optional(),
392
+ context: contextDataSchema2,
393
+ version: z2.literal(2).optional(),
394
+ actions: z2.array(taskActionSchema2).min(1, "At least one action is required")
395
+ });
396
+ function refineContextPublicUrls2(data, ctx) {
397
+ const error = validateContextPublicUrls(data.context);
398
+ if (error) {
399
+ ctx.addIssue({
400
+ code: z2.ZodIssueCode.custom,
401
+ message: error,
402
+ path: ["context"]
403
+ });
404
+ }
405
+ }
406
+ var taskContextSchema2 = taskContextObjectSchema2.superRefine(
407
+ refineContextPublicUrls2
408
+ );
409
+ var assignToSchema2 = z2.object({
410
+ users: z2.array(z2.string().email()).optional(),
411
+ groups: z2.array(z2.string().min(1)).optional()
412
+ }).refine(
413
+ (data) => {
414
+ const groups = data.groups ?? [];
415
+ if (groups.includes("all") && groups.length > 1) {
416
+ return false;
417
+ }
418
+ return true;
419
+ },
420
+ { message: 'Cannot combine "all" with other group slugs' }
421
+ );
422
+ var threadUpdateMessageSchema2 = z2.string().min(1).max(500);
423
+ var threadUpdateStatuses2 = [
424
+ "info",
425
+ "queued",
426
+ "running",
427
+ "waiting",
428
+ "succeeded",
429
+ "failed",
430
+ "cancelled"
431
+ ];
432
+ var threadUpdateStatusSchema2 = z2.enum(threadUpdateStatuses2);
433
+ var threadUpdateInputSchema2 = z2.object({
434
+ message: threadUpdateMessageSchema2,
435
+ status: threadUpdateStatusSchema2.optional()
436
+ });
437
+ var taskPriorities2 = ["low", "normal", "high", "urgent"];
438
+ var taskPrioritySchema2 = z2.enum(taskPriorities2);
439
+ var nonNegativeInt2 = z2.number().int().nonnegative();
440
+ var agentCostTokensSchema2 = z2.object({
441
+ input: nonNegativeInt2.optional(),
442
+ output: nonNegativeInt2.optional(),
443
+ total: nonNegativeInt2.optional()
444
+ });
445
+ var agentCostSchema2 = z2.object({
446
+ tokens: agentCostTokensSchema2.optional(),
447
+ eur: z2.number().nonnegative().optional(),
448
+ usd: z2.number().nonnegative().optional()
449
+ });
450
+ var AGENT_INFO_MAX_KEYS2 = 32;
451
+ var AGENT_TOOL_CALLS_MAX_KEYS2 = 64;
452
+ var agentToolCallsSchema2 = z2.record(z2.string().min(1), nonNegativeInt2);
453
+ var agentTelemetrySchema2 = z2.object({
454
+ version: z2.string().min(1).optional(),
455
+ toolCallCount: nonNegativeInt2.optional(),
456
+ toolCalls: agentToolCallsSchema2.optional(),
457
+ cost: agentCostSchema2.optional(),
458
+ info: z2.record(z2.string(), z2.unknown()).optional()
459
+ }).refine(
460
+ (data) => {
461
+ const keys = data.info ? Object.keys(data.info) : [];
462
+ return keys.length <= AGENT_INFO_MAX_KEYS2;
463
+ },
464
+ { message: `agent.info may contain at most ${AGENT_INFO_MAX_KEYS2} keys` }
465
+ ).refine(
466
+ (data) => {
467
+ const keys = data.toolCalls ? Object.keys(data.toolCalls) : [];
468
+ return keys.length <= AGENT_TOOL_CALLS_MAX_KEYS2;
469
+ },
470
+ {
471
+ message: `agent.toolCalls may contain at most ${AGENT_TOOL_CALLS_MAX_KEYS2} tool names`
472
+ }
473
+ );
474
+ var createTaskBodySchema2 = taskContextObjectSchema2.extend({
475
+ assignTo: assignToSchema2.optional(),
476
+ /**
477
+ * Groups related tasks together. When omitted, the server generates one and
478
+ * returns it so the caller can reuse it on later tasks in the same thread.
479
+ */
480
+ threadId: z2.string().min(1).optional(),
481
+ /**
482
+ * Optional thread priority. When set, applies to the whole thread and
483
+ * overwrites any previous priority. Omit on later tasks to leave unchanged.
484
+ */
485
+ priority: taskPrioritySchema2.optional(),
486
+ /**
487
+ * Optional initial status update logged against the task's thread. Shows in
488
+ * the inbox status bar and the thread update log.
489
+ */
490
+ update: threadUpdateInputSchema2.optional(),
491
+ agent: agentTelemetrySchema2.optional()
492
+ }).superRefine(refineContextPublicUrls2);
493
+ var threadUpdateBodySchema = threadUpdateInputSchema2;
149
494
 
150
495
  // src/approval-result.ts
151
496
  var TaskTimeoutError = class extends Error {
@@ -259,7 +604,7 @@ var RobotRock = class {
259
604
  ...task2.update !== void 0 ? { update: task2.update } : {},
260
605
  ...task2.agent !== void 0 ? { agent: task2.agent } : {}
261
606
  };
262
- const validation = createTaskBodySchema.safeParse(bodyPayload);
607
+ const validation = createTaskBodySchema2.safeParse(bodyPayload);
263
608
  if (!validation.success) {
264
609
  throw new RobotRockError(
265
610
  `Invalid task: ${validation.error.issues[0]?.message}`,