Skip to content

Commit 3a53eb5

Browse files
authored
Break out mastra copilot dependency to subpackage (#312)
* refactor: move copilot stuff to its own file * update package.json to accomodate separate import
1 parent 6cfd184 commit 3a53eb5

File tree

3 files changed

+91
-57
lines changed

3 files changed

+91
-57
lines changed

typescript-sdk/integrations/mastra/package.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,25 @@
1414
"dist/**",
1515
"README.md"
1616
],
17+
"exports": {
18+
".": {
19+
"types": "./dist/index.d.ts",
20+
"import": "./dist/index.mjs",
21+
"require": "./dist/index.js"
22+
},
23+
"./copilotkit": {
24+
"types": "./dist/copilotkit.d.ts",
25+
"import": "./dist/copilotkit.mjs",
26+
"require": "./dist/copilotkit.js"
27+
}
28+
},
29+
"typesVersions": {
30+
"*": {
31+
"copilotkit": [
32+
"dist/copilotkit.d.ts"
33+
]
34+
}
35+
},
1736
"scripts": {
1837
"build": "tsup",
1938
"dev": "tsup --watch",
@@ -23,6 +42,20 @@
2342
"link:global": "pnpm link --global",
2443
"unlink:global": "pnpm unlink --global"
2544
},
45+
"tsup": {
46+
"entry": {
47+
"index": "src/index.ts",
48+
"copilotkit": "src/copilotkit.ts"
49+
},
50+
"dts": true,
51+
"format": [
52+
"cjs",
53+
"esm"
54+
],
55+
"splitting": false,
56+
"sourcemap": true,
57+
"clean": true
58+
},
2659
"dependencies": {
2760
"@ai-sdk/ui-utils": "^1.1.19",
2861
"@mastra/client-js": "^0.10.18",
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { AbstractAgent } from "@ag-ui/client";
2+
import {
3+
CopilotRuntime,
4+
copilotRuntimeNodeHttpEndpoint,
5+
CopilotServiceAdapter,
6+
ExperimentalEmptyAdapter,
7+
} from "@copilotkit/runtime";
8+
import { RuntimeContext } from "@mastra/core/runtime-context";
9+
import { registerApiRoute } from "@mastra/core/server";
10+
import { MastraAgent } from "./mastra";
11+
export function registerCopilotKit<T extends Record<string, any> | unknown = unknown>({
12+
path,
13+
resourceId,
14+
serviceAdapter = new ExperimentalEmptyAdapter(),
15+
agents,
16+
setContext,
17+
}: {
18+
path: string;
19+
resourceId: string;
20+
serviceAdapter?: CopilotServiceAdapter;
21+
agents?: Record<string, AbstractAgent>;
22+
setContext?: (c: any, runtimeContext: RuntimeContext<T>) => void | Promise<void>;
23+
}) {
24+
return registerApiRoute(path, {
25+
method: `ALL`,
26+
handler: async (c) => {
27+
const mastra = c.get("mastra");
28+
29+
const runtimeContext = new RuntimeContext<T>();
30+
31+
if (setContext) {
32+
await setContext(c, runtimeContext);
33+
}
34+
35+
const aguiAgents =
36+
agents ||
37+
MastraAgent.getLocalAgents({
38+
resourceId,
39+
mastra,
40+
runtimeContext,
41+
});
42+
43+
const runtime = new CopilotRuntime({
44+
agents: aguiAgents,
45+
});
46+
47+
const handler = copilotRuntimeNodeHttpEndpoint({
48+
endpoint: path,
49+
runtime,
50+
serviceAdapter,
51+
});
52+
53+
return handler.handle(c.req.raw, {});
54+
},
55+
});
56+
}

typescript-sdk/integrations/mastra/src/utils.ts

Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
import type { Message } from "@ag-ui/client";
22
import { AbstractAgent } from "@ag-ui/client";
3-
import {
4-
CopilotRuntime,
5-
copilotRuntimeNodeHttpEndpoint,
6-
CopilotServiceAdapter,
7-
ExperimentalEmptyAdapter,
8-
} from "@copilotkit/runtime";
9-
import type { CoreMessage } from "@mastra/core";
10-
import { registerApiRoute } from "@mastra/core/server";
11-
import type { Mastra } from "@mastra/core";
3+
import { MastraClient } from "@mastra/client-js";
4+
import type { CoreMessage, Mastra } from "@mastra/core";
125
import { Agent as LocalMastraAgent } from "@mastra/core/agent";
136
import { RuntimeContext } from "@mastra/core/runtime-context";
14-
import { MastraClient } from "@mastra/client-js";
157
import { MastraAgent } from "./mastra";
168

179
export function convertAGUIMessagesToMastra(messages: Message[]): CoreMessage[] {
@@ -66,53 +58,6 @@ export function convertAGUIMessagesToMastra(messages: Message[]): CoreMessage[]
6658
return result;
6759
}
6860

69-
export function registerCopilotKit<T extends Record<string, any> | unknown = unknown>({
70-
path,
71-
resourceId,
72-
serviceAdapter = new ExperimentalEmptyAdapter(),
73-
agents,
74-
setContext,
75-
}: {
76-
path: string;
77-
resourceId: string;
78-
serviceAdapter?: CopilotServiceAdapter;
79-
agents?: Record<string, AbstractAgent>;
80-
setContext?: (c: any, runtimeContext: RuntimeContext<T>) => void | Promise<void>;
81-
}) {
82-
return registerApiRoute(path, {
83-
method: `ALL`,
84-
handler: async (c) => {
85-
const mastra = c.get("mastra");
86-
87-
const runtimeContext = new RuntimeContext<T>();
88-
89-
if (setContext) {
90-
await setContext(c, runtimeContext);
91-
}
92-
93-
const aguiAgents =
94-
agents ||
95-
MastraAgent.getLocalAgents({
96-
resourceId,
97-
mastra,
98-
runtimeContext,
99-
});
100-
101-
const runtime = new CopilotRuntime({
102-
agents: aguiAgents,
103-
});
104-
105-
const handler = copilotRuntimeNodeHttpEndpoint({
106-
endpoint: path,
107-
runtime,
108-
serviceAdapter,
109-
});
110-
111-
return handler.handle(c.req.raw, {});
112-
},
113-
});
114-
}
115-
11661
export interface GetRemoteAgentsOptions {
11762
mastraClient: MastraClient;
11863
resourceId?: string;

0 commit comments

Comments
 (0)