service.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /** React-free Client Workspace service and command facade. */
  2. import { Service, type Context } from '@deepseek-ai/cordis'
  3. import type { SessionId } from '@deepseek-ai/dsh-session/types'
  4. import type { RemoteFailure } from '@deepseek-ai/dsh-typert-protocol'
  5. import type { WorkspaceId } from '@deepseek-ai/dsh-workspace/types'
  6. import type { WorkspaceView } from '../types.ts'
  7. import type { ClientWorkspaceModel, WorkspaceSnapshot } from './model.ts'
  8. /** Structured create failure for callers that distinguish Host business errors. */
  9. export class WorkspaceCreateError extends Error {
  10. override readonly name = 'WorkspaceCreateError'
  11. /** @param rpcError - Host business or folded carrier failure. */
  12. constructor(readonly rpcError: RemoteFailure) {
  13. super(`workspace create failed: ${rpcError.code}: ${rpcError.message}`)
  14. }
  15. }
  16. /** Bare observable source for the Workspace Controller snapshot. */
  17. export interface WorkspaceSource {
  18. /** Read the identity-stable current snapshot. */
  19. getSnapshot(): WorkspaceSnapshot
  20. /**
  21. * Subscribe to snapshot changes.
  22. * @param listener - invalidation callback.
  23. * @returns unsubscribe function.
  24. */
  25. subscribe(listener: () => void): () => void
  26. }
  27. /** Workspace Controller's Client service face. */
  28. export interface IWorkspaces {
  29. /** Host-authoritative Workspace rows, order, archive set, and follow lifecycle. */
  30. readonly list: WorkspaceSource
  31. /**
  32. * Register an existing path as a Workspace.
  33. * @param input - Host create payload.
  34. * @returns the created or idempotently resolved Workspace.
  35. */
  36. create(input: { path: string }): Promise<WorkspaceView>
  37. /**
  38. * Rename a Workspace.
  39. * @param workspaceId - target Workspace.
  40. * @param title - new display title.
  41. * @returns the renamed Workspace.
  42. */
  43. rename(workspaceId: WorkspaceId, title: string): Promise<WorkspaceView>
  44. /**
  45. * Delete a Workspace registration without deleting Sessions or files.
  46. * @param workspaceId - target Workspace.
  47. */
  48. delete(workspaceId: WorkspaceId): Promise<void>
  49. /**
  50. * Move a Workspace within the Host registry order.
  51. * @param workspaceId - Workspace to move.
  52. * @param beforeWorkspaceId - anchor Workspace; omitted appends.
  53. */
  54. insertBefore(workspaceId: WorkspaceId, beforeWorkspaceId?: WorkspaceId): Promise<void>
  55. /**
  56. * Archive a Session from Workspace grouping surfaces.
  57. * @param sessionId - Session to archive.
  58. */
  59. archiveSession(sessionId: SessionId): Promise<void>
  60. /**
  61. * Move a Session within one Workspace account.
  62. * @param workspaceId - owning Workspace.
  63. * @param sessionId - Session to move.
  64. * @param beforeSessionId - anchor Session; omitted appends.
  65. * @returns the changed Workspace.
  66. */
  67. insertSessionBefore(
  68. workspaceId: WorkspaceId,
  69. sessionId: SessionId,
  70. beforeSessionId?: SessionId,
  71. ): Promise<WorkspaceView>
  72. }
  73. /** Owns the bare Workspace snapshot and Workspace-only commands. */
  74. export class WorkspaceController extends Service implements IWorkspaces {
  75. readonly list: WorkspaceSource
  76. /**
  77. * @param ctx - Client root Context.
  78. * @param model - Remote-backed Workspace state model.
  79. */
  80. constructor(ctx: Context, private readonly model: ClientWorkspaceModel) {
  81. super(ctx, 'workspaces')
  82. this.list = model
  83. }
  84. async create(input: { path: string }): Promise<WorkspaceView> {
  85. const result = await this.model.create(input)
  86. if (!result.ok) throw new WorkspaceCreateError(result.error)
  87. return result.value.workspace
  88. }
  89. async rename(workspaceId: WorkspaceId, title: string): Promise<WorkspaceView> {
  90. const result = await this.model.rename(workspaceId, title)
  91. if (!result.ok) throw commandError('rename', result.error)
  92. return result.value.workspace
  93. }
  94. async delete(workspaceId: WorkspaceId): Promise<void> {
  95. const result = await this.model.delete(workspaceId)
  96. if (!result.ok) throw commandError('delete', result.error)
  97. }
  98. async insertBefore(workspaceId: WorkspaceId, beforeWorkspaceId?: WorkspaceId): Promise<void> {
  99. const result = await this.model.insertBefore(workspaceId, beforeWorkspaceId)
  100. if (!result.ok) throw commandError('reorder', result.error)
  101. }
  102. async archiveSession(sessionId: SessionId): Promise<void> {
  103. const result = await this.model.archiveSession(sessionId)
  104. if (!result.ok) throw commandError('session archive', result.error)
  105. }
  106. async insertSessionBefore(
  107. workspaceId: WorkspaceId,
  108. sessionId: SessionId,
  109. beforeSessionId?: SessionId,
  110. ): Promise<WorkspaceView> {
  111. const result = await this.model.insertSessionBefore(workspaceId, sessionId, beforeSessionId)
  112. if (!result.ok) throw commandError('move', result.error)
  113. return result.value.workspace
  114. }
  115. }
  116. function commandError(operation: string, failure: RemoteFailure): Error {
  117. return new Error(`workspace ${operation} failed: ${failure.code}: ${failure.message}`)
  118. }