|
| 1 | +package huggingface |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/1Password/shell-plugins/sdk" |
| 7 | + "github.com/1Password/shell-plugins/sdk/importer" |
| 8 | + "github.com/1Password/shell-plugins/sdk/provision" |
| 9 | + "github.com/1Password/shell-plugins/sdk/schema" |
| 10 | + "github.com/1Password/shell-plugins/sdk/schema/credname" |
| 11 | + "github.com/1Password/shell-plugins/sdk/schema/fieldname" |
| 12 | +) |
| 13 | + |
| 14 | +func UserAccessToken() schema.CredentialType { |
| 15 | + return schema.CredentialType{ |
| 16 | + Name: credname.APIToken, |
| 17 | + DocsURL: sdk.URL("https://huggingface.co/docs/hub/security-tokens"), |
| 18 | + ManagementURL: sdk.URL("https://huggingface.co/settings/tokens"), |
| 19 | + Fields: []schema.CredentialField{ |
| 20 | + { |
| 21 | + Name: fieldname.UserAccessToken, |
| 22 | + MarkdownDescription: "Token used to authenticate to HuggingFace.", |
| 23 | + Secret: true, |
| 24 | + Composition: &schema.ValueComposition{ |
| 25 | + Length: 37, |
| 26 | + Prefix: "hf_", |
| 27 | + Charset: schema.Charset{ |
| 28 | + Uppercase: true, |
| 29 | + Lowercase: true, |
| 30 | + Symbols: true, |
| 31 | + }, |
| 32 | + }, |
| 33 | + }, |
| 34 | + { |
| 35 | + Name: fieldname.Endpoint, |
| 36 | + MarkdownDescription: "Endpoint used to connect to HuggingFace CLI", |
| 37 | + Optional: true, |
| 38 | + Secret: false, |
| 39 | + Composition: &schema.ValueComposition{ |
| 40 | + Charset: schema.Charset{ |
| 41 | + Uppercase: true, |
| 42 | + Lowercase: true, |
| 43 | + Digits: true, |
| 44 | + Symbols: true, |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + { |
| 49 | + Name: fieldname.APIUrl, |
| 50 | + MarkdownDescription: "HF Inference Endpoint used to connect to HuggingFace CLI", |
| 51 | + Optional: true, |
| 52 | + Secret: false, |
| 53 | + Composition: &schema.ValueComposition{ |
| 54 | + Charset: schema.Charset{ |
| 55 | + Uppercase: true, |
| 56 | + Lowercase: true, |
| 57 | + Digits: true, |
| 58 | + Symbols: true, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping), |
| 64 | + Importer: importer.TryAll( |
| 65 | + importer.TryEnvVarPair(defaultEnvVarMapping), |
| 66 | + TryHuggingFaceTokenFile(), |
| 67 | + )} |
| 68 | +} |
| 69 | + |
| 70 | +var defaultEnvVarMapping = map[string]sdk.FieldName{ |
| 71 | + "HUGGING_FACE_HUB_TOKEN": fieldname.UserAccessToken, |
| 72 | + "HF_ENDPOINT": fieldname.Endpoint, |
| 73 | + "HF_INFERENCE_ENDPOINT": fieldname.APIUrl, |
| 74 | +} |
| 75 | + |
| 76 | +func TryHuggingFaceTokenFile() sdk.Importer { |
| 77 | + return importer.TryFile("~/.cache/huggingface/token", func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) { |
| 78 | + fileData := string(contents) |
| 79 | + |
| 80 | + out.AddCandidate(sdk.ImportCandidate{ |
| 81 | + Fields: map[sdk.FieldName]string{ |
| 82 | + fieldname.UserAccessToken: fileData, |
| 83 | + }, |
| 84 | + }) |
| 85 | + }) |
| 86 | + |
| 87 | +} |
0 commit comments