This file provides context and instructions for AI coding agents working on the USAi API Node.js module - an open-source library for integrating with government AI services.
The USAi API is a TypeScript/Node.js client library that provides an OpenAI-compatible interface for accessing government AI models (Claude, Llama, Gemini) through the USAi.gov platform. This is a beta open-source project designed for government agencies and contractors.
- TypeScript with strict mode
- Node.js (20.x, 22.x support)
- Jest for testing
- ESLint + Prettier for code quality
- OpenAI-compatible API design patterns
# Install dependencies
npm install
# Development build with watch mode
npm run dev
# Production build
npm run build
# Run all tests
npm test
# Run tests with coverage
npm run test:coverage
# Lint code
npm run lint
# Format code
npm run format
# Type checking
npm run type-checksrc/- TypeScript source codeindex.ts- Main exportsclient.ts- Core USAiAPI classhttp-client.ts- HTTP client with retry logictypes.ts- Type definitionserrors.ts- Custom error classes
tests/- Jest test filesexamples/- Usage examples including Jupyter notebookdist/- Built output (ESM + CommonJS)
- TypeScript compilation to both ESM (
dist/esm/) and CommonJS (dist/cjs/) - Dual package.json approach for proper module resolution
- Source maps included for debugging
- Unit tests for all core functionality
- Integration tests for API interactions
- Error handling tests for edge cases
- Enhanced features tests for government-specific functionality
- Target: >90% code coverage
- Strict mode enabled - no
anytypes - Explicit return types for public methods
- Interface-based design over class inheritance
- Comprehensive JSDoc for all public APIs
- Prettier configuration - 2 spaces, single quotes, no semicolons
- ESLint rules - TypeScript recommended + custom government rules
- Import organization - external imports first, then internal
- File naming - kebab-case for files, PascalCase for classes
// Preferred: Interface-based design
interface USAiConfig {
apiKey: string
baseURL?: string
timeout?: number
}
// Preferred: Explicit error handling
async function makeRequest(): Promise<Result<T, USAiError>> {
try {
// Implementation
} catch (error) {
return new USAiError('Specific error message', error)
}
}
// Preferred: Government-focused documentation
/**
* Processes government documents for AI analysis
* @param document - Document content (PDF, DOCX, TXT)
* @param options - Processing options for compliance
* @returns Promise<DocumentAnalysis> - Analysis results
* @government-use Approved for federal agency use
*/# Run all tests
npm test
# Run specific test file
npm test -- client.test.ts
# Run tests in watch mode
npm run test:watch
# Generate coverage report
npm run test:coverage- All new features must include tests
- Edge cases should be covered
- Error scenarios must be tested
- Government compliance features need specific tests
- API compatibility with OpenAI interface must be validated
- Unit Tests - Individual function/method testing
- Integration Tests - API endpoint interactions
- Error Handling - Network failures, invalid responses
- Government Features - Document processing, enhanced embeddings
- Security Tests - Input validation, sanitization
- No hardcoded secrets - use environment variables
- Input validation on all user data
- Sanitization of file uploads
- Rate limiting respect for government APIs
- Audit logging for compliance tracking
// Correct: Environment-based configuration
const config = {
apiKey: process.env.USAI_API_KEY,
baseURL: process.env.USAI_BASE_URL || 'https://api.usai.gov'
}
// Wrong: Hardcoded credentials
const config = {
apiKey: 'sk-123456789', // NEVER do this
}- Run
npm auditbefore commits - Validate all file upload handling
- Test rate limiting behavior
- Ensure no sensitive data in logs
# Clean previous builds
npm run clean
# Build for production
npm run build
# Verify build output
npm run build:verify
# Prepare for publishing
npm run prepublishOnly- Dual module support (ESM + CommonJS)
- TypeScript declarations included
- Source maps for debugging
- Government compliance metadata in package.json
The project uses GitHub Actions for:
- Multi-version testing (Node 20, 22)
- Security scanning (npm audit, Snyk, CodeQL)
- Government compliance checks
- Automated dependency updates
When working on government-related features:
- Document processing must handle classified markings
- Embeddings should support government data classification
- API responses must include compliance metadata
- Error messages should not leak sensitive information
- Support for agency-specific configurations
- FedRAMP compliance considerations
- ATO documentation generation support
- FISMA controls alignment
- Create interface in
src/types.ts - Implement in client (
src/client.ts) - Add comprehensive tests in
tests/ - Update examples in
examples/ - Document in README if user-facing
- Reproduce issue with test case
- Identify root cause in codebase
- Implement fix with minimal changes
- Verify fix with existing tests
- Add regression test to prevent recurrence
- Profile with Node.js built-in profiler
- Measure HTTP client performance
- Optimize TypeScript compilation
- Monitor memory usage in long-running processes
[scope] brief description
Examples:
[client] add document processing support
[tests] improve error handling coverage
[docs] update government compliance guide
[security] fix input validation vulnerability
-
npm run lintpasses without errors -
npm testpasses all tests -
npm run buildcompletes successfully - Security scan (
npm audit) shows no vulnerabilities - Documentation updated for user-facing changes
- Examples updated if API changes
- Government compliance considered for new features
- Code quality - TypeScript best practices
- Test coverage - New code must be tested
- Security review - Government security standards
- Performance impact - No significant degradation
- Documentation - Clear and comprehensive
# Clean and rebuild
npm run clean && npm run build
# Check TypeScript errors
npm run type-check
# Verify dependencies
npm ci# Run specific failing test
npm test -- --testNamePattern="failing test name"
# Debug with verbose output
npm test -- --verbose
# Check for async issues
npm test -- --detectOpenHandles- Verify
package.jsonexports configuration - Check TypeScript module resolution
- Ensure dual module build is working
- Test both ESM and CommonJS imports
- Use
--inspectflag for Node.js debugging - Profile HTTP requests with timing
- Monitor memory usage patterns
- Check for memory leaks in long tests
- This is an open-source government project
- Security is paramount - government standards apply
- Compliance documentation is required for changes
- Multi-stakeholder codebase (agencies, contractors, public)
- Government-first design - agencies are primary users
- OpenAI compatibility - familiar interface for developers
- Security by default - all features assume sensitive data
- Community-driven - open to contributions and feedback
- Consider government use cases first
- Maintain OpenAI API compatibility
- Include security considerations in design
- Document compliance implications
- Test with realistic government scenarios
Project Status: Beta - Open Source
Target Users: Government Agencies, Contractors, Public Sector
Security Level: Government-ready with compliance features
Contribution Policy: Community contributions welcome