model-selection.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * Agent-scoped model selection shared by runtime entry points.
  3. * @module @deepseek-ai/dsh-agent/model-selection
  4. */
  5. import type { Context } from '@deepseek-ai/cordis'
  6. import type { LlmCallConfig, ReasoningEffortId } from '@deepseek-ai/dsh-llm'
  7. /** Complete provider, model, and optional reasoning effort selected for one live Agent. */
  8. export interface ModelSelection {
  9. /** Registered provider route. */
  10. provider: string
  11. /** Provider-owned model id. */
  12. model: string
  13. /** Adapter-owned reasoning effort, or provider/default behavior when absent. */
  14. reasoningEffort?: ReasoningEffortId
  15. }
  16. /** Mutable model selection plus the value captured for the current step. */
  17. export interface ModelSelectionRef {
  18. /** Model selected for the next step that enters prompt assembly. */
  19. current: ModelSelection | undefined
  20. /** Selection captured when the current step entered prompt assembly. */
  21. assembled: ModelSelection | undefined
  22. }
  23. /**
  24. * Couple one mutable selection to Agent-scoped prompt assembly and request routing.
  25. * Prompt assembly snapshots the selected model before delegating, then applies
  26. * its provider/model pair and effort to request config so a
  27. * concurrent switch takes effect on a later step instead of splitting the two
  28. * surfaces. An absent selected effort clears any inherited effort, restoring
  29. * the selected model's provider/default behavior.
  30. *
  31. * @param agentCtx - The selected Agent's scoped context.
  32. * @param selection - Mutable selection owned by the calling entry point.
  33. * @returns Disposer for both scoped waterfall listeners.
  34. */
  35. export function installModelSelection(agentCtx: Context, selection: ModelSelectionRef): () => void {
  36. const disposeAssembly = agentCtx.on('system-prompt/assemble', async (_assembly, _context, next) => {
  37. const selected = selection.current
  38. const assembled = await next()
  39. selection.assembled = selected
  40. if (selected === undefined) return assembled
  41. return {
  42. ...assembled,
  43. variables: {
  44. ...assembled.variables,
  45. provider: selected.provider,
  46. model: selected.model,
  47. },
  48. }
  49. })
  50. const disposeRequest = agentCtx.on(
  51. 'agent/request',
  52. async (_payload, next): Promise<LlmCallConfig> => {
  53. const resolved = await next()
  54. const selected = selection.assembled
  55. if (selected === undefined) return resolved
  56. const { reasoningEffort: _inheritedEffort, ...withoutInheritedEffort } = resolved
  57. return {
  58. ...withoutInheritedEffort,
  59. provider: selected.provider,
  60. model: selected.model,
  61. ...selected.reasoningEffort === undefined
  62. ? {}
  63. : { reasoningEffort: selected.reasoningEffort },
  64. }
  65. },
  66. )
  67. return () => {
  68. disposeAssembly()
  69. disposeRequest()
  70. }
  71. }