@@ -0,0 +1,85 @@
|
||||
// The enablement platform over the platform's own APIs, server-side. Eligibility reads the
|
||||
// rollout flags (agent_document_tools, agent_knowledge_tools) from the flag snapshot and probes
|
||||
// the AI tools registry for the document tool family — discovery is the cheap plan/binding/flag
|
||||
// filter, and "a working OCR endpoint does not by itself make OCR a discoverable tool". Nothing is
|
||||
// provisioned for the app: the platform's knowledge service owns the index (ADR-061). The quota
|
||||
// pre-check has no platform API yet (the metering slice owns it); the indexing calls themselves
|
||||
// carry the typed quota_exhausted refusal at run time.
|
||||
|
||||
import { createRegistryClient, ToolIndexingError } from "@conusai-sdk/ai"
|
||||
import type { EnablementReason } from "../contract/index.js"
|
||||
import type { DocumentAiPlatform } from "../core/ports.js"
|
||||
|
||||
export const AGENT_DOCUMENT_TOOLS_FLAG = "agent_document_tools"
|
||||
export const AGENT_KNOWLEDGE_TOOLS_FLAG = "agent_knowledge_tools"
|
||||
|
||||
const FLAG_KEYS = [AGENT_DOCUMENT_TOOLS_FLAG, AGENT_KNOWLEDGE_TOOLS_FLAG] as const
|
||||
|
||||
export interface SdkDocumentAiPlatformOptions {
|
||||
/** The deployment contract's API URL and token. */
|
||||
apiUrl: string
|
||||
token: string
|
||||
fetch?: typeof fetch
|
||||
}
|
||||
|
||||
export function sdkDocumentAiPlatform(options: SdkDocumentAiPlatformOptions): DocumentAiPlatform {
|
||||
const http = options.fetch ?? fetch
|
||||
|
||||
const rolloutFlagsOff = async (): Promise<string | null> => {
|
||||
try {
|
||||
const response = await http(new URL(`${options.apiUrl.replace(/\/+$/, "")}/flags/snapshot`), {
|
||||
headers: { authorization: `Bearer ${options.token}`, accept: "application/json" },
|
||||
signal: AbortSignal.timeout(5_000),
|
||||
})
|
||||
if (!response.ok) return null // the flags service is not the eligibility authority
|
||||
const body = (await response.json()) as { flags?: Array<{ key?: string; enabled?: boolean }> }
|
||||
for (const flag of body.flags ?? []) {
|
||||
if (
|
||||
typeof flag.key === "string" &&
|
||||
(FLAG_KEYS as readonly string[]).includes(flag.key) &&
|
||||
flag.enabled === false
|
||||
) {
|
||||
return flag.key
|
||||
}
|
||||
}
|
||||
return null
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
async checkEligibility(): Promise<{ ok: true } | { ok: false; reason: EnablementReason; detail?: string }> {
|
||||
// The rollout flags first: a kill switch or a disabled feature refuses before any probe.
|
||||
const off = await rolloutFlagsOff()
|
||||
if (off !== null) return { ok: false, reason: "feature_disabled", detail: `the ${off} flag is off` }
|
||||
// The plan probe: can this deployment discover the document tool family at all?
|
||||
try {
|
||||
const registry = createRegistryClient({
|
||||
env: { CONUSAI_API_URL: options.apiUrl, CONUSAI_API_TOKEN: options.token },
|
||||
fetch: http,
|
||||
})
|
||||
const found = await registry.tools.search("read and index documents")
|
||||
if (found.length === 0) {
|
||||
return { ok: false, reason: "plan_required", detail: "the ai_tools plan capability lists no document tools" }
|
||||
}
|
||||
return { ok: true }
|
||||
} catch (error) {
|
||||
if (error instanceof ToolIndexingError) {
|
||||
return { ok: false, reason: "feature_disabled", detail: "the tool registry is still indexing" }
|
||||
}
|
||||
return {
|
||||
ok: false,
|
||||
reason: "plan_required",
|
||||
detail: "the AI tools registry did not answer the eligibility probe",
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
async indexingQuota() {
|
||||
// No pre-check API exists on the platform yet; the call-time quota refusal is the hard
|
||||
// gate. Until the limit endpoint lands, the admission check answers "within quota".
|
||||
return Number.MAX_SAFE_INTEGER
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user