@miphamai/cli 0.85.6 → 0.85.8

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/src/ui/picker.tsx CHANGED
@@ -1,4 +1,4 @@
1
- import React, { useState, useCallback } from 'react'
1
+ import React, { useCallback } from 'react'
2
2
  import { Box, Text, useInput } from 'ink'
3
3
  import { useI18n } from '../i18n-context'
4
4
  import { useKeyState } from './use-key-state'
@@ -34,7 +34,11 @@ export function ModelPicker({
34
34
  const providers = config.providers.filter((p) => p.status !== 'upcoming')
35
35
 
36
36
  // State
37
- const [activePanel, setActivePanel] = useState<Panel>('provider')
37
+ // 面板选择同样走 `useKeyState` —— 它与两个光标是**同一次按键里一起判读**的。
38
+ // 反例是「Tab ↓ Enter」一次刷进来:Ink 把 Tab 之后那段也拆成两拍,于是 ↓ 与
39
+ // Enter 都还在按**切换之前**那一拍算 —— ↓ 去动 provider 光标,Enter 走
40
+ // `goToProvider`(provider 面板上那条路)⇒ 一次都没选中。
41
+ const activePanel = useKeyState<Panel>('provider')
38
42
  // 光标走 `useKeyState`:一组按键可能在同一拍里到达(↓ 之后紧跟 Enter),判据必须
39
43
  // 读得到本次按键刚写下的那个索引,而不是上一张闭包里的。
40
44
  const providerIdx = useKeyState(() => {
@@ -51,7 +55,7 @@ export function ModelPicker({
51
55
  (idx: number) => {
52
56
  providerIdx.set(wrap(idx, providers.length))
53
57
  modelIdx.set(0)
54
- setActivePanel('model') // auto-switch to model panel
58
+ activePanel.set('model') // auto-switch to model panel
55
59
  },
56
60
  [providers.length, providerIdx, modelIdx],
57
61
  )
@@ -93,7 +97,7 @@ export function ModelPicker({
93
97
  }
94
98
 
95
99
  if (key.return) {
96
- if (activePanel === 'provider') {
100
+ if (activePanel.read() === 'provider') {
97
101
  goToProvider(providerIdx.read()) // switches to model panel
98
102
  } else {
99
103
  confirmSelection()
@@ -102,20 +106,20 @@ export function ModelPicker({
102
106
  }
103
107
 
104
108
  // Tab or right arrow → switch to model panel
105
- if (key.tab || (activePanel === 'provider' && input === 'l')) {
106
- setActivePanel('model')
109
+ if (key.tab || (activePanel.read() === 'provider' && input === 'l')) {
110
+ activePanel.set('model')
107
111
  return
108
112
  }
109
113
 
110
114
  // Left arrow → switch to provider panel
111
- if (key.leftArrow || (activePanel === 'model' && input === 'h')) {
112
- setActivePanel('provider')
115
+ if (key.leftArrow || (activePanel.read() === 'model' && input === 'h')) {
116
+ activePanel.set('provider')
113
117
  return
114
118
  }
115
119
 
116
120
  // Up/Down navigation
117
121
  if (key.upArrow) {
118
- if (activePanel === 'provider') {
122
+ if (activePanel.read() === 'provider') {
119
123
  providerIdx.set((prev) => wrap(prev - 1, providers.length))
120
124
  } else {
121
125
  stepModel(-1)
@@ -124,7 +128,7 @@ export function ModelPicker({
124
128
  }
125
129
 
126
130
  if (key.downArrow) {
127
- if (activePanel === 'provider') {
131
+ if (activePanel.read() === 'provider') {
128
132
  providerIdx.set((prev) => wrap(prev + 1, providers.length))
129
133
  } else {
130
134
  stepModel(1)
@@ -153,11 +157,11 @@ export function ModelPicker({
153
157
  flexDirection="column"
154
158
  width={28}
155
159
  borderStyle="single"
156
- borderColor={activePanel === 'provider' ? 'cyan' : 'gray'}
160
+ borderColor={activePanel.value === 'provider' ? 'cyan' : 'gray'}
157
161
  padding={1}
158
162
  >
159
163
  <Text bold underline dimColor>
160
- {t('ui.picker.provider_label')} {activePanel === 'provider' ? '◀' : ''}
164
+ {t('ui.picker.provider_label')} {activePanel.value === 'provider' ? '◀' : ''}
161
165
  </Text>
162
166
  {providers.map((p, i) => {
163
167
  const isCurrent = p.id === currentProvider
@@ -186,11 +190,11 @@ export function ModelPicker({
186
190
  flexDirection="column"
187
191
  width={42}
188
192
  borderStyle="single"
189
- borderColor={activePanel === 'model' ? 'cyan' : 'gray'}
193
+ borderColor={activePanel.value === 'model' ? 'cyan' : 'gray'}
190
194
  padding={1}
191
195
  >
192
196
  <Text bold underline dimColor>
193
- {t('ui.picker.models_label')} {activePanel === 'model' ? '◀' : ''}
197
+ {t('ui.picker.models_label')} {activePanel.value === 'model' ? '◀' : ''}
194
198
  {selectedProvider ? ` — ${selectedProvider.name}` : ''}
195
199
  </Text>
196
200
  {models.length === 0 && <Text dimColor> {t('ui.picker.no_active_models')}</Text>}
@@ -221,7 +225,7 @@ export function ModelPicker({
221
225
  {/* Footer hint */}
222
226
  <Box marginTop={1}>
223
227
  <Text dimColor>
224
- {activePanel === 'provider'
228
+ {activePanel.value === 'provider'
225
229
  ? t('ui.picker.select_provider_hint')
226
230
  : t('ui.picker.select_model_hint')}
227
231
  </Text>
@@ -46,13 +46,6 @@ export async function workflowAgent(
46
46
  opts: WorkflowAgentOpts = {},
47
47
  llm?: Llm,
48
48
  ): Promise<unknown> {
49
- // If provider override, switch temporarily
50
- if (opts.provider) {
51
- registry.switchProvider(opts.provider, opts.model)
52
- } else if (opts.model) {
53
- registry.switchProvider(registry.getActive().config.id, opts.model)
54
- }
55
-
56
49
  const maxRetries = opts.maxRetries ?? 2
57
50
 
58
51
  // ── Worktree isolation setup ──
@@ -93,7 +86,26 @@ export async function workflowAgent(
93
86
  let lastResult = ''
94
87
  let lastErrors: string[] = []
95
88
 
89
+ /** Set only when we actually switched — and then it is the way back. */
90
+ let restore: (() => void) | undefined
91
+
96
92
  try {
93
+ // ── Provider override, scoped to this call ──
94
+ // 「临时」必须是真的。registry 的 active 是**会话级**的:`SubAgent` 在
95
+ // `runExecution` 里读 `registry.getActiveModel()`、`registry.chat` 按 active 路由,
96
+ // 引擎的页脚与 `/model` 面板读的也是它 —— 覆盖不还原,等于一个 workflow 里的一次
97
+ // `agent(…, { provider: 'x' })` 把整台机器的会话换到 x 上。
98
+ //
99
+ // 切在这里而不是函数开头:上面那段建 worktree 会抛(`git worktree add` 非 0 即
100
+ // throw),抛在切走之前就不该已经切走。
101
+ if (opts.provider || opts.model) {
102
+ const prevProviderId = registry.getActive().config.id
103
+ const prevModelId = registry.getActiveModel()
104
+ // `opts.provider` 缺席时只换模型 —— provider 保持这一次调用开始时的那一个。
105
+ registry.switchProvider(opts.provider ?? prevProviderId, opts.model)
106
+ restore = () => registry.switchProvider(prevProviderId, prevModelId)
107
+ }
108
+
97
109
  for (let attempt = 0; attempt <= maxRetries; attempt++) {
98
110
  const retryPrompt =
99
111
  attempt === 0
@@ -158,6 +170,10 @@ export async function workflowAgent(
158
170
 
159
171
  return result
160
172
  } finally {
173
+ // 还原排在 finally 的**第一件**:下面的 worktree 清理要 spawn git、还可能抛,
174
+ // 排在它之后就等于「清理顺利时才还原」—— 而那正是最不需要还原的场合。
175
+ restore?.()
176
+
161
177
  // ── Cleanup worktree ──
162
178
  if (worktreePath) {
163
179
  // Best-effort: auto-commit any changes