@@ -10,32 +10,14 @@ import (
1010)
1111
1212///////////////////////////////////////////////////////////////////////////////
13- // TYPES
13+ // HEADLINES
1414
1515type headlines struct {
1616 * Client `json:"-"`
17- // CountryCode string `json:"country_code,omitempty" help:"The two-letter countrycode to return headlines for. Leave empty for worldwide headlines."`
18- }
19-
20- type search struct {
21- * Client `json:"-"`
22- Query string `json:"query" help:"A phrase used to search for news headlines." required:"true"`
23- }
24-
25- type category struct {
26- * Client `json:"-"`
27- Category string `json:"category" enum:"business, entertainment, health, science, sports, technology" help:"business, entertainment, health, science, sports, technology" required:"true"`
2817}
2918
3019var _ llm.Tool = (* headlines )(nil )
3120
32- var (
33- categories = []string {"business" , "entertainment" , "health" , "science" , "sports" , "technology" }
34- )
35-
36- ///////////////////////////////////////////////////////////////////////////////
37- // HEADLINES
38-
3921func (headlines ) Name () string {
4022 return "news_headlines"
4123}
@@ -51,6 +33,13 @@ func (headlines *headlines) Run(ctx context.Context) (any, error) {
5133///////////////////////////////////////////////////////////////////////////////
5234// SEARCH
5335
36+ type search struct {
37+ * Client `json:"-"`
38+ Query string `json:"query" help:"A phrase used to search for news headlines." required:"true"`
39+ }
40+
41+ var _ llm.Tool = (* search )(nil )
42+
5443func (search ) Name () string {
5544 return "news_search"
5645}
@@ -63,13 +52,24 @@ func (search *search) Run(ctx context.Context) (any, error) {
6352 if search .Query == "" {
6453 return nil , nil
6554 }
66- fmt .Printf ("search for %q\n " , search .Query )
55+ fmt .Printf (" => Search for %q\n " , search .Query )
6756 return search .Articles (OptQuery (search .Query ), OptLimit (10 ))
6857}
6958
7059///////////////////////////////////////////////////////////////////////////////
7160// CATEGORY
7261
62+ type category struct {
63+ * Client `json:"-"`
64+ Category string `json:"category" enum:"business, entertainment, health, science, sports, technology" help:"business, entertainment, health, science, sports, technology" required:"true"`
65+ }
66+
67+ var _ llm.Tool = (* category )(nil )
68+
69+ var (
70+ categories = []string {"business" , "entertainment" , "health" , "science" , "sports" , "technology" }
71+ )
72+
7373func (category ) Name () string {
7474 return "news_headlines_category"
7575}
@@ -80,158 +80,9 @@ func (category) Description() string {
8080
8181func (category * category ) Run (ctx context.Context ) (any , error ) {
8282 if ! slices .Contains (categories , category .Category ) {
83- fmt .Printf ("search for %q\n " , category .Category )
83+ fmt .Printf (" => Search for %q\n " , category .Category )
8484 return category .Articles (OptQuery (category .Category ), OptLimit (10 ))
8585 }
86- fmt .Printf ("category for %q\n " , category .Category )
86+ fmt .Printf (" => Headlines for %q\n " , category .Category )
8787 return category .Headlines (OptCategory (category .Category ), OptLimit (10 ))
8888}
89-
90- /*
91- // Return all the agent tools for the weatherapi
92- func (c *Client) Tools() []agent.Tool {
93- return []agent.Tool{
94- &tool{
95- name: "current_headlines",
96- description: "Return the current news headlines",
97- run: c.agentCurrentHeadlines,
98- }, &tool{
99- name: "current_headlines_country",
100- description: "Return the current news headlines for a country",
101- run: c.agentCountryHeadlines,
102- params: []agent.ToolParameter{
103- {
104- Name: "countrycode",
105- Description: "The two-letter country code to return headlines for",
106- Required: true,
107- },
108- },
109- }, &tool{
110- name: "current_headlines_category",
111- description: "Return the current news headlines for a business, entertainment, health, science, sports or technology",
112- run: c.agentCategoryHeadlines,
113- params: []agent.ToolParameter{
114- {
115- Name: "category",
116- Description: "business, entertainment, health, science, sports, technology",
117- Required: true,
118- },
119- },
120- }, &tool{
121- name: "search_news",
122- description: "Return the news headlines with a search query",
123- run: c.agentSearchNews,
124- params: []agent.ToolParameter{
125- {
126- Name: "query",
127- Description: "A phrase used to search for news headlines",
128- Required: true,
129- },
130- },
131- },
132- }
133- }
134-
135- ///////////////////////////////////////////////////////////////////////////////
136- // PRIVATE METHODS - TOOL
137-
138- func (*tool) Provider() string {
139- return "newsapi"
140- }
141-
142- func (t *tool) Name() string {
143- return t.name
144- }
145-
146- func (t *tool) Description() string {
147- return t.description
148- }
149-
150- func (t *tool) Params() []agent.ToolParameter {
151- return t.params
152- }
153-
154- func (t *tool) Run(ctx context.Context, call *agent.ToolCall) (*agent.ToolResult, error) {
155- return t.run(ctx, call)
156- }
157-
158- ///////////////////////////////////////////////////////////////////////////////
159- // PRIVATE METHODS - TOOL
160-
161- // Return the current general headlines
162- func (c *Client) agentCurrentHeadlines(_ context.Context, call *agent.ToolCall) (*agent.ToolResult, error) {
163- response, err := c.Headlines(OptCategory("general"), OptLimit(5))
164- if err != nil {
165- return nil, err
166- }
167- return &agent.ToolResult{
168- Id: call.Id,
169- Result: map[string]any{
170- "type": "text",
171- "headlines": response,
172- },
173- }, nil
174- }
175-
176- // Return the headlines for a specific country
177- func (c *Client) agentCountryHeadlines(_ context.Context, call *agent.ToolCall) (*agent.ToolResult, error) {
178- country, err := call.String("countrycode")
179- if err != nil {
180- return nil, err
181- }
182- country = strings.ToLower(country)
183- response, err := c.Headlines(OptCountry(country), OptLimit(5))
184- if err != nil {
185- return nil, err
186- }
187- return &agent.ToolResult{
188- Id: call.Id,
189- Result: map[string]any{
190- "type": "text",
191- "country": country,
192- "headlines": response,
193- },
194- }, nil
195- }
196-
197- // Return the headlines for a specific category
198- func (c *Client) agentCategoryHeadlines(_ context.Context, call *agent.ToolCall) (*agent.ToolResult, error) {
199- category, err := call.String("category")
200- if err != nil {
201- return nil, err
202- }
203- category = strings.ToLower(category)
204- response, err := c.Headlines(OptCategory(category), OptLimit(5))
205- if err != nil {
206- return nil, err
207- }
208- return &agent.ToolResult{
209- Id: call.Id,
210- Result: map[string]any{
211- "type": "text",
212- "category": category,
213- "headlines": response,
214- },
215- }, nil
216- }
217-
218- // Return the headlines for a specific query
219- func (c *Client) agentSearchNews(_ context.Context, call *agent.ToolCall) (*agent.ToolResult, error) {
220- query, err := call.String("query")
221- if err != nil {
222- return nil, err
223- }
224- response, err := c.Articles(OptQuery(query), OptLimit(5))
225- if err != nil {
226- return nil, err
227- }
228- return &agent.ToolResult{
229- Id: call.Id,
230- Result: map[string]any{
231- "type": "text",
232- "query": query,
233- "headlines": response,
234- },
235- }, nil
236- }
237- */
0 commit comments