Documentation
¶
Overview ¶
Package assistant 是 pt-tools 内置的 AI 助手(路线图 M19):经 OpenAI 兼容的 Chat Completions 接口(官方 openai-go) 调大模型,工具是每次运行在进程内连上的 MCP 服务(internal/mcp 的同一套工具,经 App API v1 执行)。
Service 是运行、待确认操作与保留期清理的唯一所有者:
- 每个会话同时只有一次运行,运行在后台;进度经事件流(递增序号)交给网页(SSE)、App(长轮询)与 ChatOps;
- 写工具不让模型自己确认:给模型的参数里没有 confirm,每个写操作建一条待确认记录,结论只经数据库条件更新得出, 批准了才带 confirm=true 调用一次;
- 落库与下发的只有脱敏以后的内容,模型给的原始参数只在运行的内存里。
Index ¶
- Constants
- Variables
- func WithUsageSink(ctx context.Context, sink func(Usage)) context.Context
- type Actor
- type AttachmentView
- type CandidateReview
- type CaptchaRequest
- type ChatModels
- type ChatOpsHandler
- func (h *ChatOpsHandler) Enabled(ctx context.Context) bool
- func (h *ChatOpsHandler) Handle(ctx context.Context, in chatops.InboundAssistantMessage) (chatops.Reply, string)
- func (h *ChatOpsHandler) NewChat(ctx context.Context, src chatops.Source) error
- func (h *ChatOpsHandler) SetTemplates(t *notify.Templates)
- type ChatOpsReplier
- type Cipher
- type Config
- type ConfirmationView
- type ConversationDetail
- type ConversationView
- type Event
- type EventPage
- type MediaCandidate
- type MediaChoice
- type MessageView
- type ModelList
- type ReviewVerdict
- type SendOptions
- type SendResult
- type Service
- func (s *Service) Attachment(ctx context.Context, a Actor, id uint) (models.AssistantAttachment, error)
- func (s *Service) Cancel(ctx context.Context, a Actor, convID uint) error
- func (s *Service) ChatModels(ctx context.Context) (ChatModels, error)
- func (s *Service) ChooseAnswer(ctx context.Context, question string, options []string, multiple bool) ([]int, error)
- func (s *Service) ChooseMedia(ctx context.Context, torrent, siteTitle string, cands []MediaCandidate) (MediaChoice, error)
- func (s *Service) Cleanup(ctx context.Context) (int, error)
- func (s *Service) Close(ctx context.Context) error
- func (s *Service) Conversation(ctx context.Context, a Actor, id uint) (ConversationDetail, error)
- func (s *Service) Conversations(ctx context.Context, a Actor) ([]ConversationView, error)
- func (s *Service) CreateConversation(ctx context.Context, a Actor, title string) (ConversationView, error)
- func (s *Service) Decide(ctx context.Context, a Actor, id uint, approve bool) (ConfirmationView, error)
- func (s *Service) DeleteAttachment(ctx context.Context, a Actor, id uint) error
- func (s *Service) DeleteConversation(ctx context.Context, a Actor, id uint) error
- func (s *Service) Events(ctx context.Context, a Actor, convID uint, after int64, wait time.Duration) (EventPage, error)
- func (s *Service) EventsFor(ctx context.Context, a Actor, convID uint, after int64, wait time.Duration, ...) (EventPage, error)
- func (s *Service) LatestConversation(ctx context.Context, a Actor) (ConversationView, bool, error)
- func (s *Service) ListModels(ctx context.Context, in SettingsInput) (ModelList, error)
- func (s *Service) PendingConfirmation(ctx context.Context, a Actor) (ConfirmationView, bool, error)
- func (s *Service) ReadCaptcha(ctx context.Context, visionModel string, req CaptchaRequest) (string, error)
- func (s *Service) Ready(ctx context.Context) error
- func (s *Service) ReviewCandidate(ctx context.Context, in CandidateReview) (ReviewVerdict, error)
- func (s *Service) SaveSettings(ctx context.Context, in SettingsInput) (Settings, error)
- func (s *Service) Send(ctx context.Context, a Actor, convID uint, text string) (SendResult, error)
- func (s *Service) SendWith(ctx context.Context, a Actor, convID uint, o SendOptions) (SendResult, error)
- func (s *Service) Settings(ctx context.Context) (Settings, error)
- func (s *Service) Start(ctx context.Context) error
- func (s *Service) Status(ctx context.Context, a Actor) (Status, error)
- func (s *Service) TestSettings(ctx context.Context, in SettingsInput) (TestResult, error)
- func (s *Service) TestVision(ctx context.Context, visionModel string) (VisionTestResult, error)
- func (s *Service) TestVoice(ctx context.Context, in SettingsInput) (VoiceTestResult, error)
- func (s *Service) Transcribe(ctx context.Context, wav []byte, language string) (string, error)
- func (s *Service) Upload(ctx context.Context, a Actor, name string, data []byte) (AttachmentView, error)
- type Settings
- type SettingsInput
- type Status
- type TestError
- type TestResult
- type ToolCallView
- type ToolServer
- type Usage
- type UsageView
- type VisionTestResult
- type VoiceModel
- type VoiceTestResult
Constants ¶
const ( // MaxAttachments 是一条消息最多带几个附件 MaxAttachments = 4 // MaxUploadBytes 是上传一个附件的请求体上限(含 multipart 的头) MaxUploadBytes = maxImageBytes + 64<<10 )
附件:图片(PNG、JPEG、WebP、GIF,每张最多 5 MB)与文本文件(UTF-8,每个最多 200 KB),一条消息最多 4 个。 先 Upload(还没发的只有属主看得到、删得了),发消息时带上编号。文本文件存的是脱敏以后的内容(和用户写的文字一样过 userText), 图片原样存。交给模型时,这一轮的附件全给:文本拼进消息,图片用 data URI;以前的消息里的文本在历史的字数以内照给, 图片不再给、写一句占位:不能看图的模型碰到图片会拒绝整个请求,换了模型接着聊时不该被以前的图片卡住。
const ( // EventRunStarted 是一次运行开始 EventRunStarted = "run_started" // EventDelta 是模型回答的一段文字(只在内存里,最终的回答另有 EventMessage) EventDelta = "delta" // EventReasoning 是模型推理内容(思考过程)的一段,用法同 delta;只在网页的事件流里,App API 不给(契约里没有这一种) EventReasoning = "reasoning" // EventMessage 是落库的一条消息:用户的、模型的(含工具调用)、工具结果、提示 EventMessage = "message" // EventToolStarted 是开始执行一个工具 EventToolStarted = "tool_started" // EventConfirmation 是待确认操作建好了或者有了结论 EventConfirmation = "confirmation" // EventRunFinished 是运行结束:Status 是 done、canceled 或 failed EventRunFinished = "run_finished" // EventUsage 是会话的上下文用量变了(每次模型回答以后、压缩以后):Usage 是新的用量。只在网页的事件流里 EventUsage = "usage" // EventCompacting 是开始压缩上下文(写摘要要一会儿);压缩完有一条提示消息。只在网页的事件流里 EventCompacting = "compacting" )
事件的种类。
const ( RunDone = "done" RunCanceled = "canceled" RunFailed = "failed" )
运行结束的状态。
const ( DefaultMaxSteps = 8 DefaultConfirmMinutes = 10 DefaultRetentionDays = 30 // DefaultCompactPercent 是默认在上下文用到窗口的百分之几时自动压缩 DefaultCompactPercent = 80 )
设置的默认值与取值范围。
const (
// DefaultVoiceModel 是 Voltip 的默认处理方式(应用里设的预设与场景)
DefaultVoiceModel = "voltip"
)
等 Voltip 的上限:识别(加 AI 润色)一段 5 分钟的口述实测十几秒,长的、本地模型慢的要更久;测试连接只等一会儿。
const MaxVoiceBytes = 20 << 20
MaxVoiceBytes 是一段录音最大多少字节:16 kHz 单声道 16 位约 10 分钟(页面最长录 5 分钟)。
Variables ¶
var ( ErrDisabled = errors.New("AI 助手没有启用或没有配置好") ErrNotFound = errors.New("会话或操作不存在") ErrBusy = errors.New("这个会话正在回答:等它结束或先停止") ErrTooMany = errors.New("同时进行的回答太多,稍后再试") ErrInvalid = errors.New("参数不对") ErrForbidden = errors.New("没有确认改动类操作的权限") ErrDecided = errors.New("这个操作已经处理过了") ErrClosed = errors.New("AI 助手正在关闭") )
对外的错误(web 按它们选状态码)。
var Efforts = []string{"none", "minimal", "low", "medium", "high", "xhigh"}
Efforts 是可以选的推理等级(OpenAI 的 reasoning_effort):各家认的不一样,模型服务因为它拒绝请求时去掉它重试一次。
var ErrReviewUnclear = errors.New("看不出模型回答的是还是不是")
ErrReviewUnclear 是模型的回答看不出是还是不是。
var ErrVoiceDisabled = errors.New("没有配语音输入:在「AI 助手设置」里填 Voltip 的地址与访问令牌")
ErrVoiceDisabled 是没有配语音输入(Voltip 的地址或访问令牌没填)。
var TextExts = []string{
".txt", ".md", ".markdown", ".log", ".json", ".csv", ".tsv", ".yaml", ".yml", ".toml", ".ini", ".conf", ".xml",
".nfo", ".srt", ".ass",
}
TextExts 是收的文本文件扩展名(内容还要是 UTF-8)。
Functions ¶
Types ¶
type Actor ¶
type Actor struct {
Owner string
// Entry 是入口:models.AssistantEntryWeb、AssistantEntryApp、AssistantEntryChatOps
Entry string
// Name 是显示名(记进审计的参数)
Name string
// CanWrite 为假时模型拿不到写工具,也不能确认写操作
CanWrite bool
// CanConfig 为真时模型拿得到配置工具(get_config;能写时还有 change_config):网页登录、管理员绑定、有 app:config 的令牌
CanConfig bool
// Notify 不为空时,运行把消息、待确认、确认结论与结束交给它(ChatOps 经它异步回复);文字增量不给。
// 关机取消的运行不再调用它。
Notify func(ctx context.Context, ev Event)
}
Actor 是用助手的一方。Owner 是带命名空间的属主:web:<用户名>、token:<令牌编号>、device:<设备编号>、chatops:<绑定编号>; 会话一律按属主过滤,别人的会话当作不存在。
type AttachmentView ¶
type AttachmentView struct {
ID uint `json:"id"`
Name string `json:"name"`
// Kind 是 image 或 text
Kind string `json:"kind"`
MIME string `json:"mime"`
// Size 是上传的文件大小(字节)
Size int64 `json:"size"`
}
AttachmentView 是一个附件(不带内容)。
type CandidateReview ¶
type CandidateReview struct {
// Kind 是 movie 或 tv
Kind string
Title string
OriginalTitle string
Year int
// Season 是剧集的季(电影为 0)
Season int
Aliases []string
// TorrentTitle、TorrentSubtitle 是站点上的标题与副标题;Parsed 是从标题解析出的信息(一行)
TorrentTitle string
TorrentSubtitle string
Parsed string
}
CandidateReview 是要复核的一个候选:订阅的条目与站点上的种子。
type CaptchaRequest ¶
type CaptchaRequest struct {
Image []byte
MIME string
// Length 大于 0 时告诉模型一共几个字符,并按它从回答里挑
Length int
// UpperOnly 为真时告诉模型只有大写字母和数字
UpperOnly bool
}
CaptchaRequest 是一张要认的验证码。
type ChatModels ¶
type ChatModels struct {
Default string `json:"default"`
Models []string `json:"models"`
Error string `json:"error,omitempty"`
}
ChatModels 是对话页的模型下拉框:设置里的模型(Default)在第一个,后面是服务商列出的能对话的模型。读不到列表时 只有设置里的模型,Error 写明原因;列表里没有的模型名也可以手填。
type ChatOpsHandler ¶
type ChatOpsHandler struct {
// contains filtered or unexported fields
}
ChatOpsHandler 把通知渠道里绑定用户的非命令消息交给助手(实现 chatops.AssistantHandler 与 /newchat 的 AssistantService):
- 每个绑定一个滚动会话(属主 chatops:<绑定编号>),/newchat 开新的;
- 有待确认的操作时,下一条非命令消息由这里解读:「确认」经条件更新批准,别的内容一律拒绝;
- 只登记、立即返回,回答、确认提示、超时提示都由后台运行经 Replier 发出;
- 不是管理员的绑定拿不到改动类工具。
func NewChatOpsHandler ¶
func NewChatOpsHandler(svc *Service, replier ChatOpsReplier) *ChatOpsHandler
NewChatOpsHandler 建处理器。
func (*ChatOpsHandler) Enabled ¶
func (h *ChatOpsHandler) Enabled(ctx context.Context) bool
Enabled 报告助手启用了、接口配好了、ChatOps 开关也打开了。
func (*ChatOpsHandler) Handle ¶
func (h *ChatOpsHandler) Handle(ctx context.Context, in chatops.InboundAssistantMessage) (chatops.Reply, string)
Handle 处理一条非命令消息。返回立即回复与审计结果。
func (*ChatOpsHandler) SetTemplates ¶
func (h *ChatOpsHandler) SetTemplates(t *notify.Templates)
SetTemplates 接上消息模板(在接到 ChatOps 以前调用)。
type ChatOpsReplier ¶
type ChatOpsReplier interface {
Reply(ctx context.Context, msg notify.InboundMessage, reply chatops.Reply) error
}
ChatOpsReplier 是 ChatOps 的回复出口(cmd 的 liveNotifyManager:按消息来源的通道配置找到在线的通道发送)。
type Config ¶
type Config struct {
DB *gorm.DB
Cipher Cipher
Tools ToolServer
// ResolveURL 把种子下载地址解析成站点与种子编号(v2.TrackerResolver.ResolveDownloadURL):用户贴的地址与 push_torrent
// 的 torrent_url 在交给模型、建待确认以前先换掉,passkey 不出 pt-tools
ResolveURL func(rawURL string) (site, torrentID string, ok bool)
// Version 是 pt-tools 的版本(MCP 客户端的实现信息)
Version string
// Now 为空时用 time.Now(测试里换掉)
Now func() time.Time
// HTTPClient 不为空时用它调模型(测试用;设置里的代理不再生效)
HTTPClient *http.Client
// VoiceHTTPClient 不为空时调 Voltip 用它(测试里接假服务);为空时用不走代理的客户端
VoiceHTTPClient *http.Client
}
Config 是服务的依赖。
type ConfirmationView ¶
type ConfirmationView struct {
ID uint `json:"id"`
RunID string `json:"run_id"`
ToolCallID string `json:"tool_call_id"`
Tool string `json:"tool"`
// Summary 是给人看的一句话(有预览时是预览写的,例如「暂停下载器「qb」里的「Dune」」)
Summary string `json:"summary"`
Arguments map[string]any `json:"arguments"`
// Changes 是会改什么(改以前、改以后),Impact 是可能有什么影响:写工具的预览给的,没有时为空
Changes []mcp.Change `json:"changes,omitempty"`
Impact []string `json:"impact,omitempty"`
Status string `json:"status"`
DecidedBy string `json:"decided_by,omitempty"`
DecidedAt *time.Time `json:"decided_at,omitempty"`
ExpiresAt time.Time `json:"expires_at"`
CreatedAt time.Time `json:"created_at"`
}
ConfirmationView 是一个待确认操作。
type ConversationDetail ¶
type ConversationDetail struct {
ConversationView
Messages []MessageView `json:"messages"`
Confirmations []ConfirmationView `json:"confirmations"`
// RunID 是进行中的运行(没有时为空)
RunID string `json:"run_id,omitempty"`
// Partial 是进行中的运行到现在的回答文字
Partial string `json:"partial,omitempty"`
// PartialReasoning 是进行中的运行这一步到现在的推理内容(思考过程)
PartialReasoning string `json:"partial_reasoning,omitempty"`
// LastSeq 是读事件的 after:从它接着读不会漏
LastSeq int64 `json:"last_seq"`
// Usage 是上下文用量(读不出来时没有)
Usage *UsageView `json:"usage,omitempty"`
}
ConversationDetail 是一个会话:消息、待确认操作,以及接着读事件用的位置。
type ConversationView ¶
type ConversationView struct {
ID uint `json:"id"`
Title string `json:"title"`
Entry string `json:"entry"`
Running bool `json:"running"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
ConversationView 是会话列表里的一项。
type Event ¶
type Event struct {
Seq int64 `json:"seq"`
Type string `json:"type"`
RunID string `json:"run_id,omitempty"`
// Text 是 delta 与 reasoning 的文字;Replace 为真时它是这一步到现在的全部文字(脱敏改了已经发出去的部分),整段替换
Text string `json:"text,omitempty"`
Replace bool `json:"replace,omitempty"`
Message *MessageView `json:"message,omitempty"`
Tool *ToolCallView `json:"tool,omitempty"`
Confirmation *ConfirmationView `json:"confirmation,omitempty"`
// Status 是 run_finished 的结束状态
Status string `json:"status,omitempty"`
Error string `json:"error,omitempty"`
// Usage 是 usage 事件的用量
Usage *UsageView `json:"usage,omitempty"`
At time.Time `json:"at"`
}
Event 是会话事件流里的一个事件。Seq 在这个进程里递增,重启以后的序号一定比重启前的大。
type EventPage ¶
type EventPage struct {
Events []Event `json:"events"`
// Reset 为真表示中间的事件已经丢了(或者 pt-tools 重启过):重新读一遍会话,从它的 last_seq 接着读
Reset bool `json:"reset"`
// Last 是下次读的 after
Last int64 `json:"last"`
Running bool `json:"running"`
}
EventPage 是一次读事件的结果。
type MediaCandidate ¶
type MediaCandidate struct {
// Kind 是 movie 或 tv
Kind string
Title string
OriginalTitle string
Year int
Overview string
}
MediaCandidate 是让 AI 挑的一个候选条目。
type MediaChoice ¶
MediaChoice 是 AI 认的结果:Index 是选中的候选下标(从 0 开始),-1 是都不是;Reason 是模型给的理由。
type MessageView ¶
type MessageView struct {
ID uint `json:"id"`
RunID string `json:"run_id,omitempty"`
Role string `json:"role"`
Content string `json:"content"`
// Reasoning 是模型的推理内容(思考过程,只有助手消息有)
Reasoning string `json:"reasoning,omitempty"`
// Model、Effort 是这一轮的模型与推理等级(为空是设置里的模型、不指定推理等级)
Model string `json:"model,omitempty"`
Effort string `json:"effort,omitempty"`
ToolCalls []ToolCallView `json:"tool_calls,omitempty"`
// Attachments 是用户消息带的附件
Attachments []AttachmentView `json:"attachments,omitempty"`
// ToolName、ToolCallID 是工具结果对应的调用
ToolName string `json:"tool_name,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
IsError bool `json:"is_error,omitempty"`
CreatedAt time.Time `json:"created_at"`
}
MessageView 是一条消息(脱敏以后的)。
type ModelList ¶
type ModelList struct {
Models []string `json:"models"`
// Total 是服务商列出的模型数(去掉之前)
Total int `json:"total"`
}
ModelList 是服务商 GET /models 列出的模型去掉嵌入、语音、图像这类以后剩下的,给设置页的模型下拉框。 按名字判断,不保证每个都支持 Chat Completions 与工具调用(选好以后测试连接)。
type ReviewVerdict ¶
ReviewVerdict 是复核的结论:Match 为真是「是」;Reason 是模型给的理由。
type SendOptions ¶
SendOptions 是发一条消息:文字、模型(为空用设置里的)、推理等级(为空不指定,见 Efforts)与附件(先 Upload 拿到的编号)。 文字与附件至少有一样。
type SendResult ¶
type SendResult struct {
RunID string `json:"run_id"`
Message MessageView `json:"message"`
}
SendResult 是发消息的结果:后台运行的编号与落库的用户消息。
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service 是 AI 助手服务。
func (*Service) Attachment ¶
func (s *Service) Attachment(ctx context.Context, a Actor, id uint) (models.AssistantAttachment, error)
Attachment 读属主的一个附件,连同内容。
func (*Service) ChatModels ¶
func (s *Service) ChatModels(ctx context.Context) (ChatModels, error)
ChatModels 按存着的设置读对话页的模型列表(缓存 10 分钟)。AI 助手没启用时回 ErrDisabled。
func (*Service) ChooseAnswer ¶
func (s *Service) ChooseAnswer(ctx context.Context, question string, options []string, multiple bool) ([]int, error)
ChooseAnswer 让助手的模型回答签到选择题,返回选中的选项下标(从 0 开始);单选只取第一个。
func (*Service) ChooseMedia ¶
func (s *Service) ChooseMedia(ctx context.Context, torrent, siteTitle string, cands []MediaCandidate) (MediaChoice, error)
ChooseMedia 让助手的模型从候选里挑出种子对应的条目(一次不带工具的对话,最多等 30 秒)。回答写「都不是」、 第一个编号超出范围或没有编号时 Index 为 -1。助手没启用或没配好时返回 ErrDisabled(见 Ready)。
func (*Service) Cleanup ¶
Cleanup 删掉传了一天还没发的附件,再删保留期外(updated_at 早于现在减保留天数)、没有进行中运行的会话; 保留天数为 0 时不清理会话。返回删了几个会话。
func (*Service) Conversation ¶
Conversation 读一个会话:最近的消息、待确认操作与接着读事件的位置。
func (*Service) Conversations ¶
Conversations 列出属主的会话,最近的在前。
func (*Service) CreateConversation ¶
func (s *Service) CreateConversation(ctx context.Context, a Actor, title string) (ConversationView, error)
CreateConversation 建一个会话。标题为空时用第一条消息的开头。
func (*Service) Decide ¶
func (s *Service) Decide(ctx context.Context, a Actor, id uint, approve bool) (ConfirmationView, error)
Decide 是属主确认(approve 为真)或拒绝一个待确认操作。别人的当作不存在;不能写的主体回 ErrForbidden; 已经有结论的(含同时点的另一次)回 ErrDecided 与现在的状态。
func (*Service) DeleteAttachment ¶
DeleteAttachment 删掉一个还没发的附件;发出去的跟着会话删。
func (*Service) DeleteConversation ¶
DeleteConversation 删掉会话连同消息与待确认记录;正在回答的回 ErrBusy。
func (*Service) Events ¶
func (s *Service) Events(ctx context.Context, a Actor, convID uint, after int64, wait time.Duration) (EventPage, error)
Events 读会话 after 以后的事件,没有时最多等 wait(上限 30 秒)。
func (*Service) EventsFor ¶
func (s *Service) EventsFor(ctx context.Context, a Actor, convID uint, after int64, wait time.Duration, keep func(Event) bool) (EventPage, error)
EventsFor 同 Events,keep 不为空时只给它挑中的事件(App API 只给契约里有的种类):一个也没挑中时接着等到 wait。
func (*Service) LatestConversation ¶
LatestConversation 是属主最近建的会话(ChatOps 的滚动会话);没有时 ok 为假。按建的先后(编号)选,不按更新时间: 开了新会话以后,旧会话里还在跑的回答结束时会更新旧会话的时间,下一条消息也不能回到旧会话。
func (*Service) ListModels ¶
ListModels 按输入(还没保存的也行)读服务商的模型列表。Key 的规则同 TestSettings:输入没给 Key 时用存着的, 但接口地址变了就不带。模型可以还没选,接口地址要有。
func (*Service) PendingConfirmation ¶
PendingConfirmation 是属主最近一个还在等的待确认操作(ChatOps 用它解读回复);没有时 ok 为假。
func (*Service) ReadCaptcha ¶
func (s *Service) ReadCaptcha(ctx context.Context, visionModel string, req CaptchaRequest) (string, error)
ReadCaptcha 让模型读出验证码图片上的字符,只回字母和数字;读不出时回空串。visionModel 为空时用 AI 助手的模型。
func (*Service) ReviewCandidate ¶
func (s *Service) ReviewCandidate(ctx context.Context, in CandidateReview) (ReviewVerdict, error)
ReviewCandidate 让助手的模型复核一个候选种子是不是订阅要的条目(一次不带工具的对话,最多等 30 秒)。回答的第一行看不出 是还是不是时返回 ErrReviewUnclear。助手没启用或没配好时返回 ErrDisabled(见 Ready)。
func (*Service) SaveSettings ¶
SaveSettings 保存设置。
func (*Service) SendWith ¶
func (s *Service) SendWith(ctx context.Context, a Actor, convID uint, o SendOptions) (SendResult, error)
SendWith 往会话里发一条消息并在后台开始回答,立即返回。同一个会话同时只有一次运行(另一个回 ErrBusy)。
func (*Service) TestSettings ¶
func (s *Service) TestSettings(ctx context.Context, in SettingsInput) (TestResult, error)
TestSettings 按输入(还没保存的也行)发一次很短的对话,看接口地址、模型与 Key 能不能用。 输入没给 Key 时用存着的 Key,但接口地址变了就不带:存着的 Key 只发到保存它时的地址。
func (*Service) TestVision ¶
TestVision 用现画的 6 位验证码试一下看图模型(为空时用助手的模型)认不认得出。
func (*Service) TestVoice ¶
func (s *Service) TestVoice(ctx context.Context, in SettingsInput) (VoiceTestResult, error)
TestVoice 按输入(还没保存的也行)读一次 Voltip 的 GET /models,看地址与令牌能不能用。输入没给令牌时用存着的, 但地址变了就不带。
func (*Service) Transcribe ¶
Transcribe 把一段 WAV 录音交给 Voltip 识别成文字。language 不为空时用它(不然用设置里的);录音里没有说话时回空串。
type Settings ¶
type Settings struct {
Enabled bool `json:"enabled"`
Provider string `json:"provider"`
BaseURL string `json:"base_url"`
Model string `json:"model"`
// HasAPIKey 报告存没存 API Key
HasAPIKey bool `json:"has_api_key"`
ProxyURL string `json:"proxy_url"`
ChatOps bool `json:"chatops"`
MaxSteps int `json:"max_steps"`
ConfirmMinutes int `json:"confirm_minutes"`
// RetentionDays 是会话保留几天,0 表示不清理
RetentionDays int `json:"retention_days"`
// ContextWindow 是上下文窗口(token),0 是自动
ContextWindow int `json:"context_window"`
// AutoCompact 为真时上下文用到 CompactPercent% 自动压缩
AutoCompact bool `json:"auto_compact"`
CompactPercent int `json:"compact_percent"`
// 语音输入(Voltip):访问令牌只写不读(只回有没有)
VoiceBaseURL string `json:"voice_base_url"`
HasVoiceToken bool `json:"has_voice_token"`
VoiceModel string `json:"voice_model"`
VoiceLanguage string `json:"voice_language"`
}
Settings 是界面上看到的设置:API Key 只写不读(只回有没有),代理地址里的密码换成 ***。
type SettingsInput ¶
type SettingsInput struct {
Enabled bool `json:"enabled"`
Provider string `json:"provider"`
BaseURL string `json:"base_url"`
Model string `json:"model"`
APIKey *string `json:"api_key,omitempty"`
ProxyURL *string `json:"proxy_url,omitempty"`
ChatOps bool `json:"chatops"`
MaxSteps int `json:"max_steps"`
ConfirmMinutes int `json:"confirm_minutes"`
RetentionDays *int `json:"retention_days,omitempty"`
ContextWindow *int `json:"context_window,omitempty"`
AutoCompact *bool `json:"auto_compact,omitempty"`
CompactPercent *int `json:"compact_percent,omitempty"`
// 语音输入:都是 nil 时不改。VoiceToken 为空串时清除;改了地址又没给新令牌时存着的令牌清掉(同 API Key)
VoiceBaseURL *string `json:"voice_base_url,omitempty"`
VoiceToken *string `json:"voice_token,omitempty"`
VoiceModel *string `json:"voice_model,omitempty"`
VoiceLanguage *string `json:"voice_language,omitempty"`
}
SettingsInput 是保存设置的参数。APIKey、ProxyURL 为 nil 时不改,为空串时清除;ProxyURL 与读到的(密码换成 *** 的) 地址相同时也不改。改了接口地址又没给新的 API Key 时,存着的 Key 清掉:Key 不会发到保存它时以外的地址。 MaxSteps、ConfirmMinutes 为 0 时用默认值;RetentionDays、ContextWindow、AutoCompact、CompactPercent 为 nil 时不改 (ContextWindow 为 0 是自动,CompactPercent 为 0 是默认的 80)。
type Status ¶
type Status struct {
Enabled bool `json:"enabled"`
Model string `json:"model"`
CanConfirm bool `json:"can_confirm"`
// ChatOps 报告通知渠道里的非命令消息交不交给助手
ChatOps bool `json:"chatops"`
// Voice 报告配了语音输入(Voltip 的地址与访问令牌都有)。只给网页对话页:识别接口只认网页登录,App API 不回这一项
Voice bool `json:"voice,omitempty"`
}
Status 是给各入口看的状态:启用没有、用的模型、这个主体能不能确认改动类操作。不含 Key 与接口地址。
type TestResult ¶
type TestResult struct {
Model string `json:"model"`
Reply string `json:"reply"`
LatencyMs int64 `json:"latency_ms"`
}
TestResult 是测试连接的结果。
type ToolCallView ¶
type ToolCallView struct {
ID string `json:"id"`
Name string `json:"name"`
Arguments string `json:"arguments"`
// Write 报告是不是改动类操作(要确认)
Write bool `json:"write,omitempty"`
}
ToolCallView 是模型的一个工具调用:参数是规范化脱敏以后的 JSON。
type ToolServer ¶
ToolServer 建一次运行用的 MCP 服务(web.Server.AssistantToolServer):调用方固定成 c,审计通道是 assistant。
type UsageView ¶
type UsageView struct {
// ContextTokens 是下一次请求大约要带的 token:最近一次请求服务商报的输入加输出,再加上之后的消息;
// 服务商没报(或者刚压缩过)时按字数估算,Estimated 为真
ContextTokens int64 `json:"context"`
Estimated bool `json:"estimated,omitempty"`
ContextWindow int `json:"window"`
// WindowSource 是窗口从哪儿来的:setting 设置里填的,provider 服务商的模型列表,table 内置的表,default 默认的 128K
WindowSource string `json:"window_source"`
AutoCompact bool `json:"auto_compact"`
CompactPercent int `json:"compact_percent"`
// InputTokens、OutputTokens 是本会话所有模型调用(含写摘要)服务商报的输入、输出合计
InputTokens int64 `json:"input"`
OutputTokens int64 `json:"output"`
// Compacted 报告这个会话压缩过
Compacted bool `json:"compacted,omitempty"`
}
UsageView 是会话的上下文用量(单位都是 token):现在交给模型的上下文大约多少、窗口多大、到多少自动压缩,以及本会话一共用了多少。 JSON 的字段名不带 token 这个词(App API 的回应不许出现像凭证的字段名)。
type VisionTestResult ¶
type VisionTestResult struct {
Model string `json:"model"`
Expected string `json:"expected"`
Got string `json:"got"`
OK bool `json:"ok"`
Mismatch int `json:"mismatch"`
LatencyMs int64 `json:"latency_ms"`
}
VisionTestResult 是「测试看图」的结果:现画一张验证码让模型认。Mismatch 是认错了几个字(字数不对时按缺的也算)。
type VoiceModel ¶
VoiceModel 是 Voltip 列出的一种处理方式:ID 原样填进「模型」,Name 是 Voltip 给的名字(预设、场景在应用里的名称)。
type VoiceTestResult ¶
type VoiceTestResult struct {
Models []VoiceModel `json:"models"`
LatencyMs int64 `json:"latency_ms"`
}
VoiceTestResult 是测试语音输入的结果:Voltip 列出的处理方式(GET /models 的每一项都能当模型填)。