utils

package
v1.0.39 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 6, 2026 License: MIT Imports: 39 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SuccessCode = 1000

	PROTOCOL_VERSION    = ProtocolVersion(0b0001)
	DEFAULT_HEADER_SIZE = 0b0001

	PROTOCOL_VERSION_BITS            = 4
	HEADER_BITS                      = 4
	MESSAGE_TYPE_BITS                = 4
	MESSAGE_TYPE_SPECIFIC_FLAGS_BITS = 4
	MESSAGE_SERIALIZATION_BITS       = 4
	MESSAGE_COMPRESSION_BITS         = 4
	RESERVED_BITS                    = 8

	// Message Type:
	CLIENT_FULL_REQUEST       = MessageType(0b0001)
	CLIENT_AUDIO_ONLY_REQUEST = MessageType(0b0010)
	SERVER_FULL_RESPONSE      = MessageType(0b1001)
	SERVER_ACK                = MessageType(0b1011)
	SERVER_ERROR_RESPONSE     = MessageType(0b1111)

	// Message Type Specific Flags
	NO_SEQUENCE    = MessageTypeSpecificFlags(0b0000) // no check sequence
	POS_SEQUENCE   = MessageTypeSpecificFlags(0b0001)
	NEG_SEQUENCE   = MessageTypeSpecificFlags(0b0010)
	NEG_SEQUENCE_1 = MessageTypeSpecificFlags(0b0011)

	// Message Serialization
	NO_SERIALIZATION = SerializationType(0b0000)
	JSON             = SerializationType(0b0001)
	THRIFT           = SerializationType(0b0011)
	CUSTOM_TYPE      = SerializationType(0b1111)

	// Message Compression
	NO_COMPRESSION     = CompressionType(0b0000)
	GZIP               = CompressionType(0b0001)
	CUSTOM_COMPRESSION = CompressionType(0b1111)
)

Variables

View Source
var DefaultAudioOnlyWsHeader = []byte{0x11, 0x20, 0x11, 0x00}
View Source
var DefaultFullClientWsHeader = []byte{0x11, 0x10, 0x11, 0x00}

version: b0001 (4 bits) header size: b0001 (4 bits) message type: b0001 (Full client request) (4bits) message type specific flags: b0000 (none) (4bits) message serialization method: b0001 (JSON) (4 bits) message compression: b0001 (gzip) (4bits) reserved data: 0x00 (1 byte)

View Source
var DefaultLastAudioWsHeader = []byte{0x11, 0x22, 0x11, 0x00}

Functions

func AmrToMp3 added in v1.0.24

func AmrToMp3(amrData []byte) ([]byte, error)

func AmrToOgg

func AmrToOgg(amrData []byte) ([]byte, error)

func CheckUserChatExceed

func CheckUserChatExceed(userId string) bool

func ContainsSequence added in v1.0.25

func ContainsSequence(bits MsgTypeFlagBits) bool

ContainsSequence reports whether a message type specific flag indicates messages with this kind of flag contain a sequence number in its serialized value. This determiner function should be used for common binary

func ConvertToPNGFile

func ConvertToPNGFile(imageContent []byte) (*os.File, error)

func DecreaseUserChat

func DecreaseUserChat(userId string)

func DetectAudioFormat

func DetectAudioFormat(data []byte) string

func DetectImageFormat

func DetectImageFormat(data []byte) string

func DetectVideoMimeType

func DetectVideoMimeType(data []byte) string

func DownloadFile

func DownloadFile(url string) ([]byte, error)

func Failure

func Failure(ctx context.Context, w http.ResponseWriter, r *http.Request, code int, message string, data interface{})

func FileRecognize

func FileRecognize(audioContent []byte) (string, error)

func FileToMd5

func FileToMd5(filePath string) (string, error)

func GetAbsPath added in v1.0.24

func GetAbsPath(relPath string) string

func GetAudioData added in v1.0.25

func GetAudioData(encoding string, data []byte) ([]byte, error)

func GetAudioDataDetail added in v1.0.29

func GetAudioDataDetail(encoding string, data []byte, sampleRate int, channels int) ([]byte, error)

func GetAvailImgType added in v1.0.31

func GetAvailImgType() []string

func GetAvailRecType added in v1.0.31

func GetAvailRecType() []string

func GetAvailTTSType added in v1.0.32

func GetAvailTTSType() []string

func GetAvailTxtType added in v1.0.31

func GetAvailTxtType() []string

func GetAvailVideoType added in v1.0.31

