-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathhandle-agent-card.ts
More file actions
127 lines (108 loc) · 5.25 KB
/
handle-agent-card.ts
File metadata and controls
127 lines (108 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* Copyright 2025 © BeeAI a Series of LF Projects, LLC
* SPDX-License-Identifier: Apache-2.0
*/
import type { AgentCapabilities } from '@a2a-js/sdk';
import type { ContextToken } from '../../api/types';
import type { EmbeddingDemands, EmbeddingFulfillments } from './services/embedding';
import { embeddingExtension } from './services/embedding';
import type { FormDemands, FormFulfillments } from './services/form';
import { formExtension } from './services/form';
import type { LLMDemands, LLMFulfillments } from './services/llm';
import { llmExtension } from './services/llm';
import type { MCPDemands, MCPFulfillments } from './services/mcp';
import { mcpExtension } from './services/mcp';
import type { OAuthDemands, OAuthFulfillments } from './services/oauth-provider';
import { oauthProviderExtension } from './services/oauth-provider';
import { platformApiExtension } from './services/platform';
import type { SecretDemands, SecretFulfillments } from './services/secrets';
import { secretsExtension } from './services/secrets';
import { oauthRequestExtension } from './ui/oauth';
import type { SettingsDemands, SettingsFulfillments } from './ui/settings';
import { settingsExtension } from './ui/settings';
import { extractServiceExtensionDemands, fulfillServiceExtensionDemand } from './utils';
export interface Fulfillments {
llm: (demand: LLMDemands) => Promise<LLMFulfillments>;
embedding: (demand: EmbeddingDemands) => Promise<EmbeddingFulfillments>;
mcp: (demand: MCPDemands) => Promise<MCPFulfillments>;
oauth: (demand: OAuthDemands) => Promise<OAuthFulfillments>;
settings: (demand: SettingsDemands) => Promise<SettingsFulfillments>;
secrets: (demand: SecretDemands) => Promise<SecretFulfillments>;
form: (demand: FormDemands) => Promise<FormFulfillments>;
oauthRedirectUri: () => string | null;
/**
* @deprecated - keeping this for backwards compatibility, context token is now passed via A2A client headers
*/
getContextToken: () => ContextToken;
}
const mcpExtensionExtractor = extractServiceExtensionDemands(mcpExtension);
const llmExtensionExtractor = extractServiceExtensionDemands(llmExtension);
const embeddingExtensionExtractor = extractServiceExtensionDemands(embeddingExtension);
const oauthExtensionExtractor = extractServiceExtensionDemands(oauthProviderExtension);
const settingsExtensionExtractor = extractServiceExtensionDemands(settingsExtension);
const secretExtensionExtractor = extractServiceExtensionDemands(secretsExtension);
const formExtensionExtractor = extractServiceExtensionDemands(formExtension);
const fulfillMcpDemand = fulfillServiceExtensionDemand(mcpExtension);
const fulfillLlmDemand = fulfillServiceExtensionDemand(llmExtension);
const fulfillEmbeddingDemand = fulfillServiceExtensionDemand(embeddingExtension);
const fulfillOAuthDemand = fulfillServiceExtensionDemand(oauthProviderExtension);
const fulfillSettingsDemand = fulfillServiceExtensionDemand(settingsExtension);
const fulfillSecretDemand = fulfillServiceExtensionDemand(secretsExtension);
const fulfillFormDemand = fulfillServiceExtensionDemand(formExtension);
export const handleAgentCard = (agentCard: { capabilities: AgentCapabilities }) => {
const extensions = agentCard.capabilities.extensions ?? [];
const llmDemands = llmExtensionExtractor(extensions);
const embeddingDemands = embeddingExtensionExtractor(extensions);
const mcpDemands = mcpExtensionExtractor(extensions);
const oauthDemands = oauthExtensionExtractor(extensions);
const settingsDemands = settingsExtensionExtractor(extensions);
const secretDemands = secretExtensionExtractor(extensions);
const formDemands = formExtensionExtractor(extensions);
const resolveMetadata = async (fulfillments: Fulfillments) => {
let fulfilledMetadata: Record<string, unknown> = {};
fulfilledMetadata = platformApiExtension(fulfilledMetadata, fulfillments.getContextToken());
if (llmDemands) {
fulfilledMetadata = fulfillLlmDemand(fulfilledMetadata, await fulfillments.llm(llmDemands));
}
if (embeddingDemands) {
fulfilledMetadata = fulfillEmbeddingDemand(fulfilledMetadata, await fulfillments.embedding(embeddingDemands));
}
if (mcpDemands) {
fulfilledMetadata = fulfillMcpDemand(fulfilledMetadata, await fulfillments.mcp(mcpDemands));
}
if (oauthDemands) {
fulfilledMetadata = fulfillOAuthDemand(fulfilledMetadata, await fulfillments.oauth(oauthDemands));
}
if (settingsDemands) {
fulfilledMetadata = fulfillSettingsDemand(fulfilledMetadata, await fulfillments.settings(settingsDemands));
}
if (secretDemands) {
fulfilledMetadata = fulfillSecretDemand(fulfilledMetadata, await fulfillments.secrets(secretDemands));
}
if (formDemands) {
fulfilledMetadata = fulfillFormDemand(fulfilledMetadata, await fulfillments.form(formDemands));
}
const oauthRedirectUri = fulfillments.oauthRedirectUri();
if (oauthRedirectUri) {
fulfilledMetadata = {
...fulfilledMetadata,
[oauthRequestExtension.getUri()]: {
redirect_uri: oauthRedirectUri,
},
};
}
return fulfilledMetadata;
};
return {
resolveMetadata,
demands: {
llmDemands,
embeddingDemands,
mcpDemands,
oauthDemands,
settingsDemands,
secretDemands,
formDemands,
},
};
};