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
14 changes: 14 additions & 0 deletions .changeset/calm-poems-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
"@cloudflare/codemode": patch
"@cloudflare/ai-chat": patch
"agents": patch
---

feat: split ai-chat and codemode into separate packages

Extract @cloudflare/ai-chat and @cloudflare/codemode into their own packages
with comprehensive READMEs. Update agents README to remove chat-specific
content and point to new packages. Fix documentation imports to reflect
new package structure.

Maintains backward compatibility, no breaking changes.
2 changes: 1 addition & 1 deletion docs/client-tools-continuation.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The `autoContinueAfterToolResult` option lets client tools behave like server to
## How It Works

```typescript
import { useAgentChat } from "agents/ai-react";
import { useAgentChat } from "@cloudflare/ai-chat/react";

const { messages, addToolResult } = useAgentChat({
agent,
Expand Down
2 changes: 1 addition & 1 deletion docs/codemode.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const result = streamText({
### After (With Codemode)

```typescript
import { experimental_codemode as codemode } from "agents/codemode/ai";
import { experimental_codemode as codemode } from "@cloudflare/codemode/ai";

// Define your tools as usual
const tools = {
Expand Down
14 changes: 12 additions & 2 deletions docs/context-management.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
## How It Works

```typescript
import { AIChatAgent, getCurrentAgent } from "agents";
import { AIChatAgent } from "@cloudflare/ai-chat";
import { getCurrentAgent } from "agents";

export class MyAgent extends AIChatAgent {
async customMethod() {
Expand All @@ -33,7 +34,8 @@ export class MyAgent extends AIChatAgent {
## Real-World Example

```typescript
import { AIChatAgent, getCurrentAgent } from "agents";
import { AIChatAgent } from "@cloudflare/ai-chat";
import { getCurrentAgent } from "agents";
import { generateText } from "ai";
import { openai } from "@ai-sdk/openai";

Expand Down Expand Up @@ -82,6 +84,10 @@ agent.customMethod()
### Working with AI SDK Tools

```typescript
import { AIChatAgent } from "@cloudflare/ai-chat";
import { generateText } from "ai";
import { openai } from "@ai-sdk/openai";

export class MyAgent extends AIChatAgent {
async generateResponse(prompt: string) {
// AI SDK tools automatically work
Expand All @@ -101,6 +107,9 @@ export class MyAgent extends AIChatAgent {
### Calling External Libraries

```typescript
import { AIChatAgent } from "@cloudflare/ai-chat";
import { getCurrentAgent } from "agents";

async function saveToDatabase(data: any) {
const { agent } = getCurrentAgent<MyAgent>();
// Can access agent info for logging, context, etc.
Expand Down Expand Up @@ -136,6 +145,7 @@ Gets the current agent from any context where it's available.
**Usage:**

```typescript
import { AIChatAgent } from "@cloudflare/ai-chat";
import { getCurrentAgent } from "agents";

export class MyAgent extends AIChatAgent {
Expand Down
30 changes: 19 additions & 11 deletions docs/migration-to-ai-sdk-v5.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ If you're extending `AIChatAgent`, the message handling has been updated:
#### Before:

```typescript
import { AIChatAgent } from "@cloudflare/ai-chat";

class MyAgent extends AIChatAgent<Env> {
async onChatMessage(
onFinish: StreamTextOnFinishCallback<ToolSet>,
Expand All @@ -80,6 +82,8 @@ class MyAgent extends AIChatAgent<Env> {
#### After:

```typescript
import { AIChatAgent } from "@cloudflare/ai-chat";

class MyAgent extends AIChatAgent<Env> {
async onChatMessage(
onFinish: StreamTextOnFinishCallback<ToolSet>,
Expand Down Expand Up @@ -181,7 +185,10 @@ for await (const chunk of result.fullStream) {
**Optional manual utilities:**

```typescript
import { autoTransformMessages, analyzeCorruption } from "agents";
import {
autoTransformMessages,
analyzeCorruption
} from "@cloudflare/ai-chat/ai-chat-v5-migration";

// Manual transformation (usually not needed)
const cleanMessages = autoTransformMessages(anyFormatMessages);
Expand All @@ -205,7 +212,7 @@ The `useAgentChat` hook can now automatically detect which tools require human-i
To enable this, pass your `tools` object to the `useAgentChat` hook:

```typescript
import { useAgentChat } from "agents/ai-react";
import { useAgentChat } from "@cloudflare/ai-chat/react";
import { tools } from "./tools";

// ...
Expand Down Expand Up @@ -319,15 +326,14 @@ You can optionally use the migration utilities to convert stored messages:

```typescript
import {
needsMigration,
migrateMessagesToUIFormat,
analyzeCorruption
} from "agents";
} from "@cloudflare/ai-chat/ai-chat-v5-migration";

// In your agent class
if (needsMigration(this.messages)) {
// Optional: analyze what types of corruption exist
const stats = analyzeCorruption(this.messages);
// Check if messages need migration (autoTransformMessages handles this automatically)
const stats = analyzeCorruption(this.messages);
if (stats.legacyString || stats.corruptArray) {
console.log(
`Migrating ${stats.legacyString} legacy and ${stats.corruptArray} corrupt messages`
);
Expand All @@ -341,18 +347,20 @@ if (needsMigration(this.messages)) {
## Migration Utilities Reference

```typescript
// Type guards
isUIMessage(message); // Check if already in v5 format
needsMigration(messages); // Check if any messages need migration
import {
migrateMessagesToUIFormat,
analyzeCorruption
} from "@cloudflare/ai-chat/ai-chat-v5-migration";

// Migration functions
migrateToUIMessage(message); // Migrate single message
migrateMessagesToUIFormat(messages); // Migrate array of messages

// Analysis tools
analyzeCorruption(messages); // Get stats about message format issues
```

**Note:** The `autoTransformMessages` function is used internally by `AIChatAgent` and automatically handles migration. You typically don't need to call migration utilities manually.

Migration handles:

- `{role, content: string}` → `{role, parts: [{type: "text", text}]}`
Expand Down
4 changes: 2 additions & 2 deletions docs/resumable-streaming.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ It just works!
### Server

```typescript
import { AIChatAgent } from "agents/ai-chat-agent";
import { AIChatAgent } from "@cloudflare/ai-chat";
import { streamText } from "ai";
import { openai } from "@ai-sdk/openai";

Expand All @@ -38,7 +38,7 @@ export class ChatAgent extends AIChatAgent<Env> {

```tsx
import { useAgent } from "agents/react";
import { useAgentChat } from "agents/ai-react";
import { useAgentChat } from "@cloudflare/ai-chat/react";

function Chat() {
const agent = useAgent({
Expand Down
2 changes: 1 addition & 1 deletion examples/playground/src/agents/chat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AIChatAgent } from "agents/ai-chat-agent";
import { AIChatAgent } from "@cloudflare/ai-chat";
import {
convertToModelMessages,
streamText,
Expand Down
2 changes: 1 addition & 1 deletion examples/playground/src/components/Chat.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "./Chat.css";
import { useAgentChat } from "agents/ai-react";
import { useAgentChat } from "@cloudflare/ai-chat/react";
import { useAgent } from "agents/react";
import { useCallback, useEffect, useRef, useState } from "react";

Expand Down
2 changes: 1 addition & 1 deletion examples/resumable-stream-chat/src/client.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type React from "react";
import { Suspense, useState, useRef, useEffect } from "react";
import { useAgent } from "agents/react";
import { useAgentChat } from "agents/ai-react";
import { useAgentChat } from "@cloudflare/ai-chat/react";
import type { UIMessage } from "ai";

/**
Expand Down
2 changes: 1 addition & 1 deletion examples/resumable-stream-chat/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { openai } from "@ai-sdk/openai";
import { routeAgentRequest } from "agents";
import { AIChatAgent } from "agents/ai-chat-agent";
import { AIChatAgent } from "@cloudflare/ai-chat";
import {
streamText,
convertToModelMessages,
Expand Down
2 changes: 1 addition & 1 deletion guides/human-in-the-loop/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getToolName, isToolUIPart } from "ai";
import { clientTools } from "./tools";
import { APPROVAL, toolsRequiringConfirmation } from "./utils";
import "./styles.css";
import { useAgentChat, type AITool } from "agents/ai-react";
import { useAgentChat, type AITool } from "@cloudflare/ai-chat/react";
import { useAgent } from "agents/react";
import { useCallback, useEffect, useRef, useState } from "react";

Expand Down
2 changes: 1 addition & 1 deletion guides/human-in-the-loop/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { openai } from "@ai-sdk/openai";
import { routeAgentRequest } from "agents";
import { AIChatAgent } from "agents/ai-chat-agent";
import { AIChatAgent } from "@cloudflare/ai-chat";
import {
convertToModelMessages,
type StreamTextOnFinishCallback,
Expand Down
61 changes: 54 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
"author": "Sunil Pai <spai@cloudflare.com>",
"private": true,
"scripts": {
"build": "npm run build -w agents -w hono-agents",
"build": "npm run build -w agents -w hono-agents -w @cloudflare/ai-chat -w @cloudflare/codemode",
"format": "prettier --write .",
"typecheck": "npx tsx scripts/typecheck.ts",
"check:exports": "npx tsx scripts/check-exports.ts",
"check:updates": "npx tsx scripts/check-updates.ts",
"check": "sherif && npm run check:exports && prettier --check . && biome lint && npm run typecheck",
"test": "npm run test -w agents",
"test": "npm run test -w agents -w @cloudflare/ai-chat",
"test:react": "npm run test:react -w agents",
"postinstall": "patch-package && playwright install --with-deps chromium",
"prepare": "husky"
Expand Down
Loading
Loading