cacheResponseBuiltin is the stop-hook builtin that stores the assistant's response in the agent's response cache. It is registered as a closure on the runtime's hooks registry so it can resolve the agent (and therefore its cache instance) by name from [hooks.Input.AgentName]. The hook is a no-op wh
(ctx context.Context, in *hooks.Input, _ []string)
| 104 | // where [LocalRuntime.tryReplayCachedResponse] fires stop hooks for the |
| 105 | // cached answer — free of redundant disk writes. |
| 106 | func (r *LocalRuntime) cacheResponseBuiltin(ctx context.Context, in *hooks.Input, _ []string) (*hooks.Output, error) { |
| 107 | if in == nil || in.AgentName == "" || in.LastUserMessage == "" || |
| 108 | strings.TrimSpace(in.StopResponse) == "" { |
| 109 | return nil, nil |
| 110 | } |
| 111 | a, err := r.team.Agent(in.AgentName) |
| 112 | if err != nil || a == nil { |
| 113 | slog.DebugContext(ctx, "cache_response: agent lookup failed", |
| 114 | "agent", in.AgentName, "error", err) |
| 115 | return nil, nil |
| 116 | } |
| 117 | if c := a.Cache(); c != nil { |
| 118 | // Thread the active context so the cache.store span chains |
| 119 | // onto the surrounding stop-hook trace instead of starting a |
| 120 | // detached one. Mark the operation as a successful write so |
| 121 | // the `cagent.cache.requests{operation="store"}` counter is |
| 122 | // incremented — without SetHit the store path would never |
| 123 | // register on the metric. |
| 124 | _, storeSpan := genai.RecordCacheStore(ctx, "") |
| 125 | c.Store(in.LastUserMessage, in.StopResponse) |
| 126 | storeSpan.SetHit(true) |
| 127 | storeSpan.End() |
| 128 | } |
| 129 | return nil, nil |
| 130 | } |