@app-vitals/agent-admin 1.45.0 → 1.47.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/src/agents.ts CHANGED
@@ -14,13 +14,17 @@ import { isOrgRepo } from "@app-vitals/agent-lib/org-repo";
14
14
  import { SECRET_ENV_VARS } from "@app-vitals/agent-lib/secret-env-vars";
15
15
  import type { PrismaClient } from "../prisma/client/client.ts";
16
16
  import type { AgentAnthropicIdentity } from "./agent-anthropic-identity.ts";
17
+ import type { AgentTemplateReconcileService } from "./agent-template-reconcile.ts";
17
18
  import type { AgentCronJobService } from "./agent-cron-jobs.ts";
18
19
  import type { AgentEnvService } from "./agent-envs.ts";
19
20
  import { AgentMemberService } from "./agent-members.ts";
20
21
  import type { AgentPluginService } from "./agent-plugins.ts";
21
22
  import type { AgentProvisioner } from "./agent-provisioner.ts";
22
23
  import type { AgentToolService } from "./agent-tools.ts";
23
- import { ANTHROPIC_CREDENTIALS_BLOCKED_CODE } from "./anthropic-credential-guard.ts";
24
+ import {
25
+ ANTHROPIC_CREDENTIALS_BLOCKED_CODE,
26
+ anthropicCredentialKeysIn,
27
+ } from "./anthropic-credential-guard.ts";
24
28
  import type { PrismaTransactionClient } from "./prisma-tx.ts";
25
29
  import type { SpecialtyDefaultsResolver } from "./specialty-defaults.ts";
26
30
  import {
@@ -48,6 +52,8 @@ interface CreateAgentInput {
48
52
  accountId?: string;
49
53
  /** Owner's email (PAI-1.2). Already normalized by createAgent(); omitted means no owner. */
50
54
  ownerEmail?: string;
55
+ /** ATP-2.2: create the agent detached from the agent template. */
56
+ templateDetached?: boolean;
51
57
  }
52
58
 
53
59
  export interface AgentRecord {
@@ -361,6 +367,9 @@ export class AgentService {
361
367
  ...(input.ownerEmail !== undefined
362
368
  ? { ownerEmail: input.ownerEmail }
363
369
  : {}),
370
+ ...(input.templateDetached !== undefined
371
+ ? { templateDetached: input.templateDetached }
372
+ : {}),
364
373
  },
365
374
  });
