Unify model changes and shell command argument handling

This commit is contained in:
Ian Gulliver
2026-10-20 22:53:47 -07:00
parent ba675f9a5a
commit a2de021f3c
6 changed files with 43 additions and 55 deletions
+17 -9
View File
@@ -21,6 +21,20 @@ type bashArgs struct {
TimeoutMs *int `json:"timeout_ms"`
}
func (a bashArgs) timeout() (time.Duration, error) {
if strings.TrimSpace(a.Command) == "" {
return 0, errors.New("command is empty")
}
timeout := defaultBashTimeout
if a.TimeoutMs != nil {
timeout = time.Duration(*a.TimeoutMs) * time.Millisecond
}
if timeout <= 0 || timeout > maxBashTimeout {
return 0, fmt.Errorf("timeout_ms must be between 1 and %d", maxBashTimeout.Milliseconds())
}
return timeout, nil
}
func (m *Manager) bashTool(l *live) mcp.Tool {
return mcp.Tool{
Name: "bash",
@@ -47,15 +61,9 @@ func (m *Manager) bash(ctx context.Context, l *live, raw json.RawMessage) (strin
if err != nil {
return "", fmt.Errorf("malformed bash call: %w", err)
}
if strings.TrimSpace(args.Command) == "" {
return "", errors.New("command is empty")
}
timeout := defaultBashTimeout
if args.TimeoutMs != nil {
timeout = time.Duration(*args.TimeoutMs) * time.Millisecond
}
if timeout <= 0 || timeout > maxBashTimeout {
return "", fmt.Errorf("timeout_ms must be between 1 and %d", maxBashTimeout.Milliseconds())
timeout, err := args.timeout()
if err != nil {
return "", err
}
task, err := m.store.GetTask(ctx, l.taskID)
if err != nil {
+4 -11
View File
@@ -257,8 +257,7 @@ func (m *Manager) listClients() string {
type clientBashArgs struct {
Client string `json:"client"`
Command string `json:"command"`
TimeoutMs *int `json:"timeout_ms"`
bashArgs
}
func (m *Manager) clientBash(ctx context.Context, raw json.RawMessage) (string, error) {
@@ -267,15 +266,9 @@ func (m *Manager) clientBash(ctx context.Context, raw json.RawMessage) (string,
if err != nil {
return "", fmt.Errorf("malformed client_bash call: %w", err)
}
if strings.TrimSpace(args.Command) == "" {
return "", errors.New("command is empty")
}
timeout := defaultBashTimeout
if args.TimeoutMs != nil {
timeout = time.Duration(*args.TimeoutMs) * time.Millisecond
}
if timeout <= 0 || timeout > maxBashTimeout {
return "", fmt.Errorf("timeout_ms must be between 1 and %d", maxBashTimeout.Milliseconds())
timeout, err := args.timeout()
if err != nil {
return "", err
}
exitCode, output, err := m.runClientBash(ctx, args.Client, args.Command, timeout)
+1 -8
View File
@@ -147,17 +147,10 @@ func (m *Manager) MessageLead(ctx context.Context, user api.User, text, model, s
}
}
if model != "" && model != sess.Model {
err = checkModel(sess, model)
sess, err = m.changeModel(ctx, sess, model, user.Login)
if err != nil {
return api.AgentSession{}, err
}
err = m.store.SetSessionModel(ctx, sess.Id, model, user.Login)
if err != nil {
return api.AgentSession{}, err
}
sess.Model = model
}
err = m.store.AddLeadInput(ctx, store.LeadInput{Owner: user.Login, Kind: store.LeadMessage, Actor: user.Login, Text: text, Images: images, SendID: sendID})
if err != nil {
return api.AgentSession{}, err
+1 -8
View File
@@ -262,17 +262,10 @@ func (m *Manager) Send(ctx context.Context, sessionID, actor, text, model, sendI
return api.AgentSession{}, fmt.Errorf("%w: session %s is %s", ErrConflict, sess.Id, sess.State)
}
if model != "" && model != sess.Model {
err = checkModel(sess, model)
sess, err = m.changeModel(ctx, sess, model, actor)
if err != nil {
return api.AgentSession{}, err
}
err = m.store.SetSessionModel(ctx, sess.Id, model, actor)
if err != nil {
return api.AgentSession{}, err
}
sess.Model = model
}
task, err := m.taskFor(ctx, sess)
if err != nil {
+12 -4
View File
@@ -50,11 +50,19 @@ func initialVendorSessionID(vendor api.AgentSessionVendor, id string) string {
return id
}
func checkModel(sess api.AgentSession, model string) error {
if vendorOf(model) != sess.Vendor {
return fmt.Errorf("%w: session %s is %s; %s is a %s model", ErrInvalid, sess.Id, sess.Vendor, model, vendorOf(model))
func (m *Manager) changeModel(ctx context.Context, sess api.AgentSession, model, actor string) (api.AgentSession, error) {
if model == "" || model == sess.Model {
return sess, nil
}
return nil
if vendorOf(model) != sess.Vendor {
return api.AgentSession{}, fmt.Errorf("%w: session %s is %s; %s is a %s model", ErrInvalid, sess.Id, sess.Vendor, model, vendorOf(model))
}
err := m.store.SetSessionModel(ctx, sess.Id, model, actor)
if err != nil {
return api.AgentSession{}, err
}
sess.Model = model
return sess, nil
}
type claudeProcess struct {
+1 -8
View File
@@ -64,17 +64,10 @@ func (m *Manager) Message(ctx context.Context, sessionID, actor, text, model, se
return sess, false, err
}
if model != "" && model != sess.Model {
err = checkModel(sess, model)
sess, err = m.changeModel(ctx, sess, model, actor)
if err != nil {
return api.AgentSession{}, false, err
}
err = m.store.SetSessionModel(ctx, sess.Id, model, actor)
if err != nil {
return api.AgentSession{}, false, err
}
sess.Model = model
}
task, err := m.store.GetTask(ctx, sess.TaskId)
if err != nil {