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
20 changes: 16 additions & 4 deletions src/lib/agents/media/video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,31 @@ const searchVideos = async (
schema: schema,
});

const searchRes = await searchSearxng(res.query, {
engines: ['youtube'],
});
let searchRes;
try {
searchRes = await searchSearxng(res.query, {
engines: ['youtube'],
});
} catch (error) {
console.error(`Video search failed for query "${res.query}":`, error);
return [];
}

const videos: VideoSearchResult[] = [];

searchRes.results.forEach((result) => {
if (result.thumbnail && result.url && result.title && result.iframe_src) {
// Normalize embed URL - some SearXNG instances return
// youtube-nocookie.com which doesn't resolve on all networks
const embedUrl = result.iframe_src.replace(
'www.youtube-nocookie.com/embed',
'www.youtube.com/embed',
);
videos.push({
img_src: result.thumbnail,
url: result.url,
title: result.title,
iframe_src: result.iframe_src,
iframe_src: embedUrl,
});
}
});
Expand Down
14 changes: 11 additions & 3 deletions src/lib/agents/search/researcher/actions/academicSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,17 @@ const academicSearchAction: ResearchAction<typeof schema> = {
let results: Chunk[] = [];

const search = async (q: string) => {
const res = await searchSearxng(q, {
engines: ['arxiv', 'google scholar', 'pubmed'],
});
let res;
try {
res = await searchSearxng(q, {
engines: ['arxiv', 'google scholar', 'pubmed'],
});
} catch (error) {
console.error(`Academic search failed for query "${q}":`, error);
return;
}

if (!res.results || res.results.length === 0) return;

const resultChunks: Chunk[] = res.results.map((r) => ({
content: r.content || r.title,
Expand Down
1 change: 1 addition & 0 deletions src/lib/agents/search/researcher/actions/scrapeURL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ const scrapeURLAction: ResearchAction<typeof schema> = {
},
});
} catch (error) {
console.error(`Failed to scrape URL ${url}:`, error);
results.push({
content: `Failed to fetch content from ${url}: ${error}`,
metadata: {
Expand Down
14 changes: 11 additions & 3 deletions src/lib/agents/search/researcher/actions/socialSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,17 @@ const socialSearchAction: ResearchAction<typeof schema> = {
let results: Chunk[] = [];

const search = async (q: string) => {
const res = await searchSearxng(q, {
engines: ['reddit'],
});
let res;
try {
res = await searchSearxng(q, {
engines: ['reddit'],
});
} catch (error) {
console.error(`Social search failed for query "${q}":`, error);
return;
}

if (!res.results || res.results.length === 0) return;

const resultChunks: Chunk[] = res.results.map((r) => ({
content: r.content || r.title,
Expand Down
10 changes: 9 additions & 1 deletion src/lib/agents/search/researcher/actions/webSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,15 @@ const webSearchAction: ResearchAction<typeof actionSchema> = {
let results: Chunk[] = [];

const search = async (q: string) => {
const res = await searchSearxng(q);
let res;
try {
res = await searchSearxng(q);
} catch (error) {
console.error(`SearXNG search failed for query "${q}":`, error);
return;
}

if (!res.results || res.results.length === 0) return;

const resultChunks: Chunk[] = res.results.map((r) => ({
content: r.content || r.title,
Expand Down
20 changes: 12 additions & 8 deletions src/lib/models/providers/ollama/ollamaLLM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ class OllamaLLM extends BaseLLM<OllamaConfig> {
temperature:
input.options?.temperature ?? this.config.options?.temperature ?? 0.7,
num_predict: input.options?.maxTokens ?? this.config.options?.maxTokens,
num_ctx: 32000,
num_ctx:
input.options?.contextWindowSize ??
this.config.options?.contextWindowSize,
frequency_penalty:
input.options?.frequencyPenalty ??
this.config.options?.frequencyPenalty,
Expand All @@ -105,15 +107,15 @@ class OllamaLLM extends BaseLLM<OllamaConfig> {
});

return {
content: res.message.content,
content: res.message?.content ?? '',
toolCalls:
res.message.tool_calls?.map((tc) => ({
res.message?.tool_calls?.map((tc) => ({
id: crypto.randomUUID(),
name: tc.function.name,
arguments: tc.function.arguments,
})) || [],
additionalInfo: {
reasoning: res.message.thinking,
reasoning: res.message?.thinking,
},
};
}
Expand Down Expand Up @@ -146,7 +148,9 @@ class OllamaLLM extends BaseLLM<OllamaConfig> {
top_p: input.options?.topP ?? this.config.options?.topP,
temperature:
input.options?.temperature ?? this.config.options?.temperature ?? 0.7,
num_ctx: 32000,
num_ctx:
input.options?.contextWindowSize ??
this.config.options?.contextWindowSize,
num_predict: input.options?.maxTokens ?? this.config.options?.maxTokens,
frequency_penalty:
input.options?.frequencyPenalty ??
Expand All @@ -161,9 +165,9 @@ class OllamaLLM extends BaseLLM<OllamaConfig> {

for await (const chunk of stream) {
yield {
contentChunk: chunk.message.content,
contentChunk: chunk.message?.content ?? '',
toolCallChunk:
chunk.message.tool_calls?.map((tc, i) => ({
chunk.message?.tool_calls?.map((tc, i) => ({
id: crypto
.createHash('sha256')
.update(
Expand All @@ -175,7 +179,7 @@ class OllamaLLM extends BaseLLM<OllamaConfig> {
})) || [],
done: chunk.done,
additionalInfo: {
reasoning: chunk.message.thinking,
reasoning: chunk.message?.thinking,
},
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/models/providers/openai/openaiLLM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ class OpenAILLM extends BaseLLM<OpenAIConfig> {

if (response.choices && response.choices.length > 0) {
return {
content: response.choices[0].message.content!,
content: response.choices[0].message?.content ?? '',
toolCalls:
response.choices[0].message.tool_calls
response.choices[0].message?.tool_calls
?.map((tc) => {
if (tc.type === 'function') {
return {
Expand Down
1 change: 1 addition & 0 deletions src/lib/models/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type GenerateOptions = {
stopSequences?: string[];
frequencyPenalty?: number;
presencePenalty?: number;
contextWindowSize?: number;
};

type Tool = {
Expand Down
18 changes: 15 additions & 3 deletions src/lib/searxng.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,22 @@ export const searchSearxng = async (
}

const res = await fetch(url);
const data = await res.json();

const results: SearxngSearchResult[] = data.results;
const suggestions: string[] = data.suggestions;
if (!res.ok) {
throw new Error(
`SearXNG returned status ${res.status} for query: ${query}`,
);
}

let data;
try {
data = await res.json();
} catch {
throw new Error(`SearXNG returned non-JSON response for query: ${query}`);
}

const results: SearxngSearchResult[] = data.results ?? [];
const suggestions: string[] = data.suggestions ?? [];

return { results, suggestions };
};