Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 2x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x | import {
IEngine,
IEngineAdapter,
IBaseSearchOptions,
IBaseSearchResult,
IEngineConfig,
IEngineRegistry,
IEngineLoader,
IMiddleware,
EngineErrorCode,
} from "../types.js";
import { EngineFacade } from "./EngineFacade.js";
import { EngineError } from "../errors/EngineError.js";
import { createI18nKey } from "../protocol/ProtocolValidator.js";
/**
* アダプター生成のためのファクトリ型。
*/
export type AdapterFactory<
T_OPTIONS extends IBaseSearchOptions = IBaseSearchOptions,
T_INFO = unknown,
T_RESULT extends IBaseSearchResult = IBaseSearchResult,
> = (config: IEngineConfig) => IEngineAdapter<T_OPTIONS, T_INFO, T_RESULT>;
/**
* 2026 Zenith Tier: エンジン管理の中枢ブリッジ。
*/
export class EngineBridge {
private factories = new Map<
string,
AdapterFactory<IBaseSearchOptions, unknown, IBaseSearchResult>
>();
private engines = new Map<
string,
EngineFacade<IBaseSearchOptions, unknown, IBaseSearchResult>
>();
private globalMiddlewares: IMiddleware<
IBaseSearchOptions,
unknown,
IBaseSearchResult
>[] = [];
private loaderInstance: IEngineLoader | null = null;
private registries: IEngineRegistry[] = [];
constructor(
registry?: IEngineRegistry,
private readonly loaderProvider?: () => Promise<IEngineLoader>,
) {
if (registry) {
this.registries.push(registry);
}
}
/**
* メタデータ解決のためのレジストリを追加します。
*/
addRegistry(registry: IEngineRegistry): void {
this.registries.unshift(registry);
}
/**
* グローバルミドルウェアを登録します。登録されたミドルウェアは、今後取得される全てのエンジンに適用されます。
* @param middleware - 登録するミドルウェア
* @returns ブリッジのインスタンス(メソッドチェーン用)
*/
use(
middleware: IMiddleware<IBaseSearchOptions, unknown, IBaseSearchResult>,
): this {
this.globalMiddlewares.push(middleware);
for (const engine of this.engines.values()) {
engine.use(middleware);
}
return this;
}
/**
* 特定のエンジンタイプに対するアダプターファクトリを登録します。
* @param type - アダプターのタイプ名(例: "stockfish", "yaneuraou")
* @param factory - アダプターを生成するファクトリ関数
*/
registerAdapterFactory<
T_OPTIONS extends IBaseSearchOptions,
T_INFO,
T_RESULT extends IBaseSearchResult,
>(type: string, factory: AdapterFactory<T_OPTIONS, T_INFO, T_RESULT>): void {
this.factories.set(
type,
factory as unknown as AdapterFactory<
IBaseSearchOptions,
unknown,
IBaseSearchResult
>,
);
}
/**
* 指定された設定またはIDに基づいてエンジンインスタンスを取得します。
* すでに同じIDのエンジンが生成されている場合は、既存のインスタンスを返します。
* @param idOrConfig - エンジンIDまたは詳細な設定オブジェクト
* @returns 準備完了またはロード中のエンジンインスタンス(Facade)
* @throws EngineError IDが不足している場合やアダプターが見つからない場合
*/
async getEngine<
T_OPTIONS extends IBaseSearchOptions = IBaseSearchOptions,
T_INFO = unknown,
T_RESULT extends IBaseSearchResult = IBaseSearchResult,
>(
idOrConfig: string | IEngineConfig,
): Promise<IEngine<T_OPTIONS, T_INFO, T_RESULT>> {
const config = this.resolveConfig(idOrConfig);
const id = config.id;
Iif (!id) {
throw new EngineError({
code: EngineErrorCode.VALIDATION_ERROR,
message: "Engine configuration must have an ID.",
i18nKey: createI18nKey("engine.errors.validationError"),
});
}
Iif (this.engines.has(id)) {
return this.engines.get(id) as unknown as IEngine<
T_OPTIONS,
T_INFO,
T_RESULT
>;
}
const adapterType = config.adapter;
Iif (!adapterType) {
throw new EngineError({
code: EngineErrorCode.VALIDATION_ERROR,
message: `Engine "${id}" has no adapter type defined.`,
engineId: id,
i18nKey: createI18nKey("engine.errors.invalidAdapter"),
});
}
const factory = this.factories.get(adapterType);
Iif (!factory) {
throw new EngineError({
code: EngineErrorCode.VALIDATION_ERROR,
message: `No factory registered for adapter type "${adapterType}".`,
engineId: id,
i18nKey: createI18nKey("engine.errors.invalidAdapter"),
});
}
const adapter = factory(config);
Iif (!this.isIEngineAdapter(adapter)) {
throw new EngineError({
code: EngineErrorCode.VALIDATION_ERROR,
message: `Factory for "${adapterType}" returned an object that does not implement IEngineAdapter.`,
engineId: id,
i18nKey: createI18nKey("engine.errors.validationError"),
});
}
const facade = new EngineFacade<T_OPTIONS, T_INFO, T_RESULT>(
adapter as IEngineAdapter<T_OPTIONS, T_INFO, T_RESULT>,
[...this.globalMiddlewares] as unknown as IMiddleware<
T_OPTIONS,
T_INFO,
T_RESULT
>[],
this.loaderProvider,
);
this.engines.set(
id,
facade as unknown as EngineFacade<
IBaseSearchOptions,
unknown,
IBaseSearchResult
>,
);
return facade;
}
/**
* ブリッジを破棄します。
*/
async dispose(): Promise<void> {
const disposePromises = Array.from(this.engines.values()).map((engine) =>
engine.dispose(),
);
await Promise.all(disposePromises);
this.engines.clear();
if (this.loaderProvider) {
const loader = await this.loaderProvider();
Eif (loader && typeof loader.revokeAll === "function") {
loader.revokeAll();
}
} else if (Ethis.loaderInstance) {
this.loaderInstance.revokeAll();
this.loaderInstance = null;
}
}
private resolveConfig(idOrConfig: string | IEngineConfig): IEngineConfig {
Iif (typeof idOrConfig === "string") {
let sources = null;
for (const registry of this.registries) {
sources = registry.resolve(idOrConfig);
if (sources) break;
}
if (!sources) {
throw new EngineError({
code: EngineErrorCode.VALIDATION_ERROR,
message: `Engine "${idOrConfig}" not found in any registry.`,
engineId: idOrConfig,
i18nKey: createI18nKey("engine.errors.invalidEngineId"),
});
}
return { id: idOrConfig, adapter: idOrConfig, sources };
}
return idOrConfig;
}
private isIEngineAdapter(obj: unknown): boolean {
Iif (!obj || typeof obj !== "object") return false;
const a = obj as Record<string, unknown>;
return (
typeof a["id"] === "string" &&
typeof a["name"] === "string" &&
typeof a["status"] === "string" &&
typeof a["load"] === "function" &&
typeof a["searchRaw"] === "function" &&
typeof a["stop"] === "function" &&
typeof a["onStatusChange"] === "function"
);
}
}
|