mirror of
https://github.com/hanzoai/go-openai-realtime.git
synced 2026-09-30 21:54:42 +00:00
chore: add golangci config & make ci happy
This commit is contained in:
+351
@@ -0,0 +1,351 @@
|
|||||||
|
# This code is licensed under the terms of the MIT license https://opensource.org/license/mit
|
||||||
|
# Copyright (c) 2021 Marat Reymers
|
||||||
|
|
||||||
|
## Golden config for golangci-lint v1.61.0
|
||||||
|
#
|
||||||
|
# This is the best config for golangci-lint based on my experience and opinion.
|
||||||
|
# It is very strict, but not extremely strict.
|
||||||
|
# Feel free to adapt and change it for your needs.
|
||||||
|
|
||||||
|
run:
|
||||||
|
# Timeout for analysis, e.g. 30s, 5m.
|
||||||
|
# Default: 1m
|
||||||
|
timeout: 3m
|
||||||
|
|
||||||
|
|
||||||
|
# This file contains only configs which differ from defaults.
|
||||||
|
# All possible options can be found here https://github.com/golangci/golangci-lint/blob/master/.golangci.reference.yml
|
||||||
|
linters-settings:
|
||||||
|
cyclop:
|
||||||
|
# The maximal code complexity to report.
|
||||||
|
# Default: 10
|
||||||
|
max-complexity: 30
|
||||||
|
# The maximal average package complexity.
|
||||||
|
# If it's higher than 0.0 (float) the check is enabled
|
||||||
|
# Default: 0.0
|
||||||
|
package-average: 10.0
|
||||||
|
|
||||||
|
errcheck:
|
||||||
|
# Report about not checking of errors in type assertions: `a := b.(MyStruct)`.
|
||||||
|
# Such cases aren't reported by default.
|
||||||
|
# Default: false
|
||||||
|
check-type-assertions: true
|
||||||
|
|
||||||
|
exhaustive:
|
||||||
|
# Program elements to check for exhaustiveness.
|
||||||
|
# Default: [ switch ]
|
||||||
|
check:
|
||||||
|
- switch
|
||||||
|
- map
|
||||||
|
|
||||||
|
exhaustruct:
|
||||||
|
# List of regular expressions to exclude struct packages and their names from checks.
|
||||||
|
# Regular expressions must match complete canonical struct package/name/structname.
|
||||||
|
# Default: []
|
||||||
|
exclude:
|
||||||
|
# std libs
|
||||||
|
- "^net/http.Client$"
|
||||||
|
- "^net/http.Cookie$"
|
||||||
|
- "^net/http.Request$"
|
||||||
|
- "^net/http.Response$"
|
||||||
|
- "^net/http.Server$"
|
||||||
|
- "^net/http.Transport$"
|
||||||
|
- "^net/url.URL$"
|
||||||
|
- "^os/exec.Cmd$"
|
||||||
|
- "^reflect.StructField$"
|
||||||
|
# public libs
|
||||||
|
- "^github.com/Shopify/sarama.Config$"
|
||||||
|
- "^github.com/Shopify/sarama.ProducerMessage$"
|
||||||
|
- "^github.com/mitchellh/mapstructure.DecoderConfig$"
|
||||||
|
- "^github.com/prometheus/client_golang/.+Opts$"
|
||||||
|
- "^github.com/spf13/cobra.Command$"
|
||||||
|
- "^github.com/spf13/cobra.CompletionOptions$"
|
||||||
|
- "^github.com/stretchr/testify/mock.Mock$"
|
||||||
|
- "^github.com/testcontainers/testcontainers-go.+Request$"
|
||||||
|
- "^github.com/testcontainers/testcontainers-go.FromDockerfile$"
|
||||||
|
- "^golang.org/x/tools/go/analysis.Analyzer$"
|
||||||
|
- "^google.golang.org/protobuf/.+Options$"
|
||||||
|
- "^gopkg.in/yaml.v3.Node$"
|
||||||
|
|
||||||
|
funlen:
|
||||||
|
# Checks the number of lines in a function.
|
||||||
|
# If lower than 0, disable the check.
|
||||||
|
# Default: 60
|
||||||
|
lines: 100
|
||||||
|
# Checks the number of statements in a function.
|
||||||
|
# If lower than 0, disable the check.
|
||||||
|
# Default: 40
|
||||||
|
statements: 50
|
||||||
|
# Ignore comments when counting lines.
|
||||||
|
# Default false
|
||||||
|
ignore-comments: true
|
||||||
|
|
||||||
|
gocognit:
|
||||||
|
# Minimal code complexity to report.
|
||||||
|
# Default: 30 (but we recommend 10-20)
|
||||||
|
min-complexity: 20
|
||||||
|
|
||||||
|
gocritic:
|
||||||
|
# Settings passed to gocritic.
|
||||||
|
# The settings key is the name of a supported gocritic checker.
|
||||||
|
# The list of supported checkers can be find in https://go-critic.github.io/overview.
|
||||||
|
settings:
|
||||||
|
captLocal:
|
||||||
|
# Whether to restrict checker to params only.
|
||||||
|
# Default: true
|
||||||
|
paramsOnly: false
|
||||||
|
underef:
|
||||||
|
# Whether to skip (*x).method() calls where x is a pointer receiver.
|
||||||
|
# Default: true
|
||||||
|
skipRecvDeref: false
|
||||||
|
|
||||||
|
gomodguard:
|
||||||
|
blocked:
|
||||||
|
# List of blocked modules.
|
||||||
|
# Default: []
|
||||||
|
modules:
|
||||||
|
- github.com/golang/protobuf:
|
||||||
|
recommendations:
|
||||||
|
- google.golang.org/protobuf
|
||||||
|
reason: "see https://developers.google.com/protocol-buffers/docs/reference/go/faq#modules"
|
||||||
|
- github.com/satori/go.uuid:
|
||||||
|
recommendations:
|
||||||
|
- github.com/google/uuid
|
||||||
|
reason: "satori's package is not maintained"
|
||||||
|
- github.com/gofrs/uuid:
|
||||||
|
recommendations:
|
||||||
|
- github.com/gofrs/uuid/v5
|
||||||
|
reason: "gofrs' package was not go module before v5"
|
||||||
|
|
||||||
|
govet:
|
||||||
|
# Enable all analyzers.
|
||||||
|
# Default: false
|
||||||
|
enable-all: true
|
||||||
|
# Disable analyzers by name.
|
||||||
|
# Run `go tool vet help` to see all analyzers.
|
||||||
|
# Default: []
|
||||||
|
disable:
|
||||||
|
- fieldalignment # too strict
|
||||||
|
# Settings per analyzer.
|
||||||
|
settings:
|
||||||
|
shadow:
|
||||||
|
# Whether to be strict about shadowing; can be noisy.
|
||||||
|
# Default: false
|
||||||
|
strict: true
|
||||||
|
|
||||||
|
inamedparam:
|
||||||
|
# Skips check for interface methods with only a single parameter.
|
||||||
|
# Default: false
|
||||||
|
skip-single-param: true
|
||||||
|
|
||||||
|
mnd:
|
||||||
|
# List of function patterns to exclude from analysis.
|
||||||
|
# Values always ignored: `time.Date`,
|
||||||
|
# `strconv.FormatInt`, `strconv.FormatUint`, `strconv.FormatFloat`,
|
||||||
|
# `strconv.ParseInt`, `strconv.ParseUint`, `strconv.ParseFloat`.
|
||||||
|
# Default: []
|
||||||
|
ignored-functions:
|
||||||
|
- args.Error
|
||||||
|
- flag.Arg
|
||||||
|
- flag.Duration.*
|
||||||
|
- flag.Float.*
|
||||||
|
- flag.Int.*
|
||||||
|
- flag.Uint.*
|
||||||
|
- os.Chmod
|
||||||
|
- os.Mkdir.*
|
||||||
|
- os.OpenFile
|
||||||
|
- os.WriteFile
|
||||||
|
- prometheus.ExponentialBuckets.*
|
||||||
|
- prometheus.LinearBuckets
|
||||||
|
|
||||||
|
nakedret:
|
||||||
|
# Make an issue if func has more lines of code than this setting, and it has naked returns.
|
||||||
|
# Default: 30
|
||||||
|
max-func-lines: 0
|
||||||
|
|
||||||
|
nolintlint:
|
||||||
|
# Exclude following linters from requiring an explanation.
|
||||||
|
# Default: []
|
||||||
|
allow-no-explanation: [ funlen, gocognit, lll ]
|
||||||
|
# Enable to require an explanation of nonzero length after each nolint directive.
|
||||||
|
# Default: false
|
||||||
|
require-explanation: true
|
||||||
|
# Enable to require nolint directives to mention the specific linter being suppressed.
|
||||||
|
# Default: false
|
||||||
|
require-specific: true
|
||||||
|
|
||||||
|
perfsprint:
|
||||||
|
# Optimizes into strings concatenation.
|
||||||
|
# Default: true
|
||||||
|
strconcat: false
|
||||||
|
|
||||||
|
rowserrcheck:
|
||||||
|
# database/sql is always checked
|
||||||
|
# Default: []
|
||||||
|
packages:
|
||||||
|
- github.com/jmoiron/sqlx
|
||||||
|
|
||||||
|
sloglint:
|
||||||
|
# Enforce not using global loggers.
|
||||||
|
# Values:
|
||||||
|
# - "": disabled
|
||||||
|
# - "all": report all global loggers
|
||||||
|
# - "default": report only the default slog logger
|
||||||
|
# https://github.com/go-simpler/sloglint?tab=readme-ov-file#no-global
|
||||||
|
# Default: ""
|
||||||
|
no-global: "all"
|
||||||
|
# Enforce using methods that accept a context.
|
||||||
|
# Values:
|
||||||
|
# - "": disabled
|
||||||
|
# - "all": report all contextless calls
|
||||||
|
# - "scope": report only if a context exists in the scope of the outermost function
|
||||||
|
# https://github.com/go-simpler/sloglint?tab=readme-ov-file#context-only
|
||||||
|
# Default: ""
|
||||||
|
context: "scope"
|
||||||
|
|
||||||
|
tenv:
|
||||||
|
# The option `all` will run against whole test files (`_test.go`) regardless of method/function signatures.
|
||||||
|
# Otherwise, only methods that take `*testing.T`, `*testing.B`, and `testing.TB` as arguments are checked.
|
||||||
|
# Default: false
|
||||||
|
all: true
|
||||||
|
|
||||||
|
|
||||||
|
linters:
|
||||||
|
disable-all: true
|
||||||
|
enable:
|
||||||
|
## enabled by default
|
||||||
|
- errcheck # checking for unchecked errors, these unchecked errors can be critical bugs in some cases
|
||||||
|
- gosimple # specializes in simplifying a code
|
||||||
|
- govet # reports suspicious constructs, such as Printf calls whose arguments do not align with the format string
|
||||||
|
- ineffassign # detects when assignments to existing variables are not used
|
||||||
|
- staticcheck # is a go vet on steroids, applying a ton of static analysis checks
|
||||||
|
- typecheck # like the front-end of a Go compiler, parses and type-checks Go code
|
||||||
|
- unused # checks for unused constants, variables, functions and types
|
||||||
|
## disabled by default
|
||||||
|
- asasalint # checks for pass []any as any in variadic func(...any)
|
||||||
|
- asciicheck # checks that your code does not contain non-ASCII identifiers
|
||||||
|
- bidichk # checks for dangerous unicode character sequences
|
||||||
|
- bodyclose # checks whether HTTP response body is closed successfully
|
||||||
|
# - canonicalheader # checks whether net/http.Header uses canonical header
|
||||||
|
- copyloopvar # detects places where loop variables are copied (Go 1.22+)
|
||||||
|
- cyclop # checks function and package cyclomatic complexity
|
||||||
|
- dupl # tool for code clone detection
|
||||||
|
- durationcheck # checks for two durations multiplied together
|
||||||
|
- errname # checks that sentinel errors are prefixed with the Err and error types are suffixed with the Error
|
||||||
|
- errorlint # finds code that will cause problems with the error wrapping scheme introduced in Go 1.13
|
||||||
|
- exhaustive # checks exhaustiveness of enum switch statements
|
||||||
|
- fatcontext # detects nested contexts in loops
|
||||||
|
- forbidigo # forbids identifiers
|
||||||
|
- funlen # tool for detection of long functions
|
||||||
|
- gocheckcompilerdirectives # validates go compiler directive comments (//go:)
|
||||||
|
- gochecknoglobals # checks that no global variables exist
|
||||||
|
- gochecknoinits # checks that no init functions are present in Go code
|
||||||
|
- gochecksumtype # checks exhaustiveness on Go "sum types"
|
||||||
|
- gocognit # computes and checks the cognitive complexity of functions
|
||||||
|
- goconst # finds repeated strings that could be replaced by a constant
|
||||||
|
- gocritic # provides diagnostics that check for bugs, performance and style issues
|
||||||
|
- gocyclo # computes and checks the cyclomatic complexity of functions
|
||||||
|
- godot # checks if comments end in a period
|
||||||
|
- goimports # in addition to fixing imports, goimports also formats your code in the same style as gofmt
|
||||||
|
- gomoddirectives # manages the use of 'replace', 'retract', and 'excludes' directives in go.mod
|
||||||
|
- gomodguard # allow and block lists linter for direct Go module dependencies. This is different from depguard where there are different block types for example version constraints and module recommendations
|
||||||
|
- goprintffuncname # checks that printf-like functions are named with f at the end
|
||||||
|
- gosec # inspects source code for security problems
|
||||||
|
- intrange # finds places where for loops could make use of an integer range
|
||||||
|
# - lll # reports long lines
|
||||||
|
- loggercheck # checks key value pairs for common logger libraries (kitlog,klog,logr,zap)
|
||||||
|
- makezero # finds slice declarations with non-zero initial length
|
||||||
|
- mirror # reports wrong mirror patterns of bytes/strings usage
|
||||||
|
- mnd # detects magic numbers
|
||||||
|
- musttag # enforces field tags in (un)marshaled structs
|
||||||
|
- nakedret # finds naked returns in functions greater than a specified function length
|
||||||
|
- nestif # reports deeply nested if statements
|
||||||
|
- nilerr # finds the code that returns nil even if it checks that the error is not nil
|
||||||
|
- nilnil # checks that there is no simultaneous return of nil error and an invalid value
|
||||||
|
- noctx # finds sending http request without context.Context
|
||||||
|
- nolintlint # reports ill-formed or insufficient nolint directives
|
||||||
|
- nonamedreturns # reports all named returns
|
||||||
|
- nosprintfhostport # checks for misuse of Sprintf to construct a host with port in a URL
|
||||||
|
- perfsprint # checks that fmt.Sprintf can be replaced with a faster alternative
|
||||||
|
- predeclared # finds code that shadows one of Go's predeclared identifiers
|
||||||
|
- promlinter # checks Prometheus metrics naming via promlint
|
||||||
|
- protogetter # reports direct reads from proto message fields when getters should be used
|
||||||
|
- reassign # checks that package variables are not reassigned
|
||||||
|
- revive # fast, configurable, extensible, flexible, and beautiful linter for Go, drop-in replacement of golint
|
||||||
|
- rowserrcheck # checks whether Err of rows is checked successfully
|
||||||
|
- sloglint # ensure consistent code style when using log/slog
|
||||||
|
- spancheck # checks for mistakes with OpenTelemetry/Census spans
|
||||||
|
- sqlclosecheck # checks that sql.Rows and sql.Stmt are closed
|
||||||
|
- stylecheck # is a replacement for golint
|
||||||
|
- tenv # detects using os.Setenv instead of t.Setenv since Go1.17
|
||||||
|
- testableexamples # checks if examples are testable (have an expected output)
|
||||||
|
- testifylint # checks usage of github.com/stretchr/testify
|
||||||
|
- testpackage # makes you use a separate _test package
|
||||||
|
- tparallel # detects inappropriate usage of t.Parallel() method in your Go test codes
|
||||||
|
- unconvert # removes unnecessary type conversions
|
||||||
|
- unparam # reports unused function parameters
|
||||||
|
- usestdlibvars # detects the possibility to use variables/constants from the Go standard library
|
||||||
|
- wastedassign # finds wasted assignment statements
|
||||||
|
- whitespace # detects leading and trailing whitespace
|
||||||
|
|
||||||
|
## you may want to enable
|
||||||
|
#- decorder # checks declaration order and count of types, constants, variables and functions
|
||||||
|
#- exhaustruct # [highly recommend to enable] checks if all structure fields are initialized
|
||||||
|
#- gci # controls golang package import order and makes it always deterministic
|
||||||
|
#- ginkgolinter # [if you use ginkgo/gomega] enforces standards of using ginkgo and gomega
|
||||||
|
#- godox # detects FIXME, TODO and other comment keywords
|
||||||
|
#- goheader # checks is file header matches to pattern
|
||||||
|
#- inamedparam # [great idea, but too strict, need to ignore a lot of cases by default] reports interfaces with unnamed method parameters
|
||||||
|
#- interfacebloat # checks the number of methods inside an interface
|
||||||
|
#- ireturn # accept interfaces, return concrete types
|
||||||
|
#- prealloc # [premature optimization, but can be used in some cases] finds slice declarations that could potentially be preallocated
|
||||||
|
#- tagalign # checks that struct tags are well aligned
|
||||||
|
#- varnamelen # [great idea, but too many false positives] checks that the length of a variable's name matches its scope
|
||||||
|
#- wrapcheck # checks that errors returned from external packages are wrapped
|
||||||
|
#- zerologlint # detects the wrong usage of zerolog that a user forgets to dispatch zerolog.Event
|
||||||
|
|
||||||
|
## disabled
|
||||||
|
#- containedctx # detects struct contained context.Context field
|
||||||
|
#- contextcheck # [too many false positives] checks the function whether use a non-inherited context
|
||||||
|
#- depguard # [replaced by gomodguard] checks if package imports are in a list of acceptable packages
|
||||||
|
#- dogsled # checks assignments with too many blank identifiers (e.g. x, _, _, _, := f())
|
||||||
|
#- dupword # [useless without config] checks for duplicate words in the source code
|
||||||
|
#- err113 # [too strict] checks the errors handling expressions
|
||||||
|
#- errchkjson # [don't see profit + I'm against of omitting errors like in the first example https://github.com/breml/errchkjson] checks types passed to the json encoding functions. Reports unsupported types and optionally reports occasions, where the check for the returned error can be omitted
|
||||||
|
#- execinquery # [deprecated] checks query string in Query function which reads your Go src files and warning it finds
|
||||||
|
#- exportloopref # [not necessary from Go 1.22] checks for pointers to enclosing loop variables
|
||||||
|
#- forcetypeassert # [replaced by errcheck] finds forced type assertions
|
||||||
|
#- gofmt # [replaced by goimports] checks whether code was gofmt-ed
|
||||||
|
#- gofumpt # [replaced by goimports, gofumports is not available yet] checks whether code was gofumpt-ed
|
||||||
|
#- gosmopolitan # reports certain i18n/l10n anti-patterns in your Go codebase
|
||||||
|
#- grouper # analyzes expression groups
|
||||||
|
#- importas # enforces consistent import aliases
|
||||||
|
#- maintidx # measures the maintainability index of each function
|
||||||
|
#- misspell # [useless] finds commonly misspelled English words in comments
|
||||||
|
#- nlreturn # [too strict and mostly code is not more readable] checks for a new line before return and branch statements to increase code clarity
|
||||||
|
#- paralleltest # [too many false positives] detects missing usage of t.Parallel() method in your Go test
|
||||||
|
#- tagliatelle # checks the struct tags
|
||||||
|
#- thelper # detects golang test helpers without t.Helper() call and checks the consistency of test helpers
|
||||||
|
#- wsl # [too strict and mostly code is not more readable] whitespace linter forces you to use empty lines
|
||||||
|
|
||||||
|
|
||||||
|
issues:
|
||||||
|
# Maximum count of issues with the same text.
|
||||||
|
# Set to 0 to disable.
|
||||||
|
# Default: 3
|
||||||
|
max-same-issues: 50
|
||||||
|
|
||||||
|
exclude-rules:
|
||||||
|
- source: "(noinspection|TODO)"
|
||||||
|
linters: [ godot ]
|
||||||
|
- source: "//noinspection"
|
||||||
|
linters: [ gocritic ]
|
||||||
|
- path: "_test\\.go"
|
||||||
|
linters:
|
||||||
|
- bodyclose
|
||||||
|
- dupl
|
||||||
|
- funlen
|
||||||
|
- goconst
|
||||||
|
- gosec
|
||||||
|
- noctx
|
||||||
|
- wrapcheck
|
||||||
@@ -31,7 +31,7 @@ func NewClientWithConfig(config ClientConfig) *Client {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) getUrl(model string) string {
|
func (c *Client) getURL(model string) string {
|
||||||
query := url.Values{}
|
query := url.Values{}
|
||||||
|
|
||||||
if c.config.APIType == APITypeAzure {
|
if c.config.APIType == APITypeAzure {
|
||||||
@@ -110,10 +110,10 @@ func (c *Client) Connect(ctx context.Context, opts ...ConnectOption) (*Conn, err
|
|||||||
}
|
}
|
||||||
|
|
||||||
// get url by model
|
// get url by model
|
||||||
url := c.getUrl(connectOpts.model)
|
url := c.getURL(connectOpts.model)
|
||||||
|
|
||||||
// dial
|
// dial
|
||||||
conn, _, err := websocket.Dial(ctx, url, connectOpts.dialOptions)
|
conn, _, err := websocket.Dial(ctx, url, connectOpts.dialOptions) //nolint:bodyclose // resp.Body is nil
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
+39
-39
@@ -7,7 +7,7 @@ import (
|
|||||||
openairt "github.com/WqyJh/go-openai-realtime"
|
openairt "github.com/WqyJh/go-openai-realtime"
|
||||||
"github.com/sashabaranov/go-openai"
|
"github.com/sashabaranov/go-openai"
|
||||||
"github.com/sashabaranov/go-openai/jsonschema"
|
"github.com/sashabaranov/go-openai/jsonschema"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSessionUpdateEvent(t *testing.T) {
|
func TestSessionUpdateEvent(t *testing.T) {
|
||||||
@@ -61,7 +61,7 @@ func TestSessionUpdateEvent(t *testing.T) {
|
|||||||
|
|
||||||
// data, err := json.MarshalIndent(message, "", "\t")
|
// data, err := json.MarshalIndent(message, "", "\t")
|
||||||
data, err := json.Marshal(message)
|
data, err := json.Marshal(message)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
expected := `{
|
expected := `{
|
||||||
"event_id": "test-id",
|
"event_id": "test-id",
|
||||||
"session": {
|
"session": {
|
||||||
@@ -111,11 +111,11 @@ func TestSessionUpdateEvent(t *testing.T) {
|
|||||||
},
|
},
|
||||||
"type": "session.update"
|
"type": "session.update"
|
||||||
}`
|
}`
|
||||||
assert.JSONEq(t, expected, string(data))
|
require.JSONEq(t, expected, string(data))
|
||||||
|
|
||||||
message.Session.MaxOutputTokens = 0
|
message.Session.MaxOutputTokens = 0
|
||||||
data, err = json.Marshal(message)
|
data, err = json.Marshal(message)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
expected = `{
|
expected = `{
|
||||||
"event_id": "test-id",
|
"event_id": "test-id",
|
||||||
"session": {
|
"session": {
|
||||||
@@ -164,7 +164,7 @@ func TestSessionUpdateEvent(t *testing.T) {
|
|||||||
},
|
},
|
||||||
"type": "session.update"
|
"type": "session.update"
|
||||||
}`
|
}`
|
||||||
assert.JSONEq(t, expected, string(data))
|
require.JSONEq(t, expected, string(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSessionUpdateEventSimple(t *testing.T) {
|
func TestSessionUpdateEventSimple(t *testing.T) {
|
||||||
@@ -187,7 +187,7 @@ func TestSessionUpdateEventSimple(t *testing.T) {
|
|||||||
|
|
||||||
// data, err := json.MarshalIndent(message, "", "\t")
|
// data, err := json.MarshalIndent(message, "", "\t")
|
||||||
data, err := json.Marshal(message)
|
data, err := json.Marshal(message)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
expected := `{
|
expected := `{
|
||||||
"session": {
|
"session": {
|
||||||
"modalities": [
|
"modalities": [
|
||||||
@@ -203,7 +203,7 @@ func TestSessionUpdateEventSimple(t *testing.T) {
|
|||||||
},
|
},
|
||||||
"type": "session.update"
|
"type": "session.update"
|
||||||
}`
|
}`
|
||||||
assert.JSONEq(t, expected, string(data))
|
require.JSONEq(t, expected, string(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInputAudioBufferAppendEvent(t *testing.T) {
|
func TestInputAudioBufferAppendEvent(t *testing.T) {
|
||||||
@@ -211,43 +211,43 @@ func TestInputAudioBufferAppendEvent(t *testing.T) {
|
|||||||
Audio: "test-audio",
|
Audio: "test-audio",
|
||||||
}
|
}
|
||||||
data, err := json.Marshal(message)
|
data, err := json.Marshal(message)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
expected := `{"audio":"test-audio","type":"input_audio_buffer.append"}`
|
expected := `{"audio":"test-audio","type":"input_audio_buffer.append"}`
|
||||||
assert.JSONEq(t, expected, string(data))
|
require.JSONEq(t, expected, string(data))
|
||||||
|
|
||||||
message.EventBase.EventID = "test-id"
|
message.EventBase.EventID = "test-id"
|
||||||
data, err = json.Marshal(message)
|
data, err = json.Marshal(message)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
expected = `{"event_id":"test-id","audio":"test-audio","type":"input_audio_buffer.append"}`
|
expected = `{"event_id":"test-id","audio":"test-audio","type":"input_audio_buffer.append"}`
|
||||||
assert.JSONEq(t, expected, string(data))
|
require.JSONEq(t, expected, string(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInputAudioBufferCommitEvent(t *testing.T) {
|
func TestInputAudioBufferCommitEvent(t *testing.T) {
|
||||||
message := openairt.InputAudioBufferCommitEvent{}
|
message := openairt.InputAudioBufferCommitEvent{}
|
||||||
data, err := json.Marshal(message)
|
data, err := json.Marshal(message)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
expected := `{"type":"input_audio_buffer.commit"}`
|
expected := `{"type":"input_audio_buffer.commit"}`
|
||||||
assert.JSONEq(t, expected, string(data))
|
require.JSONEq(t, expected, string(data))
|
||||||
|
|
||||||
message.EventBase.EventID = "test-id"
|
message.EventBase.EventID = "test-id"
|
||||||
data, err = json.Marshal(message)
|
data, err = json.Marshal(message)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
expected = `{"event_id":"test-id","type":"input_audio_buffer.commit"}`
|
expected = `{"event_id":"test-id","type":"input_audio_buffer.commit"}`
|
||||||
assert.JSONEq(t, expected, string(data))
|
require.JSONEq(t, expected, string(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInputAudioBufferClearEvent(t *testing.T) {
|
func TestInputAudioBufferClearEvent(t *testing.T) {
|
||||||
message := openairt.InputAudioBufferClearEvent{}
|
message := openairt.InputAudioBufferClearEvent{}
|
||||||
data, err := json.Marshal(message)
|
data, err := json.Marshal(message)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
expected := `{"type":"input_audio_buffer.clear"}`
|
expected := `{"type":"input_audio_buffer.clear"}`
|
||||||
assert.JSONEq(t, expected, string(data))
|
require.JSONEq(t, expected, string(data))
|
||||||
|
|
||||||
message.EventBase.EventID = "test-id"
|
message.EventBase.EventID = "test-id"
|
||||||
data, err = json.Marshal(message)
|
data, err = json.Marshal(message)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
expected = `{"event_id":"test-id","type":"input_audio_buffer.clear"}`
|
expected = `{"event_id":"test-id","type":"input_audio_buffer.clear"}`
|
||||||
assert.JSONEq(t, expected, string(data))
|
require.JSONEq(t, expected, string(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConversationItemCreateEvent(t *testing.T) {
|
func TestConversationItemCreateEvent(t *testing.T) {
|
||||||
@@ -266,7 +266,7 @@ func TestConversationItemCreateEvent(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
data, err := json.MarshalIndent(message, "", "\t")
|
data, err := json.MarshalIndent(message, "", "\t")
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
expected := `{
|
expected := `{
|
||||||
"previous_item_id": "test-previous-item-id",
|
"previous_item_id": "test-previous-item-id",
|
||||||
"item": {
|
"item": {
|
||||||
@@ -291,11 +291,11 @@ func TestConversationItemCreateEvent(t *testing.T) {
|
|||||||
},
|
},
|
||||||
"type": "conversation.item.create"
|
"type": "conversation.item.create"
|
||||||
}`
|
}`
|
||||||
assert.JSONEq(t, expected, string(data))
|
require.JSONEq(t, expected, string(data))
|
||||||
|
|
||||||
message.EventBase.EventID = "test-id"
|
message.EventBase.EventID = "test-id"
|
||||||
data, err = json.Marshal(message)
|
data, err = json.Marshal(message)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
expected = `{
|
expected = `{
|
||||||
"event_id": "test-id",
|
"event_id": "test-id",
|
||||||
"previous_item_id": "test-previous-item-id",
|
"previous_item_id": "test-previous-item-id",
|
||||||
@@ -321,7 +321,7 @@ func TestConversationItemCreateEvent(t *testing.T) {
|
|||||||
},
|
},
|
||||||
"type": "conversation.item.create"
|
"type": "conversation.item.create"
|
||||||
}`
|
}`
|
||||||
assert.JSONEq(t, expected, string(data))
|
require.JSONEq(t, expected, string(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConversationItemTruncateEvent(t *testing.T) {
|
func TestConversationItemTruncateEvent(t *testing.T) {
|
||||||
@@ -331,30 +331,30 @@ func TestConversationItemTruncateEvent(t *testing.T) {
|
|||||||
AudioEndMs: 2000,
|
AudioEndMs: 2000,
|
||||||
}
|
}
|
||||||
data, err := json.Marshal(message)
|
data, err := json.Marshal(message)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
expected := `{"item_id":"test-item-id","content_index":1,"audio_end_ms":2000,"type":"conversation.item.truncate"}`
|
expected := `{"item_id":"test-item-id","content_index":1,"audio_end_ms":2000,"type":"conversation.item.truncate"}`
|
||||||
assert.JSONEq(t, expected, string(data))
|
require.JSONEq(t, expected, string(data))
|
||||||
|
|
||||||
message.EventBase.EventID = "test-id"
|
message.EventBase.EventID = "test-id"
|
||||||
data, err = json.Marshal(message)
|
data, err = json.Marshal(message)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
expected = `{"event_id":"test-id","item_id":"test-item-id","content_index":1,"audio_end_ms":2000,"type":"conversation.item.truncate"}`
|
expected = `{"event_id":"test-id","item_id":"test-item-id","content_index":1,"audio_end_ms":2000,"type":"conversation.item.truncate"}`
|
||||||
assert.JSONEq(t, expected, string(data))
|
require.JSONEq(t, expected, string(data))
|
||||||
}
|
}
|
||||||
func TestConversationItemDeleteEvent(t *testing.T) {
|
func TestConversationItemDeleteEvent(t *testing.T) {
|
||||||
message := openairt.ConversationItemDeleteEvent{
|
message := openairt.ConversationItemDeleteEvent{
|
||||||
ItemID: "test-item-id",
|
ItemID: "test-item-id",
|
||||||
}
|
}
|
||||||
data, err := json.Marshal(message)
|
data, err := json.Marshal(message)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
expected := `{"item_id":"test-item-id","type":"conversation.item.delete"}`
|
expected := `{"item_id":"test-item-id","type":"conversation.item.delete"}`
|
||||||
assert.JSONEq(t, expected, string(data))
|
require.JSONEq(t, expected, string(data))
|
||||||
|
|
||||||
message.EventBase.EventID = "test-id"
|
message.EventBase.EventID = "test-id"
|
||||||
data, err = json.Marshal(message)
|
data, err = json.Marshal(message)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
expected = `{"event_id":"test-id","item_id":"test-item-id","type":"conversation.item.delete"}`
|
expected = `{"event_id":"test-id","item_id":"test-item-id","type":"conversation.item.delete"}`
|
||||||
assert.JSONEq(t, expected, string(data))
|
require.JSONEq(t, expected, string(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResponseCreateEvent(t *testing.T) {
|
func TestResponseCreateEvent(t *testing.T) {
|
||||||
@@ -371,7 +371,7 @@ func TestResponseCreateEvent(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
data, err := json.MarshalIndent(message, "", "\t")
|
data, err := json.MarshalIndent(message, "", "\t")
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
expected := `{
|
expected := `{
|
||||||
"response": {
|
"response": {
|
||||||
"modalities": [
|
"modalities": [
|
||||||
@@ -386,26 +386,26 @@ func TestResponseCreateEvent(t *testing.T) {
|
|||||||
},
|
},
|
||||||
"type": "response.create"
|
"type": "response.create"
|
||||||
}`
|
}`
|
||||||
assert.JSONEq(t, expected, string(data))
|
require.JSONEq(t, expected, string(data))
|
||||||
|
|
||||||
message.EventBase.EventID = "test-id"
|
message.EventBase.EventID = "test-id"
|
||||||
message.Response.MaxOutputTokens = openairt.Inf
|
message.Response.MaxOutputTokens = openairt.Inf
|
||||||
data, err = json.Marshal(message)
|
data, err = json.Marshal(message)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
expected = `{"event_id":"test-id","response":{"modalities":["text","audio"],"instructions":"test-instructions","voice":"alloy","output_audio_format":"g711-ulaw","tool_choice":"auto","max_output_tokens":"inf"},"type":"response.create"}`
|
expected = `{"event_id":"test-id","response":{"modalities":["text","audio"],"instructions":"test-instructions","voice":"alloy","output_audio_format":"g711-ulaw","tool_choice":"auto","max_output_tokens":"inf"},"type":"response.create"}`
|
||||||
assert.JSONEq(t, expected, string(data))
|
require.JSONEq(t, expected, string(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResponseCancelEvent(t *testing.T) {
|
func TestResponseCancelEvent(t *testing.T) {
|
||||||
message := openairt.ResponseCancelEvent{}
|
message := openairt.ResponseCancelEvent{}
|
||||||
data, err := json.Marshal(message)
|
data, err := json.Marshal(message)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
expected := `{"type":"response.cancel"}`
|
expected := `{"type":"response.cancel"}`
|
||||||
assert.JSONEq(t, expected, string(data))
|
require.JSONEq(t, expected, string(data))
|
||||||
|
|
||||||
message.EventBase.EventID = "test-id"
|
message.EventBase.EventID = "test-id"
|
||||||
data, err = json.Marshal(message)
|
data, err = json.Marshal(message)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
expected = `{"event_id":"test-id","type":"response.cancel"}`
|
expected = `{"event_id":"test-id","type":"response.cancel"}`
|
||||||
assert.JSONEq(t, expected, string(data))
|
require.JSONEq(t, expected, string(data))
|
||||||
}
|
}
|
||||||
|
|||||||
+19
-19
@@ -1,36 +1,36 @@
|
|||||||
package openairt
|
package openairt //nolint:testpackage // Need to access unexported fields
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestClient(t *testing.T) {
|
func TestClient(t *testing.T) {
|
||||||
mockToken := "test"
|
mockToken := "test"
|
||||||
client := NewClient(mockToken)
|
client := NewClient(mockToken)
|
||||||
assert.Equal(t, client.config.authToken, mockToken)
|
require.Equal(t, client.config.authToken, mockToken)
|
||||||
|
|
||||||
config := DefaultConfig(mockToken)
|
config := DefaultConfig(mockToken)
|
||||||
client = NewClientWithConfig(config)
|
client = NewClientWithConfig(config)
|
||||||
assert.Equal(t, client.config.authToken, mockToken)
|
require.Equal(t, mockToken, client.config.authToken)
|
||||||
assert.Equal(t, client.config.BaseURL, openaiAPIURLv1)
|
require.Equal(t, openaiAPIURLv1, client.config.BaseURL)
|
||||||
assert.Equal(t, client.config.APIType, APITypeOpenAI)
|
require.Equal(t, APITypeOpenAI, client.config.APIType)
|
||||||
url := client.getUrl("test-model")
|
url := client.getURL("test-model")
|
||||||
assert.Equal(t, url, openaiAPIURLv1+"?model=test-model")
|
require.Equal(t, openaiAPIURLv1+"?model=test-model", url)
|
||||||
headers := client.getHeaders()
|
headers := client.getHeaders()
|
||||||
assert.Equal(t, headers.Get("Authorization"), "Bearer "+mockToken)
|
require.Equal(t, "Bearer "+mockToken, headers.Get("Authorization"))
|
||||||
assert.Equal(t, headers.Get("OpenAI-Beta"), "realtime=v1")
|
require.Equal(t, "realtime=v1", headers.Get("OpenAI-Beta"))
|
||||||
|
|
||||||
azureUrl := "wss://my-eastus2-openai-resource.openai.azure.com/openai/realtime"
|
azureURL := "wss://my-eastus2-openai-resource.openai.azure.com/openai/realtime"
|
||||||
config = DefaultAzureConfig(mockToken, azureUrl)
|
config = DefaultAzureConfig(mockToken, azureURL)
|
||||||
client = NewClientWithConfig(config)
|
client = NewClientWithConfig(config)
|
||||||
assert.Equal(t, client.config.authToken, mockToken)
|
require.Equal(t, mockToken, client.config.authToken)
|
||||||
assert.Equal(t, client.config.BaseURL, azureUrl)
|
require.Equal(t, azureURL, client.config.BaseURL)
|
||||||
assert.Equal(t, client.config.APIType, APITypeAzure)
|
require.Equal(t, APITypeAzure, client.config.APIType)
|
||||||
assert.Equal(t, client.config.APIVersion, azureAPIVersion20241001Preview)
|
require.Equal(t, azureAPIVersion20241001Preview, client.config.APIVersion)
|
||||||
url = client.getUrl("test-model")
|
url = client.getURL("test-model")
|
||||||
assert.Equal(t, url, azureUrl+"?api-version="+azureAPIVersion20241001Preview+"&deployment=test-model")
|
require.Equal(t, azureURL+"?api-version="+azureAPIVersion20241001Preview+"&deployment=test-model", url)
|
||||||
headers = client.getHeaders()
|
headers = client.getHeaders()
|
||||||
assert.Equal(t, headers.Get("api-key"), mockToken)
|
require.Equal(t, mockToken, headers.Get("api-key"))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -79,11 +79,11 @@ func (c *ConnHandler) Start() {
|
|||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConnHandler) run() error {
|
func (c *ConnHandler) run() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-c.ctx.Done():
|
case <-c.ctx.Done():
|
||||||
return nil
|
return
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
conn.SendMessage(ctx, &openairt.ConversationItemCreateEvent{
|
conn.SendMessage(ctx, &openairt.ConversationItemCreateEvent{
|
||||||
Item: openairt.MessageItem{
|
Item: openairt.MessageItem{
|
||||||
ID: openairt.GenerateId("msg_", 10),
|
ID: openairt.GenerateID("msg_", 10),
|
||||||
Status: openairt.ItemStatusCompleted,
|
Status: openairt.ItemStatusCompleted,
|
||||||
Type: openairt.MessageItemTypeMessage,
|
Type: openairt.MessageItemTypeMessage,
|
||||||
Role: openairt.MessageRoleUser,
|
Role: openairt.MessageRoleUser,
|
||||||
|
|||||||
@@ -151,7 +151,7 @@ func main() {
|
|||||||
fmt.Println("---------------------")
|
fmt.Println("---------------------")
|
||||||
conn.SendMessage(ctx, &openairt.ConversationItemCreateEvent{
|
conn.SendMessage(ctx, &openairt.ConversationItemCreateEvent{
|
||||||
Item: openairt.MessageItem{
|
Item: openairt.MessageItem{
|
||||||
ID: openairt.GenerateId("msg_", 10),
|
ID: openairt.GenerateID("msg_", 10),
|
||||||
Status: openairt.ItemStatusCompleted,
|
Status: openairt.ItemStatusCompleted,
|
||||||
Type: openairt.MessageItemTypeMessage,
|
Type: openairt.MessageItemTypeMessage,
|
||||||
Role: openairt.MessageRoleUser,
|
Role: openairt.MessageRoleUser,
|
||||||
|
|||||||
+12
-12
@@ -8,25 +8,25 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
openairt "github.com/WqyJh/go-openai-realtime"
|
openairt "github.com/WqyJh/go-openai-realtime"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestIntOrInfMarshalJSON(t *testing.T) {
|
func TestIntOrInfMarshalJSON(t *testing.T) {
|
||||||
v := openairt.IntOrInf(10)
|
v := openairt.IntOrInf(10)
|
||||||
assert.Equal(t, 10, int(v))
|
require.Equal(t, 10, int(v))
|
||||||
assert.False(t, v.IsInf())
|
require.False(t, v.IsInf())
|
||||||
|
|
||||||
b, err := json.Marshal(v)
|
b, err := json.Marshal(v)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, []byte("10"), b)
|
require.Equal(t, []byte("10"), b)
|
||||||
|
|
||||||
v = openairt.Inf
|
v = openairt.Inf
|
||||||
assert.Equal(t, math.MaxInt, int(v))
|
require.Equal(t, math.MaxInt, int(v))
|
||||||
assert.True(t, v.IsInf())
|
require.True(t, v.IsInf())
|
||||||
|
|
||||||
b, err = json.Marshal(v)
|
b, err = json.Marshal(v)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, []byte("\"inf\""), b)
|
require.Equal(t, []byte("\"inf\""), b)
|
||||||
|
|
||||||
type TestStruct struct {
|
type TestStruct struct {
|
||||||
MaxOutputTokens openairt.IntOrInf `json:"max_output_tokens,omitempty"`
|
MaxOutputTokens openairt.IntOrInf `json:"max_output_tokens,omitempty"`
|
||||||
@@ -34,15 +34,15 @@ func TestIntOrInfMarshalJSON(t *testing.T) {
|
|||||||
|
|
||||||
s := TestStruct{}
|
s := TestStruct{}
|
||||||
b, err = json.Marshal(s)
|
b, err = json.Marshal(s)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, []byte("{}"), b)
|
require.Equal(t, []byte("{}"), b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInetAton(t *testing.T) {
|
func TestInetAton(t *testing.T) {
|
||||||
ip := "37.57.95.27"
|
ip := "37.57.95.27"
|
||||||
ret := big.NewInt(0)
|
ret := big.NewInt(0)
|
||||||
i := net.ParseIP(ip).To4()
|
i := net.ParseIP(ip).To4()
|
||||||
assert.NotNil(t, i)
|
require.NotNil(t, i)
|
||||||
ret.SetBytes(i)
|
ret.SetBytes(i)
|
||||||
t.Log(ret.Uint64())
|
t.Log(ret.Uint64())
|
||||||
}
|
}
|
||||||
|
|||||||
+69
-85
@@ -369,8 +369,48 @@ type RateLimitsUpdatedEvent struct {
|
|||||||
RateLimits []RateLimit `json:"rate_limits"`
|
RateLimits []RateLimit `json:"rate_limits"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ServerEventInterface interface {
|
||||||
|
ErrorEvent |
|
||||||
|
SessionCreatedEvent |
|
||||||
|
SessionUpdatedEvent |
|
||||||
|
ConversationCreatedEvent |
|
||||||
|
InputAudioBufferCommittedEvent |
|
||||||
|
InputAudioBufferClearedEvent |
|
||||||
|
InputAudioBufferSpeechStartedEvent |
|
||||||
|
InputAudioBufferSpeechStoppedEvent |
|
||||||
|
ConversationItemCreatedEvent |
|
||||||
|
ConversationItemInputAudioTranscriptionCompletedEvent |
|
||||||
|
ConversationItemInputAudioTranscriptionFailedEvent |
|
||||||
|
ConversationItemTruncatedEvent |
|
||||||
|
ConversationItemDeletedEvent |
|
||||||
|
ResponseCreatedEvent |
|
||||||
|
ResponseDoneEvent |
|
||||||
|
ResponseOutputItemAddedEvent |
|
||||||
|
ResponseOutputItemDoneEvent |
|
||||||
|
ResponseContentPartAddedEvent |
|
||||||
|
ResponseContentPartDoneEvent |
|
||||||
|
ResponseTextDeltaEvent |
|
||||||
|
ResponseTextDoneEvent |
|
||||||
|
ResponseAudioTranscriptDeltaEvent |
|
||||||
|
ResponseAudioTranscriptDoneEvent |
|
||||||
|
ResponseAudioDeltaEvent |
|
||||||
|
ResponseAudioDoneEvent |
|
||||||
|
ResponseFunctionCallArgumentsDeltaEvent |
|
||||||
|
ResponseFunctionCallArgumentsDoneEvent |
|
||||||
|
RateLimitsUpdatedEvent
|
||||||
|
}
|
||||||
|
|
||||||
|
func unmarshalServerEvent[T ServerEventInterface](data []byte) (T, error) {
|
||||||
|
var t T
|
||||||
|
err := json.Unmarshal(data, &t)
|
||||||
|
if err != nil {
|
||||||
|
return t, err
|
||||||
|
}
|
||||||
|
return t, nil
|
||||||
|
}
|
||||||
|
|
||||||
// UnmarshalServerEvent unmarshals the server event from the given JSON data.
|
// UnmarshalServerEvent unmarshals the server event from the given JSON data.
|
||||||
func UnmarshalServerEvent(data []byte) (ServerEvent, error) {
|
func UnmarshalServerEvent(data []byte) (ServerEvent, error) { //nolint:funlen,cyclop // TODO: optimize
|
||||||
var eventType struct {
|
var eventType struct {
|
||||||
Type ServerEventType `json:"type"`
|
Type ServerEventType `json:"type"`
|
||||||
}
|
}
|
||||||
@@ -380,117 +420,61 @@ func UnmarshalServerEvent(data []byte) (ServerEvent, error) {
|
|||||||
}
|
}
|
||||||
switch eventType.Type {
|
switch eventType.Type {
|
||||||
case ServerEventTypeError:
|
case ServerEventTypeError:
|
||||||
var event ErrorEvent
|
return unmarshalServerEvent[ErrorEvent](data)
|
||||||
err = json.Unmarshal(data, &event)
|
|
||||||
return event, err
|
|
||||||
case ServerEventTypeSessionCreated:
|
case ServerEventTypeSessionCreated:
|
||||||
var event SessionCreatedEvent
|
return unmarshalServerEvent[SessionCreatedEvent](data)
|
||||||
err = json.Unmarshal(data, &event)
|
|
||||||
return event, err
|
|
||||||
case ServerEventTypeSessionUpdated:
|
case ServerEventTypeSessionUpdated:
|
||||||
var event SessionUpdatedEvent
|
return unmarshalServerEvent[SessionUpdatedEvent](data)
|
||||||
err = json.Unmarshal(data, &event)
|
|
||||||
return event, err
|
|
||||||
case ServerEventTypeConversationCreated:
|
case ServerEventTypeConversationCreated:
|
||||||
var event ConversationCreatedEvent
|
return unmarshalServerEvent[ConversationCreatedEvent](data)
|
||||||
err = json.Unmarshal(data, &event)
|
|
||||||
return event, err
|
|
||||||
case ServerEventTypeInputAudioBufferCommitted:
|
case ServerEventTypeInputAudioBufferCommitted:
|
||||||
var event InputAudioBufferCommittedEvent
|
return unmarshalServerEvent[InputAudioBufferCommittedEvent](data)
|
||||||
err = json.Unmarshal(data, &event)
|
|
||||||
return event, err
|
|
||||||
case ServerEventTypeInputAudioBufferCleared:
|
case ServerEventTypeInputAudioBufferCleared:
|
||||||
var event InputAudioBufferClearedEvent
|
return unmarshalServerEvent[InputAudioBufferClearedEvent](data)
|
||||||
err = json.Unmarshal(data, &event)
|
|
||||||
return event, err
|
|
||||||
case ServerEventTypeInputAudioBufferSpeechStarted:
|
case ServerEventTypeInputAudioBufferSpeechStarted:
|
||||||
var event InputAudioBufferSpeechStartedEvent
|
return unmarshalServerEvent[InputAudioBufferSpeechStartedEvent](data)
|
||||||
err = json.Unmarshal(data, &event)
|
|
||||||
return event, err
|
|
||||||
case ServerEventTypeInputAudioBufferSpeechStopped:
|
case ServerEventTypeInputAudioBufferSpeechStopped:
|
||||||
var event InputAudioBufferSpeechStoppedEvent
|
return unmarshalServerEvent[InputAudioBufferSpeechStoppedEvent](data)
|
||||||
err = json.Unmarshal(data, &event)
|
|
||||||
return event, err
|
|
||||||
case ServerEventTypeConversationItemCreated:
|
case ServerEventTypeConversationItemCreated:
|
||||||
var event ConversationItemCreatedEvent
|
return unmarshalServerEvent[ConversationItemCreatedEvent](data)
|
||||||
err = json.Unmarshal(data, &event)
|
|
||||||
return event, err
|
|
||||||
case ServerEventTypeConversationItemInputAudioTranscriptionCompleted:
|
case ServerEventTypeConversationItemInputAudioTranscriptionCompleted:
|
||||||
var event ConversationItemInputAudioTranscriptionCompletedEvent
|
return unmarshalServerEvent[ConversationItemInputAudioTranscriptionCompletedEvent](data)
|
||||||
err = json.Unmarshal(data, &event)
|
|
||||||
return event, err
|
|
||||||
case ServerEventTypeConversationItemInputAudioTranscriptionFailed:
|
case ServerEventTypeConversationItemInputAudioTranscriptionFailed:
|
||||||
var event ConversationItemInputAudioTranscriptionFailedEvent
|
return unmarshalServerEvent[ConversationItemInputAudioTranscriptionFailedEvent](data)
|
||||||
err = json.Unmarshal(data, &event)
|
|
||||||
return event, err
|
|
||||||
case ServerEventTypeConversationItemTruncated:
|
case ServerEventTypeConversationItemTruncated:
|
||||||
var event ConversationItemTruncatedEvent
|
return unmarshalServerEvent[ConversationItemTruncatedEvent](data)
|
||||||
err = json.Unmarshal(data, &event)
|
|
||||||
return event, err
|
|
||||||
case ServerEventTypeConversationItemDeleted:
|
case ServerEventTypeConversationItemDeleted:
|
||||||
var event ConversationItemDeletedEvent
|
return unmarshalServerEvent[ConversationItemDeletedEvent](data)
|
||||||
err = json.Unmarshal(data, &event)
|
|
||||||
return event, err
|
|
||||||
case ServerEventTypeResponseCreated:
|
case ServerEventTypeResponseCreated:
|
||||||
var event ResponseCreatedEvent
|
return unmarshalServerEvent[ResponseCreatedEvent](data)
|
||||||
err = json.Unmarshal(data, &event)
|
|
||||||
return event, err
|
|
||||||
case ServerEventTypeResponseDone:
|
case ServerEventTypeResponseDone:
|
||||||
var event ResponseDoneEvent
|
return unmarshalServerEvent[ResponseDoneEvent](data)
|
||||||
err = json.Unmarshal(data, &event)
|
|
||||||
return event, err
|
|
||||||
case ServerEventTypeResponseOutputItemAdded:
|
case ServerEventTypeResponseOutputItemAdded:
|
||||||
var event ResponseOutputItemAddedEvent
|
return unmarshalServerEvent[ResponseOutputItemAddedEvent](data)
|
||||||
err = json.Unmarshal(data, &event)
|
|
||||||
return event, err
|
|
||||||
case ServerEventTypeResponseOutputItemDone:
|
case ServerEventTypeResponseOutputItemDone:
|
||||||
var event ResponseOutputItemDoneEvent
|
return unmarshalServerEvent[ResponseOutputItemDoneEvent](data)
|
||||||
err = json.Unmarshal(data, &event)
|
|
||||||
return event, err
|
|
||||||
case ServerEventTypeResponseContentPartAdded:
|
case ServerEventTypeResponseContentPartAdded:
|
||||||
var event ResponseContentPartAddedEvent
|
return unmarshalServerEvent[ResponseContentPartAddedEvent](data)
|
||||||
err = json.Unmarshal(data, &event)
|
|
||||||
return event, err
|
|
||||||
case ServerEventTypeResponseContentPartDone:
|
case ServerEventTypeResponseContentPartDone:
|
||||||
var event ResponseContentPartDoneEvent
|
return unmarshalServerEvent[ResponseContentPartDoneEvent](data)
|
||||||
err = json.Unmarshal(data, &event)
|
|
||||||
return event, err
|
|
||||||
case ServerEventTypeResponseTextDelta:
|
case ServerEventTypeResponseTextDelta:
|
||||||
var event ResponseTextDeltaEvent
|
return unmarshalServerEvent[ResponseTextDeltaEvent](data)
|
||||||
err = json.Unmarshal(data, &event)
|
|
||||||
return event, err
|
|
||||||
case ServerEventTypeResponseTextDone:
|
case ServerEventTypeResponseTextDone:
|
||||||
var event ResponseTextDoneEvent
|
return unmarshalServerEvent[ResponseTextDoneEvent](data)
|
||||||
err = json.Unmarshal(data, &event)
|
|
||||||
return event, err
|
|
||||||
case ServerEventTypeResponseAudioTranscriptDelta:
|
case ServerEventTypeResponseAudioTranscriptDelta:
|
||||||
var event ResponseAudioTranscriptDeltaEvent
|
return unmarshalServerEvent[ResponseAudioTranscriptDeltaEvent](data)
|
||||||
err = json.Unmarshal(data, &event)
|
|
||||||
return event, err
|
|
||||||
case ServerEventTypeResponseAudioTranscriptDone:
|
case ServerEventTypeResponseAudioTranscriptDone:
|
||||||
var event ResponseAudioTranscriptDoneEvent
|
return unmarshalServerEvent[ResponseAudioTranscriptDoneEvent](data)
|
||||||
err = json.Unmarshal(data, &event)
|
|
||||||
return event, err
|
|
||||||
case ServerEventTypeResponseAudioDelta:
|
case ServerEventTypeResponseAudioDelta:
|
||||||
var event ResponseAudioDeltaEvent
|
return unmarshalServerEvent[ResponseAudioDeltaEvent](data)
|
||||||
err = json.Unmarshal(data, &event)
|
|
||||||
return event, err
|
|
||||||
case ServerEventTypeResponseAudioDone:
|
case ServerEventTypeResponseAudioDone:
|
||||||
var event ResponseAudioDoneEvent
|
return unmarshalServerEvent[ResponseAudioDoneEvent](data)
|
||||||
err = json.Unmarshal(data, &event)
|
|
||||||
return event, err
|
|
||||||
case ServerEventTypeResponseFunctionCallArgumentsDelta:
|
case ServerEventTypeResponseFunctionCallArgumentsDelta:
|
||||||
var event ResponseFunctionCallArgumentsDeltaEvent
|
return unmarshalServerEvent[ResponseFunctionCallArgumentsDeltaEvent](data)
|
||||||
err = json.Unmarshal(data, &event)
|
|
||||||
return event, err
|
|
||||||
case ServerEventTypeResponseFunctionCallArgumentsDone:
|
case ServerEventTypeResponseFunctionCallArgumentsDone:
|
||||||
var event ResponseFunctionCallArgumentsDoneEvent
|
return unmarshalServerEvent[ResponseFunctionCallArgumentsDoneEvent](data)
|
||||||
err = json.Unmarshal(data, &event)
|
|
||||||
return event, err
|
|
||||||
case ServerEventTypeRateLimitsUpdated:
|
case ServerEventTypeRateLimitsUpdated:
|
||||||
var event RateLimitsUpdatedEvent
|
return unmarshalServerEvent[RateLimitsUpdatedEvent](data)
|
||||||
err = json.Unmarshal(data, &event)
|
|
||||||
return event, err
|
|
||||||
default:
|
default:
|
||||||
// This should never happen.
|
// This should never happen.
|
||||||
return nil, fmt.Errorf("unknown server event type: %s", eventType.Type)
|
return nil, fmt.Errorf("unknown server event type: %s", eventType.Type)
|
||||||
|
|||||||
+100
-100
@@ -5,7 +5,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
openairt "github.com/WqyJh/go-openai-realtime"
|
openairt "github.com/WqyJh/go-openai-realtime"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestErrorEvent(t *testing.T) {
|
func TestErrorEvent(t *testing.T) {
|
||||||
@@ -34,9 +34,9 @@ func TestErrorEvent(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, openairt.ServerEventTypeError, actual.ServerEventType())
|
require.Equal(t, openairt.ServerEventTypeError, actual.ServerEventType())
|
||||||
assert.Equal(t, expected, actual.(openairt.ErrorEvent))
|
require.Equal(t, expected, actual.(openairt.ErrorEvent))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestServerToolChoice(t *testing.T) {
|
func TestServerToolChoice(t *testing.T) {
|
||||||
@@ -50,33 +50,33 @@ func TestServerToolChoice(t *testing.T) {
|
|||||||
expected := openairt.ServerToolChoice{Function: expectedFunction}
|
expected := openairt.ServerToolChoice{Function: expectedFunction}
|
||||||
actual := openairt.ServerToolChoice{}
|
actual := openairt.ServerToolChoice{}
|
||||||
err := json.Unmarshal([]byte(data), &actual)
|
err := json.Unmarshal([]byte(data), &actual)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, expected, actual)
|
require.Equal(t, expected, actual)
|
||||||
assert.Equal(t, expectedFunction, actual.Get())
|
require.Equal(t, expectedFunction, actual.Get())
|
||||||
|
|
||||||
data = `"auto"`
|
data = `"auto"`
|
||||||
expected = openairt.ServerToolChoice{String: openairt.ToolChoiceAuto}
|
expected = openairt.ServerToolChoice{String: openairt.ToolChoiceAuto}
|
||||||
actual = openairt.ServerToolChoice{}
|
actual = openairt.ServerToolChoice{}
|
||||||
err = json.Unmarshal([]byte(data), &actual)
|
err = json.Unmarshal([]byte(data), &actual)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, expected, actual)
|
require.Equal(t, expected, actual)
|
||||||
assert.Equal(t, openairt.ToolChoiceAuto, actual.Get())
|
require.Equal(t, openairt.ToolChoiceAuto, actual.Get())
|
||||||
|
|
||||||
data = `"none"`
|
data = `"none"`
|
||||||
expected = openairt.ServerToolChoice{String: openairt.ToolChoiceNone}
|
expected = openairt.ServerToolChoice{String: openairt.ToolChoiceNone}
|
||||||
actual = openairt.ServerToolChoice{}
|
actual = openairt.ServerToolChoice{}
|
||||||
err = json.Unmarshal([]byte(data), &actual)
|
err = json.Unmarshal([]byte(data), &actual)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, expected, actual)
|
require.Equal(t, expected, actual)
|
||||||
assert.Equal(t, openairt.ToolChoiceNone, actual.Get())
|
require.Equal(t, openairt.ToolChoiceNone, actual.Get())
|
||||||
|
|
||||||
data = `"required"`
|
data = `"required"`
|
||||||
expected = openairt.ServerToolChoice{String: openairt.ToolChoiceRequired}
|
expected = openairt.ServerToolChoice{String: openairt.ToolChoiceRequired}
|
||||||
actual = openairt.ServerToolChoice{}
|
actual = openairt.ServerToolChoice{}
|
||||||
err = json.Unmarshal([]byte(data), &actual)
|
err = json.Unmarshal([]byte(data), &actual)
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, expected, actual)
|
require.Equal(t, expected, actual)
|
||||||
assert.Equal(t, openairt.ToolChoiceRequired, actual.Get())
|
require.Equal(t, openairt.ToolChoiceRequired, actual.Get())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSessionCreatedEvent(t *testing.T) {
|
func TestSessionCreatedEvent(t *testing.T) {
|
||||||
@@ -136,9 +136,9 @@ func TestSessionCreatedEvent(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, openairt.ServerEventTypeSessionCreated, actual.ServerEventType())
|
require.Equal(t, openairt.ServerEventTypeSessionCreated, actual.ServerEventType())
|
||||||
assert.Equal(t, expected, actual.(openairt.SessionCreatedEvent))
|
require.Equal(t, expected, actual.(openairt.SessionCreatedEvent))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSessionUpdatedEvent(t *testing.T) {
|
func TestSessionUpdatedEvent(t *testing.T) {
|
||||||
@@ -194,9 +194,9 @@ func TestSessionUpdatedEvent(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, openairt.ServerEventTypeSessionUpdated, actual.ServerEventType())
|
require.Equal(t, openairt.ServerEventTypeSessionUpdated, actual.ServerEventType())
|
||||||
assert.Equal(t, expected, actual.(openairt.SessionUpdatedEvent))
|
require.Equal(t, expected, actual.(openairt.SessionUpdatedEvent))
|
||||||
|
|
||||||
data = `{
|
data = `{
|
||||||
"event_id": "event_5678",
|
"event_id": "event_5678",
|
||||||
@@ -224,9 +224,9 @@ func TestSessionUpdatedEvent(t *testing.T) {
|
|||||||
}`
|
}`
|
||||||
expected.Session.MaxOutputTokens = openairt.Inf
|
expected.Session.MaxOutputTokens = openairt.Inf
|
||||||
actual, err = openairt.UnmarshalServerEvent([]byte(data))
|
actual, err = openairt.UnmarshalServerEvent([]byte(data))
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, openairt.ServerEventTypeSessionUpdated, actual.ServerEventType())
|
require.Equal(t, openairt.ServerEventTypeSessionUpdated, actual.ServerEventType())
|
||||||
assert.Equal(t, expected, actual.(openairt.SessionUpdatedEvent))
|
require.Equal(t, expected, actual.(openairt.SessionUpdatedEvent))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConversationCreatedEvent(t *testing.T) {
|
func TestConversationCreatedEvent(t *testing.T) {
|
||||||
@@ -249,9 +249,9 @@ func TestConversationCreatedEvent(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, openairt.ServerEventTypeConversationCreated, actual.ServerEventType())
|
require.Equal(t, openairt.ServerEventTypeConversationCreated, actual.ServerEventType())
|
||||||
assert.Equal(t, expected, actual.(openairt.ConversationCreatedEvent))
|
require.Equal(t, expected, actual.(openairt.ConversationCreatedEvent))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConversationItemCreatedEvent(t *testing.T) {
|
func TestConversationItemCreatedEvent(t *testing.T) {
|
||||||
@@ -296,9 +296,9 @@ func TestConversationItemCreatedEvent(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, openairt.ServerEventTypeConversationItemCreated, actual.ServerEventType())
|
require.Equal(t, openairt.ServerEventTypeConversationItemCreated, actual.ServerEventType())
|
||||||
assert.Equal(t, expected, actual.(openairt.ConversationItemCreatedEvent))
|
require.Equal(t, expected, actual.(openairt.ConversationItemCreatedEvent))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConversationItemInputAudioTranscriptionCompletedEvent(t *testing.T) {
|
func TestConversationItemInputAudioTranscriptionCompletedEvent(t *testing.T) {
|
||||||
@@ -319,9 +319,9 @@ func TestConversationItemInputAudioTranscriptionCompletedEvent(t *testing.T) {
|
|||||||
Transcript: "Hello, how are you?",
|
Transcript: "Hello, how are you?",
|
||||||
}
|
}
|
||||||
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, openairt.ServerEventTypeConversationItemInputAudioTranscriptionCompleted, actual.ServerEventType())
|
require.Equal(t, openairt.ServerEventTypeConversationItemInputAudioTranscriptionCompleted, actual.ServerEventType())
|
||||||
assert.Equal(t, expected, actual.(openairt.ConversationItemInputAudioTranscriptionCompletedEvent))
|
require.Equal(t, expected, actual.(openairt.ConversationItemInputAudioTranscriptionCompletedEvent))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConversationItemInputAudioTranscriptionFailedEvent(t *testing.T) {
|
func TestConversationItemInputAudioTranscriptionFailedEvent(t *testing.T) {
|
||||||
@@ -353,9 +353,9 @@ func TestConversationItemInputAudioTranscriptionFailedEvent(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, openairt.ServerEventTypeConversationItemInputAudioTranscriptionFailed, actual.ServerEventType())
|
require.Equal(t, openairt.ServerEventTypeConversationItemInputAudioTranscriptionFailed, actual.ServerEventType())
|
||||||
assert.Equal(t, expected, actual.(openairt.ConversationItemInputAudioTranscriptionFailedEvent))
|
require.Equal(t, expected, actual.(openairt.ConversationItemInputAudioTranscriptionFailedEvent))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConversationItemTruncatedEvent(t *testing.T) {
|
func TestConversationItemTruncatedEvent(t *testing.T) {
|
||||||
@@ -376,9 +376,9 @@ func TestConversationItemTruncatedEvent(t *testing.T) {
|
|||||||
AudioEndMs: 1500,
|
AudioEndMs: 1500,
|
||||||
}
|
}
|
||||||
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, openairt.ServerEventTypeConversationItemTruncated, actual.ServerEventType())
|
require.Equal(t, openairt.ServerEventTypeConversationItemTruncated, actual.ServerEventType())
|
||||||
assert.Equal(t, expected, actual.(openairt.ConversationItemTruncatedEvent))
|
require.Equal(t, expected, actual.(openairt.ConversationItemTruncatedEvent))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConversationItemDeletedEvent(t *testing.T) {
|
func TestConversationItemDeletedEvent(t *testing.T) {
|
||||||
@@ -395,9 +395,9 @@ func TestConversationItemDeletedEvent(t *testing.T) {
|
|||||||
ItemID: "msg_005",
|
ItemID: "msg_005",
|
||||||
}
|
}
|
||||||
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, openairt.ServerEventTypeConversationItemDeleted, actual.ServerEventType())
|
require.Equal(t, openairt.ServerEventTypeConversationItemDeleted, actual.ServerEventType())
|
||||||
assert.Equal(t, expected, actual.(openairt.ConversationItemDeletedEvent))
|
require.Equal(t, expected, actual.(openairt.ConversationItemDeletedEvent))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInputAudioBufferCommittedEvent(t *testing.T) {
|
func TestInputAudioBufferCommittedEvent(t *testing.T) {
|
||||||
@@ -416,9 +416,9 @@ func TestInputAudioBufferCommittedEvent(t *testing.T) {
|
|||||||
ItemID: "msg_002",
|
ItemID: "msg_002",
|
||||||
}
|
}
|
||||||
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, openairt.ServerEventTypeInputAudioBufferCommitted, actual.ServerEventType())
|
require.Equal(t, openairt.ServerEventTypeInputAudioBufferCommitted, actual.ServerEventType())
|
||||||
assert.Equal(t, expected, actual.(openairt.InputAudioBufferCommittedEvent))
|
require.Equal(t, expected, actual.(openairt.InputAudioBufferCommittedEvent))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInputAudioBufferClearedEvent(t *testing.T) {
|
func TestInputAudioBufferClearedEvent(t *testing.T) {
|
||||||
@@ -433,9 +433,9 @@ func TestInputAudioBufferClearedEvent(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, openairt.ServerEventTypeInputAudioBufferCleared, actual.ServerEventType())
|
require.Equal(t, openairt.ServerEventTypeInputAudioBufferCleared, actual.ServerEventType())
|
||||||
assert.Equal(t, expected, actual.(openairt.InputAudioBufferClearedEvent))
|
require.Equal(t, expected, actual.(openairt.InputAudioBufferClearedEvent))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInputAudioBufferSpeechStartedEvent(t *testing.T) {
|
func TestInputAudioBufferSpeechStartedEvent(t *testing.T) {
|
||||||
@@ -454,9 +454,9 @@ func TestInputAudioBufferSpeechStartedEvent(t *testing.T) {
|
|||||||
ItemID: "msg_003",
|
ItemID: "msg_003",
|
||||||
}
|
}
|
||||||
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, openairt.ServerEventTypeInputAudioBufferSpeechStarted, actual.ServerEventType())
|
require.Equal(t, openairt.ServerEventTypeInputAudioBufferSpeechStarted, actual.ServerEventType())
|
||||||
assert.Equal(t, expected, actual.(openairt.InputAudioBufferSpeechStartedEvent))
|
require.Equal(t, expected, actual.(openairt.InputAudioBufferSpeechStartedEvent))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInputAudioBufferSpeechStoppedEvent(t *testing.T) {
|
func TestInputAudioBufferSpeechStoppedEvent(t *testing.T) {
|
||||||
@@ -475,9 +475,9 @@ func TestInputAudioBufferSpeechStoppedEvent(t *testing.T) {
|
|||||||
ItemID: "msg_003",
|
ItemID: "msg_003",
|
||||||
}
|
}
|
||||||
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, openairt.ServerEventTypeInputAudioBufferSpeechStopped, actual.ServerEventType())
|
require.Equal(t, openairt.ServerEventTypeInputAudioBufferSpeechStopped, actual.ServerEventType())
|
||||||
assert.Equal(t, expected, actual.(openairt.InputAudioBufferSpeechStoppedEvent))
|
require.Equal(t, expected, actual.(openairt.InputAudioBufferSpeechStoppedEvent))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResponseCreatedEvent(t *testing.T) {
|
func TestResponseCreatedEvent(t *testing.T) {
|
||||||
@@ -508,9 +508,9 @@ func TestResponseCreatedEvent(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, openairt.ServerEventTypeResponseCreated, actual.ServerEventType())
|
require.Equal(t, openairt.ServerEventTypeResponseCreated, actual.ServerEventType())
|
||||||
assert.Equal(t, expected, actual.(openairt.ResponseCreatedEvent))
|
require.Equal(t, expected, actual.(openairt.ResponseCreatedEvent))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResponseDoneEvent(t *testing.T) {
|
func TestResponseDoneEvent(t *testing.T) {
|
||||||
@@ -579,9 +579,9 @@ func TestResponseDoneEvent(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, openairt.ServerEventTypeResponseDone, actual.ServerEventType())
|
require.Equal(t, openairt.ServerEventTypeResponseDone, actual.ServerEventType())
|
||||||
assert.Equal(t, expected, actual.(openairt.ResponseDoneEvent))
|
require.Equal(t, expected, actual.(openairt.ResponseDoneEvent))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResponseOutputItemAddedEvent(t *testing.T) {
|
func TestResponseOutputItemAddedEvent(t *testing.T) {
|
||||||
@@ -618,9 +618,9 @@ func TestResponseOutputItemAddedEvent(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, openairt.ServerEventTypeResponseOutputItemAdded, actual.ServerEventType())
|
require.Equal(t, openairt.ServerEventTypeResponseOutputItemAdded, actual.ServerEventType())
|
||||||
assert.Equal(t, expected, actual.(openairt.ResponseOutputItemAddedEvent))
|
require.Equal(t, expected, actual.(openairt.ResponseOutputItemAddedEvent))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResponseOutputItemDoneEvent(t *testing.T) {
|
func TestResponseOutputItemDoneEvent(t *testing.T) {
|
||||||
@@ -667,9 +667,9 @@ func TestResponseOutputItemDoneEvent(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, openairt.ServerEventTypeResponseOutputItemDone, actual.ServerEventType())
|
require.Equal(t, openairt.ServerEventTypeResponseOutputItemDone, actual.ServerEventType())
|
||||||
assert.Equal(t, expected, actual.(openairt.ResponseOutputItemDoneEvent))
|
require.Equal(t, expected, actual.(openairt.ResponseOutputItemDoneEvent))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResponseContentPartAddedEvent(t *testing.T) {
|
func TestResponseContentPartAddedEvent(t *testing.T) {
|
||||||
@@ -700,9 +700,9 @@ func TestResponseContentPartAddedEvent(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, openairt.ServerEventTypeResponseContentPartAdded, actual.ServerEventType())
|
require.Equal(t, openairt.ServerEventTypeResponseContentPartAdded, actual.ServerEventType())
|
||||||
assert.Equal(t, expected, actual.(openairt.ResponseContentPartAddedEvent))
|
require.Equal(t, expected, actual.(openairt.ResponseContentPartAddedEvent))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResponseContentPartDoneEvent(t *testing.T) {
|
func TestResponseContentPartDoneEvent(t *testing.T) {
|
||||||
@@ -733,9 +733,9 @@ func TestResponseContentPartDoneEvent(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, openairt.ServerEventTypeResponseContentPartDone, actual.ServerEventType())
|
require.Equal(t, openairt.ServerEventTypeResponseContentPartDone, actual.ServerEventType())
|
||||||
assert.Equal(t, expected, actual.(openairt.ResponseContentPartDoneEvent))
|
require.Equal(t, expected, actual.(openairt.ResponseContentPartDoneEvent))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResponseTextDelta(t *testing.T) {
|
func TestResponseTextDelta(t *testing.T) {
|
||||||
@@ -760,9 +760,9 @@ func TestResponseTextDelta(t *testing.T) {
|
|||||||
Delta: "Sure, I can h",
|
Delta: "Sure, I can h",
|
||||||
}
|
}
|
||||||
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, openairt.ServerEventTypeResponseTextDelta, actual.ServerEventType())
|
require.Equal(t, openairt.ServerEventTypeResponseTextDelta, actual.ServerEventType())
|
||||||
assert.Equal(t, expected, actual.(openairt.ResponseTextDeltaEvent))
|
require.Equal(t, expected, actual.(openairt.ResponseTextDeltaEvent))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResponseTextDoneEvent(t *testing.T) {
|
func TestResponseTextDoneEvent(t *testing.T) {
|
||||||
@@ -787,9 +787,9 @@ func TestResponseTextDoneEvent(t *testing.T) {
|
|||||||
Text: "Sure, I can help with that.",
|
Text: "Sure, I can help with that.",
|
||||||
}
|
}
|
||||||
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, openairt.ServerEventTypeResponseTextDone, actual.ServerEventType())
|
require.Equal(t, openairt.ServerEventTypeResponseTextDone, actual.ServerEventType())
|
||||||
assert.Equal(t, expected, actual.(openairt.ResponseTextDoneEvent))
|
require.Equal(t, expected, actual.(openairt.ResponseTextDoneEvent))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResponseAudioTranscriptDelta(t *testing.T) {
|
func TestResponseAudioTranscriptDelta(t *testing.T) {
|
||||||
@@ -814,9 +814,9 @@ func TestResponseAudioTranscriptDelta(t *testing.T) {
|
|||||||
Delta: "Hello, how can I a",
|
Delta: "Hello, how can I a",
|
||||||
}
|
}
|
||||||
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, openairt.ServerEventTypeResponseAudioTranscriptDelta, actual.ServerEventType())
|
require.Equal(t, openairt.ServerEventTypeResponseAudioTranscriptDelta, actual.ServerEventType())
|
||||||
assert.Equal(t, expected, actual.(openairt.ResponseAudioTranscriptDeltaEvent))
|
require.Equal(t, expected, actual.(openairt.ResponseAudioTranscriptDeltaEvent))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResponseAudioTranscriptDoneEvent(t *testing.T) {
|
func TestResponseAudioTranscriptDoneEvent(t *testing.T) {
|
||||||
@@ -841,9 +841,9 @@ func TestResponseAudioTranscriptDoneEvent(t *testing.T) {
|
|||||||
Transcript: "Hello, how can I assist you today?",
|
Transcript: "Hello, how can I assist you today?",
|
||||||
}
|
}
|
||||||
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, openairt.ServerEventTypeResponseAudioTranscriptDone, actual.ServerEventType())
|
require.Equal(t, openairt.ServerEventTypeResponseAudioTranscriptDone, actual.ServerEventType())
|
||||||
assert.Equal(t, expected, actual.(openairt.ResponseAudioTranscriptDoneEvent))
|
require.Equal(t, expected, actual.(openairt.ResponseAudioTranscriptDoneEvent))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResponseAudioDeltaEvent(t *testing.T) {
|
func TestResponseAudioDeltaEvent(t *testing.T) {
|
||||||
@@ -868,9 +868,9 @@ func TestResponseAudioDeltaEvent(t *testing.T) {
|
|||||||
Delta: "Base64EncodedAudioDelta",
|
Delta: "Base64EncodedAudioDelta",
|
||||||
}
|
}
|
||||||
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, openairt.ServerEventTypeResponseAudioDelta, actual.ServerEventType())
|
require.Equal(t, openairt.ServerEventTypeResponseAudioDelta, actual.ServerEventType())
|
||||||
assert.Equal(t, expected, actual.(openairt.ResponseAudioDeltaEvent))
|
require.Equal(t, expected, actual.(openairt.ResponseAudioDeltaEvent))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResponseAudioDoneEvent(t *testing.T) {
|
func TestResponseAudioDoneEvent(t *testing.T) {
|
||||||
@@ -893,9 +893,9 @@ func TestResponseAudioDoneEvent(t *testing.T) {
|
|||||||
ContentIndex: 0,
|
ContentIndex: 0,
|
||||||
}
|
}
|
||||||
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, openairt.ServerEventTypeResponseAudioDone, actual.ServerEventType())
|
require.Equal(t, openairt.ServerEventTypeResponseAudioDone, actual.ServerEventType())
|
||||||
assert.Equal(t, expected, actual.(openairt.ResponseAudioDoneEvent))
|
require.Equal(t, expected, actual.(openairt.ResponseAudioDoneEvent))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResponseFunctionCallArgumentsDelta(t *testing.T) {
|
func TestResponseFunctionCallArgumentsDelta(t *testing.T) {
|
||||||
@@ -920,9 +920,9 @@ func TestResponseFunctionCallArgumentsDelta(t *testing.T) {
|
|||||||
Delta: "{\"location\": \"San\"",
|
Delta: "{\"location\": \"San\"",
|
||||||
}
|
}
|
||||||
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, openairt.ServerEventTypeResponseFunctionCallArgumentsDelta, actual.ServerEventType())
|
require.Equal(t, openairt.ServerEventTypeResponseFunctionCallArgumentsDelta, actual.ServerEventType())
|
||||||
assert.Equal(t, expected, actual.(openairt.ResponseFunctionCallArgumentsDeltaEvent))
|
require.Equal(t, expected, actual.(openairt.ResponseFunctionCallArgumentsDeltaEvent))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResponseFunctionCallArgumentsDoneEvent(t *testing.T) {
|
func TestResponseFunctionCallArgumentsDoneEvent(t *testing.T) {
|
||||||
@@ -947,9 +947,9 @@ func TestResponseFunctionCallArgumentsDoneEvent(t *testing.T) {
|
|||||||
Arguments: "{\"location\": \"San Francisco\"}",
|
Arguments: "{\"location\": \"San Francisco\"}",
|
||||||
}
|
}
|
||||||
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, openairt.ServerEventTypeResponseFunctionCallArgumentsDone, actual.ServerEventType())
|
require.Equal(t, openairt.ServerEventTypeResponseFunctionCallArgumentsDone, actual.ServerEventType())
|
||||||
assert.Equal(t, expected, actual.(openairt.ResponseFunctionCallArgumentsDoneEvent))
|
require.Equal(t, expected, actual.(openairt.ResponseFunctionCallArgumentsDoneEvent))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRateLimitsUpdatedEvent(t *testing.T) {
|
func TestRateLimitsUpdatedEvent(t *testing.T) {
|
||||||
@@ -992,7 +992,7 @@ func TestRateLimitsUpdatedEvent(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
actual, err := openairt.UnmarshalServerEvent([]byte(data))
|
||||||
assert.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, openairt.ServerEventTypeRateLimitsUpdated, actual.ServerEventType())
|
require.Equal(t, openairt.ServerEventTypeRateLimitsUpdated, actual.ServerEventType())
|
||||||
assert.Equal(t, expected, actual.(openairt.RateLimitsUpdatedEvent))
|
require.Equal(t, expected, actual.(openairt.RateLimitsUpdatedEvent))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ import (
|
|||||||
"math/big"
|
"math/big"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GenerateId generates a random ID with a prefix and a specified length.
|
// GenerateID generates a random ID with a prefix and a specified length.
|
||||||
// The length of the returned ID is equal to the length parameter, therefore the prefix must be shorter than the length.
|
// The length of the returned ID is equal to the length parameter, therefore the prefix must be shorter than the length.
|
||||||
func GenerateId(prefix string, length int) string {
|
func GenerateID(prefix string, length int) string {
|
||||||
const chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
|
const chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
|
||||||
if length <= len(prefix) {
|
if length <= len(prefix) {
|
||||||
return prefix
|
return prefix
|
||||||
|
|||||||
+10
-10
@@ -5,21 +5,21 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
openairt "github.com/WqyJh/go-openai-realtime"
|
openairt "github.com/WqyJh/go-openai-realtime"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGenerateId(t *testing.T) {
|
func TestGenerateID(t *testing.T) {
|
||||||
generated := openairt.GenerateId("test_", 10)
|
generated := openairt.GenerateID("test_", 10)
|
||||||
t.Logf("generated: %s", generated)
|
t.Logf("generated: %s", generated)
|
||||||
assert.True(t, strings.HasPrefix(generated, "test_"))
|
require.True(t, strings.HasPrefix(generated, "test_"))
|
||||||
assert.Equal(t, 10, len(generated))
|
require.Len(t, generated, 10)
|
||||||
|
|
||||||
generated = openairt.GenerateId("test_", 20)
|
generated = openairt.GenerateID("test_", 20)
|
||||||
t.Logf("generated: %s", generated)
|
t.Logf("generated: %s", generated)
|
||||||
assert.True(t, strings.HasPrefix(generated, "test_"))
|
require.True(t, strings.HasPrefix(generated, "test_"))
|
||||||
assert.Equal(t, 20, len(generated))
|
require.Len(t, generated, 20)
|
||||||
|
|
||||||
generated = openairt.GenerateId("test_", 5)
|
generated = openairt.GenerateID("test_", 5)
|
||||||
t.Logf("generated: %s", generated)
|
t.Logf("generated: %s", generated)
|
||||||
assert.Equal(t, "test_", generated)
|
require.Equal(t, "test_", generated)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user