Class: PromptObjects::Universal::ModifyPrimitive
- Inherits:
-
Primitive
- Object
- Capability
- Primitive
- PromptObjects::Universal::ModifyPrimitive
- Defined in:
- lib/prompt_objects/universal/modify_primitive.rb
Overview
Universal capability to modify existing primitives. Allows POs to fix or improve their primitives.
Constant Summary collapse
- STDLIB_PRIMITIVES =
Stdlib primitives cannot be modified
%w[read_file list_files write_file http_get think].freeze
Instance Attribute Summary
Attributes inherited from Capability
Instance Method Summary collapse
Methods inherited from Primitive
Methods inherited from Capability
#descriptor, execution_policy, execution_policy_definition, #execution_policy_definition, #execution_policy_signature, #initialize, #resolved_execution_policy
Constructor Details
This class inherits a constructor from PromptObjects::Primitive
Instance Method Details
#description ⇒ Object
17 18 19 |
# File 'lib/prompt_objects/universal/modify_primitive.rb', line 17 def description "Modify an existing primitive's code. Only custom (environment) primitives can be modified, not stdlib." end |
#name ⇒ Object
13 14 15 |
# File 'lib/prompt_objects/universal/modify_primitive.rb', line 13 def name "modify_primitive" end |
#parameters ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/prompt_objects/universal/modify_primitive.rb', line 21 def parameters { type: "object", properties: { name: { type: "string", description: "Name of the primitive to modify" }, code: { type: "string", description: "New Ruby code for the primitive's receive method" }, description: { type: "string", description: "Optional: Update the description" }, parameters_schema: { type: "object", description: "Optional: Update the parameters schema" }, reason: { type: "string", description: "Brief explanation of why this change is needed (for commit message)" } }, required: ["name", "code"] } end |
#receive(message, context:) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/prompt_objects/universal/modify_primitive.rb', line 50 def receive(, context:) prim_name = [:name] || ["name"] new_code = [:code] || ["code"] new_description = [:description] || ["description"] new_params_schema = [:parameters_schema] || ["parameters_schema"] reason = [:reason] || ["reason"] || "Updated primitive" # Find the primitive primitive = context.env.registry.get(prim_name) unless primitive return "Error: Primitive '#{prim_name}' not found." end unless primitive.is_a?(Primitive) return "Error: '#{prim_name}' is not a primitive." end # Check if it's a stdlib primitive if STDLIB_PRIMITIVES.include?(prim_name) return "Error: Cannot modify stdlib primitive '#{prim_name}'. Stdlib primitives are built into the framework." end # Check if it's a universal capability if universal_primitive?(primitive) return "Error: Cannot modify universal capability '#{prim_name}'." end # Find the primitive file path = File.join(context.env.primitives_dir, "#{prim_name}.rb") unless File.exist?(path) return "Error: Cannot find primitive file at #{path}. Only custom primitives can be modified." end # Validate code syntax syntax_error = validate_syntax(new_code) return "Error: Invalid Ruby syntax - #{syntax_error}" if syntax_error # Get current values if not provided description = new_description || primitive.description params_schema = new_params_schema || primitive.parameters # Generate updated Ruby class class_name = prim_name.split("_").map(&:capitalize).join ruby_content = generate_ruby_class(class_name, prim_name, description, params_schema, new_code) # Write the updated file File.write(path, ruby_content, encoding: "UTF-8") # Reload the primitive begin # Remove old constant to allow re-definition if PromptObjects::Primitives.const_defined?(class_name) PromptObjects::Primitives.send(:remove_const, class_name) end load(path) klass = PromptObjects::Primitives.const_get(class_name) new_instance = klass.new # Re-register with the new instance context.env.registry.register(new_instance) # Auto-commit if in environment mode if context.env.environment? && context.env.auto_commit = "Modified primitive '#{prim_name}': #{reason}" context.env.save() end "Modified primitive '#{prim_name}'. Changes saved to #{path}." rescue SyntaxError => e "Error: Invalid Ruby syntax in generated code - #{e.}" rescue StandardError => e "Error reloading primitive: #{e.}" end end |