Working code with Tensorzero through Supabase proxy
This commit is contained in:
73
agent.go
73
agent.go
@@ -55,10 +55,11 @@ type LinuxDiagnosticAgent struct {
|
||||
|
||||
// NewLinuxDiagnosticAgent creates a new diagnostic agent
|
||||
func NewLinuxDiagnosticAgent() *LinuxDiagnosticAgent {
|
||||
endpoint := os.Getenv("NANNYAPI_ENDPOINT")
|
||||
if endpoint == "" {
|
||||
// Default endpoint - OpenAI SDK will append /chat/completions automatically
|
||||
endpoint = "http://tensorzero.netcup.internal:3000/openai/v1"
|
||||
// Get Supabase project URL for TensorZero proxy
|
||||
supabaseURL := os.Getenv("SUPABASE_PROJECT_URL")
|
||||
if supabaseURL == "" {
|
||||
fmt.Printf("Warning: SUPABASE_PROJECT_URL not set, TensorZero integration will not work\n")
|
||||
supabaseURL = "https://gpqzsricripnvbrpsyws.supabase.co" // fallback
|
||||
}
|
||||
|
||||
model := os.Getenv("NANNYAPI_MODEL")
|
||||
@@ -67,14 +68,9 @@ func NewLinuxDiagnosticAgent() *LinuxDiagnosticAgent {
|
||||
fmt.Printf("Warning: Using default model '%s'. Set NANNYAPI_MODEL environment variable for your specific function.\n", model)
|
||||
}
|
||||
|
||||
// Create OpenAI client with custom base URL
|
||||
// Note: The OpenAI SDK automatically appends "/chat/completions" to the base URL
|
||||
config := openai.DefaultConfig("")
|
||||
config.BaseURL = endpoint
|
||||
client := openai.NewClientWithConfig(config)
|
||||
|
||||
// Note: We don't use the OpenAI client anymore, we use direct HTTP to Supabase proxy
|
||||
agent := &LinuxDiagnosticAgent{
|
||||
client: client,
|
||||
client: nil, // Not used anymore
|
||||
model: model,
|
||||
executor: NewCommandExecutor(10 * time.Second), // 10 second timeout for commands
|
||||
}
|
||||
@@ -195,7 +191,7 @@ type TensorZeroResponse struct {
|
||||
EpisodeID string `json:"episode_id"`
|
||||
}
|
||||
|
||||
// sendRequest sends a request to the TensorZero API with tensorzero::episode_id support
|
||||
// sendRequest sends a request to the TensorZero API via Supabase proxy with JWT authentication
|
||||
func (a *LinuxDiagnosticAgent) sendRequest(messages []openai.ChatCompletionMessage) (*openai.ChatCompletionResponse, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
@@ -223,17 +219,14 @@ func (a *LinuxDiagnosticAgent) sendRequest(messages []openai.ChatCompletionMessa
|
||||
return nil, fmt.Errorf("failed to marshal request: %w", err)
|
||||
}
|
||||
|
||||
// Create HTTP request
|
||||
endpoint := os.Getenv("NANNYAPI_ENDPOINT")
|
||||
if endpoint == "" {
|
||||
endpoint = "http://tensorzero.netcup.internal:3000/openai/v1"
|
||||
// Get Supabase project URL and build TensorZero proxy endpoint
|
||||
supabaseURL := os.Getenv("SUPABASE_PROJECT_URL")
|
||||
if supabaseURL == "" {
|
||||
supabaseURL = "https://gpqzsricripnvbrpsyws.supabase.co"
|
||||
}
|
||||
|
||||
// Ensure the endpoint ends with /chat/completions
|
||||
if endpoint[len(endpoint)-1] != '/' {
|
||||
endpoint += "/"
|
||||
}
|
||||
endpoint += "chat/completions"
|
||||
// Build Supabase function URL with OpenAI v1 compatible path
|
||||
endpoint := supabaseURL + "/functions/v1/tensorzero-proxy/openai/v1/chat/completions"
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", endpoint, bytes.NewBuffer(requestBody))
|
||||
if err != nil {
|
||||
@@ -242,6 +235,14 @@ func (a *LinuxDiagnosticAgent) sendRequest(messages []openai.ChatCompletionMessa
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
// Add JWT authentication header
|
||||
accessToken, err := a.getAccessToken()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get access token: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", "Bearer "+accessToken)
|
||||
|
||||
// Make the request
|
||||
client := &http.Client{Timeout: 30 * time.Second}
|
||||
resp, err := client.Do(req)
|
||||
@@ -257,7 +258,7 @@ func (a *LinuxDiagnosticAgent) sendRequest(messages []openai.ChatCompletionMessa
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, string(body))
|
||||
return nil, fmt.Errorf("TensorZero API request failed with status %d: %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
// Parse TensorZero response
|
||||
@@ -274,3 +275,31 @@ func (a *LinuxDiagnosticAgent) sendRequest(messages []openai.ChatCompletionMessa
|
||||
|
||||
return &tzResponse.ChatCompletionResponse, nil
|
||||
}
|
||||
|
||||
// getAccessToken retrieves the current access token for authentication
|
||||
func (a *LinuxDiagnosticAgent) getAccessToken() (string, error) {
|
||||
// Read token from the standard token file location
|
||||
tokenPath := os.Getenv("TOKEN_PATH")
|
||||
if tokenPath == "" {
|
||||
tokenPath = "/var/lib/nannyagent/token.json"
|
||||
}
|
||||
|
||||
tokenData, err := os.ReadFile(tokenPath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to read token file: %w", err)
|
||||
}
|
||||
|
||||
var tokenInfo struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(tokenData, &tokenInfo); err != nil {
|
||||
return "", fmt.Errorf("failed to parse token file: %w", err)
|
||||
}
|
||||
|
||||
if tokenInfo.AccessToken == "" {
|
||||
return "", fmt.Errorf("access token is empty")
|
||||
}
|
||||
|
||||
return tokenInfo.AccessToken, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user