func GetAvailVideoType() []string

func GetDefaultLLMConfig added in v1.0.31

func GetDefaultLLMConfig() string

func GetImgModel added in v1.0.31

func GetImgModel(t string) string

func GetImgType added in v1.0.31

func GetImgType(llmConf *param.LLMConfig) string

func GetLLMProxyClient

func GetLLMProxyClient() *http.Client

func GetRecType added in v1.0.31

func GetRecType(llmConf *param.LLMConfig) string

func GetRobotProxyClient

func GetRobotProxyClient() *http.Client

func GetTTSModel added in v1.0.32

func GetTTSModel(t string) string

func GetTTSType added in v1.0.32

func GetTTSType(llmConf *param.LLMConfig) string

func GetTailStartOffset added in v1.0.30

func GetTailStartOffset(filePath string, lines int) (int64, error)

func GetTxtModel added in v1.0.31

func GetTxtModel(t string) string

func GetTxtType added in v1.0.31

func GetTxtType(llmConf *param.LLMConfig) string

func GetUsingImgModel added in v1.0.31

func GetUsingImgModel(ty string, model string) string

func GetUsingRecModel added in v1.0.31

func GetUsingRecModel(ty string, model string) string

func GetUsingTTSModel added in v1.0.32

func GetUsingTTSModel(ty string, model string) string

func GetUsingTxtModel added in v1.0.31

func GetUsingTxtModel(ty string, model string) string

func GetUsingVideoModel added in v1.0.31

func GetUsingVideoModel(ty string, model string) string

func GetVideoModel added in v1.0.31

func GetVideoModel(t string) string

func GetVideoType added in v1.0.31

func GetVideoType(llmConf *param.LLMConfig) string

func HandleIncomingAudio added in v1.0.25

func HandleIncomingAudio(data []byte)

func HandleJsonBody

func HandleJsonBody(r *http.Request, v interface{}) error

func MD5

func MD5(input string) string

func MP3ToOpus added in v1.0.25

func MP3ToOpus(mp3Path string) ([]byte, error)

func MP4ToMP3 added in v1.0.33

func MP4ToMP3(mp4Bytes []byte) ([]byte, error)

func MapKeysToString

func MapKeysToString(input interface{}) string

func NormalizeHTTP added in v1.0.37

func NormalizeHTTP(addr string) string

func OGGToMP3 added in v1.0.33

func OGGToMP3(oggBytes []byte) ([]byte, error)

func PCMDuration added in v1.0.24

func PCMDuration(fileSize, sampleRate, channels, bitDepth int) int

func PCMToAMR added in v1.0.24

func PCMToAMR(pcmData []byte, sampleRate int, channels int) ([]byte, error)

func PCMToMP3 added in v1.0.24

func PCMToMP3(pcmBytes []byte, sampleRate int, channels int) ([]byte, error)

func PCMToOGG added in v1.0.24

func PCMToOGG(pcmBytes []byte, sampleRate int) ([]byte, error)

func PCMToOpus added in v1.0.24

func PCMToOpus(pcmData []byte, sampleRate int, channels int) ([]byte, error)

func ParseInt

func ParseInt(str string) int

func RandomFilename

func RandomFilename(ext string) string

func ReplaceCommand

func ReplaceCommand(content string, command string, botName string) string

func SendAudio added in v1.0.25

func SendAudio(c *websocket.Conn, sessionID string, data []byte) error

func SendAudioFromWav added in v1.0.25

func SendAudioFromWav(ctx context.Context, c *websocket.Conn, sessionID string, content []byte) error

func SendSilenceAudio added in v1.0.25

func SendSilenceAudio(c *websocket.Conn, sessionID string) error

func SetStructFieldByJSONTag

func SetStructFieldByJSONTag(s interface{}, key string, value interface{}) error

func SilkToMp3 added in v1.0.24

func SilkToMp3(silkBytes []byte) ([]byte, error)

func SilkToWav

func SilkToWav(silkBytes []byte) ([]byte, error)

func StartConnection added in v1.0.25

func StartConnection(conn *websocket.Conn) error

func StartSession added in v1.0.25

func StartSession(conn *websocket.Conn, sessionID string, req *StartSessionPayload) error

func Success

func Success(ctx context.Context, w http.ResponseWriter, r *http.Request, data interface{})

func TruncateText added in v1.0.19

func TruncateText(text string, maxBytes int) string

func Unmarshal added in v1.0.25

func Unmarshal(data []byte, containsSequence ContainsSequenceFunc) (*Message, *BinaryProtocol, error)

