dialect

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package dialect encodes/decodes client wire formats against chat IR.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EncodeGeminiRequest

func EncodeGeminiRequest(req ir.ChatRequest) ([]byte, error)

EncodeGeminiRequest marshals chat IR to a Gemini generateContent request. Provider adapters use the same mapping as the client dialect.

func ParseAnthropicSSE

func ParseAnthropicSSE(r io.Reader) <-chan ir.StreamEvent

ParseAnthropicSSE reads Anthropic messages SSE into IR stream events.

func ParseErrorBody

func ParseErrorBody(body []byte) (message, typ string)

ParseErrorBody extracts a human message and machine type from common upstream error JSON shapes (OpenAI, Anthropic, Gemini, Bedrock). Unknown bodies fall back to the trimmed raw string when it looks like plain text.

func ParseOpenAISSE

func ParseOpenAISSE(r io.Reader) <-chan ir.StreamEvent

ParseOpenAISSE reads OpenAI chat.completion.chunk SSE into IR stream events.

func WriteError

func WriteError(w http.ResponseWriter, d ir.Dialect, status int, message, typ string)

WriteError writes a dialect-shaped JSON error.

func WriteUpstreamError

func WriteUpstreamError(w http.ResponseWriter, d ir.Dialect, status int, body []byte)

WriteUpstreamError remaps an upstream HTTP error body into the client dialect and writes it. Status is preserved; Content-Type is always application/json.

Types

type Anthropic

type Anthropic struct{}

Anthropic is the Anthropic messages dialect codec.

func (Anthropic) DecodeRequest

func (Anthropic) DecodeRequest(body []byte) (ir.ChatRequest, error)

func (Anthropic) EncodeResponse

func (Anthropic) EncodeResponse(resp ir.ChatResponse) ([]byte, error)

func (Anthropic) Name

func (Anthropic) Name() ir.Dialect

func (Anthropic) WriteStream

func (Anthropic) WriteStream(w io.Writer, events <-chan ir.StreamEvent) (prompt, completion int64, err error)

type Codec

type Codec interface {
	Name() ir.Dialect
	DecodeRequest(body []byte) (ir.ChatRequest, error)
	EncodeResponse(resp ir.ChatResponse) ([]byte, error)
	WriteStream(w io.Writer, events <-chan ir.StreamEvent) (prompt, completion int64, err error)
}

Codec converts between a client dialect and chat IR.

func For

func For(d ir.Dialect) (Codec, error)

For returns the codec for a dialect.

type Gemini

type Gemini struct{}

Gemini is the Google Gemini generateContent client dialect codec.

func (Gemini) DecodeRequest

func (Gemini) DecodeRequest(body []byte) (ir.ChatRequest, error)

func (Gemini) EncodeResponse

func (Gemini) EncodeResponse(resp ir.ChatResponse) ([]byte, error)

func (Gemini) Name

func (Gemini) Name() ir.Dialect

func (Gemini) WriteStream

func (Gemini) WriteStream(w io.Writer, events <-chan ir.StreamEvent) (prompt, completion int64, err error)

type OpenAI

type OpenAI struct{}

OpenAI is the OpenAI chat.completions dialect codec.

func (OpenAI) DecodeRequest

func (OpenAI) DecodeRequest(body []byte) (ir.ChatRequest, error)

func (OpenAI) EncodeResponse

func (OpenAI) EncodeResponse(resp ir.ChatResponse) ([]byte, error)

func (OpenAI) Name

func (OpenAI) Name() ir.Dialect

func (OpenAI) WriteStream

func (OpenAI) WriteStream(w io.Writer, events <-chan ir.StreamEvent) (prompt, completion int64, err error)

Jump to

Keyboard shortcuts

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