0

I'm trying to manage the context of my Google Assistant agent (in DialogFlow), using the ApiAi class in the npm package actions-on-google.

The problem is this: How can I reset the lifespan / delete a context using the npm package?

I can easily set the lifespan of a new context, and it works.

However:

  1. How do I delete a context?
  2. Setting the context to a different number does not seem to work. That is, if I set app.setContext('myContext',10) and then, 2 intents later, when the lifespan in 8, I call app.setContext('myContext',10) again, in the next intent, the lifespan is still 7. If I could answer (1) and delete a context, I'd just delete it and set it again.
Prisoner
  • 49,922
  • 7
  • 53
  • 105
eran
  • 14,496
  • 34
  • 98
  • 144
  • Can you show the code you're using to try and set or change the new lifespan value of the context? – Prisoner Oct 31 '17 at 10:30
  • @Prisoner I am simply calling app.setContext('myContext',10) in every intent handler in the webhook. It changes nothing. If I call `app.setContext('newIntentThatIsNotSet',10)` works fine. – eran Oct 31 '17 at 16:26
  • So, once a context is set, it counts down, but I can no reset it. – eran Oct 31 '17 at 16:26
  • I've both cleared and reset contexts, which is why I asked how you were setting it. Going to see what changed. – Prisoner Oct 31 '17 at 17:01

4 Answers4

1

I don't think there is a way to delete or overwrite the duration of a context. Instead, if you know that a certain context must not be active at a certain point, set a context that lasts for 1 or 2 turns and do this after each turn. This will also give you more control over the conversation, so you won't have contexts that last for 10 turns that you suddenly don't need anymore.

Bart
  • 498
  • 2
  • 8
  • Yes - that was my feeling as well. – eran Oct 31 '17 at 16:26
  • Another solution is to set the context you want to reset as an output context with a lifespan of 0 in the dialogFlow console (cannot be done in code). Which has the effect of expiring the context if the intent was matched with that content active. This is not optimal, as you can't do it from code, and you can't set a new lifespan, just delete the context. (You CAN change a counter, by deleting by setting to 0 in the console, and then calling setContext() in the code - very awkward but that is what I do atm). – eran Oct 31 '17 at 16:29
1

To delete a lifespan of context you just set it to ZERO or 0 like app.setContext('your_context',0)
Make sure you do this before calling the app.ask or app.tell

or

if not using client you could write a function that simply sets this.contexts_[your_context] = {}

The first option is definitely working for me. I have not tried the second option. Try and see if you are not setting it in the Dialogflow. Also, you can remove context setting in webhook and put lifespan as 0 in Dialogflow. That will put a line (like deprecated method) over your context.

Abhinav Tyagi
  • 5,158
  • 3
  • 30
  • 60
  • This does not work, the method call fails silently. That's the first thing I tried. The only way to remove an existing context that still has a lifespan remaining, is to put it in the intent's output context with lifespan=0. Doing this from the code simply does not work (for me). If you have an example where it does, I'd love to see it and maybe understand why mine does not work. – eran Feb 28 '18 at 13:27
0

I know this question is old, at least old enough so that we now have a v2 API and library overhaul, so I'll answer anyway with today's solution :) .

1 - To delete a context, you can use conv.contexts.delete('context1'); as specified on the Node.js library reference docs.

2- If conv.contexts.set('context1', 1); doesn't change the context's lifespan then you can easily delete it and recreate it with these two calls.

Amine I.
  • 151
  • 12
0

My experience is that you cannot overwrite context data. You can create a new context though:

agent.setContext({
    name: contextName,
    lifespan: newLifeSpan,
    // Note: Parameters are not visible until the context is passed
    // console.log() won't show them now.
    parameters: {
      // Previously saved using getContext()
      param_name : paramValue,
    }
  });
user3830747
  • 121
  • 2
  • 3