-
Notifications
You must be signed in to change notification settings - Fork 68
Add Prompt Library support #1168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
trly
wants to merge
5
commits into
main
Choose a base branch
from
trly/prompt_library
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0f22345
feat: introduce prompt library management
trly 0ba4140
feat: enhance prompt management with column selection and JSON output
trly 0e8aa2e
feat(prompts): set current user as default owner for prompt creation
trly 26a34cc
fix(prompts): align prompt tags command descriptions
trly a478aba
Merge branch 'main' into trly/prompt_library
trly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
var promptsCommands commander | ||
|
||
func init() { | ||
usage := `'src prompts' is a tool that manages prompt library prompts and tags in a Sourcegraph instance. | ||
|
||
Usage: | ||
|
||
src prompts command [command options] | ||
|
||
The commands are: | ||
|
||
list lists prompts | ||
get get a prompt by ID | ||
create create a prompt | ||
update update a prompt | ||
delete delete a prompt | ||
export export prompts to a JSON file | ||
import import prompts from a JSON file | ||
tags manage prompt tags (use "src prompts tags [command] -h" for more info) | ||
|
||
Use "src prompts [command] -h" for more information about a command. | ||
` | ||
|
||
flagSet := flag.NewFlagSet("prompts", flag.ExitOnError) | ||
handler := func(args []string) error { | ||
promptsCommands.run(flagSet, "src prompts", usage, args) | ||
return nil | ||
} | ||
|
||
// Register the command. | ||
commands = append(commands, &command{ | ||
flagSet: flagSet, | ||
handler: handler, | ||
usageFunc: func() { | ||
fmt.Println(usage) | ||
}, | ||
}) | ||
} | ||
|
||
const promptFragment = ` | ||
fragment PromptFields on Prompt { | ||
id | ||
name | ||
description | ||
definition { | ||
text | ||
} | ||
draft | ||
visibility | ||
autoSubmit | ||
mode | ||
recommended | ||
tags(first: 100) { | ||
nodes { | ||
id | ||
name | ||
} | ||
} | ||
} | ||
` | ||
|
||
const promptTagFragment = ` | ||
fragment PromptTagFields on PromptTag { | ||
id | ||
name | ||
} | ||
` | ||
|
||
type Prompt struct { | ||
ID string `json:"id"` | ||
Name string `json:"name"` | ||
Description string `json:"description"` | ||
Definition Definition `json:"definition"` | ||
Draft bool `json:"draft"` | ||
Visibility string `json:"visibility"` | ||
AutoSubmit bool `json:"autoSubmit"` | ||
Mode string `json:"mode"` | ||
Recommended bool `json:"recommended"` | ||
Tags PromptTags `json:"tags"` | ||
} | ||
|
||
type Definition struct { | ||
Text string `json:"text"` | ||
} | ||
|
||
type PromptTags struct { | ||
Nodes []PromptTag `json:"nodes"` | ||
} | ||
|
||
type PromptTag struct { | ||
ID string `json:"id"` | ||
Name string `json:"name"` | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/sourcegraph/sourcegraph/lib/errors" | ||
|
||
"github.com/sourcegraph/src-cli/internal/api" | ||
) | ||
|
||
func init() { | ||
usage := ` | ||
Examples: | ||
|
||
Create a prompt "Go Error Handling": | ||
|
||
$ src prompts create -name="Go Error Handling" \ | ||
-description="Best practices for Go error handling" \ | ||
-content="When handling errors in Go..." \ | ||
-owner=<owner-id> | ||
` | ||
|
||
flagSet := flag.NewFlagSet("create", flag.ExitOnError) | ||
usageFunc := func() { | ||
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src prompts %s':\n", flagSet.Name()) | ||
flagSet.PrintDefaults() | ||
fmt.Println(usage) | ||
} | ||
var ( | ||
nameFlag = flagSet.String("name", "", "The prompt name") | ||
descriptionFlag = flagSet.String("description", "", "Description of the prompt") | ||
contentFlag = flagSet.String("content", "", "The prompt template text content") | ||
ownerFlag = flagSet.String("owner", "", "The ID of the owner (user or organization). Defaults to current user if not specified.") | ||
tagsFlag = flagSet.String("tags", "", "Comma-separated list of tag IDs") | ||
draftFlag = flagSet.Bool("draft", false, "Whether the prompt is a draft") | ||
visibilityFlag = flagSet.String("visibility", "PUBLIC", "Visibility of the prompt (PUBLIC or SECRET)") | ||
autoSubmitFlag = flagSet.Bool("auto-submit", false, "Whether the prompt should be automatically executed in one click") | ||
modeFlag = flagSet.String("mode", "CHAT", "Mode to execute prompt (CHAT, EDIT, or INSERT)") | ||
recommendedFlag = flagSet.Bool("recommended", false, "Whether the prompt is recommended") | ||
apiFlags = api.NewFlags(flagSet) | ||
) | ||
|
||
handler := func(args []string) error { | ||
if err := flagSet.Parse(args); err != nil { | ||
return err | ||
} | ||
|
||
if *nameFlag == "" { | ||
return errors.New("provide a name for the prompt") | ||
} | ||
if *descriptionFlag == "" { | ||
return errors.New("provide a description for the prompt") | ||
} | ||
if *contentFlag == "" { | ||
return errors.New("provide content for the prompt") | ||
} | ||
client := cfg.apiClient(apiFlags, flagSet.Output()) | ||
|
||
// Use current user as default owner if not specified | ||
ownerID := *ownerFlag | ||
if ownerID == "" { | ||
var err error | ||
ownerID, err = getViewerUserID(context.Background(), client) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to get current user ID") | ||
} | ||
} | ||
|
||
// Validate mode | ||
validModes := map[string]bool{"CHAT": true, "EDIT": true, "INSERT": true} | ||
mode := strings.ToUpper(*modeFlag) | ||
if !validModes[mode] { | ||
return errors.New("mode must be one of: CHAT, EDIT, or INSERT") | ||
} | ||
|
||
// Validate visibility | ||
validVisibility := map[string]bool{"PUBLIC": true, "SECRET": true} | ||
visibility := strings.ToUpper(*visibilityFlag) | ||
if !validVisibility[visibility] { | ||
return errors.New("visibility must be either PUBLIC or SECRET") | ||
} | ||
|
||
// Parse tags into array | ||
var tagIDs []string | ||
if *tagsFlag != "" { | ||
tagIDs = strings.Split(*tagsFlag, ",") | ||
} | ||
|
||
query := `mutation CreatePrompt( | ||
$input: PromptInput! | ||
) { | ||
createPrompt(input: $input) { | ||
...PromptFields | ||
} | ||
} | ||
` + promptFragment | ||
|
||
input := map[string]interface{}{ | ||
"name": *nameFlag, | ||
"description": *descriptionFlag, | ||
"definitionText": *contentFlag, | ||
"owner": ownerID, | ||
"draft": *draftFlag, | ||
"visibility": visibility, | ||
"autoSubmit": *autoSubmitFlag, | ||
"mode": mode, | ||
"recommended": *recommendedFlag, | ||
} | ||
|
||
if len(tagIDs) > 0 { | ||
input["tags"] = tagIDs | ||
} | ||
|
||
var result struct { | ||
CreatePrompt Prompt | ||
} | ||
if ok, err := client.NewRequest(query, map[string]interface{}{ | ||
"input": input, | ||
}).Do(context.Background(), &result); err != nil || !ok { | ||
return err | ||
} | ||
|
||
fmt.Printf("Prompt created: %s\n", result.CreatePrompt.ID) | ||
return nil | ||
} | ||
|
||
// Register the command. | ||
promptsCommands = append(promptsCommands, &command{ | ||
flagSet: flagSet, | ||
handler: handler, | ||
usageFunc: usageFunc, | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
|
||
"github.com/sourcegraph/sourcegraph/lib/errors" | ||
|
||
"github.com/sourcegraph/src-cli/internal/api" | ||
) | ||
|
||
func init() { | ||
usage := ` | ||
Examples: | ||
|
||
Delete a prompt by ID: | ||
|
||
$ src prompts delete <prompt-id> | ||
|
||
` | ||
|
||
flagSet := flag.NewFlagSet("delete", flag.ExitOnError) | ||
usageFunc := func() { | ||
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src prompts %s':\n", flagSet.Name()) | ||
flagSet.PrintDefaults() | ||
fmt.Println(usage) | ||
} | ||
var ( | ||
apiFlags = api.NewFlags(flagSet) | ||
) | ||
|
||
handler := func(args []string) error { | ||
if err := flagSet.Parse(args); err != nil { | ||
return err | ||
} | ||
|
||
// Check for prompt ID as positional argument | ||
if len(flagSet.Args()) != 1 { | ||
return errors.New("provide exactly one prompt ID as an argument") | ||
} | ||
promptID := flagSet.Arg(0) | ||
|
||
client := cfg.apiClient(apiFlags, flagSet.Output()) | ||
|
||
query := `mutation DeletePrompt($id: ID!) { | ||
deletePrompt(id: $id) { | ||
alwaysNil | ||
} | ||
} | ||
` | ||
|
||
var result struct { | ||
DeletePrompt struct { | ||
AlwaysNil interface{} `json:"alwaysNil"` | ||
} | ||
} | ||
|
||
if ok, err := client.NewRequest(query, map[string]interface{}{ | ||
"id": promptID, | ||
}).Do(context.Background(), &result); err != nil || !ok { | ||
return err | ||
} | ||
|
||
fmt.Println("Prompt deleted successfully.") | ||
return nil | ||
} | ||
|
||
// Register the command. | ||
promptsCommands = append(promptsCommands, &command{ | ||
flagSet: flagSet, | ||
handler: handler, | ||
usageFunc: usageFunc, | ||
}) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about making the
100
configurable via a flag?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've been back and forth on that a few times. I really don't know how well the CLI is going to display a large number of tags anyway.
Maybe add a flag but set the default lower (10)?