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

Method WrapModelCall

pkg/middleware/logic_memory.go:180–254  ·  view source on GitHub ↗

WrapModelCall 包装模型调用,注入 Logic Memory 到 system prompt

(ctx context.Context, req *ModelRequest, handler ModelCallHandler)

Source from the content-addressed store, hash-verified

178
179// WrapModelCall 包装模型调用,注入 Logic Memory 到 system prompt
180func (m *LogicMemoryMiddleware) WrapModelCall(ctx context.Context, req *ModelRequest, handler ModelCallHandler) (*ModelResponse, error) {
181 // 如果不启用注入,直接调用下一层
182 if !m.config.EnableInjection {
183 return handler(ctx, req)
184 }
185
186 // 获取 namespace
187 namespace := m.namespaceExtractor(req)
188 if namespace == "" {
189 // 没有 namespace,跳过注入
190 return handler(ctx, req)
191 }
192
193 // 检索相关 Memory
194 memories, err := m.manager.RetrieveMemories(ctx, namespace,
195 logic.WithTopK(m.config.MaxMemories),
196 logic.WithMinConfidence(m.config.MinConfidence),
197 logic.WithOrderBy(logic.OrderByConfidence),
198 )
199 if err != nil {
200 lmLog.Error(ctx, "failed to retrieve memories", map[string]any{"error": err.Error()})
201 // 继续执行,不因为 Memory 检索失败而中断
202 return handler(ctx, req)
203 }
204
205 // 如果没有相关 Memory,直接调用
206 if len(memories) == 0 {
207 return handler(ctx, req)
208 }
209
210 // 保存原始 system prompt
211 originalSystemPrompt := req.SystemPrompt
212
213 // 构建 Memory 注入文本
214 memorySection := m.buildMemorySection(memories)
215
216 // 根据配置的注入点注入
217 switch m.config.InjectionPoint {
218 case "system_prompt_start":
219 if originalSystemPrompt != "" {
220 req.SystemPrompt = memorySection + "\n\n" + originalSystemPrompt
221 } else {
222 req.SystemPrompt = memorySection
223 }
224 case "system_prompt_end":
225 if originalSystemPrompt != "" {
226 req.SystemPrompt = originalSystemPrompt + "\n\n" + memorySection
227 } else {
228 req.SystemPrompt = memorySection
229 }
230 case "both":
231 if originalSystemPrompt != "" {
232 req.SystemPrompt = memorySection + "\n\n" + originalSystemPrompt + "\n\n" + memorySection
233 } else {
234 req.SystemPrompt = memorySection
235 }
236 default:
237 // 默认追加到末尾

Calls 8

buildMemorySectionMethod · 0.95
WithTopKFunction · 0.92
WithMinConfidenceFunction · 0.92
WithOrderByFunction · 0.92
handlerFunction · 0.85
RetrieveMemoriesMethod · 0.80
ErrorMethod · 0.65
DebugMethod · 0.65

Tested by 1