@miphamai/cli 0.85.4 → 0.85.5
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/package.json +1 -1
- package/src/agent-view/agents-standalone.tsx +42 -0
- package/src/core/context.ts +56 -5
- package/src/core/engine.ts +95 -34
- package/src/core/hooks-executor.ts +88 -11
- package/src/core/hooks.ts +26 -2
- package/src/core/instructions.ts +105 -17
- package/src/core/permission-rules.ts +1 -1
- package/src/core/permission.ts +50 -1
- package/src/core/session-log.ts +60 -0
- package/src/daemon/index.ts +2 -5
- package/src/daemon/server.ts +19 -14
- package/src/i18n-core/locales/en-US.json +5 -2
- package/src/i18n-core/locales/zh-CN.json +5 -2
- package/src/index.tsx +17 -7
- package/src/mcp/client.ts +61 -0
- package/src/mcp/instructions.ts +49 -0
- package/src/mcp/types.ts +7 -0
- package/src/plugin/claude-plugin.ts +12 -2
- package/src/plugin/plugin-loader.ts +32 -14
- package/src/plugin/plugin-manager.ts +16 -2
- package/src/plugin/plugin-validator.ts +183 -1
- package/src/providers/anthropic.ts +36 -15
- package/src/providers/fetch-utils.ts +53 -5
- package/src/security/dangerous-rm.ts +192 -0
- package/src/shared/constants.ts +18 -0
- package/src/shared/deleted-cwd.ts +46 -1
- package/src/shared/package-info.ts +1 -1
- package/src/shared/types.ts +8 -0
- package/src/ui/command-picker.tsx +18 -10
- package/src/ui/commands.ts +1 -1
- package/src/ui/config-wizard.tsx +22 -19
- package/src/ui/picker.tsx +38 -27
- package/src/ui/use-key-state.ts +55 -0
- package/src/daemon/message-bus.ts +0 -84
package/src/ui/picker.tsx
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
import React, { useState, useCallback } from 'react'
|
|
2
2
|
import { Box, Text, useInput } from 'ink'
|
|
3
3
|
import { useI18n } from '../i18n-context'
|
|
4
|
+
import { useKeyState } from './use-key-state'
|
|
4
5
|
import type { MiphamConfig, ProviderConfig } from '../shared/index.ts'
|
|
5
6
|
|
|
7
|
+
/** 环绕取模 —— 列表两端互为首尾。 */
|
|
8
|
+
const wrap = (i: number, len: number) => ((i % len) + len) % len
|
|
9
|
+
|
|
10
|
+
/** 只列启用中的模型(与 `providerList` 的 `upcoming` 过滤同一条口径)。 */
|
|
11
|
+
const activeModels = (p: ProviderConfig) => p.models.filter((m) => m.status === 'active')
|
|
12
|
+
|
|
6
13
|
interface PickerProps {
|
|
7
14
|
config: MiphamConfig
|
|
8
15
|
currentProvider: string
|
|
@@ -25,54 +32,58 @@ export function ModelPicker({
|
|
|
25
32
|
const { t } = useI18n()
|
|
26
33
|
// Get active providers only
|
|
27
34
|
const providers = config.providers.filter((p) => p.status !== 'upcoming')
|
|
28
|
-
const activeModels = (p: ProviderConfig) => p.models.filter((m) => m.status === 'active')
|
|
29
35
|
|
|
30
36
|
// State
|
|
31
37
|
const [activePanel, setActivePanel] = useState<Panel>('provider')
|
|
32
|
-
|
|
38
|
+
// 光标走 `useKeyState`:一组按键可能在同一拍里到达(↓ 之后紧跟 Enter),判据必须
|
|
39
|
+
// 读得到本次按键刚写下的那个索引,而不是上一张闭包里的。
|
|
40
|
+
const providerIdx = useKeyState(() => {
|
|
33
41
|
const idx = providers.findIndex((p) => p.id === currentProvider)
|
|
34
42
|
return idx >= 0 ? idx : 0
|
|
35
43
|
})
|
|
36
|
-
const
|
|
44
|
+
const modelIdx = useKeyState(0)
|
|
37
45
|
|
|
38
|
-
const selectedProvider = providers[providerIdx]
|
|
46
|
+
const selectedProvider = providers[providerIdx.value]
|
|
39
47
|
const models = selectedProvider ? activeModels(selectedProvider) : []
|
|
40
48
|
|
|
41
49
|
// Reset model index when provider changes
|
|
42
50
|
const goToProvider = useCallback(
|
|
43
51
|
(idx: number) => {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
setModelIdx(0)
|
|
52
|
+
providerIdx.set(wrap(idx, providers.length))
|
|
53
|
+
modelIdx.set(0)
|
|
47
54
|
setActivePanel('model') // auto-switch to model panel
|
|
48
55
|
},
|
|
49
|
-
[providers.length],
|
|
56
|
+
[providers.length, providerIdx, modelIdx],
|
|
50
57
|
)
|
|
51
58
|
|
|
52
|
-
|
|
53
|
-
|
|
59
|
+
/** 相对移动 —— 按 `read()` 叠加,同一拍里的两次 ↓ 就是两步。 */
|
|
60
|
+
const stepModel = useCallback(
|
|
61
|
+
(delta: number) => {
|
|
54
62
|
if (!selectedProvider) return
|
|
55
|
-
const
|
|
56
|
-
|
|
63
|
+
const len = models.length
|
|
64
|
+
if (len === 0) return
|
|
65
|
+
modelIdx.set((prev) => wrap(prev + delta, len))
|
|
57
66
|
},
|
|
58
|
-
[selectedProvider, models.length],
|
|
67
|
+
[selectedProvider, models.length, modelIdx],
|
|
59
68
|
)
|
|
60
69
|
|
|
61
70
|
const confirmSelection = useCallback(() => {
|
|
62
|
-
|
|
63
|
-
const
|
|
71
|
+
// 按 `read()` 取当前光标,而不是渲染时那份闭包 —— 否则「↓ Enter」确认的是上一行。
|
|
72
|
+
const provider = providers[providerIdx.read()]
|
|
73
|
+
if (!provider) return
|
|
74
|
+
const model = activeModels(provider)[modelIdx.read()]
|
|
64
75
|
if (model) {
|
|
65
76
|
// Check API key before confirming switch
|
|
66
|
-
const apiKey =
|
|
77
|
+
const apiKey = provider.apiKey
|
|
67
78
|
if (!apiKey || apiKey.trim() === '' || /^\$\{[A-Z_]+\}$/.test(apiKey.trim())) {
|
|
68
|
-
if (
|
|
69
|
-
onNeedsApiKey(
|
|
79
|
+
if (provider.id !== 'ollama' && onNeedsApiKey) {
|
|
80
|
+
onNeedsApiKey(provider.id, model.id, provider.name)
|
|
70
81
|
return
|
|
71
82
|
}
|
|
72
83
|
}
|
|
73
|
-
onSelect(
|
|
84
|
+
onSelect(provider.id, model.id)
|
|
74
85
|
}
|
|
75
|
-
}, [
|
|
86
|
+
}, [providers, providerIdx, modelIdx, onSelect, onNeedsApiKey])
|
|
76
87
|
|
|
77
88
|
useInput((input, key) => {
|
|
78
89
|
// Global keys
|
|
@@ -83,7 +94,7 @@ export function ModelPicker({
|
|
|
83
94
|
|
|
84
95
|
if (key.return) {
|
|
85
96
|
if (activePanel === 'provider') {
|
|
86
|
-
goToProvider(providerIdx) // switches to model panel
|
|
97
|
+
goToProvider(providerIdx.read()) // switches to model panel
|
|
87
98
|
} else {
|
|
88
99
|
confirmSelection()
|
|
89
100
|
}
|
|
@@ -105,18 +116,18 @@ export function ModelPicker({
|
|
|
105
116
|
// Up/Down navigation
|
|
106
117
|
if (key.upArrow) {
|
|
107
118
|
if (activePanel === 'provider') {
|
|
108
|
-
|
|
119
|
+
providerIdx.set((prev) => wrap(prev - 1, providers.length))
|
|
109
120
|
} else {
|
|
110
|
-
|
|
121
|
+
stepModel(-1)
|
|
111
122
|
}
|
|
112
123
|
return
|
|
113
124
|
}
|
|
114
125
|
|
|
115
126
|
if (key.downArrow) {
|
|
116
127
|
if (activePanel === 'provider') {
|
|
117
|
-
|
|
128
|
+
providerIdx.set((prev) => wrap(prev + 1, providers.length))
|
|
118
129
|
} else {
|
|
119
|
-
|
|
130
|
+
stepModel(1)
|
|
120
131
|
}
|
|
121
132
|
return
|
|
122
133
|
}
|
|
@@ -150,7 +161,7 @@ export function ModelPicker({
|
|
|
150
161
|
</Text>
|
|
151
162
|
{providers.map((p, i) => {
|
|
152
163
|
const isCurrent = p.id === currentProvider
|
|
153
|
-
const isSelected = i === providerIdx
|
|
164
|
+
const isSelected = i === providerIdx.value
|
|
154
165
|
const isUpcoming = p.status === 'upcoming'
|
|
155
166
|
return (
|
|
156
167
|
<Box key={p.id}>
|
|
@@ -185,7 +196,7 @@ export function ModelPicker({
|
|
|
185
196
|
{models.length === 0 && <Text dimColor> {t('ui.picker.no_active_models')}</Text>}
|
|
186
197
|
{models.map((m, i) => {
|
|
187
198
|
const isCurrentModel = selectedProvider?.id === currentProvider && m.id === currentModel
|
|
188
|
-
const isSelected = i === modelIdx
|
|
199
|
+
const isSelected = i === modelIdx.value
|
|
189
200
|
return (
|
|
190
201
|
<Box key={m.id} flexDirection="column">
|
|
191
202
|
<Text
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { useCallback, useRef, useState } from 'react'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 一个状态 + 一个「在同一次按键回调里也读得到最新值」的镜像。
|
|
5
|
+
*
|
|
6
|
+
* 为什么需要它:一组按键会在同一个 chunk 里到达(远端/远程控制,或键盘把
|
|
7
|
+
* 「↓ 然后 Enter」一起写进来),Ink 的输入解析器会把这种转义序列打头的 chunk
|
|
8
|
+
* 切开**逐个派发** —— 而 React 还没提交第一个回调里的 setState,第二个回调拿到的
|
|
9
|
+
* 就还是上一张闭包。于是「↓ Enter」按在**按下 ↓ 之前**那一行上:按键有反应,
|
|
10
|
+
* 世界却是上一拍的。
|
|
11
|
+
*
|
|
12
|
+
* 与 `useCtrlCConfirm` 的两条路同源(那里的 `isArmed()` / `armed`):
|
|
13
|
+
* - `value` 只给渲染用;
|
|
14
|
+
* - `read()` 给判据用 —— 同步读数,同一次按键回调里 `set()` 之后立刻为新值;
|
|
15
|
+
* - `set()` 接受函数式写法,并按 `read()` 的当前值求值,于是同一个 burst 里的多次
|
|
16
|
+
* 修改能**叠加**(两次 ↓ 是两步,而不是把两次都算在同一个起点上)。
|
|
17
|
+
*
|
|
18
|
+
* 注意 `set()` 之外不要再写这个 state —— 绕过它写会让镜像与真值分叉。
|
|
19
|
+
*/
|
|
20
|
+
export interface KeyState<T> {
|
|
21
|
+
/** 渲染用。 */
|
|
22
|
+
value: T
|
|
23
|
+
/**
|
|
24
|
+
* 写入。函数式写法按 `read()` 求值后落盘:同一个 burst 里调用多次会依次叠加。
|
|
25
|
+
*
|
|
26
|
+
* `T` 是函数时函数式写法不可用(按值处理)—— 本仓库的调用点都是索引/枚举。
|
|
27
|
+
*/
|
|
28
|
+
set: (next: T | ((prev: T) => T)) => void
|
|
29
|
+
/** 判据用:同步读数,`set()` 之后立刻为新值。 */
|
|
30
|
+
read: () => T
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function useKeyState<T>(initial: T | (() => T)): KeyState<T> {
|
|
34
|
+
const [value, setValue] = useState(initial)
|
|
35
|
+
const ref = useRef(value)
|
|
36
|
+
|
|
37
|
+
const set = useCallback((next: T | ((prev: T) => T)) => {
|
|
38
|
+
const resolved = typeof next === 'function' ? (next as (prev: T) => T)(ref.current) : next
|
|
39
|
+
ref.current = resolved
|
|
40
|
+
setValue(resolved)
|
|
41
|
+
}, [])
|
|
42
|
+
|
|
43
|
+
const read = useCallback(() => ref.current, [])
|
|
44
|
+
|
|
45
|
+
// 返回的对象**身份稳定**(每次渲染只刷新 `.value`):调用点会把它整个放进依赖
|
|
46
|
+
// 数组,每次渲染换一个身份会让依赖它的 effect 每次都重跑。
|
|
47
|
+
const apiRef = useRef<KeyState<T> | null>(null)
|
|
48
|
+
let api = apiRef.current
|
|
49
|
+
if (!api) {
|
|
50
|
+
api = { value, set, read }
|
|
51
|
+
apiRef.current = api
|
|
52
|
+
}
|
|
53
|
+
api.value = value
|
|
54
|
+
return api
|
|
55
|
+
}
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
// apps/cli/src/daemon/message-bus.ts — Phase 3: Inter-Agent Message Bus
|
|
2
|
-
//
|
|
3
|
-
// MessageBus provides asynchronous message passing between agents:
|
|
4
|
-
// - send() — enqueue a message from one agent to another
|
|
5
|
-
// - poll() — retrieve and clear pending messages (discards TTL-expired)
|
|
6
|
-
// - broadcastToSession() — send a message to all agents in a session
|
|
7
|
-
//
|
|
8
|
-
// Messages older than 5 minutes are automatically discarded on poll.
|
|
9
|
-
// Session-to-agent mapping is maintained externally via registerAgent/unregisterAgent.
|
|
10
|
-
|
|
11
|
-
interface BusMessage {
|
|
12
|
-
sender: string
|
|
13
|
-
content: string
|
|
14
|
-
timestamp: number
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export class MessageBus {
|
|
18
|
-
private queues = new Map<string, BusMessage[]>()
|
|
19
|
-
private sessionAgents = new Map<string, Set<string>>()
|
|
20
|
-
private readonly ttlMs = 5 * 60 * 1000 // 5 minutes
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Register an agent to a session so that broadcastToSession can reach it.
|
|
24
|
-
*/
|
|
25
|
-
registerAgent(sessionId: string, agentId: string): void {
|
|
26
|
-
if (!this.sessionAgents.has(sessionId)) {
|
|
27
|
-
this.sessionAgents.set(sessionId, new Set())
|
|
28
|
-
}
|
|
29
|
-
this.sessionAgents.get(sessionId)!.add(agentId)
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Remove an agent from all session registrations and clear its message queue.
|
|
34
|
-
*/
|
|
35
|
-
unregisterAgent(agentId: string): void {
|
|
36
|
-
for (const [, agents] of this.sessionAgents) {
|
|
37
|
-
agents.delete(agentId)
|
|
38
|
-
}
|
|
39
|
-
this.queues.delete(agentId)
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Enqueue a message from senderAgentId to receiverAgentId.
|
|
44
|
-
*/
|
|
45
|
-
send(senderAgentId: string, receiverAgentId: string, content: string): void {
|
|
46
|
-
if (!this.queues.has(receiverAgentId)) {
|
|
47
|
-
this.queues.set(receiverAgentId, [])
|
|
48
|
-
}
|
|
49
|
-
this.queues.get(receiverAgentId)!.push({
|
|
50
|
-
sender: senderAgentId,
|
|
51
|
-
content,
|
|
52
|
-
timestamp: Date.now(),
|
|
53
|
-
})
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Retrieve and clear all pending messages for an agent.
|
|
58
|
-
* Messages older than the TTL (5 min) are discarded.
|
|
59
|
-
*/
|
|
60
|
-
poll(agentId: string): BusMessage[] {
|
|
61
|
-
const queue = this.queues.get(agentId)
|
|
62
|
-
if (!queue || queue.length === 0) return []
|
|
63
|
-
|
|
64
|
-
const now = Date.now()
|
|
65
|
-
const valid = queue.filter((msg) => now - msg.timestamp < this.ttlMs)
|
|
66
|
-
|
|
67
|
-
this.queues.delete(agentId)
|
|
68
|
-
return valid
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Send a message to every agent in a session except the one specified.
|
|
73
|
-
* The sender is recorded as 'system'.
|
|
74
|
-
*/
|
|
75
|
-
broadcastToSession(sessionId: string, content: string, excludeAgentId?: string): void {
|
|
76
|
-
const agents = this.sessionAgents.get(sessionId)
|
|
77
|
-
if (!agents) return
|
|
78
|
-
for (const agentId of agents) {
|
|
79
|
-
if (agentId !== excludeAgentId) {
|
|
80
|
-
this.send('system', agentId, content)
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|