repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | // ═══════════════════════════════════════════════════════════════ |
| #2 | // AI AGENT API - Cloudflare Workers |
| #3 | // Production-ready API for AI agent authentication & wallets |
| #4 | // ═══════════════════════════════════════════════════════════════ |
| #5 | |
| #6 | import { Router } from './router'; |
| #7 | import { AgentService } from './services/agent'; |
| #8 | import { CrossmintService } from './services/crossmint'; |
| #9 | import { DeploymentService } from './services/deployment'; |
| #10 | |
| #11 | // ───────────────────────────────────────────────── |
| #12 | // TYPES |
| #13 | // ───────────────────────────────────────────────── |
| #14 | |
| #15 | export interface Env { |
| #16 | DB: D1Database; |
| #17 | SESSIONS: KVNamespace; |
| #18 | RATE_LIMITS: KVNamespace; |
| #19 | CROSSMINT_SERVERSIDE_API_KEY: string; |
| #20 | CROSSMINT_CLIENTSIDE_API_KEY?: string; |
| #21 | SOLANA_RPC_URL?: string; |
| #22 | // Model provider API keys |
| #23 | OPENAI_API_KEY?: string; |
| #24 | ANTHROPIC_API_KEY?: string; |
| #25 | PHALA_CLOUD_API_KEY?: string; |
| #26 | PHALA_API_KEY?: string; |
| #27 | DEEPSEEK_API_KEY?: string; |
| #28 | ENVIRONMENT: string; |
| #29 | CORS_ORIGIN: string; |
| #30 | } |
| #31 | |
| #32 | export interface Agent { |
| #33 | id: string; |
| #34 | name: string; |
| #35 | description: string | null; |
| #36 | api_key_hash: string; |
| #37 | api_key_prefix: string; |
| #38 | wallet_address: string | null; |
| #39 | chain: 'solana' | 'solana-devnet'; |
| #40 | status: 'active' | 'suspended' | 'pending'; |
| #41 | permissions: AgentPermissions; |
| #42 | requests_per_minute: number; |
| #43 | requests_per_day: number; |
| #44 | metadata: Record<string, unknown> | null; |
| #45 | created_at: string; |
| #46 | updated_at: string; |
| #47 | last_active_at: string; |
| #48 | } |
| #49 | |
| #50 | export interface AgentPermissions { |
| #51 | canCreateWallet: boolean; |
| #52 | canTransfer: boolean; |
| #53 | canSwap: boolean; |
| #54 | maxTransferAmount: number; |
| #55 | maxDailyVolume: number; |
| #56 | } |
| #57 | |
| #58 | export interface Session { |
| #59 | id: string; |
| #60 | agent_id: string; |
| #61 | token_hash: string; |
| #62 | ip_address: string | null; |
| #63 | user_agent: string | null; |
| #64 | created_at: string; |
| #65 | expires_at: string; |
| #66 | last_used_at: string; |
| #67 | } |
| #68 | |
| #69 | // ───────────────────────────────────────────────── |
| #70 | // CORS HEADERS |
| #71 | // ───────────────────────────────────────────────── |
| #72 | |
| #73 | function corsHeaders(env: Env): HeadersInit { |
| #74 | return { |
| #75 | 'Access-Control-Allow-Origin': env.CORS_ORIGIN || '*', |
| #76 | 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS', |
| #77 | 'Access-Control-Allow-Headers': 'Content-Type, X-Agent-API-Key, X-Agent-Session', |
| #78 | 'Access-Control-Max-Age': '86400', |
| #79 | }; |
| #80 | } |
| #81 | |
| #82 | function jsonResponse(data: unknown, status = 200, env?: Env): Response { |
| #83 | return new Response(JSON.stringify(data), { |
| #84 | status, |
| #85 | headers: { |
| #86 | 'Content-Type': 'application/json', |
| #87 | ...(env ? corsHeaders(env) : {}), |
| #88 | }, |
| #89 | }); |
| #90 | } |
| #91 | |
| #92 | function errorResponse(message: string, status = 500, env?: Env): Response { |
| #93 | return jsonResponse({ success: false, error: message }, status, env); |
| #94 | } |
| #95 | |
| #96 | // ───────────────────────────────────────────────── |
| #97 | // MAIN HANDLER |
| #98 | // ───────────────────────────────────────────────── |
| #99 | |
| #100 | export default { |
| #101 | async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> { |
| #102 | // Handle CORS preflight |
| #103 | if (request.method === 'OPTIONS') { |
| #104 | return new Response(null, { headers: corsHeaders(env) }); |
| #105 | } |
| #106 | |
| #107 | const url = new URL(request.url); |
| #108 | const path = url.pathname; |
| #109 | |
| #110 | // Initialize services |
| #111 | const agentService = new AgentService(env); |
| #112 | const crossmintService = new CrossmintService(env); |
| #113 | const deploymentService = new DeploymentService(env, env.DB); |
| #114 | const router = new Router(env, agentService, crossmintService, deploymentService); |
| #115 | |
| #116 | try { |
| #117 | // Route requests |
| #118 | if (path === '/health' || path === '/') { |
| #119 | return jsonResponse({ |
| #120 | success: true, |
| #121 | service: 'AI Agent API', |
| #122 | version: '2.1.0', |
| #123 | environment: env.ENVIRONMENT, |
| #124 | timestamp: new Date().toISOString(), |
| #125 | features: { |
| #126 | wallets: 'Crossmint smart wallets with delegated signing', |
| #127 | smartWallets: 'Smart wallets with admin signer + MPC wallets', |
| #128 | goatSdk: 'GOAT SDK tools (balance, transfer, price, swap quotes)', |
| #129 | models: 'Multi-provider AI models (OpenAI, Anthropic, Phala, DeepSeek)', |
| #130 | tools: 'On-chain Solana actions (transfer, swap, stake)', |
| #131 | streaming: 'Real-time response streaming', |
| #132 | }, |
| #133 | endpoints: { |
| #134 | agents: '/api/agents/*', |
| #135 | wallets: '/api/wallets/*', |
| #136 | smartWallets: '/api/smart-wallets/*', |
| #137 | }, |
| #138 | providers: { |
| #139 | openai: !!env.OPENAI_API_KEY, |
| #140 | anthropic: !!env.ANTHROPIC_API_KEY, |
| #141 | phala: !!env.PHALA_API_KEY, |
| #142 | deepseek: !!env.DEEPSEEK_API_KEY, |
| #143 | crossmint: !!env.CROSSMINT_SERVERSIDE_API_KEY, |
| #144 | }, |
| #145 | }, 200, env); |
| #146 | } |
| #147 | |
| #148 | // API routes |
| #149 | if (path.startsWith('/api/agents')) { |
| #150 | return await router.handleAgentRoutes(request, path); |
| #151 | } |
| #152 | |
| #153 | // Wallet routes (direct Crossmint access) |
| #154 | if (path.startsWith('/api/wallets')) { |
| #155 | return await router.handleWalletRoutes(request, path); |
| #156 | } |
| #157 | |
| #158 | // Smart wallet routes (advanced Crossmint + GOAT SDK) |
| #159 | if (path.startsWith('/api/smart-wallets')) { |
| #160 | return await router.handleSmartWalletRoutes(request, path); |
| #161 | } |
| #162 | |
| #163 | // Factory routes (v1 API) - maps to agent deployment |
| #164 | if (path.startsWith('/api/v1/factory')) { |
| #165 | return await router.handleFactoryRoutes(request, path); |
| #166 | } |
| #167 | |
| #168 | return errorResponse('Not Found', 404, env); |
| #169 | } catch (error) { |
| #170 | console.error('Request error:', error); |
| #171 | return errorResponse( |
| #172 | error instanceof Error ? error.message : 'Internal Server Error', |
| #173 | 500, |
| #174 | env |
| #175 | ); |
| #176 | } |
| #177 | }, |
| #178 | }; |
| #179 |