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, 03:37 UTC
| 1 | 'use client'; |
| 2 | |
| 3 | import { createContext, Fragment, use, useState } from 'react'; |
| 4 | import { |
| 5 | Collapsible, |
| 6 | CollapsibleContent, |
| 7 | CollapsibleTrigger, |
| 8 | } from 'fumadocs-ui/components/ui/collapsible'; |
| 9 | import { cn } from '@/lib/utils'; |
| 10 | import { Plus, X } from 'lucide-react'; |
| 11 | import type { SchemaData, SchemaUIGeneratedData } from './schema-generator'; |
| 12 | import { ExperimentalBadge } from './experimental-badge'; |
| 13 | |
| 14 | interface SchemaUIProps { |
| 15 | name: string; |
| 16 | required?: boolean; |
| 17 | as?: 'property' | 'body'; |
| 18 | generated: SchemaUIGeneratedData; |
| 19 | isResponse?: boolean; |
| 20 | } |
| 21 | |
| 22 | const DataContext = createContext<SchemaUIGeneratedData | null>(null); |
| 23 | const ResponseContext = createContext(false); |
| 24 | |
| 25 | function useData() { |
| 26 | const ctx = use(DataContext); |
| 27 | if (!ctx) throw new Error('Missing DataContext'); |
| 28 | return ctx; |
| 29 | } |
| 30 | |
| 31 | function useIsResponse() { |
| 32 | return use(ResponseContext); |
| 33 | } |
| 34 | |
| 35 | export function CustomSchemaUI({ |
| 36 | name, |
| 37 | required = false, |
| 38 | as = 'property', |
| 39 | generated, |
| 40 | isResponse = false, |
| 41 | }: SchemaUIProps) { |
| 42 | const schema = generated.refs[generated.$root]; |
| 43 | const isProperty = as === 'property' || !isExpandable(schema, generated.refs); |
| 44 | |
| 45 | return ( |
| 46 | <DataContext value={generated}> |
| 47 | <ResponseContext value={isResponse}> |
| 48 | {isProperty ? ( |
| 49 | <SchemaProperty |
| 50 | name={name} |
| 51 | $type={generated.$root} |
| 52 | required={required} |
| 53 | isRoot |
| 54 | /> |
| 55 | ) : ( |
| 56 | <SchemaContent $type={generated.$root} /> |
| 57 | )} |
| 58 | </ResponseContext> |
| 59 | </DataContext> |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | function SchemaContent({ |
| 64 | $type, |
| 65 | parentPath = '', |
| 66 | }: { |
| 67 | $type: string; |
| 68 | parentPath?: string; |
| 69 | }) { |
| 70 | const { refs } = useData(); |
| 71 | const schema = refs[$type]; |
| 72 | |
| 73 | if (schema.type === 'object' && schema.props.length > 0) { |
| 74 | return ( |
| 75 | <div className="divide-y divide-fd-border"> |
| 76 | {schema.props.map((prop) => ( |
| 77 | <SchemaProperty |
| 78 | key={prop.name} |
| 79 | name={prop.name} |
| 80 | $type={prop.$type} |
| 81 | required={prop.required} |
| 82 | parentPath={parentPath} |
| 83 | /> |
| 84 | ))} |
| 85 | </div> |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | if (schema.type === 'array') { |
| 90 | return <SchemaContent $type={schema.item.$type} parentPath={parentPath} />; |
| 91 | } |
| 92 | |
| 93 | if ((schema.type === 'or' || schema.type === 'and') && schema.items.length > 0) { |
| 94 | const label = schema.type === 'or' ? 'One of:' : 'All of:'; |
| 95 | return ( |
| 96 | <div className="space-y-2"> |
| 97 | <p className="text-xs text-fd-muted-foreground">{label}</p> |
| 98 | {schema.items.map((item, i) => ( |
| 99 | <div key={`${item.$type}-${i}`} className="pl-3 border-l-2 border-fd-border"> |
| 100 | <span className="text-sm font-medium">{item.name}</span> |
| 101 | {isExpandable(refs[item.$type], refs) && ( |
| 102 | <ExpandableContent $type={item.$type} parentPath={parentPath} /> |
| 103 | )} |
| 104 | </div> |
| 105 | ))} |
| 106 | </div> |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | return null; |
| 111 | } |
| 112 | |
| 113 | function SchemaProperty({ |
| 114 | name, |
| 115 | $type, |
| 116 | required, |
| 117 | parentPath = '', |
| 118 | isRoot = false, |
| 119 | }: { |
| 120 | name: string; |
| 121 | $type: string; |
| 122 | required: boolean; |
| 123 | parentPath?: string; |
| 124 | isRoot?: boolean; |
| 125 | }) { |
| 126 | const { refs } = useData(); |
| 127 | const isResponse = useIsResponse(); |
| 128 | const schema = refs[$type]; |
| 129 | const fullPath = parentPath ? `${parentPath}.${name}` : name; |
| 130 | |
| 131 | const hasChildren = isExpandable(schema, refs); |
| 132 | const typeDisplay = getTypeDisplay(schema); |
| 133 | |
| 134 | return ( |
| 135 | <div className={cn('py-4', !isRoot && 'first:pt-0')}> |
| 136 | {/* Property header */} |
| 137 | <div className="flex flex-wrap items-center gap-2"> |
| 138 | <span className="font-medium font-mono text-fd-foreground"> |
| 139 | {name} |
| 140 | </span> |
| 141 | <span className="text-sm font-mono text-fd-muted-foreground"> |
| 142 | {typeDisplay} |
| 143 | </span> |
| 144 | {required && !isResponse && ( |
| 145 | <span className="text-xs text-red-400 font-medium">Required</span> |
| 146 | )} |
| 147 | {schema.deprecated && ( |
| 148 | <span className="text-xs bg-yellow-500/10 text-yellow-600 dark:text-yellow-400 px-1.5 py-0.5 rounded"> |
| 149 | Deprecated |
| 150 | </span> |
| 151 | )} |
| 152 | {schema.experimental && <ExperimentalBadge />} |
| 153 | </div> |
| 154 | |
| 155 | {/* Description */} |
| 156 | {schema.description && ( |
| 157 | <div className="mt-2 text-sm text-fd-muted-foreground prose-no-margin"> |
| 158 | {schema.description} |
| 159 | </div> |
| 160 | )} |
| 161 | |
| 162 | {/* Info tags */} |
| 163 | {schema.infoTags && schema.infoTags.length > 0 && ( |
| 164 | <div className="flex flex-row gap-2 flex-wrap mt-2"> |
| 165 | {schema.infoTags.map((tag, i) => ( |
| 166 | <Fragment key={i}>{tag}</Fragment> |
| 167 | ))} |
| 168 | </div> |
| 169 | )} |
| 170 | |
| 171 | {/* Enum values */} |
| 172 | {schema.enumValues && schema.enumValues.length > 0 && ( |
| 173 | <EnumValues values={schema.enumValues} /> |
| 174 | )} |
| 175 | |
| 176 | {/* Expandable child attributes */} |
| 177 | {hasChildren && ( |
| 178 | <ExpandableContent $type={$type} parentPath={fullPath} /> |
| 179 | )} |
| 180 | </div> |
| 181 | ); |
| 182 | } |
| 183 | |
| 184 | function EnumValues({ values }: { values: string[] }) { |
| 185 | return ( |
| 186 | <div className="mt-2"> |
| 187 | <span className="text-xs text-fd-muted-foreground">Possible values:</span> |
| 188 | <div className="mt-1.5 flex flex-wrap gap-1.5"> |
| 189 | {values.map((value) => ( |
| 190 | <code |
| 191 | key={value} |
| 192 | className="rounded border border-fd-border px-1.5 py-0.5 text-xs font-mono text-fd-muted-foreground" |
| 193 | > |
| 194 | {value} |
| 195 | </code> |
| 196 | ))} |
| 197 | </div> |
| 198 | </div> |
| 199 | ); |
| 200 | } |
| 201 | |
| 202 | function ExpandableContent({ |
| 203 | $type, |
| 204 | parentPath, |
| 205 | }: { |
| 206 | $type: string; |
| 207 | parentPath: string; |
| 208 | }) { |
| 209 | const [isOpen, setIsOpen] = useState(false); |
| 210 | const { refs } = useData(); |
| 211 | const schema = refs[$type]; |
| 212 | |
| 213 | const childCount = getChildCount(schema); |
| 214 | const label = schema.type === 'array' ? 'item properties' : 'child attributes'; |
| 215 | |
| 216 | return ( |
| 217 | <Collapsible open={isOpen} onOpenChange={setIsOpen} className="mt-3"> |
| 218 | <CollapsibleTrigger className="group flex items-center gap-1 px-2 py-1 text-xs text-fd-muted-foreground hover:text-fd-foreground font-medium rounded border border-fd-border hover:bg-fd-accent/30 transition-colors"> |
| 219 | {isOpen ? ( |
| 220 | <> |
| 221 | <X className="h-3 w-3" /> |
| 222 | Hide {label} |
| 223 | </> |
| 224 | ) : ( |
| 225 | <> |
| 226 | <Plus className="h-3 w-3" /> |
| 227 | Show {childCount > 0 ? `${childCount} ` : ''}{label} |
| 228 | </> |
| 229 | )} |
| 230 | </CollapsibleTrigger> |
| 231 | <CollapsibleContent> |
| 232 | <div className="mt-2 pl-3 border-l border-fd-border"> |
| 233 | <SchemaContent $type={$type} parentPath={parentPath} /> |
| 234 | </div> |
| 235 | </CollapsibleContent> |
| 236 | </Collapsible> |
| 237 | ); |
| 238 | } |
| 239 | |
| 240 | function isExpandable( |
| 241 | schema: SchemaData, |
| 242 | refs?: Record<string, SchemaData>, |
| 243 | visited: Set<string> = new Set() |
| 244 | ): boolean { |
| 245 | if (schema.type === 'object' && schema.props.length > 0) return true; |
| 246 | if (schema.type === 'array') { |
| 247 | // Only expandable if items have structure (object/nested) |
| 248 | if (!refs) return true; |
| 249 | const itemType = schema.item.$type; |
| 250 | if (visited.has(itemType)) return false; // Circular ref - not expandable |
| 251 | const itemSchema = refs[itemType]; |
| 252 | if (!itemSchema) return false; |
| 253 | return itemSchema.type !== 'primitive'; |
| 254 | } |
| 255 | if ((schema.type === 'or' || schema.type === 'and') && schema.items.length > 0) { |
| 256 | // Only expandable if at least one variant has nested structure |
| 257 | if (!refs) return true; |
| 258 | return schema.items.some((item) => { |
| 259 | if (visited.has(item.$type)) return false; // Circular ref - not expandable |
| 260 | const itemSchema = refs[item.$type]; |
| 261 | if (!itemSchema) return false; |
| 262 | visited.add(item.$type); |
| 263 | return isExpandable(itemSchema, refs, visited); |
| 264 | }); |
| 265 | } |
| 266 | return false; |
| 267 | } |
| 268 | |
| 269 | function getTypeDisplay(schema: SchemaData): string { |
| 270 | if (schema.type === 'array') { |
| 271 | return `array of ${schema.aliasName}`; |
| 272 | } |
| 273 | return schema.typeName; |
| 274 | } |
| 275 | |
| 276 | function getChildCount(schema: SchemaData): number { |
| 277 | if (schema.type === 'object') return schema.props.length; |
| 278 | if (schema.type === 'or' || schema.type === 'and') return schema.items.length; |
| 279 | return 0; |
| 280 | } |