From ec648c32855ea327952aba397aaef98cfbfb9779 Mon Sep 17 00:00:00 2001 From: Nate Date: Wed, 19 Feb 2025 17:29:09 -0800 Subject: [PATCH 1/4] secret resolution update --- packages/config-yaml/src/interfaces/index.ts | 140 ++++++++++++++++--- packages/config-yaml/src/interfaces/slugs.ts | 10 ++ 2 files changed, 127 insertions(+), 23 deletions(-) diff --git a/packages/config-yaml/src/interfaces/index.ts b/packages/config-yaml/src/interfaces/index.ts index f8db8b1d4..caea06e53 100644 --- a/packages/config-yaml/src/interfaces/index.ts +++ b/packages/config-yaml/src/interfaces/index.ts @@ -1,5 +1,5 @@ import { SecretLocation, SecretResult, SecretType } from "./SecretResult.js"; -import { FQSN, FullSlug } from "./slugs.js"; +import { FQSN, FullSlug, PackageSlug, packageSlugsEqual } from "./slugs.js"; /** * A registry stores the content of packages @@ -27,34 +27,128 @@ export interface PlatformSecretStore { ): Promise; } +export function getLocationsToLook( + assistantSlug: PackageSlug, + blockSlug: PackageSlug | undefined, + currentUserSlug: string, + secretName: string, +): SecretLocation[] { + const locationsToLook: SecretLocation[] = [ + // Block + ...(blockSlug + ? [ + { + secretType: SecretType.Package as const, + packageSlug: blockSlug, + secretName, + }, + ] + : []), + // Assistant + { + secretType: SecretType.Package as const, + packageSlug: assistantSlug, + secretName, + }, + // Organization that owns assistant (org secrets can only be used in assistants owned by that org) + { + secretType: SecretType.Organization as const, + orgSlug: assistantSlug.ownerSlug, + secretName, + }, + // Then user + { + secretType: SecretType.User as const, + userSlug: currentUserSlug, + secretName, + }, + ]; + return locationsToLook; +} + +export async function listAvailableSecrets( + userSecretNames: string[], + orgSecretNames: string[], + assistantSecretNames: string[], + blockSecretNames: string[], + assistantSlug: PackageSlug, + blockSlug: PackageSlug | undefined, + currentUserSlug: string, +) { + // Create a set of all secret names + const allSecretNames = new Set([ + ...userSecretNames, + ...orgSecretNames, + ...assistantSecretNames, + ...blockSecretNames, + ]); + + // Use the resolution order to get a single SecretLocation for each secret name + const secretLocations: SecretLocation[] = []; + for (const secretName of allSecretNames) { + // Get the order of places to look + const locationsToLook = getLocationsToLook( + assistantSlug, + blockSlug, + currentUserSlug, + secretName, + ); + + // Go through the locations one by one + for (const secretLocation of locationsToLook) { + // "Looking in a location" in this case means looking through one of the lists of secret names + // First we get that list of secret names + let secretNamesList: string[] = []; + switch (secretLocation.secretType) { + case SecretType.User: + secretNamesList = userSecretNames; + break; + case SecretType.Organization: + secretNamesList = orgSecretNames; + break; + case SecretType.Package: + if (packageSlugsEqual(secretLocation.packageSlug, assistantSlug)) { + secretNamesList = assistantSecretNames; + } else if ( + blockSlug && + packageSlugsEqual(secretLocation.packageSlug, blockSlug) + ) { + secretNamesList = blockSecretNames; + } + break; + } + + // Then we look through that list for the matching secret name + if (secretNamesList) { + const matchingSecretName = secretNamesList.find( + (secretName) => secretName === secretLocation.secretName, + ); + if (matchingSecretName) { + // If we find a matching secret name, we add the location to the list + secretLocations.push(secretLocation); + break; + } + } + } + } + + return secretLocations; +} + export async function resolveFQSN( currentUserSlug: string, fqsn: FQSN, platformSecretStore: PlatformSecretStore, ): Promise { // First create the list of secret locations to try in order - const reversedSlugs = [...fqsn.packageSlugs].reverse(); - - const locationsToLook: SecretLocation[] = [ - // Organization first - ...reversedSlugs.map((slug) => ({ - secretType: SecretType.Organization as const, - orgSlug: slug.ownerSlug, - secretName: fqsn.secretName, - })), - // Then packages - ...reversedSlugs.map((slug) => ({ - secretType: SecretType.Package as const, - packageSlug: slug, - secretName: fqsn.secretName, - })), - // Then user - { - secretType: SecretType.User as const, - userSlug: currentUserSlug, - secretName: fqsn.secretName, - }, - ]; + const assistantSlug = fqsn.packageSlugs[0]; + const blockSlug = fqsn.packageSlugs[1]; + const locationsToLook = getLocationsToLook( + assistantSlug, + blockSlug, + currentUserSlug, + fqsn.secretName, + ); // Then try to get the secret from each location for (const secretLocation of locationsToLook) { diff --git a/packages/config-yaml/src/interfaces/slugs.ts b/packages/config-yaml/src/interfaces/slugs.ts index a6926fb5b..dd225aa10 100644 --- a/packages/config-yaml/src/interfaces/slugs.ts +++ b/packages/config-yaml/src/interfaces/slugs.ts @@ -26,6 +26,16 @@ export function encodeFullSlug(fullSlug: FullSlug): string { return `${fullSlug.ownerSlug}/${fullSlug.packageSlug}@${fullSlug.versionSlug}`; } +export function packageSlugsEqual( + pkgSlug1: PackageSlug, + pkgSlug2: PackageSlug, +): boolean { + return ( + pkgSlug1.ownerSlug === pkgSlug2.ownerSlug && + pkgSlug1.packageSlug === pkgSlug2.packageSlug + ); +} + export function decodeFullSlug(fullSlug: string): FullSlug { const [ownerSlug, packageSlug, versionSlug] = fullSlug.split(/[/@]/); return { From 683d73134f7b7a1c82ae162f4009e899b6bb0c95 Mon Sep 17 00:00:00 2001 From: Nate Date: Wed, 19 Feb 2025 17:30:31 -0800 Subject: [PATCH 2/4] bump version --- packages/config-yaml/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/config-yaml/package.json b/packages/config-yaml/package.json index 09f933d42..005213503 100644 --- a/packages/config-yaml/package.json +++ b/packages/config-yaml/package.json @@ -1,6 +1,6 @@ { "name": "@continuedev/config-yaml", - "version": "1.0.31", + "version": "1.0.32", "description": "", "main": "dist/index.js", "types": "dist/index.d.ts", From ec56580ad7676e4954745e92958b9ca4ec9f9fb3 Mon Sep 17 00:00:00 2001 From: Nate Date: Wed, 19 Feb 2025 17:45:14 -0800 Subject: [PATCH 3/4] annotate return type --- packages/config-yaml/src/interfaces/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/config-yaml/src/interfaces/index.ts b/packages/config-yaml/src/interfaces/index.ts index caea06e53..8986c170c 100644 --- a/packages/config-yaml/src/interfaces/index.ts +++ b/packages/config-yaml/src/interfaces/index.ts @@ -74,7 +74,7 @@ export async function listAvailableSecrets( assistantSlug: PackageSlug, blockSlug: PackageSlug | undefined, currentUserSlug: string, -) { +): Promise { // Create a set of all secret names const allSecretNames = new Set([ ...userSecretNames, From cb81c685f47f6a4e15ba66faebf953d0c94ee45a Mon Sep 17 00:00:00 2001 From: Nate Date: Wed, 19 Feb 2025 22:21:45 -0800 Subject: [PATCH 4/4] add models add on and free trial as secret locations --- packages/config-yaml/package.json | 2 +- .../src/interfaces/SecretResult.ts | 36 +++++++++++++++++-- packages/config-yaml/src/interfaces/index.ts | 12 ++++++- packages/config-yaml/src/load/clientRender.ts | 2 +- 4 files changed, 47 insertions(+), 5 deletions(-) diff --git a/packages/config-yaml/package.json b/packages/config-yaml/package.json index 005213503..44f05cc9a 100644 --- a/packages/config-yaml/package.json +++ b/packages/config-yaml/package.json @@ -1,6 +1,6 @@ { "name": "@continuedev/config-yaml", - "version": "1.0.32", + "version": "1.0.34", "description": "", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/packages/config-yaml/src/interfaces/SecretResult.ts b/packages/config-yaml/src/interfaces/SecretResult.ts index b15940d6e..0c34eae68 100644 --- a/packages/config-yaml/src/interfaces/SecretResult.ts +++ b/packages/config-yaml/src/interfaces/SecretResult.ts @@ -5,6 +5,8 @@ export enum SecretType { Package = "package", Organization = "organization", NotFound = "not_found", + ModelsAddOn = "models_add_on", + FreeTrial = "free_trial", } export interface OrgSecretLocation { @@ -25,6 +27,16 @@ export interface UserSecretLocation { secretName: string; } +export interface ModelsAddOnSecretLocation { + secretType: SecretType.ModelsAddOn; + secretName: string; +} + +export interface FreeTrialSecretLocation { + secretType: SecretType.FreeTrial; + secretName: string; +} + /** * If not found in user/package/org secrets, then there's a chance it's in * - the on-prem proxy @@ -40,7 +52,9 @@ export type SecretLocation = | OrgSecretLocation | PackageSecretLocation | UserSecretLocation - | NotFoundSecretLocation; + | NotFoundSecretLocation + | ModelsAddOnSecretLocation + | FreeTrialSecretLocation; export function encodeSecretLocation(secretLocation: SecretLocation): string { if (secretLocation.secretType === SecretType.Organization) { @@ -51,6 +65,10 @@ export function encodeSecretLocation(secretLocation: SecretLocation): string { return `${SecretType.Package}:${encodePackageSlug(secretLocation.packageSlug)}/${secretLocation.secretName}`; } else if (secretLocation.secretType === SecretType.NotFound) { return `${SecretType.NotFound}:${secretLocation.secretName}`; + } else if (secretLocation.secretType === SecretType.ModelsAddOn) { + return `${SecretType.ModelsAddOn}:${secretLocation.secretName}`; + } else if (secretLocation.secretType === SecretType.FreeTrial) { + return `${SecretType.FreeTrial}:${secretLocation.secretName}`; } else { throw new Error(`Invalid secret type: ${secretLocation}`); } @@ -85,6 +103,16 @@ export function decodeSecretLocation(secretLocation: string): SecretLocation { secretType: SecretType.NotFound, secretName, }; + case SecretType.ModelsAddOn: + return { + secretType: SecretType.ModelsAddOn, + secretName, + }; + case SecretType.FreeTrial: + return { + secretType: SecretType.FreeTrial, + secretName, + }; default: throw new Error(`Invalid secret type: ${secretType}`); } @@ -98,7 +126,11 @@ export interface NotFoundSecretResult { export interface FoundSecretResult { found: true; - secretLocation: OrgSecretLocation | PackageSecretLocation; + secretLocation: + | OrgSecretLocation + | PackageSecretLocation + | ModelsAddOnSecretLocation + | FreeTrialSecretLocation; fqsn: FQSN; } diff --git a/packages/config-yaml/src/interfaces/index.ts b/packages/config-yaml/src/interfaces/index.ts index 8986c170c..264a2d376 100644 --- a/packages/config-yaml/src/interfaces/index.ts +++ b/packages/config-yaml/src/interfaces/index.ts @@ -34,6 +34,11 @@ export function getLocationsToLook( secretName: string, ): SecretLocation[] { const locationsToLook: SecretLocation[] = [ + // Models Add-On + { + secretType: SecretType.ModelsAddOn as const, + secretName, + }, // Block ...(blockSlug ? [ @@ -56,12 +61,17 @@ export function getLocationsToLook( orgSlug: assistantSlug.ownerSlug, secretName, }, - // Then user + // User { secretType: SecretType.User as const, userSlug: currentUserSlug, secretName, }, + // Free Trial + { + secretType: SecretType.FreeTrial as const, + secretName, + }, ]; return locationsToLook; } diff --git a/packages/config-yaml/src/load/clientRender.ts b/packages/config-yaml/src/load/clientRender.ts index 1746be516..e5cc38fd0 100644 --- a/packages/config-yaml/src/load/clientRender.ts +++ b/packages/config-yaml/src/load/clientRender.ts @@ -71,7 +71,7 @@ export async function clientRender( return finalConfig; } -function getUnrenderedSecretLocation( +export function getUnrenderedSecretLocation( value: string | undefined, ): SecretLocation | undefined { if (!value) return undefined;