会话存储空间
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
您可以将对话中特定用户的参数值存储在
会话存储空间然后,您的 Action 可以稍后在提示中使用这些存储的值
您的网络钩子代码可以访问会话存储空间中的值
必要时进行对话。
在对话过程中,使用 types 收集的所有数据都会存储在会话中
storage。您还可以使用 webhook 调用与会话存储空间中的数据进行交互。
对于 webhook 调用,会话存储的状态通过 app.handle()
传递
请求并存储在 session
对象中。
会话存储空间中存储的数据会在对话结束后过期。
读取和写入会话存储空间数据
要在会话存储空间中更新或设置新值,请将该值分配给
网络钩子调用中 session
对象的 params
字段。以下示例
设置“exampleColor”设为“红色”会话存储量:
Node.js
// Assign color to session storage
app.handle('storeColor', conv => {
let color = 'red';
conv.session.params.exampleColor = color;
});
JSON
{
"responseJson": {
"session": {
"id": "12345678901234567890",
"params": {
"exampleColor": "red"
}
},
"prompt": {
"override": false
}
}
}
如需访问存储在会话存储空间中的数据,请将其分配给 webhook 中的变量
调用。以下示例从“exampleColor”中检索值会话中
storage:
Node.js
// Retrieve color from session storage
app.handle('getStoredColor', conv => {
let color = conv.session.params.exampleColor;
});
JSON
{
"responseJson": {
"session": {
"id": "12345678901234567890",
"params": {
"exampleColor": "red"
}
},
"prompt": {
"override": false
}
}
}
如需清除之前保存的值,请在 webhook 调用中将值设置为 null
。
以下示例清除了“exampleColor”的值会话存储量:
Node.js
// Clear color from session storage
app.handle('clearStoredColor', conv => {
conv.session.params.exampleColor = null;
});
JSON
{
"responseJson": {
"session": {
"id": "12345678901234567890",
"params": {}
},
"prompt": {
"override": false
}
}
}
在提示中引用存储的值
您可以在提示中引用存储在会话存储空间中的值。要引用
值,请使用 $session.params.PARAMETER_NAME
语法,其中
PARAMETER_NAME
是在 webhook 中指定的名称,
已设置。
例如,您之前在会话存储空间中存储了颜色值,
参数 exampleColor
。如需在提示中访问该值,您需要引用
值使用 $session.params.exampleColor
:
JSON
{
"candidates": [{
"first_simple": {
"variants": [{
"speech": "Your favorite color is $session.params.exampleColor."
}]
}
}]
}
在条件中引用存储值
您还可以在条件中引用存储在会话存储空间中的值。接收者
引用该值,请使用 session.params.PARAMETER_NAME
语法,其中 PARAMETER_NAME
是在 webhook 中给出的名称时,
参数所设置的值。
例如,您之前在会话存储空间中存储了颜色值,
参数 exampleColor
,您希望将其与值“red”匹配以
条件。在条件中,您可以使用
session.params.exampleColor
。然后,您的条件表达式将如下所示:
条件语法
session.params.exampleColor == "red"
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eYou can utilize session storage to save user-specific parameter values during a conversation, enabling your Action to reuse them in prompts and conditions, and your webhook code to access them when needed.\u003c/p\u003e\n"],["\u003cp\u003eSession storage automatically stores data gathered using types during a conversation, and you can interact with this data via webhook calls using the \u003ccode\u003esession\u003c/code\u003e object.\u003c/p\u003e\n"],["\u003cp\u003eTo modify or add values in session storage, assign them to the \u003ccode\u003eparams\u003c/code\u003e field of the \u003ccode\u003esession\u003c/code\u003e object within a webhook call; to retrieve them, assign them to a variable within a webhook call.\u003c/p\u003e\n"],["\u003cp\u003eStored values in session storage can be referenced in prompts using the \u003ccode\u003e$session.params.<PARAMETER_NAME>\u003c/code\u003e syntax and in conditions using the \u003ccode\u003esession.params.<PARAMETER_NAME>\u003c/code\u003e syntax.\u003c/p\u003e\n"],["\u003cp\u003eData stored in session storage is ephemeral and automatically deleted when the conversation concludes.\u003c/p\u003e\n"]]],[],null,["You can store parameter values for a specific user within a conversation in\nsession storage. Your Action can then use those stored values later in prompts\nand conditions, and your webhook code can access values in session storage for\nthe conversation when necessary.\n\nDuring a conversation, any data collected using [types](/assistant/conversational/types) is stored in session\nstorage. You can also interact with data in session storage using webhook calls.\nFor webhook calls, the state of session storage is passed in an `app.handle()`\nrequest and is stored in the [`session`](/assistant/conversational/reference/rest/v1/TopLevel/fulfill#Session) object.\n\nData stored in session storage expires when a conversation ends.\n\nRead and write data to session storage\n\nTo update or set a new value in session storage, assign the value to the\n`params` field of the `session` object in a webhook call. The following example\nsets \"exampleColor\" to \"red\" in session storage: \n\nNode.js \n\n```javascript\n// Assign color to session storage\napp.handle('storeColor', conv =\u003e {\n let color = 'red';\n conv.session.params.exampleColor = color;\n});\n \n```\n\nJSON \n\n```text\n{\n \"responseJson\": {\n \"session\": {\n \"id\": \"12345678901234567890\",\n \"params\": {\n \"exampleColor\": \"red\"\n }\n },\n \"prompt\": {\n \"override\": false\n }\n }\n}\n \n```\n\nTo access data stored in session storage, assign it to a variable in a webhook\ncall. The following example retrieves a value from \"exampleColor\" in session\nstorage: \n\nNode.js \n\n```javascript\n// Retrieve color from session storage\napp.handle('getStoredColor', conv =\u003e {\n let color = conv.session.params.exampleColor;\n});\n \n```\n\nJSON \n\n```text\n{\n \"responseJson\": {\n \"session\": {\n \"id\": \"12345678901234567890\",\n \"params\": {\n \"exampleColor\": \"red\"\n }\n },\n \"prompt\": {\n \"override\": false\n }\n }\n}\n \n```\n\nTo clear a previously saved value, set the value to `null` in a webhook call.\nThe following example clears the value of \"exampleColor\" in session storage: \n\nNode.js \n\n```javascript\n// Clear color from session storage\napp.handle('clearStoredColor', conv =\u003e {\n conv.session.params.exampleColor = null;\n});\n \n```\n\nJSON \n\n```text\n{\n \"responseJson\": {\n \"session\": {\n \"id\": \"12345678901234567890\",\n \"params\": {}\n },\n \"prompt\": {\n \"override\": false\n }\n }\n}\n \n```\n\nReference stored values within prompts\n\nYou can reference values stored in session storage in a [prompt](/assistant/conversational/prompts). To reference the\nvalue, use `$session.params.`\u003cvar translate=\"no\"\u003ePARAMETER_NAME\u003c/var\u003e syntax, where\n\u003cvar translate=\"no\"\u003ePARAMETER_NAME\u003c/var\u003e is the name given in the webhook when the parameter\nwas set.\n\nFor example, you previously stored a color value in session storage as the\nparameter `exampleColor`. To access that value in a prompt, you reference that\nvalue using `$session.params.exampleColor`: \n\nJSON \n\n```gdscript\n{\n \"candidates\": [{\n \"first_simple\": {\n \"variants\": [{\n \"speech\": \"Your favorite color is $session.params.exampleColor.\"\n }]\n }\n }]\n}\n \n```\n\nReference stored values within conditions\n\nYou can also reference values stored in session storage in [conditions](/assistant/conversational/conditions). To\nreference the value, use the `session.params.`\u003cvar translate=\"no\"\u003ePARAMETER_NAME\u003c/var\u003e\nsyntax, where \u003cvar translate=\"no\"\u003ePARAMETER_NAME\u003c/var\u003e is the name given in the webhook when\nthe parameter was set.\n\nFor example, you previously stored a color value in session storage as the\nparameter `exampleColor`, and you want to match it with the value \"red\" in a\ncondition. In your condition, you reference the stored value using\n`session.params.exampleColor`. Your condition expression then looks like this: \n\nCondition syntax \n\n```text\nsession.params.exampleColor == \"red\"\n \n```"]]