Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions typescript-sdk/integrations/mastra/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@
"dist/**",
"README.md"
],
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.js"
},
"./copilotkit": {
"types": "./dist/copilotkit.d.ts",
"import": "./dist/copilotkit.mjs",
"require": "./dist/copilotkit.js"
}
},
"typesVersions": {
"*": {
"copilotkit": [
"dist/copilotkit.d.ts"
]
}
},
"scripts": {
"build": "tsup",
"dev": "tsup --watch",
Expand All @@ -23,6 +42,20 @@
"link:global": "pnpm link --global",
"unlink:global": "pnpm unlink --global"
},
"tsup": {
"entry": {
"index": "src/index.ts",
"copilotkit": "src/copilotkit.ts"
},
"dts": true,
"format": [
"cjs",
"esm"
],
"splitting": false,
"sourcemap": true,
"clean": true
},
"dependencies": {
"@ag-ui/client": "workspace:*",
"@ai-sdk/ui-utils": "^1.1.19",
Expand Down
56 changes: 56 additions & 0 deletions typescript-sdk/integrations/mastra/src/copilotkit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { AbstractAgent } from "@ag-ui/client";
import {
CopilotRuntime,
copilotRuntimeNodeHttpEndpoint,
CopilotServiceAdapter,
ExperimentalEmptyAdapter,
} from "@copilotkit/runtime";
import { RuntimeContext } from "@mastra/core/runtime-context";
import { registerApiRoute } from "@mastra/core/server";
import { MastraAgent } from "./mastra";
export function registerCopilotKit<T extends Record<string, any> | unknown = unknown>({
path,
resourceId,
serviceAdapter = new ExperimentalEmptyAdapter(),
agents,
setContext,
}: {
path: string;
resourceId: string;
serviceAdapter?: CopilotServiceAdapter;
agents?: Record<string, AbstractAgent>;
setContext?: (c: any, runtimeContext: RuntimeContext<T>) => void | Promise<void>;
}) {
return registerApiRoute(path, {
method: `ALL`,
handler: async (c) => {
const mastra = c.get("mastra");

const runtimeContext = new RuntimeContext<T>();

if (setContext) {
await setContext(c, runtimeContext);
}

const aguiAgents =
agents ||
MastraAgent.getLocalAgents({
resourceId,
mastra,
runtimeContext,
});

const runtime = new CopilotRuntime({
agents: aguiAgents,
});

const handler = copilotRuntimeNodeHttpEndpoint({
endpoint: path,
runtime,
serviceAdapter,
});

return handler.handle(c.req.raw, {});
},
});
}
59 changes: 2 additions & 57 deletions typescript-sdk/integrations/mastra/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import type { Message } from "@ag-ui/client";
import { AbstractAgent } from "@ag-ui/client";
import {
CopilotRuntime,
copilotRuntimeNodeHttpEndpoint,
CopilotServiceAdapter,
ExperimentalEmptyAdapter,
} from "@copilotkit/runtime";
import type { CoreMessage } from "@mastra/core";
import { registerApiRoute } from "@mastra/core/server";
import type { Mastra } from "@mastra/core";
import { MastraClient } from "@mastra/client-js";
import type { CoreMessage, Mastra } from "@mastra/core";
import { Agent as LocalMastraAgent } from "@mastra/core/agent";
import { RuntimeContext } from "@mastra/core/runtime-context";
import { MastraClient } from "@mastra/client-js";
import { MastraAgent } from "./mastra";

export function convertAGUIMessagesToMastra(messages: Message[]): CoreMessage[] {
Expand Down Expand Up @@ -66,53 +58,6 @@ export function convertAGUIMessagesToMastra(messages: Message[]): CoreMessage[]
return result;
}

export function registerCopilotKit<T extends Record<string, any> | unknown = unknown>({
path,
resourceId,
serviceAdapter = new ExperimentalEmptyAdapter(),
agents,
setContext,
}: {
path: string;
resourceId: string;
serviceAdapter?: CopilotServiceAdapter;
agents?: Record<string, AbstractAgent>;
setContext?: (c: any, runtimeContext: RuntimeContext<T>) => void | Promise<void>;
}) {
return registerApiRoute(path, {
method: `ALL`,
handler: async (c) => {
const mastra = c.get("mastra");

const runtimeContext = new RuntimeContext<T>();

if (setContext) {
await setContext(c, runtimeContext);
}

const aguiAgents =
agents ||
MastraAgent.getLocalAgents({
resourceId,
mastra,
runtimeContext,
});

const runtime = new CopilotRuntime({
agents: aguiAgents,
});

const handler = copilotRuntimeNodeHttpEndpoint({
endpoint: path,
runtime,
serviceAdapter,
});

return handler.handle(c.req.raw, {});
},
});
}

export interface GetRemoteAgentsOptions {
mastraClient: MastraClient;
resourceId?: string;
Expand Down
Loading