Skip to main content

Question Answer Module

The @credo-ts/question-answer package implements the DIDComm Question Answer Protocol, enabling agents to ask and respond to questions.

Installation

npm install @credo-ts/question-answer

Registration

import { Agent } from '@credo-ts/core'
import { QuestionAnswerModule } from '@credo-ts/question-answer'

const agent = new Agent({
  // ... config
  modules: {
    questionAnswer: new QuestionAnswerModule(),
  },
})

What It Provides

The Question Answer module enables:
  • Send questions to connected agents
  • Receive and respond to questions
  • Multiple choice questions with predefined responses
  • Free-form text responses

Usage

Sending a Question

const { questionAnswerRecord } = await agent.modules.questionAnswer.sendQuestion({
  connectionId: connection.id,
  question: {
    questionText: 'Would you like to proceed?',
    validResponses: [
      { text: 'Yes' },
      { text: 'No' },
    ],
  },
})

Receiving Questions

import { QuestionAnswerEventTypes, QuestionAnswerRole } from '@credo-ts/question-answer'

agent.events.on(QuestionAnswerEventTypes.QuestionAnswerStateChanged, async (event) => {
  const record = event.payload.questionAnswerRecord
  
  if (record.role === QuestionAnswerRole.Responder && record.state === QuestionAnswerState.QuestionReceived) {
    console.log('Question received:', record.questionText)
    console.log('Valid responses:', record.validResponses)
    
    // Send answer
    await agent.modules.questionAnswer.sendAnswer({
      questionAnswerRecordId: record.id,
      response: 'Yes',
    })
  }
})

Receiving Answers

agent.events.on(QuestionAnswerEventTypes.QuestionAnswerStateChanged, async (event) => {
  const record = event.payload.questionAnswerRecord
  
  if (record.role === QuestionAnswerRole.Questioner && record.state === QuestionAnswerState.AnswerReceived) {
    console.log('Answer received:', record.response)
  }
})

Use Cases

  • Consent Requests - Ask for user consent before actions
  • Verification Checks - Confirm user identity or attributes
  • Service Selection - Simple yes/no or multiple choice questions
  • Feedback Collection - Gather user feedback

Question Types

Multiple Choice

question: {
  questionText: 'Select your preferred method',
  validResponses: [
    { text: 'Email' },
    { text: 'SMS' },
    { text: 'Push Notification' },
  ],
}

Free-Form Text

question: {
  questionText: 'Please provide additional details',
  validResponses: [], // Empty array allows any response
}

See Also

Build docs developers (and LLMs) love