okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-split-fb3 into f-split-int (locale keys re-added after the card-list strings)7b06ea51b

main
5.1 KiB85 linesraw
1/**
2 * Helper agents on a model the person names: the Harness `subagent` tool with a forgiving model choice.
3 *
4 * The pinned Harness's `subagent` tool takes an exact `provider` route key and model id, and its discovery tool lists them as
5 * `api-1b2c3d4e-5f/deepseek/deepseek-v3.2`. Nobody (and no small local model) can guess that from "ask DeepSeek". This plugin keeps the
6 * Harness's tool and its checks and changes three things at the seam where the tool is called:
7 *
8 *   - the choice: the model id, `provider/id`, the name in the model picker, or "the local model" (any letters) is turned into the route
9 *     the Harness needs; a close guess is not started but answered with "Did you mean ...?" for the person to confirm; nothing that fits
10 *     is answered with the plain list of models that can be used (server/agent/model-routes.mjs);
11 *   - the words the assistant reads: the tool says when to use it ("ask a subagent", "ask model X") and that the model must never be looked for
12 *     with shell commands or in the environment;
13 *   - the discovery tool lists the models by the names the person knows.
14 *
15 * It rewrites no Harness file: the two tool definitions the Harness registers for each chat are wrapped in place (their `execute` and their
16 * text), once each. A Harness update that renames them leaves the tools as they were and the test that runs the real Harness fails.
17 *
18 * Plain ESM, loaded by absolute path like strata-gate.mjs. `config.routes` is the table the profile wrote (model-routes.mjs documents a route).
19 */
20import { modelProblem, resolveModel, routeList } from '../model-routes.mjs';
21
22export const name = 'strata-models';
23export const inject = ['tools'];
24
25/** What the assistant is told in the tool's own text, so every model (a small local one and a hosted one alike) knows when and how. */
26export const SUBAGENT_GUIDANCE = ' Use this tool whenever the person asks you to ask a subagent, a helper or another model ("ask a subagent", "ask DeepSeek", "use the local model", "ask model X"): start it with this tool and do not look for the model yourself with shell commands, in the environment or on this computer. To use a particular model, give `model` as its name or id (for example "DeepSeek V3.2" or "deepseek/deepseek-v3.2", or "local" for the model running on this computer); `provider` is optional. `list_subagent_models` shows what can be used. After starting a helper, do not wait with sleep or commands: end your turn and its answer arrives as a new message.';
27const MODEL_WORDS = 'The model to use for the helper: its name as the person knows it ("DeepSeek V3.2"), its id ("deepseek/deepseek-v3.2") or "local" for the model running on this computer. Omit it to use this chat\'s model.';
28const PROVIDER_WORDS = 'Optional: the connection the model belongs to ("OpenRouter"). Usually the model alone is enough.';
29
30const wrapped = new WeakSet();
31
32/** The two wrappers' behaviour, apart from the Harness objects (the tests call these directly). */
33export function subagentArgs(routes, args) {
34  const result = resolveModel(routes, args ?? {});
35  if (result.status !== 'exact') return { ok: result.status === 'inherit', args };
36  const { provider, model } = result.route;
37  return { ok: true, args: { ...args, provider, model } };
38}
39
40export function apply(ctx, config = {}) {
41  const routes = Array.isArray(config.routes) ? config.routes : [];
42
43  const wrapSubagent = def => {
44    if (wrapped.has(def)) return;
45    wrapped.add(def);
46    const original = def.execute;
47    def.description = `${def.description}${SUBAGENT_GUIDANCE}`;
48    const props = def.parameters?.properties;
49    if (props?.model) props.model = { ...props.model, description: MODEL_WORDS };
50    if (props?.provider) props.provider = { ...props.provider, description: PROVIDER_WORDS };
51    def.execute = function execute(args, exec) {
52      const request = args && typeof args === 'object' ? args : {};
53      const problem = modelProblem(routes, request);
54      if (problem) throw new Error(problem);
55      const settled = subagentArgs(routes, request);
56      return original.call(this, settled.args, exec);
57    };
58  };
59
60  const wrapList = def => {
61    if (wrapped.has(def)) return;
62    wrapped.add(def);
63    def.description = 'List the models a helper agent can be started on, by the names the person knows. Call it with no arguments, then give `model` (a name or id from the list, or "local") to the subagent tool.';
64    def.execute = function execute() {
65      return routes.length
66        ? `Models a helper can use:\n${routeList(routes)}\nStart one with the subagent tool: model = its name or id.`
67        : 'No other model is set up. A helper agent uses this chat\'s own model.';
68    };
69  };
70
71  const wrapAll = () => {
72    let agents;
73    try { agents = ctx.get('agents')?.list?.() ?? []; } catch { return; }
74    for (const agent of agents) {
75      try {
76        const sub = ctx.tools.get('subagent', agent);
77        if (sub) wrapSubagent(sub);
78        const list = ctx.tools.get('list_subagent_models', agent);
79        if (list) wrapList(list);
80      } catch { /* a tool that is not there yet is wrapped at the next change */ }
81    }
82  };
83  ctx.on('tools/change', wrapAll);
84  wrapAll();
85}