@atlaskit/editor-plugin-show-diff
Version:
ShowDiff plugin for @atlaskit/editor-core
200 lines (190 loc) • 10.6 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.resolveDiffContributors = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _getThirdPartyAgentColor = require("@atlaskit/agent-color/get-third-party-agent-color");
var _attributions = require("./decorations/colorSchemes/attributions");
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
/** A brand name, so a literal rather than a translated message. */
var ROVO_AGENT_NAME = 'Rovo';
/** `agentType` values that render as "Rovo" rather than as a generic external agent. */
var ROVO_AGENT_TYPES = new Set(['convo-ai', 'rovo_chat']);
/** `@atlaskit/agent-color` brands show-diff renders with a dedicated presentation. */
var SHOW_DIFF_AGENT_BRANDS = {
'agent-brand-claude': 'claude',
'agent-brand-chatgpt': 'chatgpt',
'agent-brand-figma': 'figma',
'agent-brand-lovable': 'lovable',
'agent-brand-replit': 'replit',
'agent-brand-rovo': 'rovo'
};
/**
* Brands that prefer the account's real avatar over their fixed glyph when the invoking profile
* has one. Claude/ChatGPT intentionally keep their fixed glyph always (see `AGENT_KIND_ICONS` in
* `contributorAvatarRenderer.ts`).
*/
var AVATAR_ELIGIBLE_BRANDS = new Set(['figma', 'lovable', 'replit', 'rovo']);
/** Known external agent types emitted by Confluence's agent identification service that carry
* no `@atlaskit/agent-color` brand — a generic labelled fallback. */
var EXTERNAL_AGENT_NAMES = {
vscode: 'VS Code',
hermes: 'Hermes',
runlayer: 'Runlayer',
zed: 'Zed',
ampcode: 'Ampcode',
antigravity: 'Antigravity',
devin_cli: 'Devin CLI',
pi_agent: 'Pi Agent'
};
var hasAgentIdentity = function hasAgentIdentity(attribution) {
var _attribution$agentId, _attribution$agentTyp;
return Boolean(((_attribution$agentId = attribution.agentId) === null || _attribution$agentId === void 0 ? void 0 : _attribution$agentId.trim()) || ((_attribution$agentTyp = attribution.agentType) === null || _attribution$agentTyp === void 0 ? void 0 : _attribution$agentTyp.trim()));
};
var toProfilesByAccountId = function toProfilesByAccountId(profiles) {
var profilesByAccountId = new Map();
profiles === null || profiles === void 0 || profiles.forEach(function (profile) {
// Trimmed to match how attribution ids are keyed, so the two sides compare like for like.
var accountId = profile.accountId.trim();
if (accountId && !profilesByAccountId.has(accountId)) {
profilesByAccountId.set(accountId, profile);
}
});
return profilesByAccountId;
};
var resolveUser = function resolveUser(userId, profilesByAccountId) {
var profile = profilesByAccountId.get(userId);
return profile ? {
avatarUrl: profile.avatarUrl,
kind: 'user',
name: profile.name
} : undefined;
};
/** Resolve branding or a profile first, then a known type name or the external-agent fallback. */
var resolveAgent = function resolveAgent(attribution, profilesByAccountId) {
var _attribution$agentTyp2, _attribution$agentId2;
var agentType = (_attribution$agentTyp2 = attribution.agentType) === null || _attribution$agentTyp2 === void 0 ? void 0 : _attribution$agentTyp2.trim().toLowerCase();
// `agentType` may be a named id (e.g. `mcp_lovable_agent`) or a display name (e.g. `lovable`).
var brand = agentType ? (0, _getThirdPartyAgentColor.getThirdPartyAgentColor)({
agentNamedId: agentType,
agentName: agentType
}) : undefined;
var diffBrand = brand ? SHOW_DIFF_AGENT_BRANDS[brand.scheme] : undefined;
var agentId = (_attribution$agentId2 = attribution.agentId) === null || _attribution$agentId2 === void 0 ? void 0 : _attribution$agentId2.trim();
var agentProfile = agentId ? profilesByAccountId.get(agentId) : undefined;
if (diffBrand && brand) {
return _objectSpread({
agentKind: diffBrand,
kind: 'agent',
name: brand.name
}, AVATAR_ELIGIBLE_BRANDS.has(diffBrand) && agentProfile !== null && agentProfile !== void 0 && agentProfile.avatarUrl ? {
avatarUrl: agentProfile.avatarUrl
} : {});
}
if (agentProfile) {
return _objectSpread(_objectSpread({}, agentProfile.agentBrand ? {
agentKind: agentProfile.agentBrand
} : {}), {}, {
avatarUrl: agentProfile.avatarUrl,
kind: 'agent',
name: agentProfile.name
});
}
// Unknown types keep an empty name for the tag's localised "External agent" label.
if (agentType && ROVO_AGENT_TYPES.has(agentType)) {
return {
agentKind: 'rovo',
kind: 'agent',
name: ROVO_AGENT_NAME
};
}
var externalName = agentType ? EXTERNAL_AGENT_NAMES[agentType] : undefined;
return {
agentKind: 'external',
kind: 'agent',
name: externalName !== null && externalName !== void 0 ? externalName : ''
};
};
/**
* One contributor per distinct actor behind the attributed steps, resolved against the identities
* the consumer supplied. An actor the consumer cannot name is skipped rather than dropping the whole
* list: tags are per change, so its changes keep their diff colour and simply carry no tag, while
* the actors that *are* named stay credited. Returns `undefined` only when nothing resolves at all.
*/
var resolveDiffContributors = exports.resolveDiffContributors = function resolveDiffContributors(stepsWithAttribution, profiles) {
// Profiles opt callers into contributor tags; attribution alone still supplies diff colours.
if (profiles === undefined) {
return undefined;
}
var profilesByAccountId = toProfilesByAccountId(profiles);
var contributors = [];
/** Keyed as the plugin keys changes, so no two entries can collapse onto one another. */
var creditedActors = new Set();
var _iterator = _createForOfIteratorHelper(stepsWithAttribution),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var _stepAttribution$user;
var stepAttribution = _step.value.stepAttribution;
// An attribution naming nobody (e.g. one carrying only `wasOffline`) has nobody to credit.
var actor = stepAttribution ? (0, _attributions.getAttributionKey)(stepAttribution) : undefined;
if (!stepAttribution || !actor || creditedActors.has(actor)) {
continue;
}
var userId = (_stepAttribution$user = stepAttribution.userId) === null || _stepAttribution$user === void 0 ? void 0 : _stepAttribution$user.trim();
if (hasAgentIdentity(stepAttribution)) {
var agent = resolveAgent(stepAttribution, profilesByAccountId);
if (!userId) {
// Agent mode: the agent acted on its own, so it is credited on its own.
contributors.push(_objectSpread(_objectSpread({}, agent), {}, {
attribution: stepAttribution
}));
creditedActors.add(actor);
continue;
}
// User-invoked agent: credit both actors and link the agent back to the invoking user.
var invokingUser = {
userId: userId
};
var _user = resolveUser(userId, profilesByAccountId);
// Skipped if the invoking user's own step already credited them.
var invokingUserActor = (0, _attributions.getAttributionKey)(invokingUser);
if (_user && invokingUserActor && !creditedActors.has(invokingUserActor)) {
contributors.push(_objectSpread(_objectSpread({}, _user), {}, {
attribution: invokingUser
}));
creditedActors.add(invokingUserActor);
}
// The agent is always nameable, so it is credited even when its invoker is not — but
// without the connection, which would point at a contributor that was never created.
contributors.push(_objectSpread(_objectSpread({}, agent), {}, {
attribution: stepAttribution
}, _user ? {
connectedTo: invokingUser
} : {}));
creditedActors.add(actor);
continue;
}
var user = userId ? resolveUser(userId, profilesByAccountId) : undefined;
if (!user) {
// Unnamed, and a user has no generic fallback label, so this actor's changes go untagged.
continue;
}
contributors.push(_objectSpread(_objectSpread({}, user), {}, {
attribution: stepAttribution
}));
creditedActors.add(actor);
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
return contributors.length > 0 ? contributors : undefined;
};