Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

16.4 KB
Raw Blame
package types

import (
	"database/sql/driver"
	"encoding/json"
)

// PromptCacheStatus distinguishes a real cache miss from providers that do not
// expose prompt-cache accounting. Treating both as zero makes fleet-level hit
// rate dashboards misleading.
type PromptCacheStatus string

const (
	PromptCacheStatusUnsupported PromptCacheStatus = "unsupported"
	PromptCacheStatusUnreported  PromptCacheStatus = "unreported"
	PromptCacheStatusMiss        PromptCacheStatus = "miss"
	PromptCacheStatusHit         PromptCacheStatus = "hit"
)

// TokenUsage holds token consumption statistics returned by the model API.
type TokenUsage struct {
	PromptTokens     int `json:"prompt_tokens"`
	CompletionTokens int `json:"completion_tokens"`
	TotalTokens      int `json:"total_tokens"`
	// CachedTokens is the legacy alias for CacheReadTokens. It remains on the
	// wire for compatibility with existing API consumers.
	CachedTokens     int               `json:"cached_tokens,omitempty"`
	CacheReadTokens  int               `json:"cache_read_tokens,omitempty"`
	CacheWriteTokens int               `json:"cache_write_tokens,omitempty"`
	CacheMissTokens  int               `json:"cache_miss_tokens,omitempty"`
	CacheReported    bool              `json:"cache_reported"`
	CacheStatus      PromptCacheStatus `json:"cache_status,omitempty"`
	// ContextTokenScale is provider prompt tokens per cl100k-estimated token,
	// measured over the turn's rounds. Persisted with the turn so the next
	// turn's history loading and first compaction check are calibrated before
	// any provider count of their own. Zero when the turn measured none.
	ContextTokenScale float64 `json:"context_token_scale,omitempty"`
}

// SetPromptCacheUsage normalizes provider-specific cache counters into the
// shared usage model. promptTokens remains the provider's total input-token
// count; read/write/miss are descriptive subsets and must not be added to it.
func (u *TokenUsage) SetPromptCacheUsage(read, write, miss int, reported bool) {
	if u == nil {
		return
	}
	if read < 0 {
		read = 0
	}
	if write < 0 {
		write = 0
	}
	if miss < 0 {
		miss = 0
	}
	u.CachedTokens = read
	u.CacheReadTokens = read
	u.CacheWriteTokens = write
	u.CacheMissTokens = miss
	u.CacheReported = reported
	switch {
	case !reported:
		u.CacheStatus = PromptCacheStatusUnreported
	case read > 0:
		u.CacheStatus = PromptCacheStatusHit
	default:
		u.CacheStatus = PromptCacheStatusMiss
	}
}

// MarkPromptCacheUnsupported marks a provider/model path that cannot report
// provider-side prompt-cache usage.
func (u *TokenUsage) MarkPromptCacheUnsupported() {
	if u == nil {
		return
	}
	u.SetPromptCacheUsage(0, 0, 0, false)
	u.CacheStatus = PromptCacheStatusUnsupported
}

// Accumulate adds another call's usage into u, preserving the subset
// semantics: prompt/completion/total and every cache counter sum
// independently (cache counters stay subsets of the prompt count and are
// never folded into it). CacheReported ORs, and the cache status is
// recomputed from the combined counters so a single cache hit anywhere in
// the accumulated calls reads as a hit.
func (u *TokenUsage) Accumulate(other TokenUsage) {
	if u == nil {
		return
	}
	u.PromptTokens += other.PromptTokens
	u.CompletionTokens += other.CompletionTokens
	u.TotalTokens += other.TotalTokens
	u.CachedTokens += other.CachedTokens
	u.CacheReadTokens += other.CacheReadTokens
	u.CacheWriteTokens += other.CacheWriteTokens
	u.CacheMissTokens += other.CacheMissTokens
	u.CacheReported = u.CacheReported || other.CacheReported
	// A scale is a ratio, not a count: the newest measurement stands.
	if other.ContextTokenScale > 0 {
		u.ContextTokenScale = other.ContextTokenScale
	}
	switch {
	case !u.CacheReported:
		u.CacheStatus = mergeUnreportedCacheStatus(u.CacheStatus, other.CacheStatus)
	case u.CacheReadTokens > 0:
		u.CacheStatus = PromptCacheStatusHit
	default:
		u.CacheStatus = PromptCacheStatusMiss
	}
}

