Documentation
      ¶
    
    
  
    
  
    Overview ¶
Package anthropicadapter streams Anthropic Messages API events and adapts them to our AI‑SDK style SSE parts. Mapping is based on the AI‑SDK data stream protocol (start/text-start/text-delta/text-end, reasoning-*, tool-input-*, finish, finish-step) :contentReference[oaicite:0]{index=0} and Anthropic's Messages + Streaming event schemas (message_start, content_block_start/delta/stop, message_delta, message_stop, error). :contentReference[oaicite:1]{index=1} :contentReference[oaicite:2]{index=2}
NOTE: The public signature in api.txt references wshrpc.WaveAIOptsType; for this self-contained package we define WaveAIOptsType locally with the same shape. Adapt the import/alias as needed in your codebase. :contentReference[oaicite:3]{index=3}
Index ¶
- Constants
 - func HandleAIChat(w http.ResponseWriter, r *http.Request)
 - func IsCloudAIRequest(opts *wshrpc.WaveAIOptsType) bool
 - func MakeWaveAIPacket() *wshrpc.WaveAIPacketType
 - func RunAICommand(ctx context.Context, request wshrpc.WaveAIStreamRequest) chan wshrpc.RespOrErrorUnion[wshrpc.WaveAIPacketType]
 - func StreamOpenAIChatCompletions(sseHandler *SSEHandlerCh, ctx context.Context, opts *wshrpc.WaveAIOptsType, ...)
 - func StreamOpenAIResponsesAPI(sseHandler *SSEHandlerCh, ctx context.Context, opts *wshrpc.WaveAIOptsType, ...)
 - type AIBackend
 - type AnthropicBackend
 - type GoogleBackend
 - type OpenAIBackend
 - type OpenAIStreamChoice
 - type OpenAIStreamResponse
 - type OpenAIUsageResponse
 - type PerplexityBackend
 - type SSEHandlerCh
 - func (h *SSEHandlerCh) AiMsgError(errText string) error
 - func (h *SSEHandlerCh) AiMsgFinish(finishReason string, usage interface{}) error
 - func (h *SSEHandlerCh) AiMsgFinishStep() error
 - func (h *SSEHandlerCh) AiMsgReasoningDelta(reasoningId string, reasoning string) error
 - func (h *SSEHandlerCh) AiMsgReasoningEnd(reasoningId string) error
 - func (h *SSEHandlerCh) AiMsgReasoningStart(reasoningId string) error
 - func (h *SSEHandlerCh) AiMsgStart(messageId string) error
 - func (h *SSEHandlerCh) AiMsgTextDelta(textId string, text string) error
 - func (h *SSEHandlerCh) AiMsgTextEnd(textId string) error
 - func (h *SSEHandlerCh) AiMsgTextStart(textId string) error
 - func (h *SSEHandlerCh) AiMsgToolInputAvailable(toolCallId, toolName string, input json.RawMessage) error
 - func (h *SSEHandlerCh) AiMsgToolInputDelta(toolCallId, inputTextDelta string) error
 - func (h *SSEHandlerCh) AiMsgToolInputStart(toolCallId, toolName string) error
 - func (h *SSEHandlerCh) Close()
 - func (h *SSEHandlerCh) Err() error
 - func (h *SSEHandlerCh) SetupSSE() error
 - func (h *SSEHandlerCh) WriteComment(comment string) error
 - func (h *SSEHandlerCh) WriteData(data string) error
 - func (h *SSEHandlerCh) WriteError(errorMsg string) error
 - func (h *SSEHandlerCh) WriteEvent(eventType, data string) error
 - func (h *SSEHandlerCh) WriteJsonData(data interface{}) error
 
- type SSEMessage
 - type SSEMessageType
 - type StopReason
 - type StopReasonKind
 - type ToolCall
 - type ToolDefinition
 - type UseChatMessage
 - type UseChatMessagePart
 - type UseChatRequest
 - type WaveAICloudBackend
 - type WaveAICloudReqPacketType
 - type WaveAICmdInfoChatMessage
 - type WaveAICmdInfoPacketOutputType
 
