dialogue

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: MIT Imports: 4 Imported by: 0

README

dialogue

多轮对话插件示例:/profile 依次询问昵称和城市,保存到用户维度状态;/whoami 查看已保存资料。

示例覆盖:

  • ctx.Dialogue 多步骤流程。
  • DialogueWithScope(DialogueScopeUser) 用户维度对话。
  • UserState[T] typed state。

在仓库根目录本地测试:

go test ./examples/plugins/dialogue

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Plugin = absdk.Define(absdk.Spec[Config]{
	Manifest: absdk.Manifest{Name: "dialogue_example", Version: "0.1.0", Description: "多轮对话插件示例"},
	DefaultConfig: Config{
		ProfileCommand: "profile",
		SummaryCommand: "whoami",
		TTL:            "10m",
	},
	Setup: func(ctx *absdk.Context, cfg Config) error {
		ttl, err := cfg.ttl()
		if err != nil {
			return err
		}
		flow := ctx.Dialogue(
			"profile",
			absdk.DialogueWithScope(absdk.DialogueScopeUser),
			absdk.DialogueWithTTL(ttl),
		)
		flow.Step("name", func(turn *absdk.DialogueTurn) error {
			name := strings.TrimSpace(turn.Text())
			if name == "" {
				_, err := turn.ReplyText("昵称不能为空,请重新输入。")
				return err
			}
			return turn.NextText("city", draftProfile{Name: name}, "你常在哪个城市出没?")
		})
		flow.Step("city", func(turn *absdk.DialogueTurn) error {
			city := strings.TrimSpace(turn.Text())
			if city == "" {
				_, err := turn.ReplyText("城市不能为空,请重新输入。")
				return err
			}
			var draft draftProfile
			if err := turn.Load(&draft); err != nil {
				return err
			}
			value := profile{Name: draft.Name, City: city}
			if err := absdk.UserState[profile](ctx, turn.EventContext, "profile").Save(value, 0); err != nil {
				return err
			}
			return turn.EndText(fmt.Sprintf("已记住:%s,%s。", value.Name, value.City))
		})

		ctx.Command(cfg.ProfileCommand).
			Name("profile.begin").
			Handle(func(c *absdk.EventContext) error {
				return flow.BeginText(c, "name", nil, "你想让我怎么称呼你?")
			})

		ctx.Command(cfg.SummaryCommand).
			Name("profile.summary").
			Handle(func(c *absdk.EventContext) error {
				value, ok, err := absdk.UserState[profile](ctx, c, "profile").Load()
				if err != nil {
					return err
				}
				if !ok {
					_, err := c.ReplyText("我还不认识你。先发送 /" + cfg.ProfileCommand + " 建档。")
					return err
				}
				_, err = c.ReplyText(fmt.Sprintf("你是 %s,常在 %s。", value.Name, value.City))
				return err
			})

		return nil
	},
})

Functions

This section is empty.

Types

type Config

type Config struct {
	ProfileCommand string `yaml:"profile_command"`
	SummaryCommand string `yaml:"summary_command"`
	TTL            string `yaml:"ttl"`
}

func (Config) Validate

func (cfg Config) Validate() error

Jump to

Keyboard shortcuts

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