Skip to content

Commit bef9396

Browse files
feat: polling automático no Device Flow OAuth
Implementa polling automático sem necessidade de input do usuário: - Usa method 'auto' em vez de 'code' - Polling com Bun.sleep() e margem de segurança de 3s - Remove modelo qwen-coder-plus inexistente (apenas qwen3-coder-*)
1 parent ae336ff commit bef9396

2 files changed

Lines changed: 13 additions & 20 deletions

File tree

src/constants.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,4 @@ export const QWEN_MODELS = {
5050
description: 'Faster Qwen coding model for quick responses',
5151
cost: { input: 0, output: 0 },
5252
},
53-
'qwen-coder-plus': {
54-
id: 'qwen-coder-plus',
55-
name: 'Qwen Coder Plus (OAuth)',
56-
contextWindow: 131072,
57-
maxOutput: 32768,
58-
description: 'Standard Qwen coding model',
59-
cost: { input: 0, output: 0 },
60-
},
6153
} as const;

src/index.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,18 +132,19 @@ export const QwenAuthPlugin = async (_input: unknown) => {
132132
const deviceAuth = await requestDeviceAuthorization(challenge);
133133
openBrowser(deviceAuth.verification_uri_complete);
134134

135+
const POLLING_MARGIN_MS = 3000;
136+
135137
return {
136138
url: deviceAuth.verification_uri_complete,
137-
instructions: `Abra o link e autorize.\n\nCódigo: ${deviceAuth.user_code}\n\nApós autorizar, pressione Enter...`,
138-
method: 'code',
139-
callback: async (_code: string) => {
140-
// Polling após Enter
139+
instructions: `Código: ${deviceAuth.user_code}`,
140+
method: 'auto' as const,
141+
callback: async () => {
141142
const startTime = Date.now();
142143
const timeoutMs = deviceAuth.expires_in * 1000;
143-
let interval = 2000;
144+
let interval = 5000;
144145

145146
while (Date.now() - startTime < timeoutMs) {
146-
await new Promise(r => setTimeout(r, interval));
147+
await Bun.sleep(interval + POLLING_MARGIN_MS);
147148

148149
try {
149150
const tokenResponse = await pollDeviceToken(deviceAuth.device_code, verifier);
@@ -153,7 +154,7 @@ export const QwenAuthPlugin = async (_input: unknown) => {
153154
saveCredentials(credentials);
154155

155156
return {
156-
type: 'success',
157+
type: 'success' as const,
157158
access: credentials.accessToken,
158159
refresh: credentials.refreshToken || '',
159160
expires: credentials.expiryDate || Date.now() + 3600000,
@@ -162,23 +163,23 @@ export const QwenAuthPlugin = async (_input: unknown) => {
162163
} catch (e) {
163164
const msg = e instanceof Error ? e.message : '';
164165
if (msg.includes('slow_down')) {
165-
interval = Math.min(interval * 1.5, 10000);
166+
interval = Math.min(interval + 5000, 15000);
166167
} else if (!msg.includes('authorization_pending')) {
167-
return { type: 'failed' };
168+
return { type: 'failed' as const };
168169
}
169170
}
170171
}
171172

172-
return { type: 'failed' };
173+
return { type: 'failed' as const };
173174
},
174175
};
175176
} catch (e) {
176177
const msg = e instanceof Error ? e.message : 'Erro desconhecido';
177178
return {
178179
url: '',
179180
instructions: `Erro: ${msg}`,
180-
method: 'code',
181-
callback: async () => ({ type: 'failed' }),
181+
method: 'auto' as const,
182+
callback: async () => ({ type: 'failed' as const }),
182183
};
183184
}
184185
},

0 commit comments

Comments
 (0)