Skip to content

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

2.3 KB
Raw Blame
// What a bot hears when a call is refused for length: which field, the limit, that nothing happened, and how to send it
// again. Without the last part a bot retries the same words or gives up.

import { BRIEF_MAX } from "./bot-link/protocol.ts";

/** The longest brief a coordinate_bots call carries: exactly what the engine keeps of one, so the route refuses nothing
 * the receiver would have kept, and the words below name the same number the catalog gives the tool. */
export const coordinateMessageMax = BRIEF_MAX;

type Issue = { code: string; path: readonly PropertyKey[]; maximum?: number | bigint };

export const tooLongRefusal = (field: string, max: number | bigint, how: string): string =>
  `${field} is over its ${String(max)}-character limit, so nothing was done. ${how}`;

const COORDINATE_FIELDS: Readonly<Record<string, string>> = {
  message: "Send only what the teammate needs to start, and save the long detail to a file in this folder and name its path, then call coordinate_bots again.",
  done_when: "Say in a sentence or two what finished looks like, then call coordinate_bots again.",
  title: "Use a shorter title, then call coordinate_bots again.",
  reason: "Say why in one short line, then call coordinate_bots again.",
};

/** What to say when length is the only thing wrong with a coordinate_bots call; undefined when anything else is too, so the
 * general words cover it all and the bot isn't sent back to fix one field of two. */
export function coordinateTooLong(issues: readonly Issue[]): string | undefined {
  const refusals = issues.map((issue) => {
    const field = String(issue.path[0]);
    const how = COORDINATE_FIELDS[field];
    return issue.code === "too_big" && how && issue.maximum !== undefined ? tooLongRefusal(field, issue.maximum, how) : undefined;
  });
  return refusals.length && refusals.every(Boolean) ? refusals.join(" ") : undefined;
}

/** A start_thread brief that reports back travels as a hand-off, so it has a hand-off's limit; undefined when it fits. */
export function reportBackTooLong(message: string): string | undefined {
  return message.length > coordinateMessageMax
    ? tooLongRefusal("message", coordinateMessageMax, "Put only what the thread needs to start in the message, save the long detail to a file and name its full path, then call start_thread again.")
    : undefined;
}