| package httpclient
|
|
|
| import (
|
| "context"
|
| "encoding/json"
|
| "io"
|
| "net/http"
|
| "net/url"
|
|
|
| "github.com/looplj/axonhub/llm/streams"
|
| )
|
|
|
|
|
| type Request struct {
|
|
|
| Method string `json:"method"`
|
| URL string `json:"url"`
|
| Path string `json:"path"`
|
| Query url.Values `json:"query"`
|
| Headers http.Header `json:"headers"`
|
| ContentType string `json:"content_type"`
|
| Body []byte `json:"body,omitempty"`
|
|
|
|
|
|
|
|
|
|
|
| JSONBody []byte `json:"json_body,omitempty"`
|
|
|
|
|
| Auth *AuthConfig `json:"auth,omitempty"`
|
|
|
|
|
| RequestID string `json:"request_id"`
|
| ClientIP string `json:"client_ip"`
|
|
|
|
|
|
|
|
|
| RequestType string `json:"request_type"`
|
|
|
|
|
| APIFormat string `json:"api_format"`
|
|
|
|
|
| RawRequest *http.Request `json:"-"`
|
|
|
|
|
| Metadata map[string]string `json:"-"`
|
|
|
|
|
|
|
| TransformerMetadata map[string]any `json:"-"`
|
|
|
|
|
|
|
| SkipInboundQueryMerge bool `json:"-"`
|
| }
|
|
|
|
|
| type AuthConfig struct {
|
|
|
|
|
| Type string `json:"type"`
|
|
|
|
|
| APIKey string `json:"api_key,omitempty"`
|
|
|
|
|
| HeaderKey string `json:"header_key,omitempty"`
|
| }
|
|
|
| const (
|
| AuthTypeBearer = "bearer"
|
| AuthTypeAPIKey = "api_key"
|
| )
|
|
|
|
|
| type Response struct {
|
|
|
| StatusCode int `json:"status_code"`
|
|
|
|
|
| Headers http.Header `json:"headers"`
|
|
|
|
|
| Body []byte `json:"body,omitempty"`
|
|
|
|
|
| Stream io.ReadCloser `json:"-"`
|
|
|
|
|
| Request *Request `json:"-"`
|
|
|
|
|
| RawResponse *http.Response `json:"-"`
|
|
|
|
|
| RawRequest *http.Request `json:"-"`
|
| }
|
|
|
| type StreamEvent struct {
|
| LastEventID string `json:"last_event_id,omitempty"`
|
| Type string `json:"type"`
|
| Data []byte `json:"data"`
|
| }
|
|
|
|
|
| type StreamDecoder = streams.Stream[*StreamEvent]
|
|
|
|
|
| type StreamDecoderFactory func(ctx context.Context, rc io.ReadCloser) StreamDecoder
|
|
|
| type _StreamEventJSON struct {
|
| LastEventID string `json:"last_event_id,omitempty"`
|
| Type string `json:"type"`
|
| Data string `json:"data"`
|
| }
|
|
|
| func EncodeStreamEventToJSON(event *StreamEvent) ([]byte, error) {
|
| return json.Marshal(_StreamEventJSON{
|
| LastEventID: event.LastEventID,
|
| Type: event.Type,
|
| Data: string(event.Data),
|
| })
|
| }
|
|
|