diff options
| author | Dax <[email protected]> | 2025-09-01 17:15:49 -0400 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-09-01 17:15:49 -0400 |
| commit | f993541e0b538388969defc94d08c3a7af5ef873 (patch) | |
| tree | ae9898664175faf69583e0bbb1b10135c645d798 /packages/tui/internal | |
| parent | e2df3eb44dc8b032f74ed0273a53be5f83aaab13 (diff) | |
| download | opencode-f993541e0b538388969defc94d08c3a7af5ef873.tar.gz opencode-f993541e0b538388969defc94d08c3a7af5ef873.zip | |
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
Diffstat (limited to 'packages/tui/internal')
| -rw-r--r-- | packages/tui/internal/app/app.go | 62 | ||||
| -rw-r--r-- | packages/tui/internal/app/prompt.go | 4 | ||||
| -rw-r--r-- | packages/tui/internal/completions/agents.go | 3 | ||||
| -rw-r--r-- | packages/tui/internal/completions/files.go | 2 | ||||
| -rw-r--r-- | packages/tui/internal/components/chat/editor.go | 8 | ||||
| -rw-r--r-- | packages/tui/internal/components/chat/message.go | 4 | ||||
| -rw-r--r-- | packages/tui/internal/components/chat/messages.go | 2 | ||||
| -rw-r--r-- | packages/tui/internal/components/status/status.go | 14 | ||||
| -rw-r--r-- | packages/tui/internal/tui/tui.go | 32 |
9 files changed, 72 insertions, 59 deletions
diff --git a/packages/tui/internal/app/app.go b/packages/tui/internal/app/app.go index 8aaddfbcc..4b333a93c 100644 --- a/packages/tui/internal/app/app.go +++ b/packages/tui/internal/app/app.go @@ -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 } diff --git a/packages/tui/internal/app/prompt.go b/packages/tui/internal/app/prompt.go index 81c4b6a7d..8701a2072 100644 --- a/packages/tui/internal/app/prompt.go +++ b/packages/tui/internal/app/prompt.go @@ -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: diff --git a/packages/tui/internal/completions/agents.go b/packages/tui/internal/completions/agents.go index c39fe3036..d25c76d89 100644 --- a/packages/tui/internal/completions/agents.go +++ b/packages/tui/internal/completions/agents.go @@ -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) diff --git a/packages/tui/internal/completions/files.go b/packages/tui/internal/completions/files.go index bece89a89..d00873656 100644 --- a/packages/tui/internal/completions/files.go +++ b/packages/tui/internal/completions/files.go @@ -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 { diff --git a/packages/tui/internal/components/chat/editor.go b/packages/tui/internal/components/chat/editor.go index 710049707..e43d297b8 100644 --- a/packages/tui/internal/components/chat/editor.go +++ b/packages/tui/internal/components/chat/editor.go @@ -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(), diff --git a/packages/tui/internal/components/chat/message.go b/packages/tui/internal/components/chat/message.go index a19d15fd7..ca1a2a536 100644 --- a/packages/tui/internal/components/chat/message.go +++ b/packages/tui/internal/components/chat/message.go @@ -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) diff --git a/packages/tui/internal/components/chat/messages.go b/packages/tui/internal/components/chat/messages.go index b3074bb54..053d538a4 100644 --- a/packages/tui/internal/components/chat/messages.go +++ b/packages/tui/internal/components/chat/messages.go @@ -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) diff --git a/packages/tui/internal/components/status/status.go b/packages/tui/internal/components/status/status.go index 792637825..9d2059e0e 100644 --- a/packages/tui/internal/components/status/status.go +++ b/packages/tui/internal/components/status/status.go @@ -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):] } diff --git a/packages/tui/internal/tui/tui.go b/packages/tui/internal/tui/tui.go index 71e5b9f71..62a647a14 100644 --- a/packages/tui/internal/tui/tui.go +++ b/packages/tui/internal/tui/tui.go @@ -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") |
