@app-vitals/agent-admin 1.56.0 → 1.58.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/openapi.platform.json +33 -6
- package/package.json +2 -2
- package/src/admin-ui-templates-pages.ts +11 -3
- package/src/admin-ui-templates.ts +9 -0
- package/src/agent-template-items.ts +20 -0
- package/src/agent-template-items.unit.test.ts +53 -0
- package/src/agent-template-merge.ts +1 -0
- package/src/agent-template-merge.unit.test.ts +50 -0
- package/src/agent-template-reconcile.integration.test.ts +217 -0
- package/src/agent-template-reconcile.ts +83 -2
- package/src/agent-template-triggers.integration.test.ts +90 -0
- package/src/agent-templates-api.integration.test.ts +80 -0
- package/src/agent-templates-api.ts +1 -1
- package/src/agent-templates.ts +11 -0
- package/src/agents-api.ts +5 -2
- package/src/openapi-schemas.ts +3 -2
package/openapi.platform.json
CHANGED
|
@@ -987,6 +987,29 @@
|
|
|
987
987
|
"adopted",
|
|
988
988
|
"deleted"
|
|
989
989
|
]
|
|
990
|
+
},
|
|
991
|
+
"marketplace": {
|
|
992
|
+
"type": "object",
|
|
993
|
+
"properties": {
|
|
994
|
+
"created": {
|
|
995
|
+
"type": "integer"
|
|
996
|
+
},
|
|
997
|
+
"updated": {
|
|
998
|
+
"type": "integer"
|
|
999
|
+
},
|
|
1000
|
+
"adopted": {
|
|
1001
|
+
"type": "integer"
|
|
1002
|
+
},
|
|
1003
|
+
"deleted": {
|
|
1004
|
+
"type": "integer"
|
|
1005
|
+
}
|
|
1006
|
+
},
|
|
1007
|
+
"required": [
|
|
1008
|
+
"created",
|
|
1009
|
+
"updated",
|
|
1010
|
+
"adopted",
|
|
1011
|
+
"deleted"
|
|
1012
|
+
]
|
|
990
1013
|
}
|
|
991
1014
|
},
|
|
992
1015
|
"required": [
|
|
@@ -1026,7 +1049,8 @@
|
|
|
1026
1049
|
"env",
|
|
1027
1050
|
"tool",
|
|
1028
1051
|
"plugin",
|
|
1029
|
-
"cron"
|
|
1052
|
+
"cron",
|
|
1053
|
+
"marketplace"
|
|
1030
1054
|
]
|
|
1031
1055
|
},
|
|
1032
1056
|
"key": {
|
|
@@ -1037,7 +1061,8 @@
|
|
|
1037
1061
|
"enum": [
|
|
1038
1062
|
"cron_name_conflict",
|
|
1039
1063
|
"invalid_cron_schedule",
|
|
1040
|
-
"denylisted_env_key"
|
|
1064
|
+
"denylisted_env_key",
|
|
1065
|
+
"marketplace_conflict"
|
|
1041
1066
|
]
|
|
1042
1067
|
}
|
|
1043
1068
|
},
|
|
@@ -1081,7 +1106,8 @@
|
|
|
1081
1106
|
"env",
|
|
1082
1107
|
"tool",
|
|
1083
1108
|
"plugin",
|
|
1084
|
-
"cron"
|
|
1109
|
+
"cron",
|
|
1110
|
+
"marketplace"
|
|
1085
1111
|
]
|
|
1086
1112
|
},
|
|
1087
1113
|
"key": {
|
|
@@ -4395,7 +4421,8 @@
|
|
|
4395
4421
|
"env",
|
|
4396
4422
|
"tool",
|
|
4397
4423
|
"plugin",
|
|
4398
|
-
"cron"
|
|
4424
|
+
"cron",
|
|
4425
|
+
"marketplace"
|
|
4399
4426
|
]
|
|
4400
4427
|
},
|
|
4401
4428
|
"PutTemplateBody": {
|
|
@@ -4440,7 +4467,7 @@
|
|
|
4440
4467
|
"payload": {
|
|
4441
4468
|
"type": "object",
|
|
4442
4469
|
"additionalProperties": {},
|
|
4443
|
-
"description": "Kind-specific fields. env: {value, secret?}; tool: {enabled?}; plugin: {version?, enabled?}; cron: {schedule, prompt, channel?, user?, silent?, enabled?, preCheck?}.",
|
|
4470
|
+
"description": "Kind-specific fields. env: {value, secret?}; tool: {enabled?}; plugin: {version?, enabled?}; cron: {schedule, prompt, channel?, user?, silent?, enabled?, preCheck?}; marketplace: {source, ref?} (key = marketplace name).",
|
|
4444
4471
|
"example": {
|
|
4445
4472
|
"value": "lin_abc",
|
|
4446
4473
|
"secret": true
|
|
@@ -8663,7 +8690,7 @@
|
|
|
8663
8690
|
"templates"
|
|
8664
8691
|
],
|
|
8665
8692
|
"summary": "Create or replace an agent template",
|
|
8666
|
-
"description": "Creates the template for the (accountId, specialty) scope, or replaces the items of the existing one (201 / 200). Account callers may only write their own account (403 otherwise); only platform admins write deployment-wide templates. 422 for an unknown specialty, a denylisted env key (per-agent credentials: Slack tokens, GH_TOKEN, Anthropic credentials, task-store URL/token), an invalid item payload, a cron item without a name key, or a duplicate (kind, key). Env values are encrypted at rest; a secret env item sent as `\"***\"` keeps its stored value.",
|
|
8693
|
+
"description": "Creates the template for the (accountId, specialty) scope, or replaces the items of the existing one (201 / 200). Account callers may only write their own account (403 otherwise); only platform admins write deployment-wide templates. 422 for an unknown specialty, a denylisted env key (per-agent credentials: Slack tokens, GH_TOKEN, Anthropic credentials, task-store URL/token), an invalid item payload, a cron item without a name key, a marketplace item with an invalid name, source or ref (same messages as POST /agents/:id/marketplaces), or a duplicate (kind, key). Env values are encrypted at rest; a secret env item sent as `\"***\"` keeps its stored value.",
|
|
8667
8694
|
"requestBody": {
|
|
8668
8695
|
"content": {
|
|
8669
8696
|
"application/json": {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@app-vitals/agent-admin",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.58.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/app-vitals/agent.git",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
44
|
"@anthropic-ai/sdk": "0.137.0",
|
|
45
|
-
"@app-vitals/agent-lib": "1.
|
|
45
|
+
"@app-vitals/agent-lib": "1.58.0",
|
|
46
46
|
"@hono/zod-openapi": "^1.0.0",
|
|
47
47
|
"@prisma/adapter-pg": "^7.10.0",
|
|
48
48
|
"@prisma/client": "^7.10.0",
|
|
@@ -10,6 +10,10 @@
|
|
|
10
10
|
|
|
11
11
|
import { renderAdminPage } from "./admin-ui-layout.ts";
|
|
12
12
|
import { escapeHtml } from "./admin-ui-styles.ts";
|
|
13
|
+
import {
|
|
14
|
+
AGENT_TEMPLATE_ITEM_KINDS,
|
|
15
|
+
type AgentTemplateItemKind,
|
|
16
|
+
} from "./agent-template-items.ts";
|
|
13
17
|
import {
|
|
14
18
|
buildSettingsNav,
|
|
15
19
|
renderSettingsLayout,
|
|
@@ -26,7 +30,7 @@ export interface TemplateScopeOption {
|
|
|
26
30
|
}
|
|
27
31
|
|
|
28
32
|
export interface TemplateItemRow {
|
|
29
|
-
kind:
|
|
33
|
+
kind: AgentTemplateItemKind;
|
|
30
34
|
key: string;
|
|
31
35
|
payload: Record<string, unknown>;
|
|
32
36
|
}
|
|
@@ -90,6 +94,9 @@ function itemFields(
|
|
|
90
94
|
case "plugin":
|
|
91
95
|
return `<label>Version <input name="version" class="form-input" value="${escapeHtml(str(p.version))}" placeholder="latest" /></label>
|
|
92
96
|
<label><input type="checkbox" name="enabled" value="1"${checked(p.enabled)} /> Enabled</label>`;
|
|
97
|
+
case "marketplace":
|
|
98
|
+
return `<label>Source <input name="source" class="form-input" value="${escapeHtml(str(p.source))}" placeholder="owner/repo" /></label>
|
|
99
|
+
<label>Ref <input name="ref" class="form-input" value="${escapeHtml(str(p.ref))}" placeholder="default branch" /></label>`;
|
|
93
100
|
case "cron":
|
|
94
101
|
return `<label>Schedule <input name="schedule" class="form-input" value="${escapeHtml(str(p.schedule))}" placeholder="0 9 * * 1-5" /></label>
|
|
95
102
|
<label>Prompt <textarea name="prompt" class="form-input" rows="2">${escapeHtml(str(p.prompt))}</textarea></label>
|
|
@@ -106,6 +113,7 @@ const KIND_COPY = {
|
|
|
106
113
|
tool: { title: "Tools", key: "Tool pattern", ph: "Bash(git *)" },
|
|
107
114
|
plugin: { title: "Plugins", key: "Plugin", ph: "shipwright@shipwright" },
|
|
108
115
|
cron: { title: "Crons", key: "Name", ph: "daily-digest" },
|
|
116
|
+
marketplace: { title: "Marketplaces", key: "Name", ph: "example-plugins" },
|
|
109
117
|
} as const;
|
|
110
118
|
|
|
111
119
|
function kindCard(o: TemplatesPageOpts, kind: TemplateItemRow["kind"]): string {
|
|
@@ -158,11 +166,11 @@ export function renderTemplatesPage(o: TemplatesPageOpts): string {
|
|
|
158
166
|
isAdmin: o.isAdmin ?? false,
|
|
159
167
|
body: `${error}${notice}
|
|
160
168
|
<div class="card">
|
|
161
|
-
<p class="help-text">Default env vars, tools, plugins and
|
|
169
|
+
<p class="help-text">Default env vars, tools, plugins, crons and plugin marketplaces for new agents in a scope. Secret values are masked after saving; leave <code>***</code> to keep the stored value.</p>
|
|
162
170
|
${scopePicker(o)}
|
|
163
171
|
${del}
|
|
164
172
|
</div>
|
|
165
|
-
${
|
|
173
|
+
${AGENT_TEMPLATE_ITEM_KINDS.map((k) => kindCard(o, k)).join("\n")}`,
|
|
166
174
|
}),
|
|
167
175
|
});
|
|
168
176
|
}
|
|
@@ -190,6 +190,15 @@ export function registerAdminTemplatesRoutes(
|
|
|
190
190
|
enabled: flag(form, "enabled"),
|
|
191
191
|
},
|
|
192
192
|
};
|
|
193
|
+
case "marketplace":
|
|
194
|
+
return {
|
|
195
|
+
kind,
|
|
196
|
+
key,
|
|
197
|
+
payload: {
|
|
198
|
+
source: field(form, "source"),
|
|
199
|
+
ref: orNull(field(form, "ref")),
|
|
200
|
+
},
|
|
201
|
+
};
|
|
193
202
|
case "cron":
|
|
194
203
|
return {
|
|
195
204
|
kind,
|
|
@@ -12,6 +12,11 @@
|
|
|
12
12
|
|
|
13
13
|
import type { Prisma } from "../prisma/client/client.ts";
|
|
14
14
|
import { ANTHROPIC_CREDENTIAL_KEYS } from "./anthropic-credential-guard.ts";
|
|
15
|
+
import {
|
|
16
|
+
validateMarketplaceName,
|
|
17
|
+
validateMarketplaceRef,
|
|
18
|
+
validateMarketplaceSource,
|
|
19
|
+
} from "./agent-marketplaces.ts";
|
|
15
20
|
import { UnprocessableEntityError } from "./errors.ts";
|
|
16
21
|
import type { TokenCrypto } from "./token-crypto.ts";
|
|
17
22
|
|
|
@@ -20,6 +25,7 @@ export const AGENT_TEMPLATE_ITEM_KINDS = [
|
|
|
20
25
|
"tool",
|
|
21
26
|
"plugin",
|
|
22
27
|
"cron",
|
|
28
|
+
"marketplace",
|
|
23
29
|
] as const;
|
|
24
30
|
export type AgentTemplateItemKind = (typeof AGENT_TEMPLATE_ITEM_KINDS)[number];
|
|
25
31
|
|
|
@@ -55,6 +61,20 @@ export function openTemplateItemPayload(
|
|
|
55
61
|
return { ...payload, value: crypto.decrypt(payload.value) };
|
|
56
62
|
}
|
|
57
63
|
|
|
64
|
+
/**
|
|
65
|
+
* Throws UnprocessableEntityError (422) unless a marketplace item's key (the
|
|
66
|
+
* marketplace name) and payload ({source, ref?}) pass the same validators as
|
|
67
|
+
* POST /agents/:id/marketplaces.
|
|
68
|
+
*/
|
|
69
|
+
export function assertMarketplaceTemplateItem(
|
|
70
|
+
key: string,
|
|
71
|
+
payload: { source: string; ref?: string | null },
|
|
72
|
+
): void {
|
|
73
|
+
validateMarketplaceName(key);
|
|
74
|
+
validateMarketplaceSource(payload.source);
|
|
75
|
+
validateMarketplaceRef(payload.ref);
|
|
76
|
+
}
|
|
77
|
+
|
|
58
78
|
/**
|
|
59
79
|
* Env keys a template may never carry (ATP-1.3): per-agent credentials that
|
|
60
80
|
* are minted for, or provisioned onto, one specific agent. Copying them from a
|
|
@@ -5,6 +5,8 @@
|
|
|
5
5
|
|
|
6
6
|
import { describe, expect, it } from "bun:test";
|
|
7
7
|
import {
|
|
8
|
+
AGENT_TEMPLATE_ITEM_KINDS,
|
|
9
|
+
assertMarketplaceTemplateItem,
|
|
8
10
|
assertTemplateEnvKeyAllowed,
|
|
9
11
|
openTemplateItemPayload,
|
|
10
12
|
sealTemplateItemPayload,
|
|
@@ -68,3 +70,54 @@ describe("assertTemplateEnvKeyAllowed", () => {
|
|
|
68
70
|
expect(() => assertTemplateEnvKeyAllowed("LINEAR_API_KEY")).not.toThrow();
|
|
69
71
|
});
|
|
70
72
|
});
|
|
73
|
+
|
|
74
|
+
describe("marketplace template items", () => {
|
|
75
|
+
it("lists marketplace as an item kind and stores it unencrypted", () => {
|
|
76
|
+
expect(AGENT_TEMPLATE_ITEM_KINDS).toContain("marketplace");
|
|
77
|
+
const payload = { source: "example-org/plugins", ref: "v1" };
|
|
78
|
+
expect(sealTemplateItemPayload("marketplace", payload, crypto)).toBe(
|
|
79
|
+
payload,
|
|
80
|
+
);
|
|
81
|
+
expect(openTemplateItemPayload("marketplace", payload, crypto)).toBe(
|
|
82
|
+
payload,
|
|
83
|
+
);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("accepts a valid name, source and ref (ref optional or null)", () => {
|
|
87
|
+
expect(() =>
|
|
88
|
+
assertMarketplaceTemplateItem("example-plugins", {
|
|
89
|
+
source: "example-org/plugins",
|
|
90
|
+
}),
|
|
91
|
+
).not.toThrow();
|
|
92
|
+
expect(() =>
|
|
93
|
+
assertMarketplaceTemplateItem("m.1_x", {
|
|
94
|
+
source: "https://git.example.com/org/repo.git",
|
|
95
|
+
ref: null,
|
|
96
|
+
}),
|
|
97
|
+
).not.toThrow();
|
|
98
|
+
expect(() =>
|
|
99
|
+
assertMarketplaceTemplateItem("m", { source: "o/r", ref: "main" }),
|
|
100
|
+
).not.toThrow();
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("returns the per-agent API's 422 messages", () => {
|
|
104
|
+
expect(() =>
|
|
105
|
+
assertMarketplaceTemplateItem("m", {
|
|
106
|
+
source: "https://user:pw@example.com/o/r",
|
|
107
|
+
}),
|
|
108
|
+
).toThrow(
|
|
109
|
+
"source must be a GitHub 'owner/repo' or an https git URL without credentials",
|
|
110
|
+
);
|
|
111
|
+
expect(() =>
|
|
112
|
+
assertMarketplaceTemplateItem("m", { source: "not a repo" }),
|
|
113
|
+
).toThrow(UnprocessableEntityError);
|
|
114
|
+
expect(() =>
|
|
115
|
+
assertMarketplaceTemplateItem("-bad name", { source: "o/r" }),
|
|
116
|
+
).toThrow(
|
|
117
|
+
"name must start with a letter or digit and contain only letters, digits, '.', '_' and '-'",
|
|
118
|
+
);
|
|
119
|
+
expect(() =>
|
|
120
|
+
assertMarketplaceTemplateItem("m", { source: "o/r", ref: "a..b" }),
|
|
121
|
+
).toThrow("ref must be a plain branch, tag or sha");
|
|
122
|
+
});
|
|
123
|
+
});
|
|
@@ -181,3 +181,53 @@ describe("resolveSpecialtyDefaultSet", () => {
|
|
|
181
181
|
expect(resolveSpecialtyDefaultSet("nope", registry)).toBeUndefined();
|
|
182
182
|
});
|
|
183
183
|
});
|
|
184
|
+
|
|
185
|
+
describe("mergeTemplateLayers: marketplace items", () => {
|
|
186
|
+
const mk = (source: string) => ({
|
|
187
|
+
kind: "marketplace" as const,
|
|
188
|
+
key: "example-plugins",
|
|
189
|
+
payload: { source },
|
|
190
|
+
});
|
|
191
|
+
const winner = (layers: TemplateLayerInput[]) =>
|
|
192
|
+
mergeTemplateLayers(layers).marketplace.get("example-plugins");
|
|
193
|
+
|
|
194
|
+
it("applies the plugin layer precedence, narrowest layer wins", () => {
|
|
195
|
+
const layers: TemplateLayerInput[] = [
|
|
196
|
+
{ layer: "account", items: [mk("o/3")] },
|
|
197
|
+
{ layer: "account-specialty", items: [mk("o/4")] },
|
|
198
|
+
{ layer: "deployment", items: [mk("o/1")] },
|
|
199
|
+
{ layer: "specialty-default", items: [mk("o/0")] },
|
|
200
|
+
{ layer: "deployment-specialty", items: [mk("o/2")] },
|
|
201
|
+
];
|
|
202
|
+
expect(winner(layers)).toEqual({
|
|
203
|
+
kind: "marketplace",
|
|
204
|
+
key: "example-plugins",
|
|
205
|
+
payload: { source: "o/4" },
|
|
206
|
+
layer: "account-specialty",
|
|
207
|
+
});
|
|
208
|
+
const without = (...drop: string[]) =>
|
|
209
|
+
layers.filter((l) => !drop.includes(l.layer));
|
|
210
|
+
expect(winner(without("account-specialty"))?.payload.source).toBe("o/3");
|
|
211
|
+
expect(
|
|
212
|
+
winner(without("account-specialty", "account"))?.payload.source,
|
|
213
|
+
).toBe("o/2");
|
|
214
|
+
expect(
|
|
215
|
+
winner(without("account-specialty", "account", "deployment-specialty"))
|
|
216
|
+
?.payload.source,
|
|
217
|
+
).toBe("o/1");
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it("keeps marketplaces separate from plugins of the same key", () => {
|
|
221
|
+
const merged = mergeTemplateLayers([
|
|
222
|
+
{
|
|
223
|
+
layer: "deployment",
|
|
224
|
+
items: [
|
|
225
|
+
mk("o/1"),
|
|
226
|
+
{ kind: "plugin", key: "example-plugins", payload: {} },
|
|
227
|
+
],
|
|
228
|
+
},
|
|
229
|
+
]);
|
|
230
|
+
expect(merged.marketplace.size).toBe(1);
|
|
231
|
+
expect(merged.plugin.size).toBe(1);
|
|
232
|
+
});
|
|
233
|
+
});
|
|
@@ -66,6 +66,7 @@ describeOrSkip("AgentTemplateReconcileService (integration)", () => {
|
|
|
66
66
|
await prisma.agentTemplate.deleteMany();
|
|
67
67
|
await prisma.agentCronJob.deleteMany();
|
|
68
68
|
await prisma.agentTool.deleteMany();
|
|
69
|
+
await prisma.agentMarketplace.deleteMany();
|
|
69
70
|
await prisma.agentPlugin.deleteMany();
|
|
70
71
|
await prisma.agentEnv.deleteMany();
|
|
71
72
|
await prisma.agentToken.deleteMany();
|
|
@@ -820,6 +821,222 @@ describeOrSkip("AgentTemplateReconcileService (integration)", () => {
|
|
|
820
821
|
]);
|
|
821
822
|
});
|
|
822
823
|
|
|
824
|
+
// ─── marketplaces (TMK-2.3) ───────────────────────────────────────────────
|
|
825
|
+
|
|
826
|
+
describe("marketplaces", () => {
|
|
827
|
+
const mkt = (agentId: string, name: string) =>
|
|
828
|
+
prisma.agentMarketplace.findUnique({
|
|
829
|
+
where: { agentId_name: { agentId, name } },
|
|
830
|
+
});
|
|
831
|
+
|
|
832
|
+
it("creates a missing template marketplace", async () => {
|
|
833
|
+
const agentId = await agent();
|
|
834
|
+
await template(null, null, [
|
|
835
|
+
{
|
|
836
|
+
kind: "marketplace",
|
|
837
|
+
key: "mk",
|
|
838
|
+
payload: { source: "example-org/plugins", ref: "v1" },
|
|
839
|
+
},
|
|
840
|
+
]);
|
|
841
|
+
const result = await service.reconcile(agentId);
|
|
842
|
+
expect(result.counts.marketplace).toEqual({
|
|
843
|
+
created: 1,
|
|
844
|
+
updated: 0,
|
|
845
|
+
adopted: 0,
|
|
846
|
+
deleted: 0,
|
|
847
|
+
});
|
|
848
|
+
expect(await mkt(agentId, "mk")).toMatchObject({
|
|
849
|
+
source: "example-org/plugins",
|
|
850
|
+
ref: "v1",
|
|
851
|
+
rowSource: "template",
|
|
852
|
+
enabled: true,
|
|
853
|
+
});
|
|
854
|
+
});
|
|
855
|
+
|
|
856
|
+
it("updates a changed ref in place, keeping the row id", async () => {
|
|
857
|
+
const agentId = await agent();
|
|
858
|
+
const tid = await template(null, null, [
|
|
859
|
+
{
|
|
860
|
+
kind: "marketplace",
|
|
861
|
+
key: "mk",
|
|
862
|
+
payload: { source: "example-org/plugins", ref: "v1" },
|
|
863
|
+
},
|
|
864
|
+
]);
|
|
865
|
+
await service.reconcile(agentId);
|
|
866
|
+
const before = await mkt(agentId, "mk");
|
|
867
|
+
await setItems(tid, [
|
|
868
|
+
{
|
|
869
|
+
kind: "marketplace",
|
|
870
|
+
key: "mk",
|
|
871
|
+
payload: { source: "example-org/plugins", ref: "v2" },
|
|
872
|
+
},
|
|
873
|
+
]);
|
|
874
|
+
const result = await service.reconcile(agentId);
|
|
875
|
+
expect(result.counts.marketplace?.updated).toBe(1);
|
|
876
|
+
const after = await mkt(agentId, "mk");
|
|
877
|
+
expect(after?.id).toBe(before?.id);
|
|
878
|
+
expect(after?.ref).toBe("v2");
|
|
879
|
+
});
|
|
880
|
+
|
|
881
|
+
it("adopts an enabled custom row with the same name, source and ref", async () => {
|
|
882
|
+
const agentId = await agent();
|
|
883
|
+
const row = await prisma.agentMarketplace.create({
|
|
884
|
+
data: { agentId, name: "mk", source: "example-org/plugins" },
|
|
885
|
+
});
|
|
886
|
+
await template(null, null, [
|
|
887
|
+
{
|
|
888
|
+
kind: "marketplace",
|
|
889
|
+
key: "mk",
|
|
890
|
+
payload: { source: "example-org/plugins" },
|
|
891
|
+
},
|
|
892
|
+
]);
|
|
893
|
+
const result = await service.reconcile(agentId);
|
|
894
|
+
expect(result.counts.marketplace?.adopted).toBe(1);
|
|
895
|
+
expect(await mkt(agentId, "mk")).toMatchObject({
|
|
896
|
+
id: row.id,
|
|
897
|
+
rowSource: "template",
|
|
898
|
+
});
|
|
899
|
+
});
|
|
900
|
+
|
|
901
|
+
it("leaves a same-named custom row with a different source and reports a conflict", async () => {
|
|
902
|
+
const agentId = await agent();
|
|
903
|
+
await prisma.agentMarketplace.create({
|
|
904
|
+
data: { agentId, name: "mk", source: "someone/else" },
|
|
905
|
+
});
|
|
906
|
+
await template(null, null, [
|
|
907
|
+
{
|
|
908
|
+
kind: "marketplace",
|
|
909
|
+
key: "mk",
|
|
910
|
+
payload: { source: "example-org/plugins" },
|
|
911
|
+
},
|
|
912
|
+
]);
|
|
913
|
+
const before = await mkt(agentId, "mk");
|
|
914
|
+
const result = await service.reconcile(agentId);
|
|
915
|
+
expect(result.conflicts).toEqual([
|
|
916
|
+
{ kind: "marketplace", key: "mk", reason: "marketplace_conflict" },
|
|
917
|
+
]);
|
|
918
|
+
expect(await mkt(agentId, "mk")).toEqual(before);
|
|
919
|
+
});
|
|
920
|
+
|
|
921
|
+
it("deletes an orphaned template row but never a custom row", async () => {
|
|
922
|
+
const agentId = await agent();
|
|
923
|
+
const tid = await template(null, null, [
|
|
924
|
+
{
|
|
925
|
+
kind: "marketplace",
|
|
926
|
+
key: "mk",
|
|
927
|
+
payload: { source: "example-org/plugins" },
|
|
928
|
+
},
|
|
929
|
+
]);
|
|
930
|
+
await service.reconcile(agentId);
|
|
931
|
+
await prisma.agentMarketplace.create({
|
|
932
|
+
data: { agentId, name: "mine", source: "me/mine" },
|
|
933
|
+
});
|
|
934
|
+
await setItems(tid, []);
|
|
935
|
+
const result = await service.reconcile(agentId);
|
|
936
|
+
expect(result.counts.marketplace?.deleted).toBe(1);
|
|
937
|
+
expect(await mkt(agentId, "mk")).toBeNull();
|
|
938
|
+
expect((await mkt(agentId, "mine"))?.rowSource).toBe("custom");
|
|
939
|
+
});
|
|
940
|
+
|
|
941
|
+
it("does not re-add or re-enable a tombstoned row", async () => {
|
|
942
|
+
const agentId = await agent();
|
|
943
|
+
await prisma.agentMarketplace.create({
|
|
944
|
+
data: {
|
|
945
|
+
agentId,
|
|
946
|
+
name: "mk",
|
|
947
|
+
source: "example-org/plugins",
|
|
948
|
+
rowSource: "custom",
|
|
949
|
+
enabled: false,
|
|
950
|
+
},
|
|
951
|
+
});
|
|
952
|
+
await template(null, null, [
|
|
953
|
+
{
|
|
954
|
+
kind: "marketplace",
|
|
955
|
+
key: "mk",
|
|
956
|
+
payload: { source: "example-org/plugins" },
|
|
957
|
+
},
|
|
958
|
+
]);
|
|
959
|
+
const before = await mkt(agentId, "mk");
|
|
960
|
+
const result = await service.reconcile(agentId);
|
|
961
|
+
expect(result.counts.marketplace).toEqual({
|
|
962
|
+
created: 0,
|
|
963
|
+
updated: 0,
|
|
964
|
+
adopted: 0,
|
|
965
|
+
deleted: 0,
|
|
966
|
+
});
|
|
967
|
+
expect(result.conflicts).toEqual([]);
|
|
968
|
+
expect(await mkt(agentId, "mk")).toEqual(before);
|
|
969
|
+
});
|
|
970
|
+
|
|
971
|
+
it("reset drops custom rows and tombstones for template keys and re-creates them", async () => {
|
|
972
|
+
const agentId = await agent();
|
|
973
|
+
await template(null, null, [
|
|
974
|
+
{
|
|
975
|
+
kind: "marketplace",
|
|
976
|
+
key: "mk",
|
|
977
|
+
payload: { source: "example-org/plugins" },
|
|
978
|
+
},
|
|
979
|
+
{
|
|
980
|
+
kind: "marketplace",
|
|
981
|
+
key: "tomb",
|
|
982
|
+
payload: { source: "example-org/other" },
|
|
983
|
+
},
|
|
984
|
+
]);
|
|
985
|
+
await prisma.agentMarketplace.create({
|
|
986
|
+
data: { agentId, name: "mk", source: "someone/else" },
|
|
987
|
+
});
|
|
988
|
+
await prisma.agentMarketplace.create({
|
|
989
|
+
data: {
|
|
990
|
+
agentId,
|
|
991
|
+
name: "tomb",
|
|
992
|
+
source: "example-org/other",
|
|
993
|
+
rowSource: "custom",
|
|
994
|
+
enabled: false,
|
|
995
|
+
},
|
|
996
|
+
});
|
|
997
|
+
await prisma.agentMarketplace.create({
|
|
998
|
+
data: { agentId, name: "mine", source: "me/mine" },
|
|
999
|
+
});
|
|
1000
|
+
|
|
1001
|
+
await service.reset(agentId, { kind: "marketplace", key: "mk" });
|
|
1002
|
+
expect(await mkt(agentId, "mk")).toMatchObject({
|
|
1003
|
+
source: "example-org/plugins",
|
|
1004
|
+
rowSource: "template",
|
|
1005
|
+
});
|
|
1006
|
+
expect((await mkt(agentId, "tomb"))?.enabled).toBe(false);
|
|
1007
|
+
|
|
1008
|
+
await service.reset(agentId);
|
|
1009
|
+
expect(await mkt(agentId, "tomb")).toMatchObject({
|
|
1010
|
+
rowSource: "template",
|
|
1011
|
+
enabled: true,
|
|
1012
|
+
});
|
|
1013
|
+
expect((await mkt(agentId, "mine"))?.rowSource).toBe("custom");
|
|
1014
|
+
});
|
|
1015
|
+
|
|
1016
|
+
it("leaves a detached agent's marketplaces untouched", async () => {
|
|
1017
|
+
const agentId = await agent({ templateDetached: true });
|
|
1018
|
+
await prisma.agentMarketplace.create({
|
|
1019
|
+
data: {
|
|
1020
|
+
agentId,
|
|
1021
|
+
name: "old",
|
|
1022
|
+
source: "example-org/old",
|
|
1023
|
+
rowSource: "template",
|
|
1024
|
+
},
|
|
1025
|
+
});
|
|
1026
|
+
await template(null, null, [
|
|
1027
|
+
{
|
|
1028
|
+
kind: "marketplace",
|
|
1029
|
+
key: "mk",
|
|
1030
|
+
payload: { source: "example-org/plugins" },
|
|
1031
|
+
},
|
|
1032
|
+
]);
|
|
1033
|
+
const result = await service.reconcile(agentId);
|
|
1034
|
+
expect(result.status).toBe("detached");
|
|
1035
|
+
expect(await mkt(agentId, "mk")).toBeNull();
|
|
1036
|
+
expect(await mkt(agentId, "old")).not.toBeNull();
|
|
1037
|
+
});
|
|
1038
|
+
});
|
|
1039
|
+
|
|
823
1040
|
it("throws NotFoundError for an unknown agent", async () => {
|
|
824
1041
|
await expect(service.reconcile("missing")).rejects.toBeInstanceOf(
|
|
825
1042
|
NotFoundError,
|
|
@@ -12,6 +12,13 @@
|
|
|
12
12
|
* - Missing items are created with source="template".
|
|
13
13
|
* - Rows with source="template" track the template: edited in place (ids
|
|
14
14
|
* stay stable), and deleted once no layer defines their key (orphans).
|
|
15
|
+
* - Marketplaces (TMK-2.3) follow the plugin rules, keyed by name, on
|
|
16
|
+
* AgentMarketplace.rowSource: created and updated in place (source/ref)
|
|
17
|
+
* through AgentMarketplaceService.addFromTemplate(), adopted when an enabled
|
|
18
|
+
* custom row has the same source and ref, and deleted as orphans. A custom
|
|
19
|
+
* row with the same name but a different source/ref is left alone and
|
|
20
|
+
* reported in `conflicts` (marketplace_conflict). They run before plugins,
|
|
21
|
+
* whose "<plugin>@<marketplace>" names refer to them.
|
|
15
22
|
* - Rows with source="custom" are never modified. That covers admin edits and
|
|
16
23
|
* tombstones (ATP-1.2 turns a deleted template row into enabled=false,
|
|
17
24
|
* source=custom), so a deleted inherited row is never re-added.
|
|
@@ -36,11 +43,13 @@
|
|
|
36
43
|
import type {
|
|
37
44
|
AgentCronJob,
|
|
38
45
|
AgentEnv,
|
|
46
|
+
AgentMarketplace,
|
|
39
47
|
AgentPlugin,
|
|
40
48
|
AgentTool,
|
|
41
49
|
PrismaClient,
|
|
42
50
|
} from "../prisma/client/client.ts";
|
|
43
51
|
import { applySystemCronDefaults, isValidCron } from "./agent-cron-jobs.ts";
|
|
52
|
+
import { AgentMarketplaceService } from "./agent-marketplaces.ts";
|
|
44
53
|
import {
|
|
45
54
|
type AgentTemplateItemKind,
|
|
46
55
|
openTemplateItemPayload,
|
|
@@ -76,7 +85,11 @@ export interface TemplateReconcileCounts {
|
|
|
76
85
|
export interface TemplateReconcileConflict {
|
|
77
86
|
kind: AgentTemplateItemKind;
|
|
78
87
|
key: string;
|
|
79
|
-
reason:
|
|
88
|
+
reason:
|
|
89
|
+
| "cron_name_conflict"
|
|
90
|
+
| "invalid_cron_schedule"
|
|
91
|
+
| "denylisted_env_key"
|
|
92
|
+
| "marketplace_conflict";
|
|
80
93
|
}
|
|
81
94
|
|
|
82
95
|
export interface TemplateReconcileResult {
|
|
@@ -84,7 +97,13 @@ export interface TemplateReconcileResult {
|
|
|
84
97
|
/** "detached" / "unknown_specialty": nothing was written. */
|
|
85
98
|
status: "reconciled" | "detached" | "unknown_specialty";
|
|
86
99
|
/** Template-tracked rows (system crons are counted in `systemCrons`). */
|
|
87
|
-
counts: Record<
|
|
100
|
+
counts: Record<
|
|
101
|
+
Exclude<AgentTemplateItemKind, "marketplace">,
|
|
102
|
+
TemplateReconcileCounts
|
|
103
|
+
> & {
|
|
104
|
+
/** TMK-2.3: optional so the exported shape stays additive for consumers. */
|
|
105
|
+
marketplace?: TemplateReconcileCounts;
|
|
106
|
+
};
|
|
88
107
|
systemCrons: { created: number; updated: number; deleted: number };
|
|
89
108
|
/** Template items deliberately not applied. */
|
|
90
109
|
conflicts: TemplateReconcileConflict[];
|
|
@@ -111,6 +130,7 @@ function emptyResult(
|
|
|
111
130
|
tool: zeroCounts(),
|
|
112
131
|
plugin: zeroCounts(),
|
|
113
132
|
cron: zeroCounts(),
|
|
133
|
+
marketplace: zeroCounts(),
|
|
114
134
|
},
|
|
115
135
|
systemCrons: { created: 0, updated: 0, deleted: 0 },
|
|
116
136
|
conflicts: [],
|
|
@@ -290,6 +310,7 @@ export class AgentTemplateReconcileService {
|
|
|
290
310
|
const result = emptyResult(agentId, "reconciled");
|
|
291
311
|
// Env goes last: it is the only kind that encrypts on write.
|
|
292
312
|
await this.reconcileTools(tx, agentId, merged, result);
|
|
313
|
+
await this.reconcileMarketplaces(tx, agentId, merged, result);
|
|
293
314
|
await this.reconcilePlugins(tx, agentId, merged, result);
|
|
294
315
|
await this.reconcileCrons(tx, agentId, merged, defaults.crons, result);
|
|
295
316
|
await this.reconcileEnv(tx, agentId, merged, result);
|
|
@@ -314,6 +335,13 @@ export class AgentTemplateReconcileService {
|
|
|
314
335
|
pattern: { in: keys("tool", merged.tool.keys()) },
|
|
315
336
|
},
|
|
316
337
|
});
|
|
338
|
+
await tx.agentMarketplace.deleteMany({
|
|
339
|
+
where: {
|
|
340
|
+
agentId,
|
|
341
|
+
rowSource: "custom",
|
|
342
|
+
name: { in: keys("marketplace", merged.marketplace.keys()) },
|
|
343
|
+
},
|
|
344
|
+
});
|
|
317
345
|
await tx.agentPlugin.deleteMany({
|
|
318
346
|
where: {
|
|
319
347
|
agentId,
|
|
@@ -460,6 +488,59 @@ export class AgentTemplateReconcileService {
|
|
|
460
488
|
}
|
|
461
489
|
}
|
|
462
490
|
|
|
491
|
+
private async reconcileMarketplaces(
|
|
492
|
+
tx: PrismaTransactionClient,
|
|
493
|
+
agentId: string,
|
|
494
|
+
merged: MergedTemplate,
|
|
495
|
+
result: TemplateReconcileResult,
|
|
496
|
+
): Promise<void> {
|
|
497
|
+
const counts = result.counts.marketplace ?? zeroCounts();
|
|
498
|
+
result.counts.marketplace = counts;
|
|
499
|
+
const rows = await tx.agentMarketplace.findMany({ where: { agentId } });
|
|
500
|
+
const byName = new Map<string, AgentMarketplace>(
|
|
501
|
+
rows.map((r) => [r.name, r]),
|
|
502
|
+
);
|
|
503
|
+
const service = new AgentMarketplaceService(this.prisma);
|
|
504
|
+
|
|
505
|
+
for (const item of merged.marketplace.values()) {
|
|
506
|
+
const source =
|
|
507
|
+
typeof item.payload.source === "string" ? item.payload.source : "";
|
|
508
|
+
const ref =
|
|
509
|
+
typeof item.payload.ref === "string" ? item.payload.ref : null;
|
|
510
|
+
const fromTemplate = { name: item.key, source, ref };
|
|
511
|
+
const row = byName.get(item.key);
|
|
512
|
+
if (!row) {
|
|
513
|
+
await service.addFromTemplate(agentId, fromTemplate, tx);
|
|
514
|
+
counts.created++;
|
|
515
|
+
} else if (row.rowSource === "template") {
|
|
516
|
+
if (row.source !== source || row.ref !== ref || !row.enabled) {
|
|
517
|
+
await service.addFromTemplate(agentId, fromTemplate, tx);
|
|
518
|
+
counts.updated++;
|
|
519
|
+
}
|
|
520
|
+
} else if (!row.enabled) {
|
|
521
|
+
// Tombstone: the user deleted the inherited row; never re-add it.
|
|
522
|
+
} else if (row.source === source && row.ref === ref) {
|
|
523
|
+
await tx.agentMarketplace.update({
|
|
524
|
+
where: { id: row.id },
|
|
525
|
+
data: { rowSource: "template" },
|
|
526
|
+
});
|
|
527
|
+
counts.adopted++;
|
|
528
|
+
} else {
|
|
529
|
+
result.conflicts.push({
|
|
530
|
+
kind: "marketplace",
|
|
531
|
+
key: item.key,
|
|
532
|
+
reason: "marketplace_conflict",
|
|
533
|
+
});
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
for (const row of rows) {
|
|
537
|
+
if (row.rowSource === "template" && !merged.marketplace.has(row.name)) {
|
|
538
|
+
await tx.agentMarketplace.delete({ where: { id: row.id } });
|
|
539
|
+
counts.deleted++;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
|
|
463
544
|
private async reconcilePlugins(
|
|
464
545
|
tx: PrismaTransactionClient,
|
|
465
546
|
agentId: string,
|
|
@@ -10,6 +10,7 @@ import { afterEach, beforeEach, describe, expect, it } from "bun:test";
|
|
|
10
10
|
import type { PrismaClient } from "../prisma/client/client.ts";
|
|
11
11
|
import { AgentCronJobService } from "./agent-cron-jobs.ts";
|
|
12
12
|
import { AgentEnvService } from "./agent-envs.ts";
|
|
13
|
+
import { AgentMarketplaceService } from "./agent-marketplaces.ts";
|
|
13
14
|
import { AgentMemberService } from "./agent-members.ts";
|
|
14
15
|
import { AgentPluginService } from "./agent-plugins.ts";
|
|
15
16
|
import { NoopAgentProvisioner } from "./agent-provisioner.ts";
|
|
@@ -23,6 +24,7 @@ import {
|
|
|
23
24
|
createAdminApp,
|
|
24
25
|
parseAdminApiKeys,
|
|
25
26
|
} from "./agents-api.ts";
|
|
27
|
+
import { createAgentRuntimeApp } from "./api.ts";
|
|
26
28
|
import { createAdminPrismaClient } from "./prisma-client.ts";
|
|
27
29
|
import type { SpecialtyType } from "./specialty-types.ts";
|
|
28
30
|
import { makeStrictTokenCrypto } from "./token-crypto.ts";
|
|
@@ -58,6 +60,7 @@ describeOrSkip("agent template triggers (integration)", () => {
|
|
|
58
60
|
await prisma.agentTemplate.deleteMany();
|
|
59
61
|
await prisma.agentCronJob.deleteMany();
|
|
60
62
|
await prisma.agentTool.deleteMany();
|
|
63
|
+
await prisma.agentMarketplace.deleteMany();
|
|
61
64
|
await prisma.agentPlugin.deleteMany();
|
|
62
65
|
await prisma.agentEnv.deleteMany();
|
|
63
66
|
await prisma.agentToken.deleteMany();
|
|
@@ -365,4 +368,91 @@ describeOrSkip("agent template triggers (integration)", () => {
|
|
|
365
368
|
expect(await envOf(id, "MINE")).toEqual({ value: "1", source: "custom" });
|
|
366
369
|
});
|
|
367
370
|
});
|
|
371
|
+
|
|
372
|
+
describe("template marketplaces in the config bundle", () => {
|
|
373
|
+
it("fans a marketplace plus its plugin out to agents in scope and into GET /agents/:id/config", async () => {
|
|
374
|
+
const agent = await mkAgent("a");
|
|
375
|
+
const other = await mkAgent("detached", { templateDetached: true });
|
|
376
|
+
const tokens = new AgentTokenService(prisma);
|
|
377
|
+
const { rawToken } = await tokens.create(agent.id);
|
|
378
|
+
const runtimeApp = createAgentRuntimeApp({
|
|
379
|
+
agentEnvService: { getConfigBundle: async () => null },
|
|
380
|
+
agentCronJobService: { list: async () => [] },
|
|
381
|
+
agentService: new AgentService(prisma),
|
|
382
|
+
agentPluginService: new AgentPluginService(prisma),
|
|
383
|
+
agentMarketplaceService: new AgentMarketplaceService(prisma),
|
|
384
|
+
sessionSecret: "test-admin-session-secret-32-bytes!",
|
|
385
|
+
adminApiKeys: parseAdminApiKeys("root:tok-admin:*"),
|
|
386
|
+
agentTokenService: tokens,
|
|
387
|
+
} as never);
|
|
388
|
+
const config = async () => {
|
|
389
|
+
const res = await runtimeApp.request(`/${agent.id}/config`, {
|
|
390
|
+
headers: { Authorization: `Bearer ${rawToken}` },
|
|
391
|
+
});
|
|
392
|
+
expect(res.status).toBe(200);
|
|
393
|
+
return (await res.json()) as {
|
|
394
|
+
marketplaces: { name: string; source: string; ref: string | null }[];
|
|
395
|
+
plugins: { plugin: string; marketplace: string }[];
|
|
396
|
+
};
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
await templates.replace({ kind: "admin" }, {}, [
|
|
400
|
+
{
|
|
401
|
+
kind: "marketplace",
|
|
402
|
+
key: "example-mk",
|
|
403
|
+
payload: { source: "example-org/plugins", ref: "v1" },
|
|
404
|
+
},
|
|
405
|
+
{ kind: "plugin", key: "tool@example-mk" },
|
|
406
|
+
]);
|
|
407
|
+
let cfg = await config();
|
|
408
|
+
expect(cfg.marketplaces).toEqual([
|
|
409
|
+
{ name: "example-mk", source: "example-org/plugins", ref: "v1" },
|
|
410
|
+
]);
|
|
411
|
+
expect(cfg.plugins).toContainEqual({
|
|
412
|
+
plugin: "tool",
|
|
413
|
+
marketplace: "example-mk",
|
|
414
|
+
});
|
|
415
|
+
expect(
|
|
416
|
+
await prisma.agentMarketplace.count({ where: { agentId: other.id } }),
|
|
417
|
+
).toBe(0);
|
|
418
|
+
|
|
419
|
+
// Editing the template fans out again.
|
|
420
|
+
await templates.replace({ kind: "admin" }, {}, [
|
|
421
|
+
{
|
|
422
|
+
kind: "marketplace",
|
|
423
|
+
key: "example-mk",
|
|
424
|
+
payload: { source: "example-org/plugins", ref: "v2" },
|
|
425
|
+
},
|
|
426
|
+
{ kind: "plugin", key: "tool@example-mk" },
|
|
427
|
+
]);
|
|
428
|
+
cfg = await config();
|
|
429
|
+
expect(cfg.marketplaces[0]?.ref).toBe("v2");
|
|
430
|
+
|
|
431
|
+
// A tombstoned row stays out of the bundle and is not re-added by the
|
|
432
|
+
// next fan-out.
|
|
433
|
+
const marketplaces = new AgentMarketplaceService(prisma);
|
|
434
|
+
const row = await prisma.agentMarketplace.findUniqueOrThrow({
|
|
435
|
+
where: { agentId_name: { agentId: agent.id, name: "example-mk" } },
|
|
436
|
+
});
|
|
437
|
+
await marketplaces.remove(agent.id, row.id);
|
|
438
|
+
await templates.replace({ kind: "admin" }, {}, [
|
|
439
|
+
{
|
|
440
|
+
kind: "marketplace",
|
|
441
|
+
key: "example-mk",
|
|
442
|
+
payload: { source: "example-org/plugins", ref: "v3" },
|
|
443
|
+
},
|
|
444
|
+
{ kind: "plugin", key: "tool@example-mk" },
|
|
445
|
+
]);
|
|
446
|
+
cfg = await config();
|
|
447
|
+
expect(cfg.marketplaces).toEqual([]);
|
|
448
|
+
const tomb = await prisma.agentMarketplace.findUniqueOrThrow({
|
|
449
|
+
where: { id: row.id },
|
|
450
|
+
});
|
|
451
|
+
expect(tomb).toMatchObject({
|
|
452
|
+
enabled: false,
|
|
453
|
+
rowSource: "custom",
|
|
454
|
+
ref: "v2",
|
|
455
|
+
});
|
|
456
|
+
});
|
|
457
|
+
});
|
|
368
458
|
});
|
|
@@ -175,6 +175,86 @@ describeOrSkip("/templates (integration)", () => {
|
|
|
175
175
|
});
|
|
176
176
|
});
|
|
177
177
|
|
|
178
|
+
describe("marketplace items", () => {
|
|
179
|
+
const mkt = (key: string, payload: Record<string, unknown>) => ({
|
|
180
|
+
kind: "marketplace",
|
|
181
|
+
key,
|
|
182
|
+
payload,
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it("round-trips a marketplace item unchanged", async () => {
|
|
186
|
+
const item = mkt("example-plugins", { source: "example-org/plugins" });
|
|
187
|
+
const put = await call("PUT", "/templates", admin, { items: [item] });
|
|
188
|
+
expect([200, 201]).toContain(put.status);
|
|
189
|
+
const list = (await (await call("GET", "/templates", admin)).json()) as {
|
|
190
|
+
templates: {
|
|
191
|
+
items: { kind: string; key: string; payload: unknown }[];
|
|
192
|
+
}[];
|
|
193
|
+
};
|
|
194
|
+
const got = list.templates[0]?.items.find(
|
|
195
|
+
(i) => i.kind === "marketplace",
|
|
196
|
+
);
|
|
197
|
+
expect(got).toMatchObject(item);
|
|
198
|
+
const { id } = (await put.json()) as { id: string };
|
|
199
|
+
const row = await prisma.agentTemplateItem.findFirstOrThrow({
|
|
200
|
+
where: { templateId: id, kind: "marketplace" },
|
|
201
|
+
});
|
|
202
|
+
expect(row.payload).toEqual({ source: "example-org/plugins" });
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it("supports item upsert and delete", async () => {
|
|
206
|
+
const put = await call("PUT", "/templates", admin, { items: [] });
|
|
207
|
+
const { id } = (await put.json()) as { id: string };
|
|
208
|
+
const up = await call(
|
|
209
|
+
"PUT",
|
|
210
|
+
`/templates/${id}/items`,
|
|
211
|
+
admin,
|
|
212
|
+
mkt("m1", { source: "o/r", ref: "v1" }),
|
|
213
|
+
);
|
|
214
|
+
expect(up.status).toBe(200);
|
|
215
|
+
const del = await call(
|
|
216
|
+
"DELETE",
|
|
217
|
+
`/templates/${id}/items?kind=marketplace&key=m1`,
|
|
218
|
+
admin,
|
|
219
|
+
);
|
|
220
|
+
expect(del.status).toBeLessThan(300);
|
|
221
|
+
expect(await prisma.agentTemplateItem.count()).toBe(0);
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it.each([
|
|
225
|
+
["credentials in source", "m", { source: "https://u:p@example.com/o/r" }],
|
|
226
|
+
["malformed owner/repo", "m", { source: "not a repo" }],
|
|
227
|
+
["invalid name", "-bad name", { source: "o/r" }],
|
|
228
|
+
["invalid ref", "m", { source: "o/r", ref: "a..b" }],
|
|
229
|
+
])("422 for %s", async (_label, key, payload) => {
|
|
230
|
+
const res = await call("PUT", "/templates", admin, {
|
|
231
|
+
items: [mkt(key, payload)],
|
|
232
|
+
});
|
|
233
|
+
expect(res.status).toBe(422);
|
|
234
|
+
expect(await prisma.agentTemplate.count()).toBe(0);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it("422 for a duplicate marketplace key in one body", async () => {
|
|
238
|
+
const item = mkt("m", { source: "o/r" });
|
|
239
|
+
const res = await call("PUT", "/templates", admin, {
|
|
240
|
+
items: [item, item],
|
|
241
|
+
});
|
|
242
|
+
expect(res.status).toBe(422);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it("403 for an account caller writing another account or the deployment template", async () => {
|
|
246
|
+
const b = await cookie("b@example.com");
|
|
247
|
+
const items = [mkt("m", { source: "o/r" })];
|
|
248
|
+
expect(
|
|
249
|
+
(await call("PUT", "/templates", b, { accountId: accountA, items }))
|
|
250
|
+
.status,
|
|
251
|
+
).toBe(403);
|
|
252
|
+
expect(
|
|
253
|
+
(await call("PUT", "/templates", b, { accountId: null, items })).status,
|
|
254
|
+
).toBe(403);
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
|
|
178
258
|
describe("crypto and masking", () => {
|
|
179
259
|
it("encrypts env values at rest and masks secrets on GET", async () => {
|
|
180
260
|
const put = await call("PUT", "/templates", admin, {
|
|
@@ -102,7 +102,7 @@ const putTemplateRoute = createRoute({
|
|
|
102
102
|
tags: ["templates"],
|
|
103
103
|
summary: "Create or replace an agent template",
|
|
104
104
|
description:
|
|
105
|
-
'Creates the template for the (accountId, specialty) scope, or replaces the items of the existing one (201 / 200). Account callers may only write their own account (403 otherwise); only platform admins write deployment-wide templates. 422 for an unknown specialty, a denylisted env key (per-agent credentials: Slack tokens, GH_TOKEN, Anthropic credentials, task-store URL/token), an invalid item payload, a cron item without a name key, or a duplicate (kind, key). Env values are encrypted at rest; a secret env item sent as `"***"` keeps its stored value.',
|
|
105
|
+
'Creates the template for the (accountId, specialty) scope, or replaces the items of the existing one (201 / 200). Account callers may only write their own account (403 otherwise); only platform admins write deployment-wide templates. 422 for an unknown specialty, a denylisted env key (per-agent credentials: Slack tokens, GH_TOKEN, Anthropic credentials, task-store URL/token), an invalid item payload, a cron item without a name key, a marketplace item with an invalid name, source or ref (same messages as POST /agents/:id/marketplaces), or a duplicate (kind, key). Env values are encrypted at rest; a secret env item sent as `"***"` keeps its stored value.',
|
|
106
106
|
request: {
|
|
107
107
|
body: {
|
|
108
108
|
content: { "application/json": { schema: PutTemplateBodySchema } },
|
package/src/agent-templates.ts
CHANGED
|
@@ -21,6 +21,7 @@ import { z } from "zod";
|
|
|
21
21
|
import type { PrismaClient } from "../prisma/client/client.ts";
|
|
22
22
|
import {
|
|
23
23
|
AGENT_TEMPLATE_ITEM_KINDS,
|
|
24
|
+
assertMarketplaceTemplateItem,
|
|
24
25
|
type AgentTemplateItemKind,
|
|
25
26
|
assertTemplateEnvKeyAllowed,
|
|
26
27
|
openTemplateItemPayload,
|
|
@@ -92,6 +93,10 @@ const PAYLOAD_SCHEMAS = {
|
|
|
92
93
|
enabled: z.boolean().optional(),
|
|
93
94
|
preCheck: z.string().nullable().optional(),
|
|
94
95
|
}),
|
|
96
|
+
marketplace: z.object({
|
|
97
|
+
source: z.string(),
|
|
98
|
+
ref: z.string().nullable().optional(),
|
|
99
|
+
}),
|
|
95
100
|
} satisfies Record<AgentTemplateItemKind, z.ZodType>;
|
|
96
101
|
|
|
97
102
|
/** Validates one item and returns its normalized (still plaintext) payload. */
|
|
@@ -129,6 +134,12 @@ export function normalizeTemplateItem(
|
|
|
129
134
|
`invalid ${item.kind} item ${key}: ${detail}`,
|
|
130
135
|
);
|
|
131
136
|
}
|
|
137
|
+
if (item.kind === "marketplace") {
|
|
138
|
+
assertMarketplaceTemplateItem(
|
|
139
|
+
key,
|
|
140
|
+
parsed.data as { source: string; ref?: string | null },
|
|
141
|
+
);
|
|
142
|
+
}
|
|
132
143
|
return { kind: item.kind, key, payload: parsed.data };
|
|
133
144
|
}
|
|
134
145
|
|
package/src/agents-api.ts
CHANGED
|
@@ -61,6 +61,7 @@ import { deleteAgentFully } from "./agent-deletion.ts";
|
|
|
61
61
|
import type { AgentEnvService } from "./agent-envs.ts";
|
|
62
62
|
import type { AgentGitHubInstallationsService } from "./agent-github-installations.ts";
|
|
63
63
|
import { AgentMarketplaceService } from "./agent-marketplaces.ts";
|
|
64
|
+
import { AGENT_TEMPLATE_ITEM_KINDS } from "./agent-template-items.ts";
|
|
64
65
|
import type { AgentMemberService } from "./agent-members.ts";
|
|
65
66
|
import type { AgentPluginService } from "./agent-plugins.ts";
|
|
66
67
|
import {
|
|
@@ -1083,6 +1084,7 @@ const TemplateReconcileResultSchema = z
|
|
|
1083
1084
|
tool: TemplateReconcileCountsSchema,
|
|
1084
1085
|
plugin: TemplateReconcileCountsSchema,
|
|
1085
1086
|
cron: TemplateReconcileCountsSchema,
|
|
1087
|
+
marketplace: TemplateReconcileCountsSchema.optional(),
|
|
1086
1088
|
}),
|
|
1087
1089
|
systemCrons: z.object({
|
|
1088
1090
|
created: z.number().int(),
|
|
@@ -1091,12 +1093,13 @@ const TemplateReconcileResultSchema = z
|
|
|
1091
1093
|
}),
|
|
1092
1094
|
conflicts: z.array(
|
|
1093
1095
|
z.object({
|
|
1094
|
-
kind: z.enum(
|
|
1096
|
+
kind: z.enum(AGENT_TEMPLATE_ITEM_KINDS),
|
|
1095
1097
|
key: z.string(),
|
|
1096
1098
|
reason: z.enum([
|
|
1097
1099
|
"cron_name_conflict",
|
|
1098
1100
|
"invalid_cron_schedule",
|
|
1099
1101
|
"denylisted_env_key",
|
|
1102
|
+
"marketplace_conflict",
|
|
1100
1103
|
]),
|
|
1101
1104
|
}),
|
|
1102
1105
|
),
|
|
@@ -1110,7 +1113,7 @@ const TemplateDetachResultSchema = z
|
|
|
1110
1113
|
|
|
1111
1114
|
const ResetTemplateBodySchema = z
|
|
1112
1115
|
.object({
|
|
1113
|
-
kind: z.enum(
|
|
1116
|
+
kind: z.enum(AGENT_TEMPLATE_ITEM_KINDS).optional(),
|
|
1114
1117
|
key: z.string().min(1).optional(),
|
|
1115
1118
|
})
|
|
1116
1119
|
.refine((b) => (b.kind === undefined) === (b.key === undefined), {
|
package/src/openapi-schemas.ts
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
import { isGithubLogin } from "@app-vitals/agent-lib/github-login";
|
|
10
10
|
import { isOrgRepo } from "@app-vitals/agent-lib/org-repo";
|
|
11
11
|
import { z } from "@hono/zod-openapi";
|
|
12
|
+
import { AGENT_TEMPLATE_ITEM_KINDS } from "./agent-template-items.ts";
|
|
12
13
|
|
|
13
14
|
// ─── Common ───────────────────────────────────────────────────────────────────
|
|
14
15
|
|
|
@@ -1566,7 +1567,7 @@ export const PatchAccountBodySchema = z
|
|
|
1566
1567
|
// ─── Agent templates (ATP-1.3) ────────────────────────────────────────────────
|
|
1567
1568
|
|
|
1568
1569
|
export const TemplateItemKindSchema = z
|
|
1569
|
-
.enum(
|
|
1570
|
+
.enum(AGENT_TEMPLATE_ITEM_KINDS)
|
|
1570
1571
|
.openapi("TemplateItemKind");
|
|
1571
1572
|
|
|
1572
1573
|
const TemplateItemBaseSchema = z.object({
|
|
@@ -1581,7 +1582,7 @@ const TemplateItemBaseSchema = z.object({
|
|
|
1581
1582
|
.optional()
|
|
1582
1583
|
.openapi({
|
|
1583
1584
|
description:
|
|
1584
|
-
"Kind-specific fields. env: {value, secret?}; tool: {enabled?}; plugin: {version?, enabled?}; cron: {schedule, prompt, channel?, user?, silent?, enabled?, preCheck?}.",
|
|
1585
|
+
"Kind-specific fields. env: {value, secret?}; tool: {enabled?}; plugin: {version?, enabled?}; cron: {schedule, prompt, channel?, user?, silent?, enabled?, preCheck?}; marketplace: {source, ref?} (key = marketplace name).",
|
|
1585
1586
|
example: { value: "lin_abc", secret: true },
|
|
1586
1587
|
}),
|
|
1587
1588
|
});
|