PacketFlow.Intent.Plugin (packetflow v0.1.0)

Plugin system for intent extensions

This module provides a framework for creating custom intent types, routing logic, validation logic, transformation logic, and composition patterns.

Summary

Functions

Create a custom composition pattern

Create a custom intent type

Create a custom routing strategy

Define an intent plugin with custom behavior

Get all registered intent plugins

Get plugins by type

Register an intent plugin with the system

Unregister an intent plugin from the system

Functions

defcustom_composition_pattern(name, list)

(macro)

Create a custom composition pattern

Example

defcustom_composition_pattern RetryComposition do
  @pattern_type :retry
  @max_retries 3

  def compose(intents, opts) do
    max_retries = Map.get(opts, :max_retries, @max_retries)
    compose_with_retry(intents, max_retries)
  end

  defp compose_with_retry(intents, retries_left) do
    case PacketFlow.Intent.Dynamic.compose_intents(intents, :sequential) do
      {:ok, results} ->
        {:ok, results}
      {:error, _reason} when retries_left > 0 ->
        compose_with_retry(intents, retries_left - 1)
      {:error, reason} ->
        {:error, reason}
    end
  end
end

defcustom_intent_type(name, list)

(macro)

Create a custom intent type

Example

defcustom_intent_type FileOperationIntent do
  @intent_type :file_operation
  @capabilities [FileCap.read, FileCap.write]

  def new(operation, path, user_id) do
    %{
      type: @intent_type,
      operation: operation,
      path: path,
      user_id: user_id,
      capabilities: [FileCap.read(path), FileCap.write(path)]
    }
  end

  def validate(intent) do
    case intent.operation do
      :read -> validate_read_operation(intent)
      :write -> validate_write_operation(intent)
      _ -> {:error, :unsupported_operation}
    end
  end

  defp validate_read_operation(intent) do
    case File.exists?(intent.path) do
      true -> {:ok, intent}
      false -> {:error, :file_not_found}
    end
  end

  defp validate_write_operation(intent) do
    case File.writable?(Path.dirname(intent.path)) do
      true -> {:ok, intent}
      false -> {:error, :directory_not_writable}
    end
  end
end

defcustom_routing_strategy(name, list)

(macro)

Create a custom routing strategy

Example

defcustom_routing_strategy LoadBalancedRouting do
  @strategy_type :load_balanced
  @targets [:reactor1, :reactor2, :reactor3]

  def route(intent, available_targets) do
    # Load balancing logic
    target = select_target(available_targets)
    {:ok, target}
  end

  defp select_target(targets) do
    # Simple round-robin selection
    Enum.random(targets)
  end
end

defintent_plugin(name, list)

(macro)

Define an intent plugin with custom behavior

Example

defintent_plugin MyCustomIntentPlugin do
  @plugin_type :intent_validation
  @priority 10

  def validate(intent) do
    case intent.type do
      "FileReadIntent" ->
        validate_file_read(intent)
      "UserIntent" ->
        validate_user_intent(intent)
      _ ->
        {:ok, intent}
    end
  end

  def transform(intent) do
    # Transform intent as needed
    {:ok, intent}
  end

  def route(intent, targets) do
    # Custom routing logic
    {:ok, targets}
  end

  def compose(intents, strategy) do
    # Custom composition logic
    {:ok, intents}
  end

  defp validate_file_read(intent) do
    case intent.payload.path do
      path when is_binary(path) and byte_size(path) > 0 ->
        {:ok, intent}
      _ ->
        {:error, :invalid_file_path}
    end
  end

  defp validate_user_intent(intent) do
    case intent.payload.user_id do
      user_id when is_binary(user_id) and byte_size(user_id) > 0 ->
        {:ok, intent}
      _ ->
        {:error, :invalid_user_id}
    end
  end
end

get_plugins()

Get all registered intent plugins

Example

plugins = PacketFlow.Intent.Plugin.get_plugins()

get_plugins_by_type(plugin_type)

Get plugins by type

Example

validation_plugins = PacketFlow.Intent.Plugin.get_plugins_by_type(:intent_validation)
transformation_plugins = PacketFlow.Intent.Plugin.get_plugins_by_type(:intent_transformation)

register_plugin(plugin_module)

Register an intent plugin with the system

Example

PacketFlow.Intent.Plugin.register_plugin(MyCustomIntentPlugin)

unregister_plugin(plugin_module)

Unregister an intent plugin from the system

Example

PacketFlow.Intent.Plugin.unregister_plugin(MyCustomIntentPlugin)