Skip to content

feat: Add versioned /api/v1 endpoints with secure auth, credit tracking, and usage auditing#2

Merged
ChristopherTrimboli merged 14 commits intomainfrom
staging
Oct 4, 2025
Merged

feat: Add versioned /api/v1 endpoints with secure auth, credit tracking, and usage auditing#2
ChristopherTrimboli merged 14 commits intomainfrom
staging

Conversation

@samarth30
Copy link
Member

This pull request introduces a new versioned API under the /api/v1/ namespace, focusing on secure, auditable, and credit-aware endpoints for API key management, chat, image, and video generation. It implements robust authentication (supporting both user and API key auth), usage tracking, and credit deduction for resource-intensive operations. Several legacy or debug endpoints are removed or refactored for consistency.

The most important changes are:

API Key Management (CRUD):

AI Generation Endpoints (Chat, Image, Video):

  • Introduced new /api/v1/chat, /api/v1/generate-image, and /api/v1/generate-video endpoints, all requiring either user or API key authentication. These endpoints deduct credits for each operation, record detailed usage for billing/auditing, and handle errors gracefully (including fallback responses for video generation failures). (app/api/v1/chat/route.ts, app/api/v1/generate-image/route.ts, app/api/v1/generate-video/route.ts) [1] [2] [3]
  • The chat and image endpoints now record which API key (if any) was used for the request. [1] [2] [3]

Model Listing Endpoint:

  • Updated the models listing endpoint to require authentication and moved it to /api/v1/models. (app/api/v1/models/route.ts)

Third-Party Proxy and Debug Cleanup:

  • Added a proxy route for FAL AI service integration. (app/api/fal/proxy/route.ts)
  • Removed the legacy debug user endpoint. (app/api/debug/user/route.ts)
  • Removed the old image generation endpoint in favor of the new versioned one. (app/api/generate-image/route.ts)

These changes collectively provide a secure, scalable, and auditable foundation for API consumers and internal users, ensuring resource usage is tracked and billed appropriately.

@vercel
Copy link

vercel bot commented Oct 3, 2025

@samarth30 is attempting to deploy a commit to the elizaOS team on Vercel, but is not a member of this team. To resolve this issue, you can:

  • Make your repository public. Collaboration is free for open source and public repositories.
  • Add @samarth30 as a member. A Pro subscription is required to access Vercel's collaborative features.
    • If you're the owner of the team, click here and add @samarth30 as a member.
    • If you're the user who initiated this build request, click here to request access.
    • If you're already a member of the elizaOS team, make sure that your Vercel account is connected to your GitHub account.

To read more about collaboration on Vercel, click here.

@coderabbitai
Copy link

coderabbitai bot commented Oct 3, 2025

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.


Note

🎁 Summarized by CodeRabbit Free

Your organization is on the Free plan. CodeRabbit will generate a high-level summary and a walkthrough for each pull request. For a comprehensive line-by-line review, please upgrade your subscription to CodeRabbit Pro by visiting https://app.coderabbit.ai/login.

Comment @coderabbitai help to get the list of available commands and usage tips.

@samarth30 samarth30 changed the title Staging feat: Add versioned /api/v1 endpoints with secure auth, credit tracking, and usage auditing Oct 3, 2025
@ChristopherTrimboli ChristopherTrimboli added the enhancement New feature or request label Oct 4, 2025
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request introduces a comprehensive versioned API (/api/v1/) with secure authentication, comprehensive API key management, credit tracking, and usage auditing. The changes establish a robust foundation for API consumers by implementing dual authentication (user sessions and API keys), credit-based resource consumption tracking, and detailed usage analytics.

Key changes include:

  • Complete API key CRUD management with regeneration capabilities and permission-based access control
  • Secure versioned endpoints for chat, image, and video generation with credit deduction and usage tracking
  • Enhanced authentication system supporting both session-based and API key-based access
  • Video generation capabilities with FAL AI integration and fallback mechanisms

Reviewed Changes

Copilot reviewed 58 out of 59 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
lib/auth.ts Adds dual authentication system supporting both user sessions and API keys with comprehensive validation
app/api/v1/api-keys/ Implements complete CRUD operations for API key management with organization-scoped security
app/api/v1/generate-* Creates secure generation endpoints with credit tracking and usage auditing
components/api-keys/ Provides comprehensive UI for API key management with proper security practices
components/video/ Adds complete video generation interface with real-time preview and usage tracking

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@vercel
Copy link

vercel bot commented Oct 4, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
eliza-cloud-v2 Ready Ready Preview Comment Oct 4, 2025 3:47am

