|
|
@ -61,13 +61,23 @@ export class FetchService { |
|
|
|
|
|
|
|
|
private extractJsonFromWebFetchResult({ |
|
|
private extractJsonFromWebFetchResult({ |
|
|
response, |
|
|
response, |
|
|
|
|
|
sources, |
|
|
text |
|
|
text |
|
|
}: { |
|
|
}: { |
|
|
response: { body?: unknown }; |
|
|
response: { body?: unknown }; |
|
|
|
|
|
sources: { providerMetadata?: Record<string, Record<string, unknown>> }[]; |
|
|
text: string; |
|
|
text: string; |
|
|
}): string | undefined { |
|
|
}): string | undefined { |
|
|
const candidates: string[] = []; |
|
|
const candidates: string[] = []; |
|
|
|
|
|
|
|
|
|
|
|
for (const source of sources ?? []) { |
|
|
|
|
|
const content = source?.providerMetadata?.openrouter?.content; |
|
|
|
|
|
|
|
|
|
|
|
if (typeof content === 'string' && content) { |
|
|
|
|
|
candidates.push(content); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
const body = response?.body as |
|
|
const body = response?.body as |
|
|
| { |
|
|
| { |
|
|
choices?: { |
|
|
choices?: { |
|
|
@ -114,7 +124,7 @@ export class FetchService { |
|
|
try { |
|
|
try { |
|
|
const openRouterService = createOpenRouter({ apiKey: openRouterApiKey }); |
|
|
const openRouterService = createOpenRouter({ apiKey: openRouterApiKey }); |
|
|
|
|
|
|
|
|
const { response, text } = await generateText({ |
|
|
const { response, sources, text } = await generateText({ |
|
|
model: openRouterService.chat(openRouterModel), |
|
|
model: openRouterService.chat(openRouterModel), |
|
|
prompt: [ |
|
|
prompt: [ |
|
|
'Fetch the following URL and return its response body exactly as received.', |
|
|
'Fetch the following URL and return its response body exactly as received.', |
|
|
@ -123,7 +133,7 @@ export class FetchService { |
|
|
].join('\n'), |
|
|
].join('\n'), |
|
|
tools: { |
|
|
tools: { |
|
|
web_fetch: tool({ |
|
|
web_fetch: tool({ |
|
|
args: { engine: 'auto' }, |
|
|
args: { engine: 'openrouter' }, |
|
|
id: 'openrouter.web_fetch', |
|
|
id: 'openrouter.web_fetch', |
|
|
inputSchema: jsonSchema({ |
|
|
inputSchema: jsonSchema({ |
|
|
additionalProperties: true, |
|
|
additionalProperties: true, |
|
|
@ -134,7 +144,11 @@ export class FetchService { |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
const body = this.extractJsonFromWebFetchResult({ response, text }); |
|
|
const body = this.extractJsonFromWebFetchResult({ |
|
|
|
|
|
response, |
|
|
|
|
|
sources, |
|
|
|
|
|
text |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
if (!body) { |
|
|
if (!body) { |
|
|
return undefined; |
|
|
return undefined; |
|
|
|