tryReplayCachedResponse looks up the latest user message in the agent's response cache. On a hit it replays the cached answer as the assistant message, fires stop hooks (which lets user-defined stop hooks run as they would for a real response), and returns true so the caller can short-circuit the ru
( ctx context.Context, sess *session.Session, a *agent.Agent, events EventSink, )
| 51 | // because no hook event currently supports short-circuiting the run with |
| 52 | // a synthetic response. |
| 53 | func (r *LocalRuntime) tryReplayCachedResponse( |
| 54 | ctx context.Context, |
| 55 | sess *session.Session, |
| 56 | a *agent.Agent, |
| 57 | events EventSink, |
| 58 | ) bool { |
| 59 | c := a.Cache() |
| 60 | if c == nil { |
| 61 | return false |
| 62 | } |
| 63 | question := sess.GetLastUserMessageContent() |
| 64 | if question == "" { |
| 65 | return false |
| 66 | } |
| 67 | _, cacheSpan := genai.RecordCacheLookup(ctx, "") |
| 68 | cached, ok := c.Lookup(question) |
| 69 | cacheSpan.SetHit(ok && cached != "") |
| 70 | cacheSpan.End() |
| 71 | // Treat empty stored values as misses: cache_response only stores |
| 72 | // non-empty responses, so an empty entry only surfaces if the JSON |
| 73 | // file was hand-edited or downgraded from a future version. Replaying |
| 74 | // nothing would leave the user staring at a blank assistant message, |
| 75 | // so we fall through to the model instead. |
| 76 | if !ok || cached == "" { |
| 77 | return false |
| 78 | } |
| 79 | |
| 80 | slog.DebugContext(ctx, "Response cache hit; replaying cached answer", |
| 81 | "agent", a.Name(), "session_id", sess.ID) |
| 82 | modelID := a.Model(ctx).ID().String() |
| 83 | events.Emit(AgentInfo(a.Name(), modelID, a.Description(), a.WelcomeMessage())) |
| 84 | addAgentMessage(sess, a, &chat.Message{ |
| 85 | Role: chat.MessageRoleAssistant, |
| 86 | Content: cached, |
| 87 | CreatedAt: time.Now().Format(time.RFC3339), |
| 88 | Model: modelID, |
| 89 | }, events) |
| 90 | r.executeStopHooks(ctx, sess, a, cached, events) |
| 91 | return true |
| 92 | } |
| 93 | |
| 94 | // cacheResponseBuiltin is the stop-hook builtin that stores the |
| 95 | // assistant's response in the agent's response cache. It is registered |
no test coverage detected