@agy-daddy/plugin-context 0.0.0-stage → 1.0.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/LICENSE +22 -0
- package/NOTICE +7 -0
- package/README.md +13 -2
- package/agy-plugin.json +59 -0
- package/baseline.json +10 -0
- package/locales/en.json +10 -0
- package/locales/zh-CN.json +10 -0
- package/native/mcp-server.js +140 -0
- package/native/mcp.json +8 -0
- package/native/skills/context-governance/SKILL.md +10 -0
- package/package.json +31 -4
- package/settings.schema.json +84 -0
- package/src/attachment-audit.js +163 -0
- package/src/context-analyzer.js +1036 -0
- package/src/node.js +506 -0
- package/src/panel-styles.js +274 -0
- package/src/pulse-capsule.js +484 -0
- package/src/renderer.js +995 -0
- package/src/stream-speed-tracker.js +143 -0
|
@@ -0,0 +1,484 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { formatTokensShort, getRingAccentToken } = require('./context-analyzer');
|
|
4
|
+
|
|
5
|
+
const SESSION_STACK_ICON_SVG = '<svg width="11.5" height="11.5" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M8 2.5 2.5 5.5 8 8.5l5.5-3L8 2.5Z"/><path d="M2.5 8.5 8 11.5l5.5-3"/><path d="M2.5 11.5 8 14.5l5.5-3"/></svg>';
|
|
6
|
+
const SPEED_BOLT_ICON_SVG = '<svg width="11.5" height="11.5" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M9.2 1.8 3.6 9h4.1l-.9 5.2L12.4 7H8.3l.9-5.2Z"/></svg>';
|
|
7
|
+
const ALERT_TRIANGLE_ICON_SVG = '<svg width="11.5" height="11.5" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M8 2.3 14 13H2L8 2.3Z"/><path d="M8 6.4v3.2"/><circle cx="8" cy="11.4" r="0.55" fill="currentColor" stroke="none"/></svg>';
|
|
8
|
+
const COMPACT_ICON_SVG = '<svg width="11" height="11" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M8 2v4.5M5.5 4.2 8 6.7l2.5-2.5"/><path d="M8 14V9.5M5.5 11.8 8 9.3l2.5 2.5"/><path d="M2.5 8h11"/></svg>';
|
|
9
|
+
|
|
10
|
+
const PULSE_CAPSULE_CSS = `
|
|
11
|
+
@keyframes agy-pulse-stream-blink {
|
|
12
|
+
0%, 100% { opacity: 1; }
|
|
13
|
+
50% { opacity: 0.48; }
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
#agy-pulse-context-wrap {
|
|
17
|
+
display: flex;
|
|
18
|
+
align-items: center;
|
|
19
|
+
justify-content: flex-end;
|
|
20
|
+
gap: 6px;
|
|
21
|
+
width: 100%;
|
|
22
|
+
max-width: 100%;
|
|
23
|
+
box-sizing: border-box;
|
|
24
|
+
padding: 5px 2px 0 2px;
|
|
25
|
+
margin: 4px 0 0 0;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
#agy-pulse-context-capsule {
|
|
29
|
+
display: inline-flex;
|
|
30
|
+
align-items: center;
|
|
31
|
+
gap: 7px;
|
|
32
|
+
margin: 0;
|
|
33
|
+
padding: 2px 9px;
|
|
34
|
+
border-radius: 999px;
|
|
35
|
+
font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", sans-serif;
|
|
36
|
+
font-size: 11px;
|
|
37
|
+
font-weight: 500;
|
|
38
|
+
font-variant-numeric: tabular-nums;
|
|
39
|
+
line-height: 1.3;
|
|
40
|
+
background: var(--ag-bg-subcard, var(--secondary, rgba(113, 113, 122, 0.10)));
|
|
41
|
+
color: var(--ag-text-primary, var(--foreground, #18181b));
|
|
42
|
+
border: 1px solid var(--ag-border-card, var(--border, rgba(113, 113, 122, 0.24)));
|
|
43
|
+
cursor: pointer;
|
|
44
|
+
white-space: nowrap;
|
|
45
|
+
transition: background-color 140ms cubic-bezier(0.22, 1, 0.36, 1), border-color 140ms cubic-bezier(0.22, 1, 0.36, 1);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
#agy-pulse-context-capsule:hover {
|
|
49
|
+
background: var(--ag-bg-control-hover, var(--sidebar-secondary, rgba(113, 113, 122, 0.16)));
|
|
50
|
+
border-color: var(--ag-accent-border, rgba(113, 113, 122, 0.36));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
#agy-pulse-context-capsule:focus-visible,
|
|
54
|
+
#agy-pulse-compact-btn:focus-visible {
|
|
55
|
+
outline: 2px solid var(--ag-focus-ring, rgba(113, 113, 122, 0.45));
|
|
56
|
+
outline-offset: 2px;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
#agy-pulse-context-capsule[data-streaming="true"] {
|
|
60
|
+
border-color: var(--ag-accent-border, rgba(113, 113, 122, 0.42));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
#agy-pulse-context-capsule[data-streaming="true"] .agy-pulse-seg[data-kind="speed"] .agy-pulse-icon {
|
|
64
|
+
animation: agy-pulse-stream-blink 1.1s ease-in-out infinite;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
#agy-pulse-context-capsule[data-cache-risk="warn"] {
|
|
68
|
+
border-color: rgba(245, 158, 11, 0.45);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
#agy-pulse-context-capsule[data-cache-risk="critical"] {
|
|
72
|
+
border-color: rgba(239, 68, 68, 0.48);
|
|
73
|
+
color: var(--ag-ring-danger, #ef4444);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
#agy-pulse-compact-btn {
|
|
77
|
+
display: inline-flex;
|
|
78
|
+
align-items: center;
|
|
79
|
+
gap: 3px;
|
|
80
|
+
height: 22px;
|
|
81
|
+
padding: 0 8px;
|
|
82
|
+
border-radius: 999px;
|
|
83
|
+
font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", sans-serif;
|
|
84
|
+
font-size: 11px;
|
|
85
|
+
font-weight: 500;
|
|
86
|
+
background: rgba(245, 158, 11, 0.12);
|
|
87
|
+
color: var(--ag-ring-warn, #f59e0b);
|
|
88
|
+
border: 1px solid rgba(245, 158, 11, 0.42);
|
|
89
|
+
cursor: pointer;
|
|
90
|
+
white-space: nowrap;
|
|
91
|
+
transition: background-color 140ms cubic-bezier(0.22, 1, 0.36, 1), border-color 140ms cubic-bezier(0.22, 1, 0.36, 1);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
#agy-pulse-compact-btn:hover {
|
|
95
|
+
background: rgba(245, 158, 11, 0.20);
|
|
96
|
+
border-color: rgba(245, 158, 11, 0.65);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
#agy-pulse-compact-btn[hidden] {
|
|
100
|
+
display: none !important;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
.agy-pulse-seg {
|
|
104
|
+
display: inline-flex;
|
|
105
|
+
align-items: center;
|
|
106
|
+
gap: 3px;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
.agy-pulse-seg[data-tone="warn"] {
|
|
110
|
+
color: var(--ag-ring-warn, #f59e0b);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
.agy-pulse-seg[data-tone="danger"] {
|
|
114
|
+
color: var(--ag-ring-danger, #ef4444);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
.agy-pulse-icon {
|
|
118
|
+
display: inline-flex;
|
|
119
|
+
align-items: center;
|
|
120
|
+
justify-content: center;
|
|
121
|
+
flex-shrink: 0;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
#agy-pulse-context-wrap svg {
|
|
125
|
+
flex-shrink: 0;
|
|
126
|
+
display: block;
|
|
127
|
+
}
|
|
128
|
+
`.trim();
|
|
129
|
+
|
|
130
|
+
function clampPct(v) {
|
|
131
|
+
const n = Number(v);
|
|
132
|
+
if (!Number.isFinite(n)) return 0;
|
|
133
|
+
return Math.max(0, Math.min(100, Math.round(n)));
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function buildDualRatioSvg(occupancyPct = 0, cacheHitPct = 0, accentColor = 'var(--ag-custom-token-accent, var(--ag-token-accent-default, currentColor))') {
|
|
137
|
+
const occ = clampPct(occupancyPct);
|
|
138
|
+
const hit = clampPct(cacheHitPct);
|
|
139
|
+
const rOuter = 6.2;
|
|
140
|
+
const rInner = 3.4;
|
|
141
|
+
const cOuter = Number((2 * Math.PI * rOuter).toFixed(2));
|
|
142
|
+
const cInner = Number((2 * Math.PI * rInner).toFixed(2));
|
|
143
|
+
const dOuter = Number(((occ / 100) * cOuter).toFixed(2));
|
|
144
|
+
const dInner = Number(((hit / 100) * cInner).toFixed(2));
|
|
145
|
+
return `<svg width="15" height="15" viewBox="0 0 16 16" aria-hidden="true" data-occ="${occ}" data-cache="${hit}">`
|
|
146
|
+
+ `<circle cx="8" cy="8" r="${rOuter}" fill="none" stroke="rgba(113,113,122,0.22)" stroke-width="2"/>`
|
|
147
|
+
+ `<circle cx="8" cy="8" r="${rOuter}" fill="none" stroke="${accentColor}" stroke-width="2" stroke-dasharray="${dOuter} ${cOuter}" transform="rotate(-90 8 8)"/>`
|
|
148
|
+
+ `<circle cx="8" cy="8" r="${rInner}" fill="none" stroke="rgba(113,113,122,0.18)" stroke-width="1.8"/>`
|
|
149
|
+
+ `<circle cx="8" cy="8" r="${rInner}" fill="none" stroke="var(--ag-custom-ring-used, var(--ag-text-secondary, #71717a))" stroke-width="1.8" stroke-dasharray="${dInner} ${cInner}" transform="rotate(-90 8 8)"/>`
|
|
150
|
+
+ `</svg>`;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function formatSpeedCompact(tps) {
|
|
154
|
+
const n = Number(tps);
|
|
155
|
+
if (!Number.isFinite(n) || n <= 0) return '';
|
|
156
|
+
return n >= 100 ? `${Math.round(n)}/s` : `${Number(n.toFixed(1))}/s`;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function buildCapsuleSegments(snap, opts = {}) {
|
|
160
|
+
const accent = getRingAccentToken(snap?.percent || 0, {
|
|
161
|
+
isCacheDrop: Boolean(snap?.cacheDropDetected),
|
|
162
|
+
isCliff328k: snap?.cacheCliffRisk === 'critical',
|
|
163
|
+
isWarn240k: snap?.cacheCliffRisk === 'warn'
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
if (!snap || !snap.hasSession || snap.used === null) {
|
|
167
|
+
return [
|
|
168
|
+
{
|
|
169
|
+
kind: 'window',
|
|
170
|
+
iconSvg: buildDualRatioSvg(0, 0, accent),
|
|
171
|
+
text: '--'
|
|
172
|
+
}
|
|
173
|
+
];
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const usedStr = formatTokensShort(snap.used);
|
|
177
|
+
const budgetStr = formatTokensShort(snap.budget);
|
|
178
|
+
const sessionTotal = snap.sessionUsage?.totalTokens ?? snap.used;
|
|
179
|
+
|
|
180
|
+
const segments = [
|
|
181
|
+
{
|
|
182
|
+
kind: 'window',
|
|
183
|
+
iconSvg: buildDualRatioSvg(snap.percent || 0, snap.cacheHitRate || 0, accent),
|
|
184
|
+
text: `${usedStr}/${budgetStr}`
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
kind: 'session',
|
|
188
|
+
iconSvg: SESSION_STACK_ICON_SVG,
|
|
189
|
+
text: formatTokensShort(sessionTotal)
|
|
190
|
+
}
|
|
191
|
+
];
|
|
192
|
+
|
|
193
|
+
if (opts.showSpeed !== false) {
|
|
194
|
+
const live = opts.liveSpeed;
|
|
195
|
+
if (live && live.active) {
|
|
196
|
+
const liveText = live.tps ? `~${formatSpeedCompact(live.tps)}` : '…';
|
|
197
|
+
segments.push({
|
|
198
|
+
kind: 'speed',
|
|
199
|
+
iconSvg: SPEED_BOLT_ICON_SVG,
|
|
200
|
+
text: liveText,
|
|
201
|
+
streaming: true
|
|
202
|
+
});
|
|
203
|
+
} else if (snap.speed?.tps) {
|
|
204
|
+
segments.push({
|
|
205
|
+
kind: 'speed',
|
|
206
|
+
iconSvg: SPEED_BOLT_ICON_SVG,
|
|
207
|
+
text: formatSpeedCompact(snap.speed.tps)
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (snap.cacheDropDetected || snap.cacheCliffRisk === 'critical') {
|
|
213
|
+
segments.push({
|
|
214
|
+
kind: 'alert',
|
|
215
|
+
iconSvg: ALERT_TRIANGLE_ICON_SVG,
|
|
216
|
+
text: '',
|
|
217
|
+
tone: 'danger'
|
|
218
|
+
});
|
|
219
|
+
} else if (snap.cacheCliffRisk === 'warn') {
|
|
220
|
+
segments.push({
|
|
221
|
+
kind: 'alert',
|
|
222
|
+
iconSvg: ALERT_TRIANGLE_ICON_SVG,
|
|
223
|
+
text: '',
|
|
224
|
+
tone: 'warn'
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return segments;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function buildCapsuleTooltip(snap, opts = {}) {
|
|
232
|
+
if (!snap || !snap.hasSession || snap.used === null) {
|
|
233
|
+
return '当前无活跃对话,点击打开实时监测面板';
|
|
234
|
+
}
|
|
235
|
+
const su = snap.sessionUsage || {};
|
|
236
|
+
const sessionTotal = su.totalTokens ?? snap.used;
|
|
237
|
+
const pctStr = snap.percent !== null && snap.percent !== undefined ? `${snap.percent}%` : '--';
|
|
238
|
+
const live = opts.liveSpeed;
|
|
239
|
+
const speedLine = live?.active
|
|
240
|
+
? (live.tps ? `实时估算速度: ~${live.tps} tok/s (生成中)` : '实时速度: 首字等待中…')
|
|
241
|
+
: `生成速度: ${snap.speed?.tps ?? '--'} tok/s · 纯流式 ${snap.speed?.streamingTps ?? '--'} tok/s · TTFT ${snap.speed?.ttft ?? '--'}s`;
|
|
242
|
+
const lines = [
|
|
243
|
+
`上下文窗口: ${formatTokensShort(snap.used)} / ${formatTokensShort(snap.budget)} (${pctStr}) · 缓存命中 ${snap.cacheHitRate ?? 0}%`,
|
|
244
|
+
`当前对话累计: ${formatTokensShort(sessionTotal)} (${su.callsCount || 0} 次调用 · 输出 ${formatTokensShort(su.outputTokens ?? snap.outputTokens ?? 0)})`,
|
|
245
|
+
speedLine
|
|
246
|
+
];
|
|
247
|
+
if (Array.isArray(snap.alerts) && snap.alerts.length) {
|
|
248
|
+
lines.push(`风险提示: ${snap.alerts.map((a) => a.message).join(';')}`);
|
|
249
|
+
}
|
|
250
|
+
lines.push('点击展开详情面板与指标说明');
|
|
251
|
+
return lines.join('\n');
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function buildCapsuleText(snap, opts = {}) {
|
|
255
|
+
const segs = buildCapsuleSegments(snap, opts);
|
|
256
|
+
const parts = segs.map((s) => s.text).filter(Boolean);
|
|
257
|
+
return parts.length ? parts.join(' · ') : '--';
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function createPulseCapsuleController(options = {}) {
|
|
261
|
+
const doc = options.doc || globalThis.document;
|
|
262
|
+
const anchors = options.anchors;
|
|
263
|
+
const onClick = typeof options.onClick === 'function' ? options.onClick : () => {};
|
|
264
|
+
const onCompactClick = typeof options.onCompactClick === 'function' ? options.onCompactClick : () => {};
|
|
265
|
+
const mountedEntries = new Set(); // { wrap, btn, compactBtn }
|
|
266
|
+
let lastSnapshot = null;
|
|
267
|
+
let lastState = { showSpeed: options.showSpeed !== false, liveSpeed: null, showQuickCompact: false };
|
|
268
|
+
let enabled = options.enabled !== false;
|
|
269
|
+
|
|
270
|
+
function applyStyleAndContent(entry, snap, stateOpts = lastState) {
|
|
271
|
+
const { btn, compactBtn } = entry;
|
|
272
|
+
const color = getRingAccentToken(snap?.percent || 0, {
|
|
273
|
+
isCacheDrop: Boolean(snap?.cacheDropDetected),
|
|
274
|
+
isCliff328k: snap?.cacheCliffRisk === 'critical',
|
|
275
|
+
isWarn240k: snap?.cacheCliffRisk === 'warn'
|
|
276
|
+
});
|
|
277
|
+
const segments = buildCapsuleSegments(snap, stateOpts);
|
|
278
|
+
const svgMarkup = segments[0]?.iconSvg || buildDualRatioSvg(snap?.percent || 0, snap?.cacheHitRate || 0, color);
|
|
279
|
+
const targetDoc = btn?.ownerDocument || doc || globalThis.document;
|
|
280
|
+
|
|
281
|
+
if (targetDoc?.createElement) {
|
|
282
|
+
btn.textContent = '';
|
|
283
|
+
for (const seg of segments) {
|
|
284
|
+
const segEl = targetDoc.createElement('span');
|
|
285
|
+
segEl.className = 'agy-pulse-seg';
|
|
286
|
+
segEl.setAttribute('data-kind', seg.kind);
|
|
287
|
+
if (seg.tone) segEl.setAttribute('data-tone', seg.tone);
|
|
288
|
+
if (seg.streaming) segEl.setAttribute('data-streaming', 'true');
|
|
289
|
+
|
|
290
|
+
if (seg.iconSvg) {
|
|
291
|
+
const iconSpan = targetDoc.createElement('span');
|
|
292
|
+
iconSpan.className = 'agy-pulse-icon';
|
|
293
|
+
iconSpan.innerHTML = seg.iconSvg;
|
|
294
|
+
segEl.appendChild(iconSpan);
|
|
295
|
+
}
|
|
296
|
+
if (seg.text) {
|
|
297
|
+
const valSpan = targetDoc.createElement('span');
|
|
298
|
+
valSpan.className = 'agy-pulse-val';
|
|
299
|
+
valSpan.textContent = seg.text;
|
|
300
|
+
segEl.appendChild(valSpan);
|
|
301
|
+
}
|
|
302
|
+
btn.appendChild(segEl);
|
|
303
|
+
}
|
|
304
|
+
} else {
|
|
305
|
+
btn.textContent = buildCapsuleText(snap, stateOpts);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
btn.setAttribute('data-cache-risk', snap?.cacheCliffRisk || 'none');
|
|
309
|
+
btn.setAttribute('data-streaming', stateOpts?.liveSpeed?.active ? 'true' : 'false');
|
|
310
|
+
btn.setAttribute('data-dual-svg', svgMarkup);
|
|
311
|
+
btn.setAttribute('title', buildCapsuleTooltip(snap, stateOpts));
|
|
312
|
+
if (btn.style && typeof btn.style.setProperty === 'function') {
|
|
313
|
+
btn.style.setProperty('--agy-capsule-color', color);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
if (compactBtn) {
|
|
317
|
+
const showCompact = Boolean(stateOpts?.showQuickCompact);
|
|
318
|
+
compactBtn.hidden = !showCompact;
|
|
319
|
+
if (showCompact) {
|
|
320
|
+
compactBtn.removeAttribute?.('hidden');
|
|
321
|
+
if (compactBtn.style?.removeProperty) compactBtn.style.removeProperty('display');
|
|
322
|
+
} else {
|
|
323
|
+
compactBtn.setAttribute?.('hidden', 'true');
|
|
324
|
+
if (compactBtn.style?.setProperty) compactBtn.style.setProperty('display', 'none');
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
function resolveComposerContainer(el) {
|
|
330
|
+
if (!el) return null;
|
|
331
|
+
const isFooter = String(el.className || '').split(/\s+/).includes('agy-composer-footer');
|
|
332
|
+
if (isFooter && typeof el.closest === 'function') {
|
|
333
|
+
return el.closest('.surface-composer, [data-testid="agent-input-box"]') || el.parentElement || el;
|
|
334
|
+
}
|
|
335
|
+
return el;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
function ensureAttachedToComposer(rawContainerEl) {
|
|
339
|
+
const containerEl = resolveComposerContainer(rawContainerEl);
|
|
340
|
+
const targetDoc = containerEl?.ownerDocument || doc || globalThis.document;
|
|
341
|
+
if (!enabled || !containerEl || !targetDoc?.createElement) return null;
|
|
342
|
+
|
|
343
|
+
const hasParent = Boolean(containerEl.parentElement);
|
|
344
|
+
const mountParent = containerEl.parentElement || containerEl;
|
|
345
|
+
const directWrap = Array.from(mountParent.children || [])
|
|
346
|
+
.find((ch) => ch?.getAttribute?.('data-agy-context-wrap') === '1') || null;
|
|
347
|
+
const innerWrap = !directWrap && containerEl.querySelector
|
|
348
|
+
? containerEl.querySelector('[data-agy-context-wrap="1"]')
|
|
349
|
+
: null;
|
|
350
|
+
let wrap = directWrap || innerWrap;
|
|
351
|
+
let btn = wrap?.querySelector ? wrap.querySelector('[data-agy-context-capsule="1"]') : null;
|
|
352
|
+
let compactBtn = wrap?.querySelector ? wrap.querySelector('[data-agy-compact-quick="1"]') : null;
|
|
353
|
+
|
|
354
|
+
if (!wrap) {
|
|
355
|
+
wrap = targetDoc.createElement('div');
|
|
356
|
+
wrap.id = 'agy-pulse-context-wrap';
|
|
357
|
+
wrap.setAttribute('data-agy-context-wrap', '1');
|
|
358
|
+
wrap.setAttribute('data-position', 'below-composer');
|
|
359
|
+
|
|
360
|
+
btn = targetDoc.createElement('button');
|
|
361
|
+
btn.type = 'button';
|
|
362
|
+
btn.id = 'agy-pulse-context-capsule';
|
|
363
|
+
btn.className = 'agy-pulse-capsule';
|
|
364
|
+
btn.setAttribute('data-agy-context-capsule', '1');
|
|
365
|
+
btn.addEventListener('click', () => onClick(btn));
|
|
366
|
+
wrap.appendChild(btn);
|
|
367
|
+
|
|
368
|
+
compactBtn = targetDoc.createElement('button');
|
|
369
|
+
compactBtn.type = 'button';
|
|
370
|
+
compactBtn.id = 'agy-pulse-compact-btn';
|
|
371
|
+
compactBtn.setAttribute('data-agy-compact-quick', '1');
|
|
372
|
+
compactBtn.setAttribute('title', '发送 /compact 压缩当前对话上下文');
|
|
373
|
+
compactBtn.hidden = true;
|
|
374
|
+
compactBtn.setAttribute('hidden', 'true');
|
|
375
|
+
const iconSpan = targetDoc.createElement('span');
|
|
376
|
+
iconSpan.className = 'agy-pulse-icon';
|
|
377
|
+
iconSpan.innerHTML = COMPACT_ICON_SVG;
|
|
378
|
+
const lblSpan = targetDoc.createElement('span');
|
|
379
|
+
lblSpan.textContent = '压缩';
|
|
380
|
+
compactBtn.appendChild(iconSpan);
|
|
381
|
+
compactBtn.appendChild(lblSpan);
|
|
382
|
+
compactBtn.addEventListener('click', () => onCompactClick());
|
|
383
|
+
wrap.appendChild(compactBtn);
|
|
384
|
+
} else {
|
|
385
|
+
wrap.setAttribute?.('data-position', 'below-composer');
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
const parentChildren = Array.from(mountParent.children || []);
|
|
389
|
+
const cIdx = hasParent ? parentChildren.indexOf(containerEl) : -1;
|
|
390
|
+
const wIdx = parentChildren.indexOf(wrap);
|
|
391
|
+
const needMove = wrap.parentElement !== mountParent
|
|
392
|
+
|| (hasParent && cIdx >= 0 && wIdx !== cIdx + 1);
|
|
393
|
+
if (needMove) {
|
|
394
|
+
const nextRef = hasParent && cIdx >= 0
|
|
395
|
+
? (parentChildren[cIdx + 1] && parentChildren[cIdx + 1] !== wrap ? parentChildren[cIdx + 1] : null)
|
|
396
|
+
: null;
|
|
397
|
+
if (nextRef && typeof mountParent.insertBefore === 'function') {
|
|
398
|
+
mountParent.insertBefore(wrap, nextRef);
|
|
399
|
+
} else {
|
|
400
|
+
mountParent.appendChild(wrap);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
let entry = Array.from(mountedEntries).find((e) => e.wrap === wrap);
|
|
405
|
+
if (!entry) {
|
|
406
|
+
entry = { wrap, btn, compactBtn };
|
|
407
|
+
mountedEntries.add(entry);
|
|
408
|
+
}
|
|
409
|
+
applyStyleAndContent(entry, lastSnapshot, lastState);
|
|
410
|
+
return btn;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
const stopObserve = anchors?.observe
|
|
414
|
+
? anchors.observe('composer.root', (el) => {
|
|
415
|
+
if (el && enabled) ensureAttachedToComposer(el);
|
|
416
|
+
})
|
|
417
|
+
: () => {};
|
|
418
|
+
|
|
419
|
+
if (enabled && anchors?.query) {
|
|
420
|
+
const cur = anchors.query('composer.root');
|
|
421
|
+
if (cur) ensureAttachedToComposer(cur);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
return {
|
|
425
|
+
update(snapshot, stateOpts = {}) {
|
|
426
|
+
lastSnapshot = snapshot || null;
|
|
427
|
+
if (stateOpts && typeof stateOpts === 'object') {
|
|
428
|
+
lastState = {
|
|
429
|
+
showSpeed: stateOpts.showSpeed !== undefined ? stateOpts.showSpeed !== false : lastState.showSpeed,
|
|
430
|
+
liveSpeed: stateOpts.liveSpeed !== undefined ? stateOpts.liveSpeed : lastState.liveSpeed,
|
|
431
|
+
showQuickCompact: stateOpts.showQuickCompact !== undefined ? Boolean(stateOpts.showQuickCompact) : lastState.showQuickCompact
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
if (!enabled) return;
|
|
435
|
+
for (const entry of Array.from(mountedEntries)) {
|
|
436
|
+
if (!entry.wrap?.parentElement || entry.wrap.isConnected === false) {
|
|
437
|
+
mountedEntries.delete(entry);
|
|
438
|
+
continue;
|
|
439
|
+
}
|
|
440
|
+
applyStyleAndContent(entry, lastSnapshot, lastState);
|
|
441
|
+
}
|
|
442
|
+
if (mountedEntries.size === 0 && anchors?.query) {
|
|
443
|
+
const el = anchors.query('composer.root');
|
|
444
|
+
if (el) ensureAttachedToComposer(el);
|
|
445
|
+
}
|
|
446
|
+
},
|
|
447
|
+
setEnabled(nextEnabled) {
|
|
448
|
+
enabled = Boolean(nextEnabled);
|
|
449
|
+
if (!enabled) {
|
|
450
|
+
for (const entry of Array.from(mountedEntries)) {
|
|
451
|
+
if (entry.wrap?.remove) entry.wrap.remove();
|
|
452
|
+
}
|
|
453
|
+
mountedEntries.clear();
|
|
454
|
+
} else if (anchors?.query) {
|
|
455
|
+
const el = anchors.query('composer.root');
|
|
456
|
+
if (el) ensureAttachedToComposer(el);
|
|
457
|
+
}
|
|
458
|
+
},
|
|
459
|
+
getMountedCount() {
|
|
460
|
+
return mountedEntries.size;
|
|
461
|
+
},
|
|
462
|
+
dispose() {
|
|
463
|
+
if (typeof stopObserve === 'function') stopObserve();
|
|
464
|
+
for (const entry of Array.from(mountedEntries)) {
|
|
465
|
+
if (entry.wrap?.remove) entry.wrap.remove();
|
|
466
|
+
}
|
|
467
|
+
mountedEntries.clear();
|
|
468
|
+
}
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
module.exports = {
|
|
473
|
+
SESSION_STACK_ICON_SVG,
|
|
474
|
+
SPEED_BOLT_ICON_SVG,
|
|
475
|
+
ALERT_TRIANGLE_ICON_SVG,
|
|
476
|
+
COMPACT_ICON_SVG,
|
|
477
|
+
PULSE_CAPSULE_CSS,
|
|
478
|
+
buildDualRatioSvg,
|
|
479
|
+
formatSpeedCompact,
|
|
480
|
+
buildCapsuleSegments,
|
|
481
|
+
buildCapsuleTooltip,
|
|
482
|
+
buildCapsuleText,
|
|
483
|
+
createPulseCapsuleController
|
|
484
|
+
};
|