This repository was archived by the owner on Jun 11, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathSystemPromptLoader.php
More file actions
94 lines (81 loc) · 2.65 KB
/
Copy pathSystemPromptLoader.php
File metadata and controls
94 lines (81 loc) · 2.65 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
<?php
namespace Drupal\dkan_ai_query\Service;
use Drupal\Core\Extension\ExtensionPathResolver;
/**
* Resolves the active system prompt and exposes its version identifier.
*
* Source-of-truth lives in versioned markdown files at
* `prompts/query_system_prompt.v{N}.md`. Splitting the resolution out of the
* EventSubscriber means both the runtime override path and the artifact
* provenance path (which records `prompt_version`) read the same metadata.
*/
class SystemPromptLoader {
public const DEFAULT_VERSION = 'v10';
/**
* The extension path resolver.
*/
protected ExtensionPathResolver $pathResolver;
/**
* Per-instance cache of loaded prompt content keyed by version.
*
* @var array<string, string>
*/
protected array $cache = [];
/**
* Transient override of the active version (eval / debugging only).
*/
protected ?string $override = NULL;
public function __construct(ExtensionPathResolver $path_resolver) {
$this->pathResolver = $path_resolver;
}
/**
* Override the active prompt version for the lifetime of this process.
*
* Intended for the eval harness and ad-hoc debugging — long-lived requests
* should not call this. Pass NULL to clear.
*/
public function setOverride(?string $version): void {
$this->override = $version === NULL || $version === '' ? NULL : ltrim($version, 'v');
}
/**
* Load the prompt body for a given version, or NULL when the file is missing.
*/
public function load(string $version = self::DEFAULT_VERSION): ?string {
if (array_key_exists($version, $this->cache)) {
return $this->cache[$version];
}
$path = $this->resolvePath($version);
if ($path === NULL || !is_readable($path)) {
$this->cache[$version] = NULL;
return NULL;
}
$contents = file_get_contents($path);
$this->cache[$version] = $contents === FALSE ? NULL : trim($contents);
return $this->cache[$version];
}
/**
* Return the active prompt version identifier.
*
* Recorded against every persisted message so eval runs and audit
* tooling can correlate behavior with prompt changes.
*/
public function activeVersion(): string {
if ($this->override !== NULL) {
return 'v' . $this->override;
}
return self::DEFAULT_VERSION;
}
/**
* Resolve the absolute path of a prompt version's markdown file.
*/
protected function resolvePath(string $version): ?string {
$version = ltrim($version, 'v');
try {
$modulePath = $this->pathResolver->getPath('module', 'dkan_ai_query');
}
catch (\Throwable) {
return NULL;
}
return $modulePath . "/prompts/query_system_prompt.v{$version}.md";
}
}