@economic/agents 2.7.1 → 2.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -5
- package/dist/providers.d.mts +2 -2
- package/dist/providers.mjs +23 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -83,14 +83,13 @@ const { anthropic, gemini } = createAiGatewayVertexProviders({
|
|
|
83
83
|
cloudflareAiGatewayId: env.AI_GATEWAY_ID,
|
|
84
84
|
cloudflareApiToken: env.CLOUDFLARE_API_TOKEN,
|
|
85
85
|
googleCloudProjectId: env.GOOGLE_CLOUD_PROJECT_ID,
|
|
86
|
-
// Included in cf-aig-metadata on every request, including background calls.
|
|
87
|
-
metadata: { agentId: "support" },
|
|
88
86
|
});
|
|
89
87
|
```
|
|
90
88
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
by
|
|
89
|
+
When these providers are used inside an SDK agent, the SDK automatically adds
|
|
90
|
+
the entry-point agent class as `agentId` in `cf-aig-metadata`. This includes
|
|
91
|
+
requests made by child chat/facet agents and allows a shared gateway's usage to
|
|
92
|
+
be grouped by agent without application configuration or duplicated analytics.
|
|
94
93
|
|
|
95
94
|
## Quick start: an agent
|
|
96
95
|
|
package/dist/providers.d.mts
CHANGED
|
@@ -15,7 +15,7 @@ interface AnthropicVertexProviderOptions {
|
|
|
15
15
|
googleCloudProjectId: string;
|
|
16
16
|
location?: string;
|
|
17
17
|
anthropicVersion?: string;
|
|
18
|
-
/**
|
|
18
|
+
/** Custom metadata included on every request. `agentId` is injected by the SDK. */
|
|
19
19
|
metadata?: AiGatewayMetadata;
|
|
20
20
|
}
|
|
21
21
|
declare function createAnthropicProvider(options: AnthropicVertexProviderOptions): (modelId: AnthropicVertexModelId, metadata?: AiGatewayMetadata) => AnthropicMessagesLanguageModel;
|
|
@@ -28,7 +28,7 @@ interface GeminiProviderOptions {
|
|
|
28
28
|
cloudflareApiToken: string;
|
|
29
29
|
googleCloudProjectId: string;
|
|
30
30
|
location?: string;
|
|
31
|
-
/**
|
|
31
|
+
/** Custom metadata included on every request. `agentId` is injected by the SDK. */
|
|
32
32
|
metadata?: AiGatewayMetadata;
|
|
33
33
|
}
|
|
34
34
|
declare function createGeminiProvider(options: GeminiProviderOptions, metadata?: AiGatewayMetadata): GoogleGenerativeAIProvider;
|
package/dist/providers.mjs
CHANGED
|
@@ -1,13 +1,33 @@
|
|
|
1
|
+
import { getCurrentAgent } from "agents";
|
|
1
2
|
import { AnthropicMessagesLanguageModel } from "@ai-sdk/anthropic/internal";
|
|
2
3
|
import { createGoogleGenerativeAI } from "@ai-sdk/google";
|
|
3
4
|
//#region src/providers/metadata.ts
|
|
5
|
+
function getCurrentAgentId() {
|
|
6
|
+
const agent = getCurrentAgent().agent;
|
|
7
|
+
if (!agent) return void 0;
|
|
8
|
+
return agent.selfPath[0]?.className ?? agent.constructor.name;
|
|
9
|
+
}
|
|
4
10
|
function createAiGatewayHeaders(cloudflareApiToken, defaultMetadata, requestMetadata) {
|
|
5
11
|
const headers = { "cf-aig-authorization": `Bearer ${cloudflareApiToken}` };
|
|
6
12
|
const metadata = { ...defaultMetadata };
|
|
7
13
|
for (const [key, value] of Object.entries(requestMetadata ?? {})) if (!(key in metadata)) metadata[key] = value;
|
|
14
|
+
const agentId = getCurrentAgentId();
|
|
15
|
+
if (agentId) metadata.agentId = agentId;
|
|
8
16
|
if (Object.values(metadata).some((value) => value !== void 0)) headers["cf-aig-metadata"] = JSON.stringify(metadata);
|
|
9
17
|
return headers;
|
|
10
18
|
}
|
|
19
|
+
/** Adds AI Gateway authentication and SDK-owned agent metadata at request time. */
|
|
20
|
+
function createAiGatewayFetch(cloudflareApiToken, defaultMetadata, requestMetadata) {
|
|
21
|
+
return (input, init) => {
|
|
22
|
+
const headers = new Headers(input instanceof Request ? input.headers : void 0);
|
|
23
|
+
new Headers(init?.headers).forEach((value, key) => headers.set(key, value));
|
|
24
|
+
for (const [key, value] of Object.entries(createAiGatewayHeaders(cloudflareApiToken, defaultMetadata, requestMetadata))) headers.set(key, value);
|
|
25
|
+
return globalThis.fetch(input, {
|
|
26
|
+
...init,
|
|
27
|
+
headers
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
}
|
|
11
31
|
//#endregion
|
|
12
32
|
//#region src/providers/anthropic.ts
|
|
13
33
|
function createAnthropicProvider(options) {
|
|
@@ -17,7 +37,8 @@ function createAnthropicProvider(options) {
|
|
|
17
37
|
return new AnthropicMessagesLanguageModel(modelId, {
|
|
18
38
|
provider: "vertex.anthropic.messages",
|
|
19
39
|
baseURL: anthropicBaseURL,
|
|
20
|
-
headers:
|
|
40
|
+
headers: {},
|
|
41
|
+
fetch: createAiGatewayFetch(cloudflareApiToken, defaultMetadata, metadata),
|
|
21
42
|
buildRequestUrl: (baseURL, isStreaming) => `${baseURL}${modelId}:${isStreaming ? "streamRawPredict" : "rawPredict"}`,
|
|
22
43
|
transformRequestBody: (args) => {
|
|
23
44
|
const { model, ...rest } = args;
|
|
@@ -39,7 +60,7 @@ function createGeminiProvider(options, metadata) {
|
|
|
39
60
|
return createGoogleGenerativeAI({
|
|
40
61
|
apiKey: "unused",
|
|
41
62
|
baseURL: `https://gateway.ai.cloudflare.com/v1/${cloudflareAccountId}/${cloudflareAiGatewayId}/google-vertex-ai/v1/projects/${googleCloudProjectId}/locations/${location}/publishers/google/models`,
|
|
42
|
-
|
|
63
|
+
fetch: createAiGatewayFetch(cloudflareApiToken, defaultMetadata, metadata),
|
|
43
64
|
name: "gateway.google.vertex-ai"
|
|
44
65
|
});
|
|
45
66
|
}
|