Documentation
¶
Index ¶
- func MessagesToAnthropic(messages []Message) ([]anthropic.MessageParam, []anthropic.TextBlockParam, error)
- func MessagesToGoogle(messages []Message) ([]*genai.Content, error)
- func MessagesToOpenAI(messages []Message) ([]openai.ChatCompletionMessageParamUnion, error)
- func ToolsToAnthropic(tools []Tool) []anthropic.ToolUnionParam
- func ToolsToGoogle(tools []Tool) ([]*genai.Tool, error)
- func ToolsToOpenAI(tools []Tool) []openai.ChatCompletionToolParam
- func WriteDataStreamHeaders(w http.ResponseWriter)
- type Attachment
- type Chat
- type DataStream
- type DataStreamAccumulator
- type DataStreamDataPart
- type DataStreamPart
- type ErrorStreamPart
- type FileStreamPart
- type FinishMessageStreamPart
- type FinishReason
- type FinishStepStreamPart
- type GoogleStreamIterator
- type Message
- type MessageAnnotationStreamPart
- type Part
- type PartType
- type ReasoningDetail
- type ReasoningSignatureStreamPart
- type ReasoningStreamPart
- type RedactedReasoningStreamPart
- type Schema
- type SourceInfo
- type SourceStreamPart
- type StartStepStreamPart
- type TextStreamPart
- type Tool
- type ToolCall
- type ToolCallDeltaStreamPart
- type ToolCallResult
- type ToolCallStartStreamPart
- type ToolCallStreamPart
- type ToolInvocation
- type ToolInvocationState
- type ToolResultStreamPart
- type Usage
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MessagesToAnthropic ¶
func MessagesToAnthropic(messages []Message) ([]anthropic.MessageParam, []anthropic.TextBlockParam, error)
MessagesToAnthropic converts internal message format to Anthropic's API format. It extracts system messages into a separate slice of TextBlockParams and groups consecutive user/tool and assistant messages according to Anthropic's rules. It handles the case where a single assistant message part contains both the tool call and its result, splitting them into the required assistant tool_use and user tool_result blocks.
func MessagesToGoogle ¶
MessagesToGoogle converts internal message format to Google's genai.Content slice. System messages are ignored.
func MessagesToOpenAI ¶
MessagesToOpenAI converts internal message format to OpenAI's API format.
func ToolsToAnthropic ¶
func ToolsToAnthropic(tools []Tool) []anthropic.ToolUnionParam
ToolsToAnthropic converts the tool format to Anthropic's API format.
func ToolsToOpenAI ¶
func ToolsToOpenAI(tools []Tool) []openai.ChatCompletionToolParam
ToolsToOpenAI converts the tool format to OpenAI's API format.
func WriteDataStreamHeaders ¶
func WriteDataStreamHeaders(w http.ResponseWriter)
Types ¶
type Attachment ¶
type Chat ¶
Chat is the structure sent from `useChat` to the server. This can be extended if you'd like to send additional data with `body`.
type DataStream ¶
type DataStream iter.Seq2[DataStreamPart, error]
DataStream is a stream of DataStreamParts.
func AnthropicToDataStream ¶
func AnthropicToDataStream(stream *ssestream.Stream[anthropic.MessageStreamEventUnion]) DataStream
AnthropicToDataStream pipes an Anthropic stream to a DataStream.
func GoogleToDataStream ¶
func GoogleToDataStream(stream iter.Seq2[*genai.GenerateContentResponse, error]) DataStream
GoogleToDataStream pipes a Google AI stream to a DataStream.
func OpenAIToDataStream ¶
func OpenAIToDataStream(stream *ssestream.Stream[openai.ChatCompletionChunk]) DataStream
OpenAIToDataStream pipes an OpenAI stream to a DataStream.
func (DataStream) Pipe ¶
func (s DataStream) Pipe(w io.Writer) error
Pipe iterates over the DataStream and writes the parts to the writer.
func (DataStream) WithAccumulator ¶
func (s DataStream) WithAccumulator(accumulator *DataStreamAccumulator) DataStream
WithAccumulator passes parts to the accumulator which aggregates them into a single message.
func (DataStream) WithToolCalling ¶
func (s DataStream) WithToolCalling(handleToolCall func(toolCall ToolCall) any) DataStream
WithToolCalling passes tool calls to the handleToolCall function.
type DataStreamAccumulator ¶
type DataStreamAccumulator struct {
// contains filtered or unexported fields
}
DataStreamAccumulator accumulates DataStreamParts into Messages.
func (*DataStreamAccumulator) FinishReason ¶
func (a *DataStreamAccumulator) FinishReason() FinishReason
func (*DataStreamAccumulator) Messages ¶
func (a *DataStreamAccumulator) Messages() []Message
func (*DataStreamAccumulator) Push ¶
func (a *DataStreamAccumulator) Push(part DataStreamPart) error
func (*DataStreamAccumulator) Usage ¶
func (a *DataStreamAccumulator) Usage() Usage
type DataStreamDataPart ¶
type DataStreamDataPart struct {
Content []any
}
DataStreamDataPart corresponds to TYPE_ID '2'.
func (DataStreamDataPart) Format ¶
func (p DataStreamDataPart) Format() (string, error)
func (DataStreamDataPart) TypeID ¶
func (p DataStreamDataPart) TypeID() byte
type DataStreamPart ¶
DataStreamPart represents a part of the Vercel AI SDK data stream.
type ErrorStreamPart ¶
type ErrorStreamPart struct {
Content string
}
ErrorStreamPart corresponds to TYPE_ID '3'.
func (ErrorStreamPart) Format ¶
func (p ErrorStreamPart) Format() (string, error)
func (ErrorStreamPart) TypeID ¶
func (p ErrorStreamPart) TypeID() byte
type FileStreamPart ¶
FileStreamPart corresponds to TYPE_ID 'k'.
func (FileStreamPart) Format ¶
func (p FileStreamPart) Format() (string, error)
func (FileStreamPart) TypeID ¶
func (p FileStreamPart) TypeID() byte
type FinishMessageStreamPart ¶
type FinishMessageStreamPart struct { FinishReason FinishReason `json:"finishReason"` Usage Usage `json:"usage"` }
FinishMessageStreamPart corresponds to TYPE_ID 'd'.
func (FinishMessageStreamPart) Format ¶
func (p FinishMessageStreamPart) Format() (string, error)
func (FinishMessageStreamPart) TypeID ¶
func (p FinishMessageStreamPart) TypeID() byte
type FinishReason ¶
type FinishReason string
FinishReason defines the possible reasons for finishing a step or message.
const ( FinishReasonStop FinishReason = "stop" FinishReasonLength FinishReason = "length" FinishReasonContentFilter FinishReason = "content-filter" FinishReasonToolCalls FinishReason = "tool-calls" FinishReasonError FinishReason = "error" FinishReasonOther FinishReason = "other" FinishReasonUnknown FinishReason = "unknown" )
type FinishStepStreamPart ¶
type FinishStepStreamPart struct { FinishReason FinishReason `json:"finishReason"` Usage Usage `json:"usage"` IsContinued bool `json:"isContinued"` }
FinishStepStreamPart corresponds to TYPE_ID 'e'.
func (FinishStepStreamPart) Format ¶
func (p FinishStepStreamPart) Format() (string, error)
func (FinishStepStreamPart) TypeID ¶
func (p FinishStepStreamPart) TypeID() byte
type GoogleStreamIterator ¶
type GoogleStreamIterator interface {
Next() (*genai.GenerateContentResponse, error)
}
GoogleStreamIterator defines the interface for iterating over Google AI stream responses. This allows for mocking in tests.
type Message ¶
type Message struct { ID string `json:"id"` CreatedAt *json.RawMessage `json:"createdAt,omitempty"` Content string `json:"content"` Role string `json:"role"` Parts []Part `json:"parts,omitempty"` Annotations []any `json:"annotations,omitempty"` Attachments []Attachment `json:"experimental_attachments,omitempty"` }
type MessageAnnotationStreamPart ¶
type MessageAnnotationStreamPart struct {
Content []any
}
MessageAnnotationStreamPart corresponds to TYPE_ID '8'.
func (MessageAnnotationStreamPart) Format ¶
func (p MessageAnnotationStreamPart) Format() (string, error)
func (MessageAnnotationStreamPart) TypeID ¶
func (p MessageAnnotationStreamPart) TypeID() byte
type Part ¶
type Part struct { Type PartType `json:"type"` // Type: "text" Text string `json:"text,omitempty"` // Type: "reasoning" Reasoning string `json:"reasoning,omitempty"` Details []ReasoningDetail `json:"details,omitempty"` // Type: "tool-invocation" ToolInvocation *ToolInvocation `json:"toolInvocation,omitempty"` // Type: "source" Source *SourceInfo `json:"source,omitempty"` // Type: "file" MimeType string `json:"mimeType,omitempty"` Data []byte `json:"data,omitempty"` // contains filtered or unexported fields }
type ReasoningDetail ¶
type ReasoningSignatureStreamPart ¶
type ReasoningSignatureStreamPart struct {
Signature string `json:"signature"`
}
ReasoningSignatureStreamPart corresponds to TYPE_ID 'j'.
func (ReasoningSignatureStreamPart) Format ¶
func (p ReasoningSignatureStreamPart) Format() (string, error)
func (ReasoningSignatureStreamPart) TypeID ¶
func (p ReasoningSignatureStreamPart) TypeID() byte
type ReasoningStreamPart ¶
type ReasoningStreamPart struct {
Content string
}
ReasoningStreamPart corresponds to TYPE_ID 'g'.
func (ReasoningStreamPart) Format ¶
func (p ReasoningStreamPart) Format() (string, error)
func (ReasoningStreamPart) TypeID ¶
func (p ReasoningStreamPart) TypeID() byte
type RedactedReasoningStreamPart ¶
type RedactedReasoningStreamPart struct {
Data string `json:"data"`
}
RedactedReasoningStreamPart corresponds to TYPE_ID 'i'.
func (RedactedReasoningStreamPart) Format ¶
func (p RedactedReasoningStreamPart) Format() (string, error)
func (RedactedReasoningStreamPart) TypeID ¶
func (p RedactedReasoningStreamPart) TypeID() byte
type SourceInfo ¶
type SourceStreamPart ¶
type SourceStreamPart struct { SourceType string `json:"sourceType"` ID string `json:"id"` URL string `json:"url"` Title string `json:"title"` }
SourceStreamPart corresponds to TYPE_ID 'h'.
func (SourceStreamPart) Format ¶
func (p SourceStreamPart) Format() (string, error)
func (SourceStreamPart) TypeID ¶
func (p SourceStreamPart) TypeID() byte
type StartStepStreamPart ¶
type StartStepStreamPart struct {
MessageID string `json:"messageId"`
}
StartStepStreamPart corresponds to TYPE_ID 'f'.
func (StartStepStreamPart) Format ¶
func (p StartStepStreamPart) Format() (string, error)
func (StartStepStreamPart) TypeID ¶
func (p StartStepStreamPart) TypeID() byte
type TextStreamPart ¶
type TextStreamPart struct {
Content string
}
TextStreamPart corresponds to TYPE_ID '0'.
func (TextStreamPart) Format ¶
func (p TextStreamPart) Format() (string, error)
func (TextStreamPart) TypeID ¶
func (p TextStreamPart) TypeID() byte
type ToolCall ¶
type ToolCall struct { ID string `json:"id"` Name string `json:"name"` Args map[string]any `json:"args"` }
ToolCall represents a tool call *request*.
type ToolCallDeltaStreamPart ¶
type ToolCallDeltaStreamPart struct { ToolCallID string `json:"toolCallId"` ArgsTextDelta string `json:"argsTextDelta"` }
ToolCallDeltaStreamPart corresponds to TYPE_ID 'c'.
func (ToolCallDeltaStreamPart) Format ¶
func (p ToolCallDeltaStreamPart) Format() (string, error)
func (ToolCallDeltaStreamPart) TypeID ¶
func (p ToolCallDeltaStreamPart) TypeID() byte
type ToolCallResult ¶
type ToolCallStartStreamPart ¶
type ToolCallStartStreamPart struct { ToolCallID string `json:"toolCallId"` ToolName string `json:"toolName"` }
ToolCallStartStreamPart corresponds to TYPE_ID 'b'.
func (ToolCallStartStreamPart) Format ¶
func (p ToolCallStartStreamPart) Format() (string, error)
func (ToolCallStartStreamPart) TypeID ¶
func (p ToolCallStartStreamPart) TypeID() byte
type ToolCallStreamPart ¶
type ToolCallStreamPart struct { ToolCallID string `json:"toolCallId"` ToolName string `json:"toolName"` Args map[string]any `json:"args"` }
ToolCallStreamPart corresponds to TYPE_ID '9'.
func (ToolCallStreamPart) Format ¶
func (p ToolCallStreamPart) Format() (string, error)
func (ToolCallStreamPart) TypeID ¶
func (p ToolCallStreamPart) TypeID() byte
type ToolInvocation ¶
type ToolInvocationState ¶
type ToolInvocationState string
const ( ToolInvocationStateCall ToolInvocationState = "call" ToolInvocationStatePartialCall ToolInvocationState = "partial-call" ToolInvocationStateResult ToolInvocationState = "result" )
type ToolResultStreamPart ¶
type ToolResultStreamPart struct { ToolCallID string `json:"toolCallId"` Result any `json:"result"` }
ToolResultStreamPart corresponds to TYPE_ID 'a'.
func (ToolResultStreamPart) Format ¶
func (p ToolResultStreamPart) Format() (string, error)
func (ToolResultStreamPart) TypeID ¶
func (p ToolResultStreamPart) TypeID() byte