import "./paths-D4_LjrY7.js";
import "./utils-BnlaT0eO.js";
import "./theme-CG-K7m8u.js";
import "./thinking-CKGMvVgd.js";
import "./subsystem-ocBR131w.js";
import "./runtime-CRvfzclG.js";
import "./exec-CZr_EJHt.js";
import "./agent-scope-CVnlY0oG.js";
import "./model-selection-p6J3l4qF.js";
import "./github-copilot-token-CjSKohyA.js";
import "./emitter-oHo8y9ow.js";
import "./sqlite-wYNyDR1B.js";
import "./reliability-Dl7hbRzF.js";
import "./boolean-DGGu1sGA.js";
import "./env-CvUd308i.js";
import "./validation-p1IK70TM.js";
import "./config-DJ3A5x_1.js";
import "./manifest-registry-U2EDFVP4.js";
import "./redact-DcYCKU6u.js";
import "./pi-embedded-helpers-CLkeTWAT.js";
import "./sandbox-C5rKvtrA.js";
import "./chrome-BQkAVfz8.js";
import "./tailscale-CcUMoDkd.js";
import "./auth-BxKMeEw-.js";
import "./server-context-s_zNpSdX.js";
import "./frontmatter-VFr0KMAQ.js";
import "./skills-CHYyY1lO.js";
import "./routes-CT0DmotX.js";
import "./errors-DbYSuCgv.js";
import "./paths-Ct4sqLyX.js";
import "./ssrf-tneKFjA6.js";
import "./image-ops-C0D77XHD.js";
import "./store-Cdm3Z5cQ.js";
import "./ports-DvLDsgl0.js";
import "./trash-BRBQoy9m.js";
import { u as updateSessionStore } from "./sessions-Ct2d_bGl.js";
import "./message-channel-DSU96OKy.js";
import "./dock-DY0TWaYa.js";
import "./normalize-DgwtBdm-.js";
import "./accounts-BZv5WxME.js";
import "./accounts-DFaskvEN.js";
import "./accounts-T7o73ZSN.js";
import "./bindings-BbaWTQJM.js";
import "./logging-wfRfbMvt.js";
import "./plugins-DYgcd4QJ.js";
import { t as resolveDefaultSessionStorePath } from "./paths-DRyRQF9o.js";
import "./tool-images-CRPqucMe.js";
import "./tokens-DuLZdp8u.js";
import "./heartbeat-BJ0mtjXV.js";
import "./route-reply-C3GnJgPP.js";
import "./chat-trace-DE0u11RV.js";
import "./response-prefix-template-ZNVQwQzJ.js";
import { a as jsonResult, u as readStringParam } from "./common-oaZdjdal.js";
import "./gmail-setup-utils-BRj-aPVG.js";
import "./gog-credentials-Cu_qOd32.js";
import { i as getServerPort, n as generateState, o as createGoogleAuthProvider, r as getRedirectUri, t as addPendingFlow } from "./gog-oauth-server-CIRZLPno.js";
import "./env-file-jMSm0wPK.js";
import { Type } from "@sinclair/typebox";

//#region src/agents/tools/auth/gog-auth-start-tool.ts
/**
* gog_auth_start tool - Start non-blocking Google OAuth flow
*
* Delegates URL building and scope resolution to GoogleAuthProvider.
* Tool name kept as "gog_auth_start" for backward compatibility.
*/
function createGogAuthStartTool(opts) {
	const provider = createGoogleAuthProvider();
	const serviceLiterals = provider.getSupportedServices().map((s) => Type.Literal(s));
	return {
		label: "Google Auth Start",
		name: "gog_auth_start",
		description: "Start non-blocking Google OAuth flow for Gmail, Calendar, Drive, and other Google services. Returns an authorization URL for the user to visit. The agent remains responsive while waiting for authorization. IMPORTANT: Always use the default services (gmail, calendar, drive) unless the user explicitly requests only specific services. Do NOT narrow to a single service — users expect all Google services to work once authenticated.",
		parameters: Type.Object({
			email: Type.String({
				description: "Google account email address",
				minLength: 1
			}),
			services: Type.Optional(Type.Array(Type.Union(serviceLiterals), { description: "Google services to authorize (default: gmail, calendar, drive)" }))
		}),
		execute: async (_toolCallId, args) => {
			const params = args;
			const email = readStringParam(params, "email", { required: true });
			let services = params.services || [
				"gmail",
				"calendar",
				"drive"
			];
			if (opts?.agentId && opts?.sessionKey) {
				const existing = await provider.loadCredentials(opts.agentId, opts.sessionKey, email);
				if (existing?.services?.length) services = [...new Set([...existing.services, ...services])];
			}
			if (!email.includes("@")) return jsonResult({ error: "Invalid email address format" });
			if (!getServerPort()) return jsonResult({ error: "OAuth server is not running. Please contact the administrator." });
			if (!opts?.agentId || !opts?.agentDir || !opts?.sessionKey) return jsonResult({ error: "Missing agent context (agentId, agentDir, or sessionKey)" });
			const state = generateState();
			const now = Date.now();
			const expiresAt = now + 300 * 1e3;
			const redirectUri = getRedirectUri();
			let authUrl;
			try {
				authUrl = provider.buildAuthUrl({
					email,
					services,
					redirectUri,
					state
				});
			} catch {
				return jsonResult({ error: "Google OAuth client ID not configured. Options (checked in order):\n  1. Set hooks.gogOAuth.googleClientCredentialsFile in minion.json (path to downloaded Google client_secret JSON)\n  2. Set GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET environment variables\n  3. Place gog CLI credentials in ~/.config/gogcli/credentials.json" });
			}
			addPendingFlow({
				state,
				sessionKey: opts.sessionKey,
				agentId: opts.agentId,
				email,
				services,
				requestedAt: now,
				expiresAt,
				authUrl,
				providerId: provider.id
			});
			await updateSessionStore(resolveDefaultSessionStorePath(opts.agentId), (store) => {
				const session = store[opts.sessionKey];
				if (session) {
					session.gogAuthPending = {
						state,
						requestedAt: now,
						expiresAt,
						email,
						services
					};
					session.updatedAt = Date.now();
				}
			});
			return jsonResult({
				authUrl,
				state,
				expiresIn: 300,
				instructions: `IMPORTANT: You MUST paste the full authUrl as plain text in your response — do NOT wrap it in markdown link format like [text](url) or [url](url). The messaging platform auto-links plain URLs; markdown formatting breaks the URL and causes auth errors. Just paste the raw URL on its own line. The user will visit this link to authorize access to their Google account (${services.join(", ")}). You'll be notified when authentication completes (or if it times out after 5 minutes).`
			});
		}
	};
}

//#endregion
export { createGogAuthStartTool };