stainless-helper-header.mjs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * Shared utilities for tracking SDK helper usage.
  3. */
  4. /**
  5. * Symbol used to mark objects created by SDK helpers for tracking.
  6. * The value is the helper name (e.g., 'mcpTool', 'betaZodTool').
  7. */
  8. export const SDK_HELPER_SYMBOL = Symbol('anthropic.sdk.stainlessHelper');
  9. export function wasCreatedByStainlessHelper(value) {
  10. return typeof value === 'object' && value !== null && SDK_HELPER_SYMBOL in value;
  11. }
  12. /**
  13. * Collects helper names from tools and messages arrays.
  14. * Returns a deduplicated array of helper names found.
  15. */
  16. export function collectStainlessHelpers(tools, messages) {
  17. const helpers = new Set();
  18. // Collect from tools
  19. if (tools) {
  20. for (const tool of tools) {
  21. if (wasCreatedByStainlessHelper(tool)) {
  22. helpers.add(tool[SDK_HELPER_SYMBOL]);
  23. }
  24. }
  25. }
  26. // Collect from messages and their content blocks
  27. if (messages) {
  28. for (const message of messages) {
  29. if (wasCreatedByStainlessHelper(message)) {
  30. helpers.add(message[SDK_HELPER_SYMBOL]);
  31. }
  32. if (Array.isArray(message.content)) {
  33. for (const block of message.content) {
  34. if (wasCreatedByStainlessHelper(block)) {
  35. helpers.add(block[SDK_HELPER_SYMBOL]);
  36. }
  37. }
  38. }
  39. }
  40. }
  41. return Array.from(helpers);
  42. }
  43. /**
  44. * Builds x-stainless-helper header value from tools and messages.
  45. * Returns an empty object if no helpers are found.
  46. */
  47. export function stainlessHelperHeader(tools, messages) {
  48. const helpers = collectStainlessHelpers(tools, messages);
  49. if (helpers.length === 0)
  50. return {};
  51. return { 'x-stainless-helper': helpers.join(', ') };
  52. }
  53. /**
  54. * Builds x-stainless-helper header value from a file object.
  55. * Returns an empty object if the file is not marked with a helper.
  56. */
  57. export function stainlessHelperHeaderFromFile(file) {
  58. if (wasCreatedByStainlessHelper(file)) {
  59. return { 'x-stainless-helper': file[SDK_HELPER_SYMBOL] };
  60. }
  61. return {};
  62. }
  63. //# sourceMappingURL=stainless-helper-header.mjs.map