-
Notifications
You must be signed in to change notification settings - Fork 656
feat(fetch-url): support fetching images as multimodal content #2011
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| Fetch content from a URL. The content is returned either as the main text extracted from the page, or as the full response body verbatim; a note at the top of the result states which of the two you received, so you can judge how complete it is. Use this when you need to read a specific web page. | ||
| Fetch content from a URL. The content is returned either as the main text extracted from the page, as the full response body verbatim, or (for image URLs) as the image itself; a note at the top of the result states which of the two you received, so you can judge how complete it is. Use this when you need to read a specific web page or fetch an image from the internet. | ||
|
|
||
| Only fully-formed public `http`/`https` URLs are supported; other schemes and private or loopback addresses are not fetched. Very large pages may be truncated or refused. The fetch carries no login or session for the target site, so pages behind authentication (private repositories, internal dashboards) return a login page or an error instead of the real content — if the text you get back looks like a generic landing or sign-in page, treat that as the login wall, not the answer, and reach the content through a credentialed route (an authenticated CLI or MCP tool) instead. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -237,6 +237,26 @@ export class LocalFetchURLProvider implements UrlFetcher { | |
| ); | ||
| } | ||
|
|
||
| const contentType = (response.headers.get('content-type') ?? '').toLowerCase(); | ||
|
|
||
| // Image responses: read binary body and return as base64 for direct | ||
| // model consumption. | ||
| if (contentType.startsWith('image/')) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When the fetched URL returns any Useful? React with 👍 / 👎. |
||
| const arrayBuffer = await response.arrayBuffer(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For Useful? React with 👍 / 👎. |
||
| const bytes = Buffer.from(arrayBuffer); | ||
| if (bytes.length > this.maxBytes) { | ||
| throw new Error( | ||
| `Response body too large: ${String(bytes.length)} bytes exceeds maxBytes (${String(this.maxBytes)}).`, | ||
| ); | ||
| } | ||
| const base64 = bytes.toString('base64'); | ||
| return { | ||
| content: `Image fetched (${contentType}, ${String(bytes.length)} bytes)`, | ||
| kind: 'image', | ||
| imageData: { mimeType: contentType.split(';')[0]!.trim(), base64 }, | ||
| }; | ||
| } | ||
|
|
||
| // Reject oversized responses before buffering the full body. | ||
| const contentLengthRaw = response.headers.get('content-length'); | ||
| if (contentLengthRaw !== null) { | ||
|
|
@@ -263,7 +283,6 @@ export class LocalFetchURLProvider implements UrlFetcher { | |
| ); | ||
| } | ||
|
|
||
| const contentType = (response.headers.get('content-type') ?? '').toLowerCase(); | ||
| if (contentType.startsWith('text/plain') || contentType.startsWith('text/markdown')) { | ||
| return { content: body, kind: 'passthrough' }; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implementing this only in
packages/agent-coremisses the shipped kap-server path: I checkedpackages/kap-server/package.json:26(depends on@moonshot-ai/agent-core-v2) andpackages/agent-core-v2/src/app/web/tools/fetch-url-types.ts:13-18/fetch-url.ts:61-78, whereUrlFetchKindis still onlypassthrough | extractedand the tool still renders text. As a result,kimi web/kap-server users fetching an image URL still get the old HTML/text extraction behavior, so this feature won't work in the default CLI/server environment unless the v2 FetchURL provider/tool is updated too.Useful? React with 👍 / 👎.