@njuptlzf/agent-chat-web 0.2.0 → 0.3.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/dist/AgentChat.d.ts +7 -1
- package/dist/AgentChat.js +23 -5
- package/dist/AgentChatApp.d.ts +4 -1
- package/dist/AgentChatApp.js +2 -2
- package/dist/PersonasPage.js +8 -0
- package/dist/assets/_commonjs-dynamic-modules-TDtrdbi3.js +1 -0
- package/dist/assets/exceljs.min-jlEt4kSc.js +29 -0
- package/dist/assets/index-B7uSE8SM.css +1 -0
- package/dist/assets/index-D57mU5H5.js +50 -0
- package/dist/assets/index-Dviy7gnB.js +44 -0
- package/dist/assets/pdf-CfhDiWmk.js +55 -0
- package/dist/assets/pdf.worker.min-CdXBBzB4.js +1 -0
- package/dist/assets/pdf.worker.min-CjEcRF4W.mjs +28 -0
- package/dist/index.html +13 -0
- package/dist/mount.d.ts +7 -1
- package/dist/mount.js +2 -2
- package/dist/styles.css +1492 -1478
- package/dist/suggestions.d.ts +41 -0
- package/dist/suggestions.js +98 -0
- package/dist/ui/FollowUpSuggestions.d.ts +9 -0
- package/dist/ui/FollowUpSuggestions.js +13 -0
- package/dist/ui/Icon.d.ts +8 -0
- package/dist/ui/Icon.js +26 -0
- package/dist/ui/icons.d.ts +19 -0
- package/dist/ui/icons.js +25 -0
- package/dist/ui/slots.d.ts +23 -0
- package/dist/ui/slots.js +13 -0
- package/package.json +71 -72
- package/src/App.tsx +13 -13
- package/src/agent-chat/AgentChat.tsx +729 -692
- package/src/agent-chat/AgentChatApp.tsx +211 -206
- package/src/agent-chat/PersonasPage.tsx +705 -678
- package/src/agent-chat/adapter.ts +272 -272
- package/src/agent-chat/app-def-check.ts +215 -215
- package/src/agent-chat/app.tsx +327 -327
- package/src/agent-chat/auth.ts +83 -83
- package/src/agent-chat/dock.ts +161 -161
- package/src/agent-chat/docs.ts +179 -179
- package/src/agent-chat/index.ts +17 -17
- package/src/agent-chat/mount.tsx +8 -1
- package/src/agent-chat/qual.ts +55 -55
- package/src/agent-chat/sse.ts +104 -104
- package/src/agent-chat/suggestions.ts +127 -0
- package/src/agent-chat/theme.ts +23 -23
- package/src/agent-chat/ui/CodeCopyHeader.tsx +29 -29
- package/src/agent-chat/ui/ConfirmCard.tsx +32 -32
- package/src/agent-chat/ui/FollowUpSuggestions.tsx +28 -0
- package/src/agent-chat/ui/Icon.tsx +53 -0
- package/src/agent-chat/ui/Lightbox.tsx +32 -32
- package/src/agent-chat/ui/QueueBar.tsx +70 -70
- package/src/agent-chat/ui/Splitter.tsx +81 -81
- package/src/agent-chat/ui/ThemeToggle.tsx +25 -25
- package/src/agent-chat/ui/TierPicker.tsx +112 -112
- package/src/agent-chat/ui/UIPromptCard.tsx +68 -68
- package/src/agent-chat/ui/icons.ts +31 -0
- package/src/agent-chat/ui/slots.tsx +35 -0
- package/src/agent-chat/ui/timefmt.ts +110 -110
- package/src/agent-chat/ui/useNarrow.ts +20 -20
- package/src/fake-product/entry.tsx +63 -54
- package/src/main.tsx +8 -8
- package/src/styles.css +1492 -1478
|
@@ -1,68 +1,68 @@
|
|
|
1
|
-
import { useState } from "react";
|
|
2
|
-
import type { UiPromptEvent } from "../adapter.ts";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Card for an extension dialog (`ctx.ui.select/confirm/input`, custom component
|
|
6
|
-
* UI-R3 — assistant-ui has no equivalent primitive for extension UI prompts).
|
|
7
|
-
* The server parks the run inside the extension's await until this answers
|
|
8
|
-
* POST /api/ui-response, so every kind offers an explicit way out: `cancelled`
|
|
9
|
-
* maps to the fail-closed default (false / undefined) if the user dismisses
|
|
10
|
-
* instead of choosing.
|
|
11
|
-
*/
|
|
12
|
-
export function UIPromptCard({
|
|
13
|
-
prompt,
|
|
14
|
-
onAnswer,
|
|
15
|
-
}: {
|
|
16
|
-
prompt: UiPromptEvent;
|
|
17
|
-
onAnswer: (a: { value?: string; confirmed?: boolean; cancelled?: true }) => void;
|
|
18
|
-
}) {
|
|
19
|
-
const [text, setText] = useState("");
|
|
20
|
-
const submitInput = () => {
|
|
21
|
-
if (text.trim()) onAnswer({ value: text });
|
|
22
|
-
};
|
|
23
|
-
return (
|
|
24
|
-
<div className="uidialog" role={prompt.kind === "confirm" ? "alertdialog" : "dialog"} aria-label={prompt.title}>
|
|
25
|
-
<div className="uidialog-title">{prompt.title}</div>
|
|
26
|
-
{prompt.message && <div className="uidialog-msg">{prompt.message}</div>}
|
|
27
|
-
{prompt.kind === "select" && (
|
|
28
|
-
<div className="uidialog-options">
|
|
29
|
-
{(prompt.options ?? []).map((o) => (
|
|
30
|
-
<button key={o} onClick={() => onAnswer({ value: o })}>
|
|
31
|
-
{o}
|
|
32
|
-
</button>
|
|
33
|
-
))}
|
|
34
|
-
<button className="uidialog-dismiss" onClick={() => onAnswer({ cancelled: true })}>
|
|
35
|
-
取消
|
|
36
|
-
</button>
|
|
37
|
-
</div>
|
|
38
|
-
)}
|
|
39
|
-
{prompt.kind === "confirm" && (
|
|
40
|
-
<div className="uidialog-actions">
|
|
41
|
-
<button className="allow" onClick={() => onAnswer({ confirmed: true })}>
|
|
42
|
-
确定
|
|
43
|
-
</button>
|
|
44
|
-
<button className="deny" onClick={() => onAnswer({ cancelled: true })}>
|
|
45
|
-
取消
|
|
46
|
-
</button>
|
|
47
|
-
</div>
|
|
48
|
-
)}
|
|
49
|
-
{prompt.kind === "input" && (
|
|
50
|
-
<div className="uidialog-input">
|
|
51
|
-
<input
|
|
52
|
-
value={text}
|
|
53
|
-
placeholder={prompt.placeholder ?? ""}
|
|
54
|
-
autoFocus
|
|
55
|
-
onChange={(e) => setText(e.target.value)}
|
|
56
|
-
onKeyDown={(e) => e.key === "Enter" && submitInput()}
|
|
57
|
-
/>
|
|
58
|
-
<button onClick={submitInput} disabled={!text.trim()}>
|
|
59
|
-
提交
|
|
60
|
-
</button>
|
|
61
|
-
<button className="uidialog-dismiss" onClick={() => onAnswer({ cancelled: true })}>
|
|
62
|
-
取消
|
|
63
|
-
</button>
|
|
64
|
-
</div>
|
|
65
|
-
)}
|
|
66
|
-
</div>
|
|
67
|
-
);
|
|
68
|
-
}
|
|
1
|
+
import { useState } from "react";
|
|
2
|
+
import type { UiPromptEvent } from "../adapter.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Card for an extension dialog (`ctx.ui.select/confirm/input`, custom component
|
|
6
|
+
* UI-R3 — assistant-ui has no equivalent primitive for extension UI prompts).
|
|
7
|
+
* The server parks the run inside the extension's await until this answers
|
|
8
|
+
* POST /api/ui-response, so every kind offers an explicit way out: `cancelled`
|
|
9
|
+
* maps to the fail-closed default (false / undefined) if the user dismisses
|
|
10
|
+
* instead of choosing.
|
|
11
|
+
*/
|
|
12
|
+
export function UIPromptCard({
|
|
13
|
+
prompt,
|
|
14
|
+
onAnswer,
|
|
15
|
+
}: {
|
|
16
|
+
prompt: UiPromptEvent;
|
|
17
|
+
onAnswer: (a: { value?: string; confirmed?: boolean; cancelled?: true }) => void;
|
|
18
|
+
}) {
|
|
19
|
+
const [text, setText] = useState("");
|
|
20
|
+
const submitInput = () => {
|
|
21
|
+
if (text.trim()) onAnswer({ value: text });
|
|
22
|
+
};
|
|
23
|
+
return (
|
|
24
|
+
<div className="uidialog" role={prompt.kind === "confirm" ? "alertdialog" : "dialog"} aria-label={prompt.title}>
|
|
25
|
+
<div className="uidialog-title">{prompt.title}</div>
|
|
26
|
+
{prompt.message && <div className="uidialog-msg">{prompt.message}</div>}
|
|
27
|
+
{prompt.kind === "select" && (
|
|
28
|
+
<div className="uidialog-options">
|
|
29
|
+
{(prompt.options ?? []).map((o) => (
|
|
30
|
+
<button key={o} onClick={() => onAnswer({ value: o })}>
|
|
31
|
+
{o}
|
|
32
|
+
</button>
|
|
33
|
+
))}
|
|
34
|
+
<button className="uidialog-dismiss" onClick={() => onAnswer({ cancelled: true })}>
|
|
35
|
+
取消
|
|
36
|
+
</button>
|
|
37
|
+
</div>
|
|
38
|
+
)}
|
|
39
|
+
{prompt.kind === "confirm" && (
|
|
40
|
+
<div className="uidialog-actions">
|
|
41
|
+
<button className="allow" onClick={() => onAnswer({ confirmed: true })}>
|
|
42
|
+
确定
|
|
43
|
+
</button>
|
|
44
|
+
<button className="deny" onClick={() => onAnswer({ cancelled: true })}>
|
|
45
|
+
取消
|
|
46
|
+
</button>
|
|
47
|
+
</div>
|
|
48
|
+
)}
|
|
49
|
+
{prompt.kind === "input" && (
|
|
50
|
+
<div className="uidialog-input">
|
|
51
|
+
<input
|
|
52
|
+
value={text}
|
|
53
|
+
placeholder={prompt.placeholder ?? ""}
|
|
54
|
+
autoFocus
|
|
55
|
+
onChange={(e) => setText(e.target.value)}
|
|
56
|
+
onKeyDown={(e) => e.key === "Enter" && submitInput()}
|
|
57
|
+
/>
|
|
58
|
+
<button onClick={submitInput} disabled={!text.trim()}>
|
|
59
|
+
提交
|
|
60
|
+
</button>
|
|
61
|
+
<button className="uidialog-dismiss" onClick={() => onAnswer({ cancelled: true })}>
|
|
62
|
+
取消
|
|
63
|
+
</button>
|
|
64
|
+
</div>
|
|
65
|
+
)}
|
|
66
|
+
</div>
|
|
67
|
+
);
|
|
68
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 图标值解析:`branding.ui.icons.<槽名>` 的一个字符串怎么画。
|
|
3
|
+
* 三种形态:`svg:<内置名>` / `url:https?://…`(图片地址)/ 其它非空字符串(emoji 或短文本)。
|
|
4
|
+
* 任何非法值都回落默认矢量——图标坏了不该让界面报错,也不该引入任何 HTML 注入面。
|
|
5
|
+
*/
|
|
6
|
+
const BUILTIN_SVGS = ["paperclip", "plus", "image", "upload"] as const;
|
|
7
|
+
export type BuiltinSvgName = (typeof BUILTIN_SVGS)[number];
|
|
8
|
+
|
|
9
|
+
export type IconDescriptor =
|
|
10
|
+
| { kind: "svg"; name: BuiltinSvgName }
|
|
11
|
+
| { kind: "url"; href: string }
|
|
12
|
+
| { kind: "text"; text: string };
|
|
13
|
+
|
|
14
|
+
const DEFAULT_ICON: IconDescriptor = { kind: "svg", name: "paperclip" };
|
|
15
|
+
const MAX_TEXT = 8;
|
|
16
|
+
|
|
17
|
+
export function parseIconSpec(spec: string | undefined | null): IconDescriptor {
|
|
18
|
+
if (spec === undefined || spec === null) return DEFAULT_ICON;
|
|
19
|
+
const value = spec.trim();
|
|
20
|
+
if (!value) return DEFAULT_ICON;
|
|
21
|
+
if (value.startsWith("svg:")) {
|
|
22
|
+
const name = value.slice(4).trim() as BuiltinSvgName;
|
|
23
|
+
return (BUILTIN_SVGS as readonly string[]).includes(name) ? { kind: "svg", name } : DEFAULT_ICON;
|
|
24
|
+
}
|
|
25
|
+
if (value.startsWith("url:")) {
|
|
26
|
+
const href = value.slice(4).trim();
|
|
27
|
+
// 只放行 http(s);相对路径与其它协议一律回落默认(纵深防御,前端兜一道)。
|
|
28
|
+
return /^https?:\/\/\S+$/i.test(href) ? { kind: "url", href } : DEFAULT_ICON;
|
|
29
|
+
}
|
|
30
|
+
return { kind: "text", text: value.length > MAX_TEXT ? value.slice(0, MAX_TEXT) : value };
|
|
31
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { createContext, useContext, type ComponentType, type ReactNode } from "react";
|
|
2
|
+
import { Icon } from "./Icon.tsx";
|
|
3
|
+
import { FollowUpSuggestions } from "./FollowUpSuggestions.tsx";
|
|
4
|
+
|
|
5
|
+
export interface IconProps {
|
|
6
|
+
/** 图标值(`branding.ui.icons` 里该槽的字符串);缺省回落内置矢量。 */
|
|
7
|
+
spec?: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 聊天壳的视觉件槽位注册表:所有视觉件要么是 assistant-ui 原生原语,要么是
|
|
12
|
+
* 这里登记的可替换自定义组件——壳体 JSX 不写死字形、不自造建议状态机。
|
|
13
|
+
*
|
|
14
|
+
* `Slots` / `IconProps` **故意不进公共 barrel**(不新增公开导出符号):产品要
|
|
15
|
+
* 整体换皮时,在 `AgentApp` 的 `slots` 里传结构兼容的组件即可,TS 自动对上。
|
|
16
|
+
* 槽组件渲染在 `AssistantRuntimeProvider`(`FollowUpSuggestions` 还在
|
|
17
|
+
* `ThreadPrimitive.Root`)之内,可直接使用库原语;不得反向 import 平台内部路径。
|
|
18
|
+
*/
|
|
19
|
+
export interface Slots {
|
|
20
|
+
Icon: ComponentType<IconProps>;
|
|
21
|
+
FollowUpSuggestions: ComponentType;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const defaultSlots: Slots = { Icon, FollowUpSuggestions };
|
|
25
|
+
|
|
26
|
+
const SlotsContext = createContext<Slots>(defaultSlots);
|
|
27
|
+
export const SlotsProvider = SlotsContext.Provider;
|
|
28
|
+
export function useSlots(): Slots {
|
|
29
|
+
return useContext(SlotsContext);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** 纯函数:默认槽位 + 局部覆盖(未覆盖的槽保持默认),不渲染 DOM 即可单测。 */
|
|
33
|
+
export function resolveSlots(overrides?: Partial<Slots>): Slots {
|
|
34
|
+
return { ...defaultSlots, ...overrides };
|
|
35
|
+
}
|
|
@@ -1,110 +1,110 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Session-list time helpers (batch 12, DSH-inspired): human relative labels
|
|
3
|
-
* and calendar-day grouping. Pure and `now`-injected so they are testable
|
|
4
|
-
* without a clock mock.
|
|
5
|
-
*
|
|
6
|
-
* 另外提供**绝对时刻**显示(`fmtTime` / `fmtDate`):
|
|
7
|
-
* 服务端一律存 UTC ISO,页面以前直接 `iso.slice(0,16)` 截出来显示——那是 UTC,
|
|
8
|
-
* +08:00 的运维看审计流水/学习记录会以为「8 小时前」。现在统一转成浏览器本地时区。
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
const DAY = 86_400_000;
|
|
12
|
-
|
|
13
|
-
function startOfDay(t: number): number {
|
|
14
|
-
const d = new Date(t);
|
|
15
|
-
d.setHours(0, 0, 0, 0);
|
|
16
|
-
return d.getTime();
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/** Whole calendar days between the item's day and today (0 = today, 1 = yesterday). */
|
|
20
|
-
function daysAgo(iso: string, now: Date): number {
|
|
21
|
-
const t = Date.parse(iso);
|
|
22
|
-
if (Number.isNaN(t)) return 0;
|
|
23
|
-
return Math.round((startOfDay(now.getTime()) - startOfDay(t)) / DAY);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/** 刚刚 / N 分钟前 / N 小时前 / 昨天 / M月D日(跨年带年份)。 */
|
|
27
|
-
export function relativeTime(iso: string, now: Date): string {
|
|
28
|
-
const t = Date.parse(iso);
|
|
29
|
-
if (Number.isNaN(t)) return "";
|
|
30
|
-
const diff = now.getTime() - t; // negative (clock skew / future) reads as "刚刚"
|
|
31
|
-
if (diff < 60_000) return "刚刚";
|
|
32
|
-
if (diff < 3_600_000) return `${Math.floor(diff / 60_000)} 分钟前`;
|
|
33
|
-
const days = daysAgo(iso, now);
|
|
34
|
-
if (days === 0) return `${Math.floor(diff / 3_600_000)} 小时前`;
|
|
35
|
-
const d = new Date(t);
|
|
36
|
-
if (days === 1) return "昨天";
|
|
37
|
-
const md = `${d.getMonth() + 1}月${d.getDate()}日`;
|
|
38
|
-
return d.getFullYear() === now.getFullYear() ? md : `${d.getFullYear()}年${md}`;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/** Group label for a session row; buckets follow calendar days, not 24h windows. */
|
|
42
|
-
export function groupOf(iso: string, now: Date): string {
|
|
43
|
-
const days = daysAgo(iso, now);
|
|
44
|
-
if (days <= 0) return "今天";
|
|
45
|
-
if (days === 1) return "昨天";
|
|
46
|
-
if (days <= 6) return "近 7 天";
|
|
47
|
-
if (days <= 29) return "近 30 天";
|
|
48
|
-
return "更早";
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Bucket a session list in order. Assumes items are sorted newest-first (the
|
|
53
|
-
* server does that); a newer item after an older one starts a new bucket run
|
|
54
|
-
* rather than being silently merged — same behaviour as DSH's list.
|
|
55
|
-
*/
|
|
56
|
-
export function groupSessions<T extends { updatedAt: string }>(
|
|
57
|
-
items: readonly T[],
|
|
58
|
-
now: Date,
|
|
59
|
-
): Array<{ label: string; items: T[] }> {
|
|
60
|
-
const out: Array<{ label: string; items: T[] }> = [];
|
|
61
|
-
for (const item of items) {
|
|
62
|
-
const label = groupOf(item.updatedAt, now);
|
|
63
|
-
const last = out[out.length - 1];
|
|
64
|
-
if (last && last.label === label) last.items.push(item);
|
|
65
|
-
else out.push({ label, items: [item] });
|
|
66
|
-
}
|
|
67
|
-
return out;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* 绝对时刻 → 「2026-11-03 09:12」(本地时区)。
|
|
72
|
-
*
|
|
73
|
-
* - `timeZone` 只给测试用(固定时区做断言);页面调用不传 = 用户本地时区。
|
|
74
|
-
* - 空值显示「—」;**解析不了就原样返回**(不吞、不假装是今天)。
|
|
75
|
-
* - 只喂 ISO 时刻(含 Z 或偏移);别喂「2026-11-03」这种纯日期——那会被当 UTC 零点,
|
|
76
|
-
* 在负时区会退到前一天。
|
|
77
|
-
*/
|
|
78
|
-
export function fmtTime(iso: string | null | undefined, timeZone?: string): string {
|
|
79
|
-
const parts = fmtParts(iso, timeZone, true);
|
|
80
|
-
if (!parts) return iso ? String(iso) : "—";
|
|
81
|
-
return `${parts.year}-${parts.month}-${parts.day} ${parts.hour}:${parts.minute}`;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/** 绝对时刻 → 「2026-11-03」(本地时区)。语义同 `fmtTime`。 */
|
|
85
|
-
export function fmtDate(iso: string | null | undefined, timeZone?: string): string {
|
|
86
|
-
const parts = fmtParts(iso, timeZone, false);
|
|
87
|
-
if (!parts) return iso ? String(iso) : "—";
|
|
88
|
-
return `${parts.year}-${parts.month}-${parts.day}`;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
function fmtParts(
|
|
92
|
-
iso: string | null | undefined,
|
|
93
|
-
timeZone: string | undefined,
|
|
94
|
-
withTime: boolean,
|
|
95
|
-
): { year: string; month: string; day: string; hour: string; minute: string } | null {
|
|
96
|
-
if (!iso) return null;
|
|
97
|
-
const d = new Date(iso);
|
|
98
|
-
if (Number.isNaN(d.getTime())) return null;
|
|
99
|
-
const parts = new Intl.DateTimeFormat("zh-CN", {
|
|
100
|
-
...(timeZone ? { timeZone } : {}),
|
|
101
|
-
year: "numeric",
|
|
102
|
-
month: "2-digit",
|
|
103
|
-
day: "2-digit",
|
|
104
|
-
...(withTime ? { hour: "2-digit", minute: "2-digit" } : {}),
|
|
105
|
-
// h23:不用 hour12:false——某些 ICU 版本会把午夜排成 "24:00"。
|
|
106
|
-
hourCycle: withTime ? "h23" : undefined,
|
|
107
|
-
}).formatToParts(d);
|
|
108
|
-
const get = (type: string) => parts.find((p) => p.type === type)?.value ?? "";
|
|
109
|
-
return { year: get("year"), month: get("month"), day: get("day"), hour: get("hour"), minute: get("minute") };
|
|
110
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Session-list time helpers (batch 12, DSH-inspired): human relative labels
|
|
3
|
+
* and calendar-day grouping. Pure and `now`-injected so they are testable
|
|
4
|
+
* without a clock mock.
|
|
5
|
+
*
|
|
6
|
+
* 另外提供**绝对时刻**显示(`fmtTime` / `fmtDate`):
|
|
7
|
+
* 服务端一律存 UTC ISO,页面以前直接 `iso.slice(0,16)` 截出来显示——那是 UTC,
|
|
8
|
+
* +08:00 的运维看审计流水/学习记录会以为「8 小时前」。现在统一转成浏览器本地时区。
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const DAY = 86_400_000;
|
|
12
|
+
|
|
13
|
+
function startOfDay(t: number): number {
|
|
14
|
+
const d = new Date(t);
|
|
15
|
+
d.setHours(0, 0, 0, 0);
|
|
16
|
+
return d.getTime();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Whole calendar days between the item's day and today (0 = today, 1 = yesterday). */
|
|
20
|
+
function daysAgo(iso: string, now: Date): number {
|
|
21
|
+
const t = Date.parse(iso);
|
|
22
|
+
if (Number.isNaN(t)) return 0;
|
|
23
|
+
return Math.round((startOfDay(now.getTime()) - startOfDay(t)) / DAY);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** 刚刚 / N 分钟前 / N 小时前 / 昨天 / M月D日(跨年带年份)。 */
|
|
27
|
+
export function relativeTime(iso: string, now: Date): string {
|
|
28
|
+
const t = Date.parse(iso);
|
|
29
|
+
if (Number.isNaN(t)) return "";
|
|
30
|
+
const diff = now.getTime() - t; // negative (clock skew / future) reads as "刚刚"
|
|
31
|
+
if (diff < 60_000) return "刚刚";
|
|
32
|
+
if (diff < 3_600_000) return `${Math.floor(diff / 60_000)} 分钟前`;
|
|
33
|
+
const days = daysAgo(iso, now);
|
|
34
|
+
if (days === 0) return `${Math.floor(diff / 3_600_000)} 小时前`;
|
|
35
|
+
const d = new Date(t);
|
|
36
|
+
if (days === 1) return "昨天";
|
|
37
|
+
const md = `${d.getMonth() + 1}月${d.getDate()}日`;
|
|
38
|
+
return d.getFullYear() === now.getFullYear() ? md : `${d.getFullYear()}年${md}`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Group label for a session row; buckets follow calendar days, not 24h windows. */
|
|
42
|
+
export function groupOf(iso: string, now: Date): string {
|
|
43
|
+
const days = daysAgo(iso, now);
|
|
44
|
+
if (days <= 0) return "今天";
|
|
45
|
+
if (days === 1) return "昨天";
|
|
46
|
+
if (days <= 6) return "近 7 天";
|
|
47
|
+
if (days <= 29) return "近 30 天";
|
|
48
|
+
return "更早";
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Bucket a session list in order. Assumes items are sorted newest-first (the
|
|
53
|
+
* server does that); a newer item after an older one starts a new bucket run
|
|
54
|
+
* rather than being silently merged — same behaviour as DSH's list.
|
|
55
|
+
*/
|
|
56
|
+
export function groupSessions<T extends { updatedAt: string }>(
|
|
57
|
+
items: readonly T[],
|
|
58
|
+
now: Date,
|
|
59
|
+
): Array<{ label: string; items: T[] }> {
|
|
60
|
+
const out: Array<{ label: string; items: T[] }> = [];
|
|
61
|
+
for (const item of items) {
|
|
62
|
+
const label = groupOf(item.updatedAt, now);
|
|
63
|
+
const last = out[out.length - 1];
|
|
64
|
+
if (last && last.label === label) last.items.push(item);
|
|
65
|
+
else out.push({ label, items: [item] });
|
|
66
|
+
}
|
|
67
|
+
return out;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* 绝对时刻 → 「2026-11-03 09:12」(本地时区)。
|
|
72
|
+
*
|
|
73
|
+
* - `timeZone` 只给测试用(固定时区做断言);页面调用不传 = 用户本地时区。
|
|
74
|
+
* - 空值显示「—」;**解析不了就原样返回**(不吞、不假装是今天)。
|
|
75
|
+
* - 只喂 ISO 时刻(含 Z 或偏移);别喂「2026-11-03」这种纯日期——那会被当 UTC 零点,
|
|
76
|
+
* 在负时区会退到前一天。
|
|
77
|
+
*/
|
|
78
|
+
export function fmtTime(iso: string | null | undefined, timeZone?: string): string {
|
|
79
|
+
const parts = fmtParts(iso, timeZone, true);
|
|
80
|
+
if (!parts) return iso ? String(iso) : "—";
|
|
81
|
+
return `${parts.year}-${parts.month}-${parts.day} ${parts.hour}:${parts.minute}`;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** 绝对时刻 → 「2026-11-03」(本地时区)。语义同 `fmtTime`。 */
|
|
85
|
+
export function fmtDate(iso: string | null | undefined, timeZone?: string): string {
|
|
86
|
+
const parts = fmtParts(iso, timeZone, false);
|
|
87
|
+
if (!parts) return iso ? String(iso) : "—";
|
|
88
|
+
return `${parts.year}-${parts.month}-${parts.day}`;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function fmtParts(
|
|
92
|
+
iso: string | null | undefined,
|
|
93
|
+
timeZone: string | undefined,
|
|
94
|
+
withTime: boolean,
|
|
95
|
+
): { year: string; month: string; day: string; hour: string; minute: string } | null {
|
|
96
|
+
if (!iso) return null;
|
|
97
|
+
const d = new Date(iso);
|
|
98
|
+
if (Number.isNaN(d.getTime())) return null;
|
|
99
|
+
const parts = new Intl.DateTimeFormat("zh-CN", {
|
|
100
|
+
...(timeZone ? { timeZone } : {}),
|
|
101
|
+
year: "numeric",
|
|
102
|
+
month: "2-digit",
|
|
103
|
+
day: "2-digit",
|
|
104
|
+
...(withTime ? { hour: "2-digit", minute: "2-digit" } : {}),
|
|
105
|
+
// h23:不用 hour12:false——某些 ICU 版本会把午夜排成 "24:00"。
|
|
106
|
+
hourCycle: withTime ? "h23" : undefined,
|
|
107
|
+
}).formatToParts(d);
|
|
108
|
+
const get = (type: string) => parts.find((p) => p.type === type)?.value ?? "";
|
|
109
|
+
return { year: get("year"), month: get("month"), day: get("day"), hour: get("hour"), minute: get("minute") };
|
|
110
|
+
}
|
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import { useSyncExternalStore } from "react";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 视口是否窄于 maxWidth(响应式布局用)。
|
|
5
|
-
*
|
|
6
|
-
* 用 useSyncExternalStore 而不是 useState+useEffect:首帧就能拿到正确值,
|
|
7
|
-
* 不会出现"先按宽屏渲染再跳成窄屏"的闪动。服务端渲染(无 window)回落 false。
|
|
8
|
-
*/
|
|
9
|
-
export function useNarrow(maxWidth: number): boolean {
|
|
10
|
-
const query = `(max-width: ${maxWidth}px)`;
|
|
11
|
-
return useSyncExternalStore(
|
|
12
|
-
(onChange) => {
|
|
13
|
-
const mql = window.matchMedia(query);
|
|
14
|
-
mql.addEventListener("change", onChange);
|
|
15
|
-
return () => mql.removeEventListener("change", onChange);
|
|
16
|
-
},
|
|
17
|
-
() => window.matchMedia(query).matches,
|
|
18
|
-
() => false,
|
|
19
|
-
);
|
|
20
|
-
}
|
|
1
|
+
import { useSyncExternalStore } from "react";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 视口是否窄于 maxWidth(响应式布局用)。
|
|
5
|
+
*
|
|
6
|
+
* 用 useSyncExternalStore 而不是 useState+useEffect:首帧就能拿到正确值,
|
|
7
|
+
* 不会出现"先按宽屏渲染再跳成窄屏"的闪动。服务端渲染(无 window)回落 false。
|
|
8
|
+
*/
|
|
9
|
+
export function useNarrow(maxWidth: number): boolean {
|
|
10
|
+
const query = `(max-width: ${maxWidth}px)`;
|
|
11
|
+
return useSyncExternalStore(
|
|
12
|
+
(onChange) => {
|
|
13
|
+
const mql = window.matchMedia(query);
|
|
14
|
+
mql.addEventListener("change", onChange);
|
|
15
|
+
return () => mql.removeEventListener("change", onChange);
|
|
16
|
+
},
|
|
17
|
+
() => window.matchMedia(query).matches,
|
|
18
|
+
() => false,
|
|
19
|
+
);
|
|
20
|
+
}
|
|
@@ -1,54 +1,63 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* **第二产品的最小样板**:用于构建期产品切换验收。
|
|
3
|
-
*
|
|
4
|
-
* 它不是真产品,是「照抄起点」:一个 AppDef(路由表 + 侧栏 + 页面)加一个
|
|
5
|
-
* 组合根调用,就能接进 core 的壳。真实产品照它写,再补自己的页面与文案。
|
|
6
|
-
*
|
|
7
|
-
* 与真产品同契约(`vite.config.ts` 的 `appDefGuard` 会校验 appDef):
|
|
8
|
-
* - `export const appDef`:导航注册表,必须过 core 的 20 条结构契约;
|
|
9
|
-
* - `export function App`:组合根组件(`AgentApp` + 产品的事实)。
|
|
10
|
-
*
|
|
11
|
-
* 构建:`VITE_PRODUCT=fake-product npm --prefix apps/web run build`
|
|
12
|
-
* (CI 里跑这一条,等于每次都在验「切换真的有效」)。
|
|
13
|
-
*/
|
|
14
|
-
import { AgentApp } from "../agent-chat/mount.tsx";
|
|
15
|
-
import
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
*
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
<
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
1
|
+
/**
|
|
2
|
+
* **第二产品的最小样板**:用于构建期产品切换验收。
|
|
3
|
+
*
|
|
4
|
+
* 它不是真产品,是「照抄起点」:一个 AppDef(路由表 + 侧栏 + 页面)加一个
|
|
5
|
+
* 组合根调用,就能接进 core 的壳。真实产品照它写,再补自己的页面与文案。
|
|
6
|
+
*
|
|
7
|
+
* 与真产品同契约(`vite.config.ts` 的 `appDefGuard` 会校验 appDef):
|
|
8
|
+
* - `export const appDef`:导航注册表,必须过 core 的 20 条结构契约;
|
|
9
|
+
* - `export function App`:组合根组件(`AgentApp` + 产品的事实)。
|
|
10
|
+
*
|
|
11
|
+
* 构建:`VITE_PRODUCT=fake-product npm --prefix apps/web run build`
|
|
12
|
+
* (CI 里跑这一条,等于每次都在验「切换真的有效」)。
|
|
13
|
+
*/
|
|
14
|
+
import { AgentApp } from "../agent-chat/mount.tsx";
|
|
15
|
+
import { PersonasPage } from "../agent-chat/index.ts";
|
|
16
|
+
import type { AppDef } from "../agent-chat/app.tsx";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 路由键只能是**小写字母**(core 用 `/^#\/([a-z]+)/` 从 hash 取键)。
|
|
20
|
+
* 写成 "my-page" 这类取不出来,页面会像「点不动」——契约里有这一条。
|
|
21
|
+
*/
|
|
22
|
+
const ROUTES = {
|
|
23
|
+
chat: "助手",
|
|
24
|
+
home: "首页",
|
|
25
|
+
// core 内置的人格配置页:样板把它挂出来,活体才有面可验;真产品可不挂。
|
|
26
|
+
personas: "人格",
|
|
27
|
+
} as const;
|
|
28
|
+
|
|
29
|
+
const appDef: AppDef = {
|
|
30
|
+
home: "home",
|
|
31
|
+
// core 不认识这个键,它只是「坞撑满内容区」的那个路由。
|
|
32
|
+
chatRoute: "chat",
|
|
33
|
+
routes: { ...ROUTES },
|
|
34
|
+
navItems: () => [
|
|
35
|
+
{ route: "chat", label: ROUTES.chat },
|
|
36
|
+
{ route: "home", label: ROUTES.home },
|
|
37
|
+
{ route: "personas", label: ROUTES.personas },
|
|
38
|
+
],
|
|
39
|
+
page: (ctx) => {
|
|
40
|
+
if (ctx.route === "home") {
|
|
41
|
+
return (
|
|
42
|
+
<main className="content" style={{ padding: 24 }}>
|
|
43
|
+
<h2>样板首页</h2>
|
|
44
|
+
<p>这是第二产品样板:只有两个业务路由,没有业务页面。</p>
|
|
45
|
+
</main>
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
if (ctx.route === "personas") return <PersonasPage />;
|
|
49
|
+
return null;
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export { appDef };
|
|
54
|
+
|
|
55
|
+
export function App() {
|
|
56
|
+
return (
|
|
57
|
+
<AgentApp
|
|
58
|
+
def={appDef}
|
|
59
|
+
// 文案(坞标题、助手名)由产品给;core 不写死任何一句话、不认任何人格名。
|
|
60
|
+
texts={() => ({ title: "样板助手", assistantLabel: "助手" })}
|
|
61
|
+
/>
|
|
62
|
+
);
|
|
63
|
+
}
|
package/src/main.tsx
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { createRoot } from "react-dom/client";
|
|
2
|
-
import App from "./App.tsx";
|
|
3
|
-
import { applyThemeMode, loadThemeMode } from "./agent-chat/theme.ts";
|
|
4
|
-
import "./styles.css";
|
|
5
|
-
|
|
6
|
-
// Pin the saved theme mode before the first paint to avoid a flash.
|
|
7
|
-
applyThemeMode(loadThemeMode());
|
|
8
|
-
createRoot(document.getElementById("root")!).render(<App />);
|
|
1
|
+
import { createRoot } from "react-dom/client";
|
|
2
|
+
import App from "./App.tsx";
|
|
3
|
+
import { applyThemeMode, loadThemeMode } from "./agent-chat/theme.ts";
|
|
4
|
+
import "./styles.css";
|
|
5
|
+
|
|
6
|
+
// Pin the saved theme mode before the first paint to avoid a flash.
|
|
7
|
+
applyThemeMode(loadThemeMode());
|
|
8
|
+
createRoot(document.getElementById("root")!).render(<App />);
|