// mergeUnreportedCacheStatus folds the statuses of never-reported usage.
// "unsupported" survives only while every accumulated call was itself
// classified unsupported — the first accumulation adopts the incoming
// classification, and any later call that is not known-unsupported (an
// unreported or unclassified one) degrades the aggregate to unreported.
func mergeUnreportedCacheStatus(accumulated, incoming PromptCacheStatus) PromptCacheStatus {
	if accumulated == "" {
		if incoming == "" {
			return PromptCacheStatusUnreported
		}
		return incoming
	}
	if accumulated == PromptCacheStatusUnsupported && incoming == PromptCacheStatusUnsupported {
		return PromptCacheStatusUnsupported
	}
	return PromptCacheStatusUnreported
}

// PromptCacheHitRate returns cache-read tokens as a percentage of the
// provider's prompt total. Zero when the prompt is empty. A ReAct turn that
// reuses the system prefix therefore reads as a high hit rather than a miss.
func (u TokenUsage) PromptCacheHitRate() float64 {
	if u.PromptTokens <= 0 {
		return 0
	}
	return float64(u.CacheReadTokens) / float64(u.PromptTokens) * 100
}

// Value persists the usage as a jsonb column (assistant messages carry the
// turn's aggregate); nil writes SQL NULL. Mirrors the nullable-pointer
// pattern APIPrincipalConfig uses.
func (u *TokenUsage) Value() (driver.Value, error) {
	if u == nil {
		return nil, nil
	}
	return json.Marshal(u)
}

// Scan restores a jsonb usage column; NULL leaves the receiver zero-valued.
func (u *TokenUsage) Scan(value interface{}) error {
	if value == nil {
		return nil
	}
	var b []byte
	switch v := value.(type) {
	case []byte:
		b = v
	case string:
		b = []byte(v)
	default:
		return nil
	}
	return json.Unmarshal(b, u)
}

// LLMToolCall represents a function/tool call from the LLM
type LLMToolCall struct {
	ID               string           `json:"id"`
	Type             string           `json:"type"` // "function"
	Function         FunctionCall     `json:"function"`
	ProviderMetadata ToolCallMetadata `json:"provider_metadata,omitempty"`

	// ModelArguments and the resolution fields are request-local observability
	// state. ModelArguments preserves the exact JSON emitted by the model while
	// Function.Arguments is decoded to durable application identifiers before
	// tool execution. These fields must never be sent back to a provider or
	// persisted in chat history.
	ModelArguments     string   `json:"-"`
	ArgumentResolution string   `json:"-"`
	UnresolvedHandles  []string `json:"-"`
}

// ToolCallMetadata carries provider-specific tool-call state that must round-trip
// with the assistant tool call, without teaching core agent code vendor fields.
type ToolCallMetadata map[string]json.RawMessage

// ProviderMetadata carries opaque provider state attached to an assistant
// turn as a whole (OpenAI Responses reasoning items, OpenRouter
// reasoning_details, ...). Keyed by protocol / vendor namespace so several
// providers can coexist on one persisted message without collisions.
type ProviderMetadata map[string]json.RawMessage

// FunctionCall represents the function details
type FunctionCall struct {
	Name      string `json:"name"`
	Arguments string `json:"arguments"` // JSON string
}

