Class: RuboCop::Cop::Prompt::MaxTokens
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Prompt::MaxTokens
- Defined in:
- lib/rubocop/cop/prompt/max_tokens.rb
Overview
Checks that documentation text in prompt-related code doesn’t exceed the maximum token limit.
This cop identifies code in classes, modules, or methods with “prompt” in their names and calculates the token count for any string literals or heredoc content using tiktoken_ruby. By default, it warns when the content exceeds 4000 tokens.
Constant Summary collapse
- MSG =
"Documentation text exceeds maximum token limit (%<actual>d > %<max>d tokens)"- DEFAULT_MAX_TOKENS =
Default maximum token count
4000
Instance Method Summary collapse
Instance Method Details
#on_dstr(node) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/rubocop/cop/prompt/max_tokens.rb', line 46 def on_dstr(node) return unless in_prompt_context?(node) # Handle heredoc content content = node.children.filter_map do |child| child.children[0] if child.type == :str end.join return if content.strip.empty? check_token_count(node, content) end |
#on_str(node) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/rubocop/cop/prompt/max_tokens.rb', line 37 def on_str(node) return unless in_prompt_context?(node) content = node.children[0] return if content.nil? || content.strip.empty? check_token_count(node, content) end |