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 Predicate from "effect/Predicate";
import * as Cause from "effect/Cause";
import * as Schema from "effect/Schema";
import {
AuthAccessWriteScope,
type AuthSessionState,
type ChannelBinding,
ChannelFailureCategory,
type ChannelProvider,
} from "@akeru/contracts";
export function canManageChannels(
session: Pick<AuthSessionState, "authenticated" | "scopes"> | null,
): boolean {
return session?.authenticated === true && session.scopes?.includes(AuthAccessWriteScope) === true;
}
export function resolveChannelSettingsAccess(input: {
readonly isPending: boolean;
readonly session: Pick<AuthSessionState, "authenticated" | "scopes"> | null;
}): "pending" | "allowed" | "denied" {
if (input.session === null && input.isPending) return "pending";
return canManageChannels(input.session) ? "allowed" : "denied";
}
export function connectedChannelBinding(
bindings: ReadonlyArray<ChannelBinding> | undefined,
provider: ChannelProvider,
): ChannelBinding | undefined {
return bindings?.find(
(binding) => binding.provider === provider && binding.status === "connected",
);
}
const CHANNEL_IDENTITY_CONFLICT =
/^This channel connection is (?:already connected|attached) to another bot\.$/u;
/**
* Whether a failed channel command was rejected because another bot already owns the provider
* account. The server sends one of two fixed messages for this case, and only an exact match counts.
*/
export function isChannelIdentityConflict(result: {
readonly cause?: Cause.Cause<unknown>;
}): boolean {
if (!result.cause) return false;
const error = Cause.squash(result.cause);
const message =
(error === null || Predicate.isObjectOrArray(error)) && error !== null && "message" in error
? error.message
: error;
return Predicate.isString(message) && CHANNEL_IDENTITY_CONFLICT.test(message);
}
const isChannelFailureCategory = Schema.is(ChannelFailureCategory);
/** Why a failed channel command failed, when the server sent a category for it. */
export function channelFailureCategoryOf(result: {
readonly cause?: Cause.Cause<unknown>;
}): ChannelFailureCategory | undefined {
if (!result.cause) return undefined;
const error = Cause.squash(result.cause);
const category =
(error === null || Predicate.isObjectOrArray(error)) &&
error !== null &&
"channelFailureCategory" in error
? error.channelFailureCategory
: undefined;
return isChannelFailureCategory(category) ? category : undefined;
}