{"version":3,"file":"skillRuntime.mjs","names":[],"sources":["../../../../../../../src/extensions/workspaces/sessions/(backend)/_lib/skillRuntime.ts"],"sourcesContent":["import { requireDoomConfigContext } from '@agimon-ai/doompi-config';\nimport { requireHarnessRoot } from '@agimon-ai/doompi-config/harnessStore';\nimport { DOOM_CONFIG_SERVICE } from '@agimon-ai/doompi-core/config';\nimport { DOOM_CORDIS_SESSION_SERVICE, type DoomCordisSessionService } from '@agimon-ai/doompi-core/cordis-host';\nimport { DOOM_HELP_SERVICE, requireDoomHelpService } from '@agimon-ai/doompi-core/help';\nimport { type PiPluginContributions } from '@agimon-ai/doompi-core/pi-extension';\nimport {\n  createDoomSkillSourcesService,\n  DOOM_SKILL_SOURCES_SERVICE,\n  type DoomSkillSourcesService,\n} from '@agimon-ai/doompi-core/skills';\nimport { DOOM_UI_HUB_SERVICE, requireDoomUiHub } from '@agimon-ai/doompi-core/ui-hub';\nimport type { Context } from '@deepseek-ai/cordis';\nimport type { ExtensionAPI, ExtensionContext } from '@earendil-works/pi-coding-agent';\n\nimport { createActiveHelpSkillView } from '../../../../../services/helpSkills';\nimport { SKILLS_LEADER_CONTRIBUTION } from '../../../../../types/skills';\nimport { createSkillReadiness, skillInventory } from './skillReadiness';\nimport { createSkillsCommand, type SkillsCommandDependencies } from './skillsCommand';\n\n/**\n * The catalogue and the overlay load on first use.\n *\n * Discovery has to start with the session, but building the catalogue and\n * rendering it are only reached by `/skills`, so they stay off the startup path.\n */\nfunction lazyModules() {\n  let catalog: Promise<typeof import('../../../../../services/skillCatalog')> | undefined;\n  let deferred: Promise<typeof import('../../../../../services/deferredSkills')> | undefined;\n  return {\n    catalog: () => (catalog ??= import('../../../../../services/skillCatalog')),\n    deferred: () => (deferred ??= import('../../../../../services/deferredSkills')),\n  };\n}\n\ninterface SkillPluginConfig {\n  readonly pi: ExtensionAPI;\n  readonly openOverlay: SkillsCommandDependencies['openOverlay'];\n}\n\nexport function createSkillRuntime({ pi, openOverlay }: SkillPluginConfig): PiPluginContributions {\n  const load = lazyModules();\n  let activeContext: Context | undefined;\n  let activeSources: DoomSkillSourcesService | undefined;\n  const bindConfig = (cordis: Context) => {\n    cordis.inject([DOOM_CONFIG_SERVICE], (context) => {\n      activeContext = context;\n      return () => {\n        if (activeContext === context) activeContext = undefined;\n      };\n    });\n  };\n  const requireRuntimeContext = (): Context => {\n    if (!activeContext) throw new Error('Doom skill runtime is waiting for the session config service.');\n    return activeContext;\n  };\n  const requireSkillSources = (): DoomSkillSourcesService => {\n    if (!activeSources) throw new Error('Doom skill sources are waiting for the active session service.');\n    return activeSources;\n  };\n  const bindSources = (cordis: Context) => {\n    cordis.inject([DOOM_CORDIS_SESSION_SERVICE], (sessionContext) => {\n      const session = sessionContext.get(DOOM_CORDIS_SESSION_SERVICE) as DoomCordisSessionService;\n      const service = createDoomSkillSourcesService(`${session.generation}:skill-sources`);\n      sessionContext.plugin((providerContext) => {\n        providerContext.provide(DOOM_SKILL_SOURCES_SERVICE, service);\n      });\n      activeSources = service;\n      return () => {\n        if (activeSources === service) activeSources = undefined;\n        service.dispose();\n      };\n    });\n  };\n  const helpSkillView = createActiveHelpSkillView();\n\n  const bindHelp = (cordis: Context) => {\n    cordis.inject([DOOM_HELP_SERVICE], (helpContext) => {\n      const help = requireDoomHelpService(helpContext);\n      const unbind = helpSkillView.bind(help);\n      return () => {\n        unbind();\n      };\n    });\n  };\n  const bindUi = (cordis: Context) => {\n    cordis.inject([DOOM_UI_HUB_SERVICE], (uiContext) => {\n      const contribution = requireDoomUiHub(uiContext).registerLeader(SKILLS_LEADER_CONTRIBUTION);\n      return () => contribution.dispose();\n    });\n  };\n  const readiness = createSkillReadiness(pi, helpSkillView, load.deferred, requireRuntimeContext);\n  return {\n    services: [bindConfig, bindSources, bindHelp, bindUi],\n    commands: [\n      createSkillsCommand({\n        cordisContext: requireRuntimeContext,\n        extensionSources: () => requireSkillSources().list(),\n        inventory: async (ctx) => {\n          return skillInventory(helpSkillView, readiness)(ctx);\n        },\n        repositoryRoot: async (_ctx: ExtensionContext) =>\n          requireHarnessRoot(requireDoomConfigContext(requireRuntimeContext()).harness),\n        buildCatalog: async (request) => (await load.catalog()).buildSkillCatalog(request),\n        openOverlay,\n      }),\n    ],\n    events: readiness.events,\n    onStop: readiness.onStop,\n    onDispose() {\n      helpSkillView.dispose();\n    },\n  };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AA0BA,SAAS,cAAc;CACrB,IAAI;CACJ,IAAI;CACJ,OAAO;EACL,eAAgB,YAAY,OAAO;EACnC,gBAAiB,aAAa,OAAO;CACvC;AACF;AAOA,SAAgB,mBAAmB,EAAE,IAAI,eAAyD;CAChG,MAAM,OAAO,YAAY;CACzB,IAAI;CACJ,IAAI;CACJ,MAAM,cAAc,WAAoB;EACtC,OAAO,OAAO,CAAC,mBAAmB,IAAI,YAAY;GAChD,gBAAgB;GAChB,aAAa;IACX,IAAI,kBAAkB,SAAS,gBAAgB,KAAA;GACjD;EACF,CAAC;CACH;CACA,MAAM,8BAAuC;EAC3C,IAAI,CAAC,eAAe,MAAM,IAAI,MAAM,+DAA+D;EACnG,OAAO;CACT;CACA,MAAM,4BAAqD;EACzD,IAAI,CAAC,eAAe,MAAM,IAAI,MAAM,gEAAgE;EACpG,OAAO;CACT;CACA,MAAM,eAAe,WAAoB;EACvC,OAAO,OAAO,CAAC,2BAA2B,IAAI,mBAAmB;GAC/D,MAAM,UAAU,eAAe,IAAI,2BAA2B;GAC9D,MAAM,UAAU,8BAA8B,GAAG,QAAQ,WAAW,eAAe;GACnF,eAAe,QAAQ,oBAAoB;IACzC,gBAAgB,QAAQ,4BAA4B,OAAO;GAC7D,CAAC;GACD,gBAAgB;GAChB,aAAa;IACX,IAAI,kBAAkB,SAAS,gBAAgB,KAAA;IAC/C,QAAQ,QAAQ;GAClB;EACF,CAAC;CACH;CACA,MAAM,gBAAgB,0BAA0B;CAEhD,MAAM,YAAY,WAAoB;EACpC,OAAO,OAAO,CAAC,iBAAiB,IAAI,gBAAg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