docs(comments): 注释中文化 · internal/runtime/multimodal(21 文件,纯替换)
- 只翻注释/文档字符串,代码零变化:go/scanner 词法流比对 21 文件 token+字面量全等,增删行数 1:1(467/467) - 刻意保留非散文英文:JSON Schema description、工具 Description() 返回值、adapter prompt、API 路径与命令示例、环境变量名 - 逐行确认代码前缀未变,未触碰其他模块
This commit is contained in:
1 parent
b9e042d07d
commit
b0fbb8bc0d
21 files changed
+467
-467
No files matched your search
@@ -10,23 +10,23 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Size caps for reading image bytes and response bodies, so a hostile or
|
||||
// malformed source cannot exhaust memory. The 50MB cap matches the file-upload
|
||||
// limit in DevDocs/02-development-constraints.md §11.
|
||||
// 读取图像字节与响应体的体积上限,使恶意或
|
||||
// 畸形来源无法耗尽内存。50MB 上限与
|
||||
// DevDocs/02-development-constraints.md §11 的文件上传限制一致。
|
||||
const (
|
||||
maxImageBytes = 50 << 20
|
||||
maxFetchedBody = 50 << 20
|
||||
)
|
||||
|
||||
// DataURI builds a base64 data: image URI from mime and raw bytes.
|
||||
// DataURI 由 mime 与原始字节构造一个 base64 data: 图像 URI。
|
||||
func DataURI(mime string, data []byte) string {
|
||||
return "data:" + mime + ";base64," + base64.StdEncoding.EncodeToString(data)
|
||||
}
|
||||
|
||||
// Resolve loads the bytes for an ImageRef. Paths and base64 payloads are read
|
||||
// locally; URLs are fetched with an http.Client (the provided client, or a
|
||||
// default one when nil). For base64 input the Data field is returned verbatim as
|
||||
// the payload and the MIME type is guessed from a data: prefix.
|
||||
// Resolve 加载一个 ImageRef 对应的字节。路径与 base64 载荷在
|
||||
// 本地读取;URL 用 http.Client 抓取(传入的 client,为 nil 时
|
||||
// 用默认 client)。对 base64 输入,Data 字段原样作为
|
||||
// 载荷返回,MIME 类型从 data: 前缀推断。
|
||||
func Resolve(ctx context.Context, ref ImageRef, client *http.Client) (mime string, data []byte, err error) {
|
||||
switch ref.Source {
|
||||
case SourcePath:
|
||||
@@ -40,7 +40,7 @@ func Resolve(ctx context.Context, ref ImageRef, client *http.Client) (mime strin
|
||||
}
|
||||
}
|
||||
|
||||
// readFile reads an image file and guesses its MIME type from the extension.
|
||||
// readFile 读取一个图像文件,并从扩展名推断其 MIME 类型。
|
||||
func readFile(path string) (string, []byte, error) {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
@@ -54,7 +54,7 @@ func readFile(path string) (string, []byte, error) {
|
||||
return mimeByExt(path), data, nil
|
||||
}
|
||||
|
||||
// decodeBase64 returns the decoded bytes of a base64/data: payload and its MIME.
|
||||
// decodeBase64 返回 base64/data: 载荷解码后的字节及其 MIME。
|
||||
func decodeBase64(src string) (string, []byte, error) {
|
||||
mime, encoded, isDataURI := splitDataURI(src)
|
||||
if isDataURI {
|
||||
@@ -71,7 +71,7 @@ func decodeBase64(src string) (string, []byte, error) {
|
||||
return "image/png", decoded, nil
|
||||
}
|
||||
|
||||
// fetchURL downloads an image over http(s); only http/https schemes are allowed.
|
||||
// fetchURL 通过 http(s) 下载一张图像;只允许 http/https 协议。
|
||||
func fetchURL(ctx context.Context, rawURL string, client *http.Client) (string, []byte, error) {
|
||||
if !strings.HasPrefix(rawURL, "http://") && !strings.HasPrefix(rawURL, "https://") {
|
||||
return "", nil, fmt.Errorf("vision: %w: %q", ErrUnsupportedSource, rawURL)
|
||||
@@ -98,8 +98,8 @@ func fetchURL(ctx context.Context, rawURL string, client *http.Client) (string,
|
||||
return mimeByContentType(resp.Header.Get("Content-Type")), data, nil
|
||||
}
|
||||
|
||||
// splitDataURI parses a "data:<mime>;base64,<payload>" prefix. The second return
|
||||
// is false when src is not a data: URI.
|
||||
// splitDataURI 解析 "data:<mime>;base64,<payload>" 前缀。src 不是
|
||||
// data: URI 时第二个返回值是 false。
|
||||
func splitDataURI(src string) (mime, payload string, ok bool) {
|
||||
if !strings.HasPrefix(src, "data:") {
|
||||
return "", "", false
|
||||
@@ -124,7 +124,7 @@ func splitDataURI(src string) (mime, payload string, ok bool) {
|
||||
return mime, payload, true
|
||||
}
|
||||
|
||||
// mimeByExt guesses a MIME type from a file path extension.
|
||||
// mimeByExt 从文件路径扩展名推断 MIME 类型。
|
||||
func mimeByExt(path string) string {
|
||||
switch strings.ToLower(pathExtension(path)) {
|
||||
case ".png":
|
||||
@@ -140,8 +140,8 @@ func mimeByExt(path string) string {
|
||||
}
|
||||
}
|
||||
|
||||
// mimeByContentType returns a MIME type from an HTTP Content-Type header,
|
||||
// defaulting to image/png when absent or non-image.
|
||||
// mimeByContentType 从 HTTP Content-Type 头返回 MIME 类型,
|
||||
// 缺失或非图像时默认为 image/png。
|
||||
func mimeByContentType(ct string) string {
|
||||
if ct == "" || !strings.HasPrefix(ct, "image/") {
|
||||
return "image/png"
|
||||
@@ -149,7 +149,7 @@ func mimeByContentType(ct string) string {
|
||||
return strings.TrimSpace(strings.Split(ct, ";")[0])
|
||||
}
|
||||
|
||||
// pathExtension returns the file extension with its dot, or empty when none.
|
||||
// pathExtension 返回带点号的文件扩展名,没有则返回空串。
|
||||
func pathExtension(path string) string {
|
||||
base := path
|
||||
if idx := strings.LastIndexByte(path, '/'); idx >= 0 {
|
||||
@@ -164,15 +164,15 @@ func pathExtension(path string) string {
|
||||
return base[dot:]
|
||||
}
|
||||
|
||||
// fileExists reports whether path exists on disk and is a regular file.
|
||||
// fileExists 报告 path 是否存在于磁盘且为常规文件。
|
||||
func fileExists(path string) bool {
|
||||
info, err := os.Stat(path)
|
||||
return err == nil && info.Mode().IsRegular()
|
||||
}
|
||||
|
||||
// defaultHTTPClient builds the HTTP client described in
|
||||
// DevDocs/02-development-constraints.md §2.3. A fresh client is returned per
|
||||
// call rather than a package global, which the constraints forbid.
|
||||
// defaultHTTPClient 构造 DevDocs/02-development-constraints.md §2.3
|
||||
// 所述的 HTTP client。这里每次调用返回一个新 client,
|
||||
// 而不是包级全局变量——后者为约束所禁止。
|
||||
func defaultHTTPClient() *http.Client {
|
||||
return &http.Client{Transport: http.DefaultTransport}
|
||||
}
|
||||
Reference in new issue
Block a user