@agentrica/mcp 0.4.0 → 0.6.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/CHANGELOG.md +75 -0
- package/README.en.md +101 -27
- package/README.md +100 -28
- package/dist/catalog/src/departments.d.ts +2 -0
- package/dist/catalog/src/departments.js +5 -0
- package/dist/mcp/src/init.d.ts +1 -1
- package/dist/mcp/src/init.js +9 -6
- package/dist/mcp/src/page/app.js +74 -5
- package/dist/mcp/src/page/index.html +5 -0
- package/dist/mcp/src/page/style.css +9 -0
- package/dist/mcp/src/panel.d.ts +17 -1
- package/dist/mcp/src/panel.js +40 -2
- package/dist/mcp/src/prompt.d.ts +8 -2
- package/dist/mcp/src/prompt.js +18 -2
- package/dist/mcp/src/ratings.d.ts +86 -0
- package/dist/mcp/src/ratings.js +159 -0
- package/dist/mcp/src/server.js +183 -19
- package/dist/mcp/src/setup.d.ts +25 -0
- package/dist/mcp/src/setup.js +161 -0
- package/dist/mcp/src/update.d.ts +10 -0
- package/dist/mcp/src/update.js +65 -0
- package/package.json +6 -4
- package/starter/CONVENTIONS.md +1 -1
package/dist/mcp/src/init.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/** I subagent di Agentrica nominano il progetto solo in questa frase (lo controlla init.test.ts). */
|
|
2
2
|
export declare const SUBAGENT_PROJECT = "del progetto Agentrica.";
|
|
3
3
|
/** I file del progetto nuovo, per percorso relativo, con i segnaposto già sostituiti. */
|
|
4
|
-
export declare function projectFiles(name: string, path: string, date: string): Map<string, string>;
|
|
4
|
+
export declare function projectFiles(name: string, path: string, date: string, platform?: NodeJS.Platform): Map<string, string>;
|
|
5
5
|
/** Esegue il comando; restituisce il codice di uscita. */
|
|
6
6
|
export declare function runInit(args: string[]): number;
|
package/dist/mcp/src/init.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
// sola lettura. Se anche un solo file esiste già non scrive nulla.
|
|
6
6
|
import { execFileSync } from "node:child_process";
|
|
7
7
|
import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
8
|
-
import { basename, dirname, join, relative, resolve } from "node:path";
|
|
8
|
+
import { basename, dirname, join, relative, resolve, sep } from "node:path";
|
|
9
9
|
import { parseArgs } from "node:util";
|
|
10
10
|
import { AGENTS_DIR } from "./catalog.js";
|
|
11
11
|
import { VERSION } from "./prompt.js";
|
|
@@ -20,23 +20,26 @@ const RENAMED = { gitignore: ".gitignore" };
|
|
|
20
20
|
function filesIn(dir) {
|
|
21
21
|
return readdirSync(dir, { recursive: true, withFileTypes: true })
|
|
22
22
|
.filter((entry) => entry.isFile() && entry.name !== ".DS_Store")
|
|
23
|
-
|
|
23
|
+
// Percorsi con «/» anche su Windows: sono le chiavi dei file del progetto.
|
|
24
|
+
.map((entry) => relative(dir, join(entry.parentPath, entry.name)).split(sep).join("/"))
|
|
24
25
|
.sort();
|
|
25
26
|
}
|
|
26
27
|
/** I file del progetto nuovo, per percorso relativo, con i segnaposto già sostituiti. */
|
|
27
|
-
export function projectFiles(name, path, date) {
|
|
28
|
+
export function projectFiles(name, path, date, platform = process.platform) {
|
|
28
29
|
const values = { "{{PROJECT_NAME}}": name, "{{PROJECT_PATH}}": path, "{{DATE}}": date };
|
|
29
30
|
const fill = (text) => Object.entries(values).reduce((out, [key, value]) => out.split(key).join(value), text);
|
|
30
31
|
const files = new Map();
|
|
31
32
|
for (const file of filesIn(STARTER_DIR))
|
|
32
33
|
files.set(RENAMED[file] ?? file, fill(readFileSync(join(STARTER_DIR, file), "utf8")));
|
|
33
34
|
for (const file of filesIn(AGENTS_DIR))
|
|
34
|
-
files.set(
|
|
35
|
+
files.set(`agents/${file}`, readFileSync(join(AGENTS_DIR, file), "utf8"));
|
|
35
36
|
for (const file of filesIn(SUBAGENTS_DIR)) {
|
|
36
37
|
const text = readFileSync(join(SUBAGENTS_DIR, file), "utf8");
|
|
37
|
-
files.set(
|
|
38
|
+
files.set(`.claude/agents/${file}`, text.split(SUBAGENT_PROJECT).join(`del progetto ${name}.`));
|
|
38
39
|
}
|
|
39
|
-
|
|
40
|
+
// Su Windows npx è npx.cmd, che Claude Code non avvia senza cmd /c (F23).
|
|
41
|
+
const npx = ["-y", `@agentrica/mcp@${VERSION}`, "--panel"];
|
|
42
|
+
const server = platform === "win32" ? { command: "cmd", args: ["/c", "npx", ...npx] } : { command: "npx", args: npx };
|
|
40
43
|
files.set(".mcp.json", `${JSON.stringify({ mcpServers: { agentrica: server } }, null, 2)}\n`);
|
|
41
44
|
return files;
|
|
42
45
|
}
|
package/dist/mcp/src/page/app.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Pagina del pannello: legge /api/state, si aggiorna con /api/events e manda le risposte a /api/answer.
|
|
1
|
+
// Pagina del pannello: legge /api/state (con l'avviso di versione nuova, F23), si aggiorna con /api/events e manda le risposte a /api/answer e i voti a /api/rate.
|
|
2
2
|
// I dati del progetto entrano nel DOM solo come testo (textContent), mai come HTML.
|
|
3
3
|
"use strict";
|
|
4
4
|
|
|
@@ -33,7 +33,10 @@ function inline(text) {
|
|
|
33
33
|
const time = (iso) => new Date(iso).toLocaleTimeString("it-IT", { hour: "2-digit", minute: "2-digit" });
|
|
34
34
|
const empty = (text) => el("p", { class: "empty" }, text);
|
|
35
35
|
|
|
36
|
-
function renderHeader({ project }) {
|
|
36
|
+
function renderHeader({ project, update }) {
|
|
37
|
+
const banner = $("update");
|
|
38
|
+
banner.hidden = !update;
|
|
39
|
+
banner.textContent = update ?? "";
|
|
37
40
|
const plural = (n, one, many) => `${n} ${n === 1 ? one : many}`;
|
|
38
41
|
const files = project.git
|
|
39
42
|
? [
|
|
@@ -132,12 +135,12 @@ function renderActivity({ activity }) {
|
|
|
132
135
|
: el(
|
|
133
136
|
"ol",
|
|
134
137
|
{ class: "activity" },
|
|
135
|
-
activity.map((a) =>
|
|
138
|
+
activity.map((a, i) =>
|
|
136
139
|
el(
|
|
137
140
|
"li",
|
|
138
|
-
{ "data-dept": a.department },
|
|
141
|
+
{ "data-dept": a.department, class: i === 0 ? "active" : null },
|
|
139
142
|
el("time", { datetime: a.time, class: "mono" }, time(a.time)),
|
|
140
|
-
el("span", { class: "dot" }, el("strong", {}, a.codeName), ` — ${a.role}
|
|
143
|
+
el("span", { class: "dot" }, el("strong", {}, a.codeName), ` — ${a.role}, reparto ${a.department}`, i === 0 && el("span", { class: "now" }, "attivo")),
|
|
141
144
|
a.task && el("span", { class: "task" }, a.task),
|
|
142
145
|
),
|
|
143
146
|
),
|
|
@@ -145,6 +148,71 @@ function renderActivity({ activity }) {
|
|
|
145
148
|
);
|
|
146
149
|
}
|
|
147
150
|
|
|
151
|
+
// Un modulo per agente usato, creato una volta sola: un aggiornamento non deve cancellare un commento a metà.
|
|
152
|
+
function renderRatings({ activity, rating }) {
|
|
153
|
+
const card = $("ratings-card");
|
|
154
|
+
card.hidden = !rating;
|
|
155
|
+
if (!rating) return;
|
|
156
|
+
const box = $("ratings");
|
|
157
|
+
const agents = [...new Map(activity.map((a) => [a.id, a])).values()];
|
|
158
|
+
for (const agent of agents) {
|
|
159
|
+
const node = box.querySelector(`[data-agent="${CSS.escape(agent.id)}"]`);
|
|
160
|
+
const done = rating.rated.includes(agent.id);
|
|
161
|
+
if (!node) box.append(done ? ratedNote(agent) : rateForm(agent, rating));
|
|
162
|
+
else if (done && node.tagName === "FORM" && !node.dataset.sent) node.replaceWith(ratedNote(agent));
|
|
163
|
+
}
|
|
164
|
+
box.querySelector(".empty")?.remove();
|
|
165
|
+
if (!box.querySelector("[data-agent]")) box.append(empty("Quando usi un agente, qui puoi dargli un voto."));
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const ratedNote = (agent) => el("p", { class: "sent", "data-agent": agent.id, "data-dept": agent.department }, el("strong", {}, agent.codeName), " — valutato in questa sessione.");
|
|
169
|
+
|
|
170
|
+
function rateForm(agent, rating) {
|
|
171
|
+
const name = `r-${agent.id}`;
|
|
172
|
+
const error = el("p", { class: "error", role: "alert", hidden: true });
|
|
173
|
+
const stars = [1, 2, 3, 4, 5].map((n) =>
|
|
174
|
+
el("label", { class: "star" }, el("input", { type: "radio", name, value: String(n) }), el("span", {}, `${n}`)),
|
|
175
|
+
);
|
|
176
|
+
const form = el(
|
|
177
|
+
"form",
|
|
178
|
+
{ class: "question", "data-agent": agent.id, "data-dept": agent.department, onsubmit: (event) => sendRating(event, agent, form, error) },
|
|
179
|
+
el(
|
|
180
|
+
"fieldset",
|
|
181
|
+
{},
|
|
182
|
+
el("legend", {}, el("span", { class: "dot" }, el("strong", {}, agent.codeName)), ` — ${agent.role}, reparto ${agent.department}. Voto da 1 a 5:`),
|
|
183
|
+
el("div", { class: "stars" }, stars),
|
|
184
|
+
),
|
|
185
|
+
el("label", { class: "note" }, "Cosa è andato bene o cosa migliorare (facoltativo)", el("textarea", { name: "comment", maxlength: String(rating.maxComment) })),
|
|
186
|
+
el("label", { class: "check" }, el("input", { type: "checkbox", name: "publish" }), "Puoi pubblicare il mio commento sul sito di Agentrica"),
|
|
187
|
+
el("p", { class: "hint" }, rating.notice),
|
|
188
|
+
el("button", { type: "submit", class: "send" }, "Invia la valutazione"),
|
|
189
|
+
error,
|
|
190
|
+
);
|
|
191
|
+
return form;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
async function sendRating(event, agent, form, error) {
|
|
195
|
+
event.preventDefault();
|
|
196
|
+
const picked = form.querySelector("input[type=radio]:checked");
|
|
197
|
+
if (!picked) {
|
|
198
|
+
error.textContent = "Scegli un voto da 1 a 5.";
|
|
199
|
+
error.hidden = false;
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
const body = { agentId: agent.id, rating: Number(picked.value), comment: form.elements.comment.value, publish: form.elements.publish.checked };
|
|
203
|
+
const response = await fetch("/api/rate", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) }).catch(() => null);
|
|
204
|
+
const result = response ? await response.json().catch(() => null) : null;
|
|
205
|
+
if (!result?.ok) {
|
|
206
|
+
error.textContent = result?.message ?? (response ? "Valutazione non valida." : "Il pannello non risponde: la sessione è ancora aperta?");
|
|
207
|
+
error.hidden = false;
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
form.dataset.sent = "true";
|
|
211
|
+
form.replaceChildren(
|
|
212
|
+
el("p", { class: "sent", role: "status" }, `Valutazione di ${agent.codeName} inviata, grazie. Codice della valutazione: `, el("code", {}, result.usageId), ". Conservalo per chiedere di cancellarla o di non pubblicare il commento."),
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
|
|
148
216
|
function table(head, rows) {
|
|
149
217
|
return el(
|
|
150
218
|
"div",
|
|
@@ -277,6 +345,7 @@ function render() {
|
|
|
277
345
|
renderCounts(state);
|
|
278
346
|
renderQuestions(state);
|
|
279
347
|
renderActivity(state);
|
|
348
|
+
renderRatings(state);
|
|
280
349
|
renderPhases(state);
|
|
281
350
|
renderFilters();
|
|
282
351
|
renderFindings(state);
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
</header>
|
|
24
24
|
<div id="announce" class="sr-only" aria-live="polite"></div>
|
|
25
25
|
<main class="wrap">
|
|
26
|
+
<p class="update" id="update" role="status" hidden></p>
|
|
26
27
|
<p class="problems" id="problems" hidden></p>
|
|
27
28
|
|
|
28
29
|
<div id="view-sessione" class="view" data-view="sessione">
|
|
@@ -36,6 +37,10 @@
|
|
|
36
37
|
<h2 id="h-agents">Agenti in uso</h2>
|
|
37
38
|
<div id="activity"></div>
|
|
38
39
|
</section>
|
|
40
|
+
<section aria-labelledby="h-ratings" class="card" id="ratings-card" hidden>
|
|
41
|
+
<h2 id="h-ratings">Valuta gli agenti</h2>
|
|
42
|
+
<div id="ratings"></div>
|
|
43
|
+
</section>
|
|
39
44
|
</div>
|
|
40
45
|
</div>
|
|
41
46
|
|
|
@@ -67,6 +67,7 @@ h1:focus { outline: none; }
|
|
|
67
67
|
h2 { font-size: 1.15rem; margin: 0 0 12px; }
|
|
68
68
|
h3 { font-size: 1rem; margin: 16px 0 8px; }
|
|
69
69
|
.empty, .hint { color: var(--muted-foreground); margin: 0; }
|
|
70
|
+
.update { margin: 0 0 12px; padding: 12px 16px; border: 1px solid var(--aisa); border-radius: var(--radius); }
|
|
70
71
|
.problems { margin: 0; padding: 12px 16px; border: 1px solid var(--destructive); border-radius: var(--radius); white-space: pre-line; }
|
|
71
72
|
|
|
72
73
|
.question { border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 16px; background: var(--secondary); }
|
|
@@ -91,6 +92,14 @@ ol.activity { list-style: none; margin: 0; padding: 0; }
|
|
|
91
92
|
ol.activity li { display: grid; grid-template-columns: 3.5em 1fr; gap: 0 8px; padding: 6px 0; border-top: 1px solid var(--border); }
|
|
92
93
|
ol.activity li:first-child { border-top: 0; }
|
|
93
94
|
.dot::before { content: "●"; color: var(--dept); margin-right: 6px; }
|
|
95
|
+
ol.activity li.active strong { color: var(--dept); }
|
|
96
|
+
.now { margin-left: 8px; font-size: 0.8rem; color: var(--background); background: var(--dept); border-radius: 999px; padding: 0 8px; font-weight: 600; }
|
|
97
|
+
.stars { display: flex; flex-wrap: wrap; gap: 6px; }
|
|
98
|
+
.star { display: inline-flex; align-items: center; gap: 4px; min-height: 44px; min-width: 44px; padding: 0 8px; border: 1px solid var(--border); border-radius: 8px; cursor: pointer; }
|
|
99
|
+
.star:has(input:checked) { border-color: var(--dept); color: var(--dept); font-weight: 700; }
|
|
100
|
+
.star input { accent-color: var(--dept); }
|
|
101
|
+
label.check { display: flex; gap: 8px; align-items: center; margin-top: 8px; }
|
|
102
|
+
#ratings .question + .question, #ratings .sent + .question, #ratings .question + .sent { margin-top: 12px; }
|
|
94
103
|
.task { grid-column: 2; color: var(--muted-foreground); overflow-wrap: anywhere; }
|
|
95
104
|
|
|
96
105
|
.scroll { overflow-x: auto; }
|
package/dist/mcp/src/panel.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type SendResult } from "./ratings.ts";
|
|
1
2
|
export interface Choice {
|
|
2
3
|
label: string;
|
|
3
4
|
description: string;
|
|
@@ -24,6 +25,18 @@ export interface Activity {
|
|
|
24
25
|
department: string;
|
|
25
26
|
task: string;
|
|
26
27
|
}
|
|
28
|
+
/** Le valutazioni viste dal pannello (F20): il server MCP le passa, il pannello non apre connessioni da solo. */
|
|
29
|
+
export interface PanelRatings {
|
|
30
|
+
isRated(agentId: string): boolean;
|
|
31
|
+
/** Invia una valutazione confermata nella pagina. */
|
|
32
|
+
rate(input: RateInput): Promise<SendResult>;
|
|
33
|
+
}
|
|
34
|
+
export interface RateInput {
|
|
35
|
+
agentId: string;
|
|
36
|
+
rating: number;
|
|
37
|
+
comment: string;
|
|
38
|
+
publish: boolean;
|
|
39
|
+
}
|
|
27
40
|
export interface Panel {
|
|
28
41
|
/** URL con il token: apre la sessione del browser. */
|
|
29
42
|
url: string;
|
|
@@ -35,6 +48,9 @@ export interface Panel {
|
|
|
35
48
|
}
|
|
36
49
|
/** Controlla una risposta della pagina contro la domanda: etichette esistenti, una sola se la scelta è singola. */
|
|
37
50
|
export declare function parseAnswer(question: QuestionInput, body: unknown): Answer | null;
|
|
51
|
+
/** Controlla una valutazione dalla pagina: agente fra quelli usati, voto intero 1–5, commento corto, casella booleana. */
|
|
52
|
+
export declare function parseRate(used: ReadonlySet<string>, body: unknown): RateInput | null;
|
|
38
53
|
/** Apre l'URL nel browser predefinito; se non riesce (es. sessione remota) l'URL resta nella risposta del tool. */
|
|
39
54
|
export declare function openBrowser(url: string): void;
|
|
40
|
-
|
|
55
|
+
/** `update` dà l'avviso di versione nuova (F23), se c'è: il pannello lo mostra in un banner. */
|
|
56
|
+
export declare function startPanel(root: string, ratings?: PanelRatings, update?: () => string | undefined): Promise<Panel>;
|
package/dist/mcp/src/panel.js
CHANGED
|
@@ -9,6 +9,7 @@ import { readFileSync } from "node:fs";
|
|
|
9
9
|
import { createServer } from "node:http";
|
|
10
10
|
import { join } from "node:path";
|
|
11
11
|
import { readProject } from "./project.js";
|
|
12
|
+
import { MAX_COMMENT, NOTICE } from "./ratings.js";
|
|
12
13
|
const MAX_BODY_BYTES = 16_000;
|
|
13
14
|
const MAX_NOTE = 2000;
|
|
14
15
|
const MAX_ACTIVITY = 20;
|
|
@@ -79,6 +80,19 @@ export function parseAnswer(question, body) {
|
|
|
79
80
|
return null;
|
|
80
81
|
return { choices: picked, note: note.trim() };
|
|
81
82
|
}
|
|
83
|
+
/** Controlla una valutazione dalla pagina: agente fra quelli usati, voto intero 1–5, commento corto, casella booleana. */
|
|
84
|
+
export function parseRate(used, body) {
|
|
85
|
+
if (typeof body !== "object" || body === null)
|
|
86
|
+
return null;
|
|
87
|
+
const { agentId, rating, comment = "", publish = false } = body;
|
|
88
|
+
if (typeof agentId !== "string" || !used.has(agentId))
|
|
89
|
+
return null;
|
|
90
|
+
if (typeof rating !== "number" || !Number.isInteger(rating) || rating < 1 || rating > 5)
|
|
91
|
+
return null;
|
|
92
|
+
if (typeof comment !== "string" || comment.length > MAX_COMMENT || typeof publish !== "boolean")
|
|
93
|
+
return null;
|
|
94
|
+
return { agentId, rating, comment: comment.trim(), publish };
|
|
95
|
+
}
|
|
82
96
|
/** Apre l'URL nel browser predefinito; se non riesce (es. sessione remota) l'URL resta nella risposta del tool. */
|
|
83
97
|
export function openBrowser(url) {
|
|
84
98
|
const [command, args] = process.platform === "darwin" ? ["open", [url]] : process.platform === "win32" ? ["cmd", ["/c", "start", "", url]] : ["xdg-open", [url]];
|
|
@@ -91,7 +105,9 @@ export function openBrowser(url) {
|
|
|
91
105
|
// Nessun browser: l'utente apre l'URL a mano.
|
|
92
106
|
}
|
|
93
107
|
}
|
|
94
|
-
|
|
108
|
+
/** `update` dà l'avviso di versione nuova (F23), se c'è: il pannello lo mostra in un banner. */
|
|
109
|
+
export async function startPanel(root, ratings, update = () => undefined) {
|
|
110
|
+
const pageDir = join(import.meta.dirname, "page");
|
|
95
111
|
const token = randomBytes(32).toString("hex");
|
|
96
112
|
const pages = Object.fromEntries(Object.entries(PAGE_FILES).map(([route, [file, type]]) => [route, { body: readFileSync(join(pageDir, file)), type }]));
|
|
97
113
|
const pending = new Map();
|
|
@@ -135,7 +151,9 @@ export async function startPanel(root, pageDir = join(import.meta.dirname, "page
|
|
|
135
151
|
if (req.method === "GET" && url.pathname === "/api/state") {
|
|
136
152
|
const project = await readProject(root);
|
|
137
153
|
const questions = [...pending.values()].map((p) => p.question);
|
|
138
|
-
|
|
154
|
+
const rated = ratings ? [...new Set(activity.map((a) => a.id))].filter((id) => ratings.isRated(id)) : [];
|
|
155
|
+
const rating = ratings ? { notice: NOTICE, maxComment: MAX_COMMENT, rated } : null;
|
|
156
|
+
return send(res, 200, JSON.stringify({ project, questions, activity, rating, update: update() ?? null }), "application/json; charset=utf-8");
|
|
139
157
|
}
|
|
140
158
|
if (req.method === "GET" && url.pathname === "/api/events") {
|
|
141
159
|
res.writeHead(200, { ...SECURITY_HEADERS, "Content-Type": "text/event-stream; charset=utf-8", Connection: "keep-alive" });
|
|
@@ -144,6 +162,26 @@ export async function startPanel(root, pageDir = join(import.meta.dirname, "page
|
|
|
144
162
|
req.on("close", () => viewers.delete(res));
|
|
145
163
|
return;
|
|
146
164
|
}
|
|
165
|
+
if (req.method === "POST" && url.pathname === "/api/rate" && ratings) {
|
|
166
|
+
if (req.headers.origin !== origin)
|
|
167
|
+
return send(res, 403, "Origine non valida.");
|
|
168
|
+
if (!req.headers["content-type"]?.startsWith("application/json"))
|
|
169
|
+
return send(res, 415, "Serve JSON.");
|
|
170
|
+
let body;
|
|
171
|
+
try {
|
|
172
|
+
body = JSON.parse(await readBody(req));
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
return send(res, 400, "Richiesta non valida.");
|
|
176
|
+
}
|
|
177
|
+
const input = parseRate(new Set(activity.map((a) => a.id)), body);
|
|
178
|
+
if (!input)
|
|
179
|
+
return send(res, 400, "Valutazione non valida.");
|
|
180
|
+
// L'invio dalla pagina è la conferma dell'utente: la pagina mostra l'avviso accanto al pulsante.
|
|
181
|
+
const result = await ratings.rate(input);
|
|
182
|
+
changed();
|
|
183
|
+
return send(res, result.ok ? 200 : 502, JSON.stringify(result), "application/json; charset=utf-8");
|
|
184
|
+
}
|
|
147
185
|
if (req.method === "POST" && url.pathname === "/api/answer") {
|
|
148
186
|
if (req.headers.origin !== origin)
|
|
149
187
|
return send(res, 403, "Origine non valida.");
|
package/dist/mcp/src/prompt.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Agent, Department } from "../../catalog/src/index.ts";
|
|
2
2
|
/** Versione del server, uguale a quella di package.json (lo controlla prompt.test.ts). */
|
|
3
|
-
export declare const VERSION = "0.
|
|
3
|
+
export declare const VERSION = "0.6.0";
|
|
4
4
|
/**
|
|
5
5
|
* Un reparto della squadra di --agents. Non ha `director`: con un reparto filtrato il direttore può essere fuori
|
|
6
6
|
* squadra, così il tipo impedisce di usarlo senza controllare (rilievo di F12).
|
|
@@ -14,6 +14,8 @@ export interface Found {
|
|
|
14
14
|
export declare function findAgent(catalog: readonly SquadDepartment[], id: string): Found | undefined;
|
|
15
15
|
/** «AISA-02 · ALTAIR». */
|
|
16
16
|
export declare function codeName(agent: Agent): string;
|
|
17
|
+
/** Nome del prompt (da 0.6.0): id in minuscolo e reparto, «aisa-02-software», così il menu dei comandi mostra il reparto. */
|
|
18
|
+
export declare function promptName({ department, agent }: Found): string;
|
|
17
19
|
/** Una riga per agente, per l'elenco dei prompt e il tool elenca_agenti. */
|
|
18
20
|
export declare function agentSummary({ department, agent }: Found): string;
|
|
19
21
|
/**
|
|
@@ -22,4 +24,8 @@ export declare function agentSummary({ department, agent }: Found): string;
|
|
|
22
24
|
* senza elenco, tutto il catalogo. Un nome sconosciuto o un elenco vuoto è un errore che lo nomina.
|
|
23
25
|
*/
|
|
24
26
|
export declare function selectSquad(catalog: readonly Department[], spec?: string): SquadDepartment[];
|
|
25
|
-
|
|
27
|
+
/** La riga con cui l'agente si presenta: «AISA-02 · ALTAIR — Senior Software Architect, reparto AISA (Software)». */
|
|
28
|
+
export declare function introLine({ department, agent }: Found): string;
|
|
29
|
+
export declare function agentText(found: Found, task?: string, { ratings }?: {
|
|
30
|
+
ratings?: boolean;
|
|
31
|
+
}): string;
|
package/dist/mcp/src/prompt.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** Versione del server, uguale a quella di package.json (lo controlla prompt.test.ts). */
|
|
2
|
-
export const VERSION = "0.
|
|
2
|
+
export const VERSION = "0.6.0";
|
|
3
3
|
/** Cerca un agente per id, senza distinguere maiuscole e minuscole («aisa-02» o «AISA-02»). */
|
|
4
4
|
export function findAgent(catalog, id) {
|
|
5
5
|
const wanted = id.toUpperCase();
|
|
@@ -14,6 +14,10 @@ export function findAgent(catalog, id) {
|
|
|
14
14
|
export function codeName(agent) {
|
|
15
15
|
return `${agent.id} · ${agent.star}`;
|
|
16
16
|
}
|
|
17
|
+
/** Nome del prompt (da 0.6.0): id in minuscolo e reparto, «aisa-02-software», così il menu dei comandi mostra il reparto. */
|
|
18
|
+
export function promptName({ department, agent }) {
|
|
19
|
+
return `${agent.id.toLowerCase()}-${department.short.toLowerCase()}`;
|
|
20
|
+
}
|
|
17
21
|
/** Una riga per agente, per l'elenco dei prompt e il tool elenca_agenti. */
|
|
18
22
|
export function agentSummary({ department, agent }) {
|
|
19
23
|
const position = agent.number === 1 ? "direttore" : "specialista";
|
|
@@ -50,13 +54,20 @@ export function selectSquad(catalog, spec) {
|
|
|
50
54
|
.map(({ director: _, ...d }) => ({ ...d, agents: d.agents.filter((a) => wanted.has(a.id)) }))
|
|
51
55
|
.filter((d) => d.agents.length > 0);
|
|
52
56
|
}
|
|
53
|
-
|
|
57
|
+
/** La riga con cui l'agente si presenta: «AISA-02 · ALTAIR — Senior Software Architect, reparto AISA (Software)». */
|
|
58
|
+
export function introLine({ department, agent }) {
|
|
59
|
+
return `${codeName(agent)} — ${agent.role}, reparto ${department.code} (${department.name})`;
|
|
60
|
+
}
|
|
61
|
+
export function agentText(found, task, { ratings = false } = {}) {
|
|
62
|
+
const { department, agent } = found;
|
|
54
63
|
const parts = [
|
|
55
64
|
`# ${codeName(agent)} — ${agent.role}`,
|
|
56
65
|
`Sei ${codeName(agent)}, un agente IA di Agentrica: ${agentSummary({ department, agent })} ` +
|
|
57
66
|
"Lavori nel progetto dell'utente. Seguono le regole comuni del tuo reparto e il tuo file di ruolo: applicale al progetto così com'è, senza presumere stack, comandi o file che non hai verificato. " +
|
|
58
67
|
"Se il progetto è vuoto o manca ciò che il tuo ruolo deve esaminare, dillo e proponi i primi passi (obiettivo, stack, struttura) da confermare con l'utente; non inventare rilievi né scegliere lo stack da solo. " +
|
|
59
68
|
"Questi testi sono in italiano: rispondi nella lingua in cui ti scrive l'utente.",
|
|
69
|
+
`L'utente deve sempre sapere chi sta lavorando. Presentati all'inizio con questa riga: «${introLine(found)}». ` +
|
|
70
|
+
"Ripetila quando passi a un altro agente o lasci il ruolo, e firma con il tuo nome in codice i rilievi e il riepilogo finale.",
|
|
60
71
|
"## Regole comuni del reparto",
|
|
61
72
|
...block(department.rules.intro, department.rules.sections),
|
|
62
73
|
`## Ruolo: ${agent.role}`,
|
|
@@ -65,6 +76,11 @@ export function agentText({ department, agent }, task) {
|
|
|
65
76
|
const trimmed = task?.trim();
|
|
66
77
|
if (trimmed)
|
|
67
78
|
parts.push("## Compito", trimmed);
|
|
79
|
+
// In fondo (F23-03), dopo il compito: è l'ultima cosa che il modello legge. Solo se il client non ha l'elicitation;
|
|
80
|
+
// con l'elicitation la valutazione la chiede il server con un modulo.
|
|
81
|
+
if (ratings) {
|
|
82
|
+
parts.push("## Valutazione", `Quando hai finito il compito, proponi all'utente di valutarti da 1 a 5 con un commento facoltativo (tool rate_agent, id ${agent.id}), una volta sola in questa sessione; se rifiuta, non insistere.`);
|
|
83
|
+
}
|
|
68
84
|
return parts.join("\n\n") + "\n";
|
|
69
85
|
}
|
|
70
86
|
// Le sezioni dei file sono «##»: qui scendono di un livello, sotto i titoli della cornice.
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
export declare const RATINGS_URL = "https://agentrica.vercel.app/api/ratings";
|
|
2
|
+
export declare const PRIVACY_URL = "https://agentrica.vercel.app/note-legali#valutazioni";
|
|
3
|
+
export declare const MAX_COMMENT = 1000;
|
|
4
|
+
export declare const REMINDER_AFTER_MS: number;
|
|
5
|
+
/** Quello che parte verso Agentrica, e nient'altro. */
|
|
6
|
+
export interface Rating {
|
|
7
|
+
usageId: string;
|
|
8
|
+
agentId: string;
|
|
9
|
+
rating: number;
|
|
10
|
+
comment: string;
|
|
11
|
+
publish: boolean;
|
|
12
|
+
mcpVersion: string;
|
|
13
|
+
}
|
|
14
|
+
export interface RatingInput {
|
|
15
|
+
agentId: string;
|
|
16
|
+
rating: number;
|
|
17
|
+
comment?: string | undefined;
|
|
18
|
+
publish?: boolean | undefined;
|
|
19
|
+
}
|
|
20
|
+
export type SendResult = {
|
|
21
|
+
ok: true;
|
|
22
|
+
usageId: string;
|
|
23
|
+
} | {
|
|
24
|
+
ok: false;
|
|
25
|
+
message: string;
|
|
26
|
+
};
|
|
27
|
+
export interface RatingsOptions {
|
|
28
|
+
version: string;
|
|
29
|
+
url?: string;
|
|
30
|
+
fetch?: typeof fetch;
|
|
31
|
+
now?: () => number;
|
|
32
|
+
}
|
|
33
|
+
export declare const NOTICE: string;
|
|
34
|
+
export declare function createRatings({ version, url, fetch: doFetch, now }: RatingsOptions): {
|
|
35
|
+
/** L'agente è stato caricato: da qui parte il conto del promemoria. */
|
|
36
|
+
loaded(agentId: string): void;
|
|
37
|
+
isRated: (agentId: string) => boolean;
|
|
38
|
+
/** Il modulo si può mostrare per questo agente? Se sì, da qui in poi conta come mostrato. */
|
|
39
|
+
claimAsk(agentId: string): boolean;
|
|
40
|
+
/** Il client non ha mostrato il modulo: torna possibile il promemoria (e un altro modulo). */
|
|
41
|
+
unclaimAsk: (agentId: string) => undefined;
|
|
42
|
+
/** La valutazione senza codice: per il pannello, dove l'invio dalla pagina è già la conferma. */
|
|
43
|
+
build: (input: RatingInput) => Rating;
|
|
44
|
+
/** Primo passo: prepara il contenuto e restituisce il codice da usare per inviarlo. */
|
|
45
|
+
draft(input: RatingInput): {
|
|
46
|
+
code: string;
|
|
47
|
+
rating: Rating;
|
|
48
|
+
};
|
|
49
|
+
/** Secondo passo: invia il contenuto del codice, una volta sola. */
|
|
50
|
+
confirm(code: string, agentId: string): Promise<SendResult>;
|
|
51
|
+
/** Invio di un contenuto già confermato dall'utente (dal secondo passo o dal pannello). */
|
|
52
|
+
send(rating: Rating): Promise<SendResult>;
|
|
53
|
+
/** Agenti caricati da almeno 30 minuti, non valutati e non ancora ricordati: si ricordano una volta sola. */
|
|
54
|
+
due(): string[];
|
|
55
|
+
};
|
|
56
|
+
export type Ratings = ReturnType<typeof createRatings>;
|
|
57
|
+
/** Testo dell'anteprima: cosa parte, l'avviso e il codice. */
|
|
58
|
+
export declare function previewText(name: string, code: string, rating: Rating): string;
|
|
59
|
+
/** Campi del modulo di valutazione (elicitation MCP): solo tipi semplici, la pubblicazione parte spenta. */
|
|
60
|
+
export declare const RATING_FORM: {
|
|
61
|
+
type: "object";
|
|
62
|
+
properties: {
|
|
63
|
+
rating: {
|
|
64
|
+
type: "string";
|
|
65
|
+
title: string;
|
|
66
|
+
enum: string[];
|
|
67
|
+
enumNames: string[];
|
|
68
|
+
};
|
|
69
|
+
comment: {
|
|
70
|
+
type: "string";
|
|
71
|
+
title: string;
|
|
72
|
+
description: string;
|
|
73
|
+
maxLength: number;
|
|
74
|
+
};
|
|
75
|
+
publish: {
|
|
76
|
+
type: "boolean";
|
|
77
|
+
title: string;
|
|
78
|
+
default: boolean;
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
required: string[];
|
|
82
|
+
};
|
|
83
|
+
/** Il testo in cima al modulo: chi si valuta e cosa parte. */
|
|
84
|
+
export declare function formMessage(name: string): string;
|
|
85
|
+
/** La risposta del modulo, validata come il resto: voto intero 1–5, commento al più MAX_COMMENT, booleano. */
|
|
86
|
+
export declare function parseForm(agentId: string, content: Record<string, unknown> | undefined): RatingInput | undefined;
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
// Valutazioni degli agenti (F20): anteprima con codice monouso, invio all'endpoint del sito, promemoria. Da F23 anche il
|
|
2
|
+
// modulo dell'elicitation MCP: inviare il modulo vale come conferma, e parte solo ciò che c'è nel modulo.
|
|
3
|
+
// È l'unico punto del server che apre una connessione, e solo dopo la conferma dell'utente: l'invio parte con il codice
|
|
4
|
+
// dell'anteprima e manda esattamente il contenuto mostrato. Niente compito, file o percorsi: solo i campi di Rating.
|
|
5
|
+
// Tutto resta in memoria (nessun file); i tempi e fetch si passano dall'esterno per i test.
|
|
6
|
+
import { randomBytes, randomUUID } from "node:crypto";
|
|
7
|
+
export const RATINGS_URL = "https://agentrica.vercel.app/api/ratings";
|
|
8
|
+
export const PRIVACY_URL = "https://agentrica.vercel.app/note-legali#valutazioni";
|
|
9
|
+
export const MAX_COMMENT = 1000;
|
|
10
|
+
const CODE_TTL_MS = 10 * 60_000;
|
|
11
|
+
const SEND_TIMEOUT_MS = 5_000;
|
|
12
|
+
export const REMINDER_AFTER_MS = 30 * 60_000;
|
|
13
|
+
export const NOTICE = "Parte solo ciò che vedi qui: niente compito, codice o file del progetto. " +
|
|
14
|
+
`Non scrivere nel commento nomi, dati personali o parti del progetto. Informativa: ${PRIVACY_URL}`;
|
|
15
|
+
export function createRatings({ version, url = RATINGS_URL, fetch: doFetch = fetch, now = Date.now }) {
|
|
16
|
+
// Un id casuale per agente e sessione: non collega sessioni né agenti diversi, ferma solo i doppi invii.
|
|
17
|
+
const usage = new Map();
|
|
18
|
+
const loadedAt = new Map();
|
|
19
|
+
const rated = new Set();
|
|
20
|
+
const reminded = new Set();
|
|
21
|
+
// Agenti per cui il server ha già mostrato il modulo (F23): una volta per agente, un rifiuto basta.
|
|
22
|
+
const asked = new Set();
|
|
23
|
+
const drafts = new Map();
|
|
24
|
+
const build = (input) => ({
|
|
25
|
+
usageId: usageId(input.agentId),
|
|
26
|
+
agentId: input.agentId,
|
|
27
|
+
rating: input.rating,
|
|
28
|
+
comment: (input.comment ?? "").trim(),
|
|
29
|
+
publish: input.publish ?? false,
|
|
30
|
+
mcpVersion: version,
|
|
31
|
+
});
|
|
32
|
+
const usageId = (agentId) => {
|
|
33
|
+
let id = usage.get(agentId);
|
|
34
|
+
if (!id)
|
|
35
|
+
usage.set(agentId, (id = randomUUID()));
|
|
36
|
+
return id;
|
|
37
|
+
};
|
|
38
|
+
return {
|
|
39
|
+
/** L'agente è stato caricato: da qui parte il conto del promemoria. */
|
|
40
|
+
loaded(agentId) {
|
|
41
|
+
if (!loadedAt.has(agentId))
|
|
42
|
+
loadedAt.set(agentId, now());
|
|
43
|
+
usageId(agentId);
|
|
44
|
+
},
|
|
45
|
+
isRated: (agentId) => rated.has(agentId),
|
|
46
|
+
/** Il modulo si può mostrare per questo agente? Se sì, da qui in poi conta come mostrato. */
|
|
47
|
+
claimAsk(agentId) {
|
|
48
|
+
if (!loadedAt.has(agentId) || rated.has(agentId) || asked.has(agentId))
|
|
49
|
+
return false;
|
|
50
|
+
asked.add(agentId);
|
|
51
|
+
return true;
|
|
52
|
+
},
|
|
53
|
+
/** Il client non ha mostrato il modulo: torna possibile il promemoria (e un altro modulo). */
|
|
54
|
+
unclaimAsk: (agentId) => void asked.delete(agentId),
|
|
55
|
+
/** La valutazione senza codice: per il pannello, dove l'invio dalla pagina è già la conferma. */
|
|
56
|
+
build,
|
|
57
|
+
/** Primo passo: prepara il contenuto e restituisce il codice da usare per inviarlo. */
|
|
58
|
+
draft(input) {
|
|
59
|
+
for (const [code, entry] of drafts)
|
|
60
|
+
if (entry.expires <= now())
|
|
61
|
+
drafts.delete(code);
|
|
62
|
+
const rating = build(input);
|
|
63
|
+
const code = randomBytes(4).toString("hex");
|
|
64
|
+
drafts.set(code, { rating, expires: now() + CODE_TTL_MS });
|
|
65
|
+
return { code, rating };
|
|
66
|
+
},
|
|
67
|
+
/** Secondo passo: invia il contenuto del codice, una volta sola. */
|
|
68
|
+
async confirm(code, agentId) {
|
|
69
|
+
const entry = drafts.get(code);
|
|
70
|
+
if (entry && entry.rating.agentId !== agentId)
|
|
71
|
+
return { ok: false, message: "Codice non valido per questo agente: prepara di nuovo l'anteprima." };
|
|
72
|
+
drafts.delete(code);
|
|
73
|
+
if (!entry || entry.expires <= now())
|
|
74
|
+
return { ok: false, message: "Codice scaduto o già usato: prepara di nuovo l'anteprima." };
|
|
75
|
+
return this.send(entry.rating);
|
|
76
|
+
},
|
|
77
|
+
/** Invio di un contenuto già confermato dall'utente (dal secondo passo o dal pannello). */
|
|
78
|
+
async send(rating) {
|
|
79
|
+
if (rated.has(rating.agentId))
|
|
80
|
+
return { ok: false, message: `${rating.agentId} è già stato valutato in questa sessione.` };
|
|
81
|
+
let response;
|
|
82
|
+
try {
|
|
83
|
+
response = await doFetch(url, {
|
|
84
|
+
method: "POST",
|
|
85
|
+
headers: { "Content-Type": "application/json" },
|
|
86
|
+
body: JSON.stringify(rating),
|
|
87
|
+
signal: AbortSignal.timeout(SEND_TIMEOUT_MS),
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
// Offline, endpoint giù, timeout o rete negata dal permission model di Node (--permission senza --allow-net).
|
|
92
|
+
return { ok: false, message: "Invio non riuscito: Agentrica non è raggiungibile (rete assente, timeout o rete non permessa). Riprova più tardi." };
|
|
93
|
+
}
|
|
94
|
+
if (response.ok || response.status === 409) {
|
|
95
|
+
// 409: lo stesso usageId è già registrato, quindi il voto c'è.
|
|
96
|
+
rated.add(rating.agentId);
|
|
97
|
+
return { ok: true, usageId: rating.usageId };
|
|
98
|
+
}
|
|
99
|
+
if (response.status === 429)
|
|
100
|
+
return { ok: false, message: "Hai già valutato questo agente oggi da questa rete." };
|
|
101
|
+
if (response.status === 400)
|
|
102
|
+
return { ok: false, message: "Valutazione respinta: dati non validi." };
|
|
103
|
+
return { ok: false, message: `Invio non riuscito (errore ${response.status} del servizio). Riprova più tardi.` };
|
|
104
|
+
},
|
|
105
|
+
/** Agenti caricati da almeno 30 minuti, non valutati e non ancora ricordati: si ricordano una volta sola. */
|
|
106
|
+
due() {
|
|
107
|
+
const ids = [...loadedAt].filter(([id, at]) => now() - at >= REMINDER_AFTER_MS && !rated.has(id) && !reminded.has(id) && !asked.has(id)).map(([id]) => id);
|
|
108
|
+
for (const id of ids)
|
|
109
|
+
reminded.add(id);
|
|
110
|
+
return ids;
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
/** Testo dell'anteprima: cosa parte, l'avviso e il codice. */
|
|
115
|
+
export function previewText(name, code, rating) {
|
|
116
|
+
return [
|
|
117
|
+
`Anteprima della valutazione di ${name}:`,
|
|
118
|
+
`- voto: ${rating.rating}/5`,
|
|
119
|
+
`- commento: ${rating.comment || "(nessuno)"}`,
|
|
120
|
+
`- pubblicazione del commento sul sito: ${rating.publish ? "sì" : "no"}`,
|
|
121
|
+
`- versione: ${rating.mcpVersion}`,
|
|
122
|
+
"",
|
|
123
|
+
NOTICE,
|
|
124
|
+
"",
|
|
125
|
+
`Mostra questa anteprima all'utente e chiedigli se inviarla. Solo dopo il suo sì richiama rate_agent con code "${code}" (valido 10 minuti, una volta). Se dice no, non fare nulla.`,
|
|
126
|
+
].join("\n");
|
|
127
|
+
}
|
|
128
|
+
/** Campi del modulo di valutazione (elicitation MCP): solo tipi semplici, la pubblicazione parte spenta. */
|
|
129
|
+
export const RATING_FORM = {
|
|
130
|
+
type: "object",
|
|
131
|
+
properties: {
|
|
132
|
+
// Una scelta e non un numero: Claude Code mostra un intero come campo di testo libero (prova F23-01).
|
|
133
|
+
rating: {
|
|
134
|
+
type: "string",
|
|
135
|
+
title: "Voto",
|
|
136
|
+
enum: ["5", "4", "3", "2", "1"],
|
|
137
|
+
enumNames: ["5 · benissimo", "4 · bene", "3 · così così", "2 · male", "1 · malissimo"],
|
|
138
|
+
},
|
|
139
|
+
comment: { type: "string", title: "Commento (facoltativo)", description: "Cosa è andato bene o cosa migliorare", maxLength: MAX_COMMENT },
|
|
140
|
+
publish: { type: "boolean", title: "Pubblica il commento sul sito di Agentrica", default: false },
|
|
141
|
+
},
|
|
142
|
+
required: ["rating"],
|
|
143
|
+
};
|
|
144
|
+
/** Il testo in cima al modulo: chi si valuta e cosa parte. */
|
|
145
|
+
export function formMessage(name) {
|
|
146
|
+
return `Valuta ${name}. Invia il modulo per mandare la valutazione ad Agentrica, in forma anonima; rifiuta e non te la chiediamo più per questo agente. ${NOTICE}`;
|
|
147
|
+
}
|
|
148
|
+
/** La risposta del modulo, validata come il resto: voto intero 1–5, commento al più MAX_COMMENT, booleano. */
|
|
149
|
+
export function parseForm(agentId, content) {
|
|
150
|
+
if (!content)
|
|
151
|
+
return undefined;
|
|
152
|
+
const { comment = "", publish = false } = content;
|
|
153
|
+
const rating = typeof content.rating === "string" && /^[1-5]$/.test(content.rating) ? Number(content.rating) : undefined;
|
|
154
|
+
if (rating === undefined)
|
|
155
|
+
return undefined;
|
|
156
|
+
if (typeof comment !== "string" || comment.length > MAX_COMMENT || typeof publish !== "boolean")
|
|
157
|
+
return undefined;
|
|
158
|
+
return { agentId, rating, comment, publish };
|
|
159
|
+
}
|