session.ts 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * The outward session face. Feature packages never see the concrete Session
  3. * class: components read lifecycle state through `useSession` (the
  4. * ObservableSnapshot half), and orchestration code calls the behavior verbs
  5. * below — nothing else. Widening this interface is the explicit act of
  6. * widening what features may do to a session (and what every test fixture
  7. * must stub); implementation-internal entry points (history staging, wire-frame
  8. * dispatch) stay on the class, invisible out here.
  9. */
  10. import type { AttachmentIdType, ImageAttachmentRef } from '@deepseek-ai/dsh-attachment'
  11. import type { MessageId } from '@deepseek-ai/dsh-llm/brand'
  12. import type { SessionId } from '@deepseek-ai/dsh-session/types'
  13. import type { RemoteResult } from '@deepseek-ai/dsh-typert-protocol'
  14. import type { ObservableSnapshot } from '@deepseek-ai/dsh-client-store'
  15. import type { PromptContentPart, QueueAction } from '../../types.ts'
  16. import type { ClientResult } from './result.ts'
  17. import type { SessionSnapshot } from './snapshot.ts'
  18. /** Key-addressed projection read face (the useProjection resolution path; see ProjectionValueStore). */
  19. export interface ProjectionsFace {
  20. /**
  21. * The identity-stable bare observable for one projection key (absence is
  22. * an `undefined` snapshot, never a missing face).
  23. * @param key - projection key.
  24. * @returns the key's value face.
  25. */
  26. faceOf(key: string): ObservableSnapshot<unknown>
  27. }
  28. /** Identity plus the behavior verbs features may invoke on a session. */
  29. export interface ISession {
  30. /** The session's host identity (agent id — same axis). */
  31. readonly sessionId: SessionId
  32. /** Host-computed projection values by key (the useProjection seat). */
  33. readonly projections: ProjectionsFace
  34. /**
  35. * Send a prompt into the session.
  36. * @param content - text plus browser-owned temporary image uploads.
  37. * @param mode - 'queue' appends a turn; 'steer' interrupts the running one.
  38. * @returns acceptance, or the business error (also mirrored into snapshot.promptError).
  39. */
  40. prompt(
  41. content: PromptContentPart[],
  42. mode: 'queue' | 'steer',
  43. signal?: AbortSignal,
  44. ): Promise<ClientResult<{ accepted: true }>>
  45. /**
  46. * Resolve one durable image referenced by this session.
  47. * @param attachmentId - opaque id found in the folded session log.
  48. * @returns the authenticated reference and decoded bytes.
  49. */
  50. readAttachment(
  51. attachmentId: AttachmentIdType,
  52. ): Promise<ClientResult<{ attachment: ImageAttachmentRef; data: Uint8Array }>>
  53. /**
  54. * Apply one edit, remove, or strict steer action to a still-pending queue occurrence.
  55. * @param itemId - agent-owned inbox occurrence identity.
  56. * @param action - requested queue operation.
  57. * @returns acceptance, or a business/transport error.
  58. */
  59. updateQueue(itemId: MessageId, action: QueueAction): Promise<ClientResult<{ accepted: true }>>
  60. /**
  61. * Cancel the running turn. Pending queued work remains and resumes in FIFO
  62. * order after the Host reaches cancellation quiescence.
  63. * @returns acceptance, or the business error.
  64. */
  65. cancel(): Promise<ClientResult<{ accepted: true }>>
  66. /**
  67. * Rename this session (explicit user title; pins it against automatic
  68. * regeneration).
  69. * @param title - raw title text (the host normalizes acceptance).
  70. * @returns the normalized accepted title and its event seq, or the business error.
  71. */
  72. rename(title: string): Promise<ClientResult<{ title: string; seq: number }>>
  73. /**
  74. * Extend the history window backwards (older messages pagination).
  75. * @returns completion; failures land in snapshot.openState/loadingOlder.
  76. */
  77. loadOlder(): Promise<void>
  78. /**
  79. * Execute one slash-command line against this session's agent — pure
  80. * admission semantics (the host executor durably logs the lifecycle).
  81. * @param line - the full command line, leading slash included.
  82. * @returns the admission result, or the Remote face's error branch.
  83. */
  84. command(line: string): Promise<RemoteResult<{ matched: boolean }>>
  85. }
  86. /**
  87. * The full outward face: behavior verbs plus the Session lifecycle read side
  88. * (the `useSession` hook source). This is the type carried by
  89. * `SessionBinding.session` and the provide channel.
  90. */
  91. export type SessionFace = ISession & ObservableSnapshot<SessionSnapshot>