api-proxy-question.spec.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { describe, expect, it } from 'vitest'
  2. import { Context } from 'cordis'
  3. import type { Agent } from '@deepseek-ai/dsh-agent'
  4. import SessionStore from '@deepseek-ai/dsh-session'
  5. import UserInteractionService from '@deepseek-ai/dsh-user-interaction'
  6. import type { ApiProxy, MuxFrame, RpcRequest } from '@deepseek-ai/dsh-host-apiproxy/api'
  7. import { RpcId } from '@deepseek-ai/dsh-host-apiproxy/api/rpc'
  8. import { createApiProxy } from '../src/api-proxy.ts'
  9. async function harness(): Promise<{ ctx: Context; api: ApiProxy }> {
  10. const ctx = new Context()
  11. await ctx.plugin(SessionStore)
  12. await ctx.plugin(UserInteractionService)
  13. return {
  14. ctx,
  15. api: createApiProxy(ctx, { provider: 'p', model: 'm', cwd: '/tmp', workspaceRoot: '/tmp' }),
  16. }
  17. }
  18. function agent(id: string): Agent {
  19. return { id } as unknown as Agent
  20. }
  21. function openMux(api: ApiProxy, abort: AbortController): {
  22. envelopes: RpcRequest<MuxFrame>[]
  23. waitForQuestion(): Promise<RpcRequest<Extract<MuxFrame, { type: 'question/requested' }>>>
  24. } {
  25. const envelopes: RpcRequest<MuxFrame>[] = []
  26. let resolveQuestion!: (value: RpcRequest<Extract<MuxFrame, { type: 'question/requested' }>>) => void
  27. const question = new Promise<RpcRequest<Extract<MuxFrame, { type: 'question/requested' }>>>((resolve) => {
  28. resolveQuestion = resolve
  29. })
  30. void (async () => {
  31. for await (const envelope of api.events.mux({ rpcId: RpcId('question-mux'), payload: {} }, abort.signal)) {
  32. envelopes.push(envelope)
  33. if (envelope.payload.type === 'question/requested') {
  34. resolveQuestion(envelope as RpcRequest<Extract<MuxFrame, { type: 'question/requested' }>>)
  35. }
  36. }
  37. })()
  38. return { envelopes, waitForQuestion: () => question }
  39. }
  40. function answer(
  41. envelope: RpcRequest<Extract<MuxFrame, { type: 'question/requested' }>>,
  42. selected: string[],
  43. custom?: string,
  44. ): Parameters<ApiProxy['respond']>[0] {
  45. return {
  46. type: 'client-response',
  47. rpcId: envelope.rpcId,
  48. result: {
  49. ok: true,
  50. value: {
  51. sessionId: envelope.payload.sessionId,
  52. answer: {
  53. answers: [{
  54. id: envelope.payload.questions[0]?.id,
  55. selected,
  56. ...custom === undefined ? {} : { custom },
  57. }],
  58. },
  59. },
  60. },
  61. }
  62. }
  63. describe('question response validation', () => {
  64. it('accepts selected options with custom text for multi-select questions', async () => {
  65. const { ctx, api } = await harness()
  66. const abort = new AbortController()
  67. const mux = openMux(api, abort)
  68. const asked = ctx.userInteraction.ask({
  69. agent: agent('session-multi'),
  70. questions: [{
  71. id: 'targets',
  72. question: 'Choose targets and add another',
  73. multiSelect: true,
  74. options: [{ label: 'Code' }, { label: 'Docs' }],
  75. }],
  76. })
  77. const envelope = await mux.waitForQuestion()
  78. expect(await api.respond(answer(envelope, ['Code', 'Docs'], 'Release notes')))
  79. .toEqual({ accepted: true })
  80. await expect(asked).resolves.toEqual({
  81. answers: [{ id: 'targets', selected: ['Code', 'Docs'], custom: 'Release notes' }],
  82. })
  83. expect(mux.envelopes.some(item => item.payload.type === 'question/resolved')).toBe(true)
  84. abort.abort()
  85. })
  86. it('keeps selected options and custom text mutually exclusive for single-select questions', async () => {
  87. const { ctx, api } = await harness()
  88. const abort = new AbortController()
  89. const mux = openMux(api, abort)
  90. const asked = ctx.userInteraction.ask({
  91. agent: agent('session-single'),
  92. questions: [{
  93. id: 'target',
  94. question: 'Choose one target',
  95. options: [{ label: 'Code' }, { label: 'Docs' }],
  96. }],
  97. })
  98. const envelope = await mux.waitForQuestion()
  99. expect(await api.respond(answer(envelope, ['Code'], 'Release notes')))
  100. .toEqual({ accepted: false, reason: 'bad-response' })
  101. expect(await api.respond(answer(envelope, [], 'Release notes')))
  102. .toEqual({ accepted: true })
  103. await expect(asked).resolves.toEqual({
  104. answers: [{ id: 'target', selected: [], custom: 'Release notes' }],
  105. })
  106. abort.abort()
  107. })
  108. })