Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { ASSERTED_STRING_FORMATS, type StringFormat } from './backend/types'; |
| 2 | import type { JsonSchemaObject, Serializable } from './types'; |
| 3 | import { its } from './utils/its'; |
| 4 | |
| 5 | const ASSERTED_FORMAT_SET: ReadonlySet<StringFormat> = new Set<StringFormat>( |
| 6 | ASSERTED_STRING_FORMATS |
| 7 | ); |
| 8 | |
| 9 | export const isAssertedStringFormat = (format: unknown): format is StringFormat => |
| 10 | typeof format === 'string' && ASSERTED_FORMAT_SET.has(format as StringFormat); |
| 11 | |
| 12 | /** |
| 13 | * The description text a node carries: its `description`, else its `title`, |
| 14 | * followed by the `example` or `examples` it declares. `undefined` when there |
| 15 | * is nothing to say. |
| 16 | */ |
| 17 | export const describeText = (jsonSchema: JsonSchemaObject): string | undefined => { |
| 18 | let description = ''; |
| 19 | |
| 20 | if (jsonSchema.description) { |
| 21 | description = jsonSchema.description; |
| 22 | } else if (jsonSchema.title) { |
| 23 | description = jsonSchema.title; |
| 24 | } |
| 25 | |
| 26 | if (jsonSchema.example !== undefined) { |
| 27 | const exampleText = `Example: ${JSON.stringify(jsonSchema.example)}`; |
| 28 | description = description ? `${description}\n${exampleText}` : exampleText; |
| 29 | } else if (jsonSchema.examples !== undefined && Array.isArray(jsonSchema.examples)) { |
| 30 | const examples = jsonSchema.examples; |
| 31 | if (examples.length > 0) { |
| 32 | const exampleText = |
| 33 | examples.length === 1 |
| 34 | ? `Example: ${JSON.stringify(examples[0])}` |
| 35 | : `Examples:\n${examples.map((ex: unknown) => ` ${JSON.stringify(ex)}`).join('\n')}`; |
| 36 | description = description ? `${description}\n${exampleText}` : exampleText; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | return description || undefined; |
| 41 | }; |
| 42 | |
| 43 | /** Whether the number parser applies its integer check to this node. */ |
| 44 | const isIntegerNumber = (jsonSchema: JsonSchemaObject): boolean => |
| 45 | its.a.primitive(jsonSchema, 'integer') || |
| 46 | (its.a.primitive(jsonSchema, 'number') && |
| 47 | (jsonSchema.format === 'int64' || jsonSchema.multipleOf === 1)); |
| 48 | |
| 49 | /** |
| 50 | * The JSON Schema annotations a node carries beyond what its checks imply: |
| 51 | * `format`, `contentEncoding`, and `contentMediaType` verbatim, and |
| 52 | * `type: "integer"` wherever the integer check applies, since that check is |
| 53 | * a refinement a serializer cannot see. Everything else the serializer |
| 54 | * derives from the checks themselves, or is deliberately left out. |
| 55 | */ |
| 56 | export const collectAnnotations = ( |
| 57 | jsonSchema: JsonSchemaObject |
| 58 | ): Readonly<Record<string, Serializable>> | undefined => { |
| 59 | // Draft 7 ignores the keywords next to `$ref`, so none of them describe the value. |
| 60 | if ('$ref' in jsonSchema) { |
| 61 | return undefined; |
| 62 | } |
| 63 | |
| 64 | const annotations: Record<string, Serializable> = {}; |
| 65 | |
| 66 | if (typeof jsonSchema.format === 'string') { |
| 67 | annotations.format = jsonSchema.format; |
| 68 | } |
| 69 | if (typeof jsonSchema.contentEncoding === 'string') { |
| 70 | annotations.contentEncoding = jsonSchema.contentEncoding; |
| 71 | } |
| 72 | if (typeof jsonSchema.contentMediaType === 'string') { |
| 73 | annotations.contentMediaType = jsonSchema.contentMediaType; |
| 74 | } |
| 75 | if (isIntegerNumber(jsonSchema)) { |
| 76 | annotations.type = 'integer'; |
| 77 | } |
| 78 | |
| 79 | return Object.keys(annotations).length > 0 ? annotations : undefined; |
| 80 | }; |