@@ -24,7 +24,7 @@ func NewLlmModule(repo repository.LlmRepository) *LlmModule {
2424}
2525
2626// GenerateNotebook validates and proxies the generate request.
27- func (m * LlmModule ) GenerateNotebook (ctx context.Context , body io.Reader ) (* http.Response , error ) {
27+ func (m * LlmModule ) GenerateNotebook (ctx context.Context , body io.Reader , userID string ) (* http.Response , error ) {
2828 bodyBytes , err := io .ReadAll (body )
2929 if err != nil {
3030 return nil , fmt .Errorf ("failed to read request body: %w" , err )
@@ -35,10 +35,12 @@ func (m *LlmModule) GenerateNotebook(ctx context.Context, body io.Reader) (*http
3535 return nil , fmt .Errorf ("failed to decode request body as JSON: %w" , err )
3636 }
3737
38- if err := IsUserIDandNotebookIDPresent (requestData ); err != nil {
38+ if err := IsNotebookIDPresent (requestData ); err != nil {
3939 return nil , err
4040 }
4141
42+ requestData ["user_id" ] = userID
43+
4244 finalBodyBytes , err := json .Marshal (requestData )
4345 if err != nil {
4446 return nil , fmt .Errorf ("failed to re-encode request body: %w" , err )
@@ -48,7 +50,7 @@ func (m *LlmModule) GenerateNotebook(ctx context.Context, body io.Reader) (*http
4850}
4951
5052// ModifyNotebook validates and proxies the modify request.
51- func (m * LlmModule ) ModifyNotebook (ctx context.Context , sessionID string , body io.Reader ) (* http.Response , error ) {
53+ func (m * LlmModule ) ModifyNotebook (ctx context.Context , sessionID string , body io.Reader , userID string ) (* http.Response , error ) {
5254 bodyBytes , err := io .ReadAll (body )
5355 if err != nil {
5456 return nil , fmt .Errorf ("failed to read request body: %w" , err )
@@ -59,10 +61,12 @@ func (m *LlmModule) ModifyNotebook(ctx context.Context, sessionID string, body i
5961 return nil , fmt .Errorf ("failed to decode request body as JSON: %w" , err )
6062 }
6163
62- if err := IsUserIDandNotebookIDPresent (requestData ); err != nil {
64+ if err := IsNotebookIDPresent (requestData ); err != nil {
6365 return nil , err
6466 }
6567
68+ requestData ["user_id" ] = userID
69+
6670 if instruction , ok := requestData ["instruction" ].(string ); ! ok || instruction == "" {
6771 return nil , fmt .Errorf ("request body must contain a non-empty 'instruction' string" )
6872 }
@@ -75,11 +79,16 @@ func (m *LlmModule) ModifyNotebook(ctx context.Context, sessionID string, body i
7579 return nil , fmt .Errorf ("'notebook' object must contain a 'cells' array" )
7680 }
7781
78- return m .Repo .ModifyNotebook (ctx , bytes .NewBuffer (bodyBytes ))
82+ finalBodyBytes , err := json .Marshal (requestData )
83+ if err != nil {
84+ return nil , fmt .Errorf ("failed to re-encode request body: %w" , err )
85+ }
86+
87+ return m .Repo .ModifyNotebook (ctx , bytes .NewBuffer (finalBodyBytes ))
7988}
8089
8190// FixNotebook validates and proxies the fix request.
82- func (m * LlmModule ) FixNotebook (ctx context.Context , sessionID string , body io.Reader ) (* http.Response , error ) {
91+ func (m * LlmModule ) FixNotebook (ctx context.Context , sessionID string , body io.Reader , userID string ) (* http.Response , error ) {
8392 bodyBytes , err := io .ReadAll (body )
8493 if err != nil {
8594 return nil , fmt .Errorf ("failed to read request body: %w" , err )
@@ -90,10 +99,12 @@ func (m *LlmModule) FixNotebook(ctx context.Context, sessionID string, body io.R
9099 return nil , fmt .Errorf ("failed to decode request body as JSON: %w" , err )
91100 }
92101
93- if err := IsUserIDandNotebookIDPresent (requestData ); err != nil {
102+ if err := IsNotebookIDPresent (requestData ); err != nil {
94103 return nil , err
95104 }
96105
106+ requestData ["user_id" ] = userID
107+
97108 if traceback , ok := requestData ["traceback" ].(string ); ! ok || traceback == "" {
98109 return nil , fmt .Errorf ("request body must contain a non-empty 'traceback' string" )
99110 }
@@ -106,26 +117,22 @@ func (m *LlmModule) FixNotebook(ctx context.Context, sessionID string, body io.R
106117 return nil , fmt .Errorf ("'notebook' object must contain a 'cells' array" )
107118 }
108119
109- return m .Repo .FixNotebook (ctx , bytes .NewBuffer (bodyBytes ))
120+ finalBodyBytes , err := json .Marshal (requestData )
121+ if err != nil {
122+ return nil , fmt .Errorf ("failed to re-encode request body: %w" , err )
123+ }
124+
125+ return m .Repo .FixNotebook (ctx , bytes .NewBuffer (finalBodyBytes ))
110126}
111127
112- func IsUserIDandNotebookIDPresent (requestData map [string ]any ) error {
113- // TODO: User ID should not be passed in the body.
114- // TODO: It should be extracted from the auth context, which i am not going to do now :)
115- // making sure user_id and notebook_id are present
116- if _ , hasUserID := requestData ["user_id" ]; ! hasUserID {
117- return fmt .Errorf ("request body must contain 'user_id'" )
118- }
128+ func IsNotebookIDPresent (requestData map [string ]any ) error {
129+ // making sure notebook_id is present
119130 if _ , hasNotebookID := requestData ["notebook_id" ]; ! hasNotebookID {
120131 return fmt .Errorf ("request body must contain 'notebook_id'" )
121132 }
122133 notebookIDStr , isString := requestData ["notebook_id" ].(string )
123134 if ! isString || notebookIDStr == "" {
124135 return fmt .Errorf ("'notebook_id' must be a non-empty string" )
125136 }
126- userIDStr , isString := requestData ["user_id" ].(string )
127- if ! isString || userIDStr == "" {
128- return fmt .Errorf ("'user_id' must be a non-empty string" )
129- }
130137 return nil
131138}
0 commit comments