index.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 { ISession, ProjectionsFace, SessionFace } from './contract/session.ts'
  42. export type { ISessions } from './contract/sessions.ts'
  43. export { MutableSessionEventSource } from './contract/events.ts'
  44. export type {
  45. SessionEventChange,
  46. SessionEventLike,
  47. SessionEventLikeEntry,
  48. SessionEventSource,
  49. SessionEventWindow,
  50. SessionLiveEventEntry,
  51. } from './contract/events.ts'
  52. export type {
  53. OpenState,
  54. PromptError,
  55. SessionSnapshot,
  56. } from './contract/snapshot.ts'
  57. export type { ClientFailure, ClientResult } from './contract/result.ts'
  58. declare module '@deepseek-ai/cordis' {
  59. interface Context {
  60. /** Client Session object layer and Agent scope owner. */
  61. sessions: import('./contract/sessions.ts').ISessions
  62. }
  63. }
  64. /** Required wire, Remote, and Context projection services. */
  65. export const inject = [
  66. 'connection',
  67. 'typert',
  68. 'remote',
  69. 'remote.commands',
  70. 'remote.session',
  71. 'remote.subagents',
  72. ]
  73. /**
  74. * Install Client Session state and its reconnecting control stream.
  75. * @param ctx - Client Cordis context.
  76. */
  77. export function apply(ctx: Context): void {
  78. const connection = ctx.get('connection') as ConnectionHandle
  79. const remotes = ctx.remote as unknown as SessionRemotes
  80. const sessions = new ClientSessions(ctx, remotes)
  81. ctx.remote.$on('api-session/added', (summary) => { sessions.handleSessionAdded(summary) })
  82. ctx.remote.$on('api-session/removed', (sessionId) => { sessions.handleSessionRemoved(sessionId) })
  83. ctx.remote.$on('api-session/status', (sessionId, running) => {
  84. sessions.handleSessionStatus(sessionId, running)
  85. })
  86. ctx.remote.$on('api-session/activity', (sessionId, updatedAt) => {
  87. sessions.handleSessionActivity(sessionId, updatedAt)
  88. })
  89. ctx.remote.$on('api-session/error', (sessionId, message) => {
  90. sessions.handleSessionError(sessionId, message)
  91. })
  92. const control = createSessionControlStream(remotes, {
  93. accept: (frame) => { sessions.handleControlFrame(frame) },
  94. failed: (error) => { console.error('[session-controller] control stream failed:', error) },
  95. })
  96. control.start()
  97. ctx.on('connection/reset', () => { sessions.handleConnected() })
  98. if (connection.hostDescription.getSnapshot() !== undefined) sessions.handleConnected()
  99. ctx.typert.contexts.registerClient('agent', {
  100. identity: candidate => sessions.scopeOf(candidate),
  101. resolve: sessionId => sessions.resolveAgentScope(sessionId),
  102. })
  103. ctx.effect(() => async () => { await control.dispose() }, 'session-controller.client.control')
  104. }