Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

8.2 KB
Raw Blame
// The shapes a routine request takes: the tool input a bot sends, and the normalized operation a card stores.
import { z } from "zod";

import {
  ENGINE_ID, MAX_DATE_MS, TIME, intervalWindowMinutes, refineStoredInterval, storedCronScheduleSchema,
  storedIntervalWindowSchema, storedLimitResetDefinition, storedWeekdaysSchema,
} from "../shared/routine-schedule-schema.ts";
import type { JsonValue } from "./schema.ts";

export { ENGINE_ID, MAX_DATE_MS, TIME, intervalWindowMinutes, storedCronScheduleSchema, storedIntervalWindowSchema, storedWeekdaysSchema };
export const ROUTINE_ID = /^[A-Za-z0-9_-]{1,128}$/;

export const jsonObjectSchema = z.record(z.string(), z.custom<JsonValue>());
export const toolIntervalWindowSchema = z.object({
  start: z.string().max(5),
  end: z.string().max(5),
}).strict();

export const routineToolScheduleSchema = z.discriminatedUnion("type", [
  z.object({ type: z.literal("cron"), expression: z.string().max(256), timeZone: z.string().max(128) }).strict(),
  z.object({ type: z.literal("limit_reset"), engine: z.string().max(128).optional(), repeat: z.enum(["once", "every_reset"]).optional() }).strict(),
  z.object({ type: z.literal("once"), at: z.string().max(64) }).strict(),
  z.object({
    type: z.literal("weekly"),
    time: z.string().max(5),
    weekdays: z.array(z.string().max(9)).min(1).max(7),
  }).strict(),
  z.object({
    type: z.literal("interval"),
    everyMinutes: z.number(),
    anchorAt: z.string().max(64).optional(),
    weekdays: z.array(z.string().max(9)).min(1).max(7).nullable().optional(),
    window: toolIntervalWindowSchema.nullable().optional(),
    endsAt: z.string().max(64).nullable().optional(),
  }).strict(),
]);

export const routineToolDefinitionSchema = z.object({
  name: z.string().max(80),
  instructions: z.string().max(20_000),
  schedule: routineToolScheduleSchema,
  runOn: z.enum(["maus", "cloud"]).optional(),
  durationMinutes: z.number().optional(),
  timeoutMinutes: z.number().nullable().optional(),
  continuity: z.boolean().optional(),
  sameConversation: z.boolean().optional(),
  overlap: z.enum(["skip", "queue"]).optional(),
  runOnStartup: z.boolean().optional(),
}).strict();

export const routineToolChangesSchema = routineToolDefinitionSchema
  .omit({ timeoutMinutes: true })
  .partial()
  .extend({ timeoutMinutes: z.number().nullable().optional() })
  .strict()
  .refine(
  (changes) => Object.values(changes).some((value) => value !== undefined),
  "Choose at least one routine field to update",
);

/** Authorized by the harness route (existence + same section) before it gets here, and again at confirm time. */
export const targetBotSchema = z.object({
  botId: z.string().min(1).max(128),
  name: z.string().min(1).max(80),
}).strict();
export const routineProposalSchema = z.discriminatedUnion("action", [
  z.object({ action: z.literal("create"), routine: routineToolDefinitionSchema, forBot: targetBotSchema.optional() }).strict(),
  z.object({ action: z.literal("update"), routineId: z.string().max(128), changes: routineToolChangesSchema, forBot: targetBotSchema.optional() }).strict(),
  z.object({ action: z.literal("pause"), routineId: z.string().max(128), forBot: targetBotSchema.optional() }).strict(),
  z.object({ action: z.literal("resume"), routineId: z.string().max(128), forBot: targetBotSchema.optional() }).strict(),
  z.object({ action: z.literal("run_now"), routineId: z.string().max(128), forBot: targetBotSchema.optional() }).strict(),
  z.object({ action: z.literal("delete"), routineId: z.string().max(128), forBot: targetBotSchema.optional() }).strict(),
]);

