Skip to content

Commit e07f8ea

Browse files
tomkisjezekra1
authored andcommitted
chore: creatingh react context for a2a
Signed-off-by: Tomas Weiss <[email protected]>
1 parent 9996516 commit e07f8ea

File tree

14 files changed

+157
-87
lines changed

14 files changed

+157
-87
lines changed

apps/agentstack-sdk-ts/src/client/a2a/create-authenticated-fetch.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
export function createAuthenticatedFetch(
7-
token: string,
8-
baseFetch?: typeof fetch,
9-
): typeof fetch {
6+
export function createAuthenticatedFetch(token: string, baseFetch?: typeof fetch): typeof fetch {
107
const fetchImpl = baseFetch ?? (typeof globalThis.fetch !== 'undefined' ? globalThis.fetch : undefined);
118

129
if (!fetchImpl) {

apps/agentstack-sdk-ts/src/client/a2a/extensions/handle-agent-card.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ export interface Fulfillments {
3333
secrets: (demand: SecretDemands) => Promise<SecretFulfillments>;
3434
form: (demand: FormDemands) => Promise<FormFulfillments>;
3535
oauthRedirectUri: () => string | null;
36+
/**
37+
* @deprecated - keeping this for backwards compatibility, context token is now passed via A2A client headers
38+
*/
3639
getContextToken: () => ContextToken;
3740
}
3841

apps/agentstack-ui/src/modules/platform-context/api/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ export async function matchProviders(matchProvidersParams: MatchProvidersParams)
5454
}
5555

5656
export async function createContextToken(createContextTokenParams: CreateContextTokenParams) {
57-
console.log(createContextTokenParams);
5857
const result = await agentstackClient.createContextToken(createContextTokenParams);
5958
return result.token;
6059
}

apps/agentstack-ui/src/modules/platform-context/api/keys.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ export const contextKeys = {
1212
histories: () => [...contextKeys.all(), 'history'] as const,
1313
history: ({ contextId, query = {} }: ListContextHistoryParams) =>
1414
[...contextKeys.histories(), contextId, query] as const,
15+
tokens: () => [...contextKeys.all(), 'token'] as const,
16+
token: (contextId: string, providerId: string) => [...contextKeys.tokens(), contextId, providerId] as const,
1517
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright 2025 © BeeAI a Series of LF Projects, LLC
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import { useQuery } from '@tanstack/react-query';
7+
import type { ContextToken } from 'agentstack-sdk';
8+
9+
import { useApp } from '#contexts/App/index.ts';
10+
import type { Agent } from '#modules/agents/api/types.ts';
11+
12+
import { usePlatformContext } from '../../contexts';
13+
import { createContextToken } from '..';
14+
import { contextKeys } from '../keys';
15+
16+
export function useContextToken(agent: Agent) {
17+
const {
18+
config: { contextTokenPermissions },
19+
} = useApp();
20+
const { contextId } = usePlatformContext();
21+
22+
return useQuery<ContextToken>({
23+
queryKey: contextKeys.token(contextId ?? '', agent.provider.id),
24+
queryFn: async () => {
25+
if (!contextId) {
26+
throw new Error('Context ID is not set.');
27+
}
28+
29+
const token = await createContextToken({
30+
contextId,
31+
contextPermissions: contextTokenPermissions.grant_context_permissions ?? {},
32+
globalPermissions: contextTokenPermissions.grant_global_permissions ?? {},
33+
});
34+
35+
if (!token) {
36+
throw new Error('Could not generate context token');
37+
}
38+
39+
return token;
40+
},
41+
enabled: !!contextId,
42+
staleTime: Infinity,
43+
});
44+
}

apps/agentstack-ui/src/modules/platform-context/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const contextTokenPermissionsDefaults: DeepRequired<ContextTokenPermissio
1515
llm: ['*'],
1616
embeddings: ['*'],
1717
model_providers: [],
18-
a2a_proxy: [],
18+
a2a_proxy: ['*'],
1919
providers: [],
2020
provider_variables: [],
2121
contexts: [],

apps/agentstack-ui/src/modules/runs/api/queries/useBuildA2AClient.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
import { useQuery } from '@tanstack/react-query';
7+
78
import { buildA2AClient, type CreateA2AClientParams } from '#api/a2a/client.ts';
89

910
import { runKeys } from '../keys';
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Copyright 2025 © BeeAI a Series of LF Projects, LLC
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
'use client';
7+
import type { PropsWithChildren } from 'react';
8+
import { useMemo } from 'react';
9+
10+
import type { Agent } from '#modules/agents/api/types.ts';
11+
import { useContextToken } from '#modules/platform-context/api/queries/useContextToken.ts';
12+
import { useBuildA2AClient } from '#modules/runs/api/queries/useBuildA2AClient.ts';
13+
14+
import { A2AClientContext } from './a2a-client-context';
15+
16+
interface Props {
17+
agent: Agent;
18+
}
19+
20+
export function A2AClientProvider({ agent, children }: PropsWithChildren<Props>) {
21+
const { data: contextToken } = useContextToken(agent);
22+
const { agentClient } = useBuildA2AClient({
23+
providerId: agent.provider.id,
24+
authToken: contextToken,
25+
});
26+
27+
const contextValue = useMemo(() => {
28+
if (!contextToken || !agentClient) {
29+
return null;
30+
}
31+
32+
return {
33+
contextToken,
34+
agentClient,
35+
};
36+
}, [contextToken, agentClient]);
37+
38+
if (!contextValue) {
39+
return null;
40+
}
41+
42+
return <A2AClientContext.Provider value={contextValue}>{children}</A2AClientContext.Provider>;
43+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Copyright 2025 © BeeAI a Series of LF Projects, LLC
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
'use client';
6+
import type { ContextToken } from 'agentstack-sdk';
7+
import { createContext } from 'react';
8+
9+
import type { AgentA2AClient } from '#api/a2a/types.ts';
10+
11+
export const A2AClientContext = createContext<A2AClientContextValue | null>(null);
12+
13+
export interface A2AClientContextValue {
14+
contextToken: ContextToken;
15+
agentClient: AgentA2AClient;
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright 2025 © BeeAI a Series of LF Projects, LLC
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import { use } from 'react';
7+
8+
import { A2AClientContext } from './a2a-client-context';
9+
10+
export function useA2AClient() {
11+
const context = use(A2AClientContext);
12+
13+
if (!context) {
14+
throw new Error('useA2AClient must be used within A2AClientProvider');
15+
}
16+
17+
return context;
18+
}
19+
20+
export { A2AClientProvider } from './A2AClientProvider';

0 commit comments

Comments
 (0)