index.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /** Client Session object layer, Agent scopes, and Remote lifecycle wiring. */
  2. import type { Context } from '@deepseek-ai/cordis'
  3. import type {} from '@deepseek-ai/dsh-agent/types'
  4. import type { ConnectionHandle } from '@deepseek-ai/dsh-client-connection/client'
  5. import { createSessionControlStream } from './transport.ts'
  6. import { ClientSessions } from './sessions/service.ts'
  7. import type { SessionRemotes } from './sessions/remotes.ts'
  8. import type {} from '../remote-events.ts'
  9. export {
  10. createSessionControlStream,
  11. SessionEventStream,
  12. SESSION_SEARCH_RESULT_LIMIT,
  13. SESSION_SEARCH_SNIPPET_MAX_CODE_POINTS,
  14. sessionStreamFailure,
  15. } from './transport.ts'
  16. export type {
  17. ClientSessionPageRequest,
  18. SessionControlStream,
  19. SessionControlStreamOptions,
  20. SessionEventStreamOptions,
  21. SessionJournalChange,
  22. SessionRemote,
  23. } from './transport.ts'
  24. export { createScope, scopeOf } from './scope.ts'
  25. export type { AgentContext, AgentScopeHandle } from './scope.ts'
  26. export { SessionCreateError, SessionForkError } from './sessions/service.ts'
  27. export type { SessionBinding, SessionListState, SessionSummary } from './sessions/service.ts'
  28. export type {
  29. SessionListPhase,
  30. SessionListSnapshot,
  31. SessionSearchResultItem,
  32. SubagentCatalogSnapshot,
  33. } from './sessions/manager.ts'
  34. export type { Session } from './sessions/session.ts'
  35. export type {
  36. ProjectionsBaseline,
  37. ProjectionValueStore,
  38. SessionProjectionMap,
  39. UseProjection,
  40. } from './sessions/projection-store.ts'
  41. export type {
  42. BeginSubmissionInput,
  43. ISession,
  44. PendingSubmissionRetirement,
  45. ProjectionsFace,
  46. SessionFace,
  47. SubmissionHandle,
  48. } from './contract/session.ts'
  49. export type { ISessions } from './contract/sessions.ts'
  50. export { MutableSessionEventSource } from './contract/events.ts'
  51. export type {
  52. SessionEventChange,
  53. SessionEventLike,
  54. SessionEventLikeEntry,
  55. SessionEventSource,
  56. SessionEventWindow,
  57. SessionLiveEventEntry,
  58. } from './contract/events.ts'
  59. export type {
  60. OpenState,
  61. PendingSubmission,
  62. PendingSubmissionImage,
  63. PromptError,
  64. QueuedMessage,
  65. SessionSnapshot,
  66. } from './contract/snapshot.ts'
  67. export type { ClientFailure, ClientResult } from './contract/result.ts'
  68. declare module '@deepseek-ai/cordis' {
  69. interface Context {
  70. /** Client Session object layer and Agent scope owner. */
  71. sessions: import('./contract/sessions.ts').ISessions
  72. }
  73. }
  74. /** Required wire, Remote, and Context projection services. */
  75. export const inject = [
  76. 'connection',
  77. 'typert',
  78. 'remote',
  79. 'remote.commands',
  80. 'remote.session',
  81. 'remote.subagents',
  82. ]
  83. /**
  84. * Install Client Session state and its reconnecting control stream.
  85. * @param ctx - Client Cordis context.
  86. */
  87. export function apply(ctx: Context): void {
  88. const connection = ctx.get('connection') as ConnectionHandle
  89. const remotes = ctx.remote as unknown as SessionRemotes
  90. const sessions = new ClientSessions(ctx, remotes)
  91. ctx.remote.$on('api-session/added', (summary) => { sessions.handleSessionAdded(summary) })
  92. ctx.remote.$on('api-session/removed', (sessionId) => { sessions.handleSessionRemoved(sessionId) })
  93. ctx.remote.$on('api-session/status', (sessionId, running) => {
  94. sessions.handleSessionStatus(sessionId, running)
  95. })
  96. ctx.remote.$on('api-session/activity', (sessionId, updatedAt) => {
  97. sessions.handleSessionActivity(sessionId, updatedAt)
  98. })
  99. ctx.remote.$on('api-session/error', (sessionId, message) => {
  100. sessions.handleSessionError(sessionId, message)
  101. })
  102. const control = createSessionControlStream(remotes, {
  103. accept: (frame) => { sessions.handleControlFrame(frame) },
  104. failed: (error) => { console.error('[session-controller] control stream failed:', error) },
  105. })
  106. control.start()
  107. ctx.on('connection/reset', () => { sessions.handleConnected() })
  108. if (connection.generation.getSnapshot() !== undefined) sessions.handleConnected()
  109. ctx.typert.contexts.registerClient('agent', {
  110. identity: candidate => sessions.scopeOf(candidate),
  111. resolve: sessionId => sessions.resolveAgentScope(sessionId),
  112. })
  113. ctx.effect(() => async () => { await control.dispose() }, 'session-controller.client.control')
  114. }