Unmarshal deserializes the binary `data` into a Message and also returns the Binary

func Utf16len

func Utf16len(s string) int

Utf16len calculates the length of a string in UTF-16 code units.

func ValueToString

func ValueToString(value interface{}) string

func WavToPCMBytes added in v1.0.29

func WavToPCMBytes(wavData []byte) ([]byte, error)

Types

type ASRPayload added in v1.0.25

type ASRPayload struct {
	Extra map[string]interface{} `json:"extra"`
}

type AsrClient

type AsrClient struct {
	Appid    string
	Token    string
	Cluster  string
	Workflow string
	Format   string
	Codec    string
	SegSize  int
	Url      string
}

func BuildAsrClient

func BuildAsrClient() AsrClient

func (*AsrClient) ConstructRequest

func (client *AsrClient) ConstructRequest() []byte

func (*AsrClient) RequestAsr

func (client *AsrClient) RequestAsr(audioData []byte) (AsrResponse, error)

type AsrResponse

type AsrResponse struct {
	Reqid    string   `json:"reqid"`
	Code     int      `json:"code"`
	Message  string   `json:"message"`
	Sequence int      `json:"sequence"`
	Results  []Result `json:"result,omitempty"`
}

type AudioConfig added in v1.0.25

type AudioConfig struct {
	Channel    int    `json:"channel"`
	Format     string `json:"format"`
	SampleRate int    `json:"sample_rate"`
}

type BinaryProtocol added in v1.0.25

type BinaryProtocol struct {
	// contains filtered or unexported fields
}

BinaryProtocol implements the binary protocol serialization and deserialization used in Lab-Speech MDD, TTS, ASR, etc. services. For more details, read: https://bytedance.feishu.cn/docs/doccnT0t71J4LCQCS0cnB4Eca8D

func NewBinaryProtocol added in v1.0.25

func NewBinaryProtocol() *BinaryProtocol

NewBinaryProtocol returns a new BinaryProtocol instance.

func (*BinaryProtocol) Clone added in v1.0.25

func (p *BinaryProtocol) Clone() *BinaryProtocol

Clone returns a clone of current BinaryProtocol

func (*BinaryProtocol) Compression added in v1.0.25

func (p *BinaryProtocol) Compression() CompressionBits

Compression returns the bits value of protocol compression method.

func (*BinaryProtocol) HeaderSize added in v1.0.25

func (p *BinaryProtocol) HeaderSize() int

HeaderSize returns the protocol header size.

func (*BinaryProtocol) Marshal added in v1.0.25

func (p *BinaryProtocol) Marshal(msg *Message) ([]byte, error)

Marshal serializes the message to a sequence of binary data.

func (*BinaryProtocol) Serialization added in v1.0.25

func (p *BinaryProtocol) Serialization() SerializationBits

Serialization returns the bits value of protocol serialization method.

func (*BinaryProtocol) SetCompression added in v1.0.25

func (p *BinaryProtocol) SetCompression(c CompressionBits, f CompressFunc)

SetCompression sets the compression method.

func (*BinaryProtocol) SetHeaderSize added in v1.0.25

func (p *BinaryProtocol) SetHeaderSize(s HeaderSizeBits)

SetHeaderSize sets the protocol header size.

func (*BinaryProtocol) SetSerialization added in v1.0.25

func (p *BinaryProtocol) SetSerialization(s SerializationBits)

SetSerialization sets the serialization method.

func (*BinaryProtocol) SetVersion added in v1.0.25

func (p *BinaryProtocol) SetVersion(v VersionBits)

SetVersion sets the protocol version.

func (*BinaryProtocol) Version added in v1.0.25

func (p *BinaryProtocol) Version() int

Version returns the integral version value.

type ChatTTSTextPayload added in v1.0.25

type ChatTTSTextPayload struct {
	Start   bool   `json:"start"`
	End     bool   `json:"end"`
	Content string `json:"content"`
}

type ChatTextQueryPayload added in v1.0.25

type ChatTextQueryPayload struct {
	Content string `json:"content"`
}

type CompressFunc added in v1.0.25

type CompressFunc func([]byte) ([]byte, error)

CompressFunc defines the functional type that does the compression operation.

type CompressionBits added in v1.0.25

type CompressionBits uint8

CompressionBits defines the 4-bit compression method type.

const (
	CompressionNone   CompressionBits = 0
	CompressionGzip   CompressionBits = 0b1
	CompressionCustom CompressionBits = 0b1111
)

