OneBot: Leantime 改造版源码(BOM/Univer 表格/AI 接管/品牌替换等)
This commit is contained in:
38
app/Core/Support/LLMStringSanitizer.php
Normal file
38
app/Core/Support/LLMStringSanitizer.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace Leantime\Core\Support;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* Sanitizes strings for safe LLM/AI consumption, prevents prompt injection attacks.
|
||||
*
|
||||
* Delegates to the Str::sanitizeForLLM() macro for the actual sanitization logic.
|
||||
* This class provides a static API for use in entity formatters and other contexts
|
||||
* where the Str macro call pattern is less convenient.
|
||||
*/
|
||||
class LLMStringSanitizer
|
||||
{
|
||||
/**
|
||||
* Sanitize a string for safe use with LLM APIs.
|
||||
*
|
||||
* Removes potential prompt injection patterns and problematic characters
|
||||
* that could interfere with JSON serialization or system prompts.
|
||||
*
|
||||
* @param mixed $input The value to sanitize
|
||||
* @param bool $removeNewlines Whether to strip newlines from the result
|
||||
* @return string The sanitized string
|
||||
*/
|
||||
public static function sanitizeForLLM(mixed $input, bool $removeNewlines = false): string
|
||||
{
|
||||
if ($input === null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (! is_string($input)) {
|
||||
return (string) $input;
|
||||
}
|
||||
|
||||
return Str::sanitizeForLLM($input, $removeNewlines);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user