import { projectLocaleToken, type Locale } from 'zhin.js/i18n'; import { databaseHostToken, definePlugin, definePluginCapabilityGraph, outboundHostToken, pluginConsoleRpcToken, type DeliveryTarget, } from "zhin.js"; import { httpHostToken } from "@zhin.js/host-http"; import { createCalendarNotifier } from "./src/notify.js"; import { assertCalendarTimezone, type CalendarConfig, type CalendarConsoleEventItem, createCalendarConsoleRpc, createCalendarProvider, createOAuthTokenManagers, } from "./src/index.js"; import { buildRedirectUri, CalendarAuthService, DEFAULT_OAUTH_CALLBACK_PATH, localOriginFromAddress, } from "./src/chat-auth.js"; import { CalendarCredentialStore, type CalendarStateStore, } from "./src/credential-store.js"; import { registerOAuthCallbackRoute } from "./src/http-callback.js"; import { calendarRuntimeStore } from "./src/runtime.js"; export type { CalendarConsoleEventItem }; export default definePlugin({ name: "calendar", capabilityGraph: definePluginCapabilityGraph({ provides: [calendarRuntimeStore, pluginConsoleRpcToken], injects: [databaseHostToken, outboundHostToken, httpHostToken, projectLocaleToken], features: { commands: ["calendar/auth", "calendar/logout", "calendar/status"], skills: ["calendar-scheduling"], tools: [ "skill/calendar-scheduling/create_event", "skill/calendar-scheduling/find_free_time", "skill/calendar-scheduling/list_events", ], pages: ["index"], }, }), setup(context) { const config = context.config.get(); assertCalendarTimezone(config.timezone); const resources = context.resources; const credentials = resources.has(databaseHostToken) ? new CalendarCredentialStore( resources.use(databaseHostToken) .state as unknown as CalendarStateStore ) : undefined; const outbound = resources.has(outboundHostToken) ? resources.use(outboundHostToken) : undefined; const http = resources.has(httpHostToken) ? resources.use(httpHostToken) : undefined; const notify = createCalendarNotifier(outbound, (target) => context.isEnabledFor(target)); const callbackPath = config.oauth_callback_path?.trim() || DEFAULT_OAUTH_CALLBACK_PATH; const resolveRedirectUri = (): string | undefined => { const configuredBase = config.oauth_redirect_base_url?.trim() ?? ""; if (configuredBase) { return buildRedirectUri(configuredBase, callbackPath); } const address = http?.address; if (!address) { return undefined; } return buildRedirectUri(localOriginFromAddress(address), callbackPath); }; const managers = createOAuthTokenManagers(config, { credentials, onAuthorizationLost: (provider, message) => auth.notifyAuthorizationLost(provider, message), }); let projectLocale: Locale | undefined; try { projectLocale = context.resources.use(projectLocaleToken); } catch { projectLocale = undefined; } const auth = new CalendarAuthService({ config, locale: projectLocale, managers, credentials, notify, resolveRedirectUri, }); const provider = createCalendarProvider(config, { managers }); const runtime = { config, provider, auth: auth as CalendarAuthService, }; calendarRuntimeStore.provide(context, runtime); if (config.provider !== "none" && !provider) { console.warn( "[calendar] provider 已配置但凭据为空,日历工具将不可用;Google/Microsoft 请先配置 client_id,再在聊天中发送 calendar auth " ); } const releaseRoute = http ? registerOAuthCallbackRoute(http, callbackPath, (searchParams) => auth.handleCallback(searchParams) ) : undefined; return { consoleRpc: createCalendarConsoleRpc(runtime), dispose: () => { releaseRoute?.(); }, }; }, });