Skip to content

Commit 650f26d

Browse files
authored
fix(ui): created context metadata (#1302)
1 parent 88d7a2d commit 650f26d

File tree

7 files changed

+63
-15
lines changed

7 files changed

+63
-15
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {
1313
ListContextHistoryParams,
1414
ListContextsParams,
1515
MatchModelProvidersParams,
16+
UpdateContextParams,
1617
} from './types';
1718

1819
export async function createContext(body: CreateContextParams) {
@@ -27,8 +28,17 @@ export async function listContexts({ query }: ListContextsParams) {
2728
return ensureData(response);
2829
}
2930

30-
export async function deleteContext({ context_id }: DeleteContextParams) {
31-
const response = await api.DELETE('/api/v1/contexts/{context_id}', { params: { path: { context_id } } });
31+
export async function updateContext({ context_id, metadata }: UpdateContextParams) {
32+
const response = await api.PUT('/api/v1/contexts/{context_id}', {
33+
body: { metadata },
34+
params: { path: { context_id } },
35+
});
36+
37+
return ensureData(response);
38+
}
39+
40+
export async function deleteContext(path: DeleteContextParams) {
41+
const response = await api.DELETE('/api/v1/contexts/{context_id}', { params: { path } });
3242

3343
return ensureData(response);
3444
}
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 { useMutation } from '@tanstack/react-query';
7+
8+
import { updateContext } from '..';
9+
import { contextKeys } from '../keys';
10+
11+
export function useUpdateContext() {
12+
const mutation = useMutation({
13+
mutationFn: updateContext,
14+
meta: {
15+
invalidates: [contextKeys.lists()],
16+
},
17+
});
18+
19+
return mutation;
20+
}

apps/beeai-ui/src/modules/platform-context/api/types.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,21 @@ import type { ApiPath, ApiQuery, ApiRequest, ApiResponse } from '#@types/utils.t
77

88
import type { ContextMetadata, ModelCapability } from '../types';
99

10-
type CreateContextRequest = ApiRequest<'/api/v1/contexts'>;
11-
export type CreateContextParams = CreateContextRequest & {
12-
metadata: Required<Pick<ContextMetadata, 'agent_name' | 'provider_id'>>;
13-
};
10+
export type CreateContextRequest = ApiRequest<'/api/v1/contexts'>;
11+
export type CreateContextParams = CreateContextRequest;
1412
export type CreateContextResponse = ApiResponse<'/api/v1/contexts', 'post', 'application/json', 201>;
1513

1614
export type ListContextsQuery = ApiQuery<'/api/v1/contexts'>;
1715
export type ListContextsResponse = ApiResponse<'/api/v1/contexts'>;
1816
export type ListContextsParams = { query?: ListContextsQuery };
1917

20-
type DeleteContextPath = ApiPath<'/api/v1/contexts/{context_id}'>;
18+
export type UpdateContextPath = ApiPath<'/api/v1/contexts/{context_id}', 'put'>;
19+
export type UpdateContextRequest = ApiRequest<'/api/v1/contexts/{context_id}', 'put'> & {
20+
metadata: Pick<ContextMetadata, 'agent_name' | 'provider_id'>;
21+
};
22+
export type UpdateContextParams = UpdateContextPath & UpdateContextRequest;
23+
24+
export type DeleteContextPath = ApiPath<'/api/v1/contexts/{context_id}'>;
2125
export type DeleteContextParams = DeleteContextPath;
2226

2327
export type ListContextHistoryQuery = ApiQuery<'/api/v1/contexts/{context_id}/history'>;

apps/beeai-ui/src/modules/platform-context/contexts/PlatformContextProvider.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { useParamsFromUrl } from '#hooks/useParamsFromUrl.ts';
99
import type { Agent } from '#modules/agents/api/types.ts';
1010

1111
import { useCreateContext } from '../api/mutations/useCreateContext';
12+
import { useUpdateContext } from '../api/mutations/useUpdateContext';
1213
import type { ListContextHistoryResponse } from '../api/types';
1314
import { PlatformContext } from './platform-context';
1415

@@ -24,7 +25,7 @@ export function PlatformContextProvider({ history, children }: PropsWithChildren
2425
setContextId(urlContextId ?? null);
2526
}, [urlContextId]);
2627

27-
const { mutateAsync: createContextMutate } = useCreateContext({
28+
const { mutateAsync: createContext } = useCreateContext({
2829
onSuccess: (context) => {
2930
if (!context) {
3031
throw new Error(`Context has not been created`);
@@ -34,20 +35,27 @@ export function PlatformContextProvider({ history, children }: PropsWithChildren
3435
},
3536
});
3637

38+
const { mutateAsync: updateContext } = useUpdateContext();
39+
3740
const resetContext = useCallback(() => {
3841
setContextId(null);
3942
}, []);
4043

41-
const createContext = useCallback(
44+
const updateContextWithAgentMetadata = useCallback(
4245
async (agent: Agent) => {
43-
await createContextMutate({
46+
if (!contextId) {
47+
return;
48+
}
49+
50+
await updateContext({
51+
context_id: contextId,
4452
metadata: {
4553
agent_name: agent.name ?? '',
4654
provider_id: agent.provider.id ?? '',
4755
},
4856
});
4957
},
50-
[createContextMutate],
58+
[contextId, updateContext],
5159
);
5260

5361
const getContextId = useCallback(() => {
@@ -66,6 +74,7 @@ export function PlatformContextProvider({ history, children }: PropsWithChildren
6674
createContext,
6775
getContextId,
6876
resetContext,
77+
updateContextWithAgentMetadata,
6978
}}
7079
>
7180
{children}

apps/beeai-ui/src/modules/platform-context/contexts/platform-context.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6+
import type { UseMutateAsyncFunction } from '@tanstack/react-query';
67
import { createContext } from 'react';
78

89
import type { Agent } from '#modules/agents/api/types.ts';
910
import type { ContextId } from '#modules/tasks/api/types.ts';
1011

11-
import type { ListContextHistoryResponse } from '../api/types';
12+
import type { CreateContextParams, CreateContextResponse, ListContextHistoryResponse } from '../api/types';
1213

1314
export type ContextToken = {
1415
token: string;
@@ -21,7 +22,8 @@ interface PlatformContextValue {
2122

2223
getContextId: () => ContextId;
2324
resetContext: () => void;
24-
createContext: (agent: Agent) => Promise<void>;
25+
createContext: UseMutateAsyncFunction<CreateContextResponse | undefined, Error, CreateContextParams>;
26+
updateContextWithAgentMetadata: (agent: Agent) => Promise<void>;
2527
}
2628

2729
export const PlatformContext = createContext<PlatformContextValue | null>(null);

apps/beeai-ui/src/modules/platform-context/hooks/useEnsurePlatformContext.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function useEnsurePlatformContext(agent?: Agent) {
2222

2323
const ensureContext = async () => {
2424
if (!contextId) {
25-
await createContext(agent);
25+
await createContext({});
2626
}
2727
};
2828

apps/beeai-ui/src/modules/runs/contexts/agent-run/AgentRunProvider.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ function AgentRunProvider({
106106
const pendingSubscription = useRef<() => void>(undefined);
107107
const pendingRun = useRef<ChatRun>(undefined);
108108

109-
const { contextId, getContextId } = usePlatformContext();
109+
const { contextId, getContextId, updateContextWithAgentMetadata } = usePlatformContext();
110110
const { getFullfilments } = useAgentDemands();
111111
const { files, clearFiles } = useFileUpload();
112112

@@ -203,6 +203,7 @@ function AgentRunProvider({
203203
}
204204

205205
checkPendingRun();
206+
updateContextWithAgentMetadata(agent);
206207
setIsPending(true);
207208
setStats({ startTime: Date.now() });
208209

@@ -295,6 +296,8 @@ function AgentRunProvider({
295296
getFullfilments,
296297
setMessages,
297298
agentClient,
299+
agent,
300+
updateContextWithAgentMetadata,
298301
updateCurrentAgentMessage,
299302
handleError,
300303
],

0 commit comments

Comments
 (0)