@elliotsystems/eai 0.1.3 → 0.1.5
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 +93 -54
- package/bin/eai +154 -0
- package/lib/connect.js +205 -0
- package/lib/gdrive.js +238 -0
- package/lib/logout.js +73 -0
- package/lib/resolve.js +9 -15
- package/lib/setup.js +600 -0
- package/lib/stage.js +45 -4
- package/lib/status.js +133 -0
- package/lib/token-env.js +53 -17
- package/lib/update-check.js +27 -0
- package/lib/update.js +303 -0
- package/package.json +2 -4
- package/plugin/opencode-plugin/src/classifier.ts +2 -1
- package/plugin/opencode-plugin/src/elliot-auth.ts +56 -104
- package/plugin/opencode-plugin/src/identity.ts +69 -0
- package/plugin/opencode-plugin/src/index.ts +13 -28
- package/plugin/opencode-plugin/src/mcp-keys.ts +128 -1
- package/plugin/opencode-plugin/src/policy.ts +36 -4
- package/plugin/opencode-plugin/src/proxy-provider.ts +124 -72
- package/plugin/opencode-plugin/src/scope.ts +2 -1
- package/plugin/opencode-plugin/src/tui/knowledge.ts +9 -3
|
@@ -1,27 +1,70 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Reads `~/.elliot/config.json` — written by `eai setup`/`elliot-ai setup`.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* file exists inside `opencode-plugin` rather than importing the CLI package
|
|
9
|
-
* because the plugin ships and is loaded independently of `elliot-cli` — see
|
|
10
|
-
* `docs/superpowers/plans/2026-09-03-eai-proxy-auth.md`.
|
|
4
|
+
* Used two ways: `readElliotConfig() !== null` is "is the user logged in at
|
|
5
|
+
* all" (the proxy-provider config hook is a no-op otherwise); and its
|
|
6
|
+
* `litellm_base_url`/`litellm_master_key` fields are the LLM provider's
|
|
7
|
+
* connection info directly.
|
|
11
8
|
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
9
|
+
* Formerly also home to `freshToken`/`isExpired` — a self-refreshing JWT
|
|
10
|
+
* mechanism for the per-instance credential `/llm-proxy` used to validate.
|
|
11
|
+
* Removed along with `/llm-proxy`: the LiteLLM gateway this now points at
|
|
12
|
+
* authenticates with ONE static, shared `litellm_master_key` (LiteLLM is
|
|
13
|
+
* deliberately DB-less — no per-tenant/per-instance credential is possible
|
|
14
|
+
* without one), which never expires and so never needs refreshing.
|
|
15
15
|
*/
|
|
16
16
|
import * as fs from "node:fs"
|
|
17
17
|
import * as os from "node:os"
|
|
18
18
|
import * as path from "node:path"
|
|
19
19
|
|
|
20
|
+
/**
|
|
21
|
+
* One LiteLLM credential this person can use from OpenCode — their own
|
|
22
|
+
* personal key, or a team's shared key (Phase 4, docs/LITELLM_GATEWAY.md
|
|
23
|
+
* §14 / PRD PO-4/4.3/4.4). `provider_id` is the OpenCode provider id
|
|
24
|
+
* `configHook` registers it under directly (e.g. `elliot` for the personal
|
|
25
|
+
* default, `elliot-team-<slug>` per team) — computed backend-side
|
|
26
|
+
* (`onboarding.py`), not derived here, so naming stays in one place.
|
|
27
|
+
*/
|
|
28
|
+
export interface ElliotCredential {
|
|
29
|
+
provider_id: string
|
|
30
|
+
/** Shown in OpenCode's own model picker, e.g. "Elliot (Personal)" or
|
|
31
|
+
* "Elliot — Team Platform". */
|
|
32
|
+
label: string
|
|
33
|
+
api_key: string
|
|
34
|
+
/** Present for a team credential, absent for the personal one. */
|
|
35
|
+
team_id?: string
|
|
36
|
+
/**
|
|
37
|
+
* This credential's own team's Phase 4 model restriction
|
|
38
|
+
* (docs/LITELLM_GATEWAY.md §14), or absent/null for unrestricted — always
|
|
39
|
+
* absent on the personal credential, which has no team and so no
|
|
40
|
+
* restriction concept. LiteLLM gateway audit finding #5: each provider
|
|
41
|
+
* `configHook` registers must be filtered by ITS OWN credential's
|
|
42
|
+
* restriction, not a shared list — a user on both a restricted and an
|
|
43
|
+
* unrestricted team must not have one leak into the other.
|
|
44
|
+
*/
|
|
45
|
+
allowed_models?: string[] | null
|
|
46
|
+
}
|
|
47
|
+
|
|
20
48
|
export interface ElliotConfig {
|
|
21
49
|
jwt_token?: string
|
|
22
|
-
refresh_token?: string
|
|
23
50
|
backend_url?: string
|
|
24
|
-
|
|
51
|
+
litellm_base_url?: string
|
|
52
|
+
litellm_master_key?: string
|
|
53
|
+
/**
|
|
54
|
+
* Every credential this person can use, personal key first (Phase 4).
|
|
55
|
+
* Absent on a config written by an older `eai setup` (or a backend that
|
|
56
|
+
* predates this field) — `configHook` falls back to the single
|
|
57
|
+
* `litellm_master_key` provider in that case, so an old config keeps
|
|
58
|
+
* working unchanged rather than losing LLM access outright.
|
|
59
|
+
*/
|
|
60
|
+
credentials?: ElliotCredential[]
|
|
61
|
+
/**
|
|
62
|
+
* The single scoped team's own Phase 4 model restriction, when
|
|
63
|
+
* `credentials` is absent and the legacy `litellm_master_key` fallback
|
|
64
|
+
* provider is registered instead — same field, same meaning as
|
|
65
|
+
* `ElliotCredential.allowed_models`, just for the one-provider path.
|
|
66
|
+
*/
|
|
67
|
+
allowed_models?: string[] | null
|
|
25
68
|
[k: string]: unknown
|
|
26
69
|
}
|
|
27
70
|
|
|
@@ -40,94 +83,3 @@ export function readElliotConfig(): ElliotConfig | null {
|
|
|
40
83
|
return null
|
|
41
84
|
}
|
|
42
85
|
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Persist the config, owner-only (0600) — this file holds a live bearer
|
|
46
|
-
* token. Creates `~/.elliot` (0700) if it doesn't exist yet.
|
|
47
|
-
*/
|
|
48
|
-
export function writeElliotConfig(cfg: ElliotConfig): void {
|
|
49
|
-
const configPath = elliotConfigPath()
|
|
50
|
-
const configDir = path.dirname(configPath)
|
|
51
|
-
if (!fs.existsSync(configDir)) {
|
|
52
|
-
fs.mkdirSync(configDir, { recursive: true, mode: 0o700 })
|
|
53
|
-
}
|
|
54
|
-
fs.writeFileSync(configPath, JSON.stringify(cfg, null, 2), { mode: 0o600 })
|
|
55
|
-
fs.chmodSync(configPath, 0o600)
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/** `exp` (seconds since epoch) from a JWT's middle segment, or null. */
|
|
59
|
-
function decodeJwtExp(token: string): number | null {
|
|
60
|
-
try {
|
|
61
|
-
const parts = token.split(".")
|
|
62
|
-
if (parts.length < 2) return null
|
|
63
|
-
const payload = JSON.parse(Buffer.from(parts[1], "base64").toString("utf-8"))
|
|
64
|
-
return typeof payload?.exp === "number" ? payload.exp : null
|
|
65
|
-
} catch {
|
|
66
|
-
return null
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const REFRESH_SKEW_MS = 60_000
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* True if the access token is expired or within `REFRESH_SKEW_MS` of
|
|
74
|
-
* expiring. Prefers the stored `access_token_expires_at`; falls back to
|
|
75
|
-
* decoding `jwt_token`'s `exp` claim; true (treat as expired -> refresh) when
|
|
76
|
-
* neither is decodable.
|
|
77
|
-
*/
|
|
78
|
-
export function isExpired(cfg: ElliotConfig, nowMs = Date.now()): boolean {
|
|
79
|
-
if (cfg.access_token_expires_at) {
|
|
80
|
-
const expiresAtMs = new Date(cfg.access_token_expires_at).getTime()
|
|
81
|
-
if (Number.isFinite(expiresAtMs)) {
|
|
82
|
-
return expiresAtMs - REFRESH_SKEW_MS <= nowMs
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
const exp = cfg.jwt_token ? decodeJwtExp(cfg.jwt_token) : null
|
|
86
|
-
if (exp === null) return true
|
|
87
|
-
return exp * 1000 - REFRESH_SKEW_MS <= nowMs
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
interface RefreshResponse {
|
|
91
|
-
access_token: string
|
|
92
|
-
expires_in_seconds: number
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
interface FreshTokenDeps {
|
|
96
|
-
fetchImpl?: typeof fetch
|
|
97
|
-
write?: (cfg: ElliotConfig) => void
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* A valid access token, refreshing + persisting when expired and a
|
|
102
|
-
* `refresh_token`/`backend_url` are present. NEVER throws: any failure
|
|
103
|
-
* (network, non-2xx, malformed response) falls back to the existing
|
|
104
|
-
* `jwt_token` — the proxy 401s on a stale token rather than this crashing the
|
|
105
|
-
* model call.
|
|
106
|
-
*/
|
|
107
|
-
export async function freshToken(
|
|
108
|
-
cfg: ElliotConfig,
|
|
109
|
-
deps?: FreshTokenDeps,
|
|
110
|
-
): Promise<string | null> {
|
|
111
|
-
try {
|
|
112
|
-
if (cfg.jwt_token && !isExpired(cfg)) return cfg.jwt_token
|
|
113
|
-
if (!cfg.refresh_token || !cfg.backend_url) return cfg.jwt_token ?? null
|
|
114
|
-
|
|
115
|
-
const fetchImpl = deps?.fetchImpl ?? fetch
|
|
116
|
-
const write = deps?.write ?? writeElliotConfig
|
|
117
|
-
|
|
118
|
-
const response = await fetchImpl(`${cfg.backend_url}/auth/refresh`, {
|
|
119
|
-
method: "POST",
|
|
120
|
-
headers: { "Content-Type": "application/json" },
|
|
121
|
-
body: JSON.stringify({ refresh_token: cfg.refresh_token }),
|
|
122
|
-
})
|
|
123
|
-
if (!response.ok) return cfg.jwt_token ?? null
|
|
124
|
-
|
|
125
|
-
const data = (await response.json()) as RefreshResponse
|
|
126
|
-
cfg.jwt_token = data.access_token
|
|
127
|
-
cfg.access_token_expires_at = new Date(Date.now() + data.expires_in_seconds * 1000).toISOString()
|
|
128
|
-
write(cfg)
|
|
129
|
-
return data.access_token
|
|
130
|
-
} catch {
|
|
131
|
-
return cfg.jwt_token ?? null
|
|
132
|
-
}
|
|
133
|
-
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Make the agent call itself Elliot-AI.
|
|
3
|
+
*
|
|
4
|
+
* The visual layer was branded long before this: wordmark, palette, terminal
|
|
5
|
+
* title. But the agent's own sense of identity lives in the system prompt, and
|
|
6
|
+
* on a stock runtime that prompt opens with "You are OpenCode, ...". So asking
|
|
7
|
+
* the CLI "who are you?" got "I'm opencode" — every surface said Elliot except
|
|
8
|
+
* the one the user actually talks to.
|
|
9
|
+
*
|
|
10
|
+
* Fixed here rather than in the prompt files, because those are compiled into
|
|
11
|
+
* the stock upstream binary and patching them would put Elliot back inside
|
|
12
|
+
* OpenCode. `experimental.chat.system.transform` is upstream's own hook for
|
|
13
|
+
* exactly this (`packages/plugin/src/index.ts`), fired from both
|
|
14
|
+
* `agent/agent.ts` and `session/llm/request.ts`, so the fix stays in the
|
|
15
|
+
* overlay and survives an upstream upgrade.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** The product name the assistant should use for itself. */
|
|
19
|
+
export const PRODUCT_NAME = "Elliot-AI"
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Matches ONLY the identity clause that opens a system prompt.
|
|
23
|
+
*
|
|
24
|
+
* Anchored to the start and requiring the following comma, because the prompts
|
|
25
|
+
* also contain `https://opencode.ai/docs` and
|
|
26
|
+
* `https://github.com/anomalyco/opencode`. Those are real upstream links: a
|
|
27
|
+
* blanket rename would silently turn working URLs into 404s, and would also
|
|
28
|
+
* corrupt references to `opencode.json` and `.opencode/` that the tool guidance
|
|
29
|
+
* depends on. Eight identity variants exist across the prompt files (`You are
|
|
30
|
+
* OpenCode,` / `You are opencode,`), and all of them share this shape.
|
|
31
|
+
*/
|
|
32
|
+
const IDENTITY = /^You are opencode,/i
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Prepended so the model can answer "what is your name" and "who made you"
|
|
36
|
+
* without inferring it from a rewritten sentence halfway down a long prompt.
|
|
37
|
+
*
|
|
38
|
+
* It states only what is true. The underlying model is deliberately not named:
|
|
39
|
+
* that is a provider detail that changes, and guessing it is how an assistant
|
|
40
|
+
* ends up confidently telling a customer something wrong.
|
|
41
|
+
*/
|
|
42
|
+
const IDENTITY_PREAMBLE = [
|
|
43
|
+
`You are ${PRODUCT_NAME}, the coding agent made by Elliot Systems.`,
|
|
44
|
+
`When asked who or what you are, say you are ${PRODUCT_NAME} by Elliot Systems.`,
|
|
45
|
+
`Never refer to yourself as OpenCode.`,
|
|
46
|
+
`OpenCode is the open-source harness you run on, not your identity. If a user`,
|
|
47
|
+
`asks directly what you are built on, you may say you are built on OpenCode.`,
|
|
48
|
+
].join(" ")
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Rewrite the system prompt in place.
|
|
52
|
+
*
|
|
53
|
+
* Idempotent: re-running leaves the result unchanged, which matters because the
|
|
54
|
+
* hook fires per request and on a long session would otherwise stack the
|
|
55
|
+
* preamble dozens of times and eat the context window.
|
|
56
|
+
*/
|
|
57
|
+
export function applyElliotIdentity(system: string[]): string[] {
|
|
58
|
+
if (system.some((entry) => entry.startsWith(IDENTITY_PREAMBLE))) return system
|
|
59
|
+
|
|
60
|
+
for (let i = 0; i < system.length; i++) {
|
|
61
|
+
const entry = system[i]
|
|
62
|
+
if (typeof entry === "string" && IDENTITY.test(entry)) {
|
|
63
|
+
system[i] = entry.replace(IDENTITY, `You are ${PRODUCT_NAME},`)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
system.unshift(IDENTITY_PREAMBLE)
|
|
68
|
+
return system
|
|
69
|
+
}
|
|
@@ -20,9 +20,9 @@
|
|
|
20
20
|
* `.opencode/plugin/` file re-export.
|
|
21
21
|
*/
|
|
22
22
|
import { isElliotKnowledgeTool, parseOptions, shellVerdict } from "./policy.js"
|
|
23
|
-
import {
|
|
24
|
-
import { readElliotConfig } from "./elliot-auth.js"
|
|
23
|
+
import { configHook } from "./proxy-provider.js"
|
|
25
24
|
import { formatScope, scopeForWorktree } from "./scope.js"
|
|
25
|
+
import { applyElliotIdentity } from "./identity.js"
|
|
26
26
|
|
|
27
27
|
// Structural mirrors of the host types (packages/plugin/src/index.ts @
|
|
28
28
|
// v1.18.19). Declared locally so this package type-checks and unit-tests
|
|
@@ -66,6 +66,10 @@ export interface Hooks {
|
|
|
66
66
|
input: PermissionInput,
|
|
67
67
|
output: { status: "ask" | "deny" | "allow" },
|
|
68
68
|
) => Promise<void>
|
|
69
|
+
"experimental.chat.system.transform"?: (
|
|
70
|
+
input: { sessionID?: string; model: unknown },
|
|
71
|
+
output: { system: string[] },
|
|
72
|
+
) => Promise<void>
|
|
69
73
|
config?: (input: any) => Promise<void>
|
|
70
74
|
auth?: AuthHook
|
|
71
75
|
dispose?: () => Promise<void>
|
|
@@ -82,25 +86,6 @@ export const ElliotPlugin: Plugin = async (input, rawOptions) => {
|
|
|
82
86
|
// the one source the model cannot influence.
|
|
83
87
|
const worktree = input.worktree || input.directory
|
|
84
88
|
|
|
85
|
-
// Seed a placeholder auth entry for the `elliot` provider so the host's
|
|
86
|
-
// auth loader fires at all (`provider.ts:1584` skips the loader entirely
|
|
87
|
-
// when nothing is stored for its provider id — see Task-1 findings). The
|
|
88
|
-
// value itself is never the credential that reaches the wire; the auth
|
|
89
|
-
// loader's `fetch` attaches the live bearer token per request. Guarded so
|
|
90
|
-
// a client-RPC failure never breaks plugin init, and skipped outright when
|
|
91
|
-
// the user isn't logged into Elliot.
|
|
92
|
-
if (readElliotConfig()) {
|
|
93
|
-
try {
|
|
94
|
-
await input.client?.auth?.set({
|
|
95
|
-
path: { id: ELLIOT_PROVIDER_ID },
|
|
96
|
-
body: { type: "api", key: "elliot-managed" },
|
|
97
|
-
})
|
|
98
|
-
} catch {
|
|
99
|
-
// Never break plugin init on a seeding failure — the auth loader
|
|
100
|
-
// simply won't fire this session, same as the no-login path.
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
89
|
return {
|
|
105
90
|
/**
|
|
106
91
|
* One handler, two jobs — a JS object literal may carry only one
|
|
@@ -165,6 +150,13 @@ export const ElliotPlugin: Plugin = async (input, rawOptions) => {
|
|
|
165
150
|
* thrown tool error — so this is the forward-compatible home. It can only ever
|
|
166
151
|
* tighten.
|
|
167
152
|
*/
|
|
153
|
+
// The agent introduces itself as OpenCode on a stock runtime, because that
|
|
154
|
+
// is what the compiled system prompt says. Rewritten here so the fix lives
|
|
155
|
+
// in the overlay instead of in the harness. See ./identity.ts.
|
|
156
|
+
"experimental.chat.system.transform": async (_hookInput, output) => {
|
|
157
|
+
applyElliotIdentity(output.system)
|
|
158
|
+
},
|
|
159
|
+
|
|
168
160
|
"permission.ask": async (permInput, output) => {
|
|
169
161
|
if (permInput.type !== "bash") return
|
|
170
162
|
const command = permInput.metadata?.["command"]
|
|
@@ -183,13 +175,6 @@ export const ElliotPlugin: Plugin = async (input, rawOptions) => {
|
|
|
183
175
|
config: async (c) => {
|
|
184
176
|
configHook(c)
|
|
185
177
|
},
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* Self-refreshing auth loader for the `elliot` provider — see
|
|
189
|
-
* `proxy-provider.ts`. Only fires once the seeding above has stored an
|
|
190
|
-
* auth entry for `ELLIOT_PROVIDER_ID`.
|
|
191
|
-
*/
|
|
192
|
-
auth: makeAuthHook(),
|
|
193
178
|
}
|
|
194
179
|
}
|
|
195
180
|
|
|
@@ -24,7 +24,134 @@ export function sanitizeKey(value: string): string {
|
|
|
24
24
|
/** The backend tool behind every Elliot knowledge lookup. */
|
|
25
25
|
export const KNOWLEDGE_TOOL = "knowledge_search"
|
|
26
26
|
|
|
27
|
-
/** The agent-visible key for `KNOWLEDGE_TOOL` on a given MCP server.
|
|
27
|
+
/** The agent-visible key for `KNOWLEDGE_TOOL` on a given MCP server.
|
|
28
|
+
*
|
|
29
|
+
* Still the PRE-GATEWAY spelling, and still correct for a runtime wired
|
|
30
|
+
* straight to Elliot. It is no longer the only spelling that arrives — see
|
|
31
|
+
* `toCanonicalTool`.
|
|
32
|
+
*/
|
|
28
33
|
export function knowledgeToolKey(serverName: string): string {
|
|
29
34
|
return `${sanitizeKey(serverName)}_${KNOWLEDGE_TOOL}`
|
|
30
35
|
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* LiteLLM joins its server alias to the tool name with a HYPHEN; OpenCode
|
|
39
|
+
* joins its own server key with an UNDERSCORE. Both appear in one wire name
|
|
40
|
+
* and they are not interchangeable.
|
|
41
|
+
*
|
|
42
|
+
* The SEPARATOR is LiteLLM's composition rule and is fixed. The ALIAS is
|
|
43
|
+
* configuration — it is the `mcp_servers` key in `deploy/litellm/config.yaml`
|
|
44
|
+
* — and this module no longer owns a copy of it. It used to:
|
|
45
|
+
*
|
|
46
|
+
* export const GATEWAY_SERVER_ALIAS = "elliot"
|
|
47
|
+
*
|
|
48
|
+
* restated from the backend and "kept in step by a test". That is four copies
|
|
49
|
+
* of one literal across two languages, and the failure when they diverge is
|
|
50
|
+
* silent: an unrecognised tool name is one whose repo scope is never injected,
|
|
51
|
+
* so the agent answers tenant-wide and says nothing about it.
|
|
52
|
+
*
|
|
53
|
+
* The alias now arrives as a plugin option, served by the backend from
|
|
54
|
+
* `litellm_mcp_server_alias` (`routers/well_known.py`).
|
|
55
|
+
*/
|
|
56
|
+
const GATEWAY_SEPARATOR = "-"
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The canonical Elliot tool name behind an agent-visible key.
|
|
60
|
+
*
|
|
61
|
+
* Runtime traffic now reaches Elliot through LiteLLM's MCP gateway, and each
|
|
62
|
+
* hop adds its own namespace. All four spellings below are real, depending on
|
|
63
|
+
* whether the runtime is behind the gateway and how the host composed the key:
|
|
64
|
+
*
|
|
65
|
+
* elliot_elliot-knowledge_search OpenCode prefix + gateway prefix
|
|
66
|
+
* elliot-knowledge_search gateway prefix only
|
|
67
|
+
* elliot_knowledge_search OpenCode prefix only (pre-gateway)
|
|
68
|
+
* knowledge_search bare/canonical
|
|
69
|
+
*
|
|
70
|
+
* Stripping is DETERMINISTIC and bounded: at most one server prefix, then at
|
|
71
|
+
* most one gateway prefix, each only from the front. Never a suffix or
|
|
72
|
+
* substring match — that is what stops a server called `not-elliot` from
|
|
73
|
+
* collecting scope hints meant for ours, and it is why the configured server
|
|
74
|
+
* names are required rather than inferred.
|
|
75
|
+
*
|
|
76
|
+
* Idempotent: feeding a canonical name back in returns it unchanged.
|
|
77
|
+
*/
|
|
78
|
+
/**
|
|
79
|
+
* The canonical tool name when the gateway alias is NOT knowable.
|
|
80
|
+
*
|
|
81
|
+
* For the TUI only. The TUI plugin entry (`tui: TuiPlugin = async (api)`)
|
|
82
|
+
* receives no options — the host hands it a UI api and nothing else — so it
|
|
83
|
+
* cannot be told the configured alias, and inventing a config channel for a
|
|
84
|
+
* sidebar would be a worse answer than this one.
|
|
85
|
+
*
|
|
86
|
+
* So it matches on STRUCTURE rather than on a specific alias: strip one
|
|
87
|
+
* configured server prefix, then allow ONE unknown `<something>-` layer.
|
|
88
|
+
*
|
|
89
|
+
* THIS IS NOT A FALLBACK TO A GUESSED ALIAS, and it does not weaken the
|
|
90
|
+
* anti-substring property that `toCanonicalTool` exists to hold. That property
|
|
91
|
+
* comes from the SERVER prefix, which the TUI does know (`api.state.mcp()`
|
|
92
|
+
* supplies it) and which is still required here. By the time an unknown
|
|
93
|
+
* gateway layer is considered, the key has already been proven to belong to a
|
|
94
|
+
* configured Elliot server — `not-elliot_knowledge_search` never reaches that
|
|
95
|
+
* line.
|
|
96
|
+
*
|
|
97
|
+
* Confined to display. Nothing authorizes on this: the sidebar decides which
|
|
98
|
+
* transcript rows to render, and the cost of a wrong answer is a missing or
|
|
99
|
+
* extra row, not a wrong scope. Authorization uses `toCanonicalTool` with the
|
|
100
|
+
* configured alias, on the backend, where the alias is known.
|
|
101
|
+
*/
|
|
102
|
+
export function toCanonicalToolUnderAnyGateway(
|
|
103
|
+
toolKey: string,
|
|
104
|
+
serverNames: readonly string[],
|
|
105
|
+
): string {
|
|
106
|
+
if (!toolKey) return toolKey
|
|
107
|
+
let rest = toolKey
|
|
108
|
+
for (const server of serverNames) {
|
|
109
|
+
const prefix = `${sanitizeKey(server)}_`
|
|
110
|
+
if (rest.startsWith(prefix)) {
|
|
111
|
+
rest = rest.slice(prefix.length)
|
|
112
|
+
break
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
// One unknown gateway layer, and only one: `<alias>-<tool>`. The alias may
|
|
116
|
+
// not itself contain the separator, which is what keeps this bounded.
|
|
117
|
+
const layer = rest.indexOf(GATEWAY_SEPARATOR)
|
|
118
|
+
if (layer > 0) {
|
|
119
|
+
const remainder = rest.slice(layer + GATEWAY_SEPARATOR.length)
|
|
120
|
+
if (remainder) return remainder
|
|
121
|
+
}
|
|
122
|
+
return rest
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function toCanonicalTool(
|
|
126
|
+
toolKey: string,
|
|
127
|
+
serverNames: readonly string[],
|
|
128
|
+
gatewayAlias: string,
|
|
129
|
+
): string {
|
|
130
|
+
if (!toolKey) return toolKey
|
|
131
|
+
if (!gatewayAlias) {
|
|
132
|
+
// No silent `|| "elliot"`. A missing alias means the caller is
|
|
133
|
+
// misconfigured, and guessing one would restore the duplicated literal
|
|
134
|
+
// this parameter exists to remove — while hiding the misconfiguration
|
|
135
|
+
// behind behaviour that happens to be right in one deployment.
|
|
136
|
+
throw new Error("toCanonicalTool requires the configured gateway alias")
|
|
137
|
+
}
|
|
138
|
+
const gatewayPrefix = `${gatewayAlias}${GATEWAY_SEPARATOR}`
|
|
139
|
+
let rest = toolKey
|
|
140
|
+
|
|
141
|
+
// One OpenCode `<server>_` prefix, and only for a server we are configured
|
|
142
|
+
// for. An unconfigured server's tools are not ours to recognise.
|
|
143
|
+
for (const server of serverNames) {
|
|
144
|
+
const prefix = `${sanitizeKey(server)}_`
|
|
145
|
+
if (rest.startsWith(prefix)) {
|
|
146
|
+
rest = rest.slice(prefix.length)
|
|
147
|
+
break
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Then one LiteLLM `<alias>-` prefix.
|
|
152
|
+
if (rest.startsWith(gatewayPrefix)) {
|
|
153
|
+
rest = rest.slice(gatewayPrefix.length)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return rest
|
|
157
|
+
}
|
|
@@ -19,7 +19,7 @@ import {
|
|
|
19
19
|
killsAgentRuntime,
|
|
20
20
|
looksLikeCredentialExfiltration,
|
|
21
21
|
} from "./classifier.js"
|
|
22
|
-
import { KNOWLEDGE_TOOL,
|
|
22
|
+
import { KNOWLEDGE_TOOL, sanitizeKey, toCanonicalTool } from "./mcp-keys.js"
|
|
23
23
|
|
|
24
24
|
const OptionsSchema = z
|
|
25
25
|
.object({
|
|
@@ -30,6 +30,27 @@ const OptionsSchema = z
|
|
|
30
30
|
* what let the hooks recognise Elliot tools.
|
|
31
31
|
*/
|
|
32
32
|
serverNames: z.array(z.string().min(1)).min(1).default(["elliot"]),
|
|
33
|
+
/**
|
|
34
|
+
* The LiteLLM MCP gateway's alias for Elliot — the `mcp_servers` key in
|
|
35
|
+
* `deploy/litellm/config.yaml`, which LiteLLM also uses as the prefix on
|
|
36
|
+
* every tool it proxies (`<alias>-<tool>`).
|
|
37
|
+
*
|
|
38
|
+
* REQUIRED, and deliberately without a default. This value used to be a
|
|
39
|
+
* constant in `mcp-keys.ts`, a fourth copy of a literal that also lives in
|
|
40
|
+
* the backend settings, the backend namespace module and the LiteLLM YAML.
|
|
41
|
+
* A default here would be that copy again: a plugin configured without the
|
|
42
|
+
* option would keep working against a guess, and would break silently on
|
|
43
|
+
* the day someone renamed the YAML key — recognising no gateway-prefixed
|
|
44
|
+
* tool, injecting no repo scope, and reporting nothing.
|
|
45
|
+
*
|
|
46
|
+
* The backend serves it with the plugin spec (`routers/well_known.py`),
|
|
47
|
+
* from `litellm_mcp_server_alias`.
|
|
48
|
+
*
|
|
49
|
+
* Distinct from `serverNames`, which is OpenCode's own server key and
|
|
50
|
+
* joins with an UNDERSCORE. Both read "elliot" in the default deployment
|
|
51
|
+
* and they are not the same value.
|
|
52
|
+
*/
|
|
53
|
+
gatewayAlias: z.string().min(1),
|
|
33
54
|
})
|
|
34
55
|
.strict()
|
|
35
56
|
|
|
@@ -53,11 +74,22 @@ export { KNOWLEDGE_TOOL, sanitizeKey }
|
|
|
53
74
|
|
|
54
75
|
/**
|
|
55
76
|
* Is this agent-visible tool key Elliot's knowledge tool on one of our
|
|
56
|
-
* configured servers?
|
|
57
|
-
*
|
|
77
|
+
* configured servers?
|
|
78
|
+
*
|
|
79
|
+
* NORMALISED FIRST, then matched exactly. It used to compare against one
|
|
80
|
+
* composed spelling, which stopped matching the moment runtime traffic moved
|
|
81
|
+
* behind the LiteLLM MCP gateway and the key gained a second namespace
|
|
82
|
+
* (`elliot_elliot-knowledge_search`). A knowledge call that is not recognised
|
|
83
|
+
* here is one that silently loses its repository scope — the agent still
|
|
84
|
+
* answers, just tenant-wide instead of repo-focused, and nothing in the reply
|
|
85
|
+
* says so.
|
|
86
|
+
*
|
|
87
|
+
* The anti-substring property is unchanged: `toCanonicalTool` strips only a
|
|
88
|
+
* prefix belonging to a CONFIGURED server, so `not-elliot_knowledge_search`
|
|
89
|
+
* still does not match.
|
|
58
90
|
*/
|
|
59
91
|
export function isElliotKnowledgeTool(toolKey: string, options: ElliotPluginOptions): boolean {
|
|
60
|
-
return options.serverNames.
|
|
92
|
+
return toCanonicalTool(toolKey, options.serverNames, options.gatewayAlias) === KNOWLEDGE_TOOL
|
|
61
93
|
}
|
|
62
94
|
|
|
63
95
|
// -- Shell policy verdicts ---------------------------------------------------
|