ai

package
v1.11.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: GPL-3.0 Imports: 28 Imported by: 0

Documentation

Overview

Package ai 实现 ai binder:会话管理、消息收发、provider 配置、AI 工具审批。

同时暴露 ToolExecutor 接口给 opsctl,让 opsctl 在外部触发 AI 工具调用时复用同一执行路径。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AI

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

AI binder。

func New

func New(appCtx context.Context, lang LangProvider, pool *sshpool.Pool) *AI

New 构造 ai binder。

func (*AI) Cleanup

func (a *AI) Cleanup()

Cleanup 占位(ai service 没持有需要主动关的资源)。

func (*AI) ClearQueuedAIMessages

func (a *AI) ClearQueuedAIMessages(convID int64) []string

ClearQueuedAIMessages 清空 cago Runner 尚未消费的 Steer 队列。

func (*AI) CreateAIProvider

func (a *AI) CreateAIProvider(name, providerType, apiBase, apiKey, model string, maxOutputTokens, contextWindow int, reasoningEnabled bool, reasoningEffort string) (*AIProviderInfo, error)

CreateAIProvider 创建新 Provider

func (*AI) CreateConversation

func (a *AI) CreateConversation() (*conversation_entity.Conversation, error)

CreateConversation 创建新会话

func (*AI) DeleteAIProvider

func (a *AI) DeleteAIProvider(id int64) error

DeleteAIProvider 删除 Provider

func (*AI) DeleteConversation

func (a *AI) DeleteConversation(id int64) error

DeleteConversation 删除会话

func (*AI) DrainAIFlushAck

func (a *AI) DrainAIFlushAck()

DrainAIFlushAck 在 emit ai:flush-all 之前清空 channel 上一次残留,避免拿到旧 ack。

func (*AI) FetchAIModels

func (a *AI) FetchAIModels(providerType, apiBase, apiKey string) ([]AIModelInfo, error)

FetchAIModels 从 API 获取可用模型列表

func (*AI) GetActiveAIProvider

func (a *AI) GetActiveAIProvider() (*AIProviderInfo, error)

GetActiveAIProvider 获取当前激活的 Provider

func (*AI) GetCurrentConversationID

func (a *AI) GetCurrentConversationID() int64

GetCurrentConversationID 获取当前会话ID

func (*AI) GetModelDefaults

func (a *AI) GetModelDefaults(model string) *AIModelInfo

GetModelDefaults 获取模型的默认参数,未知模型返回 fallback 默认值

func (*AI) InitAIProvider

func (a *AI) InitAIProvider()

InitAIProvider 启动时加载激活的 Provider。

func (*AI) ListAIProviders

func (a *AI) ListAIProviders() ([]AIProviderInfo, error)

ListAIProviders 列出所有 Provider

func (*AI) ListConversations

func (a *AI) ListConversations() ([]*conversation_entity.Conversation, error)

ListConversations 获取会话列表

func (*AI) LoadConversationMessages

func (a *AI) LoadConversationMessages(id int64) ([]ConversationDisplayMessage, error)

LoadConversationMessages 只读加载会话消息,不修改 currentConversationID。

func (*AI) QueueAIMessage

func (a *AI) QueueAIMessage(convID int64, queueID string, content string) error

QueueAIMessage 在生成过程中通过 cago Runner.Steer 把用户消息注入当前 turn。

func (*AI) RemoveQueuedAIMessage

func (a *AI) RemoveQueuedAIMessage(convID int64, queueID string) bool

RemoveQueuedAIMessage 尝试从 cago Runner 尚未消费的 Steer 队列里删除一条消息。

func (*AI) RespondAIApproval

func (a *AI) RespondAIApproval(confirmID string, resp permission.ApprovalResponse)

RespondAIApproval 前端响应 AI 审批请求(统一入口)

func (*AI) RespondPermission

func (a *AI) RespondPermission(behavior, message string)

RespondPermission 前端响应权限确认请求

func (*AI) SaveConversationMessages

func (a *AI) SaveConversationMessages(convID int64, displayMsgs []ConversationDisplayMessage) error

