This commit is contained in:
Shawn Morreau
2025-11-26 15:14:21 -05:00
16 changed files with 76701 additions and 111145 deletions
+53 -27
View File
@@ -10,6 +10,7 @@ import type { AgentResult } from "./agents/shared.ts";
import type { AgentName, AgentName as AgentNameType, Payload } from "./external.ts";
import { agentsManifest } from "./external.ts";
import { createMcpConfigs } from "./mcp/config.ts";
import { startMcpHttpServer } from "./mcp/server.ts";
import { modes } from "./modes.ts";
import packageJson from "./package.json" with { type: "json" };
import { fetchRepoSettings } from "./utils/api.ts";
@@ -48,39 +49,29 @@ export interface MainResult {
}
export async function main(inputs: Inputs): Promise<MainResult> {
let githubInstallationToken: string | undefined;
let pollInterval: NodeJS.Timeout | null = null;
let mcpServerClose: (() => Promise<void>) | undefined;
let githubInstallationToken: string | undefined;
try {
// parse payload early to extract agent
const payload = parsePayload(inputs);
// resolve agent before initializing context
githubInstallationToken = await setupGitHubInstallationToken();
const repoContext = parseRepoContext();
const { agentName, agent } = await resolveAgent(
inputs,
payload,
githubInstallationToken,
repoContext
);
const partialCtx = await initializeContext(
inputs,
agentName,
agent,
githubInstallationToken,
repoContext
);
const partialCtx = await initializeContext(inputs, payload);
const ctx = partialCtx as MainContext;
ctx.payload = payload;
githubInstallationToken = ctx.githubInstallationToken;
setupGitAuth(ctx.githubInstallationToken, ctx.repoContext);
setupGitAuth({
githubInstallationToken: ctx.githubInstallationToken,
repoContext: ctx.repoContext,
});
await setupTempDirectory(ctx);
setupMcpLogPolling(ctx);
pollInterval = ctx.pollInterval;
setupGitBranch(ctx.payload);
await startMcpServer(ctx);
mcpServerClose = ctx.mcpServerClose;
setupMcpServers(ctx);
await installAgentCli(ctx);
validateApiKey(ctx);
@@ -99,6 +90,9 @@ export async function main(inputs: Inputs): Promise<MainResult> {
if (pollInterval) {
clearInterval(pollInterval);
}
if (mcpServerClose) {
await mcpServerClose();
}
if (githubInstallationToken) {
await revokeInstallationToken(githubInstallationToken);
}
@@ -179,6 +173,8 @@ interface MainContext {
mcpLogPath: string;
pollInterval: NodeJS.Timeout | null;
payload: Payload;
mcpServerUrl: string;
mcpServerClose: () => Promise<void>;
mcpServers: ReturnType<typeof createMcpConfigs>;
cliPath: string;
apiKey: string;
@@ -186,21 +182,33 @@ interface MainContext {
async function initializeContext(
inputs: Inputs,
agentName: AgentNameType,
agent: (typeof agents)[AgentNameType],
githubInstallationToken: string,
repoContext: RepoContext
): Promise<Omit<MainContext, "payload" | "mcpServers" | "cliPath" | "apiKey">> {
payload: Payload
): Promise<
Omit<MainContext, "mcpServerUrl" | "mcpServerClose" | "mcpServers" | "cliPath" | "apiKey">
> {
log.info(`🐸 Running pullfrog/action@${packageJson.version}...`);
Inputs.assert(inputs);
setupGitConfig();
const githubInstallationToken = await setupGitHubInstallationToken();
const repoContext = parseRepoContext();
// resolve agent and update payload with resolved agent name
const { agentName, agent } = await resolveAgent(
inputs,
payload,
githubInstallationToken,
repoContext
);
const resolvedPayload = { ...payload, agent: agentName };
return {
inputs,
githubInstallationToken,
repoContext,
agentName,
agent,
payload: resolvedPayload,
sharedTempDir: "",
mcpLogPath: "",
pollInterval: null,
@@ -293,9 +301,27 @@ function parsePayload(inputs: Inputs): Payload {
}
}
function setupMcpServers(ctx: MainContext): void {
async function startMcpServer(ctx: MainContext): Promise<void> {
// Set environment variables for MCP server tools
const repoContext = parseRepoContext();
const githubRepository = `${repoContext.owner}/${repoContext.name}`;
const allModes = [...modes, ...(ctx.payload.modes || [])];
ctx.mcpServers = createMcpConfigs(ctx.githubInstallationToken, allModes, ctx.payload);
process.env.GITHUB_INSTALLATION_TOKEN = ctx.githubInstallationToken;
process.env.GITHUB_REPOSITORY = githubRepository;
process.env.PULLFROG_MODES = JSON.stringify(allModes);
process.env.PULLFROG_PAYLOAD = JSON.stringify(ctx.payload);
// GITHUB_RUN_ID is already set in GitHub Actions, no need to set it here
const { url, close } = await startMcpHttpServer();
ctx.mcpServerUrl = url;
ctx.mcpServerClose = close;
log.info(`🚀 MCP server started at ${url}`);
}
function setupMcpServers(ctx: MainContext): void {
ctx.mcpServers = createMcpConfigs(ctx.mcpServerUrl);
log.debug(`📋 MCP Config: ${JSON.stringify(ctx.mcpServers, null, 2)}`);
}