Add Counter action to count things (#38)

* add host

* fix: make user name explicit in IRC

* feat: Add counter action
This commit is contained in:
Richard Palethorpe
2025-03-11 21:54:35 +00:00
committed by GitHub
parent 3763f320b9
commit 7486e68a17
4 changed files with 104 additions and 3 deletions

View File

@@ -7,8 +7,8 @@ services:
# Image list (dockerhub): https://hub.docker.com/r/localai/localai
image: localai/localai:latest-cpu
command:
- rombo-org_rombo-llm-v3.0-qwen-32b # minimum suggested model
#- marco-o1 (smaller)
# - rombo-org_rombo-llm-v3.0-qwen-32b # minimum suggested model
- marco-o1 # (smaller)
- granite-embedding-107m-multilingual
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/readyz"]
@@ -67,5 +67,7 @@ services:
- LOCALAGENT_STATE_DIR=/pool
- LOCALAGENT_TIMEOUT=5m
- LOCALAGENT_ENABLE_CONVERSATIONS_LOGGING=false
extra_hosts:
- "host.docker.internal:host-gateway"
volumes:
- ./volumes/localagent/:/pool

View File

@@ -25,6 +25,7 @@ const (
ActionBrowse = "browse"
ActionSendMail = "send_mail"
ActionGenerateImage = "generate_image"
ActionCounter = "counter"
)
var AvailableActions = []string{
@@ -39,6 +40,7 @@ var AvailableActions = []string{
ActionWikipedia,
ActionSendMail,
ActionGenerateImage,
ActionCounter,
}
func Actions(a *state.AgentConfig) func(ctx context.Context) []agent.Action {
@@ -80,6 +82,8 @@ func Actions(a *state.AgentConfig) func(ctx context.Context) []agent.Action {
allActions = append(allActions, actions.NewBrowse(config))
case ActionSendMail:
allActions = append(allActions, actions.NewSendMail(config))
case ActionCounter:
allActions = append(allActions, actions.NewCounter(config))
}
}

View File

@@ -0,0 +1,95 @@
package actions
import (
"context"
"fmt"
"sync"
"github.com/mudler/LocalAgent/core/action"
"github.com/sashabaranov/go-openai/jsonschema"
)
// CounterAction manages named counters that can be created, updated, and queried
type CounterAction struct {
counters map[string]int
mutex sync.RWMutex
}
// NewCounter creates a new counter action
func NewCounter(config map[string]string) *CounterAction {
return &CounterAction{
counters: make(map[string]int),
mutex: sync.RWMutex{},
}
}
// Run executes the counter action
func (a *CounterAction) Run(ctx context.Context, params action.ActionParams) (action.ActionResult, error) {
// Parse parameters
request := struct {
Name string `json:"name"`
Adjustment int `json:"adjustment"`
}{}
if err := params.Unmarshal(&request); err != nil {
return action.ActionResult{}, fmt.Errorf("invalid parameters: %w", err)
}
if request.Name == "" {
return action.ActionResult{}, fmt.Errorf("counter name cannot be empty")
}
a.mutex.Lock()
defer a.mutex.Unlock()
// Get current value or initialize if it doesn't exist
currentValue, exists := a.counters[request.Name]
// Update the counter
newValue := currentValue + request.Adjustment
a.counters[request.Name] = newValue
// Prepare the response message
var message string
if !exists && request.Adjustment == 0 {
message = fmt.Sprintf("Created counter '%s' with initial value 0", request.Name)
} else if !exists {
message = fmt.Sprintf("Created counter '%s' with initial value %d", request.Name, newValue)
} else if request.Adjustment > 0 {
message = fmt.Sprintf("Increased counter '%s' by %d to %d", request.Name, request.Adjustment, newValue)
} else if request.Adjustment < 0 {
message = fmt.Sprintf("Decreased counter '%s' by %d to %d", request.Name, -request.Adjustment, newValue)
} else {
message = fmt.Sprintf("Current value of counter '%s' is %d", request.Name, newValue)
}
return action.ActionResult{
Result: message,
Metadata: map[string]any{
"counter_name": request.Name,
"counter_value": newValue,
"adjustment": request.Adjustment,
"is_new": !exists,
},
}, nil
}
// Definition returns the action definition
func (a *CounterAction) Definition() action.ActionDefinition {
return action.ActionDefinition{
Name: "counter",
Description: "Create, update, or query named counters. Specify a name and an adjustment value (positive to increase, negative to decrease, zero to query).",
Properties: map[string]jsonschema.Definition{
"name": {
Type: jsonschema.String,
Description: "The name of the counter to create, update, or query.",
},
"adjustment": {
Type: jsonschema.Integer,
Description: "The value to adjust the counter by. Positive to increase, negative to decrease, zero to query the current value.",
},
},
Required: []string{"name", "adjustment"},
}
}

View File

@@ -101,7 +101,7 @@ func (i *IRC) Start(a *agent.Agent) {
}
xlog.Info("Recv message", "message", message, "sender", sender, "channel", channel)
cleanedMessage := "I am " + sender + ". " + cleanUpMessage(message, i.nickname)
cleanedMessage := "My name is " + sender + ". " + cleanUpMessage(message, i.nickname)
go func() {
res := a.Ask(