Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dto/openai_compaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
type OpenAIResponsesCompactionResponse struct {
ID string `json:"id"`
Object string `json:"object"`
CreatedAt int `json:"created_at"`
CreatedAt UnixTimestamp `json:"created_at"`
Output json.RawMessage `json:"output"`
Usage *Usage `json:"usage"`
Error any `json:"error,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion dto/openai_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ type OutputTokenDetails struct {
type OpenAIResponsesResponse struct {
ID string `json:"id"`
Object string `json:"object"`
CreatedAt int `json:"created_at"`
CreatedAt UnixTimestamp `json:"created_at"`
Status string `json:"status"`
Error any `json:"error,omitempty"`
IncompleteDetails *IncompleteDetails `json:"incomplete_details,omitempty"`
Expand Down
37 changes: 37 additions & 0 deletions dto/values.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package dto

import (
"bytes"
"encoding/json"
"fmt"
"strconv"
)

Expand Down Expand Up @@ -29,6 +31,41 @@ func (i IntValue) MarshalJSON() ([]byte, error) {
return json.Marshal(int(i))
}

type UnixTimestamp int64

func (t *UnixTimestamp) UnmarshalJSON(data []byte) error {
trimmed := bytes.TrimSpace(data)
if len(trimmed) == 0 || bytes.Equal(trimmed, []byte("null")) {
return nil
}
if trimmed[0] == '"' {
return fmt.Errorf("timestamp must be json number, got string")
}

var n json.Number
if err := json.Unmarshal(trimmed, &n); err != nil {
return err
}
if i, err := n.Int64(); err == nil {
*t = UnixTimestamp(i)
return nil
}
f, err := n.Float64()
if err != nil {
return err
}
*t = UnixTimestamp(int64(f))
return nil
}

func (t UnixTimestamp) MarshalJSON() ([]byte, error) {
return json.Marshal(int64(t))
}

func (t UnixTimestamp) Int64() int64 {
return int64(t)
}

type BoolValue bool

func (b *BoolValue) UnmarshalJSON(data []byte) error {
Expand Down
61 changes: 61 additions & 0 deletions dto/values_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package dto

import (
"testing"

"github.com/QuantumNous/new-api/common"
"github.com/stretchr/testify/require"
)

func TestUnixTimestampUnmarshalJSON(t *testing.T) {
testCases := []struct {
name string
input string
expected int64
}{
{
name: "integer number",
input: `1768488160`,
expected: 1768488160,
},
{
name: "scientific number",
input: `1.76848816E9`,
expected: 1768488160,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var ts UnixTimestamp
err := common.UnmarshalJsonStr(tc.input, &ts)
require.NoError(t, err)
require.Equal(t, tc.expected, ts.Int64())
})
}
}

func TestUnixTimestampUnmarshalJSONRejectsString(t *testing.T) {
var ts UnixTimestamp
err := common.UnmarshalJsonStr(`"1768488160"`, &ts)
require.Error(t, err)
}

func TestResponsesStreamResponseCreatedAtScientificNotation(t *testing.T) {
payload := `{"response":{"id":"resp_1","created_at":1.76848816E9}}`

var resp ResponsesStreamResponse
err := common.UnmarshalJsonStr(payload, &resp)
require.NoError(t, err)
require.NotNil(t, resp.Response)
require.Equal(t, int64(1768488160), resp.Response.CreatedAt.Int64())
}

func TestResponsesCompactionResponseCreatedAtScientificNotation(t *testing.T) {
payload := `{"id":"resp_1","object":"response","created_at":1.76848816E9}`

var resp OpenAIResponsesCompactionResponse
err := common.UnmarshalJsonStr(payload, &resp)
require.NoError(t, err)
require.Equal(t, int64(1768488160), resp.CreatedAt.Int64())
}
4 changes: 2 additions & 2 deletions relay/channel/openai/chat_via_responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func OaiResponsesToChatStreamHandler(c *gin.Context, info *relaycommon.RelayInfo
model = streamResp.Response.Model
}
if streamResp.Response.CreatedAt != 0 {
createAt = int64(streamResp.Response.CreatedAt)
createAt = streamResp.Response.CreatedAt.Int64()
}
}

Expand Down Expand Up @@ -439,7 +439,7 @@ func OaiResponsesToChatStreamHandler(c *gin.Context, info *relaycommon.RelayInfo
model = streamResp.Response.Model
}
if streamResp.Response.CreatedAt != 0 {
createAt = int64(streamResp.Response.CreatedAt)
createAt = streamResp.Response.CreatedAt.Int64()
}
if streamResp.Response.Usage != nil {
if streamResp.Response.Usage.InputTokens != 0 {
Expand Down