@economic/agents 2.7.0 → 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 +5 -0
- package/dist/providers.d.mts +12 -4
- package/dist/providers.mjs +34 -8
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -86,6 +86,11 @@ const { anthropic, gemini } = createAiGatewayVertexProviders({
|
|
|
86
86
|
});
|
|
87
87
|
```
|
|
88
88
|
|
|
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.
|
|
93
|
+
|
|
89
94
|
## Quick start: an agent
|
|
90
95
|
|
|
91
96
|
Subclass `Agent` and implement `getModel` and `getSystemPrompt`. Add tools with `getTools`, skills with `getSkills`. Expose a `@callable` method to run a turn — `saveMessages` injects a message, runs the turn, and persists the result:
|
package/dist/providers.d.mts
CHANGED
|
@@ -2,6 +2,10 @@ import { AnthropicMessagesLanguageModel } from "@ai-sdk/anthropic/internal";
|
|
|
2
2
|
import { GoogleGenerativeAIProvider } from "@ai-sdk/google";
|
|
3
3
|
import { AnthropicProvider } from "@ai-sdk/anthropic";
|
|
4
4
|
|
|
5
|
+
//#region src/providers/metadata.d.ts
|
|
6
|
+
/** Custom metadata serialized into Cloudflare's `cf-aig-metadata` header. */
|
|
7
|
+
type AiGatewayMetadata = Record<string, unknown>;
|
|
8
|
+
//#endregion
|
|
5
9
|
//#region src/providers/anthropic.d.ts
|
|
6
10
|
type AnthropicVertexModelId = Parameters<AnthropicProvider>[0];
|
|
7
11
|
interface AnthropicVertexProviderOptions {
|
|
@@ -11,8 +15,10 @@ interface AnthropicVertexProviderOptions {
|
|
|
11
15
|
googleCloudProjectId: string;
|
|
12
16
|
location?: string;
|
|
13
17
|
anthropicVersion?: string;
|
|
18
|
+
/** Custom metadata included on every request. `agentId` is injected by the SDK. */
|
|
19
|
+
metadata?: AiGatewayMetadata;
|
|
14
20
|
}
|
|
15
|
-
declare function createAnthropicProvider(options: AnthropicVertexProviderOptions): (modelId: AnthropicVertexModelId, metadata?:
|
|
21
|
+
declare function createAnthropicProvider(options: AnthropicVertexProviderOptions): (modelId: AnthropicVertexModelId, metadata?: AiGatewayMetadata) => AnthropicMessagesLanguageModel;
|
|
16
22
|
//#endregion
|
|
17
23
|
//#region src/providers/gemini.d.ts
|
|
18
24
|
type GeminiModelId = Parameters<GoogleGenerativeAIProvider>[0];
|
|
@@ -22,14 +28,16 @@ interface GeminiProviderOptions {
|
|
|
22
28
|
cloudflareApiToken: string;
|
|
23
29
|
googleCloudProjectId: string;
|
|
24
30
|
location?: string;
|
|
31
|
+
/** Custom metadata included on every request. `agentId` is injected by the SDK. */
|
|
32
|
+
metadata?: AiGatewayMetadata;
|
|
25
33
|
}
|
|
26
|
-
declare function createGeminiProvider(options: GeminiProviderOptions, metadata?:
|
|
34
|
+
declare function createGeminiProvider(options: GeminiProviderOptions, metadata?: AiGatewayMetadata): GoogleGenerativeAIProvider;
|
|
27
35
|
//#endregion
|
|
28
36
|
//#region src/providers/index.d.ts
|
|
29
37
|
interface AiGatewayVertexProvidersOptions extends AnthropicVertexProviderOptions, GeminiProviderOptions {}
|
|
30
38
|
declare function createAiGatewayVertexProviders(options: AiGatewayVertexProvidersOptions): {
|
|
31
|
-
anthropic: (modelId: AnthropicVertexModelId, metadata?:
|
|
39
|
+
anthropic: (modelId: AnthropicVertexModelId, metadata?: AiGatewayMetadata) => import("@ai-sdk/anthropic/internal").AnthropicMessagesLanguageModel;
|
|
32
40
|
gemini: import("@ai-sdk/google").GoogleGenerativeAIProvider;
|
|
33
41
|
};
|
|
34
42
|
//#endregion
|
|
35
|
-
export { AiGatewayVertexProvidersOptions, type AnthropicVertexModelId, type AnthropicVertexProviderOptions, type GeminiModelId, type GeminiProviderOptions, createAiGatewayVertexProviders, createAnthropicProvider, createGeminiProvider };
|
|
43
|
+
export { type AiGatewayMetadata, AiGatewayVertexProvidersOptions, type AnthropicVertexModelId, type AnthropicVertexProviderOptions, type GeminiModelId, type GeminiProviderOptions, createAiGatewayVertexProviders, createAnthropicProvider, createGeminiProvider };
|
package/dist/providers.mjs
CHANGED
|
@@ -1,16 +1,44 @@
|
|
|
1
|
+
import { getCurrentAgent } from "agents";
|
|
1
2
|
import { AnthropicMessagesLanguageModel } from "@ai-sdk/anthropic/internal";
|
|
2
3
|
import { createGoogleGenerativeAI } from "@ai-sdk/google";
|
|
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
|
+
}
|
|
10
|
+
function createAiGatewayHeaders(cloudflareApiToken, defaultMetadata, requestMetadata) {
|
|
11
|
+
const headers = { "cf-aig-authorization": `Bearer ${cloudflareApiToken}` };
|
|
12
|
+
const metadata = { ...defaultMetadata };
|
|
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;
|
|
16
|
+
if (Object.values(metadata).some((value) => value !== void 0)) headers["cf-aig-metadata"] = JSON.stringify(metadata);
|
|
17
|
+
return headers;
|
|
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
|
+
}
|
|
31
|
+
//#endregion
|
|
3
32
|
//#region src/providers/anthropic.ts
|
|
4
33
|
function createAnthropicProvider(options) {
|
|
5
|
-
const { anthropicVersion = "vertex-2023-10-16", cloudflareAccountId, cloudflareAiGatewayId, cloudflareApiToken, googleCloudProjectId, location = "europe-west1" } = options;
|
|
34
|
+
const { anthropicVersion = "vertex-2023-10-16", cloudflareAccountId, cloudflareAiGatewayId, cloudflareApiToken, googleCloudProjectId, location = "europe-west1", metadata: defaultMetadata } = options;
|
|
6
35
|
const anthropicBaseURL = `${`${`https://gateway.ai.cloudflare.com/v1/${cloudflareAccountId}/${cloudflareAiGatewayId}/`}google-vertex-ai/v1/projects/${googleCloudProjectId}/locations/${location}/publishers/`}anthropic/models/`;
|
|
7
36
|
return function anthropic(modelId, metadata) {
|
|
8
|
-
const headers = { "cf-aig-authorization": `Bearer ${cloudflareApiToken}` };
|
|
9
|
-
if (metadata) headers["cf-aig-metadata"] = JSON.stringify(metadata);
|
|
10
37
|
return new AnthropicMessagesLanguageModel(modelId, {
|
|
11
38
|
provider: "vertex.anthropic.messages",
|
|
12
39
|
baseURL: anthropicBaseURL,
|
|
13
|
-
headers,
|
|
40
|
+
headers: {},
|
|
41
|
+
fetch: createAiGatewayFetch(cloudflareApiToken, defaultMetadata, metadata),
|
|
14
42
|
buildRequestUrl: (baseURL, isStreaming) => `${baseURL}${modelId}:${isStreaming ? "streamRawPredict" : "rawPredict"}`,
|
|
15
43
|
transformRequestBody: (args) => {
|
|
16
44
|
const { model, ...rest } = args;
|
|
@@ -28,13 +56,11 @@ function createAnthropicProvider(options) {
|
|
|
28
56
|
//#endregion
|
|
29
57
|
//#region src/providers/gemini.ts
|
|
30
58
|
function createGeminiProvider(options, metadata) {
|
|
31
|
-
const { cloudflareAccountId, cloudflareAiGatewayId, cloudflareApiToken, googleCloudProjectId, location = "europe-west1" } = options;
|
|
32
|
-
const headers = { "cf-aig-authorization": `Bearer ${cloudflareApiToken}` };
|
|
33
|
-
if (metadata) headers["cf-aig-metadata"] = JSON.stringify(metadata);
|
|
59
|
+
const { cloudflareAccountId, cloudflareAiGatewayId, cloudflareApiToken, googleCloudProjectId, location = "europe-west1", metadata: defaultMetadata } = options;
|
|
34
60
|
return createGoogleGenerativeAI({
|
|
35
61
|
apiKey: "unused",
|
|
36
62
|
baseURL: `https://gateway.ai.cloudflare.com/v1/${cloudflareAccountId}/${cloudflareAiGatewayId}/google-vertex-ai/v1/projects/${googleCloudProjectId}/locations/${location}/publishers/google/models`,
|
|
37
|
-
|
|
63
|
+
fetch: createAiGatewayFetch(cloudflareApiToken, defaultMetadata, metadata),
|
|
38
64
|
name: "gateway.google.vertex-ai"
|
|
39
65
|
});
|
|
40
66
|
}
|