adachsoft / ai-agent-context-message-injector
Generic conversation message injector decorator for adachsoft/ai-agent.
Package info
gitlab.com/a.adach/ai-agent-context-message-injector
pkg:composer/adachsoft/ai-agent-context-message-injector
Requires
- php: >=8.3
- adachsoft/ai-agent: ^0.14
- adachsoft/ai-conversation-model: ^0.2.0
- psr/clock: ^1.0
Requires (Dev)
- adachsoft/php-code-style: ^0.8.0
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^12.4
- rector/rector: ^2.9
- symplify/phpstan-rules: ^14.19
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Generic conversation message injector decorator for adachsoft/ai-agent.
Purpose
This library provides a generic ConversationContextStrategyInterface decorator that injects an additional ChatMessageDto from the shared adachsoft/ai-conversation-model package into the context produced by any inner strategy. The content is provided by a pluggable SPI (MessageContentProviderInterface), while the role and injection position are fully configurable via strategy params.
Key ideas:
- Decorator pattern
- SPI content provider
- No domain dependencies on specific tools or working memory
- Simple model: one injected message per decorator; N decorators = N injected messages
- Three injection positions:
start,before_last,end
Requirements
- PHP
>=8.3 adachsoft/ai-agent(^0.14)adachsoft/ai-conversation-model(^0.2.0)psr/clock(^1.0)
Installation
Install via Composer in a project that already uses adachsoft/ai-agent:
composer require adachsoft/ai-agent-context-message-injector
Configuration
The main entry point is the MessageInjectorConversationContextStrategy. It wraps an existing ConversationContextStrategyInterface implementation and injects a single message into its result.
Configuration parameters are defined in MessageInjectorStrategyParams:
injection_role(one ofsystem,developer,user, orassistant; default:"user")injection_position("start" | "before_last" | "end", default:"end")content_prefix(string, default: empty)content_suffix(string, default: empty)inject_when_empty(bool, default:false)empty_content_placeholder(string, default: empty)
Content for the injected message is provided by an implementation of MessageContentProviderInterface.
Example wiring (pseudo-code)
use AdachSoft\AiAgentContextMessageInjector\MessageInjectorConversationContextStrategy;
use AdachSoft\AiAgentContextMessageInjector\MessageInjectorStrategyParams;
use AdachSoft\AiAgentContextMessageInjector\Spi\MessageContentProviderInterface;
use AdachSoft\AiAgent\Spi\Conversation\ConversationContextStrategyInterface;
final class StaticSystemPromptProvider implements MessageContentProviderInterface
{
public function provide(string $conversationId): ?string
{
return 'You are a helpful assistant.';
}
}
$innerStrategy = /* some ConversationContextStrategyInterface implementation */;
$provider = new StaticSystemPromptProvider();
$strategy = new MessageInjectorConversationContextStrategy(
conversationContextStrategy: $innerStrategy,
messageContentProvider: $provider,
);
$params = [
MessageInjectorStrategyParams::INJECTION_ROLE => 'system',
MessageInjectorStrategyParams::INJECTION_POSITION => 'start',
];
$resultConversation = $strategy->transform(
$conversationId,
$fullConversation,
$params,
);
Testing
This package ships with a dedicated PHPUnit test suite under tests/. In a monorepo setup, include this directory in your root phpunit.xml configuration.