relay-opencode 0.0.0-stage → 0.2.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 +21 -0
- package/README.md +122 -2
- package/dist/artificial-analysis.d.ts +50 -0
- package/dist/artificial-analysis.js +153 -0
- package/dist/chat-facts.d.ts +26 -0
- package/dist/chat-facts.js +96 -0
- package/dist/contract.d.ts +2727 -0
- package/dist/contract.js +111 -0
- package/dist/costs.d.ts +14 -0
- package/dist/costs.js +47 -0
- package/dist/decider.d.ts +73 -0
- package/dist/decider.js +199 -0
- package/dist/gui-experimental.cjs +92 -0
- package/dist/gui-styles.d.ts +5 -0
- package/dist/gui-styles.js +95 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +62 -0
- package/dist/intelligence.d.ts +38 -0
- package/dist/intelligence.js +123 -0
- package/dist/inventory.d.ts +34 -0
- package/dist/inventory.js +28 -0
- package/dist/ranking.d.ts +31 -0
- package/dist/ranking.js +78 -0
- package/dist/relay-auto.d.ts +39 -0
- package/dist/relay-auto.js +96 -0
- package/dist/relay-command.d.ts +74 -0
- package/dist/relay-command.js +211 -0
- package/dist/relay-store.d.ts +124 -0
- package/dist/relay-store.js +196 -0
- package/dist/relay-view.d.ts +124 -0
- package/dist/relay-view.js +189 -0
- package/dist/review-helper.d.ts +8 -0
- package/dist/review-helper.js +68 -0
- package/dist/router.d.ts +122 -0
- package/dist/router.js +209 -0
- package/dist/rpc.d.ts +2725 -0
- package/dist/rpc.js +116 -0
- package/dist/settings.d.ts +16 -0
- package/dist/settings.js +22 -0
- package/dist/standings.d.ts +18 -0
- package/dist/standings.js +34 -0
- package/dist/tui.d.ts +3 -0
- package/dist/tui.js +26 -0
- package/dist/usage-log.d.ts +135 -0
- package/dist/usage-log.js +210 -0
- package/dist/usage-view.d.ts +70 -0
- package/dist/usage-view.js +104 -0
- package/package.json +46 -4
package/dist/router.js
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { offered } from './settings.js';
|
|
2
|
+
import { modelName, modes } from './relay-store.js';
|
|
3
|
+
import { aboutUsd, parseUsd, pricesAt } from './costs.js';
|
|
4
|
+
import { rank } from './ranking.js';
|
|
5
|
+
// Pure rules: which of a mode's models can be used now, which "use it for" lines the decider is asked about, and what its answer means for the chat.
|
|
6
|
+
// Nothing here calls the host, and nothing here may import from `node:` (the panel bundles this file).
|
|
7
|
+
/** The error's own message when it has one, so a person sees why something was refused; else the fallback. */
|
|
8
|
+
export function describeError(error, fallback) {
|
|
9
|
+
const message = typeof error === 'string' ? error : error && typeof error === 'object' && typeof error.message === 'string' ? error.message : '';
|
|
10
|
+
return message.trim() ? message : fallback;
|
|
11
|
+
}
|
|
12
|
+
/** Below this the decider is not sure of the mode, and the mode is Plan. */
|
|
13
|
+
export const UNSURE_BELOW = 0.6;
|
|
14
|
+
/** Below this the decider is not sure of the line, and no line counts as chosen. */
|
|
15
|
+
export const LINE_UNSURE_BELOW = 0.5;
|
|
16
|
+
export const MAX_LINES = 8;
|
|
17
|
+
export const REVIEW_RULES_KEY = 'relay-review-rules';
|
|
18
|
+
export const reviewRules = [
|
|
19
|
+
'When you review work in this chat, follow these rules.',
|
|
20
|
+
'1. Report a blocking finding only when you have concrete evidence of a defect or of an unmet requirement. Everything else is an advisory note, and a note never calls for another round.',
|
|
21
|
+
'2. End every review with a verdict: "approve" or "request changes". Approve when no blocking finding remains.',
|
|
22
|
+
'3. When you check work again after a fix, look only at whether the earlier blocking findings are fixed and whether the fix broke something. Do not start a new full review.',
|
|
23
|
+
'4. One piece of work gets at most three checks. After the third, stop and list what is still open so the person can decide.',
|
|
24
|
+
].join('\n');
|
|
25
|
+
const labels = { plan: 'Plan', execute: 'Execute', review: 'Review' };
|
|
26
|
+
export const modeLabel = (mode) => labels[mode];
|
|
27
|
+
/** Cuts text to at most `max` characters without ending inside a two-unit character (a surrogate pair). */
|
|
28
|
+
export function safeHead(text, max) {
|
|
29
|
+
const head = text.slice(0, max);
|
|
30
|
+
return /[\uD800-\uDBFF]$/.test(head) ? head.slice(0, -1) : head;
|
|
31
|
+
}
|
|
32
|
+
/** A failure's message as part of a note: one line, at most 200 characters. */
|
|
33
|
+
export const brief = (error) => safeHead(describeError(error, 'unknown error').replace(/\s+/g, ' ').trim(), 200);
|
|
34
|
+
const plain = ({ providerID, id, variant }) => ({ providerID, id, variant });
|
|
35
|
+
const same = (a, b) => a.providerID === b.providerID && a.id === b.id && a.variant === b.variant;
|
|
36
|
+
/**
|
|
37
|
+
* How much of a model's window a chat may fill. OpenCode 2.3.0 compacts a chat before a request once less than 15% of the window is free,
|
|
38
|
+
* or less than 16,000 tokens for a window of 32,000 or more (read in its source, not measured).
|
|
39
|
+
*/
|
|
40
|
+
const room = (window) => window - Math.max(Math.floor(window * 0.15), window >= 32_000 ? 16_000 : 0);
|
|
41
|
+
/** The models of one mode's list that can be used now, in the person's order. When none can, the note says why. `current` is the model the chat is on, when that is known. */
|
|
42
|
+
export function candidates(mode, list, models, attachment, current = null) {
|
|
43
|
+
const label = modeLabel(mode), none = (note) => ({ usable: [], note });
|
|
44
|
+
if (!list.length)
|
|
45
|
+
return none(`no model chosen for ${label}`);
|
|
46
|
+
const listed = list.flatMap(choice => { const model = offered(models, choice); return model ? [{ choice, model }] : []; });
|
|
47
|
+
if (!listed.length)
|
|
48
|
+
return none(`no chosen ${label} model is offered right now`);
|
|
49
|
+
const seeing = attachment.needsImage ? listed.filter(item => item.model.capabilities.input.includes('image')) : listed;
|
|
50
|
+
if (!seeing.length)
|
|
51
|
+
return none(`no chosen ${label} model takes pictures`);
|
|
52
|
+
// shortcut: the new message itself is not counted, and a file is counted at four bytes to a token, so a chat close to a model's limit can still overflow it. A model whose size is not listed (0) is kept.
|
|
53
|
+
const fitting = seeing.filter(item => !(item.model.limit.context > 0 && attachment.tokens > item.model.limit.context));
|
|
54
|
+
if (!fitting.length)
|
|
55
|
+
return none(`the attached files are too big for every chosen ${label} model`);
|
|
56
|
+
// The whole chat must fit as well, with the attached files on top. The model the chat is on holds it already, whatever its effort.
|
|
57
|
+
const whole = attachment.chat && attachment.chat.tokens > 0 ? attachment.chat.tokens + attachment.tokens : 0;
|
|
58
|
+
const holding = fitting.filter(item => !(item.model.limit.context > 0 && whole > room(item.model.limit.context)) || (current !== null && item.choice.providerID === current.providerID && item.choice.id === current.id));
|
|
59
|
+
if (!holding.length)
|
|
60
|
+
return none(`this chat is too long for every chosen ${label} model`);
|
|
61
|
+
return { usable: holding.map(item => item.choice), note: null };
|
|
62
|
+
}
|
|
63
|
+
/** The usable models the decider is asked about: those with a line, at most eight. A line answer's index points into this list. */
|
|
64
|
+
export const described = (usable) => usable.filter(choice => choice.useFor !== '').slice(0, MAX_LINES);
|
|
65
|
+
/** The lines to ask about, for each mode where there is a choice to make: two or more usable models, and at least one line. */
|
|
66
|
+
export function linesFor(settings, models, attachment, current = null) {
|
|
67
|
+
const lines = {};
|
|
68
|
+
for (const mode of modes) {
|
|
69
|
+
const { usable } = candidates(mode, settings.modes[mode], models, attachment, current), asked = described(usable);
|
|
70
|
+
if (usable.length > 1 && asked.length)
|
|
71
|
+
lines[mode] = asked.map(choice => choice.useFor);
|
|
72
|
+
}
|
|
73
|
+
return lines;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* One model out of a mode's usable ones (never empty). When no line fits clearly, the first model of the list is used, whatever the chat is
|
|
77
|
+
* on: the person's order is the default (the owner, 19 Nov). A short follow-up such as "Implement it." fits no line, and keeping the chat's
|
|
78
|
+
* model kept the plan's dearer model for all the work (docs/compatibility.md, 19 Nov). The cost check after this can still keep the chat's model.
|
|
79
|
+
*/
|
|
80
|
+
export function pickModel(usable, line) {
|
|
81
|
+
const asked = described(usable);
|
|
82
|
+
if (usable.length === 1 || !asked.length)
|
|
83
|
+
return { model: plain(usable[0]), fit: null, note: null };
|
|
84
|
+
const chosen = line && line.index !== null ? asked[line.index] : undefined;
|
|
85
|
+
if (line && chosen && line.confidence >= LINE_UNSURE_BELOW)
|
|
86
|
+
return { model: plain(chosen), fit: line.confidence, note: null };
|
|
87
|
+
const reason = line && (line.index === null || chosen) ? 'no line fits clearly' : 'no answer about the lines';
|
|
88
|
+
return { model: plain(usable[0]), fit: null, note: `${reason}, so the first model is picked` };
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* One model out of a mode's usable ones. With no line in the mode's list and two or more usable models, Relay chooses by itself
|
|
92
|
+
* (src/ranking.ts): big work goes to the highest index, small work to the cheapest, and an unsure size to the highest index, since unclear
|
|
93
|
+
* work counts as big (the owner, 25 Nov). The first model is used when Relay can't rank the list or the decider gives no size (19 Nov). Otherwise the decider's line decides, as `pickModel` says.
|
|
94
|
+
* `standings` undefined keeps that older rule for every mode; null means the stored copies could not be read in time.
|
|
95
|
+
*/
|
|
96
|
+
export function choose(mode, list, usable, answer, standings, writer) {
|
|
97
|
+
const lined = () => pickModel(usable, answer.lines[mode]);
|
|
98
|
+
const first = (reason) => ({ model: plain(usable[0]), fit: null, note: `${reason}, so the first model is picked` });
|
|
99
|
+
if (standings === undefined)
|
|
100
|
+
return lined();
|
|
101
|
+
if (standings === null)
|
|
102
|
+
return list.some(choice => choice.useFor !== '') || usable.length < 2 ? lined() : first('the index and prices could not be read in time');
|
|
103
|
+
const ranked = rank(mode, list, usable, standings, writer);
|
|
104
|
+
if (ranked === null)
|
|
105
|
+
return lined();
|
|
106
|
+
if (ranked.kind === 'unranked')
|
|
107
|
+
return first(ranked.reason);
|
|
108
|
+
if (same(ranked.strongest, ranked.cheapest))
|
|
109
|
+
return { model: ranked.strongest, fit: null, note: null };
|
|
110
|
+
const size = answer.sizes?.[mode];
|
|
111
|
+
if (size && size.confidence >= LINE_UNSURE_BELOW) {
|
|
112
|
+
return size.size === 'big' ? { model: ranked.strongest, fit: size.confidence, note: 'big work, so the highest index' } : { model: ranked.cheapest, fit: size.confidence, note: 'small work, so the cheapest' };
|
|
113
|
+
}
|
|
114
|
+
if (!size)
|
|
115
|
+
return first('no size answer');
|
|
116
|
+
return { model: ranked.strongest, fit: null, note: 'not sure how big the work is, so the highest index' };
|
|
117
|
+
}
|
|
118
|
+
/** The modes whose size the decider is asked: those Relay ranks by itself, where the strongest is not also the cheapest. */
|
|
119
|
+
export function sizesFor(settings, models, attachment, current, standings) {
|
|
120
|
+
if (standings === null)
|
|
121
|
+
return [];
|
|
122
|
+
return modes.filter(mode => {
|
|
123
|
+
const list = settings.modes[mode], ranked = rank(mode, list, candidates(mode, list, models, attachment, current).usable, standings, current);
|
|
124
|
+
return ranked?.kind === 'ranked' && !same(ranked.strongest, ranked.cheapest);
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
// Stay or go: a step down to a cheaper model is made only when it pays. Arithmetic only, on whole numbers; no model is asked. The method
|
|
128
|
+
// and its worked examples are in docs/research/2026-11-17-relay-cost-and-model-choice.md (section 4.2). Nothing in it has been measured.
|
|
129
|
+
// shortcut: a cache is taken to live five minutes, OpenCode's default. A longer lifetime set in OpenCode is not read, so a chat that was idle for more than five minutes is always moved.
|
|
130
|
+
const CACHE_MS = 5 * 60_000;
|
|
131
|
+
// shortcut: each coming request is taken to read 1,500 new tokens and write 500, and a line with fewer than three records to take 6 requests, until Relay's records give real averages.
|
|
132
|
+
const NEW_IN = 1500n, NEW_OUT = 500n, USUAL_REQUESTS = 6;
|
|
133
|
+
/** A model's prices for a chat of `tokens`, in trillionths of a dollar per million tokens. Null when there is nothing to compare: no price, or $0, which can mean "not listed". */
|
|
134
|
+
function priceFor(cost, tokens) {
|
|
135
|
+
if (!cost)
|
|
136
|
+
return null;
|
|
137
|
+
const at = pricesAt(cost, tokens);
|
|
138
|
+
if (at.inputPerMillion === null || at.outputPerMillion === null)
|
|
139
|
+
return null;
|
|
140
|
+
try {
|
|
141
|
+
const input = parseUsd(at.inputPerMillion), output = parseUsd(at.outputPerMillion);
|
|
142
|
+
if (input === 0n && output === 0n)
|
|
143
|
+
return null;
|
|
144
|
+
const read = at.cacheReadPerMillion === null ? 0n : parseUsd(at.cacheReadPerMillion), write = at.cacheWritePerMillion === null ? 0n : parseUsd(at.cacheWritePerMillion);
|
|
145
|
+
// With no cached-read price the model is taken to have no cache; with no cache-write price, writing costs what input costs.
|
|
146
|
+
return { read: read > 0n ? read : input, write: write > input ? write : input, output };
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/** Whether the chat's model stays for this message although the line chose `pick`, with the note for the record. Only a move between two models of this mode's list is weighed. */
|
|
153
|
+
function stayOrGo(mode, pick, current, usable, models, attachment) {
|
|
154
|
+
const go = (note = null) => ({ stay: false, note }), { chat } = attachment;
|
|
155
|
+
const is = (model, choice) => model.providerID === choice.providerID && model.id === choice.id;
|
|
156
|
+
if (chat === undefined || current === null || is(pick, current) || !usable.some(choice => same(choice, current)))
|
|
157
|
+
return go();
|
|
158
|
+
if (chat === null)
|
|
159
|
+
return go("the chat's size could not be read, so no cost check");
|
|
160
|
+
// Nothing to lose: a new chat, one that was just compacted, a last answer from another model, or a cache that has run out.
|
|
161
|
+
const last = chat.answered[0];
|
|
162
|
+
if (!last || !is(last, current) || last.agoMs > CACHE_MS)
|
|
163
|
+
return go();
|
|
164
|
+
const size = Math.max(0, Math.round(chat.tokens)), priceOf = (choice) => priceFor(models.find(model => is(model, choice))?.cost, size);
|
|
165
|
+
const from = priceOf(current), to = priceOf(pick);
|
|
166
|
+
if (!from || !to)
|
|
167
|
+
return go('no price listed, so no cost check');
|
|
168
|
+
// Tokens times a price for a million tokens, so every amount here is a million times too big until it is shown.
|
|
169
|
+
const request = (price) => BigInt(size) * price.read + NEW_IN * price.write + NEW_OUT * price.output, less = request(from) - request(to);
|
|
170
|
+
if (less <= 0n)
|
|
171
|
+
return go(); // a step up: the line asked for the stronger model
|
|
172
|
+
const name = modelName(pick), took = (attachment.past ?? []).flatMap(item => item.mode === mode && item.model === name && item.reply ? [item.reply.requests] : []).sort((a, b) => a - b);
|
|
173
|
+
const requests = took.length >= 3 ? Math.max(1, Math.round(took[Math.floor((took.length - 1) / 2)])) : USUAL_REQUESTS;
|
|
174
|
+
// What the pick must read again: the whole chat, or only what was added since it last answered here, while its cache lives.
|
|
175
|
+
const held = chat.answered.find(item => is(item, pick) && item.agoMs <= CACHE_MS)?.tokens ?? 0;
|
|
176
|
+
const saving = BigInt(requests) * less, extra = BigInt(Math.max(0, size - Math.round(held))) * (to.write - to.read);
|
|
177
|
+
if (2n * saving >= 3n * extra)
|
|
178
|
+
return go(); // it pays when the saving is at least one and a half times the extra
|
|
179
|
+
return { stay: true, note: `moving this chat to ${name} would cost ${aboutUsd(extra / 1000000n)} to read it again and save ${aboutUsd(saving / 1000000n)} over ${requests} requests${saving >= extra ? ', less than one and a half times that cost' : ''}, so the chat's model stays` };
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* `unsure` says what an answer under UNSURE_BELOW means: Plan for `/relay`, no change for a chat whose switch is on. `standings` lets Relay
|
|
183
|
+
* choose by itself in a mode with no lines (see `choose`); left out, every mode follows its lines as before.
|
|
184
|
+
*/
|
|
185
|
+
export function route(answer, settings, models, attachment, current, unsure = 'plan', standings) {
|
|
186
|
+
if (answer.kind === 'none')
|
|
187
|
+
return { switch: false, mode: null, note: answer.reason };
|
|
188
|
+
const doubtful = answer.confidence < UNSURE_BELOW;
|
|
189
|
+
if (doubtful && unsure === 'stay')
|
|
190
|
+
return { switch: false, mode: null, note: 'not sure, so the chat stays as it is' };
|
|
191
|
+
const mode = doubtful ? 'plan' : answer.mode, notes = doubtful ? ['not sure, so Plan'] : [];
|
|
192
|
+
const { usable, note } = candidates(mode, settings.modes[mode], models, attachment, current);
|
|
193
|
+
if (note !== null)
|
|
194
|
+
return { switch: false, mode, note: [...notes, note].join('; ') };
|
|
195
|
+
const chosen = choose(mode, settings.modes[mode], usable, answer, standings, current), weighed = stayOrGo(mode, chosen.model, current, usable, models, attachment);
|
|
196
|
+
for (const said of [chosen.note, weighed.note])
|
|
197
|
+
if (said !== null)
|
|
198
|
+
notes.push(said);
|
|
199
|
+
// Whichever model is used, the mode still sets the agent and the review rules.
|
|
200
|
+
const kept = weighed.stay && current !== null;
|
|
201
|
+
return { switch: true, mode, agent: mode === 'execute' ? 'build' : 'plan', model: kept ? plain(current) : chosen.model, fit: kept ? null : chosen.fit, note: notes.length ? notes.join('; ') : null };
|
|
202
|
+
}
|
|
203
|
+
/** Whether a helper that checks work can be given a Review model at all, and if not, why. */
|
|
204
|
+
export function checksStatus(settings, models) {
|
|
205
|
+
if (!settings.decider)
|
|
206
|
+
return { active: false, note: 'no decider chosen' };
|
|
207
|
+
const { note } = candidates('review', settings.modes.review, models, { needsImage: false, tokens: 0 });
|
|
208
|
+
return { active: note === null, note };
|
|
209
|
+
}
|