export const storedScheduleSchema = z.discriminatedUnion("type", [
  storedCronScheduleSchema,
  z.object({ type: z.literal("limit_reset"), ...storedLimitResetDefinition }).strict(),
  z.object({ type: z.literal("once"), at: z.number().int().nonnegative() }).strict(),
  z.object({
    type: z.literal("daily"),
    time: z.string().regex(TIME),
    weekdays: storedWeekdaysSchema,
  }).strict(),
  z.object({
    type: z.literal("interval"),
    everyMinutes: z.number().int().min(5).max(1_440),
    anchorAt: z.number().int().nonnegative().max(MAX_DATE_MS).optional(),
    weekdays: storedWeekdaysSchema.optional(),
    window: storedIntervalWindowSchema.optional(),
    endsAt: z.number().int().nonnegative().max(MAX_DATE_MS).optional(),
  }).strict(),
]).superRefine((schedule, context) => refineStoredInterval(schedule, context));
export const storedScheduleChangesSchema = z.discriminatedUnion("type", [
  storedCronScheduleSchema,
  z.object({ type: z.literal("limit_reset"), ...storedLimitResetDefinition }).strict(),
  z.object({ type: z.literal("once"), at: z.number().int().nonnegative() }).strict(),
  z.object({
    type: z.literal("daily"),
    time: z.string().regex(TIME),
    weekdays: storedWeekdaysSchema,
  }).strict(),
  z.object({
    type: z.literal("interval"),
    everyMinutes: z.number().int().min(5).max(1_440),
    anchorAt: z.number().int().nonnegative().max(MAX_DATE_MS).optional(),
    weekdays: storedWeekdaysSchema.nullable().optional(),
    window: storedIntervalWindowSchema.nullable().optional(),
    endsAt: z.number().int().nonnegative().max(MAX_DATE_MS).nullable().optional(),
  }).strict(),
]).superRefine((schedule, context) => refineStoredInterval(schedule, context));
export const storedDefinitionSchema = z.object({
  name: z.string().trim().min(1).max(80),
  instructions: z.string().trim().min(1).max(20_000),
  schedule: storedScheduleSchema,
  runOn: z.enum(["maus", "cloud"]),
  durationMinutes: z.number().int().min(5).max(240),
  timeoutMinutes: z.number().int().min(5).max(1_440).optional(),
  continuity: z.boolean().optional(),
  sameConversation: z.boolean().optional(),
  overlap: z.enum(["skip", "queue"]).optional(),
  runOnStartup: z.boolean().optional(),
}).strict();
export const storedChangesSchema = storedDefinitionSchema
  .omit({ schedule: true, timeoutMinutes: true })
  .partial()
  .extend({
    schedule: storedScheduleChangesSchema.optional(),
    timeoutMinutes: z.number().int().min(5).max(1_440).nullable().optional(),
  })
  .strict()
  .refine(
  (changes) => Object.values(changes).some((value) => value !== undefined),
  "Stored routine update must change at least one field",
);
export const storedManageBase = {
  routineId: z.string().regex(ROUTINE_ID),
  expectedUpdatedAt: z.number().int().nonnegative(),
  forBot: targetBotSchema.optional(),
};
export const storedOperationSchema = z.discriminatedUnion("action", [
  z.object({ action: z.literal("create"), routine: storedDefinitionSchema, forBot: targetBotSchema.optional() }).strict(),
  z.object({ action: z.literal("update"), ...storedManageBase, changes: storedChangesSchema }).strict(),
  z.object({ action: z.literal("pause"), ...storedManageBase }).strict(),
  z.object({ action: z.literal("resume"), ...storedManageBase }).strict(),
  z.object({ action: z.literal("run_now"), ...storedManageBase }).strict(),
  z.object({ action: z.literal("delete"), ...storedManageBase }).strict(),
]);
export const storedSnapshotSchema = storedDefinitionSchema.extend({
  enabled: z.boolean(),
  target: z.literal("room-goal").optional(),
  groupId: z.string().min(1).max(128).optional(),
  resultsThreadId: z.string().min(1).max(128).optional(),
}).strict();
export const storedUndoSchema = z.object({
  name: z.string().max(200),
  schedule: storedScheduleSchema.optional(),
  appliedUpdatedAt: z.number().int().nonnegative().optional(),
  before: storedSnapshotSchema.optional(),
  ownersBefore: z.boolean().optional(),
}).strict();
export const routineRequestCardDataSchema = z.object({
  version: z.literal(1),
  requestId: z.string().min(1).max(128),
  botId: z.string().min(1).max(128),
  threadId: z.string().min(1).max(128),
  createdAt: z.number().int().nonnegative(),
  operation: storedOperationSchema,
  appliedAt: z.number().int().nonnegative().optional(),
  resultId: z.string().min(1).max(128).optional(),
  undo: storedUndoSchema.optional(),
}).strict();

export type RoutineToolScheduleInput = z.infer<typeof routineToolScheduleSchema>;
export type RoutineToolDefinitionInput = z.infer<typeof routineToolDefinitionSchema>;
export type RoutineToolChangesInput = z.infer<typeof routineToolChangesSchema>;
export type RoutineProposalInput = z.input<typeof routineProposalSchema>;
export type ParsedRoutineProposal = z.output<typeof routineProposalSchema>;