Skip to content

An AI-powered search engine with a generative UI

feat: add chat-cli tool for API testing

Yoshiki Miura227073fc13b3
diff --git a/bun.lock b/bun.lock
index 96095080..2bc4fdf2 100644
--- a/bun.lock
+++ b/bun.lock
@@ -42,7 +42,7 @@
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.0",
"cmdk": "1.0.0",
- "dotenv": "^16.5.0",
+ "dotenv": "^17.2.1",
"drizzle-kit": "^0.31.4",
"drizzle-orm": "^0.44.3",
"embla-carousel-react": "^8.0.0",
@@ -1024,7 +1024,7 @@
"domutils": ["[email protected]", "", { "dependencies": { "dom-serializer": "^2.0.0", "domelementtype": "^2.3.0", "domhandler": "^5.0.3" } }, "sha512-6kZKyUajlDuqlHKVX1w7gyslj9MPIXzIFiz/rGu35uC1wMi+kMhQwGhl4lt9unC9Vb9INnY9Z3/ZA3+FhASLaw=="],
- "dotenv": ["[email protected]", "", {}, "sha512-m/C+AwOAr9/W1UOIZUo232ejMNnJAJtYQjUbHoNTBNTJSvqzzDh7vnrei3o3r3m9blf6ZoDkvcw0VmozNRFJxg=="],
+ "dotenv": ["[email protected]", "", {}, "sha512-kQhDYKZecqnM0fCnzI5eIv5L4cAe/iRI+HqMbO/hbRdTAeXDG+M9FjipUxNfbARuEg4iHIbhnhs78BCHNbSxEQ=="],
"drizzle-kit": ["[email protected]", "", { "dependencies": { "@drizzle-team/brocli": "^0.10.2", "@esbuild-kit/esm-loader": "^2.5.5", "esbuild": "^0.25.4", "esbuild-register": "^3.5.0" }, "bin": { "drizzle-kit": "bin.cjs" } }, "sha512-tCPWVZWZqWVx2XUsVpJRnH9Mx0ClVOf5YUHerZ5so1OKSlqww4zy1R5ksEdGRcO3tM3zj0PYN6V48TbQCL1RfA=="],
diff --git a/package.json b/package.json
index 8cc7a608..a4a3350f 100644
--- a/package.json
+++ b/package.json
@@ -13,7 +13,8 @@
"format:check": "prettier --check .",
"migrate": "bun run lib/db/migrate.ts",
"test": "vitest",
- "test:watch": "vitest --watch"
+ "test:watch": "vitest --watch",
+ "chat": "bun run scripts/chat-cli.ts"
},
"dependencies": {
"@ai-sdk/anthropic": "^2.0.0-beta.8",
@@ -54,7 +55,7 @@
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.0",
"cmdk": "1.0.0",
- "dotenv": "^16.5.0",
+ "dotenv": "^17.2.1",
"drizzle-kit": "^0.31.4",
"drizzle-orm": "^0.44.3",
"embla-carousel-react": "^8.0.0",
diff --git a/scripts/README.md b/scripts/README.md
new file mode 100644
index 00000000..e2b67e7d
--- /dev/null
+++ b/scripts/README.md
@@ -0,0 +1,96 @@
+# Scripts
+
+This directory contains utility scripts for testing and development.
+
+## chat-cli.ts
+
+A command-line interface for testing the chat API without a browser client. This script allows you to interact with the chat API directly, making it easier to debug server-side issues and test API functionality.
+
+### Features
+
+- Send messages to the chat API via command line
+- Real-time Server-Sent Events (SSE) streaming output
+- Support for different AI models
+- Search mode enabled by default
+- Chat session continuity
+- Secure authentication via environment variables
+- URL validation for security (localhost only)
+
+### Setup
+
+1. **Add authentication to `.env.local`**:
+ ```env
+ MORPHIC_COOKIES="your-cookie-string-here"
+ ```
+
+### Usage
+
+```bash
+# Using npm script (recommended)
+bun chat -m "Hello, how are you?"
+
+# Direct usage
+bun scripts/chat-cli.ts -m "Hello, how are you?"
+
+# Disable search mode
+bun chat -m "Tell me a joke" --no-search
+
+# Use a specific model
+bun chat -m "Explain quantum computing" --model claude-3-5-sonnet
+
+# Continue an existing chat
+bun chat -c "chat_123" -m "Tell me more"
+
+# Show help
+bun chat --help
+```
+
+### Options
+
+- `-m, --message <text>` - Message to send (default: "Hello, how are you?")
+- `-u, --url <url>` - API URL (default: http://localhost:3000/api/chat, localhost only)
+- `-c, --chat-id <id>` - Chat ID for session continuity (default: auto-generated)
+- `-s, --search` - Enable search mode (default: true)
+- `--no-search` - Disable search mode
+- `--model <name>` - AI model to use: `gpt-4o`, `gpt-4o-mini`, `claude-3-5-sonnet`
+- `-h, --help` - Show help message
+
+### Output Format
+
+The script displays:
+
+- πŸš€ Request details
+- πŸ€– Selected model
+- πŸ” Search mode status
+- πŸ’¬ Chat ID for reference
+- Real-time AI responses with proper formatting
+- πŸ”§ Tool usage (when search mode is enabled)
+- βœ… Completion status
+
+### Security Features
+
+- **Authentication**: Uses environment variables only (no file-based auth)
+- **URL Validation**: Only allows localhost and local network URLs
+- **No Sensitive Logging**: Cookies are never displayed in logs
+- **Input Sanitization**: Message length limited to 10,000 characters
+
+### Requirements
+
+- Bun runtime
+- Local development server running (`bun dev`)
+- Valid authentication cookies in `.env.local`
+
+### Troubleshooting
+
+If you encounter "User not authenticated" errors:
+
+1. Ensure you're logged into Morphic in your browser
+2. Get fresh cookies from DevTools
+3. Update `MORPHIC_COOKIES` in `.env.local`
+4. Cookies expire after ~1 hour, so refresh them if needed
+
+For other issues:
+
+- Check the development server is running: `bun dev`
+- Verify `.env.local` exists and contains `MORPHIC_COOKIES`
+- Use `DEBUG=1` prefix for verbose output
diff --git a/scripts/chat-cli.ts b/scripts/chat-cli.ts
new file mode 100755
index 00000000..1eccb5a7
--- /dev/null
+++ b/scripts/chat-cli.ts
@@ -0,0 +1,374 @@
+#!/usr/bin/env tsx
+
+import { config as dotenvConfig } from 'dotenv'
+import { Readable } from 'stream'
+
+// Load environment variables from .env.local
+dotenvConfig({ path: '.env.local' })
+
+interface ChatApiConfig {
+ apiUrl: string
+ message: string
+ chatId?: string
+ selectedModel?: {
+ id: string
+ name: string
+ provider: string
+ providerId: string
+ enabled: boolean
+ toolCallType: string
+ }
+ searchMode?: boolean
+}
+
+interface UIMessage {
+ id: string
+ role: 'user' | 'assistant' | 'system'
+ content?: string
+ parts: Array<{
+ type: string
+ text?: string
+ [key: string]: any
+ }>
+ createdAt: Date
+}
+
+class ChatApiTester {
+ private config: ChatApiConfig
+
+ private validateUrl(url?: string): string | undefined {
+ if (!url) return undefined
+
+ try {
+ const parsed = new URL(url)
+ // Only allow localhost and local network
+ if (
+ parsed.hostname === 'localhost' ||
+ parsed.hostname === '127.0.0.1' ||
+ parsed.hostname.startsWith('192.168.') ||
+ parsed.hostname.startsWith('10.') ||
+ parsed.hostname.endsWith('.local')
+ ) {
+ return url
+ }
+ console.error('❌ Only local URLs are allowed for security')
+ process.exit(1)
+ } catch {
+ console.error('❌ Invalid URL format')
+ process.exit(1)
+ }
+ }
+
+ constructor(config: Partial<ChatApiConfig> = {}) {
+ this.config = {
+ apiUrl:
+ this.validateUrl(config.apiUrl) || 'http://localhost:3000/api/chat',
+ message: config.message || 'Hello, how are you?',
+ chatId: config.chatId || this.generateId(),
+ selectedModel: config.selectedModel || {
+ id: 'gpt-4o-mini',
+ name: 'GPT-4o mini',
+ provider: 'OpenAI',
+ providerId: 'openai',
+ enabled: true,
+ toolCallType: 'native'
+ },
+ searchMode: config.searchMode ?? true
+ }
+ }
+
+ private generateId(): string {
+ return `chat_${Date.now()}_${Math.random().toString(36).substring(2, 11)}`
+ }
+
+ private createUserMessage(text: string): UIMessage {
+ // Limit message length for safety
+ const sanitizedText = text.slice(0, 10000)
+
+ return {
+ id: this.generateId(),
+ role: 'user',
+ content: sanitizedText,
+ parts: [
+ {
+ type: 'text',
+ text: sanitizedText
+ }
+ ],
+ createdAt: new Date()
+ }
+ }
+
+ private loadCookiesFromEnv(): string | undefined {
+ if (process.env.MORPHIC_COOKIES) {
+ console.log('πŸͺ Using cookies from MORPHIC_COOKIES environment variable')
+ return process.env.MORPHIC_COOKIES
+ }
+ return undefined
+ }
+
+ async sendMessage(message?: string): Promise<void> {
+ const userMessage = this.createUserMessage(message || this.config.message)
+
+ // Load cookies from env
+ const cookies = this.loadCookiesFromEnv()
+
+ const payload = {
+ message: userMessage,
+ chatId: this.config.chatId,
+ trigger: 'submit-user-message'
+ }
+
+ console.log('πŸš€ Sending request to:', this.config.apiUrl)
+ console.log('πŸ“¦ Payload:', JSON.stringify(payload, null, 2))
+ console.log('πŸ€– Selected Model:', this.config.selectedModel?.name)
+ console.log('πŸ” Search Mode:', this.config.searchMode)
+ console.log('πŸ’¬ Chat ID:', this.config.chatId)
+ console.log('\n---\n')
+
+ const headers = {
+ 'Content-Type': 'application/json',
+ Cookie:
+ cookies ||
+ [
+ `selectedModel=${encodeURIComponent(JSON.stringify(this.config.selectedModel))}`,
+ `search-mode=${this.config.searchMode}`
+ ]
+ .filter(Boolean)
+ .join('; ')
+ }
+
+ // Only show headers in debug mode, without sensitive data
+ if (process.env.DEBUG) {
+ console.log('πŸ” Request headers:')
+ console.log(` Content-Type: ${headers['Content-Type']}`)
+ console.log(` Cookie: [REDACTED]`)
+ }
+
+ try {
+ const response = await fetch(this.config.apiUrl, {
+ method: 'POST',
+ headers,
+ body: JSON.stringify(payload)
+ })
+
+ if (!response.ok) {
+ const errorText = await response.text()
+ throw new Error(
+ `HTTP ${response.status}: ${response.statusText}\n${errorText}`
+ )
+ }
+
+ if (!response.body) {
+ throw new Error('No response body')
+ }
+
+ console.log('πŸ“‘ Response received, starting stream...\n')
+
+ // Convert Web Streams API to Node.js stream
+ const nodeReadable = Readable.fromWeb(response.body as any)
+
+ // Create parser stream
+
+ // Process SSE stream
+ const textDecoder = new TextDecoder()
+ let buffer = ''
+ let hasReceivedData = false
+
+ for await (const chunk of nodeReadable) {
+ buffer += textDecoder.decode(chunk, { stream: true })
+ const lines = buffer.split('\n')
+ buffer = lines.pop() || ''
+
+ for (const line of lines) {
+ if (line.trim() === '') continue
+
+ // Debug: log raw lines
+ if (process.env.DEBUG) {
+ console.log(`πŸ” Raw line: ${line}`)
+ }
+
+ if (line.startsWith('data: ')) {
+ hasReceivedData = true
+ const data = line.slice(6)
+ if (data === '[DONE]') {
+ console.log('\n\nβœ… Stream finished')
+ continue
+ }
+
+ try {
+ const parsed = JSON.parse(data)
+
+ if (parsed.type === 'text') {
+ process.stdout.write(parsed.text || '')
+ } else if (parsed.type === 'text-delta') {
+ process.stdout.write(parsed.delta || '')
+ } else if (parsed.type === 'reasoning') {
+ console.log(`\nπŸ€” Reasoning: ${parsed.text}`)
+ } else if (parsed.type?.startsWith('tool-')) {
+ console.log(`\nπŸ”§ Tool: ${parsed.type}`)
+ console.log(` State: ${parsed.state}`)
+ if (parsed.input) {
+ console.log(` Input: ${JSON.stringify(parsed.input)}`)
+ }
+ if (parsed.output) {
+ console.log(` Output: ${JSON.stringify(parsed.output)}`)
+ }
+ } else if (parsed.type === 'finish') {
+ console.log('\n\nβœ… Stream completed')
+ } else if (parsed.type === 'error') {
+ console.error(
+ `\n❌ Error: ${parsed.error || parsed.errorText || JSON.stringify(parsed)}`
+ )
+ } else if (process.env.DEBUG) {
+ // Only show raw event types in debug mode
+ console.log(`\nπŸ“¦ Event type: ${parsed.type || 'unknown'}`)
+ }
+ } catch (error) {
+ if (process.env.DEBUG) {
+ console.log(`πŸ“„ Parse error for data`)
+ }
+ }
+ } else if (line.startsWith('event: ')) {
+ const eventName = line.slice(7)
+ console.log(`πŸ“¨ Event: ${eventName}`)
+ }
+ }
+ }
+
+ if (!hasReceivedData) {
+ console.log('\n⚠️ No data received from stream')
+ }
+
+ console.log('\n\n✨ Request completed successfully')
+ } catch (error) {
+ console.error(
+ '\n❌ Error:',
+ error instanceof Error ? error.message : error
+ )
+ process.exit(1)
+ }
+ }
+}
+
+// Parse command line arguments
+function parseArgs(): Partial<ChatApiConfig> {
+ const args = process.argv.slice(2)
+ const config: Partial<ChatApiConfig> = {}
+
+ for (let i = 0; i < args.length; i++) {
+ switch (args[i]) {
+ case '-m':
+ case '--message':
+ config.message = args[++i]
+ break
+ case '-u':
+ case '--url':
+ config.apiUrl = args[++i]
+ break
+ case '-c':
+ case '--chat-id':
+ config.chatId = args[++i]
+ break
+ case '-s':
+ case '--search':
+ config.searchMode = true
+ break
+ case '--no-search':
+ config.searchMode = false
+ break
+ case '--model':
+ const modelId = args[++i]
+ // Map common model names
+ const modelMap: Record<string, any> = {
+ 'gpt-4o': {
+ id: 'gpt-4o',
+ name: 'GPT-4o',
+ provider: 'OpenAI',
+ providerId: 'openai',
+ enabled: true,
+ toolCallType: 'native'
+ },
+ 'gpt-4o-mini': {
+ id: 'gpt-4o-mini',
+ name: 'GPT-4o mini',
+ provider: 'OpenAI',
+ providerId: 'openai',
+ enabled: true,
+ toolCallType: 'native'
+ },
+ 'claude-3-5-sonnet': {
+ id: 'claude-3-5-sonnet-latest',
+ name: 'Claude 3.5 Sonnet',
+ provider: 'Anthropic',
+ providerId: 'anthropic',
+ enabled: true,
+ toolCallType: 'native'
+ }
+ }
+ config.selectedModel = modelMap[modelId] || modelMap['gpt-4o-mini']
+ break
+ case '-h':
+ case '--help':
+ console.log(`
+Chat API Test Script
+
+Usage: bun scripts/chat-cli.ts [options]
+
+Options:
+ -m, --message <text> Message to send (default: "Hello, how are you?")
+ -u, --url <url> API URL (default: http://localhost:3000/api/chat)
+ -c, --chat-id <id> Chat ID (default: auto-generated)
+ -s, --search Enable search mode (default: true)
+ --no-search Disable search mode
+ --model <name> Model to use: gpt-4o, gpt-4o-mini, claude-3-5-sonnet
+ -h, --help Show this help message
+
+Examples:
+ # Simple test
+ bun scripts/chat-cli.ts -m "What is the weather like?"
+
+ # Without search mode
+ bun scripts/chat-cli.ts -m "Tell me a joke" --no-search
+
+ # With specific model
+ bun scripts/chat-cli.ts -m "Tell me a joke" --model claude-3-5-sonnet
+
+ # Continue existing chat
+ bun scripts/chat-cli.ts -c "chat_123" -m "Tell me more"
+
+Note: Without authentication, you may get "User not authenticated" errors.
+
+Authentication:
+
+1. Add to .env.local: MORPHIC_COOKIES="your-cookie-string"
+2. The script will automatically load cookies when it runs
+
+To get cookies:
+1. Open DevTools > Network tab
+2. Make any request in Morphic
+3. Click on the request > Headers > Request Headers > Cookie
+4. Copy the entire Cookie value
+5. Add to .env.local as MORPHIC_COOKIES="copied-value"
+`)
+ process.exit(0)
+ }
+ }
+
+ return config
+}
+
+// Main execution
+async function main() {
+ const config = parseArgs()
+ const tester = new ChatApiTester(config)
+
+ await tester.sendMessage()
+}
+
+// Run the script
+main().catch(error => {
+ console.error('Fatal error:', error)
+ process.exit(1)
+})
+