@rubics-code/agent-kit 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +80 -2
- package/dist/approval.d.ts +20 -0
- package/dist/approval.js +32 -0
- package/dist/assemble.d.ts +24 -0
- package/dist/assemble.js +19 -0
- package/dist/briefing.d.ts +12 -0
- package/dist/briefing.js +22 -0
- package/dist/capabilities.d.ts +2 -0
- package/dist/capabilities.js +6 -0
- package/dist/credentials.d.ts +28 -0
- package/dist/credentials.js +134 -0
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +4 -0
- package/dist/group.d.ts +68 -0
- package/dist/group.js +89 -0
- package/dist/groups/cloudflare/client.d.ts +42 -0
- package/dist/groups/cloudflare/client.js +76 -0
- package/dist/groups/cloudflare/guidance.md +7 -0
- package/dist/groups/cloudflare/memory.d.ts +19 -0
- package/dist/groups/cloudflare/memory.js +43 -0
- package/dist/groups/cloudflare/tools.d.ts +3 -0
- package/dist/groups/cloudflare/tools.js +140 -0
- package/dist/groups/infisical/client.d.ts +30 -0
- package/dist/groups/infisical/client.js +133 -0
- package/dist/groups/infisical/guidance.md +8 -0
- package/dist/groups/infisical/memory.d.ts +18 -0
- package/dist/groups/infisical/memory.js +43 -0
- package/dist/groups/infisical/tools.d.ts +3 -0
- package/dist/groups/infisical/tools.js +88 -0
- package/dist/groups/mxroute/client.d.ts +90 -0
- package/dist/groups/mxroute/client.js +86 -0
- package/dist/groups/mxroute/guidance.md +13 -0
- package/dist/groups/mxroute/memory.d.ts +34 -0
- package/dist/groups/mxroute/memory.js +83 -0
- package/dist/groups/mxroute/tools.d.ts +3 -0
- package/dist/groups/mxroute/tools.js +158 -0
- package/dist/groups/openrouter/client.d.ts +66 -0
- package/dist/groups/openrouter/client.js +66 -0
- package/dist/groups/openrouter/guidance.md +7 -0
- package/dist/groups/openrouter/memory.d.ts +26 -0
- package/dist/groups/openrouter/memory.js +59 -0
- package/dist/groups/openrouter/tools.d.ts +3 -0
- package/dist/groups/openrouter/tools.js +130 -0
- package/dist/groups/registrar/client.d.ts +48 -0
- package/dist/groups/registrar/client.js +75 -0
- package/dist/groups/registrar/guidance.md +9 -0
- package/dist/groups/registrar/memory.d.ts +16 -0
- package/dist/groups/registrar/memory.js +27 -0
- package/dist/groups/registrar/tools.d.ts +4 -0
- package/dist/groups/registrar/tools.js +102 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +35 -0
- package/dist/policy.d.ts +12 -0
- package/dist/policy.js +1 -0
- package/dist/result.d.ts +2 -0
- package/dist/result.js +17 -0
- package/dist/runtime/agent.d.ts +28 -0
- package/dist/runtime/agent.js +28 -0
- package/dist/runtime/container.d.ts +8 -0
- package/dist/runtime/container.js +41 -0
- package/dist/runtime/run.d.ts +51 -0
- package/dist/runtime/run.js +152 -0
- package/dist/runtime/transcribe.d.ts +7 -0
- package/dist/runtime/transcribe.js +37 -0
- package/dist/runtime/turns.d.ts +13 -0
- package/dist/runtime/turns.js +23 -0
- package/dist/runtime/web/index.html +452 -0
- package/dist/runtime/web/server.d.ts +20 -0
- package/dist/runtime/web/server.js +150 -0
- package/dist/schemas.d.ts +2 -0
- package/dist/schemas.js +6 -0
- package/dist/secret-delivery.d.ts +18 -0
- package/dist/secret-delivery.js +45 -0
- package/dist/secret-store.d.ts +13 -0
- package/dist/secret-store.js +1 -0
- package/dist/secrets.d.ts +5 -0
- package/dist/secrets.js +13 -0
- package/package.json +57 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Rubics Code
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,81 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @rubics-code/agent-kit
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The machinery for an agent: tool groups, the approval gate, secret delivery, key loading (env, Infisical Agent Vault, Infisical identity), the agent loop, a terminal chat, a local web chat, and a container mode. An agent gives data; the Kit does the rest.
|
|
4
|
+
|
|
5
|
+
Each write tool goes through an approval gate: the operator sees a summary that code writes from the arguments, never the model, and must approve it. A new secret (a password, an API key) never goes into the conversation. It goes to the operator's terminal, or into Infisical.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
Node 24 or later.
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @rubics-code/agent-kit
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## An agent
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { readFileSync } from "node:fs";
|
|
19
|
+
import { type AgentConfig, cloudflareGroup, infisicalGroup, runAgent } from "@rubics-code/agent-kit";
|
|
20
|
+
|
|
21
|
+
export const SCOUT: AgentConfig = {
|
|
22
|
+
name: "Scout",
|
|
23
|
+
model: "deepseek/deepseek-v4-flash-0731",
|
|
24
|
+
groups: [cloudflareGroup, infisicalGroup],
|
|
25
|
+
system: readFileSync(new URL("../home/prompts/system.md", import.meta.url), "utf8"),
|
|
26
|
+
policy: { agent: "Scout", operator: "Greg", maxKeyLimitUsd: 20, infisical: { /* readablePath, writablePath, envLabel */ } },
|
|
27
|
+
infisical: { url: "https://secrets.example.com", projectId: "…", path: "/scout", env: () => "prod", tokenKey: "SCOUT_INFISICAL_TOKEN" },
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
await runAgent(SCOUT);
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Run it with `node src/main.ts`. Node 24 runs TypeScript directly.
|
|
34
|
+
|
|
35
|
+
## A tool group
|
|
36
|
+
|
|
37
|
+
A group is one `ToolGroupDef` object: an id, a name, the service host (for Agent Vault), the env keys, a setup hint, the briefing guidance, `connect` (keys to client), and `tools` (client to tools). Make each write tool with `approvedTool`, so the operator must approve it. Add the group to the agent's `groups` list. A group can live in the agent's own repo.
|
|
38
|
+
|
|
39
|
+
## Env variables
|
|
40
|
+
|
|
41
|
+
`<PREFIX>` is the agent's name in upper case, or `envPrefix`.
|
|
42
|
+
|
|
43
|
+
| Variable | Use |
|
|
44
|
+
| --- | --- |
|
|
45
|
+
| `<PREFIX>_MODEL` | Replaces the pinned model, for a test. |
|
|
46
|
+
| `<PREFIX>_WEB_PORT` | The web chat port. Default 4141. |
|
|
47
|
+
| `<PREFIX>_KEYS=user` | Skips the identity file and uses your own keys. |
|
|
48
|
+
| `<PREFIX>_HEADLESS=1` | Container mode: no terminal, a health server, and new secrets go to Infisical. |
|
|
49
|
+
| `<PREFIX>_HEALTH_PORT` | The health port in container mode. Default 8080. |
|
|
50
|
+
|
|
51
|
+
## Container mode
|
|
52
|
+
|
|
53
|
+
The web chat stays on `127.0.0.1` inside the container. Reach it with `kubectl port-forward`. `GET /healthz` answers on all addresses. A new secret with no `storeAt` goes to the Infisical folder `<infisical.path>/inbox` (or `secretInbox`). In an image, start the agent behind Agent Vault with the Infisical CLI. The CLI mints a session with a machine identity, then removes the identity before the agent starts:
|
|
54
|
+
|
|
55
|
+
```sh
|
|
56
|
+
exec infisical agent-vault run --no-skills -- \
|
|
57
|
+
env -u INFISICAL_UNIVERSAL_AUTH_CLIENT_ID -u INFISICAL_UNIVERSAL_AUTH_CLIENT_SECRET node src/main.ts
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Set `INFISICAL_DOMAIN`, `INFISICAL_AGENT_VAULT_PROXY_ADDRESS`, `INFISICAL_AGENT_VAULT_PROJECT_ID`, `INFISICAL_AGENT_VAULT_ACCESS_BUNDLE`, `INFISICAL_UNIVERSAL_AUTH_CLIENT_ID` and `INFISICAL_UNIVERSAL_AUTH_CLIENT_SECRET`, and `NODE_USE_ENV_PROXY=1`. The `infisical/cli` image holds a static binary at `/bin/infisical` that you can copy into a `node:24-slim` image.
|
|
61
|
+
|
|
62
|
+
## Build and checks
|
|
63
|
+
|
|
64
|
+
| Command | What it does |
|
|
65
|
+
| --- | --- |
|
|
66
|
+
| `npm test` | The tests. `test/fixture.ts` is a test agent. |
|
|
67
|
+
| `npm run type-check` | Type-checks `src/` and `test/`. |
|
|
68
|
+
| `npm run build` | Compiles `src/` to `dist/` and copies the `.md` and `.html` files that the code reads. |
|
|
69
|
+
| `npm run pack-check` | Packs the package, installs the tarball in an empty folder, runs a consumer, and type-checks a consumer file. |
|
|
70
|
+
|
|
71
|
+
## Release
|
|
72
|
+
|
|
73
|
+
Make a GitHub release with a tag `v<major>.<minor>.<patch>`, for example `v0.2.0`. The `release` workflow sets that version, runs the tests, and publishes to npm. `version` in `package.json` stays `0.0.0`: the tag is the version. A release that is marked as a prerelease goes to the npm tag `next`.
|
|
74
|
+
|
|
75
|
+
The workflow uses trusted publishing (OIDC), so no npm token is stored, and npm adds provenance. The first release can use a short-lived `NPM_TOKEN` secret in the `npm` environment, because npm lets you add a trusted publisher only to a package that exists.
|
|
76
|
+
|
|
77
|
+
Until 1.0, a minor version can break the interface.
|
|
78
|
+
|
|
79
|
+
## License
|
|
80
|
+
|
|
81
|
+
MIT
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface ApprovalRequest {
|
|
2
|
+
kind: "approval";
|
|
3
|
+
tool: string;
|
|
4
|
+
summary: string;
|
|
5
|
+
args: Record<string, unknown>;
|
|
6
|
+
argsHash: string;
|
|
7
|
+
}
|
|
8
|
+
export interface ApprovalDecision {
|
|
9
|
+
approved: boolean;
|
|
10
|
+
argsHash: string;
|
|
11
|
+
reason?: string;
|
|
12
|
+
}
|
|
13
|
+
export declare function isApprovalRequest(value: unknown): value is ApprovalRequest;
|
|
14
|
+
export declare function hashArgs(args: Record<string, unknown>): string;
|
|
15
|
+
export declare function requireApproval(tool: string, summary: string, args: Record<string, unknown>): {
|
|
16
|
+
approved: true;
|
|
17
|
+
} | {
|
|
18
|
+
approved: false;
|
|
19
|
+
reason: string;
|
|
20
|
+
};
|
package/dist/approval.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { interrupt } from "@langchain/langgraph";
|
|
3
|
+
export function isApprovalRequest(value) {
|
|
4
|
+
return typeof value === "object" && value !== null && value.kind === "approval";
|
|
5
|
+
}
|
|
6
|
+
export function hashArgs(args) {
|
|
7
|
+
return createHash("sha256").update(canonical(args)).digest("hex");
|
|
8
|
+
}
|
|
9
|
+
function canonical(value) {
|
|
10
|
+
if (Array.isArray(value))
|
|
11
|
+
return `[${value.map(canonical).join(",")}]`;
|
|
12
|
+
if (value && typeof value === "object") {
|
|
13
|
+
const entries = Object.entries(value)
|
|
14
|
+
.filter(([, v]) => v !== undefined)
|
|
15
|
+
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
|
|
16
|
+
return `{${entries.map(([k, v]) => `${JSON.stringify(k)}:${canonical(v)}`).join(",")}}`;
|
|
17
|
+
}
|
|
18
|
+
return JSON.stringify(value);
|
|
19
|
+
}
|
|
20
|
+
// Pauses the graph until the operator decides. The tool body re-runs from the
|
|
21
|
+
// start on resume, so nothing with a side effect may come before this call.
|
|
22
|
+
export function requireApproval(tool, summary, args) {
|
|
23
|
+
const request = { kind: "approval", tool, summary, args, argsHash: hashArgs(args) };
|
|
24
|
+
const decision = interrupt(request);
|
|
25
|
+
if (!decision || decision.approved !== true) {
|
|
26
|
+
return { approved: false, reason: decision?.reason ?? "rejected" };
|
|
27
|
+
}
|
|
28
|
+
if (decision.argsHash !== request.argsHash) {
|
|
29
|
+
return { approved: false, reason: "the approval does not match these arguments" };
|
|
30
|
+
}
|
|
31
|
+
return { approved: true };
|
|
32
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { StructuredTool } from "@langchain/core/tools";
|
|
2
|
+
import { type ConnectContext, type GroupStatus, type ToolGroupDef } from "./group.js";
|
|
3
|
+
import type { AgentPolicy } from "./policy.js";
|
|
4
|
+
import type { SecretSink } from "./secrets.js";
|
|
5
|
+
export type AnyGroup = ToolGroupDef<unknown>;
|
|
6
|
+
export type Services = Partial<Record<string, unknown>>;
|
|
7
|
+
export interface Enable {
|
|
8
|
+
has(key: string): boolean;
|
|
9
|
+
vaultHosts?: Set<string> | undefined;
|
|
10
|
+
hosts?: Record<string, string | undefined>;
|
|
11
|
+
}
|
|
12
|
+
export declare function connectServices(groups: readonly AnyGroup[], ctx: ConnectContext): Services;
|
|
13
|
+
export declare function assembleAgent(opts: {
|
|
14
|
+
groups: readonly AnyGroup[];
|
|
15
|
+
system: string;
|
|
16
|
+
policy: AgentPolicy;
|
|
17
|
+
services: Services;
|
|
18
|
+
secrets: SecretSink;
|
|
19
|
+
enable: Enable;
|
|
20
|
+
}): {
|
|
21
|
+
tools: StructuredTool[];
|
|
22
|
+
briefing: string;
|
|
23
|
+
statuses: GroupStatus[];
|
|
24
|
+
};
|
package/dist/assemble.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { assembleBriefing } from "./briefing.js";
|
|
2
|
+
import { mountedTools, reviewGroup } from "./group.js";
|
|
3
|
+
import { infisicalGroup } from "./groups/infisical/tools.js";
|
|
4
|
+
// Makes each group's service from the session's keys.
|
|
5
|
+
export function connectServices(groups, ctx) {
|
|
6
|
+
return Object.fromEntries(groups.map((def) => [def.id, def.connect(ctx)]));
|
|
7
|
+
}
|
|
8
|
+
export function assembleAgent(opts) {
|
|
9
|
+
const { groups, policy, services } = opts;
|
|
10
|
+
const statuses = groups.map((def) => reviewGroup(def, { has: opts.enable.has, hasStore: !!services[infisicalGroup.id], vaultHosts: opts.enable.vaultHosts }, { host: opts.enable.hosts?.[def.id] }));
|
|
11
|
+
// Mount a service only when its group is on. In Agent Vault mode every key
|
|
12
|
+
// is a placeholder, so a service can exist for a host the session lacks.
|
|
13
|
+
const on = (id) => statuses.some((s) => s.id === id && s.enabled);
|
|
14
|
+
const store = on(infisicalGroup.id) ? services[infisicalGroup.id] : undefined;
|
|
15
|
+
const shared = { secrets: opts.secrets, store, policy };
|
|
16
|
+
const tools = groups.flatMap((def) => mountedTools(def, on(def.id) ? services[def.id] : undefined, shared));
|
|
17
|
+
const briefing = assembleBriefing({ system: opts.system, defs: groups, statuses, policy });
|
|
18
|
+
return { tools, briefing, statuses };
|
|
19
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { GroupStatus } from "./group.js";
|
|
2
|
+
import type { AgentPolicy } from "./policy.js";
|
|
3
|
+
export interface GuidanceLike {
|
|
4
|
+
id: string;
|
|
5
|
+
guidance: string;
|
|
6
|
+
}
|
|
7
|
+
export declare function assembleBriefing(opts: {
|
|
8
|
+
system: string;
|
|
9
|
+
defs: readonly GuidanceLike[];
|
|
10
|
+
statuses: readonly GroupStatus[];
|
|
11
|
+
policy: AgentPolicy;
|
|
12
|
+
}): string;
|
package/dist/briefing.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// The briefing: the agent's system prompt, the guidance of every included
|
|
2
|
+
// group, and the capability note. Placeholders in the prose — {{operator}},
|
|
3
|
+
// {{agent}}, {{maxKeyLimitUsd}} — are substituted from the policy. The model
|
|
4
|
+
// never writes any of it.
|
|
5
|
+
import { capabilityNote } from "./capabilities.js";
|
|
6
|
+
export function assembleBriefing(opts) {
|
|
7
|
+
const fill = (text) => text
|
|
8
|
+
.replaceAll("{{operator}}", opts.policy.operator)
|
|
9
|
+
.replaceAll("{{agent}}", opts.policy.agent)
|
|
10
|
+
.replaceAll("{{maxKeyLimitUsd}}", String(opts.policy.maxKeyLimitUsd));
|
|
11
|
+
const byId = new Map(opts.defs.map((d) => [d.id, d]));
|
|
12
|
+
const parts = [fill(opts.system)];
|
|
13
|
+
for (const status of opts.statuses) {
|
|
14
|
+
if (!status.enabled)
|
|
15
|
+
continue;
|
|
16
|
+
const def = byId.get(status.id);
|
|
17
|
+
if (def?.guidance)
|
|
18
|
+
parts.push(fill(def.guidance));
|
|
19
|
+
}
|
|
20
|
+
parts.push(capabilityNote(opts.statuses, opts.policy.operator));
|
|
21
|
+
return parts.join("\n\n");
|
|
22
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export function capabilityNote(groups, operator) {
|
|
2
|
+
const lines = groups.map((g) => g.enabled
|
|
3
|
+
? `- ${g.name}: on.`
|
|
4
|
+
: `- ${g.name}: OFF. Missing: ${g.missing.join(", ")}. ${g.setup}`);
|
|
5
|
+
return `Your tool groups now. If ${operator} asks why a tool is not there, give this reason:\n${lines.join("\n")}`;
|
|
6
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { HttpInfisical } from "./groups/infisical/client.js";
|
|
2
|
+
export interface Credentials<K extends string = string> {
|
|
3
|
+
mode: "env" | "agent-vault" | "identity";
|
|
4
|
+
get(name: K): string;
|
|
5
|
+
optional(name: K): string | undefined;
|
|
6
|
+
holds(name: string): boolean;
|
|
7
|
+
}
|
|
8
|
+
export interface InfisicalConfig {
|
|
9
|
+
url: string;
|
|
10
|
+
projectId: string;
|
|
11
|
+
path: string;
|
|
12
|
+
env: (path: string) => string;
|
|
13
|
+
}
|
|
14
|
+
export declare const placeholder: (name: string) => string;
|
|
15
|
+
export declare function isAgentVault(env?: NodeJS.ProcessEnv): boolean;
|
|
16
|
+
export declare function loadCredentials<K extends string = string>(env?: NodeJS.ProcessEnv): Credentials<K>;
|
|
17
|
+
export declare function readIdentityFile(file: URL | string): NodeJS.ProcessEnv | undefined;
|
|
18
|
+
export declare function loadIdentityCredentials<K extends string = string>(env: NodeJS.ProcessEnv, config: InfisicalConfig, opts?: {
|
|
19
|
+
fetch?: typeof fetch;
|
|
20
|
+
}): Promise<{
|
|
21
|
+
creds: Credentials<K>;
|
|
22
|
+
infisical: HttpInfisical;
|
|
23
|
+
}>;
|
|
24
|
+
export declare function sessionHosts(infisicalUrl: string, env?: NodeJS.ProcessEnv, f?: typeof fetch): Promise<Set<string> | undefined>;
|
|
25
|
+
export declare function sessionToken(env?: NodeJS.ProcessEnv): string | undefined;
|
|
26
|
+
export declare function groupKeys(groups: readonly {
|
|
27
|
+
keys: readonly string[];
|
|
28
|
+
}[]): string[];
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
// How an agent gets its keys. Three modes:
|
|
2
|
+
// - env: `infisical run` puts the real values in the environment.
|
|
3
|
+
// - agent-vault: `infisical agent-vault run` starts the agent behind the Agent
|
|
4
|
+
// Vault proxy. The agent sends placeholders such as __MXROUTE_API_KEY__, and
|
|
5
|
+
// the proxy puts in the real values, so the process never holds a key. The
|
|
6
|
+
// session's services (sessionHosts) decide which tool groups are on.
|
|
7
|
+
// - identity: the agent logs in with its Infisical machine identity and reads
|
|
8
|
+
// its keys from its own Infisical folder.
|
|
9
|
+
// The Agent Vault services must use the placeholders that `placeholder` makes.
|
|
10
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
11
|
+
import { parseEnv } from "node:util";
|
|
12
|
+
import { HttpInfisical } from "./groups/infisical/client.js";
|
|
13
|
+
export const placeholder = (name) => `__${name}__`;
|
|
14
|
+
// `infisical agent-vault run` puts the session token into HTTPS_PROXY as the
|
|
15
|
+
// password of the user x-agent-vault. Some CLI versions also set
|
|
16
|
+
// INFISICAL_AGENT_VAULT_SESSION_TOKEN; 0.45.4 does not.
|
|
17
|
+
export function isAgentVault(env = process.env) {
|
|
18
|
+
if (env.INFISICAL_AGENT_VAULT_SESSION_TOKEN)
|
|
19
|
+
return true;
|
|
20
|
+
try {
|
|
21
|
+
return new URL(env.HTTPS_PROXY ?? env.https_proxy ?? "").username === "x-agent-vault";
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
// Env mode or Agent Vault mode, from the environment.
|
|
28
|
+
export function loadCredentials(env = process.env) {
|
|
29
|
+
if (isAgentVault(env)) {
|
|
30
|
+
if (!env.HTTPS_PROXY)
|
|
31
|
+
throw new Error("Agent Vault mode: HTTPS_PROXY is not set");
|
|
32
|
+
// Without it, Node's fetch ignores HTTPS_PROXY and sends the placeholders
|
|
33
|
+
// directly to the API.
|
|
34
|
+
if (env.NODE_USE_ENV_PROXY !== "1")
|
|
35
|
+
throw new Error("Agent Vault mode: set NODE_USE_ENV_PROXY=1");
|
|
36
|
+
const present = (name) => {
|
|
37
|
+
if (env[name])
|
|
38
|
+
throw new Error(`Agent Vault mode: ${name} is in the environment. Remove it.`);
|
|
39
|
+
return placeholder(name);
|
|
40
|
+
};
|
|
41
|
+
return { mode: "agent-vault", get: present, optional: present, holds: (name) => !env[name] };
|
|
42
|
+
}
|
|
43
|
+
return {
|
|
44
|
+
mode: "env",
|
|
45
|
+
get(name) {
|
|
46
|
+
const value = env[name];
|
|
47
|
+
if (!value)
|
|
48
|
+
throw new Error(`${name} is not set`);
|
|
49
|
+
return value;
|
|
50
|
+
},
|
|
51
|
+
optional: (name) => env[name] || undefined,
|
|
52
|
+
holds: (name) => !!env[name],
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
// The agent's identity file, read directly. A shell can export its own
|
|
56
|
+
// INFISICAL_CLIENT_ID (for example from a repo-root .env), and node
|
|
57
|
+
// --env-file never replaces a variable that is set, so the file must not go
|
|
58
|
+
// through the environment.
|
|
59
|
+
export function readIdentityFile(file) {
|
|
60
|
+
if (!existsSync(file))
|
|
61
|
+
return undefined;
|
|
62
|
+
const v = parseEnv(readFileSync(file, "utf8"));
|
|
63
|
+
if (!v.INFISICAL_CLIENT_ID || !v.INFISICAL_CLIENT_SECRET) {
|
|
64
|
+
throw new Error(`${file instanceof URL ? file.pathname : file} needs INFISICAL_CLIENT_ID and INFISICAL_CLIENT_SECRET`);
|
|
65
|
+
}
|
|
66
|
+
return { INFISICAL_CLIENT_ID: v.INFISICAL_CLIENT_ID, INFISICAL_CLIENT_SECRET: v.INFISICAL_CLIENT_SECRET };
|
|
67
|
+
}
|
|
68
|
+
// Identity mode. It removes the client secret from the given object after
|
|
69
|
+
// the client has it.
|
|
70
|
+
export async function loadIdentityCredentials(env, config, opts = {}) {
|
|
71
|
+
const clientId = env.INFISICAL_CLIENT_ID;
|
|
72
|
+
const clientSecret = env.INFISICAL_CLIENT_SECRET;
|
|
73
|
+
if (!clientId || !clientSecret)
|
|
74
|
+
throw new Error("Identity mode: INFISICAL_CLIENT_ID and INFISICAL_CLIENT_SECRET are not set");
|
|
75
|
+
delete env.INFISICAL_CLIENT_SECRET;
|
|
76
|
+
const infisical = new HttpInfisical({ clientId, clientSecret }, {
|
|
77
|
+
baseUrl: config.url,
|
|
78
|
+
projectId: config.projectId,
|
|
79
|
+
env: config.env,
|
|
80
|
+
...(opts.fetch ? { fetch: opts.fetch } : {}),
|
|
81
|
+
});
|
|
82
|
+
const values = await infisical.readAll(config.path);
|
|
83
|
+
return {
|
|
84
|
+
infisical,
|
|
85
|
+
creds: {
|
|
86
|
+
mode: "identity",
|
|
87
|
+
get(name) {
|
|
88
|
+
const value = values[name];
|
|
89
|
+
if (!value)
|
|
90
|
+
throw new Error(`${name} is not in Infisical ${config.path}`);
|
|
91
|
+
return value;
|
|
92
|
+
},
|
|
93
|
+
optional: (name) => values[name] || undefined,
|
|
94
|
+
holds: (name) => !!values[name],
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
export async function sessionHosts(infisicalUrl, env = process.env, f = fetch) {
|
|
99
|
+
const base = (env.INFISICAL_AGENT_VAULT_URL || infisicalUrl).replace(/\/+$/, "").replace(/\/api$/, "");
|
|
100
|
+
const token = sessionToken(env);
|
|
101
|
+
try {
|
|
102
|
+
const res = await f(`${base}/api/v1/agent-vault/session/services`, {
|
|
103
|
+
headers: { Accept: "application/json", ...(token ? { Authorization: `Bearer ${token}` } : {}) },
|
|
104
|
+
});
|
|
105
|
+
if (!res.ok)
|
|
106
|
+
return undefined;
|
|
107
|
+
const json = (await res.json());
|
|
108
|
+
const hosts = (json.services ?? [])
|
|
109
|
+
.filter((s) => !s.status || s.status === "ready")
|
|
110
|
+
.flatMap((s) => s.hosts ?? [])
|
|
111
|
+
.map((h) => h.toLowerCase().replace(/:443$/, ""));
|
|
112
|
+
return new Set(hosts);
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
// The session token is the password in HTTPS_PROXY (user x-agent-vault), so
|
|
119
|
+
// the agent holds it already; it grants this session only.
|
|
120
|
+
export function sessionToken(env = process.env) {
|
|
121
|
+
if (env.INFISICAL_AGENT_VAULT_SESSION_TOKEN)
|
|
122
|
+
return env.INFISICAL_AGENT_VAULT_SESSION_TOKEN;
|
|
123
|
+
try {
|
|
124
|
+
const url = new URL(env.HTTPS_PROXY ?? env.https_proxy ?? "");
|
|
125
|
+
return url.username === "x-agent-vault" && url.password ? decodeURIComponent(url.password) : undefined;
|
|
126
|
+
}
|
|
127
|
+
catch {
|
|
128
|
+
return undefined;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
// Every key that a group list can turn on, for a key list or a check.
|
|
132
|
+
export function groupKeys(groups) {
|
|
133
|
+
return [...new Set(groups.flatMap((g) => g.keys))];
|
|
134
|
+
}
|
package/dist/errors.d.ts
ADDED
package/dist/errors.js
ADDED
package/dist/group.d.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { StructuredTool } from "@langchain/core/tools";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import type { AgentPolicy } from "./policy.js";
|
|
4
|
+
import type { SecretSink } from "./secrets.js";
|
|
5
|
+
import type { SecretStore } from "./secret-store.js";
|
|
6
|
+
export interface SharedDeps {
|
|
7
|
+
secrets: SecretSink;
|
|
8
|
+
store?: SecretStore | undefined;
|
|
9
|
+
policy: AgentPolicy;
|
|
10
|
+
}
|
|
11
|
+
export interface ToolMeta {
|
|
12
|
+
name: string;
|
|
13
|
+
approval: boolean;
|
|
14
|
+
}
|
|
15
|
+
export interface ToolGroupDef<S> {
|
|
16
|
+
id: string;
|
|
17
|
+
name: string;
|
|
18
|
+
host: string;
|
|
19
|
+
keys: readonly string[];
|
|
20
|
+
setup: string;
|
|
21
|
+
guidance: string;
|
|
22
|
+
review?(env: EnableEnv): {
|
|
23
|
+
enabled: boolean;
|
|
24
|
+
missing: string[];
|
|
25
|
+
setup?: string;
|
|
26
|
+
};
|
|
27
|
+
connect(ctx: ConnectContext): S | undefined;
|
|
28
|
+
tools(service: S, shared: SharedDeps): StructuredTool[];
|
|
29
|
+
}
|
|
30
|
+
export interface ConnectContext {
|
|
31
|
+
holds(key: string): boolean;
|
|
32
|
+
get(key: string): string;
|
|
33
|
+
store?: SecretStore | undefined;
|
|
34
|
+
}
|
|
35
|
+
export declare function holdsAll(ctx: ConnectContext, keys: readonly string[]): boolean;
|
|
36
|
+
export declare function approvedTool<S extends z.ZodType, D = undefined>(def: {
|
|
37
|
+
name: string;
|
|
38
|
+
description: string;
|
|
39
|
+
operator: string;
|
|
40
|
+
schema: S;
|
|
41
|
+
prepare: (args: z.infer<S>) => Promise<{
|
|
42
|
+
summary: string;
|
|
43
|
+
args?: Record<string, unknown>;
|
|
44
|
+
data?: D;
|
|
45
|
+
early?: string;
|
|
46
|
+
}>;
|
|
47
|
+
run: (args: z.infer<S>, data: D) => Promise<unknown>;
|
|
48
|
+
}): StructuredTool;
|
|
49
|
+
export declare function toolCatalog(tools: readonly StructuredTool[]): ToolMeta[];
|
|
50
|
+
export declare function defCatalog(def: ToolGroupDef<unknown>): ToolMeta[];
|
|
51
|
+
export declare function mountedTools<S>(def: ToolGroupDef<S>, service: S | undefined, shared: SharedDeps): StructuredTool[];
|
|
52
|
+
export interface EnableEnv {
|
|
53
|
+
has(key: string): boolean;
|
|
54
|
+
hasStore: boolean;
|
|
55
|
+
vaultHosts?: Set<string> | undefined;
|
|
56
|
+
host?: string | undefined;
|
|
57
|
+
}
|
|
58
|
+
export interface GroupStatus {
|
|
59
|
+
id: string;
|
|
60
|
+
name: string;
|
|
61
|
+
enabled: boolean;
|
|
62
|
+
missing: string[];
|
|
63
|
+
setup: string;
|
|
64
|
+
catalog: ToolMeta[];
|
|
65
|
+
}
|
|
66
|
+
export declare function reviewGroup(def: ToolGroupDef<unknown>, env: EnableEnv, opts?: {
|
|
67
|
+
host?: string | undefined;
|
|
68
|
+
}): GroupStatus;
|
package/dist/group.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// One self-describing tool group: everything the rest of the system needs to
|
|
2
|
+
// know about a group lives in its definition. The capabilities list, the Agent
|
|
3
|
+
// Vault host check, the tools panel and the briefing are derived from these
|
|
4
|
+
// definitions, so an id, a key or an approval flag is written once, here.
|
|
5
|
+
import { tool } from "langchain";
|
|
6
|
+
import { z } from "zod";
|
|
7
|
+
import { requireApproval } from "./approval.js";
|
|
8
|
+
import { rejected, result } from "./result.js";
|
|
9
|
+
// Whether the session holds each key in the list.
|
|
10
|
+
export function holdsAll(ctx, keys) {
|
|
11
|
+
return keys.every((k) => ctx.holds(k));
|
|
12
|
+
}
|
|
13
|
+
// A write tool. The gate is not optional and cannot be skipped: the tool body
|
|
14
|
+
// always calls requireApproval before run, with a summary that code composes
|
|
15
|
+
// from the arguments. prepare does the reads the summary needs, may normalize
|
|
16
|
+
// what the gate hashes (args), may hand data to run, and may return an early
|
|
17
|
+
// result instead of gating (nothing to do, or a precondition failed).
|
|
18
|
+
const APPROVED = Symbol("kit.approved");
|
|
19
|
+
export function approvedTool(def) {
|
|
20
|
+
const t = tool((args) => result(async () => {
|
|
21
|
+
const pre = await def.prepare(args);
|
|
22
|
+
if (pre.early !== undefined)
|
|
23
|
+
return pre.early;
|
|
24
|
+
const gate = requireApproval(def.name, pre.summary, pre.args ?? args);
|
|
25
|
+
if (!gate.approved)
|
|
26
|
+
return rejected(gate.reason, def.operator);
|
|
27
|
+
return def.run(args, pre.data);
|
|
28
|
+
}), { name: def.name, description: def.description, schema: def.schema });
|
|
29
|
+
Object.defineProperty(t, APPROVED, { value: true });
|
|
30
|
+
return t;
|
|
31
|
+
}
|
|
32
|
+
// Dependencies used only to build a definition's tool closures for the
|
|
33
|
+
// catalog. A definition must never call them: build time makes closures, it
|
|
34
|
+
// does no work.
|
|
35
|
+
const definitionDeps = {
|
|
36
|
+
secrets: { async deliver() { } },
|
|
37
|
+
store: undefined,
|
|
38
|
+
policy: {
|
|
39
|
+
agent: "AGENT",
|
|
40
|
+
operator: "OPERATOR",
|
|
41
|
+
maxKeyLimitUsd: 100,
|
|
42
|
+
infisical: {
|
|
43
|
+
readablePath: z.string(),
|
|
44
|
+
writablePath: z.string(),
|
|
45
|
+
envLabel: () => "ENV",
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
// A group's catalog, derived by building its tools once. approval is
|
|
50
|
+
// structural: true exactly when the tool was made with approvedTool, so the
|
|
51
|
+
// catalog can never drift from the gate calls in the bodies.
|
|
52
|
+
export function toolCatalog(tools) {
|
|
53
|
+
return tools.map((t) => ({ name: t.name, approval: APPROVED in t }));
|
|
54
|
+
}
|
|
55
|
+
export function defCatalog(def) {
|
|
56
|
+
return toolCatalog(def.tools(undefined, definitionDeps));
|
|
57
|
+
}
|
|
58
|
+
// Mounting: the tools of a group whose service is on, none otherwise.
|
|
59
|
+
export function mountedTools(def, service, shared) {
|
|
60
|
+
return service ? def.tools(service, shared) : [];
|
|
61
|
+
}
|
|
62
|
+
// Takes the unknown-typed definition because it only reads the group's
|
|
63
|
+
// description (id, name, keys, host, setup, review) and never its service.
|
|
64
|
+
export function reviewGroup(def, env, opts = {}) {
|
|
65
|
+
// The agent may override a group's host (the Infisical deployment's host is
|
|
66
|
+
// the agent's to name); otherwise the definition's host stands.
|
|
67
|
+
const fullEnv = { ...env, host: opts.host ?? def.host };
|
|
68
|
+
let enabled;
|
|
69
|
+
let missing;
|
|
70
|
+
let setup = def.setup;
|
|
71
|
+
if (def.review) {
|
|
72
|
+
const r = def.review(fullEnv);
|
|
73
|
+
enabled = r.enabled;
|
|
74
|
+
missing = r.missing;
|
|
75
|
+
if (r.setup)
|
|
76
|
+
setup = r.setup;
|
|
77
|
+
}
|
|
78
|
+
else if (fullEnv.vaultHosts) {
|
|
79
|
+
const on = !!fullEnv.host && fullEnv.vaultHosts.has(fullEnv.host);
|
|
80
|
+
enabled = on;
|
|
81
|
+
missing = on ? [] : fullEnv.host ? [fullEnv.host] : ["the service"];
|
|
82
|
+
setup = "Add an Agent Vault service for this host to the access bundle, then restart the agent.";
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
missing = def.keys.filter((k) => !fullEnv.has(k));
|
|
86
|
+
enabled = missing.length === 0;
|
|
87
|
+
}
|
|
88
|
+
return { id: def.id, name: def.name, enabled, missing, setup, catalog: defCatalog(def) };
|
|
89
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { ServiceError } from "../../errors.js";
|
|
2
|
+
export interface Zone {
|
|
3
|
+
id: string;
|
|
4
|
+
name: string;
|
|
5
|
+
}
|
|
6
|
+
export interface DnsRecord {
|
|
7
|
+
id: string;
|
|
8
|
+
type: string;
|
|
9
|
+
name: string;
|
|
10
|
+
content: string;
|
|
11
|
+
priority?: number;
|
|
12
|
+
ttl: number;
|
|
13
|
+
proxied?: boolean;
|
|
14
|
+
comment?: string | null;
|
|
15
|
+
}
|
|
16
|
+
export interface NewDnsRecord {
|
|
17
|
+
type: "MX" | "TXT";
|
|
18
|
+
name: string;
|
|
19
|
+
content: string;
|
|
20
|
+
priority?: number;
|
|
21
|
+
comment: string;
|
|
22
|
+
}
|
|
23
|
+
export interface CloudflareDns {
|
|
24
|
+
findZone(domain: string): Promise<Zone | null>;
|
|
25
|
+
listRecords(zoneId: string): Promise<DnsRecord[]>;
|
|
26
|
+
createRecord(zoneId: string, record: NewDnsRecord): Promise<DnsRecord>;
|
|
27
|
+
}
|
|
28
|
+
export declare class CloudflareError extends ServiceError {
|
|
29
|
+
readonly status: number;
|
|
30
|
+
readonly codes: number[];
|
|
31
|
+
constructor(status: number, codes: number[], message: string);
|
|
32
|
+
}
|
|
33
|
+
export declare class HttpCloudflareDns implements CloudflareDns {
|
|
34
|
+
#private;
|
|
35
|
+
constructor(token: string, opts?: {
|
|
36
|
+
baseUrl?: string;
|
|
37
|
+
fetch?: typeof fetch;
|
|
38
|
+
});
|
|
39
|
+
findZone(domain: string): Promise<Zone | null>;
|
|
40
|
+
listRecords(zoneId: string): Promise<DnsRecord[]>;
|
|
41
|
+
createRecord(zoneId: string, record: NewDnsRecord): Promise<DnsRecord>;
|
|
42
|
+
}
|