Files
new-api/relay/requestplugin/socket.go
T
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

698 lines
23 KiB
Go

package requestplugin
import (
"bytes"
"context"
"slices"
"strconv"
"strings"
"sync"
"time"
"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/constant"
"github.com/QuantumNous/new-api/logger"
"github.com/QuantumNous/new-api/pkg/jsplugin"
relaycommon "github.com/QuantumNous/new-api/relay/common"
"github.com/QuantumNous/new-api/relaykit/dto"
"github.com/QuantumNous/new-api/relaykit/types"
"github.com/QuantumNous/new-api/setting/request_plugin_setting"
"github.com/gin-gonic/gin"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
// Bounds of what a Realtime session keeps for its log.
const (
maxRealtimeRecord = 32
maxRealtimeSteps = 64
// maxRealtimeCancels bounds the response.cancel events whose upstream
// errors the session still drops.
maxRealtimeCancels = 8
)
// SocketIngress takes the execution plan of one Responses WebSocket
// response.create and runs the ingress stage on its stored body, as Ingress
// does for an HTTP request. changed reports a patched body, which the caller
// decodes again. The plan stays on c for the later stages, the reply check
// and the report.
func SocketIngress(c *gin.Context, format types.RelayFormat) (bool, []relaycommon.RequestPluginStep, []relaycommon.RequestPluginRecord, *types.NewAPIError) {
plan := NewPlan()
if plan == nil {
return false, nil, nil, nil
}
plan.socket = true
return plan.ingress(c, format, common.GetContextKeyString(c, constant.ContextKeyOriginalModel))
}
// SocketResponse is the reply check and the report capture of one response
// of a Responses WebSocket: the events of the response go to the client
// through it.
type SocketResponse struct {
writer *replyWriter
capture *responseCapture
send func([]byte)
stopErr *types.NewAPIError
}
// ResponsesSocket starts the reply check and the report capture of one
// Responses WebSocket response, once its upstream request is sent. send
// writes one event to the client. It returns nil when no instance checks the
// reply and none may report it: the session then sends every event itself.
func ResponsesSocket(c *gin.Context, info *relaycommon.RelayInfo, send func([]byte)) *SocketResponse {
plan := planOf(c)
if plan == nil || info == nil {
return nil
}
call := attemptCall(c, info)
plan.capture(c, call.Group, call.Model, call.Bound)
socket := &SocketResponse{capture: plan.response, send: send}
if plan.replies {
if instances := plan.scope(StageReply, call.Group, call.Model, call.Bound); len(instances) > 0 {
call.Format = string(types.RelayFormatOpenAIResponses)
check := newReplyCheck(c.Request.Context(), plan, instances)
check.call, check.stream = call, true
socket.writer = &replyWriter{c: c, info: info, format: types.RelayFormatOpenAIResponses, check: check, mode: replyStream, recordAt: -1, socket: socket.out}
}
}
if socket.writer == nil && socket.capture == nil {
return nil
}
return socket
}
// out sends one event to the client and keeps it for the report.
func (s *SocketResponse) out(message []byte) {
s.send(message)
if s.capture != nil {
s.capture.event(message)
}
}
// Event passes one event of the response to the client once the reply check
// released its text. After a stop it drops the event.
func (s *SocketResponse) Event(message []byte) {
w := s.writer
switch {
case w == nil || w.mode == replyPassing && !w.stopped:
s.out(message)
case !w.stopped:
w.message(message)
}
}
// Terminal checks the event that ends the response and returns it as the
// client gets it, for the session to send; the events released before it go
// out first. It returns nil when the check stopped the response.
func (s *SocketResponse) Terminal(message []byte) []byte {
if w := s.writer; w != nil && w.stopped {
return nil
} else if w != nil && w.mode != replyPassing {
// The last event released is the terminal one; the session sends it.
var last []byte
w.socket = func(data []byte) {
if last != nil {
s.out(last)
}
last = data
}
w.message(message)
if !w.stopped {
w.endStream()
}
w.socket = s.out
if w.stopped {
if last != nil {
s.out(last)
}
return nil
}
if last != nil {
message = last
}
}
if s.capture != nil {
s.capture.event(message)
}
return message
}
// Finish ends the check of a response that ended without its terminal
// event: the rest of the text is checked and the events waiting go out.
func (s *SocketResponse) Finish() {
if w := s.writer; w != nil && w.mode != replyPassing && !w.stopped {
w.endStream()
}
}
// Stopped returns the error the client gets for a response an instance
// stopped, and nil for any other.
func (s *SocketResponse) Stopped() *types.NewAPIError {
if s == nil || s.writer == nil || !s.writer.stopped {
return nil
}
if s.stopErr == nil {
s.stopErr = s.writer.stopError()
}
return s.stopErr
}
// StartRealtime takes the execution plan of a Realtime session. Its
// instances run on the session's events once the session knows its channel
// (NewRealtime).
func StartRealtime(c *gin.Context) {
if plan := NewPlan(); plan != nil {
c.Set(planContextKey, plan)
}
}
// Realtime runs the request plugins of one Realtime session: the ingress
// stage on the text the client sends, the reply check on the text of each
// response, and a report per response. The client reader calls ClientEvent
// and the upstream reader ServerEvent, at the same time.
type Realtime struct {
c *gin.Context
ctx context.Context
plan *Plan
call Call
tctx jsplugin.TransformContext
request string
ingress []*request_plugin_setting.Instance
reply []*request_plugin_setting.Instance
reports []reportTarget
toClient func([]byte)
toTarget func([]byte)
// audioSeen belongs to the client reader.
audioSeen bool
// These belong to the upstream reader.
responses map[string]*realtimeResponse
cancels []string
stopping int
mu sync.Mutex
closed bool
steps []relaycommon.RequestPluginStep
record []relaycommon.RequestPluginRecord
// pending are the entries since the last report.
pending []relaycommon.RequestPluginRecord
}
// realtimeResponse is one response of a Realtime session.
type realtimeResponse struct {
id string
started time.Time
writer *replyWriter
capture *responseCapture
stopErr *types.NewAPIError
}
// stopped returns the error the client got for a response an instance
// stopped, and nil for any other.
func (response *realtimeResponse) stopped() *types.NewAPIError {
if response.writer == nil || !response.writer.stopped {
return nil
}
if response.stopErr == nil {
response.stopErr = response.writer.stopError()
}
return response.stopErr
}
// NewRealtime starts the request plugins of a Realtime session on its
// channel. toClient and toTarget write one event to the client and to the
// upstream. It returns nil when no instance runs at ingress, checks the
// reply or may report: the session then relays its events untouched.
func NewRealtime(c *gin.Context, info *relaycommon.RelayInfo, toClient, toTarget func([]byte)) *Realtime {
plan := planOf(c)
if plan == nil || info == nil {
return nil
}
call := attemptCall(c, info)
r := &Realtime{c: c, ctx: c.Request.Context(), plan: plan, request: c.GetString(common.RequestIdKey), toClient: toClient, toTarget: toTarget, responses: map[string]*realtimeResponse{}}
r.ingress = plan.scope(StageIngress, call.Group, call.Model, call.Bound)
if plan.replies {
r.reply = plan.scope(StageReply, call.Group, call.Model, call.Bound)
}
if plan.reports {
for _, instance := range plan.scope(jsplugin.ReportHook, call.Group, call.Model, call.Bound) {
r.reports = append(r.reports, reportTarget{plugin: plan.plugins[instance.Plugin], instance: instance})
}
}
if len(r.ingress) == 0 && len(r.reply) == 0 && len(r.reports) == 0 {
return nil
}
call.Format = string(types.RelayFormatOpenAIRealtime)
r.call = call
r.tctx = jsplugin.TransformContext{Format: call.Format, Path: call.Path, Model: call.Model, UpstreamModel: call.UpstreamModel, Group: call.Group, Channel: call.Channel, User: call.User, Token: call.Token}
return r
}
// ClientEvent runs the ingress stage on a client event that carries text:
// session.update, conversation.item.create and response.create. eventType
// is the type the session already read. patched is the event with the
// instances' changes, nil when they changed nothing; dropped reports an
// event they rejected, which the client got an error event for. Any other
// event passes as it is, unread.
func (r *Realtime) ClientEvent(eventType string, message []byte) (patched []byte, dropped bool) {
if len(r.ingress) == 0 {
return nil, false
}
var members [][2]string
switch eventType {
case "input_audio_buffer.append":
if !r.audioSeen {
r.audioSeen = true
r.audioUnchecked()
}
return nil, false
case "session.update":
members = [][2]string{{"instructions", "session.instructions"}}
case "conversation.item.create":
members = [][2]string{{"input", "item"}}
case "response.create":
members = [][2]string{{"instructions", "response.instructions"}, {"input", "response.input"}}
default:
return nil, false
}
view, audio := realtimeView(message, members)
if view == nil || r.isClosed() {
return nil, false
}
call := r.call
call.Stage, call.Format, call.Body = StageIngress, string(types.RelayFormatOpenAIResponses), view
outcome := r.plan.Run(r.ctx, call)
r.add(auditSteps(outcome), outcome.Record)
apiErr := settle(r.c, outcome)
if apiErr == nil && !bytes.Equal(outcome.Body, view) {
var err error
if patched, err = realtimeWriteBack(message, outcome.Body, members, audio); err != nil {
logger.LogWarn(r.c, common.LogText("request plugin changes to a Realtime %s event were refused: %v", eventType, err))
apiErr = settle(r.c, Outcome{Failed: &Failure{Class: errorInvalidEffect, Detail: err.Error()}})
}
}
if apiErr != nil {
r.toClient(realtimeError(apiErr, gjson.GetBytes(message, "event_id").Str))
return nil, true
}
return patched, false
}
// realtimeView is the Responses-shaped body of a client event's text:
// members maps each view member to its path in the event. The audio of
// input items stays out of the view; audio holds each part it left out.
func realtimeView(message []byte, members [][2]string) ([]byte, map[string]string) {
var view []byte
var audio map[string]string
for _, member := range members {
value := gjson.GetBytes(message, member[1])
if !value.Exists() {
continue
}
raw := value.Raw
if member[0] == "input" {
if value.IsObject() {
raw = "[" + raw + "]"
}
for i, item := range gjson.Parse(raw).Array() {
for k, part := range item.Get("content").Array() {
if data := part.Get("audio"); data.Exists() {
path := strconv.Itoa(i) + ".content." + strconv.Itoa(k) + ".audio"
if audio == nil {
audio = map[string]string{}
}
audio[path] = data.Raw
raw, _ = sjson.Delete(raw, path)
}
}
}
}
if view == nil {
view = []byte("{}")
}
view, _ = sjson.SetRawBytes(view, member[0], []byte(raw))
}
return view, audio
}
// realtimeWriteBack writes the patched view members back to the client
// event, with the audio the view left out.
func realtimeWriteBack(message, patched []byte, members [][2]string, audio map[string]string) ([]byte, error) {
for _, member := range members {
value := gjson.GetBytes(patched, member[0])
if !value.Exists() {
if gjson.GetBytes(message, member[1]).Exists() {
return nil, errRealtimeMember(member[0])
}
continue
}
raw := value.Raw
if member[0] == "input" {
for path, data := range audio {
if !gjson.Get(raw, strings.TrimSuffix(path, ".audio")).IsObject() {
return nil, errRealtimeMember(member[0])
}
raw, _ = sjson.SetRaw(raw, path, data)
}
if member[1] == "item" {
// conversation.item.create holds one item.
items := gjson.Parse(raw).Array()
if len(items) != 1 {
return nil, errRealtimeMember(member[0])
}
raw = items[0].Raw
}
}
var err error
if message, err = sjson.SetRawBytes(message, member[1], []byte(raw)); err != nil {
return nil, err
}
}
return message, nil
}
type errRealtimeMember string
func (e errRealtimeMember) Error() string {
return "a Realtime event keeps the shape of its " + string(e)
}
// realtimeError is a Realtime error event for a client event the instances
// stopped, or for a response they stopped (eventID "").
func realtimeError(apiErr *types.NewAPIError, eventID string) []byte {
openAIErr := apiErr.ToOpenAIError()
errorObject := gin.H{"type": openAIErr.Type, "code": openAIErr.Code, "message": openAIErr.Message, "param": openAIErr.Param}
if eventID != "" {
errorObject["event_id"] = eventID
}
encoded, _ := common.Marshal(gin.H{"type": "error", "event_id": "event_" + common.GetUUID(), "error": errorObject})
return encoded
}
// audioUnchecked records, once per session, that the ingress instances do
// not check its audio input.
func (r *Realtime) audioUnchecked() {
steps := make([]relaycommon.RequestPluginStep, 0, len(r.ingress))
record := make([]relaycommon.RequestPluginRecord, 0, len(r.ingress))
for _, instance := range r.ingress {
step := relaycommon.RequestPluginStep{Instance: instance.ID, Name: instance.Name, Plugin: instance.Plugin, Stage: StageIngress, Action: "unchecked"}
entry := relaycommon.RequestPluginRecord{Plugin: instance.Plugin, Name: map[string]string{"en": instance.Plugin}, Stage: StageIngress, Unchecked: true, Audio: true}
if plugin := r.plan.plugins[instance.Plugin]; plugin != nil {
step.Version, entry.Name = plugin.Meta.Version, pluginName(plugin.Meta)
}
steps, record = append(steps, step), append(record, entry)
}
r.add(steps, record)
}
// add keeps steps and record entries for the session's log and its next
// report, within their bounds: entries past the bound are counted in a last
// {truncated: n}.
func (r *Realtime) add(steps []relaycommon.RequestPluginStep, record []relaycommon.RequestPluginRecord) {
if len(steps) == 0 && len(record) == 0 {
return
}
r.mu.Lock()
defer r.mu.Unlock()
r.steps = append(r.steps, steps[:min(len(steps), maxRealtimeSteps-len(r.steps))]...)
r.record = appendCapped(r.record, record)
if len(r.reports) > 0 {
r.pending = appendCapped(r.pending, record)
}
}
func appendCapped(record, entries []relaycommon.RequestPluginRecord) []relaycommon.RequestPluginRecord {
for _, entry := range entries {
switch {
case len(record) < maxRealtimeRecord:
record = append(record, entry)
case len(record) == maxRealtimeRecord:
record = append(record, relaycommon.RequestPluginRecord{Truncated: 1})
default:
record[len(record)-1].Truncated++
}
}
return record
}
// Close ends the session's plugin work and puts its steps and record on
// info, where the consume log reads them. Events after it pass untouched.
func (r *Realtime) Close(info *relaycommon.RelayInfo) {
r.mu.Lock()
defer r.mu.Unlock()
r.closed = true
info.RequestPluginSteps = append(info.RequestPluginSteps, r.steps...)
info.RequestPluginRecord = append(info.RequestPluginRecord, r.record...)
}
func (r *Realtime) isClosed() bool {
r.mu.Lock()
defer r.mu.Unlock()
return r.closed
}
// ServerEvent passes one upstream event to the client: the text of each
// response through its reply check, and the events of each response to its
// report, which runs at response.done. eventType is the type the session
// already read; usage and quota are what a response.done was billed, nil
// and 0 for any other event. Audio deltas are never read.
func (r *Realtime) ServerEvent(eventType string, message []byte, usage *dto.RealtimeUsage, quota int) {
if len(r.reply) == 0 && len(r.reports) == 0 || r.isClosed() {
r.toClient(message)
return
}
switch {
case eventType == "response.audio.delta" || eventType == "response.output_audio.delta":
// The audio of a stopped response is dropped; only then is the
// event read for its response.
if r.stopping > 0 {
if response := r.responseOf(message); response != nil && response.stopped() != nil {
return
}
}
r.toClient(message)
return
case eventType == "error" && len(r.cancels) > 0:
// An error about a response.cancel the session sent is its own.
if index := slices.Index(r.cancels, gjson.GetBytes(message, "error.event_id").Str); index >= 0 {
r.cancels = slices.Delete(r.cancels, index, index+1)
return
}
case eventType == "response.created":
response := r.start(gjson.GetBytes(message, "response.id").Str)
r.event(response, eventType, message)
return
case eventType == "response.done":
if response := r.responseOf(message); response != nil {
r.done(response, message, usage, quota)
return
}
case strings.HasPrefix(eventType, "response."):
if response := r.responseOf(message); response != nil {
r.event(response, eventType, message)
return
}
case strings.HasPrefix(eventType, "conversation.item."):
if response := r.itemResponse(message); response != nil {
r.event(response, eventType, message)
return
}
}
r.toClient(message)
}
// start begins the check and the capture of a response.
func (r *Realtime) start(id string) *realtimeResponse {
response := &realtimeResponse{id: id, started: time.Now()}
if len(r.reports) > 0 {
response.capture = &responseCapture{socket: true}
}
if len(r.reply) > 0 {
check := newReplyCheck(r.ctx, r.plan, r.reply)
check.call, check.stream = r.call, true
response.writer = &replyWriter{c: r.c, format: types.RelayFormatOpenAIRealtime, check: check, mode: replyStream, recordAt: -1,
socket: func(message []byte) { response.out(r, message) }}
}
r.responses[id] = response
return response
}
// out sends one event of a response to the client and keeps it for the
// report.
func (response *realtimeResponse) out(r *Realtime, message []byte) {
r.toClient(message)
if response.capture != nil {
response.capture.event(message)
}
}
// responseOf is the response an event belongs to. While one response runs,
// which is the common case, the event is not read for it.
func (r *Realtime) responseOf(message []byte) *realtimeResponse {
if len(r.responses) == 1 {
for _, response := range r.responses {
return response
}
}
id := gjson.GetBytes(message, "response_id").Str
if id == "" {
id = gjson.GetBytes(message, "response.id").Str
}
return r.responses[id]
}
// itemResponse is the response whose check reads the item of a
// conversation.item event: one that wrote text to it and still checks it.
func (r *Realtime) itemResponse(message []byte) *realtimeResponse {
var id string
for _, response := range r.responses {
w := response.writer
if w == nil || w.mode == replyPassing && !w.stopped {
continue
}
if id == "" {
if id = gjson.GetBytes(message, "item.id").Str; id == "" {
return nil
}
}
for _, output := range w.check.outputs {
if output.itemID == id {
return response
}
}
}
return nil
}
// event passes one event of a response through its check.
func (r *Realtime) event(response *realtimeResponse, eventType string, message []byte) {
w := response.writer
switch {
case w == nil || w.mode == replyPassing && !w.stopped:
response.out(r, message)
case w.stopped:
// An item as the conversation holds it shows what the client got.
if strings.HasPrefix(eventType, "conversation.item.") {
response.out(r, w.released(message))
}
default:
w.message(message)
if w.stopped {
r.stop(response, true)
}
}
}
// done ends a response at its response.done: the check reads the rest of
// its text, and the report gets the response.
func (r *Realtime) done(response *realtimeResponse, message []byte, usage *dto.RealtimeUsage, quota int) {
w := response.writer
switch {
case w != nil && w.stopped:
response.out(r, w.released(message))
r.stopping--
case w != nil && w.mode != replyPassing:
w.message(message)
if !w.stopped {
w.endStream()
}
if w.stopped {
r.stop(response, false)
response.out(r, w.released(message))
r.stopping--
}
default:
response.out(r, message)
}
delete(r.responses, response.id)
if w != nil {
r.add(auditSteps(Outcome{Steps: w.check.steps()}), w.check.record())
}
r.report(response, usage, quota)
}
// stop ends a response an instance stopped: the client gets an error event
// and, while the response runs, the upstream a response.cancel. Its other
// events are dropped until its response.done.
func (r *Realtime) stop(response *realtimeResponse, cancel bool) {
r.stopping++
response.out(r, realtimeError(response.stopped(), ""))
if !cancel {
return
}
id := "event_" + common.GetUUID()
if len(r.cancels) == maxRealtimeCancels {
r.cancels = r.cancels[1:]
}
r.cancels = append(r.cancels, id)
encoded, _ := common.Marshal(gin.H{"type": "response.cancel", "event_id": id, "response_id": response.id})
r.toTarget(encoded)
}
// released is a Realtime event with the text of each output the check read
// set to what the client got.
func (w *replyWriter) released(message []byte) []byte {
_, full, _ := w.check.realtimeFields(gjson.ParseBytes(message))
for _, field := range full {
if released := field.output.released.String(); released != field.text {
if next, err := sjson.SetBytes(message, field.path, released); err == nil {
message = next
}
}
}
return message
}
// report queues the report of a finished response: the events the client
// got, its usage, its quota and the entries since the last report.
func (r *Realtime) report(response *realtimeResponse, usage *dto.RealtimeUsage, quota int) {
if response.capture == nil {
return
}
capture := response.capture
kept, written := capture.kept[:len(capture.kept):len(capture.kept)], capture.written
if !reports.admit(int64(len(kept))) {
return
}
r.mu.Lock()
pending := r.pending
r.pending = nil
r.mu.Unlock()
job := &reportJob{
targets: r.reports,
tctx: r.tctx,
response: kept,
size: int64(len(kept)),
record: reportRecord{
RequestID: r.request,
StartedAt: response.started.UnixMilli(),
DurationMs: time.Since(response.started).Milliseconds(),
RequestPlugins: pending,
Response: &reportResponse{Status: 200, Stream: true, Bytes: written, Truncated: written > int64(len(kept))},
Cost: reportCost{Quota: quota, USD: float64(quota) / common.QuotaPerUnit},
},
}
job.record.User = reportUser{ID: r.call.User.ID, Username: r.call.User.Username}
if token := r.call.Token; token != nil {
job.record.Token = reportToken{ID: token.ID, Name: token.Name}
}
if usage != nil {
job.record.Usage = map[string]any{
"promptTokens": usage.InputTokens,
"completionTokens": usage.OutputTokens,
"cacheTokens": usage.InputTokenDetails.CachedTokens,
"textInput": usage.InputTokenDetails.TextTokens,
"textOutput": usage.OutputTokenDetails.TextTokens,
"audioInput": usage.InputTokenDetails.AudioTokens,
"audioOutput": usage.OutputTokenDetails.AudioTokens,
}
}
if apiErr := response.stopped(); apiErr != nil {
job.record.Error = newReportError(types.RelayFormatOpenAIRealtime, apiErr, nil)
job.record.Response.Status = apiErr.StatusCode
}
reports.push(job)
}