Skip to main content

Extending Gojent

Gojent keeps its execution loop private and opens four small interfaces at the seams (llm.Model/llm.Streamer, tool.Tool, session.Store, and agent.Policy/Approver/Sink). You can plug in custom model backends, durable storage engines, and cross-cutting middleware without modifying core code, then prove compatibility with the built-in llmtest and sessiontest conformance suites.

Implement a custom model

A model adapter implements three methods on llm.Model, and optionally adds llm.Streamer for incremental streaming:

type Model interface {
	ModelID() string
	Info() Info
	Generate(ctx context.Context, req *Request) (*Response, error)
}

type Streamer interface {
	Model
	Stream(ctx context.Context, req *Request) (Stream, error)
}

Keep Info() accurate: the runner inspects Info().Features before building requests instead of probing for optional behavior, and adapters must return llm.ErrUnsupported when asked for a capability they do not support.

Verify any custom llm.Model with core/llm/llmtest, which checks context cancellation and verifies that Generate and Stream agree on text, reasoning, tool calls, usage, and finish reasons:

package custommodel_test

import (
	"testing"

	"github.com/ijaidev/gojent/core/llm"
	"github.com/ijaidev/gojent/core/llm/llmtest"
	"github.com/ijaidev/gojent/providers/mockllm"
)

func TestCustomModelConformance(t *testing.T) {
	llmtest.Run(t, func(_ *testing.T) llm.Model {
		return mockllm.New("conformance response")
	}, llmtest.Case{
		Name:         "BasicText",
		Request:      &llm.Request{Messages: []llm.Message{llm.UserMessage("hello")}},
		Text:         "conformance response",
		FinishReason: llm.FinishReasonStop,
	})
}

Implement a custom session store

Implement Load and atomic Append on session.Store: return messages in chronological order, isolate conversations by both ID and ResourceID, and deep-copy values if your backend reuses buffers.

Verify your implementation against core/session/sessiontest, which tests empty sessions, chronological ordering, tenant isolation, atomic batch appends, replay state survival, session validation, and context cancellation:

package customstore_test

import (
	"testing"

	"github.com/ijaidev/gojent/core/session"
	"github.com/ijaidev/gojent/core/session/sessiontest"
)

func TestCustomStoreConformance(t *testing.T) {
	sessiontest.Run(t, func(t *testing.T) session.Store {
		s := session.NewMemory()
		t.Cleanup(func() { _ = s.Close() })
		return s
	})
}

Wrap seams for middleware

Instead of adding framework hooks to the core loop, compose behavior by wrapping llm.Model, tool.Tool, or session.Store:

package middleware

import (
	"context"
	"log"
	"time"

	"github.com/ijaidev/gojent/core/llm"
)

type loggingModel struct {
	next llm.Model
}

func (m loggingModel) ModelID() string { return m.next.ModelID() }
func (m loggingModel) Info() llm.Info  { return m.next.Info() }

func (m loggingModel) Generate(ctx context.Context, req *llm.Request) (*llm.Response, error) {
	start := time.Now()
	resp, err := m.next.Generate(ctx, req)
	log.Printf("model=%s duration=%s err=%v", m.next.ModelID(), time.Since(start), err)
	return resp, err
}

If the wrapped model also supports streaming, implement llm.Streamer on the wrapper (or delegate conditionally) so streaming runs continue to stream incrementally. See core/llm and core/session on pkg.go.dev.