index.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /**
  2. * Exclusive named registration for the computer-use capability.
  3. * @module @deepseek-ai/dsh-computer-use
  4. */
  5. import { Context, Service } from '@deepseek-ai/cordis'
  6. import type { ComputerUseProviderName } from './brand.ts'
  7. declare module '@deepseek-ai/cordis' {
  8. interface Context {
  9. computerUse: ComputerUseRegistry
  10. }
  11. }
  12. /** Owns one optional provider registration in the shared computer-use service. */
  13. export class ComputerUseRegistry extends Service {
  14. private registration: ComputerUseProviderName | undefined
  15. constructor(ctx: Context) {
  16. super(ctx, 'computerUse')
  17. }
  18. /** Name of the registered provider, including while its resources are closing. */
  19. get providerName(): ComputerUseProviderName | undefined {
  20. return this.registration
  21. }
  22. /**
  23. * Reserve the sole provider slot until the contribution is disposed.
  24. * A second registration fails even when it repeats the current name. Providers
  25. * must stop their tools and await owned work before releasing this registration.
  26. * @param name - provider-owned name used in registration diagnostics.
  27. * @returns the effect disposer for this exact registration.
  28. */
  29. register(name: ComputerUseProviderName): () => Promise<void> {
  30. if (this.registration !== undefined) {
  31. throw new Error(`computer use provider "${this.registration}" is already registered`)
  32. }
  33. return this.ctx.effect(() => {
  34. this.registration = name
  35. return () => {
  36. this.registration = undefined
  37. }
  38. }, 'computerUse.register()')
  39. }
  40. }
  41. export default ComputerUseRegistry