relay-opencode 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/dist/rpc.js ADDED
@@ -0,0 +1,73 @@
1
+ import { RelayRpc } from './contract.js';
2
+ export { RelayRpc } from './contract.js';
3
+ import { listProjectModels } from './inventory.js';
4
+ import { getRelaySettings, saveRelaySettings, recentDecisions, modelName, chatSwitch, setChatSwitch } from './relay-store.js';
5
+ import { checksStatus, describeError, linesFor, route } from './router.js';
6
+ import { decide } from './decider.js';
7
+ import { INDEX_SOURCE, indexFor, loadIndexTable } from './intelligence.js';
8
+ const chatID = (input) => { const id = input.sessionID.trim(); if (!id)
9
+ throw new Error('Name the chat'); return id; };
10
+ /** The panel's and the pill's calls. Relay's settings are one set for every project; only the model lists come from this project's host. */
11
+ export function registerRpc(context, options = {}) {
12
+ const relayStore = context.storage;
13
+ // One fetch of the index at a time, shared by the calls that arrive while it runs.
14
+ let indexLoad;
15
+ const indexTable = () => indexLoad ??= loadIndexTable(context.storage, options.fetcher ?? fetch, (options.now ?? Date.now)()).finally(() => { indexLoad = undefined; });
16
+ const relayView = async (settings, chat, deciders) => ({ directory: context.location.directory, settings, decisions: await recentDecisions(relayStore), deciders, checks: checksStatus(settings, chat) });
17
+ return context.rpc.register(RelayRpc, guarded({
18
+ listModels: async () => ({ directory: context.location.directory, models: await listProjectModels(context) }),
19
+ listIntelligence: async () => {
20
+ const [found, chat, deciders] = await Promise.all([indexTable(), listProjectModels(context), listProjectModels(context, 'decision')]);
21
+ const seen = new Set();
22
+ const scores = [...chat, ...deciders].flatMap(model => {
23
+ const key = `${model.providerID}/${model.id}`, hit = seen.has(key) ? null : indexFor(model, found.table);
24
+ seen.add(key);
25
+ return hit ? [{ providerID: model.providerID, id: model.id, index: hit.index, matched: hit.matched }] : [];
26
+ });
27
+ return { directory: context.location.directory, source: INDEX_SOURCE, fetchedAt: found.fetchedAt, note: found.note, scores };
28
+ },
29
+ getRelay: async () => {
30
+ const [settings, chat, deciders] = await Promise.all([getRelaySettings(relayStore), listProjectModels(context), listProjectModels(context, 'decision')]);
31
+ return relayView(settings, chat, deciders);
32
+ },
33
+ saveRelay: async (input) => {
34
+ const { revision, ...next } = input;
35
+ const [chat, deciders] = await Promise.all([listProjectModels(context), listProjectModels(context, 'decision')]);
36
+ return relayView(await saveRelaySettings(relayStore, revision, next, chat, deciders), chat, deciders);
37
+ },
38
+ // The pill: one chat's switch, that chat's newest record, and whether Relay can switch anything at all.
39
+ getRelayChat: async (input) => {
40
+ const sessionID = chatID(input);
41
+ const [on, decisions, settings] = await Promise.all([chatSwitch(relayStore, sessionID), recentDecisions(relayStore), getRelaySettings(relayStore)]);
42
+ return { on, last: decisions.find(item => item.sessionID === sessionID) ?? null, decider: settings.decider !== null };
43
+ },
44
+ getRelayDecisions: async () => ({ directory: context.location.directory, decisions: await recentDecisions(relayStore) }),
45
+ setRelayChat: async (input) => {
46
+ const sessionID = chatID(input), on = input.on;
47
+ await setChatSwitch(relayStore, sessionID, on);
48
+ return { on };
49
+ },
50
+ testDecider: async (input) => {
51
+ const text = input.text;
52
+ if (!text.trim())
53
+ throw new Error('Type a sample message');
54
+ const [settings, models] = await Promise.all([getRelaySettings(relayStore), listProjectModels(context)]);
55
+ // The same path as /relay with nothing attached and no chat: the lines asked about and the pick come from the router.
56
+ const attachment = { needsImage: false, tokens: 0 };
57
+ const answer = await decide({ decisions: request => context.experimental.generate.decisions(request) }, settings.decider, { kind: 'chat', text, agent: null, files: [], images: 0, lines: linesFor(settings, models, attachment) });
58
+ const result = route(answer, settings, models, attachment, null);
59
+ return { directory: context.location.directory, mode: result.mode, confidence: answer.kind === 'answer' ? answer.confidence : null, model: result.switch ? modelName(result.model) : null, fit: result.switch ? result.fit : null, ms: answer.ms, costUsd: answer.kind === 'answer' ? answer.costUsd : null, note: result.note };
60
+ },
61
+ }));
62
+ }
63
+ /** A handler that throws reaches the client as "RPC call failed" in the real host; return its message as the declared 'rejected' error so the panel can show it. */
64
+ function guarded(handlers) {
65
+ return Object.fromEntries(Object.entries(handlers).map(([name, handler]) => [name, async (input, call) => {
66
+ try {
67
+ return await handler(input, call);
68
+ }
69
+ catch (error) {
70
+ return call.error('rejected', describeError(error, 'The request failed.').slice(0, 500), {});
71
+ }
72
+ }]));
73
+ }
@@ -0,0 +1,16 @@
1
+ export type ModelChoice = {
2
+ providerID: string;
3
+ id: string;
4
+ variant: string | null;
5
+ };
6
+ export type OfferedModel = {
7
+ providerID: string;
8
+ id: string;
9
+ enabled: boolean;
10
+ status: string;
11
+ variants: readonly string[];
12
+ };
13
+ /** One list of model choices: every entry well formed, and none twice. */
14
+ export declare function validateChoices(value: unknown): ModelChoice[];
15
+ /** The listed model a choice points at, when it can be used now: listed, enabled, not deprecated, and offering the chosen variant. */
16
+ export declare function offered<M extends OfferedModel>(models: readonly M[], choice: ModelChoice): M | undefined;
@@ -0,0 +1,22 @@
1
+ // The checks every model choice goes through: well formed, not listed twice, and offered by the host now.
2
+ const id = (choice) => JSON.stringify([choice.providerID, choice.id, choice.variant]);
3
+ /** One list of model choices: every entry well formed, and none twice. */
4
+ export function validateChoices(value) {
5
+ if (!Array.isArray(value))
6
+ throw new Error('Invalid role choices');
7
+ const seen = new Set();
8
+ for (const choice of value) {
9
+ if (!choice || typeof choice !== 'object' || Array.isArray(choice) || Object.keys(choice).sort().join(',') !== 'id,providerID,variant' || typeof choice.providerID !== 'string' || !choice.providerID.trim() || typeof choice.id !== 'string' || !choice.id.trim() || !(choice.variant === null || (typeof choice.variant === 'string' && choice.variant.trim())))
10
+ throw new Error('Invalid model choice');
11
+ const identity = id(choice);
12
+ if (seen.has(identity))
13
+ throw new Error('Duplicate model choice');
14
+ seen.add(identity);
15
+ }
16
+ return structuredClone(value);
17
+ }
18
+ /** The listed model a choice points at, when it can be used now: listed, enabled, not deprecated, and offering the chosen variant. */
19
+ export function offered(models, choice) {
20
+ const model = models.find(m => m.providerID === choice.providerID && m.id === choice.id);
21
+ return model && model.enabled && model.status !== 'deprecated' && (choice.variant === null || model.variants.includes(choice.variant)) ? model : undefined;
22
+ }
package/package.json CHANGED
@@ -1,6 +1,47 @@
1
1
  {
2
2
  "name": "relay-opencode",
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": "Relay for OpenCode: picks the model for each message (plan, execute or review), switches the chat to it, and sends the message.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/AsadKhalil/relay-opencode.git"
9
+ },
10
+ "homepage": "https://github.com/AsadKhalil/relay-opencode#readme",
11
+ "bugs": "https://github.com/AsadKhalil/relay-opencode/issues",
12
+ "keywords": [
13
+ "opencode",
14
+ "opencode-plugin",
15
+ "relay",
16
+ "model-routing"
17
+ ],
18
+ "type": "module",
19
+ "scripts": {
20
+ "test": "tsx --test tests/*.test.ts",
21
+ "typecheck": "tsc --noEmit",
22
+ "build": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\" && tsc -p tsconfig.build.json && vite build",
23
+ "smoke:host:model": "node scripts/smoke-host-model.mjs",
24
+ "examples:decider": "tsx scripts/decider-examples.ts",
25
+ "prepublishOnly": "npm run typecheck && npm test && npm run build && node scripts/check-package.mjs"
26
+ },
27
+ "dependencies": {
28
+ "@opencode/plugin": "2.3.0"
29
+ },
30
+ "devDependencies": {
31
+ "@opencode/client": "2.3.0",
32
+ "@opencode/schema": "2.3.0",
33
+ "@types/node": "^24.0.0",
34
+ "solid-js": "^1.9.17",
35
+ "tsx": "^4.0.0",
36
+ "typescript": "^5.9.0",
37
+ "vite": "^7.3.7",
38
+ "vite-plugin-solid": "^2.11.10"
39
+ },
40
+ "exports": {
41
+ ".": "./dist/index.js",
42
+ "./gui-experimental": "./dist/gui-experimental.cjs"
43
+ },
44
+ "files": [
45
+ "dist"
46
+ ]
47
+ }