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
8 changes: 8 additions & 0 deletions doc-agents/src/agent/agent_pulse/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getTutorialMeta } from './tutorial';
import docQAAgent from '@agent/doc_qa';
import type { Action } from './types';
import { ActionType } from './types';
import { documentPathToUrl } from '@utils/url';

interface ToolState {
action: Action | null;
Expand Down Expand Up @@ -60,6 +61,13 @@ export async function createTools(state: ToolState, agentContext: any) {
const response = await docQAAgent.run({
message: query,
});
// Transform document URLs from raw paths to proper URLs
if (response.documents && Array.isArray(response.documents)) {
response.documents = response.documents.map((doc) => ({
...doc,
url: documentPathToUrl(doc.url),
}));
}
return response;
} catch (error) {
agentContext.logger.error('Error calling doc-qa agent: %s', error);
Expand Down
33 changes: 1 addition & 32 deletions doc-agents/src/api/doc-qa/route.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,10 @@
import { createRouter } from '@agentuity/runtime';
import docQAAgent from '@agent/doc_qa';
import { bearerTokenAuth } from '@middleware/auth';
import { documentPathToUrl } from '@utils/url';

const router = createRouter();

/**
* Transforms a document path (e.g., "Agents/index.mdx") to a proper URL (e.g., "/Agents")
*/
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;
}

// POST /api/doc-qa - Answer questions about documentation (bearer token protected, no user context)
router.post('/', bearerTokenAuth, docQAAgent.validator(), async (c) => {
const data = c.req.valid('json');
Expand Down
33 changes: 33 additions & 0 deletions doc-agents/src/utils/url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Transforms a document path (e.g., "Get-Started/quickstart.mdx#7-deploy") to a proper URL (e.g., "/Get-Started/quickstart#7-deploy")
*/
export function documentPathToUrl(docPath?: string | null): string {
if (!docPath) {
return '/';
}
// 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 with leading slash
let url = `/${segments.join('/')}`;
if (url === '/') {
url = '/';
}
if (hash) {
url += `#${hash}`;
}
return url;
}
3 changes: 2 additions & 1 deletion doc-agents/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"paths": {
"@agent/*": ["./src/agent/*"],
"@api/*": ["./src/api/*"],
"@middleware/*": ["./src/middleware/*"]
"@middleware/*": ["./src/middleware/*"],
"@utils/*": ["./src/utils/*"]
}
},
"include": ["src/**/*", "app.ts"]
Expand Down