@copilotkit/web-components 1.62.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 +21 -0
- package/README.md +90 -0
- package/dist/index.cjs +12 -0
- package/dist/index.d.cts +7 -0
- package/dist/index.d.mts +7 -0
- package/dist/index.mjs +7 -0
- package/dist/index.umd.js +2548 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/threads-drawer/copilotkit-threads-drawer.cjs +677 -0
- package/dist/threads-drawer/copilotkit-threads-drawer.cjs.map +1 -0
- package/dist/threads-drawer/copilotkit-threads-drawer.d.cts +182 -0
- package/dist/threads-drawer/copilotkit-threads-drawer.d.cts.map +1 -0
- package/dist/threads-drawer/copilotkit-threads-drawer.d.mts +182 -0
- package/dist/threads-drawer/copilotkit-threads-drawer.d.mts.map +1 -0
- package/dist/threads-drawer/copilotkit-threads-drawer.mjs +676 -0
- package/dist/threads-drawer/copilotkit-threads-drawer.mjs.map +1 -0
- package/dist/threads-drawer/define.cjs +15 -0
- package/dist/threads-drawer/define.cjs.map +1 -0
- package/dist/threads-drawer/define.d.cts +17 -0
- package/dist/threads-drawer/define.d.cts.map +1 -0
- package/dist/threads-drawer/define.d.mts +17 -0
- package/dist/threads-drawer/define.d.mts.map +1 -0
- package/dist/threads-drawer/define.mjs +15 -0
- package/dist/threads-drawer/define.mjs.map +1 -0
- package/dist/threads-drawer/generated-tokens.cjs +33 -0
- package/dist/threads-drawer/generated-tokens.cjs.map +1 -0
- package/dist/threads-drawer/generated-tokens.d.cts +32 -0
- package/dist/threads-drawer/generated-tokens.d.cts.map +1 -0
- package/dist/threads-drawer/generated-tokens.d.mts +32 -0
- package/dist/threads-drawer/generated-tokens.d.mts.map +1 -0
- package/dist/threads-drawer/generated-tokens.mjs +32 -0
- package/dist/threads-drawer/generated-tokens.mjs.map +1 -0
- package/dist/threads-drawer/index.cjs +11 -0
- package/dist/threads-drawer/index.d.cts +6 -0
- package/dist/threads-drawer/index.d.mts +6 -0
- package/dist/threads-drawer/index.mjs +6 -0
- package/dist/threads-drawer/styles.cjs +499 -0
- package/dist/threads-drawer/styles.cjs.map +1 -0
- package/dist/threads-drawer/styles.d.cts +24 -0
- package/dist/threads-drawer/styles.d.cts.map +1 -0
- package/dist/threads-drawer/styles.d.mts +24 -0
- package/dist/threads-drawer/styles.d.mts.map +1 -0
- package/dist/threads-drawer/styles.mjs +499 -0
- package/dist/threads-drawer/styles.mjs.map +1 -0
- package/dist/threads-drawer/types.d.cts +97 -0
- package/dist/threads-drawer/types.d.cts.map +1 -0
- package/dist/threads-drawer/types.d.mts +97 -0
- package/dist/threads-drawer/types.d.mts.map +1 -0
- package/package.json +85 -0
|
@@ -0,0 +1,676 @@
|
|
|
1
|
+
import { drawerStyles } from "./styles.mjs";
|
|
2
|
+
import { LitElement, html, nothing } from "lit";
|
|
3
|
+
import { repeat } from "lit/directives/repeat.js";
|
|
4
|
+
import { classMap } from "lit/directives/class-map.js";
|
|
5
|
+
|
|
6
|
+
//#region src/threads-drawer/copilotkit-threads-drawer.ts
|
|
7
|
+
/** Tag name the element registers under. */
|
|
8
|
+
const COPILOTKIT_THREADS_DRAWER_TAG = "copilotkit-threads-drawer";
|
|
9
|
+
/** Mobile breakpoint (px). At or below this width the drawer is a modal overlay. */
|
|
10
|
+
const MOBILE_BREAKPOINT = 768;
|
|
11
|
+
/**
|
|
12
|
+
* Whether an inbound error message should surface an error state. Only a
|
|
13
|
+
* non-empty, non-whitespace string counts — `null`, `""`, and whitespace-only
|
|
14
|
+
* strings are treated as "no error" so an empty error never conflates with a
|
|
15
|
+
* real one (and never blanks the list behind an empty error panel).
|
|
16
|
+
*/
|
|
17
|
+
function hasErrorMessage(value) {
|
|
18
|
+
return value !== null && value.trim().length > 0;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Builds the per-row slot name (`row:<id>`) used for consumer projection, but
|
|
22
|
+
* only when the thread id is safe to embed in a slot `name` / `slot` attribute
|
|
23
|
+
* token. A slot name is matched character-for-character against the consumer's
|
|
24
|
+
* `slot="..."` attribute; whitespace splits attribute tokens and quotes/angle
|
|
25
|
+
* brackets can break attribute serialization, so an id containing any of those
|
|
26
|
+
* would silently fail projection. For such an id we return `null` and the row
|
|
27
|
+
* renders its default name span (no per-row slot), which is the safe fallback.
|
|
28
|
+
*/
|
|
29
|
+
function rowSlotName(id) {
|
|
30
|
+
if (id.length === 0 || /[\s"'<>=]/.test(id)) return null;
|
|
31
|
+
return `row:${id}`;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Inline row-action icons. The element is framework-agnostic Lit, so it cannot
|
|
35
|
+
* depend on a React icon library — these are the lucide `archive`,
|
|
36
|
+
* `archive-restore`, and `trash-2` glyphs inlined as SVG, drawn with
|
|
37
|
+
* `currentColor` so they inherit the button's themed color.
|
|
38
|
+
*/
|
|
39
|
+
const iconArchive = html`
|
|
40
|
+
<svg
|
|
41
|
+
class="row-action-icon"
|
|
42
|
+
viewBox="0 0 24 24"
|
|
43
|
+
fill="none"
|
|
44
|
+
stroke="currentColor"
|
|
45
|
+
stroke-width="2"
|
|
46
|
+
stroke-linecap="round"
|
|
47
|
+
stroke-linejoin="round"
|
|
48
|
+
aria-hidden="true"
|
|
49
|
+
>
|
|
50
|
+
<rect width="20" height="5" x="2" y="3" rx="1" />
|
|
51
|
+
<path d="M4 8v11a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8" />
|
|
52
|
+
<path d="M10 12h4" />
|
|
53
|
+
</svg>
|
|
54
|
+
`;
|
|
55
|
+
const iconUnarchive = html`
|
|
56
|
+
<svg
|
|
57
|
+
class="row-action-icon"
|
|
58
|
+
viewBox="0 0 24 24"
|
|
59
|
+
fill="none"
|
|
60
|
+
stroke="currentColor"
|
|
61
|
+
stroke-width="2"
|
|
62
|
+
stroke-linecap="round"
|
|
63
|
+
stroke-linejoin="round"
|
|
64
|
+
aria-hidden="true"
|
|
65
|
+
>
|
|
66
|
+
<rect width="20" height="5" x="2" y="3" rx="1" />
|
|
67
|
+
<path d="M4 8v11a2 2 0 0 0 2 2h2" />
|
|
68
|
+
<path d="M20 8v11a2 2 0 0 1-2 2h-2" />
|
|
69
|
+
<path d="m9 15 3-3 3 3" />
|
|
70
|
+
<path d="M12 12v9" />
|
|
71
|
+
</svg>
|
|
72
|
+
`;
|
|
73
|
+
const iconDelete = html`
|
|
74
|
+
<svg
|
|
75
|
+
class="row-action-icon"
|
|
76
|
+
viewBox="0 0 24 24"
|
|
77
|
+
fill="none"
|
|
78
|
+
stroke="currentColor"
|
|
79
|
+
stroke-width="2"
|
|
80
|
+
stroke-linecap="round"
|
|
81
|
+
stroke-linejoin="round"
|
|
82
|
+
aria-hidden="true"
|
|
83
|
+
>
|
|
84
|
+
<path d="M3 6h18" />
|
|
85
|
+
<path
|
|
86
|
+
d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"
|
|
87
|
+
/>
|
|
88
|
+
<line x1="10" x2="10" y1="11" y2="17" />
|
|
89
|
+
<line x1="14" x2="14" y1="11" y2="17" />
|
|
90
|
+
</svg>
|
|
91
|
+
`;
|
|
92
|
+
const iconLauncher = html`
|
|
93
|
+
<svg
|
|
94
|
+
class="launcher-icon"
|
|
95
|
+
viewBox="0 0 24 24"
|
|
96
|
+
fill="none"
|
|
97
|
+
stroke="currentColor"
|
|
98
|
+
stroke-width="2"
|
|
99
|
+
stroke-linecap="round"
|
|
100
|
+
stroke-linejoin="round"
|
|
101
|
+
aria-hidden="true"
|
|
102
|
+
>
|
|
103
|
+
<rect width="18" height="18" x="3" y="3" rx="2" />
|
|
104
|
+
<path d="M9 3v18" />
|
|
105
|
+
<path d="m14 9 3 3-3 3" />
|
|
106
|
+
</svg>
|
|
107
|
+
`;
|
|
108
|
+
/**
|
|
109
|
+
* `<copilotkit-threads-drawer>` — a public, self-contained, controlled, framework-agnostic
|
|
110
|
+
* threads drawer rendered into a shadow root.
|
|
111
|
+
*
|
|
112
|
+
* Pure VIEW: domain data (threads/loading/error/etc.) comes IN as properties and
|
|
113
|
+
* user intent goes OUT as bubbling+composed DOM `CustomEvent`s. The element owns
|
|
114
|
+
* VIEW state (open/collapsed, Active/All filter, confirm-delete dialog, per-row
|
|
115
|
+
* animations) while the consumer owns DOMAIN state. `open` is additionally
|
|
116
|
+
* externally controllable via the `open` property + `open-change` event so a
|
|
117
|
+
* host can coordinate mobile open/close.
|
|
118
|
+
*
|
|
119
|
+
* The element is AUTHORITATIVE over row order and the Active/All filter.
|
|
120
|
+
*/
|
|
121
|
+
var CopilotKitThreadsDrawer = class extends LitElement {
|
|
122
|
+
constructor(..._args) {
|
|
123
|
+
super(..._args);
|
|
124
|
+
this.label = "Threads";
|
|
125
|
+
this.threads = [];
|
|
126
|
+
this.loading = false;
|
|
127
|
+
this.error = null;
|
|
128
|
+
this.activeThreadId = null;
|
|
129
|
+
this.licensed = true;
|
|
130
|
+
this.licenseUrl = "https://docs.copilotkit.ai/intelligence";
|
|
131
|
+
this.hasMore = false;
|
|
132
|
+
this.fetchingMore = false;
|
|
133
|
+
this.fetchMoreError = null;
|
|
134
|
+
this.open = true;
|
|
135
|
+
this.collapsed = false;
|
|
136
|
+
this._filter = "active";
|
|
137
|
+
this._confirmingDeleteId = null;
|
|
138
|
+
this._viewportIsMobile = false;
|
|
139
|
+
this._hasMemories = false;
|
|
140
|
+
this._hasFooter = false;
|
|
141
|
+
this._mediaQuery = null;
|
|
142
|
+
this._onMediaChange = (event) => {
|
|
143
|
+
this._viewportIsMobile = event.matches;
|
|
144
|
+
};
|
|
145
|
+
this._onKeyDown = (event) => {
|
|
146
|
+
if (event.key === "Escape") {
|
|
147
|
+
if (this._confirmingDeleteId !== null) {
|
|
148
|
+
this._confirmingDeleteId = null;
|
|
149
|
+
event.stopPropagation();
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (this._viewportIsMobile && this.open) {
|
|
153
|
+
this._setOpen(false);
|
|
154
|
+
event.stopPropagation();
|
|
155
|
+
}
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
if (event.key === "Tab") this._trapTab(event);
|
|
159
|
+
};
|
|
160
|
+
this._seenNamed = /* @__PURE__ */ new Set();
|
|
161
|
+
this._justRevealed = /* @__PURE__ */ new Set();
|
|
162
|
+
this._scrollLocked = false;
|
|
163
|
+
this._prevBodyOverflow = "";
|
|
164
|
+
this._trapTab = (event) => {
|
|
165
|
+
if (event.key !== "Tab" || !this._isMobileModalOpen()) return;
|
|
166
|
+
const focusable = this._composedFocusable();
|
|
167
|
+
if (focusable.length === 0) return;
|
|
168
|
+
const first = focusable[0];
|
|
169
|
+
const last = focusable[focusable.length - 1];
|
|
170
|
+
const active = this.renderRoot.activeElement ?? document.activeElement;
|
|
171
|
+
if (event.shiftKey && active === first) {
|
|
172
|
+
last.focus();
|
|
173
|
+
event.preventDefault();
|
|
174
|
+
} else if (!event.shiftKey && active === last) {
|
|
175
|
+
first.focus();
|
|
176
|
+
event.preventDefault();
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
static {
|
|
181
|
+
this.styles = drawerStyles;
|
|
182
|
+
}
|
|
183
|
+
static {
|
|
184
|
+
this.properties = {
|
|
185
|
+
threads: { attribute: false },
|
|
186
|
+
loading: { type: Boolean },
|
|
187
|
+
error: { type: String },
|
|
188
|
+
activeThreadId: {
|
|
189
|
+
attribute: "active-thread-id",
|
|
190
|
+
type: String
|
|
191
|
+
},
|
|
192
|
+
licensed: { type: Boolean },
|
|
193
|
+
licenseUrl: {
|
|
194
|
+
attribute: "license-url",
|
|
195
|
+
type: String
|
|
196
|
+
},
|
|
197
|
+
hasMore: {
|
|
198
|
+
attribute: "has-more",
|
|
199
|
+
type: Boolean
|
|
200
|
+
},
|
|
201
|
+
fetchingMore: {
|
|
202
|
+
attribute: "fetching-more",
|
|
203
|
+
type: Boolean
|
|
204
|
+
},
|
|
205
|
+
fetchMoreError: {
|
|
206
|
+
attribute: "fetch-more-error",
|
|
207
|
+
type: String
|
|
208
|
+
},
|
|
209
|
+
label: { type: String },
|
|
210
|
+
open: {
|
|
211
|
+
type: Boolean,
|
|
212
|
+
reflect: true
|
|
213
|
+
},
|
|
214
|
+
collapsed: {
|
|
215
|
+
type: Boolean,
|
|
216
|
+
reflect: true
|
|
217
|
+
},
|
|
218
|
+
_filter: { state: true },
|
|
219
|
+
_confirmingDeleteId: { state: true },
|
|
220
|
+
_viewportIsMobile: { state: true },
|
|
221
|
+
_hasMemories: { state: true },
|
|
222
|
+
_hasFooter: { state: true }
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
connectedCallback() {
|
|
226
|
+
super.connectedCallback();
|
|
227
|
+
if (typeof window !== "undefined" && window.matchMedia) {
|
|
228
|
+
this._mediaQuery = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT}px)`);
|
|
229
|
+
this._viewportIsMobile = this._mediaQuery.matches;
|
|
230
|
+
this._mediaQuery.addEventListener("change", this._onMediaChange);
|
|
231
|
+
}
|
|
232
|
+
this.addEventListener("keydown", this._onKeyDown);
|
|
233
|
+
}
|
|
234
|
+
disconnectedCallback() {
|
|
235
|
+
super.disconnectedCallback();
|
|
236
|
+
this._mediaQuery?.removeEventListener("change", this._onMediaChange);
|
|
237
|
+
this.removeEventListener("keydown", this._onKeyDown);
|
|
238
|
+
this._releaseScrollLock();
|
|
239
|
+
}
|
|
240
|
+
willUpdate(changed) {
|
|
241
|
+
if (changed.has("threads")) {
|
|
242
|
+
const presentIds = /* @__PURE__ */ new Set();
|
|
243
|
+
const revealed = /* @__PURE__ */ new Set();
|
|
244
|
+
for (const thread of this.threads) {
|
|
245
|
+
presentIds.add(thread.id);
|
|
246
|
+
const hasName = thread.name !== null && thread.name !== "";
|
|
247
|
+
if (hasName && !this._seenNamed.has(thread.id)) revealed.add(thread.id);
|
|
248
|
+
if (hasName) this._seenNamed.add(thread.id);
|
|
249
|
+
}
|
|
250
|
+
for (const id of this._seenNamed) if (!presentIds.has(id)) this._seenNamed.delete(id);
|
|
251
|
+
this._justRevealed = revealed;
|
|
252
|
+
if (this._confirmingDeleteId !== null) {
|
|
253
|
+
if (!this._visibleThreads().some((t) => t.id === this._confirmingDeleteId)) this._confirmingDeleteId = null;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
updated(changed) {
|
|
258
|
+
if (changed.has("open") || changed.has("_viewportIsMobile")) if (this._isMobileModalOpen()) {
|
|
259
|
+
this._applyScrollLock();
|
|
260
|
+
this._focusFirstFocusable();
|
|
261
|
+
} else this._releaseScrollLock();
|
|
262
|
+
if (this._justRevealed.size > 0) this._justRevealed = /* @__PURE__ */ new Set();
|
|
263
|
+
this._syncNameClipping();
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Marks each row whose name text is truncated with `name-clipped`, so the CSS
|
|
267
|
+
* shows the name tooltip ONLY when the full name isn't already visible (an
|
|
268
|
+
* always-on bubble over every row on hover would be noise). Measured after
|
|
269
|
+
* render because truncation depends on the laid-out width.
|
|
270
|
+
*/
|
|
271
|
+
_syncNameClipping() {
|
|
272
|
+
this.renderRoot.querySelectorAll(".row-name").forEach((name) => {
|
|
273
|
+
const text = name.querySelector(".row-name-text");
|
|
274
|
+
const clipped = !!text && text.scrollWidth > text.clientWidth;
|
|
275
|
+
name.classList.toggle("name-clipped", clipped);
|
|
276
|
+
name.closest(".row")?.classList.toggle("name-clipped", clipped);
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
_isMobileModalOpen() {
|
|
280
|
+
return this._viewportIsMobile && this.open;
|
|
281
|
+
}
|
|
282
|
+
_setOpen(next) {
|
|
283
|
+
if (this.open === next) return;
|
|
284
|
+
this.open = next;
|
|
285
|
+
this._emit("open-change", { open: next });
|
|
286
|
+
}
|
|
287
|
+
_setFilter(next) {
|
|
288
|
+
if (this._filter === next) return;
|
|
289
|
+
this._filter = next;
|
|
290
|
+
this._emit("filter-change", { filter: next });
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Element-authoritative ordering + filtering. Active filter hides archived
|
|
294
|
+
* threads; ordering is most-recent-first by `lastRunAt`/`updatedAt`.
|
|
295
|
+
*/
|
|
296
|
+
_visibleThreads() {
|
|
297
|
+
const filtered = this._filter === "active" ? this.threads.filter((t) => !t.archived) : this.threads;
|
|
298
|
+
const ts = (t) => {
|
|
299
|
+
const raw = t.lastRunAt ?? t.updatedAt ?? t.createdAt;
|
|
300
|
+
const parsed = Date.parse(raw);
|
|
301
|
+
return Number.isNaN(parsed) ? null : parsed;
|
|
302
|
+
};
|
|
303
|
+
return [...filtered].map((thread, index) => ({
|
|
304
|
+
thread,
|
|
305
|
+
index,
|
|
306
|
+
ts: ts(thread)
|
|
307
|
+
})).sort((a, b) => {
|
|
308
|
+
if (a.ts === null && b.ts === null) return a.index - b.index;
|
|
309
|
+
if (a.ts === null) return 1;
|
|
310
|
+
if (b.ts === null) return -1;
|
|
311
|
+
if (a.ts !== b.ts) return b.ts - a.ts;
|
|
312
|
+
return a.index - b.index;
|
|
313
|
+
}).map((entry) => entry.thread);
|
|
314
|
+
}
|
|
315
|
+
_emit(type, detail) {
|
|
316
|
+
this.dispatchEvent(new CustomEvent(type, {
|
|
317
|
+
detail,
|
|
318
|
+
bubbles: true,
|
|
319
|
+
composed: true
|
|
320
|
+
}));
|
|
321
|
+
}
|
|
322
|
+
_applyScrollLock() {
|
|
323
|
+
if (this._scrollLocked || typeof document === "undefined") return;
|
|
324
|
+
this._prevBodyOverflow = document.body.style.overflow;
|
|
325
|
+
document.body.style.overflow = "hidden";
|
|
326
|
+
this._scrollLocked = true;
|
|
327
|
+
}
|
|
328
|
+
_releaseScrollLock() {
|
|
329
|
+
if (!this._scrollLocked || typeof document === "undefined") return;
|
|
330
|
+
document.body.style.overflow = this._prevBodyOverflow;
|
|
331
|
+
this._scrollLocked = false;
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Returns the focusable elements in the composed/flattened tree of the drawer
|
|
335
|
+
* root, INCLUDING slotted light-DOM content (so a focus trap covers slotted
|
|
336
|
+
* rows). Walks assigned nodes of each `<slot>`.
|
|
337
|
+
*/
|
|
338
|
+
_composedFocusable() {
|
|
339
|
+
const selector = "a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex=\"-1\"])";
|
|
340
|
+
const out = [];
|
|
341
|
+
const backdrop = this.renderRoot.querySelector(".backdrop");
|
|
342
|
+
if (backdrop) out.push(backdrop);
|
|
343
|
+
const root = this.renderRoot.querySelector(".root");
|
|
344
|
+
if (!root) return out;
|
|
345
|
+
const collect = (node) => {
|
|
346
|
+
node.querySelectorAll(selector).forEach((el) => out.push(el));
|
|
347
|
+
};
|
|
348
|
+
collect(root);
|
|
349
|
+
root.querySelectorAll("slot").forEach((slot) => {
|
|
350
|
+
slot.assignedElements({ flatten: true }).forEach((assigned) => {
|
|
351
|
+
if (assigned.matches(selector)) out.push(assigned);
|
|
352
|
+
collect(assigned);
|
|
353
|
+
});
|
|
354
|
+
});
|
|
355
|
+
return out;
|
|
356
|
+
}
|
|
357
|
+
_focusFirstFocusable() {
|
|
358
|
+
queueMicrotask(() => {
|
|
359
|
+
const focusable = this._composedFocusable();
|
|
360
|
+
const backdrop = this.renderRoot.querySelector(".backdrop");
|
|
361
|
+
(focusable.find((el) => el !== backdrop) ?? focusable[0])?.focus();
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
render() {
|
|
365
|
+
const rootClasses = {
|
|
366
|
+
root: true,
|
|
367
|
+
collapsed: this.collapsed && !this._viewportIsMobile,
|
|
368
|
+
mobile: this._viewportIsMobile,
|
|
369
|
+
open: this.open,
|
|
370
|
+
confirming: this._confirmingDeleteId !== null
|
|
371
|
+
};
|
|
372
|
+
return html`
|
|
373
|
+
${this._viewportIsMobile && !this.open ? html`<button
|
|
374
|
+
class="launcher"
|
|
375
|
+
part="launcher"
|
|
376
|
+
aria-label="Open threads"
|
|
377
|
+
@click=${() => this._setOpen(true)}
|
|
378
|
+
>
|
|
379
|
+
<slot name="launcher-icon">${iconLauncher}</slot>
|
|
380
|
+
</button>` : nothing}
|
|
381
|
+
${this._isMobileModalOpen() ? html`<button
|
|
382
|
+
class="backdrop"
|
|
383
|
+
part="backdrop"
|
|
384
|
+
aria-label="Close threads drawer"
|
|
385
|
+
@click=${() => this._setOpen(false)}
|
|
386
|
+
></button>` : nothing}
|
|
387
|
+
<div
|
|
388
|
+
class=${classMap(rootClasses)}
|
|
389
|
+
part="root"
|
|
390
|
+
role=${this._isMobileModalOpen() ? "dialog" : "region"}
|
|
391
|
+
aria-modal=${this._isMobileModalOpen() ? "true" : nothing}
|
|
392
|
+
aria-label=${this.label}
|
|
393
|
+
>
|
|
394
|
+
${this._renderHeader()} ${this._renderBody()} ${this._renderMemories()}
|
|
395
|
+
${this._renderFooter()} ${this._renderConfirmDialog()}
|
|
396
|
+
</div>
|
|
397
|
+
`;
|
|
398
|
+
}
|
|
399
|
+
_renderHeader() {
|
|
400
|
+
return html`
|
|
401
|
+
<div class="header" part="header">
|
|
402
|
+
<slot name="header"><span>${this.label}</span></slot>
|
|
403
|
+
<button
|
|
404
|
+
class="primary"
|
|
405
|
+
part="new-thread-button"
|
|
406
|
+
aria-label="New thread"
|
|
407
|
+
@click=${() => this._emit("new-thread", {})}
|
|
408
|
+
>
|
|
409
|
+
+ New
|
|
410
|
+
</button>
|
|
411
|
+
</div>
|
|
412
|
+
${this.licensed && (!hasErrorMessage(this.error) || this.threads.length > 0) ? html`
|
|
413
|
+
<div class="filters" part="filters" role="group" aria-label="Filter threads">
|
|
414
|
+
<button
|
|
415
|
+
class="filter-btn"
|
|
416
|
+
part="filter-active"
|
|
417
|
+
aria-pressed=${this._filter === "active"}
|
|
418
|
+
@click=${() => this._setFilter("active")}
|
|
419
|
+
>
|
|
420
|
+
Active
|
|
421
|
+
</button>
|
|
422
|
+
<button
|
|
423
|
+
class="filter-btn"
|
|
424
|
+
part="filter-all"
|
|
425
|
+
aria-pressed=${this._filter === "all"}
|
|
426
|
+
@click=${() => this._setFilter("all")}
|
|
427
|
+
>
|
|
428
|
+
All
|
|
429
|
+
</button>
|
|
430
|
+
</div>
|
|
431
|
+
` : nothing}
|
|
432
|
+
`;
|
|
433
|
+
}
|
|
434
|
+
_renderBody() {
|
|
435
|
+
if (!this.licensed) return this._renderLicensed();
|
|
436
|
+
if (hasErrorMessage(this.error) && this.threads.length === 0) return this._renderError();
|
|
437
|
+
if (this.loading && this.threads.length === 0) return this._renderLoading();
|
|
438
|
+
return this._renderList();
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* Handles the Upgrade CTA click in the locked view. Dispatches a cancelable
|
|
442
|
+
* `licensed` event carrying the resolved `licenseUrl`; unless a host calls
|
|
443
|
+
* `preventDefault()`, opens that URL in a new tab. A blank `licenseUrl`
|
|
444
|
+
* suppresses navigation so the event alone drives host behavior.
|
|
445
|
+
*/
|
|
446
|
+
_onLicensedCta() {
|
|
447
|
+
if (this.dispatchEvent(new CustomEvent("licensed", {
|
|
448
|
+
detail: { licenseUrl: this.licenseUrl },
|
|
449
|
+
bubbles: true,
|
|
450
|
+
composed: true,
|
|
451
|
+
cancelable: true
|
|
452
|
+
})) && this.licenseUrl && typeof window !== "undefined") window.open(this.licenseUrl, "_blank", "noopener,noreferrer");
|
|
453
|
+
}
|
|
454
|
+
_renderLicensed() {
|
|
455
|
+
return html`
|
|
456
|
+
<div class="licensed" part="licensed" data-testid="drawer-licensed">
|
|
457
|
+
<slot name="licensed">
|
|
458
|
+
<p>Threads are a CopilotKit Intelligence feature.</p>
|
|
459
|
+
<button
|
|
460
|
+
class="primary"
|
|
461
|
+
part="licensed-cta"
|
|
462
|
+
@click=${() => this._onLicensedCta()}
|
|
463
|
+
>
|
|
464
|
+
Upgrade
|
|
465
|
+
</button>
|
|
466
|
+
</slot>
|
|
467
|
+
</div>
|
|
468
|
+
`;
|
|
469
|
+
}
|
|
470
|
+
_renderError() {
|
|
471
|
+
return html`
|
|
472
|
+
<div class="state error" part="error" role="alert" data-testid="drawer-error">
|
|
473
|
+
<p>${this.error}</p>
|
|
474
|
+
<button
|
|
475
|
+
class="primary"
|
|
476
|
+
part="retry-button"
|
|
477
|
+
@click=${() => this._emit("retry", { scope: "initial" })}
|
|
478
|
+
>
|
|
479
|
+
Retry
|
|
480
|
+
</button>
|
|
481
|
+
</div>
|
|
482
|
+
`;
|
|
483
|
+
}
|
|
484
|
+
_renderLoading() {
|
|
485
|
+
return html`
|
|
486
|
+
<div class="state" part="loading" data-testid="drawer-loading" aria-busy="true">
|
|
487
|
+
Loading threads…
|
|
488
|
+
</div>
|
|
489
|
+
`;
|
|
490
|
+
}
|
|
491
|
+
_renderList() {
|
|
492
|
+
const visible = this._visibleThreads();
|
|
493
|
+
if (visible.length === 0) return html`
|
|
494
|
+
<div class="state" part="empty" data-testid="drawer-empty">
|
|
495
|
+
<slot name="empty">No threads yet.</slot>
|
|
496
|
+
</div>
|
|
497
|
+
`;
|
|
498
|
+
return html`
|
|
499
|
+
<ul class="list" part="list" role="listbox" aria-label=${this.label}>
|
|
500
|
+
${repeat(visible, (thread) => thread.id, (thread) => this._renderRow(thread))}
|
|
501
|
+
</ul>
|
|
502
|
+
${this._renderFetchMore()}
|
|
503
|
+
`;
|
|
504
|
+
}
|
|
505
|
+
_renderRow(thread) {
|
|
506
|
+
const isActive = thread.id === this.activeThreadId;
|
|
507
|
+
const hasName = thread.name !== null && thread.name !== "";
|
|
508
|
+
const justRevealed = this._justRevealed.has(thread.id);
|
|
509
|
+
const rowClasses = {
|
|
510
|
+
row: true,
|
|
511
|
+
active: isActive,
|
|
512
|
+
archived: thread.archived
|
|
513
|
+
};
|
|
514
|
+
const nameClasses = {
|
|
515
|
+
"row-name": true,
|
|
516
|
+
placeholder: !hasName,
|
|
517
|
+
revealed: justRevealed
|
|
518
|
+
};
|
|
519
|
+
const slotName = rowSlotName(thread.id);
|
|
520
|
+
const nameSpan = html`<span
|
|
521
|
+
class=${classMap(nameClasses)}
|
|
522
|
+
part="row-name"
|
|
523
|
+
data-tooltip=${hasName ? thread.name : nothing}
|
|
524
|
+
>
|
|
525
|
+
<span class="row-name-text"
|
|
526
|
+
>${hasName ? thread.name : "New thread"}</span
|
|
527
|
+
>
|
|
528
|
+
</span>`;
|
|
529
|
+
return html`
|
|
530
|
+
<li
|
|
531
|
+
class=${classMap(rowClasses)}
|
|
532
|
+
part=${isActive ? "row row-active" : "row"}
|
|
533
|
+
role="option"
|
|
534
|
+
aria-selected=${isActive ? "true" : "false"}
|
|
535
|
+
tabindex="0"
|
|
536
|
+
data-thread-id=${thread.id}
|
|
537
|
+
@click=${() => this._emit("thread-selected", { threadId: thread.id })}
|
|
538
|
+
@keydown=${(e) => {
|
|
539
|
+
if (e.key === "Enter" || e.key === " ") {
|
|
540
|
+
e.preventDefault();
|
|
541
|
+
this._emit("thread-selected", { threadId: thread.id });
|
|
542
|
+
}
|
|
543
|
+
}}
|
|
544
|
+
>
|
|
545
|
+
${slotName !== null ? html`<slot name=${slotName}>${nameSpan}</slot>` : nameSpan}
|
|
546
|
+
${thread.archived ? html`<button
|
|
547
|
+
class="row-action"
|
|
548
|
+
part="row-unarchive"
|
|
549
|
+
data-tooltip="Unarchive"
|
|
550
|
+
aria-label=${`Unarchive thread ${thread.name ?? "New thread"}`}
|
|
551
|
+
@click=${(e) => {
|
|
552
|
+
e.stopPropagation();
|
|
553
|
+
this._emit("unarchive", { threadId: thread.id });
|
|
554
|
+
}}
|
|
555
|
+
>
|
|
556
|
+
${iconUnarchive}
|
|
557
|
+
</button>` : html`<button
|
|
558
|
+
class="row-action"
|
|
559
|
+
part="row-archive"
|
|
560
|
+
data-tooltip="Archive"
|
|
561
|
+
aria-label=${`Archive thread ${thread.name ?? "New thread"}`}
|
|
562
|
+
@click=${(e) => {
|
|
563
|
+
e.stopPropagation();
|
|
564
|
+
this._emit("archive", { threadId: thread.id });
|
|
565
|
+
}}
|
|
566
|
+
>
|
|
567
|
+
${iconArchive}
|
|
568
|
+
</button>`}
|
|
569
|
+
<button
|
|
570
|
+
class="row-action danger"
|
|
571
|
+
part="row-delete"
|
|
572
|
+
data-tooltip="Delete"
|
|
573
|
+
aria-label=${`Delete thread ${thread.name ?? "New thread"}`}
|
|
574
|
+
@click=${(e) => {
|
|
575
|
+
e.stopPropagation();
|
|
576
|
+
this._confirmingDeleteId = thread.id;
|
|
577
|
+
}}
|
|
578
|
+
>
|
|
579
|
+
${iconDelete}
|
|
580
|
+
</button>
|
|
581
|
+
</li>
|
|
582
|
+
`;
|
|
583
|
+
}
|
|
584
|
+
_renderFetchMore() {
|
|
585
|
+
if (hasErrorMessage(this.fetchMoreError)) return html`
|
|
586
|
+
<div class="fetch-more-error" part="fetch-more-error" role="alert">
|
|
587
|
+
Couldn't load more —
|
|
588
|
+
<button
|
|
589
|
+
class="row-action"
|
|
590
|
+
part="fetch-more-retry"
|
|
591
|
+
@click=${() => this._emit("retry", { scope: "fetch-more" })}
|
|
592
|
+
>
|
|
593
|
+
retry
|
|
594
|
+
</button>
|
|
595
|
+
</div>
|
|
596
|
+
`;
|
|
597
|
+
if (this.fetchingMore) return html`
|
|
598
|
+
<div class="state" part="fetching-more" aria-busy="true">Loading more…</div>
|
|
599
|
+
`;
|
|
600
|
+
if (this.hasMore) return html`
|
|
601
|
+
<button
|
|
602
|
+
class="load-more"
|
|
603
|
+
part="load-more"
|
|
604
|
+
@click=${() => this._emit("load-more", {})}
|
|
605
|
+
>
|
|
606
|
+
Load more
|
|
607
|
+
</button>
|
|
608
|
+
`;
|
|
609
|
+
return nothing;
|
|
610
|
+
}
|
|
611
|
+
_renderMemories() {
|
|
612
|
+
return html`
|
|
613
|
+
<div class="memories" part="memories" ?hidden=${!this._hasMemories}>
|
|
614
|
+
<slot
|
|
615
|
+
name="memories"
|
|
616
|
+
@slotchange=${(e) => {
|
|
617
|
+
this._hasMemories = e.target.assignedElements().length > 0;
|
|
618
|
+
}}
|
|
619
|
+
></slot>
|
|
620
|
+
</div>
|
|
621
|
+
`;
|
|
622
|
+
}
|
|
623
|
+
_renderFooter() {
|
|
624
|
+
return html`
|
|
625
|
+
<div class="footer" part="footer" ?hidden=${!this._hasFooter}>
|
|
626
|
+
<slot
|
|
627
|
+
name="footer"
|
|
628
|
+
@slotchange=${(e) => {
|
|
629
|
+
this._hasFooter = e.target.assignedElements().length > 0;
|
|
630
|
+
}}
|
|
631
|
+
></slot>
|
|
632
|
+
</div>
|
|
633
|
+
`;
|
|
634
|
+
}
|
|
635
|
+
_renderConfirmDialog() {
|
|
636
|
+
if (this._confirmingDeleteId === null) return nothing;
|
|
637
|
+
const id = this._confirmingDeleteId;
|
|
638
|
+
return html`
|
|
639
|
+
<div class="dialog-backdrop" part="dialog-backdrop">
|
|
640
|
+
<div
|
|
641
|
+
class="dialog"
|
|
642
|
+
part="confirm-dialog"
|
|
643
|
+
role="alertdialog"
|
|
644
|
+
aria-modal="true"
|
|
645
|
+
aria-label="Confirm delete"
|
|
646
|
+
data-testid="drawer-confirm-delete"
|
|
647
|
+
>
|
|
648
|
+
<p>Delete this thread? This cannot be undone.</p>
|
|
649
|
+
<div class="dialog-actions">
|
|
650
|
+
<button
|
|
651
|
+
class="row-action"
|
|
652
|
+
part="confirm-cancel"
|
|
653
|
+
@click=${() => this._confirmingDeleteId = null}
|
|
654
|
+
>
|
|
655
|
+
Cancel
|
|
656
|
+
</button>
|
|
657
|
+
<button
|
|
658
|
+
class="primary"
|
|
659
|
+
part="confirm-delete"
|
|
660
|
+
@click=${() => {
|
|
661
|
+
this._confirmingDeleteId = null;
|
|
662
|
+
this._emit("delete", { threadId: id });
|
|
663
|
+
}}
|
|
664
|
+
>
|
|
665
|
+
Delete
|
|
666
|
+
</button>
|
|
667
|
+
</div>
|
|
668
|
+
</div>
|
|
669
|
+
</div>
|
|
670
|
+
`;
|
|
671
|
+
}
|
|
672
|
+
};
|
|
673
|
+
|
|
674
|
+
//#endregion
|
|
675
|
+
export { COPILOTKIT_THREADS_DRAWER_TAG, CopilotKitThreadsDrawer };
|
|
676
|
+
//# sourceMappingURL=copilotkit-threads-drawer.mjs.map
|