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.

2.8 KB
Raw Blame
import { createAgentSession } from "@/lib/agent/session-store";
import type { CapabilityRuntime } from "@/lib/capabilities/runtime";
import type { Scope } from "@/lib/capabilities/scope";
import type { CapabilityTool } from "@/lib/capabilities/tool";
import { findActiveChannelSource } from "@/lib/workflow/graph-triggers";
import { startWorkflowGraph } from "@/lib/workflow/graph-engine";
import { startAgentGroupRun } from "@/lib/agent-group";
import { ChannelError } from "./errors";
import { channelById, recordChannelActivity } from "./store";
import type { ChannelInboundEvent } from "./types";
import { admitChannelEvent } from "./event-admission";
import { createHash } from "node:crypto";

export type ChannelWorkflowRuntime = {
  scope: Scope;
  capabilities?: CapabilityRuntime;
  resolveTool: (name: string) => CapabilityTool | undefined;
};

function workflowInput(channel: Awaited<ReturnType<typeof channelById>>, event: ChannelInboundEvent) {
  const { raw: _raw, ...safeEvent } = event;
  return {
    channel: { id: channel.id, name: channel.name, provider: channel.provider },
    event: safeEvent,
  };
}

export async function dispatchChannelInbound(
  channelId: string,
  event: ChannelInboundEvent,
  runtime: ChannelWorkflowRuntime,
) {
  const channel = await channelById(channelId);
  await recordChannelActivity(channelId);
  if (channel.agentGroupId) {
    if (!await admitChannelEvent(channelId, event.eventId)) return { agentGroup: null, duplicate: true };
    const run = await startAgentGroupRun(channel.agentGroupId, event.text ?? "");
    return { agentGroup: { runId: run.id, groupId: channel.agentGroupId } };
  }
  if (!channel.workflowId) return { workflow: null };

  const source = await findActiveChannelSource(channel.workflowId, channel.id, channel.workflowBinding);
  if (!source) throw new ChannelError("channel_workflow_trigger_not_found", 409);
  if (!await admitChannelEvent(channelId, event.eventId)) return { workflow: null, duplicate: true };

  const session = await createAgentSession(source.principal, "cli", {
    title: "Channel: " + channel.name,
    titleSource: "auto",
  });
  const context = {
    principal: source.principal,
    actor: source.principal,
    sessionId: session.id,
    scope: runtime.scope,
    ...(runtime.capabilities ? { capabilities: runtime.capabilities } : {}),
  } as const;
  const idempotency = "channel:" + createHash("sha256").update(JSON.stringify([channel.id, event.eventId])).digest("hex");
  const run = await startWorkflowGraph(
    source.graph,
    workflowInput(channel, event),
    idempotency,
    context,
    runtime.resolveTool,
    source.principal,
    { type: "channel", nodeId: source.node.id, receivedAt: event.receivedAt },
  );
  return { workflow: { runId: run.id, state: run.state, graphId: source.graph.id } };
}