@app-vitals/agent-admin 1.41.0 → 1.43.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/package.json +2 -2
- package/prisma/client/internal/class.ts +3 -3
- package/prisma/client/internal/prismaNamespace.ts +1 -0
- package/prisma/client/internal/prismaNamespaceBrowser.ts +1 -0
- package/prisma/client/models/AgentEnv.ts +36 -1
- package/prisma/migrations/20261114000000_add_agent_env_enabled/migration.sql +3 -0
- package/prisma/schema/schema.prisma +1 -0
- package/src/admin-boot.ts +7 -5
- package/src/admin-ui-github-enterprise-pages.ts +1 -1
- package/src/admin-ui-templates-pages.ts +155 -0
- package/src/admin-ui-templates-pages.unit.test.ts +54 -0
- package/src/admin-ui-templates.smoke.test.ts +292 -0
- package/src/admin-ui-templates.ts +316 -0
- package/src/admin-ui.ts +24 -0
- package/src/agent-cron-jobs.integration.test.ts +77 -0
- package/src/agent-cron-jobs.ts +66 -10
- package/src/agent-deletion.ts +2 -2
- package/src/agent-envs.integration.test.ts +108 -0
- package/src/agent-envs.ts +95 -19
- package/src/agent-plugins.integration.test.ts +52 -0
- package/src/agent-plugins.ts +51 -4
- package/src/agent-tools.integration.test.ts +55 -0
- package/src/agent-tools.ts +35 -4
- package/src/agents-api.smoke.test.ts +1 -0
- package/src/agents.ts +1 -1
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/admin-ui-templates.smoke.test.ts
|
|
3
|
+
* Smoke tests for /admin/settings/templates (ATP-2.3) via app.request(), over
|
|
4
|
+
* a stateful in-memory template service that reuses the real item validation
|
|
5
|
+
* (denylist, payload shapes) and secret masking. No DB, no network.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { describe, expect, it } from "bun:test";
|
|
9
|
+
import type { MiddlewareHandler } from "hono";
|
|
10
|
+
import { Hono } from "hono";
|
|
11
|
+
import type { AdminUIEnv } from "./admin-ui.ts";
|
|
12
|
+
import { registerAdminTemplatesRoutes } from "./admin-ui-templates.ts";
|
|
13
|
+
import {
|
|
14
|
+
type AgentTemplateService,
|
|
15
|
+
normalizeTemplateItem,
|
|
16
|
+
SECRET_MASK,
|
|
17
|
+
type TemplateActor,
|
|
18
|
+
type TemplateItemInput,
|
|
19
|
+
type TemplateView,
|
|
20
|
+
} from "./agent-templates.ts";
|
|
21
|
+
import type { CallerScope } from "./caller-scope.ts";
|
|
22
|
+
import { NotFoundError, UnprocessableEntityError } from "./errors.ts";
|
|
23
|
+
|
|
24
|
+
const now = new Date("2026-11-14T00:00:00Z");
|
|
25
|
+
const SPECIALTIES = ["specialist", "reviewer"];
|
|
26
|
+
|
|
27
|
+
function fakeService() {
|
|
28
|
+
const rows: TemplateView[] = [];
|
|
29
|
+
const secrets = new Map<string, string>();
|
|
30
|
+
let seq = 0;
|
|
31
|
+
const mask = (t: TemplateView): TemplateView => t;
|
|
32
|
+
const apply = (t: TemplateView, raw: TemplateItemInput) => {
|
|
33
|
+
const item = normalizeTemplateItem(raw);
|
|
34
|
+
const payload = { ...item.payload };
|
|
35
|
+
if (item.kind === "env" && payload.secret === true) {
|
|
36
|
+
secrets.set(`${t.id}:${item.key}`, String(payload.value));
|
|
37
|
+
payload.value = SECRET_MASK;
|
|
38
|
+
}
|
|
39
|
+
t.items = [
|
|
40
|
+
...t.items.filter((i) => !(i.kind === item.kind && i.key === item.key)),
|
|
41
|
+
{ kind: item.kind, key: item.key, payload, updatedAt: now },
|
|
42
|
+
];
|
|
43
|
+
};
|
|
44
|
+
const visible = (a: TemplateActor, t: TemplateView) =>
|
|
45
|
+
a.kind === "admin" || t.accountId === a.accountId;
|
|
46
|
+
const service: Pick<
|
|
47
|
+
AgentTemplateService,
|
|
48
|
+
"list" | "replace" | "putItem" | "deleteItem" | "delete"
|
|
49
|
+
> = {
|
|
50
|
+
async list(actor, f = {}) {
|
|
51
|
+
return rows
|
|
52
|
+
.filter((t) => visible(actor, t))
|
|
53
|
+
.filter((t) => f.accountId === undefined || t.accountId === f.accountId)
|
|
54
|
+
.filter((t) => f.specialty === undefined || t.specialty === f.specialty)
|
|
55
|
+
.map(mask);
|
|
56
|
+
},
|
|
57
|
+
async replace(actor, scope, items) {
|
|
58
|
+
const accountId =
|
|
59
|
+
actor.kind === "account" ? actor.accountId : (scope.accountId ?? null);
|
|
60
|
+
const specialty = scope.specialty ?? null;
|
|
61
|
+
if (specialty !== null && !SPECIALTIES.includes(specialty)) {
|
|
62
|
+
throw new UnprocessableEntityError(`unknown specialty ${specialty}`);
|
|
63
|
+
}
|
|
64
|
+
const t: TemplateView = {
|
|
65
|
+
id: `t${++seq}`,
|
|
66
|
+
accountId,
|
|
67
|
+
specialty,
|
|
68
|
+
createdAt: now,
|
|
69
|
+
updatedAt: now,
|
|
70
|
+
items: [],
|
|
71
|
+
};
|
|
72
|
+
for (const i of items) apply(t, i);
|
|
73
|
+
rows.push(t);
|
|
74
|
+
return { template: t, created: true };
|
|
75
|
+
},
|
|
76
|
+
async putItem(actor, id, input) {
|
|
77
|
+
const t = rows.find((r) => r.id === id && visible(actor, r));
|
|
78
|
+
if (!t) throw new NotFoundError("nope");
|
|
79
|
+
apply(t, input);
|
|
80
|
+
return t;
|
|
81
|
+
},
|
|
82
|
+
async deleteItem(actor, id, kind, key) {
|
|
83
|
+
const t = rows.find((r) => r.id === id && visible(actor, r));
|
|
84
|
+
if (!t) throw new NotFoundError("nope");
|
|
85
|
+
t.items = t.items.filter((i) => !(i.kind === kind && i.key === key));
|
|
86
|
+
},
|
|
87
|
+
async delete(actor, id) {
|
|
88
|
+
const i = rows.findIndex((r) => r.id === id && visible(actor, r));
|
|
89
|
+
if (i < 0) throw new NotFoundError("nope");
|
|
90
|
+
rows.splice(i, 1);
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
return { service, rows, secrets };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function setup(
|
|
97
|
+
opts: { isAdmin?: boolean; scope?: CallerScope } = {},
|
|
98
|
+
): { app: Hono<AdminUIEnv> } & ReturnType<typeof fakeService> {
|
|
99
|
+
const svc = fakeService();
|
|
100
|
+
const app = new Hono<AdminUIEnv>();
|
|
101
|
+
const requireAuth = (async (c, next) => {
|
|
102
|
+
c.set("userEmail", "u@x.com");
|
|
103
|
+
c.set("isAdmin", opts.isAdmin ?? true);
|
|
104
|
+
await next();
|
|
105
|
+
}) as MiddlewareHandler<AdminUIEnv>;
|
|
106
|
+
registerAdminTemplatesRoutes(app, {
|
|
107
|
+
requireAuth,
|
|
108
|
+
templateService: svc.service,
|
|
109
|
+
callerScopeResolver: async () =>
|
|
110
|
+
opts.scope ?? { kind: "scoped", accountId: null, agentIds: [] },
|
|
111
|
+
accounts: {
|
|
112
|
+
listWithCounts: async () =>
|
|
113
|
+
[
|
|
114
|
+
{ id: "acct-1", name: "Acme" },
|
|
115
|
+
{ id: "acct-2", name: "Globex" },
|
|
116
|
+
] as never,
|
|
117
|
+
getWithCounts: async (id: string) =>
|
|
118
|
+
({ id, name: id === "acct-1" ? "Acme" : "Globex" }) as never,
|
|
119
|
+
},
|
|
120
|
+
specialtyRegistry: {
|
|
121
|
+
listTypes: () =>
|
|
122
|
+
SPECIALTIES.map((name) => ({ name, displayName: `Type ${name}` })),
|
|
123
|
+
},
|
|
124
|
+
html: (content, o) =>
|
|
125
|
+
new Response(content, {
|
|
126
|
+
status: o?.status ?? 200,
|
|
127
|
+
headers: { "Content-Type": "text/html" },
|
|
128
|
+
}),
|
|
129
|
+
});
|
|
130
|
+
return { app, ...svc };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const BASE = "/admin/settings/templates";
|
|
134
|
+
const form = (fields: Record<string, string>) => ({
|
|
135
|
+
method: "POST",
|
|
136
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
137
|
+
body: new URLSearchParams(fields).toString(),
|
|
138
|
+
});
|
|
139
|
+
const member: CallerScope = {
|
|
140
|
+
kind: "scoped",
|
|
141
|
+
accountId: "acct-1",
|
|
142
|
+
agentIds: [],
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
describe("/admin/settings/templates smoke", () => {
|
|
146
|
+
it("renders the page with the scope picker for an admin", async () => {
|
|
147
|
+
const { app } = setup();
|
|
148
|
+
const res = await app.request(BASE);
|
|
149
|
+
expect(res.status).toBe(200);
|
|
150
|
+
const html = await res.text();
|
|
151
|
+
expect(html).toContain("Deployment-wide");
|
|
152
|
+
expect(html).toContain("Acme");
|
|
153
|
+
expect(html).toContain("Globex");
|
|
154
|
+
expect(html).toContain("All agent types");
|
|
155
|
+
expect(html).toContain("Type reviewer");
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it("creates a deployment-wide secret env var and shows it masked", async () => {
|
|
159
|
+
const { app, rows, secrets } = setup();
|
|
160
|
+
const res = await app.request(
|
|
161
|
+
`${BASE}/items`,
|
|
162
|
+
form({
|
|
163
|
+
accountId: "",
|
|
164
|
+
specialty: "",
|
|
165
|
+
kind: "env",
|
|
166
|
+
key: "API_KEY",
|
|
167
|
+
value: "s3cr3t",
|
|
168
|
+
secret: "1",
|
|
169
|
+
}),
|
|
170
|
+
);
|
|
171
|
+
expect(res.status).toBe(302);
|
|
172
|
+
expect(rows[0]).toMatchObject({ accountId: null, specialty: null });
|
|
173
|
+
expect(secrets.get("t1:API_KEY")).toBe("s3cr3t");
|
|
174
|
+
const page = await app.request(res.headers.get("location") as string);
|
|
175
|
+
const html = await page.text();
|
|
176
|
+
expect(html).toContain("API_KEY");
|
|
177
|
+
expect(html).toContain('value="***"');
|
|
178
|
+
expect(html).not.toContain("s3cr3t");
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it("updates, then removes an item, then deletes the template", async () => {
|
|
182
|
+
const { app, rows } = setup();
|
|
183
|
+
const scope = { accountId: "acct-2", specialty: "reviewer" };
|
|
184
|
+
await app.request(
|
|
185
|
+
`${BASE}/items`,
|
|
186
|
+
form({ ...scope, kind: "tool", key: "Bash", enabled: "1" }),
|
|
187
|
+
);
|
|
188
|
+
await app.request(
|
|
189
|
+
`${BASE}/items`,
|
|
190
|
+
form({ ...scope, kind: "plugin", key: "p@m", version: "1.2" }),
|
|
191
|
+
);
|
|
192
|
+
expect(rows).toHaveLength(1);
|
|
193
|
+
expect(rows[0]?.items.map((i) => i.key).sort()).toEqual(["Bash", "p@m"]);
|
|
194
|
+
const rm = await app.request(
|
|
195
|
+
`${BASE}/items/delete`,
|
|
196
|
+
form({ ...scope, kind: "tool", key: "Bash" }),
|
|
197
|
+
);
|
|
198
|
+
expect(rm.status).toBe(302);
|
|
199
|
+
expect(rows[0]?.items.map((i) => i.key)).toEqual(["p@m"]);
|
|
200
|
+
const del = await app.request(`${BASE}/delete`, form(scope));
|
|
201
|
+
expect(del.status).toBe(302);
|
|
202
|
+
expect(rows).toHaveLength(0);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it("creates a cron item", async () => {
|
|
206
|
+
const { app, rows } = setup();
|
|
207
|
+
await app.request(
|
|
208
|
+
`${BASE}/items`,
|
|
209
|
+
form({
|
|
210
|
+
accountId: "",
|
|
211
|
+
specialty: "",
|
|
212
|
+
kind: "cron",
|
|
213
|
+
key: "digest",
|
|
214
|
+
schedule: "0 9 * * *",
|
|
215
|
+
prompt: "Summarize",
|
|
216
|
+
enabled: "1",
|
|
217
|
+
}),
|
|
218
|
+
);
|
|
219
|
+
expect(rows[0]?.items[0]).toMatchObject({
|
|
220
|
+
kind: "cron",
|
|
221
|
+
key: "digest",
|
|
222
|
+
payload: { schedule: "0 9 * * *", prompt: "Summarize", enabled: true },
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
it("shows a denylisted env key error inline", async () => {
|
|
227
|
+
const { app, rows } = setup();
|
|
228
|
+
const res = await app.request(
|
|
229
|
+
`${BASE}/items`,
|
|
230
|
+
form({
|
|
231
|
+
accountId: "",
|
|
232
|
+
specialty: "",
|
|
233
|
+
kind: "env",
|
|
234
|
+
key: "SLACK_BOT_TOKEN",
|
|
235
|
+
value: "x",
|
|
236
|
+
}),
|
|
237
|
+
);
|
|
238
|
+
expect(res.status).toBe(422);
|
|
239
|
+
const html = await res.text();
|
|
240
|
+
expect(html).toContain("alert-error");
|
|
241
|
+
expect(html).toContain("per-agent credential");
|
|
242
|
+
expect(rows).toHaveLength(0);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it("shows an unknown specialty error inline", async () => {
|
|
246
|
+
const { app } = setup();
|
|
247
|
+
const res = await app.request(
|
|
248
|
+
`${BASE}/items`,
|
|
249
|
+
form({
|
|
250
|
+
accountId: "",
|
|
251
|
+
specialty: "ghost",
|
|
252
|
+
kind: "tool",
|
|
253
|
+
key: "Bash",
|
|
254
|
+
}),
|
|
255
|
+
);
|
|
256
|
+
expect(res.status).toBe(422);
|
|
257
|
+
expect(await res.text()).toContain("unknown specialty ghost");
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it("offers an account member only their own account", async () => {
|
|
261
|
+
const { app } = setup({ isAdmin: false, scope: member });
|
|
262
|
+
const html = await (await app.request(BASE)).text();
|
|
263
|
+
expect(html).toContain("Acme");
|
|
264
|
+
expect(html).not.toContain("Globex");
|
|
265
|
+
expect(html).not.toContain("Deployment-wide");
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
it("pins member writes to their account and rejects other accounts", async () => {
|
|
269
|
+
const { app, rows } = setup({ isAdmin: false, scope: member });
|
|
270
|
+
const ok = await app.request(
|
|
271
|
+
`${BASE}/items`,
|
|
272
|
+
form({ accountId: "", specialty: "", kind: "tool", key: "Bash" }),
|
|
273
|
+
);
|
|
274
|
+
expect(ok.status).toBe(302);
|
|
275
|
+
expect(rows[0]?.accountId).toBe("acct-1");
|
|
276
|
+
const other = await app.request(
|
|
277
|
+
`${BASE}/items`,
|
|
278
|
+
form({ accountId: "acct-2", specialty: "", kind: "tool", key: "Bash" }),
|
|
279
|
+
);
|
|
280
|
+
expect(other.status).toBe(403);
|
|
281
|
+
expect((await app.request(`${BASE}?accountId=acct-2`)).status).toBe(403);
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
it("403s a non-admin without an account", async () => {
|
|
285
|
+
const { app } = setup({ isAdmin: false });
|
|
286
|
+
expect((await app.request(BASE)).status).toBe(403);
|
|
287
|
+
expect(
|
|
288
|
+
(await app.request(`${BASE}/items`, form({ kind: "tool", key: "x" })))
|
|
289
|
+
.status,
|
|
290
|
+
).toBe(403);
|
|
291
|
+
});
|
|
292
|
+
});
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/admin-ui-templates.ts
|
|
3
|
+
* GET/POST /admin/settings/templates — the Settings > Templates editor
|
|
4
|
+
* (ATP-2.3). A thin UI over AgentTemplateService (the same service behind the
|
|
5
|
+
* ATP-1.3 /templates API), called directly with the same actor rules: platform
|
|
6
|
+
* admins edit every scope (including deployment-wide); an account member sees
|
|
7
|
+
* and edits only their own account's scopes; everyone else gets 403. Service
|
|
8
|
+
* errors (denylisted env key, unknown specialty, bad payload) are shown inline.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { Context, Hono, MiddlewareHandler } from "hono";
|
|
12
|
+
import { accountIdFromScope } from "./account-agent-create.ts";
|
|
13
|
+
import type { AccountService } from "./accounts.ts";
|
|
14
|
+
import type { AdminUIEnv } from "./admin-ui.ts";
|
|
15
|
+
import {
|
|
16
|
+
ADMIN_TEMPLATES_PATH as BASE,
|
|
17
|
+
renderTemplatesPage,
|
|
18
|
+
type TemplateItemRow,
|
|
19
|
+
type TemplateScopeOption,
|
|
20
|
+
} from "./admin-ui-templates-pages.ts";
|
|
21
|
+
import {
|
|
22
|
+
AGENT_TEMPLATE_ITEM_KINDS,
|
|
23
|
+
type AgentTemplateItemKind,
|
|
24
|
+
} from "./agent-template-items.ts";
|
|
25
|
+
import type {
|
|
26
|
+
AgentTemplateService,
|
|
27
|
+
TemplateActor,
|
|
28
|
+
TemplateItemInput,
|
|
29
|
+
TemplateView,
|
|
30
|
+
} from "./agent-templates.ts";
|
|
31
|
+
import type { CallerScopeResolver } from "./caller-scope.ts";
|
|
32
|
+
import { ApiError, ForbiddenError } from "./errors.ts";
|
|
33
|
+
import type { SpecialtyTypeResolver } from "./specialty-types.ts";
|
|
34
|
+
|
|
35
|
+
export interface AdminTemplatesRouteDeps {
|
|
36
|
+
requireAuth: MiddlewareHandler<AdminUIEnv>;
|
|
37
|
+
templateService: Pick<
|
|
38
|
+
AgentTemplateService,
|
|
39
|
+
"list" | "replace" | "putItem" | "deleteItem" | "delete"
|
|
40
|
+
>;
|
|
41
|
+
callerScopeResolver: CallerScopeResolver;
|
|
42
|
+
/** Optional: without it admins can only pick deployment-wide. */
|
|
43
|
+
accounts?: Pick<AccountService, "listWithCounts" | "getWithCounts">;
|
|
44
|
+
specialtyRegistry: Pick<SpecialtyTypeResolver, "listTypes">;
|
|
45
|
+
html: (content: string, opts?: { status?: number }) => Response;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const NOTICES: Record<string, string> = {
|
|
49
|
+
saved: "Saved.",
|
|
50
|
+
removed: "Item removed.",
|
|
51
|
+
deleted: "Template deleted.",
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const isKind = (v: string): v is AgentTemplateItemKind =>
|
|
55
|
+
(AGENT_TEMPLATE_ITEM_KINDS as readonly string[]).includes(v);
|
|
56
|
+
|
|
57
|
+
interface Scope {
|
|
58
|
+
/** "" = deployment-wide. */
|
|
59
|
+
accountId: string;
|
|
60
|
+
/** "" = every agent type. */
|
|
61
|
+
specialty: string;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function registerAdminTemplatesRoutes(
|
|
65
|
+
app: Hono<AdminUIEnv>,
|
|
66
|
+
deps: AdminTemplatesRouteDeps,
|
|
67
|
+
): void {
|
|
68
|
+
async function resolveActor(c: Context<AdminUIEnv>): Promise<TemplateActor> {
|
|
69
|
+
if (c.var.isAdmin === true) return { kind: "admin" };
|
|
70
|
+
const accountId = accountIdFromScope(
|
|
71
|
+
await deps.callerScopeResolver(c.var.userEmail, false),
|
|
72
|
+
);
|
|
73
|
+
if (accountId === null) {
|
|
74
|
+
throw new ForbiddenError("Admin or account access required");
|
|
75
|
+
}
|
|
76
|
+
return { kind: "account", accountId };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Account members are pinned to their account; naming another is 403. */
|
|
80
|
+
function resolveScope(
|
|
81
|
+
actor: TemplateActor,
|
|
82
|
+
accountId: string,
|
|
83
|
+
specialty: string,
|
|
84
|
+
): Scope {
|
|
85
|
+
if (actor.kind === "account") {
|
|
86
|
+
if (accountId !== "" && accountId !== actor.accountId) {
|
|
87
|
+
throw new ForbiddenError(
|
|
88
|
+
"cannot manage another account's or deployment-wide templates",
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
return { accountId: actor.accountId, specialty };
|
|
92
|
+
}
|
|
93
|
+
return { accountId, specialty };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async function findTemplate(
|
|
97
|
+
actor: TemplateActor,
|
|
98
|
+
scope: Scope,
|
|
99
|
+
): Promise<TemplateView | undefined> {
|
|
100
|
+
const templates = await deps.templateService.list(actor, {
|
|
101
|
+
...(scope.accountId !== "" ? { accountId: scope.accountId } : {}),
|
|
102
|
+
...(scope.specialty !== "" ? { specialty: scope.specialty } : {}),
|
|
103
|
+
});
|
|
104
|
+
return templates.find(
|
|
105
|
+
(t) =>
|
|
106
|
+
(t.accountId ?? "") === scope.accountId &&
|
|
107
|
+
(t.specialty ?? "") === scope.specialty,
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async function accountOptions(
|
|
112
|
+
actor: TemplateActor,
|
|
113
|
+
): Promise<TemplateScopeOption[]> {
|
|
114
|
+
if (actor.kind === "account") {
|
|
115
|
+
const acct = await deps.accounts?.getWithCounts(actor.accountId);
|
|
116
|
+
return [{ value: actor.accountId, label: acct?.name ?? actor.accountId }];
|
|
117
|
+
}
|
|
118
|
+
const accounts = (await deps.accounts?.listWithCounts()) ?? [];
|
|
119
|
+
return [
|
|
120
|
+
{ value: "", label: "Deployment-wide (all accounts)" },
|
|
121
|
+
...accounts.map((a) => ({ value: a.id, label: a.name })),
|
|
122
|
+
];
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function specialtyOptions(): TemplateScopeOption[] {
|
|
126
|
+
return [
|
|
127
|
+
{ value: "", label: "All agent types" },
|
|
128
|
+
...deps.specialtyRegistry
|
|
129
|
+
.listTypes()
|
|
130
|
+
.map((t) => ({ value: t.name, label: t.displayName })),
|
|
131
|
+
];
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async function renderPage(
|
|
135
|
+
c: Context<AdminUIEnv>,
|
|
136
|
+
actor: TemplateActor,
|
|
137
|
+
scope: Scope,
|
|
138
|
+
flash: { error?: string; notice?: string; status?: number } = {},
|
|
139
|
+
): Promise<Response> {
|
|
140
|
+
const template = await findTemplate(actor, scope);
|
|
141
|
+
return deps.html(
|
|
142
|
+
renderTemplatesPage({
|
|
143
|
+
userEmail: c.var.userEmail,
|
|
144
|
+
accountOptions: await accountOptions(actor),
|
|
145
|
+
specialtyOptions: specialtyOptions(),
|
|
146
|
+
accountId: scope.accountId,
|
|
147
|
+
specialty: scope.specialty,
|
|
148
|
+
items: (template?.items ?? []) as TemplateItemRow[],
|
|
149
|
+
templateExists: template !== undefined,
|
|
150
|
+
...(flash.error ? { error: flash.error } : {}),
|
|
151
|
+
...(flash.notice ? { notice: flash.notice } : {}),
|
|
152
|
+
}),
|
|
153
|
+
{ status: flash.status ?? 200 },
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const field = (form: FormData, name: string) =>
|
|
158
|
+
(form.get(name)?.toString() ?? "").trim();
|
|
159
|
+
const flag = (form: FormData, name: string) => form.get(name) !== null;
|
|
160
|
+
const orNull = (v: string) => (v === "" ? null : v);
|
|
161
|
+
|
|
162
|
+
function itemFromForm(form: FormData): TemplateItemInput {
|
|
163
|
+
const kind = field(form, "kind");
|
|
164
|
+
if (!isKind(kind)) throw new ApiError(422, `unknown item kind ${kind}`);
|
|
165
|
+
const key = field(form, "key");
|
|
166
|
+
switch (kind) {
|
|
167
|
+
case "env":
|
|
168
|
+
return {
|
|
169
|
+
kind,
|
|
170
|
+
key,
|
|
171
|
+
// Not trimmed: a value's whitespace is the user's business.
|
|
172
|
+
payload: {
|
|
173
|
+
value: form.get("value")?.toString() ?? "",
|
|
174
|
+
secret: flag(form, "secret"),
|
|
175
|
+
},
|
|
176
|
+
};
|
|
177
|
+
case "tool":
|
|
178
|
+
return { kind, key, payload: { enabled: flag(form, "enabled") } };
|
|
179
|
+
case "plugin":
|
|
180
|
+
return {
|
|
181
|
+
kind,
|
|
182
|
+
key,
|
|
183
|
+
payload: {
|
|
184
|
+
version: orNull(field(form, "version")),
|
|
185
|
+
enabled: flag(form, "enabled"),
|
|
186
|
+
},
|
|
187
|
+
};
|
|
188
|
+
case "cron":
|
|
189
|
+
return {
|
|
190
|
+
kind,
|
|
191
|
+
key,
|
|
192
|
+
payload: {
|
|
193
|
+
schedule: field(form, "schedule"),
|
|
194
|
+
prompt: field(form, "prompt"),
|
|
195
|
+
channel: orNull(field(form, "channel")),
|
|
196
|
+
user: orNull(field(form, "user")),
|
|
197
|
+
preCheck: orNull(field(form, "preCheck")),
|
|
198
|
+
silent: flag(form, "silent"),
|
|
199
|
+
enabled: flag(form, "enabled"),
|
|
200
|
+
},
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** Runs a mutation; service errors render inline, success redirects. */
|
|
206
|
+
async function mutate(
|
|
207
|
+
c: Context<AdminUIEnv>,
|
|
208
|
+
notice: string,
|
|
209
|
+
run: (actor: TemplateActor, scope: Scope, form: FormData) => Promise<void>,
|
|
210
|
+
): Promise<Response> {
|
|
211
|
+
let actor: TemplateActor;
|
|
212
|
+
try {
|
|
213
|
+
actor = await resolveActor(c);
|
|
214
|
+
} catch {
|
|
215
|
+
return new Response("Forbidden", { status: 403 });
|
|
216
|
+
}
|
|
217
|
+
let form: FormData;
|
|
218
|
+
try {
|
|
219
|
+
form = await c.req.formData();
|
|
220
|
+
} catch {
|
|
221
|
+
form = new FormData();
|
|
222
|
+
}
|
|
223
|
+
const requested = {
|
|
224
|
+
accountId: field(form, "accountId"),
|
|
225
|
+
specialty: field(form, "specialty"),
|
|
226
|
+
};
|
|
227
|
+
let scope: Scope;
|
|
228
|
+
try {
|
|
229
|
+
scope = resolveScope(actor, requested.accountId, requested.specialty);
|
|
230
|
+
} catch {
|
|
231
|
+
return new Response("Forbidden", { status: 403 });
|
|
232
|
+
}
|
|
233
|
+
try {
|
|
234
|
+
await run(actor, scope, form);
|
|
235
|
+
} catch (err) {
|
|
236
|
+
if (err instanceof ApiError && err.statusCode < 500) {
|
|
237
|
+
return renderPage(c, actor, scope, {
|
|
238
|
+
error: err.message,
|
|
239
|
+
status: err.statusCode,
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
throw err;
|
|
243
|
+
}
|
|
244
|
+
const qs = new URLSearchParams({
|
|
245
|
+
...(scope.accountId !== "" ? { accountId: scope.accountId } : {}),
|
|
246
|
+
...(scope.specialty !== "" ? { specialty: scope.specialty } : {}),
|
|
247
|
+
notice,
|
|
248
|
+
});
|
|
249
|
+
return c.redirect(`${BASE}?${qs}`, 302);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
app.get(BASE, deps.requireAuth, async (c) => {
|
|
253
|
+
let actor: TemplateActor;
|
|
254
|
+
try {
|
|
255
|
+
actor = await resolveActor(c);
|
|
256
|
+
} catch {
|
|
257
|
+
return new Response("Forbidden", { status: 403 });
|
|
258
|
+
}
|
|
259
|
+
let scope: Scope;
|
|
260
|
+
try {
|
|
261
|
+
scope = resolveScope(
|
|
262
|
+
actor,
|
|
263
|
+
c.req.query("accountId")?.trim() ?? "",
|
|
264
|
+
c.req.query("specialty")?.trim() ?? "",
|
|
265
|
+
);
|
|
266
|
+
} catch {
|
|
267
|
+
return new Response("Forbidden", { status: 403 });
|
|
268
|
+
}
|
|
269
|
+
const key = c.req.query("notice");
|
|
270
|
+
return renderPage(c, actor, scope, {
|
|
271
|
+
...(key && NOTICES[key] ? { notice: NOTICES[key] } : {}),
|
|
272
|
+
});
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
app.post(`${BASE}/items`, deps.requireAuth, (c) =>
|
|
276
|
+
mutate(c, "saved", async (actor, scope, form) => {
|
|
277
|
+
const item = itemFromForm(form);
|
|
278
|
+
const template = await findTemplate(actor, scope);
|
|
279
|
+
if (template) {
|
|
280
|
+
await deps.templateService.putItem(actor, template.id, item);
|
|
281
|
+
} else {
|
|
282
|
+
await deps.templateService.replace(
|
|
283
|
+
actor,
|
|
284
|
+
{
|
|
285
|
+
accountId: scope.accountId === "" ? null : scope.accountId,
|
|
286
|
+
specialty: scope.specialty === "" ? null : scope.specialty,
|
|
287
|
+
},
|
|
288
|
+
[item],
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
}),
|
|
292
|
+
);
|
|
293
|
+
|
|
294
|
+
app.post(`${BASE}/items/delete`, deps.requireAuth, (c) =>
|
|
295
|
+
mutate(c, "removed", async (actor, scope, form) => {
|
|
296
|
+
const kind = field(form, "kind");
|
|
297
|
+
if (!isKind(kind)) throw new ApiError(422, `unknown item kind ${kind}`);
|
|
298
|
+
const template = await findTemplate(actor, scope);
|
|
299
|
+
if (!template) throw new ApiError(404, "template not found");
|
|
300
|
+
await deps.templateService.deleteItem(
|
|
301
|
+
actor,
|
|
302
|
+
template.id,
|
|
303
|
+
kind,
|
|
304
|
+
field(form, "key"),
|
|
305
|
+
);
|
|
306
|
+
}),
|
|
307
|
+
);
|
|
308
|
+
|
|
309
|
+
app.post(`${BASE}/delete`, deps.requireAuth, (c) =>
|
|
310
|
+
mutate(c, "deleted", async (actor, scope) => {
|
|
311
|
+
const template = await findTemplate(actor, scope);
|
|
312
|
+
if (!template) throw new ApiError(404, "template not found");
|
|
313
|
+
await deps.templateService.delete(actor, template.id);
|
|
314
|
+
}),
|
|
315
|
+
);
|
|
316
|
+
}
|
package/src/admin-ui.ts
CHANGED
|
@@ -52,6 +52,8 @@ import {
|
|
|
52
52
|
import { renderZeroQuotaNotice } from "./admin-ui-account-pages.ts";
|
|
53
53
|
import { registerAdminAccountsRoutes } from "./admin-ui-accounts.ts";
|
|
54
54
|
import { registerAdminGithubEnterpriseRoutes } from "./admin-ui-github-enterprise.ts";
|
|
55
|
+
import { registerAdminTemplatesRoutes } from "./admin-ui-templates.ts";
|
|
56
|
+
import type { AgentTemplateService } from "./agent-templates.ts";
|
|
55
57
|
import {
|
|
56
58
|
type GithubAccessPreviewView,
|
|
57
59
|
renderGithubAccessError,
|
|
@@ -437,6 +439,14 @@ export interface AdminUIDeps {
|
|
|
437
439
|
* (additive): when absent the Settings page and its nav entry are not
|
|
438
440
|
* registered.
|
|
439
441
|
*/
|
|
442
|
+
/**
|
|
443
|
+
* ATP-2.3: the template service behind Settings > Templates. Optional;
|
|
444
|
+
* absent → no /admin/settings/templates routes.
|
|
445
|
+
*/
|
|
446
|
+
templateService?: Pick<
|
|
447
|
+
AgentTemplateService,
|
|
448
|
+
"list" | "replace" | "putItem" | "deleteItem" | "delete"
|
|
449
|
+
>;
|
|
440
450
|
githubEnterpriseApi?: (
|
|
441
451
|
path: string,
|
|
442
452
|
init?: RequestInit,
|
|
@@ -855,6 +865,7 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
|
|
|
855
865
|
accountCreatedNotifier,
|
|
856
866
|
accountServices,
|
|
857
867
|
githubEnterpriseApi,
|
|
868
|
+
templateService,
|
|
858
869
|
agentsApi,
|
|
859
870
|
specialtyRegistry = new SpecialtyTypes(),
|
|
860
871
|
specialtyDefaults,
|
|
@@ -3706,6 +3717,19 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
|
|
|
3706
3717
|
});
|
|
3707
3718
|
}
|
|
3708
3719
|
|
|
3720
|
+
// ─── Template editor (ATP-2.3) ───────────────────────────────────────────
|
|
3721
|
+
|
|
3722
|
+
if (templateService) {
|
|
3723
|
+
registerAdminTemplatesRoutes(app, {
|
|
3724
|
+
requireAuth,
|
|
3725
|
+
templateService,
|
|
3726
|
+
callerScopeResolver,
|
|
3727
|
+
...(accountServices ? { accounts: accountServices.accounts } : {}),
|
|
3728
|
+
specialtyRegistry,
|
|
3729
|
+
html,
|
|
3730
|
+
});
|
|
3731
|
+
}
|
|
3732
|
+
|
|
3709
3733
|
// ─── Account page (SSP-3.2) ───────────────────────────────────────────────
|
|
3710
3734
|
|
|
3711
3735
|
if (accountServices) {
|