Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions deploy/helm/javo/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,7 @@ config:
# url: http://mem0-nebari-mem0.mem0.svc:8000 # the mem0 pack's Service
# timeout: 10s # search, list, get and delete calls
# addTimeout: 120s # add calls, which run model extraction inline
# recallTimeout: 2s # the search before an agent's model call
# apiKeySecret:
# namespace: mem0 # required: the namespace the mem0 pack is installed in
# name: mem0-admin-key
Expand Down
116 changes: 108 additions & 8 deletions internal/ai/agentproxy/agentproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package agentproxy
import (
"bufio"
"bytes"
"cmp"
"context"
"encoding/json"
"errors"
Expand All @@ -21,6 +22,10 @@ import (
"slices"
"strconv"
"strings"
"sync"
"time"

"github.com/nebari-dev/javo/internal/memory"
)

// Route is where the endpoint is served; an agent's base URL is
Expand Down Expand Up @@ -70,6 +75,30 @@ type Proxy struct {
Caller func(ctx context.Context) (person string, ok bool)
// Client sends the requests; nil is http.DefaultClient.
Client *http.Client
// Memory is the person's memory store; nil turns memory off, and requests
// are forwarded exactly as without it.
Memory Memory
// Recall caches recall results so the steps of one agent tool loop do not
// each search; nil searches every time.
Recall *RecallCache
// RecallTimeout is the longest a call waits for memories before going on
// without them; zero or less is 2 seconds.
RecallTimeout time.Duration

learnSlotsOnce sync.Once
learnSlots chan struct{}
// learning counts running learn goroutines, so tests can wait for them.
learning sync.WaitGroup
}

// defaultRecallTimeout is used when Proxy.RecallTimeout is not set.
const defaultRecallTimeout = 2 * time.Second

// Memory is what the proxy needs from the memory store.
type Memory interface {
Search(ctx context.Context, owner, agent, query string, limit int) ([]memory.Memory, error)
Add(ctx context.Context, owner, agent string, msgs []memory.Message) ([]memory.Memory, error)
Remember(ctx context.Context, owner, text string) ([]memory.Memory, error)
}

// Register adds the routes to mux.
Expand Down Expand Up @@ -134,6 +163,31 @@ func (p *Proxy) chat(w http.ResponseWriter, r *http.Request) {
return
}
body, model, stream := rewrite(body, up)
// text, typed and skipLearning are for learning from the exchange; recall
// below changes body only when it adds the note.
text, typed, skipLearning := "", false, false
if p.Memory != nil {
var msgs []json.RawMessage
if json.Unmarshal(body["messages"], &msgs) == nil && msgs != nil {
text, typed = lastUserText(msgs)
skipLearning = housekeeping(msgs, text)
if skipLearning && typed && !summarization(msgs) {
slog.Info("memory.learn_skipped", "user", person, "reason", "too_long")
}
if text != "" && !skipLearning {
if mems := p.recall(r.Context(), person, text); len(mems) > 0 {
if with := withNote(msgs, mems); len(with) != len(msgs) || !sameFirst(with, msgs) {
body["messages"], _ = json.Marshal(with)
}
}
}
}
}
// ans collects the answer text only when the exchange may be learned.
var ans *answer
if p.Memory != nil && typed && !skipLearning && text != "" {
ans = &answer{}
}
out, _ := json.Marshal(body)

req, err := http.NewRequestWithContext(r.Context(), http.MethodPost, up.base()+"/chat/completions", bytes.NewReader(out))
Expand Down Expand Up @@ -161,18 +215,27 @@ func (p *Proxy) chat(w http.ResponseWriter, r *http.Request) {
}
w.WriteHeader(resp.StatusCode)
var in, outTok int
var werr error
if strings.HasPrefix(resp.Header.Get("Content-Type"), "text/event-stream") {
in, outTok, _ = copyEvents(w, resp.Body)
in, outTok, _, werr = copyEvents(w, resp.Body, ans)
} else {
b, _ := io.ReadAll(io.LimitReader(resp.Body, maxAnswerBytes))
_, _ = w.Write(b)
b, rerr := io.ReadAll(io.LimitReader(resp.Body, maxAnswerBytes))
_, werr = w.Write(b)
werr = cmp.Or(rerr, werr)
in, outTok, _ = usageOf(b)
ans.addMessage(b)
}
result := "ok"
if resp.StatusCode >= 400 {
result = fmt.Sprintf("status %d", resp.StatusCode)
}
audit(person, model, in, outTok, result)
// werr covers a failed read or write; the context covers an agent that went
// away while every write still succeeded. Either way the agent did not get
// the whole answer, so the exchange is not learned.
if ans != nil && resp.StatusCode/100 == 2 && werr == nil && r.Context().Err() == nil {
p.startLearning(r.Context(), person, text, ans.String())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A real agent disconnect still gets learned here, with a cut-off answer. The disconnect cancels r.Context(), the upstream read fails, and copyEvents returns a nil error (line 343 ignores sc.Err()), so werr stays nil. The brokenWriter test in learn_test.go:262 fails the write instead, so it doesn't cover this path. Returning sc.Err() from copyEvents and also requiring r.Context().Err() == nil before startLearning would close it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 5882449, with both changes. The non-streamed path had the same hole: it ignored the io.ReadAll error, so a disconnect there learned the typed turn alone. It now carries the read error too.

I added five subtests to TestLearnSkipsFailedAnswers covering a cancelled context mid-stream, during a non-streamed answer, and at the end of a complete stream, plus an upstream connection drop in each path. I also checked that each part of the fix is needed: dropping sc.Err(), the read error, or the context check each makes its own test fail.

}
}

