Skip to content

Commit 6b6dd33

Browse files
authored
Merge pull request #1 from tuananh/remove-dummy-param
fix: remove dummy param, add num_stories param, default to 10
2 parents c922c21 + 439bec8 commit 6b6dd33

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

main.go

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"encoding/json"
55
"fmt"
6-
"strings"
76

87
pdk "github.com/extism/go-pdk"
98
"github.com/tidwall/gjson"
@@ -18,31 +17,48 @@ type HNStory struct {
1817
}
1918

2019
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)
2235
}
2336

2437
func Describe() (ListToolsResult, error) {
2538
return ListToolsResult{
2639
Tools: []ToolDescription{
2740
{
2841
Name: "hackernews",
29-
Description: "Get top 5 stories from Hacker News",
42+
Description: "Get top stories from Hacker News (max 100)",
3043
InputSchema: map[string]interface{}{
3144
"type": "object",
3245
"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,
3652
},
3753
},
38-
"required": []string{"random_string"},
54+
"required": []string{"num_stories"},
3955
},
4056
},
4157
},
4258
}, nil
4359
}
4460

45-
func fetchHackerNews() (CallToolResult, error) {
61+
func fetchHackerNews(numStories int) (CallToolResult, error) {
4662
// Fetch top stories
4763
req := pdk.NewHTTPRequest(pdk.MethodGet, "https://hacker-news.firebaseio.com/v0/topstories.json")
4864
resp := req.Send()
@@ -56,29 +72,23 @@ func fetchHackerNews() (CallToolResult, error) {
5672
return CallToolResult{}, fmt.Errorf("failed to parse story IDs: %v", err)
5773
}
5874

59-
// Get top 5 stories
75+
// Get requested number of stories
6076
var stories []HNStory
61-
for i := 0; i < 5 && i < len(storyIDs); i++ {
77+
for i := 0; i < numStories && i < len(storyIDs); i++ {
6278
story, err := fetchStory(storyIDs[i])
6379
if err != nil {
6480
return CallToolResult{}, fmt.Errorf("failed to fetch story %d: %v", storyIDs[i], err)
6581
}
6682
stories = append(stories, story)
6783
}
6884

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)
7989
}
90+
text := string(jsonBytes)
8091

81-
text := output.String()
8292
return CallToolResult{
8393
Content: []Content{
8494
{

0 commit comments

Comments
 (0)