scripts — scripts
scripts — scripts
The scripts directory serves as the automation hub for the project, encompassing build processes, testing, maintenance, and utility tasks. Understanding these scripts is crucial for developers to contribute effectively, run local builds, and ensure code quality.
This document details the purpose, usage, and internal workings of the key scripts within this module.
1. Documentation & Book Generation
This set of scripts is responsible for compiling the project's documentation, particularly the "LLM Agents: Du Concept à la Production" book, into various formats and generating supporting assets.
scripts/build-book.sh
This is the primary script for generating the complete book in PDF, EPUB, and HTML formats. It orchestrates the entire build process, including prerequisite checks, content validation, file preparation, and format-specific generation.
Purpose: To provide a single entry point for building the book, ensuring consistency across different output formats.
Usage:
./scripts/build-book.sh # Build PDF + EPUB + HTML (default)
./scripts/build-book.sh pdf # Build PDF only
./scripts/build-book.sh epub # Build EPUB only
./scripts/build-book.sh html # Build HTML only
./scripts/build-book.sh validate # Run validation checks without building
./scripts/build-book.sh clean # Remove generated output files
./scripts/build-book.sh help # Show usage instructions
Prerequisites:
pandoc(version >= 2.19)xelatex(for PDF generation, part oftexlive-xetexandtexlive-fonts-extrapackages)- DejaVu fonts (for emoji support in PDF)
Execution Flow:
The main function dispatches to various helper functions based on the command-line argument.
check_prerequisites(): Verifiespandocandxelatexinstallations.validate_book(): Performs checks on the source Markdown files:
- Ensures all numbered chapters (00-19) are present.
- Verifies the existence of annexes (
annexe-a-transformers.md,annexe-b-projet-final.md,glossaire.md,bibliographie.md). - Counts SVG images.
- Provides word and estimated page counts.
prepare_files():
- Creates the
docs/livre/outputdirectory. - Combines all individual Markdown chapter files into a single
output/book-combined.mdfile, inserting\pagebreakdirectives between chapters for PDF formatting. The order of chapters is hardcoded within the script.
build_pdf(): Usespandocwithxelatexas the PDF engine to convertbook-combined.mdtollm-agents-book.pdf. It includes metadata (title, author, date), table of contents, section numbering, and custom fonts. It attempts a fallback build without custom fonts if the first attempt fails.build_epub(): Usespandocto convertbook-combined.mdtollm-agents-book.epub, applyingstyles/epub.cssif available.build_html(): Usespandocto convertbook-combined.mdtollm-agents-book.html, applyingtemplates/html.templateif available.cleanup(): Removes temporary LaTeX files (.aux,.log,.out).
graph TD
A[main "$@"] --> B{Command?}
B -- validate --> C[check_prerequisites]
C --> D[validate_book]
B -- pdf/epub/html/all --> C
D --> E[prepare_files]
E -- pdf --> F[build_pdf]
E -- epub --> G[build_epub]
E -- html --> H[build_html]
F --> I[cleanup]
G --> I
H --> I
B -- clean --> J[Remove output/]
B -- help --> K[show_help]
scripts/generate-pdf.sh, scripts/generate-epub.sh
These are older, standalone scripts for generating PDF and EPUB formats, respectively. They are largely superseded by build-book.sh but remain for historical context or specific needs. They directly call pandoc with hardcoded chapter lists and metadata.
scripts/generate-html.js
This Node.js script generates a single HTML file for the book. Unlike build-book.sh which uses pandoc for HTML, this script uses marked to convert Markdown to HTML and embeds custom CSS.
Purpose: To provide a simple, self-contained HTML version of the book, potentially for quick previews or environments where pandoc is not readily available.
Execution Flow:
- Reads a hardcoded list of Markdown chapter files from
docs/livre. - Uses
marked.parse()to convert each chapter's content to HTML. - Combines the HTML with embedded CSS into a single
livre-grok-cli.htmlfile.
scripts/nanobanana.ts
This TypeScript script is a custom diagram generator. It takes a list of diagram specifications and renders them as SVG files, which are then used in the book.
Purpose: To programmatically create consistent and stylized SVG diagrams (e.g., pyramids, flowcharts, box diagrams) based on structured data, avoiding manual SVG creation.
Execution Flow:
- Defines an array of
DiagramSpecobjects, each containing a name, type (e.g.,box,pyramid,flow), title, and specific data for the diagram. - The
generateSVG()function takes aDiagramSpecand renders it into an SVG string using hardcoded logic for each diagram type. - Writes the generated SVG strings to files in
docs/livre/images.
2. Testing & Validation
These scripts are vital for maintaining code quality, detecting issues early, and running comprehensive test suites.
scripts/check-circular-deps.ts
This script uses madge to detect circular dependencies within the TypeScript source code.
Purpose: To prevent architectural issues and maintain a clean dependency graph, which is crucial for code maintainability and understanding.
Execution Flow:
- Uses
madgeto analyze thesrcdirectory for.tsand.tsxfiles, respectingtsconfig.json. - Retrieves a list of all circular dependencies.
- Filters out "known cycles" defined in
KNOWN_CYCLES. These are intentional or currently accepted cycles. - If new circular dependencies are found, it prints them and exits with code
1; otherwise, it exits with0.
scripts/run-all-tests.ts
This is an "Ultra-Complete Real-Conditions Test Runner" designed to execute a vast array of test categories, including extended and advanced scenarios, against the Gemini API.
Purpose: To provide a comprehensive integration and end-to-end testing suite that simulates real-world conditions, ensuring the robustness and correctness of the Code Buddy agent and its components.
Usage:
npx tsx scripts/run-all-tests.ts
npx tsx scripts/run-all-tests.ts --extended-only # Run only categories 26-125
Prerequisites:
GOOGLE_API_KEYorGEMINI_API_KEYenvironment variable.tsxfor running TypeScript files directly.
Execution Flow:
- Initializes
GeminiProviderwith the API key. - Defines a
categoryPlanarray, which is a long list ofCategoryDefobjects. EachCategoryDefspecifies a test category name and a function that returns an array of individual tests. - Iterates through the
categoryPlan, running each category's tests usingrunCategory(). - Introduces
INTER_CATEGORY_DELAYto space out API calls and prevent rate limiting. - Collects results for all categories.
- Prints a final report summarizing total tests, passes, failures, skips, errors, token usage, and estimated API cost.
Key Components:
- Imports numerous test categories from
scripts/tests/*.tsfiles (e.g.,cat26ContextManagerV2,cat70GeminiStructuredOutput,cat125HookEventCoverage). - Uses
GeminiProviderfromsrc/providers/gemini-provider.tsfor API interactions. - Relies on
runCategoryandsleeputility functions (defined inscripts/tests/types.js).
scripts/real-conditions-burnin.ps1
This PowerShell script is designed for long-running "burn-in" tests, continuously executing various test suites over an extended period.
Purpose: To identify intermittent failures, memory leaks, or performance degradation under sustained load, simulating real-world continuous operation.
Usage:
.\scripts\real-conditions-burnin.ps1 -Hours 48 -Profile mixed -MaxCycles 1000
Parameters:
Hours: Duration of the campaign (default: 24).MaxCycles: Maximum number of test cycles to run (0 for unlimited).PauseSeconds: Delay between cycles.Profile:mixed(unit + e2e) ore2e-only.CoverageEveryNCycles: How often to take a code coverage snapshot.RepoRoot,LogRoot: Custom paths for the repository and logs.
Execution Flow:
- Sets up logging directories and files.
- Defines
e2eFiles(e.g.,test-e2e.mjs) that must exist. Restore-TodoFile: Manages thetodo.mdfile, restoring its initial state after each cycle.Get-CyclePlan: Determines which test suites to run in each cycle based on theProfileand currentCyclenumber. This includesnpm testfor unit/integration suites andnodefor e2e tests.- Enters a loop that runs for the specified
HoursorMaxCycles. - Inside the loop, it executes each step in the
CyclePlanusingInvoke-Step, capturing stdout/stderr. - If a step is
coverage-snapshot, it callsRead-CoverageSummaryto parsecoverage/coverage-summary.json. - Records detailed results for each cycle and step in JSONL files.
- Generates a final summary report.
scripts/check-raw.ts
This script generates and inspects raw documentation output from the Code Buddy's internal documentation generation system.
Purpose: To quickly verify the output of the internal documentation generation, particularly for API references and environment variables, without running the full build process.
Execution Flow:
- Initializes the
KnowledgeGraph(src/knowledge/knowledge-graph.js). - Populates the deep code graph (
src/knowledge/code-graph-deep-populator.js). - Generates documentation using
generateDocs()(src/docs/docs-generator.js). - Reads specific generated Markdown files (
.codebuddy/docs/9-api-reference.mdand8-configuration.md). - Prints excerpts of these files to the console for inspection.
3. Codebase Maintenance & Migration
These scripts are used for automated refactoring, fixing common issues, and migrating test files to new frameworks or conventions.
scripts/fix-all-tests.mjs
This comprehensive script applies multiple fixes to Vitest test files, primarily addressing ESM compatibility and common migration issues from Jest.
Purpose: To automate the conversion and cleanup of test files to be compatible with Vitest and ESM, reducing manual effort during framework transitions.
Fixes Applied (in order):
jest.setTimeout/vi.setTimeout→vi.setConfig({ testTimeout: ... })jest.requireActual/vi.requireActual→await vi.importActual- Adds
defaultexport to mock factories for Node.js built-in modules (crypto,chalk,os,path,react,fs-extra,fs,fs/promises). - Replaces
require()withimportfor mocked modules. - Converts arrow functions to regular
function()inmockImplementation(especially for constructors). - Fixes
jest.requireMockby extracting mock objects beforevi.mock. - Fixes
vi.mockfactories that don't return objects.
Execution Flow:
walkDir('tests'): Recursively finds all.test.tsfiles in thetestsdirectory.- Iterates through each file, applying the defined regex-based and string manipulation fixes.
- If a file is modified, it's written back to disk, and a log message is printed.
scripts/fix-arrow-mocks.mjs, scripts/fix-fs-mocks.mjs, scripts/fix-fs-promises-mocks.mjs, scripts/fix-mock-defaults.mjs
These are specialized versions or subsets of the fixes found in fix-all-tests.mjs. They target specific patterns related to:
fix-arrow-mocks.mjs: Converting arrow functions inmockImplementationto regular functions, particularly for constructor mocks.fix-fs-mocks.mjs: Ensuringfs,path, andreactmocks correctly export adefaultproperty.fix-fs-promises-mocks.mjs: Similar tofix-fs-mocks.mjsbut specifically forfs/promisesandchild_process, and also handlesrequire()toimportconversion for these modules.fix-mock-defaults.mjs: A more general script to adddefault: impltovi.mockfactories for a list of common modules (crypto,chalk,os,fs-extra,react).
Purpose: To provide granular control over specific test file refactoring tasks, allowing for targeted fixes without running the full fix-all-tests.mjs suite.
scripts/fix-channel-imports.cjs
This CommonJS script modifies import paths within the src/channels directory.
Purpose: To correct import statements that might incorrectly reference ../index.js (a barrel file) to instead reference ../core.js, likely to resolve circular dependency issues or improve module isolation.
Execution Flow:
- Uses
fast-globto find all.tsfiles insrc/channels/. - Excludes
src/channels/index.tsandsrc/channels/core.tsfrom modification. - Reads each file, replaces
from '../index.js'withfrom '../core.js', and writes back the modified content.
scripts/fix-critical-issues.sh
This Bash script automates the resolution of several critical setup and dependency issues identified in an audit.
Purpose: To provide a robust, automated way to prepare the development environment, install system-level tools, and resolve common dependency conflicts.
Execution Flow:
- Fix Zod version conflict: Updates
package.jsonto usezod: ^3.25.0if^4.1.13is found. - Install
ripgrep: Detects the operating system (apt-get,brew,yum) and installsripgrep(used for fast code searching). - Clean previous installation: Removes
node_modulesandpackage-lock.json. - Install dependencies: Runs
npm install, with a fallback tonpm install --legacy-peer-depsif the first attempt fails. - Verify installation: Checks if critical dependencies (
zod,openai,typescript, etc.) are present innode_modules. - Build TypeScript: Runs
npm run buildand checks fordist/index.js. - Run basic tests: Invokes
node dist/index.js --helpto verify CLI executability. - Update
SECURITY.md: Prompts the user to update the security contact email if it's stillsecurity@example.com. - Provides a final summary and next steps.
4. External Data & Campaigns
These scripts handle interactions with external data sources and orchestrate complex, multi-step operations.
scripts/fetch-models-snapshot.ts
This script downloads model pricing and context window information from LiteLLM's GitHub repository.
Purpose: To keep the project's internal model configuration (src/config/models-snapshot.json) up-to-date with the latest capabilities and context windows of various LLMs, without manual updates.
Execution Flow:
- Fetches
model_prices_and_context_window.jsonfrom the LiteLLM GitHub repository. - Parses the JSON data.
- Filters and extracts relevant fields (
max_tokens,max_input_tokens,max_output_tokens,supports_vision,supports_function_calling) for chat/completion models. - Writes the processed data to
src/config/models-snapshot.json. - Includes graceful fallback mechanisms to keep the existing snapshot file or write an empty one if the fetch fails.
scripts/codebuddy-real-app-campaign.mjs
This script runs a "real application campaign" where the Code Buddy agent is tasked with building several small applications from scratch in isolated directories.
Purpose: To rigorously test the Code Buddy agent's end-to-end capabilities in a realistic development scenario, evaluating its ability to understand prompts, use tools, generate code, and pass validation tests.
Execution Flow:
- Sets up a campaign root directory (
apps/codebuddy-real-campaign). - Reads the
GOOGLE_API_KEYfrom.env. - Defines an array of
scenarios, each with:
- An
id(e.g.,level-1-cli). - A
promptdescribing the application to build. - A
validatefunction to run tests/checks on the generated application.
- For each scenario:
- Creates a dedicated directory.
- Constructs a
cliCmdto invoke the Code Buddy agent (src/index.ts) with specific parameters (e.g.,--directory,--model,--auto-approve,--prompt). - Executes the Code Buddy command, capturing its output.
extractToolStats(): Parses the agent's output to count tool calls and unique tools used.- Executes the scenario's
validatefunction, which typically involvesnpm install,npm test, and running asmoke-test.mjswithin the generated application's directory. - Records the results (tool calls, validation status) in a report.
- Writes a final
campaign-report.jsonsummarizing all scenario outcomes.
Key Functions:
run(command, cwd, allowFail): Executes shell commands, captures output, and handles failures.ensureDir(dir): Creates directories recursively.readGoogleApiKey(): Retrieves the API key from.env.extractToolStats(responseText): Parses agent output for tool usage.findAppDir(baseDir): Locates the actual application directory within a scenario's output.scenarioPrompt(levelName, goal): Generates a structured prompt for the agent.