feat: enrich receive messages with display names
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||
|
||||
"isphere-ai-bridge/internal/isphere"
|
||||
"isphere-ai-bridge/internal/msglib"
|
||||
)
|
||||
|
||||
const ToolNameReceiveMessages = "isphere_receive_messages"
|
||||
@@ -30,6 +31,10 @@ type ReceiveMessagesArgs struct {
|
||||
}
|
||||
|
||||
func RegisterISphereReadTools(server *mcp.Server, source ReceiveMessagesSource) {
|
||||
RegisterISphereReadToolsWithDisplayEntities(server, source, nil)
|
||||
}
|
||||
|
||||
func RegisterISphereReadToolsWithDisplayEntities(server *mcp.Server, source ReceiveMessagesSource, displaySource DisplayEntitySource) {
|
||||
if source == nil {
|
||||
source = isphere.EncryptedPacketLogSource{}
|
||||
}
|
||||
@@ -51,8 +56,17 @@ func RegisterISphereReadTools(server *mcp.Server, source ReceiveMessagesSource)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
displayNames := receiveMessageDisplayNames{}
|
||||
if displaySource != nil && len(result.Messages) > 0 {
|
||||
loaded, err := loadReceiveMessageDisplayNames(ctx, displaySource)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
displayNames = loaded
|
||||
}
|
||||
return nil, receiveMessagesResultToMap(result, started, time.Now().UTC(), receiveMessagesMapOptions{
|
||||
IncludeAttachmentMetadata: includeReceiveMessageAttachmentMetadata(input),
|
||||
DisplayNames: displayNames,
|
||||
}), nil
|
||||
})
|
||||
}
|
||||
@@ -83,6 +97,12 @@ func includeReceiveMessageAttachmentMetadata(input ReceiveMessagesArgs) bool {
|
||||
|
||||
type receiveMessagesMapOptions struct {
|
||||
IncludeAttachmentMetadata bool
|
||||
DisplayNames receiveMessageDisplayNames
|
||||
}
|
||||
|
||||
type receiveMessageDisplayNames struct {
|
||||
Contacts map[string]string
|
||||
Groups map[string]string
|
||||
}
|
||||
|
||||
func receiveMessagesResultToMap(result isphere.ReceiveMessagesResult, started time.Time, finished time.Time, options receiveMessagesMapOptions) map[string]any {
|
||||
@@ -92,13 +112,14 @@ func receiveMessagesResultToMap(result isphere.ReceiveMessagesResult, started ti
|
||||
if options.IncludeAttachmentMetadata {
|
||||
attachments = receiveMessageAttachments(message)
|
||||
}
|
||||
senderName := nullableDisplayName(options.DisplayNames.Contacts, message.SenderID)
|
||||
messages = append(messages, map[string]any{
|
||||
"id": message.ID,
|
||||
"message_id": message.ID,
|
||||
"conversation_id": message.ConversationID,
|
||||
"conversation_type": message.ConversationType,
|
||||
"sender_id": message.SenderID,
|
||||
"sender_name": nil,
|
||||
"sender_name": senderName,
|
||||
"receiver_id": message.ReceiverID,
|
||||
"text": message.Text,
|
||||
"content_type": receiveMessageContentType(message, attachments),
|
||||
@@ -116,7 +137,7 @@ func receiveMessagesResultToMap(result isphere.ReceiveMessagesResult, started ti
|
||||
}
|
||||
return map[string]any{
|
||||
"ok": true,
|
||||
"conversation": receiveMessagesConversation(result.Messages),
|
||||
"conversation": receiveMessagesConversation(result.Messages, options.DisplayNames),
|
||||
"messages": messages,
|
||||
"next_cursor": nil,
|
||||
"audit": map[string]any{
|
||||
@@ -130,18 +151,85 @@ func receiveMessagesResultToMap(result isphere.ReceiveMessagesResult, started ti
|
||||
}
|
||||
}
|
||||
|
||||
func receiveMessagesConversation(messages []isphere.Message) any {
|
||||
func receiveMessagesConversation(messages []isphere.Message, displayNames receiveMessageDisplayNames) any {
|
||||
if len(messages) == 0 {
|
||||
return nil
|
||||
}
|
||||
first := messages[0]
|
||||
conversationType := contractConversationType(first.ConversationType)
|
||||
displayName := first.ConversationID
|
||||
if conversationType == "group" {
|
||||
if name := lookupDisplayName(displayNames.Groups, first.SenderID, first.ReceiverID, first.ConversationID); name != "" {
|
||||
displayName = name
|
||||
}
|
||||
} else if name := lookupDisplayName(displayNames.Contacts, first.SenderID, first.ReceiverID); name != "" {
|
||||
displayName = name
|
||||
}
|
||||
return map[string]any{
|
||||
"conversation_id": first.ConversationID,
|
||||
"conversation_type": contractConversationType(first.ConversationType),
|
||||
"display_name": first.ConversationID,
|
||||
"conversation_type": conversationType,
|
||||
"display_name": displayName,
|
||||
}
|
||||
}
|
||||
|
||||
func loadReceiveMessageDisplayNames(ctx context.Context, source DisplayEntitySource) (receiveMessageDisplayNames, error) {
|
||||
contacts, err := source.DisplayEntities(ctx, msglib.DisplayEntitiesOptions{EntityType: msglib.EntityTypeContacts, Limit: msglib.MaxDisplayEntityLimit})
|
||||
if err != nil {
|
||||
return receiveMessageDisplayNames{}, err
|
||||
}
|
||||
groups, err := source.DisplayEntities(ctx, msglib.DisplayEntitiesOptions{EntityType: msglib.EntityTypeGroups, Limit: msglib.MaxDisplayEntityLimit})
|
||||
if err != nil {
|
||||
return receiveMessageDisplayNames{}, err
|
||||
}
|
||||
return receiveMessageDisplayNames{
|
||||
Contacts: displayEntityNameMap(contacts, msglib.EntityTypeContacts),
|
||||
Groups: displayEntityNameMap(groups, msglib.EntityTypeGroups),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func displayEntityNameMap(entities []msglib.DisplayEntity, entityType string) map[string]string {
|
||||
names := map[string]string{}
|
||||
for _, entity := range entities {
|
||||
if entity.EntityType != "" && entity.EntityType != entityType {
|
||||
continue
|
||||
}
|
||||
jid := strings.ToLower(strings.TrimSpace(entity.JID))
|
||||
name := strings.TrimSpace(entity.DisplayName)
|
||||
if jid == "" || name == "" {
|
||||
continue
|
||||
}
|
||||
if _, exists := names[jid]; !exists {
|
||||
names[jid] = name
|
||||
}
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
func nullableDisplayName(names map[string]string, id string) any {
|
||||
if name := lookupDisplayName(names, id); name != "" {
|
||||
return name
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func lookupDisplayName(names map[string]string, candidates ...string) string {
|
||||
if len(names) == 0 {
|
||||
return ""
|
||||
}
|
||||
for _, candidate := range candidates {
|
||||
for _, part := range strings.Split(candidate, "|") {
|
||||
key := strings.ToLower(strings.TrimSpace(part))
|
||||
if key == "" {
|
||||
continue
|
||||
}
|
||||
if name := names[key]; name != "" {
|
||||
return name
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func contractConversationType(value string) string {
|
||||
switch strings.ToLower(strings.TrimSpace(value)) {
|
||||
case "groupchat", "group":
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||
|
||||
"isphere-ai-bridge/internal/isphere"
|
||||
"isphere-ai-bridge/internal/msglib"
|
||||
)
|
||||
|
||||
func TestISphereReceiveMessagesToolReturnsSourceMessages(t *testing.T) {
|
||||
@@ -142,6 +143,76 @@ func TestISphereReceiveMessagesToolReturnsSourceMessages(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestISphereReceiveMessagesToolUsesInjectedDisplayEntities(t *testing.T) {
|
||||
fake := &fakeReceiveMessagesSource{
|
||||
result: isphere.ReceiveMessagesResult{Messages: []isphere.Message{{
|
||||
ID: "msg-display-1",
|
||||
ConversationID: "project-room@conference|sender@example",
|
||||
ConversationType: "groupchat",
|
||||
SenderID: "sender@example",
|
||||
ReceiverID: "project-room@conference",
|
||||
Text: "keep this body unchanged",
|
||||
Timestamp: "1783423807000",
|
||||
}}},
|
||||
}
|
||||
fakeDisplay := &fakeDisplayEntitySource{entities: []msglib.DisplayEntity{
|
||||
{EntityType: msglib.EntityTypeContacts, SourceTable: "TD_CustomEffigy", JID: "sender@example", DisplayName: "Sender Name", Confidence: 0.9},
|
||||
{EntityType: msglib.EntityTypeGroups, SourceTable: "TD_WorkGroupAuth", JID: "project-room@conference", DisplayName: "Project Room", Confidence: 0.9},
|
||||
}}
|
||||
session, cleanup := connectToolsTestSession(t, func(server *mcp.Server) {
|
||||
RegisterISphereReadToolsWithDisplayEntities(server, fake, fakeDisplay)
|
||||
})
|
||||
defer cleanup()
|
||||
|
||||
callResult, err := session.CallTool(context.Background(), &mcp.CallToolParams{
|
||||
Name: ToolNameReceiveMessages,
|
||||
Arguments: map[string]any{"query": "body", "limit": 5},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("call %s: %v", ToolNameReceiveMessages, err)
|
||||
}
|
||||
if callResult.IsError {
|
||||
t.Fatalf("call result is error: %+v", callResult)
|
||||
}
|
||||
|
||||
var decoded struct {
|
||||
Conversation *struct {
|
||||
ConversationID string `json:"conversation_id"`
|
||||
ConversationType string `json:"conversation_type"`
|
||||
DisplayName string `json:"display_name"`
|
||||
} `json:"conversation"`
|
||||
Messages []struct {
|
||||
SenderID string `json:"sender_id"`
|
||||
SenderName string `json:"sender_name"`
|
||||
Text string `json:"text"`
|
||||
ContentText string `json:"content_text"`
|
||||
} `json:"messages"`
|
||||
}
|
||||
payload, _ := json.Marshal(callResult.StructuredContent)
|
||||
if err := json.Unmarshal(payload, &decoded); err != nil {
|
||||
t.Fatalf("decode structured content %s: %v", payload, err)
|
||||
}
|
||||
if decoded.Conversation == nil || decoded.Conversation.DisplayName != "Project Room" || decoded.Conversation.ConversationType != "group" {
|
||||
t.Fatalf("unexpected conversation: %+v", decoded.Conversation)
|
||||
}
|
||||
if len(decoded.Messages) != 1 {
|
||||
t.Fatalf("messages = %+v, want one", decoded.Messages)
|
||||
}
|
||||
msg := decoded.Messages[0]
|
||||
if msg.SenderID != "sender@example" || msg.SenderName != "Sender Name" {
|
||||
t.Fatalf("unexpected sender fields: %+v", msg)
|
||||
}
|
||||
if msg.Text != "keep this body unchanged" || msg.ContentText != "keep this body unchanged" {
|
||||
t.Fatalf("message text changed: %+v", msg)
|
||||
}
|
||||
if len(fakeDisplay.queries) != 2 {
|
||||
t.Fatalf("display queries = %+v, want contacts and groups lookup", fakeDisplay.queries)
|
||||
}
|
||||
if fakeDisplay.queries[0].EntityType != msglib.EntityTypeContacts || fakeDisplay.queries[1].EntityType != msglib.EntityTypeGroups {
|
||||
t.Fatalf("display query entity types = %+v", fakeDisplay.queries)
|
||||
}
|
||||
}
|
||||
|
||||
func TestISphereReceiveMessagesToolValidatesRemainingContractArgs(t *testing.T) {
|
||||
fake := &fakeReceiveMessagesSource{
|
||||
result: isphere.ReceiveMessagesResult{Messages: []isphere.Message{{
|
||||
|
||||
Reference in New Issue
Block a user