@hex4c59/pi-ext-copilot-auto 0.0.0-stage → 0.1.1
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/CHANGELOG.md +9 -0
- package/LICENSE +21 -0
- package/README.md +316 -3
- package/docs/PROTOCOL.md +860 -0
- package/extensions/copilot-auto.ts +341 -0
- package/lib/account.ts +157 -0
- package/lib/auth.ts +156 -0
- package/lib/catalog.ts +299 -0
- package/lib/client.ts +516 -0
- package/lib/debug.ts +39 -0
- package/lib/protocol.ts +243 -0
- package/lib/sessions.ts +182 -0
- package/lib/stream.ts +306 -0
- package/lib/token.ts +83 -0
- package/package.json +46 -6
package/lib/catalog.ts
ADDED
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copilot 模型目录与 Auto 模型元数据。
|
|
3
|
+
*
|
|
4
|
+
* 目录来自 `/models`,其中的 `capabilities`、`billing.token_prices` 等字段
|
|
5
|
+
* 决定 pi 侧的 contextWindow、maxTokens、图片支持与成本。`/models` 会随
|
|
6
|
+
* 客户端版本返回不同条目,因此所有字段都按可选处理并提供保守回退。
|
|
7
|
+
*
|
|
8
|
+
* `/auto` 协商时服务端会直接下发选中模型的同类对象,因此这里的映射函数
|
|
9
|
+
* 同时服务于「目录条目」与「路由结果」两种来源。
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import type { CatalogEntry } from './protocol.ts'
|
|
13
|
+
|
|
14
|
+
/** pi 侧一个模型定义所需的元数据,已与协议解耦。 */
|
|
15
|
+
export interface ModelSpec {
|
|
16
|
+
id: string
|
|
17
|
+
name: string
|
|
18
|
+
/** 是否支持思考级别。由 `capabilities.supports.reasoning_effort` 推断。 */
|
|
19
|
+
reasoning: boolean
|
|
20
|
+
/** pi 思考级别到此模型 `reasoning.effort` 值的映射。 */
|
|
21
|
+
thinkingLevelMap?: Record<string, string | null>
|
|
22
|
+
input: ('text' | 'image')[]
|
|
23
|
+
cost: { input: number; output: number; cacheRead: number; cacheWrite: number }
|
|
24
|
+
contextWindow: number
|
|
25
|
+
maxTokens: number
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** 保守回退值:目录缺失时仍能发出可用请求。 */
|
|
29
|
+
const FALLBACK = {
|
|
30
|
+
contextWindow: 128_000,
|
|
31
|
+
maxTokens: 16_384,
|
|
32
|
+
} as const
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Auto 条目的保守上限。
|
|
36
|
+
*
|
|
37
|
+
* `contextWindow`(无候选时):Auto 的上下文取决于服务端选中的模型,
|
|
38
|
+
* 取候选最小值最安全;实测候选最小为 256000,因此这里用 256000。
|
|
39
|
+
*
|
|
40
|
+
* `maxTokens` 必须单独设小:pi 的压缩摘要请求会以 `model.maxTokens`
|
|
41
|
+
* 为输出上限。若沿用目录里的 128000,服务端需要为超长会话生成极大响应,
|
|
42
|
+
* 实测等待约 175 秒后连接被关闭(`UND_ERR_SOCKET`),压缩失败。
|
|
43
|
+
* 摘要本身 16k 足够,取所有候选输出上限与 16k 的较小者。
|
|
44
|
+
*/
|
|
45
|
+
const AUTO_FALLBACK = {
|
|
46
|
+
contextWindow: 256_000,
|
|
47
|
+
maxTokens: 16_384,
|
|
48
|
+
} as const
|
|
49
|
+
|
|
50
|
+
/** Auto 摘要输出的安全上限,避免压缩请求被服务端拖到断连。 */
|
|
51
|
+
const AUTO_SUMMARY_MAX_TOKENS = 16_384
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Copilot 的 `token_prices` 计价换算。
|
|
55
|
+
*
|
|
56
|
+
* 实测存在两种结构:
|
|
57
|
+
* - 扁平:`{ batch_size: 1e6, input_price: 175000000000 }`(旧目录格式)
|
|
58
|
+
* - 嵌套:`{ batch_size: 1e6, default: { input_price: 20, ... } }`(新格式)
|
|
59
|
+
*
|
|
60
|
+
* 前者以 1e9 为基准换算成「每百万 token 美元」;后者本身已是每百万美元单价,
|
|
61
|
+
* 只需按 batch_size 归一。这里统一按「每百万 token 美元」返回。
|
|
62
|
+
*/
|
|
63
|
+
function perMillion(value: number | undefined, nested: boolean): number {
|
|
64
|
+
if (value === undefined || !Number.isFinite(value) || value <= 0) return 0
|
|
65
|
+
if (nested) return Math.round(value * 1e6) / 1e6
|
|
66
|
+
return Math.round((value / 1e9) * 1e6) / 1e6
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** 从条目里取出归一化后的价格组。 */
|
|
70
|
+
function priceGroup(entry: CatalogEntry): {
|
|
71
|
+
input: number
|
|
72
|
+
output: number
|
|
73
|
+
cacheRead: number
|
|
74
|
+
cacheWrite: number
|
|
75
|
+
} {
|
|
76
|
+
const prices = entry.billing?.token_prices
|
|
77
|
+
if (prices === undefined) return { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }
|
|
78
|
+
|
|
79
|
+
const nested = prices.default
|
|
80
|
+
if (nested !== undefined) {
|
|
81
|
+
return {
|
|
82
|
+
input: perMillion(nested.input_price, true),
|
|
83
|
+
output: perMillion(nested.output_price, true),
|
|
84
|
+
cacheRead: perMillion(nested.cache_read_price ?? nested.cache_price, true),
|
|
85
|
+
cacheWrite: perMillion(nested.cache_write_price, true),
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
input: perMillion(prices.input_price, false),
|
|
90
|
+
output: perMillion(prices.output_price, false),
|
|
91
|
+
cacheRead: perMillion(prices.cache_price, false),
|
|
92
|
+
cacheWrite: 0,
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* pi 思考级别到 Copilot `reasoning_effort` 的映射。
|
|
98
|
+
*
|
|
99
|
+
* Copilot 允许的值随模型变化(实测含 `none`/`low`/`medium`/`high`/`xhigh`/`max`)。
|
|
100
|
+
* 未列出的级别用 null 显式标记为不支持,让 pi 的 `/thinking` 不显示无效选项。
|
|
101
|
+
* 注意 `none` 对应 pi 的 `off`。
|
|
102
|
+
*/
|
|
103
|
+
function buildThinkingLevelMap(efforts: string[]): Record<string, string | null> | undefined {
|
|
104
|
+
if (efforts.length === 0) return undefined
|
|
105
|
+
const supported = new Set(efforts)
|
|
106
|
+
const pick = (...candidates: string[]): string | null => {
|
|
107
|
+
for (const candidate of candidates) {
|
|
108
|
+
if (supported.has(candidate)) return candidate
|
|
109
|
+
}
|
|
110
|
+
return null
|
|
111
|
+
}
|
|
112
|
+
return {
|
|
113
|
+
off: pick('none'),
|
|
114
|
+
minimal: pick('minimal', 'low'),
|
|
115
|
+
low: pick('low'),
|
|
116
|
+
medium: pick('medium'),
|
|
117
|
+
high: pick('high'),
|
|
118
|
+
xhigh: pick('xhigh', 'high'),
|
|
119
|
+
max: pick('max', 'xhigh', 'high'),
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** 判断目录条目是否支持图片输入。 */
|
|
124
|
+
function supportsVision(entry: CatalogEntry): boolean {
|
|
125
|
+
return entry.capabilities?.supports?.vision === true
|
|
126
|
+
|| (entry.capabilities?.limits?.vision?.supported_media_types?.length ?? 0) > 0
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** 一个条目是否声明了会返回 PDF 的能力。 */
|
|
130
|
+
function supportsPdf(entry: CatalogEntry): boolean {
|
|
131
|
+
const media = entry.capabilities?.limits?.vision?.supported_media_types ?? []
|
|
132
|
+
return media.includes('application/pdf')
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** 由一个目录条目构造 pi 模型定义。 */
|
|
136
|
+
export function specFromCatalog(entry: CatalogEntry): ModelSpec {
|
|
137
|
+
const limits = entry.capabilities?.limits
|
|
138
|
+
const supports = entry.capabilities?.supports
|
|
139
|
+
const efforts = supports?.reasoning_effort ?? []
|
|
140
|
+
|
|
141
|
+
const spec: ModelSpec = {
|
|
142
|
+
id: entry.id,
|
|
143
|
+
name: entry.name ?? entry.id,
|
|
144
|
+
reasoning: efforts.length > 0,
|
|
145
|
+
input: supportsVision(entry) ? ['text', 'image'] : ['text'],
|
|
146
|
+
cost: priceGroup(entry),
|
|
147
|
+
contextWindow: limits?.max_context_window_tokens ?? limits?.max_prompt_tokens ?? FALLBACK.contextWindow,
|
|
148
|
+
maxTokens: limits?.max_output_tokens ?? FALLBACK.maxTokens,
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const map = buildThinkingLevelMap(efforts)
|
|
152
|
+
if (map !== undefined) spec.thinkingLevelMap = map
|
|
153
|
+
return spec
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* 构造 Auto 虚拟模型的 spec。
|
|
158
|
+
*
|
|
159
|
+
* 对应 OpenCode 的 `XK`:Auto 不是一个真实模型,而是从**所有可路由条目**的
|
|
160
|
+
* 能力中取最小值拼出来的展示项,因此它的上限必须覆盖全部候选,
|
|
161
|
+
* 否则会出现「选择器允许 900k 上下文但实际选中模型只有 200k」的错配。
|
|
162
|
+
*
|
|
163
|
+
* 无候选时返回 undefined——这表示当前账号没有 Auto 通道可用的模型。
|
|
164
|
+
*/
|
|
165
|
+
export function autoSpec(candidates: readonly CatalogEntry[]): ModelSpec | undefined {
|
|
166
|
+
const routable = candidates.filter(isAutoRoutable)
|
|
167
|
+
if (routable.length === 0) return undefined
|
|
168
|
+
|
|
169
|
+
const contexts: number[] = []
|
|
170
|
+
const prompts: number[] = []
|
|
171
|
+
const outputs: number[] = []
|
|
172
|
+
for (const entry of routable) {
|
|
173
|
+
const limits = entry.capabilities?.limits
|
|
174
|
+
const context = limits?.max_context_window_tokens ?? limits?.max_prompt_tokens
|
|
175
|
+
if (context !== undefined) contexts.push(context)
|
|
176
|
+
if (limits?.max_prompt_tokens !== undefined) prompts.push(limits.max_prompt_tokens)
|
|
177
|
+
if (limits?.max_output_tokens !== undefined) outputs.push(limits.max_output_tokens)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const min = (values: number[], fallback: number): number =>
|
|
181
|
+
values.length === 0 ? fallback : Math.min(...values)
|
|
182
|
+
|
|
183
|
+
// Auto 的价格无法预先确定(取决于服务端选中哪个模型),保守取 0 交由
|
|
184
|
+
// 用量回报体现实际消耗;折扣倍率在会话结果里另行展示。
|
|
185
|
+
return {
|
|
186
|
+
id: AUTO_MODEL_ID,
|
|
187
|
+
name: 'Auto',
|
|
188
|
+
reasoning: routable.some((entry) => (entry.capabilities?.supports?.reasoning_effort?.length ?? 0) > 0),
|
|
189
|
+
input: ['text', 'image'],
|
|
190
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
191
|
+
contextWindow: min(contexts, AUTO_FALLBACK.contextWindow),
|
|
192
|
+
maxTokens: Math.min(min(outputs, AUTO_FALLBACK.maxTokens), AUTO_SUMMARY_MAX_TOKENS),
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Auto 路由模型的 id。pi 侧选择它即表示「让服务端决定」。
|
|
198
|
+
*
|
|
199
|
+
* 为什么不用 `auto`:pi 的模型目录里已有 openrouter 的同名物理模型
|
|
200
|
+
* (`id: "auto"`, `name: "Auto"`)。实测把本扩展的条目也叫 `auto` 时,
|
|
201
|
+
* 它在 `getModels()` 之后被过滤掉,`--model auto` 只能回退成“自定义模型 id”
|
|
202
|
+
* 并提示 not found;改用不冲突的 id 后正常出现在选择器中。
|
|
203
|
+
* 显示名仍然是 `Auto`。
|
|
204
|
+
*/
|
|
205
|
+
export const AUTO_MODEL_ID = 'auto-routed'
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* 一个条目是否参与 Auto 路由。
|
|
209
|
+
*
|
|
210
|
+
* 判定依据与 OpenCode 一致:**存在 `billing.auto_discount` 字段**。
|
|
211
|
+
* 这比 `policy.state === "enabled"` 可靠——后者会把未开通模型也标为 enabled。
|
|
212
|
+
*/
|
|
213
|
+
export function isAutoRoutable(entry: CatalogEntry): boolean {
|
|
214
|
+
return entry.billing?.auto_discount !== undefined
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* 判断条目能否通过 `/responses` 调用。
|
|
219
|
+
*
|
|
220
|
+
* `supported_endpoints` 缺失(如早期 `gpt-4o` 条目)意味着无法确认,
|
|
221
|
+
* 按不支持处理。
|
|
222
|
+
*/
|
|
223
|
+
export function supportsResponsesEndpoint(entry: CatalogEntry): boolean {
|
|
224
|
+
const endpoints = entry.supported_endpoints
|
|
225
|
+
if (endpoints === undefined) return false
|
|
226
|
+
return endpoints.some((endpoint) => endpoint === '/responses' || endpoint === 'ws:/responses')
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* 从目录中挑选 Auto 通道可用模型。
|
|
231
|
+
*
|
|
232
|
+
* 只保留明确支持 Responses 的条目;这是 `/models/session` 下发模型
|
|
233
|
+
* 能够被本扩展真正调用的必要条件。
|
|
234
|
+
*/
|
|
235
|
+
export function selectableModels(entries: CatalogEntry[], ids: readonly string[]): ModelSpec[] {
|
|
236
|
+
const byId = new Map(entries.map((entry) => [entry.id, entry]))
|
|
237
|
+
const specs: ModelSpec[] = []
|
|
238
|
+
for (const id of ids) {
|
|
239
|
+
const entry = byId.get(id)
|
|
240
|
+
// 目录里没有该 id 时仍允许使用,但按保守回退处理:
|
|
241
|
+
// session 服务端明确下发了 id,说明它可调用。
|
|
242
|
+
specs.push(entry === undefined
|
|
243
|
+
? { id, name: id, reasoning: false, input: ['text'],
|
|
244
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
245
|
+
contextWindow: FALLBACK.contextWindow, maxTokens: FALLBACK.maxTokens }
|
|
246
|
+
: specFromCatalog(entry))
|
|
247
|
+
}
|
|
248
|
+
return specs
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* 按 `/models/session` 实际授权范围过滤目录条目。
|
|
253
|
+
*
|
|
254
|
+
* 这是本扩展区分「目录里写着能用」与「账号真的能用」的关键。
|
|
255
|
+
* Copilot 的 `/models` 会把未开通的模型标记成 `policy.state = "enabled"`
|
|
256
|
+
* 并带齐 `capabilities`,但请求时一律返回 `model_not_supported`。
|
|
257
|
+
* 只有 `/models/session` 的 `available_models` 才是该通道的真实授权边界。
|
|
258
|
+
*
|
|
259
|
+
* 过滤规则:
|
|
260
|
+
* - 授权清单为空(协商失败、未返回,或走了 `/auto`)时不过滤;
|
|
261
|
+
* - 否则只保留同时出现在目录与授权清单中的条目。
|
|
262
|
+
*/
|
|
263
|
+
export function filterByAuthorization(
|
|
264
|
+
entries: CatalogEntry[],
|
|
265
|
+
authorized: readonly string[],
|
|
266
|
+
): CatalogEntry[] {
|
|
267
|
+
if (authorized.length === 0) return entries
|
|
268
|
+
const allowed = new Set(authorized)
|
|
269
|
+
return entries.filter((entry) => allowed.has(entry.id))
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* 汇总构建模型列表所需的一切。
|
|
274
|
+
*
|
|
275
|
+
* 返回两类结果:
|
|
276
|
+
* - `specs`:真实模型(目录里支持 Responses 的条目)
|
|
277
|
+
* - `auto`:Auto 虚拟条目,仅当存在可路由候选时才有
|
|
278
|
+
*
|
|
279
|
+
* 之所以把 Auto 与真实模型并列:`/auto` 会选中目录之外的模型
|
|
280
|
+
* (实测 `gpt-5.6-luna`),因此不能靠 `available_models` 裁剪掉 Auto 能力;
|
|
281
|
+
* 但也不能把全部未授权模型暴露出来,否则用户手动选择时必然失败。
|
|
282
|
+
*/
|
|
283
|
+
export function buildModelList(entries: readonly CatalogEntry[], authorized: readonly string[]): {
|
|
284
|
+
specs: ModelSpec[]
|
|
285
|
+
auto: ModelSpec | undefined
|
|
286
|
+
routable: CatalogEntry[]
|
|
287
|
+
} {
|
|
288
|
+
const responses = entries.filter(supportsResponsesEndpoint)
|
|
289
|
+
const routable = responses.filter(isAutoRoutable)
|
|
290
|
+
const visible = filterByAuthorization(responses, authorized)
|
|
291
|
+
return {
|
|
292
|
+
specs: visible.map(specFromCatalog),
|
|
293
|
+
auto: autoSpec(routable),
|
|
294
|
+
routable,
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/** 导出内部判定,供测试与状态展示复用。 */
|
|
299
|
+
export { supportsPdf, supportsVision }
|