feat(github): add action to read github project's README (#64)
Signed-off-by: mudler <mudler@localai.io>
This commit is contained in:
committed by
GitHub
parent
0e240077ab
commit
1e5b3f501f
@@ -24,6 +24,7 @@ const (
|
|||||||
ActionGithubRepositoryCreateOrUpdate = "github-repository-create-or-update-content"
|
ActionGithubRepositoryCreateOrUpdate = "github-repository-create-or-update-content"
|
||||||
ActionGithubIssueReader = "github-issue-reader"
|
ActionGithubIssueReader = "github-issue-reader"
|
||||||
ActionGithubIssueCommenter = "github-issue-commenter"
|
ActionGithubIssueCommenter = "github-issue-commenter"
|
||||||
|
ActionGithubREADME = "github-readme"
|
||||||
ActionScraper = "scraper"
|
ActionScraper = "scraper"
|
||||||
ActionWikipedia = "wikipedia"
|
ActionWikipedia = "wikipedia"
|
||||||
ActionBrowse = "browse"
|
ActionBrowse = "browse"
|
||||||
@@ -45,6 +46,7 @@ var AvailableActions = []string{
|
|||||||
ActionGithubRepositoryCreateOrUpdate,
|
ActionGithubRepositoryCreateOrUpdate,
|
||||||
ActionGithubIssueReader,
|
ActionGithubIssueReader,
|
||||||
ActionGithubIssueCommenter,
|
ActionGithubIssueCommenter,
|
||||||
|
ActionGithubREADME,
|
||||||
ActionScraper,
|
ActionScraper,
|
||||||
ActionBrowse,
|
ActionBrowse,
|
||||||
ActionWikipedia,
|
ActionWikipedia,
|
||||||
@@ -94,6 +96,8 @@ func Actions(a *state.AgentConfig) func(ctx context.Context, pool *state.AgentPo
|
|||||||
allActions = append(allActions, actions.NewGithubRepositoryGetContent(ctx, config))
|
allActions = append(allActions, actions.NewGithubRepositoryGetContent(ctx, config))
|
||||||
case ActionGithubRepositoryCreateOrUpdate:
|
case ActionGithubRepositoryCreateOrUpdate:
|
||||||
allActions = append(allActions, actions.NewGithubRepositoryCreateOrUpdateContent(ctx, config))
|
allActions = append(allActions, actions.NewGithubRepositoryCreateOrUpdateContent(ctx, config))
|
||||||
|
case ActionGithubREADME:
|
||||||
|
allActions = append(allActions, actions.NewGithubRepositoryREADME(ctx, config))
|
||||||
case ActionScraper:
|
case ActionScraper:
|
||||||
allActions = append(allActions, actions.NewScraper(config))
|
allActions = append(allActions, actions.NewScraper(config))
|
||||||
case ActionWikipedia:
|
case ActionWikipedia:
|
||||||
|
|||||||
75
services/actions/githubrepositoryreadme.go
Normal file
75
services/actions/githubrepositoryreadme.go
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
package actions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/google/go-github/v69/github"
|
||||||
|
"github.com/mudler/LocalAgent/core/action"
|
||||||
|
"github.com/sashabaranov/go-openai/jsonschema"
|
||||||
|
)
|
||||||
|
|
||||||
|
type GithubRepositoryREADME struct {
|
||||||
|
token, customActionName string
|
||||||
|
context context.Context
|
||||||
|
client *github.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewGithubRepositoryREADME(ctx context.Context, config map[string]string) *GithubRepositoryREADME {
|
||||||
|
client := github.NewClient(nil).WithAuthToken(config["token"])
|
||||||
|
|
||||||
|
return &GithubRepositoryREADME{
|
||||||
|
client: client,
|
||||||
|
token: config["token"],
|
||||||
|
customActionName: config["customActionName"],
|
||||||
|
context: ctx,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GithubRepositoryREADME) Run(ctx context.Context, params action.ActionParams) (action.ActionResult, error) {
|
||||||
|
result := struct {
|
||||||
|
Repository string `json:"repository"`
|
||||||
|
Owner string `json:"owner"`
|
||||||
|
}{}
|
||||||
|
err := params.Unmarshal(&result)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("error: %v", err)
|
||||||
|
|
||||||
|
return action.ActionResult{}, err
|
||||||
|
}
|
||||||
|
fileContent, _, err := g.client.Repositories.GetReadme(g.context, result.Owner, result.Repository, &github.RepositoryContentGetOptions{})
|
||||||
|
if err != nil {
|
||||||
|
resultString := fmt.Sprintf("Error getting content : %v", err)
|
||||||
|
return action.ActionResult{Result: resultString}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var content string
|
||||||
|
if fileContent.Content != nil {
|
||||||
|
content = *fileContent.Content
|
||||||
|
}
|
||||||
|
|
||||||
|
return action.ActionResult{Result: content}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GithubRepositoryREADME) Definition() action.ActionDefinition {
|
||||||
|
actionName := "github_readme"
|
||||||
|
actionDescription := "Get the README file of a GitHub repository to have a basic understanding of the project."
|
||||||
|
if g.customActionName != "" {
|
||||||
|
actionName = g.customActionName
|
||||||
|
}
|
||||||
|
return action.ActionDefinition{
|
||||||
|
Name: action.ActionDefinitionName(actionName),
|
||||||
|
Description: actionDescription,
|
||||||
|
Properties: map[string]jsonschema.Definition{
|
||||||
|
"repository": {
|
||||||
|
Type: jsonschema.String,
|
||||||
|
Description: "The repository to search in",
|
||||||
|
},
|
||||||
|
"owner": {
|
||||||
|
Type: jsonschema.String,
|
||||||
|
Description: "The owner of the repository",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Required: []string{"repository", "owner"},
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user