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 { scopeThreadRef } from "@akeru/client-runtime/environment";
import { EnvironmentId, type ScopedThreadRef, ThreadId } from "@akeru/contracts";
import { DraftId } from "./composerDraftStore";
export type ThreadRouteTarget =
| {
kind: "server";
threadRef: ScopedThreadRef;
}
| {
kind: "draft";
draftId: DraftId;
};
type DraftThreadRouteState = {
environmentId: EnvironmentId;
threadId: ThreadId;
promotedTo?: ScopedThreadRef | null;
};
export type ThreadRouteRenderState = "loading" | "ready" | "missing";
export function resolveThreadRouteRenderState(input: {
bootstrapComplete: boolean;
serverThreadShellExists: boolean;
serverThreadDetailExists: boolean;
serverThreadDetailDeleted: boolean;
draftThreadExists: boolean;
}): ThreadRouteRenderState {
if (!input.bootstrapComplete) {
return "loading";
}
if (input.serverThreadDetailExists || input.draftThreadExists) {
return "ready";
}
if (input.serverThreadDetailDeleted) {
return "missing";
}
return input.serverThreadShellExists ? "loading" : "missing";
}
export function buildThreadRouteParams(ref: ScopedThreadRef) {
return {
environmentId: ref.environmentId,
threadId: ref.threadId,
};
}
export function buildDraftThreadRouteParams(draftId: DraftId) {
return { draftId };
}
export function resolveThreadRouteRef(
params: Partial<Record<"environmentId" | "threadId", string | undefined>>,
): ScopedThreadRef | null {
if (!params.environmentId || !params.threadId) {
return null;
}
return scopeThreadRef(EnvironmentId.make(params.environmentId), ThreadId.make(params.threadId));
}
export function resolveThreadRouteTarget(
params: Partial<Record<"environmentId" | "threadId" | "draftId", string | undefined>>,
): ThreadRouteTarget | null {
if (params.environmentId && params.threadId) {
return {
kind: "server",
threadRef: scopeThreadRef(
EnvironmentId.make(params.environmentId),
ThreadId.make(params.threadId),
),
};
}
if (!params.draftId) {
return null;
}
return {
kind: "draft",
draftId: DraftId.make(params.draftId),
};
}
/**
* Resolves the thread represented by either a canonical thread route or a
* draft route whose promotion to a server thread has been recorded.
*/
export function resolveActiveThreadRouteRef(
target: ThreadRouteTarget | null,
draftThread: DraftThreadRouteState | null,
): ScopedThreadRef | null {
if (target?.kind === "server") {
return target.threadRef;
}
if (target?.kind !== "draft" || !draftThread?.promotedTo) {
return null;
}
return draftThread.promotedTo;
}