|
| 1 | +package newsapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + // Packages |
| 7 | + llm "github.com/mutablelogic/go-llm" |
| 8 | +) |
| 9 | + |
| 10 | +/////////////////////////////////////////////////////////////////////////////// |
| 11 | +// TYPES |
| 12 | + |
| 13 | +type headlines struct { |
| 14 | + *Client `json:"-"` |
| 15 | +} |
| 16 | + |
| 17 | +var _ llm.Tool = (*headlines)(nil) |
| 18 | + |
| 19 | +/////////////////////////////////////////////////////////////////////////////// |
| 20 | +// HEADLINES |
| 21 | + |
| 22 | +func (headlines) Name() string { |
| 23 | + return "current_headlines" |
| 24 | +} |
| 25 | + |
| 26 | +func (headlines) Description() string { |
| 27 | + return "Return the current news headlines" |
| 28 | +} |
| 29 | + |
| 30 | +func (headlines *headlines) Run(ctx context.Context) (any, error) { |
| 31 | + response, err := headlines.Headlines(OptCategory("general"), OptLimit(5)) |
| 32 | + if err != nil { |
| 33 | + return nil, err |
| 34 | + } |
| 35 | + return map[string]any{ |
| 36 | + "type": "text", |
| 37 | + "headlines": response, |
| 38 | + }, nil |
| 39 | +} |
| 40 | + |
| 41 | +/* |
| 42 | +// Return all the agent tools for the weatherapi |
| 43 | +func (c *Client) Tools() []agent.Tool { |
| 44 | + return []agent.Tool{ |
| 45 | + &tool{ |
| 46 | + name: "current_headlines", |
| 47 | + description: "Return the current news headlines", |
| 48 | + run: c.agentCurrentHeadlines, |
| 49 | + }, &tool{ |
| 50 | + name: "current_headlines_country", |
| 51 | + description: "Return the current news headlines for a country", |
| 52 | + run: c.agentCountryHeadlines, |
| 53 | + params: []agent.ToolParameter{ |
| 54 | + { |
| 55 | + Name: "countrycode", |
| 56 | + Description: "The two-letter country code to return headlines for", |
| 57 | + Required: true, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, &tool{ |
| 61 | + name: "current_headlines_category", |
| 62 | + description: "Return the current news headlines for a business, entertainment, health, science, sports or technology", |
| 63 | + run: c.agentCategoryHeadlines, |
| 64 | + params: []agent.ToolParameter{ |
| 65 | + { |
| 66 | + Name: "category", |
| 67 | + Description: "business, entertainment, health, science, sports, technology", |
| 68 | + Required: true, |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, &tool{ |
| 72 | + name: "search_news", |
| 73 | + description: "Return the news headlines with a search query", |
| 74 | + run: c.agentSearchNews, |
| 75 | + params: []agent.ToolParameter{ |
| 76 | + { |
| 77 | + Name: "query", |
| 78 | + Description: "A phrase used to search for news headlines", |
| 79 | + Required: true, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + } |
| 84 | +} |
| 85 | +
|
| 86 | +/////////////////////////////////////////////////////////////////////////////// |
| 87 | +// PRIVATE METHODS - TOOL |
| 88 | +
|
| 89 | +func (*tool) Provider() string { |
| 90 | + return "newsapi" |
| 91 | +} |
| 92 | +
|
| 93 | +func (t *tool) Name() string { |
| 94 | + return t.name |
| 95 | +} |
| 96 | +
|
| 97 | +func (t *tool) Description() string { |
| 98 | + return t.description |
| 99 | +} |
| 100 | +
|
| 101 | +func (t *tool) Params() []agent.ToolParameter { |
| 102 | + return t.params |
| 103 | +} |
| 104 | +
|
| 105 | +func (t *tool) Run(ctx context.Context, call *agent.ToolCall) (*agent.ToolResult, error) { |
| 106 | + return t.run(ctx, call) |
| 107 | +} |
| 108 | +
|
| 109 | +/////////////////////////////////////////////////////////////////////////////// |
| 110 | +// PRIVATE METHODS - TOOL |
| 111 | +
|
| 112 | +// Return the current general headlines |
| 113 | +func (c *Client) agentCurrentHeadlines(_ context.Context, call *agent.ToolCall) (*agent.ToolResult, error) { |
| 114 | + response, err := c.Headlines(OptCategory("general"), OptLimit(5)) |
| 115 | + if err != nil { |
| 116 | + return nil, err |
| 117 | + } |
| 118 | + return &agent.ToolResult{ |
| 119 | + Id: call.Id, |
| 120 | + Result: map[string]any{ |
| 121 | + "type": "text", |
| 122 | + "headlines": response, |
| 123 | + }, |
| 124 | + }, nil |
| 125 | +} |
| 126 | +
|
| 127 | +// Return the headlines for a specific country |
| 128 | +func (c *Client) agentCountryHeadlines(_ context.Context, call *agent.ToolCall) (*agent.ToolResult, error) { |
| 129 | + country, err := call.String("countrycode") |
| 130 | + if err != nil { |
| 131 | + return nil, err |
| 132 | + } |
| 133 | + country = strings.ToLower(country) |
| 134 | + response, err := c.Headlines(OptCountry(country), OptLimit(5)) |
| 135 | + if err != nil { |
| 136 | + return nil, err |
| 137 | + } |
| 138 | + return &agent.ToolResult{ |
| 139 | + Id: call.Id, |
| 140 | + Result: map[string]any{ |
| 141 | + "type": "text", |
| 142 | + "country": country, |
| 143 | + "headlines": response, |
| 144 | + }, |
| 145 | + }, nil |
| 146 | +} |
| 147 | +
|
| 148 | +// Return the headlines for a specific category |
| 149 | +func (c *Client) agentCategoryHeadlines(_ context.Context, call *agent.ToolCall) (*agent.ToolResult, error) { |
| 150 | + category, err := call.String("category") |
| 151 | + if err != nil { |
| 152 | + return nil, err |
| 153 | + } |
| 154 | + category = strings.ToLower(category) |
| 155 | + response, err := c.Headlines(OptCategory(category), OptLimit(5)) |
| 156 | + if err != nil { |
| 157 | + return nil, err |
| 158 | + } |
| 159 | + return &agent.ToolResult{ |
| 160 | + Id: call.Id, |
| 161 | + Result: map[string]any{ |
| 162 | + "type": "text", |
| 163 | + "category": category, |
| 164 | + "headlines": response, |
| 165 | + }, |
| 166 | + }, nil |
| 167 | +} |
| 168 | +
|
| 169 | +// Return the headlines for a specific query |
| 170 | +func (c *Client) agentSearchNews(_ context.Context, call *agent.ToolCall) (*agent.ToolResult, error) { |
| 171 | + query, err := call.String("query") |
| 172 | + if err != nil { |
| 173 | + return nil, err |
| 174 | + } |
| 175 | + response, err := c.Articles(OptQuery(query), OptLimit(5)) |
| 176 | + if err != nil { |
| 177 | + return nil, err |
| 178 | + } |
| 179 | + return &agent.ToolResult{ |
| 180 | + Id: call.Id, |
| 181 | + Result: map[string]any{ |
| 182 | + "type": "text", |
| 183 | + "query": query, |
| 184 | + "headlines": response, |
| 185 | + }, |
| 186 | + }, nil |
| 187 | +} |
| 188 | +*/ |
0 commit comments