@allbluecn/web-app 0.27.21 → 0.27.22

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@allbluecn/web-app",
3
- "version": "0.27.21",
3
+ "version": "0.27.22",
4
4
  "license": "AGPL-3.0-or-later",
5
5
  "type": "module",
6
6
  "files": [
@@ -61,12 +61,14 @@ beforeEach(() => {
61
61
  describe('useHarnessSession putMedia 接线(T2.11 ④)', () => {
62
62
  it('暴露的 putMedia 转发到 client.putMedia,返回 MediaRecord', async () => {
63
63
  const { result } = renderHook(() => useHarnessSession({ threadId: 't1' }))
64
- expect(typeof result.current.putMedia).toBe('function')
64
+ const putMedia = result.current.putMedia
65
+ expect(typeof putMedia).toBe('function')
65
66
 
66
67
  const file = { body: new Blob(['x']), mimeType: 'image/png', name: 'cat.png' }
67
68
  let record: unknown
68
69
  await act(async () => {
69
- record = await result.current.putMedia(file)
70
+ // putMedia 仅在有 threadId(client 存在)时定义;本用例 threadId='t1'
71
+ record = await putMedia?.(file)
70
72
  })
71
73
 
72
74
  expect(putMediaMock).toHaveBeenCalledWith(file)
@@ -233,8 +233,10 @@ export function useHarnessChatAdapter(session: HarnessSession): HarnessChatLike
233
233
 
234
234
  const [error, setError] = useState<Error | null>(null)
235
235
 
236
- // 订阅 view 错误事件(harness 无 error 状态字段,经 on('error') 捕获)
236
+ // 订阅 view 错误事件(harness 无 error 状态字段,经 on('error') 捕获)。
237
+ // 空态 view=null(无连接,见 use-harness-session 空态注释):跳过订阅。
237
238
  useEffect(() => {
239
+ if (!view) return
238
240
  const off = view.on('error', (message: string) => {
239
241
  setError(new Error(message))
240
242
  })
@@ -299,7 +301,7 @@ export function useHarnessChatAdapter(session: HarnessSession): HarnessChatLike
299
301
  // AppChat:harness 消息流渲染组件(吃原始 ViewMessage)。用 createElement 而非
300
302
  // JSX,保持本文件为纯 TS 数据层(无 JSX 语法)。
301
303
  const AppChat = useMemo<ComponentType<Record<string, never>>>(
302
- () => () => createElement(HarnessSessionAppChat, { view }),
304
+ () => () => (view ? createElement(HarnessSessionAppChat, { view }) : null),
303
305
  [view],
304
306
  )
305
307
 
@@ -19,12 +19,12 @@
19
19
  // client-tools-dispatcher、queue-bar 消费。
20
20
 
21
21
  import { useCallback, useMemo } from 'react'
22
- import { useStore } from '@tanstack/react-store'
22
+ import { createStore, useStore } from '@tanstack/react-store'
23
23
  import { createHarnessClient } from '@tanstack/ai-harness/client'
24
24
  import { createSessionView } from '@tanstack/ai-harness/view'
25
25
  import type { AnyHarness } from '@tanstack/ai-harness'
26
26
  import type { MediaRecord } from '@tanstack/ai-harness/client'
27
- import type { ViewMessage } from '@tanstack/ai-harness/view'
27
+ import type { SessionViewState, ViewMessage } from '@tanstack/ai-harness/view'
28
28
 
29
29
  /** harness 会话 hook 的参数。threadId 由侧栏/工作台以 ChatThread 单一真源提供。 */
30
30
  export interface UseHarnessSessionOptions {
@@ -47,41 +47,85 @@ export function useHarnessSession({ threadId }: UseHarnessSessionOptions) {
47
47
  // createSessionView 自身处理重连/gap/媒体 URL 刷新,无需在此重试。
48
48
  // client 一并暴露:putMedia(附件上传,T2.11 ④)与 hostEvents(title 写回,
49
49
  // T2.11 ②)都在 client 上,view 不转发这两个能力。
50
+ // client + view 稳定引用:threadId 变化才重建(view 持有 SSE 连接与 store)。
51
+ // createSessionView 自身处理重连/gap/媒体 URL 刷新,无需在此重试。
52
+ // client 一并暴露:putMedia(附件上传,T2.11 ④)与 hostEvents(title 写回,
53
+ // T2.11 ②)都在 client 上,view 不转发这两个能力。
54
+ //
55
+ // 空态(无 threadId)不建 client/view:官方 HarnessClientOptions.threadId 必填,
56
+ // 缺省时 client.events() 的请求会被服务端 400 拒(threadId is required),重连
57
+ // 退避耗尽 → 「连接已断开 + 重试次数已达上限」假故障(桌面真机 CDP 实测)。
58
+ // 首条发送走 Provider 建会话(createConversationFn)→ pendingSend 槽,threadId
59
+ // 就位后本 useMemo 以新 threadId 重建并连接——空态动作均 no-op(不会被调用)。
50
60
  const { client, view } = useMemo(() => {
61
+ if (!threadId) return { client: null, view: null }
51
62
  const client = createHarnessClient<AnyHarness>({ url: '/api/harness', threadId })
52
63
  return { client, view: createSessionView(client) }
53
64
  }, [threadId])
54
65
 
66
+ // 空态占位 store:与 view.store 同型(SessionViewState)的全空值(hooks 规则——
67
+ // useStore 必须无条件调用;类型对齐否则 useStore 泛型在 store 联合上坍缩)。
68
+ const EMPTY_STORE = useMemo(
69
+ () =>
70
+ createStore<SessionViewState>({
71
+ threadId: '',
72
+ status: 'idle',
73
+ connection: 'closed',
74
+ messages: [],
75
+ approvals: [],
76
+ clientTools: [],
77
+ interrupts: [],
78
+ questions: [],
79
+ signIns: [],
80
+ waits: [],
81
+ agents: [],
82
+ queuedTurns: 0,
83
+ waitingInputs: [],
84
+ commands: [],
85
+ config: [],
86
+ tools: [],
87
+ models: [],
88
+ plugins: {},
89
+ }),
90
+ [],
91
+ )
92
+ const store = view?.store ?? EMPTY_STORE
93
+
55
94
  // live 状态切片:useStore 订阅 view.store(TanStack Store),按字段选择避免全量重渲染
56
- const messages = useStore(view.store, (s) => s.messages)
57
- const status = useStore(view.store, (s) => s.status)
58
- const connection = useStore(view.store, (s) => s.connection)
59
- const approvals = useStore(view.store, (s) => s.approvals)
60
- const interrupts = useStore(view.store, (s) => s.interrupts)
61
- const questions = useStore(view.store, (s) => s.questions)
62
- const clientTools = useStore(view.store, (s) => s.clientTools)
63
- const queuedTurns = useStore(view.store, (s) => s.queuedTurns)
64
- const waitingInputs = useStore(view.store, (s) => s.waitingInputs)
95
+ const messages = useStore(store, (s) => s.messages)
96
+ const status = useStore(store, (s) => s.status)
97
+ const connection = useStore(store, (s) => s.connection)
98
+ const approvals = useStore(store, (s) => s.approvals)
99
+ const interrupts = useStore(store, (s) => s.interrupts)
100
+ const questions = useStore(store, (s) => s.questions)
101
+ const clientTools = useStore(store, (s) => s.clientTools)
102
+ const queuedTurns = useStore(store, (s) => s.queuedTurns)
103
+ const waitingInputs = useStore(store, (s) => s.waitingInputs)
65
104
 
66
- // 动作:view.send 返回 Promise<void>;approve/reject 是同步 fire(内部 resolve)
105
+ // 动作:view.send 返回 Promise<void>;approve/reject 是同步 fire(内部 resolve)。
106
+ // 空态(view=null)no-op——首条发送由 Provider 建会话 + pendingSend 接管。
67
107
  const send = useCallback(
68
- (text: string, attachments?: ReadonlyArray<MediaRecord>) => view.send(text, attachments),
108
+ (text: string, attachments?: ReadonlyArray<MediaRecord>) => view?.send(text, attachments),
69
109
  [view],
70
110
  )
71
111
  const approve = useCallback((id: string, editedArgs?: unknown) => {
112
+ if (!view) return
72
113
  view.approve(editedArgs === undefined ? { id } : { id, editedArgs })
73
114
  }, [view])
74
115
  const reject = useCallback((id: string) => {
75
- view.reject({ id })
116
+ view?.reject({ id })
76
117
  }, [view])
77
- const cancel = useCallback(() => view.cancel(), [view])
118
+ const cancel = useCallback(() => view?.cancel(), [view])
78
119
 
79
120
  // 附件上传器(T2.11 ④):harness client.putMedia 暴露给 composer 的 useAttachments。
80
121
  // 用 client.putMedia(接受 Blob)而非 view.putMedia(MediaBody 仅 ArrayBuffer|Uint8Array,
81
122
  // 见 view/index.d.ts:101),与 use-attachments 的 File/Blob 通道类型对齐。
82
- // 签名与 useAttachments 的 putMedia 上传器一致:{ body: Blob; mimeType; name } → MediaRecord。
83
- const putMedia = useCallback(
84
- (file: { body: Blob; mimeType: string; name: string }) => client.putMedia(file),
123
+ // 空态无 client → undefined(composer useAttachments 回落 blobRef 旧链路径)。
124
+ const putMedia = useMemo(
125
+ () =>
126
+ client
127
+ ? (file: { body: Blob; mimeType: string; name: string }) => client.putMedia(file)
128
+ : undefined,
85
129
  [client],
86
130
  )
87
131
 
@@ -112,8 +112,8 @@ export const SDK_GROUPS: SdkGroup[] = [
112
112
  "items": [
113
113
  {
114
114
  "name": "@allbluecn/web-app",
115
- "version": "v0.27.21",
116
- "lastUpdate": "Nov 9, 2026",
115
+ "version": "v0.27.22",
116
+ "lastUpdate": "Nov 10, 2026",
117
117
  "source": "apps/web/package.json",
118
118
  "docsUrl": "https://github.com/allbluecn/allblue"
119
119
  },