feat(embed): session sidebar grouping and embed theme alignment (#1657)
Group embed sessions in the sidebar by channel, replace session source filter with grouping buckets, and align shared chat components with embed theme colors.
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
<template>
|
||||
<t-popup
|
||||
v-model:visible="visible"
|
||||
trigger="click"
|
||||
placement="bottom-left"
|
||||
:overlay-style="{ padding: 0 }"
|
||||
:overlay-inner-style="{ padding: 0 }"
|
||||
>
|
||||
<template #content>
|
||||
<div class="session-group-card">
|
||||
<div class="session-group-header">{{ headerLabel }}</div>
|
||||
<div class="session-group-list">
|
||||
<button
|
||||
v-for="item in modes"
|
||||
:key="item.value"
|
||||
type="button"
|
||||
class="session-group-row"
|
||||
:class="{ active: item.value === current }"
|
||||
@click="handleSelect(item.value)"
|
||||
>
|
||||
<span class="session-group-row-name">{{ item.label }}</span>
|
||||
<t-icon
|
||||
v-if="item.value === current"
|
||||
name="check"
|
||||
class="session-group-row-check"
|
||||
size="14px"
|
||||
/>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
<slot />
|
||||
</t-popup>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref } from 'vue'
|
||||
import type { SessionGroupMode } from './sessionGrouping'
|
||||
|
||||
interface GroupModeItem {
|
||||
value: SessionGroupMode
|
||||
label: string
|
||||
}
|
||||
|
||||
defineProps<{
|
||||
modes: GroupModeItem[]
|
||||
current: SessionGroupMode
|
||||
headerLabel: string
|
||||
}>()
|
||||
|
||||
const emit = defineEmits<{
|
||||
(e: 'select', value: SessionGroupMode): void
|
||||
}>()
|
||||
|
||||
const visible = ref(false)
|
||||
|
||||
const handleSelect = (value: SessionGroupMode): void => {
|
||||
visible.value = false
|
||||
emit('select', value)
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped lang="less">
|
||||
.session-group-card {
|
||||
min-width: 180px;
|
||||
max-width: 240px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
padding: 0;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.session-group-header {
|
||||
padding: 8px 12px 6px;
|
||||
font-size: 11px;
|
||||
font-weight: 600;
|
||||
color: var(--td-text-color-placeholder);
|
||||
border-bottom: 1px solid var(--td-component-border);
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
.session-group-list {
|
||||
padding: 6px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1px;
|
||||
}
|
||||
|
||||
.session-group-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
padding: 6px 10px;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
background: transparent;
|
||||
color: var(--td-text-color-primary);
|
||||
font-size: 13px;
|
||||
line-height: 1.4;
|
||||
cursor: pointer;
|
||||
transition: background 0.15s ease, color 0.15s ease;
|
||||
text-align: left;
|
||||
width: 100%;
|
||||
|
||||
&:hover {
|
||||
background: var(--td-bg-color-secondarycontainer);
|
||||
}
|
||||
|
||||
&.active {
|
||||
color: var(--td-brand-color);
|
||||
font-weight: 500;
|
||||
}
|
||||
}
|
||||
|
||||
.session-group-row-name {
|
||||
flex: 1 1 auto;
|
||||
}
|
||||
|
||||
.session-group-row-check {
|
||||
flex: 0 0 auto;
|
||||
color: var(--td-brand-color);
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,56 @@
|
||||
<template>
|
||||
<div
|
||||
:class="[
|
||||
'submenu_item',
|
||||
!batchMode && activePath === item.path ? 'submenu_item_active' : '',
|
||||
batchMode && selectedIds.includes(item.id) ? 'submenu_item_selected' : '',
|
||||
batchMode ? 'submenu_item_batch' : '',
|
||||
]"
|
||||
@mouseenter="emit('hover-in')"
|
||||
@mouseleave="emit('hover-out')"
|
||||
@click="batchMode ? emit('toggle-select') : emit('navigate')"
|
||||
>
|
||||
<t-checkbox
|
||||
v-if="batchMode"
|
||||
class="batch-checkbox"
|
||||
:checked="selectedIds.includes(item.id)"
|
||||
@click.stop
|
||||
@change="emit('toggle-select')"
|
||||
/>
|
||||
<span class="submenu_title" :class="batchMode ? 'submenu_title--batch' : ''" :title="item.title">
|
||||
<t-icon v-if="item.is_pinned" name="pin" class="submenu_pin_icon" />
|
||||
<span class="submenu_title-text">{{ item.title }}</span>
|
||||
</span>
|
||||
<t-dropdown
|
||||
v-if="!batchMode"
|
||||
:options="menuOptions"
|
||||
placement="bottom-right"
|
||||
trigger="click"
|
||||
@click="emit('menu-click', $event)"
|
||||
>
|
||||
<div @click.stop class="menu-more-wrap">
|
||||
<t-icon name="ellipsis" class="menu-more" />
|
||||
</div>
|
||||
</t-dropdown>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
defineProps<{
|
||||
item: { id: string; path: string; title: string; is_pinned?: boolean }
|
||||
batchMode: boolean
|
||||
activePath: string
|
||||
selectedIds: string[]
|
||||
menuOptions: any[]
|
||||
/** 渠道文件夹下的会话(样式与聊天区会话共用文案列对齐) */
|
||||
nested?: boolean
|
||||
}>()
|
||||
|
||||
const emit = defineEmits<{
|
||||
(e: 'navigate'): void
|
||||
(e: 'toggle-select'): void
|
||||
(e: 'menu-click', data: { value: string }): void
|
||||
(e: 'hover-in'): void
|
||||
(e: 'hover-out'): void
|
||||
}>()
|
||||
</script>
|
||||
+696
-358
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,98 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
|
||||
import {
|
||||
EMBED_SESSION_MARKER_PREFIX,
|
||||
classifyDateBucket,
|
||||
configuredPlatforms,
|
||||
groupSessions,
|
||||
groupSessionsBySource,
|
||||
resolveSessionOrigin,
|
||||
shouldShowSessionSourceBadge,
|
||||
} from './sessionGrouping.ts'
|
||||
|
||||
const sourceLabels = {
|
||||
web: 'Web',
|
||||
embedFallback: 'Embed',
|
||||
embedChannel: (id: string) => `Embed ${id}`,
|
||||
imPlatform: (p: string) => `IM ${p}`,
|
||||
}
|
||||
|
||||
test('resolveSessionOrigin distinguishes web, IM, and embed sessions', () => {
|
||||
assert.deepEqual(resolveSessionOrigin({ id: '1' }), { kind: 'web' })
|
||||
assert.deepEqual(resolveSessionOrigin({ id: '2', im_platform: 'feishu' }), {
|
||||
kind: 'im',
|
||||
platform: 'feishu',
|
||||
})
|
||||
assert.deepEqual(
|
||||
resolveSessionOrigin({
|
||||
id: '3',
|
||||
description: `${EMBED_SESSION_MARKER_PREFIX}ch-1`,
|
||||
}),
|
||||
{ kind: 'embed', channelId: 'ch-1' },
|
||||
)
|
||||
})
|
||||
|
||||
test('configuredPlatforms returns distinct platform keys in first-seen order', () => {
|
||||
const channels = [
|
||||
{ platform: 'feishu' },
|
||||
{ platform: 'wecom' },
|
||||
{ platform: 'feishu' },
|
||||
{ platform: '' },
|
||||
]
|
||||
assert.deepEqual(configuredPlatforms(channels), ['feishu', 'wecom'])
|
||||
})
|
||||
|
||||
test('groupSessionsBySource orders web, configured IM, then embed channels', () => {
|
||||
const sessions = [
|
||||
{ id: 'w', title: 'web chat' },
|
||||
{ id: 'f', title: 'feishu', im_platform: 'feishu' },
|
||||
{ id: 'e', title: 'embed', description: `${EMBED_SESSION_MARKER_PREFIX}ec-1` },
|
||||
]
|
||||
const groups = groupSessionsBySource(sessions, sourceLabels, { 'ec-1': 'Help widget' }, ['feishu'], 'Pinned')
|
||||
assert.deepEqual(
|
||||
groups.map((g) => g.key),
|
||||
['web', 'im:feishu', 'embed:ec-1'],
|
||||
)
|
||||
assert.equal(groups[2]?.label, 'Help widget')
|
||||
})
|
||||
|
||||
test('groupSessions date mode keeps pinned sessions in their own bucket', () => {
|
||||
const now = new Date().toISOString()
|
||||
const groups = groupSessions(
|
||||
'date',
|
||||
[
|
||||
{ id: 'p', is_pinned: true, updated_at: now },
|
||||
{ id: 't', updated_at: now },
|
||||
],
|
||||
{
|
||||
pinnedLabel: 'Pinned',
|
||||
bucketLabels: {
|
||||
pinned: 'Pinned',
|
||||
today: 'Today',
|
||||
yesterday: 'Yesterday',
|
||||
last7Days: '7d',
|
||||
last30Days: '30d',
|
||||
lastYear: 'Year',
|
||||
earlier: 'Earlier',
|
||||
},
|
||||
categorizeDate: () => 'today',
|
||||
sourceLabels,
|
||||
embedChannelNames: {},
|
||||
configuredImPlatforms: [],
|
||||
},
|
||||
)
|
||||
assert.deepEqual(
|
||||
groups.map((g) => g.key),
|
||||
['pinned', 'today'],
|
||||
)
|
||||
})
|
||||
|
||||
test('classifyDateBucket buckets recent sessions as today', () => {
|
||||
assert.equal(classifyDateBucket(new Date().toISOString()), 'today')
|
||||
})
|
||||
|
||||
test('shouldShowSessionSourceBadge is off when channels use separate folders', () => {
|
||||
assert.equal(shouldShowSessionSourceBadge('date'), false)
|
||||
assert.equal(shouldShowSessionSourceBadge('none'), false)
|
||||
})
|
||||
@@ -0,0 +1,277 @@
|
||||
// Pure logic for sidebar session grouping (replaces the old source filter).
|
||||
|
||||
export type SessionGroupMode = 'none' | 'date'
|
||||
|
||||
export const SESSION_GROUP_MODE_STORAGE_KEY = 'weknora:session-group-mode'
|
||||
export const DEFAULT_SESSION_GROUP_MODE: SessionGroupMode = 'none'
|
||||
|
||||
/** Mirrors backend types.EmbedSessionMarkerPrefix */
|
||||
export const EMBED_SESSION_MARKER_PREFIX = 'embed_channel:'
|
||||
|
||||
export interface SessionForGrouping {
|
||||
id: string
|
||||
title?: string
|
||||
is_pinned?: boolean
|
||||
created_at?: string
|
||||
updated_at?: string
|
||||
im_platform?: string
|
||||
description?: string
|
||||
originalIndex?: number
|
||||
}
|
||||
|
||||
export interface SessionGroup<T extends SessionForGrouping = SessionForGrouping> {
|
||||
key: string
|
||||
label: string
|
||||
items: T[]
|
||||
}
|
||||
|
||||
export type SessionOrigin =
|
||||
| { kind: 'web' }
|
||||
| { kind: 'im'; platform: string }
|
||||
| { kind: 'embed'; channelId: string }
|
||||
|
||||
export interface GroupModeOption {
|
||||
value: SessionGroupMode
|
||||
label: string
|
||||
}
|
||||
|
||||
export interface SourceGroupLabels {
|
||||
web: string
|
||||
embedFallback: string
|
||||
embedChannel: (channelId: string) => string
|
||||
imPlatform: (platform: string) => string
|
||||
}
|
||||
|
||||
// Distinct platform keys from the tenant's IM channel overview, in first-seen order.
|
||||
export function configuredPlatforms(channels: Array<{ platform: string }>): string[] {
|
||||
const seen = new Set<string>()
|
||||
const out: string[] = []
|
||||
for (const c of channels) {
|
||||
if (!c.platform || seen.has(c.platform)) continue
|
||||
seen.add(c.platform)
|
||||
out.push(c.platform)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
export function buildGroupModeOptions(labels: {
|
||||
none: string
|
||||
date: string
|
||||
}): GroupModeOption[] {
|
||||
return [
|
||||
{ value: 'none', label: labels.none },
|
||||
{ value: 'date', label: labels.date },
|
||||
]
|
||||
}
|
||||
|
||||
export function readStoredGroupMode(): SessionGroupMode {
|
||||
if (typeof localStorage === 'undefined') return DEFAULT_SESSION_GROUP_MODE
|
||||
const raw = localStorage.getItem(SESSION_GROUP_MODE_STORAGE_KEY)
|
||||
if (raw === 'none' || raw === 'date') return raw
|
||||
// Legacy "source" mode — channels are now separate folders like OpenAI projects.
|
||||
if (raw === 'source') return 'none'
|
||||
return DEFAULT_SESSION_GROUP_MODE
|
||||
}
|
||||
|
||||
export function storeGroupMode(mode: SessionGroupMode): void {
|
||||
if (typeof localStorage === 'undefined') return
|
||||
localStorage.setItem(SESSION_GROUP_MODE_STORAGE_KEY, mode)
|
||||
}
|
||||
|
||||
export function resolveSessionOrigin(session: SessionForGrouping): SessionOrigin {
|
||||
const platform = (session.im_platform || '').trim().toLowerCase()
|
||||
if (platform) return { kind: 'im', platform }
|
||||
const desc = (session.description || '').trim()
|
||||
if (desc.startsWith(EMBED_SESSION_MARKER_PREFIX)) {
|
||||
const channelId = desc.slice(EMBED_SESSION_MARKER_PREFIX.length).trim()
|
||||
if (channelId) return { kind: 'embed', channelId }
|
||||
}
|
||||
return { kind: 'web' }
|
||||
}
|
||||
|
||||
export function originGroupKey(origin: SessionOrigin): string {
|
||||
switch (origin.kind) {
|
||||
case 'web':
|
||||
return 'web'
|
||||
case 'im':
|
||||
return `im:${origin.platform}`
|
||||
case 'embed':
|
||||
return `embed:${origin.channelId}`
|
||||
}
|
||||
}
|
||||
|
||||
function labelForOrigin(
|
||||
origin: SessionOrigin,
|
||||
labels: SourceGroupLabels,
|
||||
embedChannelNames: Record<string, string>,
|
||||
): string {
|
||||
switch (origin.kind) {
|
||||
case 'web':
|
||||
return labels.web
|
||||
case 'im':
|
||||
return labels.imPlatform(origin.platform)
|
||||
case 'embed': {
|
||||
const name = embedChannelNames[origin.channelId]?.trim()
|
||||
return name || labels.embedChannel(origin.channelId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function classifyDateBucket(dateStr: string | undefined): DateBucketKey {
|
||||
if (!dateStr) return 'earlier'
|
||||
|
||||
const date = new Date(dateStr)
|
||||
const now = new Date()
|
||||
const today = new Date(now.getFullYear(), now.getMonth(), now.getDate())
|
||||
const yesterday = new Date(today.getTime() - 24 * 60 * 60 * 1000)
|
||||
const sevenDaysAgo = new Date(today.getTime() - 7 * 24 * 60 * 60 * 1000)
|
||||
const thirtyDaysAgo = new Date(today.getTime() - 30 * 24 * 60 * 60 * 1000)
|
||||
const oneYearAgo = new Date(today.getTime() - 365 * 24 * 60 * 60 * 1000)
|
||||
const sessionDate = new Date(date.getFullYear(), date.getMonth(), date.getDate())
|
||||
|
||||
if (sessionDate.getTime() >= today.getTime()) return 'today'
|
||||
if (sessionDate.getTime() >= yesterday.getTime()) return 'yesterday'
|
||||
if (date.getTime() >= sevenDaysAgo.getTime()) return 'last7Days'
|
||||
if (date.getTime() >= thirtyDaysAgo.getTime()) return 'last30Days'
|
||||
if (date.getTime() >= oneYearAgo.getTime()) return 'lastYear'
|
||||
return 'earlier'
|
||||
}
|
||||
|
||||
const DATE_BUCKET_ORDER = [
|
||||
'pinned',
|
||||
'today',
|
||||
'yesterday',
|
||||
'last7Days',
|
||||
'last30Days',
|
||||
'lastYear',
|
||||
'earlier',
|
||||
] as const
|
||||
|
||||
export type DateBucketKey = (typeof DATE_BUCKET_ORDER)[number]
|
||||
|
||||
export function groupSessionsByDate<T extends SessionForGrouping>(
|
||||
sessions: T[],
|
||||
bucketLabels: Record<DateBucketKey, string>,
|
||||
categorize: (session: T) => DateBucketKey,
|
||||
): SessionGroup<T>[] {
|
||||
const buckets = new Map<DateBucketKey, T[]>()
|
||||
for (const key of DATE_BUCKET_ORDER) buckets.set(key, [])
|
||||
|
||||
for (const session of sessions) {
|
||||
const bucket: DateBucketKey = session.is_pinned ? 'pinned' : categorize(session)
|
||||
buckets.get(bucket)!.push(session)
|
||||
}
|
||||
|
||||
return DATE_BUCKET_ORDER
|
||||
.filter((key) => (buckets.get(key)?.length ?? 0) > 0)
|
||||
.map((key) => ({
|
||||
key,
|
||||
label: bucketLabels[key],
|
||||
items: buckets.get(key)!,
|
||||
}))
|
||||
}
|
||||
|
||||
export function groupSessionsBySource<T extends SessionForGrouping>(
|
||||
sessions: T[],
|
||||
labels: SourceGroupLabels,
|
||||
embedChannelNames: Record<string, string>,
|
||||
configuredImPlatforms: string[],
|
||||
pinnedLabel: string,
|
||||
): SessionGroup<T>[] {
|
||||
const pinned: T[] = []
|
||||
const byKey = new Map<string, T[]>()
|
||||
|
||||
for (const session of sessions) {
|
||||
if (session.is_pinned) {
|
||||
pinned.push(session)
|
||||
continue
|
||||
}
|
||||
const origin = resolveSessionOrigin(session)
|
||||
const key = originGroupKey(origin)
|
||||
if (!byKey.has(key)) byKey.set(key, [])
|
||||
byKey.get(key)!.push(session)
|
||||
}
|
||||
|
||||
const orderedKeys: string[] = []
|
||||
if (byKey.has('web')) orderedKeys.push('web')
|
||||
|
||||
const seenIm = new Set<string>()
|
||||
for (const platform of configuredImPlatforms) {
|
||||
const key = `im:${platform}`
|
||||
if (byKey.has(key)) {
|
||||
orderedKeys.push(key)
|
||||
seenIm.add(platform)
|
||||
}
|
||||
}
|
||||
const extraIm = Array.from(byKey.keys())
|
||||
.filter((k) => k.startsWith('im:') && !seenIm.has(k.slice(3)))
|
||||
.sort((a, b) => a.localeCompare(b))
|
||||
orderedKeys.push(...extraIm)
|
||||
|
||||
const embedKeys = Array.from(byKey.keys())
|
||||
.filter((k) => k.startsWith('embed:'))
|
||||
.sort((a, b) => {
|
||||
const idA = a.slice(7)
|
||||
const idB = b.slice(7)
|
||||
const nameA = embedChannelNames[idA] || idA
|
||||
const nameB = embedChannelNames[idB] || idB
|
||||
return nameA.localeCompare(nameB)
|
||||
})
|
||||
orderedKeys.push(...embedKeys)
|
||||
|
||||
const groups: SessionGroup<T>[] = []
|
||||
if (pinned.length > 0) {
|
||||
groups.push({ key: 'pinned', label: pinnedLabel, items: pinned })
|
||||
}
|
||||
for (const key of orderedKeys) {
|
||||
const items = byKey.get(key)
|
||||
if (!items?.length) continue
|
||||
const sample = items[0]
|
||||
const origin = resolveSessionOrigin(sample)
|
||||
groups.push({
|
||||
key,
|
||||
label: labelForOrigin(origin, labels, embedChannelNames),
|
||||
items,
|
||||
})
|
||||
}
|
||||
return groups
|
||||
}
|
||||
|
||||
export function groupSessionsFlat<T extends SessionForGrouping>(
|
||||
sessions: T[],
|
||||
pinnedLabel: string,
|
||||
): SessionGroup<T>[] {
|
||||
const pinned = sessions.filter((s) => s.is_pinned)
|
||||
const rest = sessions.filter((s) => !s.is_pinned)
|
||||
const groups: SessionGroup<T>[] = []
|
||||
if (pinned.length > 0) groups.push({ key: 'pinned', label: pinnedLabel, items: pinned })
|
||||
if (rest.length > 0) groups.push({ key: 'all', label: '', items: rest })
|
||||
return groups
|
||||
}
|
||||
|
||||
export function groupSessions<T extends SessionForGrouping>(
|
||||
mode: SessionGroupMode,
|
||||
sessions: T[],
|
||||
opts: {
|
||||
pinnedLabel: string
|
||||
bucketLabels: Record<DateBucketKey, string>
|
||||
categorizeDate: (session: T) => DateBucketKey
|
||||
sourceLabels: SourceGroupLabels
|
||||
embedChannelNames: Record<string, string>
|
||||
configuredImPlatforms: string[]
|
||||
},
|
||||
): SessionGroup<T>[] {
|
||||
if (!sessions.length) return []
|
||||
switch (mode) {
|
||||
case 'none':
|
||||
return groupSessionsFlat(sessions, opts.pinnedLabel)
|
||||
case 'date':
|
||||
default:
|
||||
return groupSessionsByDate(sessions, opts.bucketLabels, opts.categorizeDate)
|
||||
}
|
||||
}
|
||||
|
||||
/** Chat list rows show source badges when channels live in separate folders. */
|
||||
export function shouldShowSessionSourceBadge(_mode: SessionGroupMode): boolean {
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
|
||||
import {
|
||||
applyBucketCountProbe,
|
||||
bucketVisible,
|
||||
createEmptyBucket,
|
||||
prependSessionToWebBucket,
|
||||
type BucketDefinition,
|
||||
} from './sessionSidebarBuckets.ts'
|
||||
|
||||
const imDef: BucketDefinition = {
|
||||
key: 'im:feishu',
|
||||
apiSource: 'feishu',
|
||||
label: 'Feishu',
|
||||
kind: 'im',
|
||||
platform: 'feishu',
|
||||
}
|
||||
|
||||
test('bucketVisible hides channel buckets until count is known', () => {
|
||||
const bucket = createEmptyBucket(imDef)
|
||||
assert.equal(bucketVisible(bucket), false)
|
||||
})
|
||||
|
||||
test('bucketVisible hides channel buckets with zero sessions after probe', () => {
|
||||
const bucket = applyBucketCountProbe(createEmptyBucket(imDef), 0)
|
||||
assert.equal(bucketVisible(bucket), false)
|
||||
})
|
||||
|
||||
test('bucketVisible shows channel buckets with sessions after probe', () => {
|
||||
const bucket = applyBucketCountProbe(createEmptyBucket(imDef), 3)
|
||||
assert.equal(bucketVisible(bucket), true)
|
||||
})
|
||||
|
||||
test('prependSessionToWebBucket inserts new session at front and bumps total', () => {
|
||||
const webDef: BucketDefinition = {
|
||||
key: 'web',
|
||||
apiSource: 'web',
|
||||
label: 'Chats',
|
||||
kind: 'web',
|
||||
}
|
||||
const bucket = { ...createEmptyBucket(webDef), total: 2, items: [{ id: 'a' }] }
|
||||
const next = prependSessionToWebBucket(bucket, { id: 'b', title: 'New' })
|
||||
assert.deepEqual(next.items.map((s) => s.id), ['b', 'a'])
|
||||
assert.equal(next.total, 3)
|
||||
})
|
||||
|
||||
test('prependSessionToWebBucket is idempotent for existing session', () => {
|
||||
const webDef: BucketDefinition = {
|
||||
key: 'web',
|
||||
apiSource: 'web',
|
||||
label: 'Chats',
|
||||
kind: 'web',
|
||||
}
|
||||
const bucket = { ...createEmptyBucket(webDef), total: 1, items: [{ id: 'a' }] }
|
||||
const next = prependSessionToWebBucket(bucket, { id: 'a', title: 'Same' })
|
||||
assert.equal(next, bucket)
|
||||
})
|
||||
@@ -0,0 +1,172 @@
|
||||
// OpenAI-style sidebar: channel folders (IM / embed) + flat/date-grouped chats,
|
||||
// each bucket paginates independently via the API `source` param.
|
||||
|
||||
import type { SessionForGrouping } from './sessionGrouping'
|
||||
|
||||
export const SIDEBAR_BUCKET_PAGE_SIZE = 30
|
||||
|
||||
export type SidebarBucketKind = 'web' | 'im' | 'embed'
|
||||
|
||||
export interface SidebarSessionBucket {
|
||||
key: string
|
||||
apiSource: string
|
||||
label: string
|
||||
kind: SidebarBucketKind
|
||||
platform?: string
|
||||
page: number
|
||||
total: number
|
||||
items: SessionForGrouping[]
|
||||
loaded: boolean
|
||||
loading: boolean
|
||||
/** 已通过轻量 count 探测(page_size=1),用于隐藏无会话的渠道文件夹 */
|
||||
countKnown: boolean
|
||||
}
|
||||
|
||||
export interface BucketDefinition {
|
||||
key: string
|
||||
apiSource: string
|
||||
label: string
|
||||
kind: SidebarBucketKind
|
||||
platform?: string
|
||||
}
|
||||
|
||||
export function createEmptyBucket(def: BucketDefinition): SidebarSessionBucket {
|
||||
return {
|
||||
...def,
|
||||
page: 0,
|
||||
total: 0,
|
||||
items: [],
|
||||
loaded: false,
|
||||
loading: false,
|
||||
countKnown: false,
|
||||
}
|
||||
}
|
||||
|
||||
export function bucketHasMore(bucket: SidebarSessionBucket): boolean {
|
||||
return bucket.items.length < bucket.total
|
||||
}
|
||||
|
||||
export function bucketVisible(bucket: SidebarSessionBucket): boolean {
|
||||
if (!isChannelBucket(bucket)) return true
|
||||
if (!bucket.countKnown) return false
|
||||
return bucket.total > 0
|
||||
}
|
||||
|
||||
export function applyBucketCountProbe(
|
||||
bucket: SidebarSessionBucket,
|
||||
total: number,
|
||||
): SidebarSessionBucket {
|
||||
return { ...bucket, total, countKnown: true }
|
||||
}
|
||||
|
||||
export function isChannelBucket(bucket: SidebarSessionBucket): boolean {
|
||||
return bucket.kind === 'im' || bucket.kind === 'embed'
|
||||
}
|
||||
|
||||
export function isChannelBucketKey(key: string): boolean {
|
||||
return key.startsWith('im:') || key.startsWith('embed:')
|
||||
}
|
||||
|
||||
export function buildBucketDefinitions(
|
||||
imPlatforms: string[],
|
||||
embedChannels: Record<string, string>,
|
||||
labels: {
|
||||
web: string
|
||||
imPlatform: (platform: string) => string
|
||||
embedChannel: (name: string) => string
|
||||
},
|
||||
): BucketDefinition[] {
|
||||
const imDefs = imPlatforms.map((platform) => ({
|
||||
key: `im:${platform}`,
|
||||
apiSource: platform,
|
||||
label: labels.imPlatform(platform),
|
||||
kind: 'im' as const,
|
||||
platform,
|
||||
}))
|
||||
const embedDefs = Object.entries(embedChannels).map(([id, name]) => ({
|
||||
key: `embed:${id}`,
|
||||
apiSource: `embed:${id}`,
|
||||
label: labels.embedChannel(name || id.slice(0, 8)),
|
||||
kind: 'embed' as const,
|
||||
}))
|
||||
return [
|
||||
...imDefs,
|
||||
...embedDefs,
|
||||
{
|
||||
key: 'web',
|
||||
apiSource: 'web',
|
||||
label: labels.web,
|
||||
kind: 'web',
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
export function mergeBucketPage(
|
||||
bucket: SidebarSessionBucket,
|
||||
rows: SessionForGrouping[],
|
||||
total: number,
|
||||
page: number,
|
||||
): SidebarSessionBucket {
|
||||
const seen = new Set(bucket.items.map((s) => s.id))
|
||||
const merged = [...bucket.items]
|
||||
for (const row of rows) {
|
||||
if (seen.has(row.id)) continue
|
||||
seen.add(row.id)
|
||||
merged.push(row)
|
||||
}
|
||||
return {
|
||||
...bucket,
|
||||
page,
|
||||
total,
|
||||
items: merged,
|
||||
loaded: true,
|
||||
loading: false,
|
||||
}
|
||||
}
|
||||
|
||||
export function flattenBucketItems(
|
||||
buckets: Record<string, SidebarSessionBucket>,
|
||||
order: string[],
|
||||
): SessionForGrouping[] {
|
||||
const out: SessionForGrouping[] = []
|
||||
const seen = new Set<string>()
|
||||
for (const key of order) {
|
||||
const bucket = buckets[key]
|
||||
if (!bucket) continue
|
||||
for (const item of bucket.items) {
|
||||
if (seen.has(item.id)) continue
|
||||
seen.add(item.id)
|
||||
out.push(item)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
export function prependSessionToWebBucket(
|
||||
bucket: SidebarSessionBucket,
|
||||
session: SessionForGrouping,
|
||||
): SidebarSessionBucket {
|
||||
if (bucket.items.some((row) => row.id === session.id)) return bucket
|
||||
return {
|
||||
...bucket,
|
||||
items: [session, ...bucket.items],
|
||||
total: bucket.total + 1,
|
||||
loaded: true,
|
||||
}
|
||||
}
|
||||
|
||||
export function removeSessionFromBuckets(
|
||||
buckets: Record<string, SidebarSessionBucket>,
|
||||
sessionId: string,
|
||||
): Record<string, SidebarSessionBucket> {
|
||||
const next: Record<string, SidebarSessionBucket> = {}
|
||||
for (const [key, bucket] of Object.entries(buckets)) {
|
||||
const items = bucket.items.filter((s) => s.id !== sessionId)
|
||||
next[key] = {
|
||||
...bucket,
|
||||
items,
|
||||
total: Math.max(0, bucket.total - (items.length < bucket.items.length ? 1 : 0)),
|
||||
}
|
||||
}
|
||||
return next
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
|
||||
import {
|
||||
DEFAULT_SESSION_SOURCE,
|
||||
buildSourceOptions,
|
||||
configuredPlatforms,
|
||||
shouldShowSourceFilter,
|
||||
} from './sessionSourceFilter.ts'
|
||||
|
||||
test('defaults the sidebar to web sessions so IM stays hidden until opted into', () => {
|
||||
assert.equal(DEFAULT_SESSION_SOURCE, 'web')
|
||||
})
|
||||
|
||||
test('source filter only appears once at least one IM channel is configured', () => {
|
||||
assert.equal(shouldShowSourceFilter([]), false)
|
||||
assert.equal(shouldShowSourceFilter(['feishu']), true)
|
||||
assert.equal(shouldShowSourceFilter(['feishu', 'wecom']), true)
|
||||
})
|
||||
|
||||
test('configuredPlatforms returns distinct platform keys in first-seen order', () => {
|
||||
const channels = [
|
||||
{ platform: 'feishu' },
|
||||
{ platform: 'wecom' },
|
||||
{ platform: 'feishu' }, // duplicate: a second feishu channel
|
||||
{ platform: '' }, // malformed row: ignored
|
||||
]
|
||||
assert.deepEqual(configuredPlatforms(channels), ['feishu', 'wecom'])
|
||||
})
|
||||
|
||||
test('configuredPlatforms tolerates an empty overview', () => {
|
||||
assert.deepEqual(configuredPlatforms([]), [])
|
||||
})
|
||||
|
||||
test('buildSourceOptions puts web first, then platforms with injected labels', () => {
|
||||
const opts = buildSourceOptions(['feishu', 'wecom'], 'My chats', (p) => `LABEL:${p}`)
|
||||
assert.deepEqual(opts, [
|
||||
{ value: 'web', label: 'My chats', platform: null },
|
||||
{ value: 'feishu', label: 'LABEL:feishu', platform: 'feishu' },
|
||||
{ value: 'wecom', label: 'LABEL:wecom', platform: 'wecom' },
|
||||
])
|
||||
})
|
||||
|
||||
test('buildSourceOptions with no IM platforms yields just the web option', () => {
|
||||
assert.deepEqual(buildSourceOptions([], 'My chats', (p) => p), [
|
||||
{ value: 'web', label: 'My chats', platform: null },
|
||||
])
|
||||
})
|
||||
@@ -1,57 +0,0 @@
|
||||
// Pure logic for the sidebar's session source filter (issue #1560).
|
||||
//
|
||||
// IM-channel sessions are auto-generated operational traffic, not the user's own
|
||||
// chats, and IM is an optional module. So the sidebar defaults to web sessions
|
||||
// and only surfaces a quiet source filter for tenants that actually configured a
|
||||
// channel — keeping IM low-key rather than promoting it to a first-class group.
|
||||
|
||||
// 'web' = the user's own chats (no IM mapping); any other value is a platform
|
||||
// key (e.g. 'feishu') selecting that channel's sessions. Sent verbatim as the
|
||||
// `source` query param. The `(string & {})` arm keeps 'web' as an autocomplete
|
||||
// hint instead of letting the union collapse to a bare `string`.
|
||||
export type SessionSource = 'web' | (string & {})
|
||||
|
||||
export const DEFAULT_SESSION_SOURCE: SessionSource = 'web'
|
||||
|
||||
// The source filter control is only rendered once the tenant has at least one
|
||||
// configured IM channel — tenants that never enabled IM see an unchanged,
|
||||
// channel-free sidebar.
|
||||
export function shouldShowSourceFilter(configured: string[]): boolean {
|
||||
return configured.length > 0
|
||||
}
|
||||
|
||||
// Distinct platform keys from the tenant's IM channel overview, in first-seen
|
||||
// order. Duplicates (multiple channels on one platform) collapse to one option,
|
||||
// and malformed rows with no platform are dropped.
|
||||
export function configuredPlatforms(channels: Array<{ platform: string }>): string[] {
|
||||
const seen = new Set<string>()
|
||||
const out: string[] = []
|
||||
for (const c of channels) {
|
||||
if (!c.platform || seen.has(c.platform)) continue
|
||||
seen.add(c.platform)
|
||||
out.push(c.platform)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
export interface SourceOption {
|
||||
value: SessionSource
|
||||
label: string
|
||||
// null for the user's own web chats; the platform key for an IM source.
|
||||
platform: string | null
|
||||
}
|
||||
|
||||
// Build the source switcher's options: the user's own web chats first, then each
|
||||
// configured IM platform in first-seen order. Labels are injected so this stays
|
||||
// pure and unit-testable (no i18n dependency); logos are resolved by the caller
|
||||
// (where the platform→asset map lives), keeping the dropdown component dumb.
|
||||
export function buildSourceOptions(
|
||||
platforms: string[],
|
||||
webLabel: string,
|
||||
labelForPlatform: (platform: string) => string,
|
||||
): SourceOption[] {
|
||||
return [
|
||||
{ value: DEFAULT_SESSION_SOURCE, label: webLabel, platform: null },
|
||||
...platforms.map((p) => ({ value: p, label: labelForPlatform(p), platform: p })),
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user