-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* beta1 * beta2 * beta3 * beta4 * beta5 * beta6 * beta7 * beta8 * beta9 * beta10 * beta11 * beta12 * beta13 * beta14 * beta15 * beta16 * beta16 * beta19 * beta20 * beta21 * beta22 * beta23 * beta24 * beta25 * beta27 * beta28 * beta29 * beta30 * beta31 * beta33 * beta34 * beta35 * beta36 * beta37 * beta38 * beta39 * beta40 * beta41 * beta42 * beta43 * beta44 * beta45 * beta45 * beta46 * beat48 * beta49 * beta50 * beta51 * beta52 * beta53 * beta54 * beta55 * beta57 * beta58 * beta59 * beta61 * beta62 * beta63 * beta63 * beta64 * beta65 * beta66 * beta67 * beta70 * beta71 * beta72 * beta72 * beta74 * beta75 * beta76 * beta77 * beta78 * beta79 * beta80 * beta81 * beta82 * beta83 * beta85 * beta86 * beta87 * beta88 * beta89 * beta90 * beta91 * beta92 * beta93 * beta94 * beta94 * beta96 * beta97 * beta98 * beta99 * beta100 * beta101 * beta102 * beta104 * beta105 * beta106 * beta107 * beta108 * beta109 * beta110 * beta111 * beta112
- Loading branch information
1 parent
38e4f5a
commit c86d2b5
Showing
11 changed files
with
907 additions
and
6 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package applogic | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/json" | ||
"strings" | ||
|
||
"github.com/hoshinonyaruko/gensokyo-llm/fmtf" | ||
) | ||
|
||
func ParseMessageContent(message interface{}) string { | ||
messageText := "" | ||
|
||
switch message := message.(type) { | ||
case string: | ||
fmtf.Printf("params.message is a string\n") | ||
messageText = message | ||
case []interface{}: | ||
//多个映射组成的切片 | ||
fmtf.Printf("params.message is a slice (segment_type_koishi)\n") | ||
for _, segment := range message { | ||
segmentMap, ok := segment.(map[string]interface{}) | ||
if !ok { | ||
continue | ||
} | ||
|
||
segmentType, ok := segmentMap["type"].(string) | ||
if !ok { | ||
continue | ||
} | ||
|
||
segmentContent := "" | ||
switch segmentType { | ||
case "text": | ||
segmentContent, _ = segmentMap["data"].(map[string]interface{})["text"].(string) | ||
case "image": | ||
fileContent, _ := segmentMap["data"].(map[string]interface{})["file"].(string) | ||
segmentContent = "[CQ:image,file=" + fileContent + "]" | ||
case "voice": | ||
fileContent, _ := segmentMap["data"].(map[string]interface{})["file"].(string) | ||
segmentContent = "[CQ:record,file=" + fileContent + "]" | ||
case "record": | ||
fileContent, _ := segmentMap["data"].(map[string]interface{})["file"].(string) | ||
segmentContent = "[CQ:record,file=" + fileContent + "]" | ||
case "at": | ||
qqNumber, _ := segmentMap["data"].(map[string]interface{})["qq"].(string) | ||
segmentContent = "[CQ:at,qq=" + qqNumber + "]" | ||
case "markdown": | ||
mdContent, ok := segmentMap["data"].(map[string]interface{})["data"] | ||
if ok { | ||
if mdContentMap, isMap := mdContent.(map[string]interface{}); isMap { | ||
// mdContent是map[string]interface{},按map处理 | ||
mdContentBytes, err := json.Marshal(mdContentMap) | ||
if err != nil { | ||
fmtf.Printf("Error marshaling mdContentMap to JSON:%v", err) | ||
} | ||
encoded := base64.StdEncoding.EncodeToString(mdContentBytes) | ||
segmentContent = "[CQ:markdown,data=" + encoded + "]" | ||
} else if mdContentStr, isString := mdContent.(string); isString { | ||
// mdContent是string | ||
if strings.HasPrefix(mdContentStr, "base64://") { | ||
// 如果以base64://开头,直接使用 | ||
segmentContent = "[CQ:markdown,data=" + mdContentStr + "]" | ||
} else { | ||
// 处理实体化后的JSON文本 | ||
mdContentStr = strings.ReplaceAll(mdContentStr, "&", "&") | ||
mdContentStr = strings.ReplaceAll(mdContentStr, "[", "[") | ||
mdContentStr = strings.ReplaceAll(mdContentStr, "]", "]") | ||
mdContentStr = strings.ReplaceAll(mdContentStr, ",", ",") | ||
|
||
// 将处理过的字符串视为JSON对象,进行序列化和编码 | ||
var jsonMap map[string]interface{} | ||
if err := json.Unmarshal([]byte(mdContentStr), &jsonMap); err != nil { | ||
fmtf.Printf("Error unmarshaling string to JSON:%v", err) | ||
} | ||
mdContentBytes, err := json.Marshal(jsonMap) | ||
if err != nil { | ||
fmtf.Printf("Error marshaling jsonMap to JSON:%v", err) | ||
} | ||
encoded := base64.StdEncoding.EncodeToString(mdContentBytes) | ||
segmentContent = "[CQ:markdown,data=" + encoded + "]" | ||
} | ||
} | ||
} else { | ||
fmtf.Printf("Error marshaling markdown segment to interface,contain type but data is nil.") | ||
} | ||
} | ||
|
||
messageText += segmentContent | ||
} | ||
case map[string]interface{}: | ||
//单个映射 | ||
fmtf.Printf("params.message is a map (segment_type_trss)\n") | ||
messageType, _ := message["type"].(string) | ||
switch messageType { | ||
case "text": | ||
messageText, _ = message["data"].(map[string]interface{})["text"].(string) | ||
case "image": | ||
fileContent, _ := message["data"].(map[string]interface{})["file"].(string) | ||
messageText = "[CQ:image,file=" + fileContent + "]" | ||
case "voice": | ||
fileContent, _ := message["data"].(map[string]interface{})["file"].(string) | ||
messageText = "[CQ:record,file=" + fileContent + "]" | ||
case "record": | ||
fileContent, _ := message["data"].(map[string]interface{})["file"].(string) | ||
messageText = "[CQ:record,file=" + fileContent + "]" | ||
case "at": | ||
qqNumber, _ := message["data"].(map[string]interface{})["qq"].(string) | ||
messageText = "[CQ:at,qq=" + qqNumber + "]" | ||
case "markdown": | ||
mdContent, ok := message["data"].(map[string]interface{})["data"] | ||
if ok { | ||
if mdContentMap, isMap := mdContent.(map[string]interface{}); isMap { | ||
// mdContent是map[string]interface{},按map处理 | ||
mdContentBytes, err := json.Marshal(mdContentMap) | ||
if err != nil { | ||
fmtf.Printf("Error marshaling mdContentMap to JSON:%v", err) | ||
} | ||
encoded := base64.StdEncoding.EncodeToString(mdContentBytes) | ||
messageText = "[CQ:markdown,data=" + encoded + "]" | ||
} else if mdContentStr, isString := mdContent.(string); isString { | ||
// mdContent是string | ||
if strings.HasPrefix(mdContentStr, "base64://") { | ||
// 如果以base64://开头,直接使用 | ||
messageText = "[CQ:markdown,data=" + mdContentStr + "]" | ||
} else { | ||
// 处理实体化后的JSON文本 | ||
mdContentStr = strings.ReplaceAll(mdContentStr, "&", "&") | ||
mdContentStr = strings.ReplaceAll(mdContentStr, "[", "[") | ||
mdContentStr = strings.ReplaceAll(mdContentStr, "]", "]") | ||
mdContentStr = strings.ReplaceAll(mdContentStr, ",", ",") | ||
|
||
// 将处理过的字符串视为JSON对象,进行序列化和编码 | ||
var jsonMap map[string]interface{} | ||
if err := json.Unmarshal([]byte(mdContentStr), &jsonMap); err != nil { | ||
fmtf.Printf("Error unmarshaling string to JSON:%v", err) | ||
} | ||
mdContentBytes, err := json.Marshal(jsonMap) | ||
if err != nil { | ||
fmtf.Printf("Error marshaling jsonMap to JSON:%v", err) | ||
} | ||
encoded := base64.StdEncoding.EncodeToString(mdContentBytes) | ||
messageText = "[CQ:markdown,data=" + encoded + "]" | ||
} | ||
} | ||
} else { | ||
fmtf.Printf("Error marshaling markdown segment to interface,contain type but data is nil.") | ||
} | ||
} | ||
default: | ||
fmtf.Println("Unsupported message format: params.message field is not a string, map or slice") | ||
} | ||
return messageText | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.