-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gateway): add prompt registry crud operation
Signed-off-by: Praveen Yadav <pyadav9678@gmail.com>
- Loading branch information
Showing
35 changed files
with
5,303 additions
and
3,168 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,104 @@ | ||
package v1 | ||
|
||
import ( | ||
"context" | ||
|
||
"connectrpc.com/connect" | ||
"github.com/missingstudio/studio/backend/internal/prompt" | ||
"github.com/missingstudio/studio/backend/models" | ||
"github.com/missingstudio/studio/common/errors" | ||
promptv1 "github.com/missingstudio/studio/protos/pkg/prompt/v1" | ||
"google.golang.org/protobuf/types/known/emptypb" | ||
"google.golang.org/protobuf/types/known/structpb" | ||
) | ||
|
||
func (s *V1Handler) ListPrompts(ctx context.Context, req *connect.Request[emptypb.Empty]) (*connect.Response[promptv1.ListPromptsResponse], error) { | ||
prompts, err := s.promptService.GetAll(ctx) | ||
if err != nil { | ||
return nil, errors.NewInternalError(err.Error()) | ||
} | ||
|
||
data := []*promptv1.Prompt{} | ||
for _, p := range prompts { | ||
pmetadata, _ := structpb.NewStruct(p.Metadata) | ||
|
||
data = append(data, &promptv1.Prompt{ | ||
Id: p.ID.String(), | ||
Name: p.Name, | ||
Description: p.Description, | ||
Template: p.Template, | ||
Metadata: pmetadata, | ||
}) | ||
} | ||
|
||
return connect.NewResponse(&promptv1.ListPromptsResponse{ | ||
Prompt: data, | ||
}), nil | ||
} | ||
|
||
func (s *V1Handler) CreatePrompt(ctx context.Context, req *connect.Request[promptv1.CreatePromptRequest]) (*connect.Response[promptv1.CreatePromptResponse], error) { | ||
prompt := models.Prompt{ | ||
Name: req.Msg.Name, | ||
Description: req.Msg.Description, | ||
Template: req.Msg.Template, | ||
Metadata: req.Msg.Metadata.AsMap(), | ||
} | ||
|
||
prompt, err := s.promptService.Upsert(ctx, prompt) | ||
if err != nil { | ||
return nil, errors.NewNotFound(err.Error()) | ||
} | ||
|
||
stMetadata, err := structpb.NewStruct(prompt.Metadata) | ||
if err != nil { | ||
return nil, errors.NewInternalError(err.Error()) | ||
} | ||
|
||
return connect.NewResponse(&promptv1.CreatePromptResponse{ | ||
Name: prompt.Name, | ||
Description: prompt.Description, | ||
Template: prompt.Template, | ||
Metadata: stMetadata, | ||
}), nil | ||
} | ||
|
||
func (s *V1Handler) GetPrompt(ctx context.Context, req *connect.Request[promptv1.GetPromptRequest]) (*connect.Response[promptv1.GetPromptResponse], error) { | ||
prompt, err := s.promptService.GetByName(ctx, req.Msg.Name) | ||
if err != nil { | ||
return nil, errors.NewNotFound(err.Error()) | ||
} | ||
|
||
stMetadata, err := structpb.NewStruct(prompt.Metadata) | ||
if err != nil { | ||
return nil, errors.NewInternalError(err.Error()) | ||
} | ||
|
||
p := &promptv1.Prompt{ | ||
Id: prompt.ID.String(), | ||
Name: prompt.Name, | ||
Description: prompt.Description, | ||
Template: prompt.Template, | ||
Metadata: stMetadata, | ||
} | ||
|
||
return connect.NewResponse(&promptv1.GetPromptResponse{ | ||
Prompt: p, | ||
}), nil | ||
} | ||
|
||
func (s *V1Handler) GetPromptValue(ctx context.Context, req *connect.Request[promptv1.GetPromptValueRequest]) (*connect.Response[promptv1.GetPromptValueResponse], error) { | ||
p, err := s.promptService.GetByName(ctx, req.Msg.Name) | ||
if err != nil { | ||
return nil, errors.NewNotFound(err.Error()) | ||
} | ||
|
||
prompt := prompt.NewPrompt(p.Template, req.Msg.Data.AsMap()) | ||
value, err := prompt.Run() | ||
if err != nil { | ||
return nil, errors.NewNotFound(err.Error()) | ||
} | ||
|
||
return connect.NewResponse(&promptv1.GetPromptValueResponse{ | ||
Data: value, | ||
}), nil | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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,39 @@ | ||
package prompt | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"html/template" | ||
|
||
"github.com/gofrs/uuid" | ||
"github.com/missingstudio/studio/backend/models" | ||
) | ||
|
||
type Repository interface { | ||
GetAll(context.Context) ([]models.Prompt, error) | ||
Upsert(context.Context, models.Prompt) (models.Prompt, error) | ||
GetByID(context.Context, uuid.UUID) (models.Prompt, error) | ||
GetByName(context.Context, string) (models.Prompt, error) | ||
DeleteByID(context.Context, uuid.UUID) error | ||
} | ||
|
||
type Prompt struct { | ||
tmpl *template.Template | ||
data map[string]any | ||
} | ||
|
||
func NewPrompt(text string, data map[string]any) *Prompt { | ||
return &Prompt{ | ||
tmpl: template.Must(template.New("prompt").Parse(text)), | ||
data: data, | ||
} | ||
} | ||
|
||
func (p *Prompt) Run() (string, error) { | ||
var buf bytes.Buffer | ||
err := p.tmpl.Execute(&buf, p.data) | ||
if err != nil { | ||
return "", err | ||
} | ||
return buf.String(), nil | ||
} |
This file contains 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,60 @@ | ||
package prompt | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/gofrs/uuid" | ||
"github.com/missingstudio/studio/backend/models" | ||
) | ||
|
||
var _ Repository = &Service{} | ||
|
||
type Service struct { | ||
promptRepo Repository | ||
} | ||
|
||
func NewService(promptRepo Repository) *Service { | ||
return &Service{ | ||
promptRepo: promptRepo, | ||
} | ||
} | ||
|
||
func (s *Service) DeleteByID(ctx context.Context, promptID uuid.UUID) error { | ||
return s.promptRepo.DeleteByID(ctx, promptID) | ||
} | ||
|
||
func (s *Service) GetAll(ctx context.Context) ([]models.Prompt, error) { | ||
prompts, err := s.promptRepo.GetAll(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return prompts, nil | ||
} | ||
|
||
func (s *Service) GetByID(ctx context.Context, promptID uuid.UUID) (models.Prompt, error) { | ||
prompt, err := s.promptRepo.GetByID(ctx, promptID) | ||
if err != nil { | ||
return models.Prompt{}, err | ||
} | ||
|
||
return prompt, err | ||
} | ||
|
||
func (s *Service) GetByName(ctx context.Context, name string) (models.Prompt, error) { | ||
prompt, err := s.promptRepo.GetByName(ctx, name) | ||
if err != nil { | ||
return models.Prompt{}, err | ||
} | ||
|
||
return prompt, err | ||
} | ||
|
||
func (s *Service) Upsert(ctx context.Context, c models.Prompt) (models.Prompt, error) { | ||
id, err := s.promptRepo.Upsert(ctx, c) | ||
if err != nil { | ||
return models.Prompt{}, fmt.Errorf("failed to save prompt: %w", err) | ||
} | ||
|
||
return id, err | ||
} |
1 change: 1 addition & 0 deletions
1
gateway/internal/storage/postgres/migrations/000002_create_prompt_table.down.sql
This file contains 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 @@ | ||
DROP TABLE IF EXISTS prompts; |
11 changes: 11 additions & 0 deletions
11
gateway/internal/storage/postgres/migrations/000002_create_prompt_table.up.sql
This file contains 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,11 @@ | ||
CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; | ||
|
||
CREATE TABLE prompts ( | ||
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(), | ||
name text UNIQUE NOT NULL, | ||
description text, | ||
template text, | ||
metadata jsonb, | ||
created_at timestamp DEFAULT NOW(), | ||
updated_at timestamp DEFAULT NOW() | ||
); |
This file contains 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
Oops, something went wrong.