MCPcopy Create free account
hub / github.com/astercloud/aster / demonstrateResourceScope

Function demonstrateResourceScope

examples/memory-working/main.go:104–166  ·  view source on GitHub ↗
(ctx context.Context, backend backends.BackendProtocol)

Source from the content-addressed store, hash-verified

102}
103
104func demonstrateResourceScope(ctx context.Context, backend backends.BackendProtocol) {
105 // 创建 resource scope 的 Working Memory 管理器
106 manager, err := memory.NewWorkingMemoryManager(&memory.WorkingMemoryConfig{
107 Backend: backend,
108 BasePath: "/working_memory_resource/",
109 Scope: memory.ScopeResource,
110 })
111 if err != nil {
112 log.Fatalf("create manager: %v", err)
113 }
114
115 thread1 := "edit-session-001"
116 thread2 := "edit-session-002"
117 resource := "article-123"
118
119 // Thread 1: 第一次编辑会话
120 initialState := `# Article: Getting Started with aster
121
122## Status
123Draft version: v0.1
124Last editor: Alice
125Sections completed: Introduction, Installation
126
127## TODOs
128- [ ] Add examples section
129- [ ] Add troubleshooting guide
130- [ ] Review and polish`
131
132 err = manager.Update(ctx, thread1, resource, initialState)
133 if err != nil {
134 log.Fatalf("update from thread 1: %v", err)
135 }
136 fmt.Println("✓ Thread 1 更新了文章状态")
137
138 // Thread 2: 第二次编辑会话(读取相同的 resource)
139 stateFromThread2, _ := manager.Get(ctx, thread2, resource)
140 fmt.Printf("\n📝 Thread 2 读取到的状态(来自同一 resource):\n%s\n", stateFromThread2)
141
142 // Thread 2: 继续编辑
143 updatedState := `# Article: Getting Started with aster
144
145## Status
146Draft version: v0.2
147Last editor: Bob
148Sections completed: Introduction, Installation, Examples
149
150## TODOs
151- [x] Add examples section
152- [ ] Add troubleshooting guide
153- [ ] Review and polish`
154
155 err = manager.Update(ctx, thread2, resource, updatedState)
156 if err != nil {
157 log.Fatalf("update from thread 2: %v", err)
158 }
159 fmt.Println("\n✓ Thread 2 更新了文章状态")
160
161 // Thread 1: 再次读取,会看到 Thread 2 的更新

Callers 1

mainFunction · 0.85

Calls 4

UpdateMethod · 0.95
GetMethod · 0.95
NewWorkingMemoryManagerFunction · 0.92
PrintfMethod · 0.80

Tested by

no test coverage detected