Skip to content

Commit 329226e

Browse files
authored
Implement Basic Agent (#14)
1 parent 7332156 commit 329226e

File tree

15 files changed

+3430
-72
lines changed

15 files changed

+3430
-72
lines changed

apps/react/demo/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
},
1111
"dependencies": {
1212
"@ag-ui/client": "0.0.40-alpha.3",
13+
"@copilotkitnext/basic-agent": "workspace:*",
1314
"@copilotkitnext/core": "workspace:*",
14-
"@copilotkitnext/demo-agents": "workspace:*",
1515
"@copilotkitnext/react": "workspace:*",
1616
"@copilotkitnext/runtime": "workspace:*",
1717
"@copilotkitnext/shared": "workspace:*",

apps/react/demo/src/app/api/copilotkit/[[...slug]]/openai.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

apps/react/demo/src/app/api/copilotkit/[[...slug]]/route.ts

Lines changed: 21 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,71 +4,31 @@ import {
44
InMemoryAgentRunner,
55
} from "@copilotkitnext/runtime";
66
import { handle } from "hono/vercel";
7-
import { OpenAIAgent } from "./openai";
8-
import {
9-
AbstractAgent,
10-
BaseEvent,
11-
EventType,
12-
RunAgentInput,
13-
} from "@ag-ui/client";
14-
import { Observable } from "rxjs";
15-
16-
class MissingOpenAIKeyAgent extends AbstractAgent {
17-
clone(): MissingOpenAIKeyAgent {
18-
return new MissingOpenAIKeyAgent();
19-
}
20-
21-
protected run(input: RunAgentInput): Observable<BaseEvent> {
22-
return new Observable<BaseEvent>((observer) => {
23-
const { runId, threadId } = input;
24-
const messageId = runId ?? `missing-key-${Date.now()}`;
25-
26-
observer.next({
27-
type: EventType.RUN_STARTED,
28-
runId,
29-
threadId,
30-
} as BaseEvent);
31-
32-
observer.next({
33-
type: EventType.TEXT_MESSAGE_START,
34-
messageId,
35-
role: "assistant",
36-
} as BaseEvent);
37-
38-
observer.next({
39-
type: EventType.TEXT_MESSAGE_CONTENT,
40-
messageId,
41-
delta:
42-
"OPENAI_API_KEY is not set. Provide a key to enable the OpenAI demo agent.",
43-
} as BaseEvent);
44-
45-
observer.next({
46-
type: EventType.TEXT_MESSAGE_END,
47-
messageId,
48-
} as BaseEvent);
49-
50-
observer.next({
51-
type: EventType.RUN_FINISHED,
52-
runId,
53-
threadId,
54-
} as BaseEvent);
55-
56-
observer.complete();
57-
58-
return () => {};
59-
});
7+
import { BasicAgent } from "@copilotkitnext/basic-agent";
8+
9+
// Determine which model to use based on available API keys
10+
const getModelConfig = () => {
11+
if (process.env.OPENAI_API_KEY?.trim()) {
12+
return "openai/gpt-4o";
13+
} else if (process.env.ANTHROPIC_API_KEY?.trim()) {
14+
return "anthropic/claude-sonnet-4.5";
15+
} else if (process.env.GOOGLE_API_KEY?.trim()) {
16+
return "google/gemini-2.5-pro";
6017
}
61-
}
62-
63-
const hasOpenAIKey = Boolean(process.env.OPENAI_API_KEY?.trim());
64-
65-
const selectedAgent = hasOpenAIKey
66-
? new OpenAIAgent()
67-
: new MissingOpenAIKeyAgent();
18+
throw new Error(
19+
"No API key found. Please set OPENAI_API_KEY, ANTHROPIC_API_KEY, or GOOGLE_API_KEY environment variable."
20+
);
21+
};
22+
23+
const agent = new BasicAgent({
24+
model: getModelConfig(),
25+
prompt: "You are a helpful AI assistant.",
26+
temperature: 0.7,
27+
});
6828

6929
const runtime = new CopilotRuntime({
7030
agents: {
71-
default: selectedAgent as unknown as AbstractAgent,
31+
default: agent,
7232
},
7333
runner: new InMemoryAgentRunner(),
7434
});

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"name": "CopilotKitNext",
33
"private": true,
44
"scripts": {
5-
"predev": "turbo run build --filter='@copilotkitnext/core' --filter='@copilotkitnext/shared' --filter='@copilotkitnext/react' --filter='@copilotkitnext/angular' --filter='@copilotkitnext/demo-agents'",
5+
"predev": "turbo run build --filter='@copilotkitnext/core' --filter='@copilotkitnext/shared' --filter='@copilotkitnext/react' --filter='@copilotkitnext/angular' --filter='@copilotkitnext/basic-agent'",
66
"build": "turbo run build",
77
"clean": "turbo run clean",
8-
"dev": "turbo run dev --filter='@copilotkitnext/core' --filter='@copilotkitnext/shared' --filter='@copilotkitnext/runtime' --filter='@copilotkitnext/react' --filter='@copilotkitnext/angular' --filter='@copilotkitnext/demo-agents' --concurrency=15",
9-
"dev:packages": "turbo run dev --filter='@copilotkitnext/core' --filter='@copilotkitnext/shared' --filter='@copilotkitnext/runtime' --filter='@copilotkitnext/react' --filter='@copilotkitnext/angular' --filter='@copilotkitnext/demo-agents'",
8+
"dev": "turbo run dev --filter='@copilotkitnext/core' --filter='@copilotkitnext/shared' --filter='@copilotkitnext/runtime' --filter='@copilotkitnext/react' --filter='@copilotkitnext/angular' --filter='@copilotkitnext/basic-agent' --concurrency=15",
9+
"dev:packages": "turbo run dev --filter='@copilotkitnext/core' --filter='@copilotkitnext/shared' --filter='@copilotkitnext/runtime' --filter='@copilotkitnext/react' --filter='@copilotkitnext/angular' --filter='@copilotkitnext/basic-agent'",
1010
"demo:angular": "turbo run dev --filter='@copilotkitnext/angular-demo-server' --filter='@copilotkitnext/angular-demo'",
1111
"storybook:angular": "pnpm -C apps/angular/storybook dev",
1212
"demo:react": "pnpm -C apps/react/demo dev",

packages/basic-agent/package.json

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"name": "@copilotkitnext/basic-agent",
3+
"version": "0.0.12",
4+
"description": "Basic agent package for CopilotKit2",
5+
"main": "dist/index.js",
6+
"types": "dist/index.d.ts",
7+
"exports": {
8+
".": {
9+
"types": "./dist/index.d.ts",
10+
"import": "./dist/index.mjs",
11+
"require": "./dist/index.js"
12+
}
13+
},
14+
"publishConfig": {
15+
"access": "public"
16+
},
17+
"scripts": {
18+
"build": "tsup",
19+
"prepublishOnly": "pnpm run build",
20+
"dev": "tsup --watch",
21+
"lint": "eslint . --max-warnings 0",
22+
"check-types": "tsc --noEmit",
23+
"clean": "rm -rf dist",
24+
"test": "vitest run",
25+
"test:watch": "vitest",
26+
"test:coverage": "vitest run --coverage"
27+
},
28+
"devDependencies": {
29+
"@copilotkitnext/eslint-config": "workspace:*",
30+
"@copilotkitnext/typescript-config": "workspace:*",
31+
"@types/node": "^22.15.3",
32+
"@vitest/coverage-v8": "^3.2.4",
33+
"eslint": "^9.30.0",
34+
"tsup": "^8.5.0",
35+
"typescript": "5.8.2",
36+
"vitest": "^3.0.5"
37+
},
38+
"dependencies": {
39+
"@ag-ui/client": "0.0.40-alpha.3",
40+
"@ai-sdk/anthropic": "^2.0.22",
41+
"@ai-sdk/google": "^2.0.17",
42+
"@ai-sdk/openai": "^2.0.42",
43+
"@modelcontextprotocol/sdk": "^1.18.2",
44+
"ai": "^5.0.59",
45+
"rxjs": "^7.8.1",
46+
"zod": "^3.25.75"
47+
},
48+
"engines": {
49+
"node": ">=18"
50+
}
51+
}

0 commit comments

Comments
 (0)