@@ -7,6 +7,7 @@ import { db, tasks } from '../db'
77import { getPTYManager } from '../terminal/pty-instance'
88import { getDtachService } from '../terminal/dtach-service'
99import { getMetrics , getCurrentMetrics } from '../services/metrics-collector'
10+ import { getZAiSettings } from '../lib/settings'
1011
1112interface ClaudeInstance {
1213 pid : number
@@ -821,16 +822,21 @@ async function getClaudeOAuthToken(): Promise<string | null> {
821822 return null
822823}
823824
824- // Fetch usage from Anthropic API
825- async function fetchClaudeUsage ( token : string ) : Promise < ClaudeUsageResponse > {
825+ // Fetch usage from Anthropic or z.ai API
826+ async function fetchClaudeUsage ( token : string , baseURL ?: string ) : Promise < ClaudeUsageResponse > {
826827 try {
827- const response = await fetch ( 'https://api.anthropic.com/api/oauth/usage' , {
828+ // Use z.ai API if baseURL is provided, otherwise use Anthropic
829+ const apiUrl = baseURL
830+ ? `${ baseURL } /api/oauth/usage`
831+ : 'https://api.anthropic.com/api/oauth/usage'
832+
833+ const response = await fetch ( apiUrl , {
828834 method : 'GET' ,
829835 headers : {
830836 'Content-Type' : 'application/json' ,
831837 'User-Agent' : 'vibora/1.0.0' ,
832838 Authorization : `Bearer ${ token } ` ,
833- 'anthropic-beta' : 'oauth-2025-04-20' ,
839+ ... ( baseURL ? { } : { 'anthropic-beta' : 'oauth-2025-04-20' } ) ,
834840 } ,
835841 } )
836842
@@ -907,27 +913,36 @@ async function fetchClaudeUsage(token: string): Promise<ClaudeUsageResponse> {
907913monitoringRoutes . get ( '/claude-usage' , async ( c ) => {
908914 const now = Date . now ( )
909915
910- // Return cached data if still fresh
911- if ( cachedUsage && ( now - usageCacheTimestamp ) < USAGE_CACHE_MS ) {
912- return c . json ( cachedUsage )
916+ // Check if z.ai is enabled
917+ const zaiSettings = getZAiSettings ( )
918+
919+ // Get token: z.ai API key if enabled, otherwise OAuth token
920+ let token : string | null = null
921+ let baseURL : string | undefined
922+
923+ if ( zaiSettings . enabled && zaiSettings . apiKey ) {
924+ token = zaiSettings . apiKey
925+ baseURL = 'https://api.z.ai/api/anthropic'
926+ } else {
927+ token = await getClaudeOAuthToken ( )
913928 }
914929
915- // Get OAuth token
916- const token = await getClaudeOAuthToken ( )
917930 if ( ! token ) {
918931 const response : ClaudeUsageResponse = {
919932 available : false ,
920933 fiveHour : null ,
921934 sevenDay : null ,
922935 sevenDayOpus : null ,
923936 sevenDaySonnet : null ,
924- error : 'No Claude Code OAuth token found' ,
937+ error : zaiSettings . enabled
938+ ? 'z.ai is enabled but no API key is configured'
939+ : 'No Claude Code OAuth token found' ,
925940 }
926941 return c . json ( response )
927942 }
928943
929944 // Fetch usage from API
930- const usage = await fetchClaudeUsage ( token )
945+ const usage = await fetchClaudeUsage ( token , baseURL )
931946
932947 // Cache the result
933948 cachedUsage = usage
0 commit comments