feat(api): implement stateful responses api (#122)

* feat(api): implement stateful responses api

Signed-off-by: mudler <mudler@localai.io>

* fix(tests): align client to API changes

Signed-off-by: mudler <mudler@localai.io>

---------

Signed-off-by: mudler <mudler@localai.io>
This commit is contained in:
Ettore Di Giacinto
2025-04-01 18:00:37 +02:00
committed by GitHub
parent 86cb9f1282
commit f3c06b1bfb
6 changed files with 68 additions and 16 deletions

View File

@@ -10,11 +10,14 @@ import (
"strings"
"time"
"github.com/google/uuid"
coreTypes "github.com/mudler/LocalAgent/core/types"
"github.com/mudler/LocalAgent/pkg/llm"
"github.com/mudler/LocalAgent/pkg/xlog"
"github.com/mudler/LocalAgent/services"
"github.com/mudler/LocalAgent/services/connectors"
"github.com/mudler/LocalAgent/webui/types"
"github.com/sashabaranov/go-openai"
"github.com/sashabaranov/go-openai/jsonschema"
"github.com/mudler/LocalAgent/core/sse"
@@ -405,7 +408,7 @@ func (a *App) ListActions() func(c *fiber.Ctx) error {
}
}
func (a *App) Responses(pool *state.AgentPool) func(c *fiber.Ctx) error {
func (a *App) Responses(pool *state.AgentPool, tracker *connectors.ConversationTracker[string]) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
var request types.RequestBody
if err := c.BodyParser(&request); err != nil {
@@ -414,9 +417,15 @@ func (a *App) Responses(pool *state.AgentPool) func(c *fiber.Ctx) error {
request.SetInputByType()
agentName := request.Model
var previousResponseID string
conv := []openai.ChatCompletionMessage{}
if request.PreviousResponseID != nil {
previousResponseID = *request.PreviousResponseID
conv = tracker.GetConversation(previousResponseID)
}
messages := request.ToChatCompletionMessages()
agentName := request.Model
messages := append(conv, request.ToChatCompletionMessages()...)
a := pool.GetAgent(agentName)
if a == nil {
@@ -435,7 +444,17 @@ func (a *App) Responses(pool *state.AgentPool) func(c *fiber.Ctx) error {
xlog.Info("we got a response from the agent", "agent", agentName, "response", res.Response)
}
conv = append(conv, openai.ChatCompletionMessage{
Role: "assistant",
Content: res.Response,
})
id := uuid.New().String()
tracker.SetConversation(id, conv)
response := types.ResponseBody{
ID: id,
Object: "response",
// "created_at": 1741476542,
CreatedAt: time.Now().Unix(),

View File

@@ -1,15 +1,20 @@
package webui
import "github.com/mudler/LocalAgent/core/state"
import (
"time"
"github.com/mudler/LocalAgent/core/state"
)
type Config struct {
DefaultChunkSize int
Pool *state.AgentPool
ApiKeys []string
LLMAPIURL string
LLMAPIKey string
LLMModel string
StateDir string
DefaultChunkSize int
Pool *state.AgentPool
ApiKeys []string
LLMAPIURL string
LLMAPIKey string
LLMModel string
StateDir string
ConversationStoreDuration time.Duration
}
type Option func(*Config)
@@ -20,6 +25,16 @@ func WithDefaultChunkSize(size int) Option {
}
}
func WithConversationStoreduration(duration string) Option {
return func(c *Config) {
d, err := time.ParseDuration(duration)
if err != nil {
d = 1 * time.Hour
}
c.ConversationStoreDuration = d
}
}
func WithStateDir(dir string) Option {
return func(c *Config) {
c.StateDir = dir

View File

@@ -13,6 +13,8 @@ import (
"github.com/gofiber/fiber/v2/middleware/filesystem"
"github.com/gofiber/fiber/v2/middleware/keyauth"
"github.com/mudler/LocalAgent/core/sse"
"github.com/mudler/LocalAgent/services/connectors"
"github.com/mudler/LocalAgent/core/state"
"github.com/mudler/LocalAgent/core/types"
"github.com/mudler/LocalAgent/pkg/xlog"
@@ -75,12 +77,17 @@ func (app *App) registerRoutes(pool *state.AgentPool, webapp *fiber.App) {
webapp.Post("/api/chat/:name", app.Chat(pool))
webapp.Post("/api/notify/:name", app.Notify(pool))
webapp.Post("/v1/responses", app.Responses(pool))
conversationTracker := connectors.NewConversationTracker[string](app.config.ConversationStoreDuration)
webapp.Post("/v1/responses", app.Responses(pool, conversationTracker))
// New API endpoints for getting and updating agent configuration
webapp.Get("/api/agent/:name/config", app.GetAgentConfig(pool))
webapp.Put("/api/agent/:name/config", app.UpdateAgentConfig(pool))
// Metadata endpoint for agent configuration fields
webapp.Get("/api/agent/config/metadata", app.GetAgentConfigMeta())
// Add endpoint for getting agent config metadata
webapp.Get("/api/meta/agent/config", app.GetAgentConfigMeta())