Unify model changes and shell command argument handling
This commit is contained in:
+17
-9
@@ -21,6 +21,20 @@ type bashArgs struct {
|
|||||||
TimeoutMs *int `json:"timeout_ms"`
|
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 {
|
func (m *Manager) bashTool(l *live) mcp.Tool {
|
||||||
return mcp.Tool{
|
return mcp.Tool{
|
||||||
Name: "bash",
|
Name: "bash",
|
||||||
@@ -47,15 +61,9 @@ func (m *Manager) bash(ctx context.Context, l *live, raw json.RawMessage) (strin
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("malformed bash call: %w", err)
|
return "", fmt.Errorf("malformed bash call: %w", err)
|
||||||
}
|
}
|
||||||
if strings.TrimSpace(args.Command) == "" {
|
timeout, err := args.timeout()
|
||||||
return "", errors.New("command is empty")
|
if err != nil {
|
||||||
}
|
return "", err
|
||||||
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())
|
|
||||||
}
|
}
|
||||||
task, err := m.store.GetTask(ctx, l.taskID)
|
task, err := m.store.GetTask(ctx, l.taskID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -257,8 +257,7 @@ func (m *Manager) listClients() string {
|
|||||||
|
|
||||||
type clientBashArgs struct {
|
type clientBashArgs struct {
|
||||||
Client string `json:"client"`
|
Client string `json:"client"`
|
||||||
Command string `json:"command"`
|
bashArgs
|
||||||
TimeoutMs *int `json:"timeout_ms"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Manager) clientBash(ctx context.Context, raw json.RawMessage) (string, error) {
|
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 {
|
if err != nil {
|
||||||
return "", fmt.Errorf("malformed client_bash call: %w", err)
|
return "", fmt.Errorf("malformed client_bash call: %w", err)
|
||||||
}
|
}
|
||||||
if strings.TrimSpace(args.Command) == "" {
|
timeout, err := args.timeout()
|
||||||
return "", errors.New("command is empty")
|
if err != nil {
|
||||||
}
|
return "", err
|
||||||
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())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
exitCode, output, err := m.runClientBash(ctx, args.Client, args.Command, timeout)
|
exitCode, output, err := m.runClientBash(ctx, args.Client, args.Command, timeout)
|
||||||
|
|||||||
@@ -147,17 +147,10 @@ func (m *Manager) MessageLead(ctx context.Context, user api.User, text, model, s
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if model != "" && model != sess.Model {
|
sess, err = m.changeModel(ctx, sess, model, user.Login)
|
||||||
err = checkModel(sess, model)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return api.AgentSession{}, err
|
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})
|
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 {
|
if err != nil {
|
||||||
return api.AgentSession{}, err
|
return api.AgentSession{}, err
|
||||||
|
|||||||
@@ -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)
|
return api.AgentSession{}, fmt.Errorf("%w: session %s is %s", ErrConflict, sess.Id, sess.State)
|
||||||
}
|
}
|
||||||
|
|
||||||
if model != "" && model != sess.Model {
|
sess, err = m.changeModel(ctx, sess, model, actor)
|
||||||
err = checkModel(sess, model)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return api.AgentSession{}, err
|
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)
|
task, err := m.taskFor(ctx, sess)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -50,11 +50,19 @@ func initialVendorSessionID(vendor api.AgentSessionVendor, id string) string {
|
|||||||
return id
|
return id
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkModel(sess api.AgentSession, model string) error {
|
func (m *Manager) changeModel(ctx context.Context, sess api.AgentSession, model, actor string) (api.AgentSession, error) {
|
||||||
if vendorOf(model) != sess.Vendor {
|
if model == "" || model == sess.Model {
|
||||||
return fmt.Errorf("%w: session %s is %s; %s is a %s model", ErrInvalid, sess.Id, sess.Vendor, model, vendorOf(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 {
|
type claudeProcess struct {
|
||||||
|
|||||||
@@ -64,17 +64,10 @@ func (m *Manager) Message(ctx context.Context, sessionID, actor, text, model, se
|
|||||||
return sess, false, err
|
return sess, false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if model != "" && model != sess.Model {
|
sess, err = m.changeModel(ctx, sess, model, actor)
|
||||||
err = checkModel(sess, model)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return api.AgentSession{}, false, err
|
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)
|
task, err := m.store.GetTask(ctx, sess.TaskId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user