@zllf/pi-provider-proxy 0.2.3 → 0.2.4
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 +1 -1
- package/package.json +1 -1
- package/src/extension.js +26 -11
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.4` 固定 GitHub 版本。要求 Node.js 22.19 或更高版本,已在 Pi 1.7.0 验证。
|
|
18
18
|
|
|
19
19
|
## 配置代理
|
|
20
20
|
|
package/package.json
CHANGED
package/src/extension.js
CHANGED
|
@@ -66,7 +66,7 @@ export default function providerProxy(pi, HostRunner = ExtensionRunner) {
|
|
|
66
66
|
if (loadError) throw loadError;
|
|
67
67
|
return store;
|
|
68
68
|
};
|
|
69
|
-
const
|
|
69
|
+
const registrations = new Map();
|
|
70
70
|
const modelRoutes = createModelRoutes(id => checked().route(id));
|
|
71
71
|
const blocked = new Set();
|
|
72
72
|
const sessionNetwork = createSessionNetwork();
|
|
@@ -90,26 +90,31 @@ export default function providerProxy(pi, HostRunner = ExtensionRunner) {
|
|
|
90
90
|
}
|
|
91
91
|
function block(id, error) {
|
|
92
92
|
if (blocked.has(id)) return;
|
|
93
|
-
|
|
93
|
+
const provider = blockedProvider(id, builtins.find(p => p.id === id), error);
|
|
94
|
+
pi.registerProvider(provider);
|
|
95
|
+
registrations.set(id, provider);
|
|
94
96
|
modelRoutes.retire(id);
|
|
95
97
|
blocked.add(id);
|
|
96
98
|
}
|
|
97
99
|
function activate(id) {
|
|
98
|
-
if (
|
|
100
|
+
if (registrations.has(id)) return;
|
|
99
101
|
const { native, api } = catalog.get(id);
|
|
100
102
|
const route = () => checked().route(id);
|
|
101
103
|
if (native) {
|
|
102
|
-
|
|
104
|
+
const provider = {
|
|
103
105
|
...native,
|
|
104
106
|
auth: id === 'openai-codex' ? wrapCodexAuth(native.auth, route) : native.auth,
|
|
105
107
|
stream: modelRoutes.wrap(id, native.stream),
|
|
106
108
|
streamSimple: modelRoutes.wrap(id, native.streamSimple),
|
|
107
|
-
}
|
|
109
|
+
};
|
|
110
|
+
pi.registerProvider(provider);
|
|
111
|
+
registrations.set(id, provider);
|
|
108
112
|
} else {
|
|
109
113
|
// Omit the model list so Pi retains its models.json catalog and auth.
|
|
110
|
-
|
|
114
|
+
const streamSimple = modelRoutes.wrap(id, getApiProvider(api).streamSimple);
|
|
115
|
+
pi.registerProvider(id, { api, streamSimple });
|
|
116
|
+
registrations.set(id, streamSimple);
|
|
111
117
|
}
|
|
112
|
-
installed.add(id);
|
|
113
118
|
}
|
|
114
119
|
for (const id of loadError ? catalog.keys() : Object.keys(store.read().bindings)) {
|
|
115
120
|
if (loadError || !catalog.has(id)) block(id, loadError ?? problem('STALE_BINDING'));
|
|
@@ -118,10 +123,10 @@ export default function providerProxy(pi, HostRunner = ExtensionRunner) {
|
|
|
118
123
|
// /model can refresh models.json without reloading extensions. Check Pi's
|
|
119
124
|
// effective chat models both on selection and before dispatching a new turn.
|
|
120
125
|
function checkLiveBindings(ctx) {
|
|
121
|
-
if (!
|
|
126
|
+
if (!registrations.size || !store) return;
|
|
122
127
|
const bindings = store.read().bindings;
|
|
123
128
|
const models = ctx.modelRegistry.getAll();
|
|
124
|
-
for (const id of
|
|
129
|
+
for (const id of registrations.keys()) {
|
|
125
130
|
if (!Object.hasOwn(bindings, id) || blocked.has(id)) continue;
|
|
126
131
|
const { native, api } = catalog.get(id);
|
|
127
132
|
const accepted = new Set(native ? native.getModels().map(model => model.api) : [api]);
|
|
@@ -148,13 +153,23 @@ export default function providerProxy(pi, HostRunner = ExtensionRunner) {
|
|
|
148
153
|
}
|
|
149
154
|
pi.on('model_select', prepareModel);
|
|
150
155
|
pi.on('before_agent_start', (_event, ctx) => selectProvider(ctx.model?.provider, ctx));
|
|
151
|
-
pi.on('session_shutdown', () => {
|
|
156
|
+
pi.on('session_shutdown', (_event, ctx) => {
|
|
152
157
|
releaseEarlyListener?.();
|
|
153
158
|
releaseEarlyListener = undefined;
|
|
154
159
|
activeProvider = undefined;
|
|
155
160
|
modelRoutes.dispose();
|
|
156
161
|
sessionNetwork.dispose();
|
|
157
162
|
sessionHttp.dispose();
|
|
163
|
+
// Pi keeps its model runtime across /reload, including providers that the
|
|
164
|
+
// new extension instance may no longer register.
|
|
165
|
+
const { modelRegistry } = ctx;
|
|
166
|
+
for (const [id, registration] of registrations) {
|
|
167
|
+
const current = typeof registration === 'function'
|
|
168
|
+
? modelRegistry.getRegisteredProviderConfig(id)?.streamSimple
|
|
169
|
+
: modelRegistry.getRegisteredNativeProvider(id);
|
|
170
|
+
if (current === registration) pi.unregisterProvider(id);
|
|
171
|
+
}
|
|
172
|
+
registrations.clear();
|
|
158
173
|
});
|
|
159
174
|
pi.on('session_start', (event, ctx) => {
|
|
160
175
|
prepareModel(event, ctx);
|
|
@@ -178,7 +193,7 @@ export default function providerProxy(pi, HostRunner = ExtensionRunner) {
|
|
|
178
193
|
for (const id of blocked) {
|
|
179
194
|
if (Object.hasOwn(next.bindings, id)) continue;
|
|
180
195
|
pi.unregisterProvider(id);
|
|
181
|
-
|
|
196
|
+
registrations.delete(id);
|
|
182
197
|
blocked.delete(id);
|
|
183
198
|
}
|
|
184
199
|
configureEarlyListener();
|