Skip to content

feat: replace groq with cf-ai #133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
95 changes: 7 additions & 88 deletions app/routes/api.chat.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getModel } from "~/chat/ai/providers.server";
import type { modelID } from "~/chat/ai/providers.shared";
import { streamText, type ToolSet, type UIMessage } from "ai";
import { createWorkersAI } from "workers-ai-provider";

import type { StorageKey } from "~/chat/ai/providers.shared";
import { MCPClientManager } from "agents/mcp/client";
Expand Down Expand Up @@ -41,73 +41,11 @@ export async function action({
apiKeys: Record<StorageKey, string>;
} = await request.json();

const env = context.cloudflare.env as CloudflareEnvironment;
const model = getModel(env, apiKeys);

// // Initialize tools
// let tools = {};
// const mcpClients: any[] = [];

// // Process each MCP server configuration
// for (const mcpServer of mcpServers) {
// try {
// // Create appropriate transport based on type
// let transport:
// | MCPTransport
// | { type: "sse"; url: string; headers?: Record<string, string> };

// if (mcpServer.type === "sse") {
// // Convert headers array to object for SSE transport
// const headers: Record<string, string> = {};
// if (mcpServer.headers && mcpServer.headers.length > 0) {
// mcpServer.headers.forEach((header) => {
// if (header.key) headers[header.key] = header.value || "";
// });
// }

// transport = {
// type: "sse" as const,
// url: mcpServer.url,
// headers: Object.keys(headers).length > 0 ? headers : undefined,
// };
// } else {
// console.warn(
// `Skipping MCP server with unsupported transport type: ${mcpServer.type}`,
// );
// continue;
// }

// const mcpClient = await createMCPClient({ transport });
// mcpClients.push(mcpClient);

// const mcptools = await mcpClient.tools();
// console.log("mcptools", mcptools);

// console.log(
// `MCP tools from ${mcpServer.type} transport:`,
// Object.keys(mcptools),
// );

// // Add MCP tools to tools object
// tools = { ...tools, ...mcptools };
// } catch (error) {
// console.error("Failed to initialize MCP client:", error);
// // Continue with other servers instead of failing the entire request
// }
// }

// // Register cleanup for all clients
// if (mcpClients.length > 0) {
// request.signal.addEventListener("abort", async () => {
// for (const client of mcpClients) {
// try {
// await client.close();
// } catch (error) {
// console.error("Error closing MCP client:", error);
// }
// }
// });
// }
const env = context.cloudflare.env as CloudflareEnvironment & { AI: any };

const workersai = createWorkersAI({ binding: env.AI });

const model = workersai("@cf/deepseek-ai/deepseek-r1-distill-qwen-32b", {});

let tools: ToolSet = {};
const mcp = new MCPClientManager("my-agent", "1.0.0");
Expand All @@ -127,15 +65,9 @@ export async function action({
}
}

// console.log("messages", messages);
// console.log(
// "parts",
// messages.map((m) => m.parts.map((p) => p)),
// );

// If there was an error setting up MCP clients but we at least have composio tools, continue
const result = streamText({
model: model.languageModel(selectedModel),
model: model,
system: `You are a helpful assistant with access to a variety of tools.

Today's date is ${new Date().toISOString().split("T")[0]}.
Expand All @@ -161,19 +93,6 @@ export async function action({
messages,
tools,
maxSteps: 20,
providerOptions: {
google: {
thinkingConfig: {
thinkingBudget: 2048,
},
},
anthropic: {
thinking: {
type: "enabled",
budgetTokens: 12000,
},
},
},
onError: (error) => {
console.error(JSON.stringify(error, null, 2));
},
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"@types/react": "^19.1.2",
"@types/react-dom": "^19.1.2",
"agents": "^0.0.66",
"ai": "^4.3.9",
"ai": "^4.3.10",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
"dotenv": "^16.5.0",
Expand All @@ -81,11 +81,12 @@
"tailwind-merge": "^3.2.0",
"tailwindcss-animate": "^1.0.7",
"typewriter-effect": "^2.21.0",
"workers-ai-provider": "^0.3.0",
"zod": "^3.24.3"
},
"devDependencies": {
"@cloudflare/vite-plugin": "^0.1.21",
"@cloudflare/workers-types": "^4.20250418.0",
"@cloudflare/vite-plugin": "^1.0.12",
"@cloudflare/workers-types": "^4.20250425.0",
"@react-router/dev": "^7.5.1",
"@tailwindcss/postcss": "^4.1.4",
"@tailwindcss/vite": "^4.1.4",
Expand All @@ -102,6 +103,6 @@
"vite-tsconfig-paths": "^5.1.4",
"vitest": "^1.6.1",
"workers-mcp": "0.1.0-3",
"wrangler": "^4.12.0"
"wrangler": "^4.13.2"
}
}
Loading