Skip to content

Commit e64cf2c

Browse files
ToniCorinneivanmkc-google
authored andcommitted
cleaning up go safety snippets
1 parent 9e26e31 commit e64cf2c

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

docs/safety/index.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,15 +382,31 @@ When modifications to the tools to add guardrails aren't possible, the [**`Befor
382382
// This is an unexpected failure, return an error.
383383
return nil, fmt.Errorf("internal error: session_user_id not found in state: %w", err)
384384
}
385-
actualUserIDInArgs, _ := args["user_id_param"] // Assuming tool takes 'user_id_param'
385+
expectedUserID, ok := expectedUserIDVal.(string)
386+
if !ok {
387+
return nil, fmt.Errorf("internal error: session_user_id in state is not a string, got %T", expectedUserIDVal)
388+
}
389+
390+
actualUserIDInArgs, exists := args["user_id_param"]
391+
if !exists {
392+
// Handle case where user_id_param is not in args
393+
fmt.Println("Validation Failed: user_id_param missing from arguments!")
394+
return map[string]any{"error": "Tool call blocked: user_id_param missing from arguments."}, nil
395+
}
386396

387-
if !reflect.DeepEqual(actualUserIDInArgs, expectedUserID) {
397+
actualUserID, ok := actualUserIDInArgs.(string)
398+
if !ok {
399+
// Handle case where user_id_param is not a string
400+
fmt.Println("Validation Failed: user_id_param is not a string!")
401+
return map[string]any{"error": "Tool call blocked: user_id_param is not a string."}, nil
402+
}
403+
404+
if actualUserID != expectedUserID {
388405
fmt.Println("Validation Failed: User ID mismatch!")
389406
// Return a map to prevent tool execution and provide feedback to the model.
390407
// This is not a Go error, but a message for the agent.
391408
return map[string]any{"error": "Tool call blocked: User ID mismatch."}, nil
392409
}
393-
394410
// Return nil, nil to allow the tool call to proceed if validation passes
395411
fmt.Println("Callback validation passed.")
396412
return nil, nil

0 commit comments

Comments
 (0)