agent.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /** Agent activation, composition, and model-selection policy owned by API Session. */
  2. import { mkdir } from 'node:fs/promises'
  3. import type { Context } from '@deepseek-ai/cordis'
  4. import { installModelSelection } from '@deepseek-ai/dsh-agent'
  5. import type {
  6. Agent, AgentOptions, AgentSetup, ModelSelection as AgentModelSelection, ModelSelectionRef,
  7. } from '@deepseek-ai/dsh-agent'
  8. import type {} from '@deepseek-ai/dsh-agent-default-model'
  9. import type {} from '@deepseek-ai/dsh-agent-presets'
  10. import { ReasoningEffortId } from '@deepseek-ai/dsh-llm'
  11. import type { Session, SessionEvent, SessionHeader, SessionId } from '@deepseek-ai/dsh-session'
  12. import { SessionQueryError, type SessionObservation } from '@deepseek-ai/dsh-session-query'
  13. import { RemoteError } from '@deepseek-ai/dsh-typert-protocol'
  14. import type {} from '@deepseek-ai/dsh-typert-registry'
  15. import type { ModelSelection } from './types.ts'
  16. /** Cold Session identity absent from persistence. */
  17. export class ApiSessionNotFound extends Error {}
  18. /** Session identity whose lifecycle belongs to subagent routing. */
  19. export class ApiSessionSubagentOwnership extends Error {
  20. /** @param sessionId - identity reserved to subagent routing. */
  21. constructor(readonly sessionId: SessionId) {
  22. super(`session "${sessionId}" is a subagent session; use subagent delivery`)
  23. }
  24. }
  25. /** Explicit-id creation attempted to adopt a Session under another cwd. */
  26. export class ApiSessionCwdConflict extends Error {
  27. constructor(
  28. readonly sessionId: SessionId,
  29. readonly requestedCwd: string,
  30. readonly existingCwd: string | undefined,
  31. ) {
  32. super(
  33. existingCwd === undefined
  34. ? `session "${sessionId}" records no cwd and cannot be adopted for "${requestedCwd}"`
  35. : `session "${sessionId}" belongs to "${existingCwd}", not "${requestedCwd}"`,
  36. )
  37. }
  38. }
  39. /** Explicit-id creation attempted to adopt a Session under another preset. */
  40. export class ApiSessionPresetConflict extends Error {
  41. constructor(
  42. readonly sessionId: SessionId,
  43. readonly requestedPreset: string,
  44. readonly existingPreset: string | undefined,
  45. ) {
  46. super(
  47. existingPreset === undefined
  48. ? `session "${sessionId}" records no agent preset and cannot be adopted under "${requestedPreset}"`
  49. : `session "${sessionId}" runs agent preset "${existingPreset}", not "${requestedPreset}"`,
  50. )
  51. }
  52. }
  53. /** Failures produced while resolving one ordinary Session identity to its live Agent. */
  54. export type ApiSessionAgentError = RemoteError<'session/not-found' | 'session/agent-busy' | 'gateway/internal'>
  55. /** Result of resolving one ordinary Session identity to its live Agent. */
  56. export type ApiSessionAgentResult =
  57. | { readonly agent: Agent }
  58. | { readonly error: ApiSessionAgentError }
  59. type InstalledSelection = ModelSelectionRef & {
  60. current: AgentModelSelection
  61. consume(provider: string, model: string, reasoningEffort: string | undefined): boolean
  62. }
  63. /**
  64. * Test whether generic Session routing must leave an identity to subagent routing.
  65. * @param ctx - Host context carrying the Agent ownership registry.
  66. * @param session - attached or live Session whose ownership is tested.
  67. * @param agent - live Agent when one exists for the Session.
  68. * @returns whether subagent routing owns the Session identity.
  69. */
  70. export function hasApiSessionSubagentOwner(
  71. ctx: Context,
  72. session: Pick<Session, 'header'>,
  73. agent: Agent | undefined,
  74. ): boolean {
  75. if (session.header.origin === 'subagent') return true
  76. const parentId = session.header.parentSession
  77. if (parentId === undefined || agent === undefined) return false
  78. const parent = ctx.agents.get(parentId)
  79. return parent !== undefined && ctx.agents.isOwnedBy(agent.id, parent)
  80. }
  81. /**
  82. * Build the stable caller-facing subagent ownership rejection.
  83. * @param sessionId - Session identity owned by subagent routing.
  84. * @returns a stable Session-domain failure.
  85. */
  86. export function apiSessionSubagentOwnershipError(sessionId: SessionId): ApiSessionAgentError {
  87. return new RemoteError(
  88. 'session/agent-busy',
  89. `session "${sessionId}" is owned by subagent routing`,
  90. { reason: 'use subagent delivery for this child session' },
  91. )
  92. }
  93. /**
  94. * Inspect one cold Session without repairing, resuming, or publishing it.
  95. * @param ctx - Host context carrying Session persistence.
  96. * @param sessionId - durable Session identity.
  97. * @param signal - optional cancellation for persistence reads.
  98. * @returns the persisted header and complete event prefix.
  99. */
  100. export async function inspectApiSession(
  101. ctx: Context,
  102. sessionId: SessionId,
  103. signal?: AbortSignal,
  104. ): Promise<{ meta: SessionHeader; events: SessionEvent[] }> {
  105. try {
  106. using observation = await ctx.sessionQuery.observeSession(sessionId, {
  107. ...(signal === undefined ? {} : { signal }),
  108. projectionMode: 'none',
  109. })
  110. if (observation.header.cwd === undefined) {
  111. throw new ApiSessionNotFound(`session "${sessionId}" not found`)
  112. }
  113. return { meta: observation.header, events: [...observation.events] }
  114. } catch (error: unknown) {
  115. if (error instanceof SessionQueryError
  116. && error.code === 'SESSION_QUERY_SESSION_NOT_FOUND') {
  117. throw new ApiSessionNotFound(`session "${sessionId}" not found`)
  118. }
  119. throw error
  120. }
  121. }
  122. /** Owns every operation that may create, resume, or configure a Web Agent. */
  123. export class ApiSessionAgentController {
  124. private readonly resumes = new Map<SessionId, Promise<Agent>>()
  125. private readonly creations = new Map<SessionId, Promise<Agent>>()
  126. private readonly selections = new WeakMap<Agent, InstalledSelection>()
  127. private readonly imageAdmissionChains = new WeakMap<Agent, Promise<void>>()
  128. /** @param ctx - Host context carrying Agent, model, persistence, and Typert services. */
  129. constructor(private readonly ctx: Context) {
  130. ctx.typert.lookups.configure('agent', async (sessionId: SessionId) => {
  131. const found = await this.resolveAgent(sessionId)
  132. if ('error' in found) throw found.error
  133. return found.agent
  134. })
  135. ctx.typert.lookups.configure('session', async (sessionId: SessionId) => {
  136. const found = await this.resolveAgent(sessionId)
  137. if ('error' in found) throw found.error
  138. return found.agent.session
  139. })
  140. ctx.typert.contexts.configureHost('agent', async (sessionId: SessionId) => {
  141. const found = await this.resolveAgent(sessionId)
  142. if ('error' in found) throw found.error
  143. return found.agent.ctx
  144. })
  145. }
  146. /**
  147. * Resolve or resume one ordinary Session, deduplicating concurrent resumes.
  148. * @param sessionId - ordinary Session identity.
  149. * @returns the live Agent or a stable Session-domain failure.
  150. */
  151. async resolveAgent(sessionId: SessionId): Promise<ApiSessionAgentResult> {
  152. return this.resolve(sessionId)
  153. }
  154. /**
  155. * Resolve one ordinary Session from an already-retained exact observation.
  156. * @param observation - Host-owned observation whose preparation stays pinned through setup.
  157. * @returns the live Agent or a stable Session-domain failure.
  158. */
  159. async resolveObservedAgent(observation: SessionObservation): Promise<ApiSessionAgentResult> {
  160. return this.resolve(observation.header.id, observation)
  161. }
  162. private async resolve(
  163. sessionId: SessionId,
  164. observation?: SessionObservation,
  165. ): Promise<ApiSessionAgentResult> {
  166. const live = this.liveAgent(sessionId)
  167. if (live !== undefined) return live
  168. const attached = this.ctx.sessions.get(sessionId)
  169. if (attached !== undefined && hasApiSessionSubagentOwner(this.ctx, attached, undefined)) {
  170. return { error: apiSessionSubagentOwnershipError(sessionId) }
  171. }
  172. let resume = this.resumes.get(sessionId)
  173. if (resume === undefined) {
  174. resume = this.resume(sessionId, observation).finally(() => { this.resumes.delete(sessionId) })
  175. this.resumes.set(sessionId, resume)
  176. }
  177. try {
  178. return { agent: await resume }
  179. } catch (error: unknown) {
  180. if (error instanceof ApiSessionNotFound) {
  181. return { error: new RemoteError('session/not-found', error.message, { sessionId }) }
  182. }
  183. if (error instanceof ApiSessionSubagentOwnership) {
  184. return { error: apiSessionSubagentOwnershipError(error.sessionId) }
  185. }
  186. const raced = this.liveAgent(sessionId)
  187. if (raced !== undefined) return raced
  188. const racedSession = this.ctx.sessions.get(sessionId)
  189. if (racedSession !== undefined && hasApiSessionSubagentOwner(this.ctx, racedSession, undefined)) {
  190. return { error: apiSessionSubagentOwnershipError(sessionId) }
  191. }
  192. return {
  193. error: new RemoteError(
  194. 'gateway/internal',
  195. `resume failed for session "${sessionId}": ${String(error)}`,
  196. {},
  197. ),
  198. }
  199. }
  200. }
  201. /**
  202. * Resolve one requested identity, creating or resuming it once.
  203. * @param sessionId - requested Session identity.
  204. * @param cwd - directory the Session must own.
  205. * @param checkPersistedIdentity - whether to inspect a cold identity before creation.
  206. * @param presetId - optional Agent preset the Session must own.
  207. * @returns the matching live ordinary Agent.
  208. */
  209. async ensureSession(
  210. sessionId: SessionId,
  211. cwd: string,
  212. checkPersistedIdentity: boolean,
  213. presetId?: string,
  214. ): Promise<Agent> {
  215. let creation = this.creations.get(sessionId)
  216. if (creation === undefined) {
  217. creation = this.createOrAdopt(sessionId, cwd, checkPersistedIdentity, presetId)
  218. .catch((error: unknown) => {
  219. const live = this.ctx.agents.get(sessionId)
  220. if (live !== undefined) {
  221. if (hasApiSessionSubagentOwner(this.ctx, live.session, live)) {
  222. throw new ApiSessionSubagentOwnership(sessionId)
  223. }
  224. return live
  225. }
  226. const attached = this.ctx.sessions.get(sessionId)
  227. if (attached !== undefined && hasApiSessionSubagentOwner(this.ctx, attached, undefined)) {
  228. throw new ApiSessionSubagentOwnership(sessionId)
  229. }
  230. throw error
  231. })
  232. .finally(() => { this.creations.delete(sessionId) })
  233. this.creations.set(sessionId, creation)
  234. }
  235. const agent = await creation
  236. if (hasApiSessionSubagentOwner(this.ctx, agent.session, agent)) {
  237. throw new ApiSessionSubagentOwnership(sessionId)
  238. }
  239. if (presetId !== undefined) {
  240. this.assertPresetUnchanged(sessionId, presetId, this.presetForSession(agent.session))
  241. }
  242. if (agent.session.header.cwd !== cwd) {
  243. throw new ApiSessionCwdConflict(sessionId, cwd, agent.session.header.cwd)
  244. }
  245. return agent
  246. }
  247. /**
  248. * Install or return the Session-local model selection used by prompt assembly.
  249. * @param agent - live Agent that owns the selection.
  250. * @returns the installed mutable selection reference.
  251. */
  252. selectionFor(agent: Agent): InstalledSelection {
  253. const installed = this.selections.get(agent)
  254. if (installed !== undefined) return installed
  255. const projectionState = this.ctx.sessionProjections.stateOf(agent.session, 'modelSelection')
  256. if (projectionState === undefined) {
  257. throw new Error('api-session: required modelSelection projection is not registered')
  258. }
  259. let picked = projectionState.pending === null
  260. ? undefined
  261. : agentModelSelection(projectionState.pending)
  262. const defaultModel = this.ctx.agentDefaultModel
  263. const selection: InstalledSelection = {
  264. get current(): AgentModelSelection {
  265. if (picked !== undefined) return picked
  266. const loggedHeader = agent.session.requestHeader()
  267. if (loggedHeader === undefined) return defaultModel.currentSelection()
  268. const logged = loggedHeader.config
  269. return {
  270. provider: logged.provider,
  271. model: logged.model,
  272. // An effort the adapter defaulted is not a conversation choice: restoring
  273. // it as one would make an unchanged default read as a request change.
  274. ...(logged.reasoningEffort === undefined
  275. || loggedHeader.adapterDefaults?.reasoningEffort === true
  276. ? {}
  277. : { reasoningEffort: logged.reasoningEffort }),
  278. }
  279. },
  280. set current(next: AgentModelSelection) {
  281. picked = next
  282. },
  283. consume(provider: string, model: string, reasoningEffort: string | undefined): boolean {
  284. if (picked?.provider !== provider
  285. || picked.model !== model
  286. || picked.reasoningEffort !== reasoningEffort) return false
  287. picked = undefined
  288. return true
  289. },
  290. assembled: undefined,
  291. }
  292. installModelSelection(agent.ctx, selection)
  293. this.selections.set(agent, selection)
  294. return selection
  295. }
  296. /**
  297. * Commit and cache one validated selection for the next prompt assembly.
  298. * @param agent - live Agent that owns the selection.
  299. * @param selection - validated selection to record and apply.
  300. */
  301. selectForNextRequest(agent: Agent, selection: AgentModelSelection): void {
  302. agent.session.append('model/selection', selection)
  303. this.selectionFor(agent).current = selection
  304. }
  305. /**
  306. * Let a matching durable request header retire the execution cache.
  307. * @param agent - live Agent whose request was recorded.
  308. * @param provider - provider route used by the request.
  309. * @param model - provider-owned model used by the request.
  310. * @param reasoningEffort - adapter-owned effort used by the request.
  311. * @returns whether the pending selection was consumed.
  312. */
  313. consumeSelection(
  314. agent: Agent,
  315. provider: string,
  316. model: string,
  317. reasoningEffort: string | undefined,
  318. ): boolean {
  319. return this.selections.get(agent)?.consume(provider, model, reasoningEffort) ?? false
  320. }
  321. /**
  322. * Read the current Agent preset from the Session projection.
  323. * @param session - live Session whose projection state is available.
  324. * @returns the current preset, or undefined when the capability is absent.
  325. */
  326. presetForSession(session: Session): string | undefined {
  327. return this.ctx.sessionProjections.stateOf(session, 'agentPreset') ?? undefined
  328. }
  329. /**
  330. * Serialize image admission and model selection for one Agent.
  331. * @param agent - live Agent that owns the serialization chain.
  332. * @param operation - asynchronous operation admitted after prior work settles.
  333. * @returns the operation result or rejection.
  334. */
  335. serializeImageAdmission<Value>(agent: Agent, operation: () => Promise<Value>): Promise<Value> {
  336. const result = (this.imageAdmissionChains.get(agent) ?? Promise.resolve()).then(operation)
  337. this.imageAdmissionChains.set(agent, result.then(() => undefined, () => undefined))
  338. return result
  339. }
  340. /**
  341. * Resolve the preset id and pre-publication Agent setup for a create or resume.
  342. * @param presetId - requested preset or the configured default when omitted.
  343. * @returns the resolved preset identity and Agent setup callback.
  344. */
  345. async composeAgent(presetId: string | undefined): Promise<{
  346. readonly agentPreset?: string
  347. readonly setup: AgentSetup
  348. }> {
  349. const presets = this.ctx.get('agentPresets')
  350. if (presets === undefined) return { setup: (agentCtx) => { this.installSelection(agentCtx) } }
  351. const resolvedId = (await presets.resolve(presetId)).id
  352. return {
  353. agentPreset: resolvedId,
  354. setup: async (agentCtx) => {
  355. this.installSelection(agentCtx)
  356. await presets.mount(agentCtx, resolvedId)
  357. },
  358. }
  359. }
  360. private liveAgent(sessionId: SessionId): ApiSessionAgentResult | undefined {
  361. const agent = this.ctx.agents.get(sessionId)
  362. if (agent === undefined) return undefined
  363. return hasApiSessionSubagentOwner(this.ctx, agent.session, agent)
  364. ? { error: apiSessionSubagentOwnershipError(sessionId) }
  365. : { agent }
  366. }
  367. private async resume(sessionId: SessionId, supplied?: SessionObservation): Promise<Agent> {
  368. if (supplied !== undefined) return this.resumeObserved(sessionId, supplied)
  369. try {
  370. using observation = await this.ctx.sessionQuery.observeSession(sessionId)
  371. return await this.resumeObserved(sessionId, observation)
  372. } catch (error: unknown) {
  373. if (error instanceof SessionQueryError
  374. && error.code === 'SESSION_QUERY_SESSION_NOT_FOUND') {
  375. throw new ApiSessionNotFound(`session "${sessionId}" not found`)
  376. }
  377. throw error
  378. }
  379. }
  380. private async resumeObserved(
  381. sessionId: SessionId,
  382. observation: SessionObservation,
  383. ): Promise<Agent> {
  384. if (observation.header.id !== sessionId || observation.header.cwd === undefined) {
  385. throw new ApiSessionNotFound(`session "${sessionId}" not found`)
  386. }
  387. if (hasApiSessionSubagentOwner(this.ctx, { header: observation.header }, undefined)) {
  388. throw new ApiSessionSubagentOwnership(sessionId)
  389. }
  390. const composition = await this.composeAgent(this.presetForObservation(observation))
  391. const published = this.ctx.sessions.get(sessionId)
  392. const live = this.ctx.agents.get(sessionId)
  393. if (published !== undefined && hasApiSessionSubagentOwner(this.ctx, published, live)) {
  394. throw new ApiSessionSubagentOwnership(sessionId)
  395. }
  396. return (await this.ctx.agents.resume({
  397. resumeSessionId: sessionId,
  398. agentOptions: this.agentOptions(),
  399. setup: composition.setup,
  400. })).agent
  401. }
  402. private async createOrAdopt(
  403. sessionId: SessionId,
  404. cwd: string,
  405. checkPersistedIdentity: boolean,
  406. presetId: string | undefined,
  407. ): Promise<Agent> {
  408. const attached = this.ctx.sessions.get(sessionId)
  409. const live = this.ctx.agents.get(sessionId)
  410. if (attached !== undefined && hasApiSessionSubagentOwner(this.ctx, attached, live)) {
  411. throw new ApiSessionSubagentOwnership(sessionId)
  412. }
  413. if (live !== undefined) return live
  414. if (checkPersistedIdentity) {
  415. try {
  416. using observation = await this.ctx.sessionQuery.observeSession(sessionId)
  417. if (hasApiSessionSubagentOwner(this.ctx, { header: observation.header }, undefined)) {
  418. throw new ApiSessionSubagentOwnership(sessionId)
  419. }
  420. if (observation.header.cwd !== cwd) {
  421. throw new ApiSessionCwdConflict(sessionId, cwd, observation.header.cwd)
  422. }
  423. const storedPreset = this.presetForObservation(observation)
  424. this.assertPresetUnchanged(sessionId, presetId, storedPreset)
  425. const composition = await this.composeAgent(storedPreset)
  426. return (await this.ctx.agents.resume({
  427. resumeSessionId: sessionId,
  428. agentOptions: this.agentOptions(),
  429. setup: composition.setup,
  430. })).agent
  431. } catch (error: unknown) {
  432. if (!(error instanceof SessionQueryError)
  433. || error.code !== 'SESSION_QUERY_SESSION_NOT_FOUND') throw error
  434. }
  435. }
  436. try {
  437. await mkdir(cwd, { recursive: true })
  438. } catch (error: unknown) {
  439. throw new Error(`failed to ensure project directory "${cwd}": ${String(error)}`, { cause: error })
  440. }
  441. const composition = await this.composeAgent(presetId)
  442. return (await this.ctx.agents.create({
  443. sessionId,
  444. agentOptions: this.agentOptions(),
  445. meta: {
  446. cwd,
  447. ...(composition.agentPreset === undefined ? {} : { agentPreset: composition.agentPreset }),
  448. },
  449. setup: composition.setup,
  450. })).agent
  451. }
  452. private agentOptions(): AgentOptions {
  453. const { provider, model } = this.ctx.agentDefaultModel.currentSelection()
  454. return { provider, model }
  455. }
  456. private installSelection(agentCtx: Context): void {
  457. const agent = agentCtx.agent
  458. if (agent === undefined) throw new Error('api-session: Agent setup has no scoped Agent')
  459. this.selectionFor(agent)
  460. }
  461. /**
  462. * Read the current Agent preset from an all-projections observation.
  463. * @param observation - exact Session observation carrying its projection snapshot.
  464. * @returns the current preset, or undefined when the capability is absent.
  465. */
  466. presetForObservation(observation: SessionObservation): string | undefined {
  467. if (observation.projections === undefined) {
  468. throw new Error('api-session: Agent activation requires a projected Session observation')
  469. }
  470. return observation.projections.values.agentPreset ?? undefined
  471. }
  472. private assertPresetUnchanged(
  473. sessionId: SessionId,
  474. requested: string | undefined,
  475. existing: string | undefined,
  476. ): void {
  477. if (requested === undefined || requested === existing) return
  478. throw new ApiSessionPresetConflict(sessionId, requested, existing)
  479. }
  480. }
  481. function agentModelSelection(selection: ModelSelection): AgentModelSelection {
  482. return {
  483. provider: selection.provider,
  484. model: selection.model,
  485. ...(selection.reasoningEffort === undefined
  486. ? {}
  487. : { reasoningEffort: ReasoningEffortId(selection.reasoningEffort) }),
  488. }
  489. }