Files
prautoblogger/includes/models/class-generation-log.php
peptiderepo c723af927b feat(prompts): versioned prompt registry, v1 seed, pinning + stamping
Pipeline v2 Phase 1, commit 2/4. Prompt_Registry (read) +
Prompt_Registry_Writer (immutable create/activate/seed) over the new
prompts table; canonical v1 bodies extracted verbatim into
Prompt_Defaults(+Editorial) — single source for BOTH the seed migration
(Migrate_Prompt_Seed_V0180, flag-gated, self-healing) and the in-code
fallback, so a missing table renders byte-identical prompts and never
fatals. Call sites swapped: Content_Prompts, Analysis_Prompts,
Chief_Editor, LLM_Research_Provider. Includes the image-composer seam
keys image.rewriter_system + image.style_template (composer PR consumes,
must not add its own prompt storage).

Pinning: Cost_Tracker::set_run_id() registers the process run context,
ensures the runs ledger row, and pins active versions once per run;
log_api_call/log_image_generation stamp prompt_version (run pins) +
agent_role (stage map) on every generation_log row. Single-pass keeps
stage 'draft' and stamps content.single_pass explicitly.

Under-300 refactors: Content_Generator's four duplicated span/log blocks
consolidated into execute_stage() (301 -> 224 lines, behavior
identical); get_avg_tokens_for_stages body moved to Cost_Reporter with a
delegating wrapper kept for API compatibility.

Tests: byte-identical fallback renders (single_pass/outline/polish/
editor.system), token filling, key coverage incl. stage-map integrity.

Agent-Session: pipeline-v2-phase1-engineer-20260612
2026-06-11 16:42:56 +07:00

107 lines
3.8 KiB
PHP

<?php
declare(strict_types=1);
/**
* phpcs:ignore WordPress.Files.FileName.InvalidClassFileName -- class naming convention differs from WordPress standard
*
* Value object for an API call log entry (cost tracking).
*
* Triggered by: Cost_Tracker creates these; Metrics_Page displays them.
* Dependencies: None — pure data object.
*
* @see core/class-cost-tracker.php — Writes these to prab_generation_log table.
* @see admin/class-metrics-page.php — Reads and displays these.
*/
class PRAutoBlogger_Generation_Log {
private int $id;
private ?int $post_id;
private ?string $run_id;
private string $stage;
private string $provider;
private string $model;
private int $prompt_tokens;
private int $completion_tokens;
private float $estimated_cost;
private ?string $request_json;
private string $response_status;
private ?string $error_message;
private ?string $agent_role;
private ?string $prompt_version;
private string $created_at;
/**
* @param array<string, mixed> $data
*/
public function __construct( array $data ) {
$this->id = (int) ( $data['id'] ?? 0 );
$this->post_id = isset( $data['post_id'] ) ? (int) $data['post_id'] : null;
$this->run_id = $data['run_id'] ?? null;
$this->stage = $data['stage'] ?? '';
$this->provider = $data['provider'] ?? '';
$this->model = $data['model'] ?? '';
$this->prompt_tokens = (int) ( $data['prompt_tokens'] ?? 0 );
$this->completion_tokens = (int) ( $data['completion_tokens'] ?? 0 );
$this->estimated_cost = (float) ( $data['estimated_cost'] ?? 0.0 );
$this->request_json = $data['request_json'] ?? null;
$this->response_status = $data['response_status'] ?? 'success';
$this->error_message = $data['error_message'] ?? null;
$this->agent_role = $data['agent_role'] ?? null;
$this->prompt_version = isset( $data['prompt_version'] ) ? (string) $data['prompt_version'] : null;
$this->created_at = $data['created_at'] ?? current_time( 'mysql' );
}
public function get_id(): int {
return $this->id; }
public function get_post_id(): ?int {
return $this->post_id; }
public function get_run_id(): ?string {
return $this->run_id; }
public function get_stage(): string {
return $this->stage; }
public function get_provider(): string {
return $this->provider; }
public function get_model(): string {
return $this->model; }
public function get_prompt_tokens(): int {
return $this->prompt_tokens; }
public function get_completion_tokens(): int {
return $this->completion_tokens; }
public function get_estimated_cost(): float {
return $this->estimated_cost; }
public function get_request_json(): ?string {
return $this->request_json; }
public function get_response_status(): string {
return $this->response_status; }
public function get_error_message(): ?string {
return $this->error_message; }
public function get_agent_role(): ?string {
return $this->agent_role; }
public function get_prompt_version(): ?string {
return $this->prompt_version; }
public function get_created_at(): string {
return $this->created_at; }
/**
* @return array<string, mixed>
*/
public function to_db_row(): array {
return array(
'post_id' => $this->post_id,
'run_id' => $this->run_id,
'stage' => $this->stage,
'provider' => $this->provider,
'model' => $this->model,
'prompt_tokens' => $this->prompt_tokens,
'completion_tokens' => $this->completion_tokens,
'estimated_cost' => $this->estimated_cost,
'request_json' => $this->request_json,
'response_status' => $this->response_status,
'error_message' => $this->error_message,
'agent_role' => $this->agent_role,
'prompt_version' => $this->prompt_version,
'created_at' => $this->created_at,
);
}
}