| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- /**
- * JSON-RPC method and notification surface for out-of-process harness SDKs.
- * The surrounding context owns plugins, persistence, and configured adapters.
- *
- * @module @deepseek-ai/dsh-jsonrpc/server
- */
- import type { Context } from 'cordis'
- import { resolve } from 'node:path'
- import type { ContentBlock } from '@deepseek-ai/dsh-llm'
- import type { Agent, AgentHandle } from '@deepseek-ai/dsh-agent'
- import { carrierKeyOf, type Scoped } from '@deepseek-ai/dsh-scope'
- import { findLastMessageTurnEnd, SessionId, type TurnEndReason } from '@deepseek-ai/dsh-session'
- import type SubagentService from '@deepseek-ai/dsh-subagent'
- import type { SubagentRunEndInfo } from '@deepseek-ai/dsh-subagent'
- import * as LlmDeepSeek from '@deepseek-ai/dsh-llm-deepseek'
- import type { JsonRpcTransportPeer } from './transport.ts'
- /** Parameters for the process-wide SDK handshake. */
- export interface InitializeParams {
- /** Working directory recorded on every SDK-created session's header. */
- cwd: string
- /** Provider route every SDK-created agent runs on. */
- provider: string
- /** Model name every SDK-created agent runs on (see {@link HarnessSdkServer.initialize} for adapter fallback). */
- model: string
- }
- /** Wire-stable server identity returned by initialization. */
- export interface InitializeResult {
- /** Wire-stable server identity (`deepseek-harness-sdk-runtime`) and version. */
- serverInfo: { name: string; version: string }
- }
- /** One user turn on one SDK session. */
- export interface SessionPromptParams {
- /** The SDK-side session id; an unknown id lazily creates the agent+session pair. */
- sessionId: string
- /** The prompt content blocks, sent verbatim as the user message. */
- contentBlocks: ContentBlock[]
- }
- /** Prompt acceptance after turn settlement; outcome rides on `session.finished`. */
- export interface SessionPromptResult {
- /** Always `true`; the turn outcome is the paired `session.finished` notification. */
- accepted: true
- }
- interface SessionRecord {
- handle: AgentHandle
- lastTurnEnd: TurnEndReason | undefined
- activePrompt: boolean
- }
- /** Recover the delegating parent from the service-owned scoped carrier. */
- function subagentParentOf(carrier: Scoped<SubagentService>): Agent {
- return carrierKeyOf(carrier) as Agent
- }
- /** Deployment-specific status mapping for SDK turn and subagent outcomes. */
- export interface HarnessSdkServerOptions {
- /** Report max-token termination as an accepted result instead of an infrastructure error. */
- maxTokensAsSuccess?: boolean
- }
- function successStatus(reason: string, options: HarnessSdkServerOptions): 'ok' | 'error' {
- if (reason === 'completed') return 'ok'
- return reason === 'max-tokens' && options.maxTokensAsSuccess === true ? 'ok' : 'error'
- }
- /**
- * SDK server over one booted harness context and transport peer. Construction
- * subscribes to session, agent, and subagent lifecycle events until shutdown;
- * reinitialization is unsupported.
- */
- export class HarnessSdkServer {
- private cwd = process.cwd()
- private provider = 'deepseek'
- private model = 'deepseek'
- private llmFiber: { dispose(): Promise<void> } | undefined
- private readonly sessions = new Map<string, SessionRecord>()
- private readonly sessionCreations = new Map<string, Promise<SessionRecord>>()
- private readonly disposers: (() => void)[] = []
- private shutdownTask: Promise<Record<string, never>> | undefined
- private shuttingDown = false
- constructor(
- private readonly ctx: Context,
- private readonly transport: JsonRpcTransportPeer,
- private readonly options: HarnessSdkServerOptions = {},
- ) {
- const serverOptions = this.options
- this.disposers.push(ctx.on('session/event', (session, event) => {
- if (event.type === 'turn/end') {
- const rec = this.sessions.get(String(session.id))
- if (rec && findLastMessageTurnEnd(session.events)?.seq === event.seq) {
- rec.lastTurnEnd = event.data.reason
- }
- }
- this.transport.notify('session.event', { sessionId: String(session.id), event })
- }))
- this.disposers.push(ctx.on('session/created', (session) => {
- const parentSession = session.header.parentSession
- if (parentSession === undefined) return
- this.transport.notify('subagent.started', {
- parentSessionId: String(parentSession),
- childSessionId: String(session.id),
- })
- }))
- this.disposers.push(ctx.on('subagent/end', function (this: Scoped<SubagentService>, info: SubagentRunEndInfo) {
- const parent = subagentParentOf(this)
- // This protocol reports only in-process child sessions. The service
- // snapshots the provider's exact run provenance through child disposal;
- // matching ids or parent lineage alone never establishes locality.
- if (!info.local) return
- transport.notify('subagent.finished', {
- provider: info.provider,
- agentId: String(info.id),
- parentSessionId: String(parent.session.id),
- childSessionId: String(info.id),
- status: successStatus(info.stopReason, serverOptions),
- stopReason: info.stopReason,
- ...(info.lastAssistantMessage === undefined ? {} : { lastAssistantMessage: info.lastAssistantMessage }),
- })
- }))
- }
- /**
- * Configure the SDK route, mounting the DeepSeek fallback only when unowned.
- * @param params - SDK handshake parameters.
- * @returns server identity for the handshake.
- */
- async initialize(params: InitializeParams): Promise<InitializeResult> {
- this.cwd = resolve(params.cwd)
- this.provider = params.provider
- this.model = params.model
- if (!this.hasAdapterFor(this.provider)) {
- if (this.provider !== 'deepseek') throw new Error(`no adapter registered for provider "${this.provider}"`)
- this.llmFiber = await this.ctx.plugin(LlmDeepSeek, {})
- }
- return { serverInfo: { name: 'deepseek-harness-sdk-runtime', version: '0.0.1' } }
- }
- /**
- * Run one prompt to settlement; overlap on the same session fails.
- * @param params - target session and user content.
- * @returns acceptance after the turn settled.
- */
- async prompt(params: SessionPromptParams): Promise<SessionPromptResult> {
- const rec = await this.getOrCreateSession(params.sessionId)
- if (rec.activePrompt) throw new Error(`session already has an active prompt: ${params.sessionId}`)
- rec.activePrompt = true
- try {
- rec.lastTurnEnd = undefined
- rec.handle.agent.followup(params.contentBlocks)
- await rec.handle.agent.whenIdle()
- const status = this.finishedStatus(rec.lastTurnEnd)
- this.transport.notify('session.finished', {
- sessionId: params.sessionId,
- status,
- reason: rec.lastTurnEnd,
- })
- return { accepted: true }
- } finally {
- rec.activePrompt = false
- }
- }
- /**
- * Dispose server-owned agents, adapter, and subscriptions to quiescence.
- * The surrounding context remains running.
- * @returns empty JSON-RPC result.
- */
- shutdown(): Promise<Record<string, never>> {
- this.shutdownTask ??= this.performShutdown()
- return this.shutdownTask
- }
- private async performShutdown(): Promise<Record<string, never>> {
- this.shuttingDown = true
- const pendingCreations = [...this.sessionCreations.values()]
- await Promise.allSettled(pendingCreations)
- this.sessionCreations.clear()
- const records = [...this.sessions.values()]
- this.sessions.clear()
- const failures: unknown[] = []
- while (this.disposers.length > 0) {
- try {
- this.disposers.pop()?.()
- } catch (error) {
- failures.push(error)
- }
- }
- const teardownResults = await Promise.allSettled([
- ...records.map(rec => Promise.resolve().then(() => rec.handle.dispose())),
- ...(this.llmFiber === undefined ? [] : [Promise.resolve().then(() => this.llmFiber?.dispose())]),
- ])
- this.llmFiber = undefined
- failures.push(...teardownResults
- .filter((result): result is PromiseRejectedResult => result.status === 'rejected')
- .map(result => result.reason as unknown))
- if (failures.length === 1) throw failures[0]
- if (failures.length > 1) throw new AggregateError(failures, 'SDK server teardown failed')
- return {}
- }
- /**
- * Dispatch one incoming JSON-RPC request to its typed handler. Throws (→ a
- * JSON-RPC error response) on an unknown method.
- * @param method - the JSON-RPC method name.
- * @param params - the raw params object from the wire.
- * @returns the handler's result, to be serialized as the response.
- */
- async handleRequest(method: string, params: Record<string, unknown> | undefined): Promise<unknown> {
- switch (method) {
- case 'initialize':
- return this.initialize(params as unknown as InitializeParams)
- case 'session/prompt':
- return this.prompt(params as unknown as SessionPromptParams)
- case 'shutdown':
- return this.shutdown()
- default:
- throw new Error(`unknown DeepSeek Harness SDK runtime method: ${method}`)
- }
- }
- private async getOrCreateSession(sessionId: string): Promise<SessionRecord> {
- if (this.shuttingDown) throw new Error('SDK server is shutting down')
- const existing = this.sessions.get(sessionId)
- if (existing) return existing
- const pending = this.sessionCreations.get(sessionId)
- if (pending) return pending
- const creation = this.createSession(sessionId)
- this.sessionCreations.set(sessionId, creation)
- void creation.then(
- () => { this.sessionCreations.delete(sessionId) },
- () => { this.sessionCreations.delete(sessionId) },
- )
- return creation
- }
- private async createSession(sessionId: string): Promise<SessionRecord> {
- const handle = await this.ctx.agents.create({
- sessionId: SessionId(sessionId),
- meta: { cwd: this.cwd },
- agentOptions: { provider: this.provider, model: this.model },
- })
- const rec: SessionRecord = { handle, lastTurnEnd: undefined, activePrompt: false }
- this.sessions.set(sessionId, rec)
- return rec
- }
- private finishedStatus(reason: TurnEndReason | undefined): 'ok' | 'error' {
- if (!reason) return 'error'
- return successStatus(reason.kind, this.options)
- }
- private hasAdapterFor(provider: string): boolean {
- return this.ctx.get('llm')?.listProviders().some(entry => entry.id === provider) ?? false
- }
- }
|