Copilot SDK 图像输入的文件与 Blob 两种附件机制
Copilot SDK 支持向消息附加图像,提供文件路径与内存 Blob 两种方式,运行时自动按模型限制缩放或降质,无法处理时跳过,并需先确认模型具备视觉能力。
- **文件附件** (`type: "file"`:提供绝对路径;运行时间从磁盘读取文件,将其转换为base64,并发送到LLM. - **块附件** (`type: "blob"`:直接提供基础64-编码数据;当图像已经存在内存时有用(如截图,生成的图像,或来自API的数据). ## [概览](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/features/image-input#overview>)  | 概念 | 说明 | | --- | --- | | **文件附件** | 附加内容 `type: "file"` 绝对的 `path` 到磁盘上的图像 | | **块附件** | 附加内容 `type: "blob"`,基数64-编码 `data`和 a 键 `mimeType`不需要磁盘 I/O | | **自动编码** | 对于文件附件,运行时间读取图像并将其自动转换为基数64 | | **自动调整大小** | 运行时间会自动调整超过模型特定限制的图像大小或质量降低 | | **视觉能力** | 这个模型一定有 `capabilities.supports.vision = true` 处理图像 | ## [快速启动- 文件附件](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/features/image-input#quick-startfile-attachment>) 将图像文件附加到使用文件附件类型的任何信件中。路径必须是磁盘上图像的绝对路径。 TypeScript ```typescript import { CopilotClient } from "@github/copilot-sdk"; const client = new CopilotClient(); await client.start(); const session = await client.createSession({ model: "gpt-5.4", onPermissionRequest: async () => ({ kind: "approve-once" }), }); await session.send({ prompt: "Describe what you see in this image", attachments: [ { type: "file", path: "/absolute/path/to/screenshot.png", }, ], }); ``` Python 键 ```python from copilot import CopilotClient, PermissionDecisionApproveOnce client = CopilotClient() await client.start() session = await client.create_session( on_permission_request=lambda req, inv: PermissionDecisionApproveOnce(), model="gpt-5.4", ) await session.send( "Describe what you see in this image", attachments=[ { "type": "file", "path": "/absolute/path/to/screenshot.png", }, ], ) ``` 滚开 ```golang ctx := context.Background() client := copilot.NewClient(nil) client.Start(ctx) session, _ := client.CreateSession(ctx, &copilot.SessionConfig{ Model: "gpt-5.4", OnPermissionRequest: func(req copilot.PermissionRequest, inv copilot.PermissionInvocation) (rpc.PermissionDecision, error) { return &rpc.PermissionDecisionApproveOnce{}, nil }, }) path := "/absolute/path/to/screenshot.png" session.Send(ctx, copilot.MessageOptions{ Prompt: "Describe what you see in this image", Attachments: []copilot.Attachment{ &copilot.AttachmentFile{ DisplayName: "screenshot.png", Path: path, }, }, }) ``` 净值 ```csharp using GitHub.Copilot; using GitHub.Copilot.Rpc; await using var client = new CopilotClient(); await using var session = await client.CreateSessionAsync(new SessionConfig { Model = "gpt-5.4", OnPermissionRequest = (req, inv) => Task.FromResult(PermissionDecision.ApproveOnce()), }); await session.SendAsync(new MessageOptions { Prompt = "Describe what you see in this image", Attachments = new List<Attachment> { new AttachmentFile { Path = "/absolute/path/to/screenshot.png", DisplayName = "screenshot.png", }, }, }); ``` 爪哇语 ```java import com.github.copilot.CopilotClient; import com.github.copilot.rpc.*; import java.util.List; try (var client = new CopilotClient()) { client.start().get(); var session = client.createSession( new SessionConfig() .setModel("gpt-5.4") .setOnPermissionRequest(PermissionHandler.APPROVE_ALL) ).get(); session.send(new MessageOptions() .setPrompt("Describe what you see in this image") .setAttachments(List.of( new Attachment("file", "/absolute/path/to/screenshot.png", "screenshot.png") )) ).get(); } ``` ## [快速启动- blob 附加](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/features/image-input#quick-startblob-attachment>) 当您已经拥有内存中的图像数据(例如您的应用程序捕获的截图,或者从API中获取的图像)时,使用blob附件直接发送,而不写入磁盘. TypeScript ```typescript import { CopilotClient } from "@github/copilot-sdk"; const client = new CopilotClient(); await client.start(); const session = await client.createSession({ model: "gpt-5.4", onPermissionRequest: async () => ({ kind: "approve-once" }), }); const base64ImageData = "..."; // your base64-encoded image await session.send({ prompt: "Describe what you see in this image", attachments: [ { type: "blob", data: base64ImageData, mimeType: "image/png", displayName: "screenshot.png", }, ], }); ``` Python 键 ```python from copilot import CopilotClient, PermissionDecisionApproveOnce client = CopilotClient() await client.start() session = await client.create_session( on_permission_request=lambda req, inv: PermissionDecisionApproveOnce(), model="gpt-5.4", ) base64_image_data = "..." # your base64-encoded image await session.send( "Describe what you see in this image", attachments=[ { "type": "blob", "data": base64_image_data, "mimeType": "image/png", "displayName": "screenshot.png", }, ], ) ``` 滚开 ```golang mimeType := "image/png" displayName := "screenshot.png" session.Send(ctx, copilot.MessageOptions{ Prompt: "Describe what you see in this image", Attachments: []copilot.Attachment{ &copilot.AttachmentBlob{ Data: &base64ImageData, // base64-encoded string MIMEType: mimeType, DisplayName: &displayName, }, }, }) ``` 净值 ```csharp await session.SendAsync(new MessageOptions { Prompt = "Describe what you see in this image", Attachments = new List<Attachment> { new AttachmentBlob { Data = base64ImageData, MimeType = "image/png", DisplayName = "screenshot.png", }, }, }); ``` 爪哇语 ```java import com.github.copilot.CopilotClient; import com.github.copilot.rpc.*; import java.util.List; try (var client = new CopilotClient()) { client.start().get(); var session = client.createSession( new SessionConfig() .setModel("gpt-5.4") .setOnPermissionRequest(PermissionHandler.APPROVE_ALL) ).get(); var base64ImageData = "..."; // your base64-encoded image session.send(new MessageOptions() .setPrompt("Describe what you see in this image") .setAttachments(List.of( new BlobAttachment() .setData(base64ImageData) .setMimeType("image/png") .setDisplayName("screenshot.png") )) ).get(); } ``` ## [支持的格式](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/features/image-input#supported-formats>) 支持的图像格式包括 JPG, PNG, GIF, 以及其他常见的图像类型. 对于文件附件, 运行时间从磁盘读取图像并按需要转换它. 对于blob附件, 您直接提供基础64数据和 MIME 类型. 使用 PNG 或 JPEG 来获取最佳效果, 因为这些是得到最广泛支持的格式. 模特儿们的 `capabilities.limits.vision.supported_media_types` 字段列出它所接受的准确的MIME类型。 ## [自动处理](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/features/image-input#automatic-processing>) 运行时间会自动处理图像以适应模型的制约。不需要手动重新调整大小 。 - 超过模型尺寸或尺寸限制的图像会自动调整大小(保留侧面比)或质量降低. - 如果一个图像在处理后无法带入限制范围,则会跳过而不发送到 LLM 。 - 模特儿们的 `capabilities.limits.vision.max_prompt_image_size` 字段表示以字节表示的最大图像大小。 您可以通过模型能力对象在运行时检查这些限制。 要获得最佳经验, 请使用合理大小的 PNG 或 JPEG 图像 。 ## [远景模型能力](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/features/image-input#vision-model-capabilities>) 并非所有模型都支持视觉。 在发送图像前检查模型的能力 。 ### [能力字段](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/features/image-input#capability-fields>) | 外地 | 类型 | 说明 | | --- | --- | --- | | `capabilities.supports.vision` | `boolean` | 模型是否能够处理图像输入 | | `capabilities.limits.vision.supported_media_types` | `string[]` | MIME类型该模型接受(例如, `["image/png", "image/jpeg"]`) | | `capabilities.limits.vision.max_prompt_images` | `number` | 每个提示的最大图像数 | | `capabilities.limits.vision.max_prompt_image_size` | `number` | 最大图像大小, 以字节计 | ### [视野限制类型](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/features/image-input#vision-limits-type>) ```typescript vision?: { supported_media_types: string[]; max_prompt_images: number; max_prompt_image_size: number; // bytes }; ``` ## [接收图像结果](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/features/image-input#receiving-image-results>) 当工具返回图像(例如截图或生成的图表)时,结果包含 `"image"` 包含基础64-编码数据的内容块. | 外地 | 类型 | 说明 | | --- | --- | --- | | `type` | `"image"` | 内容块类型歧视者 | | `data` | `string` | Base64 编码图像数据 | | `mimeType` | `string` | MIME类型(例如, `"image/png"`) | 这些图像块出现在 `tool.execution_complete` 事件结果。 [精简会议活动](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/features/streaming-events>) 整个事件生命周期的指南。 ## [提示和限制](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/features/image-input#tips-and-limitations>) | 提示( T) | 细节 | | --- | --- | | **直接使用 PNG 或 JPEG** | 避免间接转换 -- -- 这些将发送到LLM 即时 | | **保持图像的合理尺寸** | 大图像可能质量降低,可能失去重要细节 | | **文件附件使用绝对路径** | 运行时间从磁盘读取文件; 相对路径可能无法正确解析 | | **使用blob附件进行内存数据** | 当您已有基础64数据(例如截图, API 响应)时, blob 避免不必要的磁盘 I/O | | **先检查视觉支持** | 将图像发送到一个没有视觉理解的非视觉模型的废品 | | **支持多个图像** | 在一个信件中附加几个附件, 最多到模型的 `max_prompt_images` 限制 | | **SVG 不支持** | SVG文件基于文本并被排除在图像处理之外 | ## [另见](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/features/image-input#see-also>) - [精简会议活动](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/features/streaming-events>): 事件生命周期,包括工具结果内容块 - [引导和排队](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/features/steering-and-queueing>): 发送带有附件的后续信件
查看原始来源