// GPT.ts import { ChatCompletionMessageParam } from 'openai/resources'; import { openai } from './setup'; import { DASH_DOCUMENTATION } from './dashDocumentation'; /** * Re-export enums/constants for compatibility with UI code. */ export enum GPTCallType { COMMANDTYPE = 'command_type', } export enum GPTDocCommand { AssignTags = 1, Filter = 2, GetInfo = 3, Sort = 4, } export const DescriptionSeperator = '======'; export const DocSeperator = '------'; /** * Split documentation into major sections based on "TABLE OF CONTENTS" headings. */ const chunkDocumentation = (docText: string): { title: string; content: string }[] => { const chunks: { title: string; content: string }[] = []; const sectionRegex = /TABLE OF CONTENTS\s*([^\n]+)\n([\s\S]*?)(?=(?:TABLE OF CONTENTS\s*[^\n]+\n)|$)/gi; let match; while ((match = sectionRegex.exec(docText))) { const title = match[1].trim(); const content = match[2].trim(); chunks.push({ title, content }); } return chunks; }; /** * Calculate relevance score using improved term matching and context awareness. */ const calculateRelevance = (chunk: { title: string; content: string }, query: string): number => { const queryTerms = query .toLowerCase() .split(/\W+/) .filter(term => term.length > 2); const content = chunk.content.toLowerCase(); const title = chunk.title.toLowerCase(); let score = 0; // Exact phrase match if (content.includes(query.toLowerCase())) { score += 20; } // Title matches for (const term of queryTerms) { if (title.includes(term)) { score += 10; } } // Content matches for (const term of queryTerms) { const wordMatches = (content.match(new RegExp(`\\b${term}\\b`, 'g')) || []).length; score += wordMatches * 2; } // Boost for multiple terms found const uniqueFound = queryTerms.filter(t => content.includes(t) || title.includes(t)).length; if (uniqueFound > 1) { score += uniqueFound * 5; } return score; }; /** * Fetch the most relevant documentation chunks for a query, boosting exact section-name matches. */ const getRelevantChunks = (query: string, maxChunks: number = 3): string => { const chunks = chunkDocumentation(DASH_DOCUMENTATION); const lowerQuery = query.toLowerCase(); // Score and boost const scored = chunks .map(chunk => { let score = calculateRelevance(chunk, query); if (lowerQuery.includes(chunk.title.toLowerCase())) { score += 50; // strong boost for exact section name } return { ...chunk, score }; }) .filter(c => c.score > 0) .sort((a, b) => b.score - a.score) .slice(0, maxChunks); if (!scored.length) return 'No relevant documentation found.'; return scored.map(c => `## ${c.title}\n\n${c.content}`).join('\n\n---\n\n'); }; /** * Determine if a query is related to Dash documentation. */ const isDocumentationQuery = (query: string): boolean => { const dashTerms = [ 'dash', 'dashboard', 'document', 'view', 'freeform', 'schema', 'stacking', 'notetaking', 'link', 'pin', 'presentation', 'tab', 'tile', 'search', 'filter', 'shortcut', 'keyboard', 'collaboration', 'share', 'annotation', 'comment', 'image', 'text', 'pdf', 'web', ]; const lowerQuery = query.toLowerCase(); return dashTerms.some(term => lowerQuery.includes(term)); }; /** * Generate a response using GPT based on the query and relevant documentation. */ export const gptTutorialAPICall = async (userQuery: string): Promise => { if (!isDocumentationQuery(userQuery)) { return "Sorry, I'm unable to help with that question based on the current documentation."; } const relevantDocs = getRelevantChunks(userQuery); const systemPrompt = `You are an expert assistant for Dash. Using ONLY the following docs, answer clearly:\n\n${relevantDocs}`; const messages: ChatCompletionMessageParam[] = [ { role: 'system', content: systemPrompt }, { role: 'user', content: userQuery }, ]; try { const response = await openai.chat.completions.create({ model: 'gpt-4-turbo', messages, temperature: 0.3, max_tokens: 512, }); return response.choices[0].message.content ?? 'No response.'; } catch (err) { console.error(err); return 'Error connecting to the API.'; } }; /** * Concise descriptions of Dash components highlighting their purpose and utility */ export const DASH_COMPONENT_DESCRIPTIONS: { [key: string]: string } = { Dashboard: 'A customizable workspace that can contain multiple tabs and views, allowing you to organize different workflows like photo albums or class notes in separate spaces.', Tab: 'A browser-like interface element that displays documents, enabling you to switch between different content while keeping your workspace organized.', Tile: 'A container that can hold multiple tabs, similar to browser windows, allowing you to view and work with multiple documents side by side.', 'Freeform View': "An unbounded 2D canvas that serves as Dash's primary workspace, perfect for spatial organization and visualizing relationships between documents.", 'Schema View': 'A spreadsheet-like interface that displays documents as rows with customizable columns, ideal for structured data viewing and manipulation.', 'Stacking View': 'A Trello-like board view that organizes documents into scrollable stacks, perfect for categorizing and managing documents by specific attributes.', 'Notetaking View': 'A multi-column layout that lets you take notes alongside your main content, ideal for research and study sessions.', Link: 'A bidirectional connection between documents that helps you establish relationships and navigate between related content.', Trail: 'A presentation tool that creates a predefined path through selected documents, transforming your workspace into a smooth, professional presentation.', Collection: 'A group of related documents that can be organized and manipulated together, helping you maintain context and relationships between content.', Mask: 'A tool that creates a focused view by hiding everything except selected content, perfect for presentations and emphasizing specific information.', Ink: 'A drawing tool that lets you create shapes, lines, and annotations directly on your documents, useful for visual communication and markup.', 'Marquee Selection': 'A rectangular selection tool that lets you highlight, annotate, or link specific portions of documents like images, PDFs, or webpages.', Animation: 'A feature that creates smooth transitions between different states of your documents, allowing you to build dynamic presentations and visualizations.', 'AI Assistant': 'An intelligent tool that helps analyze documents, search the web, and provide insights, making it easier to work with complex information.', Document: 'Any content item in Dash, such as text, images, PDFs, webpages, or data visualizations, that can be organized, linked, and manipulated within your workspace.', Filter: 'A tool that lets you narrow down your view to show only documents matching specific criteria, helping you focus on relevant content.', Toolbar: "A context-sensitive control panel that provides quick access to document-specific actions and tools based on what you're currently working with.", 'Menu Panel': 'A set of icons on the left side of the interface that provide access to core Dash features like search, files, tools, and sharing.', Topbar: 'The topmost section of the interface containing global controls for navigation, sharing, and accessing documentation and settings.', Annotation: 'A note or comment that can be attached to specific parts of documents, helping you add context and explanations to your content.', Tag: 'A label that can be added to documents to categorize and organize them, making it easier to find related content.', Search: 'A powerful tool that helps you find documents by searching through their content and metadata, with options to filter results by type.', Share: 'A feature that lets you collaborate by giving others access to your dashboards and documents with customizable permission levels.', Lightbox: 'A focused view mode that displays a document in isolation, perfect for detailed examination or presentation.', DataViz: 'A visualization tool that transforms your data into interactive charts, graphs, and tables, making it easier to understand and analyze information.', 'Smart Draw': 'An AI-powered drawing tool that helps you create and enhance visual content using natural language prompts.', 'Image Editor': 'A tool for modifying images with AI assistance, allowing you to erase and regenerate parts of images based on text prompts.', Guest: 'A view-only access mode that lets others view your shared content without making changes, perfect for presentations and demonstrations.', Permission: 'A set of access rights (Admin, Edit, Augment, View) that control what others can do with your shared documents and dashboards.', };