index.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * Model-facing `ask_user_question` tool over the `ctx.userInteraction` seam.
  3. * The tool pauses until a UI provider returns a human answer, then feeds that
  4. * answer back into the agent loop as an ordinary tool result.
  5. *
  6. * @module @deepseek-ai/dsh-tool-ask-user
  7. */
  8. import type { Context } from 'cordis'
  9. import { defineTool } from '@deepseek-ai/dsh-tools'
  10. import '@deepseek-ai/dsh-user-interaction'
  11. export const name = 'tool-ask-user'
  12. export const inject = ['tools', 'userInteraction']
  13. const description = 'Ask the user a concise question when you need confirmation, a choice, or missing information before proceeding. '
  14. + 'Send one or more questions, each with a stable id that will be echoed in the answer.'
  15. export function apply(ctx: Context): void {
  16. ctx.tools.register(defineTool({
  17. name: 'ask_user_question',
  18. description,
  19. parameters: {
  20. questions: {
  21. type: 'array',
  22. required: true,
  23. description: 'Questions to ask the user before continuing.',
  24. items: {
  25. type: 'object',
  26. additionalProperties: true,
  27. properties: {
  28. id: { type: 'string', required: true, description: 'Stable id for this question; echoed in the answer.' },
  29. question: { type: 'string', required: true, description: 'The specific question to ask the user.' },
  30. header: {
  31. type: 'string',
  32. description: 'Optional short heading for the question, such as "Confirm" or "Choose Mode".',
  33. },
  34. options: {
  35. type: 'array',
  36. description: 'Optional choices to show the user. If you recommend one, put it first and append "(Recommended)" to that label.',
  37. items: {
  38. type: 'object',
  39. additionalProperties: true,
  40. properties: {
  41. label: { type: 'string', required: true, description: 'Short user-facing option label.' },
  42. description: { type: 'string', description: 'One sentence explaining the tradeoff or impact.' },
  43. },
  44. },
  45. },
  46. multi_select: {
  47. type: 'boolean',
  48. description: 'Whether the user may select more than one option. Defaults to false.',
  49. },
  50. },
  51. },
  52. },
  53. },
  54. output: {
  55. schema: {
  56. type: 'object',
  57. additionalProperties: false,
  58. properties: {
  59. answers: {
  60. type: 'array',
  61. required: true,
  62. items: {
  63. type: 'object',
  64. additionalProperties: false,
  65. properties: {
  66. id: { type: 'string', required: true },
  67. selected: { type: 'array', required: true, items: { type: 'string' } },
  68. custom: { type: 'string' },
  69. },
  70. },
  71. },
  72. },
  73. },
  74. render: (_args, value) => [{ type: 'text', text: JSON.stringify(value) }],
  75. },
  76. async execute(args, exec) {
  77. const result = await ctx.userInteraction.ask({
  78. questions: args.questions.map(question => ({
  79. id: question.id,
  80. question: question.question,
  81. ...question.header !== undefined ? { header: question.header } : {},
  82. ...question.options !== undefined ? { options: question.options } : {},
  83. ...question.multi_select !== undefined ? { multiSelect: question.multi_select } : {},
  84. })),
  85. ...exec.agent !== undefined ? { agent: exec.agent } : {},
  86. signal: exec.signal,
  87. })
  88. return {
  89. answers: result.answers.map(answer => ({
  90. id: answer.id,
  91. selected: [...answer.selected],
  92. ...answer.custom !== undefined ? { custom: answer.custom } : {},
  93. })),
  94. }
  95. },
  96. }))
  97. }