-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata-machine-code.php
More file actions
361 lines (312 loc) · 12 KB
/
data-machine-code.php
File metadata and controls
361 lines (312 loc) · 12 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<?php
/**
* Plugin Name: Data Machine Code
* Plugin URI: https://github.com/Extra-Chill/data-machine-code
* Description: Developer tools extension for Data Machine. GitHub integration, workspace management, git operations, and code tools for WordPress AI agents.
* Version: 0.3.0
* Requires at least: 6.9
* Requires PHP: 8.2
* Requires Plugins: data-machine
* Author: Chris Huber, extrachill
* Author URI: https://chubes.net
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: data-machine-code
*/
if ( ! defined( 'WPINC' ) ) {
die;
}
define( 'DATAMACHINE_CODE_VERSION', '0.3.0' );
define( 'DATAMACHINE_CODE_PATH', plugin_dir_path( __FILE__ ) );
define( 'DATAMACHINE_CODE_URL', plugin_dir_url( __FILE__ ) );
// PSR-4 Autoloading.
require_once __DIR__ . '/vendor/autoload.php';
/**
* Bootstrap the plugin after all plugins are loaded.
*
* Data Machine core must be active — check at plugins_loaded time
* (not at plugin load time, since load order is alphabetical and
* data-machine-code loads before data-machine).
*/
function datamachine_code_bootstrap() {
if ( ! class_exists( 'DataMachine\Abilities\PermissionHelper' ) ) {
add_action( 'admin_notices', function () {
?>
<div class="notice notice-error">
<p><?php esc_html_e( 'Data Machine Code requires Data Machine core plugin to be installed and activated.', 'data-machine-code' ); ?></p>
</div>
<?php
} );
return;
}
// Load Abilities (they self-register).
new \DataMachineCode\Abilities\GitHubAbilities();
new \DataMachineCode\Abilities\WorkspaceAbilities();
// Load Handlers (they self-register).
new \DataMachineCode\Handlers\GitHub\GitHub();
// Register GitHub issue creation ability via SystemAbilities hook.
add_action( 'wp_abilities_api_init', 'datamachine_code_register_system_abilities' );
}
add_action( 'plugins_loaded', 'datamachine_code_bootstrap', 20 );
/**
* Register system-level abilities (GitHub issue creation).
*/
function datamachine_code_register_system_abilities() {
if ( ! function_exists( 'wp_register_ability' ) ) {
return;
}
wp_register_ability(
'datamachine/create-github-issue',
array(
'label' => 'Create GitHub Issue',
'description' => 'Create a new GitHub issue in a repository.',
'category' => 'datamachine',
'input_schema' => array(
'type' => 'object',
'required' => array( 'title' ),
'properties' => array(
'title' => array(
'type' => 'string',
'description' => 'Issue title.',
),
'repo' => array(
'type' => 'string',
'description' => 'Repository in owner/repo format.',
),
'body' => array(
'type' => 'string',
'description' => 'Issue body (supports GitHub Markdown).',
),
'labels' => array(
'type' => 'array',
'description' => 'Labels to apply.',
),
),
),
'output_schema' => array(
'type' => 'object',
'properties' => array(
'success' => array( 'type' => 'boolean' ),
'job_id' => array( 'type' => 'integer' ),
'error' => array( 'type' => 'string' ),
),
),
'execute_callback' => function ( array $input ) {
if ( ! class_exists( 'DataMachine\Engine\AI\System\TaskScheduler' ) ) {
return new \WP_Error( 'scheduler_unavailable', 'TaskScheduler not available.', array( 'status' => 500 ) );
}
$scheduler = new \DataMachine\Engine\AI\System\TaskScheduler();
$job_id = $scheduler->schedule( 'github_create_issue', $input );
if ( is_wp_error( $job_id ) ) {
return $job_id;
}
return $job_id;
},
'permission_callback' => function () {
return \DataMachine\Abilities\PermissionHelper::can_manage();
},
'meta' => array( 'show_in_rest' => false ),
)
);
}
/**
* Register WP-CLI commands after core is loaded.
*/
function datamachine_code_register_cli_commands() {
if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
return;
}
if ( ! class_exists( 'DataMachine\Cli\BaseCommand' ) ) {
return;
}
\WP_CLI::add_command( 'datamachine-code github', \DataMachineCode\Cli\Commands\GitHubCommand::class );
\WP_CLI::add_command( 'datamachine-code workspace', \DataMachineCode\Cli\Commands\WorkspaceCommand::class );
}
add_action( 'plugins_loaded', 'datamachine_code_register_cli_commands', 21 );
/**
* Register chat tools.
*
* Chat tools extend BaseTool from core and self-register via filters.
* Only load when Data Machine core's AI engine is available.
*/
function datamachine_code_load_chat_tools() {
if ( ! class_exists( 'DataMachine\Engine\AI\Tools\BaseTool' ) ) {
return;
}
new \DataMachineCode\Tools\GitHubIssueTool();
new \DataMachineCode\Tools\GitHubTools();
new \DataMachineCode\Tools\WorkspaceTools();
}
add_action( 'plugins_loaded', 'datamachine_code_load_chat_tools', 25 );
/**
* Register system tasks.
*/
add_filter( 'datamachine_tasks', function ( array $tasks ): array {
$tasks['github_create_issue'] = \DataMachineCode\Tasks\GitHubIssueTask::class;
return $tasks;
} );
/**
* Register code context memory file.
*
* Scaffolds contexts/code.md with GitHub, workspace, and git instructions.
* The file is written once — after that, the agent owns it.
*/
add_filter( 'datamachine_default_context_files', function ( array $defaults ): array {
$content = <<<'MD'
# Code Context
This context is active when you have developer tools available — GitHub integration, workspace file operations, and git workflows.
## GitHub Issue Creation
When using create_github_issue: include a clear title and detailed body with context, reproduction steps, and relevant log snippets. Use labels to categorize. Route to the most appropriate repo. Never create duplicates.
MD;
// Append available repos dynamically.
if ( class_exists( '\DataMachineCode\Abilities\GitHubAbilities' ) ) {
$repos = \DataMachineCode\Abilities\GitHubAbilities::getRegisteredRepos();
if ( ! empty( $repos ) ) {
$content .= "\n\nAvailable repositories for issue creation:\n";
foreach ( $repos as $entry ) {
$content .= '- ' . $entry['owner'] . '/' . $entry['repo'] . ' — ' . $entry['label'] . "\n";
}
}
}
$defaults['code'] = $content;
return $defaults;
} );
/**
* Register GitHub repos for issue creation.
*/
add_filter( 'datamachine_github_issue_repos', function ( array $repos ): array {
$default_repo = \DataMachineCode\Abilities\GitHubAbilities::getDefaultRepo();
if ( ! empty( $default_repo ) && str_contains( $default_repo, '/' ) ) {
$parts = explode( '/', $default_repo, 2 );
$repos[] = array(
'owner' => $parts[0],
'repo' => $parts[1],
'label' => 'Default (from settings)',
);
}
return $repos;
} );
/*
|--------------------------------------------------------------------------
| AGENTS.md — composable file registration
|--------------------------------------------------------------------------
| data-machine-code owns AGENTS.md as a coding-agent concern. The file is
| registered as composable in the MemoryFileRegistry, and sections are
| contributed by DM core, this plugin, and other extensions (mattic, etc.)
| via SectionRegistry.
|
| Convention copy at ABSPATH/AGENTS.md ensures coding agents (Claude Code,
| OpenCode, etc.) discover it at the expected location.
|
| Registered at plugins_loaded priority 22 (after DM core bootstrap at 20)
| to ensure MemoryFileRegistry and SectionRegistry are available.
*/
add_action( 'plugins_loaded', function () {
if ( ! class_exists( '\DataMachine\Engine\AI\MemoryFileRegistry' ) ) {
return;
}
\DataMachine\Engine\AI\MemoryFileRegistry::register( 'AGENTS.md', 5, array(
'layer' => \DataMachine\Engine\AI\MemoryFileRegistry::LAYER_SHARED,
'protected' => true,
'composable' => true,
'convention_path' => 'AGENTS.md',
'label' => 'Agent Instructions',
'description' => 'Auto-generated from registered sections. Regenerate via: wp datamachine agent compose AGENTS.md',
) );
if ( ! class_exists( '\DataMachine\Engine\AI\SectionRegistry' ) ) {
return;
}
$wp = datamachine_code_resolve_wp_cli_cmd();
// Data Machine — memory, automation, code, system.
\DataMachine\Engine\AI\SectionRegistry::register( 'AGENTS.md', 'datamachine', 10, function () use ( $wp ) {
return <<<MD
### Data Machine
Data Machine is your operating layer — memory, automation, and orchestration via WP-CLI.
**Memory:** Persistent files across sessions. Discover yours: `{$wp} datamachine agent paths`
- Read/write/search memory: `{$wp} datamachine agent read|write|search`
- Update MEMORY.md when you learn something persistent — read it first, append new info.
**Automation:** Self-scheduling workflows that run without human intervention.
- Flows: `{$wp} datamachine flow create|run|list` — scheduled or on-demand tasks
- Pipelines: `{$wp} datamachine pipeline create|list` — multi-step processing chains
- Jobs: `{$wp} datamachine jobs list|retry|summary` — monitor queued work
- Discover available step types: `{$wp} datamachine step-types list`
- Discover available handlers: `{$wp} datamachine handlers list`
**Code (data-machine-code):** Managed git workspace and GitHub integration.
- Workspace: `{$wp} datamachine-code workspace clone|read|write|edit|git`
- GitHub: `{$wp} datamachine-code github issues|pulls|repos|comment`
**System:** `{$wp} datamachine system health|prompts|run`
Use `--help` on any command to discover options and subcommands.
MD;
}, array(
'label' => 'Data Machine',
'description' => 'Memory, automation, workspace, and system operations.',
) );
// Abilities — WordPress Abilities API discovery.
\DataMachine\Engine\AI\SectionRegistry::register( 'AGENTS.md', 'abilities', 20, function () use ( $wp ) {
return <<<MD
### Abilities
WordPress Abilities are the universal tool surface. Plugins register abilities that are automatically available via WP-CLI, REST API, MCP, and chat. Discover what's available: `{$wp} help abilities`
The tool surface grows as plugins are installed — always discover before assuming what's available.
MD;
}, array(
'label' => 'Abilities',
'description' => 'WordPress Abilities API discovery.',
) );
// WordPress Source — direct reference material.
\DataMachine\Engine\AI\SectionRegistry::register( 'AGENTS.md', 'wordpress-source', 30, function () {
return <<<'MD'
### WordPress Source
Direct reference material — grep it as needed:
- `wp-content/plugins/` — all plugin source
- `wp-content/themes/` — all theme source
- `wp-includes/` — WordPress core (read-only)
MD;
}, array(
'label' => 'WordPress Source',
'description' => 'Pointers to WordPress source directories.',
) );
// Multisite — conditional, only on multisite installs.
if ( is_multisite() ) {
\DataMachine\Engine\AI\SectionRegistry::register( 'AGENTS.md', 'multisite', 40, function () use ( $wp ) {
return <<<MD
### Multisite
This is a WordPress multisite. Use `--url` to target specific sites:
```
{$wp} --url=site.example.com <command>
```
Without `--url`, commands default to the main site.
MD;
}, array(
'label' => 'Multisite',
'description' => 'Multisite-specific WP-CLI guidance.',
) );
}
}, 22 );
/**
* Resolve the WP-CLI command prefix for the current environment.
*
* Mirrors the {{WP_CLI_CMD}} substitution from wp-coding-agents setup scripts.
* Produces a command prefix like "wp --allow-root --path=/var/www/example.com"
* for server environments, or plain "wp" for local/Studio contexts.
*
* @since 0.3.0
*
* @return string WP-CLI command prefix.
*/
function datamachine_code_resolve_wp_cli_cmd(): string {
// Studio environments use a bare "wp" (Studio wraps it).
if ( defined( 'STARTER_STUDIO_PATH' ) ) {
return 'wp';
}
$parts = array( 'wp' );
// Server environments need --allow-root when running as root.
if ( function_exists( 'posix_geteuid' ) && 0 === posix_geteuid() ) {
$parts[] = '--allow-root';
}
// Add --path when ABSPATH isn't the default WordPress location.
$abspath = rtrim( ABSPATH, '/' );
if ( '/var/www/html' !== $abspath ) {
$parts[] = '--path=' . $abspath;
}
return implode( ' ', $parts );
}