🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
import { encrypt } from "@langfuse/shared/encryption";
import {
type ActionCreate,
type ActionConfig,
type GitHubDispatchActionConfigWithSecrets,
type GitHubDispatchActionCreate,
isGitHubDispatchActionConfig,
} from "@langfuse/shared";
import {
getActionByIdWithSecrets,
validateWebhookURL,
} from "@langfuse/shared/src/server";
import { TRPCError } from "@trpc/server";
import { areDestinationUrlsEquivalent } from "@/src/features/automations/destinationUrl";
interface GitHubDispatchConfigOptions {
actionConfig: ActionCreate;
actionId?: string;
projectId: string;
}
export async function processGitHubDispatchActionConfig({
actionConfig,
actionId,
projectId,
}: GitHubDispatchConfigOptions): Promise<{
finalActionConfig: ActionConfig;
githubToken?: string; // For one-time display
}> {
if (actionConfig.type !== "GITHUB_DISPATCH") {
throw new Error("Action type is not GITHUB_DISPATCH");
}
const gitHubDispatchConfig = actionConfig as GitHubDispatchActionCreate;
const existingAction = actionId
? await getActionByIdWithSecrets({ projectId, actionId })
: undefined;
let existingActionConfig: GitHubDispatchActionConfigWithSecrets | undefined;
if (existingAction) {
if (!isGitHubDispatchActionConfig(existingAction.config)) {
throw new Error(
`Existing action ${actionId} does not have valid GitHub dispatch configuration`,
);
}
existingActionConfig = existingAction.config;
}
// Determine URL to use
let urlToUse: string;
if (gitHubDispatchConfig.url && gitHubDispatchConfig.url.trim() !== "") {
urlToUse = gitHubDispatchConfig.url;
} else if (existingActionConfig?.url) {
urlToUse = existingActionConfig.url;
} else {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Repository Dispatch URL is required",
});
}
// Validate GitHub API URL format
const urlPattern =
/^https:\/\/api\.github\.com\/repos\/[^\/]+\/[^\/]+\/dispatches$/;
const enterprisePattern =
/^https:\/\/[^\/]+\/api\/v3\/repos\/[^\/]+\/[^\/]+\/dispatches$/;
if (!urlPattern.test(urlToUse) && !enterprisePattern.test(urlToUse)) {
throw new TRPCError({
code: "BAD_REQUEST",
message:
"URL must be a valid GitHub repository dispatch endpoint (e.g., https://api.github.com/repos/owner/repo/dispatches)",
});
}
try {
await validateWebhookURL(urlToUse);
} catch (error) {
throw new TRPCError({
code: "BAD_REQUEST",
message: `Invalid webhook URL: ${error instanceof Error ? error.message : "Unknown error"}`,
});
}
const isUrlChanged =
existingActionConfig !== undefined &&
!areDestinationUrlsEquivalent(urlToUse, existingActionConfig.url);
const newGithubToken =
typeof gitHubDispatchConfig.githubToken === "string" &&
gitHubDispatchConfig.githubToken.trim() !== ""
? gitHubDispatchConfig.githubToken
: undefined;
if (isUrlChanged && !newGithubToken) {
throw new TRPCError({
code: "BAD_REQUEST",
message:
"GitHub Personal Access Token is required when changing the dispatch URL",
});
}
// Determine event type to use
let eventTypeToUse: string;
if (
gitHubDispatchConfig.eventType &&
gitHubDispatchConfig.eventType.trim() !== ""
) {
eventTypeToUse = gitHubDispatchConfig.eventType;
} else if (existingActionConfig?.eventType) {
eventTypeToUse = existingActionConfig.eventType;
} else {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Event type is required",
});
}
let tokenToUse: string;
let displayToken: string;
let returnToken: string | undefined;
if (newGithubToken) {
tokenToUse = newGithubToken;
displayToken = maskGitHubToken(tokenToUse);
returnToken = tokenToUse;
} else if (existingActionConfig?.githubToken) {
tokenToUse = existingActionConfig.githubToken;
displayToken = existingActionConfig.displayGitHubToken;
returnToken = undefined;
} else {
throw new TRPCError({
code: "BAD_REQUEST",
message: "GitHub Personal Access Token is required",
});
}
return {
finalActionConfig: {
type: "GITHUB_DISPATCH",
url: urlToUse,
eventType: eventTypeToUse,
githubToken: returnToken !== undefined ? encrypt(tokenToUse) : tokenToUse,
displayGitHubToken: displayToken,
lastFailingExecutionId: existingActionConfig?.lastFailingExecutionId,
},
githubToken: returnToken,
};
}
function maskGitHubToken(token: string): string {
if (token.length < 6) {
return token;
}
const prefix = token.substring(0, 4);
const suffix = token.substring(token.length - 1);
return `${prefix}...${suffix}`;
}