
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="cfe3089b-ab9f-51c6-8e56-fc47724a45e9")}catch(e){}}();
import { z } from "zod";
import { runtimeCommandRunRequestSchema, runtimeConfigSaveRequestSchema, runtimeClineMcpOAuthRequestSchema, runtimeClineMcpSettingsSaveRequestSchema, runtimeClineOauthLoginRequestSchema, runtimeClineProviderModelsRequestSchema, runtimeClineProviderSettingsSaveRequestSchema, runtimeTaskChatAbortRequestSchema, runtimeTaskChatCancelRequestSchema, runtimeTaskChatMessagesRequestSchema, runtimeTaskChatSendRequestSchema, runtimeGitCheckoutRequestSchema, runtimeHookIngestRequestSchema, runtimeProjectAddRequestSchema, runtimeProjectRemoveRequestSchema, runtimeShellSessionStartRequestSchema, runtimeTaskSessionInputRequestSchema, runtimeTaskSessionStartRequestSchema, runtimeTaskSessionStopRequestSchema, runtimeTaskWorkspaceInfoRequestSchema, runtimeTerminalWsClientMessageSchema, runtimeWorkspaceChangesRequestSchema, runtimeWorkspaceFileSearchRequestSchema, runtimeWorkspaceStateSaveRequestSchema, runtimeWorktreeDeleteRequestSchema, runtimeWorktreeEnsureRequestSchema, } from "./api-contract.js";
const trimmedStringSchema = z.string().transform((value) => value.trim());
const positiveIntegerFromQuerySchema = z.coerce.number().int().positive();
const requiredTrimmedStringSchema = (message) => trimmedStringSchema.pipe(z.string().min(1, message));
function parseWithSchema(schema, value) {
    const parsed = schema.safeParse(value);
    if (!parsed.success) {
        throw new Error(parsed.error.issues[0]?.message ?? "Invalid request payload.");
    }
    return parsed.data;
}
export function parseWorkspaceChangesRequest(query) {
    const taskId = parseWithSchema(requiredTrimmedStringSchema("Missing taskId query parameter."), query.get("taskId") ?? "");
    const baseRef = parseWithSchema(requiredTrimmedStringSchema("Missing baseRef query parameter."), query.get("baseRef") ?? "");
    return parseWithSchema(runtimeWorkspaceChangesRequestSchema, { taskId, baseRef });
}
export function parseTaskWorkspaceInfoRequest(query) {
    const taskId = parseWithSchema(requiredTrimmedStringSchema("Missing taskId query parameter."), query.get("taskId") ?? "");
    const baseRef = parseWithSchema(requiredTrimmedStringSchema("Missing baseRef query parameter."), query.get("baseRef") ?? "");
    return parseWithSchema(runtimeTaskWorkspaceInfoRequestSchema, { taskId, baseRef });
}
export function parseOptionalTaskWorkspaceInfoRequest(query) {
    if (!query.has("taskId")) {
        if (query.has("baseRef")) {
            throw new Error("baseRef query parameter requires taskId.");
        }
        return null;
    }
    return parseTaskWorkspaceInfoRequest(query);
}
export function parseWorkspaceFileSearchRequest(query) {
    const normalizedQuery = parseWithSchema(trimmedStringSchema, query.get("q") ?? "");
    if (!normalizedQuery) {
        return { query: "" };
    }
    const rawLimit = query.get("limit");
    if (rawLimit == null || rawLimit.trim() === "") {
        return parseWithSchema(runtimeWorkspaceFileSearchRequestSchema, {
            query: normalizedQuery,
        });
    }
    const parsedLimit = positiveIntegerFromQuerySchema.safeParse(rawLimit);
    if (!parsedLimit.success) {
        throw new Error("Invalid file search limit parameter.");
    }
    return parseWithSchema(runtimeWorkspaceFileSearchRequestSchema, {
        query: normalizedQuery,
        limit: parsedLimit.data,
    });
}
export function parseGitCheckoutRequest(value) {
    const parsed = parseWithSchema(runtimeGitCheckoutRequestSchema, value);
    const branch = parsed.branch.trim();
    if (!branch) {
        throw new Error("Branch cannot be empty.");
    }
    return {
        branch,
    };
}
export function parseWorktreeEnsureRequest(value) {
    const parsed = parseWithSchema(runtimeWorktreeEnsureRequestSchema, value);
    const taskId = parsed.taskId.trim();
    if (!taskId) {
        throw new Error("Invalid worktree ensure payload.");
    }
    const baseRef = parsed.baseRef.trim();
    if (!baseRef) {
        throw new Error("Invalid worktree ensure payload.");
    }
    return {
        taskId,
        baseRef,
    };
}
export function parseWorktreeDeleteRequest(value) {
    const parsed = parseWithSchema(runtimeWorktreeDeleteRequestSchema, value);
    const taskId = parsed.taskId.trim();
    if (!taskId) {
        throw new Error("Invalid worktree delete payload.");
    }
    return {
        taskId,
    };
}
export function parseWorkspaceStateSaveRequest(value) {
    return parseWithSchema(runtimeWorkspaceStateSaveRequestSchema, value);
}
export function parseProjectAddRequest(value) {
    const parsed = parseWithSchema(runtimeProjectAddRequestSchema, value);
    const path = parsed.path.trim();
    if (!path) {
        throw new Error("Project path cannot be empty.");
    }
    return {
        path,
        initializeGit: parsed.initializeGit,
    };
}
export function parseProjectRemoveRequest(value) {
    const parsed = parseWithSchema(runtimeProjectRemoveRequestSchema, value);
    const projectId = parsed.projectId.trim();
    if (!projectId) {
        throw new Error("Project ID cannot be empty.");
    }
    return {
        projectId,
    };
}
export function parseRuntimeConfigSaveRequest(value) {
    return parseWithSchema(runtimeConfigSaveRequestSchema, value);
}
export function parseCommandRunRequest(value) {
    const parsed = parseWithSchema(runtimeCommandRunRequestSchema, value);
    const command = parsed.command.trim();
    if (!command) {
        throw new Error("Command cannot be empty.");
    }
    return {
        command,
    };
}
export function parseTaskSessionStartRequest(value) {
    const parsed = parseWithSchema(runtimeTaskSessionStartRequestSchema, value);
    const taskId = parsed.taskId.trim();
    if (!taskId) {
        throw new Error("Task session taskId cannot be empty.");
    }
    const baseRef = parsed.baseRef.trim();
    if (!baseRef) {
        throw new Error("Task session baseRef cannot be empty.");
    }
    return {
        ...parsed,
        taskId,
        baseRef,
    };
}
export function parseTaskSessionStopRequest(value) {
    const parsed = parseWithSchema(runtimeTaskSessionStopRequestSchema, value);
    const taskId = parsed.taskId.trim();
    if (!taskId) {
        throw new Error("Invalid task session stop payload.");
    }
    return {
        taskId,
    };
}
export function parseTaskSessionInputRequest(value) {
    const parsed = parseWithSchema(runtimeTaskSessionInputRequestSchema, value);
    const taskId = parsed.taskId.trim();
    if (!taskId) {
        throw new Error("Task session taskId cannot be empty.");
    }
    return {
        ...parsed,
        taskId,
    };
}
export function parseTaskChatMessagesRequest(value) {
    const parsed = parseWithSchema(runtimeTaskChatMessagesRequestSchema, value);
    const taskId = parsed.taskId.trim();
    if (!taskId) {
        throw new Error("Task chat taskId cannot be empty.");
    }
    return {
        taskId,
    };
}
export function parseTaskChatSendRequest(value) {
    const parsed = parseWithSchema(runtimeTaskChatSendRequestSchema, value);
    const taskId = parsed.taskId.trim();
    if (!taskId) {
        throw new Error("Task chat taskId cannot be empty.");
    }
    const text = parsed.text.trim();
    const hasImages = Boolean(parsed.images && parsed.images.length > 0);
    if (!text && !hasImages) {
        throw new Error("Task chat text or images are required.");
    }
    return {
        ...parsed,
        taskId,
        text,
    };
}
export function parseTaskChatAbortRequest(value) {
    const parsed = parseWithSchema(runtimeTaskChatAbortRequestSchema, value);
    const taskId = parsed.taskId.trim();
    if (!taskId) {
        throw new Error("Task chat taskId cannot be empty.");
    }
    return {
        taskId,
    };
}
export function parseTaskChatCancelRequest(value) {
    const parsed = parseWithSchema(runtimeTaskChatCancelRequestSchema, value);
    const taskId = parsed.taskId.trim();
    if (!taskId) {
        throw new Error("Task chat taskId cannot be empty.");
    }
    return {
        taskId,
    };
}
export function parseClineProviderModelsRequest(value) {
    const parsed = parseWithSchema(runtimeClineProviderModelsRequestSchema, value);
    const providerId = parsed.providerId.trim();
    if (!providerId) {
        throw new Error("Provider ID cannot be empty.");
    }
    return {
        providerId,
    };
}
export function parseClineProviderSettingsSaveRequest(value) {
    const parsed = parseWithSchema(runtimeClineProviderSettingsSaveRequestSchema, value);
    const providerId = parsed.providerId.trim();
    if (!providerId) {
        throw new Error("Provider ID cannot be empty.");
    }
    return {
        ...parsed,
        providerId,
    };
}
export function parseClineMcpSettingsSaveRequest(value) {
    const parsed = parseWithSchema(runtimeClineMcpSettingsSaveRequestSchema, value);
    const normalizedServers = parsed.servers.map((server) => {
        const name = server.name.trim();
        if (!name) {
            throw new Error("MCP server name cannot be empty.");
        }
        if (server.type === "stdio") {
            const command = server.command.trim();
            if (!command) {
                throw new Error(`MCP server "${name}" requires a command.`);
            }
            const args = server.args?.map((value) => value.trim()).filter((value) => value.length > 0);
            const cwd = server.cwd?.trim() || undefined;
            const env = server.env
                ? Object.fromEntries(Object.entries(server.env)
                    .map(([key, entry]) => [key.trim(), entry.trim()])
                    .filter(([key, entry]) => key.length > 0 && entry.length > 0))
                : undefined;
            return {
                name,
                disabled: server.disabled,
                type: "stdio",
                command,
                ...(args && args.length > 0 ? { args } : {}),
                ...(cwd ? { cwd } : {}),
                ...(env && Object.keys(env).length > 0 ? { env } : {}),
            };
        }
        const url = server.url.trim();
        if (!url) {
            throw new Error(`MCP server "${name}" requires a URL.`);
        }
        const headers = server.headers
            ? Object.fromEntries(Object.entries(server.headers)
                .map(([key, entry]) => [key.trim(), entry.trim()])
                .filter(([key, entry]) => key.length > 0 && entry.length > 0))
            : undefined;
        return {
            name,
            disabled: server.disabled,
            type: server.type,
            url,
            ...(headers && Object.keys(headers).length > 0 ? { headers } : {}),
        };
    });
    const seen = new Set();
    for (const server of normalizedServers) {
        const dedupeKey = server.name.toLowerCase();
        if (seen.has(dedupeKey)) {
            throw new Error(`MCP server "${server.name}" is duplicated.`);
        }
        seen.add(dedupeKey);
    }
    return {
        servers: normalizedServers,
    };
}
export function parseClineMcpOAuthRequest(value) {
    const parsed = parseWithSchema(runtimeClineMcpOAuthRequestSchema, value);
    const serverName = parsed.serverName.trim();
    if (!serverName) {
        throw new Error("MCP server name cannot be empty.");
    }
    return {
        serverName,
    };
}
export function parseClineOauthLoginRequest(value) {
    const parsed = parseWithSchema(runtimeClineOauthLoginRequestSchema, value);
    return {
        ...parsed,
        baseUrl: typeof parsed.baseUrl === "string" ? parsed.baseUrl.trim() || null : parsed.baseUrl,
    };
}
export function parseShellSessionStartRequest(value) {
    const parsed = parseWithSchema(runtimeShellSessionStartRequestSchema, value);
    const taskId = parsed.taskId.trim();
    if (!taskId) {
        throw new Error("Shell session taskId cannot be empty.");
    }
    if (parsed.workspaceTaskId !== undefined && !parsed.workspaceTaskId.trim()) {
        throw new Error("Invalid shell session workspaceTaskId.");
    }
    const workspaceTaskId = parsed.workspaceTaskId?.trim() || undefined;
    const baseRef = parsed.baseRef.trim();
    if (!baseRef) {
        throw new Error("Shell session baseRef cannot be empty.");
    }
    return {
        ...parsed,
        taskId,
        workspaceTaskId,
        baseRef,
    };
}
export function parseHookIngestRequest(value) {
    const parsed = parseWithSchema(runtimeHookIngestRequestSchema, value);
    const taskId = parsed.taskId.trim();
    const workspaceId = parsed.workspaceId.trim();
    if (!taskId) {
        throw new Error("Missing taskId");
    }
    if (!workspaceId) {
        throw new Error("Missing workspaceId");
    }
    const metadata = parsed.metadata
        ? {
            activityText: parsed.metadata.activityText?.trim(),
            toolName: parsed.metadata.toolName?.trim(),
            finalMessage: parsed.metadata.finalMessage?.trim(),
            hookEventName: parsed.metadata.hookEventName?.trim(),
            notificationType: parsed.metadata.notificationType?.trim(),
            source: parsed.metadata.source?.trim(),
        }
        : undefined;
    return {
        ...parsed,
        taskId,
        workspaceId,
        metadata,
    };
}
export function parseTerminalWsClientMessage(value) {
    const parsed = runtimeTerminalWsClientMessageSchema.safeParse(value);
    if (!parsed.success) {
        return null;
    }
    return parsed.data;
}
//# sourceMappingURL=api-validation.js.map
//# debugId=cfe3089b-ab9f-51c6-8e56-fc47724a45e9
