@zllf/pi-provider-proxy 0.2.3 → 0.2.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/README.md +2 -2
- package/package.json +1 -1
- package/src/extension.js +30 -12
- package/src/routing.js +5 -3
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@ pi install npm:@zllf/pi-provider-proxy
|
|
|
14
14
|
pi update npm:@zllf/pi-provider-proxy
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
-
在已打开的 Pi 会话中运行 `/reload`,然后输入 `/proxy`。也可以用 `pi install git:github.com/Zllf/pi-provider-proxy@v0.2.
|
|
17
|
+
在已打开的 Pi 会话中运行 `/reload`,然后输入 `/proxy`。也可以用 `pi install git:github.com/Zllf/pi-provider-proxy@v0.2.5` 固定 GitHub 版本。要求 Node.js 22.19 或更高版本,已在 Pi 1.7.0 验证。
|
|
18
18
|
|
|
19
19
|
## 配置代理
|
|
20
20
|
|
|
@@ -32,7 +32,7 @@ pi update npm:@zllf/pi-provider-proxy
|
|
|
32
32
|
## 支持范围
|
|
33
33
|
|
|
34
34
|
- **模型对话**支持 `openai-codex`、`openai`、`anthropic`、`deepseek` 和 `openrouter`。`models.json` 自定义服务商可使用 `openai-completions`、`openai-responses` 或 `anthropic-messages`,同一服务商下的模型须使用同一种协议。
|
|
35
|
-
- **ChatGPT 订阅**选择 `openai-codex`,即可复用 `/login`
|
|
35
|
+
- **ChatGPT 订阅**选择 `openai-codex`,即可复用 `/login` 的已有凭据。指定路由后,对话会优先使用 Codex WebSocket,并在需要时回退到 SSE。开启「Pi 网络跟随当前模型」后,WebSocket 和 SSE 都走当前 Provider 的代理。Pi 端登录换票和令牌续期也走该路由。浏览器授权页面使用浏览器的网络设置。
|
|
36
36
|
- **Pi 网络跟随当前模型**覆盖会话中使用默认 `fetch`、Undici、WebSocket 和 Node `http` / `https` 客户端的请求。当前模型选择「继承」或关闭开关时,恢复 Pi 原有网络设置。回环地址沿用原有连接方式,适用于本地 Ollama 和 OAuth 回调。显式指定连接代理的客户端、独立进程(如 `git`、`npm`、`curl`)和系统浏览器使用各自的网络设置。
|
|
37
37
|
|
|
38
38
|
## 本地开发
|
package/package.json
CHANGED
package/src/extension.js
CHANGED
|
@@ -66,8 +66,11 @@ export default function providerProxy(pi, HostRunner = ExtensionRunner) {
|
|
|
66
66
|
if (loadError) throw loadError;
|
|
67
67
|
return store;
|
|
68
68
|
};
|
|
69
|
-
const
|
|
70
|
-
const modelRoutes = createModelRoutes(
|
|
69
|
+
const registrations = new Map();
|
|
70
|
+
const modelRoutes = createModelRoutes(
|
|
71
|
+
id => checked().route(id),
|
|
72
|
+
() => store?.read().followModel === true,
|
|
73
|
+
);
|
|
71
74
|
const blocked = new Set();
|
|
72
75
|
const sessionNetwork = createSessionNetwork();
|
|
73
76
|
const sessionHttp = createSessionHttp();
|
|
@@ -90,26 +93,31 @@ export default function providerProxy(pi, HostRunner = ExtensionRunner) {
|
|
|
90
93
|
}
|
|
91
94
|
function block(id, error) {
|
|
92
95
|
if (blocked.has(id)) return;
|
|
93
|
-
|
|
96
|
+
const provider = blockedProvider(id, builtins.find(p => p.id === id), error);
|
|
97
|
+
pi.registerProvider(provider);
|
|
98
|
+
registrations.set(id, provider);
|
|
94
99
|
modelRoutes.retire(id);
|
|
95
100
|
blocked.add(id);
|
|
96
101
|
}
|
|
97
102
|
function activate(id) {
|
|
98
|
-
if (
|
|
103
|
+
if (registrations.has(id)) return;
|
|
99
104
|
const { native, api } = catalog.get(id);
|
|
100
105
|
const route = () => checked().route(id);
|
|
101
106
|
if (native) {
|
|
102
|
-
|
|
107
|
+
const provider = {
|
|
103
108
|
...native,
|
|
104
109
|
auth: id === 'openai-codex' ? wrapCodexAuth(native.auth, route) : native.auth,
|
|
105
110
|
stream: modelRoutes.wrap(id, native.stream),
|
|
106
111
|
streamSimple: modelRoutes.wrap(id, native.streamSimple),
|
|
107
|
-
}
|
|
112
|
+
};
|
|
113
|
+
pi.registerProvider(provider);
|
|
114
|
+
registrations.set(id, provider);
|
|
108
115
|
} else {
|
|
109
116
|
// Omit the model list so Pi retains its models.json catalog and auth.
|
|
110
|
-
|
|
117
|
+
const streamSimple = modelRoutes.wrap(id, getApiProvider(api).streamSimple);
|
|
118
|
+
pi.registerProvider(id, { api, streamSimple });
|
|
119
|
+
registrations.set(id, streamSimple);
|
|
111
120
|
}
|
|
112
|
-
installed.add(id);
|
|
113
121
|
}
|
|
114
122
|
for (const id of loadError ? catalog.keys() : Object.keys(store.read().bindings)) {
|
|
115
123
|
if (loadError || !catalog.has(id)) block(id, loadError ?? problem('STALE_BINDING'));
|
|
@@ -118,10 +126,10 @@ export default function providerProxy(pi, HostRunner = ExtensionRunner) {
|
|
|
118
126
|
// /model can refresh models.json without reloading extensions. Check Pi's
|
|
119
127
|
// effective chat models both on selection and before dispatching a new turn.
|
|
120
128
|
function checkLiveBindings(ctx) {
|
|
121
|
-
if (!
|
|
129
|
+
if (!registrations.size || !store) return;
|
|
122
130
|
const bindings = store.read().bindings;
|
|
123
131
|
const models = ctx.modelRegistry.getAll();
|
|
124
|
-
for (const id of
|
|
132
|
+
for (const id of registrations.keys()) {
|
|
125
133
|
if (!Object.hasOwn(bindings, id) || blocked.has(id)) continue;
|
|
126
134
|
const { native, api } = catalog.get(id);
|
|
127
135
|
const accepted = new Set(native ? native.getModels().map(model => model.api) : [api]);
|
|
@@ -148,13 +156,23 @@ export default function providerProxy(pi, HostRunner = ExtensionRunner) {
|
|
|
148
156
|
}
|
|
149
157
|
pi.on('model_select', prepareModel);
|
|
150
158
|
pi.on('before_agent_start', (_event, ctx) => selectProvider(ctx.model?.provider, ctx));
|
|
151
|
-
pi.on('session_shutdown', () => {
|
|
159
|
+
pi.on('session_shutdown', (_event, ctx) => {
|
|
152
160
|
releaseEarlyListener?.();
|
|
153
161
|
releaseEarlyListener = undefined;
|
|
154
162
|
activeProvider = undefined;
|
|
155
163
|
modelRoutes.dispose();
|
|
156
164
|
sessionNetwork.dispose();
|
|
157
165
|
sessionHttp.dispose();
|
|
166
|
+
// Pi keeps its model runtime across /reload, including providers that the
|
|
167
|
+
// new extension instance may no longer register.
|
|
168
|
+
const { modelRegistry } = ctx;
|
|
169
|
+
for (const [id, registration] of registrations) {
|
|
170
|
+
const current = typeof registration === 'function'
|
|
171
|
+
? modelRegistry.getRegisteredProviderConfig(id)?.streamSimple
|
|
172
|
+
: modelRegistry.getRegisteredNativeProvider(id);
|
|
173
|
+
if (current === registration) pi.unregisterProvider(id);
|
|
174
|
+
}
|
|
175
|
+
registrations.clear();
|
|
158
176
|
});
|
|
159
177
|
pi.on('session_start', (event, ctx) => {
|
|
160
178
|
prepareModel(event, ctx);
|
|
@@ -178,7 +196,7 @@ export default function providerProxy(pi, HostRunner = ExtensionRunner) {
|
|
|
178
196
|
for (const id of blocked) {
|
|
179
197
|
if (Object.hasOwn(next.bindings, id)) continue;
|
|
180
198
|
pi.unregisterProvider(id);
|
|
181
|
-
|
|
199
|
+
registrations.delete(id);
|
|
182
200
|
blocked.delete(id);
|
|
183
201
|
}
|
|
184
202
|
configureEarlyListener();
|
package/src/routing.js
CHANGED
|
@@ -4,7 +4,7 @@ import { codexFetch } from './codex-sse.js';
|
|
|
4
4
|
|
|
5
5
|
// The stream lease covers retries and all response events. Once a binding is
|
|
6
6
|
// retired, only streams that already hold its transport may keep using it.
|
|
7
|
-
export function createModelRoutes(resolveRoute) {
|
|
7
|
+
export function createModelRoutes(resolveRoute, followsModelRoute = () => false) {
|
|
8
8
|
const pools = new Map();
|
|
9
9
|
let disposed = false;
|
|
10
10
|
|
|
@@ -43,9 +43,11 @@ export function createModelRoutes(resolveRoute) {
|
|
|
43
43
|
const release = () => { entry.users--; closeIfIdle(entry); };
|
|
44
44
|
try {
|
|
45
45
|
const codex = model.api === 'openai-codex-responses';
|
|
46
|
+
const useSse = codex && !followsModelRoute();
|
|
46
47
|
const stream = delegate(model, transcript, {
|
|
47
|
-
...options,
|
|
48
|
-
|
|
48
|
+
...options,
|
|
49
|
+
fetch: codex ? codexFetch(entry.transport.fetch, options?.signal) : entry.transport.fetch,
|
|
50
|
+
...(useSse ? { transport: 'sse' } : {}),
|
|
49
51
|
});
|
|
50
52
|
stream.result().then(release, release);
|
|
51
53
|
return stream;
|