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:
Dax
2025-09-01 17:15:49 -04:00
committed by GitHub
parent e2df3eb44d
commit f993541e0b
112 changed files with 4303 additions and 3159 deletions

View File

@@ -27,7 +27,7 @@ type Message struct {
}
type App struct {
Info opencode.App
Project opencode.Project
Agents []opencode.Agent
Providers []opencode.Provider
Version string
@@ -101,7 +101,8 @@ type PermissionRespondedToMsg struct {
func New(
ctx context.Context,
version string,
appInfo opencode.App,
project *opencode.Project,
path *opencode.Path,
agents []opencode.Agent,
httpClient *opencode.Client,
initialModel *string,
@@ -109,10 +110,10 @@ func New(
initialAgent *string,
initialSession *string,
) (*App, error) {
util.RootPath = appInfo.Path.Root
util.CwdPath = appInfo.Path.Cwd
util.RootPath = project.Worktree
util.CwdPath, _ = os.Getwd()
configInfo, err := httpClient.Config.Get(ctx)
configInfo, err := httpClient.Config.Get(ctx, opencode.ConfigGetParams{})
if err != nil {
return nil, err
}
@@ -121,7 +122,7 @@ func New(
configInfo.Keybinds.Leader = "ctrl+x"
}
appStatePath := filepath.Join(appInfo.Path.State, "tui")
appStatePath := filepath.Join(path.State, "tui")
appState, err := LoadState(appStatePath)
if err != nil {
appState = NewState()
@@ -168,9 +169,9 @@ func New(
}
if err := theme.LoadThemesFromDirectories(
appInfo.Path.Config,
appInfo.Path.Root,
appInfo.Path.Cwd,
path.Config,
util.RootPath,
util.CwdPath,
); err != nil {
slog.Warn("Failed to load themes from directories", "error", err)
}
@@ -187,13 +188,13 @@ func New(
slog.Debug("Loaded config", "config", configInfo)
customCommands, err := httpClient.Command.List(ctx)
customCommands, err := httpClient.Command.List(ctx, opencode.CommandListParams{})
if err != nil {
return nil, err
}
app := &App{
Info: appInfo,
Project: *project,
Agents: agents,
Version: version,
StatePath: appStatePath,
@@ -459,7 +460,7 @@ func findProviderByID(providers []opencode.Provider, providerID string) *opencod
}
func (a *App) InitializeProvider() tea.Cmd {
providersResponse, err := a.Client.App.Providers(context.Background())
providersResponse, err := a.Client.App.Providers(context.Background(), opencode.AppProvidersParams{})
if err != nil {
slog.Error("Failed to list providers", "error", err)
// TODO: notify user
@@ -749,12 +750,15 @@ func (a *App) CompactSession(ctx context.Context) tea.Cmd {
}
func (a *App) MarkProjectInitialized(ctx context.Context) error {
_, err := a.Client.App.Init(ctx)
if err != nil {
slog.Error("Failed to mark project as initialized", "error", err)
return err
}
return nil
/*
_, err := a.Client.App.Init(ctx)
if err != nil {
slog.Error("Failed to mark project as initialized", "error", err)
return err
}
return nil
*/
}
func (a *App) CreateSession(ctx context.Context) (*opencode.Session, error) {
@@ -782,12 +786,14 @@ func (a *App) SendPrompt(ctx context.Context, prompt Prompt) (*App, tea.Cmd) {
a.Messages = append(a.Messages, message)
cmds = append(cmds, func() tea.Msg {
_, err := a.Client.Session.Chat(ctx, a.Session.ID, opencode.SessionChatParams{
ProviderID: opencode.F(a.Provider.ID),
ModelID: opencode.F(a.Model.ID),
Agent: opencode.F(a.Agent().Name),
MessageID: opencode.F(messageID),
Parts: opencode.F(message.ToSessionChatParams()),
_, err := a.Client.Session.Prompt(ctx, a.Session.ID, opencode.SessionPromptParams{
Model: opencode.F(opencode.SessionPromptParamsModel{
ProviderID: opencode.F(a.Provider.ID),
ModelID: opencode.F(a.Model.ID),
}),
Agent: opencode.F(a.Agent().Name),
MessageID: opencode.F(messageID),
Parts: opencode.F(message.ToSessionChatParams()),
})
if err != nil {
errormsg := fmt.Sprintf("failed to send message: %v", err)
@@ -878,7 +884,7 @@ func (a *App) Cancel(ctx context.Context, sessionID string) error {
a.compactCancel = nil
}
_, err := a.Client.Session.Abort(ctx, sessionID)
_, err := a.Client.Session.Abort(ctx, sessionID, opencode.SessionAbortParams{})
if err != nil {
slog.Error("Failed to cancel session", "error", err)
return err
@@ -887,7 +893,7 @@ func (a *App) Cancel(ctx context.Context, sessionID string) error {
}
func (a *App) ListSessions(ctx context.Context) ([]opencode.Session, error) {
response, err := a.Client.Session.List(ctx)
response, err := a.Client.Session.List(ctx, opencode.SessionListParams{})
if err != nil {
return nil, err
}
@@ -899,7 +905,7 @@ func (a *App) ListSessions(ctx context.Context) ([]opencode.Session, error) {
}
func (a *App) DeleteSession(ctx context.Context, sessionID string) error {
_, err := a.Client.Session.Delete(ctx, sessionID)
_, err := a.Client.Session.Delete(ctx, sessionID, opencode.SessionDeleteParams{})
if err != nil {
slog.Error("Failed to delete session", "error", err)
return err
@@ -919,7 +925,7 @@ func (a *App) UpdateSession(ctx context.Context, sessionID string, title string)
}
func (a *App) ListMessages(ctx context.Context, sessionId string) ([]Message, error) {
response, err := a.Client.Session.Messages(ctx, sessionId)
response, err := a.Client.Session.Messages(ctx, sessionId, opencode.SessionMessagesParams{})
if err != nil {
return nil, err
}
@@ -941,7 +947,7 @@ func (a *App) ListMessages(ctx context.Context, sessionId string) ([]Message, er
}
func (a *App) ListProviders(ctx context.Context) ([]opencode.Provider, error) {
response, err := a.Client.App.Providers(ctx)
response, err := a.Client.App.Providers(ctx, opencode.AppProvidersParams{})
if err != nil {
return nil, err
}

View File

@@ -204,8 +204,8 @@ func (m Message) ToPrompt() (*Prompt, error) {
return nil, errors.New("unknown message type")
}
func (m Message) ToSessionChatParams() []opencode.SessionChatParamsPartUnion {
parts := []opencode.SessionChatParamsPartUnion{}
func (m Message) ToSessionChatParams() []opencode.SessionPromptParamsPartUnion {
parts := []opencode.SessionPromptParamsPartUnion{}
for _, part := range m.Parts {
switch p := part.(type) {
case opencode.TextPart:

View File

@@ -30,8 +30,9 @@ func (cg *agentsContextGroup) GetChildEntries(
query = strings.TrimSpace(query)
agents, err := cg.app.Client.App.Agents(
agents, err := cg.app.Client.Agent.List(
context.Background(),
opencode.AgentListParams{},
)
if err != nil {
slog.Error("Failed to get agent list", "error", err)

View File

@@ -29,7 +29,7 @@ func (cg *filesContextGroup) GetEmptyMessage() string {
func (cg *filesContextGroup) getGitFiles() []CompletionSuggestion {
items := make([]CompletionSuggestion, 0)
status, _ := cg.app.Client.File.Status(context.Background())
status, _ := cg.app.Client.File.Status(context.Background(), opencode.FileStatusParams{})
if status != nil {
files := *status
sort.Slice(files, func(i, j int) bool {

View File

@@ -160,7 +160,7 @@ func (m *editorComponent) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if filePath := strings.TrimSpace(strings.TrimPrefix(text, "@")); strings.HasPrefix(text, "@") && filePath != "" {
statPath := filePath
if !filepath.IsAbs(filePath) {
statPath = filepath.Join(m.app.Info.Path.Cwd, filePath)
statPath = filepath.Join(util.CwdPath, filePath)
}
if _, err := os.Stat(statPath); err == nil {
attachment := m.createAttachmentFromPath(filePath)
@@ -623,7 +623,7 @@ func (m *editorComponent) SetValueWithAttachments(value string) {
if end > start {
filePath := value[start:end]
slog.Debug("test", "filePath", filePath)
if _, err := os.Stat(filepath.Join(m.app.Info.Path.Cwd, filePath)); err == nil {
if _, err := os.Stat(filepath.Join(util.CwdPath, filePath)); err == nil {
slog.Debug("test", "found", true)
attachment := m.createAttachmentFromFile(filePath)
if attachment != nil {
@@ -818,7 +818,7 @@ func (m *editorComponent) createAttachmentFromFile(filePath string) *attachment.
mediaType := getMediaTypeFromExtension(ext)
absolutePath := filePath
if !filepath.IsAbs(filePath) {
absolutePath = filepath.Join(m.app.Info.Path.Cwd, filePath)
absolutePath = filepath.Join(util.CwdPath, filePath)
}
// For text files, create a simple file reference
@@ -872,7 +872,7 @@ func (m *editorComponent) createAttachmentFromPath(filePath string) *attachment.
mediaType := getMediaTypeFromExtension(extension)
absolutePath := filePath
if !filepath.IsAbs(filePath) {
absolutePath = filepath.Join(m.app.Info.Path.Cwd, filePath)
absolutePath = filepath.Join(util.CwdPath, filePath)
}
return &attachment.Attachment{
ID: uuid.NewString(),

View File

@@ -55,6 +55,8 @@ func WithBackgroundColor(color compat.AdaptiveColor) renderingOption {
func WithNoBorder() renderingOption {
return func(c *blockRenderer) {
c.border = false
c.paddingLeft++
c.paddingRight++
}
}
@@ -185,7 +187,7 @@ func renderContentBlock(
style = style.BorderRightForeground(borderColor)
}
} else {
style = style.PaddingLeft(renderer.paddingLeft + 1).PaddingRight(renderer.paddingRight + 1)
style = style.PaddingLeft(renderer.paddingLeft).PaddingRight(renderer.paddingRight)
}
content = style.Render(content)

View File

@@ -769,6 +769,7 @@ func (m *messagesComponent) renderView() tea.Cmd {
context.Background(),
m.app.CurrentPermission.SessionID,
m.app.CurrentPermission.MessageID,
opencode.SessionMessageParams{},
)
if err != nil || response == nil {
slog.Error("Failed to get message from child session", "error", err)
@@ -1238,6 +1239,7 @@ func (m *messagesComponent) RedoLastMessage() (tea.Model, tea.Cmd) {
response, err := m.app.Client.Session.Unrevert(
context.Background(),
m.app.Session.ID,
opencode.SessionUnrevertParams{},
)
if err != nil {
slog.Error("Failed to unrevert session", "error", err)

View File

@@ -200,7 +200,7 @@ func (m *statusComponent) View() string {
func (m *statusComponent) startGitWatcher() tea.Cmd {
cmd := util.CmdHandler(
GitBranchUpdatedMsg{Branch: getCurrentGitBranch(m.app.Info.Path.Root)},
GitBranchUpdatedMsg{Branch: getCurrentGitBranch(m.app.Project.Worktree)},
)
if err := m.initWatcher(); err != nil {
return cmd
@@ -209,7 +209,7 @@ func (m *statusComponent) startGitWatcher() tea.Cmd {
}
func (m *statusComponent) initWatcher() error {
gitDir := filepath.Join(m.app.Info.Path.Root, ".git")
gitDir := filepath.Join(m.app.Project.Worktree, ".git")
headFile := filepath.Join(gitDir, "HEAD")
if info, err := os.Stat(gitDir); err != nil || !info.IsDir() {
return err
@@ -226,7 +226,7 @@ func (m *statusComponent) initWatcher() error {
}
// Also watch the ref file if HEAD points to a ref
refFile := getGitRefFile(m.app.Info.Path.Cwd)
refFile := getGitRefFile(util.CwdPath)
if refFile != headFile && refFile != "" {
if _, err := os.Stat(refFile); err == nil {
watcher.Add(refFile) // Ignore error, HEAD watching is sufficient
@@ -247,7 +247,7 @@ func (m *statusComponent) watchForGitChanges() tea.Cmd {
for {
select {
case event, ok := <-m.watcher.Events:
branch := getCurrentGitBranch(m.app.Info.Path.Root)
branch := getCurrentGitBranch(m.app.Project.Worktree)
if !ok {
return GitBranchUpdatedMsg{Branch: branch}
}
@@ -276,8 +276,8 @@ func (m *statusComponent) updateWatchedFiles() {
if m.watcher == nil {
return
}
refFile := getGitRefFile(m.app.Info.Path.Root)
headFile := filepath.Join(m.app.Info.Path.Root, ".git", "HEAD")
refFile := getGitRefFile(m.app.Project.Worktree)
headFile := filepath.Join(m.app.Project.Worktree, ".git", "HEAD")
if refFile != headFile && refFile != "" {
if _, err := os.Stat(refFile); err == nil {
// Try to add the new ref file (ignore error if already watching)
@@ -330,7 +330,7 @@ func NewStatusCmp(app *app.App) StatusComponent {
}
homePath, err := os.UserHomeDir()
cwdPath := app.Info.Path.Cwd
cwdPath := util.CwdPath
if err == nil && homePath != "" && strings.HasPrefix(cwdPath, homePath) {
cwdPath = "~" + cwdPath[len(homePath):]
}

View File

@@ -393,7 +393,7 @@ func (a Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
a.showCompletionDialog = false
// If we're in a child session, switch back to parent before sending prompt
if a.app.Session.ParentID != "" {
parentSession, err := a.app.Client.Session.Get(context.Background(), a.app.Session.ParentID)
parentSession, err := a.app.Client.Session.Get(context.Background(), a.app.Session.ParentID, opencode.SessionGetParams{})
if err != nil {
slog.Error("Failed to get parent session", "error", err)
return a, toast.NewErrorToast("Failed to get parent session")
@@ -411,7 +411,7 @@ func (a Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case app.SendCommand:
// If we're in a child session, switch back to parent before sending prompt
if a.app.Session.ParentID != "" {
parentSession, err := a.app.Client.Session.Get(context.Background(), a.app.Session.ParentID)
parentSession, err := a.app.Client.Session.Get(context.Background(), a.app.Session.ParentID, opencode.SessionGetParams{})
if err != nil {
slog.Error("Failed to get parent session", "error", err)
return a, toast.NewErrorToast("Failed to get parent session")
@@ -429,7 +429,7 @@ func (a Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case app.SendShell:
// If we're in a child session, switch back to parent before sending prompt
if a.app.Session.ParentID != "" {
parentSession, err := a.app.Client.Session.Get(context.Background(), a.app.Session.ParentID)
parentSession, err := a.app.Client.Session.Get(context.Background(), a.app.Session.ParentID, opencode.SessionGetParams{})
if err != nil {
slog.Error("Failed to get parent session", "error", err)
return a, toast.NewErrorToast("Failed to get parent session")
@@ -460,11 +460,13 @@ func (a Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
"opencode updated to "+msg.Properties.Version+", restart to apply.",
toast.WithTitle("New version installed"),
)
case opencode.EventListResponseEventIdeInstalled:
return a, toast.NewSuccessToast(
"Installed the opencode extension in "+msg.Properties.Ide,
toast.WithTitle(msg.Properties.Ide+" extension installed"),
)
/*
case opencode.EventListResponseEventIdeInstalled:
return a, toast.NewSuccessToast(
"Installed the opencode extension in "+msg.Properties.Ide,
toast.WithTitle(msg.Properties.Ide+" extension installed"),
)
*/
case opencode.EventListResponseEventSessionDeleted:
if a.app.Session != nil && msg.Properties.Info.ID == a.app.Session.ID {
a.app.Session = &opencode.Session{}
@@ -674,7 +676,7 @@ func (a Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if nextMessageID == "" {
// Last message - use unrevert to restore full conversation
response, err = a.app.Client.Session.Unrevert(context.Background(), a.app.Session.ID)
response, err = a.app.Client.Session.Unrevert(context.Background(), a.app.Session.ID, opencode.SessionUnrevertParams{})
} else {
// Revert to next message to make target the last visible
response, err = a.app.Client.Session.Revert(context.Background(), a.app.Session.ID,
@@ -1183,7 +1185,7 @@ func (a Model) executeCommand(command commands.Command) (tea.Model, tea.Cmd) {
if a.app.Session.ID == "" {
return a, nil
}
response, err := a.app.Client.Session.Share(context.Background(), a.app.Session.ID)
response, err := a.app.Client.Session.Share(context.Background(), a.app.Session.ID, opencode.SessionShareParams{})
if err != nil {
slog.Error("Failed to share session", "error", err)
return a, toast.NewErrorToast("Failed to share session")
@@ -1195,7 +1197,7 @@ func (a Model) executeCommand(command commands.Command) (tea.Model, tea.Cmd) {
if a.app.Session.ID == "" {
return a, nil
}
_, err := a.app.Client.Session.Unshare(context.Background(), a.app.Session.ID)
_, err := a.app.Client.Session.Unshare(context.Background(), a.app.Session.ID, opencode.SessionUnshareParams{})
if err != nil {
slog.Error("Failed to unshare session", "error", err)
return a, toast.NewErrorToast("Failed to unshare session")
@@ -1223,7 +1225,7 @@ func (a Model) executeCommand(command commands.Command) (tea.Model, tea.Cmd) {
var parentSession *opencode.Session
if a.app.Session.ParentID != "" {
parentSessionID = a.app.Session.ParentID
session, err := a.app.Client.Session.Get(context.Background(), parentSessionID)
session, err := a.app.Client.Session.Get(context.Background(), parentSessionID, opencode.SessionGetParams{})
if err != nil {
slog.Error("Failed to get parent session", "error", err)
return toast.NewErrorToast("Failed to get parent session")
@@ -1233,7 +1235,7 @@ func (a Model) executeCommand(command commands.Command) (tea.Model, tea.Cmd) {
parentSession = a.app.Session
}
children, err := a.app.Client.Session.Children(context.Background(), parentSessionID)
children, err := a.app.Client.Session.Children(context.Background(), parentSessionID, opencode.SessionChildrenParams{})
if err != nil {
slog.Error("Failed to get session children", "error", err)
return toast.NewErrorToast("Failed to get session children")
@@ -1281,7 +1283,7 @@ func (a Model) executeCommand(command commands.Command) (tea.Model, tea.Cmd) {
var parentSession *opencode.Session
if a.app.Session.ParentID != "" {
parentSessionID = a.app.Session.ParentID
session, err := a.app.Client.Session.Get(context.Background(), parentSessionID)
session, err := a.app.Client.Session.Get(context.Background(), parentSessionID, opencode.SessionGetParams{})
if err != nil {
slog.Error("Failed to get parent session", "error", err)
return toast.NewErrorToast("Failed to get parent session")
@@ -1291,7 +1293,7 @@ func (a Model) executeCommand(command commands.Command) (tea.Model, tea.Cmd) {
parentSession = a.app.Session
}
children, err := a.app.Client.Session.Children(context.Background(), parentSessionID)
children, err := a.app.Client.Session.Children(context.Background(), parentSessionID, opencode.SessionChildrenParams{})
if err != nil {
slog.Error("Failed to get session children", "error", err)
return toast.NewErrorToast("Failed to get session children")