// forwarded are the OpenAI chat completions fields that go to the provider;
Expand Down Expand Up @@ -259,20 +322,25 @@ func (p *Proxy) client(up Upstream) *http.Client {
}

// copyEvents passes a server-sent event stream through line by line,
// flushing each event, and reads the usage the last events report.
func copyEvents(w http.ResponseWriter, r io.Reader) (in, out int, found bool) {
// flushing each event, and reads the usage the last events report. When ans
// is not nil it collects the answer text the events carry. err is the first
// error writing to w, after which copying stops, or else the error that ended
// reading r early.
func copyEvents(w http.ResponseWriter, r io.Reader, ans *answer) (in, out int, found bool, err error) {
f, _ := w.(http.Flusher)
sc := bufio.NewScanner(r)
sc.Buffer(make([]byte, 64<<10), 16<<20)
for sc.Scan() {
line := sc.Bytes()
if data, ok := bytes.CutPrefix(line, []byte("data:")); ok {
if i, o, ok := usageOf(bytes.TrimSpace(data)); ok {
data = bytes.TrimSpace(data)
if i, o, ok := usageOf(data); ok {
in, out, found = i, o, true
}
ans.addDelta(data)
}
if _, err := w.Write(append(line, '\n')); err != nil {
return in, out, found
return in, out, found, err
}
if len(line) == 0 && f != nil {
f.Flush()
Expand All @@ -281,7 +349,7 @@ func copyEvents(w http.ResponseWriter, r io.Reader) (in, out int, found bool) {
if f != nil {
f.Flush()
}
return in, out, found
return in, out, found, sc.Err()
}

// usageOf reads the usage of an answer or an event.
Expand All @@ -303,3 +371,35 @@ func usageOf(b []byte) (in, out int, found bool) {
func audit(person, model string, in, out int, result string) {
slog.Info("ai.call", "user", person, "feature", "agent", "model", model, "tokens_in", in, "tokens_out", out, "outcome", result)
}

// sameFirst reports whether both slices start with the same message, which
// withNote does only when it added nothing.
func sameFirst(a, b []json.RawMessage) bool {
return len(a) > 0 && len(b) > 0 && bytes.Equal(a[0], b[0])
}

// recall returns the memories to show for the query: the cached result, else
// a search that waits at most the recall timeout. A failure is logged without
// the query and is not cached, so the call goes on without memories.
func (p *Proxy) recall(ctx context.Context, person, query string) []memory.Memory {
if p.Recall != nil {
if mems, ok := p.Recall.get(person, query); ok {
return mems
}
}
timeout := p.RecallTimeout
if timeout <= 0 {
timeout = defaultRecallTimeout
}
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
mems, err := p.Memory.Search(ctx, person, "", query, maxNoteMemories)
if err != nil {
slog.Warn("memory.recall_failed", "user", person, "err", err)
return nil
}
if p.Recall != nil {
p.Recall.put(person, query, mems)
}
return mems
}
4 changes: 4 additions & 0 deletions internal/ai/agentproxy/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package agentproxy

// WaitLearning blocks until every learn goroutine p started has finished.
func WaitLearning(p *Proxy) { p.learning.Wait() }
Loading