Files
ai-agent/contract/business_action_tool.go
T
t 18c9354095 refactor: 将客服后端重构为宿主可嵌入模块
- 注入数据库、运行时配置、统一响应、文件存储和平台 AI 能力,补充业务读写工具与客户快捷操作契约。

- 移除模块内重复的组织、客户、工单、标签、技能、旧工作流、MCP 和迁移实现,将身份权限与业务主体交由宿主管理。

- 使用 libSQL 重构向量存储,并完善图片消息、访客身份、排队调度、企业微信和支持聊天页面。

- 统一 HTTP、DTO 与 WebSocket 的 snake_case 协议,补齐模块初始化、业务动作和公共载荷等回归测试。
2026-08-28 22:23:13 +08:00

88 lines
3.4 KiB
Go

package contract
import (
"context"
"errors"
)
type BusinessActionFailureOutcome string
const (
// BusinessActionFailureRetryable means execution failed before the host
// operation could have produced a side effect. The same idempotency key may
// be claimed again.
BusinessActionFailureRetryable BusinessActionFailureOutcome = "retryable_failed"
// BusinessActionFailureUnknown means the host may have committed the side
// effect even though Agent Desk did not receive a definitive response. Such
// an invocation must be reconciled instead of replayed automatically.
BusinessActionFailureUnknown BusinessActionFailureOutcome = "unknown_outcome"
)
// BusinessActionResult is the customer-safe result returned after a confirmed
// host business operation. Message is sent to the customer verbatim; Data is
// retained only for idempotent replay and future structured clients.
type BusinessActionResult struct {
Message string `json:"message"`
Data any `json:"data,omitempty"`
}
// BusinessActionError separates a customer-safe explanation from its internal
// cause so model observations and chat replies never expose infrastructure
// errors returned by the host application.
type BusinessActionError struct {
Message string
Cause error
Outcome BusinessActionFailureOutcome
}
func (e *BusinessActionError) Error() string {
return e.Message
}
func (e *BusinessActionError) Unwrap() error {
return e.Cause
}
func NewBusinessActionError(message string, cause error) error {
return &BusinessActionError{Message: message, Cause: cause, Outcome: BusinessActionFailureUnknown}
}
func NewRetryableBusinessActionError(message string, cause error) error {
return &BusinessActionError{Message: message, Cause: cause, Outcome: BusinessActionFailureRetryable}
}
func NewUnknownOutcomeBusinessActionError(message string, cause error) error {
return &BusinessActionError{Message: message, Cause: cause, Outcome: BusinessActionFailureUnknown}
}
func BusinessActionErrorOutcome(err error) BusinessActionFailureOutcome {
var actionErr *BusinessActionError
if errors.As(err, &actionErr) {
return actionErr.Outcome
}
return ""
}
// BusinessActionTool lets the host expose a narrowly scoped write operation.
// Preview must perform read-only validation and produce the exact confirmation
// prompt. Execute must independently reload and validate all mutable business
// state before committing the operation.
type BusinessActionTool struct {
Code string
Description string
CustomerTypes []string
InputSchema map[string]any
// MatchIntent lets the host identify an unambiguous customer command that
// must enter the confirmation flow without relying on the language model to
// select a tool. It must be side-effect free.
MatchIntent func(string) bool
Preview func(context.Context, BusinessReadContext, map[string]any) (string, error)
// BindConfirmation binds the generated server checkpoint and canonical
// arguments to the verified request that prepared the action.
BindConfirmation func(context.Context, BusinessReadContext, map[string]any, string) error
// AuthorizeConfirmation re-verifies the current confirmation request and
// the previously bound checkpoint immediately before idempotency claiming.
AuthorizeConfirmation func(context.Context, BusinessReadContext, map[string]any, string) error
Execute func(context.Context, BusinessReadContext, map[string]any) (*BusinessActionResult, error)
}