feat: add -c and -s args to tui command following run command pattern (#1835)
This commit is contained in:
@@ -14,6 +14,7 @@ import { FileWatcher } from "../../file/watch"
|
|||||||
import { Ide } from "../../ide"
|
import { Ide } from "../../ide"
|
||||||
import { Agent } from "../../agent/agent"
|
import { Agent } from "../../agent/agent"
|
||||||
import { Flag } from "../../flag/flag"
|
import { Flag } from "../../flag/flag"
|
||||||
|
import { Session } from "../../session"
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
const OPENCODE_TUI_PATH: string
|
const OPENCODE_TUI_PATH: string
|
||||||
@@ -39,6 +40,16 @@ export const TuiCommand = cmd({
|
|||||||
alias: ["m"],
|
alias: ["m"],
|
||||||
describe: "model to use in the format of provider/model",
|
describe: "model to use in the format of provider/model",
|
||||||
})
|
})
|
||||||
|
.option("continue", {
|
||||||
|
alias: ["c"],
|
||||||
|
describe: "continue the last session",
|
||||||
|
type: "boolean",
|
||||||
|
})
|
||||||
|
.option("session", {
|
||||||
|
alias: ["s"],
|
||||||
|
describe: "session id to continue",
|
||||||
|
type: "string",
|
||||||
|
})
|
||||||
.option("prompt", {
|
.option("prompt", {
|
||||||
alias: ["p"],
|
alias: ["p"],
|
||||||
type: "string",
|
type: "string",
|
||||||
@@ -69,6 +80,19 @@ export const TuiCommand = cmd({
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
const result = await bootstrap({ cwd }, async (app) => {
|
const result = await bootstrap({ cwd }, async (app) => {
|
||||||
|
const sessionID = await (async () => {
|
||||||
|
if (args.continue) {
|
||||||
|
const list = Session.list()
|
||||||
|
const first = await list.next()
|
||||||
|
await list.return()
|
||||||
|
if (first.done) return
|
||||||
|
return first.value.id
|
||||||
|
}
|
||||||
|
if (args.session) {
|
||||||
|
return args.session
|
||||||
|
}
|
||||||
|
return undefined
|
||||||
|
})()
|
||||||
FileWatcher.init()
|
FileWatcher.init()
|
||||||
const providers = await Provider.list()
|
const providers = await Provider.list()
|
||||||
if (Object.keys(providers).length === 0) {
|
if (Object.keys(providers).length === 0) {
|
||||||
@@ -106,6 +130,7 @@ export const TuiCommand = cmd({
|
|||||||
...(args.model ? ["--model", args.model] : []),
|
...(args.model ? ["--model", args.model] : []),
|
||||||
...(args.prompt ? ["--prompt", args.prompt] : []),
|
...(args.prompt ? ["--prompt", args.prompt] : []),
|
||||||
...(args.mode ? ["--mode", args.mode] : []),
|
...(args.mode ? ["--mode", args.mode] : []),
|
||||||
|
...(sessionID ? ["--session", sessionID] : []),
|
||||||
],
|
],
|
||||||
cwd,
|
cwd,
|
||||||
stdout: "inherit",
|
stdout: "inherit",
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ func main() {
|
|||||||
var model *string = flag.String("model", "", "model to begin with")
|
var model *string = flag.String("model", "", "model to begin with")
|
||||||
var prompt *string = flag.String("prompt", "", "prompt to begin with")
|
var prompt *string = flag.String("prompt", "", "prompt to begin with")
|
||||||
var agent *string = flag.String("agent", "", "agent to begin with")
|
var agent *string = flag.String("agent", "", "agent to begin with")
|
||||||
|
var sessionID *string = flag.String("session", "", "session ID")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
url := os.Getenv("OPENCODE_SERVER")
|
url := os.Getenv("OPENCODE_SERVER")
|
||||||
@@ -96,7 +97,7 @@ func main() {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
// Create main context for the application
|
// Create main context for the application
|
||||||
app_, err := app.New(ctx, version, appInfo, agents, httpClient, model, prompt, agent)
|
app_, err := app.New(ctx, version, appInfo, agents, httpClient, model, prompt, agent, sessionID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ type App struct {
|
|||||||
InitialModel *string
|
InitialModel *string
|
||||||
InitialPrompt *string
|
InitialPrompt *string
|
||||||
InitialAgent *string
|
InitialAgent *string
|
||||||
|
InitialSession *string
|
||||||
compactCancel context.CancelFunc
|
compactCancel context.CancelFunc
|
||||||
IsLeaderSequence bool
|
IsLeaderSequence bool
|
||||||
}
|
}
|
||||||
@@ -95,6 +96,7 @@ func New(
|
|||||||
initialModel *string,
|
initialModel *string,
|
||||||
initialPrompt *string,
|
initialPrompt *string,
|
||||||
initialAgent *string,
|
initialAgent *string,
|
||||||
|
initialSession *string,
|
||||||
) (*App, error) {
|
) (*App, error) {
|
||||||
util.RootPath = appInfo.Path.Root
|
util.RootPath = appInfo.Path.Root
|
||||||
util.CwdPath = appInfo.Path.Cwd
|
util.CwdPath = appInfo.Path.Cwd
|
||||||
@@ -175,20 +177,21 @@ func New(
|
|||||||
slog.Debug("Loaded config", "config", configInfo)
|
slog.Debug("Loaded config", "config", configInfo)
|
||||||
|
|
||||||
app := &App{
|
app := &App{
|
||||||
Info: appInfo,
|
Info: appInfo,
|
||||||
Agents: agents,
|
Agents: agents,
|
||||||
Version: version,
|
Version: version,
|
||||||
StatePath: appStatePath,
|
StatePath: appStatePath,
|
||||||
Config: configInfo,
|
Config: configInfo,
|
||||||
State: appState,
|
State: appState,
|
||||||
Client: httpClient,
|
Client: httpClient,
|
||||||
AgentIndex: agentIndex,
|
AgentIndex: agentIndex,
|
||||||
Session: &opencode.Session{},
|
Session: &opencode.Session{},
|
||||||
Messages: []Message{},
|
Messages: []Message{},
|
||||||
Commands: commands.LoadFromConfig(configInfo),
|
Commands: commands.LoadFromConfig(configInfo),
|
||||||
InitialModel: initialModel,
|
InitialModel: initialModel,
|
||||||
InitialPrompt: initialPrompt,
|
InitialPrompt: initialPrompt,
|
||||||
InitialAgent: initialAgent,
|
InitialAgent: initialAgent,
|
||||||
|
InitialSession: initialSession,
|
||||||
}
|
}
|
||||||
|
|
||||||
return app, nil
|
return app, nil
|
||||||
@@ -504,6 +507,28 @@ func (a *App) InitializeProvider() tea.Cmd {
|
|||||||
Provider: *selectedProvider,
|
Provider: *selectedProvider,
|
||||||
Model: *selectedModel,
|
Model: *selectedModel,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
// Load initial session if provided
|
||||||
|
if a.InitialSession != nil && *a.InitialSession != "" {
|
||||||
|
cmds = append(cmds, func() tea.Msg {
|
||||||
|
// Find the session by ID
|
||||||
|
sessions, err := a.ListSessions(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("Failed to list sessions for initial session", "error", err)
|
||||||
|
return toast.NewErrorToast("Failed to load initial session")()
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, session := range sessions {
|
||||||
|
if session.ID == *a.InitialSession {
|
||||||
|
return SessionSelectedMsg(&session)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
slog.Warn("Initial session not found", "sessionID", *a.InitialSession)
|
||||||
|
return toast.NewErrorToast("Session not found: " + *a.InitialSession)()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
if a.InitialPrompt != nil && *a.InitialPrompt != "" {
|
if a.InitialPrompt != nil && *a.InitialPrompt != "" {
|
||||||
cmds = append(cmds, util.CmdHandler(SendPrompt{Text: *a.InitialPrompt}))
|
cmds = append(cmds, util.CmdHandler(SendPrompt{Text: *a.InitialPrompt}))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user