agent.ts 20 KB

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