Values that a CompressionBits variable can take.

type CompressionType

type CompressionType byte

type ContainsSequenceFunc added in v1.0.25

type ContainsSequenceFunc func(MsgTypeFlagBits) bool

ContainsSequenceFunc defines the functional type that checks whether the MsgTypeFlagBits indicates the existence of a sequence number in serialized data. The background is that not all responses contain a sequence number, and whether a response contains one depends on the the message type specific flag bits. What makes it more complicated is that this dependency varies in each use case (eg, TTS protocol has its own dependency specification, more details at: https://bytedance.feishu.cn/docs/doccn8MD4cZHQuvobbtouWfUVsV).

type ContentBlock added in v1.0.35

type ContentBlock struct {
	Type    string    // "text" or "image" or "video"
	Content string    // For Type="text"
	Media   MediaItem // For Type="media"
}

func ExtractContentBlocks added in v1.0.35

func ExtractContentBlocks(ctx context.Context, sourceText string, textFunc func(string) []string) []ContentBlock

type DialogPayload added in v1.0.25

type DialogPayload struct {
	DialogID      string                 `json:"dialog_id"`
	BotName       string                 `json:"bot_name"`
	SystemRole    string                 `json:"system_role"`
	SpeakingStyle string                 `json:"speaking_style"`
	Location      *LocationInfo          `json:"location,omitempty"`
	Extra         map[string]interface{} `json:"extra"`
}

type HeaderSizeBits added in v1.0.25

type HeaderSizeBits uint8

HeaderSizeBits defines the 4-bit header-size type.

const (
	HeaderSize4 HeaderSizeBits = iota + 1
	HeaderSize8
	HeaderSize12
	HeaderSize16
)

Values that a HeaderSizeBits variable can take.

type LocationInfo added in v1.0.25

type LocationInfo struct {
	Longitude   float64 `json:"longitude"`
	Latitude    float64 `json:"latitude"`
	City        string  `json:"city"`
	Country     string  `json:"country"`
	Province    string  `json:"province"`
	District    string  `json:"district"`
	Town        string  `json:"town"`
	CountryCode string  `json:"country_code"`
	Address     string  `json:"address"`
}

type MediaItem added in v1.0.35

type MediaItem struct {
	URL     string
	Content []byte
}

MediaItem struct holds the information for an extracted media link.

type Message added in v1.0.25

type Message struct {
	Type MsgType

	Event     int32
	SessionID string
	ConnectID string
	Sequence  int32
	ErrorCode uint32
	// Raw payload (not Gzip compressed). BinaryMarshal will do the
	// compression for you.
	Payload []byte
	// contains filtered or unexported fields
}

Message defines the general message content type.

func NewMessage added in v1.0.25

func NewMessage(msgType MsgType, typeFlag MsgTypeFlagBits) (*Message, error)

NewMessage returns a new Message instance of the given message type with the specific flag.

func NewMessageFromByte added in v1.0.25

func NewMessageFromByte(typeAndFlag byte) (*Message, error)

NewMessageFromByte reads the byte as the message type and specific flag bits and composes a new Message instance from them.

func ReceiveMessage added in v1.0.25

func ReceiveMessage(conn *websocket.Conn) (*Message, error)

*

  • 结合api接入文档对二进制协议进行理解,上下行统一理解
  • - header(4bytes)
  • - (4bits)version(v1) + (4bits)header_size
  • - (4bits)messageType + (4bits)messageTypeFlags
  • -- 0001 CompleteClient | -- 0001 optional has sequence
  • -- 0010 AudioOnlyClient | -- 0100 optional has event
  • -- 1001 CompleteServer | -- 1111 optional has error code
  • -- 1011 AudioOnlyServer | --
  • - (4bits)payloadFormat + (4bits)compression
  • - (8bits) reserve
  • - payload
  • - [optional 4 bytes] event
  • - [optional] session ID
  • -- (4 bytes)session ID len
  • -- session ID data
  • - (4 bytes)data len
  • - data

func (*Message) TypeFlag added in v1.0.25

func (m *Message) TypeFlag() MsgTypeFlagBits

TypeFlag returns the message type specific flag.

type MessageType

type MessageType byte

type MessageTypeSpecificFlags

type MessageTypeSpecificFlags byte

type MsgType added in v1.0.25

type MsgType int32

MsgType defines message type which determines how the message will be serialized with the

const (
	MsgTypeInvalid MsgType = iota
	MsgTypeFullClient
	MsgTypeAudioOnlyClient
	MsgTypeFullServer
	MsgTypeAudioOnlyServer
	MsgTypeFrontEndResultServer
	MsgTypeError

	MsgTypeServerACK = MsgTypeAudioOnlyServer
)

Values that a MsgType variable can take.

func (MsgType) String added in v1.0.25

func (t MsgType) String() string

type MsgTypeFlagBits added in v1.0.25

type MsgTypeFlagBits uint8

MsgTypeFlagBits defines the 4-bit message-type specific flags. The specific values should be defined in each specific usage scenario.

const (
	// For common
	MsgTypeFlagNoSeq       MsgTypeFlagBits = 0     // Non-terminal packet with no sequence
	MsgTypeFlagPositiveSeq MsgTypeFlagBits = 0b1   // Non-terminal packet with sequence > 0
	MsgTypeFlagLastNoSeq   MsgTypeFlagBits = 0b10  // last packet with no sequence
	MsgTypeFlagNegativeSeq MsgTypeFlagBits = 0b11  // last packet with sequence < 0
	MsgTypeFlagWithEvent   MsgTypeFlagBits = 0b100 // Payload contains event number (int32)
)

Values that a MsgTypeFlagBits variable can take.

type ProtocolVersion

type ProtocolVersion byte

type RequestAsr

type RequestAsr interface {
	// contains filtered or unexported methods
}

type Response

type Response struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data"`
	LogId   string      `json:"log_id"`
}

