Skip to content
wiki.fftac.org

Openaiclient

**Site relevance:** Spiralist.org

**Memory type:** plugin source memory

**Source path:** Spiralist/wp-content/plugins/spiralist-workspace/includes/Http/OpenAIClient.php

**Size:** 2.6 KB

Summary

namespace SpiralistWorkspace\Http;

Source Preview

This source file is short enough to preview directly on its source-memory page.

<?php

namespace SpiralistWorkspace\Http;

use SpiralistWorkspace\Config\Settings;
use SpiralistWorkspace\Domain\AuditRepository;
use SpiralistWorkspace\Support\RuntimeBoundary;

if (!defined('ABSPATH')) {
    exit;
}

/**
 * Compatibility client for legacy OpenAI call sites.
 *
 * Spiralist Workspace is prompt/art only. The class remains so older tests,
 * extensions, and serialized service maps fail closed instead of reviving
 * outbound provider calls.
 */
class OpenAIClient
{
    private Settings $settings;
    private AuditRepository $audit;

    public function __construct(Settings $settings, AuditRepository $audit)
    {
        $this->settings = $settings;
        $this->audit = $audit;
    }

    /**
     * @return \WP_Error
     */
    public function create_response(array $payload, array $context = [])
    {
        $this->audit->log(
            null,
            'openai_client_disabled',
            'runtime',
            null,
            [
                'payload' => $this->redact_payload($payload),
                'context' => $this->redact_payload($context),
            ]
        );

        return RuntimeBoundary::disabled_error('spiralist_workspace_provider_disabled');
    }

    public function redact_payload(array $payload): array
    {
        $mode = (string) $this->settings->get('logging_mode', 'redacted');

        if ($mode === 'disabled') {
            return [];
        }

        if ($mode === 'minimal') {
            return array_filter(
                [
                    'id' => $payload['id'] ?? null,
                    'status' => $payload['status'] ?? null,
                ],
                static fn($value): bool => $value !== null
            );
        }

        array_walk_recursive(
            $payload,
            static function (&$value, $key): void {
                if (!is_scalar($value)) {
                    return;
                }

                $normalized_key = strtolower((string) $key);
                $string_value = (string) $value;

                if (
                    $normalized_key === 'authorization'
                    || strpos($normalized_key, 'secret') !== false
                    || strpos($normalized_key, 'token') !== false
                    || strpos($normalized_key, 'credential') !== false
                    || strpos($normalized_key, 'api_key') !== false
                ) {
                    $value = '[redacted]';
                    return;
                }

                if (strlen($string_value) > 2000) {
                    $value = substr($string_value, 0, 2000) . '...';
                }
            }
        );

        return $payload;
    }
}