Files
Calcium-Ion 7b857f5a1e feat(plugins): add provider plugins, request plugins and a community marketplace (#8271)
* feat(plugins): add provider plugins, request plugins and a community marketplace

- Provider plugins turn an upstream vendor into a channel type: routes,
  protocol conversion, models, static keys, sign-in flows with token
  refresh, balance queries, per-channel config and a scheduled hook.
- Request plugins run on requests and replies (ingress, request,
  outbound, egress, reply, report), with instances by scope, model
  ranges, dry runs and per-log records. Built-ins: request filter,
  request redaction, parameter override, auto reasoning effort, image
  compression and content moderation. Sensitive words move into a
  request filter instance on first start.
- Node sync: a system_sync_topics table lets every node reload settings,
  channels, plugins and other data within seconds, and the system info
  page shows each node's state.
- Marketplace: a community source next to the official one, lookup by
  plugin code for official and community plugins, key rules for
  community plugins, install counts and takedown warnings.

* docs(plugins): name request protocols and host-only members in the plugin API

- Protocol names: ctx.format and samples use relaykit's protocol names.
  The spec lists the client paths and body of each name, so every host
  passes the same names to plugins.
- Host-specific members: channel, group, user, token, channelTypes,
  channelOther and utils.paramOverride exist only on new-api. The types
  mark them optional.
- Relay routes: the spec now writes out the route keys and the converters
  each incoming path accepts. The schema lists the converter names.

* feat(plugins): show recommended plugins from the official list too

The site now marks official plugins as recommended in its index. The
built-in official and community lists both get a Recommended group;
lists added by the administrator still cannot recommend plugins.

* feat(plugins): fall back to www.newapi.pro when www.newapi.ai does not answer

Plugin lists, plugin files and logos from www.newapi.ai are downloaded
from www.newapi.pro, the mainland China site, when newapi.ai gives no
answer within 10 seconds; the browser remembers this for a day. The
marketplace toolbar has a download site picker: Auto, or Mainland China
to always use www.newapi.pro. Saved source addresses stay on newapi.ai,
and every file is still checked against its sha256.

Community install counts go to newapi.ai first and to newapi.pro when
newapi.ai cannot be reached.

* fix(plugins): leaving the page does not switch the plugin site to www.newapi.pro

Reloading or leaving the marketplace cancels requests still running.
That was read as www.newapi.ai not answering, so the browser moved to
www.newapi.pro for a day although newapi.ai was reachable. Requests
cut off by leaving the page are no longer counted.

* feat(plugins): choose the plugin download site by hand

The marketplace no longer switches between plugin sites by itself. The
toolbar menu lists the main site www.newapi.ai, the backup site
www.newapi.pro and the other plugin lists; picking an entry is the only
thing that changes where plugins are downloaded from, and the main site
can always be picked again.

A browser that the earlier automatic switch had moved to www.newapi.pro
opens on the main site again. The download site, the source choice and
managing sources now share one menu in the list header.

* feat(plugins): show plugin usage guides and require README and changelog

Every marketplace plugin version now ships a usage guide, README.md, and a
changelog, CHANGELOG.md, next to plugin.js. The install dialog shows the
guide in the reader's language when a translation is listed, falls back to
English, and links to the source file. A guide or changelog that breaks the
format is reported with its file, line and rule instead of being shown.

The plugin API document describes both formats, the translations they may
list, and the checks the community marketplace runs before it accepts a
version: readme_missing, readme_invalid, changelog_missing and
changelog_invalid. Versions accepted before these checks are not checked
again.

* fix(plugins): address review findings on plugin parts, limits and access

- Only the root user reads request plugin instance configs; an admin who
  may bind instances to channels gets them without configs.
- Outbound hooks that grow the body are held to MAX_REQUEST_BODY_MB, as
  ingress hooks are, and answer 413 without a retry.
- An admin who cannot read plugin options keeps the balance and model
  list actions on plugin channels; the server answers whether they work.
- Reading the parts of a plugin source gives up when the source does not
  parse or code elsewhere refers to meta, as the meta preview does.

* fix(plugins): address review findings on request processing, documents and node sync

- PDF checks resolve indirect parameters and bound every value the parser
  allocates or loops on: predictor columns, object stream counts, nested
  object streams, cross-reference streams and ToUnicode ranges. Font and
  CMap work checks the deadline.
- A WebP frame must match its declared canvas, and keyword search stops at
  its match limit.
- Request processing treats a body as JSON whenever the gateway parses it
  as JSON, keeps Gemini reply parts in place, scopes instances by the
  settled auto group, hides channel header overrides from plugin param
  overrides, and captures replies only for report instances in scope.
- Request processing no longer follows the plugin master switch, which now
  covers plugin tasks and channels only.
- json.patch copies stop at MAX_REQUEST_BODY_MB, and provider prepareRequest
  effects past it answer 413.
- Image billing reads the count from the final upstream body, including the
  count members of SiliconFlow, Imagen and Replicate and the Imagen default.
- Automatic channel status changes no longer reload every node's channel
  cache. A credential refresh holds no channel lock while it waits on the
  vendor. A key that needs login is disabled and the request can retry.
- Balance requests must go to the channel host or allowedHosts, and provider
  stream events a plugin drops keep the stream alive.
- Node sync: OAuth provider writes bump inside their transaction, a failed
  startup load is retried, and each node is judged by its own topics.
- request-filter checks the text the old sensitive-word check did,
  request-redact and content-moderation handle large inputs, and
  image-compress lossless mode leaves JPEG and WebP alone.

* fix(plugins): address the second review on balances, egress and routes

- Channel other_info writes (status changes, balance queries and manual
  multi-key actions) only land while other_info is still the value read,
  and retry otherwise, so a write on one node no longer overwrites another
  node's balance or status reason. They write only their own columns.
- On the Responses path egress reads the body after the channel finalizes
  it, which is the body sent upstream.
- The Vertex AI plugin picks the region by the model the client asked for,
  as the built-in Vertex AI channel does.
- /request-processing and /security are reserved route namespaces, and a
  test checks that every console page is reserved.
- The plugin API docs say what egress reads on a provider plugin channel.

* feat(channels): run the balance update as a system task with progress

"Update all balances" now queues a channel_balance system task instead of
starting a goroutine guarded by an in-process flag. The task row holds the
progress (enabled channels queried of total), so any node can answer it,
and its active key keeps one run at a time across nodes. The
CHANNEL_UPDATE_FREQUENCY schedule becomes the same task's schedule, so
manual and scheduled runs no longer overlap.

GET /api/channel/update_balance answers with the task id, and with the
running one when a run is already queued. GET
/api/channel/update_balance/task/:task_id, under the same channel
permission, returns the status, progress and failure of balance update
tasks only. The channel list follows the task, reloads as more channels
are queried and once more at the end, and reports how the run ended.

* feat(system): sync nodes through system tasks and clean up task history

Node sync no longer needs its own table. A write to synced data adds a
pending sync_<topic> system task. Every node polls the newest task of each
topic it keeps every 3 s and reloads when that task is newer than the one
it applied. Custom OAuth providers still add their task inside the write
transaction, and lock the newest task of the topic first, so tasks commit
in id order. The master marks a sync task succeeded once every online node
that keeps the topic has applied it. Until then the task shows how many
nodes have applied it and why the others have not. The
system_sync_topics table is gone.

A composite (type, id) index answers the newest task of a type from the
index. The latest-per-type and earliest-pending queries now read ids first,
because MySQL 5.7 scanned the whole table for them.

A daily system_task_cleanup task deletes finished system tasks older than
system_task_setting.retention_days (default 30, at least 1), in batches.
It never deletes active tasks or the newest task of any type. The
retention is set under Operations > Log maintenance.
2026-11-12 20:50:58 +08:00

823 lines
26 KiB
Go

package requestplugin
import (
"bytes"
"cmp"
"context"
"encoding/base64"
"errors"
"fmt"
"hash/maphash"
"image"
"io"
"path"
"slices"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/pkg/jsplugin"
"github.com/QuantumNous/new-api/pkg/requestpatch"
relaycommon "github.com/QuantumNous/new-api/relay/common"
"github.com/tidwall/gjson"
)
// Caps of reading attachment text with utils.mediaText. A file past a cap is
// unchecked; text past a cap is truncated.
const (
// maxDocumentBytes bounds the decoded size of a PDF or DOCX file.
maxDocumentBytes = 10 << 20
// maxRequestTextChars bounds the attachment text read for one request,
// in UTF-16 code units: four attachments read whole.
maxRequestTextChars = 512 << 10
// documentTimeout bounds reading one PDF or DOCX file.
documentTimeout = 3 * time.Second
// documentWait bounds the wait for a document slot.
documentWait = 5 * time.Second
// maxRecordedAttachments bounds the attachments of one record entry.
maxRecordedAttachments = 16
// maxFetchMedia and maxFetchMediaBytes bound the media one utils.fetch
// body sends: refs and decoded bytes.
maxFetchMedia = 8
maxFetchMediaBytes = 32 << 20
)
// documentSlots bounds the PDF and DOCX files read at once in the process.
var documentSlots = make(chan struct{}, 2)
// The reasons an attachment is unchecked.
const (
uncheckedUnsupported = "unsupported"
uncheckedReference = "reference"
uncheckedTooLarge = "too_large"
uncheckedUnreadable = "unreadable"
uncheckedLimit = "limit"
uncheckedBusy = "busy"
// uncheckedNoText is a file that holds no text, such as a scanned PDF.
uncheckedNoText = "no_text"
// uncheckedNotRewritable is a file a media.text effect could not
// rewrite, so it went on as it was.
uncheckedNotRewritable = "not_rewritable"
)
// The status of an attachment in the record.
const (
attachmentChecked = "checked"
attachmentTruncated = "truncated"
attachmentUnchecked = "unchecked"
)
// Formats the host reads text from.
const (
formatText = "text"
formatDOCX = "docx"
formatPDF = "pdf"
)
const docxMime = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
// textExtensions are the file extensions of text files, for a file whose
// mime type does not say.
var textExtensions = map[string]bool{
".txt": true, ".text": true, ".md": true, ".markdown": true, ".csv": true, ".tsv": true, ".json": true, ".jsonl": true,
".ndjson": true, ".xml": true, ".yaml": true, ".yml": true, ".toml": true, ".ini": true, ".cfg": true, ".conf": true,
".log": true, ".html": true, ".htm": true, ".css": true, ".js": true, ".mjs": true, ".cjs": true, ".ts": true,
".tsx": true, ".jsx": true, ".vue": true, ".svelte": true, ".py": true, ".go": true, ".java": true, ".kt": true,
".kts": true, ".scala": true, ".c": true, ".h": true, ".cc": true, ".cpp": true, ".hpp": true, ".cs": true,
".rb": true, ".php": true, ".rs": true, ".swift": true, ".m": true, ".sh": true, ".bash": true, ".zsh": true,
".ps1": true, ".sql": true, ".r": true, ".lua": true, ".pl": true, ".dart": true, ".tex": true, ".rst": true,
".srt": true, ".vtt": true, ".properties": true, ".proto": true, ".graphql": true, ".diff": true, ".patch": true,
}
// documentExtensions are the extensions of documents the host cannot read.
var documentExtensions = map[string]bool{
".doc": true, ".xls": true, ".xlsx": true, ".ppt": true, ".pptx": true, ".odt": true, ".ods": true, ".odp": true,
".rtf": true, ".epub": true, ".pages": true, ".numbers": true, ".key": true,
}
// textFormat is the format the host reads text from for a file of mime and
// filename: text, docx or pdf; "" for any other file.
func textFormat(mime, filename string) string {
mime = strings.ToLower(mime)
extension := strings.ToLower(path.Ext(filename))
switch {
case strings.HasPrefix(mime, "image/") || strings.HasPrefix(mime, "audio/") || strings.HasPrefix(mime, "video/"):
return ""
case mime == "application/pdf" || extension == ".pdf":
return formatPDF
case mime == docxMime || extension == ".docx":
return formatDOCX
case textMime(mime) || textExtensions[extension]:
return formatText
}
return ""
}
func textMime(mime string) bool {
if strings.HasPrefix(mime, "text/") || strings.HasSuffix(mime, "+json") || strings.HasSuffix(mime, "+xml") {
return true
}
switch mime {
case "application/json", "application/xml", "application/yaml", "application/x-yaml", "application/toml",
"application/x-toml", "application/javascript", "application/x-javascript", "application/ecmascript",
"application/x-sh", "application/x-shellscript", "application/sql", "application/x-sql", "application/csv",
"application/x-ndjson", "application/jsonl", "application/graphql", "application/x-tex", "application/x-latex",
"application/x-python", "application/x-httpd-php":
return true
}
return false
}
// mediaKind is the kind utils.media and the record give a file.
func mediaKind(mime, filename string) string {
mime = strings.ToLower(mime)
switch {
case strings.HasPrefix(mime, "image/"):
return "image"
case strings.HasPrefix(mime, "audio/"):
return "audio"
case strings.HasPrefix(mime, "video/"):
return "video"
case textFormat(mime, filename) != "" || documentExtensions[strings.ToLower(path.Ext(filename))] ||
mime == "application/msword" || mime == "application/rtf" || mime == "application/epub+zip" ||
strings.HasPrefix(mime, "application/vnd.openxmlformats-officedocument.") || strings.HasPrefix(mime, "application/vnd.ms-") ||
strings.HasPrefix(mime, "application/vnd.oasis.opendocument."):
return "document"
}
return "other"
}
// requestMedia is what the hook calls of one request share about its
// attachments: the text read from each, so instances that read one
// attachment read it once, and the request's text budget.
type requestMedia struct {
mu sync.Mutex
texts map[mediaKey]extractedText
chars int
}
type mediaKey struct {
hash uint64
size int
format string
}
// extractedText is the text read from one attachment, or the reason it was
// not read.
type extractedText struct {
text string
truncated bool
unchecked string
}
var mediaSeed = maphash.MakeSeed()
// extractions counts the attachments read, for tests.
var extractions atomic.Int64
// mediaItem is one "[media:N]" of a hook call: an inline media string of the
// body, or the content of a multipart file.
type mediaItem struct {
mime string
filename string
inline *inlineMedia
raw []byte
}
func (m mediaItem) size() int {
if m.inline != nil {
return m.inline.size()
}
return len(m.raw)
}
func (m mediaItem) reader() io.Reader {
if m.inline != nil {
return base64.NewDecoder(base64.StdEncoding, strings.NewReader(m.inline.data))
}
return bytes.NewReader(m.raw)
}
func (m mediaItem) hash() uint64 {
if m.inline != nil {
return maphash.String(mediaSeed, m.inline.data)
}
return maphash.Bytes(mediaSeed, m.raw)
}
// text reads the text of item once per request, under the request's budget.
func (r *requestMedia) text(ctx context.Context, item mediaItem, format string) extractedText {
key := mediaKey{hash: item.hash(), size: item.size(), format: format}
r.mu.Lock()
if cached, ok := r.texts[key]; ok {
r.mu.Unlock()
return cached
}
budget := maxRequestTextChars - r.chars
r.mu.Unlock()
if budget <= 0 {
return extractedText{unchecked: uncheckedLimit}
}
maxChars := min(jsplugin.MaxMediaTextChars, budget)
var result extractedText
if format == formatText {
result = readText(item.reader(), maxChars)
} else {
result = readDocument(ctx, item, format, maxChars)
}
extractions.Add(1)
r.mu.Lock()
defer r.mu.Unlock()
if r.texts == nil {
r.texts = map[mediaKey]extractedText{}
}
r.texts[key] = result
r.chars += jsplugin.UTF16Len(result.text)
return result
}
// readDocument reads a PDF or DOCX file: at most two at once in the process,
// each for at most documentTimeout.
func readDocument(ctx context.Context, item mediaItem, format string, maxChars int) extractedText {
if item.size() > maxDocumentBytes {
return extractedText{unchecked: uncheckedTooLarge}
}
wait, cancel := context.WithTimeout(ctx, documentWait)
defer cancel()
select {
case documentSlots <- struct{}{}:
case <-wait.Done():
return extractedText{unchecked: uncheckedBusy}
}
defer func() { <-documentSlots }()
data := item.raw
if item.inline != nil {
decoded, err := io.ReadAll(io.LimitReader(item.reader(), maxDocumentBytes+1))
if err != nil {
return extractedText{unchecked: uncheckedUnreadable}
}
data = decoded
}
deadline := time.Now().Add(documentTimeout)
if format == formatDOCX {
return docxText(data, maxChars, deadline)
}
return pdfText(data, maxChars, deadline)
}
// callMedia is the MediaSource of one hook call: the media of the body it
// reads and the multipart files of a form body, and what the call did with
// them, for its record entry.
type callMedia struct {
request *requestMedia
view []byte
media []inlineMedia
form *formBody
// files are the contents of the form's files, read on first use.
files [][]byte
filesErr error
// listed is set when the hook listed the attachments.
listed bool
attachments []jsplugin.Attachment
marks map[string]*relaycommon.RequestPluginAttachment
// order is the order the call first marked attachments in.
order []string
}
func newCallMedia(request *requestMedia, view []byte, media []inlineMedia, form *formBody) *callMedia {
return &callMedia{request: request, view: view, media: media, form: form}
}
func mediaRef(index int) string {
return "[media:" + strconv.Itoa(index) + "]"
}
// parseMediaRef returns N of "[media:N]".
func parseMediaRef(ref string) (int, bool) {
digits, found := strings.CutPrefix(ref, "[media:")
if digits, found = strings.CutSuffix(digits, "]"); !found || digits == "" || len(digits) > 6 {
return 0, false
}
index, err := strconv.Atoi(digits)
return index, err == nil && index >= 0 && strconv.Itoa(index) == digits
}
func (m *callMedia) readFiles() {
if m.files != nil || m.filesErr != nil || m.form == nil || len(m.form.files) == 0 {
return
}
files := make([][]byte, len(m.form.files))
for index, file := range m.form.files {
content, err := m.form.fileContent(file)
if err != nil {
m.filesErr = err
return
}
files[index] = content
}
m.files = files
}
// item returns the media ref names.
func (m *callMedia) item(ref string) (mediaItem, bool) {
index, ok := parseMediaRef(ref)
if !ok {
return mediaItem{}, false
}
if index < len(m.media) {
item := &m.media[index]
return mediaItem{mime: item.mime, filename: item.filename, inline: item}, true
}
index -= len(m.media)
if m.form == nil || index >= len(m.form.files) {
return mediaItem{}, false
}
if m.readFiles(); m.files == nil {
return mediaItem{}, false
}
file := m.form.files[index]
mime := file.mime
if mime == "" || mime == "application/octet-stream" {
mime = typeByExtension(file.filename)
}
return mediaItem{mime: mime, filename: file.filename, raw: m.files[index]}, true
}
func typeByExtension(filename string) string {
switch strings.ToLower(path.Ext(filename)) {
case ".png":
return "image/png"
case ".jpg", ".jpeg":
return "image/jpeg"
case ".gif":
return "image/gif"
case ".webp":
return "image/webp"
case ".mp3":
return "audio/mpeg"
case ".wav":
return "audio/wav"
case ".m4a":
return "audio/mp4"
case ".mp4":
return "video/mp4"
case ".pdf":
return "application/pdf"
case ".docx":
return docxMime
}
if textExtensions[strings.ToLower(path.Ext(filename))] {
return "text/plain"
}
return "application/octet-stream"
}
// Attachments lists the inline media and the files the body names by id or
// URL in body order, then the multipart files.
func (m *callMedia) Attachments() []jsplugin.Attachment {
if m.listed {
return m.attachments
}
m.listed = true
if bytes.Contains(m.view, []byte("[media:")) || bytes.Contains(m.view, []byte("file")) || bytes.Contains(m.view, []byte("image")) || bytes.Contains(m.view, []byte("source")) {
m.walkAttachments(gjson.ParseBytes(m.view), "", "", "")
}
if m.form != nil {
for index, file := range m.form.files {
if item, ok := m.item(mediaRef(len(m.media) + index)); ok {
m.attachments = append(m.attachments, jsplugin.Attachment{Ref: mediaRef(len(m.media) + index), Kind: mediaKind(item.mime, item.filename),
Mime: item.mime, Size: item.size(), Filename: file.filename, Path: file.name})
}
}
}
return m.attachments
}
// walkAttachments adds the attachments in value, at pointer: the "[media:N]"
// strings of the view and the shapes that name a file by id or URL. key is
// the member value is, parentType the type member of the object around it.
func (m *callMedia) walkAttachments(value gjson.Result, pointer, key, parentType string) {
switch {
case value.IsObject():
blockType := value.Get("type").Str
switch {
case key == "file" && value.Get("file_id").Type == gjson.String:
// A chat file part: {type: "file", file: {file_id, filename}}.
m.addReference("document", "", "", value.Get("filename").Str, pointer+"/file_id")
case (blockType == "input_file" || blockType == "input_image") && value.Get("file_id").Type == gjson.String:
kind := "document"
if blockType == "input_image" {
kind = "image"
}
m.addReference(kind, "", "", value.Get("filename").Str, pointer+"/file_id")
case blockType == "input_file" && value.Get("file_url").Type == gjson.String:
m.addReference("document", "", value.Get("file_url").Str, value.Get("filename").Str, pointer+"/file_url")
case key == "source" && (blockType == "url" || blockType == "file"):
// A Claude image or document source by URL or file id.
kind := "document"
if parentType == "image" {
kind = "image"
}
if blockType == "url" {
m.addReference(kind, "", value.Get("url").Str, "", pointer+"/url")
} else {
m.addReference(kind, "", "", "", pointer+"/file_id")
}
case key == "fileData" || (key == "file_data" && value.Get("file_uri").Type == gjson.String):
// A Gemini file part.
mime := value.Get("mimeType").Str
if mime == "" {
mime = value.Get("mime_type").Str
}
uri := value.Get("fileUri").Str
if uri == "" {
uri = value.Get("file_uri").Str
}
m.addReference(mediaKind(mime, ""), mime, uri, "", pointer)
case key == "image_url" && value.Get("url").Type == gjson.String:
m.remoteImage(value.Get("url").Str, pointer+"/url")
}
value.ForEach(func(name, member gjson.Result) bool {
m.walkAttachments(member, pointer+"/"+escapePointer(name.String()), name.String(), blockType)
return true
})
case value.IsArray():
index := 0
value.ForEach(func(_, item gjson.Result) bool {
m.walkAttachments(item, pointer+"/"+strconv.Itoa(index), "", parentType)
index++
return true
})
case value.Type == gjson.String:
if index, ok := parseMediaRef(value.Str); ok && index < len(m.media) {
item := m.media[index]
m.attachments = append(m.attachments, jsplugin.Attachment{Ref: value.Str, Kind: mediaKind(item.mime, item.filename), Mime: item.mime,
Size: item.size(), Filename: item.filename, Path: pointer, Rewritable: rewritable(item)})
} else if key == "image_url" {
// A Responses input_image: {type: "input_image", image_url}.
m.remoteImage(value.Str, pointer)
}
}
}
func (m *callMedia) remoteImage(url, pointer string) {
if strings.HasPrefix(url, "https://") || strings.HasPrefix(url, "http://") {
m.addReference("image", "", url, "", pointer)
}
}
func (m *callMedia) addReference(kind, mime, url, filename, pointer string) {
m.attachments = append(m.attachments, jsplugin.Attachment{Kind: kind, Mime: mime, URL: url, Filename: filename, Path: pointer})
}
// Media describes the media ref names.
func (m *callMedia) Media(ref string) (jsplugin.MediaInfo, bool) {
item, ok := m.item(ref)
if !ok {
return jsplugin.MediaInfo{}, false
}
info := jsplugin.MediaInfo{Kind: mediaKind(item.mime, item.filename), Mime: item.mime, Size: item.size(), Filename: item.filename}
if item.inline != nil {
info.Rewritable = rewritable(*item.inline)
}
if info.Kind == "image" {
if config, _, err := image.DecodeConfig(item.reader()); err == nil {
info.Width, info.Height = config.Width, config.Height
}
}
return info, true
}
// MediaText reads the text of the media ref names: once per request, at
// most maxChars UTF-16 code units of it.
func (m *callMedia) MediaText(ctx context.Context, ref string, maxChars int) (jsplugin.MediaText, bool) {
item, ok := m.item(ref)
if !ok {
return jsplugin.MediaText{}, false
}
result := extractedText{unchecked: uncheckedUnsupported}
if format := textFormat(item.mime, item.filename); format != "" && (item.inline == nil || !item.inline.audio) {
result = m.request.text(ctx, item, format)
}
if result.unchecked != "" {
m.mark(ref, attachmentUnchecked, result.unchecked)
return jsplugin.MediaText{Unchecked: result.unchecked}, true
}
text, truncated := result.text, result.truncated
if jsplugin.UTF16Len(text) > maxChars {
text, truncated = cutUTF16(text, maxChars), true
}
if truncated {
m.mark(ref, attachmentTruncated, "")
} else {
m.mark(ref, attachmentChecked, "")
}
return jsplugin.MediaText{Text: text, Truncated: truncated}, true
}
// cutUTF16 is the longest prefix of text at most limit UTF-16 code units long.
func cutUTF16(text string, limit int) string {
units := 0
for index, char := range text {
width := 1
if char > 0xFFFF {
width = 2
}
if units+width > limit {
return text[:index]
}
units += width
}
return text
}
// mark records what the call did with an attachment: ref, or "url:" and
// the pointer of a file named by URL. A better status stays: checked over
// truncated over unchecked.
func (m *callMedia) mark(key, status, reason string) {
rank := map[string]int{attachmentUnchecked: 1, attachmentTruncated: 2, attachmentChecked: 3}
if m.marks == nil {
m.marks = map[string]*relaycommon.RequestPluginAttachment{}
}
current := m.marks[key]
if current == nil {
current = &relaycommon.RequestPluginAttachment{}
m.marks[key] = current
m.order = append(m.order, key)
}
if rank[status] > rank[current.Status] {
current.Status, current.Reason = status, reason
}
}
func (m *callMedia) markRewritten(ref string) {
m.mark(ref, attachmentChecked, "")
m.marks[ref].Rewritten = true
}
// markNotRewritable records a file a media.text effect asked to rewrite and
// the host left as it was. It replaces what the call read: the text the hook
// wanted gone is still in the file.
func (m *callMedia) markNotRewritable(ref string) {
m.mark(ref, attachmentUnchecked, "")
mark := m.marks[ref]
mark.Status, mark.Reason, mark.Rewritten = attachmentUnchecked, uncheckedNotRewritable, false
}
// exists reports whether index names inline media or a form file of the call.
func (m *callMedia) exists(index int) bool {
return index >= 0 && (index < len(m.media) || m.form != nil && index-len(m.media) < len(m.form.files))
}
// readInPart reports whether the call read only part of the text of ref.
func (m *callMedia) readInPart(ref string) bool {
mark := m.marks[ref]
return mark != nil && mark.Status == attachmentTruncated
}
// record is the attachments part of the call's record entry: every
// attachment when the call listed them, otherwise those it read, sent or
// rewrote.
func (m *callMedia) record() ([]relaycommon.RequestPluginAttachment, int) {
var items []relaycommon.RequestPluginAttachment
add := func(kind, mime, key string, reference bool) {
item := relaycommon.RequestPluginAttachment{Kind: kind, Mime: mime, Status: attachmentUnchecked}
if reference {
item.Reason = uncheckedReference
}
if mark := m.marks[key]; mark != nil {
item.Status, item.Reason, item.Rewritten = mark.Status, mark.Reason, mark.Rewritten
}
items = append(items, item)
}
if m.listed {
for _, attachment := range m.attachments {
key := attachment.Ref
if key == "" {
key = "url:" + attachment.Path
}
add(attachment.Kind, attachment.Mime, key, attachment.Ref == "")
}
} else {
// Refs number the attachments in request order.
order := slices.Clone(m.order)
slices.SortFunc(order, func(a, b string) int {
first, _ := parseMediaRef(a)
second, _ := parseMediaRef(b)
return cmp.Compare(first, second)
})
for _, ref := range order {
if item, ok := m.item(ref); ok {
add(mediaKind(item.mime, item.filename), item.mime, ref, false)
}
}
}
if len(items) > maxRecordedAttachments {
return items[:maxRecordedAttachments], len(items) - maxRecordedAttachments
}
return items, 0
}
// FetchBody returns body with each JSON string value that equals a ref of
// the call replaced by its data URL, streamed from the request. A string
// that equals the URL of a listed file marks that file as sent.
func (m *callMedia) FetchBody(body string) (io.Reader, int64, error) {
hasRefs := strings.Contains(body, `"[media:`)
urls := map[string]string{}
if m.listed {
for _, attachment := range m.attachments {
if attachment.Ref == "" && attachment.URL != "" {
urls[attachment.URL] = "url:" + attachment.Path
}
}
}
if !hasRefs && len(urls) == 0 {
return nil, 0, nil
}
type replacement struct {
start, end int
item mediaItem
}
var replacements []replacement
total := 0
var failure error
forEachJSONString(body, func(start, end int, isKey bool) {
if isKey || failure != nil {
return
}
raw := body[start+1 : end-1]
if key, ok := urls[raw]; ok {
m.mark(key, attachmentChecked, "")
return
}
item, ok := m.item(raw)
if !ok {
return
}
if len(replacements) == maxFetchMedia {
failure = fmt.Errorf("a body may send at most %d media", maxFetchMedia)
return
}
if total += item.size(); total > maxFetchMediaBytes {
failure = fmt.Errorf("the media of a body must not exceed %d bytes", maxFetchMediaBytes)
return
}
replacements = append(replacements, replacement{start: start, end: end, item: item})
m.mark(raw, attachmentChecked, "")
})
if failure != nil {
return nil, 0, failure
}
if len(replacements) == 0 {
return nil, 0, nil
}
readers := make([]io.Reader, 0, 2*len(replacements)+1)
size := int64(0)
offset := 0
for _, part := range replacements {
readers = append(readers, strings.NewReader(body[offset:part.start]))
size += int64(part.start - offset)
url, length, err := dataURL(part.item)
if err != nil {
return nil, 0, err
}
readers = append(readers, url)
size += length
offset = part.end
}
readers = append(readers, strings.NewReader(body[offset:]))
size += int64(len(body) - offset)
return io.MultiReader(readers...), size, nil
}
// dataURL streams item as a JSON string holding its data URL.
func dataURL(item mediaItem) (io.Reader, int64, error) {
header := `"data:` + safeMime(item.mime) + `;base64,`
if item.inline != nil {
data := item.inline.data
length := 0
for index := 0; index < len(data); index++ {
switch char := data[index]; {
case char == ' ' || char == '\t' || char == '\r' || char == '\n':
case 'A' <= char && char <= 'Z' || 'a' <= char && char <= 'z' || '0' <= char && char <= '9' || char == '+' || char == '/' || char == '=':
length++
default:
return nil, 0, errors.New("a media payload is not base64")
}
}
payload := io.Reader(strings.NewReader(data))
if length != len(data) {
payload = &spaceSkipper{data: data}
}
return io.MultiReader(strings.NewReader(header), payload, strings.NewReader(`"`)), int64(len(header) + length + 1), nil
}
encoded := base64.StdEncoding.EncodedLen(len(item.raw))
return io.MultiReader(strings.NewReader(header), &base64Reader{raw: item.raw}, strings.NewReader(`"`)), int64(len(header) + encoded + 1), nil
}
// safeMime is mime when it is a plain type/subtype, otherwise
// application/octet-stream: it is written into a JSON string unescaped.
func safeMime(mime string) string {
if !strings.Contains(mime, "/") {
return "application/octet-stream"
}
for index := 0; index < len(mime); index++ {
if char := mime[index]; !('a' <= char && char <= 'z' || 'A' <= char && char <= 'Z' || '0' <= char && char <= '9' || strings.IndexByte("/.+-_", char) >= 0) {
return "application/octet-stream"
}
}
return mime
}
// spaceSkipper reads data without its whitespace.
type spaceSkipper struct {
data string
}
func (s *spaceSkipper) Read(p []byte) (int, error) {
n := 0
for n < len(p) && len(s.data) > 0 {
if char := s.data[0]; char != ' ' && char != '\t' && char != '\r' && char != '\n' {
p[n] = char
n++
}
s.data = s.data[1:]
}
if n == 0 && len(s.data) == 0 {
return 0, io.EOF
}
return n, nil
}
// base64Reader reads raw as standard base64.
type base64Reader struct {
raw []byte
pending []byte
}
func (b *base64Reader) Read(p []byte) (int, error) {
if len(b.pending) == 0 {
if len(b.raw) == 0 {
return 0, io.EOF
}
chunk := b.raw[:min(len(b.raw), 3<<10)]
b.raw = b.raw[len(chunk):]
b.pending = base64.StdEncoding.AppendEncode(b.pending[:0], chunk)
}
n := copy(p, b.pending)
b.pending = b.pending[n:]
return n, nil
}
// forEachJSONString calls fn with the offsets of each string token of the
// JSON text body, quotes included, and whether it is an object key.
func forEachJSONString(body string, fn func(start, end int, isKey bool)) {
for index := 0; index < len(body); index++ {
if body[index] != '"' {
continue
}
end := index + 1
for end < len(body) && body[end] != '"' {
if body[end] == '\\' {
end++
}
end++
}
if end >= len(body) {
return
}
end++
next := end
for next < len(body) && (body[next] == ' ' || body[next] == '\t' || body[next] == '\r' || body[next] == '\n') {
next++
}
fn(index, end, next < len(body) && body[next] == ':')
index = end - 1
}
}
// rewritable reports inline media a media.text effect may rewrite: a text
// file. A PDF, a DOCX file or an image would have to be written anew, and a
// multipart file is not part of the JSON body.
func rewritable(item inlineMedia) bool {
return !item.audio && textFormat(item.mime, item.filename) == formatText
}
// mediaTextOps are the json.patch operations of a media.text effect: the
// media string replaced by text in base64, with the same mime type and
// filename.
func mediaTextOps(item inlineMedia, text string) ([]requestpatch.Operation, error) {
payload := base64.StdEncoding.EncodeToString([]byte(text))
if item.mimePath == "" {
payload = "data:" + item.mime + ";base64," + payload
}
value, err := common.Marshal(payload)
if err != nil {
return nil, err
}
return []requestpatch.Operation{{Op: "replace", Path: item.path, Value: value}}, nil
}