Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions includes/class-openclawp-agent-files-store.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* author these documents exclusively through openclaWP's own surface
* (see {@see OpenclaWP_Agent_Files_Admin}).
*
* TODO (follow-up PR): wire the file contents into the runtime system prompt
* via `OpenclaWP_Canonical_Chat_Handler`. This PR ships the CPT + admin UI
* only so admins can start authoring files while the runtime side bakes.
* Runtime consumer: {@see OpenclaWP_Runner} (see `build_turn_runner()` around
* the `using_system_instruction()` call) appends `for_agent_as_text()` to the
* agent's static description on every turn.
*
* @package OpenclaWP
* @since 0.11.0
Expand Down Expand Up @@ -106,6 +106,33 @@ public static function for_agent( string $agent_slug ): array {
return $out;
}

/**
* Return the files targeted at a given agent concatenated as plain text,
* ready to append to a system prompt.
*
* Each file is emitted as:
*
* === {post_title} ===
* {post_content}
*
* Sections are joined with a blank line. Files with empty bodies are
* skipped. Returns an empty string when no files apply — callers can
* treat the result as a safe no-op append.
*
* @param string $agent_slug Agent slug, e.g. `openclawp-example`.
*/
public static function for_agent_as_text( string $agent_slug ): string {
$sections = array();
foreach ( self::for_agent( $agent_slug ) as $post ) {
$body = (string) $post->post_content;
if ( '' === trim( $body ) ) {
continue;
}
$sections[] = '=== ' . $post->post_title . " ===\n" . $body;
}
return implode( "\n\n", $sections );
}

/**
* Fetch a file by its title (file name slot, e.g. "AGENTS.md").
*
Expand Down
24 changes: 24 additions & 0 deletions includes/class-openclawp-runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,30 @@ private static function build_turn_runner( WP_Agent $agent, array $session, stri
// WP_AI_Client_Prompt_Builder forwards these via __call, so
// method_exists() returns false for them — call directly.
$description = $agent_obj->get_description();

// Append admin-authored Agent files (CPT shipped in PR #71).
// When the CPT is empty for this agent, this is a no-op.
if ( class_exists( 'OpenclaWP_Agent_Files_Store' ) ) {
$agent_files_text = OpenclaWP_Agent_Files_Store::for_agent_as_text( $agent_obj->get_slug() );
if ( '' !== $agent_files_text ) {
$description = ( '' !== $description )
? $description . "\n\n" . $agent_files_text
: $agent_files_text;
}
}

/**
* Filter the system instruction the runner passes to the AI
* client for this turn. Receives the post-agent-files
* description + the agent slug.
*
* @since 0.11.0
*
* @param string $description Fully assembled system instruction.
* @param string $agent_slug Agent that's about to run.
*/
$description = (string) apply_filters( 'openclawp_runner_system_instruction', $description, $agent_obj->get_slug() );

if ( '' !== $description ) {
$builder = $builder->using_system_instruction( $description );
}
Expand Down
Loading