package tools import ( "context" "strings" ) type Product struct { ID string `json:"id"` Name string `json:"name"` Price float64 `json:"price"` Description string `json:"description"` } type Tool interface { GetByID(ctx context.Context, id string) (*Product, error) SearchByName(ctx context.Context, name string) ([]*Product, error) } type memoryTool struct { items []*Product } func NewMemoryProductTool() Tool { return &memoryTool{items: []*Product{ {ID: "P001", Name: "手机 A1", Price: 1999, Description: "入门级智能手机"}, {ID: "P002", Name: "手机 Pro X", Price: 5999, Description: "旗舰级拍照手机"}, {ID: "P003", Name: "笔记本 M3", Price: 8999, Description: "轻薄高性能笔记本"}, {ID: "P004", Name: "耳机 Air", Price: 1299, Description: "降噪真无线耳机"}, }} } func (m *memoryTool) GetByID(ctx context.Context, id string) (*Product, error) { for _, it := range m.items { if it.ID == id { return it, nil } } return nil, nil } func (m *memoryTool) SearchByName(ctx context.Context, name string) ([]*Product, error) { if name == "" { return nil, nil } q := strings.ToLower(name) var out []*Product for _, it := range m.items { if strings.Contains(strings.ToLower(it.Name), q) { out = append(out, it) } } return out, nil }