366
375
  const missingRequiredEnv = await this.computeMissingRequiredEnv(
@@ -747,6 +756,17 @@ export interface CreateAgentFormInput {
747
756
  * trims and lower-cases it; blank is treated as absent.
748
757
  */
749
758
  ownerEmail?: string;
759
+ /**
760
+ * ATP-2.2: true opts the new agent out of the agent template entirely — it
761
+ * gets only the built-in specialty defaults and no later template writes.
762
+ */
763
+ templateDetached?: boolean;
764
+ /**
765
+ * ATP-2.2: explicit env values from the provisioning request. They override
766
+ * the template per key and are written source=custom, so later reconciles
767
+ * never touch them.
768
+ */
769
+ env?: Record<string, string>;
750
770
  }
751
771
 
752
772
  // Deliberately simple: one @, no whitespace, a dotted domain. Enough to keep
@@ -828,6 +848,13 @@ export interface CreateAgentDeps {
828
848
  specialtyDefaults?: SpecialtyDefaultsResolver;
829
849
  /** WIF-2.1: Anthropic federation is enabled; defaults to false. */
830
850
  anthropicFederationEnabled?: boolean;
851
+ /**
852
+ * ATP-2.2: seeds the resolved agent template (specialty defaults plus the
853
+ * deployment/account layers) inside the create transaction. Optional
854
+ * (additive); absent, or for a detached agent, the built-in specialty
855
+ * defaults are copied directly.
856
+ */
857
+ templateReconcile?: Pick<AgentTemplateReconcileService, "reconcile">;
831
858
  }
832
859
 
833
860
  /**
@@ -870,7 +897,7 @@ async function checkAccountQuota(
870
897
 
871
898
  /** Discriminated outcome of the transactional portion of createAgent(). */
872
899
  type CreateAgentTxOutcome =
873
- | { ok: true; agent: AgentDetail }
900
+ | { ok: true; agent: AgentDetail; seededEnvKeys: Set<string> }
874
901
  | { ok: false; errorCode: CreateAgentErrorCode; errorMessage?: string };
875
902
 
876
903
  /**
@@ -947,7 +974,9 @@ export async function createAgent(
947
974
  if (
948
975
  deps.anthropicFederationEnabled &&
949
976
  inCluster &&
950
- (input.claudeCodeOauthToken || input.anthropicApiKey)
977
+ (input.claudeCodeOauthToken ||
978
+ input.anthropicApiKey ||
979
+ anthropicCredentialKeysIn(Object.keys(input.env ?? {})).length > 0)
951
980
  ) {
952
981
  return {
953
982
  ok: false,
@@ -979,6 +1008,7 @@ export async function createAgent(
979
1008
  ? { accountId: input.accountId }
980
1009
  : {}),
981
1010
  ...(ownerEmail !== undefined ? { ownerEmail } : {}),
1011
+ ...(input.templateDetached ? { templateDetached: true } : {}),
982
1012
  },
983
1013
  tx,
984
1014
  );
@@ -988,19 +1018,36 @@ export async function createAgent(
988
1018
  // failure so a retry with the same name doesn't collide with a
989
1019
  // half-seeded agent. Members/repos have their own dedicated handling
990
1020
  // below.
1021
+ //
1022
+ // ATP-2.2: an attached agent takes the resolved template (specialty
1023
+ // defaults + deployment/account layers) from the reconcile engine in
1024
+ // this same transaction; a detached agent (or a deployment without the
1025
+ // engine) gets the built-in specialty defaults copied directly.
1026
+ const seededEnvKeys = new Set<string>();
991
1027
  try {
992
- for (const pattern of extensionDefaults?.tools ?? type?.tools ?? []) {
993
- await deps.agentToolService.add(agent.id, pattern, tx);
994
- }
995
- for (const pluginName of resolveDefaultPlugins(
996
- extensionDefaults?.plugins,
997
- )) {
998
- await deps.agentPluginService.add(
999
- agent.id,
1000
- pluginName,
1001
- undefined,
1002
- tx,
1003
- );
1028
+ if (deps.templateReconcile && !input.templateDetached) {
1029
+ await deps.templateReconcile.reconcile(agent.id, tx);
1030
+ if (tx) {
1031
+ const rows = await tx.agentEnv.findMany({
1032
+ where: { agentId: agent.id },
1033
+ select: { key: true },
1034
+ });
1035
+ for (const r of rows) seededEnvKeys.add(r.key);
1036
+ }
1037
+ } else {
1038
+ for (const pattern of extensionDefaults?.tools ?? type?.tools ?? []) {
1039
+ await deps.agentToolService.add(agent.id, pattern, tx);
1040
+ }
1041
+ for (const pluginName of resolveDefaultPlugins(
1042
+ extensionDefaults?.plugins,
1043
+ )) {
1044
+ await deps.agentPluginService.add(
1045
+ agent.id,
1046
+ pluginName,
1047
+ undefined,
1048
+ tx,
1049
+ );
1050
+ }
1004
1051
  }
1005
1052
  } catch (err) {
1006
1053
  console.error(
@@ -1094,7 +1141,7 @@ export async function createAgent(
1094
1141
  }
1095
1142
  }
1096
1143
 
1097
- return { ok: true, agent };
1144
+ return { ok: true, agent, seededEnvKeys };
1098
1145
  },
1099
1146
  );
1100
1147
 
@@ -1116,7 +1163,13 @@ export async function createAgent(
1116
1163
  // Non-secret defaults (e.g. ANTHROPIC_MODEL) are seeded alongside so they
1117
1164
  // show up in the agent's env view; the secret credentials below are
1118
1165
  // operator-supplied only and never defaulted.
1119
- const initialEnv: Record<string, string> = { ...DEFAULT_AGENT_ENV };
1166
+ // A template env value for the same key replaces the built-in default;
1167
+ // explicit credentials below are written custom and always win.
1168
+ const initialEnv: Record<string, string> = {};
1169
+ for (const [key, value] of Object.entries(DEFAULT_AGENT_ENV)) {
1170
+ if (!txResult.seededEnvKeys.has(key)) initialEnv[key] = value;
1171
+ }
1172
+ Object.assign(initialEnv, input.env);
1120
1173
  if (input.claudeCodeOauthToken) {
1121
1174
  initialEnv.CLAUDE_CODE_OAUTH_TOKEN = input.claudeCodeOauthToken;
1122
1175
  }
@@ -188,6 +188,19 @@ export const CreateAgentBodySchema = z
188
188
  .openapi({ example: "true" }),
189
189
  claudeCodeOauthToken: z.string().optional(),
190
190
  anthropicApiKey: z.string().optional(),
191
+ /**
192
+ * Explicit env values. They override the agent template per key and are
193
+ * kept as the agent's own (never overwritten by a template reconcile).
194
+ */
195
+ env: z
196
+ .record(z.string(), z.string())
197
+ .optional()
198
+ .openapi({ example: { ANTHROPIC_MODEL: "claude-opus-4-1" } }),
199
+ /**
200
+ * Opt out of the agent template: the agent gets only the built-in
201
+ * specialty defaults and no later template writes.
202
+ */
203
+ templateDetached: z.boolean().optional().openapi({ example: false }),
191
204
  })
