Skip to content

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

7.2 KB
Raw Blame
import type { ModelCatalog, ModelSelection } from "./contracts.ts";
import { isConversational, type ReplyStyle } from "../shared/reply-style.ts";
import { offeredEffortLevels, type EffortLevel } from "../shared/wire.ts";
import { assertModelVariantSupported } from "./member-turn.ts";

export type TurnTier = "simple" | "complex";

export interface TurnSignals {
  text: string;
  /** Images or files sent with the message. */
  attachments: number;
  /** The previous bot turn used tools (activity rows after the last ask). */
  priorTurnUsedTools: boolean;
}

const ACTION = /\b(build|fix|debug|implement|refactor|write|create|generate|deploy|install|run|test|search|find|look up|browse|open|edit|change|update|delete|remove|rename|move|migrate|analy[sz]e|review|compare|research|summari[sz]e|translate|draft|send|email|schedule|book|download|upload|scrape|clone|commit|push|merge|plan|design|calculate|convert)\b/i;
const CODE = /```|`[^`\n]+`|\b(function|const|let|class|import|def|SELECT|FROM)\b|[{};]\s*$|\w+\.(ts|tsx|js|py|go|rs|json|md|sh|yml|yaml|sql|css|html)\b|https?:\/\/|(^|\s)[~.]?\/[\w.-]+\//m;
const CONTINUE = /^(yes|yep|yeah|ok(ay)?|sure|go( ahead| on)?|do it|continue|proceed|please do|sounds good|retry|try again|продолж(?:и|ай|айте|ите|ить|им)|давай(?:те)?|дальше)(?=$|[^\p{L}\p{N}_])/iu;
const ACK = /^(thanks|thank you|thx|ty|cool|nice|great|perfect|awesome|got it|lol|haha)\b[\s!.]*$/i;
const SIMPLE_CHARS = 280;
const SIMPLE_LINES = 4;
const QUICK_CONTEXT_SHARE = 0.6;

export function classifyTurn(signals: TurnSignals): TurnTier {
  const text = signals.text.trim();
  if (!text) return "complex";
  if (signals.attachments > 0) return "complex";
  if (text.length > SIMPLE_CHARS || text.split("\n").length > SIMPLE_LINES) return "complex";
  if (CODE.test(text)) return "complex";
  if (ACTION.test(text)) return "complex";
  if (CONTINUE.test(text)) return "complex";
  if (signals.priorTurnUsedTools && !ACK.test(text)) return "complex";
  if ((text.match(/\?/g) ?? []).length > 2) return "complex";
  return "simple";
}

const FAST = /(^|[-_/. ])(haiku|flash|mini|nano|lite|small|fast|instant|spark|turbo|air)(?=$|[-_/. \d])/i;

const vendor = (id: string) => {
  const m = /^(gpt|claude|gemini|glm|deepseek|grok|qwen|mistral|llama|composer|kimi)/i.exec(id);
  return m ? m[1]!.toLowerCase() : null;
};

/** Same upstream provider only; among several, the first listed wins since engines list their newest first. */
export function fastModelFor(catalog: ModelCatalog | undefined, current: string, refused: ReadonlySet<string> = new Set()): string | undefined {
  if (!catalog) return undefined;
  const currentOption = catalog.options.find((option) => option.id === current);
  if (!currentOption || currentOption.custom) return undefined;
  if (FAST.test(current)) return current;
  const provider = currentOption.provider;
  const currentVendor = vendor(current);
  const pick = catalog.options.find((option) => {
    if (option.custom || refused.has(option.id) || !FAST.test(option.id)) return false;
    if (provider || option.provider) return option.provider === provider;
    const optionVendor = vendor(option.id);
    if (currentVendor || optionVendor) return optionVendor === currentVendor;
    return true;
  });
  return pick?.id;
}

export interface RouteInput {
  conversational: boolean;
  /** false when a person picked this thread's own model. */
  followsBotModel: boolean;
  automated: boolean;
  /** The thread's last measured context, so a long conversation never moves to a smaller window. */
  contextTokens?: number;
  signals: TurnSignals;
  model: string;
  effort?: EffortLevel;
  variant?: string;
  catalog?: ModelCatalog;
  /** Quick models this engine instance refused before. */
  refused?: ReadonlySet<string>;
  effortLevels?: readonly EffortLevel[];
}

export interface RouteResult {
  tier: TurnTier | null;
  model: string;
  effort?: EffortLevel;
  variant?: string;
  /** The picker label of the quick model when the turn was moved to it. */
  quickModel?: string;
}

export function routeTurn(input: RouteInput): RouteResult {
  const keep: RouteResult = { tier: null, model: input.model, effort: input.effort, variant: input.variant };
  if (!input.conversational || !input.followsBotModel || input.automated) return keep;
  const tier = classifyTurn(input.signals);
  if (tier === "complex") return { ...keep, tier };
  const fast = fastModelFor(input.catalog, input.model, input.refused);
  if (!fast || fast === input.model) {
    const low = input.effortLevels?.includes("low") ? "low" : input.effortLevels?.includes("none") ? "none" : undefined;
    return { ...keep, tier, effort: input.effort && low ? low : input.effort };
  }
  const option = input.catalog?.options.find((candidate) => candidate.id === fast);
  if (option?.contextWindow && (input.contextTokens ?? 0) > option.contextWindow * QUICK_CONTEXT_SHARE) return { ...keep, tier };
  // Effort and variant belong to the configured model; the quick model runs on its engine's defaults.
  return { tier, model: fast, quickModel: option?.label ?? fast };
}

/** Routines, webhooks, peer turns, card continuations and unattended runs pass `automated`. */
export function routeDirectTurn(
  bot: { replyStyle?: ReplyStyle; modelSelection: ModelSelection },
  task: { modelSelection?: ModelSelection; usage?: { context?: { tokens: number } } },
  path: readonly { role: string; kind?: string }[],
  message: { text: string; images: readonly unknown[]; videos?: readonly unknown[] },
  instance: { models?: ModelCatalog; quickModelsUsable?: () => boolean; adapter: { capabilities: { effortLevels?: readonly EffortLevel[]; modelVariants?: boolean } } },
  automated: boolean,
  refused?: ReadonlySet<string>,
): RouteResult {
  const lastAsk = path.findLastIndex((m) => m.role === "user" && m.kind === "text");
  return routeTurn({
    conversational: isConversational(bot.replyStyle),
    followsBotModel: task.modelSelection === undefined,
    automated,
    signals: {
      text: message.text,
      attachments: message.images.length + (message.videos?.length ?? 0),
      priorTurnUsedTools: lastAsk >= 0 && path.slice(lastAsk + 1).some((m) => m.kind === "activity"),
    },
    model: bot.modelSelection.model,
    effort: bot.modelSelection.effort,
    variant: bot.modelSelection.variant,
    catalog: instance.quickModelsUsable?.() === false ? undefined : instance.models,
    refused,
    effortLevels: instance.models
      ? offeredEffortLevels({ models: instance.models, adapter: instance.adapter }, bot.modelSelection.model)
      : instance.adapter.capabilities.effortLevels,
    contextTokens: task.usage?.context?.tokens,
  });
}

/** The route, refused before any work starts when this engine cannot honor the effort or variant it names. */
export function checkedDirectRoute(...args: Parameters<typeof routeDirectTurn>): RouteResult {
  const route = routeDirectTurn(...args);
  const instance = args[4];
  assertModelVariantSupported({ variant: route.variant, effort: route.effort }, instance.adapter.capabilities);
  if (route.effort && !offeredEffortLevels(instance, route.model).includes(route.effort)) {
    throw Object.assign(new Error(`effort "${route.effort}" is not offered by this bot's engine — choose another level in settings`), { status: 409 });
  }
  return route;
}