@rubics-code/agent-kit 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +88 -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/cli/bin.d.ts +2 -0
- package/dist/cli/bin.js +39 -0
- package/dist/cli/init.d.ts +9 -0
- package/dist/cli/init.js +73 -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 +61 -4
- package/template/Dockerfile +19 -0
- package/template/README.md +55 -0
- package/template/_dockerignore +7 -0
- package/template/_gitignore +4 -0
- package/template/entrypoint.sh +14 -0
- package/template/evals/notes.eval.ts +55 -0
- package/template/home/memory/README.md +1 -0
- package/template/home/prompts/system.md +15 -0
- package/template/src/agent.ts +47 -0
- package/template/src/groups/notes.ts +85 -0
- package/template/src/main.ts +4 -0
- package/template/test/agent.test.ts +22 -0
- package/template/tsconfig.json +17 -0
- package/template/vitest.config.ts +7 -0
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
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// Client for the Cloudflare DNS API, v4. Only the calls the tools need.
|
|
2
|
+
import { ServiceError } from "../../errors.js";
|
|
3
|
+
export class CloudflareError extends ServiceError {
|
|
4
|
+
status;
|
|
5
|
+
codes;
|
|
6
|
+
constructor(status, codes, message) {
|
|
7
|
+
super(`Cloudflare ${status}${codes.length ? ` [${codes.join(", ")}]` : ""}: ${message}`);
|
|
8
|
+
this.name = "CloudflareError";
|
|
9
|
+
this.status = status;
|
|
10
|
+
this.codes = codes;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export class HttpCloudflareDns {
|
|
14
|
+
#token;
|
|
15
|
+
#baseUrl;
|
|
16
|
+
#fetch;
|
|
17
|
+
constructor(token, opts = {}) {
|
|
18
|
+
this.#token = token;
|
|
19
|
+
this.#baseUrl = opts.baseUrl ?? "https://api.cloudflare.com/client/v4";
|
|
20
|
+
this.#fetch = opts.fetch ?? fetch;
|
|
21
|
+
}
|
|
22
|
+
async #request(method, path, body) {
|
|
23
|
+
const res = await this.#fetch(`${this.#baseUrl}${path}`, {
|
|
24
|
+
method,
|
|
25
|
+
headers: {
|
|
26
|
+
Authorization: `Bearer ${this.#token}`,
|
|
27
|
+
Accept: "application/json",
|
|
28
|
+
...(body === undefined ? {} : { "Content-Type": "application/json" }),
|
|
29
|
+
},
|
|
30
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
31
|
+
});
|
|
32
|
+
const text = await res.text();
|
|
33
|
+
let json;
|
|
34
|
+
try {
|
|
35
|
+
json = text ? JSON.parse(text) : {};
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
throw new CloudflareError(res.status, [], `not JSON: ${text.slice(0, 200)}`);
|
|
39
|
+
}
|
|
40
|
+
if (!res.ok || json.success === false) {
|
|
41
|
+
const errors = json.errors ?? [];
|
|
42
|
+
const message = errors.map((e) => e.message).filter(Boolean).join("; ") || res.statusText;
|
|
43
|
+
throw new CloudflareError(res.status, errors.map((e) => e.code).filter((c) => typeof c === "number"), message);
|
|
44
|
+
}
|
|
45
|
+
return json;
|
|
46
|
+
}
|
|
47
|
+
async findZone(domain) {
|
|
48
|
+
const labels = domain.toLowerCase().split(".");
|
|
49
|
+
for (let i = 0; i < labels.length - 1; i++) {
|
|
50
|
+
const name = labels.slice(i).join(".");
|
|
51
|
+
const { result } = await this.#request("GET", `/zones?name=${encodeURIComponent(name)}`);
|
|
52
|
+
const zone = result?.find((z) => z.name === name);
|
|
53
|
+
if (zone)
|
|
54
|
+
return { id: zone.id, name: zone.name };
|
|
55
|
+
}
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
async listRecords(zoneId) {
|
|
59
|
+
const all = [];
|
|
60
|
+
for (let page = 1;; page++) {
|
|
61
|
+
const env = await this.#request("GET", `/zones/${encodeURIComponent(zoneId)}/dns_records?per_page=100&page=${page}`);
|
|
62
|
+
all.push(...(env.result ?? []));
|
|
63
|
+
if (page >= (env.result_info?.total_pages ?? 1))
|
|
64
|
+
return all;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
async createRecord(zoneId, record) {
|
|
68
|
+
const { result } = await this.#request("POST", `/zones/${encodeURIComponent(zoneId)}/dns_records`, {
|
|
69
|
+
...record,
|
|
70
|
+
ttl: 1,
|
|
71
|
+
});
|
|
72
|
+
if (!result)
|
|
73
|
+
throw new CloudflareError(200, [], "no record in the response");
|
|
74
|
+
return result;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# Cloudflare DNS
|
|
2
|
+
|
|
3
|
+
You can read the records in a zone and add MX and TXT records. You cannot change or delete a record, and you cannot add other record types: Terraform owns the A, AAAA and CNAME records. If a domain is not in Cloudflare, or you have no Cloudflare tools, give {{operator}} the records to add by hand.
|
|
4
|
+
|
|
5
|
+
Two SPF records break SPF, and you must merge them by hand: the tool makes no record when the name already has an SPF record, when the same record exists, or when the name is a CNAME.
|
|
6
|
+
|
|
7
|
+
Each record you make carries the comment "Created by {{agent}}, approved by {{operator}}", so {{operator}} can find it in the Cloudflare dashboard.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type CloudflareDns, type DnsRecord, type NewDnsRecord, type Zone } from "./client.js";
|
|
2
|
+
export type SeedRecord = Omit<DnsRecord, "id" | "ttl"> & {
|
|
3
|
+
ttl?: number;
|
|
4
|
+
};
|
|
5
|
+
export declare class MemoryCloudflareDns implements CloudflareDns {
|
|
6
|
+
#private;
|
|
7
|
+
readonly zones: Map<string, {
|
|
8
|
+
zone: Zone;
|
|
9
|
+
records: DnsRecord[];
|
|
10
|
+
}>;
|
|
11
|
+
readonly writes: {
|
|
12
|
+
op: string;
|
|
13
|
+
args: unknown;
|
|
14
|
+
}[];
|
|
15
|
+
constructor(seed?: Record<string, SeedRecord[]>);
|
|
16
|
+
findZone(domain: string): Promise<Zone | null>;
|
|
17
|
+
listRecords(zoneId: string): Promise<DnsRecord[]>;
|
|
18
|
+
createRecord(zoneId: string, record: NewDnsRecord): Promise<DnsRecord>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { CloudflareError } from "./client.js";
|
|
2
|
+
// In-memory Cloudflare DNS for tests and evals. It records every write.
|
|
3
|
+
export class MemoryCloudflareDns {
|
|
4
|
+
zones = new Map();
|
|
5
|
+
writes = [];
|
|
6
|
+
#next = 1;
|
|
7
|
+
constructor(seed = {}) {
|
|
8
|
+
for (const [name, records] of Object.entries(seed)) {
|
|
9
|
+
this.zones.set(name, {
|
|
10
|
+
zone: { id: `zone-${name}`, name },
|
|
11
|
+
records: records.map((r) => ({ ttl: 1, ...r, id: `rec-${this.#next++}` })),
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
#byId(zoneId) {
|
|
16
|
+
const z = [...this.zones.values()].find((v) => v.zone.id === zoneId);
|
|
17
|
+
if (!z)
|
|
18
|
+
throw new CloudflareError(404, [7003], `zone ${zoneId} not found`);
|
|
19
|
+
return z;
|
|
20
|
+
}
|
|
21
|
+
async findZone(domain) {
|
|
22
|
+
const labels = domain.split(".");
|
|
23
|
+
for (let i = 0; i < labels.length - 1; i++) {
|
|
24
|
+
const z = this.zones.get(labels.slice(i).join("."));
|
|
25
|
+
if (z)
|
|
26
|
+
return z.zone;
|
|
27
|
+
}
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
async listRecords(zoneId) {
|
|
31
|
+
return this.#byId(zoneId).records;
|
|
32
|
+
}
|
|
33
|
+
async createRecord(zoneId, record) {
|
|
34
|
+
this.writes.push({ op: "createRecord", args: { zoneId, ...record } });
|
|
35
|
+
const z = this.#byId(zoneId);
|
|
36
|
+
if (z.records.some((r) => r.type === record.type && r.name === record.name && r.content === record.content)) {
|
|
37
|
+
throw new CloudflareError(400, [81058], "An identical record already exists.");
|
|
38
|
+
}
|
|
39
|
+
const created = { ...record, id: `rec-${this.#next++}`, ttl: 1 };
|
|
40
|
+
z.records.push(created);
|
|
41
|
+
return created;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// The Cloudflare DNS tool group: mail records only. Terraform owns the A,
|
|
2
|
+
// AAAA and CNAME records, so a type outside this list could start a fight
|
|
3
|
+
// with a Terraform apply.
|
|
4
|
+
import { readFileSync } from "node:fs";
|
|
5
|
+
import { tool } from "langchain";
|
|
6
|
+
import { z } from "zod";
|
|
7
|
+
import { approvedTool, holdsAll } from "../../group.js";
|
|
8
|
+
import { HttpCloudflareDns } from "./client.js";
|
|
9
|
+
import { domain } from "../../schemas.js";
|
|
10
|
+
import { result } from "../../result.js";
|
|
11
|
+
const guidance = readFileSync(new URL("./guidance.md", import.meta.url), "utf8");
|
|
12
|
+
const RECORD_TYPES = ["MX", "TXT"];
|
|
13
|
+
const recordName = z
|
|
14
|
+
.string()
|
|
15
|
+
.regex(/^(@|[a-z0-9_]([a-z0-9_-]*[a-z0-9_])?(\.[a-z0-9_]([a-z0-9_-]*[a-z0-9_])?)*)$/, "@ for the domain itself, or a lowercase name such as x._domainkey")
|
|
16
|
+
.describe("@ for the domain itself, or the name in front of the domain, for example x._domainkey");
|
|
17
|
+
function fqdn(name, dom) {
|
|
18
|
+
if (name === "@" || name === dom)
|
|
19
|
+
return dom;
|
|
20
|
+
return name.endsWith(`.${dom}`) ? name : `${name}.${dom}`;
|
|
21
|
+
}
|
|
22
|
+
// Cloudflare can return a TXT value in quotes and an MX host with a final dot.
|
|
23
|
+
function same(type, a, b) {
|
|
24
|
+
const norm = (s) => type === "TXT" ? s.replace(/^"(.*)"$/s, "$1") : s.toLowerCase().replace(/\.$/, "");
|
|
25
|
+
return norm(a) === norm(b);
|
|
26
|
+
}
|
|
27
|
+
const isSpf = (r) => r.type === "TXT" && /^"?v=spf1(\s|"?$)/i.test(r.content);
|
|
28
|
+
function show(r) {
|
|
29
|
+
return `${r.type} ${r.name}${r.priority !== undefined && r.type === "MX" ? ` ${r.priority}` : ""} ${r.content}`;
|
|
30
|
+
}
|
|
31
|
+
function view(r) {
|
|
32
|
+
return {
|
|
33
|
+
type: r.type,
|
|
34
|
+
name: r.name,
|
|
35
|
+
content: r.content,
|
|
36
|
+
...(r.type === "MX" ? { priority: r.priority } : {}),
|
|
37
|
+
ttl: r.ttl === 1 ? "auto" : r.ttl,
|
|
38
|
+
proxied: r.proxied ?? false,
|
|
39
|
+
...(r.comment ? { comment: r.comment } : {}),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
export const cloudflareGroup = {
|
|
43
|
+
id: "cloudflare",
|
|
44
|
+
name: "Cloudflare DNS",
|
|
45
|
+
host: "api.cloudflare.com",
|
|
46
|
+
keys: ["CLOUDFLARE_API_TOKEN"],
|
|
47
|
+
setup: "Add the Cloudflare API token to the agent's secrets in Infisical, then restart the agent.",
|
|
48
|
+
guidance,
|
|
49
|
+
connect(ctx) {
|
|
50
|
+
return holdsAll(ctx, this.keys) ? new HttpCloudflareDns(ctx.get("CLOUDFLARE_API_TOKEN")) : undefined;
|
|
51
|
+
},
|
|
52
|
+
tools(cf, shared) {
|
|
53
|
+
const { policy } = shared;
|
|
54
|
+
const { operator, agent } = policy;
|
|
55
|
+
const comment = `Created by ${agent}, approved by ${operator}`;
|
|
56
|
+
async function zoneFor(dom) {
|
|
57
|
+
const zone = await cf.findZone(dom);
|
|
58
|
+
if (!zone) {
|
|
59
|
+
return `Not done: Cloudflare has no zone for ${dom} that ${agent}'s token can see. Give ${operator} the records to add by hand.`;
|
|
60
|
+
}
|
|
61
|
+
return zone;
|
|
62
|
+
}
|
|
63
|
+
const listRecords = tool(({ domain: dom, type }) => result(async () => {
|
|
64
|
+
const zone = await zoneFor(dom);
|
|
65
|
+
if (typeof zone === "string")
|
|
66
|
+
return zone;
|
|
67
|
+
const records = await cf.listRecords(zone.id);
|
|
68
|
+
return records
|
|
69
|
+
.filter((r) => r.name === dom || r.name.endsWith(`.${dom}`))
|
|
70
|
+
.filter((r) => !type || r.type === type)
|
|
71
|
+
.map(view);
|
|
72
|
+
}), {
|
|
73
|
+
name: "cloudflare_list_dns_records",
|
|
74
|
+
description: "List the DNS records of a domain in Cloudflare, for example to check the MX, SPF and DKIM records.",
|
|
75
|
+
schema: z.object({
|
|
76
|
+
domain,
|
|
77
|
+
type: z.enum(["A", "AAAA", "CNAME", "MX", "TXT", "CAA", "SRV", "NS"]).optional().describe("Only this record type"),
|
|
78
|
+
}),
|
|
79
|
+
});
|
|
80
|
+
const createRecord = approvedTool({
|
|
81
|
+
name: "cloudflare_create_dns_record",
|
|
82
|
+
operator,
|
|
83
|
+
description: `Create one MX or TXT record in Cloudflare DNS, for example a record that MXroute needs. It cannot change or delete a record, and it makes no other record types. ${operator} must approve this action.`,
|
|
84
|
+
schema: z
|
|
85
|
+
.object({
|
|
86
|
+
domain,
|
|
87
|
+
type: z.enum(RECORD_TYPES).describe("MX or TXT"),
|
|
88
|
+
name: recordName,
|
|
89
|
+
content: z.string().min(1).max(2048).describe("For MX the mail server host name; for TXT the text, with no quotes"),
|
|
90
|
+
priority: z.number().int().min(0).max(65535).optional().describe("MX only: the priority, for example 10"),
|
|
91
|
+
})
|
|
92
|
+
.refine((a) => (a.type === "MX") === (a.priority !== undefined), {
|
|
93
|
+
message: "give a priority for an MX record, and no priority for a TXT record",
|
|
94
|
+
path: ["priority"],
|
|
95
|
+
}),
|
|
96
|
+
prepare: async (args) => {
|
|
97
|
+
const zone = await zoneFor(args.domain);
|
|
98
|
+
if (typeof zone === "string")
|
|
99
|
+
return { summary: "", early: zone };
|
|
100
|
+
const name = fqdn(args.name, args.domain);
|
|
101
|
+
const atName = (await cf.listRecords(zone.id)).filter((r) => r.name === name);
|
|
102
|
+
const twin = atName.find((r) => r.type === args.type &&
|
|
103
|
+
same(args.type, r.content, args.content) &&
|
|
104
|
+
(args.type !== "MX" || r.priority === args.priority));
|
|
105
|
+
if (twin)
|
|
106
|
+
return { summary: "", early: `Already in Cloudflare: ${show(twin)}. Nothing to do.` };
|
|
107
|
+
const cname = atName.find((r) => r.type === "CNAME");
|
|
108
|
+
if (cname) {
|
|
109
|
+
return {
|
|
110
|
+
summary: "",
|
|
111
|
+
early: `Not done: ${name} is a CNAME (${cname.content}). No other record can be at that name. Tell ${operator}.`,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
const spf = isSpf(args) ? atName.find(isSpf) : undefined;
|
|
115
|
+
if (spf) {
|
|
116
|
+
return {
|
|
117
|
+
summary: "",
|
|
118
|
+
early: `Not done: ${name} already has an SPF record: ${spf.content}. A name must have one SPF record only, and you cannot change records. Give ${operator} both values to merge by hand.`,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
const now = atName.length ? atName.map(show).join("; ") : "none";
|
|
122
|
+
return {
|
|
123
|
+
summary: `Create in Cloudflare (zone ${zone.name}): ${show({ ...args, name })}. Records at that name now: ${now}`,
|
|
124
|
+
data: { zoneId: zone.id, name },
|
|
125
|
+
};
|
|
126
|
+
},
|
|
127
|
+
run: async (args, data) => {
|
|
128
|
+
const created = await cf.createRecord(data.zoneId, {
|
|
129
|
+
type: args.type,
|
|
130
|
+
name: data.name,
|
|
131
|
+
content: args.content,
|
|
132
|
+
...(args.type === "MX" ? { priority: args.priority } : {}),
|
|
133
|
+
comment,
|
|
134
|
+
});
|
|
135
|
+
return `Created ${show(created)}.`;
|
|
136
|
+
},
|
|
137
|
+
});
|
|
138
|
+
return [listRecords, createRecord];
|
|
139
|
+
},
|
|
140
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { ServiceError } from "../../errors.js";
|
|
2
|
+
import type { SecretPlace, SecretStore } from "../../secret-store.js";
|
|
3
|
+
export declare class InfisicalError extends ServiceError {
|
|
4
|
+
readonly status: number;
|
|
5
|
+
constructor(status: number, message: string);
|
|
6
|
+
}
|
|
7
|
+
export interface IdentityAuth {
|
|
8
|
+
clientId: string;
|
|
9
|
+
clientSecret: string;
|
|
10
|
+
}
|
|
11
|
+
export interface TokenAuth {
|
|
12
|
+
token: string;
|
|
13
|
+
}
|
|
14
|
+
export declare class HttpInfisical implements SecretStore {
|
|
15
|
+
#private;
|
|
16
|
+
constructor(auth: IdentityAuth | TokenAuth, opts: {
|
|
17
|
+
baseUrl: string;
|
|
18
|
+
projectId: string;
|
|
19
|
+
env: string | ((path: string) => string);
|
|
20
|
+
fetch?: typeof fetch;
|
|
21
|
+
});
|
|
22
|
+
readAll(path: string): Promise<Record<string, string>>;
|
|
23
|
+
listFolders(path: string): Promise<string[]>;
|
|
24
|
+
listSecretNames(path: string): Promise<string[]>;
|
|
25
|
+
check(place: SecretPlace): Promise<{
|
|
26
|
+
folderExists: boolean;
|
|
27
|
+
exists: boolean;
|
|
28
|
+
}>;
|
|
29
|
+
put(place: SecretPlace, value: string): Promise<void>;
|
|
30
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// Client for the Infisical API, with a machine identity (Universal Auth).
|
|
2
|
+
import { ServiceError } from "../../errors.js";
|
|
3
|
+
export class InfisicalError extends ServiceError {
|
|
4
|
+
status;
|
|
5
|
+
constructor(status, message) {
|
|
6
|
+
super(`Infisical ${status}: ${message}`);
|
|
7
|
+
this.name = "InfisicalError";
|
|
8
|
+
this.status = status;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export class HttpInfisical {
|
|
12
|
+
#auth;
|
|
13
|
+
#baseUrl;
|
|
14
|
+
#projectId;
|
|
15
|
+
#env;
|
|
16
|
+
#fetch;
|
|
17
|
+
#token;
|
|
18
|
+
constructor(auth,
|
|
19
|
+
// One environment, or one for each folder path.
|
|
20
|
+
opts) {
|
|
21
|
+
this.#auth = auth;
|
|
22
|
+
this.#baseUrl = opts.baseUrl.replace(/\/$/, "");
|
|
23
|
+
this.#projectId = opts.projectId;
|
|
24
|
+
const env = opts.env;
|
|
25
|
+
this.#env = typeof env === "string" ? () => env : env;
|
|
26
|
+
this.#fetch = opts.fetch ?? fetch;
|
|
27
|
+
}
|
|
28
|
+
async #login() {
|
|
29
|
+
if ("token" in this.#auth)
|
|
30
|
+
return this.#auth.token;
|
|
31
|
+
if (this.#token && Date.now() < this.#token.until)
|
|
32
|
+
return this.#token.value;
|
|
33
|
+
const res = await this.#fetch(`${this.#baseUrl}/api/v1/auth/universal-auth/login`, {
|
|
34
|
+
method: "POST",
|
|
35
|
+
headers: { "Content-Type": "application/json", Accept: "application/json" },
|
|
36
|
+
body: JSON.stringify({ clientId: this.#auth.clientId, clientSecret: this.#auth.clientSecret }),
|
|
37
|
+
});
|
|
38
|
+
const json = (await res.json().catch(() => ({})));
|
|
39
|
+
if (!res.ok || !json.accessToken) {
|
|
40
|
+
throw new InfisicalError(res.status, `login failed: ${json.message ?? res.statusText}`);
|
|
41
|
+
}
|
|
42
|
+
// Renew one minute early.
|
|
43
|
+
this.#token = { value: json.accessToken, until: Date.now() + ((json.expiresIn ?? 600) - 60) * 1000 };
|
|
44
|
+
return json.accessToken;
|
|
45
|
+
}
|
|
46
|
+
async #request(method, path, body, retry = true) {
|
|
47
|
+
const token = await this.#login();
|
|
48
|
+
const res = await this.#fetch(`${this.#baseUrl}${path}`, {
|
|
49
|
+
method,
|
|
50
|
+
headers: {
|
|
51
|
+
Authorization: `Bearer ${token}`,
|
|
52
|
+
Accept: "application/json",
|
|
53
|
+
...(body === undefined ? {} : { "Content-Type": "application/json" }),
|
|
54
|
+
},
|
|
55
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
56
|
+
});
|
|
57
|
+
if (res.status === 401 && retry && !("token" in this.#auth)) {
|
|
58
|
+
this.#token = undefined;
|
|
59
|
+
return this.#request(method, path, body, false);
|
|
60
|
+
}
|
|
61
|
+
const text = await res.text();
|
|
62
|
+
let json;
|
|
63
|
+
try {
|
|
64
|
+
json = text ? JSON.parse(text) : {};
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
throw new InfisicalError(res.status, `not JSON: ${text.slice(0, 200)}`);
|
|
68
|
+
}
|
|
69
|
+
if (!res.ok) {
|
|
70
|
+
throw new InfisicalError(res.status, json.message ?? res.statusText);
|
|
71
|
+
}
|
|
72
|
+
return json;
|
|
73
|
+
}
|
|
74
|
+
#q(path, params) {
|
|
75
|
+
return new URLSearchParams({ workspaceId: this.#projectId, environment: this.#env(path), ...params }).toString();
|
|
76
|
+
}
|
|
77
|
+
// Bootstrap only: the agent's own keys. Never reachable from a tool.
|
|
78
|
+
async readAll(path) {
|
|
79
|
+
const { secrets } = await this.#request("GET", `/api/v3/secrets/raw?${this.#q(path, { secretPath: path })}`);
|
|
80
|
+
return Object.fromEntries(secrets.map((s) => [s.secretKey, s.secretValue]));
|
|
81
|
+
}
|
|
82
|
+
async listFolders(path) {
|
|
83
|
+
return this.#folders(path, path);
|
|
84
|
+
}
|
|
85
|
+
// The folders in `path`, in the environment of `envPath`. To check that
|
|
86
|
+
// /agents exists, list / in the environment of /agents.
|
|
87
|
+
async #folders(path, envPath) {
|
|
88
|
+
const { folders } = await this.#request("GET", `/api/v1/folders?${this.#q(envPath, { path })}`);
|
|
89
|
+
return folders.map((f) => f.name).sort();
|
|
90
|
+
}
|
|
91
|
+
async listSecretNames(path) {
|
|
92
|
+
const { secrets } = await this.#request("GET", `/api/v3/secrets/raw?${this.#q(path, { secretPath: path, viewSecretValue: "false" })}`);
|
|
93
|
+
return secrets.map((s) => s.secretKey).sort();
|
|
94
|
+
}
|
|
95
|
+
async #folderExists(path) {
|
|
96
|
+
if (path === "/")
|
|
97
|
+
return true;
|
|
98
|
+
const parent = path.slice(0, path.lastIndexOf("/")) || "/";
|
|
99
|
+
const name = path.slice(path.lastIndexOf("/") + 1);
|
|
100
|
+
try {
|
|
101
|
+
return (await this.#folders(parent, path)).includes(name);
|
|
102
|
+
}
|
|
103
|
+
catch (err) {
|
|
104
|
+
if (err instanceof InfisicalError && err.status === 404)
|
|
105
|
+
return false;
|
|
106
|
+
throw err;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
async check(place) {
|
|
110
|
+
const folderExists = await this.#folderExists(place.path);
|
|
111
|
+
const exists = folderExists && (await this.listSecretNames(place.path)).includes(place.name);
|
|
112
|
+
return { folderExists, exists };
|
|
113
|
+
}
|
|
114
|
+
async put(place, value) {
|
|
115
|
+
let at = "";
|
|
116
|
+
for (const part of place.path.split("/").filter(Boolean)) {
|
|
117
|
+
const parent = at || "/";
|
|
118
|
+
at = `${at}/${part}`;
|
|
119
|
+
if (!(await this.#folderExists(at))) {
|
|
120
|
+
await this.#request("POST", "/api/v1/folders", {
|
|
121
|
+
workspaceId: this.#projectId,
|
|
122
|
+
environment: this.#env(at),
|
|
123
|
+
name: part,
|
|
124
|
+
path: parent,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
const body = { workspaceId: this.#projectId, environment: this.#env(place.path), secretPath: place.path, secretValue: value, type: "shared" };
|
|
129
|
+
const url = `/api/v3/secrets/raw/${encodeURIComponent(place.name)}`;
|
|
130
|
+
const exists = (await this.listSecretNames(place.path)).includes(place.name);
|
|
131
|
+
await this.#request(exists ? "PATCH" : "POST", url, body);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# Infisical
|
|
2
|
+
|
|
3
|
+
You can list folders and secret names; you never see a secret value. You can store a new secret in the folders your policy allows, and make a random secret. Each write needs approval, and the approval says when it replaces a value or makes a folder.
|
|
4
|
+
|
|
5
|
+
- When {{operator}} wants a new key for an agent, make the key with `storeAt` set to that agent's folder and the name the agent uses, for example /agents/lyra and OPENROUTER_API_KEY. List the folder first.
|
|
6
|
+
- If a secret with that name exists, the approval says that the store replaces it. Say so to {{operator}} before you call the tool.
|
|
7
|
+
- Without `storeAt`, a new secret goes to {{operator}}'s terminal.
|
|
8
|
+
- If {{operator}} pastes a key or a password into the chat, do not repeat it and do not store it. Tell {{operator}} to add it in Infisical directly.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { SecretPlace, SecretStore } from "../../secret-store.js";
|
|
2
|
+
export declare class MemoryInfisical implements SecretStore {
|
|
3
|
+
#private;
|
|
4
|
+
readonly folders: Map<string, Map<string, string>>;
|
|
5
|
+
readonly writes: {
|
|
6
|
+
op: string;
|
|
7
|
+
path: string;
|
|
8
|
+
name?: string;
|
|
9
|
+
}[];
|
|
10
|
+
constructor(seed?: Record<string, Record<string, string>>);
|
|
11
|
+
listFolders(path: string): Promise<string[]>;
|
|
12
|
+
listSecretNames(path: string): Promise<string[]>;
|
|
13
|
+
check(place: SecretPlace): Promise<{
|
|
14
|
+
folderExists: boolean;
|
|
15
|
+
exists: boolean;
|
|
16
|
+
}>;
|
|
17
|
+
put(place: SecretPlace, value: string): Promise<void>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// In-memory Infisical for tests and evals. Folder path -> secret name -> value.
|
|
2
|
+
export class MemoryInfisical {
|
|
3
|
+
folders = new Map([["/", new Map()]]);
|
|
4
|
+
writes = [];
|
|
5
|
+
constructor(seed = {}) {
|
|
6
|
+
for (const [path, secrets] of Object.entries(seed)) {
|
|
7
|
+
this.#mkdirs(path);
|
|
8
|
+
for (const [k, v] of Object.entries(secrets))
|
|
9
|
+
this.folders.get(path).set(k, v);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
#mkdirs(path, record = false) {
|
|
13
|
+
let at = "";
|
|
14
|
+
for (const part of path.split("/").filter(Boolean)) {
|
|
15
|
+
at = `${at}/${part}`;
|
|
16
|
+
if (!this.folders.has(at)) {
|
|
17
|
+
this.folders.set(at, new Map());
|
|
18
|
+
if (record)
|
|
19
|
+
this.writes.push({ op: "createFolder", path: at });
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
async listFolders(path) {
|
|
24
|
+
const prefix = path === "/" ? "/" : `${path}/`;
|
|
25
|
+
return [...this.folders.keys()]
|
|
26
|
+
.filter((p) => p.startsWith(prefix) && p !== path && !p.slice(prefix.length).includes("/"))
|
|
27
|
+
.map((p) => p.slice(prefix.length))
|
|
28
|
+
.sort();
|
|
29
|
+
}
|
|
30
|
+
async listSecretNames(path) {
|
|
31
|
+
return [...(this.folders.get(path)?.keys() ?? [])].sort();
|
|
32
|
+
}
|
|
33
|
+
async check(place) {
|
|
34
|
+
const folder = this.folders.get(place.path);
|
|
35
|
+
return { folderExists: !!folder, exists: !!folder?.has(place.name) };
|
|
36
|
+
}
|
|
37
|
+
async put(place, value) {
|
|
38
|
+
this.#mkdirs(place.path, true);
|
|
39
|
+
const folder = this.folders.get(place.path);
|
|
40
|
+
this.writes.push({ op: folder.has(place.name) ? "updateSecret" : "createSecret", path: place.path, name: place.name });
|
|
41
|
+
folder.set(place.name, value);
|
|
42
|
+
}
|
|
43
|
+
}
|