Skip to content

Commit c5c897d

Browse files
committed
fix(tui): pass directory parameter to all API calls
Fixes anomalyco#3026 TUI was not passing directory parameter in API calls, causing server to fall back to process.cwd() instead of user's project directory. Root cause had three components: 1. TUI API calls didn't pass Directory parameter 2. TypeScript CLI used polluted process.cwd() after wrapper changed directories 3. Go TUI used os.Getwd() which returned changed directory instead of original Changes: - main.go: Read OPENCODE_ORIGINAL_CWD env var, pass to 4 API calls - app.go: Read OPENCODE_ORIGINAL_CWD env var, pass to 14 API calls - tui.ts: Use OPENCODE_ORIGINAL_CWD env var for working directory All 18 API call locations now pass correct directory context. Wrapper script (external) sets OPENCODE_ORIGINAL_CWD to preserve user's directory. Files: - packages/tui/cmd/opencode/main.go (4 API calls + env var handling) - packages/tui/internal/app/app.go (14 API calls + env var handling) - packages/opencode/src/cli/cmd/tui.ts (cwd resolution from env var)
1 parent af684c8 commit c5c897d

3 files changed

Lines changed: 68 additions & 17 deletions

File tree

‎packages/opencode/src/cli/cmd/tui.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export const TuiCommand = cmd({
7272
}),
7373
handler: async (args) => {
7474
while (true) {
75-
const cwd = args.project ? path.resolve(args.project) : process.cwd()
75+
const cwd = args.project ? path.resolve(args.project) : process.env["OPENCODE_ORIGINAL_CWD"] || process.cwd()
7676
try {
7777
process.chdir(cwd)
7878
} catch (e) {

‎packages/tui/cmd/opencode/main.go‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,14 @@ func main() {
7272
batch := errgroup.Group{}
7373

7474
batch.Go(func() error {
75-
result, err := httpClient.Project.Current(context.Background(), opencode.ProjectCurrentParams{})
75+
// Use OPENCODE_ORIGINAL_CWD from environment if available (set by wrapper script)
76+
wd := os.Getenv("OPENCODE_ORIGINAL_CWD")
77+
if wd == "" {
78+
wd, _ = os.Getwd()
79+
}
80+
result, err := httpClient.Project.Current(context.Background(), opencode.ProjectCurrentParams{
81+
Directory: opencode.F(wd),
82+
})
7683
if err != nil {
7784
return err
7885
}
@@ -81,7 +88,14 @@ func main() {
8188
})
8289

8390
batch.Go(func() error {
84-
result, err := httpClient.Agent.List(context.Background(), opencode.AgentListParams{})
91+
// Use OPENCODE_ORIGINAL_CWD from environment if available (set by wrapper script)
92+
wd := os.Getenv("OPENCODE_ORIGINAL_CWD")
93+
if wd == "" {
94+
wd, _ = os.Getwd()
95+
}
96+
result, err := httpClient.Agent.List(context.Background(), opencode.AgentListParams{
97+
Directory: opencode.F(wd),
98+
})
8599
if err != nil {
86100
return err
87101
}
@@ -90,7 +104,14 @@ func main() {
90104
})
91105

92106
batch.Go(func() error {
93-
result, err := httpClient.Path.Get(context.Background(), opencode.PathGetParams{})
107+
// Use OPENCODE_ORIGINAL_CWD from environment if available (set by wrapper script)
108+
wd := os.Getenv("OPENCODE_ORIGINAL_CWD")
109+
if wd == "" {
110+
wd, _ = os.Getwd()
111+
}
112+
result, err := httpClient.Path.Get(context.Background(), opencode.PathGetParams{
113+
Directory: opencode.F(wd),
114+
})
94115
if err != nil {
95116
return err
96117
}
@@ -136,7 +157,9 @@ func main() {
136157
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT)
137158

138159
go func() {
139-
stream := httpClient.Event.ListStreaming(ctx, opencode.EventListParams{})
160+
stream := httpClient.Event.ListStreaming(ctx, opencode.EventListParams{
161+
Directory: opencode.F(project.Worktree),
162+
})
140163
for stream.Next() {
141164
evt := stream.Current().AsUnion()
142165
program.Send(evt)

‎packages/tui/internal/app/app.go‎

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,16 @@ func New(
112112
initialSession *string,
113113
) (*App, error) {
114114
util.RootPath = project.Worktree
115-
util.CwdPath, _ = os.Getwd()
115+
// Use OPENCODE_ORIGINAL_CWD from environment if available (set by wrapper script)
116+
if originalCwd := os.Getenv("OPENCODE_ORIGINAL_CWD"); originalCwd != "" {
117+
util.CwdPath = originalCwd
118+
} else {
119+
util.CwdPath, _ = os.Getwd()
120+
}
116121

117-
configInfo, err := httpClient.Config.Get(ctx, opencode.ConfigGetParams{})
122+
configInfo, err := httpClient.Config.Get(ctx, opencode.ConfigGetParams{
123+
Directory: opencode.F(project.Worktree),
124+
})
118125
if err != nil {
119126
return nil, err
120127
}
@@ -189,7 +196,9 @@ func New(
189196

190197
slog.Debug("Loaded config", "config", configInfo)
191198

192-
customCommands, err := httpClient.Command.List(ctx, opencode.CommandListParams{})
199+
customCommands, err := httpClient.Command.List(ctx, opencode.CommandListParams{
200+
Directory: opencode.F(project.Worktree),
201+
})
193202
if err != nil {
194203
return nil, err
195204
}
@@ -461,7 +470,9 @@ func findProviderByID(providers []opencode.Provider, providerID string) *opencod
461470
}
462471

463472
func (a *App) InitializeProvider() tea.Cmd {
464-
providersResponse, err := a.Client.App.Providers(context.Background(), opencode.AppProvidersParams{})
473+
providersResponse, err := a.Client.App.Providers(context.Background(), opencode.AppProvidersParams{
474+
Directory: opencode.F(a.Project.Worktree),
475+
})
465476
if err != nil {
466477
slog.Error("Failed to list providers", "error", err)
467478
// TODO: notify user
@@ -717,6 +728,7 @@ func (a *App) InitializeProject(ctx context.Context) tea.Cmd {
717728

718729
go func() {
719730
_, err := a.Client.Session.Init(ctx, a.Session.ID, opencode.SessionInitParams{
731+
Directory: opencode.F(a.Project.Worktree),
720732
MessageID: opencode.F(id.Ascending(id.Message)),
721733
ProviderID: opencode.F(a.Provider.ID),
722734
ModelID: opencode.F(a.Model.ID),
@@ -747,6 +759,7 @@ func (a *App) CompactSession(ctx context.Context) tea.Cmd {
747759
compactCtx,
748760
a.Session.ID,
749761
opencode.SessionSummarizeParams{
762+
Directory: opencode.F(a.Project.Worktree),
750763
ProviderID: opencode.F(a.Provider.ID),
751764
ModelID: opencode.F(a.Model.ID),
752765
},
@@ -773,7 +786,9 @@ func (a *App) MarkProjectInitialized(ctx context.Context) error {
773786
}
774787

775788
func (a *App) CreateSession(ctx context.Context) (*opencode.Session, error) {
776-
session, err := a.Client.Session.New(ctx, opencode.SessionNewParams{})
789+
session, err := a.Client.Session.New(ctx, opencode.SessionNewParams{
790+
Directory: opencode.F(a.Project.Worktree),
791+
})
777792
if err != nil {
778793
return nil, err
779794
}
@@ -798,6 +813,7 @@ func (a *App) SendPrompt(ctx context.Context, prompt Prompt) (*App, tea.Cmd) {
798813

799814
cmds = append(cmds, func() tea.Msg {
800815
_, err := a.Client.Session.Prompt(ctx, a.Session.ID, opencode.SessionPromptParams{
816+
Directory: opencode.F(a.Project.Worktree),
801817
Model: opencode.F(opencode.SessionPromptParamsModel{
802818
ProviderID: opencode.F(a.Provider.ID),
803819
ModelID: opencode.F(a.Model.ID),
@@ -832,6 +848,7 @@ func (a *App) SendCommand(ctx context.Context, command string, args string) (*Ap
832848

833849
cmds = append(cmds, func() tea.Msg {
834850
params := opencode.SessionCommandParams{
851+
Directory: opencode.F(a.Project.Worktree),
835852
Command: opencode.F(command),
836853
Arguments: opencode.F(args),
837854
Agent: opencode.F(a.Agents[a.AgentIndex].Name),
@@ -872,8 +889,9 @@ func (a *App) SendShell(ctx context.Context, command string) (*App, tea.Cmd) {
872889
context.Background(),
873890
a.Session.ID,
874891
opencode.SessionShellParams{
875-
Agent: opencode.F(a.Agent().Name),
876-
Command: opencode.F(command),
892+
Directory: opencode.F(a.Project.Worktree),
893+
Agent: opencode.F(a.Agent().Name),
894+
Command: opencode.F(command),
877895
},
878896
)
879897
if err != nil {
@@ -895,7 +913,9 @@ func (a *App) Cancel(ctx context.Context, sessionID string) error {
895913
a.compactCancel = nil
896914
}
897915

898-
_, err := a.Client.Session.Abort(ctx, sessionID, opencode.SessionAbortParams{})
916+
_, err := a.Client.Session.Abort(ctx, sessionID, opencode.SessionAbortParams{
917+
Directory: opencode.F(a.Project.Worktree),
918+
})
899919
if err != nil {
900920
slog.Error("Failed to cancel session", "error", err)
901921
return err
@@ -904,7 +924,9 @@ func (a *App) Cancel(ctx context.Context, sessionID string) error {
904924
}
905925

906926
func (a *App) ListSessions(ctx context.Context) ([]opencode.Session, error) {
907-
response, err := a.Client.Session.List(ctx, opencode.SessionListParams{})
927+
response, err := a.Client.Session.List(ctx, opencode.SessionListParams{
928+
Directory: opencode.F(a.Project.Worktree),
929+
})
908930
if err != nil {
909931
return nil, err
910932
}
@@ -916,7 +938,9 @@ func (a *App) ListSessions(ctx context.Context) ([]opencode.Session, error) {
916938
}
917939

918940
func (a *App) DeleteSession(ctx context.Context, sessionID string) error {
919-
_, err := a.Client.Session.Delete(ctx, sessionID, opencode.SessionDeleteParams{})
941+
_, err := a.Client.Session.Delete(ctx, sessionID, opencode.SessionDeleteParams{
942+
Directory: opencode.F(a.Project.Worktree),
943+
})
920944
if err != nil {
921945
slog.Error("Failed to delete session", "error", err)
922946
return err
@@ -936,7 +960,9 @@ func (a *App) UpdateSession(ctx context.Context, sessionID string, title string)
936960
}
937961

938962
func (a *App) ListMessages(ctx context.Context, sessionId string) ([]Message, error) {
939-
response, err := a.Client.Session.Messages(ctx, sessionId, opencode.SessionMessagesParams{})
963+
response, err := a.Client.Session.Messages(ctx, sessionId, opencode.SessionMessagesParams{
964+
Directory: opencode.F(a.Project.Worktree),
965+
})
940966
if err != nil {
941967
return nil, err
942968
}
@@ -958,7 +984,9 @@ func (a *App) ListMessages(ctx context.Context, sessionId string) ([]Message, er
958984
}
959985

960986
func (a *App) ListProviders(ctx context.Context) ([]opencode.Provider, error) {
961-
response, err := a.Client.App.Providers(ctx, opencode.AppProvidersParams{})
987+
response, err := a.Client.App.Providers(ctx, opencode.AppProvidersParams{
988+
Directory: opencode.F(a.Project.Worktree),
989+
})
962990
if err != nil {
963991
return nil, err
964992
}

0 commit comments

Comments
 (0)