@oisin-ee/pi-tiers 0.9.0 → 0.9.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/LICENSE +21 -0
- package/package.json +4 -4
- package/src/recovery.ts +3 -1
- package/src/router.ts +25 -6
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Momokaya
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oisin-ee/pi-tiers",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.1",
|
|
4
4
|
"description": "Sticky virtual model tiers with quota-aware recovery for pi.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-extension",
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
],
|
|
21
21
|
"type": "module",
|
|
22
22
|
"devDependencies": {
|
|
23
|
-
"@earendil-works/pi-ai": "
|
|
24
|
-
"@earendil-works/pi-coding-agent": "
|
|
23
|
+
"@earendil-works/pi-ai": "1.3.0",
|
|
24
|
+
"@earendil-works/pi-coding-agent": "1.3.0"
|
|
25
25
|
},
|
|
26
26
|
"peerDependencies": {
|
|
27
27
|
"@earendil-works/pi-ai": "*",
|
|
@@ -32,4 +32,4 @@
|
|
|
32
32
|
"./src/index.ts"
|
|
33
33
|
]
|
|
34
34
|
}
|
|
35
|
-
}
|
|
35
|
+
}
|
package/src/recovery.ts
CHANGED
|
@@ -9,6 +9,7 @@ import type { Context } from "./ports.ts";
|
|
|
9
9
|
import type { Router } from "./router.ts";
|
|
10
10
|
import type { Tiers } from "./config.ts";
|
|
11
11
|
import { classify } from "./policy.ts";
|
|
12
|
+
import { listed } from "./router.ts";
|
|
12
13
|
|
|
13
14
|
const ZERO = 0,
|
|
14
15
|
ONE = 1;
|
|
@@ -76,7 +77,8 @@ class Recovery {
|
|
|
76
77
|
}
|
|
77
78
|
const tier = ctx.model.id,
|
|
78
79
|
state = branchState(ctx.sessionManager.getBranch(), tier),
|
|
79
|
-
|
|
80
|
+
tierTargets = this.#tiers.get(tier),
|
|
81
|
+
targets = tierTargets === undefined ? undefined : listed(tierTargets, ctx),
|
|
80
82
|
message = event.context.contextMessages.findLast((item) => item.role === "assistant"),
|
|
81
83
|
entry = event.context.contextEntries.findLast(
|
|
82
84
|
(item: Readonly<{ sourceEntry: SessionEntry; messages: readonly unknown[] }>) =>
|
package/src/router.ts
CHANGED
|
@@ -10,7 +10,23 @@ import type { Window } from "./quota.ts";
|
|
|
10
10
|
|
|
11
11
|
const ZERO = 0,
|
|
12
12
|
ONE = 1,
|
|
13
|
-
CACHE_MS = 60_000
|
|
13
|
+
CACHE_MS = 60_000,
|
|
14
|
+
/**
|
|
15
|
+
* Keeps only targets the model registry lists, so a provider model that is not
|
|
16
|
+
* configured yet behaves like an unavailable target instead of a terminal error.
|
|
17
|
+
* @param {readonly string[]} targets Ordered tier.
|
|
18
|
+
* @param {Context} ctx Pi authority.
|
|
19
|
+
* @returns {readonly string[]} Listed targets, in tier order.
|
|
20
|
+
*/
|
|
21
|
+
listed = (targets: readonly string[], ctx: Context): readonly string[] => {
|
|
22
|
+
const available = new Set(
|
|
23
|
+
ctx.modelRegistry.getAvailable().map((candidate) => `${candidate.provider}/${candidate.id}`),
|
|
24
|
+
);
|
|
25
|
+
return targets.filter((reference) => {
|
|
26
|
+
const parsed = target(reference);
|
|
27
|
+
return available.has(`${parsed.provider}/${parsed.id}`);
|
|
28
|
+
});
|
|
29
|
+
};
|
|
14
30
|
|
|
15
31
|
/** Sticky routing with host-wide quota and gateway health. */
|
|
16
32
|
class Router {
|
|
@@ -139,19 +155,20 @@ class Router {
|
|
|
139
155
|
|
|
140
156
|
/**
|
|
141
157
|
* Routes a tier without fabricating conversation messages.
|
|
142
|
-
* @param {readonly string[]}
|
|
158
|
+
* @param {readonly string[]} tier Ordered physical models, listed or not.
|
|
143
159
|
* @param {Request} request Pi request.
|
|
144
160
|
* @param {Context} ctx Pi authority.
|
|
145
161
|
* @returns {Promise<ModelRoute<State>>} Physical dispatch.
|
|
146
162
|
*/
|
|
147
163
|
public async route(
|
|
148
|
-
|
|
164
|
+
tier: readonly string[],
|
|
149
165
|
request: Request,
|
|
150
166
|
ctx: Context,
|
|
151
167
|
): Promise<ModelRoute<State>> {
|
|
152
168
|
request.signal?.throwIfAborted();
|
|
153
169
|
await this.#refresh();
|
|
154
|
-
const
|
|
170
|
+
const targets = listed(tier, ctx),
|
|
171
|
+
previous =
|
|
155
172
|
request.reason === "direct"
|
|
156
173
|
? undefined
|
|
157
174
|
: (branchState(ctx.sessionManager.getBranch(), request.model.id) ?? request.state),
|
|
@@ -163,7 +180,9 @@ class Router {
|
|
|
163
180
|
state = await this.#retry(request, current),
|
|
164
181
|
health = await this.#effects.read(),
|
|
165
182
|
chosen =
|
|
166
|
-
|
|
183
|
+
targets.includes(state.chosen) &&
|
|
184
|
+
!state.attempted.includes(state.chosen) &&
|
|
185
|
+
this.#available(state.chosen, health)
|
|
167
186
|
? state.chosen
|
|
168
187
|
: await this.next(targets, state);
|
|
169
188
|
if (chosen === undefined) {
|
|
@@ -173,4 +192,4 @@ class Router {
|
|
|
173
192
|
}
|
|
174
193
|
}
|
|
175
194
|
|
|
176
|
-
export { Router };
|
|
195
|
+
export { Router, listed };
|