feat: enrich search with msglib display entities

This commit is contained in:
zhaoyilun
2026-07-10 03:09:37 +08:00
parent 8be4a6a6c5
commit 1277316663
11 changed files with 400 additions and 30 deletions

View File

@@ -8,6 +8,7 @@ import (
"github.com/modelcontextprotocol/go-sdk/mcp"
"isphere-ai-bridge/internal/isphere"
"isphere-ai-bridge/internal/msglib"
)
func TestISphereSearchContactsToolReturnsJIDContacts(t *testing.T) {
@@ -75,6 +76,61 @@ func TestISphereSearchContactsToolReturnsJIDContacts(t *testing.T) {
}
}
func TestISphereSearchContactsToolUsesInjectedDisplayEntities(t *testing.T) {
fakeMessages := &fakeReceiveMessagesSource{}
fakeDisplay := &fakeDisplayEntitySource{entities: []msglib.DisplayEntity{{
EntityType: msglib.EntityTypeContacts,
SourceTable: "TD_CustomEffigy",
JID: "alice@example",
DisplayName: "Alice Zhang",
Confidence: 0.9,
MatchedColumns: []string{"PersonJid", "PersonName"},
}}}
session, cleanup := connectToolsTestSession(t, func(server *mcp.Server) {
RegisterISphereContactToolsWithDisplayEntities(server, fakeMessages, fakeDisplay)
})
defer cleanup()
callResult, err := session.CallTool(context.Background(), &mcp.CallToolParams{
Name: ToolNameSearchContacts,
Arguments: map[string]any{"query": "alice", "limit": 5},
})
if err != nil {
t.Fatalf("call %s: %v", ToolNameSearchContacts, err)
}
if callResult.IsError {
t.Fatalf("call result is error: %+v", callResult)
}
var decoded struct {
Contacts []struct {
ContactID string `json:"contact_id"`
DisplayName string `json:"display_name"`
Account string `json:"account"`
Source string `json:"source"`
Confidence float64 `json:"confidence"`
RawRef string `json:"raw_ref"`
} `json:"contacts"`
}
payload, _ := json.Marshal(callResult.StructuredContent)
if err := json.Unmarshal(payload, &decoded); err != nil {
t.Fatalf("decode structured content %s: %v", payload, err)
}
if len(decoded.Contacts) != 1 {
t.Fatalf("contacts = %+v, want one", decoded.Contacts)
}
contact := decoded.Contacts[0]
if contact.ContactID != "alice@example" || contact.DisplayName != "Alice Zhang" || contact.Account != "alice@example" {
t.Fatalf("unexpected MsgLib contact identity: %+v", contact)
}
if contact.Source != "msglib_readonly" || contact.Confidence != 0.9 || contact.RawRef != "msglib:TD_CustomEffigy" {
t.Fatalf("unexpected MsgLib contact metadata: %+v", contact)
}
if len(fakeDisplay.queries) != 1 || fakeDisplay.queries[0].EntityType != msglib.EntityTypeContacts || fakeDisplay.queries[0].Query != "alice" || fakeDisplay.queries[0].Limit != 5 {
t.Fatalf("display queries = %+v", fakeDisplay.queries)
}
}
func TestISphereSearchContactsToolValidatesContractArgs(t *testing.T) {
fake := &fakeReceiveMessagesSource{
result: isphere.ReceiveMessagesResult{Messages: []isphere.Message{{
@@ -121,3 +177,13 @@ func TestISphereSearchContactsToolValidatesContractArgs(t *testing.T) {
t.Fatalf("non-empty cursor was accepted before pagination exists")
}
}
type fakeDisplayEntitySource struct {
queries []msglib.DisplayEntitiesOptions
entities []msglib.DisplayEntity
}
func (f *fakeDisplayEntitySource) DisplayEntities(_ context.Context, opts msglib.DisplayEntitiesOptions) ([]msglib.DisplayEntity, error) {
f.queries = append(f.queries, opts)
return f.entities, nil
}