Skip to content

Commit

Permalink
Update resources in architect prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
HemanthDogiparthi12 committed Jan 7, 2025
1 parent 3d6ad18 commit 3c685e7
Showing 1 changed file with 89 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type updateArchitectUserPromptFunc func(ctx context.Context, p *architectUserPro
type deleteArchitectUserPromptFunc func(ctx context.Context, p *architectUserPromptProxy, id string, allResources bool) (*platformclientv2.APIResponse, error)
type createArchitectUserPromptResourceFunc func(ctx context.Context, p *architectUserPromptProxy, id string, resource platformclientv2.Promptassetcreate) (*platformclientv2.Promptasset, *platformclientv2.APIResponse, error)
type createOrUpdateArchitectUserPromptResourcesFunc func(context.Context, *architectUserPromptProxy, *schema.ResourceData, string, bool) (*platformclientv2.APIResponse, error)
type deleteArchitectUserPromptResourceFunc func(ctx context.Context, p *architectUserPromptProxy, id string, languageCode string) (*platformclientv2.APIResponse, error)
type updateArchitectUserPromptResourceFunc func(ctx context.Context, p *architectUserPromptProxy, id string, languageCode string, body platformclientv2.Promptasset) (*platformclientv2.Promptasset, *platformclientv2.APIResponse, error)
type getArchitectUserPromptIdByNameFunc func(ctx context.Context, p *architectUserPromptProxy, name string) (string, *platformclientv2.APIResponse, error, bool)
type uploadPromptFileFunc func(ctx context.Context, p *architectUserPromptProxy, uploadUri, filename string) error
Expand All @@ -49,6 +50,7 @@ type architectUserPromptProxy struct {
createArchitectUserPromptResourceAttr createArchitectUserPromptResourceFunc
updateArchitectUserPromptResourceAttr updateArchitectUserPromptResourceFunc
createOrUpdateArchitectUserPromptResourcesAttr createOrUpdateArchitectUserPromptResourcesFunc
deleteArchitectUserPromptResourceAttr deleteArchitectUserPromptResourceFunc
getArchitectUserPromptIdByNameAttr getArchitectUserPromptIdByNameFunc
uploadPromptFileAttr uploadPromptFileFunc
getArchitectUserPromptResourcesAttr getArchitectUserPromptResourcesFunc
Expand Down Expand Up @@ -133,6 +135,11 @@ func (p *architectUserPromptProxy) updateArchitectUserPromptResource(ctx context
return p.updateArchitectUserPromptResourceAttr(ctx, p, id, languageCode, body)
}

// deleteArchitectUserPromptResource deletes a user prompt resource
func (p *architectUserPromptProxy) deleteArchitectUserPromptResource(ctx context.Context, id, languageCode string) (*platformclientv2.APIResponse, error) {
return p.deleteArchitectUserPromptResourceAttr(ctx, p, id, languageCode)
}

// getArchitectUserPromptIdByName retrieves a user prompt by name
func (p *architectUserPromptProxy) getArchitectUserPromptIdByName(ctx context.Context, name string) (string, *platformclientv2.APIResponse, error, bool) {
return p.getArchitectUserPromptIdByNameAttr(ctx, p, name)
Expand Down Expand Up @@ -258,10 +265,14 @@ func updateArchitectUserPromptResourceFn(_ context.Context, p *architectUserProm
return p.architectApi.PutArchitectPromptResource(id, languageCode, body)
}

func deleteArchitectUserPromptResourceFn(_ context.Context, p *architectUserPromptProxy, id, languageCode string) (*platformclientv2.APIResponse, error) {
return p.architectApi.DeleteArchitectPromptResource(id, languageCode)
}

func createOrUpdateArchitectUserPromptResourcesFn(ctx context.Context, p *architectUserPromptProxy, d *schema.ResourceData, promptId string, create bool) (*platformclientv2.APIResponse, error) {
var allLanguages []string

resourcesToCreate, resourcesToUpdate, resp, err := p.buildUserPromptResourcesForCreateAndUpdate(ctx, d, promptId, create)
resourcesToCreate, resourcesToUpdate, resourcesToDelete, resp, err := p.buildUserPromptResourcesForCreateAndUpdate(ctx, d, promptId, create)
if err != nil {
return resp, err
}
Expand Down Expand Up @@ -294,9 +305,30 @@ func createOrUpdateArchitectUserPromptResourcesFn(ctx context.Context, p *archit
allLanguages = append(allLanguages, *r.Language)
}

for _, language := range resourcesToDelete {
log.Printf("Deleting user prompt resource for language: %s", language)
resp, err := p.deleteArchitectUserPromptResource(ctx, d.Id(), language)
if err != nil {
return resp, fmt.Errorf("failed to delete user prompt resource for language '%s': %v", language, err)
}

removeByValue(allLanguages, language)
}

return p.verifyPromptResourceFilesAreTranscoded(ctx, promptId, allLanguages)
}

func removeByValue(slice []string, value string) []string {
for i, v := range slice {
if v == value {
// Remove the element by value
return append(slice[:i], slice[i+1:]...)
}
}
// Value not found; return the original slice return slice }
return slice
}

func getArchitectUserPromptResourcesFn(ctx context.Context, p *architectUserPromptProxy, promptId string) (*[]platformclientv2.Promptasset, *platformclientv2.APIResponse, error) {
const pageSize = 100
var allResources []platformclientv2.Promptasset
Expand Down Expand Up @@ -381,57 +413,87 @@ func (p *architectUserPromptProxy) retrieveFilenameAndUploadPromptAsset(ctx cont
return nil
}

func (p *architectUserPromptProxy) buildUserPromptResourcesForCreateAndUpdate(ctx context.Context, d *schema.ResourceData, promptId string, create bool) ([]platformclientv2.Promptassetcreate, []platformclientv2.Promptasset, *platformclientv2.APIResponse, error) {
func (p *architectUserPromptProxy) buildUserPromptResourcesForCreateAndUpdate(ctx context.Context, d *schema.ResourceData, promptId string, create bool) ([]platformclientv2.Promptassetcreate, []platformclientv2.Promptasset, []string, *platformclientv2.APIResponse, error) {
var (
toCreate []platformclientv2.Promptassetcreate
toUpdate []platformclientv2.Promptasset

toCreate []platformclientv2.Promptassetcreate
toUpdate []platformclientv2.Promptasset
toDelete []string
existingResources *[]platformclientv2.Promptasset
)

resources, ok := d.Get("resources").(*schema.Set)
if !ok || resources == nil {
return toCreate, toUpdate, nil, nil
if (!ok || resources == nil) && create {
return toCreate, toUpdate, toDelete, nil, nil
}

if !create {
// Look up the existing resources for this prompt
userPrompt, resp, err := p.getArchitectUserPrompt(ctx, d.Id(), true, true, nil, false)
if err != nil {
return toCreate, toUpdate, resp, fmt.Errorf("failed to lookup existing resources for prompt '%s': %v", d.Id(), err)
return toCreate, toUpdate, toDelete, resp, fmt.Errorf("failed to lookup existing resources for prompt '%s': %v", d.Id(), err)
}
existingResources = userPrompt.Resources
}

for _, promptResource := range resources.List() {
languageExists := false
promptResourceMap, ok := promptResource.(map[string]interface{})
if !ok {
continue
if !ok || resources == nil {
if existingResources != nil {
for _, r := range *existingResources {
toDelete = append(toDelete, *r.Language)
}
}
return toCreate, toUpdate, toDelete, nil, nil
} else {
for _, promptResource := range resources.List() {
languageExists := false
promptResourceMap, ok := promptResource.(map[string]interface{})
if !ok {
continue
}

resourceLanguage := promptResourceMap["language"].(string)
resourceLanguage := promptResourceMap["language"].(string)

if existingResources != nil {
// Check if language resource already exists
for _, r := range *existingResources {
if *r.Language == resourceLanguage {
languageExists = true
break
if existingResources != nil {
// Check if language resource already exists
for _, r := range *existingResources {
if *r.Language == resourceLanguage {
languageExists = true
break
}
}
}

if languageExists {
updateResourceStruct := buildUserPromptResourceForUpdate(promptResourceMap)
toUpdate = append(toUpdate, *updateResourceStruct)
} else {
createResourceStruct := buildUserPromptResourceForCreate(promptResourceMap)
toCreate = append(toCreate, *createResourceStruct)
}
}
}

if existingResources != nil {
for _, re := range *existingResources {
resourceExists := false
for _, promptResource := range resources.List() {
promptResourceMap, ok := promptResource.(map[string]interface{})
if !ok {
continue
}
resourceLanguage := promptResourceMap["language"].(string)
if *re.Language == resourceLanguage {
resourceExists = true
break
}
}

if languageExists {
updateResourceStruct := buildUserPromptResourceForUpdate(promptResourceMap)
toUpdate = append(toUpdate, *updateResourceStruct)
} else {
createResourceStruct := buildUserPromptResourceForCreate(promptResourceMap)
toCreate = append(toCreate, *createResourceStruct)
if !resourceExists {
toDelete = append(toDelete, *re.Language)
}
}
}

return toCreate, toUpdate, nil, nil
return toCreate, toUpdate, toDelete, nil, nil
}

// getArchitectUserPromptIdByNameFn will query user prompt by name and retry if search has not yet indexed the user prompt.
Expand Down

0 comments on commit 3c685e7

Please sign in to comment.