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 | import { createPatch } from 'diff'; |
| 2 | import { getSingularPatch } from '@pierre/diffs'; |
| 3 | import { preloadFileDiff } from '@pierre/diffs/ssr'; |
| 4 | import type { FileDiffProps } from '@pierre/diffs/react'; |
| 5 | import { DiffView, HIDE_DIFF_STATS_CSS } from './diff-view'; |
| 6 | import { FILE_BUILDS } from '@/lib/file-builds'; |
| 7 | |
| 8 | async function diffFor(file: string, prev: string, code: string) { |
| 9 | const fileDiff = getSingularPatch(createPatch(file, prev, code, '', '')); |
| 10 | const { prerenderedHTML } = await preloadFileDiff({ |
| 11 | fileDiff, |
| 12 | options: { diffStyle: 'unified', unsafeCSS: HIDE_DIFF_STATS_CSS }, |
| 13 | }); |
| 14 | return { fileDiff, prerenderedHTML }; |
| 15 | } |
| 16 | |
| 17 | function StepCard({ |
| 18 | n, |
| 19 | title, |
| 20 | file, |
| 21 | description, |
| 22 | fileDiff, |
| 23 | prerenderedHTML, |
| 24 | code, |
| 25 | }: { |
| 26 | n: number; |
| 27 | title: string; |
| 28 | file: string; |
| 29 | description?: string; |
| 30 | |
| 31 | fileDiff: FileDiffProps<undefined, undefined>['fileDiff']; |
| 32 | prerenderedHTML: string; |
| 33 | code: string; |
| 34 | }) { |
| 35 | return ( |
| 36 | <div className="overflow-hidden rounded-sm border border-fd-border bg-fd-background"> |
| 37 | <div className="flex items-center gap-2.5 border-b border-fd-border px-3 py-2"> |
| 38 | <span className="flex size-5 shrink-0 items-center justify-center rounded-sm bg-[var(--composio-brand)]/10 font-mono text-[11px] font-medium text-[var(--composio-brand)]"> |
| 39 | {n} |
| 40 | </span> |
| 41 | <span className="text-[13px] font-medium text-fd-foreground">{title}</span> |
| 42 | <span className="ml-auto font-mono text-[10px] uppercase tracking-[0.06em] text-fd-foreground/40"> |
| 43 | {file} |
| 44 | </span> |
| 45 | </div> |
| 46 | {description ? ( |
| 47 | <p className="px-3 pt-2.5 text-[13px] leading-snug text-fd-foreground/65">{description}</p> |
| 48 | ) : null} |
| 49 | <div className="p-3"> |
| 50 | <DiffView fileDiff={fileDiff} prerenderedHTML={prerenderedHTML} code={code} /> |
| 51 | </div> |
| 52 | </div> |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * FileBuildup — renders one of the example's files growing a piece at a time. |
| 58 | * Each step is a diff against the previous stage (powered by @pierre/diffs). |
| 59 | * |
| 60 | * Pass `step` (1-indexed) to render just that one step's diff, so the prose for |
| 61 | * a concept can sit right next to the code that adds it. Without `step`, renders |
| 62 | * every step with its built-in description. |
| 63 | * |
| 64 | * `name` keys into the merged FILE_BUILDS registry (see lib/file-builds.ts), |
| 65 | * which carries every example's stages under unique keys. |
| 66 | */ |
| 67 | export async function FileBuildup({ |
| 68 | name, |
| 69 | step, |
| 70 | }: { |
| 71 | name: keyof typeof FILE_BUILDS; |
| 72 | step?: number; |
| 73 | }) { |
| 74 | const build = FILE_BUILDS[name]; |
| 75 | if (!build) return null; |
| 76 | |
| 77 | if (typeof step === 'number') { |
| 78 | const i = step - 1; |
| 79 | const stage = build.stages[i]; |
| 80 | if (!stage) return null; |
| 81 | const prev = i > 0 ? build.stages[i - 1].code : ''; |
| 82 | const { fileDiff, prerenderedHTML } = await diffFor(build.file, prev, stage.code); |
| 83 | // The heading above and the diff's own filename header already name it, so |
| 84 | // render just the diff with no extra card chrome. |
| 85 | return ( |
| 86 | <div className="not-prose my-6"> |
| 87 | <DiffView fileDiff={fileDiff} prerenderedHTML={prerenderedHTML} code={stage.code} /> |
| 88 | </div> |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | const steps = []; |
| 93 | let prev = ''; |
| 94 | for (const stage of build.stages) { |
| 95 | steps.push({ ...stage, ...(await diffFor(build.file, prev, stage.code)) }); |
| 96 | prev = stage.code; |
| 97 | } |
| 98 | |
| 99 | return ( |
| 100 | <div className="not-prose my-6 flex flex-col gap-5"> |
| 101 | {steps.map((s, i) => ( |
| 102 | <StepCard |
| 103 | key={s.title} |
| 104 | n={i + 1} |
| 105 | title={s.title} |
| 106 | file={build.file} |
| 107 | description={s.description} |
| 108 | fileDiff={s.fileDiff} |
| 109 | prerenderedHTML={s.prerenderedHTML} |
| 110 | code={s.code} |
| 111 | /> |
| 112 | ))} |
| 113 | </div> |
| 114 | ); |
| 115 | } |