Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
Signed-off-by: mudler <mudler@localai.io>
This commit is contained in:
Ettore Di Giacinto
2025-04-19 22:18:14 +02:00
committed by mudler
parent ce997d2425
commit 33b8aaddfe
3 changed files with 522 additions and 0 deletions

71
pkg/stdio/example/main.go Normal file
View File

@@ -0,0 +1,71 @@
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/mudler/LocalAGI/pkg/stdio"
)
func main() {
// Start the server
server := stdio.NewServer()
go func() {
if err := server.Start(":8080"); err != nil {
log.Fatalf("Failed to start server: %v", err)
}
}()
// Give the server time to start
time.Sleep(time.Second)
// Create a client
client := stdio.NewClient("localhost:8080")
// Start the client
if err := client.Start(context.Background()); err != nil {
log.Fatalf("Failed to start client: %v", err)
}
defer client.Close()
// Set up notification handler
client.SetNotificationHandler(func(notification stdio.JSONRPCNotification) {
fmt.Printf("Received notification: %+v\n", notification)
})
// Send a request
request := stdio.JSONRPCRequest{
JSONRPC: "2.0",
ID: 1,
Method: "test",
Params: map[string]string{"hello": "world"},
}
response, err := client.SendRequest(context.Background(), request)
if err != nil {
log.Fatalf("Failed to send request: %v", err)
}
fmt.Printf("Received response: %+v\n", response)
// Send a notification
notification := stdio.JSONRPCNotification{
JSONRPC: "2.0",
Notification: struct {
Method string `json:"method"`
Params interface{} `json:"params,omitempty"`
}{
Method: "test",
Params: map[string]string{"hello": "world"},
},
}
if err := client.SendNotification(context.Background(), notification); err != nil {
log.Fatalf("Failed to send notification: %v", err)
}
// Keep the program running
select {}
}