-
Notifications
You must be signed in to change notification settings - Fork 1
move custom search dialog to its own component separate from default … #222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
24d6f86
move custom search dialog to its own component separate from default …
e3bacb1
update fumadoc-ui version -- move AI search toggle to next to search bar
1c3eb56
Merge branch 'main' of https://github.com/agentuity/docs into seng/de…
a2baa6d
add some border to toggle button
c5fde42
allow manual trigger
01f17d5
Update .github/workflows/sync-docs.yml
afterrburn 65b3e93
Update .github/workflows/sync-docs.yml
afterrburn 62cd84d
Fix code block styling
mcongrove 4b132e5
UI tweaks
mcongrove File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
import { source } from '@/lib/source'; | ||
import { NextRequest } from 'next/server'; | ||
import { getAgentConfig } from '@/lib/env'; | ||
|
||
function documentPathToUrl(docPath: string): string { | ||
// Remove the .md or .mdx extension before any # symbol | ||
const path = docPath.replace(/\.mdx?(?=#|$)/, ''); | ||
|
||
// Split path and hash (if any) | ||
const [basePath, hash] = path.split('#'); | ||
|
||
// Split the base path into segments | ||
const segments = basePath.split('/').filter(Boolean); | ||
|
||
// If the last segment is 'index', remove it | ||
if (segments.length > 0 && segments[segments.length - 1].toLowerCase() === 'index') { | ||
segments.pop(); | ||
} | ||
|
||
// Reconstruct the path | ||
let url = '/' + segments.join('/'); | ||
if (url === '/') { | ||
url = '/'; | ||
} | ||
if (hash) { | ||
url += '#' + hash; | ||
} | ||
return url; | ||
} | ||
|
||
// Helper function to get document title and description from source | ||
function getDocumentMetadata(docPath: string): { title: string; description?: string } { | ||
try { | ||
const urlPath = documentPathToUrl(docPath).substring(1).split('/'); | ||
const page = source.getPage(urlPath); | ||
|
||
if (page?.data) { | ||
return { | ||
title: page.data.title || formatPathAsTitle(docPath), | ||
description: page.data.description | ||
}; | ||
} | ||
} catch (error) { | ||
console.warn(`Failed to get metadata for ${docPath}:`, error); | ||
} | ||
|
||
return { title: formatPathAsTitle(docPath) }; | ||
} | ||
|
||
function formatPathAsTitle(docPath: string): string { | ||
return docPath | ||
.replace(/\.mdx?$/, '') | ||
.split('/') | ||
.map(segment => segment.charAt(0).toUpperCase() + segment.slice(1)) | ||
.join(' > '); | ||
} | ||
|
||
function getDocumentSnippet(docPath: string, maxLength: number = 150): string { | ||
try { | ||
const urlPath = documentPathToUrl(docPath).substring(1).split('/'); | ||
const page = source.getPage(urlPath); | ||
|
||
if (page?.data.description) { | ||
return page.data.description.length > maxLength | ||
? page.data.description.substring(0, maxLength) + '...' | ||
: page.data.description; | ||
} | ||
|
||
// Fallback description based on path | ||
const pathParts = docPath.replace(/\.mdx?$/, '').split('/'); | ||
const section = pathParts[0]; | ||
const topic = pathParts[pathParts.length - 1]; | ||
|
||
return `Learn about ${topic} in the ${section} section of our documentation.`; | ||
} catch { | ||
return `Documentation for ${formatPathAsTitle(docPath)}`; | ||
} | ||
} | ||
|
||
export async function GET(request: NextRequest) { | ||
const { searchParams } = new URL(request.url); | ||
const query = searchParams.get('query'); | ||
|
||
// If no query, return empty results | ||
if (!query || query.trim().length === 0) { | ||
return Response.json([]); | ||
} | ||
|
||
try { | ||
const agentConfig = getAgentConfig(); | ||
|
||
afterrburn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Prepare headers | ||
const headers: Record<string, string> = { | ||
'Content-Type': 'application/json', | ||
}; | ||
|
||
// Add bearer token if provided | ||
if (agentConfig.bearerToken) { | ||
headers['Authorization'] = `Bearer ${agentConfig.bearerToken}`; | ||
} | ||
|
||
const response = await fetch(agentConfig.url, { | ||
method: 'POST', | ||
headers, | ||
body: JSON.stringify({ message: query }), | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Agent API error: ${response.status} ${response.statusText}`); | ||
} | ||
|
||
const data = await response.json(); | ||
const results = []; | ||
|
||
if (data?.answer?.trim()) { | ||
results.push({ | ||
id: `ai-answer-${Date.now()}`, | ||
url: '#ai-answer', | ||
title: 'AI Answer', | ||
content: data.answer.trim(), | ||
type: 'ai-answer' | ||
}); | ||
} | ||
|
||
// Add related documents as clickable results | ||
if (data.documents && Array.isArray(data.documents) && data.documents.length > 0) { | ||
const uniqueDocuments = [...new Set(data.documents as string[])]; | ||
|
||
uniqueDocuments.forEach((docPath: string, index: number) => { | ||
try { | ||
const url = documentPathToUrl(docPath); | ||
const metadata = getDocumentMetadata(docPath); | ||
const snippet = getDocumentSnippet(docPath); | ||
|
||
results.push({ | ||
id: `doc-${Date.now()}-${index}`, | ||
url: url, | ||
title: metadata.title, | ||
content: snippet, | ||
type: 'document' | ||
}); | ||
} catch (error) { | ||
console.warn(`Failed to process document ${docPath}:`, error); | ||
} | ||
}); | ||
} | ||
|
||
console.log('Returning RAG results:', results.length, 'items'); | ||
return Response.json(results); | ||
|
||
} catch (error) { | ||
console.error('Error calling AI agent:', error); | ||
|
||
// Return error message as AI answer | ||
return Response.json([ | ||
{ | ||
id: 'error-notice', | ||
url: '#error', | ||
title: '❌ Search Error', | ||
content: 'AI search is temporarily unavailable. Please try again later or use the regular search.', | ||
type: 'ai-answer' | ||
} | ||
]); | ||
afterrburn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.