Constants ¶
const ( SSEContentType = "text/event-stream" SSECacheControl = "no-cache" SSEConnection = "keep-alive" SSEKeepaliveMsg = ": keepalive\n\n" SSEStreamStartMsg = ": stream-start\n\n" SSEKeepaliveInterval = 1 * time.Second )
const ( AiMsgStart = "start" AiMsgTextStart = "text-start" AiMsgTextDelta = "text-delta" AiMsgTextEnd = "text-end" AiMsgReasoningStart = "reasoning-start" AiMsgReasoningDelta = "reasoning-delta" AiMsgReasoningEnd = "reasoning-end" AiMsgToolInputStart = "tool-input-start" AiMsgToolInputDelta = "tool-input-delta" AiMsgToolInputAvailable = "tool-input-available" AiMsgToolOutputAvailable = "tool-output-available" // not used here, but reserved AiMsgStartStep = "start-step" AiMsgFinishStep = "finish-step" AiMsgFinish = "finish" AiMsgError = "error" )
AI message type constants
const ( AnthropicDefaultBaseURL = "https://api.anthropic.com" AnthropicDefaultAPIVersion = "2023-06-01" AnthropicDefaultMaxTokens = 1024 )
const APIType_Anthropic = "anthropic"
    const APIType_Google = "google"
    const APIType_OpenAI = "openai"
    const APIType_Perplexity = "perplexity"
    const CloudWebsocketConnectTimeout = 1 * time.Minute
    const DefaultAzureAPIVersion = "2023-05-15"
    const OpenAICloudReqStr = "openai-cloudreq"
    const PacketEOFStr = "EOF"
    const WaveAIPacketstr = "waveai"
    Variables ¶
This section is empty.
Functions ¶
func HandleAIChat ¶ added in v0.11.6
func HandleAIChat(w http.ResponseWriter, r *http.Request)
func IsCloudAIRequest ¶ added in v0.8.10
func IsCloudAIRequest(opts *wshrpc.WaveAIOptsType) bool
func MakeWaveAIPacket ¶ added in v0.11.0
func MakeWaveAIPacket() *wshrpc.WaveAIPacketType
func RunAICommand ¶ added in v0.8.10
func RunAICommand(ctx context.Context, request wshrpc.WaveAIStreamRequest) chan wshrpc.RespOrErrorUnion[wshrpc.WaveAIPacketType]
func StreamOpenAIChatCompletions ¶ added in v0.11.6
func StreamOpenAIChatCompletions(sseHandler *SSEHandlerCh, ctx context.Context, opts *wshrpc.WaveAIOptsType, messages []UseChatMessage)
func StreamOpenAIResponsesAPI ¶ added in v0.11.6
func StreamOpenAIResponsesAPI(sseHandler *SSEHandlerCh, ctx context.Context, opts *wshrpc.WaveAIOptsType, messages []UseChatMessage, tools []ToolDefinition)
Types ¶
type AIBackend ¶ added in v0.9.2
type AIBackend interface {
	StreamCompletion(
		ctx context.Context,
		request wshrpc.WaveAIStreamRequest,
	) chan wshrpc.RespOrErrorUnion[wshrpc.WaveAIPacketType]
}
    type AnthropicBackend ¶ added in v0.9.2
type AnthropicBackend struct{}
    func (AnthropicBackend) StreamCompletion ¶ added in v0.9.2
func (AnthropicBackend) StreamCompletion(ctx context.Context, request wshrpc.WaveAIStreamRequest) chan wshrpc.RespOrErrorUnion[wshrpc.WaveAIPacketType]
type GoogleBackend ¶ added in v0.11.0
type GoogleBackend struct{}
    func (GoogleBackend) StreamCompletion ¶ added in v0.11.0
func (GoogleBackend) StreamCompletion(ctx context.Context, request wshrpc.WaveAIStreamRequest) chan wshrpc.RespOrErrorUnion[wshrpc.WaveAIPacketType]
type OpenAIBackend ¶ added in v0.9.2
type OpenAIBackend struct{}
    func (OpenAIBackend) StreamCompletion ¶ added in v0.9.2
func (OpenAIBackend) StreamCompletion(ctx context.Context, request wshrpc.WaveAIStreamRequest) chan wshrpc.RespOrErrorUnion[wshrpc.WaveAIPacketType]
type OpenAIStreamChoice ¶ added in v0.11.6
type OpenAIStreamChoice struct {
	Index int `json:"index"`
	Delta struct {
		Content   string `json:"content,omitempty"`
		Reasoning string `json:"reasoning,omitempty"`
	} `json:"delta"`
	FinishReason *string `json:"finish_reason"`
}
    OpenAI Chat Completion streaming response format
type OpenAIStreamResponse ¶ added in v0.11.6
type OpenAIStreamResponse struct {
	ID      string               `json:"id"`
	Object  string               `json:"object"`
	Created int64                `json:"created"`
	Model   string               `json:"model"`
	Choices []OpenAIStreamChoice `json:"choices"`
	Usage   *OpenAIUsageResponse `json:"usage,omitempty"`
}
    type OpenAIUsageResponse ¶ added in v0.11.6
type PerplexityBackend ¶ added in v0.10.0
type PerplexityBackend struct{}
    func (PerplexityBackend) StreamCompletion ¶ added in v0.10.0
func (PerplexityBackend) StreamCompletion(ctx context.Context, request wshrpc.WaveAIStreamRequest) chan wshrpc.RespOrErrorUnion[wshrpc.WaveAIPacketType]
type SSEHandlerCh ¶ added in v0.11.6
type SSEHandlerCh struct {
	// contains filtered or unexported fields
}
    SSEHandlerCh provides channel-based Server-Sent Events functionality
func MakeSSEHandlerCh ¶ added in v0.11.6
func MakeSSEHandlerCh(w http.ResponseWriter, ctx context.Context) *SSEHandlerCh
MakeSSEHandlerCh creates a new channel-based SSE handler
func (*SSEHandlerCh) AiMsgError ¶ added in v0.11.6
func (h *SSEHandlerCh) AiMsgError(errText string) error
func (*SSEHandlerCh) AiMsgFinish ¶ added in v0.11.6
func (h *SSEHandlerCh) AiMsgFinish(finishReason string, usage interface{}) error
func (*SSEHandlerCh) AiMsgFinishStep ¶ added in v0.11.6
func (h *SSEHandlerCh) AiMsgFinishStep() error
func (*SSEHandlerCh) AiMsgReasoningDelta ¶ added in v0.11.6
func (h *SSEHandlerCh) AiMsgReasoningDelta(reasoningId string, reasoning string) error
func (*SSEHandlerCh) AiMsgReasoningEnd ¶ added in v0.11.6
func (h *SSEHandlerCh) AiMsgReasoningEnd(reasoningId string) error
func (*SSEHandlerCh) AiMsgReasoningStart ¶ added in v0.11.6
func (h *SSEHandlerCh) AiMsgReasoningStart(reasoningId string) error
func (*SSEHandlerCh) AiMsgStart ¶ added in v0.11.6
func (h *SSEHandlerCh) AiMsgStart(messageId string) error
func (*SSEHandlerCh) AiMsgTextDelta ¶ added in v0.11.6
func (h *SSEHandlerCh) AiMsgTextDelta(textId string, text string) error
func (*SSEHandlerCh) AiMsgTextEnd ¶ added in v0.11.6
func (h *SSEHandlerCh) AiMsgTextEnd(textId string) error
func (*SSEHandlerCh) AiMsgTextStart ¶ added in v0.11.6
func (h *SSEHandlerCh) AiMsgTextStart(textId string) error
func (*SSEHandlerCh) AiMsgToolInputAvailable ¶ added in v0.11.6
func (h *SSEHandlerCh) AiMsgToolInputAvailable(toolCallId, toolName string, input json.RawMessage) error
func (*SSEHandlerCh) AiMsgToolInputDelta ¶ added in v0.11.6
func (h *SSEHandlerCh) AiMsgToolInputDelta(toolCallId, inputTextDelta string) error
func (*SSEHandlerCh) AiMsgToolInputStart ¶ added in v0.11.6
func (h *SSEHandlerCh) AiMsgToolInputStart(toolCallId, toolName string) error
func (*SSEHandlerCh) Close ¶ added in v0.11.6
func (h *SSEHandlerCh) Close()
Close closes the write channel, sends [DONE], and cleans up resources
func (*SSEHandlerCh) Err ¶ added in v0.11.6
func (h *SSEHandlerCh) Err() error
Err returns any error that occurred during writing
func (*SSEHandlerCh) SetupSSE ¶ added in v0.11.6
func (h *SSEHandlerCh) SetupSSE() error
SetupSSE configures the response headers and starts the writer goroutine
func (*SSEHandlerCh) WriteComment ¶ added in v0.11.6
func (h *SSEHandlerCh) WriteComment(comment string) error
WriteComment queues an SSE comment
func (*SSEHandlerCh) WriteData ¶ added in v0.11.6
func (h *SSEHandlerCh) WriteData(data string) error
WriteData queues data to be written in SSE format
func (*SSEHandlerCh) WriteError ¶ added in v0.11.6
func (h *SSEHandlerCh) WriteError(errorMsg string) error
WriteError queues an error message and closes the handler
func (*SSEHandlerCh) WriteEvent ¶ added in v0.11.6
func (h *SSEHandlerCh) WriteEvent(eventType, data string) error
WriteEvent queues an SSE event with optional event type
func (*SSEHandlerCh) WriteJsonData ¶ added in v0.11.6
func (h *SSEHandlerCh) WriteJsonData(data interface{}) error
WriteJsonData marshals data to JSON and queues it for writing
type SSEMessage ¶ added in v0.11.6
type SSEMessage struct {
	Type      SSEMessageType
	Data      string
	EventType string // Only used for SSEMsgEvent
}
    SSEMessage represents a message to be written to the SSE stream
type SSEMessageType ¶ added in v0.11.6
type SSEMessageType string
SSEMessageType represents the type of message to write
const ( SSEMsgData SSEMessageType = "data" SSEMsgEvent SSEMessageType = "event" SSEMsgComment SSEMessageType = "comment" SSEMsgError SSEMessageType = "error" )
type StopReason ¶ added in v0.11.6
type StopReason struct {
	Kind      StopReasonKind `json:"kind"`
	RawReason string         `json:"raw_reason,omitempty"`
	MessageID string         `json:"message_id,omitempty"`
	Model     string         `json:"model,omitempty"`
	ToolCalls []ToolCall `json:"tool_calls,omitempty"`
	ErrorType string `json:"error_type,omitempty"`
	ErrorText string `json:"error_text,omitempty"`
	FinishStep bool `json:"finish_step,omitempty"`
}
    func StreamAnthropicResponses ¶ added in v0.11.6
func StreamAnthropicResponses( ctx context.Context, sse *SSEHandlerCh, opts *wshrpc.WaveAIOptsType, messages []UseChatMessage, tools []ToolDefinition, ) (*StopReason, error)
func StreamOpenAIToUseChat ¶ added in v0.11.6
func StreamOpenAIToUseChat(ctx context.Context, sseHandler *SSEHandlerCh, opts *wshrpc.WaveAIOptsType, messages []UseChatMessage, tools []ToolDefinition) (*StopReason, error)
type StopReasonKind ¶ added in v0.11.6
type StopReasonKind string
const ( StopKindDone StopReasonKind = "done" StopKindToolUse StopReasonKind = "tool_use" StopKindMaxTokens StopReasonKind = "max_tokens" StopKindContent StopReasonKind = "content_filter" StopKindCanceled StopReasonKind = "canceled" StopKindError StopReasonKind = "error" )
type ToolDefinition ¶ added in v0.11.6
type ToolDefinition struct {
	Name        string         `json:"name"`
	Description string         `json:"description"`
	InputSchema map[string]any `json:"input_schema"`
}
    ToolDefinition represents a tool that can be used by the AI model
type UseChatMessage ¶ added in v0.11.6
type UseChatMessage struct {
	Role    string               `json:"role"`
	Content string               `json:"content,omitempty"`
	Parts   []UseChatMessagePart `json:"parts,omitempty"`
}
    func (*UseChatMessage) GetContent ¶ added in v0.11.6
func (m *UseChatMessage) GetContent() string
GetContent extracts the text content from either content field or parts array
type UseChatMessagePart ¶ added in v0.11.6
type UseChatRequest ¶ added in v0.11.6
type UseChatRequest struct {
	Messages []UseChatMessage        `json:"messages"`
	Options  *wconfig.AiSettingsType `json:"options,omitempty"`
}
    type WaveAICloudBackend ¶ added in v0.9.2
type WaveAICloudBackend struct{}
    func (WaveAICloudBackend) StreamCompletion ¶ added in v0.9.2
func (WaveAICloudBackend) StreamCompletion(ctx context.Context, request wshrpc.WaveAIStreamRequest) chan wshrpc.RespOrErrorUnion[wshrpc.WaveAIPacketType]
type WaveAICloudReqPacketType ¶ added in v0.11.0
type WaveAICloudReqPacketType struct {
	Type       string                           `json:"type"`
	ClientId   string                           `json:"clientid"`
	Prompt     []wshrpc.WaveAIPromptMessageType `json:"prompt"`
	MaxTokens  int                              `json:"maxtokens,omitempty"`
	MaxChoices int                              `json:"maxchoices,omitempty"`
}
    func MakeWaveAICloudReqPacket ¶ added in v0.11.0
func MakeWaveAICloudReqPacket() *WaveAICloudReqPacketType
type WaveAICmdInfoChatMessage ¶ added in v0.11.0
type WaveAICmdInfoChatMessage struct {
	MessageID           int                            `json:"messageid"`
	IsAssistantResponse bool                           `json:"isassistantresponse,omitempty"`
	AssistantResponse   *WaveAICmdInfoPacketOutputType `json:"assistantresponse,omitempty"`
	UserQuery           string                         `json:"userquery,omitempty"`
	UserEngineeredQuery string                         `json:"userengineeredquery,omitempty"`
}