// ChatResponse chat response
type ChatResponse struct {
	Content string `json:"content"`
	// ReasoningContent 是支持思考链的模型(DeepSeek thinking、小米 MiMo、vLLM reasoning 等)
	// 在本轮输出的推理内容。需要在后续多轮请求中原样回传给那些严格校验的供应商。
	ReasoningContent string `json:"reasoning_content,omitempty"`
	// ReasoningSignature and ReasoningMetadata are the provider-issued
	// artifacts that must be replayed together with ReasoningContent on the
	// next turn (Anthropic thinking signatures, Gemini thought signatures,
	// OpenAI Responses encrypted reasoning items).
	ReasoningSignature string           `json:"reasoning_signature,omitempty"`
	ReasoningMetadata  ProviderMetadata `json:"reasoning_metadata,omitempty"`
	ToolCalls          []LLMToolCall    `json:"tool_calls,omitempty"`
	FinishReason       string           `json:"finish_reason,omitempty"`
	Usage              TokenUsage       `json:"usage"`

	// AnswerStreamed reports whether the user-facing answer text was already
	// streamed live to the final-answer UI area during this round (i.e. the
	// model answered with plain content). When true, the natural-stop branch
	// must only emit the closing
	// Done marker for AnswerEventID instead of re-emitting the whole answer —
	// otherwise the answer would render twice and "jump" at end of stream.
	// Transient, never persisted.
	AnswerStreamed bool `json:"-"`
	// AnswerEventID is the EventBus event ID under which the live answer
	// chunks were streamed, so the natural-stop branch can close the same
	// stream with a Done marker. Empty when AnswerStreamed is false.
	AnswerEventID string `json:"-"`
}

// Response type
type ResponseType string

const (
	// Answer response type
	ResponseTypeAnswer ResponseType = "answer"
	// References response type
	ResponseTypeReferences ResponseType = "references"
	// Thinking response type (for agent thought process)
	ResponseTypeThinking ResponseType = "thinking"
	// ResponseTypeProgress carries a user-visible intermediate update.
	ResponseTypeProgress ResponseType = "progress"
	// Tool call response type (for agent tool invocations)
	ResponseTypeToolCall ResponseType = "tool_call"
	// Tool result response type (for agent tool results)
	ResponseTypeToolResult ResponseType = "tool_result"
	// ResponseTypeInstallOutput carries installer progress without completing a tool.
	ResponseTypeInstallOutput ResponseType = "install_output"
	// ResponseTypeCommandOutput updates the pending tool card without completing it.
	ResponseTypeCommandOutput ResponseType = "command_output"
	// ResponseTypeWorkStarted is a background work event of shared design §7.
	// Features emit it through service.BackgroundEventEmitter, which picks the stream.
	ResponseTypeWorkStarted ResponseType = "work_started"
	// ResponseTypeWorkEvent is a background work event of shared design §7.
	// Features emit it through service.BackgroundEventEmitter, which picks the stream.
	ResponseTypeWorkEvent ResponseType = "work_event"
	// ResponseTypeWorkFinished is a background work event of shared design §7.
	// Features emit it through service.BackgroundEventEmitter, which picks the stream.
	ResponseTypeWorkFinished ResponseType = "work_finished"
	// Error response type
	ResponseTypeError ResponseType = "error"
	// Reflection response type (for agent reflection)
	ResponseTypeReflection ResponseType = "reflection"
	// Session title response type
	ResponseTypeSessionTitle ResponseType = "session_title"
	// Agent query response type (query received and processing started)
	ResponseTypeAgentQuery ResponseType = "agent_query"
	// Complete response type (agent complete)
	ResponseTypeComplete ResponseType = "complete"
	// ResponseTypeArtifactsPending is sent while skill/sandbox output is being
	// copied into persistent storage after the answer has already streamed.
	// The UI shows a toolbar placeholder until ResponseTypeComplete carries
	// the file list.
	ResponseTypeArtifactsPending ResponseType = "artifacts_pending"
	// ResponseTypeRunSuspended precedes complete when the run ended waiting
	// on a user prompt; the message stays incomplete until it is answered.
	ResponseTypeRunSuspended ResponseType = "run_suspended"
	// ResponseTypeRunResumed opens the stream of a suspended run resumed
	// after its prompt was answered. Steps saved before the suspension are
	// not replayed; the client keeps the ones it already shows.
	ResponseTypeRunResumed ResponseType = "run_resumed"
	// ToolApprovalRequired: MCP tool marked dangerous — UI must collect user approval before execution continues
	ResponseTypeToolApprovalRequired ResponseType = "tool_approval_required"
	// ToolApprovalResolved: user approved/rejected (or timeout); informational for UI replay
	ResponseTypeToolApprovalResolved ResponseType = "tool_approval_resolved"
	// ResponseTypeAgentPromptRequired indicates that the agent asks the user something (ask_user);
	// data carries the prompt kind and its request JSON.
	ResponseTypeAgentPromptRequired ResponseType = "agent_prompt_required"
	// ResponseTypeAgentPromptResolved indicates an answered, declined, timed out or canceled prompt.
	ResponseTypeAgentPromptResolved ResponseType = "agent_prompt_resolved"
	// MCPOAuthRequired: an OAuth-enabled MCP service was invoked but the user
	// has not authorized it — UI must surface an "Authorize" prompt and the
	// agent pauses until authorization completes (or the wait times out).
	ResponseTypeMCPOAuthRequired ResponseType = "mcp_oauth_required"
	// MCPOAuthResolved: authorization completed / timed out / canceled;
	// informational for UI replay.
	ResponseTypeMCPOAuthResolved ResponseType = "mcp_oauth_resolved"
	// MemoryRecalled: the long-term memories injected into this answer, so
	// the UI can show and let the user delete what influenced it.
	ResponseTypeMemoryRecalled ResponseType = "memory_recalled"
	// ResponseTypeSteer is the per-run control signal a client POSTs while a
	// turn is still running. It rides the same StreamManager keyspace as the
	// stop event but in a dedicated sub-list (AppendSteerEvents/GetSteerEvents)
	// so it never appears on the user-visible SSE stream. The running engine
	// drains it at the next round boundary (SteerSink.PollSteer) or, when the
	// loop has already exited, the run's teardown paths hand it to the next
	// run — unless the turn was stopped, in which case it is discarded.
	ResponseTypeSteer ResponseType = "steer"
	// ResponseTypeUserMessageInjected is emitted on the user-visible stream
	// right after a steered message was accepted into the running turn: a
	// user-role DB row has been persisted under the run's request_id and the
	// text was appended to the agent's message list. The frontend uses it to
	// move the queued message out of the composer overlay and into the
	// transcript.
	ResponseTypeUserMessageInjected ResponseType = "user_message_injected"
	// ResponseTypeContextCompacted is older conversation summarized away to
	// fit the context window. Surfaced because it changes what the agent
	// remembers — an answer that forgets an earlier instruction is otherwise
	// indistinguishable from the model ignoring it.
	ResponseTypeContextCompacted ResponseType = "context_compacted"
	// ResponseTypeInstallPrompt is the instruction a skill install handed to
	// the installer agent. Only the skill install transcript emits this, and
	// it emits it first, so replaying the log alone shows what was asked for
	// — the console does not have to read the durable prompt row to caption
	// the run.
	ResponseTypeInstallPrompt ResponseType = "install_prompt"
)

