Skip to content

Commit

Permalink
fix: run daemon tools as tools when referenced by other tools
Browse files Browse the repository at this point in the history
Daemon tools are usually started when other tools reference them by URL.
However, they are not run like other tools. Therefore, their credentials
are not processed as expected.

This change will detect when tools reference daemon tools and ensure
they are run like other tools.

Signed-off-by: Donnie Adams <[email protected]>
  • Loading branch information
thedadams committed Feb 2, 2025
1 parent 7ee5c80 commit 15cf300
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
26 changes: 18 additions & 8 deletions pkg/engine/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,11 @@ func (e *Engine) runHTTP(ctx context.Context, prg *types.Program, tool types.Too
}

if strings.HasSuffix(parsed.Hostname(), DaemonURLSuffix) {
referencedToolName := strings.TrimSuffix(parsed.Hostname(), DaemonURLSuffix)
referencedToolRefs, ok := tool.ToolMapping[referencedToolName]
if !ok || len(referencedToolRefs) != 1 {
return nil, fmt.Errorf("invalid reference [%s] to tool [%s] from [%s], missing \"tools: %s\" parameter", toolURL, referencedToolName, tool.Source, referencedToolName)
}
referencedTool, ok := prg.ToolSet[referencedToolRefs[0].ToolID]
if !ok {
return nil, fmt.Errorf("failed to find tool [%s] for [%s]", referencedToolName, parsed.Hostname())
referencedTool, err := DaemonTool(prg, tool, parsed.Hostname())
if err != nil {
return nil, err
}

toolURL, err = e.startDaemon(referencedTool)
if err != nil {
return nil, err
Expand Down Expand Up @@ -143,3 +139,17 @@ func (e *Engine) runHTTP(ctx context.Context, prg *types.Program, tool types.Too
Result: &s,
}, nil
}

func DaemonTool(prg *types.Program, tool types.Tool, daemonHost string) (types.Tool, error) {
referencedToolName := strings.TrimSuffix(daemonHost, DaemonURLSuffix)
referencedToolRefs, ok := tool.ToolMapping[referencedToolName]
if !ok || len(referencedToolRefs) != 1 {
return types.Tool{}, fmt.Errorf("invalid reference [%s] to tool [%s] from [%s], missing \"tools: %s\" parameter", daemonHost, referencedToolName, tool.Source, referencedToolName)
}
referencedTool, ok := prg.ToolSet[referencedToolRefs[0].ToolID]
if !ok {
return types.Tool{}, fmt.Errorf("failed to find tool [%s] for [%s]", referencedToolName, daemonHost)
}

return referencedTool, nil
}
22 changes: 22 additions & 0 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,17 @@ func (r *Runner) start(callCtx engine.Context, state *State, monitor Monitor, en
}
}

if toolName, _, ok := strings.Cut(strings.TrimSpace(callCtx.Tool.Instructions), engine.DaemonURLSuffix); callCtx.Tool.IsHTTP() && ok {
referencedTool, err := engine.DaemonTool(callCtx.Program, callCtx.Tool, strings.TrimPrefix(strings.TrimPrefix(toolName, "#!http://"), "#!https://"))
if err != nil {
return nil, err
}
res, err := r.subCall(callCtx.Ctx, callCtx, monitor, env, referencedTool.ID, "{}", "", engine.NoCategory)
if err != nil {
return res, err
}
}

ret, err := e.Start(callCtx, input)
if err != nil {
return nil, err
Expand Down Expand Up @@ -550,6 +561,17 @@ func (r *Runner) resume(callCtx engine.Context, monitor Monitor, env []string, s
})
}

if toolName, _, ok := strings.Cut(strings.TrimSpace(callCtx.Tool.Instructions), engine.DaemonURLSuffix); callCtx.Tool.IsHTTP() && ok {
referencedTool, err := engine.DaemonTool(callCtx.Program, callCtx.Tool, strings.TrimPrefix(strings.TrimPrefix(toolName, "#!http://"), "#!https://"))
if err != nil {
return nil, err
}
res, err := r.subCall(callCtx.Ctx, callCtx, monitor, env, referencedTool.ID, "{}", "", engine.NoCategory)
if err != nil {
return res, err
}
}

nextContinuation, err := e.Continue(callCtx, state.Continuation.State, engineResults...)
if err != nil {
return nil, err
Expand Down

0 comments on commit 15cf300

Please sign in to comment.