Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
import { type TurnId } from "@akeru/contracts";
import type { ComposerSubmissionIntent } from "../../composer-logic";
import { type ChatMessage, type SessionPhase, type Thread } from "../../types";
export interface LocalDispatchSnapshot {
startedAt: string;
preparingWorktree: boolean;
submissionIntent: ComposerSubmissionIntent;
latestUserMessageId: ChatMessage["id"] | null;
latestTurnTurnId: TurnId | null;
latestTurnRequestedAt: string | null;
latestTurnStartedAt: string | null;
latestTurnCompletedAt: string | null;
sessionStatus: NonNullable<Thread["session"]>["status"] | null;
sessionUpdatedAt: string | null;
}
export function createLocalDispatchSnapshot(
activeThread: Thread | undefined,
options?: {
preparingWorktree?: boolean;
submissionIntent?: ComposerSubmissionIntent;
},
): LocalDispatchSnapshot {
const latestTurn = activeThread?.latestTurn ?? null;
const session = activeThread?.session ?? null;
const latestUserMessage = activeThread?.messages.findLast((message) => message.role === "user");
return {
startedAt: new Date().toISOString(),
preparingWorktree: Boolean(options?.preparingWorktree),
submissionIntent: options?.submissionIntent ?? "foreground",
latestUserMessageId: latestUserMessage?.id ?? null,
latestTurnTurnId: latestTurn?.turnId ?? null,
latestTurnRequestedAt: latestTurn?.requestedAt ?? null,
latestTurnStartedAt: latestTurn?.startedAt ?? null,
latestTurnCompletedAt: latestTurn?.completedAt ?? null,
sessionStatus: session?.status ?? null,
sessionUpdatedAt: session?.updatedAt ?? null,
};
}
export function hasServerAcknowledgedLocalDispatch(input: {
localDispatch: LocalDispatchSnapshot | null;
phase: SessionPhase;
latestTurn: Thread["latestTurn"] | null;
latestUserMessageId: ChatMessage["id"] | null;
session: Thread["session"] | null;
hasPendingApproval: boolean;
hasPendingUserInput: boolean;
threadError: string | null | undefined;
}): boolean {
if (!input.localDispatch) {
return false;
}
if (input.hasPendingApproval || input.hasPendingUserInput || Boolean(input.threadError)) {
return true;
}
if (input.phase === "connecting") {
return false;
}
const latestTurn = input.latestTurn ?? null;
const session = input.session ?? null;
const latestUserMessageChanged =
input.localDispatch.latestUserMessageId !== input.latestUserMessageId;
const latestTurnChanged =
input.localDispatch.latestTurnTurnId !== (latestTurn?.turnId ?? null) ||
input.localDispatch.latestTurnRequestedAt !== (latestTurn?.requestedAt ?? null) ||
input.localDispatch.latestTurnStartedAt !== (latestTurn?.startedAt ?? null) ||
input.localDispatch.latestTurnCompletedAt !== (latestTurn?.completedAt ?? null);
if (input.phase === "running") {
// Steering adds a user message to the current running turn without
// necessarily changing any of the turn timestamps. Treat that projected
// message as the server acknowledgment so the composer does not remain
// stuck in its local "Sending" state until the turn settles.
if (latestUserMessageChanged) {
return true;
}
if (!latestTurnChanged) {
return false;
}
if (latestTurn?.startedAt === null || latestTurn === null) {
return false;
}
if (
session?.activeTurnId !== null &&
session?.activeTurnId !== undefined &&
latestTurn?.turnId !== session.activeTurnId
) {
return false;
}
return true;
}
return (
latestTurnChanged ||
input.localDispatch.sessionStatus !== (session?.status ?? null) ||
input.localDispatch.sessionUpdatedAt !== (session?.updatedAt ?? null)
);
}