@tbox.cn/app-template-agent 0.10.30 → 1.0.0-alpha.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.
- package/.tbox/app.json +3 -19
- package/.tbox/default-modules.json +4 -1
- package/.tbox/miniapp-containers.json +1 -1
- package/AGENTS.md +52 -148
- package/README.md +10 -8
- package/apps/AGENTS.md +13 -0
- package/apps/client/.tbox-gen/app-kind.ts +1 -0
- package/apps/client/.tbox-gen/documents/_tbox/card-preview.html +38 -0
- package/apps/client/.tbox-gen/documents/index.html +39 -0
- package/apps/client/.tbox-gen/entries/card-preview.tsx +18 -0
- package/apps/client/.tbox-gen/entries/index.tsx +18 -0
- package/apps/client/.tbox-gen/manifest.json +17 -0
- package/apps/client/.tbox-gen/registry.client.ts +6 -0
- package/apps/client/.tbox-gen/registry.server.ts +15 -0
- package/apps/client/index.html +1 -1
- package/apps/client/package.json +3 -2
- package/apps/client/src/cards/registry.ts +6 -19
- package/apps/client/src/pages/page.tsx +11 -0
- package/apps/client/src/platform/AgentChatApp.tsx +2 -10
- package/apps/client/src/platform/card-preview-page.tsx +3 -5
- package/apps/client/src/platform/mount-page.tsx +9 -14
- package/apps/client/vite.config.ts +3 -32
- package/apps/server/package.json +1 -1
- package/apps/server/src/app.ts +7 -14
- package/apps/server/src/cli/collect-registry.ts +7 -11
- package/apps/server/src/dev.ts +38 -64
- package/apps/server/src/index.ts +27 -45
- package/apps/server/src/pre-tools.ts +1 -1
- package/apps/server/src/tools/{clock.ts → clock.tool.ts} +1 -1
- package/apps/server/src/tools/{translate.ts → translate.tool.ts} +1 -1
- package/apps/server/tests/cards-registry.test.ts +1 -1
- package/apps/server/tests/helpers/register.ts +10 -0
- package/apps/server/tests/tool-runtime.test.ts +2 -2
- package/apps/server/tsconfig.json +1 -1
- package/apps/server/tsup.config.ts +6 -3
- package/{packages → modules}/provider-custom/AGENTS.md +1 -1
- package/modules/provider-custom/INTEGRATION.md +10 -0
- package/{packages → modules}/provider-custom/package.json +1 -0
- package/package.json +11 -6
- package/packages/agent-ui-kit/package.json +1 -1
- package/packages/shared/AGENTS.md +11 -0
- package/packages/shared/package.json +13 -0
- package/packages/shared/src/index.ts +8 -0
- package/packages/shared/tsconfig.json +7 -0
- package/pnpm-lock.template.yaml +183 -155
- package/pnpm-workspace.template.yaml +10 -8
- package/scripts/generate.mjs +3 -0
- package/scripts/verify-generated.mjs +22 -127
- package/apps/client/src/app.tsx +0 -11
- package/apps/client/src/business/App.tsx +0 -11
- package/apps/client/src/business/README.md +0 -99
- package/apps/client/src/business/routes.json +0 -3
- package/apps/client/src/main.tsx +0 -17
- package/apps/client/src/modules.ts +0 -33
- package/apps/server/src/app-tools.ts +0 -14
- package/apps/server/src/modules.ts +0 -38
- /package/{packages → modules}/provider-custom/LICENSE +0 -0
- /package/{packages → modules}/provider-custom/README.md +0 -0
- /package/{packages → modules}/provider-custom/src/index.ts +0 -0
- /package/{packages → modules}/provider-custom/src/server/example-adapter.ts +0 -0
- /package/{packages → modules}/provider-custom/src/server/index.ts +0 -0
- /package/{packages → modules}/provider-custom/tbox.module.json +0 -0
- /package/{packages → modules}/provider-custom/tests/provider-custom.test.ts +0 -0
- /package/{packages → modules}/provider-custom/tsconfig.json +0 -0
- /package/{packages → modules}/provider-custom/tsup.config.ts +0 -0
|
@@ -1,25 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* 生成 entry
|
|
3
|
-
* bootstrap(幂等)+ AppShell(平台能力面)+
|
|
2
|
+
* 生成 entry 的页面挂载点(与生产 documents 同一入口)。
|
|
3
|
+
* bootstrap(幂等)+ AppShell(平台能力面)+ 页面组件;卡片预览传 shell:false。
|
|
4
4
|
*/
|
|
5
|
-
import { StrictMode } from 'react';
|
|
5
|
+
import { StrictMode, type ComponentType } from 'react';
|
|
6
6
|
import { createRoot } from 'react-dom/client';
|
|
7
|
-
import { lazy, Suspense, type ComponentType } from 'react';
|
|
8
7
|
import { bootstrap } from './bootstrap';
|
|
9
8
|
import { AppShell } from './AppShell';
|
|
10
9
|
|
|
11
|
-
export function mountPage(
|
|
10
|
+
export function mountPage(Page: ComponentType, options: { shell?: boolean } = {}): void {
|
|
12
11
|
bootstrap();
|
|
13
|
-
const Page = lazy(load);
|
|
14
12
|
const el = document.getElementById('root');
|
|
15
13
|
if (!el) return;
|
|
16
|
-
|
|
17
|
-
<
|
|
18
|
-
<
|
|
19
|
-
|
|
20
|
-
<Page />
|
|
21
|
-
</Suspense>
|
|
22
|
-
</AppShell>
|
|
23
|
-
</StrictMode>,
|
|
14
|
+
const content = options.shell === false ? <Page /> : (
|
|
15
|
+
<AppShell>
|
|
16
|
+
<Page />
|
|
17
|
+
</AppShell>
|
|
24
18
|
);
|
|
19
|
+
createRoot(el).render(<StrictMode>{content}</StrictMode>);
|
|
25
20
|
}
|
|
@@ -8,6 +8,7 @@ import { pathToFileURL } from 'node:url';
|
|
|
8
8
|
// 平台微调配套 babel plugin(随 @tbox.cn/app-sdk 分发):给每个 JSX 注入 data-code-inspect-*
|
|
9
9
|
// 源码位置属性,供 inspector runtime(/__vs_inspector__.js) 点选元素→定位源码。仅 dev 启用(见下)。
|
|
10
10
|
import codeInspectPlugin from '@tbox.cn/app-agent-sdk-client/platform/code-inspect';
|
|
11
|
+
import { tboxAppPlugin } from '@tbox.cn/app-build';
|
|
11
12
|
|
|
12
13
|
const pxToRemPlugin = pxToRem({
|
|
13
14
|
rootValue: 100,
|
|
@@ -27,33 +28,6 @@ const pxToRemPlugin = pxToRem({
|
|
|
27
28
|
});
|
|
28
29
|
|
|
29
30
|
export default defineConfig(async ({ command }) => {
|
|
30
|
-
// 卡片预览能力(v3.2 文档模式 + v6 统一文档形态):serve 恒 true(dev 面平台 entry 与
|
|
31
|
-
// 业务路由同机制——entry src 替换,解耦 import.meta.env.DEV 的 NODE_ENV 敏感性);
|
|
32
|
-
// build 无 TBOX_DEV_TOKEN → false(零残留);build 有(沙箱 protected watch 构建)→ true +
|
|
33
|
-
// 平台文档输入(_tbox/card-preview.html)。能力开关 = 文档/entry 生成与否。
|
|
34
|
-
const previewCapability = !process.env.VITEST && (command === 'serve' || !!process.env.TBOX_DEV_TOKEN);
|
|
35
|
-
// MPA 多页构建插件(v3.2 文档模式):routes.json 在场 ⇔ 端到端文档模式;缺席零注册零变化。
|
|
36
|
-
// 动态 import(对齐 previewErrorReporter 惯例)——vitest 零加载 Node 端依赖;
|
|
37
|
-
// build + serve 双态装载(build 生成文档输入/输出事务;serve 生成 entry——含平台文档 entry,
|
|
38
|
-
// dev 面经 dev-runtime entry src 替换消费)。
|
|
39
|
-
let mpaPlugin: Plugin | null = null;
|
|
40
|
-
if (!process.env.VITEST) {
|
|
41
|
-
const { tboxMpaPlugin } = await import('@tbox.cn/app-agent-sdk-server/platform');
|
|
42
|
-
mpaPlugin = tboxMpaPlugin(
|
|
43
|
-
previewCapability
|
|
44
|
-
? {
|
|
45
|
-
extraDocuments: [
|
|
46
|
-
{
|
|
47
|
-
inputName: 'card-preview',
|
|
48
|
-
document: '_tbox/card-preview.html',
|
|
49
|
-
entry: '@/platform/card-preview-page',
|
|
50
|
-
title: '卡片预览',
|
|
51
|
-
},
|
|
52
|
-
],
|
|
53
|
-
}
|
|
54
|
-
: {},
|
|
55
|
-
);
|
|
56
|
-
}
|
|
57
31
|
// tbox-app-dev dev 探测行(dev 探针常量;构件区在位时启用原地装配改指,缺席零行为变化。
|
|
58
32
|
// guard 断言本标记在场——重定位需同步 dev 探针(cli-dev)与 guard 两处)
|
|
59
33
|
// __dirname 基准 = apps/client:构件区在模板根(上一级×2)
|
|
@@ -72,10 +46,10 @@ export default defineConfig(async ({ command }) => {
|
|
|
72
46
|
}
|
|
73
47
|
return {
|
|
74
48
|
plugins: [
|
|
49
|
+
tboxAppPlugin({ root: resolve(__dirname, '../..') }),
|
|
75
50
|
react({
|
|
76
51
|
babel: { plugins: command === 'serve' ? [codeInspectPlugin] : [] },
|
|
77
52
|
}),
|
|
78
|
-
...(mpaPlugin ? [mpaPlugin] : []),
|
|
79
53
|
...devPlugins,
|
|
80
54
|
],
|
|
81
55
|
// 卡片预览面守卫已随 v6 统一文档形态退役(能力开关 = 平台文档/entry 生成与否)
|
|
@@ -101,9 +75,6 @@ export default defineConfig(async ({ command }) => {
|
|
|
101
75
|
css: {
|
|
102
76
|
postcss: { plugins: [pxToRemPlugin] },
|
|
103
77
|
},
|
|
104
|
-
|
|
105
|
-
rollupOptions: { input: { index: resolve(__dirname, 'index.html') } },
|
|
106
|
-
},
|
|
107
|
-
// middlewareMode 下不需要 server 配置(由 server 端 vite middleware 托管)
|
|
78
|
+
// 构建输入由 tboxAppPlugin 按 .tbox-gen/manifest.json 注入(文档模式唯一真源)
|
|
108
79
|
};
|
|
109
80
|
});
|
package/apps/server/package.json
CHANGED
|
@@ -11,13 +11,13 @@
|
|
|
11
11
|
"typecheck": "tsc --noEmit"
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
14
|
+
"@app/shared": "file:../../packages/shared",
|
|
14
15
|
"@tbox.cn/agent-plugin": "catalog:",
|
|
15
16
|
"@tbox.cn/app-contracts": "catalog:",
|
|
16
17
|
"@app/contracts-mall": "workspace:*",
|
|
17
18
|
"@tbox-dev-js/sdk": "catalog:",
|
|
18
19
|
"express": "catalog:",
|
|
19
20
|
"zod": "catalog:",
|
|
20
|
-
"@app/provider-custom": "workspace:*",
|
|
21
21
|
"@tbox.cn/app-agent-sdk-server": "catalog:"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
package/apps/server/src/app.ts
CHANGED
|
@@ -3,13 +3,13 @@
|
|
|
3
3
|
* 客户端供给由入口决定——静态产物见 index.ts(唯一 tsup entry),vite dev 见 dev.ts(仅 tsx)。
|
|
4
4
|
* import 本模块即完成装配(与旧单文件入口时序等价:listen 仍在 start() 内最后发生)。
|
|
5
5
|
*/
|
|
6
|
-
import express from 'express';
|
|
6
|
+
import express, { type Express } from 'express';
|
|
7
7
|
import { createServer, type IncomingMessage } from 'http';
|
|
8
8
|
import type { Duplex } from 'node:stream';
|
|
9
9
|
import path from 'path';
|
|
10
10
|
import { existsSync } from 'node:fs';
|
|
11
11
|
import { fileURLToPath } from 'url';
|
|
12
|
-
|
|
12
|
+
|
|
13
13
|
// ===== 模板日志(@tbox.cn/app-agent-sdk-server createLogger:本地时间 + LEVEL + [tag] + req/conv 尾缀) =====
|
|
14
14
|
import { createLogger } from '@tbox.cn/app-agent-sdk-server';
|
|
15
15
|
const serverLogger = createLogger('server');
|
|
@@ -48,7 +48,7 @@ import {
|
|
|
48
48
|
installTboxDev,
|
|
49
49
|
} from '@tbox.cn/app-agent-sdk-server';
|
|
50
50
|
import { installInspector } from '@tbox.cn/app-agent-sdk-server/platform';
|
|
51
|
-
|
|
51
|
+
|
|
52
52
|
import { APP_PRE_TOOLS } from './pre-tools.js';
|
|
53
53
|
import { INTENT_ROUTES, createAppIntentDispatcher } from './intent-routes.js';
|
|
54
54
|
import { loadIntegrations } from './config/integration.js';
|
|
@@ -264,7 +264,7 @@ if (!isProduction) {
|
|
|
264
264
|
// authExchanges 同引用传递(provider 包 register 期自注册换码器)+ scopeDefaults 中立注入
|
|
265
265
|
// scopeDefaults 静态注入退役——域装配(含 defaultMallId)经 registry 读取视图供给
|
|
266
266
|
// (SDK ctx.scopeDefaults 惰性优先注册表 → provider getter 登录期求值)
|
|
267
|
-
const ctx = createServerContext({
|
|
267
|
+
export const ctx = createServerContext({
|
|
268
268
|
authStrategies,
|
|
269
269
|
authExchanges: auth.exchanges,
|
|
270
270
|
...(memory.store ? { memory: memory.store } : {}),
|
|
@@ -419,8 +419,6 @@ async function createEngine(
|
|
|
419
419
|
return engine;
|
|
420
420
|
}
|
|
421
421
|
|
|
422
|
-
const httpServer = createServer(app);
|
|
423
|
-
|
|
424
422
|
/** dev 态升级扩展点(dev.ts 注册):返回 true = 已认领,app 路由跳过。生产 bundle 恒空。 */
|
|
425
423
|
export type UpgradeDelegate = (request: IncomingMessage, socket: Duplex, head: Buffer) => boolean;
|
|
426
424
|
let upgradeDelegate: UpgradeDelegate | undefined;
|
|
@@ -428,15 +426,10 @@ export function registerUpgradeDelegate(delegate: UpgradeDelegate): void {
|
|
|
428
426
|
upgradeDelegate = delegate;
|
|
429
427
|
}
|
|
430
428
|
|
|
431
|
-
export async function start() {
|
|
432
|
-
|
|
433
|
-
await registerAll(ctx);
|
|
434
|
-
|
|
435
|
-
// 模板演示工具(apps 级工具, 建议迁入模块):不参与模块体系,经统一注册面注册
|
|
436
|
-
// (010:start() 与 cli/collect-registry.ts 双处同调,防注册面失同步)
|
|
437
|
-
registerAppTools(ctx);
|
|
429
|
+
export async function start(serverApp: Express = app) {
|
|
430
|
+
const httpServer = createServer(serverApp);
|
|
438
431
|
|
|
439
|
-
//
|
|
432
|
+
// 时序契约:域装配解析必须晚于模块注册——module-mall-core 在 register 期经
|
|
440
433
|
// ctx.domainAssemblies 注册装配;解析点置于 start() 头部会查空表(引入回归:
|
|
441
434
|
// 默认商圈兜底/卡片动作授权/enhanceContext 全断——见 Agent Note 2026-09-17-mall-assembly-resolution-order)
|
|
442
435
|
const assembly = mallAssembly();
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { createServerContext } from '@tbox.cn/app-agent-sdk-server';
|
|
1
|
+
import { createServerContext, registerServerModules } from '@tbox.cn/app-agent-sdk-server';
|
|
2
2
|
import type { ToolCardBinding } from '@tbox.cn/agent-plugin/cards';
|
|
3
|
-
import {
|
|
4
|
-
import { registerAppTools } from '../app-tools.js';
|
|
3
|
+
import { registry } from '../../../client/.tbox-gen/registry.server';
|
|
5
4
|
|
|
6
5
|
export interface RegistryState {
|
|
7
6
|
staticToolIds: string[];
|
|
@@ -10,16 +9,13 @@ export interface RegistryState {
|
|
|
10
9
|
bindings: ToolCardBinding[];
|
|
11
10
|
}
|
|
12
11
|
|
|
13
|
-
/**
|
|
14
|
-
* 从自注册闭环收集当前注册状态(静态工具 id / cardType)。
|
|
15
|
-
* 供 plugin-doctor / plugin-inspect 复用——替代已删除的手写 tools/registry.ts + cards/registry.ts。
|
|
16
|
-
* 与运行时(apps/server/src/index.ts start())保持一致的注册面:registerAll + registerAppTools
|
|
17
|
-
* (010:apps 级演示工具统一注册面,双处同调防失同步)。
|
|
18
|
-
*/
|
|
12
|
+
/** 从生成 registry 收集当前注册状态(与 createApp 同源)。 */
|
|
19
13
|
export async function collectRegistryState(): Promise<RegistryState> {
|
|
20
14
|
const ctx = createServerContext();
|
|
21
|
-
|
|
22
|
-
|
|
15
|
+
for (const tool of registry.tools) ctx.tools.register(tool);
|
|
16
|
+
for (const skill of registry.skills) ctx.skills.register(skill);
|
|
17
|
+
for (const card of Object.values(registry.cards)) ctx.cards.register(card as never);
|
|
18
|
+
await registerServerModules(ctx, registry.modules);
|
|
23
19
|
return {
|
|
24
20
|
staticToolIds: Object.keys(ctx.tools.getAll()),
|
|
25
21
|
cardTypes: Object.keys(ctx.cards.getAll()),
|
package/apps/server/src/dev.ts
CHANGED
|
@@ -1,17 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* vite dev 入口(仅 tsx
|
|
3
|
-
*
|
|
4
|
-
* 部署形态一律走 dist/index.js(静态产物,零 HMR WS)。
|
|
5
|
-
*
|
|
6
|
-
* 方案 W(沙箱 dev server 鉴权保护):TBOX_DEV_TOKEN 在场 → protected 双面裁决
|
|
7
|
-
* (dev 面 = 凭据 + 导航/同源信号放行 vite;static 面 = 纯 dist + SSE 刷新,零源码泄漏);
|
|
8
|
-
* 缺席 → classic,与历史行为字节级等价。裁决机制真源 = SDK platform/dev-runtime
|
|
9
|
-
* (createTboxDevRuntime),本文件只做装配:vite 实例 + fs.allow 收窄 + 面无关自由区。
|
|
2
|
+
* vite dev 入口(仅 tsx 运行):import 即启动。
|
|
3
|
+
* 单端口:HTTP + HMR;页面由 dev-runtime 的 manifest 页集合交给 Vite/生成 entry。
|
|
10
4
|
*/
|
|
11
|
-
import { createServer } from 'http';
|
|
12
|
-
import path from 'path';
|
|
5
|
+
import { createServer } from 'node:http';
|
|
6
|
+
import path from 'node:path';
|
|
13
7
|
import { existsSync } from 'node:fs';
|
|
14
|
-
import { fileURLToPath } from 'url';
|
|
8
|
+
import { fileURLToPath } from 'node:url';
|
|
15
9
|
import { build as viteBuild, createServer as createViteServer, searchForWorkspaceRoot } from 'vite';
|
|
16
10
|
import {
|
|
17
11
|
createTboxDevRuntime,
|
|
@@ -19,46 +13,39 @@ import {
|
|
|
19
13
|
isReady,
|
|
20
14
|
resolveDevTokenFromEnv,
|
|
21
15
|
} from '@tbox.cn/app-agent-sdk-server/platform';
|
|
22
|
-
import { createLogger } from '@tbox.cn/app-agent-sdk-server';
|
|
23
|
-
import {
|
|
16
|
+
import { createApp, createLogger, getRequestContext } from '@tbox.cn/app-agent-sdk-server';
|
|
17
|
+
import { generateApp } from '@tbox.cn/app-build';
|
|
18
|
+
import { app as legacyApp, ctx, registerUpgradeDelegate, setupTboxDev, start } from './app.js';
|
|
24
19
|
|
|
25
20
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
26
21
|
const serverLogger = createLogger('server');
|
|
22
|
+
const root = path.resolve(__dirname, '../../..');
|
|
23
|
+
const clientRoot = path.resolve(__dirname, '../../client');
|
|
27
24
|
|
|
28
|
-
// fail-closed:dev 入口与生产安全姿态互斥——NODE_ENV=production 下 SDK dev 面
|
|
29
|
-
// (dev-login/卡片预览 API)会静默降级,这里显式拒绝启动(生产走 dist/index.js)
|
|
30
25
|
if (process.env.NODE_ENV === 'production') {
|
|
31
26
|
serverLogger.error('dev 入口禁止 NODE_ENV=production——生产请使用 dist/index.js(pnpm start)');
|
|
32
27
|
process.exit(1);
|
|
33
28
|
}
|
|
34
29
|
|
|
35
|
-
const
|
|
36
|
-
const
|
|
30
|
+
const generated = await generateApp({ root });
|
|
31
|
+
const [{ registry }, { appKind }] = await Promise.all([
|
|
32
|
+
import('../../client/.tbox-gen/registry.server.ts'),
|
|
33
|
+
import('../../client/.tbox-gen/app-kind.ts'),
|
|
34
|
+
]);
|
|
37
35
|
|
|
38
|
-
// HMR WS 独立路径:dev 基础设施命名空间(双下划线呼应 /@vite/*),与业务命名空间(/ws*)
|
|
39
|
-
// 解耦——upgrade 认领只针对此路径,根路径归还业务路由。协议常量(vite-hmr/vite-ping)、
|
|
40
|
-
// 认领路径 posix.join(base, hmr.path) 与 Spanner 子协议补写均内聚于 SDK handleUpgrade
|
|
41
|
-
// (smoke-dev-hmr.sh 守护该契约面)。
|
|
42
36
|
const HMR_WS_PATH = '/__vite_hmr';
|
|
43
|
-
|
|
44
|
-
// HMR WS hub:永不 listen 的纯事件载体。hmr.server 在场 → vite 不自建独立端口,
|
|
45
|
-
// 客户端按页面同源端口连接。vite 对 hub 仅 on/off('upgrade')(6.4.3 源码实证)。
|
|
46
37
|
const hmrHub = createServer();
|
|
47
|
-
|
|
48
38
|
const tokenState = resolveDevTokenFromEnv(process.env);
|
|
49
39
|
const protectedMode = tokenState.mode === 'protected';
|
|
50
|
-
|
|
51
|
-
// protected 面 fs.allow 收窄(classic 恒默认不传——红线):client 源 + 工作区依赖真源。
|
|
52
|
-
// 模板原地装配(pnpm dev)时模块图经 workspace 链接解析到仓库 packages/,故并入
|
|
53
|
-
// workspace 两目录(filter(existsSync):生成应用无此目录时自动滤除,不影响沙箱半径)。
|
|
54
40
|
const workspaceRoot = searchForWorkspaceRoot(clientRoot);
|
|
55
41
|
const fsAllowNarrowed = protectedMode
|
|
56
42
|
? [
|
|
57
43
|
...new Set(
|
|
58
44
|
[
|
|
59
45
|
clientRoot,
|
|
60
|
-
path.join(
|
|
61
|
-
path.join(
|
|
46
|
+
path.join(root, 'packages'),
|
|
47
|
+
path.join(root, 'modules'),
|
|
48
|
+
path.join(root, 'node_modules'),
|
|
62
49
|
path.join(workspaceRoot, 'packages'),
|
|
63
50
|
path.join(workspaceRoot, 'node_modules'),
|
|
64
51
|
].filter((p) => existsSync(p)),
|
|
@@ -70,24 +57,19 @@ const vite = await createViteServer({
|
|
|
70
57
|
root: clientRoot,
|
|
71
58
|
server: {
|
|
72
59
|
middlewareMode: true,
|
|
73
|
-
// 沙箱域名 Host 透传放行(dev-only;dist 物理不含 vite)。安全边界见 Agent Note
|
|
74
|
-
// 2026-09-09 与方案 W Note(protected 面 /@fs 另有 allow 收窄 + deny 现值保留)
|
|
75
60
|
allowedHosts: true,
|
|
76
61
|
fs: {
|
|
77
62
|
...(fsAllowNarrowed ? { allow: fsAllowNarrowed } : {}),
|
|
78
|
-
// /@fs 加固:默认 deny 不含服务端凭证目录(client 永不需要经 /@fs 读它)
|
|
79
63
|
deny: ['.env', '.env.*', '*.{crt,pem}', '**/.git/**', '**/config/credentials/**'],
|
|
80
64
|
},
|
|
81
65
|
hmr: { server: hmrHub, path: HMR_WS_PATH },
|
|
82
66
|
},
|
|
83
|
-
// custom:不加 Vite 自己的 SPA fallback,HTML 路由由 runtime.final() 统一处理
|
|
84
67
|
appType: 'custom',
|
|
85
68
|
});
|
|
86
69
|
|
|
87
70
|
const runtime = createTboxDevRuntime({
|
|
88
71
|
vite: { middlewares: vite.middlewares, transformIndexHtml: vite.transformIndexHtml, ws: vite.ws },
|
|
89
72
|
hmrHub,
|
|
90
|
-
// 与 vite 同一 resolved config 同式 posix.join 派生——恒等无漂移
|
|
91
73
|
hmrPath: path.posix.join(vite.config.base, HMR_WS_PATH),
|
|
92
74
|
tokenState,
|
|
93
75
|
clientRoot,
|
|
@@ -97,42 +79,20 @@ const runtime = createTboxDevRuntime({
|
|
|
97
79
|
logger: serverLogger,
|
|
98
80
|
fsAllowNarrowed: protectedMode,
|
|
99
81
|
sseKeepaliveMs: process.env.TBOX_DEV_SSE_KEEPALIVE_MS ? Number(process.env.TBOX_DEV_SSE_KEEPALIVE_MS) : undefined,
|
|
100
|
-
// v3.2 严格文档模式(G8 模式开关):routes.json 在场 ⇔ strict(dev 面 entry 替换 +
|
|
101
|
-
// public 目录索引;static 面 dist 文件真源)。缺席 = legacy SPA fallback,零变化。
|
|
102
82
|
pagePolicy: {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
// 替换——与业务路由同机制;能力关 = 文件不在 = 404)
|
|
106
|
-
platformPages: [
|
|
107
|
-
{ path: '/_tbox/card-preview', document: '_tbox/card-preview.html', inputName: 'card-preview' },
|
|
108
|
-
],
|
|
83
|
+
mode: 'manifest',
|
|
84
|
+
manifest: path.join(clientRoot, '.tbox-gen', 'manifest.json'),
|
|
109
85
|
},
|
|
110
86
|
});
|
|
111
87
|
|
|
112
|
-
// tbox dev 工具族装配(幂等;先于 gate/final——/_tbox/api 路由序恒胜)。静态入口
|
|
113
|
-
// (index.ts)不调用 → 生产零挂载。
|
|
114
88
|
setupTboxDev();
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
app.use(runtime.gate);
|
|
118
|
-
|
|
119
|
-
// ── 自由区(模板差异 + 应用自定义 middleware;dev/static 两面恒可见)──
|
|
120
|
-
// 探针路由:smoke-dev-protected.sh ⑩ 断言两面可见性(dev 基础设施,dist 物理不含)
|
|
121
|
-
app.get('/__tbox_dev_freezone_probe', (_req, res) => {
|
|
89
|
+
legacyApp.use(runtime.gate);
|
|
90
|
+
legacyApp.get('/__tbox_dev_freezone_probe', (_req, res) => {
|
|
122
91
|
res.type('text/plain').send('freezone-ok');
|
|
123
92
|
});
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
app.use(runtime.final);
|
|
127
|
-
|
|
128
|
-
// 升级委托:唯一 WS 入口 = runtime.handleUpgrade(路径认领 + 子协议补写 + protected
|
|
129
|
-
// token 校验,失败 401;未认领回落 app 路由——业务 /ws、根路径、fail-fast 语义不变)
|
|
93
|
+
legacyApp.use(runtime.final);
|
|
130
94
|
registerUpgradeDelegate(runtime.handleUpgrade);
|
|
131
95
|
|
|
132
|
-
// protected 面构建通知:watch 增量构建 → BUNDLE_START(构建窗口标记:static 面 miss → 503
|
|
133
|
-
// 而非 404 抖动)→ BUNDLE_END → SSE 广播 → static 面 reload。fire-and-forget(不 await、
|
|
134
|
-
// 无显式 close——dev 会话生命周期即进程生命周期);watch 启动 emptyOutDir 清空 dist 为
|
|
135
|
-
// 已定价行为(首建完成前 strict static 面 503 等待页 / legacy 纯 404)。
|
|
136
96
|
if (protectedMode) {
|
|
137
97
|
viteBuild({ root: clientRoot, build: { watch: {}, sourcemap: false } })
|
|
138
98
|
.then((result) => {
|
|
@@ -146,7 +106,21 @@ if (protectedMode) {
|
|
|
146
106
|
.catch((err) => serverLogger.error('[dev] watch build failed:', err));
|
|
147
107
|
}
|
|
148
108
|
|
|
149
|
-
|
|
109
|
+
const app = await createApp({
|
|
110
|
+
ctx,
|
|
111
|
+
manifest: generated.manifest,
|
|
112
|
+
getManifest: () => generated.manifest,
|
|
113
|
+
appKind,
|
|
114
|
+
registry,
|
|
115
|
+
mode: 'dev',
|
|
116
|
+
resolveUser: (req) => getRequestContext(req)?.identity ?? null,
|
|
117
|
+
// CORS / JSON / auth 必须先于 createApp 的 /api/fn RPC。
|
|
118
|
+
beforeRoutes: (base) => {
|
|
119
|
+
base.use(legacyApp);
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
start(app).catch((err) => {
|
|
150
124
|
serverLogger.error('Failed to start:', err);
|
|
151
125
|
process.exit(1);
|
|
152
126
|
});
|
package/apps/server/src/index.ts
CHANGED
|
@@ -1,59 +1,41 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* dist
|
|
4
|
-
* tsup.config.ts forbid-vite 插件拦截)。NODE_ENV 只影响 SDK 安全姿态(dev-login/密钥链)。
|
|
5
|
-
*
|
|
6
|
-
* strict(routes.json 在场 ⇔ 构建期插件同源判定):dist 文件存在性 = 唯一路由真源,
|
|
7
|
-
* 未知页面 404 是文件系统的自然结果;平台文档表(卡片预览,能力开 = 构建产物在场);
|
|
8
|
-
* 统一 .html 文档管线 + 目录索引/301;dist 未就绪 503。
|
|
9
|
-
* legacy(routes.json 缺席):express.static + SPA catch-all——与历史行为字节等价。
|
|
10
|
-
* 镜像携带全量源码(docker/Dockerfile 全量 COPY),src/dist 平级锚点恒可探测。
|
|
2
|
+
* 静态生产入口:createApp 装配 manifest/registry/页面运行时,业务路由仍由 app.ts 供给。
|
|
3
|
+
* dist 形态恒静态;vite dev 仅存在于 src/dev.ts。
|
|
11
4
|
*/
|
|
12
|
-
import path from 'path';
|
|
13
|
-
import express from 'express';
|
|
5
|
+
import path from 'node:path';
|
|
14
6
|
import { existsSync } from 'node:fs';
|
|
15
|
-
import { fileURLToPath } from 'url';
|
|
16
|
-
import { createLogger } from '@tbox.cn/app-agent-sdk-server';
|
|
17
|
-
import {
|
|
18
|
-
import
|
|
7
|
+
import { fileURLToPath } from 'node:url';
|
|
8
|
+
import { createLogger, createApp, getRequestContext, type AppManifest } from '@tbox.cn/app-agent-sdk-server';
|
|
9
|
+
import { injectScript } from '@tbox.cn/app-agent-sdk-server/platform';
|
|
10
|
+
import manifest from '../../client/.tbox-gen/manifest.json';
|
|
11
|
+
import { appKind } from '../../client/.tbox-gen/app-kind';
|
|
12
|
+
import { registry } from '../../client/.tbox-gen/registry.server';
|
|
13
|
+
import { app as legacyApp, ctx, start } from './app.js';
|
|
19
14
|
|
|
20
15
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
21
16
|
const serverLogger = createLogger('server');
|
|
17
|
+
const clientDir = path.join(__dirname, '../../client/dist');
|
|
22
18
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
serverLogger.warn(
|
|
26
|
-
`client/dist 缺失(${clientDistPath})——页面将不可用(API/WS 不受影响);请先执行 pnpm build:client`,
|
|
27
|
-
);
|
|
19
|
+
if (!existsSync(path.join(clientDir, 'index.html'))) {
|
|
20
|
+
serverLogger.warn(`client/dist 缺失(${clientDir})——页面将不可用(API/WS 不受影响);请先执行 pnpm build:client`);
|
|
28
21
|
}
|
|
29
22
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
23
|
+
const app = await createApp({
|
|
24
|
+
ctx,
|
|
25
|
+
manifest: manifest as AppManifest,
|
|
26
|
+
appKind,
|
|
27
|
+
registry,
|
|
28
|
+
mode: 'prod',
|
|
29
|
+
clientDir,
|
|
30
|
+
resolveUser: (req) => getRequestContext(req)?.identity ?? null,
|
|
31
|
+
decorateHtml: (html) => injectScript(html),
|
|
32
|
+
// CORS / JSON / auth 必须先于 createApp 的 /api/fn RPC;否则应用级函数可被匿名调用。
|
|
33
|
+
beforeRoutes: (base) => {
|
|
34
|
+
base.use(legacyApp);
|
|
35
|
+
},
|
|
33
36
|
});
|
|
34
37
|
|
|
35
|
-
|
|
36
|
-
if (existsSync(routesModule)) {
|
|
37
|
-
serverLogger.info('page_mode', { mode: 'strict-pages' });
|
|
38
|
-
app.use(
|
|
39
|
-
createStrictStaticHandler({
|
|
40
|
-
distDir: clientDistPath,
|
|
41
|
-
platformPages: [
|
|
42
|
-
{ path: '/_tbox/card-preview', document: '_tbox/card-preview.html', inputName: 'card-preview' },
|
|
43
|
-
],
|
|
44
|
-
}),
|
|
45
|
-
);
|
|
46
|
-
} else {
|
|
47
|
-
serverLogger.info('page_mode', { mode: 'legacy-spa' });
|
|
48
|
-
app.use(express.static(clientDistPath));
|
|
49
|
-
app.get('*', (req, res) => {
|
|
50
|
-
if (!req.path.startsWith('/api')) {
|
|
51
|
-
res.sendFile(path.join(clientDistPath, 'index.html'));
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
start().catch((err) => {
|
|
38
|
+
start(app).catch((err) => {
|
|
57
39
|
serverLogger.error('Failed to start:', err);
|
|
58
40
|
process.exit(1);
|
|
59
41
|
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// 铁律:不直接 import @mastra/core,经 @tbox.cn/app-agent-sdk-server 的 definePreTool 入口(单例铁律)
|
|
2
2
|
import { definePreTool } from '@tbox.cn/app-agent-sdk-server';
|
|
3
|
-
import { clockTool } from './tools/clock.js';
|
|
3
|
+
import { tool as clockTool } from './tools/clock.tool.js';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* 应用级前置工具声明(010):BuildEngineOptions.preTools 通道(与模块声明合并、应用覆盖模块)。
|
|
@@ -6,7 +6,7 @@ import { z } from 'zod';
|
|
|
6
6
|
* 当前时间工具 — 前置工具(pre-tool)演示:零参工具,参数无需模型抽取,
|
|
7
7
|
* 词表命中即可工程化预执行(见 apps/server/src/pre-tools.ts)。
|
|
8
8
|
*/
|
|
9
|
-
export const
|
|
9
|
+
export const tool = createTool({
|
|
10
10
|
id: 'getCurrentTime',
|
|
11
11
|
description: '查询当前日期和时间',
|
|
12
12
|
inputSchema: z.object({}),
|
|
@@ -6,7 +6,7 @@ import { z } from 'zod';
|
|
|
6
6
|
* 翻译工具 — 中英文互译
|
|
7
7
|
* 由原 translator Skill 改造而来,现在作为 Tool 注册,LLM 自主调用
|
|
8
8
|
*/
|
|
9
|
-
export const
|
|
9
|
+
export const tool = createTool({
|
|
10
10
|
id: 'translate',
|
|
11
11
|
description:
|
|
12
12
|
'中英文互译工具。当用户需要翻译文本时调用此工具。自动检测源语言并翻译为目标语言:中文→英文,英文→中文。',
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
import { describe, it, expect } from 'vitest';
|
|
5
5
|
import { diagnosePlugins } from '@tbox.cn/agent-plugin/doctor';
|
|
6
6
|
import { createServerContext, createPluginIntegration } from '@tbox.cn/app-agent-sdk-server';
|
|
7
|
-
import { registerAll } from '
|
|
7
|
+
import { registerAll } from './helpers/register.js';
|
|
8
8
|
|
|
9
9
|
describe('工具 / 卡片 registry 一致性(自注册闭环)', () => {
|
|
10
10
|
it('C2: ctx.tools 每个 key === tool.id', async () => {
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { registerServerModules, type ServerContext } from '@tbox.cn/app-agent-sdk-server';
|
|
2
|
+
import { registry } from '../../../client/.tbox-gen/registry.server';
|
|
3
|
+
|
|
4
|
+
/** 与 createApp 同源的注册面,供单测复用生成 registry。 */
|
|
5
|
+
export async function registerAll(ctx: ServerContext): Promise<void> {
|
|
6
|
+
for (const tool of registry.tools) ctx.tools.register(tool);
|
|
7
|
+
for (const skill of registry.skills) ctx.skills.register(skill);
|
|
8
|
+
for (const card of Object.values(registry.cards)) ctx.cards.register(card as never);
|
|
9
|
+
await registerServerModules(ctx, registry.modules);
|
|
10
|
+
}
|
|
@@ -6,8 +6,8 @@ import {
|
|
|
6
6
|
createInMemoryMemoryStore,
|
|
7
7
|
resolveMemoryStore,
|
|
8
8
|
} from '@tbox.cn/app-agent-sdk-server';
|
|
9
|
-
import { registerAll } from '
|
|
10
|
-
import { translateTool } from '../src/tools/translate.js';
|
|
9
|
+
import { registerAll } from './helpers/register.js';
|
|
10
|
+
import { tool as translateTool } from '../src/tools/translate.tool.js';
|
|
11
11
|
|
|
12
12
|
describe('tool runtime 宿主装配(自注册闭环)', () => {
|
|
13
13
|
it('空装配(mall 模板):无模块工具,app 级工具正常注册', async () => {
|
|
@@ -9,9 +9,8 @@ export default defineConfig({
|
|
|
9
9
|
clean: true,
|
|
10
10
|
sourcemap: true,
|
|
11
11
|
// 本地 workspace 源码包(exports 指向 src/*.ts,node 无法直接加载)必须内联:@app/*。
|
|
12
|
-
// @tbox.cn
|
|
13
|
-
|
|
14
|
-
noExternal: [/^@app\//],
|
|
12
|
+
// @tbox.cn/* 同样内联:模板源/生成应用验证态消费 workspace 源码,离线启动不依赖发布态 dist。
|
|
13
|
+
noExternal: [/^@app\//, /^@tbox\.cn\//],
|
|
15
14
|
// vite 不进 bundle 的结构防线:静态入口图(entry: index.ts → app.ts 链)任何 vite 引用
|
|
16
15
|
// (含 dev.ts 误入 entry)在 onResolve 即失败——dev 形态仅存在于 src/dev.ts(tsx 运行)。
|
|
17
16
|
esbuildPlugins: [
|
|
@@ -35,4 +34,8 @@ export default defineConfig({
|
|
|
35
34
|
'http',
|
|
36
35
|
'fs',
|
|
37
36
|
],
|
|
37
|
+
// 内联 CJS 依赖(ws 等)的 require() 在 ESM 输出中经真实 createRequire 落地。
|
|
38
|
+
banner: {
|
|
39
|
+
js: "import { createRequire as __tboxCreateRequire } from 'module';const require=__tboxCreateRequire(import.meta.url);",
|
|
40
|
+
},
|
|
38
41
|
});
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
本包是自定义 API 的 adapter 代码落点:某服务槽声明 `provider-custom` 而 adapter 未实施时,
|
|
4
4
|
服务调用报 `PROVIDER_NOT_IMPLEMENTED`——按指引实施并注册命中后即恢复。
|
|
5
5
|
|
|
6
|
-
**先读流程知识(随 CLI
|
|
6
|
+
**先读流程知识(随 CLI 发布,无需安装;接入材料与任务路由见 [INTEGRATION.md](INTEGRATION.md))**:
|
|
7
7
|
|
|
8
8
|
```sh
|
|
9
9
|
npx --yes @tbox.cn/app-cli skills get provider-custom # 实施三步/声明形态/代码骨架/注册/凭据
|