-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata-machine-code.php
More file actions
264 lines (233 loc) · 8.9 KB
/
data-machine-code.php
File metadata and controls
264 lines (233 loc) · 8.9 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
<?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.2.2
* 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.2.2' );
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;
}
// Core DM memory section — memory file discovery and guidance.
\DataMachine\Engine\AI\SectionRegistry::register( 'AGENTS.md', 'datamachine-memory', 10, function () {
return "### Data Machine\n\nData Machine manages your persistent memory. Discover your files: `wp datamachine agent paths`\n\nUpdate MEMORY.md when you learn something persistent — read it first, append new info.";
}, array(
'label' => 'Data Machine Memory',
'description' => 'Memory file discovery and guidance.',
) );
// Workspace section — developer tools and git workspace.
\DataMachine\Engine\AI\SectionRegistry::register( 'AGENTS.md', 'datamachine-workspace', 30, function () {
return "### Workspace\n\nAll coding happens in the Data Machine workspace — a managed git sandbox with full read/write access. Discoverable: `wp help datamachine-code workspace`";
}, array(
'label' => 'Workspace',
'description' => 'Developer workspace discovery.',
) );
}, 22 );