192
205
  .openapi("CreateAgentBody");
193
206
 
@@ -0,0 +1,35 @@
1
+ import { describe, expect, it } from "bun:test";
2
+ import * as server from "./server.ts";
3
+
4
+ describe("server.ts export surface", () => {
5
+ it("exports the settings layout helpers", () => {
6
+ expect(typeof server.renderSettingsLayout).toBe("function");
7
+ expect(typeof server.mergeSettingsNavItems).toBe("function");
8
+ });
9
+
10
+ it("keeps existing exports", () => {
11
+ expect(typeof server.createAdminUIApp).toBe("function");
12
+ expect(typeof server.createAdminApp).toBe("function");
13
+ });
14
+
15
+ it("type exports compile", () => {
16
+ const item: server.SettingsNavItem = { label: "l", path: "/l" };
17
+ const section: server.SettingsNavSection = { label: "s", items: [item] };
18
+ const nav: server.SettingsNav = { sections: [section] };
19
+ const c: server.SettingsNavContribution = {
20
+ section: "s",
21
+ label: "l",
22
+ path: "/l",
23
+ };
24
+ const ext: server.AdminExtension = { settingsNavItems: () => [c] };
25
+ const opts: server.RenderSettingsLayoutOptions = {
26
+ userName: "u",
27
+ title: "t",
28
+ nav,
29
+ active: "/l",
30
+ body: "",
31
+ };
32
+ expect(server.renderSettingsLayout(opts)).toContain("l");
33
+ expect(ext.settingsNavItems).toBeDefined();
34
+ });
35
+ });
package/src/server.ts CHANGED
@@ -48,3 +48,18 @@ export { HttpGoogleAuthClient } from "./google-auth-client.ts";
48
48
  export type { GoogleAuthClient } from "./google-auth-client.ts";
49
49
  export { PrismaClient } from "../prisma/client/client.ts";
50
50
  export { createAdminPrismaClient } from "./prisma-client.ts";
51
+ export {
52
+ renderSettingsLayout,
53
+ mergeSettingsNavItems,
54
+ } from "./admin-ui-settings-layout.ts";
55
+ export type {
56
+ SettingsNav,
57
+ SettingsNavItem,
58
+ SettingsNavSection,
59
+ RenderSettingsLayoutOptions,
60
+ } from "./admin-ui-settings-layout.ts";
61
+ export type {
62
+ AdminExtension,
63
+ AdminExtensionContext,
64
+ SettingsNavContribution,
65
+ } from "./admin-extension.ts";