Repository navigation
Expand file tree
/
Copy pathSkillInstaller.php
More file actions
114 lines (93 loc) · 3.28 KB
/
Copy pathSkillInstaller.php
File metadata and controls
114 lines (93 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
49
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
<?php
declare(strict_types=1);
namespace NyonCode\WireBoost\Install;
use NyonCode\WireBoost\Support\WirePackages;
use Symfony\Component\Finder\Finder;
/**
* Copies the Agent Skill modules (each a directory containing a SKILL.md) into
* an agent's skills directory — those shipped with the package, plus any the
* project keeps in `.ai/skills`, which is the same extension point the
* guidelines have in `.ai/guidelines`.
*
* A project directory of the same name wins, one file at a time: it is read
* after the shipped one and overwrites what it names, so a project can replace
* a single SKILL.md without restating the rest of the module.
*/
class SkillInstaller
{
/**
* @param array<int, string> $sources Skill directories, least specific first.
*/
public function __construct(private array $sources, private WirePackages $packages) {}
public static function default(): self
{
return new self([
dirname(__DIR__, 2).'/resources/boost/skills',
base_path('.ai/skills'),
], app(WirePackages::class));
}
/**
* Install every skill module whose package this application has into the
* target directory.
*
* @return array<int, string> the installed skill names
*/
public function install(string $targetDirectory): array
{
$sources = array_values(array_filter($this->sources, 'is_dir'));
if ($sources === []) {
return [];
}
$installed = [];
foreach ($this->modules($sources) as $name => $directories) {
if (! $this->packages->shipsResource($name)) {
continue;
}
foreach ($directories as $directory) {
$this->copy($directory, $targetDirectory.'/'.$name);
}
$installed[] = $name;
}
return $installed;
}
/**
* Skill name => the directories that contribute to it, least specific first.
*
* Grouped explicitly rather than by handing every source to one Finder: two
* directories of the same name sort equal there, and which of them is copied
* last — the one that wins — then rests on iteration order rather than on the
* order the caller asked for.
*
* @param array<int, string> $sources
* @return array<string, array<int, string>>
*/
private function modules(array $sources): array
{
$modules = [];
foreach ($sources as $source) {
foreach (Finder::create()->directories()->in($source)->depth(0)->sortByName() as $skill) {
$modules[$skill->getFilename()][] = $skill->getPathname();
}
}
ksort($modules);
return $modules;
}
/**
* Copy one skill module, creating what it needs on the way.
*/
private function copy(string $from, string $to): void
{
$this->ensureDirectory($to);
foreach (Finder::create()->files()->in($from) as $file) {
$target = $to.'/'.$file->getRelativePathname();
$this->ensureDirectory(dirname($target));
copy($file->getPathname(), $target);
}
}
private function ensureDirectory(string $directory): void
{
if (! is_dir($directory)) {
mkdir($directory, 0755, true);
}
}
}