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.
@@ -48,12 +48,189 @@ var __callDispose = (stack, error, hasError) => {
48
48
  import { createWebhook, sleep as sleep2 } from "workflow";
49
49
 
50
50
  // src/schemas/index.ts
51
+ import { z as z2 } from "zod";
52
+
53
+ // ../core/src/utils/safe-url.ts
54
+ var BLOCKED_HOSTNAMES = /* @__PURE__ */ new Set([
55
+ "localhost",
56
+ "metadata.google.internal",
57
+ "metadata.goog"
58
+ ]);
59
+ function normalizeHostname(hostname) {
60
+ return hostname.toLowerCase().replace(/^\[|\]$/g, "");
61
+ }
62
+ function isBlockedIpv4(host) {
63
+ const match = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(host);
64
+ if (!match) {
65
+ return false;
66
+ }
67
+ const octets = match.slice(1, 5).map((part) => Number(part));
68
+ if (octets.some((octet) => Number.isNaN(octet) || octet < 0 || octet > 255)) {
69
+ return true;
70
+ }
71
+ const [a, b, _c, _d] = octets;
72
+ if (a === void 0 || b === void 0) {
73
+ return true;
74
+ }
75
+ if (a === 127 || a === 0) return true;
76
+ if (a === 10) return true;
77
+ if (a === 169 && b === 254) return true;
78
+ if (a === 192 && b === 168) return true;
79
+ if (a === 172 && b >= 16 && b <= 31) return true;
80
+ if (a === 100 && b >= 64 && b <= 127) return true;
81
+ return false;
82
+ }
83
+ function ipv4FromNumericHost(host) {
84
+ if (/^\d+$/.test(host)) {
85
+ const num = Number(host);
86
+ if (!Number.isFinite(num) || num < 0 || num > 4294967295) {
87
+ return null;
88
+ }
89
+ const a = num >>> 24 & 255;
90
+ const b = num >>> 16 & 255;
91
+ const c = num >>> 8 & 255;
92
+ const d = num & 255;
93
+ return `${a}.${b}.${c}.${d}`;
94
+ }
95
+ const hexMatch = /^0x([0-9a-f]{1,8})$/i.exec(host);
96
+ if (hexMatch) {
97
+ const num = Number.parseInt(hexMatch[1], 16);
98
+ if (!Number.isFinite(num) || num < 0 || num > 4294967295) {
99
+ return null;
100
+ }
101
+ const a = num >>> 24 & 255;
102
+ const b = num >>> 16 & 255;
103
+ const c = num >>> 8 & 255;
104
+ const d = num & 255;
105
+ return `${a}.${b}.${c}.${d}`;
106
+ }
107
+ return null;
108
+ }
109
+ function isBlockedIpv4Mapped(host) {
110
+ const mappedDotted = /^::ffff:(\d{1,3}(?:\.\d{1,3}){3})$/i.exec(host);
111
+ if (mappedDotted) {
112
+ return isBlockedIpv4(mappedDotted[1]);
113
+ }
114
+ const mappedHex = /^::ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/i.exec(host);
115
+ if (mappedHex) {
116
+ const high = Number.parseInt(mappedHex[1], 16);
117
+ const low = Number.parseInt(mappedHex[2], 16);
118
+ if (Number.isFinite(high) && Number.isFinite(low)) {
119
+ const a = high >> 8 & 255;
120
+ const b = high & 255;
121
+ const c = low >> 8 & 255;
122
+ const d = low & 255;
123
+ return isBlockedIpv4(`${a}.${b}.${c}.${d}`);
124
+ }
125
+ }
126
+ return false;
127
+ }
128
+ function isBlockedIpv6(host) {
129
+ if (host === "::1" || host === "0:0:0:0:0:0:0:1") {
130
+ return true;
131
+ }
132
+ if (host.startsWith("fc") || host.startsWith("fd")) {
133
+ return true;
134
+ }
135
+ if (host.startsWith("fe80:")) {
136
+ return true;
137
+ }
138
+ if (isBlockedIpv4Mapped(host)) {
139
+ return true;
140
+ }
141
+ return false;
142
+ }
143
+ function isBlockedHostname(hostname) {
144
+ const host = normalizeHostname(hostname);
145
+ if (!host) {
146
+ return true;
147
+ }
148
+ if (BLOCKED_HOSTNAMES.has(host)) {
149
+ return true;
150
+ }
151
+ if (host.endsWith(".localhost") || host.endsWith(".local")) {
152
+ return true;
153
+ }
154
+ const numericIpv4 = ipv4FromNumericHost(host);
155
+ if (numericIpv4 && isBlockedIpv4(numericIpv4)) {
156
+ return true;
157
+ }
158
+ return isBlockedIpv4(host) || isBlockedIpv6(host);
159
+ }
160
+ function isPublicHttpUrl(urlString) {
161
+ let url;
162
+ try {
163
+ url = new URL(urlString);
164
+ } catch {
165
+ return false;
166
+ }
167
+ if (url.protocol !== "http:" && url.protocol !== "https:") {
168
+ return false;
169
+ }
170
+ if (url.username || url.password) {
171
+ return false;
172
+ }
173
+ return !isBlockedHostname(url.hostname);
174
+ }
175
+ var PUBLIC_HTTP_URL_ERROR = "URL must be a public http:// or https:// address";
176
+
177
+ // ../core/src/schemas/task.ts
51
178
  import { z } from "zod";
52
- import {
53
- isPublicHttpUrl,
54
- PUBLIC_HTTP_URL_ERROR
55
- } from "@robotrock/core/utils";
56
- import { validateContextPublicUrls } from "@robotrock/core/schemas";
179
+
180
+ // ../core/src/schemas/context-urls.ts
181
+ function resolveLinkUrl(value) {
182
+ return value.startsWith("http://") || value.startsWith("https://") ? value : `https://${value}`;
183
+ }
184
+ function widgetType(ui, field) {
185
+ const fieldUi = ui?.[field];
186
+ if (typeof fieldUi !== "object" || fieldUi === null) {
187
+ return void 0;
188
+ }
189
+ const widget = fieldUi["ui:widget"];
190
+ return typeof widget === "string" ? widget : void 0;
191
+ }
192
+ function validateUrlValue(value, widget) {
193
+ if (widget === "image" || widget === "link") {
194
+ if (typeof value !== "string") {
195
+ return PUBLIC_HTTP_URL_ERROR;
196
+ }
197
+ const url = widget === "link" ? resolveLinkUrl(value) : value;
198
+ if (!isPublicHttpUrl(url)) {
199
+ return PUBLIC_HTTP_URL_ERROR;
200
+ }
201
+ return null;
202
+ }
203
+ if (widget === "attachments" && Array.isArray(value)) {
204
+ for (const item of value) {
205
+ if (typeof item !== "object" || item === null) {
206
+ continue;
207
+ }
208
+ const url = item.url;
209
+ if (typeof url === "string" && !isPublicHttpUrl(resolveLinkUrl(url))) {
210
+ return PUBLIC_HTTP_URL_ERROR;
211
+ }
212
+ }
213
+ }
214
+ return null;
215
+ }
216
+ function validateContextPublicUrls(context) {
217
+ if (!context?.data) {
218
+ return null;
219
+ }
220
+ for (const [field, value] of Object.entries(context.data)) {
221
+ const widget = widgetType(context.ui, field);
222
+ if (!widget) {
223
+ continue;
224
+ }
225
+ const error = validateUrlValue(value, widget);
226
+ if (error) {
227
+ return `context.data.${field}: ${error}`;
228
+ }
229
+ }
230
+ return null;
231
+ }
232
+
233
+ // ../core/src/schemas/task.ts
57
234
  var safeUrlSchema = z.string().refine((url) => isPublicHttpUrl(url), {
58
235
  message: PUBLIC_HTTP_URL_ERROR
59
236
  });
@@ -81,6 +258,7 @@ var taskActionSchema = z.object({
81
258
  schema: jsonSchema7Schema.optional(),
82
259
  ui: uiSchemaSchema.optional(),
83
260
  data: z.record(z.string(), z.unknown()).optional(),
261
+ // Optional handlers for this action - if present, must have at least 1
84
262
  handlers: z.array(handlerSchema).min(1).optional()
85
263
  });
86
264
  var uiFieldSchemaSchema = z.object({
@@ -96,6 +274,7 @@ var contextDataSchema = z.object({
96
274
  ui: contextUiSchema
97
275
  }).optional();
98
276
  var taskContextObjectSchema = z.object({
277
+ /** Source application id; omitted tasks are grouped in the default inbox. */
99
278
  app: z.string().min(1).optional(),
100
279
  type: z.string().min(1),
101
280
  name: z.string().min(1),
@@ -160,10 +339,17 @@ var agentCostSchema = z.object({
160
339
  usd: z.number().nonnegative().optional()
161
340
  });
162
341
  var AGENT_INFO_MAX_KEYS = 32;
342
+ var AGENT_TOOL_CALLS_MAX_KEYS = 64;
343
+ var agentToolCallsSchema = z.record(z.string().min(1), nonNegativeInt);
163
344
  var agentTelemetrySchema = z.object({
345
+ /** Agent or app version (semver, git SHA, deploy tag — caller-defined). */
164
346
  version: z.string().min(1).optional(),
347
+ /** Total tool invocations. When omitted but `toolCalls` is set, the sum of counts is used. */
165
348
  toolCallCount: nonNegativeInt.optional(),
349
+ /** Per-tool invocation counts keyed by tool name. */
350
+ toolCalls: agentToolCallsSchema.optional(),
166
351
  cost: agentCostSchema.optional(),
352
+ /** Free-form metadata for experiments (model, prompt variant, etc.). */
167
353
  info: z.record(z.string(), z.unknown()).optional()
168
354
  }).refine(
169
355
  (data) => {
@@ -171,6 +357,14 @@ var agentTelemetrySchema = z.object({
171
357
  return keys.length <= AGENT_INFO_MAX_KEYS;
172
358
  },
173
359
  { message: `agent.info may contain at most ${AGENT_INFO_MAX_KEYS} keys` }
360
+ ).refine(
361
+ (data) => {
362
+ const keys = data.toolCalls ? Object.keys(data.toolCalls) : [];
363
+ return keys.length <= AGENT_TOOL_CALLS_MAX_KEYS;
364
+ },
365
+ {
366
+ message: `agent.toolCalls may contain at most ${AGENT_TOOL_CALLS_MAX_KEYS} tool names`
367
+ }
174
368
  );
175
369
  var createTaskBodySchema = taskContextObjectSchema.extend({
176
370
  assignTo: assignToSchema.optional(),
@@ -189,9 +383,160 @@ var createTaskBodySchema = taskContextObjectSchema.extend({
189
383
  * the inbox status bar and the thread update log.
190
384
  */
191
385
  update: threadUpdateInputSchema.optional(),
386
+ /** Agent telemetry (version, cost, tool calls) — not shown in inbox UI. */
192
387
  agent: agentTelemetrySchema.optional()
193
388
  }).superRefine(refineContextPublicUrls);
194
- var threadUpdateBodySchema = threadUpdateInputSchema;
389
+
390
+ // src/schemas/index.ts
391
+ var safeUrlSchema2 = z2.string().refine((url) => isPublicHttpUrl(url), {
392
+ message: PUBLIC_HTTP_URL_ERROR
393
+ });
394
+ var jsonSchema7Schema2 = z2.custom(
395
+ (val) => typeof val === "object" && val !== null,
396
+ { message: "Must be a valid JSON Schema object" }
397
+ );
398
+ var uiSchemaSchema2 = z2.custom((val) => typeof val === "object" && val !== null, {
399
+ message: "Must be a valid UiSchema object"
400
+ });
401
+ var webhookHandlerSchema2 = z2.object({
402
+ type: z2.literal("webhook"),
403
+ url: safeUrlSchema2,
404
+ headers: z2.record(z2.string(), z2.string())
405
+ });
406
+ var triggerHandlerSchema2 = webhookHandlerSchema2.extend({
407
+ type: z2.literal("trigger"),
408
+ tokenId: z2.string().min(1)
409
+ });
410
+ var handlerSchema2 = z2.discriminatedUnion("type", [webhookHandlerSchema2, triggerHandlerSchema2]);
411
+ var taskActionSchema2 = z2.object({
412
+ id: z2.string().min(1),
413
+ title: z2.string().min(1),
414
+ description: z2.string().optional(),
415
+ schema: jsonSchema7Schema2.optional(),
416
+ ui: uiSchemaSchema2.optional(),
417
+ data: z2.record(z2.string(), z2.unknown()).optional(),
418
+ handlers: z2.array(handlerSchema2).min(1).optional()
419
+ });
420
+ var uiFieldSchemaSchema2 = z2.object({
421
+ "ui:widget": z2.string().optional(),
422
+ "ui:title": z2.string().optional(),
423
+ "ui:description": z2.string().optional(),
424
+ "ui:options": z2.record(z2.string(), z2.unknown()).optional(),
425
+ items: z2.lazy(() => z2.record(z2.string(), uiFieldSchemaSchema2)).optional()
426
+ }).passthrough();
427
+ var contextUiSchema2 = z2.record(z2.string(), uiFieldSchemaSchema2).optional();
428
+ var contextDataSchema2 = z2.object({
429
+ data: z2.record(z2.string(), z2.unknown()),
430
+ ui: contextUiSchema2
431
+ }).optional();
432
+ var taskContextObjectSchema2 = z2.object({
433
+ app: z2.string().min(1).optional(),
434
+ type: z2.string().min(1),
435
+ name: z2.string().min(1),
436
+ description: z2.string().optional(),
437
+ validUntil: z2.string().optional(),
438
+ context: contextDataSchema2,
439
+ version: z2.literal(2).optional(),
440
+ actions: z2.array(taskActionSchema2).min(1, "At least one action is required")
441
+ });
442
+ function refineContextPublicUrls2(data, ctx) {
443
+ const error = validateContextPublicUrls(data.context);
444
+ if (error) {
445
+ ctx.addIssue({
446
+ code: z2.ZodIssueCode.custom,
447
+ message: error,
448
+ path: ["context"]
449
+ });
450
+ }
451
+ }
452
+ var taskContextSchema2 = taskContextObjectSchema2.superRefine(
453
+ refineContextPublicUrls2
454
+ );
455
+ var assignToSchema2 = z2.object({
456
+ users: z2.array(z2.string().email()).optional(),
457
+ groups: z2.array(z2.string().min(1)).optional()
458
+ }).refine(
459
+ (data) => {
460
+ const groups = data.groups ?? [];
461
+ if (groups.includes("all") && groups.length > 1) {
462
+ return false;
463
+ }
464
+ return true;
465
+ },
466
+ { message: 'Cannot combine "all" with other group slugs' }
467
+ );
468
+ var threadUpdateMessageSchema2 = z2.string().min(1).max(500);
469
+ var threadUpdateStatuses2 = [
470
+ "info",
471
+ "queued",
472
+ "running",
473
+ "waiting",
474
+ "succeeded",
475
+ "failed",
476
+ "cancelled"
477
+ ];
478
+ var threadUpdateStatusSchema2 = z2.enum(threadUpdateStatuses2);
479
+ var threadUpdateInputSchema2 = z2.object({
480
+ message: threadUpdateMessageSchema2,
481
+ status: threadUpdateStatusSchema2.optional()
482
+ });
483
+ var taskPriorities2 = ["low", "normal", "high", "urgent"];
484
+ var taskPrioritySchema2 = z2.enum(taskPriorities2);
485
+ var nonNegativeInt2 = z2.number().int().nonnegative();
486
+ var agentCostTokensSchema2 = z2.object({
487
+ input: nonNegativeInt2.optional(),
488
+ output: nonNegativeInt2.optional(),
489
+ total: nonNegativeInt2.optional()
490
+ });
491
+ var agentCostSchema2 = z2.object({
492
+ tokens: agentCostTokensSchema2.optional(),
493
+ eur: z2.number().nonnegative().optional(),
494
+ usd: z2.number().nonnegative().optional()
495
+ });
496
+ var AGENT_INFO_MAX_KEYS2 = 32;
497
+ var AGENT_TOOL_CALLS_MAX_KEYS2 = 64;
498
+ var agentToolCallsSchema2 = z2.record(z2.string().min(1), nonNegativeInt2);
499
+ var agentTelemetrySchema2 = z2.object({
500
+ version: z2.string().min(1).optional(),
501
+ toolCallCount: nonNegativeInt2.optional(),
502
+ toolCalls: agentToolCallsSchema2.optional(),
503
+ cost: agentCostSchema2.optional(),
504
+ info: z2.record(z2.string(), z2.unknown()).optional()
505
+ }).refine(
506
+ (data) => {
507
+ const keys = data.info ? Object.keys(data.info) : [];
508
+ return keys.length <= AGENT_INFO_MAX_KEYS2;
509
+ },
510
+ { message: `agent.info may contain at most ${AGENT_INFO_MAX_KEYS2} keys` }
511
+ ).refine(
512
+ (data) => {
513
+ const keys = data.toolCalls ? Object.keys(data.toolCalls) : [];
514
+ return keys.length <= AGENT_TOOL_CALLS_MAX_KEYS2;
515
+ },
516
+ {
517
+ message: `agent.toolCalls may contain at most ${AGENT_TOOL_CALLS_MAX_KEYS2} tool names`
518
+ }
519
+ );
520
+ var createTaskBodySchema2 = taskContextObjectSchema2.extend({
521
+ assignTo: assignToSchema2.optional(),
522
+ /**
523
+ * Groups related tasks together. When omitted, the server generates one and
524
+ * returns it so the caller can reuse it on later tasks in the same thread.
525
+ */
526
+ threadId: z2.string().min(1).optional(),
527
+ /**
528
+ * Optional thread priority. When set, applies to the whole thread and
529
+ * overwrites any previous priority. Omit on later tasks to leave unchanged.
530
+ */
531
+ priority: taskPrioritySchema2.optional(),
532
+ /**
533
+ * Optional initial status update logged against the task's thread. Shows in
534
+ * the inbox status bar and the thread update log.
535
+ */
536
+ update: threadUpdateInputSchema2.optional(),
537
+ agent: agentTelemetrySchema2.optional()
538
+ }).superRefine(refineContextPublicUrls2);
539
+ var threadUpdateBodySchema = threadUpdateInputSchema2;
195
540
 
196
541
  // src/approval-result.ts
197
542
  var TaskTimeoutError = class extends Error {
@@ -305,7 +650,7 @@ var RobotRock = class {
305
650
  ...task.update !== void 0 ? { update: task.update } : {},
306
651
  ...task.agent !== void 0 ? { agent: task.agent } : {}
307
652
  };
308
- const validation = createTaskBodySchema.safeParse(bodyPayload);
653
+ const validation = createTaskBodySchema2.safeParse(bodyPayload);
309
654
  if (!validation.success) {
310
655
  throw new RobotRockError(
311
656
  `Invalid task: ${validation.error.issues[0]?.message}`,