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 EnvironmentId as EnvironmentIdType,
type OrchestrationThreadDetailPage,
type ThreadId as ThreadIdType,
} from "@akeru/contracts";
import * as Context from "effect/Context";
import * as Option from "effect/Option";
import { threadKey } from "./entities.ts";
import { type EnvironmentThreadPageState } from "./threadState.ts";
/**
* Turn window sizes for paginated thread loads: the initial page covers the
* last 10 user-anchored turns (subagent/fan-out turns ride along), each
* "load earlier" tap fetches 20 more. Sized so first paint on the heaviest
* observed threads stays around 100K gzipped while median threads load fully.
*/
export const INITIAL_THREAD_USER_TURN_LIMIT = 10;
export const OLDER_THREAD_PAGE_USER_TURN_LIMIT = 20;
export function pageStateFromSnapshot(
page: OrchestrationThreadDetailPage | undefined,
): Option.Option<EnvironmentThreadPageState> {
return page === undefined
? Option.none()
: Option.some({
beforeCursor: page.beforeCursor,
hasMore: page.hasMore,
loadingOlder: false,
});
}
interface ThreadOlderTurnRequestRegistry {
/**
* Registers the live state machine for a thread. Returns the deregistration
* cleanup; registration lives exactly as long as the machine's scope, and a
* successor machine for the same thread simply replaces the entry.
*/
readonly register: (key: string, handler: () => void) => () => void;
readonly request: (key: string) => boolean;
}
function makeThreadOlderTurnRequestRegistry(): ThreadOlderTurnRequestRegistry {
const handlers = new Map<string, () => void>();
return {
register: (key, handler) => {
handlers.set(key, handler);
return () => {
if (handlers.get(key) === handler) {
handlers.delete(key);
}
};
},
request: (key) => {
const handler = handlers.get(key);
if (handler === undefined) {
return false;
}
handler();
return true;
},
};
}
const defaultOlderTurnRequestRegistry = makeThreadOlderTurnRequestRegistry();
/**
* Channel from UI actions to the live per-thread state machines. The machines
* resolve it from the Effect environment (overridable in tests); the default
* instance is shared with the sync `requestOlderThreadTurns` entry point so
* the apps get working wiring without providing anything.
*/
export class ThreadOlderTurnRequests extends Context.Reference<ThreadOlderTurnRequestRegistry>(
"@akeru/client-runtime/state/threads/ThreadOlderTurnRequests",
{ defaultValue: () => defaultOlderTurnRequestRegistry },
) {}
/**
* Asks the live state machine for `threadId` to fetch the next older page.
* Returns false when no machine is live or no fetch was started (no cursor,
* already loading); callers render from `EnvironmentThreadState.page` and can
* treat false as "nothing to do".
*/
export function requestOlderThreadTurns(
environmentId: EnvironmentIdType,
threadId: ThreadIdType,
): boolean {
return defaultOlderTurnRequestRegistry.request(threadKey({ environmentId, threadId }));
}