pi-ccswitch-auto-switch 0.1.1 → 0.1.2

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/README.md CHANGED
@@ -66,15 +66,15 @@ Examples:
66
66
  ## Status bar
67
67
 
68
68
  ```text
69
- CCS ✓70/71 · ⏳1 · ⛔0
69
+ CCS ✓70/131 · ⏳61 · ⛔0
70
70
  ```
71
71
 
72
- - `✓70/71`: 70 healthy models out of 71 unique `provider/model` combinations in Pi's effective scope. Duplicate scoped entries are counted once.
73
- - `⏳1`: one model, provider, or endpoint health record is cooling down.
72
+ - `✓70/131`: 70 healthy models out of 131 unique `provider/model` combinations in Pi's effective scope. Duplicate scoped entries are counted once.
73
+ - `⏳61`: 61 models are currently affected by automatic model, provider, or endpoint cooldowns. One provider breaker can account for many affected models.
74
74
  - `⛔0`: no manually disabled models.
75
75
  - During a switch, `CCS ↻2/5 provider/model` means the second of at most five attempts is being made.
76
76
 
77
- When all unique models are healthy, the compact form is `CCS ✓71`. Use `/ccswitch` or `/ccswitch-test` to see Pi's raw scope entry count, the deduplicated model count, the healthy count, and the exact record behind a cooldown.
77
+ When all unique models are healthy, the compact form is `CCS ✓131`. Use `/ccswitch` or `/ccswitch-test` to see Pi's raw scope entry count, the deduplicated model count, affected model counts, and the underlying breaker-record count.
78
78
 
79
79
  ## Failover behavior
80
80
 
package/README.zh-CN.md CHANGED
@@ -58,15 +58,15 @@ pi install npm:pi-ccswitch-auto-switch
58
58
  ## 状态栏
59
59
 
60
60
  ```text
61
- CCS ✓70/71 · ⏳1 · ⛔0
61
+ CCS ✓70/131 · ⏳61 · ⛔0
62
62
  ```
63
63
 
64
- - `✓70/71`:Pi 有效范围内共有 71 个去重后的 `provider/model` 组合,其中 70 个健康;重复的 scope 条目只计一次。
65
- - `⏳1`:有一条模型、Provider 或端点健康记录仍在冷却。
64
+ - `✓70/131`:Pi 有效范围内共有 131 个去重后的 `provider/model` 组合,其中 70 个健康;重复的 scope 条目只计一次。
65
+ - `⏳61`:有 61 个模型正受模型、Provider 或端点自动冷却影响;一条 Provider 熔断记录可能同时影响许多模型。
66
66
  - `⛔0`:没有被手动禁用的模型。
67
67
  - 切换中出现 `CCS ↻2/5 provider/model`,表示正在进行最多 5 次尝试中的第 2 次。
68
68
 
69
- 全部唯一模型都健康时会压缩显示为 `CCS ✓71`。通过 `/ccswitch` 或 `/ccswitch-test` 可同时查看 Pi 原始 scope 条目数、去重模型数、健康数,以及具体哪个记录正在冷却。
69
+ 全部唯一模型都健康时会压缩显示为 `CCS ✓131`。通过 `/ccswitch` 或 `/ccswitch-test` 可同时查看 Pi 原始 scope 条目数、去重模型数、受影响模型数和底层熔断记录数。
70
70
 
71
71
  ## 故障转移逻辑
72
72
 
package/candidates.ts CHANGED
@@ -14,6 +14,18 @@ export interface CandidateSnapshot {
14
14
  sourceEntries: number
15
15
  }
16
16
 
17
+ export interface CandidateHealthSummary {
18
+ total: number
19
+ healthy: number
20
+ cooling: number
21
+ disabled: number
22
+ breakerRecords: number
23
+ }
24
+
25
+ function activeCooldown(record: HealthState['models'][string] | undefined, now: number): boolean {
26
+ return Boolean((record?.cooldownUntil && record.cooldownUntil > now) || (record?.leaseUntil && record.leaseUntil > now))
27
+ }
28
+
17
29
  function blocked(model: ModelRef, health: HealthState, now = Date.now()): boolean {
18
30
  const records = [health.models[modelKey(model)], health.providers[model.provider], health.endpoints[endpointKey(model)]]
19
31
  return records.some(record => Boolean(record?.disabled || (record?.cooldownUntil && record.cooldownUntil > now) || (record?.leaseUntil && record.leaseUntil > now)))
@@ -39,6 +51,25 @@ export function candidateSnapshot(scoped: readonly ScopedModel[], available: Mod
39
51
  }
40
52
  }
