Documentation
¶
Overview ¶
Package copilot provides chat completion logic for GitHub Copilot AI provider.
Package copilot provides configuration and initialization for GitHub Copilot AI provider.
Package copilot provides error handling with RichError integration for the Copilot provider.
Package copilot provides a GitHub Copilot AI provider implementation. It includes support for chat completions, streaming, tool calling, and GitHub OAuth authentication.
Package copilot provides tool calling support for GitHub Copilot AI provider. Copilot uses OpenAI-compatible tool calling format.
Package copilot provides type definitions for GitHub Copilot AI provider.
Index ¶
- Constants
- func ConvertToolCallsFromDelta(toolCalls []ToolCall) []types.ToolCall
- func ConvertToolCallsFromResponse(toolCalls []ToolCall) []types.ToolCall
- func ConvertToolChoice(toolChoice *types.ToolChoice) interface{}
- func CreateCopilotProvider(config types.ProviderConfig) (types.Provider, error)
- func FormatToolCallArguments(args map[string]interface{}) (string, error)
- func NewConnectivityCache() *connectivityCache
- func NewModelCache(ttl time.Duration) *modelCache
- func ParseToolCallArguments(args string) (map[string]interface{}, error)
- func RegisterCopilotFactory(factory types.ProviderFactory)
- func ValidateToolDefinition(tool types.Tool) error
- type ChatChoice
- type ChatCompletionChunk
- type ChatCompletionRequest
- type ChatCompletionResponse
- type ChatMessage
- type ChunkChoice
- type ContentPart
- type CopilotConfig
- type CopilotErrorDetail
- type CopilotErrorResponse
- type CopilotProvider
- func (p *CopilotProvider) Authenticate(ctx context.Context, authConfig types.AuthConfig) error
- func (p *CopilotProvider) Configure(config types.ProviderConfig) error
- func (p *CopilotProvider) Description() string
- func (p *CopilotProvider) GenerateChatCompletion(ctx context.Context, options types.GenerateOptions) (types.ChatCompletionStream, error)
- func (p *CopilotProvider) GetAuthStatus() map[string]interface{}
- func (p *CopilotProvider) GetBaseURL() string
- func (p *CopilotProvider) GetConfig() types.ProviderConfig
- func (p *CopilotProvider) GetCopilotToken(ctx context.Context) (string, error)
- func (p *CopilotProvider) GetDefaultModel() string
- func (p *CopilotProvider) GetModels(ctx context.Context) ([]types.Model, error)
- func (p *CopilotProvider) GetToolFormat() types.ToolFormat
- func (p *CopilotProvider) HealthCheck(ctx context.Context) error
- func (p *CopilotProvider) InvokeServerTool(ctx context.Context, toolName string, params interface{}) (interface{}, error)
- func (p *CopilotProvider) IsAPIKeyConfigured() bool
- func (p *CopilotProvider) IsAuthenticated() bool
- func (p *CopilotProvider) IsAuthenticationError(err error) bool
- func (p *CopilotProvider) IsClientError(err error) bool
- func (p *CopilotProvider) IsOAuthConfigured() bool
- func (p *CopilotProvider) IsRetryableError(err error) bool
- func (p *CopilotProvider) Logout(ctx context.Context) error
- func (p *CopilotProvider) Name() string
- func (p *CopilotProvider) RefreshAllOAuthTokens(ctx context.Context) error
- func (p *CopilotProvider) SetCredentialProvider(provider types.CredentialProvider)
- func (p *CopilotProvider) SupportsResponsesAPI() bool
- func (p *CopilotProvider) SupportsStreaming() bool
- func (p *CopilotProvider) SupportsToolCalling() bool
- func (p *CopilotProvider) TestConnectivity(ctx context.Context) error
- func (p *CopilotProvider) TestConnectivityWithOptions(ctx context.Context, bypassCache bool) error
- func (p *CopilotProvider) ToProviderError(richErr error) *types.ProviderError
- func (p *CopilotProvider) Type() types.ProviderType
- type CopilotStream
- type CopilotTokenResponse
- type DeltaMessage
- type ErrorDetail
- type ErrorResponse
- type GitHubAccessTokenResponse
- type GitHubDeviceCodeResponse
- type ImageURL
- type ModelCapabilities
- type ModelData
- type ModelLimits
- type ModelSupports
- type ModelsResponse
- type Tool
- type ToolCall
- type ToolCallBuilder
- func (b *ToolCallBuilder) AddToolCall(id, name, arguments string) *ToolCallBuilder
- func (b *ToolCallBuilder) AddToolCallWithMap(id, name string, arguments map[string]interface{}) (*ToolCallBuilder, error)
- func (b *ToolCallBuilder) Build() []ToolCall
- func (b *ToolCallBuilder) ToInternal() []types.ToolCall
- type ToolCallFunction
- type ToolChoice
- type ToolChoiceFunction
- type ToolFunction
- type ToolValidationError
- type Usage
Constants ¶
const ( // OAuth GitHubClientID = "Iv1.b507a08c87ecfe98" GitHubOAuthScopes = "read:user" // GitHub Endpoints GitHubDeviceCodeURL = "https://github.com/login/device/code" GitHubAccessTokenURL = "https://github.com/login/oauth/access_token" GitHubVerificationURL = "https://github.com/login/device" GitHubAPIBaseURL = "https://api.github.com" GitHubCopilotTokenURL = "https://api.github.com/copilot_internal/v2/token" GitHubCopilotUserURL = "https://api.github.com/copilot_internal/user" // Copilot API Endpoints CopilotBaseURL = "https://api.githubcopilot.com" CopilotBusinessBaseURL = "https://api.business.githubcopilot.com" CopilotEnterpriseBaseURL = "https://api.enterprise.githubcopilot.com" // Version Information VSCodeVersion = "1.104.3" CopilotChatVersion = "0.26.7" // Headers EditorPluginVersion = "copilot-chat/0.26.7" UserAgent = "GitHubCopilotChat/0.26.7" CopilotIntegrationID = "vscode-chat" OpenAIIntent = "conversation-panel" GitHubAPIVersion = "2025-04-01" VSCodeUserAgentLibrary = "electron-fetch" // Token Management TokenRefreshBuffer = 60 // seconds before expiry to refresh // Defaults DefaultMaxTokens = 4096 DefaultTemperature = 1.0 DefaultTopP = 1.0 )
Constants for Copilot API
const ( AccountTypeIndividual = "individual" AccountTypeBusiness = "business" AccountTypeEnterprise = "enterprise" )
Account types
Variables ¶
This section is empty.
Functions ¶
func ConvertToolCallsFromDelta ¶
ConvertToolCallsFromDelta converts streaming delta tool calls to internal format
func ConvertToolCallsFromResponse ¶
ConvertToolCallsFromResponse converts Copilot tool calls to internal format
func ConvertToolChoice ¶
func ConvertToolChoice(toolChoice *types.ToolChoice) interface{}
ConvertToolChoice converts internal tool choice to Copilot/OpenAI format
func CreateCopilotProvider ¶
func CreateCopilotProvider(config types.ProviderConfig) (types.Provider, error)
CreateCopilotProvider creates a new Copilot provider instance
func FormatToolCallArguments ¶
FormatToolCallArguments formats tool call arguments as JSON string
func NewConnectivityCache ¶
func NewConnectivityCache() *connectivityCache
NewConnectivityCache creates a new connectivity cache
func NewModelCache ¶
NewModelCache creates a new model cache
func ParseToolCallArguments ¶
ParseToolCallArguments parses tool call arguments from JSON string
func RegisterCopilotFactory ¶
func RegisterCopilotFactory(factory types.ProviderFactory)
RegisterCopilotFactory registers the Copilot provider with the factory
func ValidateToolDefinition ¶
ValidateToolDefinition validates a tool definition
Types ¶
type ChatChoice ¶
type ChatChoice struct {
Index int `json:"index"`
Message ChatMessage `json:"message"`
FinishReason string `json:"finish_reason"`
}
ChatChoice represents a choice in the response
type ChatCompletionChunk ¶
type ChatCompletionChunk struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Choices []ChunkChoice `json:"choices"`
Usage *Usage `json:"usage,omitempty"`
}
ChatCompletionChunk represents a streaming chunk
type ChatCompletionRequest ¶
type ChatCompletionRequest struct {
Model string `json:"model"`
Messages []ChatMessage `json:"messages"`
MaxTokens int `json:"max_tokens,omitempty"`
Temperature float64 `json:"temperature,omitempty"`
TopP float64 `json:"top_p,omitempty"`
Stream bool `json:"stream,omitempty"`
Stop interface{} `json:"stop,omitempty"` // string or []string
Tools []Tool `json:"tools,omitempty"`
ToolChoice interface{} `json:"tool_choice,omitempty"` // "none", "auto", "required", or ToolChoice
FrequencyPenalty float64 `json:"frequency_penalty,omitempty"`
PresencePenalty float64 `json:"presence_penalty,omitempty"`
}
ChatCompletionRequest represents a chat completion request
type ChatCompletionResponse ¶
type ChatCompletionResponse struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Choices []ChatChoice `json:"choices"`
Usage Usage `json:"usage"`
}
ChatCompletionResponse represents a chat completion response
type ChatMessage ¶
type ChatMessage struct {
Role string `json:"role"`
Content interface{} `json:"content"` // Can be string or []ContentPart
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
Name string `json:"name,omitempty"`
}
ChatMessage represents a message in the chat conversation
func CreateAssistantToolCallMessage ¶
func CreateAssistantToolCallMessage(toolCalls []ToolCall, content string) ChatMessage
CreateAssistantToolCallMessage creates an assistant message with tool calls
func CreateToolResponseMessage ¶
func CreateToolResponseMessage(toolCallID, content string) ChatMessage
CreateToolResponseMessage creates a tool response message for the API
type ChunkChoice ¶
type ChunkChoice struct {
Index int `json:"index"`
Delta DeltaMessage `json:"delta"`
FinishReason *string `json:"finish_reason"`
}
ChunkChoice represents a choice in a streaming chunk
type ContentPart ¶
type ContentPart struct {
Type string `json:"type"` // "text" or "image_url"
Text string `json:"text,omitempty"`
ImageURL *ImageURL `json:"image_url,omitempty"`
}
ContentPart represents a part of multimodal content
type CopilotConfig ¶
type CopilotConfig struct {
DisplayName string `json:"display_name,omitempty"`
// GitHub token (from OAuth device flow)
GitHubToken string `json:"github_token,omitempty"`
// Copilot API token (auto-refreshed)
CopilotToken string `json:"copilot_token,omitempty"`
// Account type: individual, business, or enterprise
AccountType string `json:"account_type,omitempty"`
// Base URL override
BaseURL string `json:"base_url,omitempty"`
// Default model
Model string `json:"model,omitempty"`
}
CopilotConfig represents Copilot-specific configuration
type CopilotErrorDetail ¶
type CopilotErrorDetail struct {
Message string `json:"message"`
Type string `json:"type"`
Code string `json:"code,omitempty"`
}
CopilotErrorDetail represents the error details in a Copilot API response.
type CopilotErrorResponse ¶
type CopilotErrorResponse struct {
Error *CopilotErrorDetail `json:"error,omitempty"`
}
CopilotErrorResponse represents a Copilot API error response.
type CopilotProvider ¶
type CopilotProvider struct {
*base.BaseProvider
// contains filtered or unexported fields
}
CopilotProvider implements the Provider interface for GitHub Copilot
func NewCopilotProvider ¶
func NewCopilotProvider(config types.ProviderConfig) *CopilotProvider
NewCopilotProvider creates a new Copilot provider
func (*CopilotProvider) Authenticate ¶
func (p *CopilotProvider) Authenticate(ctx context.Context, authConfig types.AuthConfig) error
Authenticate handles authentication with GitHub OAuth device flow
func (*CopilotProvider) Configure ¶
func (p *CopilotProvider) Configure(config types.ProviderConfig) error
Configure updates the provider configuration
func (*CopilotProvider) Description ¶
func (p *CopilotProvider) Description() string
Description returns a description of the provider
func (*CopilotProvider) GenerateChatCompletion ¶
func (p *CopilotProvider) GenerateChatCompletion( ctx context.Context, options types.GenerateOptions, ) (types.ChatCompletionStream, error)
GenerateChatCompletion generates a chat completion
func (*CopilotProvider) GetAuthStatus ¶
func (p *CopilotProvider) GetAuthStatus() map[string]interface{}
GetAuthStatus provides detailed authentication status
func (*CopilotProvider) GetBaseURL ¶
func (p *CopilotProvider) GetBaseURL() string
GetBaseURL returns the appropriate base URL based on account type or custom base URL
func (*CopilotProvider) GetConfig ¶
func (p *CopilotProvider) GetConfig() types.ProviderConfig
GetConfig returns the current provider configuration
func (*CopilotProvider) GetCopilotToken ¶
func (p *CopilotProvider) GetCopilotToken(ctx context.Context) (string, error)
GetCopilotToken returns the current Copilot token, refreshing if necessary
func (*CopilotProvider) GetDefaultModel ¶
func (p *CopilotProvider) GetDefaultModel() string
GetDefaultModel returns the default model for the provider
func (*CopilotProvider) GetToolFormat ¶
func (p *CopilotProvider) GetToolFormat() types.ToolFormat
GetToolFormat returns the OpenAI tool format (Copilot is OpenAI-compatible)
func (*CopilotProvider) HealthCheck ¶
func (p *CopilotProvider) HealthCheck(ctx context.Context) error
HealthCheck performs a health check on the provider
func (*CopilotProvider) InvokeServerTool ¶
func (p *CopilotProvider) InvokeServerTool( ctx context.Context, toolName string, params interface{}, ) (interface{}, error)
InvokeServerTool invokes a server tool (not yet implemented for Copilot)
func (*CopilotProvider) IsAPIKeyConfigured ¶
func (p *CopilotProvider) IsAPIKeyConfigured() bool
IsAPIKeyConfigured checks if API key authentication is properly configured For Copilot, this means having a valid Copilot token
func (*CopilotProvider) IsAuthenticated ¶
func (p *CopilotProvider) IsAuthenticated() bool
IsAuthenticated checks if the provider is authenticated
func (*CopilotProvider) IsAuthenticationError ¶
func (p *CopilotProvider) IsAuthenticationError(err error) bool
IsAuthenticationError checks if an error is authentication-related.
func (*CopilotProvider) IsClientError ¶
func (p *CopilotProvider) IsClientError(err error) bool
IsClientError checks if an error is a client-side error that won't be fixed by retrying.
func (*CopilotProvider) IsOAuthConfigured ¶
func (p *CopilotProvider) IsOAuthConfigured() bool
IsOAuthConfigured checks if OAuth authentication is properly configured
func (*CopilotProvider) IsRetryableError ¶
func (p *CopilotProvider) IsRetryableError(err error) bool
IsRetryableError checks if an error is potentially retryable. This works with both RichError and standard errors.
func (*CopilotProvider) Logout ¶
func (p *CopilotProvider) Logout(ctx context.Context) error
Logout clears the authentication credentials
func (*CopilotProvider) Name ¶
func (p *CopilotProvider) Name() string
Name returns the display name of the provider
func (*CopilotProvider) RefreshAllOAuthTokens ¶
func (p *CopilotProvider) RefreshAllOAuthTokens(ctx context.Context) error
RefreshAllOAuthTokens refreshes the Copilot token
func (*CopilotProvider) SetCredentialProvider ¶
func (p *CopilotProvider) SetCredentialProvider(provider types.CredentialProvider)
SetCredentialProvider sets a dynamic credential provider for OAuth credentials
func (*CopilotProvider) SupportsResponsesAPI ¶
func (p *CopilotProvider) SupportsResponsesAPI() bool
SupportsResponsesAPI returns false as Copilot doesn't support the Responses API
func (*CopilotProvider) SupportsStreaming ¶
func (p *CopilotProvider) SupportsStreaming() bool
SupportsStreaming returns true if the provider supports streaming
func (*CopilotProvider) SupportsToolCalling ¶
func (p *CopilotProvider) SupportsToolCalling() bool
SupportsToolCalling returns true if the provider supports tool calling
func (*CopilotProvider) TestConnectivity ¶
func (p *CopilotProvider) TestConnectivity(ctx context.Context) error
TestConnectivity performs a lightweight connectivity test
func (*CopilotProvider) TestConnectivityWithOptions ¶
func (p *CopilotProvider) TestConnectivityWithOptions(ctx context.Context, bypassCache bool) error
TestConnectivityWithOptions performs a connectivity test with optional cache bypass
func (*CopilotProvider) ToProviderError ¶
func (p *CopilotProvider) ToProviderError(richErr error) *types.ProviderError
ToProviderError converts a RichError to types.ProviderError for backward compatibility.
func (*CopilotProvider) Type ¶
func (p *CopilotProvider) Type() types.ProviderType
Type returns the provider type
type CopilotStream ¶
type CopilotStream struct {
// contains filtered or unexported fields
}
CopilotStream implements ChatCompletionStream for Copilot SSE streaming
func NewCopilotStream ¶
func NewCopilotStream(resp *http.Response) *CopilotStream
NewCopilotStream creates a new Copilot stream
func (*CopilotStream) Next ¶
func (s *CopilotStream) Next() (types.ChatCompletionChunk, error)
Next returns the next chunk from the stream
type CopilotTokenResponse ¶
type CopilotTokenResponse struct {
Token string `json:"token"`
ExpiresAt int64 `json:"expires_at"`
RefreshIn int `json:"refresh_in"`
}
CopilotTokenResponse represents the response from Copilot token endpoint
type DeltaMessage ¶
type DeltaMessage struct {
Role string `json:"role,omitempty"`
Content string `json:"content,omitempty"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
}
DeltaMessage represents a delta in streaming
type ErrorDetail ¶
ErrorDetail represents error details
type ErrorResponse ¶
type ErrorResponse struct {
Error ErrorDetail `json:"error"`
}
ErrorResponse represents an error response
type GitHubAccessTokenResponse ¶
type GitHubAccessTokenResponse struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
Scope string `json:"scope"`
Error string `json:"error,omitempty"`
ErrorDescription string `json:"error_description,omitempty"`
}
GitHubAccessTokenResponse represents the response from GitHub access token endpoint
type GitHubDeviceCodeResponse ¶
type GitHubDeviceCodeResponse struct {
DeviceCode string `json:"device_code"`
UserCode string `json:"user_code"`
VerificationURI string `json:"verification_uri"`
ExpiresIn int `json:"expires_in"`
Interval int `json:"interval"`
}
GitHubDeviceCodeResponse represents the response from GitHub device code endpoint
type ImageURL ¶
type ImageURL struct {
URL string `json:"url"`
Detail string `json:"detail,omitempty"` // "low", "high", or "auto"
}
ImageURL represents an image URL for vision
type ModelCapabilities ¶
type ModelCapabilities struct {
Object string `json:"object"`
Family string `json:"family"`
Limits ModelLimits `json:"limits"`
Supports ModelSupports `json:"supports"`
Tokenizer string `json:"tokenizer"`
Type string `json:"type"`
}
ModelCapabilities represents model capabilities
type ModelData ¶
type ModelData struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
OwnedBy string `json:"owned_by"`
Name string `json:"name"`
Vendor string `json:"vendor"`
Version string `json:"version"`
Preview bool `json:"preview"`
ModelPickerEnabled bool `json:"model_picker_enabled"`
Capabilities ModelCapabilities `json:"capabilities"`
}
ModelData represents a model in the models list
type ModelLimits ¶
type ModelLimits struct {
MaxContextWindowTokens int `json:"max_context_window_tokens"`
MaxOutputTokens int `json:"max_output_tokens"`
MaxPromptTokens int `json:"max_prompt_tokens"`
}
ModelLimits represents model limits
type ModelSupports ¶
type ModelSupports struct {
ToolCalls bool `json:"tool_calls"`
ParallelToolCalls bool `json:"parallel_tool_calls"`
}
ModelSupports represents what the model supports
type ModelsResponse ¶
ModelsResponse represents the response from the models endpoint
type Tool ¶
type Tool struct {
Type string `json:"type"` // "function"
Function ToolFunction `json:"function"`
}
Tool represents a tool definition
func ConvertTools ¶
ConvertTools converts internal tools to Copilot/OpenAI format
type ToolCall ¶
type ToolCall struct {
ID string `json:"id"`
Type string `json:"type"` // "function"
Function ToolCallFunction `json:"function"`
}
ToolCall represents a tool call in a response
type ToolCallBuilder ¶
type ToolCallBuilder struct {
// contains filtered or unexported fields
}
ToolCallBuilder helps build tool call responses
func NewToolCallBuilder ¶
func NewToolCallBuilder() *ToolCallBuilder
NewToolCallBuilder creates a new tool call builder
func (*ToolCallBuilder) AddToolCall ¶
func (b *ToolCallBuilder) AddToolCall(id, name, arguments string) *ToolCallBuilder
AddToolCall adds a tool call to the builder
func (*ToolCallBuilder) AddToolCallWithMap ¶
func (b *ToolCallBuilder) AddToolCallWithMap(id, name string, arguments map[string]interface{}) (*ToolCallBuilder, error)
AddToolCallWithMap adds a tool call with arguments as a map
func (*ToolCallBuilder) Build ¶
func (b *ToolCallBuilder) Build() []ToolCall
Build returns the tool calls
func (*ToolCallBuilder) ToInternal ¶
func (b *ToolCallBuilder) ToInternal() []types.ToolCall
ToInternal converts to internal tool call format
type ToolCallFunction ¶
ToolCallFunction represents a function call
type ToolChoice ¶
type ToolChoice struct {
Type string `json:"type"` // "function"
Function ToolChoiceFunction `json:"function"`
}
ToolChoice represents a specific tool choice
type ToolChoiceFunction ¶
type ToolChoiceFunction struct {
Name string `json:"name"`
}
ToolChoiceFunction represents a function choice
type ToolFunction ¶
type ToolFunction struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Parameters map[string]interface{} `json:"parameters"`
}
ToolFunction represents a function definition
type ToolValidationError ¶
ToolValidationError represents a tool validation error
func (*ToolValidationError) Error ¶
func (e *ToolValidationError) Error() string