Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

12.3 KB
Raw Blame
import type {
  CandidateDestination,
  CommandApprovalGrant,
  Destination,
  EgressPolicy,
  Principal,
  ScopeId,
  SessionEntry,
  TurnRequest,
  ActorAssertion,
} from "../../types.ts";
import { turnOriginRequestFields } from "../turn-origin.ts";
import { isOverheardEntry } from "../../sessions/session-store.ts";
import type { DeliveryStore } from "../../delivery/delivery-store.ts";
import type { SkillBundle, SkillBundleStore } from "../../skills/skill-bundle-store.ts";
import type { SkillResolution } from "../../skills/skill-store.ts";
import { hostMatches } from "../../resolution/egress-policy.ts";
import { swallow } from "../../util/errors.ts";
import { hashId } from "../../util/crypto.ts";
import type { OrchestratorInput } from "./types.ts";

export const MAX_AUTO_ATTACHMENT_SCREEN_BYTES = 12_000;

export function isScreenableTextAttachment(mimetype: string): boolean {
  const mime = mimetype.split(";", 1)[0]!.trim().toLowerCase();
  return (
    mime.startsWith("text/") ||
    [
      "application/json",
      "application/xml",
      "application/javascript",
      "application/yaml",
      "application/x-yaml",
    ].includes(mime)
  );
}

export function stripAckPrefix(text: string, ack: string | undefined): string {
  if (!text || !ack) return text;
  const lead = text.trimStart();
  if (!lead.startsWith(ack)) return text;
  return lead.slice(ack.length).trimStart();
}

export function headLooksLikeText(head: Buffer): boolean {
  if (head.length === 0) return true;
  if (head.includes(0)) return false;
  let suspicious = 0;
  for (const ch of head.toString("utf8")) {
    const c = ch.codePointAt(0)!;
    if (c === 0xfffd || (c < 0x20 && c !== 0x09 && c !== 0x0a && c !== 0x0d)) suspicious++;
  }
  return suspicious / head.length < 0.1;
}

const CONNECTOR_SKILL_PROVIDERS: Readonly<Record<string, string>> = {
  dropbox: "dropbox",
  "email-draft-in-voice": "google",
  "email-voice-profile": "google",
  "google-drive-sheets": "google",
  "google-workspace": "google",
  linear: "linear",
  "morning-digest": "x",
  "slack-drafts": "slack",
  x: "x",
  zoom: "zoom",
};

export function filterConnectorSkills(
  resolved: SkillResolution[],
  availableProviders: readonly string[],
): SkillResolution[] {
  const available = new Set(availableProviders);
  return resolved.filter((entry) => {
    const provider = entry.skill ? CONNECTOR_SKILL_PROVIDERS[entry.skill.manifest.name] : undefined;
    return !provider || available.has(provider);
  });
}

export async function loadActiveBundles(store: SkillBundleStore, resolved: SkillResolution[]): Promise<SkillBundle[]> {
  const packIds = [...new Set(resolved.map((r) => r.skill?.pack?.packId).filter((id): id is string => Boolean(id)))];
  const bundles = await Promise.all(packIds.map((id) => store.get(id)));
  return bundles.filter((b): b is SkillBundle => b !== null);
}

export function egressClaimAllowingControlPlane(
  egress: EgressPolicy,
  apiBaseUrl: string,
  denyPrivateNetworks = false,
): EgressPolicy | undefined {
  const allowedHosts = egress.allowedHosts ?? [];
  const deniedHosts = egress.deniedHosts ?? [];
  if (allowedHosts.length === 0 && deniedHosts.length === 0 && !denyPrivateNetworks) return undefined;
  let coreHost = "";
  if (apiBaseUrl) {
    try {
      coreHost = new URL(apiBaseUrl).hostname;
    } catch (e) {
      swallow("orchestrator: apiBaseUrl parse", e);
    }
  }
  const safeDenied = coreHost ? deniedHosts.filter((d) => !hostMatches(coreHost, d)) : deniedHosts;
  const allowedWithCore =
    coreHost && allowedHosts.length > 0 && !allowedHosts.includes(coreHost)
      ? [...allowedHosts, coreHost]
      : allowedHosts;
  const claim: EgressPolicy = {
    allowedHosts: allowedWithCore,
    ...(denyPrivateNetworks ? { denyPrivateNetworks: true } : {}),
    ...(denyPrivateNetworks && coreHost ? { privateNetworkAllowedHosts: [coreHost] } : {}),
    ...(safeDenied.length ? { deniedHosts: safeDenied } : {}),
  };
  return claim.allowedHosts.length || claim.deniedHosts?.length || claim.denyPrivateNetworks ? claim : undefined;
}

