adachsoft/ai-tool-call
Composer 安装命令:
composer require adachsoft/ai-tool-call
包简介
Unified AI tool-calling abstraction for PHP 8.3 with a clean Public API and an extensible SPI for custom tools.
README 文档
README
Unified AI tool-calling abstraction for PHP 8.3 with a clean Public API and an extensible SPI for custom tools.
- Public API to list and call tools via a simple facade.
- SPI to implement your own tools (factories) in a framework-agnostic way.
- Zero framework assumptions, manual wiring via a small Builder.
- Uses adachsoft/collection for strict, immutable collections.
Requirements
- PHP ^8.3
- adachsoft/collection ^3.0
Installation
composer require adachsoft/ai-tool-call
Quick Start (SPI with factories)
use AdachSoft\AiToolCall\PublicApi\Builder\AiToolCallFacadeBuilder;
use AdachSoft\AiToolCall\PublicApi\Dto\ToolCallRequestDto;
use AdachSoft\AiToolCall\SPI\Collection\ConfigMap;
use AdachSoft\AiToolCall\SPI\Collection\KeyValueMap;
use AdachSoft\AiToolCall\SPI\Collection\TagsCollection;
use AdachSoft\AiToolCall\SPI\Dto\ToolCallRequestDto as SpiRequestDto;
use AdachSoft\AiToolCall\SPI\Dto\ToolCallResultDto as SpiResultDto;
use AdachSoft\AiToolCall\SPI\Dto\ToolDefinitionDto;
use AdachSoft\AiToolCall\SPI\Exception\ToolConfigurationException;
use AdachSoft\AiToolCall\SPI\Factory\ToolFactoryInterface;
use AdachSoft\AiToolCall\SPI\ToolInterface;
final class EchoTool implements ToolInterface
{
public static function getDefinition(): ToolDefinitionDto
{
return new ToolDefinitionDto(
name: 'echo',
description: 'Returns input as output',
parametersSchema: [
'type' => 'object',
'properties' => [
'text' => ['type' => 'string'],
],
],
tags: new TagsCollection(['demo']),
enabled: true,
);
}
public function callTool(SpiRequestDto $request): SpiResultDto
{
return new SpiResultDto(
'echo',
new KeyValueMap(['text' => $request->parameters->get('text')])
);
}
}
final class EchoToolFactory implements ToolFactoryInterface
{
public function getToolClass(): string
{
return EchoTool::class;
}
public function create(ConfigMap $config): ToolInterface
{
if ($config->has('invalid')) {
throw new ToolConfigurationException('Invalid configuration for EchoTool.');
}
return new EchoTool();
}
}
$facade = AiToolCallFacadeBuilder::new()
->withSpiFactories([new EchoToolFactory()])
->withToolConfigs([
'echo' => new ConfigMap([]),
])
->build();
// List available tools (includes built-in current_datetime and your SPI tools)
$tools = $facade->listAvailableTools();
foreach ($tools as $tool) {
echo $tool->name . PHP_EOL;
}
// Call a SPI tool
$result = $facade->callTool(new ToolCallRequestDto(
toolName: 'echo',
parameters: ['text' => 'hello'],
));
var_dump($result->result);
// Call the built-in current_datetime tool
$currentDateTimeResult = $facade->callTool(new ToolCallRequestDto(
toolName: 'current_datetime',
parameters: [],
));
var_dump($currentDateTimeResult->result);
Public API
- Facade:
AdachSoft\AiToolCall\PublicApi\AiToolCallFacadeInterfacecallTool(ToolCallRequestDto): ToolCallResultDtolistAvailableTools(): AvailableToolCollectionlistAvailableToolsByTags(array $tags): AvailableToolCollection
DTOs
ToolCallRequestDto(string $toolName, array $parameters)ToolCallResultDto(string $toolName, mixed $result)AvailableToolDto(string $name, string $description, array $parametersSchema, array $tags)AvailableToolCollection
SPI (Service Provider Interface)
Implement custom tools by fulfilling the SPI contracts.
AdachSoft\AiToolCall\SPI\ToolInterfacecallTool(ToolCallRequestDto): ToolCallResultDtostatic getDefinition(): ToolDefinitionDto
AdachSoft\AiToolCall\SPI\Factory\ToolFactoryInterfacegetToolClass(): stringcreate(ConfigMap $config): ToolInterface
ToolDefinitionDtoname: stringdescription: stringparametersSchema: array<string, mixed>tags: TagsCollectionenabled: bool
Collections used in SPI:
TagsCollection(immutable list of strings)KeyValueMap(immutable map string->mixed)ConfigMap(immutable map string->mixed)ToolCollection(immutable collection of SPI tools)ToolFactoryCollection(immutable collection of SPI tool factories)
Minimal tool example (SPI-only)
use AdachSoft\AiToolCall\SPI\ToolInterface;
use AdachSoft\AiToolCall\SPI\Collection\ToolCollection as SpiToolCollection;
use AdachSoft\AiToolCall\SPI\Dto\ToolDefinitionDto;
use AdachSoft\AiToolCall\SPI\Dto\ToolCallRequestDto as SpiRequest;
use AdachSoft\AiToolCall\SPI\Dto\ToolCallResultDto as SpiResult;
use AdachSoft\AiToolCall\SPI\Collection\TagsCollection;
use AdachSoft\AiToolCall\SPI\Collection\KeyValueMap;
final class EchoTool implements ToolInterface
{
public static function getDefinition(): ToolDefinitionDto
{
return new ToolDefinitionDto(
name: 'echo',
description: 'Returns input as output',
parametersSchema: ['type' => 'object', 'properties' => ['text' => ['type' => 'string']]],
tags: new TagsCollection(['demo']),
enabled: true,
);
}
public function callTool(SpiRequest $request): SpiResult
{
return new SpiResult('echo', new KeyValueMap(['text' => $request->parameters->get('text')]));
}
}
$tools = new SpiToolCollection([new EchoTool()]);
Tool discovery & registration
- No autoscan. The builder does not scan directories.
- Register tools via:
- SPI factories using
withSpiFactories()and optionalwithToolConfigs()for configuration maps, or - Direct SPI tools using
withSpiTools()when tools are already fully configured.
- SPI factories using
- The builder always registers
CurrentDateTimeToolCalleras a built-in domain tool by default.
Error handling
- Public API throws
PublicApi\Exception\ToolCallFailedExceptionon errors during tool execution. - Public API throws
PublicApi\Exception\ToolRegistrationExceptionwhen tool registration or configuration fails while building the facade (e.g. duplicate tool names or invalid factory/config types). - SPI exceptions:
SPI\Exception\InvalidToolCallExceptionSPI\Exception\ToolExecutionExceptionSPI\Exception\ToolConfigurationExceptionThese are mapped through the adapter when calling SPI tools through the Domain.
Versioning
- Semantic Versioning.
- Package version is controlled by Git tags (do not set version in composer.json).
License
MIT
adachsoft/ai-tool-call 适用场景与选型建议
adachsoft/ai-tool-call 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 79 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 10 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「adapter」 「collection」 「ai」 「spi」 「php8.3」 「tool-calling」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 adachsoft/ai-tool-call 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 adachsoft/ai-tool-call 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 adachsoft/ai-tool-call 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Adapter designed to add Framework Agnosticism for Caching within PHP Packages.
flysystem cache Adapter
Virtual Filesystem Storage Adapter for Laravel
A simple interface for composite Flysystem adapters
This package provides type-safe extension of the laravel collection, forcing a single type of object.
Traits to build collections of specific objects
统计信息
- 总下载量: 79
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 21
- 依赖项目数: 23
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-31