refactor(wecom): simplify stream message structure by introducing WeComAIBotMsgItem and WeComAIBotMsgItemImage types
This commit is contained in:
parent
880c402ab7
commit
79bc06c0ba
1 changed files with 23 additions and 83 deletions
|
|
@ -98,18 +98,24 @@ type WeComAIBotMessage struct {
|
||||||
} `json:"event,omitempty"`
|
} `json:"event,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// WeComAIBotStreamInfo represents the detailed stream content in streaming responses
|
// WeComAIBotMsgItemImage holds the image payload inside a stream message item.
|
||||||
|
type WeComAIBotMsgItemImage struct {
|
||||||
|
Base64 string `json:"base64"`
|
||||||
|
MD5 string `json:"md5"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// WeComAIBotMsgItem is a single item inside a stream's msg_item list.
|
||||||
|
type WeComAIBotMsgItem struct {
|
||||||
|
MsgType string `json:"msgtype"`
|
||||||
|
Image *WeComAIBotMsgItemImage `json:"image,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// WeComAIBotStreamInfo represents the detailed stream content in streaming responses.
|
||||||
type WeComAIBotStreamInfo struct {
|
type WeComAIBotStreamInfo struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Finish bool `json:"finish"`
|
Finish bool `json:"finish"`
|
||||||
Content string `json:"content,omitempty"`
|
Content string `json:"content,omitempty"`
|
||||||
MsgItem []struct {
|
MsgItem []WeComAIBotMsgItem `json:"msg_item,omitempty"`
|
||||||
MsgType string `json:"msgtype"`
|
|
||||||
Image *struct {
|
|
||||||
Base64 string `json:"base64"`
|
|
||||||
MD5 string `json:"md5"`
|
|
||||||
} `json:"image,omitempty"`
|
|
||||||
} `json:"msg_item,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// WeComAIBotStreamResponse represents the streaming response format
|
// WeComAIBotStreamResponse represents the streaming response format
|
||||||
|
|
@ -457,18 +463,7 @@ func (c *WeComAIBotChannel) processMessage(
|
||||||
})
|
})
|
||||||
return c.encryptResponse("", timestamp, nonce, WeComAIBotStreamResponse{
|
return c.encryptResponse("", timestamp, nonce, WeComAIBotStreamResponse{
|
||||||
MsgType: "stream",
|
MsgType: "stream",
|
||||||
Stream: struct {
|
Stream: WeComAIBotStreamInfo{
|
||||||
ID string `json:"id"`
|
|
||||||
Finish bool `json:"finish"`
|
|
||||||
Content string `json:"content,omitempty"`
|
|
||||||
MsgItem []struct {
|
|
||||||
MsgType string `json:"msgtype"`
|
|
||||||
Image *struct {
|
|
||||||
Base64 string `json:"base64"`
|
|
||||||
MD5 string `json:"md5"`
|
|
||||||
} `json:"image,omitempty"`
|
|
||||||
} `json:"msg_item,omitempty"`
|
|
||||||
}{
|
|
||||||
ID: c.generateStreamID(),
|
ID: c.generateStreamID(),
|
||||||
Finish: true,
|
Finish: true,
|
||||||
Content: "Unsupported message type: " + msg.MsgType,
|
Content: "Unsupported message type: " + msg.MsgType,
|
||||||
|
|
@ -586,18 +581,7 @@ func (c *WeComAIBotChannel) handleStreamMessage(
|
||||||
)
|
)
|
||||||
return c.encryptResponse(streamID, timestamp, nonce, WeComAIBotStreamResponse{
|
return c.encryptResponse(streamID, timestamp, nonce, WeComAIBotStreamResponse{
|
||||||
MsgType: "stream",
|
MsgType: "stream",
|
||||||
Stream: struct {
|
Stream: WeComAIBotStreamInfo{
|
||||||
ID string `json:"id"`
|
|
||||||
Finish bool `json:"finish"`
|
|
||||||
Content string `json:"content,omitempty"`
|
|
||||||
MsgItem []struct {
|
|
||||||
MsgType string `json:"msgtype"`
|
|
||||||
Image *struct {
|
|
||||||
Base64 string `json:"base64"`
|
|
||||||
MD5 string `json:"md5"`
|
|
||||||
} `json:"image,omitempty"`
|
|
||||||
} `json:"msg_item,omitempty"`
|
|
||||||
}{
|
|
||||||
ID: streamID,
|
ID: streamID,
|
||||||
Finish: true,
|
Finish: true,
|
||||||
Content: "Task not found or already finished. Please resend your message to start a new session.",
|
Content: "Task not found or already finished. Please resend your message to start a new session.",
|
||||||
|
|
@ -626,18 +610,7 @@ func (c *WeComAIBotChannel) handleImageMessage(
|
||||||
// For now, just acknowledge receipt without echoing the image
|
// For now, just acknowledge receipt without echoing the image
|
||||||
return c.encryptResponse("", timestamp, nonce, WeComAIBotStreamResponse{
|
return c.encryptResponse("", timestamp, nonce, WeComAIBotStreamResponse{
|
||||||
MsgType: "stream",
|
MsgType: "stream",
|
||||||
Stream: struct {
|
Stream: WeComAIBotStreamInfo{
|
||||||
ID string `json:"id"`
|
|
||||||
Finish bool `json:"finish"`
|
|
||||||
Content string `json:"content,omitempty"`
|
|
||||||
MsgItem []struct {
|
|
||||||
MsgType string `json:"msgtype"`
|
|
||||||
Image *struct {
|
|
||||||
Base64 string `json:"base64"`
|
|
||||||
MD5 string `json:"md5"`
|
|
||||||
} `json:"image,omitempty"`
|
|
||||||
} `json:"msg_item,omitempty"`
|
|
||||||
}{
|
|
||||||
ID: c.generateStreamID(),
|
ID: c.generateStreamID(),
|
||||||
Finish: true,
|
Finish: true,
|
||||||
Content: fmt.Sprintf(
|
Content: fmt.Sprintf(
|
||||||
|
|
@ -657,18 +630,7 @@ func (c *WeComAIBotChannel) handleMixedMessage(
|
||||||
logger.WarnC("wecom_aibot", "Mixed message type not yet fully implemented")
|
logger.WarnC("wecom_aibot", "Mixed message type not yet fully implemented")
|
||||||
return c.encryptResponse("", timestamp, nonce, WeComAIBotStreamResponse{
|
return c.encryptResponse("", timestamp, nonce, WeComAIBotStreamResponse{
|
||||||
MsgType: "stream",
|
MsgType: "stream",
|
||||||
Stream: struct {
|
Stream: WeComAIBotStreamInfo{
|
||||||
ID string `json:"id"`
|
|
||||||
Finish bool `json:"finish"`
|
|
||||||
Content string `json:"content,omitempty"`
|
|
||||||
MsgItem []struct {
|
|
||||||
MsgType string `json:"msgtype"`
|
|
||||||
Image *struct {
|
|
||||||
Base64 string `json:"base64"`
|
|
||||||
MD5 string `json:"md5"`
|
|
||||||
} `json:"image,omitempty"`
|
|
||||||
} `json:"msg_item,omitempty"`
|
|
||||||
}{
|
|
||||||
ID: c.generateStreamID(),
|
ID: c.generateStreamID(),
|
||||||
Finish: true,
|
Finish: true,
|
||||||
Content: "Mixed message type is not yet supported",
|
Content: "Mixed message type is not yet supported",
|
||||||
|
|
@ -695,18 +657,7 @@ func (c *WeComAIBotChannel) handleEventMessage(
|
||||||
streamID := c.generateStreamID()
|
streamID := c.generateStreamID()
|
||||||
return c.encryptResponse(streamID, timestamp, nonce, WeComAIBotStreamResponse{
|
return c.encryptResponse(streamID, timestamp, nonce, WeComAIBotStreamResponse{
|
||||||
MsgType: "stream",
|
MsgType: "stream",
|
||||||
Stream: struct {
|
Stream: WeComAIBotStreamInfo{
|
||||||
ID string `json:"id"`
|
|
||||||
Finish bool `json:"finish"`
|
|
||||||
Content string `json:"content,omitempty"`
|
|
||||||
MsgItem []struct {
|
|
||||||
MsgType string `json:"msgtype"`
|
|
||||||
Image *struct {
|
|
||||||
Base64 string `json:"base64"`
|
|
||||||
MD5 string `json:"md5"`
|
|
||||||
} `json:"image,omitempty"`
|
|
||||||
} `json:"msg_item,omitempty"`
|
|
||||||
}{
|
|
||||||
ID: streamID,
|
ID: streamID,
|
||||||
Finish: true,
|
Finish: true,
|
||||||
Content: c.config.WelcomeMessage,
|
Content: c.config.WelcomeMessage,
|
||||||
|
|
@ -765,18 +716,7 @@ func (c *WeComAIBotChannel) getStreamResponse(task *streamTask, timestamp, nonce
|
||||||
|
|
||||||
response := WeComAIBotStreamResponse{
|
response := WeComAIBotStreamResponse{
|
||||||
MsgType: "stream",
|
MsgType: "stream",
|
||||||
Stream: struct {
|
Stream: WeComAIBotStreamInfo{
|
||||||
ID string `json:"id"`
|
|
||||||
Finish bool `json:"finish"`
|
|
||||||
Content string `json:"content,omitempty"`
|
|
||||||
MsgItem []struct {
|
|
||||||
MsgType string `json:"msgtype"`
|
|
||||||
Image *struct {
|
|
||||||
Base64 string `json:"base64"`
|
|
||||||
MD5 string `json:"md5"`
|
|
||||||
} `json:"image,omitempty"`
|
|
||||||
} `json:"msg_item,omitempty"`
|
|
||||||
}{
|
|
||||||
ID: task.StreamID,
|
ID: task.StreamID,
|
||||||
Finish: finish,
|
Finish: finish,
|
||||||
Content: content,
|
Content: content,
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue