{"version":3,"file":"server.cjs","names":["DEFAULT_AUTO_STOP_DELAYS","decideOnRecheck","decideOnSettled","defineRoot","createBackgroundWorkGate","defineHook","defineHook","defineHook","defineHook","defineServerPlugin","rootSession","composeRootHooks","hookAgentSettled","hookAgentStart","hookSessionShutdown","hookSessionTree"],"sources":["../../src/services/serverIdleShutdown/index.ts","../../src/extensions/workspaces/sessions/(backend)/root.server.ts","../../src/extensions/workspaces/sessions/(backend)/hook/agent-settled.server.ts","../../src/extensions/workspaces/sessions/(backend)/hook/agent-start.server.ts","../../src/extensions/workspaces/sessions/(backend)/hook/session-shutdown.server.ts","../../src/extensions/workspaces/sessions/(backend)/hook/session-tree.server.ts","../../generated/server.ts"],"sourcesContent":["import type { DoomHeadlessHostService, DoomHeadlessHook } from '@agimon-ai/doompi-core/headless';\n\nimport { DEFAULT_AUTO_STOP_DELAYS } from '../../constants/idlePolicy';\nimport { decideOnRecheck, decideOnSettled } from '../idlePolicy';\nimport type { ServerIdleShutdown } from './type';\nexport function createServerIdleShutdown(\n  host: DoomHeadlessHostService,\n  hasActiveBackgroundWork: () => boolean = () => false,\n): ServerIdleShutdown {\n  let timer: ReturnType<typeof setTimeout> | undefined;\n  let watching = false;\n  const stopWatching = (): void => {\n    if (timer !== undefined) clearTimeout(timer);\n    timer = undefined;\n    watching = false;\n  };\n  const schedule = (delayMs: number): void => {\n    stopWatching();\n    watching = true;\n    timer = setTimeout(() => {\n      timer = undefined;\n      if (!watching) return;\n      void host.context.session\n        .activity()\n        .then((activity) => {\n          if (!watching) return;\n          if (hasActiveBackgroundWork()) {\n            schedule(DEFAULT_AUTO_STOP_DELAYS.cooldownMs);\n            return;\n          }\n          const decision = decideOnRecheck(activity, DEFAULT_AUTO_STOP_DELAYS);\n          if (decision.action === 'shutdown') host.context.shutdown();\n          else if (decision.action === 'stand-down') stopWatching();\n          else schedule(decision.delayMs);\n        })\n        .catch(() => stopWatching());\n    }, delayMs);\n  };\n\n  const hooks: DoomHeadlessHook[] = [\n    {\n      event: 'agent_start',\n      handle: stopWatching,\n    },\n    {\n      event: 'agent_settled',\n      async handle() {\n        const decision = decideOnSettled(await host.context.session.activity(), DEFAULT_AUTO_STOP_DELAYS);\n        if (decision.action === 'recheck') schedule(decision.delayMs);\n        else stopWatching();\n      },\n    },\n    {\n      event: 'session_shutdown',\n      handle: stopWatching,\n    },\n    {\n      event: 'session_tree',\n      async handle() {\n        if (!watching) return;\n        if (hasActiveBackgroundWork()) {\n          schedule(DEFAULT_AUTO_STOP_DELAYS.cooldownMs);\n          return;\n        }\n        const decision = decideOnRecheck(await host.context.session.activity(), DEFAULT_AUTO_STOP_DELAYS);\n        if (decision.action === 'shutdown') host.context.shutdown();\n        else if (decision.action === 'stand-down') stopWatching();\n        else schedule(decision.delayMs);\n      },\n    },\n  ];\n  return { hooks, dispose: stopWatching };\n}\n","import { defineRoot } from '@agimon-ai/doompi-core/extensionFile';\nimport type { DoomServerPluginContext } from '@agimon-ai/doompi-core/serverFacet';\n\nimport { createBackgroundWorkGate } from '../../../../services/backgroundWorkGate';\nimport { createServerIdleShutdown } from '../../../../services/serverIdleShutdown';\nexport default defineRoot(({ agent }: DoomServerPluginContext) => {\n  if (!agent) throw new Error('Autostop requires a session host.');\n  const backgroundWork = createBackgroundWorkGate();\n  const watch = createServerIdleShutdown(agent, () => backgroundWork.hasActiveWork(agent.context.sessionId));\n  return { value: watch, services: [backgroundWork.plugin], onDispose: watch.dispose };\n});\n","import { defineHook, type WithRoot } from '@agimon-ai/doompi-core/extensionFile';\nimport type { DoomServerPluginContext } from '@agimon-ai/doompi-core/serverFacet';\n\ntype AutoStopServerScope = Awaited<ReturnType<typeof import('../root.server').default>>['value'];\nexport default defineHook((context: WithRoot<DoomServerPluginContext, AutoStopServerScope>) =>\n  context.root.hooks.find((hook) => hook.event === 'agent_settled')!,\n);\n","import { defineHook, type WithRoot } from '@agimon-ai/doompi-core/extensionFile';\nimport type { DoomServerPluginContext } from '@agimon-ai/doompi-core/serverFacet';\n\ntype AutoStopServerScope = Awaited<ReturnType<typeof import('../root.server').default>>['value'];\nexport default defineHook((context: WithRoot<DoomServerPluginContext, AutoStopServerScope>) =>\n  context.root.hooks.find((hook) => hook.event === 'agent_start')!,\n);\n","import { defineHook, type WithRoot } from '@agimon-ai/doompi-core/extensionFile';\nimport type { DoomServerPluginContext } from '@agimon-ai/doompi-core/serverFacet';\n\ntype AutoStopServerScope = Awaited<ReturnType<typeof import('../root.server').default>>['value'];\nexport default defineHook((context: WithRoot<DoomServerPluginContext, AutoStopServerScope>) =>\n  context.root.hooks.find((hook) => hook.event === 'session_shutdown')!,\n);\n","import { defineHook, type WithRoot } from '@agimon-ai/doompi-core/extensionFile';\nimport type { DoomServerPluginContext } from '@agimon-ai/doompi-core/serverFacet';\n\ntype AutoStopServerScope = Awaited<ReturnType<typeof import('../root.server').default>>['value'];\nexport default defineHook((context: WithRoot<DoomServerPluginContext, AutoStopServerScope>) =>\n  context.root.hooks.find((hook) => hook.event === 'session_tree')!,\n);\n","// Generated by @agimon-ai/doompi-build. Do not edit by hand.\nimport { defineServerPlugin, type DoomServerSessionPlugin } from '@agimon-ai/doompi-core/serverFacet';\nimport { composeRootHooks } from '@agimon-ai/doompi-core/extensionFile';\n\nimport rootSession from '../src/extensions/workspaces/sessions/(backend)/root.server';\nimport hookAgentSettled from '../src/extensions/workspaces/sessions/(backend)/hook/agent-settled.server';\nimport hookAgentStart from '../src/extensions/workspaces/sessions/(backend)/hook/agent-start.server';\nimport hookSessionShutdown from '../src/extensions/workspaces/sessions/(backend)/hook/session-shutdown.server';\nimport hookSessionTree from '../src/extensions/workspaces/sessions/(backend)/hook/session-tree.server';\n\ntype Factory<T, C> = (context: C) => T;\nconst at = <T, C>(value: T | Factory<T, C>, context: C): T =>\n  typeof value === 'function' ? (value as Factory<T, C>)(context) : value;\nconst via = <T>(identity: Record<string, unknown>, value: unknown): T =>\n  (value === undefined ? undefined : { ...identity, ...(value as object) }) as T;\n\nexport const facet = defineServerPlugin({\n  name: '@agimon-ai/doompi-autostop',\n  session: async (context) => {\n    const scopeSession = await rootSession(context);\n    const contextSession = { ...context, root: scopeSession.value };\n    return {\n      ...composeRootHooks(scopeSession),\n      get hooks(): DoomServerSessionPlugin['hooks'] { return [via({ event: 'agent_settled' }, at(hookAgentSettled, contextSession)), via({ event: 'agent_start' }, at(hookAgentStart, contextSession)), via({ event: 'session_shutdown' }, at(hookSessionShutdown, contextSession)), via({ event: 'session_tree' }, at(hookSessionTree, contextSession))]; },\n      services: [...(scopeSession.services ?? [])],\n      get activities(): DoomServerSessionPlugin['activities'] { return [...(scopeSession.activities ?? [])]; },\n    };\n  },\n});\n\nexport default facet;\n"],"mappings":";;;;;;;;;AAKA,SAAgB,yBACd,MACA,gCAA+C,OAC3B;CACpB,IAAI;CACJ,IAAI,WAAW;CACf,MAAM,qBAA2B;EAC/B,IAAI,UAAU,KAAA,GAAW,aAAa,KAAK;EAC3C,QAAQ,KAAA;EACR,WAAW;CACb;CACA,MAAM,YAAY,YAA0B;EAC1C,aAAa;EACb,WAAW;EACX,QAAQ,iBAAiB;GACvB,QAAQ,KAAA;GACR,IAAI,CAAC,UAAU;GACf,KAAU,QAAQ,QACf,SAAS,CAAC,CACV,MAAM,aAAa;IAClB,IAAI,CAAC,UAAU;IACf,IAAI,wBAAwB,GAAG;KAC7B,SAASA,mBAAAA,yBAAyB,UAAU;KAC5C;IACF;IACA,MAAM,WAAWC,mBAAAA,gBAAgB,UAAUD,mBAAAA,wBAAwB;IACnE,IAAI,SAAS,WAAW,YAAY,KAAK,QAAQ,SAAS;SACrD,IAAI,SAAS,WAAW,cAAc,aAAa;SACnD,SAAS,SAAS,OAAO;GAChC,CAAC,CAAC,CACD,YAAY,aAAa,CAAC;EAC/B,GAAG,OAAO;CACZ;CAkCA,OAAO;EAAE,OAAA;GA/BP;IACE,OAAO;IACP,QAAQ;GACV;GACA;IACE,OAAO;IACP,MAAM,SAAS;KACb,MAAM,WAAWE,mBAAAA,gBAAgB,MAAM,KAAK,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