-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Expand file tree
/
Copy pathhandler.ts
More file actions
166 lines (143 loc) · 4.8 KB
/
Copy pathhandler.ts
File metadata and controls
166 lines (143 loc) · 4.8 KB
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
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import type { Config } from '../config/config.js';
import {
openBrowserSecurely,
shouldLaunchBrowser,
} from '../utils/secure-browser-launcher.js';
import { debugLogger } from '../utils/debugLogger.js';
import { getErrorMessage } from '../utils/errors.js';
import type { FallbackIntent, FallbackRecommendation } from './types.js';
import { classifyFailureKind } from '../availability/errorClassification.js';
import {
buildFallbackPolicyContext,
resolvePolicyChain,
resolvePolicyAction,
applyAvailabilityTransition,
} from '../availability/policyHelpers.js';
export const UPGRADE_URL_PAGE = 'https://goo.gle/set-up-gemini-code-assist';
export async function handleFallback(
config: Config,
failedModel: string,
authType?: string,
error?: unknown,
): Promise<string | boolean | null> {
const chain = resolvePolicyChain(config);
const { failedPolicy, candidates } = buildFallbackPolicyContext(
chain,
failedModel,
);
const failureKind = classifyFailureKind(error);
const availability = config.getModelAvailabilityService();
const getAvailabilityContext = () => {
if (!failedPolicy) return undefined;
return { service: availability, policy: failedPolicy };
};
let fallbackModel: string;
if (!candidates.length) {
fallbackModel = failedModel;
} else {
const selection = availability.selectFirstAvailable(
candidates.map((policy) => policy.model),
);
const lastResortPolicy = candidates.find((policy) => policy.isLastResort);
const selectedFallbackModel =
selection.selectedModel ?? lastResortPolicy?.model;
const selectedPolicy = candidates.find(
(policy) => policy.model === selectedFallbackModel,
);
if (
!selectedFallbackModel ||
selectedFallbackModel === failedModel ||
!selectedPolicy
) {
return null;
}
fallbackModel = selectedFallbackModel;
// failureKind is already declared and calculated above
const action = resolvePolicyAction(failureKind, selectedPolicy);
if (action === 'silent') {
applyAvailabilityTransition(getAvailabilityContext, failureKind);
return processIntent(config, 'retry_always', fallbackModel);
}
// This will be used in the future when FallbackRecommendation is passed through UI
const recommendation: FallbackRecommendation = {
...selection,
selectedModel: fallbackModel,
action,
failureKind,
failedPolicy,
selectedPolicy,
};
void recommendation;
}
const handler = config.getFallbackModelHandler();
if (typeof handler !== 'function') {
return null;
}
try {
const intent = await handler(failedModel, fallbackModel, error);
// If the user chose to switch/retry, we apply the availability transition
// to the failed model (e.g. marking it terminal if it had a quota error).
// We DO NOT apply it if the user chose 'stop' or 'retry_later', allowing
// them to try again later with the same model state.
if (intent === 'retry_always' || intent === 'retry_once') {
applyAvailabilityTransition(getAvailabilityContext, failureKind);
}
return await processIntent(config, intent, fallbackModel);
} catch (handlerError) {
debugLogger.error('Fallback handler failed:', handlerError);
return null;
}
}
async function handleUpgrade() {
if (!shouldLaunchBrowser()) {
debugLogger.log(
`Cannot open browser in this environment. Please visit: ${UPGRADE_URL_PAGE}`,
);
return;
}
try {
await openBrowserSecurely(UPGRADE_URL_PAGE);
} catch (error) {
debugLogger.warn(
'Failed to open browser automatically:',
getErrorMessage(error),
);
}
}
async function processIntent(
config: Config,
intent: FallbackIntent | null,
fallbackModel: string,
): Promise<string | boolean> {
switch (intent) {
case 'retry_always':
// TODO(telemetry): Implement generic fallback event logging. Existing
// logFlashFallback is specific to a single Model.
config.activateFallbackMode(fallbackModel);
return fallbackModel;
case 'retry_once':
// For distinct retry (retry_once), we do NOT set the active model permanently.
// The FallbackStrategy will handle routing to the available model for this turn
// based on the availability service state (which is updated before this).
return fallbackModel;
case 'retry_with_credits':
return true;
case 'stop':
// Do not switch model on stop. User wants to stay on current model (and stop).
return false;
case 'retry_later':
return false;
case 'upgrade':
await handleUpgrade();
return false;
default:
throw new Error(
`Unexpected fallback intent received from fallbackModelHandler: "${intent}"`,
);
}
}