form.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package compapi
  2. import (
  3. "time"
  4. "wechat-api/internal/types"
  5. )
  6. type FormClient struct {
  7. MismatchClient
  8. }
  9. func (me *FormClient) ResponseFormatSetting(req *types.CompApiReq) ResponseFormatConfig {
  10. nowTime := time.Now().Format("2006-01-02 15:04:05")
  11. weekday := time.Now().Weekday().String()
  12. //Message重构的配置
  13. me.ResformatConfig.SysmesArgs = []any{nowTime, weekday}
  14. me.ResformatConfig.SysmesTmpl = `# 任务
  15. 请帮助user从聊天记录中提取表单值,并返回一个JSON格式的表单值。
  16. # 背景信息
  17. 当前时间是:%s
  18. 当前星期是:%s
  19. # 返回值示例
  20. * 如表单类型为 input、autoComplete、textarea,返回示例:["表单值"]
  21. * 如表单类型为 radio、select,返回示例:["值1"]
  22. * 如表单类型为 checkbox,返回示例:["值1", "值2"]
  23. * 如表单类型为 cascader,返回示例:["一级值1", "二级值3"]
  24. * 如表单类型为 date,返回示例:["2025-01-01"]
  25. * 如没有找到某个表单相关的值,请不要返回该表单`
  26. me.ResformatConfig.UsermesArgs = []any{req.Variables["form_data"], req.Variables["chat_history"]}
  27. me.ResformatConfig.UsermesTmpl = `# 待提取表单
  28. %s
  29. # 聊天记录
  30. %s`
  31. //ResponseFormat设置的配置
  32. me.ResformatConfig.ResformatDesc = "从通话记录中提取表单"
  33. me.ResformatConfig.ResformatTxt = `[{
  34. "dataIndex": str, # 表单ID
  35. "value": list[str] # 表单值列表
  36. }]`
  37. type formStruct struct {
  38. DataIndex string `json:"dataIndex" jsonschema_description:"表单ID"`
  39. Value []string `json:"value" jsonschema_description:"值列表"`
  40. }
  41. me.ResformatConfig.ResformatStruct = []formStruct{}
  42. me.ResformatConfig.HaveSet = true
  43. return me.ResformatConfig
  44. }
  45. func (me *FormClient) BuildRequest(req *types.CompApiReq) error {
  46. //设置相关个性化ResponseFormat配置信息
  47. if !me.ResformatConfig.HaveSet {
  48. me.ResponseFormatSetting(req)
  49. }
  50. return me.MismatchClient.BuildRequest(req)
  51. }
  52. // 向Client.getClientActFace工厂方法注册
  53. func init() {
  54. builder := func(c *Client) (clientActionFace, error) {
  55. return &FormClient{MismatchClient{StdClient: StdClient{Client: c}}}, nil
  56. }
  57. err := RegisterClient("form", builder)
  58. if err != nil {
  59. //panic(fmt.Sprintf("Failed to register client type 'mismatch': %v", err))
  60. }
  61. }