load(); } /** * Load settings from options table with static caching * * @return void */ private function load(): void { // Check static cache first (fastest) if ( isset( self::$cache['data'] ) ) { $this->data = self::$cache['data']; return; } // Try options table (primary source) $option_data = get_option( 'aisentic_settings', null ); if ( is_array( $option_data ) && ! empty( $option_data ) ) { // Merge saved settings over the defaults. // Why: a site that saved before a key was added still gets that key's default. $this->data = $this->normalize( $this->deep_merge( $this->getDefaults(), $option_data ) ); self::$cache['data'] = $this->data; return; } // Fall back to defaults $this->data = $this->getDefaults(); self::$cache['data'] = $this->data; } /** * Get all settings * * @return array */ public function getAll(): array { return $this->data; } /** * Get a setting value by path * * @param string $path Dot-separated path (e.g., 'vectorDBProviders.pinecone.key'). * @param mixed $default Default value if not found. * @return mixed */ public function get( string $path, $default = null ) { $keys = explode( '.', $path ); $value = $this->data; foreach ( $keys as $key ) { if ( ! isset( $value[ $key ] ) ) { return $default; } $value = $value[ $key ]; } return $value; } /** * Save settings to database * * @param array $data Settings data. * @return bool */ public function save( array $data ): bool { // Clean on save too, so the saved data matches what the next load reads. if ( isset( $data['voice'] ) && is_array( $data['voice'] ) ) { $data['voice'] = $this->clean_voice( $data['voice'] ); } $this->data = $data; // Clear static cache self::$cache = []; // Save to options table (primary storage) return update_option( 'aisentic_settings', $data, false ); } /** * Set a specific setting value by path * * @param string $path Dot-separated path. * @param mixed $value Value to set. * @return void */ public function set( string $path, $value ): void { $keys = explode( '.', $path ); $data = &$this->data; foreach ( $keys as $i => $key ) { if ( $i === count( $keys ) - 1 ) { $data[ $key ] = $value; } else { if ( ! isset( $data[ $key ] ) || ! is_array( $data[ $key ] ) ) { $data[ $key ] = []; } $data = &$data[ $key ]; } } } /** * Deep-merge partial data onto the currently loaded settings and save. * * Callers that only own a slice of the settings tree (e.g. one admin UI tab) * must not wipe out sibling top-level keys they don't know about — save() * itself is a plain replace, so route partial saves through here instead. * * @param array $data Partial settings data. * @return bool */ public function merge_and_save( array $data ): bool { return $this->save( $this->deep_merge( $this->data, $data ) ); } /** * Recursively merge $overrides onto $base. * * Associative (keyed) sub-arrays are merged key-by-key so omitted keys survive. * Sequential (list) sub-arrays and scalars are replaced wholesale — index-merging * a list would leave stale trailing elements from the old value behind. * * @param array $base Existing data. * @param array $overrides Partial data to layer on top. * @return array */ private function deep_merge( array $base, array $overrides ): array { foreach ( $overrides as $key => $value ) { if ( is_array( $value ) && $this->is_assoc( $value ) && isset( $base[ $key ] ) && is_array( $base[ $key ] ) && $this->is_assoc( $base[ $key ] ) ) { $base[ $key ] = $this->deep_merge( $base[ $key ], $value ); } else { $base[ $key ] = $value; } } return $base; } /** * Whether an array is associative (keyed) rather than a sequential list. * * @param array $arr Array to check. * @return bool */ private function is_assoc( array $arr ): bool { return [] !== $arr && array_keys( $arr ) !== range( 0, count( $arr ) - 1 ); } /** * Normalize legacy settings shapes to the canonical structure. * * Older data and hand-crafted JSON imports stored the Google/OpenAI keys * under 'key'; the canonical name is now 'apiKey' (see getDefaults() and the * settings UI). Copy any legacy value forward on read so existing sites keep * working — and survive re-export/re-import — without a DB migration. * * @param array $data Raw settings data from storage. * @return array Normalized settings data. */ private function normalize( array $data ): array { foreach ( [ 'google', 'openai' ] as $provider ) { if ( ! isset( $data['llmProviders'][ $provider ] ) || ! is_array( $data['llmProviders'][ $provider ] ) ) { continue; } $config = $data['llmProviders'][ $provider ]; if ( empty( $config['apiKey'] ) && ! empty( $config['key'] ) ) { $data['llmProviders'][ $provider ]['apiKey'] = $config['key']; } } if ( isset( $data['voice'] ) && is_array( $data['voice'] ) ) { $data['voice'] = $this->clean_voice( $data['voice'] ); } return $data; } /** * Make the Voice tab's values safe to act on. * * - Removes retired keys. * - Turns switch values like "1" and "0" into real booleans. Why: the * string "0" is truthy, so voice would stay on after the owner turned it off. * - Keeps maxCallMinutes between 0 and 120. A value below 0 goes back to * the default, because Call_Window reads 0 as "no limit". * * @param array $voice The voice block, as stored or as posted. * @return array */ private function clean_voice( array $voice ): array { // Remove retired keys. Removing them from the defaults is not enough, // because the saved copy is merged over the defaults. foreach ( self::RETIRED_VOICE_KEYS as $gone ) { unset( $voice[ $gone ] ); } foreach ( self::VOICE_SWITCHES as $switch ) { if ( isset( $voice[ $switch ] ) ) { $voice[ $switch ] = filter_var( $voice[ $switch ], FILTER_VALIDATE_BOOLEAN ); } } if ( isset( $voice['maxCallMinutes'] ) ) { $minutes = (int) $voice['maxCallMinutes']; $voice['maxCallMinutes'] = 0 > $minutes ? self::VOICE_MAX_CALL_MINUTES : min( self::MAX_CALL_MINUTES, $minutes ); } return $voice; } /** * Get default settings structure * * @return array */ private function getDefaults(): array { return [ 'general' => [ 'rateLimitPerMinute' => 20, ], 'llmProviders' => [ 'aisentic' => [ 'endpoint' => AISENTIC_AI_PROVIDER, 'apiKey' => '', 'email' => '', ], 'google' => [ 'apiKey' => '' ], 'openai' => [ 'apiKey' => '' ], ], 'vectorDBProviders' => [ 'pinecone' => [ 'key' => '', 'indexUrl' => '', 'namespace' => 'default', 'embeddingProvider' => 'openai', 'embeddingModel' => 'text-embedding-3-small', 'embeddingDimensions' => 512, ], ], 'adminAssistant' => [ 'provider' => 'aisentic', 'model' => 'aisentic-auto', 'maxTokens' => 2048, 'contextMessageLimit' => 30, 'tools' => [], ], 'voice' => [ 'enabled' => false, 'maxCallMinutes' => self::VOICE_MAX_CALL_MINUTES, 'silenceEndSeconds' => 30, 'maxAnswerChars' => self::VOICE_MAX_ANSWER_CHARS, 'maxUploadBytes' => 5242880, 'consentGiven' => false, // Play each sentence as it is made. Needs cURL. 'streamAudio' => true, // Say a short line ("Of course") while the model thinks. 'openingLine' => true, // Say a holding line only after a long silence. 'fillerLine' => true, // Let the transcription model write the opening line. // The provider must also send it (GATEWAY_TRANSCRIPTION_WAITING_LINE). 'dynamicOpeningLine' => false, // Do not speak a number until a tool has looked it up. 'holdUnverifiedFacts' => true, ], 'publicChat' => [ 'maxTokens' => 1024, 'contextMessageLimit' => 20, /* * No 'widget' or 'integrations' block here on purpose. Both are * described by field schemas in Aisentic\PublicAgent\Panel_Registry, and * Public_Agent_Settings reads a default from the schema when nothing is * saved. Repeating the values here would give the same setting two * sources of truth. */ ], 'helpscout' => [ 'enabled' => false, ], ]; } }