YuguCompat.java

// SPDX-License-Identifier: Apache-2.0
package com.stkouyu;

import com.shengzhiai.yugu.stcompat.internal.CompatConfig;
import com.shengzhiai.yugu.stcompat.internal.RetryPolicy;
import com.shengzhiai.yugu.stcompat.internal.YLog;

/**
 * Configuration hooks of the Yugu compat layer: the only public class of {@code com.stkouyu}
 * that the Shengtong jar does not have. Apps written for the Shengtong SDK do not need it.
 * Speech synthesis and report lookup are in {@code com.shengzhiai.yugu.stcompat.TtsClient} and
 * {@code com.shengzhiai.yugu.stcompat.ReportClient}.
 */
public final class YuguCompat {
    /** Version of the compat layer. */
    public static final String VERSION = "2.2.0";

    public static final int LOG_OFF = 0;
    public static final int LOG_ERROR = 1;
    public static final int LOG_WARN = 2;
    public static final int LOG_INFO = 3;
    public static final int LOG_DEBUG = 4;

    private YuguCompat() {
    }

    /**
     * Platform base URL, for example a sandbox or a private deployment. Wins over
     * {@code EngineSetting.setServerAddress}. null restores the default {@code https://open.shengzhiai.com}.
     * TtsClient and ReportClient created without their own base URL use it too.
     *
     * @throws IllegalArgumentException when the URL has no http, https, ws or wss scheme
     */
    public static void setBaseUrl(String baseUrl) {
        CompatConfig.setBaseUrlOverride(baseUrl);
    }

    /** Base URL override, or null when the EngineSetting address (or the default) is used. */
    public static String getBaseUrl() {
        return CompatConfig.baseUrlOverride();
    }

    /**
     * SDK log level, {@link #LOG_OFF} to {@link #LOG_DEBUG}; wins over the EngineSetting log options.
     * Default WARN. At INFO every evaluation logs one timing line, at DEBUG every stage.
     */
    public static void setLogLevel(int level) {
        YLog.setLevelPinned(level);
    }

    public static int getLogLevel() {
        return YLog.getLevel();
    }

    /**
     * Retry policy of every HTTP call (shared by all Yugu SDKs). Defaults: 2 retries, 200 ms initial
     * delay, multiplier 2.0, 4000 ms maximum delay, jitter 0.3. Retries reuse the tokenId as
     * Idempotency-Key, so a retried evaluation is billed once.
     */
    public static void setRetryPolicy(int maxRetries, long initialDelayMs, double multiplier, long maxDelayMs,
                                      double jitter) {
        CompatConfig.setRetryPolicy(new RetryPolicy(maxRetries, initialDelayMs, multiplier, maxDelayMs, jitter,
                true, 30000));
    }

    /**
     * Timeouts in ms: connect (default 10000 or EngineSetting.setConnectTimeout), read per attempt
     * (default 120000 or EngineSetting.setServerTimeout) and total per evaluation including retries
     * (default 300000). Values of 0 or less restore the default.
     */
    public static void setTimeouts(int connectTimeoutMs, int readTimeoutMs, long totalTimeoutMs) {
        CompatConfig.setTimeouts(connectTimeoutMs, readTimeoutMs, totalTimeoutMs);
    }

    /**
     * Streaming upload (default true): recordings and {@code setIsStream(true)} sessions send their
     * audio over the platform's compat WebSocket while recording, whatever realtime_feedback is, and
     * the result arrives shortly after stop. false uploads the whole recording after stop as in
     * 2.1.0; realtime_feedback 1 still streams then. existsAudioTrans always uploads the file.
     * Applies to sessions started afterwards.
     */
    public static void setStreamUpload(boolean enabled) {
        CompatConfig.setStreamUpload(enabled);
    }

    public static boolean isStreamUpload() {
        return CompatConfig.streamUpload();
    }

    /**
     * What an unsupported coreType does (SDK 2.2.0), for example {@code choice.rec}, {@code open.eval},
     * {@code asr.rec}, {@code asr.eval}, {@code align.eval} or a French, Japanese or Korean kernel.
     *
     * <p>false (default): as before, the error JSON with errId 60003 and {@code error}
     * {@code 不支持的评测内核:<coreType>} arrives through the callback ({@code onScore},
     * {@code onRecordEnd(json)} or {@code skegn_callback}). true: every {@code startRecord} and
     * {@code existsAudioTrans} overload and {@code SkEgn.skegn_start} throw
     * {@link IllegalArgumentException} with that same text instead, before any other check, so also
     * when the engine is not initialised or busy; no callback follows. Either way one WARN line names
     * the coreType and errId 60003, and there is no network request and no microphone. Every other
     * error keeps its callback.
     */
    public static void setThrowOnUnsupportedCoreType(boolean enabled) {
        CompatConfig.setThrowOnUnsupportedCoreType(enabled);
    }

    /** Whether an unsupported coreType throws, see {@link #setThrowOnUnsupportedCoreType(boolean)}. */
    public static boolean isThrowOnUnsupportedCoreType() {
        return CompatConfig.throwOnUnsupportedCoreType();
    }

    /** Version string, {@code yugu-stkouyu-compat/2.2.0 (api 1.0.62)}. */
    public static String getVersion() {
        return CompatConfig.SDK_VERSION;
    }

    /**
     * Restores base URL, log level, retry policy, timeouts, streaming upload and the exception for an
     * unsupported coreType to their defaults.
     */
    public static void reset() {
        CompatConfig.setBaseUrlOverride(null);
        CompatConfig.setRetryPolicy(null);
        CompatConfig.setTimeouts(0, 0, 0);
        CompatConfig.setStreamUpload(true);
        CompatConfig.setThrowOnUnsupportedCoreType(false);
        YLog.resetLevel();
    }
}