@smg1024/jev-router 0.0.0-stage → 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 smg1024
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/README.md CHANGED
@@ -1,3 +1,55 @@
1
- # Temporary Holding Version
1
+ # @smg1024/jev-router
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A [Pi](https://pi.dev) package that adds a virtual model, `jev/auto`. For each new user task it asks the Jev classifier (`typesafe/jev-latest`) to pick:
4
+
5
+ - a physical chat model from the eligible catalog, and
6
+ - a thinking effort (`off` … `xhigh`).
7
+
8
+ Continuations, retries, and compaction reuse the previous decision, so a turn stays on one model.
9
+
10
+ ## Install
11
+
12
+ ```bash
13
+ pi install npm:@smg1024/jev-router
14
+ ```
15
+
16
+ Then make `jev/auto` the default in `~/.pi/agent/settings.json`:
17
+
18
+ ```json
19
+ {
20
+ "defaultProvider": "jev",
21
+ "defaultModel": "auto"
22
+ }
23
+ ```
24
+
25
+ To install from Git instead, use `pi install git:github.com/smg1024/jev-router`.
26
+
27
+ ## Requirements
28
+
29
+ - A Typesafe API key for the `typesafe/jev-latest` classifier. Inside Pi, run `/login` and choose Typesafe, or set `TYPESAFE_API_KEY` in your environment.
30
+ - At least one physical chat model with credentials configured.
31
+ - For image prompts, at least one eligible model that accepts image input.
32
+
33
+ ## Behavior
34
+
35
+ | Situation | Result |
36
+ |---|---|
37
+ | New user task | Jev picks a model and effort from the eligible catalog |
38
+ | `continuation` / `retry` | Reuses the previous (or failed) model and effort |
39
+ | `direct` (internal calls) | First eligible model, `low` effort, no classification |
40
+ | `enabledModels` / `--models` set | Only those models are eligible |
41
+ | Virtual models (`pi-virtual`) | Never chosen as a target |
42
+
43
+ Errors are raised for: no eligible model, missing classifier, classifier failure, or an invalid classifier choice.
44
+
45
+ ## Test
46
+
47
+ ```bash
48
+ bun run test
49
+ ```
50
+
51
+ The test uses a mock Jev classifier and does not call any API.
52
+
53
+ ## License
54
+
55
+ [MIT](LICENSE)
@@ -0,0 +1,54 @@
1
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
+
3
+ const effort = {
4
+ off: "Mechanical transformation or trivial lookup; no deliberate reasoning needed",
5
+ low: "Straightforward explanation, localized edit, or routine question",
6
+ medium: "Ordinary implementation, debugging, or analysis with several steps",
7
+ high: "Difficult diagnosis, design trade-offs, or interacting constraints",
8
+ xhigh: "Exceptionally difficult reasoning where additional deliberation is worth the cost",
9
+ };
10
+
11
+ export default function (pi: ExtensionAPI) {
12
+ pi.registerVirtualModel({
13
+ provider: "jev", id: "auto", name: "Auto (Jev)",
14
+ async route(request, ctx) {
15
+ const registry = ctx.modelRegistry;
16
+ const content = request.messages.findLast(m => m.role === "user")?.content ?? "";
17
+ const image = Array.isArray(content) && content.some(b => b.type === "image");
18
+ const scope = ctx.scopedModels;
19
+ const models = registry.getAvailable().filter(m => m.api !== "pi-virtual" &&
20
+ (!scope.length || scope.some(s => s.model.provider === m.provider && s.model.id === m.id)) &&
21
+ (!image || m.input.includes("image")));
22
+ if (!models.length) throw new Error("jev/auto: no eligible physical chat model. Check Pi credentials, model scope and image support.");
23
+
24
+ const prior = request.reason === "retry" ? request.failed : request.previous;
25
+ if (request.reason !== "user" && prior) {
26
+ const model = models.find(m => m.provider === prior.model.provider && m.id === prior.model.id);
27
+ if (model) return { model, thinkingLevel: prior.thinkingLevel ?? "off" };
28
+ }
29
+ // Compaction and other internal calls aren't new user tasks.
30
+ if (request.reason === "direct") return { model: models[0], thinkingLevel: "low" };
31
+
32
+ const jev = registry.findOfType("classifier", "typesafe", "jev-latest");
33
+ if (!jev) throw new Error("jev/auto: typesafe/jev-latest is unavailable. Check TYPESAFE_API_KEY.");
34
+ const prompt = typeof content === "string" ? content : content.flatMap(b => b.type === "text" ? [b.text] : []).join("\n");
35
+ const candidates = new Map<string, (typeof models)[number]>(models.map((m, i) => [`m${i}`, m] as const));
36
+ const criteria = Object.fromEntries([...candidates].map(([key, m]) => [key,
37
+ `${m.provider}/${m.id} (${m.name}); reasoning: ${m.reasoning}; context: ${m.contextWindow}; input/output rates per million tokens: ${m.cost.input}/${m.cost.output}`]));
38
+ const result = await registry.classify(jev, {
39
+ state: { prompt: prompt.slice(0, 16_000), imageAttached: image },
40
+ questions: {
41
+ model: { type: "choice", instructions: "Select a capable model for the task. Prefer lower cost for routine work and stronger reasoning for hard work. Zero rates may mean unknown pricing.", criteria },
42
+ effort: { type: "choice", instructions: "Select the least thinking effort sufficient to complete this task reliably.", criteria: effort },
43
+ },
44
+ }, { signal: request.signal });
45
+ request.signal?.throwIfAborted();
46
+ if (result.stopReason !== "stop") throw new Error(`jev/auto: Jev classification failed: ${result.errorMessage ?? result.stopReason}`);
47
+ const { model: pick, effort: level } = result.answers;
48
+ const model = pick?.type === "choice" ? candidates.get(pick.choice) : undefined;
49
+ if (!model || level?.type !== "choice" || !Object.hasOwn(effort, level.choice)) throw new Error("jev/auto: invalid classifier choice.");
50
+ // Pi clamps this to the selected model's supported thinking levels before dispatch.
51
+ return { model, thinkingLevel: model.reasoning ? level.choice as keyof typeof effort : "off" };
52
+ },
53
+ });
54
+ }
package/package.json CHANGED
@@ -1,6 +1,25 @@
1
1
  {
2
2
  "name": "@smg1024/jev-router",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Pi extension that adds a virtual jev/auto model routing each task to a physical model and thinking effort via Jev classification.",
5
+ "type": "module",
6
+ "keywords": ["pi-package", "pi-extension"],
7
+ "license": "MIT",
8
+ "publishConfig": {
9
+ "access": "public"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/smg1024/jev-router.git"
14
+ },
15
+ "files": ["extensions", "tests", "README.md", "LICENSE"],
16
+ "scripts": {
17
+ "test": "bun tests/demo.mjs"
18
+ },
19
+ "pi": {
20
+ "extensions": ["./extensions/index.ts"]
21
+ },
22
+ "peerDependencies": {
23
+ "@earendil-works/pi-coding-agent": "*"
24
+ }
25
+ }
package/tests/demo.mjs ADDED
@@ -0,0 +1,41 @@
1
+ import assert from 'node:assert/strict';
2
+ import install from '../extensions/index.ts';
3
+
4
+ let route;
5
+ install({ registerVirtualModel(def) {
6
+ assert.equal(`${def.provider}/${def.id}`, 'jev/auto');
7
+ route = def.route;
8
+ } });
9
+ const a = {provider: 'local', id: 'custom', name: 'Small', input: ['text'], reasoning: false, cost: {input: 0, output: 0}, contextWindow: 8192};
10
+ const b = {...a, provider: 'other', name: 'Large', input: ['text', 'image'], reasoning: true};
11
+ const virtual = {...b, api: 'pi-virtual', id: 'auto'};
12
+ let catalog = [virtual, a, b], calls = 0, chosen = 'm1', level = 'high';
13
+ const ctx = {scopedModels: [], modelRegistry: {
14
+ getAvailable: () => catalog,
15
+ findOfType: (...args) => { assert.deepEqual(args, ['classifier', 'typesafe', 'jev-latest']); return {}; },
16
+ classify: async (_, {questions}) => {
17
+ calls++;
18
+ assert.doesNotMatch(JSON.stringify(questions), /other\/auto/);
19
+ const choice = value => ({type: 'choice', choice: value});
20
+ return {stopReason: 'stop', answers: {model: choice(chosen), effort: choice(level)}};
21
+ },
22
+ }};
23
+ const run = (reason, options = {}) => route({reason, messages: [{role:'user', content:'Test task'}], ...options}, ctx);
24
+ const first = await run('user');
25
+ assert.deepEqual(first, {model: b, thinkingLevel: 'high'});
26
+ assert.deepEqual(await run('continuation', {previous: first}), first);
27
+ assert.deepEqual(await run('retry', {failed: first}), first);
28
+ assert.equal(calls, 1);
29
+ chosen = 'm0';
30
+ assert.deepEqual(await run('user', {previous: first}), {model: a, thinkingLevel: 'off'});
31
+ assert.equal(calls, 2);
32
+ catalog = [a, {...b, id:'new'}];
33
+ ctx.scopedModels = [{model: catalog[1]}];
34
+ level = 'low';
35
+ assert.deepEqual(await run('user'), {model: catalog[1], thinkingLevel: 'low'});
36
+ ctx.scopedModels = [{model: virtual}];
37
+ await assert.rejects(run('user'), /no eligible/);
38
+ ctx.scopedModels = [];
39
+ chosen = 'invented';
40
+ await assert.rejects(run('user'), /invalid classifier choice/);
41
+ console.log('Passed: per-prompt model and effort, sticky turn, dynamic catalog, scope and invalid choices (mock Jev).');