Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

5.9 KB
Raw Blame
package bench

import (
	"bytes"
	"context"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"time"
)

const OpenAIResponsesURL = "https://api.openai.com/v1/responses"

type OpenAIRunner struct {
	baseURL       string
	apiKey        string
	model         string
	maxTokens     int
	temperature   float64
	promptCaching bool
	usage         UsageCounters
	client        *http.Client
	retryCfg      RetryConfig
}

func NewOpenAIRunner(apiKey, model string, maxTokens int, temperature float64, promptCaching bool) *OpenAIRunner {
	return &OpenAIRunner{
		baseURL:       OpenAIResponsesURL,
		apiKey:        apiKey,
		model:         model,
		maxTokens:     maxTokens,
		temperature:   temperature,
		promptCaching: promptCaching,
		client:        &http.Client{Timeout: 5 * time.Minute},
		retryCfg:      DefaultRetryConfig(),
	}
}

func (r *OpenAIRunner) Provider() string     { return "openai" }
func (r *OpenAIRunner) Source() string       { return "openai-responses" }
func (r *OpenAIRunner) Model() string        { return r.model }
func (r *OpenAIRunner) Usage() UsageCounters { return r.usage }

func (r *OpenAIRunner) ToolDefinitions() interface{} {
	return []map[string]interface{}{
		{
			"type":        "function",
			"name":        "run_command",
			"description": "Run a shell command in a persistent bash session rooted at tests/tools/. Use ./scripts/pt or ./scripts/ab directly.",
			"parameters": map[string]interface{}{
				"type": "object",
				"properties": map[string]interface{}{
					"command":         map[string]interface{}{"type": "string"},
					"timeout_seconds": map[string]interface{}{"type": "integer", "minimum": 1, "maximum": 600},
				},
				"required": []string{"command"},
			},
		},
	}
}

func (r *OpenAIRunner) InitialConversation(userPrompt string) []interface{} {
	return []interface{}{
		map[string]interface{}{
			"role": "user",
			"content": []interface{}{
				map[string]interface{}{"type": "input_text", "text": userPrompt},
			},
		},
	}
}

func (r *OpenAIRunner) Send(systemPrompt string, conversation []interface{}) (interface{}, error) {
	body := map[string]interface{}{
		"model":             r.model,
		"instructions":      systemPrompt,
		"tools":             r.ToolDefinitions(),
		"input":             conversation,
		"max_output_tokens": r.maxTokens,
	}
	if r.promptCaching {
		body["prompt_cache_retention"] = "24h"
	}

	payload, err := json.Marshal(body)
	if err != nil {
		return nil, fmt.Errorf("marshal request: %w", err)
	}

	var result map[string]interface{}
	resp, err := DoWithRetry(context.Background(), r.retryCfg, func() (*http.Response, error) {
		req, err := http.NewRequest("POST", r.baseURL, bytes.NewReader(payload))
		if err != nil {
			return nil, err
		}
		req.Header.Set("Content-Type", "application/json")
		req.Header.Set("Authorization", "Bearer "+r.apiKey)
		return r.client.Do(req)
	})
	if err != nil {
		return nil, err
	}
	defer func() { _ = resp.Body.Close() }()

	respBody, err := io.ReadAll(resp.Body)
	if err != nil {
		return nil, fmt.Errorf("read response: %w", err)
	}

	if err := json.Unmarshal(respBody, &result); err != nil {
		return nil, fmt.Errorf("unmarshal response: %w", err)
	}

	if resp.StatusCode >= 400 {
		return nil, fmt.Errorf("OpenAI API error %d: %s", resp.StatusCode, string(respBody))
	}

	r.updateUsage(result)
	return result, nil
}

func (r *OpenAIRunner) updateUsage(result map[string]interface{}) {
	usage, _ := result["usage"].(map[string]interface{})
	if usage == nil {
		return
	}
	r.usage.RequestCount++

	totalInput := toInt(usage["input_tokens"])
	cached := 0
	if details, ok := usage["input_tokens_details"].(map[string]interface{}); ok {
		cached = toInt(details["cached_tokens"])
	}
	r.usage.InputTokens += max(0, totalInput-cached)
	r.usage.OutputTokens += toInt(usage["output_tokens"])
	r.usage.CacheReadInputTokens += cached
}

func (r *OpenAIRunner) ExtractToolCalls(response interface{}, defaultTimeout time.Duration) []ToolCall {
	resp, _ := response.(map[string]interface{})
	output, _ := resp["output"].([]interface{})
	var calls []ToolCall
	for _, item := range output {
		m, _ := item.(map[string]interface{})
		if m["type"] != "function_call" {
			continue
		}
		argsStr, _ := m["arguments"].(string)
		var args map[string]interface{}
		if argsStr != "" {
			// On parse failure args stays nil and the tool call is skipped below.
			_ = json.Unmarshal([]byte(argsStr), &args)
		}
		cmd, _ := args["command"].(string)
		if cmd == "" {
			continue
		}
		timeout := int(defaultTimeout.Seconds())
		if t := toInt(args["timeout_seconds"]); t > 0 {
			timeout = t
		}
		callID := fmt.Sprintf("%v", m["call_id"])
		if callID == "" || callID == "<nil>" {
			callID = fmt.Sprintf("%v", m["id"])
		}
		calls = append(calls, ToolCall{
			ID:             callID,
			Command:        cmd,
			TimeoutSeconds: timeout,
		})
	}
	return calls
}

func (r *OpenAIRunner) AppendToolResults(conversation []interface{}, response interface{}, results []ToolExecutionResult) []interface{} {
	resp, _ := response.(map[string]interface{})
	output, _ := resp["output"].([]interface{})
	conversation = append(conversation, output...)
	for _, res := range results {
		conversation = append(conversation, map[string]interface{}{
			"type":    "function_call_output",
			"call_id": res.ID,
			"output":  res.Content,
		})
	}
	return conversation
}

func (r *OpenAIRunner) ExtractFinalText(response interface{}) string {
	resp, _ := response.(map[string]interface{})

	if text, ok := resp["output_text"].(string); ok && text != "" {
		return text
	}

	output, _ := resp["output"].([]interface{})
	var texts []string
	for _, item := range output {
		m, _ := item.(map[string]interface{})
		if m["type"] != "message" {
			continue
		}
		content, _ := m["content"].([]interface{})
		for _, part := range content {
			p, _ := part.(map[string]interface{})
			ptype, _ := p["type"].(string)
			if ptype == "output_text" || ptype == "text" {
				if text, ok := p["text"].(string); ok && text != "" {
					texts = append(texts, text)
				}
			}
		}
	}
	return joinStrings(texts, "\n")
}