@@ -58,7 +58,7 @@ |
||
| 58 | 58 | { |
| 59 | 59 | return [ |
| 60 | 60 | 'message' => $message, |
| 61 | - 'original_tokens' => $this->tokenAnalyzer->estimateJson(['content' => $message])['tokens_estimate'], |
|
| 61 | + 'original_tokens' => $this->tokenAnalyzer->estimateJson([ 'content' => $message ])[ 'tokens_estimate' ], |
|
| 62 | 62 | 'compressed_tokens' => $this->tokenAnalyzer->estimate($message), |
| 63 | 63 | 'analysis' => $this->tokenAnalyzer->analyze($message), |
| 64 | 64 | ]; |
@@ -27,18 +27,18 @@ discard block |
||
| 27 | 27 | ?array $options = null |
| 28 | 28 | ): array { |
| 29 | 29 | if (!$this->isEnabled()) { |
| 30 | - return ['success' => false, 'error' => 'OpenAI adapter not enabled']; |
|
| 30 | + return [ 'success' => false, 'error' => 'OpenAI adapter not enabled' ]; |
|
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | $model ??= $this->getDefaultModel(); |
| 34 | - $options ??= []; |
|
| 34 | + $options ??= [ ]; |
|
| 35 | 35 | |
| 36 | 36 | $toonMessage = $this->compressMessage($message); |
| 37 | 37 | |
| 38 | 38 | $payload = array_merge([ |
| 39 | 39 | 'model' => $model, |
| 40 | 40 | 'messages' => [ |
| 41 | - ['role' => 'user', 'content' => $toonMessage], |
|
| 41 | + [ 'role' => 'user', 'content' => $toonMessage ], |
|
| 42 | 42 | ], |
| 43 | 43 | 'temperature' => 0.7, |
| 44 | 44 | ], $options); |
@@ -72,16 +72,16 @@ discard block |
||
| 72 | 72 | ?array $options = null |
| 73 | 73 | ): array { |
| 74 | 74 | if (!$this->isEnabled()) { |
| 75 | - return ['success' => false, 'error' => 'OpenAI adapter not enabled']; |
|
| 75 | + return [ 'success' => false, 'error' => 'OpenAI adapter not enabled' ]; |
|
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | $model ??= $this->getDefaultModel(); |
| 79 | - $options ??= []; |
|
| 79 | + $options ??= [ ]; |
|
| 80 | 80 | |
| 81 | 81 | // Compress messages |
| 82 | 82 | $compressedMessages = array_map(fn ($msg) => [ |
| 83 | 83 | ...$msg, |
| 84 | - 'content' => $this->compressMessage($msg['content']), |
|
| 84 | + 'content' => $this->compressMessage($msg[ 'content' ]), |
|
| 85 | 85 | ], $messages); |
| 86 | 86 | |
| 87 | 87 | $payload = array_merge([ |
@@ -97,12 +97,12 @@ discard block |
||
| 97 | 97 | ->json(); |
| 98 | 98 | |
| 99 | 99 | $originalTokens = array_sum(array_map( |
| 100 | - fn ($msg) => $this->tokenAnalyzer->estimate($msg['content']), |
|
| 100 | + fn ($msg) => $this->tokenAnalyzer->estimate($msg[ 'content' ]), |
|
| 101 | 101 | $messages |
| 102 | 102 | )); |
| 103 | 103 | |
| 104 | 104 | $compressedTokens = array_sum(array_map( |
| 105 | - fn ($msg) => $this->tokenAnalyzer->estimate($msg['content']), |
|
| 105 | + fn ($msg) => $this->tokenAnalyzer->estimate($msg[ 'content' ]), |
|
| 106 | 106 | $compressedMessages |
| 107 | 107 | )); |
| 108 | 108 | |
@@ -147,7 +147,7 @@ discard block |
||
| 147 | 147 | $timeout = config('laravel-toon.adapters.openai.timeout', 30); |
| 148 | 148 | |
| 149 | 149 | $this->client = Http::baseUrl($baseUrl) |
| 150 | - ->withHeader('Authorization', 'Bearer '.$apiKey) |
|
| 150 | + ->withHeader('Authorization', 'Bearer ' . $apiKey) |
|
| 151 | 151 | ->withHeader('Content-Type', 'application/json') |
| 152 | 152 | ->timeout($timeout); |
| 153 | 153 | } |
@@ -27,18 +27,18 @@ discard block |
||
| 27 | 27 | ?array $options = null |
| 28 | 28 | ): array { |
| 29 | 29 | if (!$this->isEnabled()) { |
| 30 | - return ['success' => false, 'error' => 'Mistral adapter not enabled']; |
|
| 30 | + return [ 'success' => false, 'error' => 'Mistral adapter not enabled' ]; |
|
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | $model ??= $this->getDefaultModel(); |
| 34 | - $options ??= []; |
|
| 34 | + $options ??= [ ]; |
|
| 35 | 35 | |
| 36 | 36 | $toonMessage = $this->compressMessage($message); |
| 37 | 37 | |
| 38 | 38 | $payload = array_merge([ |
| 39 | 39 | 'model' => $model, |
| 40 | 40 | 'messages' => [ |
| 41 | - ['role' => 'user', 'content' => $toonMessage], |
|
| 41 | + [ 'role' => 'user', 'content' => $toonMessage ], |
|
| 42 | 42 | ], |
| 43 | 43 | ], $options); |
| 44 | 44 | |
@@ -71,16 +71,16 @@ discard block |
||
| 71 | 71 | ?array $options = null |
| 72 | 72 | ): array { |
| 73 | 73 | if (!$this->isEnabled()) { |
| 74 | - return ['success' => false, 'error' => 'Mistral adapter not enabled']; |
|
| 74 | + return [ 'success' => false, 'error' => 'Mistral adapter not enabled' ]; |
|
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 77 | $model ??= $this->getDefaultModel(); |
| 78 | - $options ??= []; |
|
| 78 | + $options ??= [ ]; |
|
| 79 | 79 | |
| 80 | 80 | // Compress messages |
| 81 | 81 | $compressedMessages = array_map(fn ($msg) => [ |
| 82 | 82 | ...$msg, |
| 83 | - 'content' => $this->compressMessage($msg['content']), |
|
| 83 | + 'content' => $this->compressMessage($msg[ 'content' ]), |
|
| 84 | 84 | ], $messages); |
| 85 | 85 | |
| 86 | 86 | $payload = array_merge([ |
@@ -95,12 +95,12 @@ discard block |
||
| 95 | 95 | ->json(); |
| 96 | 96 | |
| 97 | 97 | $originalTokens = array_sum(array_map( |
| 98 | - fn ($msg) => $this->tokenAnalyzer->estimate($msg['content']), |
|
| 98 | + fn ($msg) => $this->tokenAnalyzer->estimate($msg[ 'content' ]), |
|
| 99 | 99 | $messages |
| 100 | 100 | )); |
| 101 | 101 | |
| 102 | 102 | $compressedTokens = array_sum(array_map( |
| 103 | - fn ($msg) => $this->tokenAnalyzer->estimate($msg['content']), |
|
| 103 | + fn ($msg) => $this->tokenAnalyzer->estimate($msg[ 'content' ]), |
|
| 104 | 104 | $compressedMessages |
| 105 | 105 | )); |
| 106 | 106 | |
@@ -145,7 +145,7 @@ discard block |
||
| 145 | 145 | $timeout = config('laravel-toon.adapters.mistral.timeout', 30); |
| 146 | 146 | |
| 147 | 147 | $this->client = Http::baseUrl($baseUrl) |
| 148 | - ->withHeader('Authorization', 'Bearer '.$apiKey) |
|
| 148 | + ->withHeader('Authorization', 'Bearer ' . $apiKey) |
|
| 149 | 149 | ->withHeader('Content-Type', 'application/json') |
| 150 | 150 | ->timeout($timeout); |
| 151 | 151 | } |
@@ -27,11 +27,11 @@ discard block |
||
| 27 | 27 | ?array $options = null |
| 28 | 28 | ): array { |
| 29 | 29 | if (!$this->isEnabled()) { |
| 30 | - return ['success' => false, 'error' => 'Anthropic adapter not enabled']; |
|
| 30 | + return [ 'success' => false, 'error' => 'Anthropic adapter not enabled' ]; |
|
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | $model ??= $this->getDefaultModel(); |
| 34 | - $options ??= []; |
|
| 34 | + $options ??= [ ]; |
|
| 35 | 35 | |
| 36 | 36 | $toonMessage = $this->compressMessage($message); |
| 37 | 37 | |
@@ -39,7 +39,7 @@ discard block |
||
| 39 | 39 | 'model' => $model, |
| 40 | 40 | 'max_tokens' => 1024, |
| 41 | 41 | 'messages' => [ |
| 42 | - ['role' => 'user', 'content' => $toonMessage], |
|
| 42 | + [ 'role' => 'user', 'content' => $toonMessage ], |
|
| 43 | 43 | ], |
| 44 | 44 | ], $options); |
| 45 | 45 | |
@@ -72,16 +72,16 @@ discard block |
||
| 72 | 72 | ?array $options = null |
| 73 | 73 | ): array { |
| 74 | 74 | if (!$this->isEnabled()) { |
| 75 | - return ['success' => false, 'error' => 'Anthropic adapter not enabled']; |
|
| 75 | + return [ 'success' => false, 'error' => 'Anthropic adapter not enabled' ]; |
|
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | $model ??= $this->getDefaultModel(); |
| 79 | - $options ??= []; |
|
| 79 | + $options ??= [ ]; |
|
| 80 | 80 | |
| 81 | 81 | // Compress messages |
| 82 | 82 | $compressedMessages = array_map(fn ($msg) => [ |
| 83 | 83 | ...$msg, |
| 84 | - 'content' => $this->compressMessage($msg['content']), |
|
| 84 | + 'content' => $this->compressMessage($msg[ 'content' ]), |
|
| 85 | 85 | ], $messages); |
| 86 | 86 | |
| 87 | 87 | $payload = array_merge([ |
@@ -97,12 +97,12 @@ discard block |
||
| 97 | 97 | ->json(); |
| 98 | 98 | |
| 99 | 99 | $originalTokens = array_sum(array_map( |
| 100 | - fn ($msg) => $this->tokenAnalyzer->estimate($msg['content']), |
|
| 100 | + fn ($msg) => $this->tokenAnalyzer->estimate($msg[ 'content' ]), |
|
| 101 | 101 | $messages |
| 102 | 102 | )); |
| 103 | 103 | |
| 104 | 104 | $compressedTokens = array_sum(array_map( |
| 105 | - fn ($msg) => $this->tokenAnalyzer->estimate($msg['content']), |
|
| 105 | + fn ($msg) => $this->tokenAnalyzer->estimate($msg[ 'content' ]), |
|
| 106 | 106 | $compressedMessages |
| 107 | 107 | )); |
| 108 | 108 | |
@@ -22,29 +22,29 @@ discard block |
||
| 22 | 22 | public function register(): void |
| 23 | 23 | { |
| 24 | 24 | $this->mergeConfigFrom( |
| 25 | - __DIR__.'/../config/laravel-toon.php', |
|
| 25 | + __DIR__ . '/../config/laravel-toon.php', |
|
| 26 | 26 | 'laravel-toon' |
| 27 | 27 | ); |
| 28 | 28 | |
| 29 | 29 | // Registrar servicios principales |
| 30 | - $this->app->singleton('toon', function ($app) { |
|
| 30 | + $this->app->singleton('toon', function($app) { |
|
| 31 | 31 | return new ToonService(); |
| 32 | 32 | }); |
| 33 | 33 | |
| 34 | - $this->app->singleton(ToonService::class, function ($app) { |
|
| 34 | + $this->app->singleton(ToonService::class, function($app) { |
|
| 35 | 35 | return $app->make('toon'); |
| 36 | 36 | }); |
| 37 | 37 | |
| 38 | - $this->app->singleton(TokenAnalyzer::class, function ($app) { |
|
| 38 | + $this->app->singleton(TokenAnalyzer::class, function($app) { |
|
| 39 | 39 | return new TokenAnalyzer(); |
| 40 | 40 | }); |
| 41 | 41 | |
| 42 | - $this->app->singleton(CompressionMetrics::class, function ($app) { |
|
| 42 | + $this->app->singleton(CompressionMetrics::class, function($app) { |
|
| 43 | 43 | return new CompressionMetrics(); |
| 44 | 44 | }); |
| 45 | 45 | |
| 46 | - $this->app->singleton(CostCalculator::class, function ($app) { |
|
| 47 | - return new CostCalculator(config('laravel-toon.cost_calculation.models', [])); |
|
| 46 | + $this->app->singleton(CostCalculator::class, function($app) { |
|
| 47 | + return new CostCalculator(config('laravel-toon.cost_calculation.models', [ ])); |
|
| 48 | 48 | }); |
| 49 | 49 | } |
| 50 | 50 | |
@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | { |
| 56 | 56 | // Publicar configuración |
| 57 | 57 | $this->publishes([ |
| 58 | - __DIR__.'/../config/laravel-toon.php' => config_path('laravel-toon.php'), |
|
| 58 | + __DIR__ . '/../config/laravel-toon.php' => config_path('laravel-toon.php'), |
|
| 59 | 59 | ], 'laravel-toon-config'); |
| 60 | 60 | |
| 61 | 61 | // Registrar comandos Artisan |
@@ -32,10 +32,10 @@ |
||
| 32 | 32 | |
| 33 | 33 | private static function normalizeArray(array $array): mixed |
| 34 | 34 | { |
| 35 | - $result = []; |
|
| 35 | + $result = [ ]; |
|
| 36 | 36 | foreach ($array as $key => $item) { |
| 37 | 37 | $normalizedKey = is_int($key) ? $key : (string)$key; |
| 38 | - $result[$normalizedKey] = self::normalize($item); |
|
| 38 | + $result[ $normalizedKey ] = self::normalize($item); |
|
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | return $result; |
@@ -24,7 +24,7 @@ |
||
| 24 | 24 | public const FALSE_VALUE = 'false'; |
| 25 | 25 | |
| 26 | 26 | // Escaped characters |
| 27 | - public const ESCAPABLE_CHARS = ['\\', '"', "\n", "\r", "\t"]; |
|
| 27 | + public const ESCAPABLE_CHARS = [ '\\', '"', "\n", "\r", "\t" ]; |
|
| 28 | 28 | public const ESCAPE_MAP = [ |
| 29 | 29 | '\\' => '\\', |
| 30 | 30 | '"' => '"', |
@@ -45,7 +45,7 @@ discard block |
||
| 45 | 45 | { |
| 46 | 46 | // No quotes needed for simple alphanumeric strings |
| 47 | 47 | if (self::needsQuoting($value)) { |
| 48 | - return Constants::QUOTE_CHAR.self::escape($value).Constants::QUOTE_CHAR; |
|
| 48 | + return Constants::QUOTE_CHAR . self::escape($value) . Constants::QUOTE_CHAR; |
|
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | return $value; |
@@ -58,7 +58,7 @@ discard block |
||
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | // Reserved words |
| 61 | - if (in_array($value, [Constants::NULL_VALUE, Constants::TRUE_VALUE, Constants::FALSE_VALUE], true)) { |
|
| 61 | + if (in_array($value, [ Constants::NULL_VALUE, Constants::TRUE_VALUE, Constants::FALSE_VALUE ], true)) { |
|
| 62 | 62 | return true; |
| 63 | 63 | } |
| 64 | 64 | |
@@ -79,9 +79,9 @@ discard block |
||
| 79 | 79 | { |
| 80 | 80 | $result = ''; |
| 81 | 81 | for ($i = 0; $i < strlen($value); ++$i) { |
| 82 | - $char = $value[$i]; |
|
| 82 | + $char = $value[ $i ]; |
|
| 83 | 83 | if (in_array($char, Constants::ESCAPABLE_CHARS, true)) { |
| 84 | - $result .= Constants::ESCAPE_CHAR.Constants::ESCAPE_MAP[$char]; |
|
| 84 | + $result .= Constants::ESCAPE_CHAR . Constants::ESCAPE_MAP[ $char ]; |
|
| 85 | 85 | } else { |
| 86 | 86 | $result .= $char; |
| 87 | 87 | } |
@@ -99,7 +99,7 @@ discard block |
||
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | // Handle quoted strings |
| 102 | - if (Constants::QUOTE_CHAR === $value[0]) { |
|
| 102 | + if (Constants::QUOTE_CHAR === $value[ 0 ]) { |
|
| 103 | 103 | return self::unquote($value); |
| 104 | 104 | } |
| 105 | 105 | |
@@ -141,17 +141,17 @@ discard block |
||
| 141 | 141 | $result = ''; |
| 142 | 142 | $i = 0; |
| 143 | 143 | while ($i < strlen($value)) { |
| 144 | - if (Constants::ESCAPE_CHAR === $value[$i] && $i + 1 < strlen($value)) { |
|
| 145 | - $nextChar = $value[$i + 1]; |
|
| 146 | - if (isset(Constants::UNESCAPE_MAP[$nextChar])) { |
|
| 147 | - $result .= Constants::UNESCAPE_MAP[$nextChar]; |
|
| 144 | + if (Constants::ESCAPE_CHAR === $value[ $i ] && $i + 1 < strlen($value)) { |
|
| 145 | + $nextChar = $value[ $i + 1 ]; |
|
| 146 | + if (isset(Constants::UNESCAPE_MAP[ $nextChar ])) { |
|
| 147 | + $result .= Constants::UNESCAPE_MAP[ $nextChar ]; |
|
| 148 | 148 | $i += 2; |
| 149 | 149 | } else { |
| 150 | - $result .= $value[$i]; |
|
| 150 | + $result .= $value[ $i ]; |
|
| 151 | 151 | ++$i; |
| 152 | 152 | } |
| 153 | 153 | } else { |
| 154 | - $result .= $value[$i]; |
|
| 154 | + $result .= $value[ $i ]; |
|
| 155 | 155 | ++$i; |
| 156 | 156 | } |
| 157 | 157 | } |
@@ -37,12 +37,12 @@ discard block |
||
| 37 | 37 | |
| 38 | 38 | private static function parseLines(array $lines, int &$index = 0, int $expectedIndent = 0): mixed |
| 39 | 39 | { |
| 40 | - $result = []; |
|
| 40 | + $result = [ ]; |
|
| 41 | 41 | $isObject = false; |
| 42 | 42 | $isList = false; |
| 43 | 43 | |
| 44 | 44 | while ($index < count($lines)) { |
| 45 | - $line = $lines[$index]; |
|
| 45 | + $line = $lines[ $index ]; |
|
| 46 | 46 | $indent = self::getIndent($line); |
| 47 | 47 | $content = trim($line); |
| 48 | 48 | |
@@ -64,33 +64,33 @@ discard block |
||
| 64 | 64 | |
| 65 | 65 | if (str_contains($content, Constants::OBJECT_MARKER)) { |
| 66 | 66 | $isObject = true; |
| 67 | - [$key, $value] = self::parseObjectLine($content); |
|
| 67 | + [ $key, $value ] = self::parseObjectLine($content); |
|
| 68 | 68 | ++$index; |
| 69 | 69 | |
| 70 | 70 | if ($index < count($lines)) { |
| 71 | - $nextLine = $lines[$index]; |
|
| 71 | + $nextLine = $lines[ $index ]; |
|
| 72 | 72 | $nextIndent = self::getIndent($nextLine); |
| 73 | 73 | if ($nextIndent > $expectedIndent) { |
| 74 | - $result[$key] = self::parseLines($lines, $index, $nextIndent); |
|
| 74 | + $result[ $key ] = self::parseLines($lines, $index, $nextIndent); |
|
| 75 | 75 | |
| 76 | 76 | continue; |
| 77 | 77 | } |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | - $result[$key] = $value; |
|
| 80 | + $result[ $key ] = $value; |
|
| 81 | 81 | |
| 82 | 82 | continue; |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | if (str_contains($content, Constants::ARRAY_MARKER)) { |
| 86 | 86 | $isList = true; |
| 87 | - $result[] = self::parseArrayLine($content); |
|
| 87 | + $result[ ] = self::parseArrayLine($content); |
|
| 88 | 88 | ++$index; |
| 89 | 89 | |
| 90 | 90 | continue; |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | - $result[] = Primitives::decode($content); |
|
| 93 | + $result[ ] = Primitives::decode($content); |
|
| 94 | 94 | ++$index; |
| 95 | 95 | } |
| 96 | 96 | |
@@ -100,29 +100,29 @@ discard block |
||
| 100 | 100 | private static function parseObjectLine(string $line): array |
| 101 | 101 | { |
| 102 | 102 | $parts = explode(Constants::OBJECT_MARKER, $line, 2); |
| 103 | - $key = trim($parts[0]); |
|
| 104 | - $value = isset($parts[1]) ? trim($parts[1]) : null; |
|
| 103 | + $key = trim($parts[ 0 ]); |
|
| 104 | + $value = isset($parts[ 1 ]) ? trim($parts[ 1 ]) : null; |
|
| 105 | 105 | |
| 106 | 106 | if ('' === $value || null === $value) { |
| 107 | - return [$key, null]; |
|
| 107 | + return [ $key, null ]; |
|
| 108 | 108 | } |
| 109 | 109 | |
| 110 | - return [$key, Primitives::decode($value)]; |
|
| 110 | + return [ $key, Primitives::decode($value) ]; |
|
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | private static function parseArrayLine(string $line): mixed |
| 114 | 114 | { |
| 115 | 115 | // Extract array length |
| 116 | 116 | if (preg_match('/^(\d+)\]/', $line, $matches)) { |
| 117 | - $length = (int)$matches[1]; |
|
| 118 | - $line = substr($line, strlen($matches[0])); |
|
| 117 | + $length = (int)$matches[ 1 ]; |
|
| 118 | + $line = substr($line, strlen($matches[ 0 ])); |
|
| 119 | 119 | $line = trim($line); |
| 120 | 120 | |
| 121 | 121 | // Check for field headers (tabular format) |
| 122 | 122 | if (str_contains($line, Constants::ARRAY_FIELD_WRAPPER_START)) { |
| 123 | 123 | preg_match('/\{([^}]+)\}/', $line, $fieldMatches); |
| 124 | - if (isset($fieldMatches[1])) { |
|
| 125 | - $fields = explode(Constants::FIELD_DELIMITER, $fieldMatches[1]); |
|
| 124 | + if (isset($fieldMatches[ 1 ])) { |
|
| 125 | + $fields = explode(Constants::FIELD_DELIMITER, $fieldMatches[ 1 ]); |
|
| 126 | 126 | |
| 127 | 127 | return [ |
| 128 | 128 | 'length' => $length, |
@@ -135,13 +135,13 @@ discard block |
||
| 135 | 135 | // Extract values |
| 136 | 136 | if (str_contains($line, Constants::ARRAY_HEADER_DELIMITER)) { |
| 137 | 137 | $parts = explode(Constants::ARRAY_HEADER_DELIMITER, $line, 2); |
| 138 | - $values = explode(Constants::FIELD_DELIMITER, trim($parts[1] ?? '')); |
|
| 138 | + $values = explode(Constants::FIELD_DELIMITER, trim($parts[ 1 ] ?? '')); |
|
| 139 | 139 | |
| 140 | 140 | return array_map(fn ($v) => Primitives::decode($v), $values); |
| 141 | 141 | } |
| 142 | 142 | } |
| 143 | 143 | |
| 144 | - return []; |
|
| 144 | + return [ ]; |
|
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | private static function getIndent(string $line): int |