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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 | 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 42x 42x 42x 42x 42x 42x 42x 33x 33x 33x 113x 113x 2x 2x 2x 150x 5x 145x 6x 139x 55x 28x 27x 2x 25x 48x 3x 45x 3x 42x 69x 1x 68x 1x 67x 12x 12x 1x 11x 11x 11x 11x 11x 11x 11x 2x 2x 2x 4x 2x 2x 2x 9x 5x 1x 3x 3x 3x 3x 3x 3x 1x 11x 11x 7x 4x 20x 33x 33x 33x 8x 8x 8x 17x 17x 17x 3x 3x 3x 6x 6x 2x 14x 14x 14x 7x 7x 7x 8x 3x 3x 1x 7x 3x 3x 3x | import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ApplicationState, SystemPrompt } from '../core-entities';
import { GlobalConfigService } from '../global-config/global-config.service';
const ACTIVE_SESSION_KEY = 'activeSessionId';
const WEBSOCKET_ENABLED_KEY = 'websocketEnabled';
const AUTO_CONTEXT_FETCH_ENABLED_KEY = 'autoContextFetchEnabled';
const AUTO_SEND_TO_AI_STUDIO_ENABLED_KEY = 'autoSendToAIStudioEnabled';
const MANUAL_LLM_ENABLED_KEY = 'manualLlmEnabled';
const EXECUTION_STRATEGY_KEY = 'executionStrategy';
const THEME_KEY = 'theme';
const CONTEXT_TOKEN_LIMIT_KEY = 'contextTokenLimit';
const YOLO_MODE_ENABLED_KEY = 'yoloModeEnabled';
const YOLO_MODE_MESSAGE_KEY = 'yoloModeMessage';
const OPENROUTER_API_KEY = 'openrouterApiKey';
const ZAI_API_KEY = 'zaiApiKey';
const ALIBABA_API_KEY = 'alibabaApiKey';
const OLLAMA_API_KEY = 'ollamaApiKey';
const RECENT_MODELS_KEY = 'recentModels';
const OPENAI_API_KEY = 'openaiApiKey';
const OPENAI_BASE_URL = 'openaiBaseUrl';
const STREAMING_ENABLED_KEY = 'streamingEnabled';
const TOOLS_ENABLED_KEY = 'toolsEnabled';
const SYNTAX_VALIDATION_ENABLED_KEY = 'syntaxValidationEnabled';
const FOLLOWUP_TOKEN_LIMIT_KEY = 'followupTokenLimit';
const HISTORY_COMPRESSION_ENABLED_KEY = 'historyCompressionEnabled';
const LLM_RETRY_ENABLED_KEY = 'llmRetryEnabled';
const LLM_RETRY_MAX_ATTEMPTS_KEY = 'llmRetryMaxAttempts';
const DEFAULT_INSTANCE = 'default';
export type SettingSource = 'project' | 'global' | 'default';
export interface SettingWithSource<T> {
value: T;
source: SettingSource;
}
@Injectable()
export class ApplicationStateService {
private readonly instance: string;
// In-memory cache for effective settings (refreshed on state change)
private effectiveSettingsCache: Record<
string,
SettingWithSource<unknown>
> | null = null;
private effectiveSettingsCacheTime: number | null = null;
private readonly EFFECTIVE_SETTINGS_TTL = 5 * 1000; // 5 seconds - short TTL since settings can change
constructor(
@InjectRepository(ApplicationState)
private applicationStateRepository: Repository<ApplicationState>,
@InjectRepository(SystemPrompt)
private systemPromptsRepository: Repository<SystemPrompt>,
private globalConfigService: GlobalConfigService,
) {
this.instance = process.env.REPOBURG_INSTANCE_NAME || DEFAULT_INSTANCE;
}
private async setState(key: string, value: string): Promise<void> {
await this.applicationStateRepository.save({
instance: this.instance,
key: key,
value: value,
});
// Invalidate effective settings cache on any state change
this.effectiveSettingsCache = null;
this.effectiveSettingsCacheTime = null;
}
private async getState(key: string): Promise<string | null> {
const state = await this.applicationStateRepository.findOneBy({
instance: this.instance,
key: key,
});
return state?.value || null;
}
private async deleteState(key: string): Promise<void> {
await this.applicationStateRepository.delete({
instance: this.instance,
key: key,
});
// Invalidate effective settings cache on any state change
this.effectiveSettingsCache = null;
this.effectiveSettingsCacheTime = null;
}
// Helper: Get effective value with resolution chain (project -> global -> default)
private getEffectiveBoolean(
projectValue: string | null,
globalValue: boolean | undefined,
defaultValue: boolean,
): SettingWithSource<boolean> {
if (projectValue !== null) {
return { value: projectValue === 'true', source: 'project' };
}
if (globalValue !== undefined) {
return { value: globalValue, source: 'global' };
}
return { value: defaultValue, source: 'default' };
}
private getEffectiveString(
projectValue: string | null,
globalValue: string | undefined,
defaultValue: string,
): SettingWithSource<string> {
if (projectValue !== null) {
return { value: projectValue, source: 'project' };
}
if (globalValue !== undefined) {
return { value: globalValue, source: 'global' };
}
return { value: defaultValue, source: 'default' };
}
private getEffectiveNumber(
projectValue: string | null,
globalValue: number | undefined,
defaultValue: number,
): SettingWithSource<number> {
if (projectValue !== null) {
return { value: parseInt(projectValue, 10), source: 'project' };
}
if (globalValue !== undefined) {
return { value: globalValue, source: 'global' };
}
return { value: defaultValue, source: 'default' };
}
private getEffectiveNullableString(
projectValue: string | null,
globalValue: string | undefined,
): SettingWithSource<string | null> {
if (projectValue !== null) {
return { value: projectValue, source: 'project' };
}
if (globalValue !== undefined) {
return { value: globalValue, source: 'global' };
}
return { value: null, source: 'default' };
}
// Get all effective settings with sources
async getEffectiveSettings(): Promise<
Record<string, SettingWithSource<unknown>>
> {
// Check cache first
const now = Date.now();
if (
this.effectiveSettingsCache &&
this.effectiveSettingsCacheTime &&
now - this.effectiveSettingsCacheTime < this.EFFECTIVE_SETTINGS_TTL
) {
return this.effectiveSettingsCache;
}
const globalConfig = this.globalConfigService.getConfig();
// Fetch all project overrides in one query
const projectStates = await this.applicationStateRepository.find({
where: { instance: this.instance },
});
const projectMap = new Map(projectStates.map((s) => [s.key, s.value]));
const settings = {
executionStrategy: this.getEffectiveString(
projectMap.get(EXECUTION_STRATEGY_KEY) || null,
globalConfig.executionStrategy,
'apply_revert',
),
websocketEnabled: this.getEffectiveBoolean(
projectMap.get(WEBSOCKET_ENABLED_KEY) || null,
globalConfig.websocketEnabled,
true,
),
autoContextFetchEnabled: this.getEffectiveBoolean(
projectMap.get(AUTO_CONTEXT_FETCH_ENABLED_KEY) || null,
globalConfig.autoContextFetchEnabled,
true,
),
autoSendToAIStudioEnabled: this.getEffectiveBoolean(
projectMap.get(AUTO_SEND_TO_AI_STUDIO_ENABLED_KEY) || null,
globalConfig.autoSendToAIStudioEnabled,
false,
),
manualLlmEnabled: this.getEffectiveBoolean(
projectMap.get(MANUAL_LLM_ENABLED_KEY) || null,
globalConfig.manualLlmEnabled,
false,
),
yoloModeEnabled: this.getEffectiveBoolean(
projectMap.get(YOLO_MODE_ENABLED_KEY) || null,
globalConfig.yoloModeEnabled,
false,
),
yoloModeMessage: this.getEffectiveString(
projectMap.get(YOLO_MODE_MESSAGE_KEY) || null,
globalConfig.yoloModeMessage,
'continue or final',
),
streamingEnabled: this.getEffectiveBoolean(
projectMap.get(STREAMING_ENABLED_KEY) || null,
globalConfig.streamingEnabled,
false,
),
toolsEnabled: this.getEffectiveBoolean(
projectMap.get(TOOLS_ENABLED_KEY) || null,
globalConfig.toolsEnabled,
true,
),
syntaxValidationEnabled: this.getEffectiveBoolean(
projectMap.get(SYNTAX_VALIDATION_ENABLED_KEY) || null,
globalConfig.syntaxValidationEnabled,
true,
),
historyCompressionEnabled: this.getEffectiveBoolean(
projectMap.get(HISTORY_COMPRESSION_ENABLED_KEY) || null,
globalConfig.historyCompressionEnabled,
true,
),
llmRetryEnabled: this.getEffectiveBoolean(
projectMap.get(LLM_RETRY_ENABLED_KEY) || null,
globalConfig.llmRetryEnabled,
true,
),
llmRetryMaxAttempts: this.getEffectiveNumber(
projectMap.get(LLM_RETRY_MAX_ATTEMPTS_KEY) || null,
globalConfig.llmRetryMaxAttempts,
3,
),
contextTokenLimit: this.getEffectiveNumber(
projectMap.get(CONTEXT_TOKEN_LIMIT_KEY) || null,
globalConfig.contextTokenLimit,
500000,
),
followupTokenLimit: this.getEffectiveNumber(
projectMap.get(FOLLOWUP_TOKEN_LIMIT_KEY) || null,
globalConfig.followupTokenLimit,
32000,
),
openrouterApiKey: this.getEffectiveNullableString(
projectMap.get(OPENROUTER_API_KEY) || null,
globalConfig.openrouterApiKey,
),
zaiApiKey: this.getEffectiveNullableString(
projectMap.get(ZAI_API_KEY) || null,
globalConfig.zaiApiKey,
),
alibabaApiKey: this.getEffectiveNullableString(
projectMap.get(ALIBABA_API_KEY) || null,
globalConfig.alibabaApiKey,
),
ollamaApiKey: this.getEffectiveNullableString(
projectMap.get(OLLAMA_API_KEY) || null,
globalConfig.ollamaApiKey,
),
openaiApiKey: this.getEffectiveNullableString(
projectMap.get(OPENAI_API_KEY) || null,
globalConfig.openaiApiKey,
),
openaiBaseUrl: this.getEffectiveNullableString(
projectMap.get(OPENAI_BASE_URL) || null,
globalConfig.openaiBaseUrl,
),
};
// Update cache
this.effectiveSettingsCache = settings;
this.effectiveSettingsCacheTime = now;
return settings;
}
// Get project-level overrides only
async getProjectOverrides(): Promise<Record<string, string>> {
const states = await this.applicationStateRepository.find({
where: { instance: this.instance },
});
const overrides: Record<string, string> = {};
for (const state of states) {
// Exclude project-only settings that shouldn't show as overrides
if (
state.key !== ACTIVE_SESSION_KEY &&
state.key !== RECENT_MODELS_KEY &&
state.key !== THEME_KEY &&
state.key !== MANUAL_LLM_ENABLED_KEY
) {
overrides[state.key] = state.value;
}
}
return overrides;
}
// Clear a project override
async clearOverride(key: string): Promise<void> {
await this.deleteState(key);
}
async setActiveSessionId(sessionId: string): Promise<void> {
await this.setState(ACTIVE_SESSION_KEY, sessionId);
}
async getActiveSessionId(): Promise<string | null> {
return this.getState(ACTIVE_SESSION_KEY);
}
async setWebsocketEnabled(enabled: boolean): Promise<void> {
await this.setState(WEBSOCKET_ENABLED_KEY, String(enabled));
}
async getWebsocketEnabled(): Promise<boolean> {
const projectValue = await this.getState(WEBSOCKET_ENABLED_KEY);
const globalValue = this.globalConfigService.get('websocketEnabled');
return this.getEffectiveBoolean(projectValue, globalValue, true).value;
}
async getWebsocketEnabledWithSource(): Promise<SettingWithSource<boolean>> {
const projectValue = await this.getState(WEBSOCKET_ENABLED_KEY);
const globalValue = this.globalConfigService.get('websocketEnabled');
return this.getEffectiveBoolean(projectValue, globalValue, true);
}
async setAutoContextFetchEnabled(enabled: boolean): Promise<void> {
await this.setState(AUTO_CONTEXT_FETCH_ENABLED_KEY, String(enabled));
}
async getAutoContextFetchEnabled(): Promise<boolean> {
const projectValue = await this.getState(AUTO_CONTEXT_FETCH_ENABLED_KEY);
const globalValue = this.globalConfigService.get('autoContextFetchEnabled');
return this.getEffectiveBoolean(projectValue, globalValue, true).value;
}
async getAutoContextFetchEnabledWithSource(): Promise<
SettingWithSource<boolean>
> {
const projectValue = await this.getState(AUTO_CONTEXT_FETCH_ENABLED_KEY);
const globalValue = this.globalConfigService.get('autoContextFetchEnabled');
return this.getEffectiveBoolean(projectValue, globalValue, true);
}
async setAutoSendToAIStudioEnabled(enabled: boolean): Promise<void> {
await this.setState(AUTO_SEND_TO_AI_STUDIO_ENABLED_KEY, String(enabled));
}
async getAutoSendToAIStudioEnabled(): Promise<boolean> {
const projectValue = await this.getState(
AUTO_SEND_TO_AI_STUDIO_ENABLED_KEY,
);
const globalValue = this.globalConfigService.get(
'autoSendToAIStudioEnabled',
);
return this.getEffectiveBoolean(projectValue, globalValue, false).value;
}
async getAutoSendToAIStudioEnabledWithSource(): Promise<
SettingWithSource<boolean>
> {
const projectValue = await this.getState(
AUTO_SEND_TO_AI_STUDIO_ENABLED_KEY,
);
const globalValue = this.globalConfigService.get(
'autoSendToAIStudioEnabled',
);
return this.getEffectiveBoolean(projectValue, globalValue, false);
}
async setManualLlmEnabled(enabled: boolean): Promise<void> {
await this.setState(MANUAL_LLM_ENABLED_KEY, String(enabled));
}
async getManualLlmEnabled(): Promise<boolean> {
const value = await this.getState(MANUAL_LLM_ENABLED_KEY);
if (value === null) {
return false;
}
return value === 'true';
}
async setExecutionStrategy(strategy: string): Promise<void> {
await this.setState(EXECUTION_STRATEGY_KEY, strategy);
}
async getExecutionStrategy(): Promise<string> {
const projectValue = await this.getState(EXECUTION_STRATEGY_KEY);
const globalValue = this.globalConfigService.get('executionStrategy');
return this.getEffectiveString(projectValue, globalValue, 'apply_revert')
.value;
}
async getExecutionStrategyWithSource(): Promise<SettingWithSource<string>> {
const projectValue = await this.getState(EXECUTION_STRATEGY_KEY);
const globalValue = this.globalConfigService.get('executionStrategy');
return this.getEffectiveString(projectValue, globalValue, 'apply_revert');
}
async setTheme(theme: string): Promise<void> {
await this.setState(THEME_KEY, theme);
}
async getTheme(): Promise<string> {
const theme = await this.getState(THEME_KEY);
return theme || 'system';
}
async setContextTokenLimit(limit: number): Promise<void> {
await this.setState(CONTEXT_TOKEN_LIMIT_KEY, String(limit));
}
async getContextTokenLimit(): Promise<number> {
const projectValue = await this.getState(CONTEXT_TOKEN_LIMIT_KEY);
const globalValue = this.globalConfigService.get('contextTokenLimit');
return this.getEffectiveNumber(projectValue, globalValue, 500000).value;
}
async getContextTokenLimitWithSource(): Promise<SettingWithSource<number>> {
const projectValue = await this.getState(CONTEXT_TOKEN_LIMIT_KEY);
const globalValue = this.globalConfigService.get('contextTokenLimit');
return this.getEffectiveNumber(projectValue, globalValue, 500000);
}
async setYoloModeEnabled(enabled: boolean): Promise<void> {
await this.setState(YOLO_MODE_ENABLED_KEY, String(enabled));
}
async getYoloModeEnabled(): Promise<boolean> {
const projectValue = await this.getState(YOLO_MODE_ENABLED_KEY);
const globalValue = this.globalConfigService.get('yoloModeEnabled');
return this.getEffectiveBoolean(projectValue, globalValue, false).value;
}
async getYoloModeEnabledWithSource(): Promise<SettingWithSource<boolean>> {
const projectValue = await this.getState(YOLO_MODE_ENABLED_KEY);
const globalValue = this.globalConfigService.get('yoloModeEnabled');
return this.getEffectiveBoolean(projectValue, globalValue, false);
}
async setOpenrouterApiKey(apiKey: string): Promise<void> {
await this.setState(OPENROUTER_API_KEY, apiKey);
}
async getOpenrouterApiKey(): Promise<string | null> {
const projectValue = await this.getState(OPENROUTER_API_KEY);
const globalValue = this.globalConfigService.get('openrouterApiKey');
return this.getEffectiveNullableString(projectValue, globalValue).value;
}
async getOpenrouterApiKeyWithSource(): Promise<
SettingWithSource<string | null>
> {
const projectValue = await this.getState(OPENROUTER_API_KEY);
const globalValue = this.globalConfigService.get('openrouterApiKey');
return this.getEffectiveNullableString(projectValue, globalValue);
}
async setZaiApiKey(apiKey: string): Promise<void> {
await this.setState(ZAI_API_KEY, apiKey);
}
async getZaiApiKey(): Promise<string | null> {
const projectValue = await this.getState(ZAI_API_KEY);
const globalValue = this.globalConfigService.get('zaiApiKey');
return this.getEffectiveNullableString(projectValue, globalValue).value;
}
async getZaiApiKeyWithSource(): Promise<SettingWithSource<string | null>> {
const projectValue = await this.getState(ZAI_API_KEY);
const globalValue = this.globalConfigService.get('zaiApiKey');
return this.getEffectiveNullableString(projectValue, globalValue);
}
async setAlibabaApiKey(apiKey: string): Promise<void> {
await this.setState(ALIBABA_API_KEY, apiKey);
}
async getAlibabaApiKey(): Promise<string | null> {
const projectValue = await this.getState(ALIBABA_API_KEY);
const globalValue = this.globalConfigService.get('alibabaApiKey');
return this.getEffectiveNullableString(projectValue, globalValue).value;
}
async getAlibabaApiKeyWithSource(): Promise<
SettingWithSource<string | null>
> {
const projectValue = await this.getState(ALIBABA_API_KEY);
const globalValue = this.globalConfigService.get('alibabaApiKey');
return this.getEffectiveNullableString(projectValue, globalValue);
}
async setOllamaApiKey(apiKey: string): Promise<void> {
await this.setState(OLLAMA_API_KEY, apiKey);
}
async getOllamaApiKey(): Promise<string | null> {
const projectValue = await this.getState(OLLAMA_API_KEY);
const globalValue = this.globalConfigService.get('ollamaApiKey');
return this.getEffectiveNullableString(projectValue, globalValue).value;
}
async getOllamaApiKeyWithSource(): Promise<SettingWithSource<string | null>> {
const projectValue = await this.getState(OLLAMA_API_KEY);
const globalValue = this.globalConfigService.get('ollamaApiKey');
return this.getEffectiveNullableString(projectValue, globalValue);
}
async trackUsedModel(modelId: string): Promise<void> {
const recent = await this.getRecentModels();
const filtered = recent.filter((m) => m !== modelId);
const updated = [modelId, ...filtered].slice(0, 5);
await this.setState(RECENT_MODELS_KEY, JSON.stringify(updated));
}
async getRecentModels(): Promise<string[]> {
const value = await this.getState(RECENT_MODELS_KEY);
if (!value) return ['alibaba/qwen3.5-plus', 'zai/glm-5'];
try {
return JSON.parse(value);
} catch {
return ['alibaba/qwen3.5-plus', 'zai/glm-5'];
}
}
async setStreamingEnabled(enabled: boolean): Promise<void> {
await this.setState(STREAMING_ENABLED_KEY, String(enabled));
}
async getStreamingEnabled(): Promise<boolean> {
const projectValue = await this.getState(STREAMING_ENABLED_KEY);
const globalValue = this.globalConfigService.get('streamingEnabled');
return this.getEffectiveBoolean(projectValue, globalValue, false).value;
}
async getStreamingEnabledWithSource(): Promise<SettingWithSource<boolean>> {
const projectValue = await this.getState(STREAMING_ENABLED_KEY);
const globalValue = this.globalConfigService.get('streamingEnabled');
return this.getEffectiveBoolean(projectValue, globalValue, false);
}
async setToolsEnabled(enabled: boolean): Promise<void> {
await this.setState(TOOLS_ENABLED_KEY, String(enabled));
}
async getToolsEnabled(): Promise<boolean> {
const projectValue = await this.getState(TOOLS_ENABLED_KEY);
const globalValue = this.globalConfigService.get('toolsEnabled');
return this.getEffectiveBoolean(projectValue, globalValue, true).value;
}
async getToolsEnabledWithSource(): Promise<SettingWithSource<boolean>> {
const projectValue = await this.getState(TOOLS_ENABLED_KEY);
const globalValue = this.globalConfigService.get('toolsEnabled');
return this.getEffectiveBoolean(projectValue, globalValue, true);
}
async setSyntaxValidationEnabled(enabled: boolean): Promise<void> {
await this.setState(SYNTAX_VALIDATION_ENABLED_KEY, String(enabled));
}
async getSyntaxValidationEnabled(): Promise<boolean> {
const projectValue = await this.getState(SYNTAX_VALIDATION_ENABLED_KEY);
const globalValue = this.globalConfigService.get('syntaxValidationEnabled');
return this.getEffectiveBoolean(projectValue, globalValue, true).value;
}
async getSyntaxValidationEnabledWithSource(): Promise<
SettingWithSource<boolean>
> {
const projectValue = await this.getState(SYNTAX_VALIDATION_ENABLED_KEY);
const globalValue = this.globalConfigService.get('syntaxValidationEnabled');
return this.getEffectiveBoolean(projectValue, globalValue, true);
}
async setYoloModeMessage(message: string): Promise<void> {
await this.setState(YOLO_MODE_MESSAGE_KEY, message);
}
async getYoloModeMessage(): Promise<string> {
const projectValue = await this.getState(YOLO_MODE_MESSAGE_KEY);
const globalValue = this.globalConfigService.get('yoloModeMessage');
return this.getEffectiveString(
projectValue,
globalValue,
'continue or final',
).value;
}
async getYoloModeMessageWithSource(): Promise<SettingWithSource<string>> {
const projectValue = await this.getState(YOLO_MODE_MESSAGE_KEY);
const globalValue = this.globalConfigService.get('yoloModeMessage');
return this.getEffectiveString(
projectValue,
globalValue,
'continue or final',
);
}
async setFollowupTokenLimit(limit: number): Promise<void> {
await this.setState(FOLLOWUP_TOKEN_LIMIT_KEY, String(limit));
}
async getFollowupTokenLimit(): Promise<number> {
const projectValue = await this.getState(FOLLOWUP_TOKEN_LIMIT_KEY);
const globalValue = this.globalConfigService.get('followupTokenLimit');
return this.getEffectiveNumber(projectValue, globalValue, 32000).value;
}
async getFollowupTokenLimitWithSource(): Promise<SettingWithSource<number>> {
const projectValue = await this.getState(FOLLOWUP_TOKEN_LIMIT_KEY);
const globalValue = this.globalConfigService.get('followupTokenLimit');
return this.getEffectiveNumber(projectValue, globalValue, 32000);
}
/**
* Get effective followup token limit with system prompt override support.
* Resolution: system prompt → project override → global config → default
*/
async getEffectiveFollowupTokenLimit(
systemPromptId?: string | null,
): Promise<number> {
// 1. Check system prompt level first
if (systemPromptId) {
const systemPrompt = await this.systemPromptsRepository.findOneBy({
id: systemPromptId,
});
if (systemPrompt && systemPrompt.followup_token_limit !== null) {
return systemPrompt.followup_token_limit;
}
}
// 2. Fall back to existing resolution (project → global → default)
return this.getFollowupTokenLimit();
}
async setHistoryCompressionEnabled(enabled: boolean): Promise<void> {
await this.setState(HISTORY_COMPRESSION_ENABLED_KEY, String(enabled));
}
async getHistoryCompressionEnabled(): Promise<boolean> {
const projectValue = await this.getState(HISTORY_COMPRESSION_ENABLED_KEY);
const globalValue = this.globalConfigService.get(
'historyCompressionEnabled',
);
return this.getEffectiveBoolean(projectValue, globalValue, true).value;
}
async getHistoryCompressionEnabledWithSource(): Promise<
SettingWithSource<boolean>
> {
const projectValue = await this.getState(HISTORY_COMPRESSION_ENABLED_KEY);
const globalValue = this.globalConfigService.get(
'historyCompressionEnabled',
);
return this.getEffectiveBoolean(projectValue, globalValue, true);
}
async setLlmRetryEnabled(enabled: boolean): Promise<void> {
await this.setState(LLM_RETRY_ENABLED_KEY, String(enabled));
}
async getLlmRetryEnabled(): Promise<boolean> {
const projectValue = await this.getState(LLM_RETRY_ENABLED_KEY);
const globalValue = this.globalConfigService.get('llmRetryEnabled');
return this.getEffectiveBoolean(projectValue, globalValue, true).value;
}
async getLlmRetryEnabledWithSource(): Promise<SettingWithSource<boolean>> {
const projectValue = await this.getState(LLM_RETRY_ENABLED_KEY);
const globalValue = this.globalConfigService.get('llmRetryEnabled');
return this.getEffectiveBoolean(projectValue, globalValue, true);
}
async setLlmRetryMaxAttempts(attempts: number): Promise<void> {
await this.setState(LLM_RETRY_MAX_ATTEMPTS_KEY, String(attempts));
}
async getLlmRetryMaxAttempts(): Promise<number> {
const projectValue = await this.getState(LLM_RETRY_MAX_ATTEMPTS_KEY);
const globalValue = this.globalConfigService.get('llmRetryMaxAttempts');
const result = this.getEffectiveNumber(projectValue, globalValue, 3).value;
return Math.min(Math.max(result, 1), 10);
}
async getLlmRetryMaxAttemptsWithSource(): Promise<SettingWithSource<number>> {
const projectValue = await this.getState(LLM_RETRY_MAX_ATTEMPTS_KEY);
const globalValue = this.globalConfigService.get('llmRetryMaxAttempts');
const result = this.getEffectiveNumber(projectValue, globalValue, 3);
return {
value: Math.min(Math.max(result.value, 1), 10),
source: result.source,
};
}
// ===== OpenAI Compatible Provider =====
async setOpenaiApiKey(apiKey: string): Promise<void> {
await this.setState(OPENAI_API_KEY, apiKey);
}
async getOpenaiApiKey(): Promise<string | null> {
const projectValue = await this.getState(OPENAI_API_KEY);
const globalValue = this.globalConfigService.get('openaiApiKey');
return this.getEffectiveNullableString(projectValue, globalValue).value;
}
async getOpenaiApiKeyWithSource(): Promise<SettingWithSource<string | null>> {
const projectValue = await this.getState(OPENAI_API_KEY);
const globalValue = this.globalConfigService.get('openaiApiKey');
return this.getEffectiveNullableString(projectValue, globalValue);
}
async setOpenaiBaseUrl(baseUrl: string): Promise<void> {
await this.setState(OPENAI_BASE_URL, baseUrl);
}
async getOpenaiBaseUrl(): Promise<string | null> {
const projectValue = await this.getState(OPENAI_BASE_URL);
const globalValue = this.globalConfigService.get('openaiBaseUrl');
return this.getEffectiveNullableString(projectValue, globalValue).value;
}
async getOpenaiBaseUrlWithSource(): Promise<
SettingWithSource<string | null>
> {
const projectValue = await this.getState(OPENAI_BASE_URL);
const globalValue = this.globalConfigService.get('openaiBaseUrl');
return this.getEffectiveNullableString(projectValue, globalValue);
}
}
|