Skip to content

fix: run daemon tools as tools when referenced by other tools #943

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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