Skip to content

feat(fetch-url): support fetching images from URLs#1266

Open
bj456736 wants to merge 3 commits into
MoonshotAI:mainfrom
bj456736:auto-pr-20260701-1800
Open

feat(fetch-url): support fetching images from URLs#1266
bj456736 wants to merge 3 commits into
MoonshotAI:mainfrom
bj456736:auto-pr-20260701-1800

Conversation

@bj456736

@bj456736 bj456736 commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Problem

The FetchURL tool can only return text content. When a URL points directly to an image (e.g. a chart PNG, a diagram SVG, or a photo), the tool receives binary image data but has no way to present it to the model as a viewable image.

Solution

Extend the fetch URL pipeline to detect image responses and render them as image_url content parts:

  1. Type extension — Add 'image' to UrlFetchKind and introduce UrlFetchImageData with base64 + MIME type.

  2. Local providerLocalFetchURLProvider now checks content-type before deciding how to consume the response body. For image/* it streams the body as arrayBuffer(), encodes to base64, and returns kind: 'image' with imageData.

  3. Moonshot providerMoonshotFetchURLProvider also checks the response content-type and handles image responses the same way, falling back to local fetcher on any error as before.

  4. Tool executionFetchURLTool.execution() now branches: when imageData is present it returns a ContentPart[] with a text note + an image_url part using a data:…base64,… URL, so the model sees the image directly.

  5. Docs & tests — Updated tool description to mention image support. Added tests covering image fetch in all three layers (tool, local provider, moonshot provider integration via fallback).

Files changed

  • packages/agent-core/src/tools/builtin/web/fetch-url.ts
  • packages/agent-core/src/tools/builtin/web/fetch-url.md
  • packages/agent-core/src/tools/providers/local-fetch-url.ts
  • packages/agent-core/src/tools/providers/moonshot-fetch-url.ts
  • packages/agent-core/test/tools/fetch-url.test.ts
  • packages/agent-core/test/tools/providers/local-fetch-url.test.ts

All 3348 tests in packages/agent-core/test pass (212 test files).

qer added 3 commits June 30, 2026 18:22
Closes MoonshotAI#1016

The LLM sometimes passes 'completed' as the status for TodoList items,
but the schema only accepted 'pending' | 'in_progress' | 'done'. This
produced two problems:

1. Validation failed when the model used 'completed'.
2. Even if validation passed, statusMarker() had no case for 'completed'
   and fell through to the unreachable default branch.

Changes:
- Extend TodoStatus union to include 'completed' so it is accepted at
  the type level.
- Map 'completed' -> 'done' in setTodos() so persisted state stays
  clean.
- Handle 'completed' in statusMarker() so it renders as '[done]'.
- Update the markdown description to explicitly warn against using
  'completed'.
- Add a test confirming 'completed' is accepted and mapped to 'done'.
Extend FetchURL to support image responses:
- Add 'image' kind to UrlFetchResult with base64 imageData field
- LocalFetchURLProvider: detect image/* content-type, stream binary data,
  and return base64-encoded imageData
- MoonshotFetchURLProvider: handle image responses from coding-fetch service
- FetchURLTool: render returned imageData as image_url content parts
  so the model can view fetched images directly
- Update tool description to mention image support
- Add tests for image fetching in all three layers

Fixes: Fetch 工具支持把图片也拉下来
@changeset-bot

changeset-bot Bot commented Jul 1, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 6909f86

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6909f86392

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

title: z.string().min(1).describe('Short, actionable title for the todo.'),
status: z.enum(['pending', 'in_progress', 'done']).describe('Current status of the todo.'),
status: z
.preprocess((val) => (val === 'completed' ? 'done' : val), z.enum(['pending', 'in_progress', 'done']))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Normalize completed after tool-call validation

When the model actually calls TodoList, args are validated by AJV from tool.parameters before resolveExecution runs, and toInputJsonSchema explicitly notes there is no Zod parse/strip step before dispatch (packages/agent-core/src/tools/support/input-schema.ts:41, packages/agent-core/src/loop/tool-call.ts:202-225). With zod's input JSON schema for this preprocess still derived from the inner enum, status: "completed" is rejected before the new setTodos normalization can run, so the added acceptance only works in tests that bypass preflight validation. Please either make the JSON schema explicitly allow the handled alias or normalize after validation in the loop/tool path.

Useful? React with 👍 / 👎.


// Image responses: stream as binary and return as base64-encoded image data
// so the tool can render them directly as image_url content parts.
if (contentType.startsWith('image/')) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Restrict URL images to supported MIME types

For any URL that returns image/svg+xml, image/bmp, image/tiff, etc., this new branch emits a base64 image_url with that MIME type. Downstream Anthropic conversion only accepts base64 PNG/JPEG/GIF/WebP (packages/kosong/src/providers/anthropic.ts:446-470), and the file media path already treats those as the accepted image formats (packages/agent-core/src/tools/support/file-type.ts:379-381), so fetching one of these URLs can make the next model call fail instead of producing a useful tool result. Please gate this path, and the matching Moonshot image path, to supported image MIME types or return a controlled error/text fallback for the rest.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant