Skip to content

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.

3.5 KB
Raw Blame
import * as Match from "effect/Match";
import {
  type ClientOrchestrationCommand,
  OrchestrationDispatchCommandError,
} from "@akeru/contracts";
import * as Cause from "effect/Cause";
import * as Effect from "effect/Effect";

import {
  channelFailureMessage,
  channelFailurePresentation,
  isChannelTransportError,
  type ChannelFailurePresentation,
  type ChannelOperationError,
  type ChannelRuntimeShape,
} from "./ChannelRuntime.ts";

export type ChannelCommand = Extract<
  ClientOrchestrationCommand,
  { readonly type: `channel.${string}` }
>;

export function isChannelCommand(command: ClientOrchestrationCommand): command is ChannelCommand {
  return command.type.startsWith("channel.");
}

export const executeChannelCommand = (
  runtime: ChannelRuntimeShape,
  command: ChannelCommand,
): Effect.Effect<{ readonly sequence: number }, ChannelOperationError> =>
  Match.value(command).pipe(
    Match.when({ type: "channel.connect" }, (command) => runtime.connect(command)),
    Match.when({ type: "channel.connection.save" }, (command) => runtime.saveConnection(command)),
    Match.when({ type: "channel.connection.delete" }, (command) =>
      runtime.deleteConnection(command.connectionId),
    ),
    Match.when({ type: "channel.attach" }, (command) =>
      runtime.attach(command.botId, command.connectionId, command.projectId, command.provider),
    ),
    Match.when({ type: "channel.change-project" }, (command) =>
      runtime.changeProject(command.botId, command.provider, command.projectId),
    ),
    Match.when({ type: "channel.disconnect" }, (command) =>
      runtime.disconnect(command.botId, command.provider),
    ),
    Match.when({ type: "channel.detach" }, (command) =>
      runtime.detach(command.botId, command.provider),
    ),
    Match.when({ type: "channel.reconnect" }, (command) =>
      runtime.reconnect(command.botId, command.provider),
    ),
    Match.orElse((command) => runtime.sendChannelMessage(command)),
    Effect.map((sequence) => ({ sequence })),
  );

/**
 * The only way a channel command failure leaves the server. Returns fixed, client-safe text
 * and logs just the command type and failure category, never the provider error or cause.
 */
export const channelCommandFailure = (
  command: ChannelCommand,
  cause: Cause.Cause<unknown>,
): Effect.Effect<ChannelFailurePresentation> => {
  const error = Cause.hasInterruptsOnly(cause) ? undefined : Cause.squash(cause);

  const presented =
    error === undefined
      ? { message: "Channel command was interrupted. Try again." }
      : channelFailurePresentation(error);

  // A provider error after a reply post began is ambiguous: the message may have been delivered.
  // A definite rejection, or a check that failed before posting, keeps its own category.
  const deliveryUnknown = channelFailureMessage("delivery-unknown");

  const failure: ChannelFailurePresentation =
    command.type === "channel.send" &&
    (isChannelTransportError(error) || presented.message === deliveryUnknown)
      ? { message: deliveryUnknown, category: "delivery-unknown" }
      : presented;

  return Effect.logWarning("channel command failed", {
    commandType: command.type,
    category: failure.category ?? "internal",
  }).pipe(Effect.as(failure));
};

/** The client error for a channel failure. The category lets clients explain it and offer a repair. */
export const channelDispatchError = (failure: ChannelFailurePresentation) =>
  new OrchestrationDispatchCommandError({
    message: failure.message,
    ...(failure.category ? { channelFailureCategory: failure.category } : {}),
  });