Files
Cyrene_v2/backend/services/ai-core/model/model.go
T
Aska 7c02d31194 feat(ai-core): wire function calling into the LLM pipeline
- llm: ChatStreamWithTools sends OpenAI-format tools; streamed tool_call
  deltas are reassembled by index into complete ToolCalls on the Done token
- model: LLMMessage gains ToolCallID/ToolCalls; StreamToken gains ToolCalls
- orchestrator: new ToolExecutor interface and streamWithTools loop —
  execute requested tools, feed results back, up to 3 rounds, final round
  forces a plain text answer; normal chats still stream token by token.
  Also adds Pipeline.FinishRound (assistant reply recording + post-round
  hook), wired to transports in the next commit.
- main: inject the plugin registry via toolExecAdapter

Includes an httptest-based test covering delta reassembly, tool
execution, and result feedback.
2026-08-20 19:40:58 +08:00

103 lines
3.5 KiB
Go

// Package model defines shared data types for the AI-Core engine.
package model
import "time"
// Message represents a single chat message in a conversation.
type Message struct {
ID string `json:"id"`
SessionID string `json:"session_id"`
Role string `json:"role"` // "user" | "assistant" | "action" | "system"
Content string `json:"content"`
CreatedAt time.Time `json:"created_at"`
}
// Session represents a conversation session.
type Session struct {
ID string `json:"id"`
UserID string `json:"user_id"`
Title string `json:"title"`
State string `json:"state"` // "active" | "idle"
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// SubSessionResult holds the result from a single subsession provider.
type SubSessionResult struct {
Provider string `json:"provider"` // "general" | "memory" | "iot" | "review"
Content string `json:"content"`
Error string `json:"error,omitempty"`
}
// Intent represents the detected intent of a user message.
type Intent struct {
Type string `json:"type"` // "chat" | "iot_control" | "iot_query" | "greeting" | "memory_trigger" | "knowledge" | "task"
Confidence float64 `json:"confidence"` // 0.0 - 1.0
Entities []string `json:"entities"` // Detected entities (device names, etc.)
RawText string `json:"raw_text"`
}
// LLMMessage is the standardized format sent to LLM APIs.
type LLMMessage struct {
Role string `json:"role"` // "system" | "user" | "assistant" | "tool"
Content string `json:"content"`
Name string `json:"name,omitempty"`
// ToolCallID links a tool result message to the call that produced it (role="tool").
ToolCallID string `json:"tool_call_id,omitempty"`
// ToolCalls carries the calls requested by the assistant (role="assistant").
// Marshaled to the OpenAI wire format by the llm package, hence "-".
ToolCalls []ToolCall `json:"-"`
}
// LLMResponse represents a non-streaming LLM response.
type LLMResponse struct {
Content string `json:"content"`
FinishReason string `json:"finish_reason"` // "stop" | "length" | "tool_calls"
Model string `json:"model"`
TokensUsed int `json:"tokens_used"`
}
// StreamToken is a single token from a streaming LLM response.
type StreamToken struct {
Content string `json:"content"`
FinishReason string `json:"finish_reason,omitempty"`
Done bool `json:"done"`
// ToolCalls is set on the final Done token when the model requested tool calls.
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
}
// ReviewSegment represents a parsed action/chat segment from LLM output.
type ReviewSegment struct {
Type string `json:"type"` // "action" | "chat"
Content string `json:"content"`
}
// ToolCall represents a tool call requested by the LLM.
type ToolCall struct {
ID string `json:"id"`
Name string `json:"name"`
Arguments map[string]any `json:"arguments"`
}
// Intent types
const (
IntentChat = "chat"
IntentIoTControl = "iot_control"
IntentIoTQuery = "iot_query"
IntentGreeting = "greeting"
IntentMemoryTrigger = "memory_trigger"
IntentKnowledge = "knowledge"
IntentTask = "task"
)
// LLM purposes for model routing
type LLMPurpose string
const (
PurposeChat LLMPurpose = "chat"
PurposeIntentAnalysis LLMPurpose = "intent_analysis"
PurposeDeepThinking LLMPurpose = "deep_thinking"
PurposeMemoryExtraction LLMPurpose = "memory_extraction"
PurposeToolCalling LLMPurpose = "tool_calling"
)