return metadatas and conversations in job results. Consume them in Slack connector to use attachments in responses

This commit is contained in:
Ettore Di Giacinto
2025-03-01 22:10:21 +01:00
parent 4d6b04c6ed
commit 3a7b5e1461
10 changed files with 106 additions and 48 deletions

View File

@@ -25,7 +25,7 @@ type StateAction struct{}
// TODO: A special action is then used to let the LLM itself update its memory
// periodically during self-processing, and the same action is ALSO exposed
// during the conversation to let the user put for example, a new goal to the agent.
type StateResult struct {
type AgentInternalState struct {
NowDoing string `json:"doing_now"`
DoingNext string `json:"doing_next"`
DoneHistory []string `json:"done_history"`
@@ -81,7 +81,7 @@ You have a short memory with: %+v
=====================
`
func (c StateResult) String() string {
func (c AgentInternalState) String() string {
return fmt.Sprintf(
fmtT,
c.NowDoing,

View File

@@ -12,7 +12,7 @@ import (
type ActionState struct {
ActionCurrentState
Result string
action.ActionResult
}
type ActionCurrentState struct {

View File

@@ -31,7 +31,7 @@ type Agent struct {
context *action.ActionContext
currentReasoning string
currentState *action.StateResult
currentState *action.AgentInternalState
nextAction Action
nextActionParams *action.ActionParams
currentConversation Messages
@@ -67,7 +67,7 @@ func New(opts ...Option) (*Agent, error) {
options: options,
client: client,
Character: options.character,
currentState: &action.StateResult{},
currentState: &action.AgentInternalState{},
context: action.NewContext(ctx, cancel),
}
@@ -239,15 +239,15 @@ func (a *Agent) Memory() RAGDB {
return a.options.ragdb
}
func (a *Agent) runAction(chosenAction Action, params action.ActionParams) (result string, err error) {
for _, action := range a.systemInternalActions() {
if action.Definition().Name == chosenAction.Definition().Name {
res, err := action.Run(a.actionContext, params)
func (a *Agent) runAction(chosenAction Action, params action.ActionParams) (result action.ActionResult, err error) {
for _, act := range a.systemInternalActions() {
if act.Definition().Name == chosenAction.Definition().Name {
res, err := act.Run(a.actionContext, params)
if err != nil {
return "", fmt.Errorf("error running action: %w", err)
return action.ActionResult{}, fmt.Errorf("error running action: %w", err)
}
result = res.Result
result = res
}
}
@@ -255,11 +255,11 @@ func (a *Agent) runAction(chosenAction Action, params action.ActionParams) (resu
if chosenAction.Definition().Name.Is(action.StateActionName) {
// We need to store the result in the state
state := action.StateResult{}
state := action.AgentInternalState{}
err = params.Unmarshal(&state)
if err != nil {
return "", fmt.Errorf("error unmarshalling state of the agent: %w", err)
return action.ActionResult{}, fmt.Errorf("error unmarshalling state of the agent: %w", err)
}
// update the current state with the one we just got from the action
a.currentState = &state
@@ -267,7 +267,7 @@ func (a *Agent) runAction(chosenAction Action, params action.ActionParams) (resu
// update the state file
if a.options.statefile != "" {
if err := a.SaveState(a.options.statefile); err != nil {
return "", err
return action.ActionResult{}, err
}
}
}
@@ -445,6 +445,7 @@ func (a *Agent) consumeJob(job *Job, role string) {
Role: "assistant",
Content: reasoning,
})
job.Result.Conversation = a.currentConversation
job.Result.SetResponse(reasoning)
job.Result.Finish(nil)
return
@@ -486,7 +487,8 @@ func (a *Agent) consumeJob(job *Job, role string) {
}
if !job.Callback(ActionCurrentState{chosenAction, actionParams, reasoning}) {
job.Result.SetResult(ActionState{ActionCurrentState{chosenAction, actionParams, reasoning}, "stopped by callback"})
job.Result.SetResult(ActionState{ActionCurrentState{chosenAction, actionParams, reasoning}, action.ActionResult{Result: "stopped by callback"}})
job.Result.Conversation = a.currentConversation
job.Result.Finish(nil)
return
}
@@ -512,6 +514,7 @@ func (a *Agent) consumeJob(job *Job, role string) {
Content: message.Message,
}
}()
job.Result.Conversation = a.currentConversation
job.Result.SetResponse("decided to initiate a new conversation")
job.Result.Finish(nil)
return
@@ -524,7 +527,7 @@ func (a *Agent) consumeJob(job *Job, role string) {
//job.Result.Finish(fmt.Errorf("error running action: %w", err))
//return
// make the LLM aware of the error of running the action instead of stopping the job here
result = fmt.Sprintf("Error running tool: %v", err)
result.Result = fmt.Sprintf("Error running tool: %v", err)
}
stateResult := ActionState{ActionCurrentState{chosenAction, actionParams, reasoning}, result}
@@ -549,7 +552,7 @@ func (a *Agent) consumeJob(job *Job, role string) {
// result of calling the function
a.currentConversation = append(a.currentConversation, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleTool,
Content: result,
Content: result.Result,
Name: chosenAction.Definition().Name.String(),
ToolCallID: chosenAction.Definition().Name.String(),
})
@@ -561,6 +564,7 @@ func (a *Agent) consumeJob(job *Job, role string) {
// to continue using another tool given the result
followingAction, followingParams, reasoning, err := a.pickAction(ctx, reEvaluationTemplate, a.currentConversation)
if err != nil {
job.Result.Conversation = a.currentConversation
job.Result.Finish(fmt.Errorf("error picking action: %w", err))
return
}
@@ -592,12 +596,15 @@ func (a *Agent) consumeJob(job *Job, role string) {
a.currentConversation = append(a.currentConversation, msg)
job.Result.SetResponse(msg.Content)
job.Result.Conversation = a.currentConversation
job.Result.Finish(nil)
return
}
}
}
job.Result.Conversation = a.currentConversation
// At this point can only be a reply action
xlog.Info("Computing reply", "agent", a.Character.Name)
@@ -605,6 +612,7 @@ func (a *Agent) consumeJob(job *Job, role string) {
replyResponse := action.ReplyResponse{}
if err := actionParams.Unmarshal(&replyResponse); err != nil {
job.Result.Conversation = a.currentConversation
job.Result.Finish(fmt.Errorf("error unmarshalling reply response: %w", err))
return
}
@@ -628,6 +636,7 @@ func (a *Agent) consumeJob(job *Job, role string) {
if a.options.enableHUD {
prompt, err := renderTemplate(hudTemplate, a.prepareHUD(), a.systemInternalActions(), reasoning)
if err != nil {
job.Result.Conversation = a.currentConversation
job.Result.Finish(fmt.Errorf("error renderTemplate: %w", err))
return
}
@@ -663,6 +672,7 @@ func (a *Agent) consumeJob(job *Job, role string) {
}
a.currentConversation = append(a.currentConversation, msg)
job.Result.Conversation = a.currentConversation
job.Result.SetResponse(msg.Content)
job.Result.Finish(nil)
return
@@ -672,6 +682,7 @@ func (a *Agent) consumeJob(job *Job, role string) {
xlog.Debug("Conversation", "conversation", fmt.Sprintf("%+v", a.currentConversation))
msg, err := a.askLLM(ctx, a.currentConversation)
if err != nil {
job.Result.Conversation = a.currentConversation
job.Result.Finish(err)
return
}
@@ -690,7 +701,7 @@ func (a *Agent) consumeJob(job *Job, role string) {
a.currentConversation = append(a.currentConversation, msg)
job.Result.SetResponse(msg.Content)
xlog.Info("Response from LLM", "response", msg.Content, "agent", a.Character.Name)
job.Result.Conversation = a.currentConversation
job.Result.Finish(nil)
}

View File

@@ -23,10 +23,11 @@ type Job struct {
type JobResult struct {
sync.Mutex
// The result of a job
State []ActionState
Response string
Error error
ready chan bool
State []ActionState
Conversation []openai.ChatCompletionMessage
Response string
Error error
ready chan bool
}
type JobOption func(*Job)

View File

@@ -14,10 +14,10 @@ import (
// all information that should be displayed to the LLM
// in the prompts
type PromptHUD struct {
Character Character `json:"character"`
CurrentState action.StateResult `json:"current_state"`
PermanentGoal string `json:"permanent_goal"`
ShowCharacter bool `json:"show_character"`
Character Character `json:"character"`
CurrentState action.AgentInternalState `json:"current_state"`
PermanentGoal string `json:"permanent_goal"`
ShowCharacter bool `json:"show_character"`
}
type Character struct {
@@ -42,7 +42,7 @@ func Load(path string) (*Character, error) {
return &c, nil
}
func (a *Agent) State() action.StateResult {
func (a *Agent) State() action.AgentInternalState {
return *a.currentState
}