@@ -3,7 +3,6 @@ package main
3
3
import (
4
4
"encoding/json"
5
5
"fmt"
6
- "strings"
7
6
8
7
pdk "github.com/extism/go-pdk"
9
8
"github.com/tidwall/gjson"
@@ -18,31 +17,48 @@ type HNStory struct {
18
17
}
19
18
20
19
func Call (input CallToolRequest ) (CallToolResult , error ) {
21
- return fetchHackerNews ()
20
+ numStories := 10 // default value
21
+ if args , ok := input .Params .Arguments .(map [string ]interface {}); ok {
22
+ if n , exists := args ["num_stories" ]; exists {
23
+ if val , ok := n .(float64 ); ok {
24
+ numStories = int (val )
25
+ if numStories > 100 {
26
+ numStories = 100
27
+ }
28
+ if numStories < 1 {
29
+ numStories = 10
30
+ }
31
+ }
32
+ }
33
+ }
34
+ return fetchHackerNews (numStories )
22
35
}
23
36
24
37
func Describe () (ListToolsResult , error ) {
25
38
return ListToolsResult {
26
39
Tools : []ToolDescription {
27
40
{
28
41
Name : "hackernews" ,
29
- Description : "Get top 5 stories from Hacker News" ,
42
+ Description : "Get top stories from Hacker News (max 100) " ,
30
43
InputSchema : map [string ]interface {}{
31
44
"type" : "object" ,
32
45
"properties" : map [string ]interface {}{
33
- "random_string" : map [string ]interface {}{
34
- "description" : "Dummy parameter for no-parameter tools" ,
35
- "type" : "string" ,
46
+ "num_stories" : map [string ]interface {}{
47
+ "description" : "Number of top stories to fetch (max 100, defaults to 10)" ,
48
+ "type" : "integer" ,
49
+ "minimum" : 1 ,
50
+ "maximum" : 100 ,
51
+ "default" : 10 ,
36
52
},
37
53
},
38
- "required" : []string {"random_string " },
54
+ "required" : []string {"num_stories " },
39
55
},
40
56
},
41
57
},
42
58
}, nil
43
59
}
44
60
45
- func fetchHackerNews () (CallToolResult , error ) {
61
+ func fetchHackerNews (numStories int ) (CallToolResult , error ) {
46
62
// Fetch top stories
47
63
req := pdk .NewHTTPRequest (pdk .MethodGet , "https://hacker-news.firebaseio.com/v0/topstories.json" )
48
64
resp := req .Send ()
@@ -56,29 +72,23 @@ func fetchHackerNews() (CallToolResult, error) {
56
72
return CallToolResult {}, fmt .Errorf ("failed to parse story IDs: %v" , err )
57
73
}
58
74
59
- // Get top 5 stories
75
+ // Get requested number of stories
60
76
var stories []HNStory
61
- for i := 0 ; i < 5 && i < len (storyIDs ); i ++ {
77
+ for i := 0 ; i < numStories && i < len (storyIDs ); i ++ {
62
78
story , err := fetchStory (storyIDs [i ])
63
79
if err != nil {
64
80
return CallToolResult {}, fmt .Errorf ("failed to fetch story %d: %v" , storyIDs [i ], err )
65
81
}
66
82
stories = append (stories , story )
67
83
}
68
84
69
- // Format output
70
- var output strings.Builder
71
- output .WriteString ("# Top 5 Hacker News Stories\n \n " )
72
- for i , story := range stories {
73
- output .WriteString (fmt .Sprintf ("## %d. %s\n " , i + 1 , story .Title ))
74
- output .WriteString (fmt .Sprintf ("Score: %d | Author: %s\n " , story .Score , story .By ))
75
- if story .URL != "" {
76
- output .WriteString (fmt .Sprintf ("URL: %s\n " , story .URL ))
77
- }
78
- output .WriteString ("\n " )
85
+ // Convert stories to JSON string
86
+ jsonBytes , err := json .Marshal (stories )
87
+ if err != nil {
88
+ return CallToolResult {}, fmt .Errorf ("failed to marshal stories: %v" , err )
79
89
}
90
+ text := string (jsonBytes )
80
91
81
- text := output .String ()
82
92
return CallToolResult {
83
93
Content : []Content {
84
94
{
0 commit comments