feat(dingtalk): add reasoning mode and support evidence
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@@ -28,6 +29,8 @@ type SettingInput struct{ TenantKey, Channel, StaffID, UnionID, DisplayName, Dep
|
||||
type ControllerRepository interface {
|
||||
IsAdministrator(context.Context, string, string) (bool, error)
|
||||
SetPreference(context.Context, string, string, string, string) error
|
||||
SetReasoningMaxRounds(context.Context, string, string, string, int, time.Time) error
|
||||
SetReasoningHintEnabled(context.Context, string, string, string, bool, time.Time) error
|
||||
SaveBinding(context.Context, ResourceBinding) error
|
||||
TestAndActivateSupportGroup(context.Context, ResourceBinding) error
|
||||
DetectBootstrapCandidate(context.Context, BootstrapCandidate) error
|
||||
@@ -109,6 +112,32 @@ func (c *Controller) Handle(ctx context.Context, in SettingInput) (string, error
|
||||
}
|
||||
return "✅ 回答语言已设置为 " + args[1] + "。", nil
|
||||
}
|
||||
if action == "rounds" {
|
||||
if len(args) != 2 {
|
||||
return "智能推理自动关闭轮数必须是1-50之间的整数。", nil
|
||||
}
|
||||
rounds, parseErr := strconv.Atoi(args[1])
|
||||
if parseErr != nil || rounds < 1 || rounds > 50 {
|
||||
return "智能推理自动关闭轮数必须是1-50之间的整数。", nil
|
||||
}
|
||||
if err := c.repo.SetReasoningMaxRounds(ctx, in.TenantKey, defaultChannel(in.Channel), in.StaffID, rounds, c.now()); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return fmt.Sprintf("✅ 智能推理将在%d轮对话后自动关闭。", rounds), nil
|
||||
}
|
||||
if action == "hint" {
|
||||
if len(args) != 2 || (args[1] != "on" && args[1] != "off") {
|
||||
return "用法:/setting hint <on|off>。", nil
|
||||
}
|
||||
enabled := args[1] == "on"
|
||||
if err := c.repo.SetReasoningHintEnabled(ctx, in.TenantKey, defaultChannel(in.Channel), in.StaffID, enabled, c.now()); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if enabled {
|
||||
return "✅ 智能推理剩余轮次提醒已开启。", nil
|
||||
}
|
||||
return "✅ 智能推理剩余轮次提醒已关闭。", nil
|
||||
}
|
||||
admin, err := c.repo.IsAdministrator(ctx, in.TenantKey, in.StaffID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -164,7 +193,10 @@ func settingMenu(admin, _ bool) string {
|
||||
"- auto:跟随当前提问语言(推荐)\n" +
|
||||
"- zh-CN:简体中文\n" +
|
||||
"- en / ja / ko / es:英语 / 日语 / 韩语 / 西班牙语\n" +
|
||||
"例如:/setting language en"
|
||||
"例如:/setting language en\n\n" +
|
||||
"智能推理自动关闭轮数:/setting rounds <1-50>\n" +
|
||||
"例如:/setting rounds 15\n" +
|
||||
"剩余轮次提醒:/setting hint <on|off>"
|
||||
if !admin {
|
||||
return menu
|
||||
}
|
||||
|
||||
@@ -11,6 +11,8 @@ import (
|
||||
type memoryRepo struct {
|
||||
admins map[string]bool
|
||||
prefs map[string]string
|
||||
rounds map[string]int
|
||||
hints map[string]bool
|
||||
bindings map[string]ResourceBinding
|
||||
candidate BootstrapCandidate
|
||||
sessions map[string]SettingSession
|
||||
@@ -18,7 +20,7 @@ type memoryRepo struct {
|
||||
}
|
||||
|
||||
func newMemoryRepo() *memoryRepo {
|
||||
return &memoryRepo{admins: map[string]bool{}, prefs: map[string]string{}, bindings: map[string]ResourceBinding{}, sessions: map[string]SettingSession{}}
|
||||
return &memoryRepo{admins: map[string]bool{}, prefs: map[string]string{}, rounds: map[string]int{}, hints: map[string]bool{}, bindings: map[string]ResourceBinding{}, sessions: map[string]SettingSession{}}
|
||||
}
|
||||
func (r *memoryRepo) IsAdministrator(_ context.Context, tenant, staff string) (bool, error) {
|
||||
return r.admins[tenant+"/"+staff], nil
|
||||
@@ -27,6 +29,14 @@ func (r *memoryRepo) SetPreference(_ context.Context, tenant, channel, staff, lo
|
||||
r.prefs[tenant+"/"+staff] = locale
|
||||
return nil
|
||||
}
|
||||
func (r *memoryRepo) SetReasoningMaxRounds(_ context.Context, tenant, channel, staff string, rounds int, _ time.Time) error {
|
||||
r.rounds[tenant+"/"+channel+"/"+staff] = rounds
|
||||
return nil
|
||||
}
|
||||
func (r *memoryRepo) SetReasoningHintEnabled(_ context.Context, tenant, channel, staff string, enabled bool, _ time.Time) error {
|
||||
r.hints[tenant+"/"+channel+"/"+staff] = enabled
|
||||
return nil
|
||||
}
|
||||
func (r *memoryRepo) SaveBinding(_ context.Context, b ResourceBinding) error {
|
||||
if r.saveErr != nil {
|
||||
return r.saveErr
|
||||
@@ -69,6 +79,43 @@ func TestControllerLanguageForOrdinaryUser(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestControllerReasoningRoundsForOrdinaryUser(t *testing.T) {
|
||||
r := newMemoryRepo()
|
||||
c := NewController(r)
|
||||
out, err := c.Handle(context.Background(), SettingInput{TenantKey: "t", Channel: "dingtalk", StaffID: "u", Text: "/setting rounds 15", ConversationType: "direct"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if r.rounds["t/dingtalk/u"] != 15 || !strings.Contains(out, "15轮") {
|
||||
t.Fatalf("rounds=%v out=%q", r.rounds, out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestControllerRejectsOutOfRangeReasoningRounds(t *testing.T) {
|
||||
for _, value := range []string{"0", "51", "abc"} {
|
||||
r := newMemoryRepo()
|
||||
out, err := NewController(r).Handle(context.Background(), SettingInput{TenantKey: "t", Channel: "dingtalk", StaffID: "u", Text: "/setting rounds " + value, ConversationType: "direct"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(r.rounds) != 0 || !strings.Contains(out, "1-50") {
|
||||
t.Fatalf("value=%q rounds=%v out=%q", value, r.rounds, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestControllerReasoningHintForOrdinaryUser(t *testing.T) {
|
||||
r := newMemoryRepo()
|
||||
c := NewController(r)
|
||||
out, err := c.Handle(context.Background(), SettingInput{TenantKey: "t", Channel: "dingtalk", StaffID: "u", Text: "/setting hint off", ConversationType: "direct"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if r.hints["t/dingtalk/u"] || !strings.Contains(out, "已关闭") {
|
||||
t.Fatalf("hints=%v out=%q", r.hints, out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestControllerSettingMenuExplainsLanguageWithoutAdminActionsToOrdinaryUser(t *testing.T) {
|
||||
r := newMemoryRepo()
|
||||
c := NewController(r)
|
||||
|
||||
Reference in New Issue
Block a user