Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

fix: forward identity for provider agreements (#4708)

Developers Digestb72cd5fe2286
diff --git a/apps/api/src/controllers/v2/scrape-alexandria.ts b/apps/api/src/controllers/v2/scrape-alexandria.ts
index 0f3e6fd2d..9f71e48b9 100644
--- a/apps/api/src/controllers/v2/scrape-alexandria.ts
+++ b/apps/api/src/controllers/v2/scrape-alexandria.ts
@@ -125,6 +125,7 @@ export async function providerScrapeController(
teamId: req.auth.team_id,
orgId: orgIdFromAcuc(req.acuc),
apiKeyId: req.acuc.api_key_id ?? null,
+ apiKeyIdText: req.acuc.api_key_id_text ?? null,
flags: req.acuc.flags,
calls: body.alexandria,
resultAuthorization: req.get("authorization"),
diff --git a/apps/api/src/services/alexandria/client.ts b/apps/api/src/services/alexandria/client.ts
index 272854f33..494a3f88d 100644
--- a/apps/api/src/services/alexandria/client.ts
+++ b/apps/api/src/services/alexandria/client.ts
@@ -16,6 +16,7 @@ export async function exchangeRequest(input: {
requestId?: string;
maximumCredits?: number;
resultAuthorization?: string;
+ termsIdentity?: { organizationId: string; apiKeyId: string };
}): Promise<ExchangeResponse> {
if (!config.FIRE_EXCHANGE_URL) throw new Error("Exchange is not configured");
const base = config.FIRE_EXCHANGE_URL.replace(/\/+$/, "");
@@ -30,6 +31,12 @@ export async function exchangeRequest(input: {
? { authorization: input.resultAuthorization }
: {}),
"x-exchange-team-id": input.teamId,
+ ...(input.termsIdentity
+ ? {
+ "x-exchange-organization-id": input.termsIdentity.organizationId,
+ "x-exchange-api-key-id": input.termsIdentity.apiKeyId,
+ }
+ : {}),
"x-exchange-extended-catalog-access": "true",
...(input.requestId ? { "x-request-id": input.requestId } : {}),
...(input.maximumCredits === undefined
diff --git a/apps/api/src/services/alexandria/retrieve.test.ts b/apps/api/src/services/alexandria/retrieve.test.ts
index e25c69b5a..29763804c 100644
--- a/apps/api/src/services/alexandria/retrieve.test.ts
+++ b/apps/api/src/services/alexandria/retrieve.test.ts
@@ -384,3 +384,113 @@ it("forwards an optional version to quote and execution", async () => {
expect(request.body.requests).toEqual([pinned]);
}
});
+
+it("forwards verified terms identity only for terms-only executions", async () => {
+ for (const capability of ["terms/show", "terms/accept"]) {
+ mocks.store.clear();
+ mocks.request.mockClear();
+ const termsCall = {
+ provider: "firecrawl",
+ capability,
+ options: { provider: "benzinga" },
+ };
+ exchangeAnswers({
+ success: true,
+ creditsCost: 0,
+ results: [{ ...termsCall, creditsCost: 0, data: {} }],
+ });
+ await run({ calls: [termsCall], apiKeyIdText: "verified-key" });
+ expect(executions()[0][0].termsIdentity).toEqual({
+ organizationId: "org",
+ apiKeyId: "verified-key",
+ });
+ expect(
+ mocks.request.mock.calls
+ .filter(([arg]) => arg.path !== "/v1/retrieve")
+ .every(([arg]) => arg.termsIdentity === undefined),
+ ).toBe(true);
+ }
+ for (const overrides of [
+ {},
+ { calls: [{ provider: "firecrawl", capability: "terms/show" }, call] },
+ {
+ calls: [{ provider: "firecrawl", capability: "terms/accept" }],
+ orgId: null,
+ },
+ {
+ calls: [{ provider: "firecrawl", capability: "terms/accept" }],
+ apiKeyId: null,
+ },
+ ]) {
+ mocks.store.clear();
+ mocks.request.mockClear();
+ exchangeAnswers(answer, 200, { status: 200, body: { maximumCredits: 0 } });
+ await run(overrides);
+ expect(executions()[0][0].termsIdentity).toBeUndefined();
+ }
+});
+
+it("binds terms replay to exact organization and credential identity", async () => {
+ const termsCall = {
+ provider: "firecrawl",
+ capability: "terms/accept",
+ options: { provider: "benzinga" },
+ };
+ exchangeAnswers(
+ {
+ success: true,
+ creditsCost: 0,
+ results: [{ ...termsCall, creditsCost: 0, data: {} }],
+ },
+ 200,
+ { status: 200, body: { maximumCredits: 0 } },
+ );
+ const input = { calls: [termsCall], apiKeyIdText: "9007199254740993" };
+ expect((await run(input)).executed).toBe(true);
+ expect(executions()[0][0].termsIdentity.apiKeyId).toBe("9007199254740993");
+ expect((await run(input)).executed).toBe(false);
+ for (const change of [
+ { apiKeyIdText: "9007199254740994" },
+ { orgId: "other-org" },
+ { apiKeyIdText: undefined },
+ ]) {
+ expect(await run({ ...input, ...change })).toMatchObject({
+ status: 409,
+ executed: false,
+ });
+ }
+ expect(executions()).toHaveLength(1);
+ expect(
+ (
+ await run({
+ ...input,
+ apiKeyIdText: "9007199254740994",
+ requestId: "another-request",
+ })
+ ).executed,
+ ).toBe(true);
+});
+
+it("does not derive terms credential identity from numeric API-key IDs", async () => {
+ const termsCall = { provider: "firecrawl", capability: "terms/show" };
+ exchangeAnswers(
+ {
+ success: true,
+ creditsCost: 0,
+ results: [{ ...termsCall, creditsCost: 0, data: {} }],
+ },
+ 200,
+ { status: 200, body: { maximumCredits: 0 } },
+ );
+ for (const apiKeyId of [12, Number("9007199254740993")]) {
+ mocks.store.clear();
+ const previousCount = executions().length;
+ expect(await run({ calls: [termsCall], apiKeyId })).toMatchObject({
+ executed: true,
+ });
+ expect(executions()).toHaveLength(previousCount + 1);
+ const execution = executions().at(-1);
+ if (!execution) throw new Error("Expected a provider execution");
+ expect(execution[0].termsIdentity).toBeUndefined();
+ }
+});
diff --git a/apps/api/src/services/alexandria/retrieve.ts b/apps/api/src/services/alexandria/retrieve.ts
index f54970b5f..28f5a777e 100644
--- a/apps/api/src/services/alexandria/retrieve.ts
+++ b/apps/api/src/services/alexandria/retrieve.ts
@@ -82,6 +82,7 @@ export async function retrieveProviders(input: {
* can be named, which is a skipped hold, the same as an unresolvable org. */
orgId: string | null;
apiKeyId: number | null;
+ apiKeyIdText?: string | null;
flags: TeamFlags | null | undefined;
calls: ProviderCall[];
requestId: string;
@@ -119,10 +120,26 @@ export async function retrieveProviders(input: {
),
);
+ const termsOnly =
+ input.calls.length > 0 &&
+ input.calls.every(
+ call =>
+ call.provider === "firecrawl" &&
+ (call.capability === "terms/show" ||
+ call.capability === "terms/accept"),
+ );
+ const termsIdentity =
+ termsOnly && input.orgId && input.apiKeyIdText
+ ? { organizationId: input.orgId, apiKeyId: input.apiKeyIdText }
+ : undefined;
const billable = !input.bypassBilling;
const id = hash([input.teamId, input.requestId]);
const key = `alexandria:retrieve:${id}`;
- const fingerprint = hash([input.calls, billable]);
+ const fingerprint = hash(
+ termsOnly
+ ? [input.calls, billable, termsIdentity ?? null]
+ : [input.calls, billable],
+ );
const record: Retrieval = {
fingerprint,
phase: "executing",
@@ -333,6 +350,7 @@ export async function retrieveProviders(input: {
...(loadsSavedResult && input.resultAuthorization
? { resultAuthorization: input.resultAuthorization }
: {}),
+ ...(termsIdentity ? { termsIdentity } : {}),
maximumCredits,
}).catch(error => {
throw new Error(`Exchange did not answer: ${error?.message ?? error}`);