Contexts and fulfillment

When using fulfillment, you can activate and gather active contexts. If you're developing fulfillment with Node.js, you can use the fulfillment library to retrieve context data. If you're developing in other languages, you can parse the JSON in the HTTPS request to your fulfillment.

Get active contexts using the fulfillment library

To retrieve attached contexts via the fulfillment library, use the following method:

agent.context.get('context-name');

This will provide the context information in the following format:

{
  'name': 'context-name',
  'lifespan': 5,
  'parameters': {
    'parameter-name':'parameter-value'
  }
}

Get active contexts using JSON

To retrieve activated contexts via a webhook, see the queryResult.outputContexts list in the webhook request JSON payload:

{
  "queryResult": {
    "outputContexts": [
      {
        "name": "projects/your-agents-project-id/agent/sessions/88d13aa8-2999-4f71-b233-39cbf3a824a0/contexts/<CONTEXT-NAME>",
        "lifespanCount": 5,
        "parameters": {
          "param-name": "param value"
        }
      }
    ],
    … // Unrelated information
  },
  "session": "projects/<PROJECT-ID>/agent/sessions/<SESSION-ID>",
  … // Unrelated information
}

Set a context using the fulfillment library

To set a context via the fulfillment library, use the following method:

agent.context.set({
  'name':'context-name',
  'lifespan': 5,
  'parameters':{
    'parameter-name':'parameter-value'
    }
});

Set a context using JSON

To set a context via a webhook, use the following response:

{
  "outputContexts": [
    {
      "name": "projects/<PROJECT-ID>/agent/sessions/<SESSION-ID>/contexts/<CONTEXT-NAME>",
      "lifespanCount": 5,
      "parameters": {
        "param": "param value"
      }
    }
  ],
  ...// Unrelated information
}

Clearing contexts using the fulfillment library

To clear a context when using the fulfillment library, use one of the following:

agent.context.set({
  'name': 'context-name',
  'lifespan': 0,
  'parameters': {
    'parameter':'parameter-value'
  }
})

OR agent.context.delete('context-name')