Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,25 @@ export interface UserIntegration {
isAuthenticated: boolean
}

interface CachedStrata {
response: StrataCreateResponse
expiresAt: number
}

export class KlavisClient {
private baseUrl: string
private strataCache = new Map<string, CachedStrata>()
private pendingRequests = new Map<string, Promise<StrataCreateResponse>>()
private static STRATA_CACHE_TTL = 5 * 60 * 1000 // 5 minutes

constructor(baseUrl?: string) {
this.baseUrl = baseUrl || EXTERNAL_URLS.KLAVIS_PROXY
}

private buildStrataCacheKey(userId: string, servers: string[]): string {
return `${userId}:${[...servers].sort().join(',')}`
}
Comment thread
DaniAkash marked this conversation as resolved.

private async request<T>(
method: string,
path: string,
Expand Down Expand Up @@ -77,18 +89,44 @@ export class KlavisClient {
}

/**
* Create Strata instance with specified servers
* Returns strataServerUrl for MCP connection and oauthUrls for authentication
* Create Strata instance with specified servers.
* Cached by (userId, sorted servers) with 5-min TTL.
* Concurrent requests for the same key are deduplicated.
*/
async createStrata(
userId: string,
servers: string[],
): Promise<StrataCreateResponse> {
return this.request<StrataCreateResponse>(
const cacheKey = this.buildStrataCacheKey(userId, servers)

const cached = this.strataCache.get(cacheKey)
if (cached && cached.expiresAt > Date.now()) {
return cached.response
}

const pending = this.pendingRequests.get(cacheKey)
if (pending) return pending

const promise = this.request<StrataCreateResponse>(
'POST',
'/mcp-server/strata/create',
{ userId, servers },
)
.then((response) => {
this.strataCache.set(cacheKey, {
response,
expiresAt: Date.now() + KlavisClient.STRATA_CACHE_TTL,
})
this.pendingRequests.delete(cacheKey)
return response
})
.catch((error) => {
this.pendingRequests.delete(cacheKey)
throw error
})

this.pendingRequests.set(cacheKey, promise)
return promise
}

/**
Expand Down
Loading