ToolError.mjs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. /**
  2. * An error that can be thrown from a tool's `run` method to return structured
  3. * content blocks as the error result, rather than just a string message.
  4. *
  5. * When the ToolRunner catches this error, it will use the `content` property
  6. * as the tool result with `is_error: true`.
  7. *
  8. * @example
  9. * ```ts
  10. * const tool = {
  11. * name: 'my_tool',
  12. * run: async (input) => {
  13. * if (somethingWentWrong) {
  14. * throw new ToolError([
  15. * { type: 'text', text: 'Error details here' },
  16. * { type: 'image', source: { type: 'base64', data: '...', media_type: 'image/png' } },
  17. * ]);
  18. * }
  19. * return 'success';
  20. * },
  21. * };
  22. * ```
  23. */
  24. export class ToolError extends Error {
  25. constructor(content) {
  26. const message = typeof content === 'string' ? content : (content
  27. .map((block) => {
  28. if (block.type === 'text')
  29. return block.text;
  30. return `[${block.type}]`;
  31. })
  32. .join(' '));
  33. super(message);
  34. this.name = 'ToolError';
  35. this.content = content;
  36. }
  37. }
  38. //# sourceMappingURL=ToolError.mjs.map