@@ -43,15 +43,15 @@ import {
4343 configureGitmcpConfigBuilder ,
4444 buildGitmcpMcpServerConfig ,
4545 TaskTreeService ,
46- detectWikiLanguage ,
47- wikiVariantPath ,
4846 isWikiVariantFile ,
47+ endpointQuotaKind ,
4948} from "@deeporca/core" ;
5049import { extractTaskTrajectory } from "./task-trajectory" ;
5150import { buildSymbolGraph } from "./symbol-graph-query" ;
5251import type { ModelConfigSelection , UserPromptContent } from "@deeporca/core" ;
5352import { IpcEvent , IpcRequest } from "../shared/ipc.js" ;
5453import { ensureDembrandtBrowserProvider , getDembrandtCdpEndpoint } from "./tools/dembrandt-browser" ;
54+ import { fetchEndpointQuota } from "./endpoint-quota.js" ;
5555import type {
5656 CodegraphIndexEntry ,
5757 CrgIndexEntry ,
@@ -60,6 +60,7 @@ import type {
6060 KnowledgeArchmapSurface ,
6161 KnowledgeSourceStatus ,
6262 KnowledgeStatusResponse ,
63+ EndpointQuotaResponse ,
6364 MemoryRoutingStatus ,
6465 KnowledgeSymbol ,
6566 KnowledgeSymbolGraph ,
@@ -1230,6 +1231,39 @@ function resolveRegisteredRoot(rootArg?: string): string | null {
12301231 return known . has ( root ) ? root : null ;
12311232}
12321233
1234+ function registerEndpointQuotaIpc ( { handle } : IpcHelpers ) : void {
1235+ // Quota follows the ENDPOINT (subscription/prepaid providers): the probe is
1236+ // selected by the endpoint's own baseURL and keyed by its apiKey. Hosts
1237+ // without a quota surface short-circuit ok:false — a key must never travel
1238+ // to a host the platform doesn't own.
1239+ handle ( IpcRequest . EndpointQuota , async ( endpointId ?: string ) : Promise < EndpointQuotaResponse > => {
1240+ if ( ! endpointId ) return { ok : false , error : "missing endpoint id" } ;
1241+ try {
1242+ const settings = resolveCurrentSettings ( getBridge ( ) . projectRoot ) ;
1243+ const endpoint = settings . endpoints . find ( ( ep ) => ep . id === endpointId ) ;
1244+ if ( ! endpoint ) return { ok : false , error : "unknown endpoint" } ;
1245+ const kind = endpointQuotaKind ( endpoint . baseURL ) ;
1246+ if ( ! kind ) return { ok : false , error : "endpoint has no quota probe" } ;
1247+ const result = await fetchEndpointQuota ( kind , endpoint . apiKey ) ;
1248+ if ( ! result . ok ) return { ok : false , error : result . error } ;
1249+ if ( result . kind === "opencode-subscription" ) {
1250+ return { ok : true , kind : result . kind , limits : result . limits } ;
1251+ }
1252+ return {
1253+ ok : true ,
1254+ kind : result . kind ,
1255+ type : result . account . type ,
1256+ balance : result . account . balance ,
1257+ totalCashBalance : result . account . totalCashBalance ,
1258+ totalVoucherBalance : result . account . totalVoucherBalance ,
1259+ fetchedAt : result . fetchedAt ,
1260+ } ;
1261+ } catch ( err ) {
1262+ return { ok : false , error : err instanceof Error ? err . message : String ( err ) } ;
1263+ }
1264+ } ) ;
1265+ }
1266+
12331267function registerKnowledgeIpc ( { handle } : IpcHelpers ) : void {
12341268 handle ( IpcRequest . KnowledgeStatus , async ( rootArg ?: string ) : Promise < KnowledgeStatusResponse > => {
12351269 const pinned = resolveRegisteredRoot ( rootArg ) ;
@@ -1296,17 +1330,20 @@ function registerKnowledgeIpc({ handle }: IpcHelpers): void {
12961330 }
12971331 : { state : "empty" , detail : "未构建" } ;
12981332
1333+ // Base pages only: the removed bilingual stage's legacy `*.zh.md` /
1334+ // `*.en.md` siblings stay on disk but must not count as pages.
1335+ const isCountedWikiPage = ( n : string ) : boolean => n . endsWith ( ".md" ) && ! isWikiVariantFile ( n ) ;
1336+
12991337 // OpenWiki — page count under openwiki/ (recursive for modules/ + workflows/).
13001338 const wikiDir = join ( root , "openwiki" ) ;
13011339 let wikiPages = 0 ;
13021340 if ( existsSync ( wikiDir ) ) {
1303- wikiPages = countDirFiles ( wikiDir , ( n ) => n . endsWith ( ".md" ) ) ;
1341+ wikiPages = countDirFiles ( wikiDir , isCountedWikiPage ) ;
13041342 for ( const sub of [ "modules" , "workflows" ] ) {
1305- wikiPages += countDirFiles ( join ( wikiDir , sub ) , ( n ) => n . endsWith ( ".md" ) ) ;
1343+ wikiPages += countDirFiles ( join ( wikiDir , sub ) , isCountedWikiPage ) ;
13061344 }
13071345 }
1308- const wikiSync =
1309- freshness . wikiSync ?? ( existsSync ( wikiDir ) ? newestMtime ( wikiDir , ( n ) => n . endsWith ( ".md" ) ) : undefined ) ;
1346+ const wikiSync = freshness . wikiSync ?? ( existsSync ( wikiDir ) ? newestMtime ( wikiDir , isCountedWikiPage ) : undefined ) ;
13101347 const openwiki : KnowledgeSourceStatus = existsSync ( wikiDir )
13111348 ? {
13121349 state : wikiPages === 0 ? "empty" : isStale ( wikiSync ) ? "stale" : "indexed" ,
@@ -2096,28 +2133,10 @@ function registerWikiIpc({ handle, handlePrivileged }: IpcHelpers): void {
20962133 } catch {
20972134 // unreadable — leave undated
20982135 }
2099- // Bilingual translation sibling (wiki.translate build stage):
2100- // detect the base page's language with the SAME heuristic core
2101- // uses, then probe the other-language variant. Cheap existence
2102- // check on a path we derived ourselves — no renderer input.
2103- let translation : WikiPageEntry [ "translation" ] ;
2104- try {
2105- const head = await readFile ( absPath , "utf8" ) ;
2106- const lang = detectWikiLanguage ( head ) ;
2107- if ( lang ) {
2108- const vLang : "zh" | "en" = lang === "zh" ? "en" : "zh" ;
2109- const vRel = wikiVariantPath ( rel , vLang ) ;
2110- const vStat = await stat ( join ( wikiDir , vRel ) ) ;
2111- translation = { lang : vLang , path : vRel , mtime : vStat . mtime . toISOString ( ) } ;
2112- }
2113- } catch {
2114- // No variant (or unreadable base) — no toggle.
2115- }
21162136 entries . push ( {
21172137 path : rel ,
21182138 title : await wikiPageTitle ( absPath , item . name ) ,
21192139 mtime,
2120- ...( translation ? { translation } : { } ) ,
21212140 } ) ;
21222141 }
21232142 }
@@ -2245,6 +2264,7 @@ function registerIpc(): void {
22452264 registerCrgIpc ( helpers ) ;
22462265 registerMemoryIpc ( helpers ) ;
22472266 registerKnowledgeIpc ( helpers ) ;
2267+ registerEndpointQuotaIpc ( helpers ) ;
22482268 registerTaskTreeIpc ( helpers ) ;
22492269 registerDesignIpc ( helpers ) ;
22502270 registerA2uiIpc ( helpers ) ;
0 commit comments