opencodego

package
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 4, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package opencodego holds shared constants and helpers for the OpenCode Go gateway (https://opencode.ai/docs/go/). Models are discovered via GET /v1/models; chat routing picks /v1/chat/completions or /v1/messages per model family.

Index

Constants

View Source
const DefaultBaseURL = "https://opencode.ai/zen/go/v1"

DefaultBaseURL is the OpenCode Go API root.

Variables

View Source
var KnownModels = map[string]ModelMetadata{

	"minimax-m3": {
		Protocol: "anthropic", InputPer1M: 0.30, OutputPer1M: 1.20,
		CachedRead: 0.06, Context: 128000, MaxOutput: 8000,
	},
	"minimax-m2.7": {
		Protocol: "anthropic", InputPer1M: 0.30, OutputPer1M: 1.20,
		CachedRead: 0.06, CachedWrite: 0.375, Context: 128000, MaxOutput: 8000,
	},
	"minimax-m2.5": {
		Protocol: "anthropic", InputPer1M: 0.30, OutputPer1M: 1.20,
		CachedRead: 0.06, CachedWrite: 0.375, Context: 128000, MaxOutput: 8000,
	},
	"qwen3.7-max": {
		Protocol: "anthropic", InputPer1M: 2.50, OutputPer1M: 7.50,
		CachedRead: 0.50, CachedWrite: 3.125, Context: 128000, MaxOutput: 8000,
	},
	"qwen3.7-plus": {
		Protocol: "anthropic", InputPer1M: 0.40, OutputPer1M: 1.60,
		CachedRead: 0.04, CachedWrite: 0.50, Context: 128000, MaxOutput: 8000,
		TierThreshold: 256000, TieredInputPer1M: 1.20, TieredOutputPer1M: 4.80,
		TieredCachedRead: 0.12, TieredCachedWrite: 1.50,
	},
	"qwen3.6-plus": {
		Protocol: "anthropic", InputPer1M: 0.50, OutputPer1M: 3.00,
		CachedRead: 0.05, CachedWrite: 0.625, Context: 128000, MaxOutput: 8000,
		TierThreshold: 256000, TieredInputPer1M: 2.00, TieredOutputPer1M: 6.00,
		TieredCachedRead: 0.20, TieredCachedWrite: 2.50,
	},
	"qwen3.5-plus": {
		Protocol: "anthropic", InputPer1M: 0.50, OutputPer1M: 3.00,
		CachedRead: 0.05, Context: 128000, MaxOutput: 8000,
	},

	"glm-5.1": {
		Protocol: "openai", InputPer1M: 1.40, OutputPer1M: 4.40,
		CachedRead: 0.26, Context: 128000, MaxOutput: 8000,
	},
	"glm-5": {
		Protocol: "openai", InputPer1M: 1.00, OutputPer1M: 3.20,
		CachedRead: 0.20, Context: 128000, MaxOutput: 8000,
	},
	"kimi-k2.7-code": {
		Protocol: "openai", InputPer1M: 0.95, OutputPer1M: 4.00,
		CachedRead: 0.19, Context: 128000, MaxOutput: 8000,
	},
	"kimi-k2.6": {
		Protocol: "openai", InputPer1M: 0.95, OutputPer1M: 4.00,
		CachedRead: 0.16, Context: 128000, MaxOutput: 8000,
	},
	"kimi-k2.5": {
		Protocol: "openai", InputPer1M: 0.95, OutputPer1M: 4.00,
		Context: 128000, MaxOutput: 8000,
	},
	"deepseek-v4-pro": {
		Protocol: "openai", InputPer1M: 1.74, OutputPer1M: 3.48,
		CachedRead: 0.0145, Context: 128000, MaxOutput: 8000,
	},
	"deepseek-v4-flash": {
		Protocol: "openai", InputPer1M: 0.14, OutputPer1M: 0.28,
		CachedRead: 0.0028, Context: 128000, MaxOutput: 8000,
	},
	"mimo-v2.5": {
		Protocol: "openai", InputPer1M: 0.14, OutputPer1M: 0.28,
		CachedRead: 0.0028, Context: 128000, MaxOutput: 8000,
	},
	"mimo-v2.5-pro": {
		Protocol: "openai", InputPer1M: 1.74, OutputPer1M: 3.48,
		CachedRead: 0.0145, Context: 128000, MaxOutput: 8000,
	},
	"mimo-v2-pro": {
		Protocol: "openai", InputPer1M: 1.74, OutputPer1M: 3.48,
		Context: 128000, MaxOutput: 8000,
	},
	"mimo-v2-omni": {
		Protocol: "openai", InputPer1M: 0.14, OutputPer1M: 0.28,
		Context: 128000, MaxOutput: 8000,
	},
	"hy3-preview": {
		Protocol: "openai", InputPer1M: 0.50, OutputPer1M: 2.00,
		Context: 128000, MaxOutput: 8000,
	},
}

KnownModels is the static metadata map keyed by model ID. Merge with live /v1/models response — new models not listed here still work, just without pricing/protocol metadata.

Functions

func AllKnownModelIDs

func AllKnownModelIDs() []string

AllKnownModelIDs returns all model IDs from the static metadata.

func EstimateCost

func EstimateCost(model string, inputTokens, outputTokens int, inputPricePer1M, outputPricePer1M float64) float64

EstimateCost estimates the USD cost for a request given token counts and model pricing.

func EstimateCostTiered

func EstimateCostTiered(inputTokens, outputTokens int, base, tiered PricingTier, tierThreshold int) float64

EstimateCostTiered estimates USD cost with tiered pricing support. When tierThreshold > 0 and total input tokens exceed it, the higher rate applies to the portion above the threshold. Same for output tokens.

func FormatStatus

func FormatStatus(s UsageStatus) string

FormatStatus returns a human-readable spend summary.

func NativeModelID

func NativeModelID(id string) string

NativeModelID strips OpenCode config prefixes (opencode-go/kimi-k2.6 → kimi-k2.6).

func ProtocolForModel

func ProtocolForModel(modelID string) string

ProtocolForModel derives the API protocol from model name patterns. Returns "anthropic" or "openai". This is the heuristic fallback when the live API doesn't include protocol metadata. MiniMax and Qwen3.x use Anthropic /v1/messages; everything else uses OpenAI /v1/chat/completions.

func ProtocolMapSnapshot

func ProtocolMapSnapshot() map[string]string

ProtocolMapSnapshot returns a copy of the current protocol map for testing/debugging.

func ResetProtocolMap

func ResetProtocolMap()

ResetProtocolMap clears the dynamic protocol map. For testing only.

func UpdateProtocolMap

func UpdateProtocolMap(entries []struct{ ID, Protocol string })

UpdateProtocolMap refreshes the dynamic protocol map from live fetch entries. Called after every successful FetchOpenCodeGo in the discover pipeline.

func UsesMessagesAPI

func UsesMessagesAPI(modelID string) bool

UsesMessagesAPI reports whether a model should use Anthropic /v1/messages on OpenCode Go (see opencode.ai/docs/go endpoints table).

Resolution order:

  1. Dynamic protocol map (populated from live /v1/models response)
  2. Heuristic fallback (model name pattern matching)

Types

type ModelMetadata

type ModelMetadata struct {
	Protocol    string  // "openai" or "anthropic"
	InputPer1M  float64 // USD per 1M input tokens
	OutputPer1M float64 // USD per 1M output tokens
	CachedRead  float64 // USD per 1M cached read tokens (0 if none)
	CachedWrite float64 // USD per 1M cached write tokens (0 if none)
	Context     int     // context window tokens
	MaxOutput   int     // max output tokens
	// Tiered pricing (Qwen models: different rate above threshold)
	TierThreshold     int     // 0 = no tiering (e.g., 256000 for Qwen)
	TieredInputPer1M  float64 // rate above threshold
	TieredOutputPer1M float64
	TieredCachedRead  float64
	TieredCachedWrite float64
}

ModelMetadata holds static per-model data from the docs.

func MetadataForModel

func MetadataForModel(modelID string) (ModelMetadata, bool)

MetadataForModel returns static metadata for a model, or zero value if unknown.

type PricingTier

type PricingTier struct {
	InputPer1M  float64
	OutputPer1M float64
}

PricingTier holds pricing for a single tier (base or over-threshold).

type UsageLimits

type UsageLimits struct {
	FiveHourLimit float64
	WeeklyLimit   float64
	MonthlyLimit  float64
}

UsageLimits defines the dollar-denominated spending caps.

func DefaultUsageLimits

func DefaultUsageLimits() UsageLimits

DefaultUsageLimits returns the limits from the OpenCode Go docs.

type UsageRecord

type UsageRecord struct {
	Timestamp    time.Time
	Model        string
	InputTokens  int
	OutputTokens int
	CostUSD      float64
}

UsageRecord tracks a single spend event.

type UsageStatus

type UsageStatus struct {
	FiveHourSpend float64
	FiveHourLimit float64
	WeeklySpend   float64
	WeeklyLimit   float64
	MonthlySpend  float64
	MonthlyLimit  float64
}

UsageStatus returns the current spend vs limits.

type UsageTracker

type UsageTracker struct {
	// contains filtered or unexported fields
}

UsageTracker tracks cumulative spend with sliding time windows.

func NewUsageTracker

func NewUsageTracker(limits UsageLimits) *UsageTracker

NewUsageTracker creates a tracker with the given limits.

func (*UsageTracker) Record

func (t *UsageTracker) Record(r UsageRecord)

Record adds a spend event.

func (*UsageTracker) SpendInWindow

func (t *UsageTracker) SpendInWindow(window time.Duration) float64

SpendInWindow returns total USD spent in the given duration window.

func (*UsageTracker) Status

func (t *UsageTracker) Status() UsageStatus

Status returns current spend vs all three limits.

func (*UsageTracker) WouldExceedLimit

func (t *UsageTracker) WouldExceedLimit(additionalCost float64) error

WouldExceedLimit checks if adding `additionalCost` would exceed any limit. Returns nil if OK, or an error describing which limit would be exceeded.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL