@roaming-ai/dsh-group-chat 0.2.2 → 0.3.1

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.
Files changed (103) hide show
  1. package/README.md +91 -27
  2. package/lib/client.js +1254 -598
  3. package/lib/client.js.map +1 -1
  4. package/lib/index.js +573 -109
  5. package/lib/types/client/components/AsidePanel.d.ts +3 -19
  6. package/lib/types/client/components/Bubble.d.ts +6 -3
  7. package/lib/types/client/components/ChatPanel.d.ts +1 -0
  8. package/lib/types/client/components/ConstraintList.d.ts +11 -0
  9. package/lib/types/client/components/FailCard.d.ts +9 -0
  10. package/lib/types/client/components/Fold.d.ts +19 -0
  11. package/lib/types/client/components/HoverTip.d.ts +22 -0
  12. package/lib/types/client/components/MessageFlow.d.ts +1 -0
  13. package/lib/types/client/components/MsgActions.d.ts +14 -0
  14. package/lib/types/client/components/NavPanel.d.ts +9 -11
  15. package/lib/types/client/components/RoleDrawer.d.ts +3 -2
  16. package/lib/types/client/components/ToolRow.d.ts +2 -2
  17. package/lib/types/client/hooks/useComposer.d.ts +6 -1
  18. package/lib/types/client/hooks/useGroupChatState.d.ts +0 -16
  19. package/lib/types/client/lib/composer-draft.d.ts +17 -0
  20. package/lib/types/client/lib/model.d.ts +1 -7
  21. package/lib/types/client/utils/utils.d.ts +4 -4
  22. package/lib/types/core/constraints.d.ts +61 -0
  23. package/lib/types/core/errors.d.ts +54 -0
  24. package/lib/types/core/json.d.ts +6 -0
  25. package/lib/types/core/types.d.ts +76 -60
  26. package/lib/types/host/api/actions.d.ts +1 -1
  27. package/lib/types/host/engine/conversation.d.ts +5 -3
  28. package/lib/types/host/engine/defaults.d.ts +13 -0
  29. package/lib/types/host/engine/fold.d.ts +17 -0
  30. package/lib/types/host/engine/index.d.ts +1 -0
  31. package/lib/types/host/engine/retitle.d.ts +5 -5
  32. package/lib/types/host/service.d.ts +1 -1
  33. package/lib/types/host/state.d.ts +2 -0
  34. package/lib/types/index.d.ts +2 -0
  35. package/lib/types/shared/file-mention-grammar.d.ts +3 -12
  36. package/package.json +1 -1
  37. package/src/client/GroupChatPanel.tsx +25 -59
  38. package/src/client/components/AsidePanel.tsx +50 -23
  39. package/src/client/components/Bubble.tsx +45 -18
  40. package/src/client/components/ChatPanel.tsx +17 -12
  41. package/src/client/components/Composer.tsx +26 -20
  42. package/src/client/components/ConstraintList.tsx +64 -0
  43. package/src/client/components/FailCard.tsx +49 -0
  44. package/src/client/components/Fold.tsx +72 -0
  45. package/src/client/components/HoverTip.tsx +134 -0
  46. package/src/client/components/MessageFlow.tsx +78 -54
  47. package/src/client/components/MsgActions.tsx +85 -0
  48. package/src/client/components/NavPanel.tsx +102 -79
  49. package/src/client/components/RoleDrawer.tsx +29 -7
  50. package/src/client/components/ThinkRow.tsx +7 -3
  51. package/src/client/components/ToolRow.tsx +9 -5
  52. package/src/client/hooks/useComposer.ts +54 -56
  53. package/src/client/hooks/useGroupChatState.ts +7 -17
  54. package/src/client/lib/composer-draft.ts +47 -0
  55. package/src/client/lib/model.ts +1 -14
  56. package/src/client/lib/styles.ts +73 -12
  57. package/src/client/react-dom-shim.d.ts +2 -0
  58. package/src/client/utils/utils.ts +6 -6
  59. package/src/core/constraints.ts +204 -0
  60. package/src/core/errors.ts +174 -0
  61. package/src/core/json.ts +18 -0
  62. package/src/core/types.ts +52 -19
  63. package/src/host/api/actions.ts +49 -14
  64. package/src/host/broadcast.ts +3 -3
  65. package/src/host/engine/conversation.ts +104 -40
  66. package/src/host/engine/defaults.ts +28 -0
  67. package/src/host/engine/fold.ts +99 -0
  68. package/src/host/engine/index.ts +1 -0
  69. package/src/host/engine/retitle.ts +24 -41
  70. package/src/host/materials/materials.ts +2 -0
  71. package/src/host/persistence/persistence.ts +18 -1
  72. package/src/host/service.ts +1 -1
  73. package/src/host/state.ts +5 -4
  74. package/src/host/tools/tools.ts +2 -2
  75. package/src/index.ts +2 -0
  76. package/src/shared/file-mention-grammar.test.ts +1 -38
  77. package/src/shared/file-mention-grammar.ts +7 -33
  78. package/lib/types/client/Bubble.d.ts +0 -10
  79. package/lib/types/client/Glyph.d.ts +0 -12
  80. package/lib/types/client/GroupChatPanel.old.d.ts +0 -10
  81. package/lib/types/client/GroupChatSettingsSection.d.ts +0 -15
  82. package/lib/types/client/PermissionSelect.d.ts +0 -13
  83. package/lib/types/client/RoleDrawer.d.ts +0 -17
  84. package/lib/types/client/ThinkRow.d.ts +0 -9
  85. package/lib/types/client/ToolRow.d.ts +0 -9
  86. package/lib/types/client/api.d.ts +0 -8
  87. package/lib/types/client/components.d.ts +0 -22
  88. package/lib/types/client/drawer.d.ts +0 -17
  89. package/lib/types/client/glyph.d.ts +0 -12
  90. package/lib/types/client/model.d.ts +0 -78
  91. package/lib/types/client/panel.d.ts +0 -10
  92. package/lib/types/client/settings.d.ts +0 -15
  93. package/lib/types/client/styles.d.ts +0 -10
  94. package/lib/types/client/ui.d.ts +0 -17
  95. package/lib/types/client/utils.d.ts +0 -22
  96. package/lib/types/host/actions.d.ts +0 -34
  97. package/lib/types/host/conversation.d.ts +0 -25
  98. package/lib/types/host/http.d.ts +0 -16
  99. package/lib/types/host/materials.d.ts +0 -32
  100. package/lib/types/host/persistence.d.ts +0 -31
  101. package/lib/types/host/routes.d.ts +0 -11
  102. package/lib/types/host/store.d.ts +0 -65
  103. package/lib/types/host/tools.d.ts +0 -28
@@ -0,0 +1,47 @@
1
+ /**
2
+ * 未发送草稿:按会话 id 分槽,模块级跨挂载存活。
3
+ * 主会话⇄群聊会整页卸载 composer,contentEditable 的 HTML 必须自行记住。
4
+ * @module dsh-group-chat/client/composer-draft
5
+ */
6
+
7
+ export interface ComposerDraft {
8
+ html: string
9
+ text: string
10
+ }
11
+
12
+ const LIMIT = 32
13
+ const drafts = new Map<string, ComposerDraft>()
14
+
15
+ const empty = (): ComposerDraft => ({ html: '', text: '' })
16
+
17
+ /** 读指定会话草稿;命中则提到 LRU 最近端。 */
18
+ export function readComposerDraft(sessionId: string | null | undefined): ComposerDraft {
19
+ if (!sessionId) return empty()
20
+ const hit = drafts.get(sessionId)
21
+ if (!hit) return empty()
22
+ drafts.delete(sessionId)
23
+ drafts.set(sessionId, hit)
24
+ return hit
25
+ }
26
+
27
+ /** 写入;空草稿删槽。超出上限淘汰最旧槽。 */
28
+ export function writeComposerDraft(sessionId: string | null | undefined, html: string, text: string): void {
29
+ if (!sessionId) return
30
+ drafts.delete(sessionId)
31
+ if (!html && !text) return
32
+ drafts.set(sessionId, { html, text })
33
+ if (drafts.size > LIMIT) {
34
+ const oldest = drafts.keys().next().value
35
+ if (oldest !== undefined) drafts.delete(oldest)
36
+ }
37
+ }
38
+
39
+ /** 发送成功或明确丢弃时摘槽。 */
40
+ export function clearComposerDraft(sessionId: string | null | undefined): void {
41
+ if (sessionId) drafts.delete(sessionId)
42
+ }
43
+
44
+ /** 测试用:清空全部槽位。 */
45
+ export function resetComposerDrafts(): void {
46
+ drafts.clear()
47
+ }
@@ -3,7 +3,7 @@
3
3
  * @module dsh-group-chat/client/model
4
4
  */
5
5
 
6
- import type { RoleRecord, Snapshot, ToolCallRecord } from '../../core/types.ts'
6
+ import type { Snapshot } from '../../core/types.ts'
7
7
 
8
8
  /** 会话列表状态派生与文案(core 纯函数,供 NavPanel 渲染)。 */
9
9
  export { sessStatus, SESS_STATUS_LABEL, type SessStatus } from '../../core/status.ts'
