Skip to content

Conversation

eric642
Copy link
Contributor

@eric642 eric642 commented Sep 12, 2025

Add a function for judging tool interruption errors, which is convenient for scenarios where tools are called manually to determine interruption errors.

@eric642
Copy link
Contributor Author

eric642 commented Sep 15, 2025

@hugoaguirre @apascal07 please review it

@eric642
Copy link
Contributor Author

eric642 commented Sep 17, 2025

@apascal07 @hugoaguirre please review it

@apascal07
Copy link
Collaborator

apascal07 commented Sep 18, 2025

Can I ask how you're expecting to use this? toolInterruptError is not meant to be user facing. Tools are also not meant to be called directly, although they can. Generally that type of usage is meant for flows.

@eric642
Copy link
Contributor Author

eric642 commented Sep 22, 2025

@apascal07 I hope to be able to decide on the invocation of the tools by myself. When I use the option ai.WithReturnToolRequests(true), I need to invoke the tools myself.

resp, err := prompt.Execute(ctx,
		ai.WithInput(map[string]any{
			"date":     time.Now().Format(time.DateTime),
			"question": query,
		}),
		ai.WithTools(convert(reportTool.ReadFileTool(ctx), pptTool.GeneratePPTOutlineTool(g), pptTool.ModifyPPTPageTool(g), pptTool.InitializePPTTool())...),
		ai.WithReturnToolRequests(true),
		ai.WithModel(model),
		//ai.WithMaxTurns(math.MaxInt),
		ai.WithMiddleware(logReq()),
		ai.WithStreaming(stream),
		ai.WithConfig(openaigo.ChatCompletionNewParams{
			ReasoningEffort: shared.ReasoningEffortLow,
		}),
	)

When the user invokes the tool themselves, there needs to be a method to detect interruptions.

for _, req := range resp.ToolRequests() {
			tool := genkit.LookupTool(g, req.Name)
			if tool == nil {
				tool = genkit.LookupTool(g2, req.Name)
				if tool == nil {
					return "", fmt.Errorf("tool %s not found", req.Name)
				}
			}
			log.Info().Str("tool", req.Name).Any("input", req.Input).Msg("tool call")

			output, err := tool.RunRaw(ctx, req.Input)
			if err != nil {
				if meta, is := ai.IsToolInterruptError(err); is {
					return meta["value"].(string), nil
				}
				return "", fmt.Errorf("tool run failed:%v", err)
			}
			parts = append(parts, ai.NewToolResponsePart(&ai.ToolResponse{
				Name:   req.Name,
				Ref:    req.Ref,
				Output: output,
			}))
		}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

2 participants