@@ -6,19 +6,24 @@ import {
66 type ServerProvider ,
77 type ServerProviderModel ,
88 type ServerProviderSlashCommand ,
9+ type ServerProviderUsageLimits ,
910} from "@t3tools/contracts" ;
1011import { createModelCapabilities } from "@t3tools/shared/model" ;
1112import * as DateTime from "effect/DateTime" ;
13+ import * as Clock from "effect/Clock" ;
1214import * as Effect from "effect/Effect" ;
1315import * as Option from "effect/Option" ;
1416import * as Predicate from "effect/Predicate" ;
1517import * as Result from "effect/Result" ;
18+ import * as Ref from "effect/Ref" ;
19+ import * as Semaphore from "effect/Semaphore" ;
1620import * as Stream from "effect/Stream" ;
1721import * as SubscriptionRef from "effect/SubscriptionRef" ;
1822import type * as EffectAcpErrors from "effect-acp/errors" ;
1923import type * as EffectAcpSchema from "effect-acp/compat" ;
2024
2125import type { AcpSessionRuntimeStartResult } from "../acp/AcpSessionRuntime.ts" ;
26+ import { resolveUsageLimitsAfterProbe } from "../providerUsageLimits.ts" ;
2227import { makeManagedServerProvider } from "../makeManagedServerProvider.ts" ;
2328import {
2429 makeManualOnlyProviderMaintenanceCapabilities ,
@@ -123,6 +128,7 @@ interface AntigravityProviderOptions {
123128 EffectAcpErrors . AcpError | ProviderSetupError
124129 > ;
125130 readonly supportsTextGeneration : Effect . Effect < boolean > ;
131+ readonly usageLimits ?: Effect . Effect < ServerProviderUsageLimits > ;
126132 readonly maintenanceCapabilities ?: ProviderMaintenanceCapabilities ;
127133 /** Auth type and label published once a session authenticates. */
128134 readonly auth ?: { readonly type : string ; readonly label : string } ;
@@ -134,6 +140,9 @@ export const makeAntigravityProvider = Effect.fn("makeAntigravityProvider")(func
134140 options : AntigravityProviderOptions ,
135141) {
136142 const checkedAt = DateTime . formatIso ( yield * DateTime . now ) ;
143+ const scope = yield * Effect . scope ;
144+ const usageSemaphore = yield * Semaphore . make ( 1 ) ;
145+ const lastUsageAttempt = yield * Ref . make < number | undefined > ( undefined ) ;
137146 const initialDraft = {
138147 ...buildServerProvider ( {
139148 presentation : { displayName : "Antigravity" , showInteractionModeToggle : false } ,
@@ -169,6 +178,40 @@ export const makeAntigravityProvider = Effect.fn("makeAntigravityProvider")(func
169178 Effect . flatMap ( ( state ) => options . stampIdentity ( state . draft ) ) ,
170179 ) ;
171180
181+ const refreshUsageLimits = Effect . fn ( "AntigravityProvider.refreshUsageLimits" ) ( function * (
182+ revision : number ,
183+ force = false ,
184+ ) {
185+ const read = options . usageLimits ;
186+ if ( ! read ) return ;
187+ yield * usageSemaphore . withPermits ( 1 ) (
188+ Effect . gen ( function * ( ) {
189+ const state = yield * SubscriptionRef . get ( metadata ) ;
190+ if ( state . authRevision !== revision || ! state . draft . enabled || ! state . draft . installed )
191+ return ;
192+ const now = yield * Clock . currentTimeMillis ;
193+ const last = yield * Ref . get ( lastUsageAttempt ) ;
194+ if ( ! force && last !== undefined && now - last < 30_000 ) return ;
195+ yield * Ref . set ( lastUsageAttempt , now ) ;
196+ const limits = yield * read ;
197+ yield * SubscriptionRef . update ( metadata , ( current ) =>
198+ current . authRevision !== revision
199+ ? current
200+ : {
201+ ...current ,
202+ draft : {
203+ ...current . draft ,
204+ usageLimits : resolveUsageLimitsAfterProbe ( {
205+ published : current . draft . usageLimits ,
206+ probed : limits ,
207+ } ) ,
208+ } ,
209+ } ,
210+ ) ;
211+ } ) ,
212+ ) ;
213+ } ) ;
214+
172215 const checkProvider = Effect . fn ( "checkAntigravityProvider" ) ( function * ( ) {
173216 if ( ! settings . enabled ) return yield * getSnapshot ;
174217 const before = yield * SubscriptionRef . get ( metadata ) ;
@@ -231,7 +274,8 @@ export const makeAntigravityProvider = Effect.fn("makeAntigravityProvider")(func
231274 } ,
232275 } satisfies AntigravityProviderState ;
233276 } ) ;
234- return yield * options . stampIdentity ( next . draft ) ;
277+ if ( initialized !== undefined ) yield * refreshUsageLimits ( next . authRevision , true ) ;
278+ return yield * getSnapshot ;
235279 } ) ;
236280
237281 const maintenanceCapabilities =
@@ -250,7 +294,11 @@ export const makeAntigravityProvider = Effect.fn("makeAntigravityProvider")(func
250294 enrichSnapshot : ( { publishSnapshot } ) =>
251295 SubscriptionRef . changes ( metadata ) . pipe (
252296 Stream . runForEach ( ( state ) =>
253- options . stampIdentity ( state . draft ) . pipe ( Effect . flatMap ( publishSnapshot ) ) ,
297+ options
298+ . stampIdentity ( state . draft )
299+ . pipe (
300+ Effect . flatMap ( ( snapshot ) => publishSnapshot ( snapshot , { replaceUsageLimits : true } ) ) ,
301+ ) ,
254302 ) ,
255303 ) ,
256304 } ) ;
@@ -262,17 +310,14 @@ export const makeAntigravityProvider = Effect.fn("makeAntigravityProvider")(func
262310 const before = yield * SubscriptionRef . get ( metadata ) ;
263311 const supportsTextGeneration = yield * options . supportsTextGeneration ;
264312 const updatedAt = DateTime . formatIso ( yield * DateTime . now ) ;
265- yield * SubscriptionRef . update ( metadata , ( state ) => {
266- if (
267- state . authRevision !== before . authRevision &&
268- state . draft . auth . status === "unauthenticated"
269- ) {
270- return state ;
313+ const revision = yield * SubscriptionRef . modify ( metadata , ( state ) => {
314+ if ( state . authRevision !== before . authRevision ) {
315+ return [ undefined , state ] as const ;
271316 }
272317 const { message : _previousMessage , ...draft } = state . draft ;
273318 const workspaces = draft . workspaceSnapshots ?? [ ] ;
274319 const workspace = cwd ? workspaces . find ( ( entry ) => entry . cwd === cwd ) : undefined ;
275- return {
320+ const next = {
276321 authRevision : state . authRevision + 1 ,
277322 draft : {
278323 ...draft ,
@@ -302,7 +347,14 @@ export const makeAntigravityProvider = Effect.fn("makeAntigravityProvider")(func
302347 : { } ) ,
303348 } ,
304349 } satisfies AntigravityProviderState ;
350+ return [ next . authRevision , next ] as const ;
305351 } ) ;
352+ if ( revision !== undefined ) {
353+ yield * refreshUsageLimits (
354+ revision ,
355+ before . draft . auth . status !== "authenticated" || ! before . draft . usageLimits ?. windows . length ,
356+ ) . pipe ( Effect . forkIn ( scope ) ) ;
357+ }
306358 } ) ;
307359
308360 const onConfigOptionsUpdated = Effect . fn ( "AntigravityProvider.onConfigOptionsUpdated" ) ( function * (
@@ -351,6 +403,7 @@ export const makeAntigravityProvider = Effect.fn("makeAntigravityProvider")(func
351403
352404 const clearAccountMetadata = Effect . fn ( "AntigravityProvider.clearAccountMetadata" ) ( function * ( ) {
353405 const updatedAt = DateTime . formatIso ( yield * DateTime . now ) ;
406+ yield * Ref . set ( lastUsageAttempt , undefined ) ;
354407 yield * SubscriptionRef . update (
355408 metadata ,
356409 ( state ) =>
@@ -367,6 +420,7 @@ export const makeAntigravityProvider = Effect.fn("makeAntigravityProvider")(func
367420 skills : [ ] ,
368421 workspaceSnapshots : [ ] ,
369422 supportsTextGeneration : false ,
423+ usageLimits : undefined ,
370424 } ,
371425 } ) satisfies AntigravityProviderState ,
372426 ) ;
0 commit comments