add start/pause (to improve further)

This commit is contained in:
Ettore Di Giacinto
2024-04-13 00:38:58 +02:00
parent 74b158e9f1
commit 54de6221d1
4 changed files with 77 additions and 0 deletions

View File

@@ -32,6 +32,7 @@ type Agent struct {
nextAction Action
currentConversation Messages
selfEvaluationInProgress bool
pause bool
newConversations chan openai.ChatCompletionMessage
}
@@ -137,6 +138,24 @@ func (a *Agent) Stop() {
a.context.Cancel()
}
func (a *Agent) Pause() {
a.Lock()
defer a.Unlock()
a.pause = true
}
func (a *Agent) Resume() {
a.Lock()
defer a.Unlock()
a.pause = false
}
func (a *Agent) Paused() bool {
a.Lock()
defer a.Unlock()
return a.pause
}
func (a *Agent) runAction(chosenAction Action, decisionResult *decisionResult) (result string, err error) {
for _, action := range a.systemInternalActions() {
if action.Definition().Name == chosenAction.Definition().Name {
@@ -174,6 +193,18 @@ func (a *Agent) runAction(chosenAction Action, decisionResult *decisionResult) (
}
func (a *Agent) consumeJob(job *Job, role string) {
a.Lock()
paused := a.pause
a.Unlock()
if paused {
if a.options.debugMode {
fmt.Println("Agent is paused, skipping job")
}
job.Result.Finish(fmt.Errorf("agent is paused"))
return
}
// We are self evaluating if we consume the job as a system role
selfEvaluation := role == SystemRole

View File

@@ -137,6 +137,27 @@ func (a *App) Delete(pool *AgentPool) func(c *fiber.Ctx) error {
}
}
func (a *App) Pause(pool *AgentPool) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
fmt.Println("Pausing agent", c.Params("name"))
agent := pool.GetAgent(c.Params("name"))
if agent != nil {
agent.Pause()
}
return c.Redirect("/agents")
}
}
func (a *App) Start(pool *AgentPool) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
agent := pool.GetAgent(c.Params("name"))
if agent != nil {
agent.Resume()
}
return c.Redirect("/agents")
}
}
func (a *App) Create(pool *AgentPool) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
config := AgentConfig{}

View File

@@ -24,8 +24,13 @@ func RegisterRoutes(webapp *fiber.App, pool *AgentPool, db *InMemoryDatabase, ap
})
webapp.Get("/agents", func(c *fiber.Ctx) error {
statuses := map[string]bool{}
for _, a := range pool.List() {
statuses[a] = !pool.GetAgent(a).Paused()
}
return c.Render("views/agents", fiber.Map{
"Agents": pool.List(),
"Status": statuses,
})
})
@@ -72,6 +77,8 @@ func RegisterRoutes(webapp *fiber.App, pool *AgentPool, db *InMemoryDatabase, ap
webapp.Post("/chat/:name", app.Chat(pool))
webapp.Post("/create", app.Create(pool))
webapp.Get("/delete/:name", app.Delete(pool))
webapp.Put("/pause/:name", app.Pause(pool))
webapp.Put("/start/:name", app.Start(pool))
webapp.Post("/knowledgebase", app.KnowledgeBase(db))
webapp.Post("/knowledgebase/upload", app.KnowledgeBaseFile(db))

View File

@@ -40,6 +40,12 @@
<th scope="col" class="px-6 py-3 text-center">
<span class="sr-only">Talk</span>
</th>
<th scope="col" class="px-6 py-3 text-center">
<span class="sr-only">Start</span>
</th>
<th scope="col" class="px-6 py-3 text-center">
<span class="sr-only">Stop</span>
</th>
<th scope="col" class="px-6 py-3 text-center">
<span class="sr-only">Delete</span>
</th>
@@ -47,10 +53,12 @@
</thead>
<tbody class="bg-gray-800 divide-y divide-gray-700">
<!-- Dynamic agent rows go here -->
{{ $status := .Status }}
{{ range .Agents }}
<tr hx-ext="sse">
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-300">{{.}}</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-300">
Online: {{ index $status . }}
<a href="/status/{{.}}" class="text-indigo-500 hover:text-indigo-400">
<i class="fas fa-info"></i> Status
</a>
@@ -60,6 +68,16 @@
<i class="fas fa-comments"></i> Talk
</a>
</td>
<td class="px-6 py-4 whitespace-nowrap text-center text-sm font-medium">
<button hx-put="/start/{{.}}" class="text-indigo-500 hover:text-indigo-400">
Start
</button>
</td>
<td class="px-6 py-4 whitespace-nowrap text-center text-sm font-medium">
<button hx-put="/pause/{{.}}" class="text-indigo-500 hover:text-indigo-400">
Pause
</button>
</td>
<td class="px-6 py-4 whitespace-nowrap text-center text-sm font-medium">
<a href="/delete/{{.}}" class="text-red-500 hover:text-red-400">
<i class="fas fa-trash-alt"></i> Delete