Files
wbin90620 34b72da2c5 model, object: no panic on a local upstream or a completion model, each token priced once
hanzo-inc/cloud#460 (moved from #116) listed eight defects at c7f85eed. Seven
were still on main; this fixes them.

A LocalModelProvider of type Zen, Enso or Hanzo had no client, and neither did
DeepSeek with a model other than chat or reasoner, so the first call
dereferenced nil. dialect() now names each type's client and flush once, with
typed values in place of an `any` asserted in each loop, and QueryText refuses
a type with no client. That also ends the assertion panics: OpenAI completion
models asserted the answer-only flush while holding the think one, and so did
Local and DigitalOcean completion models in local.go. Both completion loops
now skip a chunk with no choice, and local.go's no longer returns a nil result
with no error when the count fails.

DeepSeek, Mistral, SiliconFlow, Baichuan and BaiduCloud priced the total token
count at both the input and the output rate. The prompt is now priced at the
input rate and the answer at the output rate. gpt-4.5's case sat after
gpt-4's, which also matches it, so gpt-4.5 billed as gpt-4; it now comes first
and bills at the rate the pricing table publishes.

GetMessagesForChats bound the chat names to a bare "{:p0}" WHERE; it is
dbx.In("chat", ...). updateErrorText's branches were inverted, so a record
with no id updated WHERE id = 0. The Guacamole handshake returned nil on a
failed write, and UploadVideo returned "", nil on each failure; both return
the error.

Item 7 (gateway frames) is left: the gateway's own errors are gateway frames
now, but its chat, models and balance fast paths still answer with cloud
frames. That needs hanzoai/gateway's side confirmed first.
2026-11-09 04:15:19 -05:00

182 lines
5.4 KiB
Go

// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
// Portions Copyright 2023 The OpenAgent Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package video
import (
"bytes"
"errors"
"log/slog"
"time"
"github.com/alibabacloud-go/tea/tea"
vod20170321 "github.com/alibabacloud-go/vod-20170321/v2/client"
"github.com/hanzoai/ai/util"
)
// errNoVodClient is returned instead of dereferencing a nil VodClient.
//
// VodClient is package-level and stays nil until SetVodClient runs, so on any
// deployment where the Aliyun VOD provider is not configured, every function
// below called straight through nil and panicked. These run inside request
// handlers, so an unconfigured optional provider took the whole process down —
// and the trigger could be as ordinary as a video record with no id.
//
// One check, named once, used by all four entry points.
var errNoVodClient = errors.New("video: VOD client not configured (SetVodClient was never called)")
func GetVideoPlayAuth(videoId string) (string, error) {
request := &vod20170321.GetVideoPlayAuthRequest{
VideoId: new(videoId),
}
if VodClient == nil {
return "", errNoVodClient
}
resp, err := VodClient.GetVideoPlayAuth(request)
if err != nil {
return "", err
}
if resp == nil || resp.Body == nil {
return "", errNoVodClient
}
playAuth := tea.StringValue(resp.Body.PlayAuth)
return playAuth, nil
}
type UploadAddress struct {
Endpoint string `json:"Endpoint"`
Bucket string `json:"Bucket"`
FileName string `json:"FileName"`
}
type UploadAuth struct {
SecurityToken string `json:"SecurityToken"`
AccessKeyId string `json:"AccessKeyId"`
ExpireUTCTime time.Time `json:"ExpireUTCTime"`
AccessKeySecret string `json:"AccessKeySecret"`
Expiration string `json:"Expiration"`
Region string `json:"Region"`
}
func UploadVideo(fileId string, filename string, fileBuffer *bytes.Buffer) (string, error) {
// https://help.aliyun.com/document_detail/476208.html
request := &vod20170321.CreateUploadVideoRequest{
FileName: new(filename),
Title: new(fileId),
}
if VodClient == nil {
return "", errNoVodClient
}
// Every failure below returns its error. They used to return "", nil, so the
// caller saw success with no id and reported only that the id was empty:
// whatever went wrong (VOD, the upload address, OSS) was never said.
resp, err := VodClient.CreateUploadVideo(request)
if err != nil {
return "", err
}
encodedUploadAddress := tea.StringValue(resp.Body.UploadAddress)
videoId := tea.StringValue(resp.Body.VideoId)
encodedUploadAuth := tea.StringValue(resp.Body.UploadAuth)
uploadAddressStr := util.DecodeBase64(encodedUploadAddress)
uploadAuthStr := util.DecodeBase64(encodedUploadAuth)
uploadAddress := &UploadAddress{}
err = util.JsonToStruct(uploadAddressStr, uploadAddress)
if err != nil {
return "", err
}
uploadAuth := &UploadAuth{}
err = util.JsonToStruct(uploadAuthStr, uploadAuth)
if err != nil {
return "", err
}
ossClient, err := getOssClient(uploadAddress.Endpoint, uploadAuth.AccessKeyId, uploadAuth.AccessKeySecret, uploadAuth.SecurityToken)
if err != nil {
return "", err
}
err = uploadLocalFile(ossClient, uploadAddress.Bucket, uploadAddress.FileName, fileBuffer)
if err != nil {
return "", err
}
return videoId, nil
}
func GetVideoCoverUrl(videoId string) string {
request := &vod20170321.GetVideoInfoRequest{
VideoId: new(videoId),
}
// No url is the answer when none could be read: an error's text is not an
// address, and a row that stores one as its url never asks again.
if VodClient == nil {
slog.Warn("vod: video info not read", "video", videoId, "err", errNoVodClient)
return ""
}
resp, err := VodClient.GetVideoInfo(request)
if err != nil {
slog.Warn("vod: video info not read", "video", videoId, "err", err)
return ""
}
// resp.Body.Video is three dereferences on a response we do not control;
// a nil anywhere in that chain is a panic, not a missing cover image.
if resp == nil || resp.Body == nil || resp.Body.Video == nil {
return ""
}
return tea.StringValue(resp.Body.Video.CoverURL)
}
func GetVideoFileUrl(videoId string) string {
request := &vod20170321.GetMezzanineInfoRequest{
VideoId: new(videoId),
}
if VodClient == nil {
slog.Warn("vod: mezzanine info not read", "video", videoId, "err", errNoVodClient)
return ""
}
resp, err := VodClient.GetMezzanineInfo(request)
if err != nil {
slog.Warn("vod: mezzanine info not read", "video", videoId, "err", err)
return ""
}
if resp == nil || resp.Body == nil || resp.Body.Mezzanine == nil {
return ""
}
// err is nil on this path, so it is not the thing to report — a mezzanine that
// carries no url is an absent file, which is what the nil checks above answer.
downloadUrl := tea.StringValue(resp.Body.Mezzanine.FileURL)
if downloadUrl == "" {
slog.Warn("vod: mezzanine carries no file url", "video", videoId)
return ""
}
return downloadUrl
}