sessions.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * The outward sessions-service face — what `ctx.sessions` exposes to feature
  3. * packages. Transport entry points and implementation internals stay on
  4. * the concrete class. Widening this interface is the
  5. * explicit act of widening what features may do to the sessions domain.
  6. */
  7. import type { Context } from '@deepseek-ai/cordis'
  8. import type { SubagentAddress } from '@deepseek-ai/dsh-subagent/client'
  9. import type { SessionId } from '@deepseek-ai/dsh-session/types'
  10. import type { WorkspaceId } from '@deepseek-ai/dsh-workspace/types'
  11. import type { AgentContext } from '../scope.ts'
  12. import type { SessionSearchResultItem } from '../sessions/manager.ts'
  13. import type { SessionBinding, SessionListState } from '../sessions/service.ts'
  14. import type { ClientResult } from './result.ts'
  15. import type { SessionFace } from './session.ts'
  16. import type { ObservableSnapshot } from '@deepseek-ai/dsh-client-store'
  17. export type { AgentContext } from '../scope.ts'
  18. /** The sessions-service face injected as `ctx.sessions`. */
  19. export interface ISessions {
  20. /** The useSessions standard feed (list rows + current selection; read face — writes stay inside the domain). */
  21. readonly list: ObservableSnapshot<SessionListState>
  22. /**
  23. * The `session.search` result bound the wire schema fixes, exposed to
  24. * presentation as injected data. Not per-connection state: every transport
  25. * (fixture included) reports the same number.
  26. */
  27. readonly searchResultLimit: number
  28. /**
  29. * Create or adopt a Session on the Host.
  30. * @param opts - target workspace, directory, and optional preallocated identity.
  31. * @returns the Session identity after its local binding is addressable.
  32. */
  33. create(opts?: {
  34. workspaceId?: WorkspaceId
  35. cwd?: string
  36. sessionId?: SessionId
  37. }): Promise<SessionId>
  38. /**
  39. * Select a session as current.
  40. * @param id - session id (must exist in the list; unknown ids fail loud).
  41. */
  42. open(id: SessionId): void
  43. /**
  44. * Open a healthy catalog child through its exact direct-parent address.
  45. * @param address - catalog-derived parent and child ids.
  46. */
  47. openSubagent(address: SubagentAddress): void
  48. /**
  49. * Resolve an already discovered direct-parent address without opening it.
  50. * @param id - possible addressed child id.
  51. * @returns the retained address, when present.
  52. */
  53. subagentAddress(id: SessionId): SubagentAddress | undefined
  54. /**
  55. * Mark whether a catalog menu is consuming live membership updates.
  56. * @param parentSessionId - catalog owner.
  57. * @param open - current menu state.
  58. */
  59. setSubagentCatalogOpen(parentSessionId: SessionId, open: boolean): void
  60. /**
  61. * Refresh one direct-child catalog.
  62. * @param parentSessionId - catalog owner.
  63. * @returns completion of the current or newly started refresh.
  64. */
  65. refreshSubagents(parentSessionId: SessionId): Promise<void>
  66. /** Clear the current selection into the no-session view state. */
  67. clear(): void
  68. /**
  69. * Refresh the Host-authoritative Session list.
  70. * @returns completion of the current or newly started Session-list refresh.
  71. */
  72. refresh(): Promise<void>
  73. /**
  74. * Search the Host's visible message-content index. Results stay
  75. * request-local; the list snapshot remains the metadata authority.
  76. * @param query - non-blank literal phrase.
  77. * @param signal - cancellation for a superseded search.
  78. * @returns bounded results, or a business/transport error.
  79. */
  80. search(
  81. query: string,
  82. signal: AbortSignal,
  83. ): Promise<ClientResult<{ items: SessionSearchResultItem[]; hasMore: boolean }>>
  84. /**
  85. * Fork a session from a completed-turn prefix of the source; on resolution
  86. * the child is in the list store and `open()` can target it.
  87. * @param opts - source session id, the optional event seq anchoring the
  88. * cut (the boundary is the first turn/end at or after it; an in-log
  89. * anchor in an open turn is unavailable rather than clipped backward),
  90. * and whether to increment an inherited durable title before resolving.
  91. * @returns the child session id.
  92. * @throws when the fork fails, or when a requested child-title rename fails after creation.
  93. */
  94. fork(opts: { sessionId: SessionId; atSeq?: number; increaseTitle?: boolean }): Promise<SessionId>
  95. /**
  96. * Resolve an Agent-scoped context view (use-and-discard).
  97. * @param id - session id.
  98. * @returns scoped ctx, or undefined for a session neither listed nor already scoped.
  99. */
  100. scope(id: SessionId): AgentContext | undefined
  101. /**
  102. * Read the Agent scope tag off a context (service-method boundary: fetch
  103. * bundles must reach scope resolution through ctx.sessions).
  104. * @param ctx - any client context.
  105. * @returns the session id, or undefined on root contexts.
  106. */
  107. scopeOf(ctx: Context): SessionId | undefined
  108. /**
  109. * Resolve the session face behind an Agent-scoped context.
  110. * @param ctx - an Agent-scoped context.
  111. * @returns the session face, or undefined when the ctx is untagged or its scope was pruned.
  112. */
  113. sessionOf(ctx: Context): SessionFace | undefined
  114. /**
  115. * Resolve the stable session binding (scope-addressed assembly feed).
  116. * @param id - session id.
  117. * @returns binding, or undefined for a session neither listed nor already scoped.
  118. */
  119. binding(id: SessionId): SessionBinding | undefined
  120. }