feat: add response cache and compact output mode

Response cache (api-client.ts):
- In-memory TTL cache (5 min default) avoids duplicate API calls
  when LLMs repeat the same tool call within a conversation.
- Cache key = full URL with params; only 200 OK responses are cached.
- Exposed via client.cache for manual clear() if needed.
- Configurable TTL via TorrentClawClient constructor.

Compact mode (search_content tool):
- New compact boolean parameter (default: false).
- When true, magnet links use short form magnet:?xt=urn:btih:{hash}
  (~60 chars) instead of full magnets with trackers (~300 chars).
- Short magnets are still valid and clickable (DHT peer discovery).
- Also generates a magnet from info_hash even when API returns null.
- Saves ~10K chars per typical search (5 torrents x 10 results).

Tests: 100 total (15 new), all passing.
This commit is contained in:
Deivid Soto 2026-02-09 17:57:11 +01:00
parent e011c0f63e
commit bf459740fe
5 changed files with 349 additions and 9 deletions

View file

@ -94,6 +94,12 @@ export function registerSearchContent(
.describe(
"ISO 3166-1 country code for streaming availability (e.g. US, ES, GB, DE). If provided, results include which streaming services offer each title. If omitted, no streaming data is returned.",
),
compact: z
.boolean()
.default(false)
.describe(
"When true, returns shorter magnet links (hash only, no trackers) to reduce output size. Magnets are still clickable. Recommended for large result sets or when context window is limited.",
),
},
async (params) => {
try {
@ -111,7 +117,14 @@ export function registerSearchContent(
limit: params.limit ?? 10,
country: params.country,
});
return { content: [{ type: "text", text: formatSearchResults(data) }] };
return {
content: [
{
type: "text",
text: formatSearchResults(data, { compact: params.compact }),
},
],
};
} catch (error) {
const message =
error instanceof ApiError