@allbluecn/web-app 0.27.19 → 0.27.21
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 +1 -1
- package/src/components/billing/__tests__/use-feature.test.tsx +85 -0
- package/src/components/billing/use-feature.ts +18 -1
- package/src/lib/changelog/sdk-versions.ts +1 -1
- package/src/routes/api/harness/$.ts +48 -32
- package/src/server/harness/__tests__/media-secret.test.ts +47 -0
- package/src/server/harness/host.server.ts +27 -0
package/package.json
CHANGED
|
@@ -10,8 +10,14 @@ vi.mock('@/lib/auth/auth-client', () => ({
|
|
|
10
10
|
useSession: vi.fn(),
|
|
11
11
|
}))
|
|
12
12
|
|
|
13
|
+
// 桌面运行时判定(Bug#2:桌面未登录 session=null 时 Pro 功能需 fail-closed)
|
|
14
|
+
vi.mock('@/lib/desktop-env', () => ({
|
|
15
|
+
isDesktopClient: vi.fn(() => false),
|
|
16
|
+
}))
|
|
17
|
+
|
|
13
18
|
import { useSubscription } from '../subscription-provider'
|
|
14
19
|
import { useSession } from '@/lib/auth/auth-client'
|
|
20
|
+
import { isDesktopClient } from '@/lib/desktop-env'
|
|
15
21
|
|
|
16
22
|
const mockUseSubscription = useSubscription as ReturnType<typeof vi.fn>
|
|
17
23
|
const mockUseSession = useSession as ReturnType<typeof vi.fn>
|
|
@@ -97,4 +103,83 @@ describe('useFeature', () => {
|
|
|
97
103
|
const { result } = renderHook(() => useFeature('hostedAi'))
|
|
98
104
|
expect(result.current.enabled).toBe(false)
|
|
99
105
|
})
|
|
106
|
+
|
|
107
|
+
// ===== tier-oss(D5 完全开放,T0.1):与登录态/订阅态无关,任何状态直接放行 =====
|
|
108
|
+
// 桌面真机验收 Bug#2 正解:guest/未登录不得把已开放能力误锁成「升级到 Pro」。
|
|
109
|
+
|
|
110
|
+
it('tier-oss 特性(sandboxExecution)guest 也放行(D5 完全开放)', () => {
|
|
111
|
+
mockUseSession.mockReturnValue({ data: { user: { id: 'local-guest' } } })
|
|
112
|
+
mockUseSubscription.mockReturnValue({
|
|
113
|
+
planName: 'open-source',
|
|
114
|
+
features: {},
|
|
115
|
+
unavailable: [],
|
|
116
|
+
loading: false,
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
const { result } = renderHook(() => useFeature('sandboxExecution'))
|
|
120
|
+
expect(result.current.enabled).toBe(true)
|
|
121
|
+
expect(result.current.requiresLogin).toBeUndefined()
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
it('tier-oss 特性桌面未登录(session=null)也放行', () => {
|
|
125
|
+
vi.mocked(isDesktopClient).mockReturnValue(true)
|
|
126
|
+
mockUseSession.mockReturnValue({ data: null })
|
|
127
|
+
mockUseSubscription.mockReturnValue({
|
|
128
|
+
planName: 'open-source',
|
|
129
|
+
features: {},
|
|
130
|
+
unavailable: [],
|
|
131
|
+
loading: false,
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
const { result } = renderHook(() => useFeature('mediaGeneration'))
|
|
135
|
+
expect(result.current.enabled).toBe(true)
|
|
136
|
+
expect(result.current.requiresLogin).toBeUndefined()
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
// ===== Pro-tier 特性:桌面未登录/guest 仍 fail-closed(#648 语义保留)=====
|
|
140
|
+
|
|
141
|
+
it('Pro-tier 特性桌面未登录(session=null)→ requiresLogin(fail-closed)', () => {
|
|
142
|
+
vi.mocked(isDesktopClient).mockReturnValue(true)
|
|
143
|
+
mockUseSession.mockReturnValue({ data: null })
|
|
144
|
+
mockUseSubscription.mockReturnValue({
|
|
145
|
+
planName: 'open-source',
|
|
146
|
+
features: { hostedAi: false },
|
|
147
|
+
unavailable: [],
|
|
148
|
+
loading: false,
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
const { result } = renderHook(() => useFeature('hostedAi'))
|
|
152
|
+
expect(result.current.enabled).toBe(false)
|
|
153
|
+
expect(result.current.requiresLogin).toBe(true)
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
it('Pro-tier 特性 guest → requiresLogin', () => {
|
|
157
|
+
mockUseSession.mockReturnValue({ data: { user: { id: 'local-guest' } } })
|
|
158
|
+
mockUseSubscription.mockReturnValue({
|
|
159
|
+
planName: 'open-source',
|
|
160
|
+
features: { hostedAi: false },
|
|
161
|
+
unavailable: [],
|
|
162
|
+
loading: false,
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
const { result } = renderHook(() => useFeature('hostedAi'))
|
|
166
|
+
expect(result.current.enabled).toBe(false)
|
|
167
|
+
expect(result.current.requiresLogin).toBe(true)
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
// 云/web 未登录不收敛(营销态可展示 Pro 入口引导注册),仅桌面壳内 fail-closed
|
|
171
|
+
it('云/web 未登录(session=null,非桌面)→ 不受桌面分支影响', () => {
|
|
172
|
+
vi.mocked(isDesktopClient).mockReturnValue(false)
|
|
173
|
+
mockUseSession.mockReturnValue({ data: null })
|
|
174
|
+
mockUseSubscription.mockReturnValue({
|
|
175
|
+
planName: 'open-source',
|
|
176
|
+
features: { hostedAi: false },
|
|
177
|
+
unavailable: [],
|
|
178
|
+
loading: false,
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
const { result } = renderHook(() => useFeature('hostedAi'))
|
|
182
|
+
expect(result.current.enabled).toBe(false)
|
|
183
|
+
expect(result.current.requiresLogin).toBeUndefined()
|
|
184
|
+
})
|
|
100
185
|
})
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useSubscription } from './subscription-provider'
|
|
2
2
|
import { useSession } from '@/lib/auth/auth-client'
|
|
3
|
-
import {
|
|
3
|
+
import { isDesktopClient } from '@/lib/desktop-env'
|
|
4
|
+
import { isOssFeature, type FeatureKey } from '@allbluecn/kernel'
|
|
4
5
|
|
|
5
6
|
// 桌面访客会话固定 userId(服务端唯一来源 src/server/desktop-guest.ts 的
|
|
6
7
|
// GUEST_USER_ID;客户端组件禁止 import server 模块,故镜像常量,值由
|
|
@@ -23,10 +24,26 @@ export function useFeature(feature: FeatureKey): FeatureState {
|
|
|
23
24
|
return { enabled: false, loading: false, unavailable: true }
|
|
24
25
|
}
|
|
25
26
|
|
|
27
|
+
// tier 'oss' 特性(D5 完全开放,T0.1):免费能力与登录态/订阅态无关,任何状态直接
|
|
28
|
+
// 放行——注册表是唯一权威。先于 guest/桌面未登录判定,否则 guest 会把沙箱档、
|
|
29
|
+
// 媒体生成等已开放能力误锁成「升级到 Pro」(桌面真机验收 Bug#2 正解;#648 的
|
|
30
|
+
// fail-closed 修复只应约束 Pro-tier 特性)。
|
|
31
|
+
if (isOssFeature(feature)) {
|
|
32
|
+
return { enabled: true, loading: false }
|
|
33
|
+
}
|
|
34
|
+
|
|
26
35
|
if (isGuest) {
|
|
27
36
|
return { enabled: false, loading: false, requiresLogin: true }
|
|
28
37
|
}
|
|
29
38
|
|
|
39
|
+
// 桌面未登录(session 仍为 null,尚未回落 guest):与 guest 同为「本机匿名」态,
|
|
40
|
+
// Pro 功能必须 fail-closed,否则 OSS_BASE_FEATURES 白名单会让沙箱档等 Pro 入口
|
|
41
|
+
// 在登录前泄漏(真机验收 Bug#2)。桌面 guest 一旦回落,走上面 isGuest 分支同口径。
|
|
42
|
+
// 云/web 未登录不在此列(营销态可展示 Pro 入口引导注册),仅桌面壳内收敛。
|
|
43
|
+
if (!session && isDesktopClient()) {
|
|
44
|
+
return { enabled: false, loading: false, requiresLogin: true }
|
|
45
|
+
}
|
|
46
|
+
|
|
30
47
|
return {
|
|
31
48
|
enabled: features[feature] === true,
|
|
32
49
|
loading,
|
|
@@ -112,7 +112,7 @@ export const SDK_GROUPS: SdkGroup[] = [
|
|
|
112
112
|
"items": [
|
|
113
113
|
{
|
|
114
114
|
"name": "@allbluecn/web-app",
|
|
115
|
-
"version": "v0.27.
|
|
115
|
+
"version": "v0.27.21",
|
|
116
116
|
"lastUpdate": "Nov 9, 2026",
|
|
117
117
|
"source": "apps/web/package.json",
|
|
118
118
|
"docsUrl": "https://github.com/allbluecn/allblue"
|
|
@@ -40,44 +40,60 @@ async function handleHarnessRequest(request: Request): Promise<Response> {
|
|
|
40
40
|
if (!isHarnessHostEnabled()) return notFound()
|
|
41
41
|
|
|
42
42
|
// server-only 薄适配层经 server.handlers 剥离,客户端不会加载;动态 import 使
|
|
43
|
-
// 门控关闭路径完全不触达 harness
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
43
|
+
// 门控关闭路径完全不触达 harness 装配(避免无谓构造)。整段包顶层 try/catch:
|
|
44
|
+
// 官方 createHarnessHandler 内部虽把 handler 异常转 400,但**动态 import 与 host
|
|
45
|
+
// 首次构造(getHarnessHost → createPrismaPersistence → @allbluecn/database 顶层
|
|
46
|
+
// await createPrismaClient)发生在该内部 try/catch 之前**——桌面真机未登录验收发现
|
|
47
|
+
// 这段抛错(PGlite 未就绪 / 迁移未跑 / env 缺失等)会以裸框架 500 逃逸,客户端
|
|
48
|
+
// describe/snapshot/transcript 三个 GET 全部 `Harness * failed (500)` 且 SSE 重连
|
|
49
|
+
// 退避耗尽 → 「重试次数已达上限」。收敛为结构化 503,不再让装配期故障裸奔。
|
|
50
|
+
try {
|
|
51
|
+
const [{ getHarnessHost, harnessMediaSecret }, { allblueHarness }, { createHarnessHandler }, { getCurrentSession }, { checkAiRateLimit, RateLimitExceededError }] = await Promise.all([
|
|
52
|
+
import('@/server/harness/host.server'),
|
|
53
|
+
import('@/server/harness/define.server'),
|
|
54
|
+
import('@tanstack/ai-harness/server'),
|
|
55
|
+
import('@/lib/session'),
|
|
56
|
+
import('@/server/lib/rate-limit'),
|
|
57
|
+
])
|
|
51
58
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
59
|
+
const url = new URL(request.url)
|
|
60
|
+
const isControl = url.pathname.endsWith('/control')
|
|
61
|
+
if (request.method === 'POST' && isControl) {
|
|
62
|
+
const op = await readControlOp(request)
|
|
63
|
+
if (op && RATE_LIMITED_OPS.has(op)) {
|
|
64
|
+
// 限流身份取 access 层同源会话(client context 不可信,P0-2)
|
|
65
|
+
const session = await getCurrentSession(request)
|
|
66
|
+
const userId = session?.user?.id
|
|
67
|
+
if (userId) {
|
|
68
|
+
try {
|
|
69
|
+
await checkAiRateLimit(userId, 'chat')
|
|
70
|
+
} catch (err) {
|
|
71
|
+
if (err instanceof RateLimitExceededError) {
|
|
72
|
+
return new Response(
|
|
73
|
+
JSON.stringify({ error: 'rate_limited', retryAfterMs: err.retryAfterMs }),
|
|
74
|
+
{ status: 429, headers: { 'content-type': 'application/json', 'retry-after': String(Math.ceil(err.retryAfterMs / 1000)) } },
|
|
75
|
+
)
|
|
76
|
+
}
|
|
77
|
+
throw err
|
|
69
78
|
}
|
|
70
|
-
throw err
|
|
71
79
|
}
|
|
72
80
|
}
|
|
73
81
|
}
|
|
74
|
-
}
|
|
75
82
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
83
|
+
// host 带 access(getCurrentSession 映射 + owner-only + 抢建防护 + acceptInput
|
|
84
|
+
// 可信上下文覆写)。access/acceptInput 已在 getHarnessHost 单例内装配。
|
|
85
|
+
// media 签名密钥:host 带 ownership(Prisma LogStore)时官方强制,缺失即抛错
|
|
86
|
+
// (桌面真机 Bug#1 根因,详见 harnessMediaSecret 注释)。
|
|
87
|
+
const host = getHarnessHost()
|
|
88
|
+
const handler = createHarnessHandler({ host, harness: allblueHarness, media: { secret: harnessMediaSecret() } })
|
|
89
|
+
return await handler(request)
|
|
90
|
+
} catch (error) {
|
|
91
|
+
console.error('[harness] 请求处理失败(host 装配或 handler 异常):', error)
|
|
92
|
+
return new Response(
|
|
93
|
+
JSON.stringify({ error: 'harness_unavailable', message: error instanceof Error ? error.message : String(error) }),
|
|
94
|
+
{ status: 503, headers: { 'content-type': 'application/json' } },
|
|
95
|
+
)
|
|
96
|
+
}
|
|
81
97
|
}
|
|
82
98
|
|
|
83
99
|
export const Route = createFileRoute('/api/harness/$')({
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// AllBlue - 理想之海
|
|
3
|
+
// Copyright (C) 2026 AllBlue Contributors
|
|
4
|
+
//
|
|
5
|
+
// harness 媒体签名密钥(桌面真机 Bug#1 根因修复的契约钉住):
|
|
6
|
+
// host 带 ownership(Prisma LogStore)时官方 createHarnessHandler 强制 media
|
|
7
|
+
// {secret}(http.js:417),缺省即抛错 → /api/harness 全 500。密钥从 AUTH_SECRET
|
|
8
|
+
// 派生:同实例重启后稳定(媒体 URL 跨重启可验证);派生而非复用鉴权密钥本体。
|
|
9
|
+
|
|
10
|
+
import { afterEach, describe, expect, it } from 'vitest'
|
|
11
|
+
import { harnessMediaSecret } from '@/server/harness/host.server'
|
|
12
|
+
|
|
13
|
+
describe('harnessMediaSecret', () => {
|
|
14
|
+
afterEach(() => {
|
|
15
|
+
delete process.env.AUTH_SECRET
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
it('AUTH_SECRET 存在 → 稳定派生(同 env 两次调用一致)', () => {
|
|
19
|
+
process.env.AUTH_SECRET = 'secret-a'
|
|
20
|
+
const a1 = harnessMediaSecret()
|
|
21
|
+
const a2 = harnessMediaSecret()
|
|
22
|
+
expect(a1).toBe(a2)
|
|
23
|
+
expect(a1).toHaveLength(64) // sha256 hex
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
it('AUTH_SECRET 变化 → 密钥变化(域分离派生随鉴权密钥轮换)', () => {
|
|
27
|
+
process.env.AUTH_SECRET = 'secret-a'
|
|
28
|
+
const a = harnessMediaSecret()
|
|
29
|
+
process.env.AUTH_SECRET = 'secret-b'
|
|
30
|
+
const b = harnessMediaSecret()
|
|
31
|
+
expect(a).not.toBe(b)
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it('AUTH_SECRET 缺失 → 回落进程内随机密钥(进程内稳定,跨进程不同)', async () => {
|
|
35
|
+
delete process.env.AUTH_SECRET
|
|
36
|
+
const warn = console.warn
|
|
37
|
+
console.warn = () => {}
|
|
38
|
+
try {
|
|
39
|
+
const e1 = harnessMediaSecret()
|
|
40
|
+
const e2 = harnessMediaSecret()
|
|
41
|
+
expect(e1).toBe(e2)
|
|
42
|
+
expect(e1).toHaveLength(64)
|
|
43
|
+
} finally {
|
|
44
|
+
console.warn = warn
|
|
45
|
+
}
|
|
46
|
+
})
|
|
47
|
+
})
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
// access.server.ts 落地;本文件先用最小 owner-only 闭合(防无界访问),T2.3 细化。
|
|
20
20
|
|
|
21
21
|
import { createHarnessHost, type AcceptInput, type AnyHarness, type HarnessAccess, type HarnessHost, type HarnessPersistence, type OwnershipOptions } from '@tanstack/ai-harness'
|
|
22
|
+
import { createHash, randomBytes } from 'node:crypto'
|
|
22
23
|
|
|
23
24
|
import { isHarnessHostEnabled } from '@/server/harness/gate.server'
|
|
24
25
|
import { allblueHarness } from '@/server/harness/define.server'
|
|
@@ -119,6 +120,32 @@ export function getHarnessHost(): HarnessHost<HarnessHostUser> {
|
|
|
119
120
|
return host
|
|
120
121
|
}
|
|
121
122
|
|
|
123
|
+
/**
|
|
124
|
+
* harness 媒体 URL 签名密钥(官方 HarnessHandlerOptions.media):http.js:417 强制——
|
|
125
|
+
* host 带 ownership(Prisma LogStore 按 conformance 契约提供 acquire/renew 等方法,
|
|
126
|
+
* isSharedHost 判真)时 createHarnessHandler 必须显式传 media {secret}|{keys},
|
|
127
|
+
* 否则构造即抛错,/api/harness 全部请求 500(桌面真机验收 Bug#1 根因,已由
|
|
128
|
+
* #648 收敛为 503;本函数是其根因修复)。
|
|
129
|
+
*
|
|
130
|
+
* 取 AUTH_SECRET 派生(sha256 域分离,不复用鉴权密钥本体):桌面壳由 Electron
|
|
131
|
+
* app-config.json 注入(每安装稳定),web dev/生产走 env——同实例重启后旧签名仍可
|
|
132
|
+
* 验证。AUTH_SECRET 缺失时回落进程内随机密钥并告警(=官方无 ownership 时的随机
|
|
133
|
+
* 签名语义:重启后旧媒体 URL 失效,仅影响媒体时效,不影响会话)。
|
|
134
|
+
*/
|
|
135
|
+
let ephemeralMediaSecret: string | undefined
|
|
136
|
+
|
|
137
|
+
export function harnessMediaSecret(): string {
|
|
138
|
+
const authSecret = process.env.AUTH_SECRET
|
|
139
|
+
if (authSecret) {
|
|
140
|
+
return createHash('sha256').update(`allblue:harness-media:${authSecret}`).digest('hex')
|
|
141
|
+
}
|
|
142
|
+
ephemeralMediaSecret ??= randomBytes(32).toString('hex')
|
|
143
|
+
console.warn(
|
|
144
|
+
'[harness] AUTH_SECRET 未设置:媒体签名密钥回落进程内随机值,重启后已签发的媒体 URL 将失效。',
|
|
145
|
+
)
|
|
146
|
+
return ephemeralMediaSecret
|
|
147
|
+
}
|
|
148
|
+
|
|
122
149
|
/**
|
|
123
150
|
* 关闭并清除 host 单例(测试 / 桌面退出用)。
|
|
124
151
|
*
|