Skip to content

Open source CapCut alternative.

TypeScript
3.0 KB
Raw Blame
import { geminiModelRoleNames } from "@/lib/inference/gemini-models";
import { UI_TOOLS } from "@/cut/lib/projectCommands";
import { AI_SKILL_INDEX, readSkill } from "@/cut/server/ai/catalog";
import { projectOperation } from "../projectOperation";
import { useEditor } from "../store";
import { buildAiContext } from "../aiContext";
import { MEDIA_RUNTIME_TOOLS, PAGE_ONLY_TOOLS, runProjectCommand } from "../aiTools";
import { normalizeRef } from "../assetRef";
import { DETACHED_UI_NOTE } from "../chatResume";
import { NO_CREDITS_MESSAGE } from "../credits";
import { hostedPost } from "../hosted";
import { bindHeadlessSession, type HeadlessSession } from "../headless/bind";
import { headlessRuntime } from "../headless/runtime";
import { refsToParts } from "../refMedia";
import type { CutAgentDeps } from "./cutAgent";
import { currentDebris } from "./debris";

// The headless wiring for the pi chat loop. The runner opens a project into
// the store (openProjectDoc), runs the turn with these deps, and pushes the
// doc back up. Every transport rides the session's origin and auth headers.

export type { HeadlessSession };

export function headlessDeps(session: HeadlessSession): CutAgentDeps {
  bindHeadlessSession(session);
  return {
    post: (payload, signal) => hostedPost("/api/inference/responses", payload, signal),
    judge: (payload, signal) => hostedPost("/api/inference/judge", payload, signal),
    execTool: async (name, args) => {
      if (name === "list_skills") return { skills: AI_SKILL_INDEX };
      if (name === "read_skill") {
        const doc = readSkill(String(args.name ?? ""));
        if (!doc) throw new Error(`No such skill. Available: ${AI_SKILL_INDEX.join(", ")}`);
        return doc;
      }
      if (UI_TOOLS.has(name)) return { noEditor: true, note: DETACHED_UI_NOTE };
      // The worker installs canvas and decoders at startup and these run as
      // written. A process that came up without them says so plainly.
      if (PAGE_ONLY_TOOLS.has(name))
        throw new Error("This tool runs in the editor tab, and this session has none. Tell the user to open the project in Donkey Cut and ask again.");
      if (MEDIA_RUNTIME_TOOLS.has(name)) {
        const rt = headlessRuntime();
        if (!rt?.raster || !rt.media)
          throw new Error(
            "This session has no media runtime, so it cannot decode or draw media. Work from the editor state and the transcript instead."
          );
      }
      const projectId = useEditor.getState().projectId;
      if (!projectId) throw new Error("No project open.");
      return runProjectCommand(projectOperation(projectId), name, args);
    },
    models: {
      simple: geminiModelRoleNames.chatSimple,
      complex: geminiModelRoleNames.chat,
    },
    buildContext: () => buildAiContext(),
    resolveRefs: async (meta) => {
      const refs = meta.map(normalizeRef).filter((r) => r !== null);
      return (await refsToParts(refs)).parts;
    },
    debris: currentDebris,
    noCreditsMessage: NO_CREDITS_MESSAGE,
  };
}