@yandy0725/pi-subagents 0.5.4 → 0.6.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/README.md +43 -2
- package/README.zh.md +38 -2
- package/package.json +5 -5
- package/src/config/invocation-config.ts +9 -2
- package/src/config/model-tiers.ts +66 -0
- package/src/layered-settings.ts +10 -3
- package/src/service/service-adapter.ts +2 -2
- package/src/session/model-resolver.ts +9 -0
- package/src/settings.ts +52 -1
- package/src/tools/agent-tool.ts +17 -1
- package/src/tools/spawn-config.ts +20 -2
package/README.md
CHANGED
|
@@ -13,6 +13,7 @@ A [pi](https://pi.dev) extension providing a focused, in-process sub-agent core
|
|
|
13
13
|
- **Graceful turn limits** — agents get a "wrap up" warning before hard abort
|
|
14
14
|
- **Case-insensitive types** — `"explore"`, `"Explore"`, `"EXPLORE"` all work
|
|
15
15
|
- **Fuzzy model selection** — specify models by name (`"haiku"`, `"sonnet"`) instead of full IDs
|
|
16
|
+
- **Model tiers** — pick `fast` / `standard` / `advanced` and map each tier to a real model in config, instead of naming a model per call
|
|
16
17
|
- **Context inheritance** — optionally fork the parent conversation into a sub-agent
|
|
17
18
|
- **Styled notifications** — background results render as themed notification boxes
|
|
18
19
|
- **Event bus** — lifecycle events (`subagents:created`, `started`, `completed`, `failed`, `steered`, `compacted`) via `pi.events`
|
|
@@ -74,7 +75,8 @@ Launch a sub-agent.
|
|
|
74
75
|
| `prompt` | string | yes | The task for the agent |
|
|
75
76
|
| `description` | string | yes | Short 3-5 word summary (shown in UI) |
|
|
76
77
|
| `subagent_type` | string | yes | Agent type (built-in or custom) |
|
|
77
|
-
| `model` | string | no | Model override (`provider/modelId` or fuzzy name) |
|
|
78
|
+
| `model` | string | no | Model override (`provider/modelId` or fuzzy name). Mutually exclusive with `model_tier` |
|
|
79
|
+
| `model_tier` | string | no | Capability tier: `fast` / `standard` / `advanced`. Mutually exclusive with `model` |
|
|
78
80
|
| `thinking` | string | no | off / minimal / low / medium / high / xhigh |
|
|
79
81
|
| `max_turns` | number | no | Max agentic turns (unlimited by default) |
|
|
80
82
|
| `run_in_background` | boolean | no | Run without blocking |
|
|
@@ -100,6 +102,37 @@ Send a steering message to a running agent.
|
|
|
100
102
|
| `agent_id` | string | yes | Agent ID to steer |
|
|
101
103
|
| `message` | string | yes | Message to inject into agent conversation |
|
|
102
104
|
|
|
105
|
+
## Model Tiers
|
|
106
|
+
|
|
107
|
+
The `subagent` tool accepts `model_tier` as an alternative to `model`: the caller asks for a
|
|
108
|
+
capability class and each user decides which real model sits behind it.
|
|
109
|
+
|
|
110
|
+
| Tier | Intent |
|
|
111
|
+
|------|--------|
|
|
112
|
+
| `fast` | Cheapest and fastest, least capable (flash / mini / haiku class) |
|
|
113
|
+
| `standard` | Balanced default (sonnet class) |
|
|
114
|
+
| `advanced` | Most capable, most expensive (opus class) |
|
|
115
|
+
|
|
116
|
+
The mapping lives in `subagents.json` — `~/.pi/agent/subagents.json` (global) and
|
|
117
|
+
`<cwd>/.pi/subagents.json` (project). The two layers merge **per tier**, so a project can
|
|
118
|
+
override one tier without restating the others:
|
|
119
|
+
|
|
120
|
+
```json
|
|
121
|
+
{
|
|
122
|
+
"modelTiers": {
|
|
123
|
+
"fast": "deepseek-flash",
|
|
124
|
+
"standard": "deepseek-flash",
|
|
125
|
+
"advanced": "qwen3.8-max"
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
Tier values go through the same resolver as `model`, so `provider/modelId` and fuzzy names
|
|
131
|
+
(e.g. `"haiku"`) both work. `model` and `model_tier` are mutually exclusive, and calling a
|
|
132
|
+
tier that is not configured returns an error naming the file to fix — a tier request is
|
|
133
|
+
never silently downgraded to the parent model. If an agent type pins `model` in its
|
|
134
|
+
frontmatter, that model wins over both parameters.
|
|
135
|
+
|
|
103
136
|
## Commands
|
|
104
137
|
|
|
105
138
|
| Command | Description |
|
|
@@ -113,13 +146,21 @@ collapsed form (summaries and previews), so a `read` call is represented by its
|
|
|
113
146
|
call line only. The viewer has no expand key, so the "… to expand" hints some
|
|
114
147
|
tools print do not apply here.
|
|
115
148
|
|
|
149
|
+
The viewer follows the live tail: the newest lines stay pinned to the bottom until you scroll up.
|
|
150
|
+
Upward keys (`↑`/`k`, `PgUp`/`shift+up`, `Home`) **unconditionally** pause following — streaming can
|
|
151
|
+
add lines between a keypress and the render, so a "was I at the bottom?" test against that stale
|
|
152
|
+
value would snap a viewport you just left back down. A downward key that lands on the bottom
|
|
153
|
+
(`↓`/`j`, `PgDn`/`shift+down`) resumes following, and `end` jumps to the bottom and force-resumes;
|
|
154
|
+
`q`/`Esc` closes. The paused state has **no visual indicator** (the footer keeps showing
|
|
155
|
+
`<n> lines · <pct>%` and the key hints).
|
|
156
|
+
|
|
116
157
|
## Concurrency
|
|
117
158
|
|
|
118
159
|
Background agents are subject to a configurable concurrency limit (default: 4). Excess agents queue automatically. Foreground agents bypass the queue.
|
|
119
160
|
|
|
120
161
|
## Persistent Settings
|
|
121
162
|
|
|
122
|
-
Settings set via `/subagents:settings` persist in `<cwd>/.pi/subagents.json` (project) and can be overridden globally in `~/.pi/agent/subagents.json`.
|
|
163
|
+
Settings set via `/subagents:settings` persist in `<cwd>/.pi/subagents.json` (project) and can be overridden globally in `~/.pi/agent/subagents.json`. The same files also hold the [`modelTiers`](#model-tiers) mapping; saving other settings preserves it.
|
|
123
164
|
|
|
124
165
|
## Events
|
|
125
166
|
|
package/README.zh.md
CHANGED
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
- **优雅轮次限制** — 代理在硬中止前收到"收尾"警告
|
|
14
14
|
- **大小写不敏感** — `"explore"`、`"Explore"`、`"EXPLORE"` 均可
|
|
15
15
|
- **模糊模型选择** — 按名称(`"haiku"`、`"sonnet"`)而非完整 ID 指定模型
|
|
16
|
+
- **模型分层** — 用 `fast` / `standard` / `advanced` 档位代替逐次点名模型,每档对应的真实模型写在配置里
|
|
16
17
|
- **上下文继承** — 可选择将父对话分叉给子代理
|
|
17
18
|
- **风格化通知** — 后台结果渲染为主题化通知框
|
|
18
19
|
- **事件总线** — 通过 `pi.events` 发出生命周期事件(`subagents:created`、`started`、`completed`、`failed`、`steered`、`compacted`)
|
|
@@ -74,7 +75,8 @@ max_turns: 30
|
|
|
74
75
|
| `prompt` | string | 是 | 代理要执行的任务 |
|
|
75
76
|
| `description` | string | 是 | 3-5 词简短描述(UI 中显示) |
|
|
76
77
|
| `subagent_type` | string | 是 | 代理类型(内置或自定义) |
|
|
77
|
-
| `model` | string | 否 | 模型覆盖(`provider/modelId`
|
|
78
|
+
| `model` | string | 否 | 模型覆盖(`provider/modelId` 或模糊名称;与 `model_tier` 互斥) |
|
|
79
|
+
| `model_tier` | string | 否 | 能力档位:`fast` / `standard` / `advanced`(与 `model` 互斥) |
|
|
78
80
|
| `thinking` | string | 否 | off / minimal / low / medium / high / xhigh |
|
|
79
81
|
| `max_turns` | number | 否 | 最大代理轮次(默认无限制) |
|
|
80
82
|
| `run_in_background` | boolean | 否 | 后台运行不阻塞 |
|
|
@@ -100,6 +102,34 @@ max_turns: 30
|
|
|
100
102
|
| `agent_id` | string | 是 | 要转向的代理 ID |
|
|
101
103
|
| `message` | string | 是 | 注入代理对话的消息 |
|
|
102
104
|
|
|
105
|
+
## 模型分层
|
|
106
|
+
|
|
107
|
+
`subagent` 工具除 `model` 外还接受 `model_tier`:调用方只表达"要什么档位的模型",
|
|
108
|
+
由使用者在配置里决定每档对应哪个真实模型。
|
|
109
|
+
|
|
110
|
+
| 档位 | 定位 |
|
|
111
|
+
|------|------|
|
|
112
|
+
| `fast` | 最便宜、最快,能力最弱(flash / mini / haiku 级) |
|
|
113
|
+
| `standard` | 均衡的默认档(sonnet 级) |
|
|
114
|
+
| `advanced` | 能力最强、最贵(opus 级) |
|
|
115
|
+
|
|
116
|
+
映射写在 `subagents.json`:全局 `~/.pi/agent/subagents.json`,项目 `<cwd>/.pi/subagents.json`。
|
|
117
|
+
两层**按档位合并**,项目只改一档时无需重复其余档位:
|
|
118
|
+
|
|
119
|
+
```json
|
|
120
|
+
{
|
|
121
|
+
"modelTiers": {
|
|
122
|
+
"fast": "deepseek-flash",
|
|
123
|
+
"standard": "deepseek-flash",
|
|
124
|
+
"advanced": "qwen3.8-max"
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
档位取值与 `model` 走同一套解析,`provider/modelId` 和模糊名称(如 `"haiku"`)都可以。
|
|
130
|
+
`model` 与 `model_tier` 互斥;调用未配置的档位会直接报错并指出该改哪个文件——不会静默
|
|
131
|
+
降级为父模型。若代理类型的 frontmatter 固定了 `model`,该模型优先于两个参数。
|
|
132
|
+
|
|
103
133
|
## 命令
|
|
104
134
|
|
|
105
135
|
| 命令 | 描述 |
|
|
@@ -111,13 +141,19 @@ max_turns: 30
|
|
|
111
141
|
工具结果按 Pi 的折叠形态显示(摘要与预览),因此 `read` 调用只显示其调用行。
|
|
112
142
|
查看器没有展开键,部分工具调用行里提示的"to expand"键在此不适用。
|
|
113
143
|
|
|
144
|
+
查看器跟随实时尾部:未滚动时最新行始终贴在底部。向上键(`↑`/`k`、`PgUp`/`shift+up`、`Home`)会
|
|
145
|
+
**无条件**暂停跟随 —— 流式输出可能在按键与渲染之间新增行,用那个已经过期的值去判断"我是不是在底部"
|
|
146
|
+
会把刚刚离开的视口又拽回底部。向下键落到底部(`↓`/`j`、`PgDn`/`shift+down`)即恢复跟随,`end`
|
|
147
|
+
直接跳到底部并强制恢复;`q`/`Esc` 关闭。暂停状态**没有可视指示**(footer 始终显示 `<n> lines · <pct>%`
|
|
148
|
+
与键位提示)。
|
|
149
|
+
|
|
114
150
|
## 并发控制
|
|
115
151
|
|
|
116
152
|
后台代理受可配置并发限制约束(默认 4)。超出限制的代理自动排队。前台代理绕过队列。
|
|
117
153
|
|
|
118
154
|
## 持久化设置
|
|
119
155
|
|
|
120
|
-
通过 `/subagents:settings` 设置的参数持久化到 `<cwd>/.pi/subagents.json`(项目级),也可在 `~/.pi/agent/subagents.json`
|
|
156
|
+
通过 `/subagents:settings` 设置的参数持久化到 `<cwd>/.pi/subagents.json`(项目级),也可在 `~/.pi/agent/subagents.json` 中设置全局默认值。同一批文件也保存 [`modelTiers`](#模型分层) 映射;保存其他设置时会保留它。
|
|
121
157
|
|
|
122
158
|
## 事件
|
|
123
159
|
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "0.
|
|
6
|
+
"version": "0.6.0",
|
|
7
7
|
"description": "Focused, in-process sub-agent core for pi — autonomous agents plus a typed API and lifecycle events",
|
|
8
8
|
"license": "MIT",
|
|
9
9
|
"repository": {
|
|
@@ -38,10 +38,10 @@
|
|
|
38
38
|
"typebox": "*"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@biomejs/biome": "^2.5.
|
|
42
|
-
"@types/node": "^26.6.
|
|
43
|
-
"typebox": "^1.3.
|
|
41
|
+
"@biomejs/biome": "^2.5.15",
|
|
42
|
+
"@types/node": "^26.6.5",
|
|
43
|
+
"typebox": "^1.3.37",
|
|
44
44
|
"typescript": "~7.0.2",
|
|
45
|
-
"vitest": "^5.0.
|
|
45
|
+
"vitest": "^5.0.4"
|
|
46
46
|
}
|
|
47
47
|
}
|
|
@@ -2,6 +2,7 @@ import type { AgentConfig, ThinkingLevel } from "../types";
|
|
|
2
2
|
|
|
3
3
|
interface AgentInvocationParams {
|
|
4
4
|
model?: string;
|
|
5
|
+
model_tier?: string;
|
|
5
6
|
thinking?: string;
|
|
6
7
|
max_turns?: number;
|
|
7
8
|
run_in_background?: boolean;
|
|
@@ -11,6 +12,12 @@ interface AgentInvocationParams {
|
|
|
11
12
|
export function resolveAgentInvocationConfig(
|
|
12
13
|
agentConfig: AgentConfig | undefined,
|
|
13
14
|
params: AgentInvocationParams,
|
|
15
|
+
/**
|
|
16
|
+
* Model reference already resolved from `params.model_tier` by the caller
|
|
17
|
+
* (spawn-config resolves the tier so it can surface config errors). Precedence
|
|
18
|
+
* matches `params.model`: agent config wins, then params, then the tier.
|
|
19
|
+
*/
|
|
20
|
+
tierModel?: string,
|
|
14
21
|
): {
|
|
15
22
|
modelInput?: string;
|
|
16
23
|
modelFromParams: boolean;
|
|
@@ -20,8 +27,8 @@ export function resolveAgentInvocationConfig(
|
|
|
20
27
|
runInBackground: boolean;
|
|
21
28
|
} {
|
|
22
29
|
return {
|
|
23
|
-
modelInput: agentConfig?.model ?? params.model,
|
|
24
|
-
modelFromParams: agentConfig?.model == null && params.model != null,
|
|
30
|
+
modelInput: agentConfig?.model ?? params.model ?? tierModel,
|
|
31
|
+
modelFromParams: agentConfig?.model == null && (params.model ?? tierModel) != null,
|
|
25
32
|
thinking: (agentConfig?.thinking ?? params.thinking) as ThinkingLevel | undefined,
|
|
26
33
|
maxTurns: agentConfig?.maxTurns ?? params.max_turns,
|
|
27
34
|
inheritContext: agentConfig?.inheritContext ?? params.inherit_context ?? false,
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* model-tiers.ts — Capability/cost tiers for the subagent tool's `model_tier` param.
|
|
3
|
+
*
|
|
4
|
+
* A tier is an indirection: the calling agent asks for a class of model
|
|
5
|
+
* (`fast` | `standard` | `advanced`) and the user decides which real model each
|
|
6
|
+
* class maps to in `subagents.json` (`modelTiers`). See README for the intended
|
|
7
|
+
* semantics of each tier.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export const MODEL_TIERS = ["fast", "standard", "advanced"] as const;
|
|
11
|
+
|
|
12
|
+
export type ModelTier = (typeof MODEL_TIERS)[number];
|
|
13
|
+
|
|
14
|
+
/** Partial tier → model mapping as configured in `subagents.json`. */
|
|
15
|
+
export type ModelTierMap = Partial<Record<ModelTier, string>>;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Case-insensitively map a raw value to a canonical tier.
|
|
19
|
+
* Returns undefined for unknown names and non-strings.
|
|
20
|
+
*/
|
|
21
|
+
export function normalizeModelTier(value: unknown): ModelTier | undefined {
|
|
22
|
+
if (typeof value !== "string") return undefined;
|
|
23
|
+
const lower = value.trim().toLowerCase();
|
|
24
|
+
return (MODEL_TIERS as readonly string[]).includes(lower) ? (lower as ModelTier) : undefined;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Successful resolution — `model` is the configured model reference, not a Model instance. */
|
|
28
|
+
export interface ModelTierResolutionResult {
|
|
29
|
+
model: string;
|
|
30
|
+
error?: undefined;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Failed resolution: unknown tier name or no mapping configured for a known tier. */
|
|
34
|
+
export interface ModelTierResolutionError {
|
|
35
|
+
model?: undefined;
|
|
36
|
+
error: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export type ModelTierResolution = ModelTierResolutionResult | ModelTierResolutionError;
|
|
40
|
+
|
|
41
|
+
/** Where users configure the tier map — repeated in error messages. */
|
|
42
|
+
const SETTINGS_HINT = "Configure it in .pi/subagents.json or ~/.pi/agent/subagents.json";
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Resolve a `model_tier` param value to the model reference configured for it.
|
|
46
|
+
* An unknown tier or an unconfigured mapping is an explicit error — a tier
|
|
47
|
+
* request is never silently downgraded to another model.
|
|
48
|
+
*/
|
|
49
|
+
export function resolveModelTier(input: unknown, tiers: ModelTierMap | undefined): ModelTierResolution {
|
|
50
|
+
const tier = normalizeModelTier(input);
|
|
51
|
+
if (!tier) {
|
|
52
|
+
return {
|
|
53
|
+
error: `Unknown model tier: "${String(input)}". Valid tiers: ${MODEL_TIERS.join(", ")}.`,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const configured = tiers?.[tier];
|
|
58
|
+
const model = typeof configured === "string" ? configured.trim() : "";
|
|
59
|
+
if (!model) {
|
|
60
|
+
return {
|
|
61
|
+
error: `Model tier "${tier}" is not configured. ${SETTINGS_HINT}, e.g. {"modelTiers": {"${tier}": "provider/modelId"}}.`,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return { model };
|
|
66
|
+
}
|
package/src/layered-settings.ts
CHANGED
|
@@ -67,6 +67,12 @@ export interface LayeredSettingsSource<T> {
|
|
|
67
67
|
* e.g. `"pi-subagents"` → `"[pi-subagents] Ignoring malformed settings at …"`.
|
|
68
68
|
*/
|
|
69
69
|
warnLabel: string;
|
|
70
|
+
/**
|
|
71
|
+
* Optional merge of the two layers (global first, project second).
|
|
72
|
+
* Defaults to a shallow spread with project keys winning. Supply this to
|
|
73
|
+
* merge nested objects field-by-field instead of replacing them wholesale.
|
|
74
|
+
*/
|
|
75
|
+
merge?: (global: Partial<T>, project: Partial<T>) => Partial<T>;
|
|
70
76
|
}
|
|
71
77
|
|
|
72
78
|
/**
|
|
@@ -75,17 +81,18 @@ export interface LayeredSettingsSource<T> {
|
|
|
75
81
|
* - A missing file is silent — returns `{}` for that layer.
|
|
76
82
|
* - A file that exists but cannot be parsed warns to stderr and returns `{}` for
|
|
77
83
|
* that layer, so startup proceeds normally.
|
|
78
|
-
* - The two layers are merged with a shallow spread; project keys win.
|
|
84
|
+
* - The two layers are merged with a shallow spread; project keys win. Pass
|
|
85
|
+
* `merge` to override that (e.g. to merge nested objects per field).
|
|
79
86
|
*
|
|
80
87
|
* Throws nothing. All error conditions produce a warning and fall back to `{}`.
|
|
81
88
|
*
|
|
82
89
|
* @public
|
|
83
90
|
*/
|
|
84
91
|
export function loadLayeredSettings<T>(source: LayeredSettingsSource<T>): Partial<T> {
|
|
85
|
-
const { agentDir, cwd, filename, sanitize, warnLabel } = source;
|
|
92
|
+
const { agentDir, cwd, filename, sanitize, warnLabel, merge } = source;
|
|
86
93
|
const global = readLayer(join(agentDir, filename), sanitize, warnLabel);
|
|
87
94
|
const project = readLayer(join(cwd, ".pi", filename), sanitize, warnLabel);
|
|
88
|
-
return { ...global, ...project };
|
|
95
|
+
return merge ? merge(global, project) : { ...global, ...project };
|
|
89
96
|
}
|
|
90
97
|
|
|
91
98
|
// ── Private helpers ──────────────────────────────────────────────────────────
|
|
@@ -102,8 +102,8 @@ export class SubagentsServiceAdapter implements SubagentsService {
|
|
|
102
102
|
if (record?.status !== "running") {
|
|
103
103
|
return false;
|
|
104
104
|
}
|
|
105
|
-
|
|
106
|
-
return
|
|
105
|
+
// 透传 Subagent.steer() 的返回:true = 已投递到会话,false = 会话未就绪已入队缓冲。
|
|
106
|
+
return record.steer(message);
|
|
107
107
|
}
|
|
108
108
|
|
|
109
109
|
async waitForAll(): Promise<void> {
|
|
@@ -61,6 +61,11 @@ export function resolveInvocationModel(
|
|
|
61
61
|
export function resolveModel(input: string, registry: ModelRegistry): any | string {
|
|
62
62
|
// Available models (those with auth configured)
|
|
63
63
|
const all = (registry.getAvailable?.() ?? registry.getAll()) as ModelEntry[];
|
|
64
|
+
|
|
65
|
+
// 空查询会让 substring/every 判定对每个模型恒真,从而任意命中第一个模型;显式拒绝。
|
|
66
|
+
if (input.trim() === "") {
|
|
67
|
+
return noMatchError(input, all);
|
|
68
|
+
}
|
|
64
69
|
const availableSet = new Set(all.map((m) => `${m.provider}/${m.id}`.toLowerCase()));
|
|
65
70
|
|
|
66
71
|
// 1. Exact match: "provider/modelId" — only if available (has auth)
|
|
@@ -113,6 +118,10 @@ export function resolveModel(input: string, registry: ModelRegistry): any | stri
|
|
|
113
118
|
}
|
|
114
119
|
|
|
115
120
|
// 3. No match — list available models
|
|
121
|
+
return noMatchError(input, all);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function noMatchError(input: string, all: ModelEntry[]): string {
|
|
116
125
|
const modelList = all
|
|
117
126
|
.map((m) => ` ${m.provider}/${m.id}`)
|
|
118
127
|
.sort()
|
package/src/settings.ts
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
import { mkdirSync, writeFileSync } from "node:fs";
|
|
6
6
|
import { dirname, join } from "node:path";
|
|
7
|
+
import { MODEL_TIERS, type ModelTierMap } from "./config/model-tiers";
|
|
7
8
|
import { type LayeredSettingsSource, loadLayeredSettings } from "./layered-settings";
|
|
8
9
|
export interface SubagentsSettings {
|
|
9
10
|
maxConcurrent?: number;
|
|
@@ -14,6 +15,13 @@ export interface SubagentsSettings {
|
|
|
14
15
|
*/
|
|
15
16
|
defaultMaxTurns?: number;
|
|
16
17
|
graceTurns?: number;
|
|
18
|
+
/**
|
|
19
|
+
* Capability/cost tier → real model mapping for the `subagent` tool's
|
|
20
|
+
* `model_tier` param. Tier names are fixed (`fast` | `standard` | `advanced`);
|
|
21
|
+
* values accept anything `resolveModel` understands (e.g. "provider/modelId"
|
|
22
|
+
* or a fuzzy name like "haiku").
|
|
23
|
+
*/
|
|
24
|
+
modelTiers?: ModelTierMap;
|
|
17
25
|
}
|
|
18
26
|
|
|
19
27
|
/** Emit callback — a subset of `pi.events.emit` to keep helpers testable. */
|
|
@@ -30,6 +38,7 @@ export class SettingsManager {
|
|
|
30
38
|
private _defaultMaxTurns: number | undefined = undefined;
|
|
31
39
|
private _graceTurns: number = DEFAULT_GRACE_TURNS;
|
|
32
40
|
private _maxConcurrent: number = DEFAULT_MAX_CONCURRENT;
|
|
41
|
+
private _modelTiers: ModelTierMap = {};
|
|
33
42
|
|
|
34
43
|
private readonly emit: SettingsEmit;
|
|
35
44
|
private readonly cwd: string;
|
|
@@ -77,6 +86,12 @@ export class SettingsManager {
|
|
|
77
86
|
this._maxConcurrent = Math.max(1, n);
|
|
78
87
|
}
|
|
79
88
|
|
|
89
|
+
// ── modelTiers: tier → model map for the subagent tool's model_tier param ──
|
|
90
|
+
|
|
91
|
+
get modelTiers(): ModelTierMap {
|
|
92
|
+
return this._modelTiers;
|
|
93
|
+
}
|
|
94
|
+
|
|
80
95
|
// ── Lifecycle methods ──
|
|
81
96
|
|
|
82
97
|
/**
|
|
@@ -89,6 +104,7 @@ export class SettingsManager {
|
|
|
89
104
|
if (typeof settings.maxConcurrent === "number") this.maxConcurrent = settings.maxConcurrent;
|
|
90
105
|
if (typeof settings.defaultMaxTurns === "number") this.defaultMaxTurns = settings.defaultMaxTurns;
|
|
91
106
|
if (typeof settings.graceTurns === "number") this.graceTurns = settings.graceTurns;
|
|
107
|
+
this._modelTiers = settings.modelTiers ? { ...settings.modelTiers } : {};
|
|
92
108
|
this.emit("subagents:settings_loaded", { settings });
|
|
93
109
|
return settings;
|
|
94
110
|
}
|
|
@@ -96,12 +112,15 @@ export class SettingsManager {
|
|
|
96
112
|
/**
|
|
97
113
|
* Snapshot current in-memory values for persistence.
|
|
98
114
|
* `defaultMaxTurns` uses 0 as the on-disk marker for unlimited (undefined).
|
|
115
|
+
* `modelTiers` is omitted when empty so hand-edited tier maps are only
|
|
116
|
+
* rewritten (not replaced with an empty object) when they exist.
|
|
99
117
|
*/
|
|
100
|
-
snapshot(): { maxConcurrent: number; defaultMaxTurns: number; graceTurns: number } {
|
|
118
|
+
snapshot(): { maxConcurrent: number; defaultMaxTurns: number; graceTurns: number; modelTiers?: ModelTierMap } {
|
|
101
119
|
return {
|
|
102
120
|
maxConcurrent: this._maxConcurrent,
|
|
103
121
|
defaultMaxTurns: this._defaultMaxTurns ?? 0,
|
|
104
122
|
graceTurns: this._graceTurns,
|
|
123
|
+
...(Object.keys(this._modelTiers).length > 0 ? { modelTiers: { ...this._modelTiers } } : {}),
|
|
105
124
|
};
|
|
106
125
|
}
|
|
107
126
|
|
|
@@ -178,9 +197,27 @@ function sanitize(raw: unknown): SubagentsSettings {
|
|
|
178
197
|
) {
|
|
179
198
|
out.graceTurns = r.graceTurns as number;
|
|
180
199
|
}
|
|
200
|
+
const modelTiers = sanitizeModelTiers(r.modelTiers);
|
|
201
|
+
if (modelTiers) out.modelTiers = modelTiers;
|
|
181
202
|
return out;
|
|
182
203
|
}
|
|
183
204
|
|
|
205
|
+
/**
|
|
206
|
+
* Keep only known tier names with non-empty string values (trimmed).
|
|
207
|
+
* Returns `undefined` when nothing survives — an empty map is the same as
|
|
208
|
+
* "not configured" everywhere else.
|
|
209
|
+
*/
|
|
210
|
+
function sanitizeModelTiers(raw: unknown): ModelTierMap | undefined {
|
|
211
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return undefined;
|
|
212
|
+
const source = raw as Record<string, unknown>;
|
|
213
|
+
const out: ModelTierMap = {};
|
|
214
|
+
for (const tier of MODEL_TIERS) {
|
|
215
|
+
const value = source[tier];
|
|
216
|
+
if (typeof value === "string" && value.trim()) out[tier] = value.trim();
|
|
217
|
+
}
|
|
218
|
+
return Object.keys(out).length > 0 ? out : undefined;
|
|
219
|
+
}
|
|
220
|
+
|
|
184
221
|
function projectPath(cwd: string): string {
|
|
185
222
|
return join(cwd, ".pi", "subagents.json");
|
|
186
223
|
}
|
|
@@ -193,9 +230,23 @@ export function loadSettings(agentDir: string, cwd: string): SubagentsSettings {
|
|
|
193
230
|
filename: "subagents.json",
|
|
194
231
|
sanitize,
|
|
195
232
|
warnLabel: "pi-subagents",
|
|
233
|
+
merge: mergeSettings,
|
|
196
234
|
} satisfies LayeredSettingsSource<SubagentsSettings>);
|
|
197
235
|
}
|
|
198
236
|
|
|
237
|
+
/**
|
|
238
|
+
* Shallow-merge scalars (project wins), but merge `modelTiers` per tier so a
|
|
239
|
+
* project can override one tier without restating the global map.
|
|
240
|
+
*/
|
|
241
|
+
function mergeSettings(global: SubagentsSettings, project: SubagentsSettings): SubagentsSettings {
|
|
242
|
+
const modelTiers: ModelTierMap = { ...global.modelTiers, ...project.modelTiers };
|
|
243
|
+
return {
|
|
244
|
+
...global,
|
|
245
|
+
...project,
|
|
246
|
+
...(Object.keys(modelTiers).length > 0 ? { modelTiers } : {}),
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
|
|
199
250
|
/**
|
|
200
251
|
* Write project-local settings. Global is never touched from code.
|
|
201
252
|
* Returns `true` on success, `false` if the write (or mkdir) failed so the
|
package/src/tools/agent-tool.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { defineTool } from "@earendil-works/pi-coding-agent";
|
|
|
4
4
|
import { Text } from "@earendil-works/pi-tui";
|
|
5
5
|
import { Type } from "typebox";
|
|
6
6
|
import type { AgentTypeRegistry } from "../config/agent-types";
|
|
7
|
+
import { MODEL_TIERS, type ModelTierMap } from "../config/model-tiers";
|
|
7
8
|
import type { ParentSnapshot } from "../lifecycle/parent-snapshot";
|
|
8
9
|
import type { AgentSpawnConfig } from "../lifecycle/subagent-manager";
|
|
9
10
|
import { spawnBackground } from "../tools/background-spawner";
|
|
@@ -40,8 +41,15 @@ export interface AgentToolRuntime {
|
|
|
40
41
|
export type AgentToolSettings = {
|
|
41
42
|
readonly defaultMaxTurns: number | undefined;
|
|
42
43
|
readonly maxConcurrent: number;
|
|
44
|
+
readonly modelTiers?: ModelTierMap;
|
|
43
45
|
};
|
|
44
46
|
|
|
47
|
+
/** LLM-facing summary of which tiers are configured, e.g. `fast → deepseek-flash`. */
|
|
48
|
+
function buildTierListText(tiers: ModelTierMap | undefined): string {
|
|
49
|
+
const configured = MODEL_TIERS.filter((tier) => tiers?.[tier]).map((tier) => `${tier} → ${tiers?.[tier]}`);
|
|
50
|
+
return configured.length > 0 ? configured.join(", ") : "none configured";
|
|
51
|
+
}
|
|
52
|
+
|
|
45
53
|
// ---- Class ----
|
|
46
54
|
|
|
47
55
|
export class AgentTool {
|
|
@@ -113,6 +121,7 @@ export class AgentTool {
|
|
|
113
121
|
toToolDefinition() {
|
|
114
122
|
const typeListText = this.typeListText;
|
|
115
123
|
const availableTypesText = this.availableTypesText;
|
|
124
|
+
const tierListText = buildTierListText(this.settings.modelTiers);
|
|
116
125
|
const registry = this.registry;
|
|
117
126
|
|
|
118
127
|
return defineTool({
|
|
@@ -134,6 +143,7 @@ Guidelines:
|
|
|
134
143
|
- Use resume with an agent ID to continue a previous agent's work.
|
|
135
144
|
- Use steer_subagent to send mid-run messages to a running background agent.
|
|
136
145
|
- Use model to specify a different model (as "provider/modelId", or fuzzy e.g. "haiku", "sonnet").
|
|
146
|
+
- Use model_tier to pick a preconfigured capability tier instead of naming a model — never pass both. Tiers: fast (cheapest, fastest, least capable), standard (balanced default), advanced (most capable, most expensive). Configured: ${tierListText}.
|
|
137
147
|
- Use thinking to control extended thinking level.
|
|
138
148
|
- Use inherit_context if the agent needs the parent conversation history.
|
|
139
149
|
`,
|
|
@@ -150,7 +160,13 @@ Guidelines:
|
|
|
150
160
|
model: Type.Optional(
|
|
151
161
|
Type.String({
|
|
152
162
|
description:
|
|
153
|
-
'Optional model override. Accepts "provider/modelId" or fuzzy name (e.g. "haiku", "sonnet"). Omit to use the agent type\'s default.',
|
|
163
|
+
'Optional model override. Accepts "provider/modelId" or fuzzy name (e.g. "haiku", "sonnet"). Omit to use the agent type\'s default. Mutually exclusive with model_tier.',
|
|
164
|
+
}),
|
|
165
|
+
),
|
|
166
|
+
model_tier: Type.Optional(
|
|
167
|
+
Type.Union([Type.Literal("fast"), Type.Literal("standard"), Type.Literal("advanced")], {
|
|
168
|
+
description:
|
|
169
|
+
'Optional capability tier: "fast" (cheapest and fastest, least capable), "standard" (balanced default), or "advanced" (most capable, most expensive). Mutually exclusive with model. The real model behind each tier is configured in subagents.json (modelTiers).',
|
|
154
170
|
}),
|
|
155
171
|
),
|
|
156
172
|
thinking: Type.Optional(
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
import type { Model } from "@earendil-works/pi-ai";
|
|
11
11
|
import type { AgentTypeRegistry } from "../config/agent-types";
|
|
12
12
|
import { resolveAgentInvocationConfig } from "../config/invocation-config";
|
|
13
|
+
import { type ModelTierMap, resolveModelTier } from "../config/model-tiers";
|
|
13
14
|
import { normalizeMaxTurns } from "../lifecycle/turn-limits";
|
|
14
15
|
import { resolveInvocationModel } from "../session/model-resolver";
|
|
15
16
|
import type { AgentInvocation, SubagentType, ThinkingLevel } from "../types";
|
|
@@ -81,7 +82,7 @@ export function resolveSpawnConfig(
|
|
|
81
82
|
params: Record<string, unknown>,
|
|
82
83
|
registry: AgentTypeRegistry,
|
|
83
84
|
modelInfo: ModelInfo,
|
|
84
|
-
settings: { readonly defaultMaxTurns: number | undefined },
|
|
85
|
+
settings: { readonly defaultMaxTurns: number | undefined; readonly modelTiers?: ModelTierMap },
|
|
85
86
|
): ResolvedSpawnConfig | SpawnConfigError {
|
|
86
87
|
const rawType = params.subagent_type as SubagentType;
|
|
87
88
|
const resolved = registry.resolveType(rawType);
|
|
@@ -96,9 +97,26 @@ export function resolveSpawnConfig(
|
|
|
96
97
|
|
|
97
98
|
const displayName = getDisplayName(subagentType, registry);
|
|
98
99
|
|
|
100
|
+
// `model` and `model_tier` are two ways to express the same choice — never both.
|
|
101
|
+
const tierInput = params.model_tier;
|
|
102
|
+
if (params.model != null && tierInput != null) {
|
|
103
|
+
return { error: 'Specify either "model" or "model_tier" (capability tier), not both.' };
|
|
104
|
+
}
|
|
105
|
+
|
|
99
106
|
// Merge agent config defaults with tool-call params
|
|
100
107
|
const customConfig = registry.resolveAgentConfig(subagentType);
|
|
101
|
-
|
|
108
|
+
|
|
109
|
+
// A model pinned by the agent type wins over tool-call params, so the tier is
|
|
110
|
+
// only resolved when it can actually apply (mirrors how a locked config model
|
|
111
|
+
// makes an unresolvable `model` param a silent no-op).
|
|
112
|
+
let tierModel: string | undefined;
|
|
113
|
+
if (tierInput != null && customConfig?.model == null) {
|
|
114
|
+
const tierResolution = resolveModelTier(tierInput, settings.modelTiers);
|
|
115
|
+
if (tierResolution.error) return { error: tierResolution.error };
|
|
116
|
+
tierModel = tierResolution.model;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const resolvedConfig = resolveAgentInvocationConfig(customConfig, params, tierModel);
|
|
102
120
|
|
|
103
121
|
// Resolve model
|
|
104
122
|
const resolution = resolveInvocationModel(
|