type Result

type Result struct {
	// required
	Text       string `json:"text"`
	Confidence int    `json:"confidence"`
	// if show_language == true
	Language string `json:"language,omitempty"`
	// if show_utterances == true
	Utterances []Utterance `json:"utterances,omitempty"`
}

type SayHelloPayload added in v1.0.25

type SayHelloPayload struct {
	Content string `json:"content"`
}

type SerializationBits added in v1.0.25

type SerializationBits uint8

SerializationBits defines the 4-bit serialization method type.

const (
	SerializationRaw    SerializationBits = 0
	SerializationJSON   SerializationBits = 0b1 << 4
	SerializationThrift SerializationBits = 0b11 << 4
	SerializationCustom SerializationBits = 0b1111 << 4
)

Values that a SerializationBits variable can take.

type SerializationType

type SerializationType byte

type StartSessionPayload added in v1.0.25

type StartSessionPayload struct {
	ASR    ASRPayload    `json:"asr"`
	TTS    TTSPayload    `json:"tts"`
	Dialog DialogPayload `json:"dialog"`
}

type TTSPayload added in v1.0.25

type TTSPayload struct {
	Speaker     string      `json:"speaker"`
	AudioConfig AudioConfig `json:"audio_config"`
}

type Usage added in v1.0.25

type Usage struct {
	InputTextTokens   int `json:"input_text_tokens"`
	InputAudioTokens  int `json:"input_audio_tokens"`
	CachedTextTokens  int `json:"cached_text_tokens"`
	CachedAudioTokens int `json:"cached_audio_tokens"`
	OutputTextTokens  int `json:"output_text_tokens"`
	OutputAudioTokens int `json:"output_audio_tokens"`
}

type UsageResponse added in v1.0.25

type UsageResponse struct {
	Usage *Usage `json:"usage"`
}

func GetDialogUsage added in v1.0.25

func GetDialogUsage(data []byte) *UsageResponse

type Utterance

type Utterance struct {
	Text      string `json:"text"`
	StartTime int    `json:"start_time"`
	EndTime   int    `json:"end_time"`
	Definite  bool   `json:"definite"`
	Words     []Word `json:"words"`
	// if show_language = true
	Language string `json:"language"`
}

type VersionBits added in v1.0.25

type VersionBits uint8

VersionBits defines the 4-bit version type.

const (
	Version1 VersionBits = (iota + 1) << 4
	Version2
	Version3
	Version4
)

Values that a VersionBits variable can take.

type Word

type Word struct {
	Text      string `json:"text"`
	StartTime int    `json:"start_time"`
	EndTime   int    `json:"end_time"`
	Pronounce string `json:"pronounce"`
	// in docs example - blank_time
	BlankDuration int `json:"blank_duration"`
}

type WsHeader

type WsHeader struct {
	ProtocolVersion          ProtocolVersion
	DefaultHeaderSize        int
	MessageType              MessageType
	MessageTypeSpecificFlags MessageTypeSpecificFlags
	SerializationType        SerializationType
	CompressionType          CompressionType
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL