Skip to content

Commit 5da63e8

Browse files
committed
update code
1 parent 0bea326 commit 5da63e8

File tree

3 files changed

+26
-24
lines changed

3 files changed

+26
-24
lines changed

integrations/mastra/typescript/src/copilotkit.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import {
55
CopilotServiceAdapter,
66
ExperimentalEmptyAdapter,
77
} from "@copilotkit/runtime";
8-
import { RuntimeContext } from "@mastra/core/runtime-context";
8+
import { RequestContext } from "@mastra/core/request-context";
99
import { registerApiRoute } from "@mastra/core/server";
1010
import { MastraAgent } from "./mastra";
11+
1112
export function registerCopilotKit<T extends Record<string, any> | unknown = unknown>({
1213
path,
1314
resourceId,
@@ -19,25 +20,25 @@ export function registerCopilotKit<T extends Record<string, any> | unknown = unk
1920
resourceId: string;
2021
serviceAdapter?: CopilotServiceAdapter;
2122
agents?: Record<string, AbstractAgent>;
22-
setContext?: (c: any, runtimeContext: RuntimeContext<T>) => void | Promise<void>;
23+
setContext?: (c: any, requestContext: RequestContext<T>) => void | Promise<void>;
2324
}) {
2425
return registerApiRoute(path, {
2526
method: `ALL`,
2627
handler: async (c) => {
2728
const mastra = c.get("mastra");
2829

29-
const runtimeContext = new RuntimeContext<T>();
30+
const requestContext = new RequestContext<T>();
3031

3132
if (setContext) {
32-
await setContext(c, runtimeContext);
33+
await setContext(c, requestContext);
3334
}
3435

3536
const aguiAgents =
3637
agents ||
3738
MastraAgent.getLocalAgents({
3839
resourceId,
3940
mastra,
40-
runtimeContext,
41+
requestContext,
4142
});
4243

4344
const runtime = new CopilotRuntime({

integrations/mastra/typescript/src/mastra.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ import type {
1414
import { AbstractAgent, EventType } from "@ag-ui/client";
1515
import type { StorageThreadType } from "@mastra/core";
1616
import { Agent as LocalMastraAgent } from "@mastra/core/agent";
17-
import { RuntimeContext } from "@mastra/core/runtime-context";
17+
import { RequestContext } from "@mastra/core/request-context";
1818
import { randomUUID } from "@ag-ui/client";
1919
import { Observable } from "rxjs";
2020
import { MastraClient } from "@mastra/client-js";
21-
type RemoteMastraAgent = ReturnType<MastraClient["getAgent"]>;
2221
import {
2322
convertAGUIMessagesToMastra,
2423
GetLocalAgentsOptions,
@@ -31,10 +30,12 @@ import {
3130
getNetwork,
3231
} from "./utils";
3332

33+
type RemoteMastraAgent = ReturnType<MastraClient["getAgent"]>;
34+
3435
export interface MastraAgentConfig extends AgentConfig {
3536
agent: LocalMastraAgent | RemoteMastraAgent;
3637
resourceId?: string;
37-
runtimeContext?: RuntimeContext;
38+
requestContext?: RequestContext;
3839
}
3940

4041
interface MastraAgentStreamOptions {
@@ -53,14 +54,14 @@ interface MastraAgentStreamOptions {
5354
export class MastraAgent extends AbstractAgent {
5455
agent: LocalMastraAgent | RemoteMastraAgent;
5556
resourceId?: string;
56-
runtimeContext?: RuntimeContext;
57+
requestContext?: RequestContext;
5758

5859
constructor(private config: MastraAgentConfig) {
59-
const { agent, resourceId, runtimeContext, ...rest } = config;
60+
const { agent, resourceId, requestContext, ...rest } = config;
6061
super(rest);
6162
this.agent = agent;
6263
this.resourceId = resourceId;
63-
this.runtimeContext = runtimeContext ?? new RuntimeContext();
64+
this.requestContext = requestContext ?? new RequestContext();
6465
}
6566

6667
public clone() {
@@ -270,8 +271,8 @@ export class MastraAgent extends AbstractAgent {
270271
const resourceId = this.resourceId ?? threadId;
271272

272273
const convertedMessages = convertAGUIMessagesToMastra(messages);
273-
this.runtimeContext?.set("ag-ui", { context: inputContext });
274-
const runtimeContext = this.runtimeContext;
274+
this.requestContext?.set("ag-ui", { context: inputContext });
275+
const requestContext = this.requestContext;
275276

276277
if (this.isLocalMastraAgent(this.agent)) {
277278
// Local agent - use the agent's stream method directly
@@ -281,7 +282,7 @@ export class MastraAgent extends AbstractAgent {
281282
resourceId,
282283
runId,
283284
clientTools,
284-
runtimeContext,
285+
requestContext,
285286
});
286287

287288
// For local agents, the response should already be a stream

integrations/mastra/typescript/src/utils.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { AbstractAgent } from "@ag-ui/client";
33
import { MastraClient } from "@mastra/client-js";
44
import type { CoreMessage, Mastra } from "@mastra/core";
55
import { Agent as LocalMastraAgent } from "@mastra/core/agent";
6-
import { RuntimeContext } from "@mastra/core/runtime-context";
6+
import { RequestContext } from "@mastra/core/request-context";
77
import { MastraAgent } from "./mastra";
88

99
const toMastraTextContent = (content: Message["content"]): string => {
@@ -116,13 +116,13 @@ export async function getRemoteAgents({
116116
export interface GetLocalAgentsOptions {
117117
mastra: Mastra;
118118
resourceId?: string;
119-
runtimeContext?: RuntimeContext;
119+
requestContext?: RequestContext;
120120
}
121121

122122
export function getLocalAgents({
123123
mastra,
124124
resourceId,
125-
runtimeContext,
125+
requestContext,
126126
}: GetLocalAgentsOptions): Record<string, AbstractAgent> {
127127
const agents = mastra.getAgents() || {};
128128

@@ -132,7 +132,7 @@ export function getLocalAgents({
132132
agentId,
133133
agent,
134134
resourceId,
135-
runtimeContext,
135+
requestContext,
136136
});
137137
return acc;
138138
},
@@ -146,14 +146,14 @@ export interface GetLocalAgentOptions {
146146
mastra: Mastra;
147147
agentId: string;
148148
resourceId?: string;
149-
runtimeContext?: RuntimeContext;
149+
requestContext?: RequestContext;
150150
}
151151

152152
export function getLocalAgent({
153153
mastra,
154154
agentId,
155155
resourceId,
156-
runtimeContext,
156+
requestContext,
157157
}: GetLocalAgentOptions) {
158158
const agent = mastra.getAgent(agentId);
159159
if (!agent) {
@@ -163,18 +163,18 @@ export function getLocalAgent({
163163
agentId,
164164
agent,
165165
resourceId,
166-
runtimeContext,
166+
requestContext,
167167
}) as AbstractAgent;
168168
}
169169

170170
export interface GetNetworkOptions {
171171
mastra: Mastra;
172172
networkId: string;
173173
resourceId?: string;
174-
runtimeContext?: RuntimeContext;
174+
requestContext?: RequestContext;
175175
}
176176

177-
export function getNetwork({ mastra, networkId, resourceId, runtimeContext }: GetNetworkOptions) {
177+
export function getNetwork({ mastra, networkId, resourceId, requestContext }: GetNetworkOptions) {
178178
const network = mastra.getAgent(networkId);
179179
if (!network) {
180180
throw new Error(`Network ${networkId} not found`);
@@ -183,6 +183,6 @@ export function getNetwork({ mastra, networkId, resourceId, runtimeContext }: Ge
183183
agentId: network.name!,
184184
agent: network as unknown as LocalMastraAgent,
185185
resourceId,
186-
runtimeContext,
186+
requestContext,
187187
}) as AbstractAgent;
188188
}

0 commit comments

Comments
 (0)