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 * as Schema from "effect/Schema";
import * as Option from "effect/Option";
import { ThreadId } from "@akeru/contracts";
/**
* Opaque, exclusive cursor for windowed thread detail reads. Encodes the thread
* id and the keyset boundary of an already-delivered page: the boundary turn's
* anchor timestamp (`COALESCE(requested_at, started_at, '')`) and turn id.
* Passing it back requests the adjacent disjoint slice of strictly older turns
* under `(anchor, turn_id)` ordering.
*
* The boundary is deliberately NOT a `projection_turns.row_id`: row ids are
* rewritten by the revert projector (delete + re-upsert) and by projection
* rebuilds, which would silently invalidate every persisted cursor with no
* event emitted. The (anchor, turnId) pair is derived from event content, so
* cursors survive both and no client-side refresh machinery is needed. The
* anchor doubles as the time bound for rows with no turn linkage (straggler
* user messages, turnless activities). The thread id is embedded so a cursor
* can never be replayed against a different thread. Clients must treat the
* string as opaque.
*/
export interface ThreadDetailPageCursor {
readonly threadId: ThreadId;
readonly beforeAnchorAt: string;
/** Boundary turn id; "" for the rare turn row with a null turn_id. */
readonly beforeTurnId: string;
}
export function encodeThreadDetailPageCursor(cursor: ThreadDetailPageCursor): string {
return Buffer.from(
JSON.stringify({ t: cursor.threadId, a: cursor.beforeAnchorAt, i: cursor.beforeTurnId }),
).toString("base64url");
}
const decodeCursor = Schema.decodeUnknownOption(
Schema.Struct({
t: Schema.String.check(Schema.isMinLength(1)),
a: Schema.String,
i: Schema.String,
}),
);
/**
* Returns null for anything that is not a well-formed cursor. Callers degrade
* a malformed or foreign-thread cursor to a first-page request.
*/
export function decodeThreadDetailPageCursor(encoded: string): ThreadDetailPageCursor | null {
let parsed: unknown;
try {
parsed = JSON.parse(Buffer.from(encoded, "base64url").toString("utf8"));
} catch {
return null;
}
const decoded = decodeCursor(parsed);
if (Option.isNone(decoded)) return null;
// Empty anchors and turn IDs are valid keyset boundaries.
const record = decoded.value;
return { threadId: ThreadId.make(record.t), beforeAnchorAt: record.a, beforeTurnId: record.i };
}