Elffuss BraidHub

kikocis/llama-stack public

7e907e713048 · 1.2 KiBRawPermalink
1# Copyright (c) Meta Platforms, Inc. and affiliates.
2# All rights reserved.
3#
4# This source code is licensed under the terms described in the LICENSE file in
5# the root directory of this source tree.
6
7from codeshield.cs import CodeShield
8from termcolor import cprint
9
10from .base import ShieldResponse, TextShield
11from llama_stack.apis.safety import * # noqa: F403
12
13
14class CodeScannerShield(TextShield):
15 def get_shield_type(self) -> ShieldType:
16 return BuiltinShield.code_scanner_guard
17
18 async def run_impl(self, text: str) -> ShieldResponse:
19 cprint(f"Running CodeScannerShield on {text[50:]}", color="magenta")
20 result = await CodeShield.scan_code(text)
21 if result.is_insecure:
22 return ShieldResponse(
23 shield_type=BuiltinShield.code_scanner_guard,
24 is_violation=True,
25 violation_type=",".join(
26 [issue.pattern_id for issue in result.issues_found]
27 ),
28 violation_return_message="Sorry, I found security concerns in the code.",
29 )
30 else:
31 return ShieldResponse(
32 shield_type=BuiltinShield.code_scanner_guard, is_violation=False
33 )