Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 04:45 UTC
| 1 | /** |
| 2 | * Anthropic-bound tool-schema key sanitization. |
| 3 | * |
| 4 | * The traversal/restoration mechanism lives in `@composio/core` |
| 5 | * (`sanitizeSchemaPropertyKeys` / `restoreOriginalKeys`); this module supplies the |
| 6 | * Anthropic key constraint as a {@link KeySanitizationPolicy} and re-exports a |
| 7 | * policy-bound sanitizer plus the restore side, so call sites in this package |
| 8 | * import everything from one place. |
| 9 | * |
| 10 | * Anthropic's Messages API validates every property key against |
| 11 | * `^[a-zA-Z0-9_.-]{1,64}$` and rejects the entire `tools` array on a single |
| 12 | * violation. Composio tools can surface keys that break this — OData parameters |
| 13 | * like `$top` / `@microsoft.graph.conflictBehavior` (illegal `$`/`@`), or |
| 14 | * over-long `__`-flattened keys. |
| 15 | * |
| 16 | * @module providers/anthropic/sanitize-keys |
| 17 | */ |
| 18 | import { |
| 19 | sanitizeSchemaPropertyKeys as sanitizeWithPolicy, |
| 20 | restoreOriginalKeys, |
| 21 | mappingHasRenames, |
| 22 | type KeyMapping, |
| 23 | type KeySanitizationPolicy, |
| 24 | } from '@composio/core'; |
| 25 | |
| 26 | export { restoreOriginalKeys, mappingHasRenames }; |
| 27 | export type { KeyMapping }; |
| 28 | |
| 29 | /** Anthropic's allowed property-key pattern. */ |
| 30 | const VALID_KEY_RE = /^[a-zA-Z0-9_.-]{1,64}$/; |
| 31 | |
| 32 | /** Maximum length Anthropic accepts for a property key. */ |
| 33 | const MAX_KEY_LENGTH = 64; |
| 34 | |
| 35 | /** |
| 36 | * Readable replacements for the two most common illegal characters — the OData |
| 37 | * prefixes Composio surfaces — so aliases stay recognizable on the wire (e.g. |
| 38 | * `$top` -> `dollar_top`, `@odata.type` -> `at_odata.type`). Any other illegal |
| 39 | * character falls back to `_` (see {@link sanitizeKey}). |
| 40 | */ |
| 41 | const ILLEGAL_CHAR_MAP: Record<string, string> = { |
| 42 | $: 'dollar_', |
| 43 | '@': 'at_', |
| 44 | }; |
| 45 | |
| 46 | /** |
| 47 | * Deterministic, dependency-free hash (djb2) rendered as a short base-36 string. |
| 48 | * Used as a uniqueness suffix when truncating long keys or resolving collisions. |
| 49 | */ |
| 50 | function shortHash(input: string): string { |
| 51 | let hash = 5381; |
| 52 | for (let i = 0; i < input.length; i++) { |
| 53 | hash = (hash * 33) ^ input.charCodeAt(i); |
| 54 | } |
| 55 | // Force unsigned and keep it short but collision-resistant enough for keys. |
| 56 | return (hash >>> 0).toString(36).slice(0, 7); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Rewrites a single key so it matches Anthropic's pattern. Replaces known illegal |
| 61 | * characters with readable aliases, any remaining illegal character with `_`, |
| 62 | * substitutes a non-empty fallback for an otherwise-empty result, and truncates |
| 63 | * keys over 64 characters with a deterministic hash suffix. |
| 64 | */ |
| 65 | function sanitizeKey(name: string): string { |
| 66 | let sanitized = name; |
| 67 | for (const [char, replacement] of Object.entries(ILLEGAL_CHAR_MAP)) { |
| 68 | sanitized = sanitized.split(char).join(replacement); |
| 69 | } |
| 70 | sanitized = sanitized.replace(/[^a-zA-Z0-9_.-]/g, '_'); |
| 71 | |
| 72 | // Anthropic's pattern requires at least one character; an empty original (or |
| 73 | // one made entirely of stripped characters) would otherwise emit `''`, which |
| 74 | // still fails validation. Fall back to a deterministic non-empty alias. |
| 75 | if (sanitized.length === 0) { |
| 76 | sanitized = `key_${shortHash(name)}`; |
| 77 | } |
| 78 | |
| 79 | if (sanitized.length > MAX_KEY_LENGTH) { |
| 80 | const suffix = `_${shortHash(name)}`; |
| 81 | sanitized = sanitized.slice(0, MAX_KEY_LENGTH - suffix.length) + suffix; |
| 82 | } |
| 83 | |
| 84 | return sanitized; |
| 85 | } |
| 86 | |
| 87 | /** The Anthropic key-sanitization policy consumed by core's sanitizer. */ |
| 88 | const anthropicKeyPolicy: KeySanitizationPolicy = { |
| 89 | isValidKey(name) { |
| 90 | return VALID_KEY_RE.test(name); |
| 91 | }, |
| 92 | |
| 93 | aliasKey(name, taken) { |
| 94 | const candidate = sanitizeKey(name); |
| 95 | if (!taken.has(candidate)) { |
| 96 | return candidate; |
| 97 | } |
| 98 | // Collision (different originals mapping to the same alias) — append a hash, |
| 99 | // then an incrementing counter if that still collides. The counter changes |
| 100 | // every iteration, so this always makes progress and can't spin. |
| 101 | const suffix = `_${shortHash(name)}`; |
| 102 | const base = candidate.slice(0, MAX_KEY_LENGTH - suffix.length); |
| 103 | let resolved = base + suffix; |
| 104 | for (let counter = 1; taken.has(resolved); counter++) { |
| 105 | const tail = `${suffix}_${counter}`; |
| 106 | resolved = base.slice(0, MAX_KEY_LENGTH - tail.length) + tail; |
| 107 | } |
| 108 | return resolved; |
| 109 | }, |
| 110 | }; |
| 111 | |
| 112 | /** |
| 113 | * Sanitizes a tool `input_schema` so every property key satisfies Anthropic's |
| 114 | * `^[a-zA-Z0-9_.-]{1,64}$` constraint, returning the rewritten schema and a |
| 115 | * {@link KeyMapping} for restoring the original keys at execution time. |
| 116 | * |
| 117 | * `inputParameters` should be dereferenced first (see `wrapTool`) so keys that |
| 118 | * are reachable only through a `$ref` are sanitized and restored as well. |
| 119 | */ |
| 120 | export function sanitizeSchemaPropertyKeys<T extends Record<string, unknown>>( |
| 121 | schema: T |
| 122 | ): { schema: T; mapping: KeyMapping } { |
| 123 | return sanitizeWithPolicy(schema, anthropicKeyPolicy); |
| 124 | } |