export function stripTurnBoilerplate(text: string): string {
  const kept = text
    .split("\n\n")
    .filter((p) => !/^\s*\[/.test(p))
    .join("\n\n")
    .trim();
  return kept || text.trim();
}

export function visibleTitleEntryText(entry: SessionEntry): string | undefined {
  const payload = entry.payload as { text?: unknown; display?: unknown } | null;
  const value = entry.type === "user" && typeof payload?.display === "string" ? payload.display : payload?.text;
  return typeof value === "string" ? value.trim() || undefined : undefined;
}

export function renderTitleTranscript(entries: SessionEntry[]): string {
  const lines: string[] = [];
  for (const e of entries) {
    if (e.type !== "user" && e.type !== "assistant") continue;
    if (isOverheardEntry(e)) continue;
    const raw = visibleTitleEntryText(e);
    if (!raw) continue;
    const text = e.type === "user" ? stripTurnBoilerplate(raw) : raw;
    if (!text) continue;
    lines.push(`${e.type === "user" ? "User" : "Assistant"}:\n${text}`);
    if (lines.length >= 6) break;
  }
  return lines.join("\n\n");
}

const DELIVERY_NOTE_ITEM_MAX_CHARS = 200;

export async function recentPrincipalDeliveryNote(
  deliveries: DeliveryStore | undefined,
  threadRef: string,
  since = 0,
): Promise<string> {
  if (!deliveries) return "";
  const recent = (await deliveries.listByRecipientThread(threadRef, { limit: 5 })).filter(
    (d) => (d.deliveredAt ?? d.createdAt) > since,
  );
  if (!recent.length) return "";
  const lines = recent.map((d) => {
    const from = d.destination.onBehalfOf ? ` from ${d.destination.onBehalfOf}` : "";
    const trigger = d.provenance?.trigger ? ` [${d.provenance.trigger}]` : "";
    const flattened = d.text.trim().replace(/\s+/g, " ");
    const text =
      flattened.length > DELIVERY_NOTE_ITEM_MAX_CHARS
        ? `${flattened.slice(0, DELIVERY_NOTE_ITEM_MAX_CHARS)}…`
        : flattened;
    return `- ${new Date(d.deliveredAt ?? d.createdAt).toISOString()}${trigger}${from}: ${text}`;
  });
  return ["Delivered here since the user's last message (delivery events, not your turns):", ...lines].join("\n");
}

function principalAssertion(p: Principal): ActorAssertion {
  return {
    externalId: p.id,
    ...(p.type === "guest" ? { isExternalGuest: true } : {}),
    ...(p.teamIds ? { teamIds: p.teamIds } : {}),
    ...(p.displayName ? { displayName: p.displayName } : {}),
  };
}

export function replayableRequest(input: OrchestratorInput): TurnRequest {
  const c = input.conversation;
  return {
    surface: input.surface ?? "unknown",
    ...(input.slackSource ? { slackSource: input.slackSource } : {}),
    ...(input.externalSlack ? { externalSlack: input.externalSlack } : {}),
    ...(input.sessionSenderId ? { sessionSenderId: input.sessionSenderId } : {}),
    ...(input.delegatingRunId ? { delegatingRunId: input.delegatingRunId } : {}),
    ...(input.scopeVersion ? { scopeVersion: input.scopeVersion } : {}),
    ...(input.deliveryTarget ? { deliveryTarget: input.deliveryTarget } : {}),
    ...(input.deliveryCandidates?.length ? { deliveryCandidates: input.deliveryCandidates } : {}),
    actor: principalAssertion(input.actor),
    conversation: {
      kind: c.kind,
      threadRef: c.threadRef,
      ...(c.channelRef ? { channelRef: c.channelRef } : {}),
      ...(c.channelName ? { channelName: c.channelName } : {}),
      audience: c.audience.map(principalAssertion),
      ...(c.isPrivate !== undefined ? { isPrivate: c.isPrivate } : {}),
      ...(c.isMpim !== undefined ? { isMpim: c.isMpim } : {}),
      ...(c.publishMembers ? { publishMembers: c.publishMembers.map(principalAssertion) } : {}),
    },
    text: input.text,
    ...(input.gatewayContext ? { gatewayContext: input.gatewayContext } : {}),
    ...(input.analyticsSuppressed ? { analyticsSuppressed: true } : {}),
    ...(input.proactiveOpener ? { proactiveOpener: true } : {}),
    ...turnOriginRequestFields(input.origin),
    ...(input.conversationHeader ? { conversationHeader: input.conversationHeader } : {}),
    ...(input.priorTurns?.length ? { priorTurns: input.priorTurns } : {}),
    ...(input.overheard?.length ? { overheard: input.overheard } : {}),
    ...(input.detectContext ? { detectContext: input.detectContext } : {}),
    ...(input.detectOpener ? { detectOpener: input.detectOpener } : {}),
    ...(input.attachments?.length ? { attachments: input.attachments } : {}),
    ...(input.harness ? { harness: input.harness } : {}),
    ...(input.model ? { model: input.model } : {}),
    ...(input.thinkingLevel ? { thinkingLevel: input.thinkingLevel } : {}),
    ...(input.fastMode !== undefined ? { fastMode: input.fastMode } : {}),
    ...(input.readOnly ? { readOnly: true } : {}),
    ...(input.privateSessionMessage
      ? { privateSessionMessage: true as const, sessionMessageDepth: input.sessionMessageDepth }
      : {}),
    ...(input.skipMemory ? { skipMemory: true } : {}),
    ...(input.surfaceTools ? { surfaceTools: true } : {}),
    ...(input.addressed ? { addressed: true } : {}),
    ...(input.envelopeWrapped ? { envelopeWrapped: true } : {}),
    ...(input.displayText ? { displayText: input.displayText } : {}),
    ...(typeof input.turnWallClockMs === "number" ? { turnWallClockMs: input.turnWallClockMs } : {}),
    ...(input.timezone ? { timezone: input.timezone } : {}),
  };
}

export function approvalGrantId(
  grant: Pick<CommandApprovalGrant, "actorId" | "command" | "scope" | "sessionId" | "approvalKey">,
): string {
  return hashId([grant.actorId, grant.scope, grant.sessionId ?? "*", grant.approvalKey ?? grant.command], 24);
}

function candidateKey(target: string, audienceScopeId: ScopeId): string {
  return hashId([target, audienceScopeId]);
}

const SURFACE_ENQUEUE_ACTIONS = new Set(["post", "reach", "react", "edit", "delete"]);

export function completedSurfaceEnqueues(
  entries: readonly SessionEntry[],
  sinceSeq: number,
  surfaceTool: string,
): number {
  const calls: Array<{ enqueues: boolean; callId: string | undefined; done: boolean }> = [];
  for (const e of entries) {
    if (e.seq <= sinceSeq) continue;
    if (e.type === "tool_call") {
      const p = e.payload as { tool?: unknown; action?: unknown; callId?: unknown } | null;
      calls.push({
        enqueues: p?.tool === surfaceTool && SURFACE_ENQUEUE_ACTIONS.has(String(p?.action)),
        callId: typeof p?.callId === "string" ? p.callId : undefined,
        done: false,
      });
    } else if (e.type === "tool_result") {
      const payload = e.payload as { callId?: unknown; interrupted?: unknown } | null;
      if (payload?.interrupted === true) continue;
      const cid = payload?.callId;
      const call =
        typeof cid === "string" ? calls.find((c) => c.callId === cid && !c.done) : calls.findLast((c) => !c.done);
      if (call) call.done = true;
    }
  }
  return calls.filter((c) => c.enqueues && c.done).length;
}

export interface TurnPostKeys {
  seed(completed: number): void;
  take(): number;
  key(destination: Destination, seq: number): string;
}

export function turnPostKeys(turnKey: string): TurnPostKeys {
  let next = 0;
  return {
    seed(completed) {
      next = completed;
    },
    take: () => next++,
    key: (destination, seq) => `post:${turnKey}:${destination.type}:${destination.target}:${seq}`,
  };
}

export function replaceThreadSegment(target: string, threadTs: string): string {
  const i = target.lastIndexOf(":");
  const channel = i < 0 ? target : target.slice(0, i);
  return `${channel}:${threadTs}`;
}

export function deliveryCandidatesFor(
  surface: string | undefined,
  deliveryTarget: string | undefined,
  surfaceCandidates: { target: string; label: string }[] | undefined,
  sessionScopeId: ScopeId,
): { candidates: CandidateDestination[]; defaultKey?: string } {
  if (!surface) return { candidates: [] };
  let raw: { target: string; label: string }[] = [];
  if (surfaceCandidates && surfaceCandidates.length > 0) raw = surfaceCandidates;
  else if (deliveryTarget) raw = [{ target: deliveryTarget, label: "this conversation" }];
  const seen = new Set<string>();
  const candidates: CandidateDestination[] = [];
  for (const c of raw) {
    const key = candidateKey(c.target, sessionScopeId);
    if (seen.has(key)) continue;
    seen.add(key);
    candidates.push({ type: surface, target: c.target, audienceScopeId: sessionScopeId, key, label: c.label });
  }
  const defaultKey = candidates.find((c) => c.target === deliveryTarget)?.key ?? candidates[0]?.key;
  return { candidates, ...(defaultKey ? { defaultKey } : {}) };
}