41
53
 
54
+ export function summarizeCandidateHealth(models: ModelRef[], health: HealthState, now = Date.now()): CandidateHealthSummary {
55
+ let healthy = 0
56
+ let cooling = 0
57
+ let disabled = 0
58
+ for (const model of models) {
59
+ const modelRecord = health.models[modelKey(model)]
60
+ if (modelRecord?.disabled) {
61
+ disabled += 1
62
+ continue
63
+ }
64
+ const records = [modelRecord, health.providers[model.provider], health.endpoints[endpointKey(model)]]
65
+ if (records.some(record => activeCooldown(record, now))) cooling += 1
66
+ else healthy += 1
67
+ }
68
+ const breakerRecords = [...Object.values(health.models), ...Object.values(health.providers), ...Object.values(health.endpoints)]
69
+ .filter(record => activeCooldown(record, now)).length
70
+ return { total: models.length, healthy, cooling, disabled, breakerRecords }
71
+ }
72
+
42
73
  export function chooseCandidate(models: ModelRef[], options: CandidateOptions): ModelRef | undefined {
43
74
  const current = options.current
44
75
  const candidates = models.filter(model => {
package/index.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { ExtensionAPI, ExtensionContext, FailureObservation, ModelRef } from './types.ts'
2
2
  import { classifyFailure, parseRetryAfter } from './classify.ts'
3
- import { candidateSnapshot, effectiveCandidates, chooseCandidate } from './candidates.ts'
3
+ import { candidateSnapshot, effectiveCandidates, chooseCandidate, summarizeCandidateHealth } from './candidates.ts'
4
4
  import { HealthStore, endpointKey, modelKey } from './health.ts'
5
5
 
6
6
  const FIRST_RESPONSE_TIMEOUT = 90_000
@@ -49,16 +49,11 @@ export default function (pi: ExtensionAPI) {
49
49
  const status = (ctx: ExtensionContext) => {
50
50
  const state = health.snapshot
51
51
  const { models } = candidateSnapshot(ctx.scopedModels, ctx.modelRegistry.getAvailable())
52
- const now = Date.now()
53
- const disabled = Object.values(state.models).filter(item => item.disabled).length
54
- const cooling = [...Object.values(state.models), ...Object.values(state.providers), ...Object.values(state.endpoints)]
55
- .filter(item => item.cooldownUntil && item.cooldownUntil > now).length
56
- const unhealthy = models.filter(model => health.isBlocked(model)).length
57
- const healthy = Math.max(0, models.length - unhealthy)
52
+ const counts = summarizeCandidateHealth(models, state)
58
53
  const plain = round?.phase === 'switching' ? `CCS ↻${round.attempts}/${MAX_ATTEMPTS} ${round.model ? modelKey(round.model) : ''}` :
59
- cooling || disabled ? `CCS ✓${healthy}/${models.length} · ⏳${cooling} · ⛔${disabled}` : `CCS ✓${models.length}`
54
+ counts.cooling || counts.disabled ? `CCS ✓${counts.healthy}/${counts.total} · ⏳${counts.cooling} · ⛔${counts.disabled}` : `CCS ✓${counts.total}`
60
55
  const theme = ctx.ui.theme
61
- ctx.ui.setStatus('ccswitch-ha', theme ? theme.fg(cooling || disabled ? 'warning' : 'success', plain) : plain)
56
+ ctx.ui.setStatus('ccswitch-ha', theme ? theme.fg(counts.cooling || counts.disabled ? 'warning' : 'success', plain) : plain)
62
57
  }
63
58
  const notify = (ctx: ExtensionContext, message: string, type: 'info' | 'warning' | 'error' = 'info') => {
64
59
  if (ctx.hasUI) ctx.ui.notify(message, type)
@@ -83,19 +78,22 @@ export default function (pi: ExtensionAPI) {
83
78
  const state = health.snapshot
84
79
  const snapshot = candidateSnapshot(ctx.scopedModels, ctx.modelRegistry.getAvailable())
85
80
  const candidates = snapshot.models
86
- const healthy = candidates.filter(model => !health.isBlocked(model)).length
81
+ const counts = summarizeCandidateHealth(candidates, state)
82
+ const now = Date.now()
87
83
  const rows = candidates.slice(0, 10).map(model => {
88
- const record = state.models[modelKey(model)] ?? state.providers[model.provider]
89
- const suffix = record?.disabled ? '禁用' : record?.cooldownUntil && record.cooldownUntil > Date.now() ? `冷却 ${Math.ceil((record.cooldownUntil - Date.now()) / 60_000)}m` : '健康'
84
+ const modelRecord = state.models[modelKey(model)]
85
+ const records = [modelRecord, state.providers[model.provider], state.endpoints[endpointKey(model)]]
86
+ const blockedUntil = Math.max(0, ...records.flatMap(record => [record?.cooldownUntil ?? 0, record?.leaseUntil ?? 0]))
87
+ const suffix = modelRecord?.disabled ? '手动禁用' : blockedUntil > now ? `自动冷却 ${Math.max(1, Math.ceil((blockedUntil - now) / 60_000))}m` : '健康'
90
88
  return `${modelKey(model)} ${suffix}`
91
89
  })
92
90
  if (!ctx.ui.select) {
93
91
  const source = snapshot.source === 'scoped' ? `Pi scope ${snapshot.sourceEntries} 条` : `Pi 注册表 ${snapshot.sourceEntries} 条`
94
- notify(ctx, `CCSwitch:${source} · 唯一模型 ${candidates.length} · 健康 ${healthy}`, 'info')
92
+ notify(ctx, `CCSwitch:${source} · 唯一模型 ${counts.total} · 健康 ${counts.healthy} · 自动冷却 ${counts.cooling} · 手动禁用 ${counts.disabled}`, 'info')
95
93
  return
96
94
  }
97
95
  const scopeLabel = snapshot.source === 'scoped' ? `Pi scope:${snapshot.sourceEntries} 条` : `Pi 可用注册表:${snapshot.sourceEntries} 条`
98
- const action = await ctx.ui.select(`CCSwitch 健康面板\n当前:${key(ctx.model) ?? '无'}\n${scopeLabel} · 唯一模型:${candidates.length} · 健康:${healthy}\n${rows.join('\n') || '没有可用模型'}`, ['刷新', '重新激活当前模型', '禁用当前模型', '重置当前模型历史', '关闭'])
96
+ const action = await ctx.ui.select(`CCSwitch 健康面板\n当前:${key(ctx.model) ?? '无'}\n${scopeLabel} · 唯一模型:${counts.total} · 健康:${counts.healthy} · 自动冷却:${counts.cooling} · 手动禁用:${counts.disabled}\n熔断记录:${counts.breakerRecords}\n${rows.join('\n') || '没有可用模型'}`, ['刷新', '重新激活当前模型', '禁用当前模型', '重置当前模型历史', '关闭'])
99
97
  if (action === '刷新') await refresh(ctx)
100
98
  if (action === '重新激活当前模型' && ctx.model) { health.reactivate(ctx.model); await health.flush(); status(ctx); notify(ctx, '已重新激活当前模型') }
101
99
  if (action === '禁用当前模型' && ctx.model) { health.disable(modelKey(ctx.model), true); await health.flush(); status(ctx); notify(ctx, '已禁用当前模型', 'warning') }
@@ -238,8 +236,8 @@ export default function (pi: ExtensionAPI) {
238
236
  pi.registerCommand('ccswitch-test', { description: '检查 CCSwitch 候选模型和健康状态(不切换模型)', handler: async (_args, ctx) => {
239
237
  await refresh(ctx)
240
238
  const snapshot = candidateSnapshot(ctx.scopedModels, ctx.modelRegistry.getAvailable())
241
- const healthy = snapshot.models.filter(model => !health.isBlocked(model)).length
239
+ const counts = summarizeCandidateHealth(snapshot.models, health.snapshot)
242
240
  const source = snapshot.source === 'scoped' ? `Pi scope ${snapshot.sourceEntries} 条` : `Pi 注册表 ${snapshot.sourceEntries} 条`
243
- notify(ctx, `CCSwitch 自检:${source} · 唯一模型 ${snapshot.models.length} · 健康 ${healthy}`, snapshot.models.length ? 'info' : 'warning')
241
+ notify(ctx, `CCSwitch 自检:${source} · 唯一模型 ${counts.total} · 健康 ${counts.healthy} · 自动冷却 ${counts.cooling} · 手动禁用 ${counts.disabled} · 熔断记录 ${counts.breakerRecords}`, counts.total ? 'info' : 'warning')
244
242
  }})
245
243
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-ccswitch-auto-switch",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Provider-first automatic model failover extension for Pi and CC Switch",
5
5
  "license": "MIT",
6
6
  "keywords": [