import { env } from "./env.js";
import { getLogger as getLogger$1 } from "./logger.js";
import { recoverOrphanedWorkflows, world } from "./workflow/world.js";
import { startOpenTelemetrySDK } from "./instrumentation.js";
import "./sentry.js";
import { startSchedulerWorkflow } from "./domains/run/services/SchedulerService.js";
import { createAgentsHono } from "./createApp.js";
import { createAgentsApp, createAgentsAuth } from "./factory.js";
import { ensurePlaygroundAppConfig } from "./startup/playground-app.js";
import { CredentialStoreRegistry, createDefaultCredentialStores } from "@inkeep/agents-core";
import { Hono } from "hono";
import { createEmailService } from "@inkeep/agents-email";

//#region src/index.ts
startOpenTelemetrySDK();
const logger = getLogger$1("agents-api-init");
const defaultConfig = {
	port: 3002,
	serverOptions: {
		requestTimeout: 12e4,
		keepAliveTimeout: 6e4,
		keepAlive: true
	}
};
const sandboxConfig = process.env.SANDBOX_VERCEL_TEAM_ID && process.env.SANDBOX_VERCEL_PROJECT_ID && process.env.SANDBOX_VERCEL_TOKEN ? {
	provider: "vercel",
	runtime: "node22",
	timeout: 6e4,
	vcpus: 4,
	teamId: process.env.SANDBOX_VERCEL_TEAM_ID,
	projectId: process.env.SANDBOX_VERCEL_PROJECT_ID,
	token: process.env.SANDBOX_VERCEL_TOKEN
} : {
	provider: "native",
	runtime: "node22",
	timeout: 3e4,
	vcpus: 2
};
const socialProviders = process.env.PUBLIC_GOOGLE_CLIENT_ID && process.env.GOOGLE_CLIENT_SECRET ? { google: {
	prompt: "select_account",
	display: "popup",
	clientId: process.env.PUBLIC_GOOGLE_CLIENT_ID,
	clientSecret: process.env.GOOGLE_CLIENT_SECRET
} } : void 0;
const emailService = createEmailService();
const auth = createAgentsAuth({ socialProviders }, emailService);
const app = createAgentsHono({
	serverConfig: defaultConfig,
	credentialStores: new CredentialStoreRegistry(createDefaultCredentialStores()),
	auth,
	sandboxConfig
});
setTimeout(async () => {
	try {
		await ensurePlaygroundAppConfig();
	} catch (err) {
		logger.error({ error: err }, "Failed to configure playground app");
	}
}, 3e3);
const workflowWorld = process.env.WORKFLOW_TARGET_WORLD || "local";
if (workflowWorld === "@workflow/world-postgres" || workflowWorld === "local") {
	const STARTUP_DELAY_MS = 3e3;
	logger.info({
		targetWorld: workflowWorld,
		delayMs: STARTUP_DELAY_MS
	}, "Scheduling workflow world worker start");
	setTimeout(async () => {
		try {
			if (workflowWorld === "@workflow/world-postgres") {
				await world.start();
				logger.info({}, "Workflow world worker started successfully");
			} else logger.info({ targetWorld: workflowWorld }, "Workflow world does not require explicit start");
			const recoveredCount = await recoverOrphanedWorkflows();
			if (recoveredCount > 0) logger.info({ recoveredCount }, "Recovered orphaned workflow(s)");
			await startSchedulerWorkflow();
			logger.info({}, "Scheduler workflow started");
		} catch (err) {
			logger.error({ error: err }, "Failed to start workflow world");
		}
	}, STARTUP_DELAY_MS);
}
if (env.ENVIRONMENT === "development" && env.SLACK_APP_TOKEN) {
	const SOCKET_MODE_DELAY_MS = 3e3;
	logger.info({ delayMs: SOCKET_MODE_DELAY_MS }, "Scheduling Slack Socket Mode start");
	setTimeout(async () => {
		try {
			const { startSocketMode } = await import("@inkeep/agents-work-apps/slack");
			await startSocketMode(env.SLACK_APP_TOKEN);
		} catch (err) {
			if (err.code === "MODULE_NOT_FOUND") logger.error({}, "SLACK_APP_TOKEN is set but @slack/socket-mode is not installed. Run: pnpm add -D @slack/socket-mode (in packages/agents-work-apps)");
			else logger.error({ error: err }, "Failed to start Slack Socket Mode");
		}
	}, SOCKET_MODE_DELAY_MS);
}
var src_default = app;

//#endregion
export { Hono, auth, createAgentsApp, createAgentsHono, src_default as default };