SaveConversationMessages 前端调用,保存显示消息到数据库。

func (*AI) SendAIMessage

func (a *AI) SendAIMessage(convID int64, messages []runner.Message, aiCtx runner.AIContext) error

SendAIMessage 发送 AI 消息,通过 Wails Events 流式返回

func (*AI) SetActiveAIProvider

func (a *AI) SetActiveAIProvider(id int64) error

SetActiveAIProvider 切换激活 Provider 并创建 Agent

func (*AI) SetExtensionService

func (a *AI) SetExtensionService(svc *extension_svc.Service)

SetExtensionService 由 main.go 注入 extension_svc,供 SendAIMessage 注入 SKILL.md。

func (*AI) SetKafkaService

func (a *AI) SetKafkaService(svc *kafka_svc.Service)

SetKafkaService 由 main.go 注入:AI tool 在 chat ctx 中通过 helper.WithKafkaService 暴露给 handler。

func (*AI) SetSerialManager

func (a *AI) SetSerialManager(mgr *serial_svc.Manager)

SetSerialManager 同上。

func (*AI) SetWindowActivator

func (a *AI) SetWindowActivator(w WindowActivator)

SetWindowActivator 由 main.go 注入:local-tool 审批弹出时需要把窗口拉前台。

func (*AI) Startup

func (a *AI) Startup(ctx context.Context)

Startup 初始化 AI provider、订阅 flush ack 事件。

func (*AI) StopAIGeneration

func (a *AI) StopAIGeneration(convID int64) error

StopAIGeneration 调用 cago Runner.Cancel 触发取消。

func (*AI) SwitchConversation

func (a *AI) SwitchConversation(id int64) ([]ConversationDisplayMessage, error)

SwitchConversation 切换到指定会话,返回显示消息

func (*AI) UpdateAIProvider

func (a *AI) UpdateAIProvider(id int64, name, providerType, apiBase, apiKey, model string, maxOutputTokens, contextWindow int, reasoningEnabled bool, reasoningEffort string) error

UpdateAIProvider 更新 Provider

func (*AI) UpdateConversationTitle

func (a *AI) UpdateConversationTitle(id int64, title string) error

UpdateConversationTitle 更新会话标题。

func (*AI) WaitAIFlushAck

func (a *AI) WaitAIFlushAck() <-chan struct{}

WaitAIFlushAck 暴露给 main.go 的 OnBeforeClose,等待前端 flush 完成。

type AIModelInfo

type AIModelInfo struct {
	ID              string `json:"id"`
	MaxOutputTokens int    `json:"maxOutputTokens"`
	ContextWindow   int    `json:"contextWindow"`
}

AIModelInfo 模型信息

type AIProviderInfo

type AIProviderInfo struct {
	ID               int64  `json:"id"`
	Name             string `json:"name"`
	Type             string `json:"type"`
	APIBase          string `json:"apiBase"`
	APIKey           string `json:"apiKey"`
	MaskedAPIKey     string `json:"maskedApiKey"`
	Model            string `json:"model"`
	MaxOutputTokens  int    `json:"maxOutputTokens"`
	ContextWindow    int    `json:"contextWindow"`
	ReasoningEnabled bool   `json:"reasoningEnabled"`
	ReasoningEffort  string `json:"reasoningEffort"`
	IsActive         bool   `json:"isActive"`
}

AIProviderInfo 返回给前端的 Provider 信息

type ConversationDisplayMessage

type ConversationDisplayMessage struct {
	Role       string                             `json:"role"`
	Content    string                             `json:"content"`
	Blocks     []conversation_entity.ContentBlock `json:"blocks"`
	TokenUsage *conversation_entity.TokenUsage    `json:"tokenUsage,omitempty"`
}

ConversationDisplayMessage 返回给前端的会话消息(用于恢复显示)

type LangProvider

type LangProvider interface {
	Lang() string
}

LangProvider 由 system binder 实现。

type WindowActivator

type WindowActivator interface {
	ActivateWindow()
}

WindowActivator 由 system binder 实现:审批弹窗时把窗口拉到前台。

Jump to

Keyboard shortcuts

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