@tbox.cn/app-sdk-core 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +9 -0
- package/dist/index.d.ts +153 -0
- package/dist/index.js +1 -0
- package/package.json +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 tbox.cn
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# @tbox.cn/app-sdk-core
|
|
2
|
+
|
|
3
|
+
tbox AI Agent 平台 SDK 共享内核(app-sdk-client / app-sdk-server 共同依赖,DAG 顶点)。
|
|
4
|
+
|
|
5
|
+
- `logger-core`:纯函数日志域(createLogger / TboxLogger / detailForLog / sanitizeForLog / truncateForLog)
|
|
6
|
+
- `module-types`:模块装配类型推导(InferModuleCards——装配数组运行时唯一真源,类型零维护)
|
|
7
|
+
- `card-preview-types`:卡片预览 dev 面 DTO(双端 type-only 中立位)
|
|
8
|
+
|
|
9
|
+
零运行时依赖(type-only 依赖 `@tbox.cn/app-contracts`)。2026-09 自 `@tbox.cn/app-sdk` 三包拆分而来(该单包已退役)。
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { CardActionPresentation, CardEnvelopeProjection, TboxCardPayload } from '@tbox.cn/app-contracts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 日志核心(双端同构,零环境依赖):级别过滤 + 行格式 + 长字段截断 + 键级脱敏。
|
|
5
|
+
*
|
|
6
|
+
* 行格式(本地时区,人读优先;多机聚合歧义由部署侧统一 TZ 解决):
|
|
7
|
+
* 2026-08-18 14:38:19.102 WARN [tag] message req=<id> conv=<id>
|
|
8
|
+
* - 尾缀 req=/conv= 仅在 run 上下文存在时追加(server ALS / client run 上下文注入);
|
|
9
|
+
* - sink 恒为 console.*(plugin-doctor 猴补丁与测试 spyOn 锚点依赖);
|
|
10
|
+
* - 级别与上下文每次 emit 惰性读取(getter 注入,测试可运行时切换);
|
|
11
|
+
* - [TBOX_TRACE:...] 行由 trace-exporter 单独产出,不走本核心,且不受级别控制。
|
|
12
|
+
*/
|
|
13
|
+
type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'silent';
|
|
14
|
+
declare const LOG_LEVELS: readonly LogLevel[];
|
|
15
|
+
interface LogContext {
|
|
16
|
+
requestId?: string;
|
|
17
|
+
conversationId?: string;
|
|
18
|
+
}
|
|
19
|
+
interface TboxLogger {
|
|
20
|
+
debug(msg: string, ...args: unknown[]): void;
|
|
21
|
+
info(msg: string, ...args: unknown[]): void;
|
|
22
|
+
warn(msg: string, ...args: unknown[]): void;
|
|
23
|
+
error(msg: string, ...args: unknown[]): void;
|
|
24
|
+
}
|
|
25
|
+
/** 级别解析:非法/缺省回退 fallback(每次调用求值方由调用方保证) */
|
|
26
|
+
declare function resolveLevel(raw: string | undefined, fallback: LogLevel): LogLevel;
|
|
27
|
+
/** 本地时间 YYYY-MM-DD HH:mm:ss.SSS(逐段 pad,Node/浏览器同构;禁 toISOString) */
|
|
28
|
+
declare function formatLocalTime(d: Date): string;
|
|
29
|
+
/** 行组装:时间 LEVEL [tag] msg + 条件尾缀 */
|
|
30
|
+
declare function formatLine(input: {
|
|
31
|
+
time: string;
|
|
32
|
+
level: Exclude<LogLevel, 'silent'>;
|
|
33
|
+
tag: string;
|
|
34
|
+
msg: string;
|
|
35
|
+
ctx?: LogContext;
|
|
36
|
+
}): string;
|
|
37
|
+
/** 键级遮蔽:命中名单的值替换为 ***(精确键+变体子串、大小写不敏感、递归对象/数组;循环引用原样返回) */
|
|
38
|
+
declare function sanitizeForLog(value: unknown, seen?: WeakSet<object>): unknown;
|
|
39
|
+
/** 序列化 + 截断(单位 = 字符,与 trace-exporter clampValue 语义一致) */
|
|
40
|
+
declare function truncateForLog(value: unknown, maxChars?: number): string;
|
|
41
|
+
/** 脱敏 + 截断一步到位(详情日志标准管线) */
|
|
42
|
+
declare function detailForLog(value: unknown, maxChars?: number): string;
|
|
43
|
+
/**
|
|
44
|
+
* 公开 createLogger:级别双端探测(TBOX_LOG_LEVEL → localStorage['tbox:logLevel'] →
|
|
45
|
+
* ?logLevel= → info,typeof 守卫保证 Node/浏览器同构安全);上下文经注册器
|
|
46
|
+
* (server 壳注册 runLogScope ALS;client 壳注册 run 上下文——双入口 re-export 同一 binding)。
|
|
47
|
+
*/
|
|
48
|
+
type ContextProvider = () => LogContext | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* 壳注册上下文提供者(server/client 壳各自在模块加载时调用一次;追加式)。
|
|
51
|
+
* 解析取注册序首个非 undefined 结果:根入口双壳同载时 server ALS 优先、其作用域外
|
|
52
|
+
* 回落 client run 上下文(评审修复 L1——原"末位覆盖"会使 server 侧尾缀静默丢失);
|
|
53
|
+
* 双端各自 bundle 时只有一个提供者,行为不变。
|
|
54
|
+
*/
|
|
55
|
+
declare function setLogContextProvider(fn: ContextProvider): void;
|
|
56
|
+
declare function createLogger(tag: string): TboxLogger;
|
|
57
|
+
type ConsoleSink = {
|
|
58
|
+
debug(msg: string, ...args: unknown[]): void;
|
|
59
|
+
log(msg: string, ...args: unknown[]): void;
|
|
60
|
+
warn(msg: string, ...args: unknown[]): void;
|
|
61
|
+
error(msg: string, ...args: unknown[]): void;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* createLogger 唯一实现(server/client 入口 re-export 同一 binding——根入口 export * 防同名冲突)。
|
|
65
|
+
* opts 注入 getter 保证 core 零环境 API;emit 时惰性求值级别与上下文(跨请求复用安全)。
|
|
66
|
+
*/
|
|
67
|
+
declare function createCoreLogger(opts: {
|
|
68
|
+
tag: string;
|
|
69
|
+
getLevel: () => LogLevel;
|
|
70
|
+
getContext: () => LogContext | undefined;
|
|
71
|
+
sink?: ConsoleSink;
|
|
72
|
+
}): TboxLogger;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 双端共享的模块装配类型工具。
|
|
76
|
+
*
|
|
77
|
+
* InferModuleCards:从模块装配数组自动推导 app 端 CardType 联合。
|
|
78
|
+
* 装配数组是运行时唯一真源,类型零维护、无漂移面——增删模块后
|
|
79
|
+
* CardType 自动收缩/扩展,卡片覆盖(CardOverrides)等消费方随之报错。
|
|
80
|
+
*
|
|
81
|
+
* 示例(apps/server|client/src/modules.ts 自由区):
|
|
82
|
+
* ```ts
|
|
83
|
+
* export type CardType = InferModuleCards<(typeof modules)[number]>;
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
type InferModuleCards<M> = M extends {
|
|
87
|
+
cards: infer C extends Record<string, unknown>;
|
|
88
|
+
} ? keyof C : never;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* 卡片预览 dev 面 DTO(双端 type-only 共享中立位;logger-core 先例)。
|
|
92
|
+
*
|
|
93
|
+
* - server(preview-core / card-preview 安装器)与 client(card-preview-logic / page)
|
|
94
|
+
* 共同消费;**不经 server/client index 导出**(内部面,防 API 泄漏);
|
|
95
|
+
* - 统一样本模型(SPEC v7 F2):每样本自带完整渲染要素(data + envelope 投影 + 质量问题),
|
|
96
|
+
* 初始态 = 服务端由 sampleData 合成的第 0 个样本(initial: true),客户端零合成逻辑。
|
|
97
|
+
*/
|
|
98
|
+
|
|
99
|
+
/** 预览样本:一格 = 一个卡上看得出差别的状态 */
|
|
100
|
+
interface TboxCardPreviewSample {
|
|
101
|
+
/** 样本名(初始态为「初始态」) */
|
|
102
|
+
label: string;
|
|
103
|
+
/** 考察点说明(含等价输入合并说明) */
|
|
104
|
+
note?: string;
|
|
105
|
+
/** 分组标题(考察维度) */
|
|
106
|
+
group?: string;
|
|
107
|
+
/** 样本数据(meta.samples 原样 / 初始态为 sampleData) */
|
|
108
|
+
data: Record<string, unknown>;
|
|
109
|
+
/** 历史卡形态(动作禁用分支) */
|
|
110
|
+
isHistory?: boolean;
|
|
111
|
+
/** 令牌缺席形态(授权过期分支——页面不铸 token) */
|
|
112
|
+
noTokens?: boolean;
|
|
113
|
+
/** 信封投影(meta.project(data));project 抛错 → envelopeError(不阻断) */
|
|
114
|
+
envelope?: CardEnvelopeProjection;
|
|
115
|
+
envelopeError?: string;
|
|
116
|
+
/** 样本质量问题(safeParse 失败 + shape 越界键),页面徽标呈现 */
|
|
117
|
+
issues?: string[];
|
|
118
|
+
/** true = 服务端由 sampleData 合成的初始态 */
|
|
119
|
+
initial?: boolean;
|
|
120
|
+
}
|
|
121
|
+
/** 预览目录条目:单卡型一行 */
|
|
122
|
+
interface TboxCardPreviewCard {
|
|
123
|
+
/** 属主模块(未声明 declaration 的模块归 '(未声明)') */
|
|
124
|
+
moduleId: string;
|
|
125
|
+
cardType: string;
|
|
126
|
+
displayName: string;
|
|
127
|
+
description: string;
|
|
128
|
+
/** 该 cardType 的非 hidden 动作呈现字段(kind/label/style/enabled/...) */
|
|
129
|
+
actions: CardActionPresentation[];
|
|
130
|
+
samples: TboxCardPreviewSample[];
|
|
131
|
+
}
|
|
132
|
+
/** GET /_tbox/api/cards 响应 */
|
|
133
|
+
interface TboxCardPreviewCatalog {
|
|
134
|
+
cards: TboxCardPreviewCard[];
|
|
135
|
+
}
|
|
136
|
+
/** POST /_tbox/api/card-action 成功响应(显式 kind,页面零猜测) */
|
|
137
|
+
interface TboxPreviewActionResult {
|
|
138
|
+
ok: true;
|
|
139
|
+
kind: 'update' | 'card' | 'none';
|
|
140
|
+
/** kind=update:id=surfaceId(原位替换);kind=card:结果卡替换当前预览实例 */
|
|
141
|
+
card?: TboxCardPayload;
|
|
142
|
+
/** 附带文字(toast 呈现) */
|
|
143
|
+
text?: string;
|
|
144
|
+
/** 宿主效果(预览不执行,页面 toast 计数提示) */
|
|
145
|
+
effects?: unknown[];
|
|
146
|
+
}
|
|
147
|
+
/** POST /_tbox/api/card-action 失败响应 */
|
|
148
|
+
interface TboxPreviewActionError {
|
|
149
|
+
ok: false;
|
|
150
|
+
error: string;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export { type InferModuleCards, LOG_LEVELS, type LogContext, type LogLevel, type TboxCardPreviewCard, type TboxCardPreviewCatalog, type TboxCardPreviewSample, type TboxLogger, type TboxPreviewActionError, type TboxPreviewActionResult, createCoreLogger, createLogger, detailForLog, formatLine, formatLocalTime, resolveLevel, sanitizeForLog, setLogContextProvider, truncateForLog };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var s=["debug","info","warn","error","silent"];function i(n,e){return n&&s.includes(n)?n:e}function f(n){let e=(t,o=2)=>String(t).padStart(o,"0");return`${n.getFullYear()}-${e(n.getMonth()+1)}-${e(n.getDate())} ${e(n.getHours())}:${e(n.getMinutes())}:${e(n.getSeconds())}.${e(n.getMilliseconds(),3)}`}function L(n){let e=`${n.time} ${n.level.toUpperCase().padEnd(5)} [${n.tag}] ${n.msg}`;return n.ctx?.requestId&&(e+=` req=${n.ctx.requestId}`),n.ctx?.conversationId&&(e+=` conv=${n.ctx.conversationId}`),e}var l=new Set(["token","sign","signature","digest","mobile","password","salt","secret","session_key","sessionkey","authcode","auth_code"]),x=["token","password","secret","phone","mobile","signature"];function k(n){let e=n.toLowerCase();return l.has(e)?!0:x.some(t=>e.includes(t))}function g(n,e=new WeakSet){if(Array.isArray(n))return e.has(n)?n:(e.add(n),n.map(t=>g(t,e)));if(typeof n=="object"&&n!==null){if(e.has(n))return n;e.add(n);let t={};for(let[o,r]of Object.entries(n))t[o]=k(o)?"***":g(r,e);return t}return n}function v(n,e=1024){let t;try{t=JSON.stringify(n)??String(n)}catch{t=String(n)}return t.length<=e?t:t.slice(0,e)+`…[truncated +${t.length-e} chars]`}function S(n,e=1024){return v(g(n),e)}var d=[];function C(n){d.push(n)}function w(){for(let n of d){let e=n();if(e!==void 0)return e}}function m(){let n=globalThis.process;if(n?.env?.TBOX_LOG_LEVEL)return i(n.env.TBOX_LOG_LEVEL,"info");try{if(typeof localStorage<"u")return i(localStorage.getItem("tbox:logLevel")??void 0,"info")}catch{}if(typeof location<"u")try{let e=new URLSearchParams(location.search).get("logLevel");if(e)return i(e,"info")}catch{}return"info"}function b(n){return p({tag:n,getLevel:m,getContext:w})}function p(n){let e=n.sink??console,t=(o,r,u)=>{let c=n.getLevel();if(c==="silent"||s.indexOf(o)<s.indexOf(c))return;let a=L({time:f(new Date),level:o,tag:n.tag,msg:r,ctx:n.getContext()});e[o==="info"?"log":o](a,...u)};return{debug:(o,...r)=>t("debug",o,r),info:(o,...r)=>t("info",o,r),warn:(o,...r)=>t("warn",o,r),error:(o,...r)=>t("error",o,r)}}export{s as LOG_LEVELS,p as createCoreLogger,b as createLogger,S as detailForLog,L as formatLine,f as formatLocalTime,i as resolveLevel,g as sanitizeForLog,C as setLogContextProvider,v as truncateForLog};
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tbox.cn/app-sdk-core",
|
|
3
|
+
"version": "0.20.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "tbox AI Agent 平台 SDK 共享内核(logger-core 纯函数日志域 + module-types 模块类型推导)——app-sdk-client/server 共同依赖,零 npm 依赖(DAG 顶点)。",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"import": "./dist/index.js"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist",
|
|
14
|
+
"README.md",
|
|
15
|
+
"LICENSE"
|
|
16
|
+
],
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=20.0.0"
|
|
19
|
+
},
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@types/node": "24.13.3",
|
|
25
|
+
"tsup": "8.5.1",
|
|
26
|
+
"typescript": "5.9.3",
|
|
27
|
+
"vitest": "4.1.10"
|
|
28
|
+
},
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@tbox.cn/app-contracts": "0.12.0"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "tsup",
|
|
35
|
+
"typecheck": "tsc --noEmit",
|
|
36
|
+
"test": "vitest run"
|
|
37
|
+
}
|
|
38
|
+
}
|