mirror of
https://github.com/aljazceru/opencode.git
synced 2025-12-28 13:14:28 +01:00
Refactor to support multiple instances inside single opencode process (#2360)
This release has a bunch of minor breaking changes if you are using opencode plugins or sdk 1. storage events have been removed (we might bring this back but had some issues) 2. concept of `app` is gone - there is a new concept called `project` and endpoints to list projects and get the current project 3. plugin receives `directory` which is cwd and `worktree` which is where the root of the project is if it's a git repo 4. the session.chat function has been renamed to session.prompt in sdk. it no longer requires model to be passed in (model is now an object) 5. every endpoint takes an optional `directory` parameter to operate as though opencode is running in that directory
This commit is contained in:
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
@@ -19,6 +18,7 @@ import (
|
||||
"github.com/sst/opencode/internal/clipboard"
|
||||
"github.com/sst/opencode/internal/tui"
|
||||
"github.com/sst/opencode/internal/util"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
var Version = "dev"
|
||||
@@ -37,14 +37,6 @@ func main() {
|
||||
|
||||
url := os.Getenv("OPENCODE_SERVER")
|
||||
|
||||
appInfoStr := os.Getenv("OPENCODE_APP_INFO")
|
||||
var appInfo opencode.App
|
||||
err := json.Unmarshal([]byte(appInfoStr), &appInfo)
|
||||
if err != nil {
|
||||
slog.Error("Failed to unmarshal app info", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
stat, err := os.Stdin.Stat()
|
||||
if err != nil {
|
||||
slog.Error("Failed to stat stdin", "error", err)
|
||||
@@ -73,17 +65,43 @@ func main() {
|
||||
option.WithBaseURL(url),
|
||||
)
|
||||
|
||||
// Fetch agents from the /agent endpoint
|
||||
agentsPtr, err := httpClient.App.Agents(context.Background())
|
||||
var agents []opencode.Agent
|
||||
var path *opencode.Path
|
||||
var project *opencode.Project
|
||||
|
||||
batch := errgroup.Group{}
|
||||
|
||||
batch.Go(func() error {
|
||||
result, err := httpClient.Project.Current(context.Background(), opencode.ProjectCurrentParams{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
project = result
|
||||
return nil
|
||||
})
|
||||
|
||||
batch.Go(func() error {
|
||||
result, err := httpClient.Agent.List(context.Background(), opencode.AgentListParams{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
agents = *result
|
||||
return nil
|
||||
})
|
||||
|
||||
batch.Go(func() error {
|
||||
result, err := httpClient.Path.Get(context.Background(), opencode.PathGetParams{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
path = result
|
||||
return nil
|
||||
})
|
||||
|
||||
err = batch.Wait()
|
||||
if err != nil {
|
||||
slog.Error("Failed to fetch agents", "error", err)
|
||||
os.Exit(1)
|
||||
panic(err)
|
||||
}
|
||||
if agentsPtr == nil {
|
||||
slog.Error("No agents returned from server")
|
||||
os.Exit(1)
|
||||
}
|
||||
agents := *agentsPtr
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
@@ -91,7 +109,7 @@ func main() {
|
||||
logger := slog.New(apiHandler)
|
||||
slog.SetDefault(logger)
|
||||
|
||||
slog.Debug("TUI launched", "app", appInfoStr, "agents_count", len(agents), "url", url)
|
||||
slog.Debug("TUI launched")
|
||||
|
||||
go func() {
|
||||
err = clipboard.Init()
|
||||
@@ -101,7 +119,7 @@ func main() {
|
||||
}()
|
||||
|
||||
// Create main context for the application
|
||||
app_, err := app.New(ctx, version, appInfo, agents, httpClient, model, prompt, agent, sessionID)
|
||||
app_, err := app.New(ctx, version, project, path, agents, httpClient, model, prompt, agent, sessionID)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -118,12 +136,9 @@ func main() {
|
||||
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT)
|
||||
|
||||
go func() {
|
||||
stream := httpClient.Event.ListStreaming(ctx)
|
||||
stream := httpClient.Event.ListStreaming(ctx, opencode.EventListParams{})
|
||||
for stream.Next() {
|
||||
evt := stream.Current().AsUnion()
|
||||
if _, ok := evt.(opencode.EventListResponseEventStorageWrite); ok {
|
||||
continue
|
||||
}
|
||||
program.Send(evt)
|
||||
}
|
||||
if err := stream.Err(); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user