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

Function middlewareExample

examples/logic-memory/main.go:255–316  ·  view source on GitHub ↗

middlewareExample demonstrates LogicMemoryMiddleware integration

(ctx context.Context)

Source from the content-addressed store, hash-verified

253
254// middlewareExample demonstrates LogicMemoryMiddleware integration
255func middlewareExample(ctx context.Context) {
256 fmt.Println("--- Example 3: Middleware Integration ---")
257
258 store := logic.NewInMemoryStore()
259
260 // Pre-populate some memories
261 _ = store.Save(ctx, &logic.LogicMemory{
262 ID: "mem-1",
263 Namespace: "user:charlie",
264 Scope: logic.ScopeUser,
265 Type: "preference",
266 Key: "response_format",
267 Value: "bullet_points",
268 Description: "Charlie prefers bullet-point lists over paragraphs",
269 Provenance: &memory.MemoryProvenance{
270 SourceType: memory.SourceUserInput,
271 Confidence: 0.85,
272 },
273 })
274
275 manager, _ := logic.NewManager(&logic.ManagerConfig{Store: store})
276
277 // Create the middleware
278 mw, err := middleware.NewLogicMemoryMiddleware(&middleware.LogicMemoryMiddlewareConfig{
279 Manager: manager,
280
281 // Capture configuration
282 EnableCapture: true,
283 AsyncCapture: true, // Non-blocking capture
284
285 // Injection configuration
286 EnableInjection: true,
287 MaxMemories: 5,
288 MinConfidence: 0.6,
289 InjectionPoint: "system_prompt_end",
290 })
291 if err != nil {
292 log.Fatalf("Failed to create middleware: %v", err)
293 }
294
295 fmt.Printf("Middleware created: %s (priority: %d)\n", mw.Name(), mw.Priority())
296 fmt.Printf("Configuration: %+v\n", mw.GetConfig())
297
298 // Manually capture events
299 mw.CaptureUserFeedback("user:charlie", "Great response!", 5, nil)
300 mw.CaptureUserRevision("user:charlie",
301 "The results are as follows:",
302 "Here are the results:",
303 nil,
304 )
305
306 fmt.Println("Events captured via middleware")
307
308 // Get available tools
309 tools := mw.Tools()
310 fmt.Printf("Available tools: %d\n", len(tools))
311 for _, t := range tools {
312 fmt.Printf(" - %s: %s\n", t.Name(), t.Description())

Callers 1

mainFunction · 0.85

Calls 12

SaveMethod · 0.95
GetConfigMethod · 0.95
CaptureUserFeedbackMethod · 0.95
CaptureUserRevisionMethod · 0.95
ToolsMethod · 0.95
NewInMemoryStoreFunction · 0.92
NewManagerFunction · 0.92
NewLogicMemoryMiddlewareFunction · 0.92
PrintfMethod · 0.80
NameMethod · 0.65
PriorityMethod · 0.65
DescriptionMethod · 0.65

Tested by

no test coverage detected