server.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /**
  2. * `HarnessSdkServer`: the JSON-RPC method surface the `dsh-jsonrpc` plugin
  3. * serves to out-of-process SDK clients (e.g. the Python `deepseek_harness`
  4. * package). Requests: `initialize` → `session/prompt`* → `shutdown`.
  5. * Notifications pushed to the host: `session.event` (every durable session
  6. * event, verbatim), `session.finished` (per prompt turn settle),
  7. * `subagent.started` / `subagent.finished` (child-session lineage and run
  8. * outcomes). The server owns only the SDK-facing session map — the harness
  9. * itself is the context the plugin mounts in; plugins, persistence, and
  10. * the LLM adapter set all come from the external `cordis.yml`.
  11. *
  12. * @module @deepseek-ai/dsh-jsonrpc/server
  13. */
  14. import type { Context } from 'cordis'
  15. import { resolve } from 'node:path'
  16. import type { ContentBlock } from '@deepseek-ai/dsh-llm'
  17. import type { AgentHandle } from '@deepseek-ai/dsh-agent'
  18. import { SessionId, type TurnEndReason } from '@deepseek-ai/dsh-session'
  19. import type { SubagentRunEndInfo, SubagentRunInfo } from '@deepseek-ai/dsh-subagent'
  20. import * as LlmDeepSeek from '@deepseek-ai/dsh-llm-deepseek'
  21. import type { JsonRpcTransportPeer } from './transport.ts'
  22. /** Parameters of the `initialize` request (once per process, before any prompt). */
  23. export interface InitializeParams {
  24. /** Working directory recorded on every SDK-created session's header. */
  25. cwd: string
  26. /** Model name every SDK-created agent runs on (see {@link HarnessSdkServer.initialize} for adapter fallback). */
  27. model: string
  28. }
  29. /** Result of the `initialize` request: the server's identity for the SDK handshake. */
  30. export interface InitializeResult {
  31. /** Wire-stable server identity (`deepseek-harness-sdk-runtime`) and version. */
  32. serverInfo: { name: string; version: string }
  33. }
  34. /**
  35. * Parameters of a `session/prompt` request: one user turn on one SDK session,
  36. * with at most one in flight per session.
  37. */
  38. export interface SessionPromptParams {
  39. /** The SDK-side session id; an unknown id lazily creates the agent+session pair. */
  40. sessionId: string
  41. /** The prompt content blocks, sent verbatim as the user message. */
  42. contentBlocks: ContentBlock[]
  43. }
  44. /** Result of a `session/prompt` request: the prompt ran to turn settle (outcome rides on `session.finished`). */
  45. export interface SessionPromptResult {
  46. /** Always `true`; the turn outcome is the paired `session.finished` notification. */
  47. accepted: true
  48. }
  49. interface SessionRecord {
  50. handle: AgentHandle
  51. lastTurnEnd: TurnEndReason | undefined
  52. activePrompt: boolean
  53. }
  54. /** Runtime-local agent identity plus optional durable fork lineage. */
  55. interface LocalAgentRecord {
  56. parentSessionId?: SessionId
  57. }
  58. /** Pending local runs that share one provider/id correlation key. */
  59. interface PendingLocalRuns {
  60. count: number
  61. parentSessionId?: SessionId
  62. parentAmbiguous: boolean
  63. }
  64. /**
  65. * The SDK server over a booted harness context. Constructing it subscribes to
  66. * session, agent, and subagent lifecycle events, forwarding durable session
  67. * events and SDK-facing completion notifications while retaining local-run
  68. * identity across child disposal. The subscriptions live until
  69. * {@link shutdown}. One instance serves one transport peer for the process
  70. * lifetime — there is no re-`initialize`.
  71. */
  72. export class HarnessSdkServer {
  73. private cwd = process.cwd()
  74. private model = 'deepseek'
  75. private llmFiber: { dispose(): Promise<void> } | undefined
  76. private readonly sessions = new Map<string, SessionRecord>()
  77. private readonly sessionCreations = new Map<string, Promise<SessionRecord>>()
  78. private readonly localAgents = new Map<SessionId, LocalAgentRecord>()
  79. private readonly localRuns = new Map<string, Map<SessionId, PendingLocalRuns>>()
  80. private readonly disposers: (() => void)[] = []
  81. private shutdownTask: Promise<Record<string, never>> | undefined
  82. private shuttingDown = false
  83. constructor(
  84. private readonly ctx: Context,
  85. private readonly transport: JsonRpcTransportPeer,
  86. ) {
  87. this.disposers.push(ctx.on('session/event', (session, event) => {
  88. if (event.type === 'turn/end') {
  89. const rec = this.sessions.get(String(session.id))
  90. if (rec) rec.lastTurnEnd = event.data.reason
  91. }
  92. this.transport.notify('session.event', { sessionId: String(session.id), event })
  93. }))
  94. this.disposers.push(ctx.on('session/created', (session) => {
  95. const parentSession = session.header.parentSession
  96. if (parentSession === undefined) return
  97. this.transport.notify('subagent.started', {
  98. parentSessionId: String(parentSession),
  99. childSessionId: String(session.id),
  100. })
  101. }))
  102. // Cache runtime-local identity and optional lineage for each agent lifetime.
  103. // Parent lineage is not required by the provider contract, so an empty
  104. // record remains a load-bearing locality marker.
  105. this.disposers.push(ctx.on('agent/created', (agent) => {
  106. const parentSessionId = agent.session.header.parentSession
  107. this.localAgents.set(agent.id, parentSessionId === undefined ? {} : { parentSessionId })
  108. }))
  109. this.disposers.push(ctx.on('agent/disposed', (agent) => {
  110. this.localAgents.delete(agent.id)
  111. }))
  112. // Snapshot locality per provider/id run key. A provider may settle one run,
  113. // continue the same live child in another run, and dispose that child before
  114. // the later result settles. Counts preserve every completion without
  115. // assuming settlement order. If id reuse produces disagreeing lineage, the
  116. // optional parent is omitted until that pending group drains rather than
  117. // attributed to the wrong completion.
  118. this.disposers.push(ctx.on('subagent/start', (info: SubagentRunInfo) => {
  119. const agent = this.ctx.agents.get(info.id)
  120. const cachedLocalAgent = this.localAgents.get(info.id)
  121. const localAgent = cachedLocalAgent ?? (agent === undefined
  122. ? undefined
  123. : agent.session.header.parentSession === undefined
  124. ? {}
  125. : { parentSessionId: agent.session.header.parentSession })
  126. if (localAgent === undefined) return
  127. const providerRuns = this.localRuns.get(info.provider) ?? new Map<SessionId, PendingLocalRuns>()
  128. const pending = providerRuns.get(info.id)
  129. if (pending === undefined) {
  130. providerRuns.set(info.id, localAgent.parentSessionId === undefined
  131. ? { count: 1, parentAmbiguous: false }
  132. : { count: 1, parentSessionId: localAgent.parentSessionId, parentAmbiguous: false })
  133. } else {
  134. pending.count += 1
  135. if (pending.parentSessionId !== localAgent.parentSessionId) pending.parentAmbiguous = true
  136. }
  137. this.localRuns.set(info.provider, providerRuns)
  138. }))
  139. this.disposers.push(ctx.on('subagent/end', (info: SubagentRunEndInfo) => {
  140. const agent = this.ctx.agents.get(info.id)
  141. const providerRuns = this.localRuns.get(info.provider)
  142. const pending = providerRuns?.get(info.id)
  143. if (pending !== undefined) {
  144. pending.count -= 1
  145. if (pending.count === 0) providerRuns?.delete(info.id)
  146. if (providerRuns?.size === 0) this.localRuns.delete(info.provider)
  147. }
  148. // This protocol reports LOCAL child sessions. A lineage-bearing child
  149. // has the session/created-driven start notification above; a parentless
  150. // local provider still gets its terminal notification. A remote provider
  151. // has neither a cached creation nor a live local agent and is ignored.
  152. if (pending === undefined && agent === undefined) return
  153. const parentSessionId = pending === undefined
  154. ? agent?.session.header.parentSession
  155. : pending.parentAmbiguous ? undefined : pending.parentSessionId
  156. this.transport.notify('subagent.finished', {
  157. provider: info.provider,
  158. agentId: String(info.id),
  159. ...(parentSessionId === undefined ? {} : { parentSessionId: String(parentSessionId) }),
  160. childSessionId: String(info.id),
  161. status: info.stopReason === 'completed' ? 'ok' : 'error',
  162. stopReason: info.stopReason,
  163. ...(info.lastAssistantMessage === undefined ? {} : { lastAssistantMessage: info.lastAssistantMessage }),
  164. })
  165. }))
  166. }
  167. /**
  168. * Handle `initialize`: record the SDK deployment facts (cwd, model) and, when
  169. * no registered adapter serves `params.model`, mount the DeepSeek adapter for
  170. * it (credentials from `$DEEPSEEK_API_KEY`/`$DEEPSEEK_BASE_URL`) — a config
  171. * that already registered an adapter for the model wins.
  172. * @param params - the SDK handshake parameters.
  173. * @returns the server identity for the handshake.
  174. */
  175. async initialize(params: InitializeParams): Promise<InitializeResult> {
  176. this.cwd = resolve(params.cwd)
  177. this.model = params.model
  178. if (!this.llmFiber && !this.hasAdapterFor(this.model)) {
  179. this.llmFiber = await this.ctx.plugin(LlmDeepSeek, { models: [this.model] })
  180. }
  181. return { serverInfo: { name: 'deepseek-harness-sdk-runtime', version: '0.0.1' } }
  182. }
  183. /**
  184. * Handle `session/prompt`: get-or-create the session's agent, send the
  185. * content as the user message, await turn settle (quiescence), then notify
  186. * `session.finished` with the settled turn's outcome. A session accepts at
  187. * most one prompt at a time; an overlapping request fails immediately while
  188. * other sessions remain independent.
  189. * @param params - the target session id and prompt content.
  190. * @returns `{ accepted: true }` after the turn settled.
  191. */
  192. async prompt(params: SessionPromptParams): Promise<SessionPromptResult> {
  193. const rec = await this.getOrCreateSession(params.sessionId)
  194. if (rec.activePrompt) throw new Error(`session already has an active prompt: ${params.sessionId}`)
  195. rec.activePrompt = true
  196. try {
  197. rec.lastTurnEnd = undefined
  198. rec.handle.agent.send(params.contentBlocks)
  199. await rec.handle.agent.whenIdle()
  200. const status = this.finishedStatus(rec.lastTurnEnd)
  201. this.transport.notify('session.finished', {
  202. sessionId: params.sessionId,
  203. status,
  204. reason: rec.lastTurnEnd,
  205. })
  206. return { accepted: true }
  207. } finally {
  208. rec.activePrompt = false
  209. }
  210. }
  211. /**
  212. * Handle `shutdown`: dispose every SDK-created agent handle (awaiting loop
  213. * quiescence), unmount the adapter fiber this server mounted (if any), and
  214. * detach the event subscriptions. The CONTEXT stays up — the bin disposes it
  215. * as part of process exit.
  216. * @returns an empty object (the JSON-RPC result).
  217. */
  218. shutdown(): Promise<Record<string, never>> {
  219. this.shutdownTask ??= this.performShutdown()
  220. return this.shutdownTask
  221. }
  222. private async performShutdown(): Promise<Record<string, never>> {
  223. this.shuttingDown = true
  224. const pendingCreations = [...this.sessionCreations.values()]
  225. await Promise.allSettled(pendingCreations)
  226. this.sessionCreations.clear()
  227. const records = [...this.sessions.values()]
  228. this.sessions.clear()
  229. this.localAgents.clear()
  230. this.localRuns.clear()
  231. const failures: unknown[] = []
  232. while (this.disposers.length > 0) {
  233. try {
  234. this.disposers.pop()?.()
  235. } catch (error) {
  236. failures.push(error)
  237. }
  238. }
  239. const teardownResults = await Promise.allSettled([
  240. ...records.map(rec => Promise.resolve().then(() => rec.handle.dispose())),
  241. ...(this.llmFiber === undefined ? [] : [Promise.resolve().then(() => this.llmFiber?.dispose())]),
  242. ])
  243. this.llmFiber = undefined
  244. failures.push(...teardownResults
  245. .filter((result): result is PromiseRejectedResult => result.status === 'rejected')
  246. .map(result => result.reason as unknown))
  247. if (failures.length === 1) throw failures[0]
  248. if (failures.length > 1) throw new AggregateError(failures, 'SDK server teardown failed')
  249. return {}
  250. }
  251. /**
  252. * Dispatch one incoming JSON-RPC request to its typed handler. Throws (→ a
  253. * JSON-RPC error response) on an unknown method.
  254. * @param method - the JSON-RPC method name.
  255. * @param params - the raw params object from the wire.
  256. * @returns the handler's result, to be serialized as the response.
  257. */
  258. async handleRequest(method: string, params: Record<string, unknown> | undefined): Promise<unknown> {
  259. switch (method) {
  260. case 'initialize':
  261. return this.initialize(params as unknown as InitializeParams)
  262. case 'session/prompt':
  263. return this.prompt(params as unknown as SessionPromptParams)
  264. case 'shutdown':
  265. return this.shutdown()
  266. default:
  267. throw new Error(`unknown DeepSeek Harness SDK runtime method: ${method}`)
  268. }
  269. }
  270. private async getOrCreateSession(sessionId: string): Promise<SessionRecord> {
  271. if (this.shuttingDown) throw new Error('SDK server is shutting down')
  272. const existing = this.sessions.get(sessionId)
  273. if (existing) return existing
  274. const pending = this.sessionCreations.get(sessionId)
  275. if (pending) return pending
  276. const creation = this.createSession(sessionId)
  277. this.sessionCreations.set(sessionId, creation)
  278. void creation.then(
  279. () => { this.sessionCreations.delete(sessionId) },
  280. () => { this.sessionCreations.delete(sessionId) },
  281. )
  282. return creation
  283. }
  284. private async createSession(sessionId: string): Promise<SessionRecord> {
  285. const handle = await this.ctx.agents.create({
  286. sessionId: SessionId(sessionId),
  287. meta: { cwd: this.cwd },
  288. agentOptions: { model: this.model },
  289. })
  290. const rec: SessionRecord = { handle, lastTurnEnd: undefined, activePrompt: false }
  291. this.sessions.set(sessionId, rec)
  292. return rec
  293. }
  294. private finishedStatus(reason: TurnEndReason | undefined): 'ok' | 'error' {
  295. if (!reason) return 'error'
  296. return reason.kind === 'completed' ? 'ok' : 'error'
  297. }
  298. private hasAdapterFor(model: string): boolean {
  299. return this.ctx.get('llm')?.models().includes(model) ?? false
  300. }
  301. }