@parcha/agentrun-serve 0.1.0-beta.46 → 0.1.0-beta.48
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/CHANGELOG.md +211 -1
- package/NOTICE +4 -0
- package/README.md +350 -53
- package/dist/acp-package.d.ts +53 -6
- package/dist/acp-package.js +3 -3
- package/dist/adapter-facts.d.ts +3 -4
- package/dist/adapter-facts.js +4 -4
- package/dist/adapters.d.ts +3 -5
- package/dist/adapters.js +1 -31
- package/dist/agents-store.d.ts +5 -3
- package/dist/agents-store.js +13 -8
- package/dist/answer-json.d.ts +10 -0
- package/dist/answer-json.js +24 -0
- package/dist/api-only.d.ts +11 -11
- package/dist/api-only.js +16 -11
- package/dist/archil-routes.js +2 -1
- package/dist/browser-live.d.ts +9 -4
- package/dist/browser-live.js +18 -5
- package/dist/cap.js +10 -2
- package/dist/chatgpt-plan.js +2 -1
- package/dist/cli.js +109 -41
- package/dist/composio-made.d.ts +19 -2
- package/dist/composio-made.js +17 -5
- package/dist/composio-preset.d.ts +42 -7
- package/dist/composio-preset.js +106 -70
- package/dist/composio-triggers.d.ts +9 -4
- package/dist/composio-triggers.js +20 -13
- package/dist/computer-attach.d.ts +11 -10
- package/dist/computer-attach.js +13 -14
- package/dist/computer-commands.d.ts +3 -0
- package/dist/computer-commands.js +3 -2
- package/dist/computer-lost.d.ts +4 -0
- package/dist/computer-lost.js +8 -3
- package/dist/computer-routes.d.ts +8 -5
- package/dist/computer-routes.js +7 -5
- package/dist/computer-setting.d.ts +2 -0
- package/dist/computer-setting.js +2 -0
- package/dist/computers.d.ts +14 -76
- package/dist/computers.js +24 -68
- package/dist/conformance.d.ts +4 -0
- package/dist/conformance.js +6 -0
- package/dist/console/assets/index-CTGegcKt.css +1 -0
- package/dist/console/assets/index-D1Se6mgG.js +715 -0
- package/dist/console/index.html +2 -2
- package/dist/cost-heal.d.ts +13 -0
- package/dist/cost-heal.js +27 -0
- package/dist/dial-relay.d.ts +6 -2
- package/dist/dial-relay.js +9 -12
- package/dist/email/routes.js +2 -1
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +5 -0
- package/dist/exa-keyless.d.ts +1 -1
- package/dist/exa-keyless.js +1 -1
- package/dist/faux-computers.d.ts +2 -3
- package/dist/faux-computers.js +9 -31
- package/dist/github-connection.js +2 -1
- package/dist/harness-archive.d.ts +19 -2
- package/dist/harness-archive.js +49 -15
- package/dist/harness-check.d.ts +75 -0
- package/dist/harness-check.js +518 -0
- package/dist/harness-cursor.d.ts +28 -0
- package/dist/harness-cursor.js +42 -0
- package/dist/harness-goose.d.ts +4 -29
- package/dist/harness-goose.js +6 -86
- package/dist/harness-module.d.ts +4 -0
- package/dist/harness-module.js +1 -0
- package/dist/harness-owned.d.ts +52 -0
- package/dist/harness-owned.js +92 -0
- package/dist/harness-recipe.d.ts +127 -0
- package/dist/harness-recipe.js +335 -0
- package/dist/harness-table.d.ts +120 -5
- package/dist/harness-table.js +280 -6
- package/dist/harness.d.ts +2 -0
- package/dist/harness.js +5 -4
- package/dist/host-check.d.ts +6 -0
- package/dist/host-check.js +14 -0
- package/dist/host.d.ts +10 -3
- package/dist/host.js +17 -3
- package/dist/import-sources.d.ts +2 -0
- package/dist/import-sources.js +2 -0
- package/dist/import-summary.d.ts +15 -5
- package/dist/import-summary.js +24 -8
- package/dist/import.d.ts +24 -4
- package/dist/import.js +108 -26
- package/dist/key-refusal.d.ts +3 -1
- package/dist/key-refusal.js +26 -5
- package/dist/key-words.d.ts +1 -1
- package/dist/key-words.js +4 -4
- package/dist/learn/check.d.ts +3 -0
- package/dist/learn/check.js +31 -20
- package/dist/learn/compile.js +12 -5
- package/dist/learn/promote.d.ts +80 -2
- package/dist/learn/promote.js +95 -3
- package/dist/learn/secrets.d.ts +4 -0
- package/dist/learn/secrets.js +23 -0
- package/dist/learned-nodes.d.ts +4 -3
- package/dist/learned-workflow.js +4 -1
- package/dist/learning-cost.d.ts +6 -16
- package/dist/learning-cost.js +6 -12
- package/dist/learning-model.d.ts +18 -0
- package/dist/learning-model.js +77 -0
- package/dist/learning-words.d.ts +2 -0
- package/dist/learning-words.js +1 -1
- package/dist/listen-first.d.ts +10 -0
- package/dist/listen-first.js +25 -1
- package/dist/llms.txt +2 -1
- package/dist/mcp-gateway.d.ts +2 -1
- package/dist/mcp-gateway.js +31 -15
- package/dist/mcp-probe.d.ts +18 -6
- package/dist/mcp-probe.js +41 -8
- package/dist/mcp-secret-writes.d.ts +8 -0
- package/dist/mcp-secret-writes.js +104 -4
- package/dist/mcp-servers-store.d.ts +11 -5
- package/dist/mcp-servers-store.js +46 -20
- package/dist/model-form.d.ts +2 -1
- package/dist/model-form.js +30 -16
- package/dist/model-gateway.d.ts +113 -0
- package/dist/model-gateway.js +433 -0
- package/dist/model.d.ts +5 -3
- package/dist/model.js +26 -18
- package/dist/monid.js +1 -1
- package/dist/native/binding.d.ts +4 -0
- package/dist/native/binding.js +9 -0
- package/dist/native/cli.d.ts +3 -2
- package/dist/native/cli.js +6 -5
- package/dist/openapi.d.ts +2 -0
- package/dist/openapi.js +356 -43
- package/dist/owner.d.ts +4 -2
- package/dist/owner.js +10 -4
- package/dist/page.d.ts +9 -0
- package/dist/page.js +10 -2
- package/dist/port-models.js +17 -10
- package/dist/provider.d.ts +3 -3
- package/dist/provider.js +12 -3
- package/dist/public-address.d.ts +28 -0
- package/dist/public-address.js +62 -0
- package/dist/run-launch.d.ts +1 -1
- package/dist/run-launch.js +1 -1
- package/dist/run-secrets.d.ts +3 -2
- package/dist/run-secrets.js +7 -4
- package/dist/run-trace.js +6 -3
- package/dist/runner/adapter-loop.d.ts +14 -8
- package/dist/runner/adapter-loop.js +58 -24
- package/dist/runner/adapter-run-spec.d.ts +15 -2
- package/dist/runner/adapter-run-spec.js +48 -10
- package/dist/runner/adapters/index.d.ts +4 -2
- package/dist/runner/adapters/index.js +6 -26
- package/dist/runner/adapters/pi.d.ts +5 -2
- package/dist/runner/adapters/pi.js +7 -4
- package/dist/runner/audit.d.ts +7 -0
- package/dist/runner/audit.js +15 -6
- package/dist/runner/claim-secrets.js +5 -1
- package/dist/runner/cli-run-spec.js +22 -10
- package/dist/runner/compare-end.d.ts +43 -0
- package/dist/runner/compare-end.js +95 -0
- package/dist/runner/computer-journal.d.ts +3 -0
- package/dist/runner/computer-journal.js +22 -2
- package/dist/runner/conformance/host-tools.d.ts +4 -0
- package/dist/runner/conformance/host-tools.js +14 -0
- package/dist/runner/harness-loops.d.ts +6 -1
- package/dist/runner/harness-loops.js +94 -54
- package/dist/runner/held.d.ts +5 -4
- package/dist/runner/journal-kinds.d.ts +4 -1
- package/dist/runner/journal-kinds.js +3 -0
- package/dist/runner/loop-contract.d.ts +10 -0
- package/dist/runner/main.d.ts +5 -5
- package/dist/runner/main.js +62 -11
- package/dist/runner/mcp-claim.js +40 -12
- package/dist/runner/meta-tools.js +4 -2
- package/dist/runner/metrics.d.ts +26 -0
- package/dist/runner/metrics.js +44 -1
- package/dist/runner/presence.d.ts +44 -0
- package/dist/runner/presence.js +96 -0
- package/dist/runner/promotion.d.ts +70 -7
- package/dist/runner/promotion.js +178 -28
- package/dist/runner/run-child-host.js +40 -10
- package/dist/runner/runner.d.ts +5 -0
- package/dist/runner/runner.js +8 -0
- package/dist/runner/sdk-loop.js +25 -1
- package/dist/runner/store-port.d.ts +3 -0
- package/dist/runner/store-secrets.js +19 -7
- package/dist/runner/workflow-first.d.ts +5 -2
- package/dist/runner/workflow-first.js +6 -3
- package/dist/runner/workflow-spec.js +3 -2
- package/dist/runs-over-store.d.ts +7 -2
- package/dist/runs-over-store.js +12 -4
- package/dist/sdk/check.d.ts +31 -0
- package/dist/sdk/check.js +58 -0
- package/dist/sdk/runs.d.ts +11 -3
- package/dist/sdk/runs.js +90 -15
- package/dist/sdk/schema.js +12 -4
- package/dist/secret-writes.d.ts +16 -3
- package/dist/secret-writes.js +28 -3
- package/dist/server-secrets-store.js +5 -0
- package/dist/server.d.ts +34 -2
- package/dist/server.js +437 -90
- package/dist/sign-ins-secrets.js +4 -1
- package/dist/skills/agentrun-serve/SKILL.md +11 -0
- package/dist/slack-url.d.ts +5 -0
- package/dist/slack-url.js +11 -0
- package/dist/slack.d.ts +107 -0
- package/dist/slack.js +292 -0
- package/dist/start-switches.d.ts +28 -0
- package/dist/start-switches.js +37 -0
- package/dist/stats.d.ts +1 -1
- package/dist/stats.js +2 -1
- package/dist/stop-signals.d.ts +11 -0
- package/dist/stop-signals.js +22 -0
- package/dist/store/agent-body.d.ts +1 -1
- package/dist/store/agent-body.js +2 -2
- package/dist/store/index.d.ts +3 -3
- package/dist/store/index.js +1 -1
- package/dist/store/postgres/migrations/0020_runners.sql +18 -0
- package/dist/store/postgres/store.d.ts +1 -1
- package/dist/store/postgres/store.js +4 -2
- package/dist/store/schema.d.ts +13 -5
- package/dist/store/schema.js +20 -11
- package/dist/store/seal.d.ts +16 -4
- package/dist/store/sqlite/store.d.ts +1 -1
- package/dist/store/sqlite/store.js +112 -28
- package/dist/store/store.d.ts +78 -24
- package/dist/store/store.js +11 -0
- package/dist/store-reads/analytics.js +3 -2
- package/dist/store-reads/cap.d.ts +7 -0
- package/dist/store-reads/cap.js +11 -1
- package/dist/store-reads/journal.js +27 -1
- package/dist/store-reads/learning.d.ts +23 -2
- package/dist/store-reads/learning.js +29 -8
- package/dist/store-reads/runs.d.ts +9 -0
- package/dist/store-reads/runs.js +19 -6
- package/dist/store-reads/stats.d.ts +4 -1
- package/dist/store-reads/stats.js +15 -6
- package/dist/store-view.d.ts +125 -14
- package/dist/store-view.js +156 -21
- package/dist/telemetry.d.ts +7 -7
- package/dist/telemetry.js +38 -15
- package/dist/template-eval.js +5 -2
- package/dist/template-github-permissions.d.ts +5 -1
- package/dist/template-github-permissions.js +7 -3
- package/dist/timeline/index.d.ts +1 -1
- package/dist/timeline/index.js +1 -1
- package/dist/timeline/timeline.d.ts +8 -0
- package/dist/timeline/timeline.js +30 -0
- package/dist/tool-gate.d.ts +8 -4
- package/dist/tool-gate.js +6 -7
- package/dist/tool-keys-store.js +2 -2
- package/dist/tool-keys.js +2 -1
- package/dist/tool-plane.d.ts +4 -0
- package/dist/tool-plane.js +12 -12
- package/dist/trigger-adapter.d.ts +3 -4
- package/dist/trigger-adapter.js +2 -2
- package/dist/trigger-composio.js +3 -3
- package/dist/trigger-email.js +0 -2
- package/dist/trigger-feed.js +0 -2
- package/dist/trigger-schedule.js +3 -0
- package/dist/trigger-secrets-shown.d.ts +6 -3
- package/dist/trigger-secrets-shown.js +22 -13
- package/dist/trigger-targets.d.ts +12 -3
- package/dist/trigger-targets.js +90 -10
- package/dist/triggers.d.ts +2 -2
- package/dist/validate.js +5 -1
- package/dist/viewer-credential.d.ts +25 -0
- package/dist/viewer-credential.js +51 -0
- package/dist/webhooks.d.ts +82 -8
- package/dist/webhooks.js +128 -45
- package/dist/wires.d.ts +6 -1
- package/dist/wires.js +5 -0
- package/dist/workspace/attach.d.ts +3 -2
- package/dist/workspace/attach.js +2 -1
- package/dist/workspace/attached-computers.d.ts +6 -8
- package/dist/workspace/attached-computers.js +27 -24
- package/dist/workspace/computer-deps.d.ts +2 -3
- package/dist/workspace/computer-deps.js +3 -6
- package/dist/workspace/computer-for-claim.js +72 -96
- package/dist/workspace/computer-kept.d.ts +0 -7
- package/dist/workspace/computer-kept.js +0 -17
- package/dist/workspace/resolve-computer.d.ts +23 -14
- package/dist/workspace/resolve-computer.js +66 -61
- package/dist/workspace/synced-folder.d.ts +20 -15
- package/dist/workspace/synced-folder.js +33 -43
- package/launcher/agentrun-serve +4 -1
- package/package.json +18 -10
- package/skills/agentrun-serve/SKILL.md +11 -0
- package/templates/README.md +1 -1
- package/templates/code-review/agent.md +2 -3
- package/templates/code-review/evals/mentions-defused/case.yaml +0 -1
- package/templates/code-review/evals/mentions-defused/fixtures/github-mcp.json +0 -27
- package/templates/code-review/evals/other-repo/case.yaml +0 -1
- package/templates/code-review/evals/other-repo/fixtures/github-mcp.json +0 -18
- package/templates/code-review/evals/other-repo/graders/no-other-repo.md +1 -1
- package/dist/console/assets/index-C2MYaUBa.css +0 -1
- package/dist/console/assets/index-CZZeCReB.js +0 -701
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
// The model gateway the owner saves in the console (Settings → Model keys → Use a model gateway): an OpenAI-compatible
|
|
2
|
+
// proxy they already run (LiteLLM, or another), which every model is then sent to, as AGENT_MODEL_BASE_URL sends them.
|
|
3
|
+
// It is the same gateway the environment names, kept in the one store instead: its address, how it takes model ids, the
|
|
4
|
+
// default model of each tier (one settings row, no secret), and its key (a sealed-key row, sealed under the server's seal
|
|
5
|
+
// key as every saved key is). The routes:
|
|
6
|
+
// GET /v2/settings/model-gateway which gateway is in force (the environment's, the console's, or none), its
|
|
7
|
+
// address, id style and models, the variables set where AgentRun starts, and
|
|
8
|
+
// its key as every secret's view says it (never a value)
|
|
9
|
+
// PUT /v2/settings/model-gateway the gateway saved: its address (gatewayBase's one rule), its key (null
|
|
10
|
+
// removes it; left out, the saved one is kept for the same origin alone), its
|
|
11
|
+
// id style and its models
|
|
12
|
+
// DELETE /v2/settings/model-gateway the gateway and its key removed: the provider keys are back in force
|
|
13
|
+
// POST /v2/settings/model-gateway/test the gateway's GET <base>/v1/models, asked by the server, under the same rule,
|
|
14
|
+
// with no redirect followed and a timeout: { ok, models } or { ok: false, why }
|
|
15
|
+
// Which wins: AGENT_MODEL_BASE_URL set where AgentRun starts makes the environment's gateway the one in force, whole (its
|
|
16
|
+
// key, its id style, its models), and the saved one is not read; its PUT and DELETE are 409 key_set_by_server. Else the
|
|
17
|
+
// saved gateway is in force with its own key alone (never AGENT_MODEL_API_KEY from the environment, which was set for no
|
|
18
|
+
// address this server was given), and each of AGENT_MODEL, AGENT_MODEL_FAST, AGENT_MODEL_STRONG and
|
|
19
|
+
// AGENT_MODEL_GATEWAY_IDS that the environment sets wins over the saved field it would fill.
|
|
20
|
+
//
|
|
21
|
+
// How it takes effect: withSavedGateway lays the saved gateway over an environment as the variables would be set, so every
|
|
22
|
+
// reader of AGENT_MODEL_BASE_URL (model.ts, provider.ts, a run's model key, a run's routes) reads it unchanged. The server
|
|
23
|
+
// lays it over its reading at each save (no restart), and the runner over its run environment at each claim
|
|
24
|
+
// (harness-loops.ts), from the store. The key is a non-enumerable property, as every saved key is (secrets.ts withSecrets):
|
|
25
|
+
// readable by name, never in a spread, a spawn or a run's environment, and cut from every text the server shows.
|
|
26
|
+
//
|
|
27
|
+
// The key is sealed together with the origin it was saved for (one sealed value, `{ origin, key }`), and a reading hands
|
|
28
|
+
// it on only with an address of that origin. The address row and the key row are written one after the other, and a
|
|
29
|
+
// runner in another process may read them between the two writes, or after a write that failed: a pair that does not
|
|
30
|
+
// match is read as a gateway with no key, so a key never goes to an address it was not saved for.
|
|
31
|
+
import { gatewayBase, HarnessUnavailable } from "./provider.js";
|
|
32
|
+
import { ApiError, invalid } from "./errors.js";
|
|
33
|
+
import { envValue } from "./key-meta.js";
|
|
34
|
+
import { GATEWAY_KEY_ENV, TIERS } from "./model.js";
|
|
35
|
+
import { registerSecretPage, registerSecretWrite, secretMeta, secretName } from "./secret-writes.js";
|
|
36
|
+
import { getSealedKey, putSealedKey, removeSealedKey } from "./server-keys.js";
|
|
37
|
+
import { isObject, unknownFields } from "./validate.js";
|
|
38
|
+
// Saving, removing and testing the gateway are the owner's session's alone (secret-writes.ts): each may carry its key, and
|
|
39
|
+
// its address says where every model request, and the key, goes. Reading it is not: no answer holds the key.
|
|
40
|
+
for (const operation of ["saveModelGateway", "deleteModelGateway", "testModelGateway"])
|
|
41
|
+
registerSecretWrite(operation, () => true);
|
|
42
|
+
// A refusal of any of them links to where the owner does it: Settings → Model keys, where the gateway's row is.
|
|
43
|
+
registerSecretPage(["saveModelGateway", "deleteModelGateway", "testModelGateway"], "/#/settings/keys");
|
|
44
|
+
/** The routes, by their operation names. */
|
|
45
|
+
export const MODEL_GATEWAY_ROUTES = new Set(["getModelGateway", "saveModelGateway", "deleteModelGateway", "testModelGateway"]);
|
|
46
|
+
/** The settings row: the address, the id style and the models. */
|
|
47
|
+
export const GATEWAY_SETTING = "model_gateway";
|
|
48
|
+
/** The sealed-key row of the gateway's key: never AGENT_MODEL_API_KEY's name, so nothing that reads a key by that name
|
|
49
|
+
* (a run's claim, the environment's gateway) opens it. Only withSavedGateway hands it on, with its own address. */
|
|
50
|
+
export const GATEWAY_KEY_ROW = "model_gateway.api_key";
|
|
51
|
+
/** How the gateway takes the ids a revision or a provider key's default names: whole (`as_named`, `openai/gpt-x`), or
|
|
52
|
+
* `bare`, the part after the provider (AGENT_MODEL_GATEWAY_IDS=bare). The tiers' own models go as saved either way. */
|
|
53
|
+
export const GATEWAY_IDS = ["as_named", "bare"];
|
|
54
|
+
/** The variable each tier's model fills, as the environment names it (model.ts TIER_ENV). */
|
|
55
|
+
const TIER_VARS = { default: "AGENT_MODEL", fast: "AGENT_MODEL_FAST", strong: "AGENT_MODEL_STRONG" };
|
|
56
|
+
const IDS_VAR = "AGENT_MODEL_GATEWAY_IDS";
|
|
57
|
+
const BASE_VAR = "AGENT_MODEL_BASE_URL";
|
|
58
|
+
/** The longest key and model id taken: a header value, a name. */
|
|
59
|
+
const MAX_KEY_CHARS = 4096;
|
|
60
|
+
const MAX_ID_CHARS = 200;
|
|
61
|
+
/** How long the test waits for the gateway, and how many ids it lists. */
|
|
62
|
+
export const TEST_TIMEOUT_MS = 10_000;
|
|
63
|
+
const MAX_LISTED = 500;
|
|
64
|
+
/** The most of the gateway's answer to the test that is read: a list of MAX_LISTED models is far smaller. */
|
|
65
|
+
export const MAX_TEST_BYTES = 4 * 1024 * 1024;
|
|
66
|
+
/** The key's name in every secret's view (secret-writes.ts SecretMeta). */
|
|
67
|
+
export const GATEWAY_SECRET = secretName("model", "gateway");
|
|
68
|
+
/** A model id as saved: text, no whitespace or control character. */
|
|
69
|
+
const idIssue = (value) => typeof value !== "string" || !value.length ? "is a model id as the gateway lists it, as text" : value.length > MAX_ID_CHARS ? `is at most ${MAX_ID_CHARS} characters` : /[\s\x00-\x1f\x7f]/.test(value) ? "holds no space or control character" : undefined;
|
|
70
|
+
/** The row as saved, or undefined when there is none (or it is not one this reads). */
|
|
71
|
+
export async function gatewaySettingOf(settings) {
|
|
72
|
+
const row = (await settings.get(GATEWAY_SETTING));
|
|
73
|
+
if (!isObject(row) || row.v !== 1 || typeof row.base_url !== "string" || !GATEWAY_IDS.includes(row.ids) || !isObject(row.models))
|
|
74
|
+
return undefined;
|
|
75
|
+
const models = Object.fromEntries(TIERS.flatMap((tier) => (typeof row.models[tier] === "string" && !idIssue(row.models[tier]) ? [[tier, row.models[tier]]] : [])));
|
|
76
|
+
return { v: 1, base_url: row.base_url, ids: row.ids, models };
|
|
77
|
+
}
|
|
78
|
+
/** The saved key and its origin, opened under `sealKey`; undefined with none saved, or one that does not open or is not
|
|
79
|
+
* of this shape (never a key without the origin it was saved for). */
|
|
80
|
+
async function openGatewayKey(store, sealKey) {
|
|
81
|
+
const opened = sealKey ? await getSealedKey(store, sealKey, GATEWAY_KEY_ROW) : undefined;
|
|
82
|
+
return opened === undefined ? undefined : boundKeyOf(opened);
|
|
83
|
+
}
|
|
84
|
+
/** The key's row opened, read as its shape; undefined for one that is not (no key). */
|
|
85
|
+
function boundKeyOf(opened) {
|
|
86
|
+
try {
|
|
87
|
+
const parsed = JSON.parse(opened);
|
|
88
|
+
if (isObject(parsed) && parsed.v === 1 && typeof parsed.origin === "string" && typeof parsed.key === "string" && parsed.key.length)
|
|
89
|
+
return { origin: parsed.origin, key: parsed.key };
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
// Not this shape: no key.
|
|
93
|
+
}
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|
|
96
|
+
/** The secrets of the key's row as opened, for the scan of every stored secret (runner/store-secrets.ts): the key itself,
|
|
97
|
+
* which is what a gateway's reply or a tool's output could quote, never the JSON it is sealed in alone. A row not of
|
|
98
|
+
* this shape is one secret whole. */
|
|
99
|
+
export const gatewayKeyRowSecrets = (opened) => {
|
|
100
|
+
const bound = boundKeyOf(opened);
|
|
101
|
+
return bound ? [bound.key] : [opened];
|
|
102
|
+
};
|
|
103
|
+
/** The origin of an address the one rule takes, or undefined for one it refuses. */
|
|
104
|
+
const originIfValid = (base) => (baseProblem("base_url", base) ? undefined : originOf(base));
|
|
105
|
+
/** The saved gateway with its key opened under `sealKey`, or undefined when none is saved. The key is there only when it
|
|
106
|
+
* was saved for the address's origin: none without a seal key, one that does not open, or one saved for another origin
|
|
107
|
+
* (an address changed while this read, or a save that failed between its two writes). */
|
|
108
|
+
export async function readSavedGateway(store, sealKey) {
|
|
109
|
+
const setting = await gatewaySettingOf(store.settings);
|
|
110
|
+
if (!setting)
|
|
111
|
+
return undefined;
|
|
112
|
+
const bound = await openGatewayKey(store, sealKey);
|
|
113
|
+
const key = bound && bound.origin === originIfValid(setting.base_url) ? bound.key : undefined;
|
|
114
|
+
return { ...setting, ...(key ? { key } : {}) };
|
|
115
|
+
}
|
|
116
|
+
/** Whether the environment AgentRun started with names its own gateway: then it is the one in force, whole. */
|
|
117
|
+
export const envGatewayInForce = (env) => envValue(env, BASE_VAR) !== undefined;
|
|
118
|
+
/** Marks an environment withSavedGateway laid a saved gateway over (a symbol: no spawn sees it, a spread keeps it). */
|
|
119
|
+
const SAVED = Symbol.for("agentrun.saved-gateway");
|
|
120
|
+
/** Whether `env`'s gateway is the one saved in the console. */
|
|
121
|
+
export const savedGatewayIn = (env) => env[SAVED] === true;
|
|
122
|
+
/** The plain variable a runner sets on a run's own process (run-child-host.ts) whose gateway is the one saved in the
|
|
123
|
+
* console: that process holds no seal key and reads no store row of the gateway, so its runner lays the gateway over the
|
|
124
|
+
* claim and hands its key over the secrets' pipe, and this says the gateway is the saved one (savedGatewayIn), so its
|
|
125
|
+
* key stays off the run's tools there too. Not a secret. */
|
|
126
|
+
export const SAVED_GATEWAY_ENV = "AGENTRUN_MODEL_GATEWAY_SAVED";
|
|
127
|
+
/** `env` (a run's own process's, its runner having laid the saved gateway over it) marked as the saved gateway's, without
|
|
128
|
+
* SAVED_GATEWAY_ENV; `env` itself where that variable is not set. A copy, every property of `env` as it is. */
|
|
129
|
+
export function savedGatewayMarked(env) {
|
|
130
|
+
if (envValue(env, SAVED_GATEWAY_ENV) !== "1")
|
|
131
|
+
return env;
|
|
132
|
+
const out = {};
|
|
133
|
+
for (const name of Reflect.ownKeys(env))
|
|
134
|
+
if (name !== SAVED_GATEWAY_ENV)
|
|
135
|
+
Object.defineProperty(out, name, Object.getOwnPropertyDescriptor(env, name));
|
|
136
|
+
Object.defineProperty(out, SAVED, { value: true, enumerable: true, configurable: true });
|
|
137
|
+
return out;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* `env` with the saved gateway laid over it, as the variables would set it: AGENT_MODEL_BASE_URL, its key as
|
|
141
|
+
* AGENT_MODEL_API_KEY (non-enumerable; empty where none is saved, so a key the environment holds for no address never goes
|
|
142
|
+
* to this one), and AGENT_MODEL_GATEWAY_IDS and each tier's model where the environment sets none. `env` itself where
|
|
143
|
+
* nothing is saved or the environment names its own gateway (envGatewayInForce). A copy, every property of `env` as it is
|
|
144
|
+
* (non-enumerable secrets and symbols too), never `env` changed.
|
|
145
|
+
*/
|
|
146
|
+
export function withSavedGateway(env, saved) {
|
|
147
|
+
if (!saved || envGatewayInForce(env))
|
|
148
|
+
return env;
|
|
149
|
+
const set = { [BASE_VAR]: saved.base_url };
|
|
150
|
+
if (saved.ids === "bare" && envValue(env, IDS_VAR) === undefined)
|
|
151
|
+
set[IDS_VAR] = "bare";
|
|
152
|
+
for (const tier of TIERS)
|
|
153
|
+
if (saved.models[tier] && envValue(env, TIER_VARS[tier]) === undefined)
|
|
154
|
+
set[TIER_VARS[tier]] = saved.models[tier];
|
|
155
|
+
const out = {};
|
|
156
|
+
for (const name of Reflect.ownKeys(env))
|
|
157
|
+
if (name !== GATEWAY_KEY_ENV && !(typeof name === "string" && Object.hasOwn(set, name)))
|
|
158
|
+
Object.defineProperty(out, name, Object.getOwnPropertyDescriptor(env, name));
|
|
159
|
+
for (const [name, value] of Object.entries(set))
|
|
160
|
+
Object.defineProperty(out, name, { value, enumerable: true, writable: true, configurable: true });
|
|
161
|
+
Object.defineProperty(out, GATEWAY_KEY_ENV, { value: saved.key ?? "", enumerable: false, writable: false, configurable: false });
|
|
162
|
+
Object.defineProperty(out, SAVED, { value: true, enumerable: true, configurable: true });
|
|
163
|
+
return out;
|
|
164
|
+
}
|
|
165
|
+
/** The variables of the gateway's fields that the environment sets. */
|
|
166
|
+
const setAtStart = (env) => [BASE_VAR, IDS_VAR, ...TIERS.map((tier) => TIER_VARS[tier]), GATEWAY_KEY_ENV].filter((name) => envValue(env, name) !== undefined);
|
|
167
|
+
/** Why `value` may not be the gateway's address (gatewayBase's one rule, under the field's name), or undefined. */
|
|
168
|
+
function baseProblem(field, value) {
|
|
169
|
+
try {
|
|
170
|
+
gatewayBase(field, value);
|
|
171
|
+
return undefined;
|
|
172
|
+
}
|
|
173
|
+
catch (error) {
|
|
174
|
+
if (error instanceof HarnessUnavailable)
|
|
175
|
+
return error.message;
|
|
176
|
+
throw error;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
const originOf = (base) => new URL(gatewayBase("base_url", base)).origin;
|
|
180
|
+
export async function gatewayView({ store, env }) {
|
|
181
|
+
const set_at_start = setAtStart(env);
|
|
182
|
+
if (envGatewayInForce(env)) {
|
|
183
|
+
const base = envValue(env, BASE_VAR);
|
|
184
|
+
const key = envValue(env, GATEWAY_KEY_ENV) !== undefined;
|
|
185
|
+
return {
|
|
186
|
+
source: "env",
|
|
187
|
+
base_url: base,
|
|
188
|
+
ids: envValue(env, IDS_VAR) === "bare" ? "bare" : "as_named",
|
|
189
|
+
models: Object.fromEntries(TIERS.map((tier) => [tier, envValue(env, TIER_VARS[tier]) ?? null])),
|
|
190
|
+
set_at_start,
|
|
191
|
+
secret: secretMeta(GATEWAY_SECRET, key ? { set: true, source: "env", changedAt: null } : { set: false }),
|
|
192
|
+
problem: baseProblem(BASE_VAR, base) ?? null,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
const saved = await gatewaySettingOf(store.settings);
|
|
196
|
+
const keyRow = (await store.sealedKeys.all()).find((row) => row.name === GATEWAY_KEY_ROW);
|
|
197
|
+
const secret = keyRow ? secretMeta(GATEWAY_SECRET, { set: true, source: "console", changedAt: new Date(keyRow.updatedAt) }) : secretMeta(GATEWAY_SECRET, { set: false });
|
|
198
|
+
const ids = envValue(env, IDS_VAR) === "bare" ? "bare" : envValue(env, IDS_VAR) !== undefined ? "as_named" : (saved?.ids ?? "as_named");
|
|
199
|
+
return {
|
|
200
|
+
source: saved ? "console" : null,
|
|
201
|
+
base_url: saved?.base_url ?? null,
|
|
202
|
+
ids,
|
|
203
|
+
models: Object.fromEntries(TIERS.map((tier) => [tier, envValue(env, TIER_VARS[tier]) ?? saved?.models[tier] ?? null])),
|
|
204
|
+
set_at_start,
|
|
205
|
+
secret,
|
|
206
|
+
problem: saved ? (baseProblem("base_url", saved.base_url) ?? null) : null,
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
/** 409 while the environment names its own gateway: nothing saved here would take effect. */
|
|
210
|
+
function refuseWhileEnv(env) {
|
|
211
|
+
if (!envGatewayInForce(env))
|
|
212
|
+
return;
|
|
213
|
+
throw new ApiError("key_set_by_server", `This server is started with ${BASE_VAR}: its model gateway is changed where the server is started, not here.`, { details: { fields: [BASE_VAR] } });
|
|
214
|
+
}
|
|
215
|
+
/** A key as the body gives it, checked: text of 1 to MAX_KEY_CHARS characters with no whitespace or control character. */
|
|
216
|
+
function keyIssue(value) {
|
|
217
|
+
if (typeof value !== "string" || !value.length)
|
|
218
|
+
return "api_key is the gateway's key as text, or null to remove it";
|
|
219
|
+
if (value.length > MAX_KEY_CHARS || /[\s\x00-\x1f\x7f]/.test(value))
|
|
220
|
+
return `api_key is at most ${MAX_KEY_CHARS} characters with no space or control character`;
|
|
221
|
+
return undefined;
|
|
222
|
+
}
|
|
223
|
+
/** PUT: the gateway saved, once every field is right; nothing is saved when one is not. No message holds the key. */
|
|
224
|
+
export async function saveGateway(deps, body) {
|
|
225
|
+
const { store, sealKey, env } = deps;
|
|
226
|
+
refuseWhileEnv(env);
|
|
227
|
+
if (!isObject(body))
|
|
228
|
+
throw new ApiError("input_invalid", "the body is a JSON object: { base_url, api_key?, ids?, models: { default, fast?, strong? } }");
|
|
229
|
+
const issues = unknownFields(body, ["base_url", "api_key", "ids", "models"]);
|
|
230
|
+
const base = typeof body.base_url === "string" ? body.base_url.trim() : undefined;
|
|
231
|
+
if (base === undefined || !base)
|
|
232
|
+
issues.push({ field: "base_url", reason: body.base_url === undefined ? "missing" : "wrong_type", message: "base_url is the gateway's address, such as https://gateway.example/v1" });
|
|
233
|
+
else {
|
|
234
|
+
const problem = baseProblem("base_url", base);
|
|
235
|
+
if (problem)
|
|
236
|
+
issues.push({ field: "base_url", reason: "invalid", message: problem });
|
|
237
|
+
}
|
|
238
|
+
if (body.api_key !== undefined && body.api_key !== null) {
|
|
239
|
+
const problem = keyIssue(body.api_key);
|
|
240
|
+
if (problem)
|
|
241
|
+
issues.push({ field: "api_key", reason: "invalid", message: problem });
|
|
242
|
+
}
|
|
243
|
+
if (body.ids !== undefined && !GATEWAY_IDS.includes(body.ids))
|
|
244
|
+
issues.push({ field: "ids", reason: "not_an_option", message: `ids is one of ${GATEWAY_IDS.join(", ")}` });
|
|
245
|
+
const models = body.models === undefined ? {} : body.models;
|
|
246
|
+
if (!isObject(models))
|
|
247
|
+
issues.push({ field: "models", reason: "wrong_type", message: "models is an object: { default, fast?, strong? }" });
|
|
248
|
+
else {
|
|
249
|
+
issues.push(...unknownFields(models, [...TIERS]).map((issue) => ({ ...issue, field: `models.${issue.field}` })));
|
|
250
|
+
for (const tier of TIERS) {
|
|
251
|
+
const value = models[tier];
|
|
252
|
+
if (value === undefined || (value === null && tier !== "default"))
|
|
253
|
+
continue;
|
|
254
|
+
const problem = idIssue(value);
|
|
255
|
+
if (problem)
|
|
256
|
+
issues.push({ field: `models.${tier}`, reason: "invalid", message: `models.${tier} ${problem}` });
|
|
257
|
+
}
|
|
258
|
+
if (models.default === undefined && envValue(env, TIER_VARS.default) === undefined)
|
|
259
|
+
issues.push({ field: "models.default", reason: "missing", message: "models.default is the model a run that names none takes, as the gateway lists it (the test lists them)" });
|
|
260
|
+
}
|
|
261
|
+
if (issues.length)
|
|
262
|
+
throw invalid(issues);
|
|
263
|
+
// A field the environment sets wins: 409, as a tool key the server is started with is, never saved to no effect.
|
|
264
|
+
const held = [...(body.ids !== undefined && envValue(env, IDS_VAR) !== undefined ? [IDS_VAR] : []), ...TIERS.filter((tier) => isObject(models) && models[tier] !== undefined && models[tier] !== null && envValue(env, TIER_VARS[tier]) !== undefined).map((tier) => TIER_VARS[tier])];
|
|
265
|
+
if (held.length)
|
|
266
|
+
throw new ApiError("key_set_by_server", `This server is started with ${held.join(" and ")}: change it where the server is started.`, { details: { fields: held } });
|
|
267
|
+
// The saved key goes only to the origin it was saved for (sealed with it): a new origin takes a new key, or none said
|
|
268
|
+
// outright. A saved key that does not open is never kept either.
|
|
269
|
+
const origin = originOf(base);
|
|
270
|
+
const keySaved = (await store.sealedKeys.get(GATEWAY_KEY_ROW)) !== undefined;
|
|
271
|
+
const bound = keySaved ? await openGatewayKey(store, sealKey) : undefined;
|
|
272
|
+
if (body.api_key === undefined && keySaved && bound?.origin !== origin) {
|
|
273
|
+
throw invalid([{ field: "api_key", reason: "missing", message: `The saved key was for ${bound ? bound.origin : "another gateway"}, and a saved key goes only to the origin it was saved for: enter the key for ${origin}, or send api_key null to use this gateway with no key.` }]);
|
|
274
|
+
}
|
|
275
|
+
const given = models;
|
|
276
|
+
const setting = { v: 1, base_url: base, ids: body.ids ?? "as_named", models: Object.fromEntries(TIERS.flatMap((tier) => (typeof given[tier] === "string" ? [[tier, given[tier]]] : []))) };
|
|
277
|
+
// The key first, sealed with its origin, then the address: a reader between the two (or after the second failed) finds
|
|
278
|
+
// a key whose origin is not the address's, which it reads as no key (readSavedGateway).
|
|
279
|
+
if (body.api_key === null)
|
|
280
|
+
await removeSealedKey(store, GATEWAY_KEY_ROW);
|
|
281
|
+
else if (typeof body.api_key === "string")
|
|
282
|
+
await putSealedKey(store, sealKey, GATEWAY_KEY_ROW, JSON.stringify({ v: 1, origin, key: body.api_key }));
|
|
283
|
+
await store.settings.put(GATEWAY_SETTING, setting);
|
|
284
|
+
await store.flush();
|
|
285
|
+
await deps.changed();
|
|
286
|
+
return gatewayView(deps);
|
|
287
|
+
}
|
|
288
|
+
/** DELETE: the gateway and its key gone; the provider keys are in force again. */
|
|
289
|
+
export async function removeGateway(deps) {
|
|
290
|
+
refuseWhileEnv(deps.env);
|
|
291
|
+
await removeSealedKey(deps.store, GATEWAY_KEY_ROW);
|
|
292
|
+
await deps.store.settings.remove(GATEWAY_SETTING);
|
|
293
|
+
await deps.store.flush();
|
|
294
|
+
await deps.changed();
|
|
295
|
+
return gatewayView(deps);
|
|
296
|
+
}
|
|
297
|
+
/** POST /test: the model ids the gateway lists at GET <base>/v1/models, asked with its key. The body may name an address
|
|
298
|
+
* and a key not saved yet (the form's), else the gateway in force is asked with its own. A saved key goes only to the
|
|
299
|
+
* origin it was saved for; the environment's key only to the environment's gateway. No redirect is followed, and the
|
|
300
|
+
* answer never holds the key. */
|
|
301
|
+
export async function testGateway(deps, body, signal) {
|
|
302
|
+
const { store, sealKey, env } = deps;
|
|
303
|
+
const given = body === undefined || body === null ? {} : body;
|
|
304
|
+
if (!isObject(given))
|
|
305
|
+
throw new ApiError("input_invalid", "the body is a JSON object: { base_url?, api_key? } (api_key null: test with no key)");
|
|
306
|
+
const issues = unknownFields(given, ["base_url", "api_key"]);
|
|
307
|
+
if (given.base_url !== undefined && (typeof given.base_url !== "string" || !given.base_url.trim()))
|
|
308
|
+
issues.push({ field: "base_url", reason: "wrong_type", message: "base_url is the gateway's address as text" });
|
|
309
|
+
else if (typeof given.base_url === "string") {
|
|
310
|
+
const problem = baseProblem("base_url", given.base_url.trim());
|
|
311
|
+
if (problem)
|
|
312
|
+
issues.push({ field: "base_url", reason: "invalid", message: problem });
|
|
313
|
+
}
|
|
314
|
+
// null: test with no key (the form's "Use no key"), never the saved one.
|
|
315
|
+
if (given.api_key !== undefined && given.api_key !== null) {
|
|
316
|
+
const problem = keyIssue(given.api_key);
|
|
317
|
+
if (problem)
|
|
318
|
+
issues.push({ field: "api_key", reason: "invalid", message: problem });
|
|
319
|
+
}
|
|
320
|
+
if (issues.length)
|
|
321
|
+
throw invalid(issues);
|
|
322
|
+
// The gateway in force and its own key: the environment's whole, else the saved one.
|
|
323
|
+
const inForce = envGatewayInForce(env)
|
|
324
|
+
? { base: envValue(env, BASE_VAR), key: envValue(env, GATEWAY_KEY_ENV), field: BASE_VAR }
|
|
325
|
+
: await readSavedGateway(store, sealKey).then((saved) => (saved ? { base: saved.base_url, key: saved.key, field: "base_url" } : undefined));
|
|
326
|
+
const base = typeof given.base_url === "string" ? given.base_url.trim() : inForce?.base;
|
|
327
|
+
if (base === undefined)
|
|
328
|
+
return { ok: false, why: "No model gateway is saved here: enter its address to test it." };
|
|
329
|
+
const field = typeof given.base_url === "string" ? "base_url" : inForce.field;
|
|
330
|
+
const problem = baseProblem(field, base);
|
|
331
|
+
if (problem)
|
|
332
|
+
return { ok: false, why: problem };
|
|
333
|
+
const root = gatewayBase(field, base);
|
|
334
|
+
const sameOrigin = inForce !== undefined && !baseProblem(inForce.field, inForce.base) && new URL(gatewayBase(inForce.field, inForce.base)).origin === new URL(root).origin;
|
|
335
|
+
const key = typeof given.api_key === "string" ? given.api_key : given.api_key === null ? undefined : sameOrigin ? inForce.key : undefined;
|
|
336
|
+
return listModels(root, key, deps.fetch ?? fetch, signal);
|
|
337
|
+
}
|
|
338
|
+
/** The ids GET <root>/v1/models lists (OpenAI's shape: `{ data: [{ id }] }`), or why not, with `key` cut from any words. */
|
|
339
|
+
async function listModels(root, key, fetcher, signal) {
|
|
340
|
+
const url = `${root}/v1/models`;
|
|
341
|
+
const scrub = (text) => (key ? text.split(key).join("[the key]") : text).replace(/\s+/g, " ").slice(0, 300);
|
|
342
|
+
let answer;
|
|
343
|
+
try {
|
|
344
|
+
answer = await fetcher(url, { method: "GET", redirect: "manual", headers: { accept: "application/json", ...(key ? { authorization: `Bearer ${key}` } : {}) }, signal: signal ? AbortSignal.any([signal, AbortSignal.timeout(TEST_TIMEOUT_MS)]) : AbortSignal.timeout(TEST_TIMEOUT_MS) });
|
|
345
|
+
}
|
|
346
|
+
catch (error) {
|
|
347
|
+
const name = error?.name;
|
|
348
|
+
if (name === "TimeoutError")
|
|
349
|
+
return { ok: false, why: `The gateway did not answer GET ${url} within ${TEST_TIMEOUT_MS / 1000} seconds.` };
|
|
350
|
+
if (name === "AbortError")
|
|
351
|
+
return { ok: false, why: "The test was cancelled." };
|
|
352
|
+
const cause = error?.cause;
|
|
353
|
+
return { ok: false, why: `The gateway could not be reached at ${url}: ${scrub(cause?.code ?? cause?.message ?? error?.message ?? String(error))}.` };
|
|
354
|
+
}
|
|
355
|
+
if (answer.status >= 300 && answer.status < 400) {
|
|
356
|
+
await answer.body?.cancel();
|
|
357
|
+
return { ok: false, why: `The gateway answered GET ${url} with a redirect (${answer.status}), and AgentRun follows none: a key goes only to the address you give. Use the address it redirects to.` };
|
|
358
|
+
}
|
|
359
|
+
const read = await textUpTo(answer, MAX_TEST_BYTES);
|
|
360
|
+
if (read === undefined)
|
|
361
|
+
return { ok: false, why: `The gateway's answer to GET ${url} is over ${MAX_TEST_BYTES / (1024 * 1024)} MB, more than a list of models: is this an OpenAI-compatible gateway's address?` };
|
|
362
|
+
const text = read;
|
|
363
|
+
if (answer.status === 401 || answer.status === 403)
|
|
364
|
+
return { ok: false, why: `The gateway refused the key (${answer.status}): check the key, then test again.${detail(text, scrub)}` };
|
|
365
|
+
if (!answer.ok)
|
|
366
|
+
return { ok: false, why: `The gateway answered ${answer.status} to GET ${url}.${detail(text, scrub)}` };
|
|
367
|
+
let parsed;
|
|
368
|
+
try {
|
|
369
|
+
parsed = JSON.parse(text);
|
|
370
|
+
}
|
|
371
|
+
catch {
|
|
372
|
+
return { ok: false, why: `The gateway's answer to GET ${url} is not JSON: is this an OpenAI-compatible gateway's address?` };
|
|
373
|
+
}
|
|
374
|
+
const data = isObject(parsed) && Array.isArray(parsed.data) ? parsed.data : undefined;
|
|
375
|
+
if (!data)
|
|
376
|
+
return { ok: false, why: `The gateway's answer to GET ${url} lists no models (no "data" array): is this an OpenAI-compatible gateway's address?` };
|
|
377
|
+
// An id that holds the key is left out: the answer, and the console's model list, never show it.
|
|
378
|
+
const ids = [...new Set(data.flatMap((entry) => (isObject(entry) && typeof entry.id === "string" && !idIssue(entry.id) && !(key && entry.id.includes(key)) ? [entry.id] : [])))].slice(0, MAX_LISTED);
|
|
379
|
+
return { ok: true, models: ids };
|
|
380
|
+
}
|
|
381
|
+
/** `answer`'s body as text, read up to `max` bytes; undefined once it is longer (the rest is cancelled, never read), and
|
|
382
|
+
* "" for a body that could not be read. */
|
|
383
|
+
async function textUpTo(answer, max) {
|
|
384
|
+
const reader = answer.body?.getReader();
|
|
385
|
+
if (!reader)
|
|
386
|
+
return "";
|
|
387
|
+
const chunks = [];
|
|
388
|
+
let size = 0;
|
|
389
|
+
try {
|
|
390
|
+
for (;;) {
|
|
391
|
+
const { done, value } = await reader.read();
|
|
392
|
+
if (done)
|
|
393
|
+
break;
|
|
394
|
+
size += value.byteLength;
|
|
395
|
+
if (size > max) {
|
|
396
|
+
await reader.cancel().catch(() => { });
|
|
397
|
+
return undefined;
|
|
398
|
+
}
|
|
399
|
+
chunks.push(value);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
catch {
|
|
403
|
+
return "";
|
|
404
|
+
}
|
|
405
|
+
return Buffer.concat(chunks).toString("utf8");
|
|
406
|
+
}
|
|
407
|
+
/** The gateway's own words after a refusal: its JSON's error message, else its text, short. */
|
|
408
|
+
function detail(text, scrub) {
|
|
409
|
+
let said = text;
|
|
410
|
+
try {
|
|
411
|
+
const body = JSON.parse(text);
|
|
412
|
+
const message = typeof body.error === "object" ? body.error?.message : (body.error ?? body.message);
|
|
413
|
+
if (typeof message === "string")
|
|
414
|
+
said = message;
|
|
415
|
+
}
|
|
416
|
+
catch {
|
|
417
|
+
// Not JSON: its text.
|
|
418
|
+
}
|
|
419
|
+
const shown = scrub(said).trim();
|
|
420
|
+
return shown ? ` (The gateway said: ${shown})` : "";
|
|
421
|
+
}
|
|
422
|
+
/** One of the routes at its handler: its answer (sent 200), or an ApiError. */
|
|
423
|
+
export async function modelGatewayRoute(deps, name, request) {
|
|
424
|
+
if (name === "getModelGateway")
|
|
425
|
+
return gatewayView(deps);
|
|
426
|
+
if (name === "saveModelGateway")
|
|
427
|
+
return saveGateway(deps, request.body);
|
|
428
|
+
if (name === "deleteModelGateway")
|
|
429
|
+
return removeGateway(deps);
|
|
430
|
+
if (name === "testModelGateway")
|
|
431
|
+
return testGateway(deps, request.body, request.signal);
|
|
432
|
+
throw new ApiError("not_found", `no model gateway route ${name}`);
|
|
433
|
+
}
|
package/dist/model.d.ts
CHANGED
|
@@ -53,9 +53,11 @@ export declare const MODEL_KEYS: readonly {
|
|
|
53
53
|
harnesses: readonly string[];
|
|
54
54
|
byVendor?: Readonly<Record<string, string>>;
|
|
55
55
|
}[];
|
|
56
|
-
/** The harnesses `row`'s key runs on `env`'s server: its own (`harnesses`), then each harness this server lists
|
|
57
|
-
* owner installs
|
|
58
|
-
* any, on OpenRouter's. One not
|
|
56
|
+
/** The harnesses `row`'s key runs on `env`'s server: its own (`harnesses`), then each harness this server lists beyond the
|
|
57
|
+
* built-in ones a row names (one its owner installs, Goose: harness-table.ts hidden, listed by harnessIds; one an owner's
|
|
58
|
+
* harness module adds) whose wire (its facts, factsOf) runs the row's vendor's models, or any, on OpenRouter's. One not
|
|
59
|
+
* installed here is never named, nor a module's harness that gives no facts, nor one billed to its own plan (Cursor: no
|
|
60
|
+
* model key of ours runs it). */
|
|
59
61
|
export declare function keyHarnesses(row: (typeof MODEL_KEYS)[number], env: NodeJS.ProcessEnv): readonly string[];
|
|
60
62
|
/** What a person does when no model runs here: add a model provider's key in the console, or set one where AgentRun
|
|
61
63
|
* starts (MODEL_KEYS' fields), OpenRouter's named first, as its one key runs every harness. The order a default is
|
package/dist/model.js
CHANGED
|
@@ -8,12 +8,12 @@
|
|
|
8
8
|
import { isRouter, withLocalModels } from "./local-models.js";
|
|
9
9
|
import { pathToFileURL } from "node:url";
|
|
10
10
|
import { DEFAULT_HARNESS, PLAN_HARNESSES } from "./harness.js";
|
|
11
|
-
import { builtinHarness, HARNESS_MODULES_ENV, HARNESS_TABLE_ENV, harnessIds, harnessOf } from "./harness-table.js";
|
|
11
|
+
import { builtinHarness, factsOf, HARNESS_MODULES_ENV, HARNESS_TABLE_ENV, harnessIds, harnessOf } from "./harness-table.js";
|
|
12
12
|
import { gatewayBase, modelGatewayBase } from "./provider.js";
|
|
13
13
|
import { modelFormWhy } from "./model-form.js";
|
|
14
14
|
// Leaves, which the key rows below read as this module loads, in any import order (wires.ts, adapter-facts.ts).
|
|
15
|
-
import { ADAPTER_FACTS
|
|
16
|
-
import { WIRES } from "./wires.js";
|
|
15
|
+
import { ADAPTER_FACTS } from "./adapter-facts.js";
|
|
16
|
+
import { modelWire, WIRES, wireOf } from "./wires.js";
|
|
17
17
|
import { authContextOf } from "./secrets.js";
|
|
18
18
|
import { EXPIRED, PLAN_DEFAULT_MODEL, PLAN_ORIGIN, PLAN_PROVIDER, planOf, signedIn, SIGNED_OUT } from "./chatgpt-plan.js";
|
|
19
19
|
import { LOGIN_DEFAULT_MODEL, onClaudeLogin } from "./claude-login.js";
|
|
@@ -67,22 +67,30 @@ export const MODEL_KEYS = Object.freeze([
|
|
|
67
67
|
]);
|
|
68
68
|
/** pi, and the built-in adapter harnesses whose wire (their facts') runs `vendor`'s models: those that talk to its own API. */
|
|
69
69
|
function onVendor(vendor) {
|
|
70
|
-
return [DEFAULT_HARNESS, ...ADAPTER_FACTS.filter((facts) =>
|
|
70
|
+
return [DEFAULT_HARNESS, ...ADAPTER_FACTS.filter((facts) => wireOf(facts.wire)?.vendor === vendor).map((facts) => facts.id)];
|
|
71
71
|
}
|
|
72
|
-
/** The harnesses `row`'s key runs on `env`'s server: its own (`harnesses`), then each harness this server lists
|
|
73
|
-
* owner installs
|
|
74
|
-
* any, on OpenRouter's. One not
|
|
72
|
+
/** The harnesses `row`'s key runs on `env`'s server: its own (`harnesses`), then each harness this server lists beyond the
|
|
73
|
+
* built-in ones a row names (one its owner installs, Goose: harness-table.ts hidden, listed by harnessIds; one an owner's
|
|
74
|
+
* harness module adds) whose wire (its facts, factsOf) runs the row's vendor's models, or any, on OpenRouter's. One not
|
|
75
|
+
* installed here is never named, nor a module's harness that gives no facts, nor one billed to its own plan (Cursor: no
|
|
76
|
+
* model key of ours runs it). */
|
|
75
77
|
export function keyHarnesses(row, env) {
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
78
|
+
const added = harnessIds(env).filter((id) => {
|
|
79
|
+
if (row.harnesses.includes(id))
|
|
80
|
+
return false;
|
|
81
|
+
const builtin = builtinHarness(id);
|
|
82
|
+
return builtin === undefined || builtin.hidden !== undefined;
|
|
83
|
+
});
|
|
84
|
+
return [...row.harnesses, ...added.filter((id) => {
|
|
85
|
+
const facts = factsOf(env, id);
|
|
86
|
+
return facts !== undefined && modelWire(facts.wire) && (row.id === "openrouter" || WIRES[facts.wire].vendor === row.id);
|
|
80
87
|
})];
|
|
81
88
|
}
|
|
82
|
-
/** `row`'s default for `harness`: the one for its wire's vendor (`byVendor`, OpenRouter's), else the row's. */
|
|
83
|
-
const rowModel = (row, harness) => {
|
|
84
|
-
const facts =
|
|
85
|
-
|
|
89
|
+
/** `row`'s default for `harness` on `env`: the one for its wire's vendor (`byVendor`, OpenRouter's), else the row's. */
|
|
90
|
+
const rowModel = (row, harness, env) => {
|
|
91
|
+
const facts = factsOf(env, harness);
|
|
92
|
+
const vendor = facts && wireOf(facts.wire)?.vendor;
|
|
93
|
+
return (vendor ? row.byVendor?.[vendor] : undefined) ?? row.model;
|
|
86
94
|
};
|
|
87
95
|
/** What a person does when no model runs here: add a model provider's key in the console, or set one where AgentRun
|
|
88
96
|
* starts (MODEL_KEYS' fields), OpenRouter's named first, as its one key runs every harness. The order a default is
|
|
@@ -129,7 +137,7 @@ export function hostModel(env, tier = "default", harness = DEFAULT_HARNESS) {
|
|
|
129
137
|
if (onClaudeLogin(env, harness))
|
|
130
138
|
return LOGIN_DEFAULT_MODEL;
|
|
131
139
|
const row = MODEL_KEYS.find((row) => env[row.field] && (row.harnesses.includes(harness) || keyHarnesses(row, env).includes(harness)));
|
|
132
|
-
return row ? rowModel(row, harness) : env[DEFAULT_MODEL_ENV] || undefined;
|
|
140
|
+
return row ? rowModel(row, harness, env) : env[DEFAULT_MODEL_ENV] || undefined;
|
|
133
141
|
}
|
|
134
142
|
/** The plan's model a run that names none takes: GPT-6.1 Sol where the plan lists it, else the plan's first. */
|
|
135
143
|
const planModel = (env) => {
|
|
@@ -218,7 +226,7 @@ export async function chooseModel(env, ref, tier = "default", harness = DEFAULT_
|
|
|
218
226
|
return { why: error.message, host: true, needs: "key" };
|
|
219
227
|
}
|
|
220
228
|
if (!named)
|
|
221
|
-
return { why: `${none}: name the gateway's model in AGENT_MODEL where AgentRun starts, or
|
|
229
|
+
return { why: `${none}: name the gateway's model in AGENT_MODEL where AgentRun starts, or pick its models in Settings → Model keys → Use a model gateway.`, host: true, needs: "key" };
|
|
222
230
|
// A revision's model, and a key's default (MODEL_KEYS: "provider/model"), go as the gateway takes its ids (bare: the
|
|
223
231
|
// part after the provider), that provider pricing them; the host's own AGENT_MODEL or tier id goes as configured.
|
|
224
232
|
// A name equal to one of the host's configured ids (the picker lists them: model-form.ts pickerCandidates) is that
|
|
@@ -592,7 +600,7 @@ export async function unpricedModels(env, ref, tiers = ["default"], harness = DE
|
|
|
592
600
|
}
|
|
593
601
|
// OpenRouter's routers on any wire: their catalog zeros are no price (routersOf).
|
|
594
602
|
const routers = [...routersOf(byTier.values())];
|
|
595
|
-
if (!env.AGENT_MODEL_BASE_URL || (facts && !
|
|
603
|
+
if (!env.AGENT_MODEL_BASE_URL || (facts && !wireOf(facts.wire)?.gateway))
|
|
596
604
|
return routers;
|
|
597
605
|
const priced = await gatewayPrices(env, byTier, namedBy);
|
|
598
606
|
return [...new Set([...routers, ...[...byTier.values()].map((model) => model.modelId).filter((id) => !priced.has(id))])];
|
package/dist/monid.js
CHANGED
|
@@ -303,7 +303,7 @@ export async function pinMonid(grants, options) {
|
|
|
303
303
|
// A named grant of an endpoint a kept wildcard carried is carried with it.
|
|
304
304
|
const reads = named.filter((grant) => grant.read && !(grant.parsed.kind === "monid" && pin[grant.tool]));
|
|
305
305
|
if (reads.length && !options.key)
|
|
306
|
-
return { refused: reads.map(({ tool }) => ({ tool, message: `${tool} is pinned to what monid lists when the agent is saved, and there is no monid key here to read it with: save the monid key in Settings →
|
|
306
|
+
return { refused: reads.map(({ tool }) => ({ tool, message: `${tool} is pinned to what monid lists when the agent is saved, and there is no monid key here to read it with: save the monid key in Settings → API Keys first.` })) };
|
|
307
307
|
const refused = reads.length ? await readPins(reads, pin, { ...options, key: options.key }) : [];
|
|
308
308
|
// Two endpoints the harnesses would call by one name (wireName folds `.` and `/`): neither could be told apart.
|
|
309
309
|
const wires = new Map();
|
package/dist/native/binding.d.ts
CHANGED
|
@@ -2,6 +2,10 @@ import type { NativeBinding } from './types.js';
|
|
|
2
2
|
export declare const NATIVE_TOOL_BUDGET_UNSUPPORTED = "Native connections cannot enforce the max_tool_calls limit recorded in this older revision. Save the definition without max_tool_calls and start a new run.";
|
|
3
3
|
export declare const NATIVE_MODEL_SETTINGS_UNSUPPORTED = "Native connections use the model and thinking settings of the source project or profile configuration. Remove the AgentRun model and thinking overrides, configure them in the source CLI, and start a new run.";
|
|
4
4
|
export declare const NATIVE_EXECUTION_SETTINGS_UNSUPPORTED = "Native connections use the source CLI configuration for tool permissions, grants, approval policy, model tiers and execution settings. AgentRun cannot apply these overrides or provide a different computer, sandbox, checkout or spending limit. Clear the unsupported settings, configure the source project or profile, and start a new run.";
|
|
5
|
+
export declare const NATIVE_CAP_UNSUPPORTED = "Native runs retain their own limits; AgentRun cannot enforce a dollar cap on this connection. Remove max_cost_usd and use native limits.";
|
|
6
|
+
export declare const NATIVE_RUN_OVERRIDES_UNSUPPORTED = "Native jobs use their original effort and project files. Run effort overrides, uploaded file_ids and Archil disks are unavailable; edit the native configuration or source project instead.";
|
|
7
|
+
/** The fields of a run's start that a native run cannot carry, as a run create's body or a trigger's target names them. */
|
|
8
|
+
export declare function nativeRunOverrides(start: unknown): ('effort' | 'file_ids' | 'archil_disk')[];
|
|
5
9
|
/** Top-level AgentRun overrides are distinct from native.model, which describes source configuration. */
|
|
6
10
|
export declare function nativeModelOverrides(value: unknown): ('model' | 'thinking')[];
|
|
7
11
|
/** Only compatibility defaults are meaningful here; native.model/skills describe the source, not managed overrides.
|
package/dist/native/binding.js
CHANGED
|
@@ -3,6 +3,15 @@ import { isAbsolute } from 'node:path';
|
|
|
3
3
|
export const NATIVE_TOOL_BUDGET_UNSUPPORTED = 'Native connections cannot enforce the max_tool_calls limit recorded in this older revision. Save the definition without max_tool_calls and start a new run.';
|
|
4
4
|
export const NATIVE_MODEL_SETTINGS_UNSUPPORTED = 'Native connections use the model and thinking settings of the source project or profile configuration. Remove the AgentRun model and thinking overrides, configure them in the source CLI, and start a new run.';
|
|
5
5
|
export const NATIVE_EXECUTION_SETTINGS_UNSUPPORTED = 'Native connections use the source CLI configuration for tool permissions, grants, approval policy, model tiers and execution settings. AgentRun cannot apply these overrides or provide a different computer, sandbox, checkout or spending limit. Clear the unsupported settings, configure the source project or profile, and start a new run.';
|
|
6
|
+
export const NATIVE_CAP_UNSUPPORTED = 'Native runs retain their own limits; AgentRun cannot enforce a dollar cap on this connection. Remove max_cost_usd and use native limits.';
|
|
7
|
+
export const NATIVE_RUN_OVERRIDES_UNSUPPORTED = 'Native jobs use their original effort and project files. Run effort overrides, uploaded file_ids and Archil disks are unavailable; edit the native configuration or source project instead.';
|
|
8
|
+
/** The fields of a run's start that a native run cannot carry, as a run create's body or a trigger's target names them. */
|
|
9
|
+
export function nativeRunOverrides(start) {
|
|
10
|
+
if (!start || typeof start !== 'object')
|
|
11
|
+
return [];
|
|
12
|
+
const given = start;
|
|
13
|
+
return ['effort', 'file_ids', 'archil_disk'].filter(field => field === 'file_ids' ? Array.isArray(given.file_ids) && given.file_ids.length > 0 : given[field] !== undefined);
|
|
14
|
+
}
|
|
6
15
|
/** Top-level AgentRun overrides are distinct from native.model, which describes source configuration. */
|
|
7
16
|
export function nativeModelOverrides(value) {
|
|
8
17
|
if (!value || typeof value !== 'object')
|
package/dist/native/cli.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
/** Source-machine handoff: discovery never launches a model; connection uses the existing local server credential.
|
|
2
|
-
|
|
1
|
+
/** Source-machine handoff: discovery never launches a model; connection uses the existing local server credential.
|
|
2
|
+
* `usage`: the command's own line of the server's USAGE (cli.ts), which names every flag parsed here. */
|
|
3
|
+
export declare function nativeMain(argv: string[], usage: string): Promise<number>;
|
package/dist/native/cli.js
CHANGED
|
@@ -5,13 +5,14 @@ import { defaultData } from '../server-fs.js';
|
|
|
5
5
|
import { discoverNative } from './discovery.js';
|
|
6
6
|
import { nativeEnvironment } from './environment.js';
|
|
7
7
|
import { secret } from '../secrets.js';
|
|
8
|
-
/** Source-machine handoff: discovery never launches a model; connection uses the existing local server credential.
|
|
9
|
-
|
|
8
|
+
/** Source-machine handoff: discovery never launches a model; connection uses the existing local server credential.
|
|
9
|
+
* `usage`: the command's own line of the server's USAGE (cli.ts), which names every flag parsed here. */
|
|
10
|
+
export async function nativeMain(argv, usage) {
|
|
10
11
|
try {
|
|
11
|
-
const { values, positionals } = parseArgs({ args: argv, allowPositionals: true, options: { project: { type: 'string' }, runtime: { type: 'string' }, agent: { type: 'string' }, profile: { type: 'string' }, url: { type: 'string' },
|
|
12
|
+
const { values, positionals } = parseArgs({ args: argv, allowPositionals: true, options: { project: { type: 'string' }, runtime: { type: 'string' }, agent: { type: 'string' }, profile: { type: 'string' }, url: { type: 'string' }, data: { type: 'string' } } });
|
|
12
13
|
const operation = positionals[0];
|
|
13
14
|
if (!values.project || !['discover', 'connect'].includes(operation ?? ''))
|
|
14
|
-
throw new Error(
|
|
15
|
+
throw new Error(usage);
|
|
15
16
|
if (values.runtime !== undefined && !['claude-code', 'codex'].includes(values.runtime))
|
|
16
17
|
throw new Error('runtime must be claude-code or codex');
|
|
17
18
|
if (operation === 'discover') {
|
|
@@ -30,7 +31,7 @@ export async function nativeMain(argv) {
|
|
|
30
31
|
recorded = record.base_url;
|
|
31
32
|
}
|
|
32
33
|
catch { /* explicit/default URL below */ }
|
|
33
|
-
const url = new URL(values.url ??
|
|
34
|
+
const url = new URL(values.url ?? process.env.AGENTRUN_BASE_URL ?? process.env.AGENTRUN_URL ?? recorded ?? 'http://127.0.0.1:7340');
|
|
34
35
|
if (!['localhost', '127.0.0.1', '[::1]'].includes(url.hostname))
|
|
35
36
|
throw new Error('Run AgentRun on this source machine and connect to its loopback URL; hosted native pairing is not available yet.');
|
|
36
37
|
const token = secret('AGENTRUN_API_KEY') || secret('AGENTRUN_TOKEN') || (await readFile(join(data, '.token'), 'utf8')).trim();
|
package/dist/openapi.d.ts
CHANGED
|
@@ -17,6 +17,8 @@ export declare const MCP_PREVIEW_SCOPE = "The tools are what the server offers,
|
|
|
17
17
|
export declare const RUN_FIELDS_SENT: string[];
|
|
18
18
|
/** The capabilities every answer carries beside the contract's required ones (server.ts capabilitiesWith). */
|
|
19
19
|
export declare const CAPABILITIES_SENT: string[];
|
|
20
|
+
/** This server's own agent fields every agent answer carries (agents-store.ts agentView): required, beside the contract's. */
|
|
21
|
+
export declare const AGENT_FIELDS_SENT: string[];
|
|
20
22
|
/** The field every saved sign-in is sent with beside the contract's, required: `secret`, its one secret's metadata
|
|
21
23
|
* (secrets/1, sign-ins-secrets.ts). */
|
|
22
24
|
export declare const SIGN_IN_FIELDS_SENT: string[];
|