question.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. export * as QuestionTool from "./question"
  2. import { ToolFailure } from "@opencode-ai/llm"
  3. import { Effect, Layer, Schema } from "effect"
  4. import { PermissionV2 } from "../permission"
  5. import { QuestionV2 } from "../question"
  6. import { Tool } from "./tool"
  7. import { Tools } from "./tools"
  8. export const name = "question"
  9. export const description = `Use this tool when you need to ask the user questions during execution. This allows you to:
  10. 1. Gather user preferences or requirements
  11. 2. Clarify ambiguous instructions
  12. 3. Get decisions on implementation choices as you work
  13. 4. Offer choices to the user about what direction to take.
  14. Usage notes:
  15. - When \`custom\` is enabled (default), a "Type your own answer" option is added automatically; don't include "Other" or catch-all options
  16. - Answers are returned as arrays of labels; set \`multiple: true\` to allow selecting more than one
  17. - If you recommend a specific option, make that the first option in the list and add "(Recommended)" at the end of the label`
  18. export const Input = Schema.Struct({
  19. questions: Schema.Array(QuestionV2.Prompt).annotate({ description: "Questions to ask" }),
  20. })
  21. export const Output = Schema.Struct({
  22. answers: Schema.Array(QuestionV2.Answer),
  23. })
  24. export type Output = typeof Output.Type
  25. export const toModelOutput = (
  26. questions: ReadonlyArray<QuestionV2.Prompt>,
  27. answers: ReadonlyArray<QuestionV2.Answer>,
  28. ) => {
  29. const formatted = questions
  30. .map(
  31. (question, index) =>
  32. `"${question.question}"="${answers[index]?.length ? answers[index].join(", ") : "Unanswered"}"`,
  33. )
  34. .join(", ")
  35. return `User has answered your questions: ${formatted}. You can now continue with the user's answers in mind.`
  36. }
  37. export const layer = Layer.effectDiscard(
  38. Effect.gen(function* () {
  39. const tools = yield* Tools.Service
  40. const question = yield* QuestionV2.Service
  41. const permission = yield* PermissionV2.Service
  42. yield* tools
  43. .register({
  44. [name]: Tool.make({
  45. description,
  46. input: Input,
  47. output: Output,
  48. toModelOutput: ({ input, output }) => [
  49. { type: "text", text: toModelOutput(input.questions, output.answers) },
  50. ],
  51. execute: (input, context) =>
  52. permission
  53. .assert({
  54. action: "question",
  55. resources: ["*"],
  56. sessionID: context.sessionID,
  57. agent: context.agent,
  58. source: { type: "tool", messageID: context.assistantMessageID, callID: context.toolCallID },
  59. })
  60. .pipe(
  61. Effect.mapError(() => new ToolFailure({ message: "Permission denied: question" })),
  62. Effect.andThen(
  63. question
  64. .ask({
  65. sessionID: context.sessionID,
  66. questions: input.questions,
  67. tool: { messageID: context.assistantMessageID, callID: context.toolCallID },
  68. })
  69. .pipe(Effect.orDie),
  70. ),
  71. Effect.map((answers) => ({ answers })),
  72. ),
  73. }),
  74. })
  75. .pipe(Effect.orDie)
  76. }),
  77. )