Skip to content

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

3.8 KB
Raw Blame
import type { DriverKind } from "./contracts.ts";
import { replaysTranscriptNatively } from "./runtime/capabilities.ts";

export function replaysNatively(kind: DriverKind): boolean {
  return replaysTranscriptNatively(kind);
}

// rewound and fresh coincide today but stay distinct: rewound also invalidates other instances' cursors.
export interface TurnContextInput {
  text: string;
  transcript: Array<{ role: "user" | "assistant"; text: string }>;
  rewound: boolean;
  fresh: boolean;
  externallyUpdated: boolean;
  replaysNatively: boolean;
  continuingAfterFold?: boolean;
}

export function engineIsFresh(input: {
  instanceId: string;
  lastInstanceId: string | undefined;
  resumeCursors: Record<string, unknown>;
  transcript: Array<{ role: "user" | "assistant"; text: string }>;
}): boolean {
  const { instanceId, lastInstanceId, resumeCursors, transcript } = input;
  if (!transcript.some((m) => m.role === "user")) return false;
  if (lastInstanceId !== undefined) return lastInstanceId !== instanceId || resumeCursors[instanceId] === undefined;
  const cursorIds = Object.keys(resumeCursors);
  return !(cursorIds.length === 1 && cursorIds[0] === instanceId);
}

const REWOUND_PREAMBLE =
  "[The user rewound this conversation (edited a message or switched to another version). Everything before this point was replaced by the following history:]";
const FRESH_PREAMBLE =
  "[You are joining this conversation mid-thread (the user switched this bot over to you). The conversation so far:]";
const EXTERNAL_UPDATE_PREAMBLE =
  "[This conversation received an update outside your provider session. The complete current history follows so you can use that update in your next response:]";

const FOLD_PREAMBLE =
  "[This conversation was compacted and is continuing in a fresh engine session. The conversation so far:]";
const FOLD_CONTINUATION =
  "[Continue the pending work in this conversation using the earlier context and any new results. Follow the user's standing instructions and respond in their language.]";

const RECOVERED_PREAMBLE =
  "[Your previous session for this conversation could not be resumed, so this is a new session. The conversation so far:]";

export function buildRecoveryText(input: {
  text: string;
  transcript: Array<{ role: "user" | "assistant"; text: string }>;
}): string | undefined {
  if (input.transcript.length === 0) return undefined;
  return [
    RECOVERED_PREAMBLE,
    "",
    ...input.transcript.map((m) => `${m.role === "user" ? "User" : "Assistant"}: ${m.text}`),
    "",
    "[Now reply to the user's latest message:]",
    "",
    input.text,
  ].join("\n");
}

/** Whether a turn carries on its engine's session rather than rebuilding it from the transcript. */
export const canResume = (input: Pick<TurnContextInput, "rewound" | "fresh" | "externallyUpdated">): boolean =>
  !input.rewound && !input.fresh && !input.externallyUpdated;

export function buildTurnContext(input: TurnContextInput): {
  turnText: string;
  resume: boolean;
  replayed: boolean;
} {
  const { text, transcript, rewound, externallyUpdated, replaysNatively } = input;
  const resume = canResume(input);
  const continuing = !resume && input.continuingAfterFold;
  const current = continuing ? (text ? `${text}\n\n${FOLD_CONTINUATION}` : FOLD_CONTINUATION) : text;
  const replayed = !resume && !replaysNatively && transcript.length > 0;
  if (!replayed) return { turnText: current, resume, replayed };
  return {
    turnText: [
      continuing ? FOLD_PREAMBLE : rewound ? REWOUND_PREAMBLE : externallyUpdated ? EXTERNAL_UPDATE_PREAMBLE : FRESH_PREAMBLE,
      "",
      ...transcript.map((m) => `${m.role === "user" ? "User" : "Assistant"}: ${m.text}`),
      "",
      continuing ? "[Current continuation:]" : "[Now reply to the user's latest message:]",
      "",
      current,
    ].join("\n"),
    resume,
    replayed,
  };
}