Compare commits
17 Commits
obs-fixes
...
chore/bett
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e0a57374c2 | ||
|
|
6516af6c34 | ||
|
|
77680c6fee | ||
|
|
5faa599321 | ||
|
|
6209ededff | ||
|
|
f6b6d5246c | ||
|
|
b81624bfc2 | ||
|
|
c1844f7230 | ||
|
|
15efd2d527 | ||
|
|
5e3bc0f89b | ||
|
|
12209ab926 | ||
|
|
547e9cd0c4 | ||
|
|
6a1e536ca7 | ||
|
|
eb8663ada1 | ||
|
|
ce997d2425 | ||
|
|
56cd0e05ca | ||
|
|
25bb3fb123 |
74
.github/workflows/image.yml
vendored
74
.github/workflows/image.yml
vendored
@@ -84,3 +84,77 @@ jobs:
|
|||||||
#tags: ${{ steps.prep.outputs.tags }}
|
#tags: ${{ steps.prep.outputs.tags }}
|
||||||
tags: ${{ steps.meta.outputs.tags }}
|
tags: ${{ steps.meta.outputs.tags }}
|
||||||
labels: ${{ steps.meta.outputs.labels }}
|
labels: ${{ steps.meta.outputs.labels }}
|
||||||
|
mcpbox-build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Prepare
|
||||||
|
id: prep
|
||||||
|
run: |
|
||||||
|
DOCKER_IMAGE=quay.io/mudler/localagi-mcpbox
|
||||||
|
# Use branch name as default
|
||||||
|
VERSION=${GITHUB_REF#refs/heads/}
|
||||||
|
BINARY_VERSION=$(git describe --always --tags --dirty)
|
||||||
|
SHORTREF=${GITHUB_SHA::8}
|
||||||
|
# If this is git tag, use the tag name as a docker tag
|
||||||
|
if [[ $GITHUB_REF == refs/tags/* ]]; then
|
||||||
|
VERSION=${GITHUB_REF#refs/tags/}
|
||||||
|
fi
|
||||||
|
TAGS="${DOCKER_IMAGE}:${VERSION},${DOCKER_IMAGE}:${SHORTREF}"
|
||||||
|
# If the VERSION looks like a version number, assume that
|
||||||
|
# this is the most recent version of the image and also
|
||||||
|
# tag it 'latest'.
|
||||||
|
if [[ $VERSION =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
|
||||||
|
TAGS="$TAGS,${DOCKER_IMAGE}:latest"
|
||||||
|
fi
|
||||||
|
# Set output parameters.
|
||||||
|
echo ::set-output name=binary_version::${BINARY_VERSION}
|
||||||
|
echo ::set-output name=tags::${TAGS}
|
||||||
|
echo ::set-output name=docker_image::${DOCKER_IMAGE}
|
||||||
|
- name: Set up QEMU
|
||||||
|
uses: docker/setup-qemu-action@master
|
||||||
|
with:
|
||||||
|
platforms: all
|
||||||
|
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
id: buildx
|
||||||
|
uses: docker/setup-buildx-action@master
|
||||||
|
|
||||||
|
- name: Login to DockerHub
|
||||||
|
if: github.event_name != 'pull_request'
|
||||||
|
uses: docker/login-action@v3
|
||||||
|
with:
|
||||||
|
registry: quay.io
|
||||||
|
username: ${{ secrets.DOCKER_USERNAME }}
|
||||||
|
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||||
|
- name: Extract metadata (tags, labels) for Docker
|
||||||
|
id: meta
|
||||||
|
uses: docker/metadata-action@902fa8ec7d6ecbf8d84d538b9b233a880e428804
|
||||||
|
with:
|
||||||
|
images: quay.io/mudler/localagi-mcpbox
|
||||||
|
tags: |
|
||||||
|
type=ref,event=branch,suffix=-{{date 'YYYYMMDDHHmmss'}}
|
||||||
|
type=semver,pattern={{raw}}
|
||||||
|
type=sha,suffix=-{{date 'YYYYMMDDHHmmss'}}
|
||||||
|
type=ref,event=branch
|
||||||
|
flavor: |
|
||||||
|
latest=auto
|
||||||
|
prefix=
|
||||||
|
suffix=
|
||||||
|
|
||||||
|
- name: Build
|
||||||
|
uses: docker/build-push-action@v6
|
||||||
|
with:
|
||||||
|
builder: ${{ steps.buildx.outputs.name }}
|
||||||
|
build-args: |
|
||||||
|
VERSION=${{ steps.prep.outputs.binary_version }}
|
||||||
|
context: ./
|
||||||
|
file: ./Dockerfile.mcpbox
|
||||||
|
#platforms: linux/amd64,linux/arm64
|
||||||
|
platforms: linux/amd64
|
||||||
|
push: true
|
||||||
|
#tags: ${{ steps.prep.outputs.tags }}
|
||||||
|
tags: ${{ steps.meta.outputs.tags }}
|
||||||
|
labels: ${{ steps.meta.outputs.labels }}
|
||||||
2
.github/workflows/tests.yml
vendored
2
.github/workflows/tests.yml
vendored
@@ -15,7 +15,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v4
|
||||||
- run: |
|
- run: |
|
||||||
# Add Docker's official GPG key:
|
# Add Docker's official GPG key:
|
||||||
sudo apt-get update
|
sudo apt-get update
|
||||||
|
|||||||
49
Dockerfile.mcpbox
Normal file
49
Dockerfile.mcpbox
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
# Build stage
|
||||||
|
FROM golang:1.24-alpine AS builder
|
||||||
|
|
||||||
|
# Install build dependencies
|
||||||
|
RUN apk add --no-cache git
|
||||||
|
|
||||||
|
# Set working directory
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Copy go mod files
|
||||||
|
COPY go.mod go.sum ./
|
||||||
|
|
||||||
|
# Download dependencies
|
||||||
|
RUN go mod download
|
||||||
|
|
||||||
|
# Copy source code
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
# Build the application
|
||||||
|
RUN CGO_ENABLED=0 GOOS=linux go build -o mcpbox ./cmd/mcpbox
|
||||||
|
|
||||||
|
# Final stage
|
||||||
|
FROM ubuntu:22.04
|
||||||
|
|
||||||
|
ENV DEBIAN_FRONTEND=noninteractive
|
||||||
|
|
||||||
|
# Install runtime dependencies
|
||||||
|
RUN apt-get update && apt-get install -y ca-certificates tzdata docker.io bash wget curl
|
||||||
|
|
||||||
|
# Create non-root user
|
||||||
|
#RUN adduser -D -g '' appuser
|
||||||
|
|
||||||
|
# Set working directory
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Copy binary from builder
|
||||||
|
COPY --from=builder /app/mcpbox .
|
||||||
|
|
||||||
|
# Use non-root user
|
||||||
|
#USER appuser
|
||||||
|
|
||||||
|
# Expose port
|
||||||
|
EXPOSE 8080
|
||||||
|
|
||||||
|
# Set entrypoint
|
||||||
|
ENTRYPOINT ["/app/mcpbox"]
|
||||||
|
|
||||||
|
# Default command
|
||||||
|
CMD ["-addr", ":8080"]
|
||||||
14
Makefile
14
Makefile
@@ -1,15 +1,17 @@
|
|||||||
GOCMD?=go
|
GOCMD?=go
|
||||||
IMAGE_NAME?=webui
|
IMAGE_NAME?=webui
|
||||||
|
MCPBOX_IMAGE_NAME?=mcpbox
|
||||||
ROOT_DIR:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
|
ROOT_DIR:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
|
||||||
|
|
||||||
prepare-tests:
|
prepare-tests: build-mcpbox
|
||||||
docker compose up -d --build
|
docker compose up -d --build
|
||||||
|
docker run -d -v /var/run/docker.sock:/var/run/docker.sock --privileged -p 9090:8080 --rm -ti $(MCPBOX_IMAGE_NAME)
|
||||||
|
|
||||||
cleanup-tests:
|
cleanup-tests:
|
||||||
docker compose down
|
docker compose down
|
||||||
|
|
||||||
tests: prepare-tests
|
tests: prepare-tests
|
||||||
LOCALAGI_MODEL="gemma-3-12b-it-qat" LOCALAI_API_URL="http://localhost:8081" LOCALAGI_API_URL="http://localhost:8080" $(GOCMD) run github.com/onsi/ginkgo/v2/ginkgo --fail-fast -v -r ./...
|
LOCALAGI_MCPBOX_URL="http://localhost:9090" LOCALAGI_MODEL="gemma-3-12b-it-qat" LOCALAI_API_URL="http://localhost:8081" LOCALAGI_API_URL="http://localhost:8080" $(GOCMD) run github.com/onsi/ginkgo/v2/ginkgo --fail-fast -v -r ./...
|
||||||
|
|
||||||
run-nokb:
|
run-nokb:
|
||||||
$(MAKE) run KBDISABLEINDEX=true
|
$(MAKE) run KBDISABLEINDEX=true
|
||||||
@@ -23,10 +25,16 @@ build: webui/react-ui/dist
|
|||||||
|
|
||||||
.PHONY: run
|
.PHONY: run
|
||||||
run: webui/react-ui/dist
|
run: webui/react-ui/dist
|
||||||
$(GOCMD) run ./
|
LOCALAGI_MCPBOX_URL="http://localhost:9090" $(GOCMD) run ./
|
||||||
|
|
||||||
build-image:
|
build-image:
|
||||||
docker build -t $(IMAGE_NAME) -f Dockerfile.webui .
|
docker build -t $(IMAGE_NAME) -f Dockerfile.webui .
|
||||||
|
|
||||||
image-push:
|
image-push:
|
||||||
docker push $(IMAGE_NAME)
|
docker push $(IMAGE_NAME)
|
||||||
|
|
||||||
|
build-mcpbox:
|
||||||
|
docker build -t $(MCPBOX_IMAGE_NAME) -f Dockerfile.mcpbox .
|
||||||
|
|
||||||
|
run-mcpbox:
|
||||||
|
docker run -v /var/run/docker.sock:/var/run/docker.sock --privileged -p 9090:8080 -ti mcpbox
|
||||||
22
README.md
22
README.md
@@ -2,7 +2,7 @@
|
|||||||
<img src="./webui/react-ui/public/logo_1.png" alt="LocalAGI Logo" width="220"/>
|
<img src="./webui/react-ui/public/logo_1.png" alt="LocalAGI Logo" width="220"/>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<h3 align="center"><em>Your AI. Your Hardware. Your Rules.</em></h3>
|
<h3 align="center"><em>Your AI. Your Hardware. Your Rules</em></h3>
|
||||||
|
|
||||||
<div align="center">
|
<div align="center">
|
||||||
|
|
||||||
@@ -13,9 +13,9 @@
|
|||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
We empower you building AI Agents that you can run locally, without coding.
|
Create customizable AI assistants, automations, chat bots and agents that run 100% locally. No need for agentic Python libraries or cloud service keys, just bring your GPU (or even just CPU) and a web browser.
|
||||||
|
|
||||||
**LocalAGI** is a powerful, self-hostable AI Agent platform designed for maximum privacy and flexibility. A complete drop-in replacement for OpenAI's Responses APIs with advanced agentic capabilities. No clouds. No data leaks. Just pure local AI that works on consumer-grade hardware (CPU and GPU).
|
**LocalAGI** is a powerful, self-hostable AI Agent platform that allows you to design AI automations without writing code. A complete drop-in replacement for OpenAI's Responses APIs with advanced agentic capabilities. No clouds. No data leaks. Just pure local AI that works on consumer-grade hardware (CPU and GPU).
|
||||||
|
|
||||||
## 🛡️ Take Back Your Privacy
|
## 🛡️ Take Back Your Privacy
|
||||||
|
|
||||||
@@ -37,6 +37,7 @@ LocalAGI ensures your data stays exactly where you want it—on your hardware. N
|
|||||||
- 🖼 **Multimodal Support**: Ready for vision, text, and more.
|
- 🖼 **Multimodal Support**: Ready for vision, text, and more.
|
||||||
- 🔧 **Extensible Custom Actions**: Easily script dynamic agent behaviors in Go (interpreted, no compilation!).
|
- 🔧 **Extensible Custom Actions**: Easily script dynamic agent behaviors in Go (interpreted, no compilation!).
|
||||||
- 🛠 **Fully Customizable Models**: Use your own models or integrate seamlessly with [LocalAI](https://github.com/mudler/LocalAI).
|
- 🛠 **Fully Customizable Models**: Use your own models or integrate seamlessly with [LocalAI](https://github.com/mudler/LocalAI).
|
||||||
|
- 📊 **Observability**: Monitor agent status and view detailed observable updates in real-time.
|
||||||
|
|
||||||
## 🛠️ Quickstart
|
## 🛠️ Quickstart
|
||||||
|
|
||||||
@@ -68,6 +69,11 @@ Now you can access and manage your agents at [http://localhost:8080](http://loca
|
|||||||
|
|
||||||
Still having issues? see this Youtube video: https://youtu.be/HtVwIxW3ePg
|
Still having issues? see this Youtube video: https://youtu.be/HtVwIxW3ePg
|
||||||
|
|
||||||
|
## Videos
|
||||||
|
|
||||||
|
[](https://youtu.be/HtVwIxW3ePg)
|
||||||
|
[](https://youtu.be/v82rswGJt_M)
|
||||||
|
|
||||||
## 📚🆕 Local Stack Family
|
## 📚🆕 Local Stack Family
|
||||||
|
|
||||||
🆕 LocalAI is now part of a comprehensive suite of AI tools designed to work together:
|
🆕 LocalAI is now part of a comprehensive suite of AI tools designed to work together:
|
||||||
@@ -179,14 +185,6 @@ Good (relatively small) models that have been tested are:
|
|||||||
- **✓ Effortless Setup**: Simple Docker compose setups and pre-built binaries.
|
- **✓ Effortless Setup**: Simple Docker compose setups and pre-built binaries.
|
||||||
- **✓ Feature-Rich**: From planning to multimodal capabilities, connectors for Slack, MCP support, LocalAGI has it all.
|
- **✓ Feature-Rich**: From planning to multimodal capabilities, connectors for Slack, MCP support, LocalAGI has it all.
|
||||||
|
|
||||||
## 🌐 The Local Ecosystem
|
|
||||||
|
|
||||||
LocalAGI is part of the powerful Local family of privacy-focused AI tools:
|
|
||||||
|
|
||||||
- [**LocalAI**](https://github.com/mudler/LocalAI): Run Large Language Models locally.
|
|
||||||
- [**LocalRecall**](https://github.com/mudler/LocalRecall): Retrieval-Augmented Generation with local storage.
|
|
||||||
- [**LocalAGI**](https://github.com/mudler/LocalAGI): Deploy intelligent AI agents securely and privately.
|
|
||||||
|
|
||||||
## 🌟 Screenshots
|
## 🌟 Screenshots
|
||||||
|
|
||||||
### Powerful Web UI
|
### Powerful Web UI
|
||||||
@@ -194,6 +192,8 @@ LocalAGI is part of the powerful Local family of privacy-focused AI tools:
|
|||||||

|

|
||||||

|

|
||||||

|

|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
### Connectors Ready-to-Go
|
### Connectors Ready-to-Go
|
||||||
|
|
||||||
|
|||||||
38
cmd/mcpbox/main.go
Normal file
38
cmd/mcpbox/main.go
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
|
"github.com/mudler/LocalAGI/pkg/stdio"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// Parse command line flags
|
||||||
|
addr := flag.String("addr", ":8080", "HTTP server address")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
// Create and start the server
|
||||||
|
server := stdio.NewServer()
|
||||||
|
|
||||||
|
// Handle graceful shutdown
|
||||||
|
sigChan := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
log.Printf("Starting server on %s", *addr)
|
||||||
|
if err := server.Start(*addr); err != nil {
|
||||||
|
log.Fatalf("Failed to start server: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Wait for shutdown signal
|
||||||
|
<-sigChan
|
||||||
|
log.Println("Shutting down server...")
|
||||||
|
|
||||||
|
// TODO: Implement graceful shutdown if needed
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
@@ -95,6 +95,11 @@ func (a *CustomAction) Run(ctx context.Context, params types.ActionParams) (type
|
|||||||
|
|
||||||
func (a *CustomAction) Definition() types.ActionDefinition {
|
func (a *CustomAction) Definition() types.ActionDefinition {
|
||||||
|
|
||||||
|
if a.i == nil {
|
||||||
|
xlog.Error("Interpreter is not initialized for custom action", "action", a.config["name"])
|
||||||
|
return types.ActionDefinition{}
|
||||||
|
}
|
||||||
|
|
||||||
v, err := a.i.Eval(fmt.Sprintf("%s.Definition", a.config["name"]))
|
v, err := a.i.Eval(fmt.Sprintf("%s.Definition", a.config["name"]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
xlog.Error("Error getting custom action definition", "error", err)
|
xlog.Error("Error getting custom action definition", "error", err)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package agent
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -179,6 +180,12 @@ func (a *Agent) Execute(j *types.Job) *types.JobResult {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
if j.Obs != nil {
|
if j.Obs != nil {
|
||||||
|
if len(j.ConversationHistory) > 0 {
|
||||||
|
m := j.ConversationHistory[len(j.ConversationHistory)-1]
|
||||||
|
j.Obs.Creation = &types.Creation{ ChatCompletionMessage: &m }
|
||||||
|
a.observer.Update(*j.Obs)
|
||||||
|
}
|
||||||
|
|
||||||
j.Result.AddFinalizer(func(ccm []openai.ChatCompletionMessage) {
|
j.Result.AddFinalizer(func(ccm []openai.ChatCompletionMessage) {
|
||||||
j.Obs.Completion = &types.Completion{
|
j.Obs.Completion = &types.Completion{
|
||||||
Conversation: ccm,
|
Conversation: ccm,
|
||||||
@@ -240,6 +247,7 @@ func (a *Agent) Stop() {
|
|||||||
a.Lock()
|
a.Lock()
|
||||||
defer a.Unlock()
|
defer a.Unlock()
|
||||||
xlog.Debug("Stopping agent", "agent", a.Character.Name)
|
xlog.Debug("Stopping agent", "agent", a.Character.Name)
|
||||||
|
a.closeMCPSTDIOServers()
|
||||||
a.context.Cancel()
|
a.context.Cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -989,7 +997,6 @@ func (a *Agent) periodicallyRun(timer *time.Timer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *Agent) Run() error {
|
func (a *Agent) Run() error {
|
||||||
|
|
||||||
a.startNewConversationsConsumer()
|
a.startNewConversationsConsumer()
|
||||||
xlog.Debug("Agent is now running", "agent", a.Character.Name)
|
xlog.Debug("Agent is now running", "agent", a.Character.Name)
|
||||||
// The agent run does two things:
|
// The agent run does two things:
|
||||||
@@ -1004,36 +1011,68 @@ func (a *Agent) Run() error {
|
|||||||
|
|
||||||
// Expose a REST API to interact with the agent to ask it things
|
// Expose a REST API to interact with the agent to ask it things
|
||||||
|
|
||||||
//todoTimer := time.NewTicker(a.options.periodicRuns)
|
|
||||||
timer := time.NewTimer(a.options.periodicRuns)
|
timer := time.NewTimer(a.options.periodicRuns)
|
||||||
|
|
||||||
|
// we fire the periodicalRunner only once.
|
||||||
|
go a.periodicalRunRunner(timer)
|
||||||
|
var errs []error
|
||||||
|
var muErr sync.Mutex
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
|
parallelJobs := a.options.parallelJobs
|
||||||
|
if a.options.parallelJobs == 0 {
|
||||||
|
parallelJobs = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < parallelJobs; i++ {
|
||||||
|
xlog.Debug("Starting agent worker", "worker", i)
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
e := a.run(timer)
|
||||||
|
muErr.Lock()
|
||||||
|
errs = append(errs, e)
|
||||||
|
muErr.Unlock()
|
||||||
|
wg.Done()
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
return errors.Join(errs...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *Agent) run(timer *time.Timer) error {
|
||||||
for {
|
for {
|
||||||
xlog.Debug("Agent is now waiting for a new job", "agent", a.Character.Name)
|
xlog.Debug("Agent is now waiting for a new job", "agent", a.Character.Name)
|
||||||
select {
|
select {
|
||||||
case job := <-a.jobQueue:
|
case job := <-a.jobQueue:
|
||||||
a.loop(timer, job)
|
if !timer.Stop() {
|
||||||
|
<-timer.C
|
||||||
|
}
|
||||||
|
xlog.Debug("Agent is consuming a job", "agent", a.Character.Name, "job", job)
|
||||||
|
a.consumeJob(job, UserRole)
|
||||||
|
timer.Reset(a.options.periodicRuns)
|
||||||
case <-a.context.Done():
|
case <-a.context.Done():
|
||||||
// Agent has been canceled, return error
|
// Agent has been canceled, return error
|
||||||
xlog.Warn("Agent has been canceled", "agent", a.Character.Name)
|
xlog.Warn("Agent has been canceled", "agent", a.Character.Name)
|
||||||
return ErrContextCanceled
|
return ErrContextCanceled
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *Agent) periodicalRunRunner(timer *time.Timer) {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-a.context.Done():
|
||||||
|
// Agent has been canceled, return error
|
||||||
|
xlog.Warn("periodicalRunner has been canceled", "agent", a.Character.Name)
|
||||||
|
return
|
||||||
case <-timer.C:
|
case <-timer.C:
|
||||||
a.periodicallyRun(timer)
|
a.periodicallyRun(timer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Agent) loop(timer *time.Timer, job *types.Job) {
|
|
||||||
// Remember always to reset the timer - if we don't the agent will stop..
|
|
||||||
defer timer.Reset(a.options.periodicRuns)
|
|
||||||
// Consume the job and generate a response
|
|
||||||
// TODO: Give a short-term memory to the agent
|
|
||||||
// stop and drain the timer
|
|
||||||
if !timer.Stop() {
|
|
||||||
<-timer.C
|
|
||||||
}
|
|
||||||
xlog.Debug("Agent is consuming a job", "agent", a.Character.Name, "job", job)
|
|
||||||
a.consumeJob(job, UserRole)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *Agent) Observer() Observer {
|
func (a *Agent) Observer() Observer {
|
||||||
return a.observer
|
return a.observer
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,12 +3,14 @@ package agent
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
|
|
||||||
mcp "github.com/metoro-io/mcp-golang"
|
mcp "github.com/metoro-io/mcp-golang"
|
||||||
"github.com/metoro-io/mcp-golang/transport/http"
|
"github.com/metoro-io/mcp-golang/transport/http"
|
||||||
|
stdioTransport "github.com/metoro-io/mcp-golang/transport/stdio"
|
||||||
"github.com/mudler/LocalAGI/core/types"
|
"github.com/mudler/LocalAGI/core/types"
|
||||||
|
"github.com/mudler/LocalAGI/pkg/stdio"
|
||||||
"github.com/mudler/LocalAGI/pkg/xlog"
|
"github.com/mudler/LocalAGI/pkg/xlog"
|
||||||
|
|
||||||
"github.com/sashabaranov/go-openai/jsonschema"
|
"github.com/sashabaranov/go-openai/jsonschema"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -19,6 +21,12 @@ type MCPServer struct {
|
|||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type MCPSTDIOServer struct {
|
||||||
|
Args []string `json:"args"`
|
||||||
|
Env []string `json:"env"`
|
||||||
|
Cmd string `json:"cmd"`
|
||||||
|
}
|
||||||
|
|
||||||
type mcpAction struct {
|
type mcpAction struct {
|
||||||
mcpClient *mcp.Client
|
mcpClient *mcp.Client
|
||||||
inputSchema ToolInputSchema
|
inputSchema ToolInputSchema
|
||||||
@@ -79,34 +87,15 @@ type ToolInputSchema struct {
|
|||||||
Required []string `json:"required,omitempty"`
|
Required []string `json:"required,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Agent) initMCPActions() error {
|
func (a *Agent) addTools(client *mcp.Client) (types.Actions, error) {
|
||||||
|
|
||||||
a.mcpActions = nil
|
var generatedActions types.Actions
|
||||||
var err error
|
xlog.Debug("Initializing client")
|
||||||
|
|
||||||
generatedActions := types.Actions{}
|
|
||||||
|
|
||||||
for _, mcpServer := range a.options.mcpServers {
|
|
||||||
transport := http.NewHTTPClientTransport("/mcp")
|
|
||||||
transport.WithBaseURL(mcpServer.URL)
|
|
||||||
if mcpServer.Token != "" {
|
|
||||||
transport.WithHeader("Authorization", "Bearer "+mcpServer.Token)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a new client
|
|
||||||
client := mcp.NewClient(transport)
|
|
||||||
|
|
||||||
xlog.Debug("Initializing client", "server", mcpServer.URL)
|
|
||||||
// Initialize the client
|
// Initialize the client
|
||||||
response, e := client.Initialize(a.context)
|
response, e := client.Initialize(a.context)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
xlog.Error("Failed to initialize client", "error", e.Error(), "server", mcpServer)
|
xlog.Error("Failed to initialize client", "error", e.Error())
|
||||||
if err == nil {
|
return nil, e
|
||||||
err = e
|
|
||||||
} else {
|
|
||||||
err = errors.Join(err, e)
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
xlog.Debug("Client initialized: %v", response.Instructions)
|
xlog.Debug("Client initialized: %v", response.Instructions)
|
||||||
@@ -116,7 +105,7 @@ func (a *Agent) initMCPActions() error {
|
|||||||
tools, err := client.ListTools(a.context, cursor)
|
tools, err := client.ListTools(a.context, cursor)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
xlog.Error("Failed to list tools", "error", err.Error())
|
xlog.Error("Failed to list tools", "error", err.Error())
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, t := range tools.Tools {
|
for _, t := range tools.Tools {
|
||||||
@@ -125,14 +114,14 @@ func (a *Agent) initMCPActions() error {
|
|||||||
desc = *t.Description
|
desc = *t.Description
|
||||||
}
|
}
|
||||||
|
|
||||||
xlog.Debug("Tool", "mcpServer", mcpServer, "name", t.Name, "description", desc)
|
xlog.Debug("Tool", "name", t.Name, "description", desc)
|
||||||
|
|
||||||
dat, err := json.Marshal(t.InputSchema)
|
dat, err := json.Marshal(t.InputSchema)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
xlog.Error("Failed to marshal input schema", "error", err.Error())
|
xlog.Error("Failed to marshal input schema", "error", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
xlog.Debug("Input schema", "mcpServer", mcpServer, "tool", t.Name, "schema", string(dat))
|
xlog.Debug("Input schema", "tool", t.Name, "schema", string(dat))
|
||||||
|
|
||||||
// XXX: This is a wild guess, to verify (data types might be incompatible)
|
// XXX: This is a wild guess, to verify (data types might be incompatible)
|
||||||
var inputSchema ToolInputSchema
|
var inputSchema ToolInputSchema
|
||||||
@@ -156,9 +145,81 @@ func (a *Agent) initMCPActions() error {
|
|||||||
cursor = tools.NextCursor
|
cursor = tools.NextCursor
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return generatedActions, nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *Agent) initMCPActions() error {
|
||||||
|
|
||||||
|
a.mcpActions = nil
|
||||||
|
var err error
|
||||||
|
|
||||||
|
generatedActions := types.Actions{}
|
||||||
|
|
||||||
|
// MCP HTTP Servers
|
||||||
|
for _, mcpServer := range a.options.mcpServers {
|
||||||
|
transport := http.NewHTTPClientTransport("/mcp")
|
||||||
|
transport.WithBaseURL(mcpServer.URL)
|
||||||
|
if mcpServer.Token != "" {
|
||||||
|
transport.WithHeader("Authorization", "Bearer "+mcpServer.Token)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a new client
|
||||||
|
client := mcp.NewClient(transport)
|
||||||
|
xlog.Debug("Adding tools for MCP server", "server", mcpServer)
|
||||||
|
actions, err := a.addTools(client)
|
||||||
|
if err != nil {
|
||||||
|
xlog.Error("Failed to add tools for MCP server", "server", mcpServer, "error", err.Error())
|
||||||
|
}
|
||||||
|
generatedActions = append(generatedActions, actions...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MCP STDIO Servers
|
||||||
|
|
||||||
|
a.closeMCPSTDIOServers() // Make sure we stop all previous servers if any is active
|
||||||
|
|
||||||
|
if a.options.mcpPrepareScript != "" {
|
||||||
|
xlog.Debug("Preparing MCP box", "script", a.options.mcpPrepareScript)
|
||||||
|
client := stdio.NewClient(a.options.mcpBoxURL)
|
||||||
|
client.RunProcess(a.context, "/bin/bash", []string{"-c", a.options.mcpPrepareScript}, []string{})
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, mcpStdioServer := range a.options.mcpStdioServers {
|
||||||
|
client := stdio.NewClient(a.options.mcpBoxURL)
|
||||||
|
p, err := client.CreateProcess(a.context,
|
||||||
|
mcpStdioServer.Cmd,
|
||||||
|
mcpStdioServer.Args,
|
||||||
|
mcpStdioServer.Env,
|
||||||
|
a.Character.Name)
|
||||||
|
if err != nil {
|
||||||
|
xlog.Error("Failed to create process", "error", err.Error())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
read, writer, err := client.GetProcessIO(p.ID)
|
||||||
|
if err != nil {
|
||||||
|
xlog.Error("Failed to get process IO", "error", err.Error())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
transport := stdioTransport.NewStdioServerTransportWithIO(read, writer)
|
||||||
|
|
||||||
|
// Create a new client
|
||||||
|
mcpClient := mcp.NewClient(transport)
|
||||||
|
|
||||||
|
xlog.Debug("Adding tools for MCP server (stdio)", "server", mcpStdioServer)
|
||||||
|
actions, err := a.addTools(mcpClient)
|
||||||
|
if err != nil {
|
||||||
|
xlog.Error("Failed to add tools for MCP server", "server", mcpStdioServer, "error", err.Error())
|
||||||
|
}
|
||||||
|
generatedActions = append(generatedActions, actions...)
|
||||||
}
|
}
|
||||||
|
|
||||||
a.mcpActions = generatedActions
|
a.mcpActions = generatedActions
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *Agent) closeMCPSTDIOServers() {
|
||||||
|
client := stdio.NewClient(a.options.mcpBoxURL)
|
||||||
|
client.StopGroup(a.Character.Name)
|
||||||
|
}
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ func NewSSEObserver(agent string, manager sse.Manager) *SSEObserver {
|
|||||||
|
|
||||||
func (s *SSEObserver) NewObservable() *types.Observable {
|
func (s *SSEObserver) NewObservable() *types.Observable {
|
||||||
id := atomic.AddInt32(&s.maxID, 1)
|
id := atomic.AddInt32(&s.maxID, 1)
|
||||||
|
|
||||||
return &types.Observable{
|
return &types.Observable{
|
||||||
ID: id - 1,
|
ID: id - 1,
|
||||||
Agent: s.agent,
|
Agent: s.agent,
|
||||||
|
|||||||
@@ -51,10 +51,13 @@ type options struct {
|
|||||||
conversationsPath string
|
conversationsPath string
|
||||||
|
|
||||||
mcpServers []MCPServer
|
mcpServers []MCPServer
|
||||||
|
mcpStdioServers []MCPSTDIOServer
|
||||||
|
mcpBoxURL string
|
||||||
|
mcpPrepareScript string
|
||||||
newConversationsSubscribers []func(openai.ChatCompletionMessage)
|
newConversationsSubscribers []func(openai.ChatCompletionMessage)
|
||||||
|
|
||||||
observer Observer
|
observer Observer
|
||||||
|
parallelJobs int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *options) SeparatedMultimodalModel() bool {
|
func (o *options) SeparatedMultimodalModel() bool {
|
||||||
@@ -63,6 +66,7 @@ func (o *options) SeparatedMultimodalModel() bool {
|
|||||||
|
|
||||||
func defaultOptions() *options {
|
func defaultOptions() *options {
|
||||||
return &options{
|
return &options{
|
||||||
|
parallelJobs: 1,
|
||||||
periodicRuns: 15 * time.Minute,
|
periodicRuns: 15 * time.Minute,
|
||||||
LLMAPI: llmOptions{
|
LLMAPI: llmOptions{
|
||||||
APIURL: "http://localhost:8080",
|
APIURL: "http://localhost:8080",
|
||||||
@@ -138,6 +142,13 @@ func EnableKnowledgeBaseWithResults(results int) Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WithParallelJobs(jobs int) Option {
|
||||||
|
return func(o *options) error {
|
||||||
|
o.parallelJobs = jobs
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func WithNewConversationSubscriber(sub func(openai.ChatCompletionMessage)) Option {
|
func WithNewConversationSubscriber(sub func(openai.ChatCompletionMessage)) Option {
|
||||||
return func(o *options) error {
|
return func(o *options) error {
|
||||||
o.newConversationsSubscribers = append(o.newConversationsSubscribers, sub)
|
o.newConversationsSubscribers = append(o.newConversationsSubscribers, sub)
|
||||||
@@ -198,6 +209,27 @@ func WithMCPServers(servers ...MCPServer) Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WithMCPSTDIOServers(servers ...MCPSTDIOServer) Option {
|
||||||
|
return func(o *options) error {
|
||||||
|
o.mcpStdioServers = servers
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithMCPBoxURL(url string) Option {
|
||||||
|
return func(o *options) error {
|
||||||
|
o.mcpBoxURL = url
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithMCPPrepareScript(script string) Option {
|
||||||
|
return func(o *options) error {
|
||||||
|
o.mcpPrepareScript = script
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func WithLLMAPIURL(url string) Option {
|
func WithLLMAPIURL(url string) Option {
|
||||||
return func(o *options) error {
|
return func(o *options) error {
|
||||||
o.LLMAPI.APIURL = url
|
o.LLMAPI.APIURL = url
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ var _ = Describe("Agent test", func() {
|
|||||||
agent, err = New(
|
agent, err = New(
|
||||||
WithLLMAPIURL(apiURL),
|
WithLLMAPIURL(apiURL),
|
||||||
WithModel(testModel),
|
WithModel(testModel),
|
||||||
|
WithTimeout("10m"),
|
||||||
WithRandomIdentity(),
|
WithRandomIdentity(),
|
||||||
)
|
)
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package state
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/mudler/LocalAGI/core/agent"
|
"github.com/mudler/LocalAGI/core/agent"
|
||||||
"github.com/mudler/LocalAGI/core/types"
|
"github.com/mudler/LocalAGI/core/types"
|
||||||
@@ -34,6 +36,9 @@ type AgentConfig struct {
|
|||||||
Actions []ActionsConfig `json:"actions" form:"actions"`
|
Actions []ActionsConfig `json:"actions" form:"actions"`
|
||||||
DynamicPrompts []DynamicPromptsConfig `json:"dynamic_prompts" form:"dynamic_prompts"`
|
DynamicPrompts []DynamicPromptsConfig `json:"dynamic_prompts" form:"dynamic_prompts"`
|
||||||
MCPServers []agent.MCPServer `json:"mcp_servers" form:"mcp_servers"`
|
MCPServers []agent.MCPServer `json:"mcp_servers" form:"mcp_servers"`
|
||||||
|
MCPSTDIOServers []agent.MCPSTDIOServer `json:"mcp_stdio_servers" form:"mcp_stdio_servers"`
|
||||||
|
MCPPrepareScript string `json:"mcp_prepare_script" form:"mcp_prepare_script"`
|
||||||
|
MCPBoxURL string `json:"mcp_box_url" form:"mcp_box_url"`
|
||||||
|
|
||||||
Description string `json:"description" form:"description"`
|
Description string `json:"description" form:"description"`
|
||||||
|
|
||||||
@@ -61,6 +66,7 @@ type AgentConfig struct {
|
|||||||
SystemPrompt string `json:"system_prompt" form:"system_prompt"`
|
SystemPrompt string `json:"system_prompt" form:"system_prompt"`
|
||||||
LongTermMemory bool `json:"long_term_memory" form:"long_term_memory"`
|
LongTermMemory bool `json:"long_term_memory" form:"long_term_memory"`
|
||||||
SummaryLongTermMemory bool `json:"summary_long_term_memory" form:"summary_long_term_memory"`
|
SummaryLongTermMemory bool `json:"summary_long_term_memory" form:"summary_long_term_memory"`
|
||||||
|
ParallelJobs int `json:"parallel_jobs" form:"parallel_jobs"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type AgentConfigMeta struct {
|
type AgentConfigMeta struct {
|
||||||
@@ -247,7 +253,7 @@ func NewAgentConfigMeta(
|
|||||||
Name: "enable_reasoning",
|
Name: "enable_reasoning",
|
||||||
Label: "Enable Reasoning",
|
Label: "Enable Reasoning",
|
||||||
Type: "checkbox",
|
Type: "checkbox",
|
||||||
DefaultValue: false,
|
DefaultValue: true,
|
||||||
HelpText: "Enable agent to explain its reasoning process",
|
HelpText: "Enable agent to explain its reasoning process",
|
||||||
Tags: config.Tags{Section: "AdvancedSettings"},
|
Tags: config.Tags{Section: "AdvancedSettings"},
|
||||||
},
|
},
|
||||||
@@ -260,6 +266,32 @@ func NewAgentConfigMeta(
|
|||||||
Step: 1,
|
Step: 1,
|
||||||
Tags: config.Tags{Section: "AdvancedSettings"},
|
Tags: config.Tags{Section: "AdvancedSettings"},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "parallel_jobs",
|
||||||
|
Label: "Parallel Jobs",
|
||||||
|
Type: "number",
|
||||||
|
DefaultValue: 5,
|
||||||
|
Min: 1,
|
||||||
|
Step: 1,
|
||||||
|
HelpText: "Number of concurrent tasks that can run in parallel",
|
||||||
|
Tags: config.Tags{Section: "AdvancedSettings"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "mcp_stdio_servers",
|
||||||
|
Label: "MCP STDIO Servers",
|
||||||
|
Type: "textarea",
|
||||||
|
DefaultValue: "",
|
||||||
|
HelpText: "JSON configuration for MCP STDIO servers",
|
||||||
|
Tags: config.Tags{Section: "AdvancedSettings"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "mcp_prepare_script",
|
||||||
|
Label: "MCP Prepare Script",
|
||||||
|
Type: "textarea",
|
||||||
|
DefaultValue: "",
|
||||||
|
HelpText: "Script to prepare the MCP box",
|
||||||
|
Tags: config.Tags{Section: "AdvancedSettings"},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
MCPServers: []config.Field{
|
MCPServers: []config.Field{
|
||||||
{
|
{
|
||||||
@@ -286,3 +318,148 @@ type Connector interface {
|
|||||||
AgentReasoningCallback() func(state types.ActionCurrentState) bool
|
AgentReasoningCallback() func(state types.ActionCurrentState) bool
|
||||||
Start(a *agent.Agent)
|
Start(a *agent.Agent)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UnmarshalJSON implements json.Unmarshaler for AgentConfig
|
||||||
|
func (a *AgentConfig) UnmarshalJSON(data []byte) error {
|
||||||
|
// Create a temporary type to avoid infinite recursion
|
||||||
|
type Alias AgentConfig
|
||||||
|
aux := &struct {
|
||||||
|
*Alias
|
||||||
|
MCPSTDIOServersConfig interface{} `json:"mcp_stdio_servers"`
|
||||||
|
}{
|
||||||
|
Alias: (*Alias)(a),
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.Unmarshal(data, &aux); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle MCP STDIO servers configuration
|
||||||
|
if aux.MCPSTDIOServersConfig != nil {
|
||||||
|
switch v := aux.MCPSTDIOServersConfig.(type) {
|
||||||
|
case string:
|
||||||
|
// Parse string configuration
|
||||||
|
var mcpConfig struct {
|
||||||
|
MCPServers map[string]struct {
|
||||||
|
Command string `json:"command"`
|
||||||
|
Args []string `json:"args"`
|
||||||
|
Env map[string]string `json:"env"`
|
||||||
|
} `json:"mcpServers"`
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.Unmarshal([]byte(v), &mcpConfig); err != nil {
|
||||||
|
return fmt.Errorf("failed to parse MCP STDIO servers configuration: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
a.MCPSTDIOServers = make([]agent.MCPSTDIOServer, 0, len(mcpConfig.MCPServers))
|
||||||
|
for _, server := range mcpConfig.MCPServers {
|
||||||
|
// Convert env map to slice of "KEY=VALUE" strings
|
||||||
|
envSlice := make([]string, 0, len(server.Env))
|
||||||
|
for k, v := range server.Env {
|
||||||
|
envSlice = append(envSlice, fmt.Sprintf("%s=%s", k, v))
|
||||||
|
}
|
||||||
|
|
||||||
|
a.MCPSTDIOServers = append(a.MCPSTDIOServers, agent.MCPSTDIOServer{
|
||||||
|
Cmd: server.Command,
|
||||||
|
Args: server.Args,
|
||||||
|
Env: envSlice,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
case []interface{}:
|
||||||
|
// Parse array configuration
|
||||||
|
a.MCPSTDIOServers = make([]agent.MCPSTDIOServer, 0, len(v))
|
||||||
|
for _, server := range v {
|
||||||
|
serverMap, ok := server.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("invalid server configuration format")
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd, _ := serverMap["cmd"].(string)
|
||||||
|
args := make([]string, 0)
|
||||||
|
if argsInterface, ok := serverMap["args"].([]interface{}); ok {
|
||||||
|
for _, arg := range argsInterface {
|
||||||
|
if argStr, ok := arg.(string); ok {
|
||||||
|
args = append(args, argStr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
env := make([]string, 0)
|
||||||
|
if envInterface, ok := serverMap["env"].([]interface{}); ok {
|
||||||
|
for _, e := range envInterface {
|
||||||
|
if envStr, ok := e.(string); ok {
|
||||||
|
env = append(env, envStr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
a.MCPSTDIOServers = append(a.MCPSTDIOServers, agent.MCPSTDIOServer{
|
||||||
|
Cmd: cmd,
|
||||||
|
Args: args,
|
||||||
|
Env: env,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalJSON implements json.Marshaler for AgentConfig
|
||||||
|
func (a *AgentConfig) MarshalJSON() ([]byte, error) {
|
||||||
|
// Create a temporary type to avoid infinite recursion
|
||||||
|
type Alias AgentConfig
|
||||||
|
aux := &struct {
|
||||||
|
*Alias
|
||||||
|
MCPSTDIOServersConfig string `json:"mcp_stdio_servers,omitempty"`
|
||||||
|
}{
|
||||||
|
Alias: (*Alias)(a),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert MCPSTDIOServers back to the expected JSON format
|
||||||
|
if len(a.MCPSTDIOServers) > 0 {
|
||||||
|
mcpConfig := struct {
|
||||||
|
MCPServers map[string]struct {
|
||||||
|
Command string `json:"command"`
|
||||||
|
Args []string `json:"args"`
|
||||||
|
Env map[string]string `json:"env"`
|
||||||
|
} `json:"mcpServers"`
|
||||||
|
}{
|
||||||
|
MCPServers: make(map[string]struct {
|
||||||
|
Command string `json:"command"`
|
||||||
|
Args []string `json:"args"`
|
||||||
|
Env map[string]string `json:"env"`
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert each MCPSTDIOServer to the expected format
|
||||||
|
for i, server := range a.MCPSTDIOServers {
|
||||||
|
// Convert env slice back to map
|
||||||
|
envMap := make(map[string]string)
|
||||||
|
for _, env := range server.Env {
|
||||||
|
if parts := strings.SplitN(env, "=", 2); len(parts) == 2 {
|
||||||
|
envMap[parts[0]] = parts[1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mcpConfig.MCPServers[fmt.Sprintf("server%d", i)] = struct {
|
||||||
|
Command string `json:"command"`
|
||||||
|
Args []string `json:"args"`
|
||||||
|
Env map[string]string `json:"env"`
|
||||||
|
}{
|
||||||
|
Command: server.Cmd,
|
||||||
|
Args: server.Args,
|
||||||
|
Env: envMap,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Marshal the MCP config to JSON string
|
||||||
|
mcpConfigJSON, err := json.Marshal(mcpConfig)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to marshal MCP STDIO servers configuration: %w", err)
|
||||||
|
}
|
||||||
|
aux.MCPSTDIOServersConfig = string(mcpConfigJSON)
|
||||||
|
}
|
||||||
|
|
||||||
|
return json.Marshal(aux)
|
||||||
|
}
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ type AgentPool struct {
|
|||||||
managers map[string]sse.Manager
|
managers map[string]sse.Manager
|
||||||
agentStatus map[string]*Status
|
agentStatus map[string]*Status
|
||||||
apiURL, defaultModel, defaultMultimodalModel string
|
apiURL, defaultModel, defaultMultimodalModel string
|
||||||
|
mcpBoxURL string
|
||||||
imageModel, localRAGAPI, localRAGKey, apiKey string
|
imageModel, localRAGAPI, localRAGKey, apiKey string
|
||||||
availableActions func(*AgentConfig) func(ctx context.Context, pool *AgentPool) []types.Action
|
availableActions func(*AgentConfig) func(ctx context.Context, pool *AgentPool) []types.Action
|
||||||
connectors func(*AgentConfig) []Connector
|
connectors func(*AgentConfig) []Connector
|
||||||
@@ -72,7 +73,7 @@ func loadPoolFromFile(path string) (*AgentPoolData, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewAgentPool(
|
func NewAgentPool(
|
||||||
defaultModel, defaultMultimodalModel, imageModel, apiURL, apiKey, directory string,
|
defaultModel, defaultMultimodalModel, imageModel, apiURL, apiKey, directory, mcpBoxURL string,
|
||||||
LocalRAGAPI string,
|
LocalRAGAPI string,
|
||||||
availableActions func(*AgentConfig) func(ctx context.Context, pool *AgentPool) []types.Action,
|
availableActions func(*AgentConfig) func(ctx context.Context, pool *AgentPool) []types.Action,
|
||||||
connectors func(*AgentConfig) []Connector,
|
connectors func(*AgentConfig) []Connector,
|
||||||
@@ -98,6 +99,7 @@ func NewAgentPool(
|
|||||||
apiURL: apiURL,
|
apiURL: apiURL,
|
||||||
defaultModel: defaultModel,
|
defaultModel: defaultModel,
|
||||||
defaultMultimodalModel: defaultMultimodalModel,
|
defaultMultimodalModel: defaultMultimodalModel,
|
||||||
|
mcpBoxURL: mcpBoxURL,
|
||||||
imageModel: imageModel,
|
imageModel: imageModel,
|
||||||
localRAGAPI: LocalRAGAPI,
|
localRAGAPI: LocalRAGAPI,
|
||||||
apiKey: apiKey,
|
apiKey: apiKey,
|
||||||
@@ -123,6 +125,7 @@ func NewAgentPool(
|
|||||||
pooldir: directory,
|
pooldir: directory,
|
||||||
defaultModel: defaultModel,
|
defaultModel: defaultModel,
|
||||||
defaultMultimodalModel: defaultMultimodalModel,
|
defaultMultimodalModel: defaultMultimodalModel,
|
||||||
|
mcpBoxURL: mcpBoxURL,
|
||||||
imageModel: imageModel,
|
imageModel: imageModel,
|
||||||
apiKey: apiKey,
|
apiKey: apiKey,
|
||||||
agents: make(map[string]*Agent),
|
agents: make(map[string]*Agent),
|
||||||
@@ -166,7 +169,56 @@ func (a *AgentPool) CreateAgent(name string, agentConfig *AgentConfig) error {
|
|||||||
}
|
}
|
||||||
}(a.pool[name])
|
}(a.pool[name])
|
||||||
|
|
||||||
return a.startAgentWithConfig(name, agentConfig)
|
return a.startAgentWithConfig(name, agentConfig, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *AgentPool) RecreateAgent(name string, agentConfig *AgentConfig) error {
|
||||||
|
a.Lock()
|
||||||
|
defer a.Unlock()
|
||||||
|
|
||||||
|
oldAgent := a.agents[name]
|
||||||
|
var o *types.Observable
|
||||||
|
obs := oldAgent.Observer()
|
||||||
|
if obs != nil {
|
||||||
|
o = obs.NewObservable()
|
||||||
|
o.Name = "Restarting Agent"
|
||||||
|
o.Icon = "sync"
|
||||||
|
o.Creation = &types.Creation{}
|
||||||
|
obs.Update(*o)
|
||||||
|
}
|
||||||
|
|
||||||
|
stateFile, characterFile := a.stateFiles(name)
|
||||||
|
|
||||||
|
os.Remove(stateFile)
|
||||||
|
os.Remove(characterFile)
|
||||||
|
|
||||||
|
oldAgent.Stop()
|
||||||
|
|
||||||
|
a.pool[name] = *agentConfig
|
||||||
|
delete(a.agents, name)
|
||||||
|
|
||||||
|
if err := a.save(); err != nil {
|
||||||
|
if obs != nil {
|
||||||
|
o.Completion = &types.Completion{Error: err.Error()}
|
||||||
|
obs.Update(*o)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := a.startAgentWithConfig(name, agentConfig, obs); err != nil {
|
||||||
|
if obs != nil {
|
||||||
|
o.Completion = &types.Completion{Error: err.Error()}
|
||||||
|
obs.Update(*o)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if obs != nil {
|
||||||
|
o.Completion = &types.Completion{}
|
||||||
|
obs.Update(*o)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func createAgentAvatar(APIURL, APIKey, model, imageModel, avatarDir string, agent AgentConfig) error {
|
func createAgentAvatar(APIURL, APIKey, model, imageModel, avatarDir string, agent AgentConfig) error {
|
||||||
@@ -268,8 +320,13 @@ func (a *AgentPool) GetStatusHistory(name string) *Status {
|
|||||||
return a.agentStatus[name]
|
return a.agentStatus[name]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error {
|
func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig, obs Observer) error {
|
||||||
manager := sse.NewManager(5)
|
var manager sse.Manager
|
||||||
|
if m, ok := a.managers[name]; ok {
|
||||||
|
manager = m
|
||||||
|
} else {
|
||||||
|
manager = sse.NewManager(5)
|
||||||
|
}
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
model := a.defaultModel
|
model := a.defaultModel
|
||||||
multimodalModel := a.defaultMultimodalModel
|
multimodalModel := a.defaultMultimodalModel
|
||||||
@@ -282,6 +339,10 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
|
|||||||
model = config.Model
|
model = config.Model
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if config.MCPBoxURL != "" {
|
||||||
|
a.mcpBoxURL = config.MCPBoxURL
|
||||||
|
}
|
||||||
|
|
||||||
if config.PeriodicRuns == "" {
|
if config.PeriodicRuns == "" {
|
||||||
config.PeriodicRuns = "10m"
|
config.PeriodicRuns = "10m"
|
||||||
}
|
}
|
||||||
@@ -331,6 +392,10 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
|
|||||||
// dynamicPrompts = append(dynamicPrompts, p.ToMap())
|
// dynamicPrompts = append(dynamicPrompts, p.ToMap())
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
if obs == nil {
|
||||||
|
obs = NewSSEObserver(name, manager)
|
||||||
|
}
|
||||||
|
|
||||||
opts := []Option{
|
opts := []Option{
|
||||||
WithModel(model),
|
WithModel(model),
|
||||||
WithLLMAPIURL(a.apiURL),
|
WithLLMAPIURL(a.apiURL),
|
||||||
@@ -338,7 +403,10 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
|
|||||||
WithMCPServers(config.MCPServers...),
|
WithMCPServers(config.MCPServers...),
|
||||||
WithPeriodicRuns(config.PeriodicRuns),
|
WithPeriodicRuns(config.PeriodicRuns),
|
||||||
WithPermanentGoal(config.PermanentGoal),
|
WithPermanentGoal(config.PermanentGoal),
|
||||||
|
WithMCPSTDIOServers(config.MCPSTDIOServers...),
|
||||||
|
WithMCPBoxURL(a.mcpBoxURL),
|
||||||
WithPrompts(promptBlocks...),
|
WithPrompts(promptBlocks...),
|
||||||
|
WithMCPPrepareScript(config.MCPPrepareScript),
|
||||||
// WithDynamicPrompts(dynamicPrompts...),
|
// WithDynamicPrompts(dynamicPrompts...),
|
||||||
WithCharacter(Character{
|
WithCharacter(Character{
|
||||||
Name: name,
|
Name: name,
|
||||||
@@ -407,7 +475,7 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
|
|||||||
c.AgentResultCallback()(state)
|
c.AgentResultCallback()(state)
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
WithObserver(NewSSEObserver(name, manager)),
|
WithObserver(obs),
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.HUD {
|
if config.HUD {
|
||||||
@@ -466,6 +534,10 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
|
|||||||
opts = append(opts, WithLoopDetectionSteps(config.LoopDetectionSteps))
|
opts = append(opts, WithLoopDetectionSteps(config.LoopDetectionSteps))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if config.ParallelJobs > 0 {
|
||||||
|
opts = append(opts, WithParallelJobs(config.ParallelJobs))
|
||||||
|
}
|
||||||
|
|
||||||
xlog.Info("Starting agent", "name", name, "config", config)
|
xlog.Info("Starting agent", "name", name, "config", config)
|
||||||
|
|
||||||
agent, err := New(opts...)
|
agent, err := New(opts...)
|
||||||
@@ -510,7 +582,7 @@ func (a *AgentPool) StartAll() error {
|
|||||||
if a.agents[name] != nil { // Agent already started
|
if a.agents[name] != nil { // Agent already started
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if err := a.startAgentWithConfig(name, &config); err != nil {
|
if err := a.startAgentWithConfig(name, &config, nil); err != nil {
|
||||||
xlog.Error("Failed to start agent", "name", name, "error", err)
|
xlog.Error("Failed to start agent", "name", name, "error", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -548,7 +620,7 @@ func (a *AgentPool) Start(name string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if config, ok := a.pool[name]; ok {
|
if config, ok := a.pool[name]; ok {
|
||||||
return a.startAgentWithConfig(name, &config)
|
return a.startAgentWithConfig(name, &config, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Errorf("agent %s not found", name)
|
return fmt.Errorf("agent %s not found", name)
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Creation struct {
|
type Creation struct {
|
||||||
|
ChatCompletionMessage *openai.ChatCompletionMessage `json:"chat_completion_message,omitempty"`
|
||||||
ChatCompletionRequest *openai.ChatCompletionRequest `json:"chat_completion_request,omitempty"`
|
ChatCompletionRequest *openai.ChatCompletionRequest `json:"chat_completion_request,omitempty"`
|
||||||
FunctionDefinition *openai.FunctionDefinition `json:"function_definition,omitempty"`
|
FunctionDefinition *openai.FunctionDefinition `json:"function_definition,omitempty"`
|
||||||
FunctionParams ActionParams `json:"function_params,omitempty"`
|
FunctionParams ActionParams `json:"function_params,omitempty"`
|
||||||
|
|||||||
@@ -12,6 +12,16 @@ services:
|
|||||||
- /dev/dri/card1
|
- /dev/dri/card1
|
||||||
- /dev/dri/renderD129
|
- /dev/dri/renderD129
|
||||||
|
|
||||||
|
mcpbox:
|
||||||
|
extends:
|
||||||
|
file: docker-compose.yaml
|
||||||
|
service: mcpbox
|
||||||
|
|
||||||
|
dind:
|
||||||
|
extends:
|
||||||
|
file: docker-compose.yaml
|
||||||
|
service: dind
|
||||||
|
|
||||||
localrecall:
|
localrecall:
|
||||||
extends:
|
extends:
|
||||||
file: docker-compose.yaml
|
file: docker-compose.yaml
|
||||||
|
|||||||
@@ -17,6 +17,16 @@ services:
|
|||||||
count: 1
|
count: 1
|
||||||
capabilities: [gpu]
|
capabilities: [gpu]
|
||||||
|
|
||||||
|
mcpbox:
|
||||||
|
extends:
|
||||||
|
file: docker-compose.yaml
|
||||||
|
service: mcpbox
|
||||||
|
|
||||||
|
dind:
|
||||||
|
extends:
|
||||||
|
file: docker-compose.yaml
|
||||||
|
service: dind
|
||||||
|
|
||||||
localrecall:
|
localrecall:
|
||||||
extends:
|
extends:
|
||||||
file: docker-compose.yaml
|
file: docker-compose.yaml
|
||||||
|
|||||||
@@ -46,12 +46,44 @@ services:
|
|||||||
image: busybox
|
image: busybox
|
||||||
command: ["sh", "-c", "until wget -q -O - http://localrecall:8080 > /dev/null 2>&1; do echo 'Waiting for localrecall...'; sleep 1; done; echo 'localrecall is up!'"]
|
command: ["sh", "-c", "until wget -q -O - http://localrecall:8080 > /dev/null 2>&1; do echo 'Waiting for localrecall...'; sleep 1; done; echo 'localrecall is up!'"]
|
||||||
|
|
||||||
|
mcpbox:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: Dockerfile.mcpbox
|
||||||
|
ports:
|
||||||
|
- "8080"
|
||||||
|
volumes:
|
||||||
|
- ./volumes/mcpbox:/app/data
|
||||||
|
environment:
|
||||||
|
- DOCKER_HOST=tcp://dind:2375
|
||||||
|
depends_on:
|
||||||
|
dind:
|
||||||
|
condition: service_healthy
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "wget", "-q", "-O", "-", "http://localhost:8080/processes"]
|
||||||
|
interval: 30s
|
||||||
|
timeout: 10s
|
||||||
|
retries: 3
|
||||||
|
|
||||||
|
dind:
|
||||||
|
image: docker:dind
|
||||||
|
privileged: true
|
||||||
|
environment:
|
||||||
|
- DOCKER_TLS_CERTDIR=""
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "docker", "info"]
|
||||||
|
interval: 10s
|
||||||
|
timeout: 5s
|
||||||
|
retries: 3
|
||||||
|
|
||||||
localagi:
|
localagi:
|
||||||
depends_on:
|
depends_on:
|
||||||
localai:
|
localai:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
localrecall-healthcheck:
|
localrecall-healthcheck:
|
||||||
condition: service_completed_successfully
|
condition: service_completed_successfully
|
||||||
|
mcpbox:
|
||||||
|
condition: service_healthy
|
||||||
build:
|
build:
|
||||||
context: .
|
context: .
|
||||||
dockerfile: Dockerfile.webui
|
dockerfile: Dockerfile.webui
|
||||||
@@ -68,6 +100,7 @@ services:
|
|||||||
- LOCALAGI_STATE_DIR=/pool
|
- LOCALAGI_STATE_DIR=/pool
|
||||||
- LOCALAGI_TIMEOUT=5m
|
- LOCALAGI_TIMEOUT=5m
|
||||||
- LOCALAGI_ENABLE_CONVERSATIONS_LOGGING=false
|
- LOCALAGI_ENABLE_CONVERSATIONS_LOGGING=false
|
||||||
|
- LOCALAGI_MCPBOX_URL=http://mcpbox:8080
|
||||||
extra_hosts:
|
extra_hosts:
|
||||||
- "host.docker.internal:host-gateway"
|
- "host.docker.internal:host-gateway"
|
||||||
volumes:
|
volumes:
|
||||||
|
|||||||
2
main.go
2
main.go
@@ -23,6 +23,7 @@ var apiKeysEnv = os.Getenv("LOCALAGI_API_KEYS")
|
|||||||
var imageModel = os.Getenv("LOCALAGI_IMAGE_MODEL")
|
var imageModel = os.Getenv("LOCALAGI_IMAGE_MODEL")
|
||||||
var conversationDuration = os.Getenv("LOCALAGI_CONVERSATION_DURATION")
|
var conversationDuration = os.Getenv("LOCALAGI_CONVERSATION_DURATION")
|
||||||
var localOperatorBaseURL = os.Getenv("LOCALOPERATOR_BASE_URL")
|
var localOperatorBaseURL = os.Getenv("LOCALOPERATOR_BASE_URL")
|
||||||
|
var mcpboxURL = os.Getenv("LOCALAGI_MCPBOX_URL")
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
if baseModel == "" {
|
if baseModel == "" {
|
||||||
@@ -61,6 +62,7 @@ func main() {
|
|||||||
apiURL,
|
apiURL,
|
||||||
apiKey,
|
apiKey,
|
||||||
stateDir,
|
stateDir,
|
||||||
|
mcpboxURL,
|
||||||
localRAG,
|
localRAG,
|
||||||
services.Actions(map[string]string{
|
services.Actions(map[string]string{
|
||||||
"browser-agent-runner-base-url": localOperatorBaseURL,
|
"browser-agent-runner-base-url": localOperatorBaseURL,
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package api
|
package localoperator
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
|||||||
325
pkg/stdio/client.go
Normal file
325
pkg/stdio/client.go
Normal file
@@ -0,0 +1,325 @@
|
|||||||
|
package stdio
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Client implements the transport.Interface for stdio processes
|
||||||
|
type Client struct {
|
||||||
|
baseURL string
|
||||||
|
processes map[string]*Process
|
||||||
|
groups map[string][]string
|
||||||
|
mu sync.RWMutex
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewClient creates a new stdio transport client
|
||||||
|
func NewClient(baseURL string) *Client {
|
||||||
|
return &Client{
|
||||||
|
baseURL: baseURL,
|
||||||
|
processes: make(map[string]*Process),
|
||||||
|
groups: make(map[string][]string),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateProcess starts a new process in a group
|
||||||
|
func (c *Client) CreateProcess(ctx context.Context, command string, args []string, env []string, groupID string) (*Process, error) {
|
||||||
|
log.Printf("Creating process: command=%s, args=%v, groupID=%s", command, args, groupID)
|
||||||
|
|
||||||
|
req := struct {
|
||||||
|
Command string `json:"command"`
|
||||||
|
Args []string `json:"args"`
|
||||||
|
Env []string `json:"env"`
|
||||||
|
GroupID string `json:"group_id"`
|
||||||
|
}{
|
||||||
|
Command: command,
|
||||||
|
Args: args,
|
||||||
|
Env: env,
|
||||||
|
GroupID: groupID,
|
||||||
|
}
|
||||||
|
|
||||||
|
reqBody, err := json.Marshal(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to marshal request: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
url := fmt.Sprintf("%s/processes", c.baseURL)
|
||||||
|
log.Printf("Sending POST request to %s", url)
|
||||||
|
|
||||||
|
resp, err := http.Post(url, "application/json", bytes.NewReader(reqBody))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to start process: %w", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
log.Printf("Received response with status: %d", resp.StatusCode)
|
||||||
|
|
||||||
|
var result struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
body, _ := io.ReadAll(resp.Body)
|
||||||
|
|
||||||
|
if err := json.Unmarshal(body, &result); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to decode response: %w. body: %s", err, string(body))
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Successfully created process with ID: %s", result.ID)
|
||||||
|
|
||||||
|
process := &Process{
|
||||||
|
ID: result.ID,
|
||||||
|
GroupID: groupID,
|
||||||
|
CreatedAt: time.Now(),
|
||||||
|
}
|
||||||
|
|
||||||
|
c.mu.Lock()
|
||||||
|
c.processes[process.ID] = process
|
||||||
|
if groupID != "" {
|
||||||
|
c.groups[groupID] = append(c.groups[groupID], process.ID)
|
||||||
|
}
|
||||||
|
c.mu.Unlock()
|
||||||
|
|
||||||
|
return process, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetProcess returns a process by ID
|
||||||
|
func (c *Client) GetProcess(id string) (*Process, error) {
|
||||||
|
c.mu.RLock()
|
||||||
|
process, exists := c.processes[id]
|
||||||
|
c.mu.RUnlock()
|
||||||
|
|
||||||
|
if !exists {
|
||||||
|
return nil, fmt.Errorf("process not found: %s", id)
|
||||||
|
}
|
||||||
|
|
||||||
|
return process, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetGroupProcesses returns all processes in a group
|
||||||
|
func (c *Client) GetGroupProcesses(groupID string) ([]*Process, error) {
|
||||||
|
c.mu.RLock()
|
||||||
|
processIDs, exists := c.groups[groupID]
|
||||||
|
if !exists {
|
||||||
|
c.mu.RUnlock()
|
||||||
|
return nil, fmt.Errorf("group not found: %s", groupID)
|
||||||
|
}
|
||||||
|
|
||||||
|
processes := make([]*Process, 0, len(processIDs))
|
||||||
|
for _, pid := range processIDs {
|
||||||
|
if process, exists := c.processes[pid]; exists {
|
||||||
|
processes = append(processes, process)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c.mu.RUnlock()
|
||||||
|
|
||||||
|
return processes, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// StopProcess stops a single process
|
||||||
|
func (c *Client) StopProcess(id string) error {
|
||||||
|
c.mu.Lock()
|
||||||
|
process, exists := c.processes[id]
|
||||||
|
if !exists {
|
||||||
|
c.mu.Unlock()
|
||||||
|
return fmt.Errorf("process not found: %s", id)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove from group if it exists
|
||||||
|
if process.GroupID != "" {
|
||||||
|
groupProcesses := c.groups[process.GroupID]
|
||||||
|
for i, pid := range groupProcesses {
|
||||||
|
if pid == id {
|
||||||
|
c.groups[process.GroupID] = append(groupProcesses[:i], groupProcesses[i+1:]...)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(c.groups[process.GroupID]) == 0 {
|
||||||
|
delete(c.groups, process.GroupID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
delete(c.processes, id)
|
||||||
|
c.mu.Unlock()
|
||||||
|
|
||||||
|
req, err := http.NewRequest(
|
||||||
|
"DELETE",
|
||||||
|
fmt.Sprintf("%s/processes/%s", c.baseURL, id),
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create request: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to stop process: %w", err)
|
||||||
|
}
|
||||||
|
resp.Body.Close()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// StopGroup stops all processes in a group
|
||||||
|
func (c *Client) StopGroup(groupID string) error {
|
||||||
|
c.mu.Lock()
|
||||||
|
processIDs, exists := c.groups[groupID]
|
||||||
|
if !exists {
|
||||||
|
c.mu.Unlock()
|
||||||
|
return fmt.Errorf("group not found: %s", groupID)
|
||||||
|
}
|
||||||
|
c.mu.Unlock()
|
||||||
|
|
||||||
|
for _, pid := range processIDs {
|
||||||
|
if err := c.StopProcess(pid); err != nil {
|
||||||
|
return fmt.Errorf("failed to stop process %s in group %s: %w", pid, groupID, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListGroups returns all group IDs
|
||||||
|
func (c *Client) ListGroups() []string {
|
||||||
|
c.mu.RLock()
|
||||||
|
defer c.mu.RUnlock()
|
||||||
|
|
||||||
|
groups := make([]string, 0, len(c.groups))
|
||||||
|
for groupID := range c.groups {
|
||||||
|
groups = append(groups, groupID)
|
||||||
|
}
|
||||||
|
return groups
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetProcessIO returns io.Reader and io.Writer for a process
|
||||||
|
func (c *Client) GetProcessIO(id string) (io.Reader, io.Writer, error) {
|
||||||
|
log.Printf("Getting IO for process: %s", id)
|
||||||
|
|
||||||
|
process, err := c.GetProcess(id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse the base URL to get the host
|
||||||
|
baseURL, err := url.Parse(c.baseURL)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, fmt.Errorf("failed to parse base URL: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Connect to WebSocket
|
||||||
|
u := url.URL{
|
||||||
|
Scheme: "ws",
|
||||||
|
Host: baseURL.Host,
|
||||||
|
Path: fmt.Sprintf("/ws/%s", process.ID),
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Connecting to WebSocket at: %s", u.String())
|
||||||
|
|
||||||
|
conn, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, fmt.Errorf("failed to connect to WebSocket: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Successfully connected to WebSocket for process: %s", id)
|
||||||
|
|
||||||
|
// Create reader and writer
|
||||||
|
reader := &websocketReader{conn: conn}
|
||||||
|
writer := &websocketWriter{conn: conn}
|
||||||
|
|
||||||
|
return reader, writer, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// websocketReader implements io.Reader for WebSocket
|
||||||
|
type websocketReader struct {
|
||||||
|
conn *websocket.Conn
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *websocketReader) Read(p []byte) (n int, err error) {
|
||||||
|
_, message, err := r.conn.ReadMessage()
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
n = copy(p, message)
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// websocketWriter implements io.Writer for WebSocket
|
||||||
|
type websocketWriter struct {
|
||||||
|
conn *websocket.Conn
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *websocketWriter) Write(p []byte) (n int, err error) {
|
||||||
|
// Use BinaryMessage type for better compatibility
|
||||||
|
err = w.conn.WriteMessage(websocket.BinaryMessage, p)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("failed to write WebSocket message: %w", err)
|
||||||
|
}
|
||||||
|
return len(p), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close closes all connections and stops all processes
|
||||||
|
func (c *Client) Close() error {
|
||||||
|
c.mu.Lock()
|
||||||
|
defer c.mu.Unlock()
|
||||||
|
|
||||||
|
// Stop all processes
|
||||||
|
for id := range c.processes {
|
||||||
|
if err := c.StopProcess(id); err != nil {
|
||||||
|
return fmt.Errorf("failed to stop process %s: %w", id, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RunProcess executes a command and returns its output
|
||||||
|
func (c *Client) RunProcess(ctx context.Context, command string, args []string, env []string) (string, error) {
|
||||||
|
log.Printf("Running one-time process: command=%s, args=%v", command, args)
|
||||||
|
|
||||||
|
req := struct {
|
||||||
|
Command string `json:"command"`
|
||||||
|
Args []string `json:"args"`
|
||||||
|
Env []string `json:"env"`
|
||||||
|
}{
|
||||||
|
Command: command,
|
||||||
|
Args: args,
|
||||||
|
Env: env,
|
||||||
|
}
|
||||||
|
|
||||||
|
reqBody, err := json.Marshal(req)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("failed to marshal request: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
url := fmt.Sprintf("%s/run", c.baseURL)
|
||||||
|
log.Printf("Sending POST request to %s", url)
|
||||||
|
|
||||||
|
resp, err := http.Post(url, "application/json", bytes.NewReader(reqBody))
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("failed to execute process: %w", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
log.Printf("Received response with status: %d", resp.StatusCode)
|
||||||
|
|
||||||
|
var result struct {
|
||||||
|
Output string `json:"output"`
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
||||||
|
body, _ := io.ReadAll(resp.Body)
|
||||||
|
return "", fmt.Errorf("failed to decode response: %w. body: %s", err, string(body))
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Successfully executed process with output length: %d", len(result.Output))
|
||||||
|
return result.Output, nil
|
||||||
|
}
|
||||||
28
pkg/stdio/client_suite_test.go
Normal file
28
pkg/stdio/client_suite_test.go
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package stdio
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
. "github.com/onsi/ginkgo/v2"
|
||||||
|
. "github.com/onsi/gomega"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSTDIOTransport(t *testing.T) {
|
||||||
|
RegisterFailHandler(Fail)
|
||||||
|
RunSpecs(t, "STDIOTransport test suite")
|
||||||
|
}
|
||||||
|
|
||||||
|
var baseURL string
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
baseURL = os.Getenv("LOCALAGI_MCPBOX_URL")
|
||||||
|
if baseURL == "" {
|
||||||
|
baseURL = "http://localhost:8080"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = AfterSuite(func() {
|
||||||
|
client := NewClient(baseURL)
|
||||||
|
client.StopGroup("test-group")
|
||||||
|
})
|
||||||
235
pkg/stdio/client_test.go
Normal file
235
pkg/stdio/client_test.go
Normal file
@@ -0,0 +1,235 @@
|
|||||||
|
package stdio
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
mcp "github.com/metoro-io/mcp-golang"
|
||||||
|
"github.com/metoro-io/mcp-golang/transport/stdio"
|
||||||
|
"github.com/mudler/LocalAGI/pkg/xlog"
|
||||||
|
. "github.com/onsi/ginkgo/v2"
|
||||||
|
. "github.com/onsi/gomega"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ = Describe("Client", func() {
|
||||||
|
var (
|
||||||
|
client *Client
|
||||||
|
)
|
||||||
|
|
||||||
|
BeforeEach(func() {
|
||||||
|
client = NewClient(baseURL)
|
||||||
|
})
|
||||||
|
|
||||||
|
AfterEach(func() {
|
||||||
|
if client != nil {
|
||||||
|
Expect(client.Close()).To(Succeed())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
Context("Process Management", func() {
|
||||||
|
It("should create and stop a process", func() {
|
||||||
|
ctx := context.Background()
|
||||||
|
// Use a command that doesn't exit immediately
|
||||||
|
process, err := client.CreateProcess(ctx, "sh", []string{"-c", "echo 'Hello, World!'; sleep 10"}, []string{}, "test-group")
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
Expect(process).NotTo(BeNil())
|
||||||
|
Expect(process.ID).NotTo(BeEmpty())
|
||||||
|
|
||||||
|
// Get process IO
|
||||||
|
reader, writer, err := client.GetProcessIO(process.ID)
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
Expect(reader).NotTo(BeNil())
|
||||||
|
Expect(writer).NotTo(BeNil())
|
||||||
|
|
||||||
|
// Write to process
|
||||||
|
_, err = writer.Write([]byte("test input\n"))
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
|
// Read from process with timeout
|
||||||
|
buf := make([]byte, 1024)
|
||||||
|
readDone := make(chan struct{})
|
||||||
|
var readErr error
|
||||||
|
var readN int
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
readN, readErr = reader.Read(buf)
|
||||||
|
close(readDone)
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Wait for read with timeout
|
||||||
|
select {
|
||||||
|
case <-readDone:
|
||||||
|
Expect(readErr).NotTo(HaveOccurred())
|
||||||
|
Expect(readN).To(BeNumerically(">", 0))
|
||||||
|
Expect(string(buf[:readN])).To(ContainSubstring("Hello, World!"))
|
||||||
|
case <-time.After(5 * time.Second):
|
||||||
|
Fail("Timeout waiting for process output")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stop the process
|
||||||
|
err = client.StopProcess(process.ID)
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
})
|
||||||
|
|
||||||
|
It("should manage process groups", func() {
|
||||||
|
ctx := context.Background()
|
||||||
|
groupID := "test-group"
|
||||||
|
|
||||||
|
// Create multiple processes in the same group
|
||||||
|
process1, err := client.CreateProcess(ctx, "sh", []string{"-c", "echo 'Process 1'; sleep 1"}, []string{}, groupID)
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
Expect(process1).NotTo(BeNil())
|
||||||
|
|
||||||
|
process2, err := client.CreateProcess(ctx, "sh", []string{"-c", "echo 'Process 2'; sleep 1"}, []string{}, groupID)
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
Expect(process2).NotTo(BeNil())
|
||||||
|
|
||||||
|
// Get group processes
|
||||||
|
processes, err := client.GetGroupProcesses(groupID)
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
Expect(processes).To(HaveLen(2))
|
||||||
|
|
||||||
|
// List groups
|
||||||
|
groups := client.ListGroups()
|
||||||
|
Expect(groups).To(ContainElement(groupID))
|
||||||
|
|
||||||
|
// Stop the group
|
||||||
|
err = client.StopGroup(groupID)
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
})
|
||||||
|
|
||||||
|
It("should run a one-time process", func() {
|
||||||
|
ctx := context.Background()
|
||||||
|
output, err := client.RunProcess(ctx, "echo", []string{"One-time process"}, []string{})
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
Expect(output).To(ContainSubstring("One-time process"))
|
||||||
|
})
|
||||||
|
|
||||||
|
It("should handle process with environment variables", func() {
|
||||||
|
ctx := context.Background()
|
||||||
|
env := []string{"TEST_VAR=test_value"}
|
||||||
|
process, err := client.CreateProcess(ctx, "sh", []string{"-c", "env | grep TEST_VAR; sleep 1"}, env, "test-group")
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
Expect(process).NotTo(BeNil())
|
||||||
|
|
||||||
|
// Get process IO
|
||||||
|
reader, _, err := client.GetProcessIO(process.ID)
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
|
// Read environment variables with timeout
|
||||||
|
buf := make([]byte, 1024)
|
||||||
|
readDone := make(chan struct{})
|
||||||
|
var readErr error
|
||||||
|
var readN int
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
readN, readErr = reader.Read(buf)
|
||||||
|
close(readDone)
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Wait for read with timeout
|
||||||
|
select {
|
||||||
|
case <-readDone:
|
||||||
|
Expect(readErr).NotTo(HaveOccurred())
|
||||||
|
Expect(readN).To(BeNumerically(">", 0))
|
||||||
|
Expect(string(buf[:readN])).To(ContainSubstring("TEST_VAR=test_value"))
|
||||||
|
case <-time.After(5 * time.Second):
|
||||||
|
Fail("Timeout waiting for process output")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stop the process
|
||||||
|
err = client.StopProcess(process.ID)
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
})
|
||||||
|
|
||||||
|
It("should handle long-running processes", func() {
|
||||||
|
ctx := context.Background()
|
||||||
|
process, err := client.CreateProcess(ctx, "sh", []string{"-c", "echo 'Starting long process'; sleep 5"}, []string{}, "test-group")
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
Expect(process).NotTo(BeNil())
|
||||||
|
|
||||||
|
// Get process IO
|
||||||
|
reader, _, err := client.GetProcessIO(process.ID)
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
|
// Read initial output
|
||||||
|
buf := make([]byte, 1024)
|
||||||
|
readDone := make(chan struct{})
|
||||||
|
var readErr error
|
||||||
|
var readN int
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
readN, readErr = reader.Read(buf)
|
||||||
|
close(readDone)
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Wait for read with timeout
|
||||||
|
select {
|
||||||
|
case <-readDone:
|
||||||
|
Expect(readErr).NotTo(HaveOccurred())
|
||||||
|
Expect(readN).To(BeNumerically(">", 0))
|
||||||
|
Expect(string(buf[:readN])).To(ContainSubstring("Starting long process"))
|
||||||
|
case <-time.After(5 * time.Second):
|
||||||
|
Fail("Timeout waiting for process output")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait a bit to ensure process is running
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
|
||||||
|
// Stop the process
|
||||||
|
err = client.StopProcess(process.ID)
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
})
|
||||||
|
|
||||||
|
It("MCP", func() {
|
||||||
|
ctx := context.Background()
|
||||||
|
process, err := client.CreateProcess(ctx,
|
||||||
|
"docker", []string{"run", "-i", "--rm", "-e", "GITHUB_PERSONAL_ACCESS_TOKEN", "ghcr.io/github/github-mcp-server"},
|
||||||
|
[]string{"GITHUB_PERSONAL_ACCESS_TOKEN=test"}, "test-group")
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
Expect(process).NotTo(BeNil())
|
||||||
|
Expect(process.ID).NotTo(BeEmpty())
|
||||||
|
|
||||||
|
defer client.StopProcess(process.ID)
|
||||||
|
|
||||||
|
// MCP client
|
||||||
|
|
||||||
|
read, writer, err := client.GetProcessIO(process.ID)
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
Expect(read).NotTo(BeNil())
|
||||||
|
Expect(writer).NotTo(BeNil())
|
||||||
|
|
||||||
|
transport := stdio.NewStdioServerTransportWithIO(read, writer)
|
||||||
|
|
||||||
|
// Create a new client
|
||||||
|
mcpClient := mcp.NewClient(transport)
|
||||||
|
// Initialize the client
|
||||||
|
response, e := mcpClient.Initialize(ctx)
|
||||||
|
Expect(e).NotTo(HaveOccurred())
|
||||||
|
Expect(response).NotTo(BeNil())
|
||||||
|
|
||||||
|
Expect(mcpClient.Ping(ctx)).To(Succeed())
|
||||||
|
|
||||||
|
xlog.Debug("Client initialized: %v", response.Instructions)
|
||||||
|
|
||||||
|
alltools := []mcp.ToolRetType{}
|
||||||
|
var cursor *string
|
||||||
|
for {
|
||||||
|
tools, err := mcpClient.ListTools(ctx, cursor)
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
Expect(tools).NotTo(BeNil())
|
||||||
|
Expect(tools.Tools).NotTo(BeEmpty())
|
||||||
|
alltools = append(alltools, tools.Tools...)
|
||||||
|
|
||||||
|
if tools.NextCursor == nil {
|
||||||
|
break // No more pages
|
||||||
|
}
|
||||||
|
cursor = tools.NextCursor
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tool := range alltools {
|
||||||
|
xlog.Debug("Tool: %v", tool)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
473
pkg/stdio/server.go
Normal file
473
pkg/stdio/server.go
Normal file
@@ -0,0 +1,473 @@
|
|||||||
|
package stdio
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
"github.com/mudler/LocalAGI/pkg/xlog"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Process represents a running process with its stdio streams
|
||||||
|
type Process struct {
|
||||||
|
ID string
|
||||||
|
GroupID string
|
||||||
|
Cmd *exec.Cmd
|
||||||
|
Stdin io.WriteCloser
|
||||||
|
Stdout io.ReadCloser
|
||||||
|
Stderr io.ReadCloser
|
||||||
|
CreatedAt time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
// Server handles process management and stdio streaming
|
||||||
|
type Server struct {
|
||||||
|
processes map[string]*Process
|
||||||
|
groups map[string][]string // maps group ID to process IDs
|
||||||
|
mu sync.RWMutex
|
||||||
|
upgrader websocket.Upgrader
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewServer creates a new stdio server
|
||||||
|
func NewServer() *Server {
|
||||||
|
return &Server{
|
||||||
|
processes: make(map[string]*Process),
|
||||||
|
groups: make(map[string][]string),
|
||||||
|
upgrader: websocket.Upgrader{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// StartProcess starts a new process and returns its ID
|
||||||
|
func (s *Server) StartProcess(ctx context.Context, command string, args []string, env []string, groupID string) (string, error) {
|
||||||
|
xlog.Debug("Starting process", "command", command, "args", args, "groupID", groupID)
|
||||||
|
|
||||||
|
cmd := exec.CommandContext(ctx, command, args...)
|
||||||
|
|
||||||
|
if len(env) > 0 {
|
||||||
|
cmd.Env = append(os.Environ(), env...)
|
||||||
|
xlog.Debug("Process environment", "env", cmd.Env)
|
||||||
|
}
|
||||||
|
|
||||||
|
stdin, err := cmd.StdinPipe()
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("failed to create stdin pipe: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
stdout, err := cmd.StdoutPipe()
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("failed to create stdout pipe: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
stderr, err := cmd.StderrPipe()
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("failed to create stderr pipe: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := cmd.Start(); err != nil {
|
||||||
|
return "", fmt.Errorf("failed to start process: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
process := &Process{
|
||||||
|
ID: fmt.Sprintf("%d", time.Now().UnixNano()),
|
||||||
|
GroupID: groupID,
|
||||||
|
Cmd: cmd,
|
||||||
|
Stdin: stdin,
|
||||||
|
Stdout: stdout,
|
||||||
|
Stderr: stderr,
|
||||||
|
CreatedAt: time.Now(),
|
||||||
|
}
|
||||||
|
|
||||||
|
s.mu.Lock()
|
||||||
|
s.processes[process.ID] = process
|
||||||
|
if groupID != "" {
|
||||||
|
s.groups[groupID] = append(s.groups[groupID], process.ID)
|
||||||
|
}
|
||||||
|
s.mu.Unlock()
|
||||||
|
|
||||||
|
xlog.Debug("Successfully started process", "id", process.ID, "pid", cmd.Process.Pid)
|
||||||
|
return process.ID, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// StopProcess stops a running process
|
||||||
|
func (s *Server) StopProcess(id string) error {
|
||||||
|
s.mu.Lock()
|
||||||
|
process, exists := s.processes[id]
|
||||||
|
if !exists {
|
||||||
|
s.mu.Unlock()
|
||||||
|
return fmt.Errorf("process not found: %s", id)
|
||||||
|
}
|
||||||
|
|
||||||
|
xlog.Debug("Stopping process", "processID", id, "pid", process.Cmd.Process.Pid)
|
||||||
|
|
||||||
|
// Remove from group if it exists
|
||||||
|
if process.GroupID != "" {
|
||||||
|
groupProcesses := s.groups[process.GroupID]
|
||||||
|
for i, pid := range groupProcesses {
|
||||||
|
if pid == id {
|
||||||
|
s.groups[process.GroupID] = append(groupProcesses[:i], groupProcesses[i+1:]...)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(s.groups[process.GroupID]) == 0 {
|
||||||
|
delete(s.groups, process.GroupID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
delete(s.processes, id)
|
||||||
|
s.mu.Unlock()
|
||||||
|
|
||||||
|
if err := process.Cmd.Process.Kill(); err != nil {
|
||||||
|
xlog.Debug("Failed to kill process", "processID", id, "pid", process.Cmd.Process.Pid, "error", err)
|
||||||
|
return fmt.Errorf("failed to kill process: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
xlog.Debug("Successfully killed process", "processID", id, "pid", process.Cmd.Process.Pid)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// StopGroup stops all processes in a group
|
||||||
|
func (s *Server) StopGroup(groupID string) error {
|
||||||
|
s.mu.Lock()
|
||||||
|
processIDs, exists := s.groups[groupID]
|
||||||
|
if !exists {
|
||||||
|
s.mu.Unlock()
|
||||||
|
return fmt.Errorf("group not found: %s", groupID)
|
||||||
|
}
|
||||||
|
s.mu.Unlock()
|
||||||
|
|
||||||
|
for _, pid := range processIDs {
|
||||||
|
if err := s.StopProcess(pid); err != nil {
|
||||||
|
return fmt.Errorf("failed to stop process %s in group %s: %w", pid, groupID, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetGroupProcesses returns all processes in a group
|
||||||
|
func (s *Server) GetGroupProcesses(groupID string) ([]*Process, error) {
|
||||||
|
s.mu.RLock()
|
||||||
|
processIDs, exists := s.groups[groupID]
|
||||||
|
if !exists {
|
||||||
|
s.mu.RUnlock()
|
||||||
|
return nil, fmt.Errorf("group not found: %s", groupID)
|
||||||
|
}
|
||||||
|
|
||||||
|
processes := make([]*Process, 0, len(processIDs))
|
||||||
|
for _, pid := range processIDs {
|
||||||
|
if process, exists := s.processes[pid]; exists {
|
||||||
|
processes = append(processes, process)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s.mu.RUnlock()
|
||||||
|
|
||||||
|
return processes, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListGroups returns all group IDs
|
||||||
|
func (s *Server) ListGroups() []string {
|
||||||
|
s.mu.RLock()
|
||||||
|
defer s.mu.RUnlock()
|
||||||
|
|
||||||
|
groups := make([]string, 0, len(s.groups))
|
||||||
|
for groupID := range s.groups {
|
||||||
|
groups = append(groups, groupID)
|
||||||
|
}
|
||||||
|
return groups
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetProcess returns a process by ID
|
||||||
|
func (s *Server) GetProcess(id string) (*Process, error) {
|
||||||
|
s.mu.RLock()
|
||||||
|
process, exists := s.processes[id]
|
||||||
|
s.mu.RUnlock()
|
||||||
|
|
||||||
|
if !exists {
|
||||||
|
return nil, fmt.Errorf("process not found: %s", id)
|
||||||
|
}
|
||||||
|
|
||||||
|
return process, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListProcesses returns all running processes
|
||||||
|
func (s *Server) ListProcesses() []*Process {
|
||||||
|
s.mu.RLock()
|
||||||
|
defer s.mu.RUnlock()
|
||||||
|
|
||||||
|
processes := make([]*Process, 0, len(s.processes))
|
||||||
|
for _, p := range s.processes {
|
||||||
|
processes = append(processes, p)
|
||||||
|
}
|
||||||
|
|
||||||
|
return processes
|
||||||
|
}
|
||||||
|
|
||||||
|
// RunProcess executes a command and returns its output
|
||||||
|
func (s *Server) RunProcess(ctx context.Context, command string, args []string, env []string) (string, error) {
|
||||||
|
cmd := exec.CommandContext(ctx, command, args...)
|
||||||
|
|
||||||
|
if len(env) > 0 {
|
||||||
|
cmd.Env = append(os.Environ(), env...)
|
||||||
|
}
|
||||||
|
|
||||||
|
output, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
return string(output), fmt.Errorf("process failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(output), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start starts the HTTP server
|
||||||
|
func (s *Server) Start(addr string) error {
|
||||||
|
http.HandleFunc("/processes", s.handleProcesses)
|
||||||
|
http.HandleFunc("/processes/", s.handleProcess)
|
||||||
|
http.HandleFunc("/ws/", s.handleWebSocket)
|
||||||
|
http.HandleFunc("/groups", s.handleGroups)
|
||||||
|
http.HandleFunc("/groups/", s.handleGroup)
|
||||||
|
http.HandleFunc("/run", s.handleRun)
|
||||||
|
|
||||||
|
return http.ListenAndServe(addr, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleProcesses(w http.ResponseWriter, r *http.Request) {
|
||||||
|
log.Printf("Handling /processes request: method=%s", r.Method)
|
||||||
|
|
||||||
|
switch r.Method {
|
||||||
|
case http.MethodGet:
|
||||||
|
processes := s.ListProcesses()
|
||||||
|
json.NewEncoder(w).Encode(processes)
|
||||||
|
case http.MethodPost:
|
||||||
|
var req struct {
|
||||||
|
Command string `json:"command"`
|
||||||
|
Args []string `json:"args"`
|
||||||
|
Env []string `json:"env"`
|
||||||
|
GroupID string `json:"group_id"`
|
||||||
|
}
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
id, err := s.StartProcess(context.Background(), req.Command, req.Args, req.Env, req.GroupID)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(map[string]string{"id": id})
|
||||||
|
default:
|
||||||
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleProcess(w http.ResponseWriter, r *http.Request) {
|
||||||
|
id := r.URL.Path[len("/processes/"):]
|
||||||
|
|
||||||
|
switch r.Method {
|
||||||
|
case http.MethodGet:
|
||||||
|
process, err := s.GetProcess(id)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
json.NewEncoder(w).Encode(process)
|
||||||
|
case http.MethodDelete:
|
||||||
|
if err := s.StopProcess(id); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
default:
|
||||||
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleWebSocket(w http.ResponseWriter, r *http.Request) {
|
||||||
|
id := r.URL.Path[len("/ws/"):]
|
||||||
|
xlog.Debug("Handling WebSocket connection", "processID", id)
|
||||||
|
|
||||||
|
process, err := s.GetProcess(id)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if process.Cmd.ProcessState != nil && process.Cmd.ProcessState.Exited() {
|
||||||
|
xlog.Debug("Process already exited", "processID", id)
|
||||||
|
http.Error(w, "Process already exited", http.StatusGone)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
xlog.Debug("Process is running", "processID", id, "pid", process.Cmd.Process.Pid)
|
||||||
|
|
||||||
|
conn, err := s.upgrader.Upgrade(w, r, nil)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
xlog.Debug("WebSocket connection established", "processID", id)
|
||||||
|
|
||||||
|
// Create a done channel to signal process completion
|
||||||
|
done := make(chan struct{})
|
||||||
|
|
||||||
|
// Handle stdin
|
||||||
|
go func() {
|
||||||
|
defer func() {
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
xlog.Debug("Process stdin handler done", "processID", id)
|
||||||
|
default:
|
||||||
|
xlog.Debug("WebSocket stdin connection closed", "processID", id)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
for {
|
||||||
|
_, message, err := conn.ReadMessage()
|
||||||
|
if err != nil {
|
||||||
|
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseNormalClosure) {
|
||||||
|
xlog.Debug("WebSocket stdin unexpected error", "processID", id, "error", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
xlog.Debug("Received message", "processID", id, "message", string(message))
|
||||||
|
if _, err := process.Stdin.Write(message); err != nil {
|
||||||
|
if err != io.EOF {
|
||||||
|
xlog.Debug("WebSocket stdin write error", "processID", id, "error", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
xlog.Debug("Message sent to process", "processID", id, "message", string(message))
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Handle stdout and stderr
|
||||||
|
go func() {
|
||||||
|
defer func() {
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
xlog.Debug("Process output handler done", "processID", id)
|
||||||
|
default:
|
||||||
|
xlog.Debug("WebSocket output connection closed", "processID", id)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Create a buffer for reading
|
||||||
|
buf := make([]byte, 4096)
|
||||||
|
reader := io.MultiReader(process.Stdout, process.Stderr)
|
||||||
|
|
||||||
|
for {
|
||||||
|
n, err := reader.Read(buf)
|
||||||
|
if err != nil {
|
||||||
|
if err != io.EOF {
|
||||||
|
xlog.Debug("Read error", "processID", id, "error", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if n > 0 {
|
||||||
|
xlog.Debug("Sending message", "processID", id, "size", n)
|
||||||
|
if err := conn.WriteMessage(websocket.BinaryMessage, buf[:n]); err != nil {
|
||||||
|
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseNormalClosure) {
|
||||||
|
xlog.Debug("WebSocket output write error", "processID", id, "error", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
xlog.Debug("Message sent to client", "processID", id, "size", n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Wait for process to exit
|
||||||
|
xlog.Debug("Waiting for process to exit", "processID", id)
|
||||||
|
err = process.Cmd.Wait()
|
||||||
|
close(done) // Signal that the process is done
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
xlog.Debug("Process exited with error",
|
||||||
|
"processID", id,
|
||||||
|
"pid", process.Cmd.Process.Pid,
|
||||||
|
"error", err)
|
||||||
|
} else {
|
||||||
|
xlog.Debug("Process exited successfully",
|
||||||
|
"processID", id,
|
||||||
|
"pid", process.Cmd.Process.Pid)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add new handlers for group management
|
||||||
|
func (s *Server) handleGroups(w http.ResponseWriter, r *http.Request) {
|
||||||
|
switch r.Method {
|
||||||
|
case http.MethodGet:
|
||||||
|
groups := s.ListGroups()
|
||||||
|
json.NewEncoder(w).Encode(groups)
|
||||||
|
default:
|
||||||
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleGroup(w http.ResponseWriter, r *http.Request) {
|
||||||
|
groupID := r.URL.Path[len("/groups/"):]
|
||||||
|
|
||||||
|
switch r.Method {
|
||||||
|
case http.MethodGet:
|
||||||
|
processes, err := s.GetGroupProcesses(groupID)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
json.NewEncoder(w).Encode(processes)
|
||||||
|
case http.MethodDelete:
|
||||||
|
if err := s.StopGroup(groupID); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
default:
|
||||||
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleRun(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Handling /run request")
|
||||||
|
|
||||||
|
var req struct {
|
||||||
|
Command string `json:"command"`
|
||||||
|
Args []string `json:"args"`
|
||||||
|
Env []string `json:"env"`
|
||||||
|
}
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Executing one-time process: command=%s, args=%v", req.Command, req.Args)
|
||||||
|
|
||||||
|
output, err := s.RunProcess(r.Context(), req.Command, req.Args, req.Env)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("One-time process completed with output length: %d", len(output))
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(map[string]string{
|
||||||
|
"output": output,
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -10,6 +10,10 @@ import (
|
|||||||
"github.com/sashabaranov/go-openai/jsonschema"
|
"github.com/sashabaranov/go-openai/jsonschema"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
MetadataBrowserAgentHistory = "browser_agent_history"
|
||||||
|
)
|
||||||
|
|
||||||
type BrowserAgentRunner struct {
|
type BrowserAgentRunner struct {
|
||||||
baseURL, customActionName string
|
baseURL, customActionName string
|
||||||
client *api.Client
|
client *api.Client
|
||||||
@@ -62,7 +66,7 @@ func (b *BrowserAgentRunner) Run(ctx context.Context, params types.ActionParams)
|
|||||||
|
|
||||||
return types.ActionResult{
|
return types.ActionResult{
|
||||||
Result: fmt.Sprintf("Browser agent completed successfully. History:\n%s", historyStr),
|
Result: fmt.Sprintf("Browser agent completed successfully. History:\n%s", historyStr),
|
||||||
Metadata: map[string]interface{}{"browser_agent_history": stateHistory},
|
Metadata: map[string]interface{}{MetadataBrowserAgentHistory: stateHistory},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -30,9 +30,15 @@ func NewDiscord(config map[string]string) *Discord {
|
|||||||
duration = 5 * time.Minute
|
duration = 5 * time.Minute
|
||||||
}
|
}
|
||||||
|
|
||||||
|
token := config["token"]
|
||||||
|
|
||||||
|
if !strings.HasPrefix(token, "Bot ") {
|
||||||
|
token = "Bot " + token
|
||||||
|
}
|
||||||
|
|
||||||
return &Discord{
|
return &Discord{
|
||||||
conversationTracker: NewConversationTracker[string](duration),
|
conversationTracker: NewConversationTracker[string](duration),
|
||||||
token: config["token"],
|
token: token,
|
||||||
defaultChannel: config["defaultChannel"],
|
defaultChannel: config["defaultChannel"],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -77,8 +77,9 @@ func (i *IRC) Start(a *agent.Agent) {
|
|||||||
}
|
}
|
||||||
i.conn.UseTLS = false
|
i.conn.UseTLS = false
|
||||||
i.conn.AddCallback("001", func(e *irc.Event) {
|
i.conn.AddCallback("001", func(e *irc.Event) {
|
||||||
xlog.Info("Connected to IRC server", "server", i.server)
|
xlog.Info("Connected to IRC server", "server", i.server, "arguments", e.Arguments)
|
||||||
i.conn.Join(i.channel)
|
i.conn.Join(i.channel)
|
||||||
|
i.nickname = e.Arguments[0]
|
||||||
xlog.Info("Joined channel", "channel", i.channel)
|
xlog.Info("Joined channel", "channel", i.channel)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -207,6 +208,13 @@ func (i *IRC) Start(a *agent.Agent) {
|
|||||||
|
|
||||||
// Start the IRC client in a goroutine
|
// Start the IRC client in a goroutine
|
||||||
go i.conn.Loop()
|
go i.conn.Loop()
|
||||||
|
go func() {
|
||||||
|
select {
|
||||||
|
case <-a.Context().Done():
|
||||||
|
i.conn.Quit()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
// IRCConfigMeta returns the metadata for IRC connector configuration fields
|
// IRCConfigMeta returns the metadata for IRC connector configuration fields
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/mudler/LocalAGI/pkg/config"
|
"github.com/mudler/LocalAGI/pkg/config"
|
||||||
|
"github.com/mudler/LocalAGI/pkg/localoperator"
|
||||||
"github.com/mudler/LocalAGI/pkg/xlog"
|
"github.com/mudler/LocalAGI/pkg/xlog"
|
||||||
"github.com/mudler/LocalAGI/pkg/xstrings"
|
"github.com/mudler/LocalAGI/pkg/xstrings"
|
||||||
"github.com/mudler/LocalAGI/services/actions"
|
"github.com/mudler/LocalAGI/services/actions"
|
||||||
@@ -167,8 +168,38 @@ func replaceUserIDsWithNamesInMessage(api *slack.Client, message string) string
|
|||||||
return message
|
return message
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateAttachmentsFromJobResponse(j *types.JobResult) (attachments []slack.Attachment) {
|
func generateAttachmentsFromJobResponse(j *types.JobResult, api *slack.Client, channelID, ts string) (attachments []slack.Attachment) {
|
||||||
for _, state := range j.State {
|
for _, state := range j.State {
|
||||||
|
// coming from the browser agent
|
||||||
|
if history, exists := state.Metadata[actions.MetadataBrowserAgentHistory]; exists {
|
||||||
|
if historyStruct, ok := history.(*localoperator.StateHistory); ok {
|
||||||
|
state := historyStruct.States[len(historyStruct.States)-1]
|
||||||
|
// Decode base64 screenshot and upload to Slack
|
||||||
|
if state.Screenshot != "" {
|
||||||
|
screenshotData, err := base64.StdEncoding.DecodeString(state.Screenshot)
|
||||||
|
if err != nil {
|
||||||
|
xlog.Error(fmt.Sprintf("Error decoding screenshot: %v", err))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
data := string(screenshotData)
|
||||||
|
// Upload the file to Slack
|
||||||
|
_, err = api.UploadFileV2(slack.UploadFileV2Parameters{
|
||||||
|
Reader: bytes.NewReader(screenshotData),
|
||||||
|
FileSize: len(data),
|
||||||
|
ThreadTimestamp: ts,
|
||||||
|
Channel: channelID,
|
||||||
|
Filename: "screenshot.png",
|
||||||
|
InitialComment: "Browser Agent Screenshot",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
xlog.Error(fmt.Sprintf("Error uploading screenshot: %v", err))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// coming from the search action
|
// coming from the search action
|
||||||
if urls, exists := state.Metadata[actions.MetadataUrls]; exists {
|
if urls, exists := state.Metadata[actions.MetadataUrls]; exists {
|
||||||
for _, url := range xstrings.UniqueSlice(urls.([]string)) {
|
for _, url := range xstrings.UniqueSlice(urls.([]string)) {
|
||||||
@@ -375,7 +406,7 @@ func replyWithPostMessage(finalResponse string, api *slack.Client, ev *slackeven
|
|||||||
slack.MsgOptionEnableLinkUnfurl(),
|
slack.MsgOptionEnableLinkUnfurl(),
|
||||||
slack.MsgOptionText(message, true),
|
slack.MsgOptionText(message, true),
|
||||||
slack.MsgOptionPostMessageParameters(postMessageParams),
|
slack.MsgOptionPostMessageParameters(postMessageParams),
|
||||||
slack.MsgOptionAttachments(generateAttachmentsFromJobResponse(res)...),
|
slack.MsgOptionAttachments(generateAttachmentsFromJobResponse(res, api, ev.Channel, "")...),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
xlog.Error(fmt.Sprintf("Error posting message: %v", err))
|
xlog.Error(fmt.Sprintf("Error posting message: %v", err))
|
||||||
@@ -387,7 +418,7 @@ func replyWithPostMessage(finalResponse string, api *slack.Client, ev *slackeven
|
|||||||
slack.MsgOptionEnableLinkUnfurl(),
|
slack.MsgOptionEnableLinkUnfurl(),
|
||||||
slack.MsgOptionText(res.Response, true),
|
slack.MsgOptionText(res.Response, true),
|
||||||
slack.MsgOptionPostMessageParameters(postMessageParams),
|
slack.MsgOptionPostMessageParameters(postMessageParams),
|
||||||
slack.MsgOptionAttachments(generateAttachmentsFromJobResponse(res)...),
|
slack.MsgOptionAttachments(generateAttachmentsFromJobResponse(res, api, ev.Channel, "")...),
|
||||||
// slack.MsgOptionTS(ts),
|
// slack.MsgOptionTS(ts),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -408,7 +439,7 @@ func replyToUpdateMessage(finalResponse string, api *slack.Client, ev *slackeven
|
|||||||
slack.MsgOptionLinkNames(true),
|
slack.MsgOptionLinkNames(true),
|
||||||
slack.MsgOptionEnableLinkUnfurl(),
|
slack.MsgOptionEnableLinkUnfurl(),
|
||||||
slack.MsgOptionText(messages[0], true),
|
slack.MsgOptionText(messages[0], true),
|
||||||
slack.MsgOptionAttachments(generateAttachmentsFromJobResponse(res)...),
|
slack.MsgOptionAttachments(generateAttachmentsFromJobResponse(res, api, ev.Channel, msgTs)...),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
xlog.Error(fmt.Sprintf("Error updating final message: %v", err))
|
xlog.Error(fmt.Sprintf("Error updating final message: %v", err))
|
||||||
@@ -435,7 +466,7 @@ func replyToUpdateMessage(finalResponse string, api *slack.Client, ev *slackeven
|
|||||||
slack.MsgOptionLinkNames(true),
|
slack.MsgOptionLinkNames(true),
|
||||||
slack.MsgOptionEnableLinkUnfurl(),
|
slack.MsgOptionEnableLinkUnfurl(),
|
||||||
slack.MsgOptionText(finalResponse, true),
|
slack.MsgOptionText(finalResponse, true),
|
||||||
slack.MsgOptionAttachments(generateAttachmentsFromJobResponse(res)...),
|
slack.MsgOptionAttachments(generateAttachmentsFromJobResponse(res, api, ev.Channel, msgTs)...),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
xlog.Error(fmt.Sprintf("Error updating final message: %v", err))
|
xlog.Error(fmt.Sprintf("Error updating final message: %v", err))
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ oauth_config:
|
|||||||
- commands
|
- commands
|
||||||
- groups:history
|
- groups:history
|
||||||
- files:read
|
- files:read
|
||||||
|
- files:write
|
||||||
- im:history
|
- im:history
|
||||||
- im:read
|
- im:read
|
||||||
- im:write
|
- im:write
|
||||||
|
|||||||
36
webui/app.go
36
webui/app.go
@@ -176,17 +176,7 @@ func (a *App) UpdateAgentConfig(pool *state.AgentPool) func(c *fiber.Ctx) error
|
|||||||
return errorJSONMessage(c, err.Error())
|
return errorJSONMessage(c, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove the agent first
|
if err := pool.RecreateAgent(agentName, &newConfig); err != nil {
|
||||||
if err := pool.Remove(agentName); err != nil {
|
|
||||||
return errorJSONMessage(c, "Error removing agent: "+err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create agent with new config
|
|
||||||
if err := pool.CreateAgent(agentName, &newConfig); err != nil {
|
|
||||||
// Try to restore the old configuration if update fails
|
|
||||||
if restoreErr := pool.CreateAgent(agentName, oldConfig); restoreErr != nil {
|
|
||||||
return errorJSONMessage(c, fmt.Sprintf("Failed to update agent and restore failed: %v, %v", err, restoreErr))
|
|
||||||
}
|
|
||||||
return errorJSONMessage(c, "Error updating agent: "+err.Error())
|
return errorJSONMessage(c, "Error updating agent: "+err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -429,6 +419,30 @@ func (a *App) Chat(pool *state.AgentPool) func(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *App) GetActionDefinition(pool *state.AgentPool) func(c *fiber.Ctx) error {
|
||||||
|
return func(c *fiber.Ctx) error {
|
||||||
|
payload := struct {
|
||||||
|
Config map[string]string `json:"config"`
|
||||||
|
}{}
|
||||||
|
|
||||||
|
if err := c.BodyParser(&payload); err != nil {
|
||||||
|
xlog.Error("Error parsing action payload", "error", err)
|
||||||
|
return errorJSONMessage(c, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
actionName := c.Params("name")
|
||||||
|
|
||||||
|
xlog.Debug("Executing action", "action", actionName, "config", payload.Config)
|
||||||
|
a, err := services.Action(actionName, "", payload.Config, pool, map[string]string{})
|
||||||
|
if err != nil {
|
||||||
|
xlog.Error("Error creating action", "error", err)
|
||||||
|
return errorJSONMessage(c, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.JSON(a.Definition())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (a *App) ExecuteAction(pool *state.AgentPool) func(c *fiber.Ctx) error {
|
func (a *App) ExecuteAction(pool *state.AgentPool) func(c *fiber.Ctx) error {
|
||||||
return func(c *fiber.Ctx) error {
|
return func(c *fiber.Ctx) error {
|
||||||
payload := struct {
|
payload := struct {
|
||||||
|
|||||||
103
webui/react-ui/src/components/CollapsibleRawSections.jsx
Normal file
103
webui/react-ui/src/components/CollapsibleRawSections.jsx
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
import React, { useState } from 'react';
|
||||||
|
import hljs from 'highlight.js/lib/core';
|
||||||
|
import json from 'highlight.js/lib/languages/json';
|
||||||
|
import 'highlight.js/styles/monokai.css';
|
||||||
|
|
||||||
|
hljs.registerLanguage('json', json);
|
||||||
|
|
||||||
|
export default function CollapsibleRawSections({ container }) {
|
||||||
|
const [showCreation, setShowCreation] = useState(false);
|
||||||
|
const [showProgress, setShowProgress] = useState(false);
|
||||||
|
const [showCompletion, setShowCompletion] = useState(false);
|
||||||
|
const [copied, setCopied] = useState({ creation: false, progress: false, completion: false });
|
||||||
|
|
||||||
|
const handleCopy = (section, data) => {
|
||||||
|
navigator.clipboard.writeText(JSON.stringify(data, null, 2));
|
||||||
|
setCopied(prev => ({ ...prev, [section]: true }));
|
||||||
|
setTimeout(() => setCopied(prev => ({ ...prev, [section]: false })), 1200);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
{/* Creation Section */}
|
||||||
|
{container.creation && (
|
||||||
|
<div>
|
||||||
|
<h4 style={{ display: 'flex', alignItems: 'center' }}>
|
||||||
|
<span
|
||||||
|
style={{ cursor: 'pointer', display: 'flex', alignItems: 'center', flex: 1 }}
|
||||||
|
onClick={() => setShowCreation(v => !v)}
|
||||||
|
>
|
||||||
|
<i className={`fas fa-chevron-${showCreation ? 'down' : 'right'}`} style={{ marginRight: 6 }} />
|
||||||
|
Creation
|
||||||
|
</span>
|
||||||
|
<button
|
||||||
|
title="Copy Creation JSON"
|
||||||
|
onClick={e => { e.stopPropagation(); handleCopy('creation', container.creation); }}
|
||||||
|
style={{ marginLeft: 8, border: 'none', background: 'none', cursor: 'pointer', color: '#ccc' }}
|
||||||
|
>
|
||||||
|
{copied.creation ? <span style={{ color: '#6f6' }}>Copied!</span> : <i className="fas fa-copy" />}
|
||||||
|
</button>
|
||||||
|
</h4>
|
||||||
|
{showCreation && (
|
||||||
|
<pre className="hljs"><code>
|
||||||
|
<div dangerouslySetInnerHTML={{ __html: hljs.highlight(JSON.stringify(container.creation || {}, null, 2), { language: 'json' }).value }} />
|
||||||
|
</code></pre>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{/* Progress Section */}
|
||||||
|
{container.progress && container.progress.length > 0 && (
|
||||||
|
<div>
|
||||||
|
<h4 style={{ display: 'flex', alignItems: 'center' }}>
|
||||||
|
<span
|
||||||
|
style={{ cursor: 'pointer', display: 'flex', alignItems: 'center', flex: 1 }}
|
||||||
|
onClick={() => setShowProgress(v => !v)}
|
||||||
|
>
|
||||||
|
<i className={`fas fa-chevron-${showProgress ? 'down' : 'right'}`} style={{ marginRight: 6 }} />
|
||||||
|
Progress
|
||||||
|
</span>
|
||||||
|
<button
|
||||||
|
title="Copy Progress JSON"
|
||||||
|
onClick={e => { e.stopPropagation(); handleCopy('progress', container.progress); }}
|
||||||
|
style={{ marginLeft: 8, border: 'none', background: 'none', cursor: 'pointer', color: '#ccc' }}
|
||||||
|
>
|
||||||
|
{copied.progress ? <span style={{ color: '#6f6' }}>Copied!</span> : <i className="fas fa-copy" />}
|
||||||
|
</button>
|
||||||
|
</h4>
|
||||||
|
{showProgress && (
|
||||||
|
<pre className="hljs"><code>
|
||||||
|
<div dangerouslySetInnerHTML={{ __html: hljs.highlight(JSON.stringify(container.progress || {}, null, 2), { language: 'json' }).value }} />
|
||||||
|
</code></pre>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{/* Completion Section */}
|
||||||
|
{container.completion && (
|
||||||
|
<div>
|
||||||
|
<h4 style={{ display: 'flex', alignItems: 'center' }}>
|
||||||
|
<span
|
||||||
|
style={{ cursor: 'pointer', display: 'flex', alignItems: 'center', flex: 1 }}
|
||||||
|
onClick={() => setShowCompletion(v => !v)}
|
||||||
|
>
|
||||||
|
<i className={`fas fa-chevron-${showCompletion ? 'down' : 'right'}`} style={{ marginRight: 6 }} />
|
||||||
|
Completion
|
||||||
|
</span>
|
||||||
|
<button
|
||||||
|
title="Copy Completion JSON"
|
||||||
|
onClick={e => { e.stopPropagation(); handleCopy('completion', container.completion); }}
|
||||||
|
style={{ marginLeft: 8, border: 'none', background: 'none', cursor: 'pointer', color: '#ccc' }}
|
||||||
|
>
|
||||||
|
{copied.completion ? <span style={{ color: '#6f6' }}>Copied!</span> : <i className="fas fa-copy" />}
|
||||||
|
</button>
|
||||||
|
</h4>
|
||||||
|
{showCompletion && (
|
||||||
|
<pre className="hljs"><code>
|
||||||
|
<div dangerouslySetInnerHTML={{ __html: hljs.highlight(JSON.stringify(container.completion || {}, null, 2), { language: 'json' }).value }} />
|
||||||
|
</code></pre>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,6 +1,11 @@
|
|||||||
import { useState, useEffect } from 'react';
|
import { useState, useEffect } from 'react';
|
||||||
import { useOutletContext, useNavigate } from 'react-router-dom';
|
import { useOutletContext, useNavigate } from 'react-router-dom';
|
||||||
import { actionApi } from '../utils/api';
|
import { actionApi, agentApi } from '../utils/api';
|
||||||
|
import FormFieldDefinition from '../components/common/FormFieldDefinition';
|
||||||
|
import hljs from 'highlight.js/lib/core';
|
||||||
|
import json from 'highlight.js/lib/languages/json';
|
||||||
|
import 'highlight.js/styles/monokai.css';
|
||||||
|
hljs.registerLanguage('json', json);
|
||||||
|
|
||||||
function ActionsPlayground() {
|
function ActionsPlayground() {
|
||||||
const { showToast } = useOutletContext();
|
const { showToast } = useOutletContext();
|
||||||
@@ -12,6 +17,10 @@ function ActionsPlayground() {
|
|||||||
const [result, setResult] = useState(null);
|
const [result, setResult] = useState(null);
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const [loadingActions, setLoadingActions] = useState(true);
|
const [loadingActions, setLoadingActions] = useState(true);
|
||||||
|
const [actionMetadata, setActionMetadata] = useState(null);
|
||||||
|
const [agentMetadata, setAgentMetadata] = useState(null);
|
||||||
|
const [configFields, setConfigFields] = useState([]);
|
||||||
|
const [paramFields, setParamFields] = useState([]);
|
||||||
|
|
||||||
// Update document title
|
// Update document title
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -36,21 +45,106 @@ function ActionsPlayground() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
fetchActions();
|
fetchActions();
|
||||||
}, [showToast]);
|
}, []);
|
||||||
|
|
||||||
|
// Fetch agent metadata on mount
|
||||||
|
useEffect(() => {
|
||||||
|
const fetchAgentMetadata = async () => {
|
||||||
|
try {
|
||||||
|
const metadata = await agentApi.getAgentConfigMetadata();
|
||||||
|
setAgentMetadata(metadata);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error fetching agent metadata:', err);
|
||||||
|
showToast('Failed to load agent metadata', 'error');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
fetchAgentMetadata();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
// Fetch action definition when action is selected or config changes
|
||||||
|
useEffect(() => {
|
||||||
|
if (!selectedAction) return;
|
||||||
|
|
||||||
|
const fetchActionDefinition = async () => {
|
||||||
|
try {
|
||||||
|
// Get config fields from agent metadata
|
||||||
|
const actionMeta = agentMetadata?.actions?.find(action => action.name === selectedAction);
|
||||||
|
const configFields = actionMeta?.fields || [];
|
||||||
|
console.debug('Config fields:', configFields);
|
||||||
|
setConfigFields(configFields);
|
||||||
|
|
||||||
|
// Parse current config to pass to action definition
|
||||||
|
let currentConfig = {};
|
||||||
|
try {
|
||||||
|
currentConfig = JSON.parse(configJson);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error parsing current config:', err);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get parameter fields from action definition
|
||||||
|
const paramFields = await actionApi.getActionDefinition(selectedAction, currentConfig);
|
||||||
|
console.debug('Parameter fields:', paramFields);
|
||||||
|
setParamFields(paramFields);
|
||||||
|
|
||||||
|
// Reset JSON to match the new fields
|
||||||
|
setConfigJson(JSON.stringify(currentConfig, null, 2));
|
||||||
|
setParamsJson(JSON.stringify({}, null, 2));
|
||||||
|
setResult(null);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error fetching action definition:', err);
|
||||||
|
showToast('Failed to load action definition', 'error');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
fetchActionDefinition();
|
||||||
|
}, [selectedAction, agentMetadata]);
|
||||||
|
|
||||||
// Handle action selection
|
// Handle action selection
|
||||||
const handleActionChange = (e) => {
|
const handleActionChange = (e) => {
|
||||||
setSelectedAction(e.target.value);
|
setSelectedAction(e.target.value);
|
||||||
|
setConfigJson('{}');
|
||||||
|
setParamsJson('{}');
|
||||||
setResult(null);
|
setResult(null);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Handle JSON input changes
|
// Helper to generate onChange handlers for form fields
|
||||||
const handleConfigChange = (e) => {
|
const makeFieldChangeHandler = (fields, updateFn) => (e) => {
|
||||||
setConfigJson(e.target.value);
|
let value;
|
||||||
|
if (e && e.target) {
|
||||||
|
const fieldName = e.target.name;
|
||||||
|
const fieldDef = fields.find(f => f.name === fieldName);
|
||||||
|
const fieldType = fieldDef ? fieldDef.type : undefined;
|
||||||
|
if (fieldType === 'checkbox') {
|
||||||
|
value = e.target.checked;
|
||||||
|
} else if (fieldType === 'number') {
|
||||||
|
value = e.target.value === '' ? '' : Number(e.target.value);
|
||||||
|
} else {
|
||||||
|
value = e.target.value;
|
||||||
|
}
|
||||||
|
updateFn(fieldName, value);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleParamsChange = (e) => {
|
// Handle form field changes
|
||||||
setParamsJson(e.target.value);
|
const handleConfigChange = (field, value) => {
|
||||||
|
try {
|
||||||
|
const config = JSON.parse(configJson);
|
||||||
|
config[field] = value;
|
||||||
|
setConfigJson(JSON.stringify(config, null, 2));
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error updating config:', err);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleParamsChange = (field, value) => {
|
||||||
|
try {
|
||||||
|
const params = JSON.parse(paramsJson);
|
||||||
|
params[field] = value;
|
||||||
|
setParamsJson(JSON.stringify(params, null, 2));
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error updating params:', err);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Execute the selected action
|
// Execute the selected action
|
||||||
@@ -135,34 +229,31 @@ function ActionsPlayground() {
|
|||||||
|
|
||||||
{selectedAction && (
|
{selectedAction && (
|
||||||
<div className="section-box">
|
<div className="section-box">
|
||||||
<h2>Action Configuration</h2>
|
|
||||||
|
|
||||||
<form onSubmit={handleExecuteAction}>
|
<form onSubmit={handleExecuteAction}>
|
||||||
<div className="form-group mb-6">
|
{configFields.length > 0 && (
|
||||||
<label htmlFor="config-json">Configuration (JSON):</label>
|
<>
|
||||||
<textarea
|
<h2>Configuration</h2>
|
||||||
id="config-json"
|
<FormFieldDefinition
|
||||||
value={configJson}
|
fields={configFields}
|
||||||
onChange={handleConfigChange}
|
values={JSON.parse(configJson)}
|
||||||
className="form-control"
|
onChange={makeFieldChangeHandler(configFields, handleConfigChange)}
|
||||||
rows="5"
|
idPrefix="config_"
|
||||||
placeholder='{"key": "value"}'
|
|
||||||
/>
|
/>
|
||||||
<p className="text-xs text-gray-400 mt-1">Enter JSON configuration for the action</p>
|
</>
|
||||||
</div>
|
)}
|
||||||
|
|
||||||
<div className="form-group mb-6">
|
{paramFields.length > 0 && (
|
||||||
<label htmlFor="params-json">Parameters (JSON):</label>
|
<>
|
||||||
<textarea
|
<h2>Parameters</h2>
|
||||||
id="params-json"
|
<FormFieldDefinition
|
||||||
value={paramsJson}
|
fields={paramFields}
|
||||||
onChange={handleParamsChange}
|
values={JSON.parse(paramsJson)}
|
||||||
className="form-control"
|
onChange={makeFieldChangeHandler(paramFields, handleParamsChange)}
|
||||||
rows="5"
|
idPrefix="param_"
|
||||||
placeholder='{"key": "value"}'
|
|
||||||
/>
|
/>
|
||||||
<p className="text-xs text-gray-400 mt-1">Enter JSON parameters for the action</p>
|
</>
|
||||||
</div>
|
)}
|
||||||
|
|
||||||
<div className="flex justify-end">
|
<div className="flex justify-end">
|
||||||
<button
|
<button
|
||||||
@@ -194,9 +285,9 @@ function ActionsPlayground() {
|
|||||||
backgroundColor: 'rgba(30, 30, 30, 0.7)'
|
backgroundColor: 'rgba(30, 30, 30, 0.7)'
|
||||||
}}>
|
}}>
|
||||||
{typeof result === 'object' ? (
|
{typeof result === 'object' ? (
|
||||||
<pre style={{ margin: 0, whiteSpace: 'pre-wrap', wordBreak: 'break-word' }}>
|
<pre className="hljs"><code>
|
||||||
{JSON.stringify(result, null, 2)}
|
<div dangerouslySetInnerHTML={{ __html: hljs.highlight(JSON.stringify(result, null, 2), { language: 'json' }).value }}></div>
|
||||||
</pre>
|
</code></pre>
|
||||||
) : (
|
) : (
|
||||||
<pre style={{ margin: 0, whiteSpace: 'pre-wrap', wordBreak: 'break-word' }}>
|
<pre style={{ margin: 0, whiteSpace: 'pre-wrap', wordBreak: 'break-word' }}>
|
||||||
{result}
|
{result}
|
||||||
|
|||||||
@@ -1,4 +1,181 @@
|
|||||||
import { useState, useEffect } from 'react';
|
import { useState, useEffect } from 'react';
|
||||||
|
import CollapsibleRawSections from '../components/CollapsibleRawSections';
|
||||||
|
|
||||||
|
function ObservableSummary({ observable }) {
|
||||||
|
// --- CREATION SUMMARIES ---
|
||||||
|
const creation = observable?.creation || {};
|
||||||
|
// ChatCompletionRequest summary
|
||||||
|
let creationChatMsg = '';
|
||||||
|
// Prefer chat_completion_message if present (for jobs/top-level containers)
|
||||||
|
if (creation?.chat_completion_message && creation.chat_completion_message.content) {
|
||||||
|
creationChatMsg = creation.chat_completion_message.content;
|
||||||
|
} else {
|
||||||
|
const messages = creation?.chat_completion_request?.messages;
|
||||||
|
if (Array.isArray(messages) && messages.length > 0) {
|
||||||
|
const lastMsg = messages[messages.length - 1];
|
||||||
|
creationChatMsg = lastMsg?.content || '';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// FunctionDefinition summary
|
||||||
|
let creationFunctionDef = '';
|
||||||
|
if (creation?.function_definition?.name) {
|
||||||
|
creationFunctionDef = `Function: ${creation.function_definition.name}`;
|
||||||
|
}
|
||||||
|
// FunctionParams summary
|
||||||
|
let creationFunctionParams = '';
|
||||||
|
if (creation?.function_params && Object.keys(creation.function_params).length > 0) {
|
||||||
|
creationFunctionParams = `Params: ${JSON.stringify(creation.function_params)}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- COMPLETION SUMMARIES ---
|
||||||
|
const completion = observable?.completion || {};
|
||||||
|
// ChatCompletionResponse summary
|
||||||
|
let completionChatMsg = '';
|
||||||
|
const chatCompletion = completion?.chat_completion_response;
|
||||||
|
if (
|
||||||
|
chatCompletion &&
|
||||||
|
Array.isArray(chatCompletion.choices) &&
|
||||||
|
chatCompletion.choices.length > 0
|
||||||
|
) {
|
||||||
|
const lastChoice = chatCompletion.choices[chatCompletion.choices.length - 1];
|
||||||
|
// Prefer tool_call summary if present
|
||||||
|
let toolCallSummary = '';
|
||||||
|
const toolCalls = lastChoice?.message?.tool_calls;
|
||||||
|
if (Array.isArray(toolCalls) && toolCalls.length > 0) {
|
||||||
|
toolCallSummary = toolCalls.map(tc => {
|
||||||
|
let args = '';
|
||||||
|
// For OpenAI-style, arguments are in tc.function.arguments, function name in tc.function.name
|
||||||
|
if (tc.function && tc.function.arguments) {
|
||||||
|
try {
|
||||||
|
args = typeof tc.function.arguments === 'string' ? tc.function.arguments : JSON.stringify(tc.function.arguments);
|
||||||
|
} catch (e) {
|
||||||
|
args = '[Unserializable arguments]';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const toolName = tc.function?.name || tc.name || 'unknown';
|
||||||
|
return `Tool call: ${toolName}(${args})`;
|
||||||
|
}).join('\n');
|
||||||
|
}
|
||||||
|
completionChatMsg = lastChoice?.message?.content || '';
|
||||||
|
// Attach toolCallSummary to completionChatMsg for rendering
|
||||||
|
if (toolCallSummary) {
|
||||||
|
completionChatMsg = { toolCallSummary, message: completionChatMsg };
|
||||||
|
}
|
||||||
|
// Else, it's just a string
|
||||||
|
|
||||||
|
}
|
||||||
|
// Conversation summary
|
||||||
|
let completionConversation = '';
|
||||||
|
if (Array.isArray(completion?.conversation) && completion.conversation.length > 0) {
|
||||||
|
const lastConv = completion.conversation[completion.conversation.length - 1];
|
||||||
|
completionConversation = lastConv?.content ? `${lastConv.content}` : '';
|
||||||
|
}
|
||||||
|
// ActionResult summary
|
||||||
|
let completionActionResult = '';
|
||||||
|
if (completion?.action_result) {
|
||||||
|
completionActionResult = `Action Result: ${String(completion.action_result).slice(0, 100)}`;
|
||||||
|
}
|
||||||
|
// AgentState summary
|
||||||
|
let completionAgentState = '';
|
||||||
|
if (completion?.agent_state) {
|
||||||
|
completionAgentState = `Agent State: ${JSON.stringify(completion.agent_state)}`;
|
||||||
|
}
|
||||||
|
// Error summary
|
||||||
|
let completionError = '';
|
||||||
|
if (completion?.error) {
|
||||||
|
completionError = `Error: ${completion.error}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only show if any summary is present
|
||||||
|
if (!creationChatMsg && !creationFunctionDef && !creationFunctionParams &&
|
||||||
|
!completionChatMsg && !completionConversation && !completionActionResult && !completionAgentState && !completionError) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div style={{ display: 'flex', flexDirection: 'column', gap: 2, margin: '2px 0 0 0' }}>
|
||||||
|
{/* CREATION */}
|
||||||
|
{creationChatMsg && (
|
||||||
|
<div title={creationChatMsg} style={{ display: 'flex', alignItems: 'center', color: '#cfc', fontSize: 14 }}>
|
||||||
|
<i className="fas fa-comment-dots" style={{ marginRight: 6, flex: '0 0 auto' }}></i>
|
||||||
|
<span style={{ overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', display: 'block' }}>{creationChatMsg}</span>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{creationFunctionDef && (
|
||||||
|
<div title={creationFunctionDef} style={{ display: 'flex', alignItems: 'center', color: '#cfc', fontSize: 14 }}>
|
||||||
|
<i className="fas fa-code" style={{ marginRight: 6, flex: '0 0 auto' }}></i>
|
||||||
|
<span style={{ overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', display: 'block' }}>{creationFunctionDef}</span>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{creationFunctionParams && (
|
||||||
|
<div title={creationFunctionParams} style={{ display: 'flex', alignItems: 'center', color: '#fc9', fontSize: 14 }}>
|
||||||
|
<i className="fas fa-sliders-h" style={{ marginRight: 6, flex: '0 0 auto' }}></i>
|
||||||
|
<span style={{ overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', display: 'block' }}>{creationFunctionParams}</span>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{/* COMPLETION */}
|
||||||
|
{/* COMPLETION: Tool call summary if present */}
|
||||||
|
{completionChatMsg && typeof completionChatMsg === 'object' && completionChatMsg.toolCallSummary && (
|
||||||
|
<div
|
||||||
|
title={completionChatMsg.toolCallSummary}
|
||||||
|
style={{
|
||||||
|
display: 'flex',
|
||||||
|
alignItems: 'center',
|
||||||
|
color: '#ffd966', // Distinct color for tool calls
|
||||||
|
fontSize: 14,
|
||||||
|
marginTop: 2,
|
||||||
|
whiteSpace: 'pre-line',
|
||||||
|
wordBreak: 'break-all',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<i className="fas fa-tools" style={{ marginRight: 6, flex: '0 0 auto' }}></i>
|
||||||
|
<span style={{ whiteSpace: 'pre-line', display: 'block' }}>{completionChatMsg.toolCallSummary}</span>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{/* COMPLETION: Message content if present */}
|
||||||
|
{completionChatMsg && ((typeof completionChatMsg === 'object' && completionChatMsg.message) || typeof completionChatMsg === 'string') && (
|
||||||
|
<div
|
||||||
|
title={typeof completionChatMsg === 'object' ? completionChatMsg.message : completionChatMsg}
|
||||||
|
style={{
|
||||||
|
display: 'flex',
|
||||||
|
alignItems: 'center',
|
||||||
|
color: '#8fc7ff',
|
||||||
|
fontSize: 14,
|
||||||
|
marginTop: 2,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<i className="fas fa-robot" style={{ marginRight: 6, flex: '0 0 auto' }}></i>
|
||||||
|
<span style={{ overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', display: 'block' }}>{typeof completionChatMsg === 'object' ? completionChatMsg.message : completionChatMsg}</span>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{completionConversation && (
|
||||||
|
<div title={completionConversation} style={{ display: 'flex', alignItems: 'center', color: '#b8e2ff', fontSize: 14 }}>
|
||||||
|
<i className="fas fa-comments" style={{ marginRight: 6, flex: '0 0 auto' }}></i>
|
||||||
|
<span style={{ overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', display: 'block' }}>{completionConversation}</span>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{completionActionResult && (
|
||||||
|
<div title={completionActionResult} style={{ display: 'flex', alignItems: 'center', color: '#ffd700', fontSize: 14 }}>
|
||||||
|
<i className="fas fa-bolt" style={{ marginRight: 6, flex: '0 0 auto' }}></i>
|
||||||
|
<span style={{ overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', display: 'block' }}>{completionActionResult}</span>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{completionAgentState && (
|
||||||
|
<div title={completionAgentState} style={{ display: 'flex', alignItems: 'center', color: '#ffb8b8', fontSize: 14 }}>
|
||||||
|
<i className="fas fa-brain" style={{ marginRight: 6, flex: '0 0 auto' }}></i>
|
||||||
|
<span style={{ overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', display: 'block' }}>{completionAgentState}</span>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{completionError && (
|
||||||
|
<div title={completionError} style={{ display: 'flex', alignItems: 'center', color: '#f66', fontSize: 14 }}>
|
||||||
|
<i className="fas fa-exclamation-triangle" style={{ marginRight: 6, flex: '0 0 auto' }}></i>
|
||||||
|
<span style={{ overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', display: 'block' }}>{completionError}</span>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
import { useParams, Link } from 'react-router-dom';
|
import { useParams, Link } from 'react-router-dom';
|
||||||
import hljs from 'highlight.js/lib/core';
|
import hljs from 'highlight.js/lib/core';
|
||||||
import json from 'highlight.js/lib/languages/json';
|
import json from 'highlight.js/lib/languages/json';
|
||||||
@@ -7,7 +184,7 @@ import 'highlight.js/styles/monokai.css';
|
|||||||
hljs.registerLanguage('json', json);
|
hljs.registerLanguage('json', json);
|
||||||
|
|
||||||
function AgentStatus() {
|
function AgentStatus() {
|
||||||
const [showStatus, setShowStatus] = useState(true);
|
const [showStatus, setShowStatus] = useState(false);
|
||||||
const { name } = useParams();
|
const { name } = useParams();
|
||||||
const [statusData, setStatusData] = useState(null);
|
const [statusData, setStatusData] = useState(null);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
@@ -94,12 +271,20 @@ function AgentStatus() {
|
|||||||
setObservableMap(prevMap => {
|
setObservableMap(prevMap => {
|
||||||
const prev = prevMap[data.id] || {};
|
const prev = prevMap[data.id] || {};
|
||||||
const updated = {
|
const updated = {
|
||||||
...prev,
|
|
||||||
...data,
|
...data,
|
||||||
creation: data.creation,
|
...prev,
|
||||||
progress: data.progress,
|
};
|
||||||
completion: data.completion,
|
// Events can be received out of order
|
||||||
// children are always built client-side
|
if (data.creation)
|
||||||
|
updated.creation = data.creation;
|
||||||
|
if (data.completion)
|
||||||
|
updated.completion = data.completion;
|
||||||
|
if ((data.progress?.length ?? 0) > (prev.progress?.length ?? 0))
|
||||||
|
updated.progress = data.progress;
|
||||||
|
if (data.parent_id && !prevMap[data.parent_id])
|
||||||
|
prevMap[data.parent_id] = {
|
||||||
|
id: data.parent_id,
|
||||||
|
name: "unknown",
|
||||||
};
|
};
|
||||||
const newMap = { ...prevMap, [data.id]: updated };
|
const newMap = { ...prevMap, [data.id]: updated };
|
||||||
setObservableTree(buildObservableTree(newMap));
|
setObservableTree(buildObservableTree(newMap));
|
||||||
@@ -243,11 +428,16 @@ function AgentStatus() {
|
|||||||
setExpandedCards(new Map(expandedCards).set(container.id, newExpanded));
|
setExpandedCards(new Map(expandedCards).set(container.id, newExpanded));
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<div style={{ display: 'flex', gap: '10px', alignItems: 'center' }}>
|
<div style={{ display: 'flex', gap: '10px', alignItems: 'center', maxWidth: '90%' }}>
|
||||||
<i className={`fas fa-${container.icon || 'robot'}`} style={{ verticalAlign: '-0.125em' }}></i>
|
<i className={`fas fa-${container.icon || 'robot'}`} style={{ verticalAlign: '-0.125em' }}></i>
|
||||||
|
<span style={{ width: '100%' }}>
|
||||||
|
<div style={{ display: 'flex', flexDirection: 'column', flex: 1 }}>
|
||||||
<span>
|
<span>
|
||||||
<span className='stat-label'>{container.name}</span>#<span className='stat-label'>{container.id}</span>
|
<span className='stat-label'>{container.name}</span>#<span className='stat-label'>{container.id}</span>
|
||||||
</span>
|
</span>
|
||||||
|
<ObservableSummary observable={container} />
|
||||||
|
</div>
|
||||||
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<div style={{ display: 'flex', alignItems: 'center', gap: '8px' }}>
|
<div style={{ display: 'flex', alignItems: 'center', gap: '8px' }}>
|
||||||
<i
|
<i
|
||||||
@@ -270,17 +460,22 @@ function AgentStatus() {
|
|||||||
const isExpanded = expandedCards.get(childKey);
|
const isExpanded = expandedCards.get(childKey);
|
||||||
return (
|
return (
|
||||||
<div key={`${container.id}-child-${child.id}`} className='card' style={{ background: '#222', marginBottom: '0.5em' }}>
|
<div key={`${container.id}-child-${child.id}`} className='card' style={{ background: '#222', marginBottom: '0.5em' }}>
|
||||||
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', cursor: 'pointer' }}
|
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', cursor: 'hand', maxWidth: '100%' }}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
const newExpanded = !expandedCards.get(childKey);
|
const newExpanded = !expandedCards.get(childKey);
|
||||||
setExpandedCards(new Map(expandedCards).set(childKey, newExpanded));
|
setExpandedCards(new Map(expandedCards).set(childKey, newExpanded));
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<div style={{ display: 'flex', gap: '10px', alignItems: 'center' }}>
|
<div style={{ display: 'flex', maxWidth: '90%', gap: '10px', alignItems: 'center' }}>
|
||||||
<i className={`fas fa-${child.icon || 'robot'}`} style={{ verticalAlign: '-0.125em' }}></i>
|
<i className={`fas fa-${child.icon || 'robot'}`} style={{ verticalAlign: '-0.125em' }}></i>
|
||||||
|
<span style={{ width: '100%' }}>
|
||||||
|
<div style={{ display: 'flex', flexDirection: 'column', flex: 1 }}>
|
||||||
<span>
|
<span>
|
||||||
<span className='stat-label'>{child.name}</span>#<span className='stat-label'>{child.id}</span>
|
<span className='stat-label'>{child.name}</span>#<span className='stat-label'>{child.id}</span>
|
||||||
</span>
|
</span>
|
||||||
|
<ObservableSummary observable={child} />
|
||||||
|
</div>
|
||||||
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<div style={{ display: 'flex', alignItems: 'center', gap: '8px' }}>
|
<div style={{ display: 'flex', alignItems: 'center', gap: '8px' }}>
|
||||||
<i
|
<i
|
||||||
@@ -294,60 +489,14 @@ function AgentStatus() {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div style={{ display: isExpanded ? 'block' : 'none' }}>
|
<div style={{ display: isExpanded ? 'block' : 'none' }}>
|
||||||
{child.creation && (
|
<CollapsibleRawSections container={child} />
|
||||||
<div>
|
|
||||||
<h5>Creation:</h5>
|
|
||||||
<pre className="hljs"><code>
|
|
||||||
<div dangerouslySetInnerHTML={{ __html: hljs.highlight(JSON.stringify(child.creation || {}, null, 2), { language: 'json' }).value }}></div>
|
|
||||||
</code></pre>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
{child.progress && child.progress.length > 0 && (
|
|
||||||
<div>
|
|
||||||
<h5>Progress:</h5>
|
|
||||||
<pre className="hljs"><code>
|
|
||||||
<div dangerouslySetInnerHTML={{ __html: hljs.highlight(JSON.stringify(child.progress || {}, null, 2), { language: 'json' }).value }}></div>
|
|
||||||
</code></pre>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
{child.completion && (
|
|
||||||
<div>
|
|
||||||
<h5>Completion:</h5>
|
|
||||||
<pre className="hljs"><code>
|
|
||||||
<div dangerouslySetInnerHTML={{ __html: hljs.highlight(JSON.stringify(child.completion || {}, null, 2), { language: 'json' }).value }}></div>
|
|
||||||
</code></pre>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
{container.creation && (
|
<CollapsibleRawSections container={container} />
|
||||||
<div>
|
|
||||||
<h4>Creation:</h4>
|
|
||||||
<pre className="hljs"><code>
|
|
||||||
<div dangerouslySetInnerHTML={{ __html: hljs.highlight(JSON.stringify(container.creation || {}, null, 2), { language: 'json' }).value }}></div>
|
|
||||||
</code></pre>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
{container.progress && container.progress.length > 0 && (
|
|
||||||
<div>
|
|
||||||
<h4>Progress:</h4>
|
|
||||||
<pre className="hljs"><code>
|
|
||||||
<div dangerouslySetInnerHTML={{ __html: hljs.highlight(JSON.stringify(container.progress || {}, null, 2), { language: 'json' }).value }}></div>
|
|
||||||
</code></pre>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
{container.completion && (
|
|
||||||
<div>
|
|
||||||
<h4>Completion:</h4>
|
|
||||||
<pre className="hljs"><code>
|
|
||||||
<div dangerouslySetInnerHTML={{ __html: hljs.highlight(JSON.stringify(container.completion || {}, null, 2), { language: 'json' }).value }}></div>
|
|
||||||
</code></pre>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
55
webui/react-ui/src/utils/api.js
vendored
55
webui/react-ui/src/utils/api.js
vendored
@@ -24,6 +24,50 @@ const buildUrl = (endpoint) => {
|
|||||||
return `${API_CONFIG.baseUrl}${endpoint.startsWith('/') ? endpoint.substring(1) : endpoint}`;
|
return `${API_CONFIG.baseUrl}${endpoint.startsWith('/') ? endpoint.substring(1) : endpoint}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Helper function to convert ActionDefinition to FormFieldDefinition format
|
||||||
|
const convertActionDefinitionToFields = (definition) => {
|
||||||
|
if (!definition || !definition.Properties) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
const fields = [];
|
||||||
|
const required = definition.Required || [];
|
||||||
|
|
||||||
|
console.debug('Action definition:', definition);
|
||||||
|
|
||||||
|
Object.entries(definition.Properties).forEach(([name, property]) => {
|
||||||
|
const field = {
|
||||||
|
name,
|
||||||
|
label: name.charAt(0).toUpperCase() + name.slice(1),
|
||||||
|
type: 'text', // Default to text, we'll enhance this later
|
||||||
|
required: required.includes(name),
|
||||||
|
helpText: property.Description || '',
|
||||||
|
defaultValue: property.Default,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (property.enum && property.enum.length > 0) {
|
||||||
|
field.type = 'select';
|
||||||
|
field.options = property.enum;
|
||||||
|
} else {
|
||||||
|
switch (property.type) {
|
||||||
|
case 'integer':
|
||||||
|
field.type = 'number';
|
||||||
|
field.min = property.Minimum;
|
||||||
|
field.max = property.Maximum;
|
||||||
|
break;
|
||||||
|
case 'boolean':
|
||||||
|
field.type = 'checkbox';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// TODO: Handle Object and Array types which require nested fields
|
||||||
|
}
|
||||||
|
|
||||||
|
fields.push(field);
|
||||||
|
});
|
||||||
|
|
||||||
|
return fields;
|
||||||
|
};
|
||||||
|
|
||||||
// Agent-related API calls
|
// Agent-related API calls
|
||||||
export const agentApi = {
|
export const agentApi = {
|
||||||
// Get list of all agents
|
// Get list of all agents
|
||||||
@@ -216,6 +260,17 @@ export const actionApi = {
|
|||||||
return handleResponse(response);
|
return handleResponse(response);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// Get action definition
|
||||||
|
getActionDefinition: async (name, config = {}) => {
|
||||||
|
const response = await fetch(buildUrl(API_CONFIG.endpoints.actionDefinition(name)), {
|
||||||
|
method: 'POST',
|
||||||
|
headers: API_CONFIG.headers,
|
||||||
|
body: JSON.stringify(config),
|
||||||
|
});
|
||||||
|
const definition = await handleResponse(response);
|
||||||
|
return convertActionDefinitionToFields(definition);
|
||||||
|
},
|
||||||
|
|
||||||
// Execute an action for an agent
|
// Execute an action for an agent
|
||||||
executeAction: async (name, actionData) => {
|
executeAction: async (name, actionData) => {
|
||||||
const response = await fetch(buildUrl(API_CONFIG.endpoints.executeAction(name)), {
|
const response = await fetch(buildUrl(API_CONFIG.endpoints.executeAction(name)), {
|
||||||
|
|||||||
1
webui/react-ui/src/utils/config.js
vendored
1
webui/react-ui/src/utils/config.js
vendored
@@ -43,6 +43,7 @@ export const API_CONFIG = {
|
|||||||
|
|
||||||
// Action endpoints
|
// Action endpoints
|
||||||
listActions: '/api/actions',
|
listActions: '/api/actions',
|
||||||
|
actionDefinition: (name) => `/api/action/${name}/definition`,
|
||||||
executeAction: (name) => `/api/action/${name}/run`,
|
executeAction: (name) => `/api/action/${name}/run`,
|
||||||
|
|
||||||
// Status endpoint
|
// Status endpoint
|
||||||
|
|||||||
@@ -188,6 +188,7 @@ func (app *App) registerRoutes(pool *state.AgentPool, webapp *fiber.App) {
|
|||||||
// Add endpoint for getting agent config metadata
|
// Add endpoint for getting agent config metadata
|
||||||
webapp.Get("/api/meta/agent/config", app.GetAgentConfigMeta())
|
webapp.Get("/api/meta/agent/config", app.GetAgentConfigMeta())
|
||||||
|
|
||||||
|
webapp.Post("/api/action/:name/definition", app.GetActionDefinition(pool))
|
||||||
webapp.Post("/api/action/:name/run", app.ExecuteAction(pool))
|
webapp.Post("/api/action/:name/run", app.ExecuteAction(pool))
|
||||||
webapp.Get("/api/actions", app.ListActions())
|
webapp.Get("/api/actions", app.ListActions())
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user