relay-opencode 0.1.0 → 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/README.md +82 -115
- package/dist/artificial-analysis.d.ts +50 -0
- package/dist/artificial-analysis.js +153 -0
- package/dist/chat-facts.d.ts +19 -3
- package/dist/chat-facts.js +67 -12
- package/dist/contract.d.ts +1172 -6
- package/dist/contract.js +57 -3
- package/dist/costs.d.ts +7 -0
- package/dist/costs.js +22 -0
- package/dist/decider.d.ts +24 -0
- package/dist/decider.js +104 -14
- package/dist/gui-experimental.cjs +32 -6
- package/dist/gui-styles.d.ts +1 -1
- package/dist/gui-styles.js +29 -3
- package/dist/index.js +9 -3
- package/dist/intelligence.d.ts +4 -0
- package/dist/intelligence.js +10 -0
- package/dist/ranking.d.ts +31 -0
- package/dist/ranking.js +78 -0
- package/dist/relay-auto.d.ts +23 -4
- package/dist/relay-auto.js +53 -11
- package/dist/relay-command.d.ts +0 -5
- package/dist/relay-command.js +15 -13
- package/dist/relay-store.d.ts +64 -4
- package/dist/relay-store.js +94 -9
- package/dist/relay-view.d.ts +37 -1
- package/dist/relay-view.js +68 -8
- package/dist/review-helper.d.ts +1 -1
- package/dist/review-helper.js +26 -7
- package/dist/router.d.ts +30 -5
- package/dist/router.js +54 -10
- package/dist/rpc.d.ts +1168 -6
- package/dist/rpc.js +59 -16
- 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 +3 -2
package/dist/review-helper.js
CHANGED
|
@@ -1,10 +1,26 @@
|
|
|
1
1
|
import { LATE, within } from './decider.js';
|
|
2
|
-
import { getRelaySettings, modelName, recordDecision } from './relay-store.js';
|
|
3
|
-
import { brief, candidates, checksStatus,
|
|
2
|
+
import { getRelaySettings, hostVariant, modelName, recordDecision } from './relay-store.js';
|
|
3
|
+
import { brief, candidates, checksStatus, choose, linesFor, reviewRules, sizesFor, UNSURE_BELOW } from './router.js';
|
|
4
|
+
import { readStored, standingsOf } from './standings.js';
|
|
4
5
|
const nothingAttached = { needsImage: false, tokens: 0 };
|
|
5
6
|
const given = (value) => value !== undefined && value !== null && value !== '';
|
|
6
7
|
export function reviewHelperHook(deps) {
|
|
7
8
|
const readMs = deps.readMs ?? 5000;
|
|
9
|
+
/** For Relay's own choice only: what fails or takes more than half the time counts as unknown, and the helper is placed all the same. */
|
|
10
|
+
const spare = async (work) => {
|
|
11
|
+
try {
|
|
12
|
+
const value = await within(work, Math.min(readMs / 2, 1000));
|
|
13
|
+
return value === LATE ? null : value;
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
/** The calling chat's model wrote the work a check looks at, so no reviewer weaker than it is chosen (src/ranking.ts). */
|
|
20
|
+
const writerOf = async (sessionID) => {
|
|
21
|
+
const chat = await deps.session.get({ sessionID: sessionID });
|
|
22
|
+
return chat.model ? { providerID: chat.model.providerID, id: chat.model.id, variant: hostVariant(chat.model.variant) } : null;
|
|
23
|
+
};
|
|
8
24
|
/** The record is for the panel; losing it must never fail a helper. */
|
|
9
25
|
const record = async (sessionID, fields) => {
|
|
10
26
|
try {
|
|
@@ -20,14 +36,16 @@ export function reviewHelperHook(deps) {
|
|
|
20
36
|
// A call that continues an earlier helper keeps that helper as it was.
|
|
21
37
|
if (!input || typeof input !== 'object' || typeof input.prompt !== 'string' || !input.prompt.trim() || given(input.sessionID))
|
|
22
38
|
return;
|
|
23
|
-
const read = await within(() => Promise.all([getRelaySettings(deps.store), deps.chatModels()]), readMs);
|
|
39
|
+
const read = await within(() => Promise.all([getRelaySettings(deps.store), deps.chatModels(), spare(() => readStored(deps.store)), spare(() => writerOf(event.sessionID))]), readMs);
|
|
24
40
|
if (read === LATE)
|
|
25
41
|
throw new Error(`did not answer within ${readMs / 1000} seconds`);
|
|
26
|
-
const [settings, models] = read;
|
|
42
|
+
const [settings, models, stored, writer] = read;
|
|
27
43
|
if (!checksStatus(settings, models).active)
|
|
28
44
|
return;
|
|
29
45
|
const lines = linesFor(settings, models, nothingAttached).review;
|
|
30
|
-
const
|
|
46
|
+
const standings = stored === null ? null : standingsOf(stored, models, [...settings.modes.review, ...(writer ? [writer] : [])]);
|
|
47
|
+
const sizes = sizesFor(settings, models, nothingAttached, writer, standings).filter(mode => mode === 'review');
|
|
48
|
+
const answer = await deps.decide(settings.decider, { kind: 'helper', text: input.prompt, agent: null, files: [], images: 0, lines: lines ? { review: lines } : {}, sizes });
|
|
31
49
|
if (answer.kind === 'none')
|
|
32
50
|
return await record(event.sessionID, { ms: answer.ms, note: answer.reason });
|
|
33
51
|
const decided = { mode: answer.mode, confidence: answer.confidence, ms: answer.ms, costUsd: answer.costUsd };
|
|
@@ -35,8 +53,9 @@ export function reviewHelperHook(deps) {
|
|
|
35
53
|
return await record(event.sessionID, { ...decided, note: 'not a check' });
|
|
36
54
|
if (answer.confidence < UNSURE_BELOW)
|
|
37
55
|
return await record(event.sessionID, { ...decided, note: 'not sure it is a check' });
|
|
38
|
-
// A helper has no model of its own to keep, so with no clear line the first usable Review model is used.
|
|
39
|
-
const
|
|
56
|
+
// A helper has no model of its own to keep, so with no clear line, or no clear size, the first usable Review model is used.
|
|
57
|
+
const usable = candidates('review', settings.modes.review, models, nothingAttached).usable;
|
|
58
|
+
const picked = choose('review', settings.modes.review, usable, answer, standings, writer), model = modelName(picked.model);
|
|
40
59
|
event.input = { ...input, model, prompt: `${input.prompt}\n\n${reviewRules}` };
|
|
41
60
|
const asked = typeof input.model === 'string' && input.model.trim() !== model ? `its caller asked for ${input.model.trim()}` : null;
|
|
42
61
|
const notes = [asked, picked.note].filter(Boolean);
|
package/dist/router.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type ModelChoice, type OfferedModel } from './settings.js';
|
|
2
2
|
import { type Decision, type Mode, type ModeChoice, type RelaySettings } from './relay-store.js';
|
|
3
3
|
import type { ModelCostView } from './inventory.js';
|
|
4
|
+
import { type SizeAnswer, type Standings } from './ranking.js';
|
|
4
5
|
/** The error's own message when it has one, so a person sees why something was refused; else the fallback. */
|
|
5
6
|
export declare function describeError(error: unknown, fallback: string): string;
|
|
6
7
|
/** Below this the decider is not sure of the mode, and the mode is Plan. */
|
|
@@ -20,11 +21,13 @@ export type LineAnswer = {
|
|
|
20
21
|
index: number | null;
|
|
21
22
|
confidence: number;
|
|
22
23
|
};
|
|
24
|
+
/** `sizes` holds the decider's answer about how big the work is, for the modes Relay chooses for by itself (src/ranking.ts). */
|
|
23
25
|
export type DeciderAnswer = {
|
|
24
26
|
kind: 'answer';
|
|
25
27
|
mode: Mode;
|
|
26
28
|
confidence: number;
|
|
27
29
|
lines: Partial<Record<Mode, LineAnswer>>;
|
|
30
|
+
sizes?: Partial<Record<Mode, SizeAnswer>>;
|
|
28
31
|
ms: number;
|
|
29
32
|
costUsd: string | null;
|
|
30
33
|
} | {
|
|
@@ -41,7 +44,7 @@ export type RoutableModel = OfferedModel & {
|
|
|
41
44
|
};
|
|
42
45
|
cost?: ModelCostView;
|
|
43
46
|
};
|
|
44
|
-
/** What is known of a chat: its size in tokens, and each model's latest answer in it, newest first, with the chat's size then and how long ago
|
|
47
|
+
/** What is known of a chat: its size in tokens, and each model's latest answer in it, newest first, with the chat's size then and how long ago its request began. */
|
|
45
48
|
export type ChatFacts = {
|
|
46
49
|
tokens: number;
|
|
47
50
|
answered: readonly {
|
|
@@ -82,14 +85,36 @@ export declare function candidates(mode: Mode, list: readonly ModeChoice[], mode
|
|
|
82
85
|
export declare const described: (usable: readonly ModeChoice[]) => ModeChoice[];
|
|
83
86
|
/** 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. */
|
|
84
87
|
export declare function linesFor(settings: Pick<RelaySettings, 'modes'>, models: readonly RoutableModel[], attachment: Attachment, current?: ModelChoice | null): Partial<Record<Mode, string[]>>;
|
|
85
|
-
/**
|
|
86
|
-
|
|
88
|
+
/**
|
|
89
|
+
* 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
|
|
90
|
+
* 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
|
|
91
|
+
* 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.
|
|
92
|
+
*/
|
|
93
|
+
export declare function pickModel(usable: readonly ModeChoice[], line: LineAnswer | undefined): {
|
|
87
94
|
model: ModelChoice;
|
|
88
95
|
fit: number | null;
|
|
89
96
|
note: string | null;
|
|
90
97
|
};
|
|
91
|
-
/**
|
|
92
|
-
|
|
98
|
+
/**
|
|
99
|
+
* 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
|
|
100
|
+
* (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
|
|
101
|
+
* 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.
|
|
102
|
+
* `standings` undefined keeps that older rule for every mode; null means the stored copies could not be read in time.
|
|
103
|
+
*/
|
|
104
|
+
export declare function choose(mode: Mode, list: readonly ModeChoice[], usable: readonly ModeChoice[], answer: Extract<DeciderAnswer, {
|
|
105
|
+
kind: 'answer';
|
|
106
|
+
}>, standings: Standings | null | undefined, writer: ModelChoice | null): {
|
|
107
|
+
model: ModelChoice;
|
|
108
|
+
fit: number | null;
|
|
109
|
+
note: string | null;
|
|
110
|
+
};
|
|
111
|
+
/** The modes whose size the decider is asked: those Relay ranks by itself, where the strongest is not also the cheapest. */
|
|
112
|
+
export declare function sizesFor(settings: Pick<RelaySettings, 'modes'>, models: readonly RoutableModel[], attachment: Attachment, current: ModelChoice | null, standings: Standings | null): Mode[];
|
|
113
|
+
/**
|
|
114
|
+
* `unsure` says what an answer under UNSURE_BELOW means: Plan for `/relay`, no change for a chat whose switch is on. `standings` lets Relay
|
|
115
|
+
* choose by itself in a mode with no lines (see `choose`); left out, every mode follows its lines as before.
|
|
116
|
+
*/
|
|
117
|
+
export declare function route(answer: DeciderAnswer, settings: Pick<RelaySettings, 'modes'>, models: readonly RoutableModel[], attachment: Attachment, current: ModelChoice | null, unsure?: 'plan' | 'stay', standings?: Standings | null): RouteResult;
|
|
93
118
|
/** Whether a helper that checks work can be given a Review model at all, and if not, why. */
|
|
94
119
|
export declare function checksStatus(settings: Pick<RelaySettings, 'modes' | 'decider'>, models: readonly RoutableModel[]): {
|
|
95
120
|
active: boolean;
|
package/dist/router.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { offered } from './settings.js';
|
|
2
2
|
import { modelName, modes } from './relay-store.js';
|
|
3
|
-
import { aboutUsd, parseUsd } from './costs.js';
|
|
3
|
+
import { aboutUsd, parseUsd, pricesAt } from './costs.js';
|
|
4
|
+
import { rank } from './ranking.js';
|
|
4
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.
|
|
5
6
|
// Nothing here calls the host, and nothing here may import from `node:` (the panel bundles this file).
|
|
6
7
|
/** The error's own message when it has one, so a person sees why something was refused; else the fallback. */
|
|
@@ -71,8 +72,12 @@ export function linesFor(settings, models, attachment, current = null) {
|
|
|
71
72
|
}
|
|
72
73
|
return lines;
|
|
73
74
|
}
|
|
74
|
-
/**
|
|
75
|
-
|
|
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) {
|
|
76
81
|
const asked = described(usable);
|
|
77
82
|
if (usable.length === 1 || !asked.length)
|
|
78
83
|
return { model: plain(usable[0]), fit: null, note: null };
|
|
@@ -80,8 +85,44 @@ export function pickModel(usable, line, current) {
|
|
|
80
85
|
if (line && chosen && line.confidence >= LINE_UNSURE_BELOW)
|
|
81
86
|
return { model: plain(chosen), fit: line.confidence, note: null };
|
|
82
87
|
const reason = line && (line.index === null || chosen) ? 'no line fits clearly' : 'no answer about the lines';
|
|
83
|
-
|
|
84
|
-
|
|
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
|
+
});
|
|
85
126
|
}
|
|
86
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
|
|
87
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.
|
|
@@ -93,7 +134,7 @@ const NEW_IN = 1500n, NEW_OUT = 500n, USUAL_REQUESTS = 6;
|
|
|
93
134
|
function priceFor(cost, tokens) {
|
|
94
135
|
if (!cost)
|
|
95
136
|
return null;
|
|
96
|
-
const at =
|
|
137
|
+
const at = pricesAt(cost, tokens);
|
|
97
138
|
if (at.inputPerMillion === null || at.outputPerMillion === null)
|
|
98
139
|
return null;
|
|
99
140
|
try {
|
|
@@ -135,10 +176,13 @@ function stayOrGo(mode, pick, current, usable, models, attachment) {
|
|
|
135
176
|
const saving = BigInt(requests) * less, extra = BigInt(Math.max(0, size - Math.round(held))) * (to.write - to.read);
|
|
136
177
|
if (2n * saving >= 3n * extra)
|
|
137
178
|
return go(); // it pays when the saving is at least one and a half times the extra
|
|
138
|
-
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, so the chat's model stays` };
|
|
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` };
|
|
139
180
|
}
|
|
140
|
-
/**
|
|
141
|
-
|
|
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) {
|
|
142
186
|
if (answer.kind === 'none')
|
|
143
187
|
return { switch: false, mode: null, note: answer.reason };
|
|
144
188
|
const doubtful = answer.confidence < UNSURE_BELOW;
|
|
@@ -148,7 +192,7 @@ export function route(answer, settings, models, attachment, current, unsure = 'p
|
|
|
148
192
|
const { usable, note } = candidates(mode, settings.modes[mode], models, attachment, current);
|
|
149
193
|
if (note !== null)
|
|
150
194
|
return { switch: false, mode, note: [...notes, note].join('; ') };
|
|
151
|
-
const chosen =
|
|
195
|
+
const chosen = choose(mode, settings.modes[mode], usable, answer, standings, current), weighed = stayOrGo(mode, chosen.model, current, usable, models, attachment);
|
|
152
196
|
for (const said of [chosen.note, weighed.note])
|
|
153
197
|
if (said !== null)
|
|
154
198
|
notes.push(said);
|