@@ -104,16 +104,3 @@ export function draftFromRole(role: SnapshotRole): RoleDraft {
104
104
  export function blankDraft(): RoleDraft {
105
105
  return { name: '', color: null, persona: '', provider: '', model: '', temperature: undefined, reasoningEffort: 'default', enabled: true, thinking: false }
106
106
  }
107
-
108
- /** 工具调用行的展示参数。 */
109
- export type ToolCallView = ToolCallRecord
110
-
111
- /** 草稿是否可作为角色落库(宿主侧再校验一次)。 */
112
- export function draftMissing(draft: RoleDraft): string {
113
- if (!draft.name.trim()) return '角色名称不能为空'
114
- if (!draft.provider || !draft.model) return '请选择角色绑定的模型'
115
- return ''
116
- }
117
-
118
- /** RoleRecord 兼容视图(快照角色行即其展示子集)。 */
119
- export type RoleView = RoleRecord
@@ -62,7 +62,32 @@ export const CSS = [
62
62
  '.dsgc-seambtn:hover{background:var(--dsw-alias-interactive-bg-hover,rgba(128,128,128,.15));color:var(--dsw-alias-label-primary,inherit);border-color:var(--dsw-alias-border-l3,rgba(128,128,128,.4))}',
63
63
  '.dsgc-seambtn.left{left:0;border-left:none;border-radius:0 8px 8px 0;box-shadow:3px 0 10px rgba(0,0,0,.07)}',
64
64
  '.dsgc-seambtn.right{right:0;border-right:none;border-radius:8px 0 0 8px;box-shadow:-3px 0 10px rgba(0,0,0,.07)}',
65
- '.dsgc-chathead{display:flex;gap:10px;align-items:center;padding:12px 16px;border-bottom:1px solid var(--dsw-alias-border-l1,rgba(128,128,128,.2))}',
65
+ '.dsgc-chathead{display:flex;flex-direction:column;gap:6px;padding:12px 16px;border-bottom:1px solid var(--dsw-alias-border-l1,rgba(128,128,128,.2))}',
66
+ '.dsgc-chathead-row{display:flex;gap:10px;align-items:center;min-width:0}',
67
+ '.dsgc-constraints{align-self:center;box-sizing:border-box;width:76%;max-width:76%;display:flex;flex-direction:column;gap:8px;min-width:0;padding:10px 12px;border:1px solid var(--dsw-alias-border-l2,rgba(128,128,128,.3));background:var(--dsw-alias-bg-layer-3,rgba(128,128,128,.06));border-radius:12px;animation:dsgc-fade-in .18s ease-out}',
68
+ '.dsgc-chead{display:flex;flex-direction:column;gap:2px;min-width:0}',
69
+ '.dsgc-ctitle{margin:0;font:inherit;font-size:12px;font-weight:600;line-height:1.5;color:var(--dsw-alias-label-primary,inherit)}',
70
+ '.dsgc-cdesc{margin:0;font-size:11.5px;line-height:1.5;color:var(--dsw-alias-label-tertiary,inherit)}',
71
+ '.dsgc-fold{display:grid;grid-template-rows:0fr;transition:grid-template-rows .16s ease-in}',
72
+ '.dsgc-fold.open{grid-template-rows:1fr;transition:grid-template-rows .22s cubic-bezier(.16,1,.3,1)}',
73
+ '.dsgc-fold-inner{min-height:0;overflow:hidden;display:flex;flex-direction:column;gap:2px;opacity:0;transition:opacity .12s ease-in}',
74
+ '.dsgc-fold.open .dsgc-fold-inner{opacity:1;transition:opacity .18s cubic-bezier(.16,1,.3,1)}',
75
+ '.dsgc-clip{--dsgc-clip-from:var(--dsw-alias-bg-layer-3,rgba(128,128,128,.18));position:relative;min-width:0}',
76
+ '.dsgc-clip::before,.dsgc-clip::after{content:"";position:absolute;left:0;right:0;height:14px;pointer-events:none;z-index:1;opacity:0;transition:opacity .12s}',
77
+ '.dsgc-clip::before{top:0;background:linear-gradient(to bottom,var(--dsgc-clip-from),transparent)}',
78
+ '.dsgc-clip::after{bottom:0;background:linear-gradient(to top,var(--dsgc-clip-from),transparent)}',
79
+ '.dsgc-clip.can-up::before,.dsgc-clip.can-down::after{opacity:1}',
80
+ '.dsgc-clip-scroll{min-width:0;overflow-y:auto;overscroll-behavior:contain;overflow-anchor:none;scrollbar-width:thin;scrollbar-color:var(--dsw-alias-scrollbar-bg-l2,rgba(128,128,128,.35)) transparent}',
81
+ '.dsgc-clist{display:flex;flex-direction:column;gap:2px;min-width:0}',
82
+ '.dsgc-constraint{display:flex;gap:8px;align-items:flex-start;min-width:0;font-size:11.5px;line-height:1.5}',
83
+ '.dsgc-ckind{flex:none;font-weight:500}',
84
+ '.dsgc-ckind.decided{color:var(--dsw-alias-label-secondary,inherit)}',
85
+ '.dsgc-ckind.rejected{color:var(--dsw-alias-state-error-primary,#e5484d)}',
86
+ '.dsgc-ckind.open{color:var(--dsw-alias-label-tertiary,inherit)}',
87
+ '.dsgc-ctext{min-width:0;color:var(--dsw-alias-label-primary,inherit);overflow-wrap:break-word}',
88
+ '.dsgc-cmore{align-self:flex-start;border:none;background:none;padding:0;font:inherit;font-size:11.5px;line-height:1.5;color:var(--dsw-alias-label-tertiary,inherit);cursor:pointer}',
89
+ '.dsgc-cmore:hover{color:var(--dsw-alias-label-secondary,inherit)}',
90
+ '.dsgc-cmore:focus-visible{outline:2px solid var(--dsw-alias-brand-primary,var(--dsw-alias-state-business-primary,#4f6ef7));outline-offset:1px}',
66
91
  '.dsgc-sess-title{flex:none;font-weight:600;font-size:14px;color:var(--dsw-alias-label-primary,inherit);max-width:30%;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}',
67
92
  '.dsgc-topic{flex:1;background:transparent;border:none;border-bottom:1px solid transparent;padding:2px;min-height:20px;color:var(--dsw-alias-label-secondary,inherit);font:inherit;outline:none;min-width:0;transition:color .12s,border-color .12s}',
68
93
  '.dsgc-topic:hover{border-bottom-color:var(--dsw-alias-border-l2,rgba(128,128,128,.3))}',
@@ -99,7 +124,8 @@ export const CSS = [
99
124
  '.dsgc-think .dsgc-thinkrow{font-size:12px;color:var(--dsw-alias-label-secondary,inherit);border-radius:8px;padding:3px 8px;transition:background-color .12s}',
100
125
  '.dsgc-think .dsgc-thinkrow:hover{background:var(--dsw-alias-interactive-bg-hover,rgba(128,128,128,.1))}',
101
126
  '.dsgc-think .dsgc-thinksummary{color:var(--dsw-alias-label-tertiary,inherit);overflow:hidden;text-overflow:ellipsis;white-space:nowrap;max-width:44ch;font-size:11.5px;margin-left:6px}',
102
- '.dsgc-think .dsgc-thinkbody{font-size:12.5px;line-height:1.7;color:var(--dsw-alias-label-secondary,inherit);white-space:pre-wrap;word-break:break-word;border-left:2px solid var(--dsw-alias-border-l2,rgba(128,128,128,.25));margin:2px 0 4px;padding:2px 0 2px 10px;max-height:320px;overflow-y:auto;scrollbar-width:thin;scrollbar-color:var(--dsw-alias-scrollbar-bg-l2,rgba(128,128,128,.35)) transparent;animation:dsgc-think-in .2s ease-out}',
127
+ '.dsgc-think .dsgc-clip{margin:2px 0 4px;padding-left:10px;border-left:2px solid var(--dsw-alias-border-l2,rgba(128,128,128,.25))}',
128
+ '.dsgc-think .dsgc-thinkbody{font-size:12.5px;line-height:1.7;color:var(--dsw-alias-label-secondary,inherit);white-space:pre-wrap;word-break:break-word}',
103
129
  /* 首 delta 前的「思考中」占位行:ThinkRow 同语言(思考图标 + 次要色 12px 小字)
104
130
  + 三点交错呼吸动画——深度思考模型首字节可能等数秒到数十秒,空白气泡会被
105
131
  感知为卡死;delta 到达后被真实思考行/正文自然替换 */
@@ -111,17 +137,42 @@ export const CSS = [
111
137
  '.dsgc-pendingdots i:nth-child(3){animation-delay:.4s}',
112
138
  '@keyframes dsgc-dot-breathe{0%,100%{opacity:.25}50%{opacity:1}}',
113
139
  '.dsgc-sysmsg{align-self:center;font-size:11.5px;color:var(--dsw-alias-label-secondary,inherit);background:var(--dsw-alias-bg-module-platform,rgba(128,128,128,.1));border-radius:999px;padding:2px 10px;text-align:center;max-width:90%;animation:dsgc-sys-in .24s ease-out}',
140
+ /* 消息操作条:气泡外下方;角色左齐、自己右齐。占位常在,悬停显现;失败卡 always 常驻 */
141
+ '.dsgc-msgops{display:inline-flex;align-items:center;gap:2px;margin-top:2px;min-height:24px;visibility:hidden;opacity:0;transition:opacity .12s,visibility .12s}',
142
+ '.dsgc-msg:hover .dsgc-msgops,.dsgc-msg:focus-within .dsgc-msgops,.dsgc-msgops.always{visibility:visible;opacity:1}',
143
+ '@media (hover:none){.dsgc-msg .dsgc-msgops{visibility:visible;opacity:1}}',
144
+ '.dsgc-msg.mine .dsgc-msgops{justify-content:flex-end}',
145
+ '.dsgc-msgop{border:none;background:none;color:var(--dsw-alias-label-tertiary,inherit);cursor:pointer;padding:3px 6px;border-radius:6px;display:inline-flex;align-items:center;gap:4px;font:inherit;font-size:11.5px;line-height:16px;transition:background-color .12s,color .12s}',
146
+ '.dsgc-msgop svg{flex:none}',
147
+ '.dsgc-msgop:hover:not(:disabled){background:var(--dsw-alias-interactive-bg-hover,rgba(128,128,128,.15));color:var(--dsw-alias-label-primary,inherit)}',
148
+ '.dsgc-msgop:focus-visible{outline:2px solid var(--dsw-alias-brand-primary,var(--dsw-alias-state-business-primary,#4f6ef7));outline-offset:1px}',
149
+ '.dsgc-msgop.done{color:var(--dsw-alias-state-success-primary,#30a46c)}',
150
+ '.dsgc-msgop.retry{color:var(--dsw-alias-state-error-primary,#e5484d)}',
151
+ '.dsgc-msgop.retry:hover:not(:disabled){background:color-mix(in srgb,var(--dsw-alias-state-error-primary,#e5484d) 10%,transparent);color:var(--dsw-alias-state-error-primary,#e5484d)}',
152
+ '.dsgc-msgop:disabled{opacity:.4;cursor:default}',
153
+ /* 失败卡:角色消息形态,人话标题 + 可展开原文;不用左侧色条,避免 craft-floor 禁令 */
154
+ '.dsgc-fail{background:color-mix(in srgb,var(--dsw-alias-state-error-primary,#e5484d) 8%,var(--dsw-alias-bg-layer-3,rgba(128,128,128,.06)));border:1px solid color-mix(in srgb,var(--dsw-alias-state-error-primary,#e5484d) 28%,var(--dsw-alias-border-l1,rgba(128,128,128,.15)));border-radius:12px;padding:10px 12px;display:flex;flex-direction:column;gap:6px;min-width:0;animation:dsgc-fade-in .18s ease-out}',
155
+ '.dsgc-failhead{display:flex;align-items:flex-start;gap:8px;min-width:0}',
156
+ '.dsgc-failicon{flex:none;width:22px;height:22px;border-radius:50%;display:inline-flex;align-items:center;justify-content:center;color:var(--dsw-alias-state-error-primary,#e5484d);background:color-mix(in srgb,var(--dsw-alias-state-error-primary,#e5484d) 12%,transparent);margin-top:1px}',
157
+ '.dsgc-failcopy{flex:1;min-width:0;display:flex;flex-direction:column;gap:2px}',
158
+ '.dsgc-failtitle{font-size:13px;font-weight:600;line-height:1.4;color:var(--dsw-alias-label-primary,inherit)}',
159
+ '.dsgc-faildetail{font-size:12px;line-height:1.5;color:var(--dsw-alias-label-secondary,inherit);overflow-wrap:anywhere}',
160
+ '.dsgc-failmore{align-self:flex-start;border:none;background:none;padding:0;font:inherit;font-size:11.5px;line-height:1.5;color:var(--dsw-alias-label-tertiary,inherit);cursor:pointer}',
161
+ '.dsgc-failmore:hover{color:var(--dsw-alias-label-secondary,inherit)}',
162
+ '.dsgc-failmore:focus-visible{outline:2px solid var(--dsw-alias-brand-primary,var(--dsw-alias-state-business-primary,#4f6ef7));outline-offset:1px}',
163
+ '.dsgc-fail .dsgc-clip{--dsgc-clip-from:var(--dsw-alias-bg-layer-2,rgba(128,128,128,.12));border-radius:8px}',
164
+ '.dsgc-failraw{margin:0;font-size:11.5px;line-height:1.55;white-space:pre-wrap;word-break:break-word;overflow-wrap:anywhere;padding:8px 10px;border-radius:8px;background:var(--dsw-alias-bg-layer-2,transparent);color:var(--dsw-alias-label-secondary,inherit)}',
114
165
  /* ===== 工具调用行 / 确认卡片(对标思考折叠语言) ===== */
115
166
  '.dsgc-tool{margin:2px 0}',
116
167
  '.dsgc-tool .dsgc-toolrow{font-size:12px;color:var(--dsw-alias-label-secondary,inherit);border-radius:8px;padding:3px 8px;transition:background-color .12s}',
117
168
  '.dsgc-tool .dsgc-toolrow:hover{background:var(--dsw-alias-interactive-bg-hover,rgba(128,128,128,.1))}',
118
169
  '.dsgc-tool .dsgc-toolsummary{color:var(--dsw-alias-label-tertiary,inherit);overflow:hidden;text-overflow:ellipsis;white-space:nowrap;max-width:40ch;font-size:11.5px;margin-left:6px}',
119
- '.dsgc-tool .dsgc-toolbody{font-size:12px;line-height:1.6;color:var(--dsw-alias-label-secondary,inherit);white-space:pre-wrap;word-break:break-word;border-left:2px solid var(--dsw-alias-border-l2,rgba(128,128,128,.25));margin:2px 0 4px;padding:2px 0 2px 10px;max-height:260px;overflow-y:auto;scrollbar-width:thin;scrollbar-color:var(--dsw-alias-scrollbar-bg-l2,rgba(128,128,128,.35)) transparent}',
170
+ '.dsgc-tool .dsgc-clip{margin:2px 0 4px;padding-left:10px;border-left:2px solid var(--dsw-alias-border-l2,rgba(128,128,128,.25))}',
171
+ '.dsgc-tool .dsgc-toolbody{font-size:12px;line-height:1.6;color:var(--dsw-alias-label-secondary,inherit);white-space:pre-wrap;word-break:break-word}',
120
172
  '.dsgc-confirm{border:1px solid var(--dsw-alias-state-business-primary,#4f6ef7);border-radius:12px;background:var(--dsw-alias-bg-layer-3,rgba(128,128,128,.06));padding:10px 12px;display:flex;flex-direction:column;gap:8px;max-width:76%;animation:dsgc-fade-in .18s ease-out}',
121
173
  '.dsgc-confirmtitle{font-size:12px;font-weight:600;color:var(--dsw-alias-label-primary,inherit);display:flex;align-items:center;gap:6px}',
122
174
  '.dsgc-confirmcmd{font-size:12px;line-height:1.6;white-space:pre-wrap;word-break:break-word;border:1px solid var(--dsw-alias-border-l2,rgba(128,128,128,.3));border-radius:8px;padding:8px 10px;background:var(--dsw-alias-bg-layer-2,transparent);color:var(--dsw-alias-label-primary,inherit);max-height:240px;overflow-y:auto;scrollbar-width:thin;scrollbar-color:var(--dsw-alias-scrollbar-bg-l2,rgba(128,128,128,.35)) transparent}',
123
175
  '.dsgc-confirmops{display:flex;gap:8px;justify-content:flex-end}',
124
- '.dsgc-sysmsg.err{color:var(--dsw-alias-state-error-primary,#e5484d)}',
125
176
  /* 清空确认弹窗内的说明列表 */
126
177
  '.dsgc-clearnotes{margin:0;padding-left:18px;font-size:13px;line-height:1.8;color:var(--dsw-alias-label-secondary,inherit);display:flex;flex-direction:column;gap:2px}',
127
178
  '.dsgc-err{font-size:12px;color:var(--dsw-alias-state-error-primary,#e5484d);animation:dsgc-fade-in .18s ease-out}',
@@ -152,8 +203,11 @@ export const CSS = [
152
203
  '.dsgc-card .dsgc-sendrow{padding:0 12px 10px}',
153
204
  '.dsgc-stopbtn{background:var(--dsw-alias-state-error-primary,#e5484d);border-color:transparent;color:#fff;font-weight:600}',
154
205
  '.dsgc-stopbtn:hover:not(:disabled){filter:brightness(1.08);color:#fff}',
155
- '.dsgc-parts{display:flex;gap:6px;flex-wrap:wrap;align-items:center;row-gap:4px;height:24px}',
156
- '.dsgc-partslabel{font-size:12px;color:var(--dsw-alias-label-tertiary,inherit);flex:none}',
206
+ /* 标签固定在左;芯片在右侧独立换行,后续行与首行芯片左缘对齐(不折到「参与角色」下面)。
207
+ 单行 24px;角色过多时随内容增高,避免固定高度把后续行溢出叠到下方输入卡上 */
208
+ '.dsgc-parts{display:flex;align-items:flex-start;gap:6px;min-height:24px}',
209
+ '.dsgc-partslabel{font-size:12px;line-height:24px;color:var(--dsw-alias-label-tertiary,inherit);flex:none}',
210
+ '.dsgc-partlist{display:flex;flex-wrap:wrap;align-items:center;gap:6px;row-gap:4px;flex:1;min-width:0}',
157
211
  '.dsgc-partchip{display:inline-flex;gap:6px;align-items:center;border:1px solid var(--dsw-alias-border-l2,rgba(128,128,128,.3));background:transparent;color:var(--dsw-alias-label-secondary,inherit);border-radius:999px;padding:2px 10px;font-size:12px;line-height:1.5;cursor:pointer;transition:background-color .12s,color .12s,border-color .12s;font-family:inherit}',
158
212
  '.dsgc-partchip:hover{color:var(--dsw-alias-label-primary,inherit)}',
159
213
  '.dsgc-partchip.on{background:var(--dsw-alias-interactive-bg-active,rgba(128,128,128,.18));color:var(--dsw-alias-label-primary,inherit);border-color:var(--dsw-alias-border-l2,rgba(128,128,128,.3))}',
@@ -176,6 +230,12 @@ export const CSS = [
176
230
  '.dsgc-rounds .dsgc-roundbtn:hover:not(:disabled){background:var(--dsw-alias-interactive-bg-hover,rgba(128,128,128,.15));color:var(--dsw-alias-label-primary,inherit)}',
177
231
  '.dsgc-rounds .dsgc-roundbtn:disabled{opacity:.4;cursor:default}',
178
232
  '.dsgc-rounds .dsgc-roundnum{width:26px;text-align:center;font-variant-numeric:tabular-nums;font-weight:600;color:var(--dsw-alias-label-primary,inherit)}',
233
+ /* hover 气泡:对齐宿主 Tooltip.module.css,portal 到 body 以躲开 container-type */
234
+ '.dsgc-hovertip{position:fixed;z-index:100;width:max-content;max-width:50vw;padding:3px 7px;border-radius:8px;background:var(--dsw-alias-tooltip-bg,#1f1f1f);color:var(--dsw-static-neutral-bluish-00,#fff);font-size:13px;line-height:20px;white-space:pre-line;overflow-wrap:break-word;pointer-events:none;animation:dsgc-hovertip-in 150ms var(--ds-ease-in-out,ease)}',
235
+ '.dsgc-hovertip[data-side="right"]{transform:translateY(-50%)}',
236
+ '.dsgc-hovertip[data-side="top"]{transform:translate(-50%,-100%)}',
237
+ '.dsgc-hovertip[data-side="bottom"]{transform:translateX(-50%)}',
238
+ '@keyframes dsgc-hovertip-in{from{opacity:0}}',
179
239
  /* ===== @成员 / @文件弹层 ===== */
180
240
  '.dsgc-mentionwrap{position:relative}',
181
241
  '.dsgc-mention{animation:dsgc-pop-in .16s cubic-bezier(.16,1,.3,1);position:absolute;left:0;right:0;bottom:calc(100% + 6px);background:var(--dsw-alias-bg-layer-3,#fff);border:1px solid var(--dsw-alias-border-l2,rgba(128,128,128,.3));border-radius:10px;box-shadow:var(--dsw-shadow-lv3,0 8px 24px rgba(0,0,0,.18));max-height:220px;overflow-y:auto;padding:4px;z-index:30;display:flex;flex-direction:column;gap:2px;scrollbar-width:thin;scrollbar-color:var(--dsw-alias-scrollbar-bg-l2,rgba(128,128,128,.35)) transparent}',
@@ -214,10 +274,12 @@ export const CSS = [
214
274
  '.dsgc-rolename{font-weight:600;font-size:13px;flex:1;min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;color:var(--dsw-alias-label-primary,inherit)}',
215
275
  '.dsgc-role.off .dsgc-rolename{color:var(--dsw-alias-label-tertiary,inherit)}',
216
276
  '.dsgc-rolepersona{font-size:11.5px;color:var(--dsw-alias-label-tertiary,inherit);line-height:1.5;display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical;overflow:hidden;min-height:0}',
217
- '.dsgc-rolemodel{width:fit-content;max-width:100%;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;background:var(--dsw-alias-bg-module-platform,rgba(128,128,128,.12));border-radius:999px;padding:1px 8px;font-size:11px;font-weight:500;line-height:17px;color:var(--dsw-alias-label-secondary,inherit)}',
218
- '.dsgc-roleops{display:flex;gap:2px;flex:none;opacity:0;transition:opacity .12s}',
277
+ '.dsgc-rolemeta{display:flex;align-items:center;gap:6px;min-width:0;flex:1}',
278
+ '.dsgc-rolemodel{min-width:0;flex:0 1 auto;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;background:var(--dsw-alias-bg-module-platform,rgba(128,128,128,.12));border-radius:999px;padding:1px 8px;font-size:11px;font-weight:500;line-height:17px;color:var(--dsw-alias-label-secondary,inherit)}',
279
+ '.dsgc-rolethink{display:inline-flex;align-items:center;flex:none;color:var(--dsw-alias-label-tertiary,inherit)}',
280
+ '.dsgc-roleops{display:flex;gap:2px;flex:none;margin-left:auto;opacity:0;transition:opacity .12s}',
219
281
  '.dsgc-role:hover .dsgc-roleops,.dsgc-role:focus-within .dsgc-roleops{opacity:1}',
220
- '.dsgc-rolemenu{display:flex;align-items:center;justify-content:space-between;gap:8px}',
282
+ '.dsgc-rolemenu{display:flex;align-items:center;gap:8px}',
221
283
  /* ===== 表单与控件(角色抽屉内) ===== */
222
284
  '.dsgc-field{display:flex;flex-direction:column;gap:5px}',
223
285
  '.dsgc-field>label{font-size:12px;font-weight:500;color:var(--dsw-alias-label-secondary,inherit)}',
@@ -239,7 +301,6 @@ export const CSS = [
239
301
  '@keyframes dsgc-drawer-out{to{transform:translateX(32px);opacity:0}}',
240
302
  '@keyframes dsgc-msg-in{from{opacity:0;transform:translateY(6px)}}',
241
303
  '@keyframes dsgc-sys-in{from{opacity:0;transform:scale(.96)}}',
242
- '@keyframes dsgc-think-in{from{opacity:0;transform:translateY(-3px)}}',
243
304
  '@keyframes dsgc-pop-in{from{opacity:0;transform:translateY(4px)}}',
244
305
  '@keyframes dsgc-rise-in{from{opacity:0;transform:translate(-50%,6px)}}',
245
306
  '@keyframes dsgc-fade-in{from{opacity:0}}',
@@ -269,7 +330,7 @@ export const CSS = [
269
330
  '.dsgc-partchip:focus-visible,.dsgc-mentionitem:focus-visible,.dsgc-dot:focus-visible,.dsgc-opbtn:focus-visible,.dsgc-twist:focus-visible,.dsgc-addsess:focus-visible,.dsgc-grow-row:focus-visible,.dsgc-sess-row:focus-visible,.dsgc-rename:focus-visible,.dsgc-fbrow:focus-visible,.dsgc-role:focus-visible,.dsgc-roundbtn:focus-visible,.dsgc-tobtn:focus-visible,.dsgc-seambtn:focus-visible{outline:2px solid var(--dsw-alias-brand-primary,var(--dsw-alias-state-business-primary,#4f6ef7));outline-offset:1px}',
270
331
  /* ===== 窄容器:右栏覆盖式呈现 ===== */
271
332
  '@container (max-width: 880px){.dsgc-aside{position:absolute;top:0;right:0;bottom:0;z-index:15;width:min(304px,88%);box-shadow:-12px 0 32px rgba(0,0,0,.16);border-left:1px solid var(--dsw-alias-border-l2,rgba(128,128,128,.3));transition:transform .3s cubic-bezier(.16,1,.3,1),opacity .2s ease}.dsgc-aside.closed{width:min(304px,88%);padding-left:12px;padding-right:12px;border-left-width:1px;transform:translateX(calc(100% + 14px));opacity:0;transition:transform .26s ease-in,opacity .18s ease-in,visibility 0s .24s}.dsgc-seambtn.right:not(.closed){right:min(304px,88%)}}',
272
- '@container (max-width: 640px){.dsgc-nav{width:200px}.dsgc-msgbody{max-width:88%}}',
333
+ '@container (max-width: 640px){.dsgc-nav{width:200px}.dsgc-msgbody{max-width:88%}.dsgc-constraints{width:88%;max-width:88%}}',
273
334
  /* ===== 侧边栏入口对齐(外壳将插槽内容包在 panelGlyph span 内,必须用后代 :has) ===== */
274
335
  '[class*="panelList"]:has(.dsgc-entryOverlay){margin-top:4px}',
275
336
  '[class*="panelRow"]:has(.dsgc-entryOverlay){position:relative;box-sizing:border-box;height:36px;min-height:36px;padding:0 10px;font-size:13px;transition:background-color .12s,color .12s}',
@@ -292,7 +353,7 @@ export const CSS = [
292
353
  '.dsgc-entryIcon svg{width:18px;height:18px;display:block}',
293
354
  '[data-sidebar-collapsed] .dsgc-entryOverlay,[class*="collapsed"] .dsgc-entryOverlay{justify-content:center;padding:0}',
294
355
  '[data-sidebar-collapsed] [class*="panelRow"]:has(.dsgc-entryOverlay),[class*="collapsed"] [class*="panelRow"]:has(.dsgc-entryOverlay){margin:0 auto}',
295
- '@media (prefers-reduced-motion:reduce){.dsgc-dot,.dsgc-partchip,.dsgc-mentionitem,.dsgc-grow-row,.dsgc-sess-row,.dsgc-opbtn,.dsgc-addsess,.dsgc-topic,.dsgc-role,.dsgc-rename,.dsgc-input,.dsgc-select,.dsgc-textarea,.dsgc-fbrow,.dsgc-twist svg,.dsgc-roleops,.dsgc-roundbtn,.dsgc-permtrigger,.dsgc-permchevron,.dsgc-tobtn,.dsgc-seambtn,.dsgc-aside,.dsgc-aside.closed,.dsgc-nav,.dsgc-nav.closed,.dsgc-clearbtn,[class*="panelRow"]:has(.dsgc-entryOverlay){transition:none}.dsgc-dot:hover{transform:none}.dsgc-drawer,.dsgc-msg,.dsgc-sysmsg,.dsgc-mention,.dsgc-tobottom,.dsgc-err,.dsgc-empty,.dsgc-fb,.dsgc-think .dsgc-thinkbody,.dsgc-msg.live .dsgc-avatar,.dsgc-loading svg,.dsgc-pendingdots i,.dsgc-typing{animation:none}.dsgc-typing{background-position:0 0;background-size:100% 100%}}',
356
+ '@media (prefers-reduced-motion:reduce){.dsgc-dot,.dsgc-partchip,.dsgc-mentionitem,.dsgc-grow-row,.dsgc-sess-row,.dsgc-opbtn,.dsgc-addsess,.dsgc-topic,.dsgc-role,.dsgc-rename,.dsgc-input,.dsgc-select,.dsgc-textarea,.dsgc-fbrow,.dsgc-twist svg,.dsgc-roleops,.dsgc-roundbtn,.dsgc-permtrigger,.dsgc-permchevron,.dsgc-tobtn,.dsgc-seambtn,.dsgc-aside,.dsgc-aside.closed,.dsgc-nav,.dsgc-nav.closed,.dsgc-clearbtn,.dsgc-cmore,.dsgc-msgops,.dsgc-msgop,.dsgc-failmore,.dsgc-clip::before,.dsgc-clip::after,.dsgc-fold,.dsgc-fold-inner,.dsgc-fold.open,.dsgc-fold.open .dsgc-fold-inner,[class*="panelRow"]:has(.dsgc-entryOverlay){transition:none}.dsgc-dot:hover{transform:none}.dsgc-drawer,.dsgc-msg,.dsgc-sysmsg,.dsgc-mention,.dsgc-tobottom,.dsgc-err,.dsgc-empty,.dsgc-fb,.dsgc-msg.live .dsgc-avatar,.dsgc-loading svg,.dsgc-pendingdots i,.dsgc-typing,.dsgc-constraints,.dsgc-hovertip,.dsgc-fail{animation:none}.dsgc-typing{background-position:0 0;background-size:100% 100%}}',
296
357
  /* ===== 设置页(同一卡片语言) ===== */
297
358
  '.dgcs-page{display:flex;flex-direction:column;gap:14px;padding:4px 0}',
298
359
  '.dgcs-head{display:flex;flex-direction:column;gap:6px}',
@@ -1,6 +1,8 @@
1
1
  /** Platform modules (react-dom / client) have no @types in this plugin. */
2
2
  declare module 'react-dom' {
3
+ import type { ReactNode } from 'react'
3
4
  export function flushSync(fn: () => void): void
5
+ export function createPortal(children: ReactNode, container: Element | DocumentFragment): ReactNode
4
6
  }
5
7
 
6
8
  declare module 'react-dom/client' {
@@ -57,10 +57,10 @@ export function serializeInput(root: HTMLElement): string {
57
57
  return Array.from(root.childNodes).map(walk).join('')
58
58
  }
59
59
 
60
- /** @角色芯片的 HTML(原子元素:contenteditable=false + draggable,退格整删)。 */
61
- export function chipHtml(role: { id: string, name: string, color?: string }): string {
60
+ /** @角色芯片的 HTML(原子元素:contenteditable=false + draggable,退格整删;fresh=插入后需营救选区,加 data-new 标记)。 */
61
+ export function chipHtml(role: { id: string, name: string, color?: string }, fresh = false): string {
62
62
  const c = escapeHtml(role.color || '#888')
63
- return '<span class="dsgc-chipin" data-role-id="' + escapeHtml(role.id) + '" data-name="' + escapeHtml(role.name) + '" style="--role-color:' + c + '" contenteditable="false" draggable="true">' +
63
+ return '<span class="dsgc-chipin"' + (fresh ? ' data-new=""' : '') + ' data-role-id="' + escapeHtml(role.id) + '" data-name="' + escapeHtml(role.name) + '" style="--role-color:' + c + '" contenteditable="false" draggable="true">' +
64
64
  '<span class="dsgc-chipdot" style="background:' + c + '"></span>' + escapeHtml(role.name) + '</span>'
65
65
  }
66
66
 
@@ -81,11 +81,11 @@ function fileTypeIconMarkup(path: string, kind: 'file' | 'directory'): string {
81
81
  }
82
82
  }
83
83
 
84
- /** @文件芯片的 HTML(原子元素:contenteditable=false + draggable,退格整删)。 */
85
- export function fileChipHtml(path: string, kind: 'file' | 'directory'): string {
84
+ /** @文件芯片的 HTML(原子元素:contenteditable=false + draggable,退格整删;fresh 同 chipHtml)。 */
85
+ export function fileChipHtml(path: string, kind: 'file' | 'directory', fresh = false): string {
86
86
  const basename = path.split('/').pop() || path
87
87
  const dir = kind === 'directory'
88
- return '<span class="dsgc-chipin dsgc-chipin-file" data-kind="file"' + (dir ? ' data-dir="1"' : '') + ' data-path="' + escapeHtml(path) + '" contenteditable="false" draggable="true">' +
88
+ return '<span class="dsgc-chipin dsgc-chipin-file"' + (fresh ? ' data-new=""' : '') + ' data-kind="file"' + (dir ? ' data-dir="1"' : '') + ' data-path="' + escapeHtml(path) + '" contenteditable="false" draggable="true">' +
89
89
  '<span class="dsgc-chipglyph" aria-hidden="true">' + fileTypeIconMarkup(path, kind) + '</span>' + escapeHtml(basename) + '</span>'
90
90
  }
91
91
 
@@ -0,0 +1,204 @@
1
+ /**
2
+ * 会话约束备忘纯逻辑:滑动 40 条窗口、挤出集、压行、临时原文、解析与截断。
3
+ * @module dsh-group-chat/core/constraints
4
+ */
5
+
6
+ import { looseJson } from './json.ts'
7
+ import { TRANSCRIPT_TOOL_SUMMARY } from './tools.ts'
8
+ import { asConstraintKind, type MessageRecord, type SessionConstraint, type SessionRecord } from './types.ts'
9
+
10
+ /** 当场原文窗口(含系统行)。 */
11
+ export const WINDOW_SIZE = 40
12
+ /** 折叠失败时临时原文条数上限。 */
13
+ export const TEMP_MAX_MESSAGES = 20
14
+ /** 折叠失败时临时原文总长上限。 */
15
+ export const TEMP_MAX_CHARS = 16000
16
+ /** 单轮折叠最多消耗的挤出条数(含系统行;超出留待下一轮)。 */
17
+ export const FOLD_MAX_MESSAGES = 40
18
+ /** 单轮折叠输入总长上限(格式化后)。 */
19
+ export const FOLD_MAX_CHARS = 16000
20
+ /** 备忘条数上限。 */
21
+ export const CONSTRAINT_MAX_ITEMS = 12
22
+ /** 备忘总长上限(条目前缀+正文)。 */
23
+ export const CONSTRAINT_MAX_CHARS = 1200
24
+ /** 单条备忘正文上限。 */
25
+ export const CONSTRAINT_ITEM_MAX_CHARS = 160
26
+
27
+ export const KIND_LABEL: Record<SessionConstraint['kind'], string> = {
28
+ decided: '已定',
29
+ rejected: '否决',
30
+ open: '未决',
31
+ }
32
+
33
+ /** 已折入水位(缺省 0)。 */
34
+ export function constraintsWatermark(sess: SessionRecord): number {
35
+ return typeof sess.constraintsUpToSeq === 'number' && sess.constraintsUpToSeq > 0 ? sess.constraintsUpToSeq : 0
36
+ }
37
+
38
+ /** 重试:只取失败卡之前的时间线;untilId 不在列表则原样。 */
39
+ export function prefixIds(ids: string[], untilId?: string): string[] {
40
+ if (!untilId) return ids
41
+ const i = ids.indexOf(untilId)
42
+ return i >= 0 ? ids.slice(0, i) : ids
43
+ }
44
+
45
+ /**
46
+ * 新挤出:seq > 水位 且不在最近 40 条。只扫窗口外前缀(旧→新)。
47
+ * untilId:重试时把窗口截到该消息之前,不带上后面已经发生的发言。
48
+ */
49
+ export function squeezedMessages(
50
+ messages: Map<string, MessageRecord>,
51
+ sess: SessionRecord,
52
+ untilId?: string,
53
+ ): MessageRecord[] {
54
+ const ids = prefixIds(sess.messageIds, untilId)
55
+ if (ids.length <= WINDOW_SIZE) return []
56
+ const end = ids.length - WINDOW_SIZE
57
+ const upTo = constraintsWatermark(sess)
58
+ const last = messages.get(ids[end - 1])
59
+ if (last && last.seq <= upTo) return []
60
+ const out: MessageRecord[] = []
61
+ for (let i = 0; i < end; i++) {
62
+ const m = messages.get(ids[i])
63
+ if (!m || m.seq <= upTo) continue
64
+ out.push(m)
65
+ }
66
+ return out
67
+ }
68
+
69
+ /** 说话人展示名(折叠输入 / transcript 共用)。 */
70
+ export function speakerLabel(speaker: string, roleName?: string): string {
71
+ if (speaker === 'user') return '用户'
72
+ if (speaker === 'system') return '系统'
73
+ return roleName || '成员'
74
+ }
75
+
76
+ /** 单条消息压成 transcript 行(正文 8k + 工具一行摘要)。 */
77
+ export function formatTranscriptLine(m: MessageRecord, name: string): string {
78
+ let text = m.text || ''
79
+ if (text.length > 8000) text = text.slice(0, 8000) + '…(已截断)'
80
+ let line = '【' + name + '】' + text
81
+ if (Array.isArray(m.toolCalls)) {
82
+ for (const c of m.toolCalls) {
83
+ if (!c || typeof c.tool !== 'string') continue
84
+ let brief = ''
85
+ try {
86
+ brief = JSON.stringify(c.args) || ''
87
+ } catch {
88
+ brief = ''
89
+ }
90
+ if (brief.length > 60) brief = brief.slice(0, 60) + '…'
91
+ const st = c.status === 'ok' ? '成功' : c.status === 'denied' ? '用户拒绝' : '失败'
92
+ let ob = String(c.output || '')
93
+ if (ob.length > TRANSCRIPT_TOOL_SUMMARY) ob = ob.slice(0, TRANSCRIPT_TOOL_SUMMARY) + '…'
94
+ line += '\n [工具] ' + c.tool + ' ' + brief + ' → ' + st + (ob ? '(' + ob.replace(/\s+/g, ' ') + ')' : '')
95
+ }
96
+ }
97
+ return line
98
+ }
99
+
100
+ /**
101
+ * 单轮折叠消耗前缀:从最旧挤出起,最多 40 条 / 16k;系统行与失败卡计入消耗但不进模型。
102
+ * 水位只能推到 consumed 的 max seq,剩余留待下一轮。
103
+ */
104
+ export function takeFoldBatch(squeezed: MessageRecord[], nameOf: (m: MessageRecord) => string): {
105
+ consumed: MessageRecord[]
106
+ lines: string[]
107
+ hasUser: boolean
108
+ allSystem: boolean
109
+ } {
110
+ const consumed: MessageRecord[] = []
111
+ const lines: string[] = []
112
+ let hasUser = false
113
+ let chars = 0
114
+ for (const m of squeezed) {
115
+ if (m.speaker === 'system' || m.error) {
116
+ consumed.push(m)
117
+ if (consumed.length >= FOLD_MAX_MESSAGES) break
118
+ continue
119
+ }
120
+ const line = formatTranscriptLine(m, nameOf(m))
121
+ const extra = line.length + (lines.length ? 2 : 0)
122
+ if (lines.length > 0 && chars + extra > FOLD_MAX_CHARS) break
123
+ if (m.speaker === 'user') hasUser = true
124
+ lines.push(line)
125
+ chars += extra
126
+ consumed.push(m)
127
+ if (consumed.length >= FOLD_MAX_MESSAGES) break
128
+ }
129
+ return { consumed, lines, hasUser, allSystem: lines.length === 0 }
130
+ }
131
+
132
+ /**
133
+ * 未折入的挤出原文(失败缓冲):只格式化最近 20 条,再按 16k 从最旧往下丢。
134
+ */
135
+ export function tempTranscript(squeezed: MessageRecord[], nameOf: (m: MessageRecord) => string): string {
136
+ if (!squeezed.length) return ''
137
+ const usable = squeezed.filter((m) => m.speaker !== 'system' && !m.error)
138
+ if (!usable.length) return ''
139
+ const batch = usable.length > TEMP_MAX_MESSAGES ? usable.slice(-TEMP_MAX_MESSAGES) : usable
140
+ const lines = batch.map((m) => formatTranscriptLine(m, nameOf(m)))
141
+ let start = 0
142
+ let total = lines[0] ? lines[0].length : 0
143
+ for (let i = 1; i < lines.length; i++) {
144
+ total += 2 + lines[i].length
145
+ }
146
+ while (start < lines.length - 1 && total > TEMP_MAX_CHARS) {
147
+ total -= lines[start].length + 2
148
+ start++
149
+ }
150
+ let block = lines.slice(start).join('\n\n')
151
+ if (block.length > TEMP_MAX_CHARS) block = block.slice(0, TEMP_MAX_CHARS) + '…(已截断)'
152
+ return block
153
+ }
154
+
155
+ /** 水位 = 本批挤出的 max(seq);空批为 0。 */
156
+ export function squeezedMaxSeq(squeezed: MessageRecord[]): number {
157
+ let max = 0
158
+ for (const m of squeezed) if (m.seq > max) max = m.seq
159
+ return max
160
+ }
161
+
162
+ /** hydrate / 模型输出:非法 kind 丢条目;空 text 丢;条数与总长截断。 */
163
+ export function sanitizeConstraints(raw: unknown): SessionConstraint[] {
164
+ if (!Array.isArray(raw)) return []
165
+ const out: SessionConstraint[] = []
166
+ let total = 0
167
+ for (const item of raw) {
168
+ if (out.length >= CONSTRAINT_MAX_ITEMS) break
169
+ if (!item || typeof item !== 'object') continue
170
+ const kind = asConstraintKind((item as { kind?: unknown }).kind)
171
+ const text = typeof (item as { text?: unknown }).text === 'string' ? (item as { text: string }).text.trim() : ''
172
+ if (!kind || !text) continue
173
+ const clipped = text.length > CONSTRAINT_ITEM_MAX_CHARS ? text.slice(0, CONSTRAINT_ITEM_MAX_CHARS) + '…' : text
174
+ const cost = KIND_LABEL[kind].length + clipped.length
175
+ if (total + cost > CONSTRAINT_MAX_CHARS) break
176
+ out.push({ kind, text: clipped })
177
+ total += cost
178
+ }
179
+ return out
180
+ }
181
+
182
+ /**
183
+ * 解析折叠模型输出。null = 解析失败(水位不推);[] = 无新结论(水位推、备忘不动)。
184
+ */
185
+ export function parseConstraints(raw: string): SessionConstraint[] | null {
186
+ const o = looseJson(raw.replace(/```(?:json)?/g, ''))
187
+ if (o === null) return null
188
+ if (!Object.prototype.hasOwnProperty.call(o, 'constraints')) return null
189
+ if (!Array.isArray(o.constraints)) return null
190
+ return sanitizeConstraints(o.constraints)
191
+ }
192
+
193
+ /** 本批无用户消息时,新的已定/否决降为未决。 */
194
+ export function downgradeWithoutUser(list: SessionConstraint[], hasUser: boolean): SessionConstraint[] {
195
+ if (hasUser) return list
196
+ return list.map((c) => c.kind === 'open' ? c : { kind: 'open', text: c.text })
197
+ }
198
+
199
+ /** system 提示词「已确认约束」块;空则空串。 */
200
+ export function constraintBlock(list: SessionConstraint[] | undefined): string {
201
+ if (!list || !list.length) return ''
202
+ const lines = list.map((c) => '- ' + KIND_LABEL[c.kind] + ':' + c.text)
203
+ return '\n# 已确认约束\n' + lines.join('\n')
204
+ }