Vercel AI SDK Integration
Relevant source files
The following files were used as context for generating this wiki page:
The regxa project provides a specialized integration for the Vercel AI SDK, enabling Large Language Model (LLM) agents to interact with multiple package registries through a unified tool interface. This integration, primarily exposed through the packageTool, abstracts the complexities of Package URL (PURL) parsing, ecosystem-specific API calls, and data normalization.
By registering the packageTool, developers can build AI applications that can query metadata, versions, dependencies, and maintainers from npm, PyPI, crates.io, RubyGems, Packagist, and Arch Linux (ALPM/AUR) using natural language prompts.
Sources: skills/regxa-ai-tool/SKILL.md:10-15, src/ai.ts, README.md:12-14
Core Component: packageTool
The packageTool is the primary entry point for AI integration. It is designed to be compatible with the Vercel AI SDK's generateText and streamText functions. It uses a discriminated union in its input schema to handle different types of registry operations within a single tool definition.
Sources: src/ai.ts, test/unit/ai-tool.test.ts:25-29, skills/regxa-ai-tool/SKILL.md:21-30
Tool Architecture and Data Flow
The integration acts as a bridge between the AI SDK's tool calling mechanism and regxa's internal helper functions. When an LLM decides to call the tool, the execute function routes the request based on the specified operation.
The diagram above illustrates how the packageTool intercepts tool calls from the AI and routes them to specific internal helper functions based on the requested operation.
Sources: src/ai.ts, skills/regxa-ai-tool/SKILL.md:37-64, test/unit/ai-tool.test.ts:31-89
Supported Operations
The packageTool supports five primary operations. Each operation requires a PURL (Package URL) string, which is the standard identifier format across the regxa ecosystem.
| Operation | Input Parameter | Description |
|---|---|---|
package | purl (string) | Fetches package metadata including description, licenses, and latest version. |
versions | purl (string) | Lists all published versions with dates and statuses (e.g., yanked). |
dependencies | purl (string) | Lists dependencies for a specific version. PURL must include @version. |
maintainers | purl (string) | Returns a list of authors/maintainers with roles and contact info. |
bulk-packages | purls (string[]), concurrency (int) | Fetches metadata for multiple packages simultaneously. |
Sources: src/ai.ts, skills/regxa-ai-tool/SKILL.md:43-64, src/helpers.ts:1-60
Bulk Operations Logic
For bulk-packages, the tool supports a concurrency parameter (defaulting to 15, maxing at 50 for the AI tool wrapper) to manage parallel network requests. It returns a map of PURL strings to their corresponding package objects. If a specific lookup fails within a bulk request, it is silently omitted from the resulting map.
Sources: src/ai.ts, src/helpers.ts:43-70, skills/regxa-ai-tool/SKILL.md:60-64
Implementation Details
Dependency on Helpers
The packageTool is a lightweight wrapper around the high-level PURL helpers found in src/helpers.ts. These helpers are responsible for:
- Registry Factory Resolution: Using
createFromPURLto instantiate the correct registry adapter (e.g., npm vs. cargo). - PURL Validation: Ensuring the PURL format is valid before attempting network calls.
- Concurrency Management: Implementing worker loops for bulk fetches.
Sources: src/helpers.ts:7-41, src/ai.ts, src/core/purl.ts
Error Handling in AI Context
Errors encountered during execution are thrown as standard exceptions, which the Vercel AI SDK surfaces to the agent. This allows the agent to self-correct if it provides a malformed PURL or an unsupported ecosystem.
- NotFoundError: Thrown when a package or version is missing.
- InvalidPURLError: Thrown when the PURL is malformed (e.g., missing
pkg:prefix or missing version for dependency lookups). - RateLimitError: Thrown when a registry's rate limit is exceeded after internal retries fail.
Sources: skills/regxa-ai-tool/SKILL.md:92-100, src/helpers.ts:31-33, test/unit/ai-tool.test.ts
Integration Example
The tool is integrated by importing packageTool from the regxa/ai subpath. It is then passed into the tools configuration of the SDK.
import { packageTool } from "regxa/ai";
import { generateText } from "ai";
const result = await generateText({
model: openai("gpt-4o"),
tools: {
pkg: packageTool
},
prompt: "What is the latest version of pkg:pypi/requests?",
});
Sources: skills/regxa-ai-tool/SKILL.md:21-30, README.md:93-100
Summary
The Vercel AI SDK integration provides a PURL-native interface for LLMs to query various package registries. By encapsulating complex ecosystem-specific logic into a single packageTool with a discriminated union schema, regxa enables AI agents to perform structured package lookups, dependency analysis, and maintainer verification across diverse programming language ecosystems.
Sources: skills/regxa-ai-tool/SKILL.md:1-15, src/ai.ts