UNPKG

@graphteon/juricode

Version:

We are forging the future with lines of digital steel

196 lines 8.32 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.runTaskChat = void 0; const p = __importStar(require("@clack/prompts")); const chalk_1 = __importDefault(require("chalk")); const ws_client_1 = require("../api/ws-client"); const format_message_1 = require("../utils/format-message"); const index_1 = require("../index"); const open_hands_1 = __importDefault(require("../api/open-hands")); const getEditorUrl = async (conversationId) => { try { const response = await open_hands_1.default.getVSCodeUrl(conversationId); if (response.error) { throw new Error(response.error); } if (!response.vscode_url) { throw new Error('No VSCode URL available'); } const vscodeUrl = new URL(response.vscode_url); const vscodePort = parseInt(vscodeUrl.port); try { await (0, index_1.setupVSCodeTunnel)(vscodePort); } catch (error) { console.log(chalk_1.default.yellow('Note: VSCode tunnel setup failed. You may need to run:')); console.log(chalk_1.default.cyan(`ssh -L ${vscodePort}:127.0.0.1:${vscodePort} \${USER}@\${HOST}`)); } return response.vscode_url; } catch (error) { throw new Error(`Failed to get editor URL: ${error instanceof Error ? error.message : 'Unknown error'}`); } }; const formatCodeBlock = (code, language) => { const lines = code.trim().split('\n'); const maxLength = Math.max(...lines.map(line => line.length)); const border = '─'.repeat(maxLength + 2); const langHeader = language ? `${chalk_1.default.yellow(`[${language}]`)}\n` : ''; return '\n' + chalk_1.default.dim('┌' + border + '┐') + '\n' + langHeader + lines.map(line => chalk_1.default.dim('│') + ' ' + chalk_1.default.cyan(line) + ' '.repeat(maxLength - line.length) + chalk_1.default.dim(' │')).join('\n') + '\n' + chalk_1.default.dim('└' + border + '┘') + '\n'; }; const formatContent = (text) => { text = text.replace(/```(\w+)?\n([\s\S]*?)```/g, (_, lang, code) => formatCodeBlock(code, lang)); text = text.replace(/`([^`]+)`/g, (_, code) => chalk_1.default.cyan(code)); text = text.replace(/\*\*([^*]+)\*\*/g, (_, content) => chalk_1.default.bold(content)); text = text.replace(/\*([^*]+)\*/g, (_, content) => chalk_1.default.italic(content)); text = text.replace(/^- (.+)$/gm, (_, content) => '• ' + content); text = text.replace(/^(\d+)\. (.+)$/gm, (_, num, content) => `${num}. ${content}`); text = text.replace(/^> (.+)$/gm, (_, content) => chalk_1.default.dim('│ ') + chalk_1.default.italic(content)); return text; }; const runTaskChat = async (taskId) => { p.intro('Starting chat session'); const s = p.spinner(); let isSpinning = true; s.start('Connecting to chat'); const ws = new ws_client_1.WSClient(); let isAgentReady = false; let isConnected = false; try { ws.onConnect(() => { isConnected = true; s.stop('🚀 Connected to chat'); isSpinning = false; if (!isAgentReady) { s.start('Agent is processing'); isSpinning = true; } }); ws.onMessage(async (event) => { if (isSpinning) { s.stop('⛵🚀🚀🚀🚀'); isSpinning = false; } const formatted = (0, format_message_1.formatMessage)(event); const timestamp = new Date(event.timestamp).toLocaleTimeString(); if (event.source === 'user') { p.note(formatContent(formatted.text), `${chalk_1.default.blue('🧔 You')} ${chalk_1.default.dim(timestamp)}`); } else if (event.source === 'agent') { p.note(formatContent(formatted.text), `${chalk_1.default.green('🤖 Assistant')} ${chalk_1.default.dim(timestamp)}`); } else { p.note(formatContent(formatted.text), `${chalk_1.default.yellow('💻 System')} ${chalk_1.default.dim(timestamp)}`); } }); ws.onError((error) => { if (isSpinning) { s.stop(); isSpinning = false; } p.cancel(`Chat error: ${error.message}`); }); ws.onStateChange((state) => { if (state === 'awaiting_user_input' || state === 'finished') { isAgentReady = true; if (isSpinning) { s.stop(state === 'finished' ? '✅ Agent finished' : '⛵ Ready for input'); isSpinning = false; } } else { isAgentReady = false; if (isConnected && !isSpinning) { s.start('Agent is processing'); isSpinning = true; } } }); await ws.connect(taskId); while (true) { while (!isAgentReady) { await new Promise(resolve => setTimeout(resolve, 100)); } try { const message = await p.text({ message: chalk_1.default.blue('You:'), validate: input => input.length === 0 ? 'Message cannot be empty' : undefined, placeholder: 'Type /editor to get editor link' }); if (p.isCancel(message) || !message || message.toLowerCase() === 'exit') { break; } if (message === '/editor') { try { const url = await getEditorUrl(taskId); console.log('\n' + chalk_1.default.green('✨ Editor URL: ') + chalk_1.default.blue.underline(url) + '\n'); continue; } catch (error) { p.log.error('Failed to get editor URL'); continue; } } ws.send(message); isAgentReady = false; s.start('Agent is processing'); isSpinning = true; } catch (error) { p.log.error('Failed to send message'); p.log.error(error instanceof Error ? error.message : 'Unknown error'); } } ws.disconnect(); p.outro('Chat ended'); } catch (error) { if (isSpinning) { s.stop('Failed to start chat'); isSpinning = false; } p.log.error('Failed to start chat'); p.log.error(error instanceof Error ? error.message : 'Unknown error'); } }; exports.runTaskChat = runTaskChat; //# sourceMappingURL=chat.js.map