102 lines · 3.2 KB
import { loadRepoById } from "../repos";
import type { WebhookEvent } from "../events/emit";
import { repoHub } from "./hub-do";
import { toJsonText } from "./protocol";

const MAX_TEXT = 200;
const MAX_COMMITS = 20;

/** Event types agents can subscribe to: webhook events plus coordination changes. */
export const AGENT_EVENT_TYPES = [
  "push",
  "create",
  "delete",
  "issues",
  "issue_comment",
  "pull_request",
  "pull_request_review",
  "pull_request_review_comment",
  "release",
  "workflow_run",
  "claims",
] as const;

function pick(source: unknown, keys: string[]): Record<string, unknown> {
  if (typeof source !== "object" || source === null) return {};
  const out: Record<string, unknown> = {};
  for (const key of keys) {
    const value: unknown = Reflect.get(source, key);
    if (typeof value === "string") out[key] = value.slice(0, MAX_TEXT);
    else if (typeof value === "number" || typeof value === "boolean" || value === null) out[key] = value;
  }
  return out;
}

/** A bounded summary of a webhook payload: enough to react to, never whole bodies or diffs. */
export function summarizeEvent(event: WebhookEvent, payload: Record<string, unknown>): Record<string, unknown> {
  const sender = pick(payload.sender, ["login"]);
  const base = { action: typeof payload.action === "string" ? payload.action : null, sender: sender.login ?? null };
  switch (event) {
    case "push": {