PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 2.1.1
Kubio AI Page Builder v2.1.1
2.9.1 2.9.0 2.8.6 2.8.5 2.8.4 2.8.3 2.8.2 2.8.1 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.9.0 2.0.0 2.1.1 2.1.2 2.1.3 2.2.0 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.5 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.6.2 2.6.3 2.6.5 2.6.6 2.6.7 2.7.0 2.7.1 2.7.2 2.7.3 2.8.0
kubio / lib / AI / admin-tab / partials / log.php
kubio / lib / AI / admin-tab / partials Last commit date
log.php 2 years ago main.php 2 years ago
log.php
263 lines
1 <?php
2
3 use IlluminateAgnostic\Arr\Support\Arr;
4 use Kubio\Core\Utils;
5 use Kubio\FileLog;
6
7 function kubio_ai_get_logs() {
8 $logs_file = FileLog::get_log_files( 'AI' );
9 $logs = array();
10 $total_usage = 0;
11 $total_credits = 0;
12 $error_usage = 0;
13 $sections = 0;
14 $sections_credits = 0;
15
16 $lines = array();
17
18 $tpc = Arr::get( $_REQUEST, 'tpc', 25 );
19 $tpc = intval( $tpc ) ? intval( $tpc ) : 25;
20
21 foreach ( $logs_file as $log_file ) {
22 $lines = array_merge( $lines, explode( "\n", file_get_contents( $log_file ) ) );
23 }
24
25 foreach ( $lines as $line ) {
26 $log = json_decode( $line, true );
27
28 if ( ! is_array( $log ) ) {
29 continue;
30 }
31
32 $code = Arr::get( $log, 'data.code', 'Unknown code' );
33
34 $completion = Arr::get( $log, 'data.completion', array() );
35 $completion = Utils::maybeJSONDecode( $completion );
36 $type = Arr::get( $log, 'type', 'unknown' );
37 $total_tokens = Arr::get( $log, 'data.usage.total_tokens', 0 );
38
39 $total_usage += $total_tokens;
40 $credits = $total_tokens / $tpc;
41 $credits = floor( $credits ) + 0.5 > $credits ? floor( $credits ) : ceil( $credits );
42
43 if ( $type === 'error' ) {
44
45 $completion = is_array( $completion ) ? Utils::humanizeArray( $completion, ' ' ) : $completion;
46 $error_usage += $total_tokens;
47 } else {
48 $total_credits += $credits;
49
50 if ( strpos( $code, '/generate-page-section' ) !== false || strpos( $code, '/rephrase-page-section' ) !== false ) {
51 $sections++;
52 $sections_credits += $credits;
53 }
54 }
55
56 $normalized_logs = array(
57 'start_time' => Arr::get( $log, 'data.start_time', 0 ),
58 'call_id' => Arr::get( $log, 'data.call_id', null ),
59 'type' => $type,
60 'ts' => Arr::get( $log, 'date', 0 ),
61 'date' => gmdate(
62 'Y-m-d H:i:s',
63 Arr::get( $log, 'date', 0 )
64 ),
65 'code' => $code,
66 'prompts' => Arr::get( $log, 'data.prompts', array() ),
67 'completion' => is_string( $completion ) ? $completion : json_encode( $completion, JSON_PRETTY_PRINT ),
68 'usage' => array(
69 'prompt' => Arr::get( $log, 'data.usage.prompt_tokens', 0 ),
70 'completion' => Arr::get( $log, 'data.usage.completion_tokens', 0 ),
71 'total' => $total_tokens,
72 'credits' => $type === 'error' ? 0 : $credits,
73 ),
74 );
75
76 $logs[] = $normalized_logs;
77
78 }
79
80 usort(
81 $logs,
82 function( $a, $b ) {
83 return $a['start_time'] - $b['start_time'];
84 }
85 );
86
87 $section_avg_credits = $sections ? round( $sections_credits / $sections, 1 ) : 0;
88
89 return array(
90 'logs' => $logs,
91 'total_usage' => $total_usage,
92 'error_usage' => $error_usage,
93 'total_credits' => $total_credits,
94 'tpc' => $tpc,
95 'sections' => $sections,
96 'sections_credits' => $sections_credits,
97 'section_avg_credits' => $section_avg_credits,
98 );
99 }
100
101 ?>
102 <script>
103 let kubioAILogs = [];
104 function kubioAILogDownload(index){
105
106 const log = kubioAILogs[index];
107
108 const promptsArray = log.prompts.map(p=>{
109 return [
110 `### Role: ${p.role.toUpperCase()}`,
111 "\n",
112 "```",
113 p.content.replaceAll("```","``"),
114 "```",
115 ].join("\n")
116 })
117
118 const content = [
119 `# Kubio AI Log`,
120 `Log type: ${log.type}`,
121 `Log code: ${log.code}`,
122 `\n`,
123 `Site: ${window.location}`,
124 `\n\n`,
125 `## PROMPTS`,
126 ...promptsArray,
127 `\n\n\n`,
128 `## COMPLETION`,
129 "```",
130 log.completion,
131 "```",
132 `\n\n\n`,
133 `## USAGE`,
134 `\n`,
135 "```",
136 JSON.stringify(log.usage || {},null,2),
137 "```",
138 ].join("\n");
139
140
141 const file = new File([content], 'log.md', {
142 type: 'text/plain',
143 });
144
145 const link = document.createElement('a')
146 const url = URL.createObjectURL(file)
147
148 link.href = url
149 link.download = file.name
150 document.body.appendChild(link)
151 link.click()
152
153 document.body.removeChild(link)
154 window.URL.revokeObjectURL(url)
155 }
156 </script>
157 <div class="tab-page">
158 <div class="kubio-logs-main-container">
159 <div class="kubio-admin-page-section kubio-ai-logs-section">
160 <div class="kubio-admin-page-section-content kubio-ai-logs">
161 <div class="kubio-ai-section-title">
162 <?php $kubio_ai_logs_data = kubio_ai_get_logs(); ?>
163 <h2>
164 Kubio AI Logs
165 <span>
166 <?php
167 printf(
168 'Tokens: %1$s ( %2$s OK | %3$s ERR )',
169 $kubio_ai_logs_data['total_usage'],
170 $kubio_ai_logs_data['total_usage'] - $kubio_ai_logs_data['error_usage'],
171 $kubio_ai_logs_data['error_usage']
172 );
173 ?>
174 </span>
175 <span>
176 <?php
177 printf(
178 '%s credits ( %s for sections ) | %s sections | ~%s credits/section',
179 $kubio_ai_logs_data['total_credits'],
180 $kubio_ai_logs_data['sections_credits'],
181 $kubio_ai_logs_data['sections'],
182 $kubio_ai_logs_data['section_avg_credits']
183 );
184 ?>
185 </span>
186
187 </h2>
188 <div class="align-items-end d-flex flex-column" style="gap:8px">
189 <a href="<?php echo esc_url( wp_nonce_url( add_query_arg( 'action', 'kubio_clear_ai_logs', admin_url( 'admin-ajax.php' ) ), 'kubio_clear_ai_logs' ) ); ?>" class="button-link-delete"><?php echo 'Clear logs'; ?></a>
190 <form method="get">
191 <input name="page" value="kubio-get-started" type="hidden"/>
192 <input name="tab" value="ai-logs" type="hidden"/>
193 <label>
194 Tokens/Credit
195 <input name="tpc" value="<?php echo esc_attr( $kubio_ai_logs_data['tpc'] ); ?>">
196 </label>
197 <button>Set</button>
198 </form>
199 </div>
200 </div>
201 <div class="kubio-ai-logs-wrapper">
202 <?php foreach ( $kubio_ai_logs_data['logs'] as $log_index => $log ) : ?>
203 <details>
204 <script>
205 kubioAILogs.push(<?php echo json_encode( $log ); ?>);
206 </script>
207 <summary>
208 <span class="kubio-ai-log-type kubio-ai-log-type--<?php echo esc_attr( $log['type'] ); ?>"><?php echo $log['type']; ?></span>
209 <span class="kubio-ai-log-code">
210 <?php echo $log['code']; ?>
211 </span>
212 <div class="kubio-ai-log-tags">
213 <?php if ( Arr::get( $log, 'usage.total', 0 ) && $log['type'] !== 'error' ) : ?>
214 <span class="kubio-ai-log-tokens"><?php echo Arr::get( $log, 'usage.credits', 0 ); ?> credits ( <?php echo Arr::get( $log, 'usage.total', 0 ); ?> tokens )</span>
215 <?php endif; ?>
216 <?php printf( $log['call_id'] ? '<span class="kubio-ai-log-call-id">%s</span>' : '', $log['call_id'] ); ?>
217 <span class="kubio-ai-log-date"><?php echo $log['date']; ?></span>
218 <button onclick="kubioAILogDownload('<?php echo esc_attr( $log_index ); ?>')" class="button button-primary">
219 <span class="dashicons dashicons-download " style="line-height: 30px;"></span>
220 </button>
221 </div>
222 </summary>
223 <div>
224 <div class="kubio-ai-log-prompts">
225 <?php foreach ( $log['prompts'] as $prompt ) : ?>
226 <details class="kubio-ai-log-prompt">
227 <summary class="kubio-ai-log-role">
228 <span><?php echo Arr::get( $prompt, 'role', 'Unknown role' ); ?></span>
229 </summary>
230 <div class="kubio-ai-log-content">
231 <?php
232 $prompt_content = Arr::get( $prompt, 'content', 'Unknown content' );
233
234 if ( ! is_string( $prompt_content ) && ! is_numeric( $prompt_content ) ) {
235 $prompt_content = json_encode( $prompt_content, JSON_PRETTY_PRINT );
236 }
237
238 printf( '<pre>%s</pre>', esc_html( $prompt_content ) );
239 ?>
240
241 </div>
242 </details>
243 <?php endforeach; ?>
244 </div>
245 <div class="kubio-ai-log-response">
246 <pre><?php echo esc_html( $log['completion'] ); ?></pre>
247 </div>
248 <div class="kubio-ai-log-usage">
249 <span class="kubio-ai-usage-prompt"><?php printf( 'Prompt tokens: %s', Arr::get( $log, 'usage.prompt', 0 ) ); ?></span>
250 <span class="kubio-ai-usage-completion"><?php printf( 'Completion tokens: %s', Arr::get( $log, 'usage.completion', 0 ) ); ?></span>
251 <span class="kubio-ai-usage-total"><?php printf( 'Total tokens: %s', Arr::get( $log, 'usage.total', 0 ) ); ?></span>
252 <span class="kubio-ai-usage-total"><?php printf( 'Credits: %s', Arr::get( $log, 'usage.credits', 0 ) ); ?></span>
253 </div>
254 </div>
255 </details>
256 <?php endforeach; ?>
257 </div>
258 </div>
259
260 </div>
261 </div>
262 </div>
263