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

Method Update

pkg/memory/working.go:142–206  ·  view source on GitHub ↗

Update 更新 Working Memory 内容 content: 新的内容(Markdown 或 JSON 字符串) 如果配置了 Schema,会先进行验证

(ctx context.Context, threadID, resourceID, content string)

Source from the content-addressed store, hash-verified

140// content: 新的内容(Markdown 或 JSON 字符串)
141// 如果配置了 Schema,会先进行验证
142func (wm *WorkingMemoryManager) Update(ctx context.Context, threadID, resourceID, content string) error {
143 if threadID == "" && resourceID == "" {
144 return errors.New("threadID and resourceID cannot both be empty")
145 }
146
147 content = strings.TrimSpace(content)
148 if content == "" {
149 return errors.New("content cannot be empty")
150 }
151
152 // Schema 验证(如果配置)
153 if wm.schema != nil {
154 if err := wm.schema.ValidateContent(content); err != nil {
155 return fmt.Errorf("schema validation failed: %w", err)
156 }
157 }
158
159 // 构建元数据
160 now := time.Now()
161 meta := WorkingMemoryMeta{
162 ThreadID: threadID,
163 ResourceID: resourceID,
164 UpdatedAt: now,
165 }
166
167 // 尝试读取现有数据以保留 CreatedAt
168 path := wm.resolvePath(threadID, resourceID)
169 existingContent, err := wm.backend.Read(ctx, path, 0, 0)
170 if err == nil {
171 var existingData WorkingMemoryData
172 if json.Unmarshal([]byte(existingContent), &existingData) == nil {
173 meta.CreatedAt = existingData.Meta.CreatedAt
174 }
175 }
176
177 // 如果是新创建,设置 CreatedAt
178 if meta.CreatedAt.IsZero() {
179 meta.CreatedAt = now
180 }
181
182 // 设置过期时间(如果配置)
183 if wm.ttl > 0 {
184 expiresAt := now.Add(wm.ttl)
185 meta.ExpiresAt = &expiresAt
186 }
187
188 // 构建完整数据
189 data := WorkingMemoryData{
190 Meta: meta,
191 Content: content,
192 }
193
194 // 序列化为 JSON
195 jsonData, err := json.MarshalIndent(data, "", " ")
196 if err != nil {
197 return fmt.Errorf("marshal working memory: %w", err)
198 }
199

Calls 5

resolvePathMethod · 0.95
ValidateContentMethod · 0.80
ReadMethod · 0.65
AddMethod · 0.65
WriteMethod · 0.65