Skip to content

Commit cc35e7f

Browse files
committed
Some refactoring
1 parent 8988ab0 commit cc35e7f

File tree

1 file changed

+27
-48
lines changed

1 file changed

+27
-48
lines changed

modules/llm_module.go

Lines changed: 27 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,8 @@ 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-
// TODO: User ID should not be passed in the body.
39-
// TODO: It should be extracted from the auth context, which i am not going to do now :)
40-
// making sure user_id and notebook_id are present
41-
if _, hasUserID := requestData["user_id"]; !hasUserID {
42-
return nil, fmt.Errorf("request body must contain 'user_id'")
43-
}
44-
if _, hasNotebookID := requestData["notebook_id"]; !hasNotebookID {
45-
return nil, fmt.Errorf("request body must contain 'notebook_id'")
46-
}
47-
notebookIDStr, isString := requestData["notebook_id"].(string)
48-
if !isString || notebookIDStr == "" {
49-
return nil, fmt.Errorf("'notebook_id' must be a non-empty string")
50-
}
51-
userIDStr, isString := requestData["user_id"].(string)
52-
if !isString || userIDStr == "" {
53-
return nil, fmt.Errorf("'user_id' must be a non-empty string")
38+
if err := IsUserIDandNotebookIDPresent(requestData); err != nil {
39+
return nil, err
5440
}
5541

5642
finalBodyBytes, err := json.Marshal(requestData)
@@ -73,22 +59,8 @@ func (m *LlmModule) ModifyNotebook(ctx context.Context, sessionID string, body i
7359
return nil, fmt.Errorf("failed to decode request body as JSON: %w", err)
7460
}
7561

76-
// TODO: User ID should not be passed in the body.
77-
// TODO: It should be extracted from the auth context, which i am not going to do now :)
78-
// making sure user_id and notebook_id are present
79-
if _, hasUserID := requestData["user_id"]; !hasUserID {
80-
return nil, fmt.Errorf("request body must contain 'user_id'")
81-
}
82-
if _, hasNotebookID := requestData["notebook_id"]; !hasNotebookID {
83-
return nil, fmt.Errorf("request body must contain 'notebook_id'")
84-
}
85-
notebookIDStr, isString := requestData["notebook_id"].(string)
86-
if !isString || notebookIDStr == "" {
87-
return nil, fmt.Errorf("'notebook_id' must be a non-empty string")
88-
}
89-
userIDStr, isString := requestData["user_id"].(string)
90-
if !isString || userIDStr == "" {
91-
return nil, fmt.Errorf("'user_id' must be a non-empty string")
62+
if err := IsUserIDandNotebookIDPresent(requestData); err != nil {
63+
return nil, err
9264
}
9365

9466
if instruction, ok := requestData["instruction"].(string); !ok || instruction == "" {
@@ -118,22 +90,8 @@ func (m *LlmModule) FixNotebook(ctx context.Context, sessionID string, body io.R
11890
return nil, fmt.Errorf("failed to decode request body as JSON: %w", err)
11991
}
12092

121-
// TODO: User ID should not be passed in the body.
122-
// TODO: It should be extracted from the auth context, which i am not going to do now :)
123-
// making sure user_id and notebook_id are present
124-
if _, hasUserID := requestData["user_id"]; !hasUserID {
125-
return nil, fmt.Errorf("request body must contain 'user_id'")
126-
}
127-
if _, hasNotebookID := requestData["notebook_id"]; !hasNotebookID {
128-
return nil, fmt.Errorf("request body must contain 'notebook_id'")
129-
}
130-
notebookIDStr, isString := requestData["notebook_id"].(string)
131-
if !isString || notebookIDStr == "" {
132-
return nil, fmt.Errorf("'notebook_id' must be a non-empty string")
133-
}
134-
userIDStr, isString := requestData["user_id"].(string)
135-
if !isString || userIDStr == "" {
136-
return nil, fmt.Errorf("'user_id' must be a non-empty string")
93+
if err := IsUserIDandNotebookIDPresent(requestData); err != nil {
94+
return nil, err
13795
}
13896

13997
if traceback, ok := requestData["traceback"].(string); !ok || traceback == "" {
@@ -150,3 +108,24 @@ func (m *LlmModule) FixNotebook(ctx context.Context, sessionID string, body io.R
150108

151109
return m.Repo.FixNotebook(ctx, bytes.NewBuffer(bodyBytes))
152110
}
111+
112+
func IsUserIDandNotebookIDPresent(requestData map[string]interface{}) 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+
}
119+
if _, hasNotebookID := requestData["notebook_id"]; !hasNotebookID {
120+
return fmt.Errorf("request body must contain 'notebook_id'")
121+
}
122+
notebookIDStr, isString := requestData["notebook_id"].(string)
123+
if !isString || notebookIDStr == "" {
124+
return fmt.Errorf("'notebook_id' must be a non-empty string")
125+
}
126+
userIDStr, isString := requestData["user_id"].(string)
127+
if !isString || userIDStr == "" {
128+
return fmt.Errorf("'user_id' must be a non-empty string")
129+
}
130+
return nil
131+
}

0 commit comments

Comments
 (0)