cursor-switch 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +161 -0
- package/SECURITY.md +38 -0
- package/bin/cswitch.js +35 -0
- package/dist/cli.d.ts +6 -0
- package/dist/cli.js +850 -0
- package/dist/core/account-store.d.ts +25 -0
- package/dist/core/account-store.js +182 -0
- package/dist/core/cdp-engine.d.ts +75 -0
- package/dist/core/cdp-engine.js +320 -0
- package/dist/core/config-store.d.ts +38 -0
- package/dist/core/config-store.js +100 -0
- package/dist/core/cursor-paths.d.ts +22 -0
- package/dist/core/cursor-paths.js +287 -0
- package/dist/core/dashboard-launcher.d.ts +34 -0
- package/dist/core/dashboard-launcher.js +210 -0
- package/dist/core/fingerprint.d.ts +7 -0
- package/dist/core/fingerprint.js +34 -0
- package/dist/core/oauth-receiver.d.ts +31 -0
- package/dist/core/oauth-receiver.js +849 -0
- package/dist/core/official-api.d.ts +29 -0
- package/dist/core/official-api.js +205 -0
- package/dist/core/outlook-launcher.d.ts +54 -0
- package/dist/core/outlook-launcher.js +671 -0
- package/dist/core/process-manager.d.ts +25 -0
- package/dist/core/process-manager.js +133 -0
- package/dist/core/pure-login-service.d.ts +67 -0
- package/dist/core/pure-login-service.js +550 -0
- package/dist/core/refund-client.d.ts +102 -0
- package/dist/core/refund-client.js +221 -0
- package/dist/core/refund-service.d.ts +39 -0
- package/dist/core/refund-service.js +384 -0
- package/dist/core/sqlite-bridge.d.ts +23 -0
- package/dist/core/sqlite-bridge.js +207 -0
- package/dist/core/storage-bridge.d.ts +18 -0
- package/dist/core/storage-bridge.js +99 -0
- package/dist/core/token-parser.d.ts +35 -0
- package/dist/core/token-parser.js +106 -0
- package/dist/core/update-checker.d.ts +62 -0
- package/dist/core/update-checker.js +231 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +17 -0
- package/dist/menu.d.ts +12 -0
- package/dist/menu.js +2201 -0
- package/dist/types.d.ts +60 -0
- package/dist/types.js +5 -0
- package/dist/utils/browser-utils.d.ts +31 -0
- package/dist/utils/browser-utils.js +283 -0
- package/dist/utils/clipboard.d.ts +5 -0
- package/dist/utils/clipboard.js +48 -0
- package/dist/utils/live-menu.d.ts +26 -0
- package/dist/utils/live-menu.js +169 -0
- package/dist/utils/plan-formatter.d.ts +30 -0
- package/dist/utils/plan-formatter.js +153 -0
- package/dist/utils/prompt-utils.d.ts +40 -0
- package/dist/utils/prompt-utils.js +105 -0
- package/dist/utils/security.d.ts +6 -0
- package/dist/utils/security.js +27 -0
- package/dist/utils/table-renderer.d.ts +7 -0
- package/dist/utils/table-renderer.js +88 -0
- package/dist/utils/time-formatter.d.ts +13 -0
- package/dist/utils/time-formatter.js +83 -0
- package/package.json +53 -0
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 奥仔席位退款/自动退款云端 API 交互客户端
|
|
3
|
+
* 原生对接 getdoubao.com / getaozai.com 后端接口
|
|
4
|
+
*/
|
|
5
|
+
export class RefundClient {
|
|
6
|
+
apiBase;
|
|
7
|
+
constructor(apiBase = 'https://getdoubao.com') {
|
|
8
|
+
this.apiBase = apiBase.replace(/\/+$/, '');
|
|
9
|
+
}
|
|
10
|
+
getApiBase() {
|
|
11
|
+
return this.apiBase;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* 使用卡密换取 Bearer 访问令牌
|
|
15
|
+
*/
|
|
16
|
+
async login(cardCode) {
|
|
17
|
+
const code = cardCode.trim();
|
|
18
|
+
if (!code) {
|
|
19
|
+
return { success: false, error: '卡密不能为空' };
|
|
20
|
+
}
|
|
21
|
+
try {
|
|
22
|
+
const res = await fetch(`${this.apiBase}/api/v1/auth/token`, {
|
|
23
|
+
method: 'POST',
|
|
24
|
+
headers: { 'Content-Type': 'application/json' },
|
|
25
|
+
body: JSON.stringify({ card_code: code }),
|
|
26
|
+
signal: AbortSignal.timeout(12000),
|
|
27
|
+
});
|
|
28
|
+
const json = await res.json().catch(() => ({}));
|
|
29
|
+
if (!res.ok) {
|
|
30
|
+
return { success: false, error: json.detail || `卡密验证失败 (HTTP ${res.status})` };
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
success: true,
|
|
34
|
+
data: {
|
|
35
|
+
token: json.token,
|
|
36
|
+
tokenType: json.token_type || 'Bearer',
|
|
37
|
+
expiresAt: json.expires_at,
|
|
38
|
+
remaining: json.remaining ?? 0,
|
|
39
|
+
maxUses: json.max_uses ?? 0,
|
|
40
|
+
used: json.used ?? 0,
|
|
41
|
+
refundCount: json.refund_count ?? 0,
|
|
42
|
+
apiAllowed: Boolean(json.api_allowed),
|
|
43
|
+
pointsPerOp: json.points_per_op ?? 2,
|
|
44
|
+
costRule: json.cost_rule || `单次扣除 ${json.points_per_op ?? 2} 点`,
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
catch (err) {
|
|
49
|
+
return { success: false, error: err.message || '网络连接超时,请检查网络或代理' };
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* 查询当前卡密余额与最新资费令牌
|
|
54
|
+
*/
|
|
55
|
+
async getBalance(authToken) {
|
|
56
|
+
try {
|
|
57
|
+
const res = await fetch(`${this.apiBase}/api/v1/card`, {
|
|
58
|
+
headers: { Authorization: `Bearer ${authToken}` },
|
|
59
|
+
signal: AbortSignal.timeout(10000),
|
|
60
|
+
});
|
|
61
|
+
const json = await res.json().catch(() => ({}));
|
|
62
|
+
if (!res.ok) {
|
|
63
|
+
return { success: false, error: json.detail || '查询余额失败' };
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
success: true,
|
|
67
|
+
data: {
|
|
68
|
+
remaining: json.remaining ?? 0,
|
|
69
|
+
used: json.used ?? 0,
|
|
70
|
+
maxUses: json.max_uses ?? 0,
|
|
71
|
+
refundCount: json.refund_count ?? 0,
|
|
72
|
+
apiAllowed: Boolean(json.api_allowed),
|
|
73
|
+
pointsPerOp: json.points_per_op ?? 2,
|
|
74
|
+
pricingToken: json.pricing_token || '',
|
|
75
|
+
costRule: json.cost_rule || `单次扣除 ${json.points_per_op ?? 2} 点`,
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
catch (err) {
|
|
80
|
+
return { success: false, error: err.message || '网络连接异常' };
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* 确认并开通资费扣点权限 (首次使用白名单确认)
|
|
85
|
+
*/
|
|
86
|
+
async acceptTerms(authToken) {
|
|
87
|
+
try {
|
|
88
|
+
const res = await fetch(`${this.apiBase}/api/v1/auth/accept-api-terms`, {
|
|
89
|
+
method: 'POST',
|
|
90
|
+
headers: {
|
|
91
|
+
Authorization: `Bearer ${authToken}`,
|
|
92
|
+
'Content-Type': 'application/json',
|
|
93
|
+
},
|
|
94
|
+
body: JSON.stringify({ accepted: true }),
|
|
95
|
+
signal: AbortSignal.timeout(10000),
|
|
96
|
+
});
|
|
97
|
+
const json = await res.json().catch(() => ({}));
|
|
98
|
+
if (!res.ok) {
|
|
99
|
+
return { success: false, error: json.detail || '资费确认开通失败' };
|
|
100
|
+
}
|
|
101
|
+
return {
|
|
102
|
+
success: true,
|
|
103
|
+
pricingToken: json.pricing_token,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
catch (err) {
|
|
107
|
+
return { success: false, error: err.message || '网络连接异常' };
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* 提交一次席位退款/刷新请求
|
|
112
|
+
*/
|
|
113
|
+
async submitProcess(authToken, sessionToken, pricingToken) {
|
|
114
|
+
try {
|
|
115
|
+
const bodyPayload = {
|
|
116
|
+
session_token: sessionToken.trim(),
|
|
117
|
+
token: sessionToken.trim(),
|
|
118
|
+
};
|
|
119
|
+
if (pricingToken) {
|
|
120
|
+
bodyPayload.pricing_token = pricingToken;
|
|
121
|
+
}
|
|
122
|
+
const res = await fetch(`${this.apiBase}/api/v1/process`, {
|
|
123
|
+
method: 'POST',
|
|
124
|
+
headers: {
|
|
125
|
+
Authorization: `Bearer ${authToken}`,
|
|
126
|
+
'Content-Type': 'application/json',
|
|
127
|
+
},
|
|
128
|
+
body: JSON.stringify(bodyPayload),
|
|
129
|
+
signal: AbortSignal.timeout(15000),
|
|
130
|
+
});
|
|
131
|
+
const json = await res.json().catch(() => ({}));
|
|
132
|
+
if (!res.ok) {
|
|
133
|
+
return {
|
|
134
|
+
success: false,
|
|
135
|
+
message: json.detail || `提交失败 (HTTP ${res.status})`,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
if (json.maintenance) {
|
|
139
|
+
return {
|
|
140
|
+
success: false,
|
|
141
|
+
maintenance: true,
|
|
142
|
+
message: json.message || '系统维护升级中,卡密点数不受影响,请稍后再试。',
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
success: true,
|
|
147
|
+
operationId: json.operation_id,
|
|
148
|
+
status: json.status || 'processing',
|
|
149
|
+
deductedPoints: json.deducted_points,
|
|
150
|
+
remaining: json.remaining,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
catch (err) {
|
|
154
|
+
return { success: false, message: err.message || '网络超时,无法连接至退款服务网关' };
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* 长轮询跟踪任务状态直至完成或失败
|
|
159
|
+
*/
|
|
160
|
+
async pollOperation(authToken, operationId, onTick, maxWaitDurationMs = 180000) {
|
|
161
|
+
const startTime = Date.now();
|
|
162
|
+
while (Date.now() - startTime < maxWaitDurationMs) {
|
|
163
|
+
try {
|
|
164
|
+
const res = await fetch(`${this.apiBase}/api/v1/operations/${operationId}?wait=15`, {
|
|
165
|
+
headers: { Authorization: `Bearer ${authToken}` },
|
|
166
|
+
signal: AbortSignal.timeout(20000),
|
|
167
|
+
});
|
|
168
|
+
if (!res.ok) {
|
|
169
|
+
await new Promise((r) => setTimeout(r, 1500));
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
const data = await res.json();
|
|
173
|
+
const statusRes = {
|
|
174
|
+
operationId: data.operation_id || operationId,
|
|
175
|
+
status: data.status,
|
|
176
|
+
error: data.error,
|
|
177
|
+
email: data.email,
|
|
178
|
+
createdAt: data.created_at,
|
|
179
|
+
steps: data.steps,
|
|
180
|
+
};
|
|
181
|
+
if (onTick) {
|
|
182
|
+
onTick(statusRes);
|
|
183
|
+
}
|
|
184
|
+
if (statusRes.status === 'completed' || statusRes.status === 'failed') {
|
|
185
|
+
return statusRes;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
catch {
|
|
189
|
+
await new Promise((r) => setTimeout(r, 1500));
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return {
|
|
193
|
+
operationId,
|
|
194
|
+
status: 'processing',
|
|
195
|
+
error: '处理耗时较长,后台仍在排队推进中,稍后可刷新余额确认',
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* 拉取该卡密的使用明细与退款流水
|
|
200
|
+
*/
|
|
201
|
+
async getUsageHistory(authToken) {
|
|
202
|
+
try {
|
|
203
|
+
const res = await fetch(`${this.apiBase}/api/v1/card/usage`, {
|
|
204
|
+
headers: { Authorization: `Bearer ${authToken}` },
|
|
205
|
+
signal: AbortSignal.timeout(10000),
|
|
206
|
+
});
|
|
207
|
+
const json = await res.json().catch(() => ({}));
|
|
208
|
+
if (!res.ok) {
|
|
209
|
+
return { success: false, error: json.detail || '获取使用明细失败' };
|
|
210
|
+
}
|
|
211
|
+
return {
|
|
212
|
+
success: true,
|
|
213
|
+
total: json.total || (json.items || []).length,
|
|
214
|
+
items: json.items || [],
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
catch (err) {
|
|
218
|
+
return { success: false, error: err.message || '网络连接异常' };
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 奥仔席位退款业务编排器 (RefundService)
|
|
3
|
+
* 整合卡密管理、Session Token 提取、云端执行、配额自动同步与进程唤醒
|
|
4
|
+
*/
|
|
5
|
+
import { ConfigStore } from './config-store.js';
|
|
6
|
+
import { RefundClient, type CardAuthInfo } from './refund-client.js';
|
|
7
|
+
import type { AccountInfo } from '../types.js';
|
|
8
|
+
export declare class RefundService {
|
|
9
|
+
private configStore;
|
|
10
|
+
private accountStore;
|
|
11
|
+
private client;
|
|
12
|
+
constructor(customApiBase?: string);
|
|
13
|
+
getConfigStore(): ConfigStore;
|
|
14
|
+
getClient(): RefundClient;
|
|
15
|
+
/**
|
|
16
|
+
* 登录并获取有效的卡密信息与令牌 (优雅卡片交互)
|
|
17
|
+
*/
|
|
18
|
+
ensureCardAuth(providedCardCode?: string, promptIfMissing?: boolean): Promise<{
|
|
19
|
+
auth: CardAuthInfo;
|
|
20
|
+
cardCode: string;
|
|
21
|
+
} | null>;
|
|
22
|
+
/**
|
|
23
|
+
* 确保卡密已同意资费协议 (未同意则引导同意)
|
|
24
|
+
*/
|
|
25
|
+
ensureApiTerms(auth: CardAuthInfo, autoYes?: boolean): Promise<string>;
|
|
26
|
+
/**
|
|
27
|
+
* 核心席位退款执行引擎
|
|
28
|
+
*/
|
|
29
|
+
runRefundForAccount(params: {
|
|
30
|
+
account?: AccountInfo;
|
|
31
|
+
sessionToken?: string;
|
|
32
|
+
cardCode?: string;
|
|
33
|
+
autoYes?: boolean;
|
|
34
|
+
}): Promise<boolean>;
|
|
35
|
+
/**
|
|
36
|
+
* 打印卡密流水明细表格 (优雅表格卡片)
|
|
37
|
+
*/
|
|
38
|
+
printUsageHistory(cardCode?: string): Promise<void>;
|
|
39
|
+
}
|
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 奥仔席位退款业务编排器 (RefundService)
|
|
3
|
+
* 整合卡密管理、Session Token 提取、云端执行、配额自动同步与进程唤醒
|
|
4
|
+
*/
|
|
5
|
+
import chalk from 'chalk';
|
|
6
|
+
import Table from 'cli-table3';
|
|
7
|
+
import boxen from 'boxen';
|
|
8
|
+
import { ConfigStore } from './config-store.js';
|
|
9
|
+
import { AccountStore } from './account-store.js';
|
|
10
|
+
import { RefundClient } from './refund-client.js';
|
|
11
|
+
import { OfficialApiClient } from './official-api.js';
|
|
12
|
+
import { ProcessManager } from './process-manager.js';
|
|
13
|
+
import { resolveCursorPaths } from './cursor-paths.js';
|
|
14
|
+
import { maskToken } from '../utils/security.js';
|
|
15
|
+
import { safeInput, safeConfirm, safePressAnyKey } from '../utils/prompt-utils.js';
|
|
16
|
+
export class RefundService {
|
|
17
|
+
configStore;
|
|
18
|
+
accountStore;
|
|
19
|
+
client;
|
|
20
|
+
constructor(customApiBase) {
|
|
21
|
+
this.configStore = new ConfigStore();
|
|
22
|
+
this.accountStore = new AccountStore();
|
|
23
|
+
const apiBase = customApiBase || this.configStore.getSeatApiBase();
|
|
24
|
+
this.client = new RefundClient(apiBase);
|
|
25
|
+
}
|
|
26
|
+
getConfigStore() {
|
|
27
|
+
return this.configStore;
|
|
28
|
+
}
|
|
29
|
+
getClient() {
|
|
30
|
+
return this.client;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* 登录并获取有效的卡密信息与令牌 (优雅卡片交互)
|
|
34
|
+
*/
|
|
35
|
+
async ensureCardAuth(providedCardCode, promptIfMissing = true) {
|
|
36
|
+
let code = providedCardCode || this.configStore.getSeatCardCode();
|
|
37
|
+
if (!code && promptIfMissing) {
|
|
38
|
+
console.clear();
|
|
39
|
+
console.log(boxen([
|
|
40
|
+
chalk.bold.cyan('● 绑定奥仔席位卡密'),
|
|
41
|
+
chalk.dim('────────────────────────────────────────────────'),
|
|
42
|
+
'执行席位退款需要绑定卡密,请输入您的席位卡密。',
|
|
43
|
+
chalk.dim('• 格式示例 : ABCD-EFGH-1234'),
|
|
44
|
+
chalk.dim('• 本地安全 : 仅保存于本地 ~/.cursor-switch/config.json'),
|
|
45
|
+
chalk.dim('• 取消方式 : 输入 0、q 或直接按回车可取消返回'),
|
|
46
|
+
].join('\n'), {
|
|
47
|
+
padding: { top: 0, bottom: 0, left: 1, right: 1 },
|
|
48
|
+
borderColor: 'cyan',
|
|
49
|
+
borderStyle: 'round',
|
|
50
|
+
}));
|
|
51
|
+
const answer = await safeInput({ message: '席位卡密' });
|
|
52
|
+
if (!answer) {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
code = answer.trim();
|
|
56
|
+
}
|
|
57
|
+
if (!code || code === '0' || code.toLowerCase() === 'q') {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
process.stdout.write(chalk.cyan('\n正在连接奥仔席位网关验证卡密有效性... '));
|
|
61
|
+
const res = await this.client.login(code);
|
|
62
|
+
if (!res.success || !res.data) {
|
|
63
|
+
console.log(chalk.red('✖ 失败'));
|
|
64
|
+
console.log(boxen([
|
|
65
|
+
chalk.bold.red('✖ 卡密验证未通过'),
|
|
66
|
+
chalk.dim('────────────────────────────────────────────────'),
|
|
67
|
+
`• 错误反馈 : ${chalk.yellow(res.error || '无效卡密或网络不可达')}`,
|
|
68
|
+
'• 解决建议 : 请检查卡密拼写,确保无多余前后空格。',
|
|
69
|
+
].join('\n'), {
|
|
70
|
+
padding: { top: 0, bottom: 0, left: 1, right: 1 },
|
|
71
|
+
borderColor: 'red',
|
|
72
|
+
borderStyle: 'round',
|
|
73
|
+
}));
|
|
74
|
+
await safePressAnyKey();
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
console.log(chalk.green('✔ 验证通过'));
|
|
78
|
+
// 成功则自动保存到本地配置
|
|
79
|
+
this.configStore.setSeatCardCode(code);
|
|
80
|
+
return { auth: res.data, cardCode: code };
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* 确保卡密已同意资费协议 (未同意则引导同意)
|
|
84
|
+
*/
|
|
85
|
+
async ensureApiTerms(auth, autoYes = false) {
|
|
86
|
+
const balRes = await this.client.getBalance(auth.token);
|
|
87
|
+
const balance = balRes.data || {
|
|
88
|
+
remaining: auth.remaining,
|
|
89
|
+
used: auth.used,
|
|
90
|
+
maxUses: auth.maxUses,
|
|
91
|
+
refundCount: auth.refundCount,
|
|
92
|
+
apiAllowed: auth.apiAllowed,
|
|
93
|
+
pointsPerOp: auth.pointsPerOp,
|
|
94
|
+
pricingToken: '',
|
|
95
|
+
costRule: auth.costRule,
|
|
96
|
+
};
|
|
97
|
+
if (balance.apiAllowed && balance.pricingToken) {
|
|
98
|
+
return balance.pricingToken;
|
|
99
|
+
}
|
|
100
|
+
// 需要开通知情确认
|
|
101
|
+
console.log(boxen([
|
|
102
|
+
chalk.bold.yellow('● 首次使用资费确认'),
|
|
103
|
+
chalk.dim('────────────────────────────────────────────────'),
|
|
104
|
+
`• 扣点规则 : 当前单次处理扣除 ${chalk.bold.yellow(`${balance.pointsPerOp} 点`)}`,
|
|
105
|
+
'• 退款保障 : 失败由系统全额自动秒退,卡密永久有效',
|
|
106
|
+
chalk.dim('• 余额规则 : 余额不足时按剩余点数扣除并归零'),
|
|
107
|
+
].join('\n'), {
|
|
108
|
+
padding: { top: 0, bottom: 0, left: 1, right: 1 },
|
|
109
|
+
borderColor: 'yellow',
|
|
110
|
+
borderStyle: 'round',
|
|
111
|
+
}));
|
|
112
|
+
let agreed = autoYes;
|
|
113
|
+
if (!agreed) {
|
|
114
|
+
agreed = await safeConfirm({
|
|
115
|
+
message: '是否同意上述规则并为当前卡密开通权限?',
|
|
116
|
+
default: true,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
if (!agreed) {
|
|
120
|
+
throw new Error('用户取消了资费开通确认');
|
|
121
|
+
}
|
|
122
|
+
const acceptRes = await this.client.acceptTerms(auth.token);
|
|
123
|
+
if (!acceptRes.success) {
|
|
124
|
+
throw new Error(acceptRes.error || '开通资费协议失败');
|
|
125
|
+
}
|
|
126
|
+
console.log(chalk.green('✔ 资费规则确认成功,已为您开通退款权限!\n'));
|
|
127
|
+
return acceptRes.pricingToken || '';
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* 核心席位退款执行引擎
|
|
131
|
+
*/
|
|
132
|
+
async runRefundForAccount(params) {
|
|
133
|
+
const { account, cardCode, autoYes } = params;
|
|
134
|
+
// 1. 提取 Session Token
|
|
135
|
+
let rawToken = params.sessionToken || account?.workos_cursor_session_token || account?.token || '';
|
|
136
|
+
rawToken = rawToken.trim();
|
|
137
|
+
if (!rawToken) {
|
|
138
|
+
console.log(boxen([
|
|
139
|
+
chalk.bold.red('✖ 缺少 Cursor 会话凭据'),
|
|
140
|
+
chalk.dim('────────────────────────────────────────────────'),
|
|
141
|
+
'未检测到有效的 Session Token,无法向席位池提交任务。',
|
|
142
|
+
'请先登录 Cursor、使用 [收录本机账号] 或选择 [手动粘贴凭据]。',
|
|
143
|
+
].join('\n'), {
|
|
144
|
+
padding: { top: 0, bottom: 0, left: 1, right: 1 },
|
|
145
|
+
borderColor: 'red',
|
|
146
|
+
borderStyle: 'round',
|
|
147
|
+
}));
|
|
148
|
+
await safePressAnyKey();
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
// 2. 鉴权卡密 (若未绑定自动以优雅卡片引导,取消则秒退)
|
|
152
|
+
const cardRes = await this.ensureCardAuth(cardCode, true);
|
|
153
|
+
if (!cardRes) {
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
const { auth, cardCode: actualCard } = cardRes;
|
|
157
|
+
if (auth.remaining <= 0) {
|
|
158
|
+
console.log(boxen([
|
|
159
|
+
chalk.bold.red('✖ 卡密点数余额不足'),
|
|
160
|
+
chalk.dim('────────────────────────────────────────────────'),
|
|
161
|
+
`• 当前卡密 : ${chalk.white(maskToken(actualCard))}`,
|
|
162
|
+
`• 剩余点数 : ${chalk.red(auth.remaining)} 点`,
|
|
163
|
+
'• 请联系充值或在退款菜单选择 [5. 绑定/更换席位卡密]。',
|
|
164
|
+
].join('\n'), {
|
|
165
|
+
padding: { top: 0, bottom: 0, left: 1, right: 1 },
|
|
166
|
+
borderColor: 'red',
|
|
167
|
+
borderStyle: 'round',
|
|
168
|
+
}));
|
|
169
|
+
await safePressAnyKey();
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
// 3. 校验并开通资费
|
|
173
|
+
let pricingToken = '';
|
|
174
|
+
try {
|
|
175
|
+
pricingToken = await this.ensureApiTerms(auth, autoYes);
|
|
176
|
+
}
|
|
177
|
+
catch (err) {
|
|
178
|
+
if (err?.name === 'ExitPromptError') {
|
|
179
|
+
process.stdout.write('\x1b[?25h\n');
|
|
180
|
+
process.exit(0);
|
|
181
|
+
}
|
|
182
|
+
console.log(chalk.grey('\n已取消操作。\n'));
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
185
|
+
// 4. 前置确认卡 (Pre-flight Summary Confirmation)
|
|
186
|
+
if (!autoYes) {
|
|
187
|
+
console.clear();
|
|
188
|
+
const targetLabel = account?.email || '手动提取的 Session 账号';
|
|
189
|
+
console.log(boxen([
|
|
190
|
+
chalk.bold.cyan('● 席位退款确认'),
|
|
191
|
+
chalk.dim('────────────────────────────────────────────────'),
|
|
192
|
+
` 目标账号 : ${chalk.bold.white(targetLabel)}`,
|
|
193
|
+
` 席位卡密 : ${chalk.white(maskToken(actualCard))} (剩余: ${chalk.green(auth.remaining)} 点)`,
|
|
194
|
+
` 扣点规则 : 预扣 ${chalk.yellow(auth.pointsPerOp)} 点 (失败全额自动秒退)`,
|
|
195
|
+
` 预期交付 : 席位退款,账号恢复为 Free 免费状态`,
|
|
196
|
+
].join('\n'), {
|
|
197
|
+
padding: { top: 0, bottom: 0, left: 1, right: 1 },
|
|
198
|
+
borderColor: 'cyan',
|
|
199
|
+
borderStyle: 'round',
|
|
200
|
+
}));
|
|
201
|
+
const confirmProceed = await safeConfirm({
|
|
202
|
+
message: '确认立即派发退款任务?',
|
|
203
|
+
default: true,
|
|
204
|
+
});
|
|
205
|
+
if (!confirmProceed) {
|
|
206
|
+
return false;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
// 5. 提交任务
|
|
210
|
+
console.log(chalk.cyan('\n[1/3] ● 正在向官方席位团队通道提交任务...'));
|
|
211
|
+
const submitRes = await this.client.submitProcess(auth.token, rawToken, pricingToken);
|
|
212
|
+
if (!submitRes.success || !submitRes.operationId) {
|
|
213
|
+
if (submitRes.maintenance) {
|
|
214
|
+
console.log(chalk.yellow(`\n🛠️ ${submitRes.message}\n`));
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
console.log(chalk.red(`\n✖ 任务提交失败: ${submitRes.message || '未知错误'}\n`));
|
|
218
|
+
}
|
|
219
|
+
await safePressAnyKey();
|
|
220
|
+
return false;
|
|
221
|
+
}
|
|
222
|
+
const opId = submitRes.operationId;
|
|
223
|
+
console.log(chalk.grey(` ✔ 任务单号: ${opId.slice(0, 10)}... (已预扣 ${submitRes.deductedPoints || auth.pointsPerOp} 点)`));
|
|
224
|
+
// 6. 长轮询等待结果 (展示平滑进度指示)
|
|
225
|
+
console.log(chalk.cyan('[2/3] ● 正在执行席位退款处理 (耗时约 5~8 秒)...'));
|
|
226
|
+
const finalStatus = await this.client.pollOperation(auth.token, opId, (tick) => {
|
|
227
|
+
if (tick.status === 'processing') {
|
|
228
|
+
const lastStep = tick.steps && tick.steps.length > 0 ? tick.steps[tick.steps.length - 1] : null;
|
|
229
|
+
if (lastStep?.message) {
|
|
230
|
+
process.stdout.write(`\r ${chalk.dim('●')} 进度: ${chalk.grey(lastStep.message.slice(0, 42))} `);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
process.stdout.write('\r \r');
|
|
235
|
+
if (finalStatus.status === 'completed') {
|
|
236
|
+
const activeEmail = finalStatus.email || account?.email || '当前账号';
|
|
237
|
+
const remainingPoints = Math.max(0, auth.remaining - auth.pointsPerOp);
|
|
238
|
+
console.log(boxen([
|
|
239
|
+
chalk.bold.green('✔ 席位退款成功!'),
|
|
240
|
+
chalk.dim('────────────────────────────────────────────────'),
|
|
241
|
+
` 目标账号 : ${chalk.bold.white(activeEmail)}`,
|
|
242
|
+
` 账号状态 : ${chalk.bold.green('已退出席位,恢复为 Free 免费状态')}`,
|
|
243
|
+
` 卡密扣除 : ${chalk.yellow(auth.pointsPerOp)} 点 (当前卡密剩余: ${chalk.bold.green(remainingPoints)} 点)`,
|
|
244
|
+
` 自动保障 : 官方通道成功交付,失败全额秒退机制有效`,
|
|
245
|
+
].join('\n'), {
|
|
246
|
+
padding: { top: 0, bottom: 0, left: 1, right: 1 },
|
|
247
|
+
borderColor: 'green',
|
|
248
|
+
borderStyle: 'round',
|
|
249
|
+
}));
|
|
250
|
+
// 7. 自动联动:向 Cursor 官方接口拉取最新用量配额
|
|
251
|
+
if (account && account.token) {
|
|
252
|
+
process.stdout.write(chalk.dim('\n正在向 Cursor 官方接口同步最新配额状态... '));
|
|
253
|
+
try {
|
|
254
|
+
const usage = await OfficialApiClient.fetchUsage(account.token);
|
|
255
|
+
if (usage.success) {
|
|
256
|
+
account.subscription_type = usage.subscriptionType || account.subscription_type;
|
|
257
|
+
account.current_period_usage = usage.currentPeriodUsage || account.current_period_usage;
|
|
258
|
+
this.accountStore.addOrUpdate(account);
|
|
259
|
+
console.log(chalk.green('✔ 已同步'));
|
|
260
|
+
}
|
|
261
|
+
else {
|
|
262
|
+
console.log(chalk.dim('○ 稍后生效'));
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
catch {
|
|
266
|
+
console.log(chalk.dim('○ 稍后生效'));
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
// 8. 检查当前 Cursor 是否正在运行,并提示重启生效
|
|
270
|
+
if (account?.is_current && ProcessManager.isCursorRunning()) {
|
|
271
|
+
const paths = resolveCursorPaths();
|
|
272
|
+
if (paths.executablePath) {
|
|
273
|
+
const restart = autoYes || (await safeConfirm({
|
|
274
|
+
message: '检测到 Cursor 正在运行,是否立即自动重启 Cursor 使状态生效?',
|
|
275
|
+
default: true,
|
|
276
|
+
}));
|
|
277
|
+
if (restart) {
|
|
278
|
+
console.log(chalk.cyan('\n正在重启 Cursor IDE...'));
|
|
279
|
+
await ProcessManager.closeCursor();
|
|
280
|
+
ProcessManager.launchCursor(paths.executablePath);
|
|
281
|
+
console.log(chalk.green('✔ Cursor 已在后台启动,Free 状态已就绪!\n'));
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
else {
|
|
286
|
+
console.log(chalk.grey('\n提示: 打开或重启 Cursor 即可生效,当前账号已恢复为 Free 免费状态。\n'));
|
|
287
|
+
}
|
|
288
|
+
await safePressAnyKey();
|
|
289
|
+
return true;
|
|
290
|
+
}
|
|
291
|
+
else {
|
|
292
|
+
console.log(boxen([
|
|
293
|
+
chalk.bold.red('✖ 席位处理未成功'),
|
|
294
|
+
chalk.dim('────────────────────────────────────────────────'),
|
|
295
|
+
` 错误反馈 : ${chalk.yellow(finalStatus.error || '官方席位接口响应异常')}`,
|
|
296
|
+
` 安全退还 : ${chalk.green('点数已全额自动秒退至卡密,零资金损失')}`,
|
|
297
|
+
' 解决建议 : 可稍等 1 分钟后重试,或检查该账号是否已属于其他 Team。',
|
|
298
|
+
].join('\n'), {
|
|
299
|
+
padding: { top: 0, bottom: 0, left: 1, right: 1 },
|
|
300
|
+
borderColor: 'red',
|
|
301
|
+
borderStyle: 'round',
|
|
302
|
+
}));
|
|
303
|
+
await safePressAnyKey();
|
|
304
|
+
return false;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* 打印卡密流水明细表格 (优雅表格卡片)
|
|
309
|
+
*/
|
|
310
|
+
async printUsageHistory(cardCode) {
|
|
311
|
+
const cardRes = await this.ensureCardAuth(cardCode, true);
|
|
312
|
+
if (!cardRes)
|
|
313
|
+
return;
|
|
314
|
+
console.clear();
|
|
315
|
+
const { auth } = cardRes;
|
|
316
|
+
process.stdout.write(chalk.cyan(`正在向云网关拉取卡密 ${maskToken(cardRes.cardCode)} 的历史流水... `));
|
|
317
|
+
const res = await this.client.getUsageHistory(auth.token);
|
|
318
|
+
if (!res.success || !res.items) {
|
|
319
|
+
console.log(chalk.red('✖ 失败'));
|
|
320
|
+
console.log(chalk.red(`\n获取流水记录失败: ${res.error || '未知网络错误'}\n`));
|
|
321
|
+
await safePressAnyKey();
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
console.log(chalk.green('✔ 就绪\n'));
|
|
325
|
+
if (res.items.length === 0) {
|
|
326
|
+
console.log(boxen([
|
|
327
|
+
chalk.bold.cyan('● 席位卡密历史流水'),
|
|
328
|
+
chalk.dim('────────────────────────────────────────────────'),
|
|
329
|
+
` 当前卡密 : ${chalk.white(maskToken(cardRes.cardCode))}`,
|
|
330
|
+
` 点数余量 : ${chalk.green(auth.remaining)} 点`,
|
|
331
|
+
chalk.grey(' 暂无任何历史使用与退款记录。'),
|
|
332
|
+
].join('\n'), {
|
|
333
|
+
padding: { top: 0, bottom: 0, left: 1, right: 1 },
|
|
334
|
+
borderColor: 'cyan',
|
|
335
|
+
borderStyle: 'round',
|
|
336
|
+
}));
|
|
337
|
+
await safePressAnyKey();
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
console.log(boxen([
|
|
341
|
+
chalk.bold.cyan('● 席位卡密历史流水与退款明细'),
|
|
342
|
+
chalk.dim('────────────────────────────────────────────────'),
|
|
343
|
+
` 当前卡密 : ${chalk.white(maskToken(cardRes.cardCode))} (剩余: ${chalk.green(auth.remaining)} 点 · 费率: ${auth.pointsPerOp} 点/单)`,
|
|
344
|
+
` 流水条数 : 共计 ${res.items.length} 笔记录`,
|
|
345
|
+
].join('\n'), {
|
|
346
|
+
padding: { top: 0, bottom: 0, left: 1, right: 1 },
|
|
347
|
+
borderColor: 'cyan',
|
|
348
|
+
borderStyle: 'round',
|
|
349
|
+
}));
|
|
350
|
+
const table = new Table({
|
|
351
|
+
head: [
|
|
352
|
+
chalk.white('序号'),
|
|
353
|
+
chalk.white('状态'),
|
|
354
|
+
chalk.white('扣点'),
|
|
355
|
+
chalk.white('目标账号'),
|
|
356
|
+
chalk.white('提交时间'),
|
|
357
|
+
chalk.white('退还凭证'),
|
|
358
|
+
],
|
|
359
|
+
style: { head: [], border: ['dim'] },
|
|
360
|
+
});
|
|
361
|
+
res.items.forEach((item, index) => {
|
|
362
|
+
const isRefund = Boolean(item.refunded_at);
|
|
363
|
+
const statusBadge = isRefund
|
|
364
|
+
? chalk.magenta('已退回')
|
|
365
|
+
: item.status === 'completed'
|
|
366
|
+
? chalk.green('成功')
|
|
367
|
+
: item.status === 'failed'
|
|
368
|
+
? chalk.red('失败')
|
|
369
|
+
: chalk.yellow('处理中');
|
|
370
|
+
const refundCol = isRefund ? chalk.magenta(`✔ 全额秒退 (${item.refunded_at?.slice(5, 16)})`) : chalk.dim('--');
|
|
371
|
+
const timeStr = item.created_at ? item.created_at.slice(5, 16).replace('T', ' ') : '-';
|
|
372
|
+
table.push([
|
|
373
|
+
chalk.dim(String(index + 1)),
|
|
374
|
+
statusBadge,
|
|
375
|
+
chalk.yellow(`${item.deducted_points || 1} 点`),
|
|
376
|
+
chalk.white(item.user_email || '会话凭据'),
|
|
377
|
+
chalk.grey(timeStr),
|
|
378
|
+
refundCol,
|
|
379
|
+
]);
|
|
380
|
+
});
|
|
381
|
+
console.log('\n' + table.toString() + '\n');
|
|
382
|
+
await safePressAnyKey();
|
|
383
|
+
}
|
|
384
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cursor SQLite 数据库 (state.vscdb) 操作引擎
|
|
3
|
+
* 多层弹性架构:优先 Node 22+ 原生 node:sqlite -> 降级系统 sqlite3 CLI -> 优雅容错
|
|
4
|
+
* 确保在任何 Node.js 版本 (Node 18/20/22/24) 与跨平台 (Windows/macOS/Linux) 均零崩溃
|
|
5
|
+
*/
|
|
6
|
+
export declare class SqliteBridge {
|
|
7
|
+
private dbPath;
|
|
8
|
+
constructor(dbPath: string);
|
|
9
|
+
exists(): boolean;
|
|
10
|
+
createBackup(): string;
|
|
11
|
+
/**
|
|
12
|
+
* 使用 node:sqlite 获取原生数据库连接 (Node 22.5+)
|
|
13
|
+
*/
|
|
14
|
+
private getNativeDb;
|
|
15
|
+
private hasSqliteCli;
|
|
16
|
+
private execSqliteCli;
|
|
17
|
+
readKey(key: string): Promise<string | null>;
|
|
18
|
+
upsertKey(key: string, value: string): Promise<void>;
|
|
19
|
+
injectAuth(email: string, token: string, refreshToken?: string): Promise<void>;
|
|
20
|
+
readEmail(): Promise<string | null>;
|
|
21
|
+
readToken(): Promise<string | null>;
|
|
22
|
+
clearAuth(): Promise<void>;
|
|
23
|
+
}
|