@ChristopherTrimboli ChristopherTrimboli merged commit e5c4c22 into main Oct 4, 2025
3 checks passed
wtfsayo added a commit that referenced this pull request Oct 13, 2025
Critical Fixes:
- Make non-streaming credit deduction synchronous (prevents revenue loss)
- Return 402 error if credit deduction fails after API call
- Increase credit buffer from 20% to 50% for safety margin
- Fix message validation to allow null content for tool/function calls
- Fix type safety for multimodal content in estimateRequestCost

Error Handling Improvements:
- Add structured error propagation from Vercel Gateway
- Properly parse and forward OpenAI-formatted errors with status codes
- Add timeout handling (60s for chat/embeddings, 30s for models)
- Use AbortController for all gateway requests

Performance & Reliability:
- Only analytics (usage records, generation records) run in background
- Critical billing operations (credit deduction) are synchronous
- Better error messages for timeout scenarios
- Proper cleanup of timeout handlers

This addresses all critical issues from PR review:
- Issue #1: Synchronous credit deduction ✅
- Issue #2: Increased credit buffer ✅
- Issue #3: Tool call message validation ✅
- Issue #4: Multimodal content type safety ✅
- Issue #5: Gateway error propagation ✅
- Issue #6: Timeout handling ✅
0xSolace added a commit that referenced this pull request Mar 9, 2026
Critical bugs fixed:
- #1: resolveContainer() now hydrates from DB after restart instead of
  falling back to first node. Added findBySandboxId() and listByNodeId()
  repository methods for DB-backed container resolution.
- #2: create() now uses DockerNodeManager.getAvailableNode() for
  least-loaded placement instead of random env-var selection. Increments
  allocated_count on create, decrements on stop. Falls back to env var
  only when DB has no nodes registered.
- #3: Port allocation now queries DB for ports already assigned on the
  target node. allocatePort() takes an exclusion set and retries until
  it finds a free port.

Security fixes:
- #4: runCommand() now shell-escapes cmd and each arg individually via
  shellQuote() before passing to SSH exec. Container names also quoted
  in docker exec/stop/rm commands.
- #5: Added validateAgentId() check (UUID-safe chars only, max 128)
  called before any shell interpolation of agentId. getVolumePath()
  validates before constructing path.

Functional fixes:
- #6: checkHealth() now polls with retry loop (3s interval, 60s total
  timeout) matching Vercel provider behavior, instead of single-shot
  8s request.
- #7: getContainerName() now uses full agentId instead of truncated
  first-8-chars to eliminate collision risk.

Code quality fixes:
- #8: Exported DockerSandboxMetadata interface for strongly-typed
  metadata. milaidy-sandbox.ts imports and narrows with it instead
  of Record<string, unknown> + as casts.
- #9: Fixed step numbering in create() comments (was 8→10, now 8→9→10).
- #10: SSH pool key changed from hostname-only to hostname:port to
  prevent collisions when nodes share hostname with different ports.
- #11: parseDockerNodes() result now cached at module level, only
  re-parsed when MILAIDY_DOCKER_NODES env var changes.
- #12: Moved DOCKER_HEADSCALE_PLAN.md and DOCKER_PROVISIONING_SUMMARY.md
  from repo root to docs/ directory.

No new type errors (tsc --noEmit passes with only 2 pre-existing errors).
0xSolace added a commit that referenced this pull request Mar 9, 2026
Fixes all 11 findings from the automated review:

Critical:
- (#1) Fix migration number references: 0034 → 0046 in schema comments and docs
- (#2) Port race TOCTOU: add partial UNIQUE index on (node_id, bridge_port)
  for active sandboxes. allocatePort() already retries; DB constraint is the
  safety net for concurrent provisioning
- (#3) SSH pool serverless compat: add idle timeout (5min) eviction on
  getClient(), track lastActivityMs on exec()

Security:
- (#4) Remove `as any` cast on statusFilter — validate against known status
  set, return 400 for invalid values, cast to MiladySandboxStatus type
- (#5) Add SENSITIVE_KEYS set to redact TS_AUTHKEY, DATABASE_URL, etc. from
  any logged docker run commands
- (#6) Document MILADY_DOCKER_NODES fallback as seed-only path with no load
  balancing — production should use Admin API node registration

Code quality:
- (#7) Remove ssh! non-null assertions in health check route — use definite
  assignment after early-return guard
- (#8) Migration already uses IF NOT EXISTS on all statements ✓
- (#9) Unit tests noted as follow-up work (not blocking)

Nits:
- (#10) Add logger.warn fallback when Vercel sandbox has neither shutdown()
  nor close() method
- (#11) PR draft status — will mark ready after fixes are confirmed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants