Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

7.5 KB
Raw Blame
import { directConnectionValues, integrationSnapshot, resolveIntegration } from "@/lib/infra/connection-service";
import { IntegrationError } from "@/lib/infra/identity";
import { ChannelError } from "./errors";
import { rateLimited } from "@/lib/host/rate-limit";
import { channelProvider, listChannelProviders } from "./registry";
import { parseChannelPatch } from "./schema";
import { reviewedChannelBinding } from "@/lib/workflow/graph-triggers";
import {
  channelById,
  createChannel,
  deleteChannel,
  readChannelState,
  recordChannelActivity,
  recordChannelCheck,
  updateChannel,
} from "./store";
import type { ChannelInboundEvent, ChannelRecord } from "./types";
import { verifyTelegram, sendTelegramText, verifyTelegramSecret, normalizeTelegramUpdate } from "./providers/telegram";
import {
  discordApplicationMatches,
  normalizeDiscordInteraction,
  sendDiscordText,
  verifyDiscord,
  verifyDiscordSignature,
} from "./providers/discord";

async function values(channel: ChannelRecord) {
  try {
    return await directConnectionValues(channel.provider, {
      user: channel.credential.user,
      connection: channel.credential.connection,
    });
  } catch (error) {
    if (error instanceof IntegrationError) throw new ChannelError(error.code, error.status);
    throw error;
  }
}

function assertEnabled(channel: ChannelRecord) {
  if (!channel.enabled) throw new ChannelError("channel_disabled", 409);
}

export async function channelsSnapshot() {
  const state = await readChannelState();
  const root = await integrationSnapshot();
  const credentials = (await Promise.all(root.users.map(async (profile) => {
    const snapshot = await integrationSnapshot({ user: profile.id });
    return snapshot.connections
      .filter((row) => row.source === "direct" && (row.provider === "telegram" || row.provider === "discord"))
      .map((row) => ({ userLabel: profile.label, ...row }));
  }))).flat();
  const channels = await Promise.all(state.channels.map(async (row) => {
    try {
      const connection = await resolveIntegration(row.provider, row.credential);
      return { ...row, capabilities: channelProvider(row.provider), connection };
    } catch (error) {
      const code = error instanceof IntegrationError ? error.code : "connection_unavailable";
      return { ...row, capabilities: channelProvider(row.provider), connection: { state: "missing", error: code } };
    }
  }));
  return { ...state, providers: listChannelProviders(), credentials, channels };
}

async function assertCredentialRef(input: ReturnType<typeof parseChannelPatch>) {
  try {
    await directConnectionValues(input.provider, { user: input.credential.user, connection: input.credential.connection });
  } catch (error) {
    if (error instanceof IntegrationError) throw new ChannelError(error.code, error.status);
    throw error;
  }
}

export async function createChannelConfig(raw: Record<string, unknown>) {
  const parsed = parseChannelPatch(raw);
  await assertCredentialRef(parsed);
  return createChannel(raw, parsed.workflowId ? await reviewedChannelBinding(parsed.workflowId, "") : undefined);
}

export async function updateChannelConfig(id: string, expectedRevision: number, raw: Record<string, unknown>) {
  const previous = await channelById(id);
  const parsed = parseChannelPatch(raw, previous);
  await assertCredentialRef(parsed);
  return updateChannel(id, expectedRevision, raw, raw.workflowId !== undefined && parsed.workflowId ? await reviewedChannelBinding(parsed.workflowId, id) : undefined);
}

export const deleteChannelConfig = deleteChannel;

export async function testChannel(id: string) {
  const channel = await channelById(id);
  let ok = false;
  let detail = "Verification failed";
  try {
    const credential = await values(channel);
    const result = channel.provider === "telegram" ? await verifyTelegram(credential) : await verifyDiscord(credential);
    ok = true;
    detail = result.detail;
  } catch (error) {
    detail = error instanceof Error ? error.message : "verification_failed";
  }
  const stored = await recordChannelCheck(id, ok, detail);
  return { ok, detail, channel: stored };
}

export async function sendChannelText(id: string, input: { target?: string; text: string }) {
  const channel = await channelById(id);
  assertEnabled(channel);
  const target = input.target?.trim() || channel.defaultTarget;
  if (!target) throw new ChannelError("channel_target_required");
  const credential = await values(channel);
  const result = channel.provider === "telegram"
    ? await sendTelegramText(credential, target, input.text)
    : await sendDiscordText(credential, target, input.text);
  await recordChannelActivity(id);
  return { provider: channel.provider, target, ...result };
}

export async function receiveTelegram(id: string, raw: unknown, suppliedSecret: string | null): Promise<ChannelInboundEvent> {
  const channel = await channelById(id);
  if (channel.provider !== "telegram") throw new ChannelError("channel_provider_mismatch", 404);
  assertEnabled(channel);
  const event = normalizeTelegramUpdate(raw);
  // A group wired to an Agent group authenticates by its bound group id plus a
  // leading /discuss command, so it does not require a webhook secret. Bot
  // traffic is dropped here so agents never answer each other into a loop.
  if (channel.agentGroupId) return telegramGroupEvent(event);
  const credential = await values(channel);
  if (!credential.webhookSecret) throw new ChannelError("telegram_webhook_secret_required", 503);
  if (!verifyTelegramSecret(credential, suppliedSecret)) throw new ChannelError("invalid_webhook_signature", 401);
  if (rateLimited(`channel-authenticated:${id}`, 120, 60_000)) throw new ChannelError("rate_limited", 429);
  return event;
}

const DISCUSS = /^\/discuss(?:@\w+)?\s+([\s\S]+)$/;

// Returns the discussion topic for a group message that belongs to an Agent
// group, or throws to drop anything that is not an owner command.
export function telegramGroupEvent(event: ChannelInboundEvent): ChannelInboundEvent {
  const raw = event.raw as { message?: { from?: { is_bot?: boolean } } } | undefined;
  if (raw?.message?.from?.is_bot) throw new ChannelError("telegram_bot_message_ignored", 202);
  if (event.kind !== "message") throw new ChannelError("telegram_group_command_required", 202);
  const topic = event.text?.trim().match(DISCUSS)?.[1]?.trim();
  if (!topic) throw new ChannelError("telegram_group_command_required", 202);
  return { ...event, text: topic };
}

export async function receiveDiscord(
  id: string,
  bodyText: string,
  timestamp: string | null,
  signature: string | null,
): Promise<{ event: ChannelInboundEvent | null; ping: boolean }> {
  const channel = await channelById(id);
  if (channel.provider !== "discord") throw new ChannelError("channel_provider_mismatch", 404);
  assertEnabled(channel);
  const credential = await values(channel);
  if (!credential.publicKey) throw new ChannelError("discord_public_key_required", 503);
  if (!verifyDiscordSignature(credential, timestamp, signature, bodyText)) throw new ChannelError("invalid_webhook_signature", 401);
  if (rateLimited(`channel-authenticated:${id}`, 120, 60_000)) throw new ChannelError("rate_limited", 429);
  let raw: unknown;
  try { raw = JSON.parse(bodyText); } catch { throw new ChannelError("invalid_discord_interaction"); }
  if (!discordApplicationMatches(credential, raw)) throw new ChannelError("discord_application_mismatch", 401);
  if ((raw as Record<string, unknown>).type === 1) {
    await recordChannelActivity(id);
    return { event: null, ping: true };
  }
  const event = normalizeDiscordInteraction(raw);
  return { event, ping: false };
}