Skip to content
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ screenshots/
# npm lock file (using bun.lock)
package-lock.json

# Build info
*.tsbuildinfo
next-env.d.ts

# Examples framework artifacts
examples/*/node_modules/
examples/plugins/*/node_modules/
Expand Down
10 changes: 5 additions & 5 deletions bun.lock

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

5 changes: 5 additions & 0 deletions examples/agent-chat-demo/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Get your API key at https://platform.openai.com/api-keys
OPENAI_API_KEY=sk-...

# Optional: override the model (default: gpt-4o)
# OPENAI_MODEL=gpt-4o
62 changes: 62 additions & 0 deletions examples/agent-chat-demo/app/api/chat/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Chat API route — thin proxy to OpenAI.
*
* Does NOT touch the document. Tool definitions are passed to OpenAI,
* but tool execution happens on the client via the EditorBridge.
*
* Flow:
* 1. Client sends { messages, tools } to this route
* 2. Route calls OpenAI with the tools
* 3. If OpenAI returns tool_calls, route returns them to the client
* 4. Client executes tools via EditorBridge, sends results back
* 5. Repeat until OpenAI returns text
*/

import { NextRequest, NextResponse } from 'next/server';
import OpenAI from 'openai';
import type {
ChatCompletionMessageParam,
ChatCompletionTool,
} from 'openai/resources/chat/completions';

function getClient() {
return new OpenAI();
}
const model = process.env.OPENAI_MODEL || 'gpt-4o';

export async function POST(request: NextRequest) {
try {
const body = await request.json();
const { messages, tools } = body as {
messages: ChatCompletionMessageParam[];
tools: ChatCompletionTool[];
};

if (!messages || messages.length === 0) {
return NextResponse.json({ error: 'No messages provided' }, { status: 400 });
}

const openai = getClient();
const response = await openai.chat.completions.create({
model,
messages,
tools: tools && tools.length > 0 ? tools : undefined,
});

const choice = response.choices[0];
if (!choice) {
return NextResponse.json({ error: 'Empty response from AI' }, { status: 502 });
}

return NextResponse.json({
message: choice.message,
finishReason: choice.finish_reason,
});
} catch (err) {
console.error('Chat API error:', err);
return NextResponse.json(
{ error: err instanceof Error ? err.message : 'Internal error' },
{ status: 500 }
);
}
}
3 changes: 3 additions & 0 deletions examples/agent-chat-demo/app/globals.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* Import editor styles (CSS variables, toolbar layout, etc.)
In standalone usage: @import '@eigenpal/docx-js-editor/styles.css'; */
@import '../../../packages/react/src/styles/editor.css';
17 changes: 17 additions & 0 deletions examples/agent-chat-demo/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { Metadata } from 'next';
import './globals.css';

export const metadata: Metadata = {
title: 'Chat with your Doc',
description: 'Upload a DOCX and chat with AI — it can add comments and suggest changes live',
};

export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body style={{ margin: 0, fontFamily: 'system-ui, -apple-system, sans-serif' }}>
{children}
</body>
</html>
);
}
Loading