Skip to content

Commit af7260d

Browse files
Merge pull request #852 from ibuildthecloud/chat-tools
chore: support chattable code tools
2 parents f6dcb5f + f30e865 commit af7260d

File tree

3 files changed

+48
-21
lines changed

3 files changed

+48
-21
lines changed

pkg/engine/cmd.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,14 @@ func compressEnv(envs []string) (result []string) {
6868
func (e *Engine) runCommand(ctx Context, tool types.Tool, input string, toolCategory ToolCategory) (cmdOut string, cmdErr error) {
6969
id := counter.Next()
7070

71+
var combinedOutput string
7172
defer func() {
7273
e.Progress <- types.CompletionStatus{
7374
CompletionID: id,
7475
Response: map[string]any{
75-
"output": cmdOut,
76-
"err": cmdErr,
76+
"output": cmdOut,
77+
"fullOutput": combinedOutput,
78+
"err": cmdErr,
7779
},
7880
}
7981
}()
@@ -156,9 +158,11 @@ func (e *Engine) runCommand(ctx Context, tool types.Tool, input string, toolCate
156158
return fmt.Sprintf("ERROR: got (%v) while running tool, OUTPUT: %s", err, stdoutAndErr), nil
157159
}
158160
log.Errorf("failed to run tool [%s] cmd %v: %v", tool.Parameters.Name, cmd.Args, err)
161+
combinedOutput = stdoutAndErr.String()
159162
return "", fmt.Errorf("ERROR: %s: %w", result, err)
160163
}
161164

165+
combinedOutput = stdoutAndErr.String()
162166
return result.String(), IsChatFinishMessage(result.String())
163167
}
164168

pkg/engine/engine.go

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,25 @@ func populateMessageParams(ctx Context, completion *types.CompletionRequest, too
281281
return nil
282282
}
283283

284+
func (e *Engine) runCommandTools(ctx Context, tool types.Tool, input string) (*Return, error) {
285+
if tool.IsHTTP() {
286+
return e.runHTTP(ctx.Ctx, ctx.Program, tool, input)
287+
} else if tool.IsDaemon() {
288+
return e.runDaemon(ctx.Ctx, ctx.Program, tool, input)
289+
} else if tool.IsOpenAPI() {
290+
return e.runOpenAPI(tool, input)
291+
} else if tool.IsEcho() {
292+
return e.runEcho(tool)
293+
}
294+
s, err := e.runCommand(ctx, tool, input, ctx.ToolCategory)
295+
if err != nil {
296+
return nil, err
297+
}
298+
return &Return{
299+
Result: &s,
300+
}, nil
301+
}
302+
284303
func (e *Engine) Start(ctx Context, input string) (ret *Return, _ error) {
285304
tool := ctx.Tool
286305

@@ -291,22 +310,7 @@ func (e *Engine) Start(ctx Context, input string) (ret *Return, _ error) {
291310
}()
292311

293312
if tool.IsCommand() {
294-
if tool.IsHTTP() {
295-
return e.runHTTP(ctx.Ctx, ctx.Program, tool, input)
296-
} else if tool.IsDaemon() {
297-
return e.runDaemon(ctx.Ctx, ctx.Program, tool, input)
298-
} else if tool.IsOpenAPI() {
299-
return e.runOpenAPI(tool, input)
300-
} else if tool.IsEcho() {
301-
return e.runEcho(tool)
302-
}
303-
s, err := e.runCommand(ctx, tool, input, ctx.ToolCategory)
304-
if err != nil {
305-
return nil, err
306-
}
307-
return &Return{
308-
Result: &s,
309-
}, nil
313+
return e.runCommandTools(ctx, tool, input)
310314
}
311315

312316
if ctx.ToolCategory == CredentialToolCategory {
@@ -431,6 +435,14 @@ func (e *Engine) complete(ctx context.Context, state *State) (*Return, error) {
431435
}
432436

433437
func (e *Engine) Continue(ctx Context, state *State, results ...CallResult) (*Return, error) {
438+
if ctx.Tool.IsCommand() {
439+
var input string
440+
if len(results) == 1 {
441+
input = results[0].User
442+
}
443+
return e.runCommandTools(ctx, ctx.Tool, input)
444+
}
445+
434446
if state == nil {
435447
return nil, fmt.Errorf("invalid continue call, missing state")
436448
}

pkg/runner/runner.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ func (r *Runner) Chat(ctx context.Context, prevState ChatState, prg types.Progra
179179
}
180180
} else {
181181
state = state.WithResumeInput(&input)
182-
state.ResumeInput = &input
183182
}
184183

185184
state, err = r.resume(callCtx, monitor, env, state)
@@ -683,7 +682,13 @@ func (r *Runner) subCall(ctx context.Context, parentContext engine.Context, moni
683682
}, nil
684683
}
685684

686-
return r.call(callCtx, monitor, env, input)
685+
state, err := r.call(callCtx, monitor, env, input)
686+
if finishErr := (*engine.ErrChatFinish)(nil); errors.As(err, &finishErr) && callCtx.Tool.Chat {
687+
return &State{
688+
Result: &finishErr.Message,
689+
}, nil
690+
}
691+
return state, err
687692
}
688693

689694
func (r *Runner) subCallResume(ctx context.Context, parentContext engine.Context, monitor Monitor, env []string, toolID, callID string, state *State, toolCategory engine.ToolCategory) (*State, error) {
@@ -692,7 +697,13 @@ func (r *Runner) subCallResume(ctx context.Context, parentContext engine.Context
692697
return nil, err
693698
}
694699

695-
return r.resume(callCtx, monitor, env, state)
700+
state, err = r.resume(callCtx, monitor, env, state)
701+
if finishErr := (*engine.ErrChatFinish)(nil); errors.As(err, &finishErr) && callCtx.Tool.Chat {
702+
return &State{
703+
Result: &finishErr.Message,
704+
}, nil
705+
}
706+
return state, err
696707
}
697708

698709
type SubCallResult struct {

0 commit comments

Comments
 (0)