-
Notifications
You must be signed in to change notification settings - Fork 42
feat: add optional you.com search integration #303
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
Open
mouse-value-add
wants to merge
2
commits into
yashdev9274:main
Choose a base branch
from
mouse-value-add:feat/youcom-search-integration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
62 changes: 62 additions & 0 deletions
62
apps/supercode-cli/server/src/agents/tools/youcom_search.ts
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,62 @@ | ||
| import { z } from "zod" | ||
| import { youcomFetch } from "../../lib/youcom" | ||
| import { serialize, ok, fail } from "../../cli/ai/tool-result" | ||
| import { defineTool } from "../lib/define.ts" | ||
|
|
||
| const youcomSearchSchema = z.object({ | ||
| query: z.string().min(1).describe("Search query"), | ||
| maxResults: z.number().int().min(1).max(20).optional().default(5).describe("Maximum number of search results to return (1-20)"), | ||
| }) | ||
|
|
||
| export type YoucomSearchArgs = z.infer<typeof youcomSearchSchema> | ||
|
|
||
| function mapYoucomResults(data: any, maxResults: number) { | ||
| const results = data?.results | ||
| const webResults = Array.isArray(results?.web) ? results.web : [] | ||
| const newsResults = Array.isArray(results?.news) ? results.news : [] | ||
| const flat = [...webResults, ...newsResults] | ||
| return flat.slice(0, maxResults).map((item: any) => ({ | ||
| title: String(item.title ?? ""), | ||
| snippet: String(item.description ?? item.snippet ?? ""), | ||
| link: String(item.url ?? ""), | ||
| publishedDate: item.published_date ?? item.publishedDate ?? null, | ||
| })) | ||
| } | ||
|
|
||
| const _def = { | ||
| description: | ||
| "Search the web using the You.com Search API. " + | ||
| "Returns relevant results with titles, snippets, and URLs. " + | ||
| "Best for finding current information, news, documentation, and any topic the user asks about. " + | ||
| "Returns a structured result: { success: true, data: { query, results: [...], provider } } with title/snippet/link, " + | ||
| "or { success: false, error } when search is unavailable. " + | ||
| "If success is false, do NOT invent search results — relay the error to the user.", | ||
| inputSchema: youcomSearchSchema, | ||
| execute: async (input: YoucomSearchArgs, ctx?: { signal?: AbortSignal }) => { | ||
| const { query, maxResults } = youcomSearchSchema.parse(input) | ||
| return serialize(async () => { | ||
| const resp = await youcomFetch({ | ||
| apiPath: "/v1/search", | ||
| body: { query, count: maxResults }, | ||
| timeout: 30000, | ||
| signal: ctx?.signal, | ||
| }) | ||
|
|
||
| if (resp.ok) { | ||
| return ok({ | ||
| query, | ||
| provider: "youcom", | ||
| results: mapYoucomResults(resp.data, maxResults), | ||
| }) | ||
| } | ||
|
|
||
| return fail( | ||
| `Web search failed via You.com: ${resp.error}`, | ||
| resp.hint ?? "Set a valid YDC_API_KEY, or use exa_search / firecrawl_search instead.", | ||
| ) | ||
| }) | ||
| }, | ||
| } | ||
|
|
||
| export const youcomSearchTool = defineTool(_def) | ||
| export default youcomSearchTool | ||
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,2 @@ | ||
| /** Re-export shim — implementation in `src/runtime/research/youcom.ts`. */ | ||
| export * from "../runtime/research/youcom.ts" |
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
111 changes: 111 additions & 0 deletions
111
apps/supercode-cli/server/src/runtime/research/youcom.ts
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,111 @@ | ||
| import { loadEnvOnce } from "../config/load-env" | ||
|
|
||
| const YOUCOM_BASE = "https://api.ydc-index.io" | ||
|
|
||
| export interface YoucomOptions { | ||
| apiPath: string | ||
| body: Record<string, unknown> | ||
| timeout?: number | ||
| /** Optional caller-provided abort signal (e.g. the tool execution signal); combined with the timeout. */ | ||
| signal?: AbortSignal | ||
| } | ||
|
|
||
| export interface YoucomResult { | ||
| ok: boolean | ||
| data?: any | ||
| error?: string | ||
| hint?: string | ||
| status?: number | ||
| } | ||
|
|
||
| function statusHint(status: number): string | undefined { | ||
| if (status === 429) return "Rate limited. Try again later." | ||
| if (status === 401 || status === 403) return "Invalid YDC_API_KEY. Check your You.com API key." | ||
| return undefined | ||
| } | ||
|
|
||
| /** Combine an optional external signal with a timeout, following the AbortController pattern used across the repo. */ | ||
| function withTimeoutSignal(timeout: number, signal?: AbortSignal): { signal: AbortSignal; cleanup: () => void } { | ||
| const controller = new AbortController() | ||
| const timer = setTimeout(() => controller.abort(), timeout) | ||
| const onAbort = () => controller.abort() | ||
| if (signal) { | ||
| if (signal.aborted) controller.abort() | ||
| else signal.addEventListener("abort", onAbort) | ||
| } | ||
| return { | ||
| signal: controller.signal, | ||
| cleanup: () => { | ||
| clearTimeout(timer) | ||
| if (signal) signal.removeEventListener("abort", onAbort) | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| export function youcomFetch({ | ||
| apiPath, | ||
| body, | ||
| timeout = 30000, | ||
| signal, | ||
| }: YoucomOptions): Promise<YoucomResult> { | ||
| loadEnvOnce() | ||
| const apiKey = process.env.YDC_API_KEY | ||
|
|
||
| if (!apiKey) { | ||
| return Promise.resolve({ | ||
| ok: false, | ||
| error: "YDC_API_KEY is not set", | ||
| hint: "Set YDC_API_KEY to use youcom_search, or use exa_search / firecrawl_search instead.", | ||
| }) | ||
| } | ||
|
|
||
| return callYoucomDirect(apiKey, apiPath, body, timeout, signal) | ||
| } | ||
|
|
||
| async function callYoucomDirect( | ||
| apiKey: string, | ||
| apiPath: string, | ||
| body: Record<string, unknown>, | ||
| timeout: number, | ||
| signal?: AbortSignal, | ||
| ): Promise<YoucomResult> { | ||
| const timeoutSignal = withTimeoutSignal(timeout, signal) | ||
| try { | ||
| const res = await fetch(`${YOUCOM_BASE}${apiPath}`, { | ||
| method: "POST", | ||
| headers: { | ||
| "X-API-Key": apiKey, | ||
| "Content-Type": "application/json", | ||
| }, | ||
| body: JSON.stringify(body), | ||
| signal: timeoutSignal.signal, | ||
| }) | ||
|
|
||
| const data = await res.json().catch(() => ({})) | ||
|
|
||
| if (!res.ok) { | ||
| return { | ||
| ok: false, | ||
| error: `You.com returned HTTP ${res.status}`, | ||
| hint: statusHint(res.status), | ||
| status: res.status, | ||
| } | ||
| } | ||
|
|
||
| const results = data?.results | ||
| const hasWebOrNews = Array.isArray(results?.web) || Array.isArray(results?.news) | ||
| if (!data || typeof data !== "object" || (apiPath === "/v1/search" && !hasWebOrNews)) { | ||
| return { ok: false, error: "You.com returned a malformed response" } | ||
| } | ||
| return { ok: true, data } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } catch (err: any) { | ||
| const isTimeout = err?.name === "TimeoutError" || err?.name === "AbortError" | ||
| return { | ||
| ok: false, | ||
| error: isTimeout ? "Request timed out" : (err.message || String(err)), | ||
| hint: isTimeout ? "You.com API may be slow or unreachable. Try exa_search, firecrawl_search, or url_fetch instead." : undefined, | ||
| } | ||
| } finally { | ||
| timeoutSignal.cleanup() | ||
| } | ||
| } | ||
3 changes: 3 additions & 0 deletions
3
apps/supercode-cli/server/src/tools/definitions/youcom-search.ts
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,3 @@ | ||
| /** @deprecated use src/agents/tools/youcom_search.ts */ | ||
| export { youcomSearchTool } from "src/agents/tools/youcom_search.ts" | ||
| export { default } from "src/agents/tools/youcom_search.ts" |
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.