@@ -2,6 +2,8 @@ package newsapi
22
33import (
44 "context"
5+ "fmt"
6+ "slices"
57
68 // Packages
79 llm "github.com/mutablelogic/go-llm"
@@ -11,32 +13,78 @@ import (
1113// TYPES
1214
1315type headlines struct {
14- * Client `json:"-"`
15- CountryCode string `json:"country_code,omitempty" help:"The two-letter countrycode to return headlines for"`
16+ * 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"`
1628}
1729
1830var _ llm.Tool = (* headlines )(nil )
1931
32+ var (
33+ categories = []string {"business" , "entertainment" , "health" , "science" , "sports" , "technology" }
34+ )
35+
2036///////////////////////////////////////////////////////////////////////////////
2137// HEADLINES
2238
2339func (headlines ) Name () string {
24- return "current_headlines "
40+ return "news_headlines "
2541}
2642
2743func (headlines ) Description () string {
28- return "Return the current news headlines, optionally for a specific country "
44+ return "Return the current global news headlines"
2945}
3046
3147func (headlines * headlines ) Run (ctx context.Context ) (any , error ) {
32- response , err := headlines .Headlines (OptCategory ("general" ), OptLimit (5 ))
33- if err != nil {
34- return nil , err
48+ return headlines .Headlines (OptCategory ("general" ), OptLimit (10 ))
49+ }
50+
51+ ///////////////////////////////////////////////////////////////////////////////
52+ // SEARCH
53+
54+ func (search ) Name () string {
55+ return "news_search"
56+ }
57+
58+ func (search ) Description () string {
59+ return "Search the news archive with a search query"
60+ }
61+
62+ func (search * search ) Run (ctx context.Context ) (any , error ) {
63+ if search .Query == "" {
64+ return nil , nil
3565 }
36- return map [string ]any {
37- "type" : "text" ,
38- "headlines" : response ,
39- }, nil
66+ fmt .Printf ("search for %q\n " , search .Query )
67+ return search .Articles (OptQuery (search .Query ), OptLimit (10 ))
68+ }
69+
70+ ///////////////////////////////////////////////////////////////////////////////
71+ // CATEGORY
72+
73+ func (category ) Name () string {
74+ return "news_headlines_category"
75+ }
76+
77+ func (category ) Description () string {
78+ return "Return the news headlines for a specific category"
79+ }
80+
81+ func (category * category ) Run (ctx context.Context ) (any , error ) {
82+ if ! slices .Contains (categories , category .Category ) {
83+ fmt .Printf ("search for %q\n " , category .Category )
84+ return category .Articles (OptQuery (category .Category ), OptLimit (10 ))
85+ }
86+ fmt .Printf ("category for %q\n " , category .Category )
87+ return category .Headlines (OptCategory (category .Category ), OptLimit (10 ))
4088}
4189
4290/*
0 commit comments