Skip to content

Commit 15cf300

Browse files
committed
fix: run daemon tools as tools when referenced by other tools
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]>
1 parent 7ee5c80 commit 15cf300

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

pkg/engine/http.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,11 @@ func (e *Engine) runHTTP(ctx context.Context, prg *types.Program, tool types.Too
4141
}
4242

4343
if strings.HasSuffix(parsed.Hostname(), DaemonURLSuffix) {
44-
referencedToolName := strings.TrimSuffix(parsed.Hostname(), DaemonURLSuffix)
45-
referencedToolRefs, ok := tool.ToolMapping[referencedToolName]
46-
if !ok || len(referencedToolRefs) != 1 {
47-
return nil, fmt.Errorf("invalid reference [%s] to tool [%s] from [%s], missing \"tools: %s\" parameter", toolURL, referencedToolName, tool.Source, referencedToolName)
48-
}
49-
referencedTool, ok := prg.ToolSet[referencedToolRefs[0].ToolID]
50-
if !ok {
51-
return nil, fmt.Errorf("failed to find tool [%s] for [%s]", referencedToolName, parsed.Hostname())
44+
referencedTool, err := DaemonTool(prg, tool, parsed.Hostname())
45+
if err != nil {
46+
return nil, err
5247
}
48+
5349
toolURL, err = e.startDaemon(referencedTool)
5450
if err != nil {
5551
return nil, err
@@ -143,3 +139,17 @@ func (e *Engine) runHTTP(ctx context.Context, prg *types.Program, tool types.Too
143139
Result: &s,
144140
}, nil
145141
}
142+
143+
func DaemonTool(prg *types.Program, tool types.Tool, daemonHost string) (types.Tool, error) {
144+
referencedToolName := strings.TrimSuffix(daemonHost, DaemonURLSuffix)
145+
referencedToolRefs, ok := tool.ToolMapping[referencedToolName]
146+
if !ok || len(referencedToolRefs) != 1 {
147+
return types.Tool{}, fmt.Errorf("invalid reference [%s] to tool [%s] from [%s], missing \"tools: %s\" parameter", daemonHost, referencedToolName, tool.Source, referencedToolName)
148+
}
149+
referencedTool, ok := prg.ToolSet[referencedToolRefs[0].ToolID]
150+
if !ok {
151+
return types.Tool{}, fmt.Errorf("failed to find tool [%s] for [%s]", referencedToolName, daemonHost)
152+
}
153+
154+
return referencedTool, nil
155+
}

pkg/runner/runner.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,17 @@ func (r *Runner) start(callCtx engine.Context, state *State, monitor Monitor, en
350350
}
351351
}
352352

353+
if toolName, _, ok := strings.Cut(strings.TrimSpace(callCtx.Tool.Instructions), engine.DaemonURLSuffix); callCtx.Tool.IsHTTP() && ok {
354+
referencedTool, err := engine.DaemonTool(callCtx.Program, callCtx.Tool, strings.TrimPrefix(strings.TrimPrefix(toolName, "#!http://"), "#!https://"))
355+
if err != nil {
356+
return nil, err
357+
}
358+
res, err := r.subCall(callCtx.Ctx, callCtx, monitor, env, referencedTool.ID, "{}", "", engine.NoCategory)
359+
if err != nil {
360+
return res, err
361+
}
362+
}
363+
353364
ret, err := e.Start(callCtx, input)
354365
if err != nil {
355366
return nil, err
@@ -550,6 +561,17 @@ func (r *Runner) resume(callCtx engine.Context, monitor Monitor, env []string, s
550561
})
551562
}
552563

564+
if toolName, _, ok := strings.Cut(strings.TrimSpace(callCtx.Tool.Instructions), engine.DaemonURLSuffix); callCtx.Tool.IsHTTP() && ok {
565+
referencedTool, err := engine.DaemonTool(callCtx.Program, callCtx.Tool, strings.TrimPrefix(strings.TrimPrefix(toolName, "#!http://"), "#!https://"))
566+
if err != nil {
567+
return nil, err
568+
}
569+
res, err := r.subCall(callCtx.Ctx, callCtx, monitor, env, referencedTool.ID, "{}", "", engine.NoCategory)
570+
if err != nil {
571+
return res, err
572+
}
573+
}
574+
553575
nextContinuation, err := e.Continue(callCtx, state.Continuation.State, engineResults...)
554576
if err != nil {
555577
return nil, err

0 commit comments

Comments
 (0)