11# Placeholder Demo - Session State Integration
22
33This example demonstrates how to use placeholders in agent instructions with
4- session service integration. It covers two kinds of placeholders :
4+ session service integration. It covers three levels of state management :
55
6- - Unprefixed placeholder (readonly): ` {research_topics} ` . Initialized when the
7- session is created and intended not to be modified at runtime.
8- - Prefixed placeholders (modifiable): ` {user:topics} ` and ` {app:banner} ` .
9- These are backed by user/app state and can be updated via the session
10- service APIs.
6+ - ** Session-level state** : ` {research_topics} ` - Session-specific, updatable via ` UpdateSessionState ` API
7+ - ** User-level state** : ` {user:topics} ` - Shared across all sessions for a user, updatable via ` UpdateUserState ` API
8+ - ** App-level state** : ` {app:banner} ` - Shared across all users, updatable via ` UpdateAppState ` API
119
1210## Overview
1311
1412The demo implements an interactive command-line application that:
15- 1 . ** Unprefixed Placeholder (Readonly)** : ` {research_topics} ` is set at session
16- creation and is not meant to be mutated.
17- 2 . ** Prefixed Placeholders (Mutable)** : ` {user:topics} ` and ` {app:banner} ` can be
18- updated using the session service.
19- 3 . ** Dynamic Updates** : Changes to user/app state affect future responses.
20- 4 . ** Interactive Commands** : Command-line interface for managing session state
13+ 1 . ** Session-level State** : ` {research_topics} ` can be updated via ` UpdateSessionState ` API
14+ 2 . ** User-level State** : ` {user:topics} ` can be updated via ` UpdateUserState ` API
15+ 3 . ** App-level State** : ` {app:banner} ` can be updated via ` UpdateAppState ` API
16+ 4 . ** Dynamic Updates** : Changes to any state level affect future responses immediately
17+ 5 . ** Interactive Commands** : Command-line interface for managing all three state levels
2118
2219## Key Features
2320
24- - ** Placeholder Replacement** : ` {research_topics} ` , ` {user:topics} ` ,
25- ` {app:banner} ` are resolved from session state.
26- - ** Session State Management ** : In-memory session service stores app/ user state.
27- - ** Interactive Commands ** : ` /set-user-topics ` , ` /set-app-banner ` , ` /show- state` .
28- - ** Real-time Updates ** : Changes to session state immediately affect agent behavior
21+ - ** Placeholder Replacement** : ` {research_topics} ` , ` {user:topics} ` , ` {app:banner} ` are resolved from session state
22+ - ** Three-tier State Management ** : Session-level, user-level, and app-level state with different scopes
23+ - ** Interactive Commands ** : ` /set- session-topics ` , ` /set- user-topics ` , ` /set-app-banner ` , ` /show- state`
24+ - ** Real-time Updates ** : Changes to any state level immediately affect agent behavior
25+ - ** New UpdateSessionState API ** : Demonstrates direct session state updates without creating events
2926
3027## Architecture
3128
@@ -60,11 +57,11 @@ type placeholderDemo struct {
6057
6158### Research Agent
6259
63- - ** Purpose** : Specialized research assistant using placeholder values.
64- - ** Instructions** : Contains ` {research_topics} ` (readonly ), ` {user:topics?} `
65- and ` {app:banner?} ` .
60+ - ** Purpose** : Specialized research assistant using placeholder values from three state levels
61+ - ** Instructions** : Contains ` {research_topics} ` (session-level ), ` {user:topics?} ` (user-level),
62+ and ` {app:banner?} ` (app-level)
6663- ** Behavior** : Adapts based on session state; optional markers ` ? ` allow the
67- instruction to render even when a value is absent.
64+ instruction to render even when a value is absent
6865
6966## Usage
7067
@@ -85,24 +82,32 @@ go build -o placeholder-demo main.go
8582
8683The demo supports several interactive commands:
8784
88- - Set user topics (user state):
85+ - ** Set session topics** (session-level state):
86+ ``` bash
87+ /set-session-topics blockchain, web3, NFT, DeFi
88+ ```
89+ Updates ` {research_topics} ` via ` UpdateSessionState ` API. This is session-specific
90+ and only affects the current conversation.
91+
92+ - ** Set user topics** (user-level state):
8993 ``` bash
9094 /set-user-topics quantum computing, cryptography
9195 ```
92- Updates ` {user:topics} ` via ` UpdateUserState ` .
96+ Updates ` {user:topics} ` via ` UpdateUserState ` API. This is shared across all
97+ sessions for the same user.
9398
94- - Set app banner (app state):
99+ - ** Set app banner** (app-level state):
95100 ``` bash
96101 /set-app-banner Research Mode
97102 ```
98- Updates ` {app:banner} ` via ` UpdateAppState ` .
103+ Updates ` {app:banner} ` via ` UpdateAppState ` API. This is shared across all users .
99104
100- - Show current state snapshot:
105+ - ** Show current state** snapshot:
101106 ``` bash
102107 /show-state
103108 ```
104- Prints the current merged session state so you can see the keys :
105- ` research_topics ` , ` user:topics ` , ` app:banner ` .
109+ Prints the current merged session state showing all three levels :
110+ ` research_topics ` (session) , ` user:topics ` (user) , ` app:banner ` (app) .
106111
107112#### Regular Queries
108113``` bash
@@ -122,53 +127,68 @@ Ends the interactive session.
122127🔑 Placeholder Demo - Session State Integration
123128Model: deepseek-chat
124129Type 'exit' to end the session
125- Features: Dynamic placeholder replacement with session state
126- Commands: /set-user-topics <topics>, /set-app-banner <text>, /show-state
130+ Features: Unprefixed readonly and prefixed placeholders
131+ Commands:
132+ /set-session-topics <topics> - Update session-level research topics
133+ /set-user-topics <topics> - Update user-level topics
134+ /set-app-banner <text> - Update app-level banner
135+ /show-state - Show current session state
127136============================================================
128137
129138💡 Example interactions:
130139 • Ask: 'What are the latest developments?'
140+ • Update session topics: /set-session-topics 'blockchain, web3, NFT'
131141 • Set user topics: /set-user-topics 'quantum computing, cryptography'
132142 • Set app banner: /set-app-banner 'Research Mode'
133143 • Show state: /show-state
134144 • Ask: 'Explain recent breakthroughs'
135145
136- 👤 You: /show-topics
137- 📋 Current research topics: artificial intelligence, machine learning, deep learning, neural networks
146+ 👤 You: /show-state
147+ 📋 Current Session State:
148+ - research_topics: artificial intelligence, machine learning, deep learning, neural networks
149+ - user:topics: quantum computing, cryptography
150+ - app:banner: Research Mode
138151
139- 👤 You: /set-topics quantum computing, cryptography
140- ✅ Research topics updated to: quantum computing, cryptography
152+ 👤 You: /set-session-topics blockchain, web3, NFT, DeFi
153+ ✅ Session research topics updated to: blockchain, web3, NFT, DeFi
154+ 💡 The agent will now focus on these new topics in subsequent queries.
141155
142156👤 You: What are the latest developments?
143- 🔬 Research Agent: Based on the current research focus on quantum computing and cryptography , here are the latest developments...
157+ 🔬 Research Agent: Based on the current research focus on blockchain, web3, NFT, and DeFi , here are the latest developments...
144158```
145159
146160## Implementation Details
147161
148162### Placeholder Mechanism
149163
150- 1 . ** Initial Setup ** : Session is created with an unprefixed
151- ` research_topics ` value used by ` {research_topics} ` (readonly) .
152- 2 . ** Prefixed Placeholders ** : ` {user:topics} ` and ` {app:banner} ` resolve to
153- user/app state; they are populated by ` UpdateUserState ` and
154- ` UpdateAppState ` .
155- 3 . ** Optional Suffix ** : ` {...?...} ` returns empty string if the variable is not
156- present.
164+ 1 . ** Session-level State ** : ` {research_topics} ` is session-specific and can be updated
165+ via ` UpdateSessionState ` API. Changes only affect the current session .
166+ 2 . ** User-level State ** : ` {user:topics} ` resolves to user state and can be updated
167+ via ` UpdateUserState ` API. Changes affect all sessions for that user.
168+ 3 . ** App-level State ** : ` {app:banner} ` resolves to app state and can be updated
169+ via ` UpdateAppState ` API. Changes affect all users.
170+ 4 . ** Optional Suffix ** : ` {...?} ` returns empty string if the variable is not present.
157171
158172### Session State Management
159173
160174The demo uses in-memory session service for simplicity:
161175
162- - ** User State** : ` topics ` stored at user level (referenced as ` {user:topics} ` ).
163- - ** Session Persistence** : State maintained throughout session
164- - ** Real-time Updates** : Changes immediately available to agent
176+ - ** Session State** : ` research_topics ` stored at session level (referenced as ` {research_topics} ` )
177+ - ** User State** : ` topics ` stored at user level (referenced as ` {user:topics} ` )
178+ - ** App State** : ` banner ` stored at app level (referenced as ` {app:banner} ` )
179+ - ** State Persistence** : All three levels maintained throughout session
180+ - ** Real-time Updates** : Changes at any level immediately available to agent
181+ - ** State Isolation** : Each level has its own scope and lifetime
165182
166183### Command Processing
167184
168185The interactive interface processes commands through pattern matching:
169186
170- - ** State Commands** : ` /set-user-topics ` , ` /set-app-banner ` , ` /show-state ` for
171- session management
187+ - ** State Commands** :
188+ - ` /set-session-topics ` - Updates session-level state via ` UpdateSessionState `
189+ - ` /set-user-topics ` - Updates user-level state via ` UpdateUserState `
190+ - ` /set-app-banner ` - Updates app-level state via ` UpdateAppState `
191+ - ` /show-state ` - Displays merged state from all three levels
172192- ** Regular Input** : Passed directly to agent for processing
173193- ** Error Handling** : Graceful handling of invalid commands and state errors
174194
0 commit comments