-
Notifications
You must be signed in to change notification settings - Fork 238
Added new Cohere Cli plugin #334
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package cohere | ||
|
|
||
| import ( | ||
| "github.com/1Password/shell-plugins/sdk" | ||
| "github.com/1Password/shell-plugins/sdk/needsauth" | ||
| "github.com/1Password/shell-plugins/sdk/schema" | ||
| "github.com/1Password/shell-plugins/sdk/schema/credname" | ||
| ) | ||
|
|
||
| func CohereCLI() schema.Executable { | ||
| return schema.Executable{ | ||
| Name: "Cohere CLI", | ||
| Runs: []string{"co"}, | ||
| DocsURL: sdk.URL("https://docs.cohere.com/reference/command"), | ||
| NeedsAuth: needsauth.IfAll( | ||
| needsauth.NotForHelpOrVersion(), | ||
| needsauth.NotWithoutArgs(), | ||
| needsauth.NotWhenContainsArgs("config"), | ||
| needsauth.NotWhenContainsArgs("ping"), | ||
| ), | ||
| Uses: []schema.CredentialUsage{ | ||
| { | ||
| Name: credname.Credentials, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| package cohere | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
|
|
||
| "github.com/1Password/shell-plugins/sdk" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's sort imports on this file to make the linter pass. |
||
| "github.com/1Password/shell-plugins/sdk/importer" | ||
| "github.com/1Password/shell-plugins/sdk/provision" | ||
| "github.com/1Password/shell-plugins/sdk/schema" | ||
| "github.com/1Password/shell-plugins/sdk/schema/credname" | ||
| "github.com/1Password/shell-plugins/sdk/schema/fieldname" | ||
| ) | ||
|
|
||
| func Credentials() schema.CredentialType { | ||
| return schema.CredentialType{ | ||
| Name: credname.Credentials, | ||
| DocsURL: sdk.URL("https://docs.cohere.com/reference/config"), | ||
| ManagementURL: sdk.URL("https://dashboard.cohere.ai/"), | ||
| Fields: []schema.CredentialField{ | ||
| { | ||
| Name: fieldname.JWT, | ||
|
Check failure on line 22 in plugins/cohere/credentials.go
|
||
| MarkdownDescription: "JWT used to authenticate to Cohere stored in disk.", | ||
| Secret: true, | ||
| }, | ||
| { | ||
| Name: fieldname.Email, | ||
| MarkdownDescription: "Email used to authenticate to Cohere.", | ||
| Optional: true, | ||
| }, | ||
| { | ||
| Name: fieldname.URL, | ||
| MarkdownDescription: "URL of the operator server", | ||
| Optional: true, | ||
| }, | ||
AndyTitu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| DefaultProvisioner: provision.TempFile( | ||
| cohereJSON, | ||
| provision.AtFixedPath("~/.command/config")), | ||
| Importer: importer.TryAll( | ||
| TryCohereConfigFile(), | ||
| )} | ||
| } | ||
|
|
||
| func TryCohereConfigFile() sdk.Importer { | ||
| return importer.TryFile("~/.command/config", func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) { | ||
| var config Config | ||
| if err := contents.ToJSON(&config); err != nil { | ||
| out.AddError(err) | ||
| return | ||
| } | ||
|
|
||
| if config.Contexts[config.CurrentURL].JWT == "" { | ||
| return | ||
| } | ||
|
|
||
| out.AddCandidate(sdk.ImportCandidate{ | ||
| Fields: map[sdk.FieldName]string{ | ||
| fieldname.URL: config.CurrentURL, | ||
| fieldname.JWT: config.Contexts[config.CurrentURL].JWT, | ||
|
Check failure on line 60 in plugins/cohere/credentials.go
|
||
| fieldname.Email: config.Contexts[config.CurrentURL].Email, | ||
| }, | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| func cohereJSON(in sdk.ProvisionInput) ([]byte, error) { | ||
| var currentURL, jwt, email string | ||
|
|
||
| if value, ok := in.ItemFields[fieldname.JWT]; ok { | ||
|
Check failure on line 70 in plugins/cohere/credentials.go
|
||
| jwt = value | ||
| } else { | ||
| return nil, nil | ||
| } | ||
|
|
||
| if value, ok := in.ItemFields[fieldname.URL]; ok { | ||
| currentURL = value | ||
| } | ||
|
|
||
| if value, ok := in.ItemFields[fieldname.Email]; ok { | ||
| email = value | ||
| } | ||
|
|
||
| data := Config{ | ||
| CurrentURL: currentURL, | ||
| Contexts: map[string]APISettings{ | ||
| currentURL: { | ||
| JWT: jwt, | ||
| Email: email, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| jsonData, err := json.Marshal(data) | ||
| if err != nil { | ||
| return nil, err | ||
|
|
||
| } | ||
| return []byte(jsonData), nil | ||
| } | ||
|
|
||
| type Config struct { | ||
| CurrentURL string `json:"CurrentURL"` | ||
| Contexts map[string]APISettings `json:"Contexts"` | ||
| } | ||
|
|
||
| type APISettings struct { | ||
| JWT string `json:"JWT"` | ||
| Email string `json:"Email"` | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package cohere | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/1Password/shell-plugins/sdk" | ||
| "github.com/1Password/shell-plugins/sdk/plugintest" | ||
| "github.com/1Password/shell-plugins/sdk/schema/fieldname" | ||
| ) | ||
|
|
||
| func TestCredentialsProvisioner(t *testing.T) { | ||
| plugintest.TestProvisioner(t, Credentials().DefaultProvisioner, map[string]plugintest.ProvisionCase{ | ||
| "temp file": { | ||
| ItemFields: map[sdk.FieldName]string{ | ||
| fieldname.JWT: "PLEzI1N--EXAMPLE", | ||
|
Check failure on line 15 in plugins/cohere/credentials_test.go
|
||
| fieldname.URL: "https://api.os.cohere.ai", | ||
| fieldname.Email: "[email protected]", | ||
| }, | ||
| ExpectedOutput: sdk.ProvisionOutput{ | ||
| Files: map[string]sdk.OutputFile{ | ||
| "~/.command/config": { | ||
| Contents: []byte(plugintest.LoadFixture(t, "config")), | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| func TestCredentialsImporter(t *testing.T) { | ||
| plugintest.TestImporter(t, Credentials().Importer, map[string]plugintest.ImportCase{ | ||
|
|
||
| "config file": { | ||
| Files: map[string]string{ | ||
| "~/.command/config": plugintest.LoadFixture(t, "config"), | ||
| }, | ||
| ExpectedCandidates: []sdk.ImportCandidate{ | ||
| { | ||
| Fields: map[sdk.FieldName]string{ | ||
| fieldname.JWT: "PLEzI1N--EXAMPLE", | ||
|
Check failure on line 40 in plugins/cohere/credentials_test.go
|
||
| fieldname.URL: "https://api.os.cohere.ai", | ||
| fieldname.Email: "[email protected]", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package cohere | ||
|
|
||
| import ( | ||
| "github.com/1Password/shell-plugins/sdk" | ||
| "github.com/1Password/shell-plugins/sdk/schema" | ||
| ) | ||
|
|
||
| func New() schema.Plugin { | ||
| return schema.Plugin{ | ||
| Name: "cohere", | ||
| Platform: schema.PlatformInfo{ | ||
| Name: "Cohere", | ||
| Homepage: sdk.URL("https://cohere.com"), | ||
| }, | ||
| Credentials: []schema.CredentialType{ | ||
| Credentials(), | ||
| }, | ||
| Executables: []schema.Executable{ | ||
| CohereCLI(), | ||
| }, | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {"CurrentURL":"https://api.os.cohere.ai","Contexts":{"https://api.os.cohere.ai":{"JWT":"PLEzI1N--EXAMPLE","Email":"[email protected]"}}} |
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 think there may be other commands, such as
co auth login, that do not require authentication - could you have a look at what all of those may be?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.
this is not yet resolved