@withpica/mcp-server
Version:
MCP Server for withPICA — enables AI assistants to interact with your withPICA music catalog
230 lines (212 loc) • 8.49 kB
JavaScript
// Copyright (c) 2025-2026 Withpica Ltd. All rights reserved.
/**
* pica_credits_card MCP App card — who on a song has confirmed
* (ADR-152 amendment 2026-11-11, slice 2a).
*
* Shows each writer and master owner with their state. Two actions:
* - "confirm your part on pica": opens the work's page on PICA (app.openLink).
* The confirmation itself is only ever made there, signed in.
* - "ask the others": lists who will be emailed and waits for a yes, then
* calls pica_credits_ask_to_confirm once per person and shows each
* answer. Nothing is sent without that yes.
* "refresh" re-reads pica_credits_card.
*
* DOM API only — never innerHTML.
*/
import { buildAppHtml } from "./shared.js";
const CONFIRM_BODY = `
<div class="stack gap-6">
<header>
<h1 class="heading heading-lg">who has confirmed</h1>
<p class="text-secondary" id="cf-sub">reading this work…</p>
</header>
<div id="cf-groups" class="stack gap-4"></div>
<div id="cf-confirm-ask" class="card-flat" style="display: none; padding: 16px 20px;">
<p class="text-primary" id="cf-confirm-text"></p>
<div class="row gap-4" style="margin-top: 12px;">
<button class="btn-copper" id="cf-send">send</button>
<button class="btn-ghost" id="cf-cancel">cancel</button>
</div>
</div>
<p class="text-secondary" id="cf-result" style="display: none;"></p>
<div class="row gap-4" style="flex-wrap: wrap;">
<button class="btn-copper" id="cf-open">confirm your part on pica</button>
<button class="btn-ghost" id="cf-ask" style="display: none;">ask the others</button>
<button class="btn-ghost" id="cf-refresh">refresh</button>
</div>
<a id="cf-link" class="text-tertiary" style="font-size: 12px; display: none;" target="_blank" rel="noopener"></a>
</div>
`;
const CONFIRM_JS = `
var cfState = { work_id: null, song_url: null, groups: [], sending: false };
// The ask the user is saying yes to: exactly the names and work shown in the
// "email X, Y?" box, fixed when it opens. A refresh or a new result closes
// the box, so a changed list has to be shown again before anything is sent.
var cfPending = null;
function cfStateWords(p) {
if (p.state === "confirmed") return "confirmed";
if (p.state === "asked") return "asked";
if (p.state === "held") return "asked \\u00B7 waiting for an owner's ok";
return p.has_email ? "not asked yet" : "not asked yet \\u00B7 no email on record";
}
function cfAskable() {
var out = [];
cfState.groups.forEach(function (g) {
(g.people || []).forEach(function (p) {
if (p.state === "not_asked" && p.has_email &&
!out.some(function (o) { return o.person_id === p.person_id; })) {
out.push(p);
}
});
});
return out;
}
function cfRender() {
var box = document.getElementById("cf-groups");
while (box.firstChild) box.removeChild(box.firstChild);
var total = 0, confirmed = 0;
cfState.groups.forEach(function (g) {
if (!g.people || g.people.length === 0) return;
var section = document.createElement("div");
section.className = "card-flat";
section.style.padding = "12px 16px";
var h = document.createElement("div");
h.className = "text-tertiary";
h.style.fontSize = "12px";
h.textContent = g.label;
section.appendChild(h);
g.people.forEach(function (p) {
total++;
if (p.state === "confirmed") confirmed++;
var row = document.createElement("div");
row.className = "row between";
row.style.marginTop = "8px";
var name = document.createElement("span");
name.className = "text-primary";
name.textContent = p.name;
var st = document.createElement("span");
st.className = p.state === "confirmed" ? "text-accent" : "text-secondary";
st.style.fontSize = "13px";
st.textContent = cfStateWords(p);
row.appendChild(name);
row.appendChild(st);
section.appendChild(row);
});
box.appendChild(section);
});
document.getElementById("cf-sub").textContent = total === 0
? "no writers or master owners on this work yet"
: confirmed + " of " + total + " confirmed";
var ask = document.getElementById("cf-ask");
var n = cfAskable().length;
ask.style.display = n > 0 ? "inline-block" : "none";
ask.textContent = n === 1 ? "ask " + cfAskable()[0].name + " to confirm" : "ask " + n + " people to confirm";
var link = document.getElementById("cf-link");
if (cfState.song_url) {
link.href = cfState.song_url;
link.textContent = cfState.song_url;
}
}
function cfShowLink() {
var link = document.getElementById("cf-link");
if (cfState.song_url) link.style.display = "block";
}
function cfOpenSong() {
if (!cfState.song_url) return;
if (app && typeof app.openLink === "function") {
app.openLink({ url: cfState.song_url }).catch(cfShowLink);
} else {
cfShowLink();
}
}
function cfCloseAsk() {
cfPending = null;
document.getElementById("cf-confirm-ask").style.display = "none";
}
function cfStartAsk() {
var people = cfAskable();
if (people.length === 0 || !cfState.work_id) return;
cfPending = {
work_id: cfState.work_id,
people: people.map(function (p) { return { person_id: p.person_id, name: p.name }; }),
};
var names = people.map(function (p) { return p.name; }).join(", ");
document.getElementById("cf-confirm-text").textContent =
"email " + names + " asking them to confirm their part of this work?";
document.getElementById("cf-confirm-ask").style.display = "block";
}
function cfAskWords(status) {
if (status === "asked") return "asked";
if (status === "already_asked") return "asked recently, not sent again";
if (status === "needs_address") return "no email on record";
if (status === "nothing_to_confirm") return "nothing to confirm";
if (status === "self") return "that's you: confirm on pica";
return "couldn't send \\u00B7 try again, or open it on pica";
}
function cfResultStatus(r) {
if (!r) return null;
if (r.isError) return null;
var s = r.structuredContent;
if (s && typeof s === "object") {
if (s.status) return s.status;
if (s.data && s.data.status) return s.data.status;
}
var t = Array.isArray(r.content) && r.content[0] && r.content[0].text;
if (typeof t === "string") {
try { var j = JSON.parse(t.slice(t.indexOf("{"))); return j.status || (j.data && j.data.status) || null; }
catch (_) { return null; }
}
return null;
}
function cfSend() {
if (cfState.sending || !cfPending || !app || typeof app.callServerTool !== "function") return;
var workId = cfPending.work_id;
var people = cfPending.people;
cfState.sending = true;
cfCloseAsk();
var out = document.getElementById("cf-result");
out.style.display = "block";
out.textContent = "asking\\u2026";
var lines = [];
var next = function (i) {
if (i >= people.length) {
cfState.sending = false;
out.textContent = lines.join(" \\u00B7 ");
cfRefresh();
return;
}
var p = people[i];
app.callServerTool({
name: "pica_credits_ask_to_confirm",
arguments: { work_id: workId, person_id: p.person_id },
}).then(function (r) {
lines.push(p.name + ": " + cfAskWords(cfResultStatus(r)));
}).catch(function () {
lines.push(p.name + ": couldn't send \\u00B7 try again, or open it on pica");
}).then(function () { next(i + 1); });
};
next(0);
}
function cfApply(data) {
if (!data || !data.groups) return;
cfCloseAsk();
cfState.work_id = data.work_id || cfState.work_id;
cfState.song_url = data.song_url || cfState.song_url;
cfState.groups = data.groups;
cfRender();
}
function cfRefresh() {
if (!cfState.work_id || !app || typeof app.callServerTool !== "function") return;
app.callServerTool({ name: "pica_credits_card", arguments: { work_id: cfState.work_id } })
.then(function (r) { if (r && r.structuredContent) cfApply(r.structuredContent); })
.catch(function () {});
}
document.getElementById("cf-open").addEventListener("click", cfOpenSong);
document.getElementById("cf-ask").addEventListener("click", cfStartAsk);
document.getElementById("cf-send").addEventListener("click", cfSend);
document.getElementById("cf-cancel").addEventListener("click", cfCloseAsk);
document.getElementById("cf-refresh").addEventListener("click", cfRefresh);
window.onPicaToolResult = cfApply;
`;
export const CONFIRM_HTML = buildAppHtml("pica \u2014 confirmations", CONFIRM_BODY, CONFIRM_JS);
//# sourceMappingURL=confirm.js.map