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
151 changes: 79 additions & 72 deletions src/lib/agents/search/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,95 +7,102 @@ import { WidgetExecutor } from './widgets';

class APISearchAgent {
async searchAsync(session: SessionManager, input: SearchAgentInput) {
const classification = await classify({
chatHistory: input.chatHistory,
enabledSources: input.config.sources,
query: input.followUp,
llm: input.config.llm,
});

const widgetPromise = WidgetExecutor.executeAll({
classification,
chatHistory: input.chatHistory,
followUp: input.followUp,
llm: input.config.llm,
}).catch((err) => {
console.error(`Error executing widgets: ${err}`);
return [];
});

let searchPromise: Promise<ResearcherOutput> | null = null;
try {
const classification = await classify({
chatHistory: input.chatHistory,
enabledSources: input.config.sources,
query: input.followUp,
llm: input.config.llm,
});

if (!classification.classification.skipSearch) {
const researcher = new Researcher();
searchPromise = researcher.research(SessionManager.createSession(), {
const widgetPromise = WidgetExecutor.executeAll({
classification,
chatHistory: input.chatHistory,
followUp: input.followUp,
classification: classification,
config: input.config,
llm: input.config.llm,
}).catch((err) => {
console.error(`Error executing widgets: ${err}`);
return [];
});
}

const [widgetOutputs, searchResults] = await Promise.all([
widgetPromise,
searchPromise,
]);
let searchPromise: Promise<ResearcherOutput> | null = null;

if (!classification.classification.skipSearch) {
const researcher = new Researcher();
searchPromise = researcher.research(SessionManager.createSession(), {
chatHistory: input.chatHistory,
followUp: input.followUp,
classification: classification,
config: input.config,
});
}

const [widgetOutputs, searchResults] = await Promise.all([
widgetPromise,
searchPromise,
]);

if (searchResults) {
session.emit('data', {
type: 'searchResults',
data: searchResults.searchFindings,
});
}

if (searchResults) {
session.emit('data', {
type: 'searchResults',
data: searchResults.searchFindings,
type: 'researchComplete',
});
}

session.emit('data', {
type: 'researchComplete',
});
const finalContext =
searchResults?.searchFindings
.map(
(f, index) =>
`<result index=${index + 1} title=${f.metadata.title}>${f.content}</result>`,
)
.join('\n') || '';

const finalContext =
searchResults?.searchFindings
.map(
(f, index) =>
`<result index=${index + 1} title=${f.metadata.title}>${f.content}</result>`,
)
.join('\n') || '';
const widgetContext = widgetOutputs
.map((o) => {
return `<result>${o.llmContext}</result>`;
})
.join('\n-------------\n');

const widgetContext = widgetOutputs
.map((o) => {
return `<result>${o.llmContext}</result>`;
})
.join('\n-------------\n');
const finalContextWithWidgets = `<search_results note="These are the search results and assistant can cite these">\n${finalContext}\n</search_results>\n<widgets_result noteForAssistant="Its output is already showed to the user, assistant can use this information to answer the query but do not CITE this as a souce">\n${widgetContext}\n</widgets_result>`;

const finalContextWithWidgets = `<search_results note="These are the search results and assistant can cite these">\n${finalContext}\n</search_results>\n<widgets_result noteForAssistant="Its output is already showed to the user, assistant can use this information to answer the query but do not CITE this as a souce">\n${widgetContext}\n</widgets_result>`;
const writerPrompt = getWriterPrompt(
finalContextWithWidgets,
input.config.systemInstructions,
input.config.mode,
);

const writerPrompt = getWriterPrompt(
finalContextWithWidgets,
input.config.systemInstructions,
input.config.mode,
);
const answerStream = input.config.llm.streamText({
messages: [
{
role: 'system',
content: writerPrompt,
},
...input.chatHistory,
{
role: 'user',
content: input.followUp,
},
],
});

const answerStream = input.config.llm.streamText({
messages: [
{
role: 'system',
content: writerPrompt,
},
...input.chatHistory,
{
role: 'user',
content: input.followUp,
},
],
});
for await (const chunk of answerStream) {
session.emit('data', {
type: 'response',
data: chunk.contentChunk,
});
}

for await (const chunk of answerStream) {
session.emit('data', {
type: 'response',
data: chunk.contentChunk,
session.emit('end', {});
} catch (error) {
console.error('Error while running API search:', error);
session.emit('error', {
data: error instanceof Error ? error.message : 'Search failed',
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Mar 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Raw exception messages are emitted to client-facing session error events, exposing internal error details.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/lib/agents/search/api.ts, line 103:

<comment>Raw exception messages are emitted to client-facing session error events, exposing internal error details.</comment>

<file context>
@@ -7,95 +7,102 @@ import { WidgetExecutor } from './widgets';
+    } catch (error) {
+      console.error('Error while running API search:', error);
+      session.emit('error', {
+        data: error instanceof Error ? error.message : 'Search failed',
       });
     }
</file context>
Suggested change
data: error instanceof Error ? error.message : 'Search failed',
data: 'Search failed',
Fix with Cubic

});
}

session.emit('end', {});
}
}

Expand Down
Loading