// StreamResponse stream response
// FinishReasonIncomplete marks a stream that broke before the provider sent a
// finish reason (read error, timeout, stall). Whatever content and tool calls
// were collected are a partial response: callers must not treat them as a
// completed turn.
const FinishReasonIncomplete = "incomplete"

// StreamEndedEarlyError is what a consumer reports for an answer that arrived
// with FinishReasonIncomplete: the stream stopped before the provider said the
// message was finished, most often because a proxy cut the connection.
const StreamEndedEarlyError = "the model's response ended before it finished (connection interrupted)"

type StreamResponse struct {
	ID                  string                 `json:"id"`
	ResponseType        ResponseType           `json:"response_type"`
	Content             string                 `json:"content"`
	Done                bool                   `json:"done"`
	KnowledgeReferences References             `json:"knowledge_references,omitempty"`
	SessionID           string                 `json:"session_id,omitempty"`
	AssistantMessageID  string                 `json:"assistant_message_id,omitempty"`
	ToolCalls           []LLMToolCall          `json:"tool_calls,omitempty"`
	Data                map[string]interface{} `json:"data,omitempty"`
	Usage               *TokenUsage            `json:"usage,omitempty"`
	FinishReason        string                 `json:"finish_reason,omitempty"`
}

// References references
type References []*SearchResult

// Value implements the driver.Valuer interface, used to convert References to database values
func (c References) Value() (driver.Value, error) {
	return json.Marshal(c)
}

// Scan implements the sql.Scanner interface, used to convert database values to References
func (c *References) Scan(value interface{}) error {
	if value == nil {
		return nil
	}
	b, ok := value.([]byte)
	if !ok {
		return nil
	}
	return json.Unmarshal(b, c)
}