@lark-base-open/base-database-app-sdk 0.4.0 → 0.5.0-durable.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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.5.0-durable.0] - 2026-10-26
4
+
5
+ ### Added
6
+
7
+ - 新增 Durable Workflow 与 Activity 入口,支持持久化步骤、等待、进度和站点 Function 调用。
8
+
3
9
  ## 0.4.0 - 2026-10-23
4
10
 
5
11
  ### Added
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  用于开发以飞书多维表格为数据后端的 Base Database 应用,提供页面与服务端运行时、平台组件、版本化能力查询与测试工具。
4
4
 
5
- 安装:`npm install @lark-base-open/base-database-app-sdk@0.4.0`。从旧版本升级前请阅读 [变更记录](CHANGELOG.md)。
5
+ 安装此预发布版本:`npm install @lark-base-open/base-database-app-sdk@0.5.0-durable.0`。从旧版本升级前请阅读 [变更记录](CHANGELOG.md)。
6
6
 
7
7
  运行环境需支持 ES2022;页面 API 需要 Base Database 页面宿主,服务端 API 用于 Node.js / FaaS。组件需要 React 与 React DOM 19.2,页面查询能力使用 @tanstack/react-query 5.104.1;npm 会安装声明的依赖与必需 peer。底层通信依赖 base-site-channel 0.1.6。
8
8
 
@@ -34,9 +34,9 @@ SDK 文档说明本版本 API、组件、兼容语义与可编译示例。业务
34
34
 
35
35
  ## npm 发布
36
36
 
37
- SDK 0.4.0 依赖已发布的 channel 0.1.6,锁文件必须解析到 `registry.npmjs.org` 的制品。AI Function 调用需要后端与宿主支持 `functions/:key/invoke`。
37
+ 本版本依赖已发布的 channel 0.1.6,锁文件必须解析到 `registry.npmjs.org` 的制品。AI Function 调用需要后端与宿主支持 `functions/:key/invoke`。
38
38
 
39
- 本版本拟发布到 `latest`。0.4.0 默认开启写后读,数据未就绪时自动重试并可能抛出 `DataNotReadyError`;需要按[写后读说明](docs/page.md#写后读)处理新异常,或传入 `{ readYourWrites: false }` 保持普通查询行为。契约生成使用工程安装的 SDK `./codegen` 入口;已安装旧 SDK 的工程不会因为单独升级 CLI 而自动切换到 0.4.0。0.3.0 将批量 Mutation 的 ID、输入和输出绑定到生成契约,收紧公开类型接口;0.2.0 对直接 Function 调用的返回泛型也有不兼容变化,跨版本升级见[升级指引](docs/upgrade.md)。
39
+ 本预发布版本使用 `durable` 标签,保留 `latest` 指向 0.4.0。0.4.0 默认开启写后读,数据未就绪时自动重试并可能抛出 `DataNotReadyError`;需要按[写后读说明](docs/page.md#写后读)处理新异常,或传入 `{ readYourWrites: false }` 保持普通查询行为。契约生成使用工程安装的 SDK `./codegen` 入口;已安装旧 SDK 的工程不会因为单独升级 CLI 而自动切换到 0.4.0。0.3.0 将批量 Mutation 的 ID、输入和输出绑定到生成契约,收紧公开类型接口;0.2.0 对直接 Function 调用的返回泛型也有不兼容变化,跨版本升级见[升级指引](docs/upgrade.md)。
40
40
 
41
41
  `CHANGELOG.md` 只记录简要版本变化,升级与迁移指导由[升级指引](docs/upgrade.md)及配套 CLI 的建站 Skill 提供。`check:migrations` 检查当前版本、重复编号和已有 Migration 的格式,不强制新增 Migration 章节。
42
42
 
@@ -51,15 +51,19 @@ Changelog 检查不能证明迁移可用。发布者还须按升级指导在旧
51
51
  - `node scripts/sync-capabilities.mjs --check`
52
52
  - `npm run docs:exports`
53
53
  - `npm pack --dry-run --json`
54
- - `npm publish --dry-run --tag latest --registry=https://registry.npmjs.org/`
54
+ - `npm publish --dry-run --tag durable --registry=https://registry.npmjs.org/`
55
55
 
56
56
  核对打包清单包含 dist、docs、examples、README、CHANGELOG、LICENSE、capabilities.json 与 development-pairing.json,以及所有 exports 指向的文件;不得包含测试、私有源码或本地配置。SDK 不提供根入口。
57
57
 
58
58
  检查通过后,由有发布权限的用户本人执行;先确认 `npm whoami` 返回预期账号:
59
59
 
60
60
  - `npm whoami --registry=https://registry.npmjs.org/`
61
- - `npm publish --tag latest --registry=https://registry.npmjs.org/`
62
- - `npm view @lark-base-open/base-database-app-sdk@0.4.0 version dist --json --registry=https://registry.npmjs.org/`
61
+ - `npm publish --tag durable --registry=https://registry.npmjs.org/`
62
+ - `npm view @lark-base-open/base-database-app-sdk@0.5.0-durable.0 version dist --json --registry=https://registry.npmjs.org/`
63
63
  - `npm view @lark-base-open/base-database-app-sdk dist-tags versions --json --registry=https://registry.npmjs.org/`
64
64
 
65
- 回读应确认 `latest` 为 `0.4.0`。
65
+ 回读应确认 `durable` 为 `0.5.0-durable.0`,`latest` 仍为 `0.4.0`。
66
+
67
+ ## Durable Function(预发布)
68
+
69
+ `/durable/workflow` 和 `/durable/activity` 提供持久化函数 API,使用方法与运行环境见 [Durable 文档](docs/durable.md)。新增入口从 0.5.0-durable.0 提供,0.4.0 不包含这些入口。使用前需确认目标平台已部署匹配的 Durable 运行时;发布 SDK 不代表平台已开放该能力。Temporal 为可选 peer,仅 Durable 开发和执行环境安装匹配版本;普通页面和普通 Function 不依赖它。ESM import 与 CJS require 共享 Durable 模块。
package/capabilities.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "schemaVersion": 1,
3
- "sdkVersion": "0.4.0",
3
+ "sdkVersion": "0.5.0-durable.0",
4
4
  "capabilities": {
5
5
  "runtime.context": {
6
6
  "required": [
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getAttempt = getAttempt;
4
+ exports.cancellationSignal = cancellationSignal;
5
+ exports.__installCapabilityTransport = __installCapabilityTransport;
6
+ exports.invokeFunction = invokeFunction;
7
+ const activity_1 = require("@temporalio/activity");
8
+ function getAttempt() { return (0, activity_1.activityInfo)().attempt; }
9
+ function cancellationSignal() { return activity_1.Context.current().cancellationSignal; }
10
+ let capabilityTransport;
11
+ // 只由通用 Worker 启动器装配;公开 SDK 类型不暴露凭证或配置入口。
12
+ /** @internal */
13
+ function __installCapabilityTransport(transport) {
14
+ if (capabilityTransport)
15
+ throw new Error('Site capability transport is already configured');
16
+ capabilityTransport = transport;
17
+ }
18
+ /** input 与普通 Site Function 的请求体一致,例如 { params: { city: '北京' } }。 */
19
+ async function invokeFunction(functionId, input, options) {
20
+ if (!functionId || !options?.key || options.key.length > 256)
21
+ throw new Error('Function and a stable call key are required');
22
+ if (!capabilityTransport)
23
+ throw new Error('Site capability transport is unavailable');
24
+ return capabilityTransport((0, activity_1.activityInfo)().taskToken, { function_id: functionId, call_key: options.key, payload: input }, cancellationSignal());
25
+ }
@@ -0,0 +1,6 @@
1
+ export declare function getAttempt(): number;
2
+ export declare function cancellationSignal(): AbortSignal;
3
+ /** input 与普通 Site Function 的请求体一致,例如 { params: { city: '北京' } }。 */
4
+ export declare function invokeFunction<T = unknown>(functionId: string, input: Record<string, unknown>, options: {
5
+ key: string;
6
+ }): Promise<T>;
@@ -0,0 +1,6 @@
1
+ export declare function getAttempt(): number;
2
+ export declare function cancellationSignal(): AbortSignal;
3
+ /** input 与普通 Site Function 的请求体一致,例如 { params: { city: '北京' } }。 */
4
+ export declare function invokeFunction<T = unknown>(functionId: string, input: Record<string, unknown>, options: {
5
+ key: string;
6
+ }): Promise<T>;
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.runDefinition = runDefinition;
4
+ exports.callStep = callStep;
5
+ exports.sleep = sleep;
6
+ exports.reportProgress = reportProgress;
7
+ exports.waitForEvent = waitForEvent;
8
+ const workflow_1 = require("@temporalio/workflow");
9
+ let state;
10
+ let definition;
11
+ let keys;
12
+ let events;
13
+ let receivedEvents;
14
+ let eventOverflow;
15
+ function receiveEvent(name, data, id) {
16
+ if (!(definition.events ?? ['approval']).includes(name))
17
+ return;
18
+ const key = id === undefined ? undefined : name + ':' + id;
19
+ if (key && receivedEvents.has(key))
20
+ return;
21
+ if (receivedEvents.size >= 1000 || [...events.values()].reduce((n, items) => n + items.length, 0) >= 1000) {
22
+ eventOverflow = true;
23
+ return;
24
+ }
25
+ if (key)
26
+ receivedEvents.add(key);
27
+ const pending = events.get(name) ?? [];
28
+ pending.push(data);
29
+ events.set(name, pending);
30
+ }
31
+ /** @internal */
32
+ async function runDefinition(meta, fn, input) {
33
+ definition = meta;
34
+ keys = new Set();
35
+ events = new Map();
36
+ receivedEvents = new Set();
37
+ eventOverflow = false;
38
+ state = { stage: 'starting', processed: 0, total: 0, paused: false, approved: false, results: [] };
39
+ (0, workflow_1.setHandler)((0, workflow_1.defineQuery)('progress'), () => ({ ...state, results: [...state.results] }));
40
+ (0, workflow_1.setHandler)((0, workflow_1.defineSignal)('pause'), () => { state.paused = true; });
41
+ (0, workflow_1.setHandler)((0, workflow_1.defineSignal)('resume'), () => { state.paused = false; });
42
+ (0, workflow_1.setHandler)((0, workflow_1.defineSignal)('approve'), () => { state.approved = true; receiveEvent('approval', { approved: true }); });
43
+ (0, workflow_1.setHandler)((0, workflow_1.defineSignal)('event'), (event) => {
44
+ if (!event || typeof event.name !== 'string' || typeof event.id !== 'string' || event.id.length < 8 || event.id.length > 128)
45
+ return;
46
+ receiveEvent(event.name, event.data, event.id);
47
+ });
48
+ try {
49
+ const result = await fn(input);
50
+ await boundary();
51
+ state.stage = 'completed';
52
+ return result;
53
+ }
54
+ catch (e) {
55
+ if (e instanceof TypeError || e instanceof RangeError)
56
+ throw workflow_1.ApplicationFailure.nonRetryable('Durable Function definition error', 'DefinitionError');
57
+ throw e;
58
+ }
59
+ }
60
+ async function boundary() {
61
+ if (eventOverflow)
62
+ throw workflow_1.ApplicationFailure.nonRetryable('Durable event limit exceeded', 'EventLimitExceeded');
63
+ if (state.paused) {
64
+ state.stage = 'paused';
65
+ await (0, workflow_1.condition)(() => !state.paused || eventOverflow);
66
+ }
67
+ if (eventOverflow)
68
+ throw workflow_1.ApplicationFailure.nonRetryable('Durable event limit exceeded', 'EventLimitExceeded');
69
+ }
70
+ async function callStep(name, args, options) {
71
+ await boundary();
72
+ if (!definition.activities[name] || !options.key || keys.has(options.key) || keys.size >= 100)
73
+ throw workflow_1.ApplicationFailure.nonRetryable('Invalid or repeated step key', 'DefinitionError');
74
+ keys.add(options.key);
75
+ const policy = definition.activities[name];
76
+ state.stage = 'processing';
77
+ const activity = definition.id + '__' + name;
78
+ const proxy = (0, workflow_1.proxyActivities)({
79
+ activityId: options.key, startToCloseTimeout: policy.timeout, scheduleToCloseTimeout: '2 minutes',
80
+ retry: { initialInterval: '1 second', maximumInterval: '5 seconds', maximumAttempts: policy.retry.maximumAttempts },
81
+ });
82
+ return proxy[activity](args);
83
+ }
84
+ async function sleep(milliseconds) {
85
+ await boundary();
86
+ state.stage = 'durable_wait';
87
+ await (0, workflow_1.sleep)(milliseconds);
88
+ await boundary();
89
+ }
90
+ function reportProgress(progress) {
91
+ Object.assign(state, progress);
92
+ }
93
+ async function waitForEvent(name, options) {
94
+ if (!(definition.events ?? ['approval']).includes(name))
95
+ throw workflow_1.ApplicationFailure.nonRetryable('Undeclared event', 'DefinitionError');
96
+ if (!Number.isFinite(options.timeout) || options.timeout <= 0)
97
+ throw workflow_1.ApplicationFailure.nonRetryable('Invalid event timeout', 'DefinitionError');
98
+ await boundary();
99
+ state.stage = 'waiting_' + name;
100
+ if (!await (0, workflow_1.condition)(() => (events.get(name)?.length ?? 0) > 0 || eventOverflow, options.timeout))
101
+ throw workflow_1.ApplicationFailure.nonRetryable('Event wait timed out', 'EventTimeout');
102
+ await boundary();
103
+ const result = events.get(name).shift();
104
+ if (name === 'approval')
105
+ state.approved = false;
106
+ return result;
107
+ }
@@ -0,0 +1,15 @@
1
+ type Json = any;
2
+ export declare function callStep<T>(name: string, args: Json, options: {
3
+ key: string;
4
+ }): Promise<T>;
5
+ export declare function sleep(milliseconds: number): Promise<void>;
6
+ export declare function reportProgress(progress: {
7
+ stage?: string;
8
+ processed: number;
9
+ total: number;
10
+ results?: Json[];
11
+ }): void;
12
+ export declare function waitForEvent<T = Json>(name: string, options: {
13
+ timeout: number;
14
+ }): Promise<T>;
15
+ export {};
@@ -0,0 +1,15 @@
1
+ type Json = any;
2
+ export declare function callStep<T>(name: string, args: Json, options: {
3
+ key: string;
4
+ }): Promise<T>;
5
+ export declare function sleep(milliseconds: number): Promise<void>;
6
+ export declare function reportProgress(progress: {
7
+ stage?: string;
8
+ processed: number;
9
+ total: number;
10
+ results?: Json[];
11
+ }): void;
12
+ export declare function waitForEvent<T = Json>(name: string, options: {
13
+ timeout: number;
14
+ }): Promise<T>;
15
+ export {};
@@ -0,0 +1,54 @@
1
+ # Base Site Durable SDK
2
+
3
+ 供 Base Site 的 Durable Function 源码使用。Workflow 定义执行顺序和等待,Activity 执行网络请求、站点 Function 调用等业务操作。执行由 DevCenter 管理的 Worker 承载。
4
+
5
+ Durable 是 App SDK 的两个子入口。预发布版本 0.5.0-durable.0 使用 npm 的 durable 标签;0.4.0 不包含这些入口。使用前需确认目标平台已部署匹配的 Durable 运行时。
6
+
7
+ ## 构建与安装
8
+
9
+ 安装此预发布版本:`npm install @lark-base-open/base-database-app-sdk@0.5.0-durable.0`。
10
+
11
+ 本地开发 SDK 时,在 SDK 根目录执行 `npm ci`、`npm run build`、`npm pack`。构建完成类型检查、JS 编译和公开声明生成。在使用项目中通过 `npm install <tarball-path>` 安装。Temporal 是可选 peer。开发时若直接加载 Durable 模块,需要安装 `@temporalio/workflow@1.24.0` 与 `@temporalio/activity@1.24.0`;只做公开类型检查不需要加载它们。托管 Worker 负责提供匹配版本,当前固定为 1.24.0。
12
+
13
+ Durable 使用 `/durable/workflow` 和 `/durable/activity` 两个入口。Workflow API 在 Temporal Workflow 执行上下文内使用,Activity API 在托管 Worker 的 Activity 上下文内使用;普通 Node 进程中的 import 成功不代表任务可以执行。
14
+
15
+ ## 编排与步骤
16
+
17
+ `workflow.ts`:
18
+
19
+ <!-- example: durable-workflow.ts -->
20
+ ```ts
21
+ import { callStep, reportProgress, sleep } from '@lark-base-open/base-database-app-sdk/durable/workflow';
22
+
23
+ export default async function task(input: { value: number }) {
24
+ const result = await callStep<number>('double', input, { key: 'double-input' });
25
+ reportProgress({ processed: 1, total: 1 });
26
+ await sleep(1000);
27
+ return result;
28
+ }
29
+ ```
30
+
31
+ `activities.ts`:
32
+
33
+ <!-- example: durable-activities.ts -->
34
+ ```ts
35
+ export async function double(input: { value: number }): Promise<number> {
36
+ return input.value * 2;
37
+ }
38
+ ```
39
+
40
+ 在对应 Function 的 `meta.json` 中声明 `double` Activity 的 timeout 和 retry.maximumAttempts。输入 Schema、发布方式和任务调用接口由 Site/DevCenter 契约管理。
41
+
42
+ `callStep` 的 key 在同一次 Workflow 中必须唯一;等待使用 SDK 的 `sleep` 或 `waitForEvent`,外部 I/O 放入 Activity。暂停在 SDK 边界生效,不中断已经开始执行的 Activity。
43
+
44
+ ## 站点能力
45
+
46
+ Activity 通过 `invokeFunction(functionId, input, { key })` 调用同一站点的普通 Function,input 与普通调用请求体一致,例如 `{ params: { city: '北京' } }`。key 表示同一个业务调用,Activity 重试时保持不变。平台注入调用通道和执行凭证,业务源码不传 token。
47
+
48
+ 请求结果未知时 Activity 可能重试。稳定 key 不等于业务端已经去重,写操作仍须由 Function 实现幂等处理。
49
+
50
+ ## 类型维护
51
+
52
+ 公开声明由 TypeScript 源码生成,内部启动器接口不出现在公开声明中。修改源码后执行 `npm run build`。平台私有传输实现不进入 SDK tarball。
53
+
54
+ SDK 的构建、声明与打包测试由本目录独立维护。执行端消费本包的 tarball;平台凭证传输和 Worker 管理不进入此包。
package/docs/exports.md CHANGED
@@ -43,3 +43,15 @@
43
43
  值:`CodegenError`、`SUPPORTED_CONTRACT_FORMAT_VERSIONS`、`generateFunctionClients`、`validateContractData`。
44
44
 
45
45
  类型与值完整清单:`ClientContract`、`CodegenError`、`FunctionContractData`、`FunctionInputContract`、`GeneratedFile`、`SUPPORTED_CONTRACT_FORMAT_VERSIONS`、`generateFunctionClients`、`validateContractData`。
46
+
47
+ ## durable/workflow
48
+
49
+ 值:`callStep`、`reportProgress`、`sleep`、`waitForEvent`。
50
+
51
+ 类型与值完整清单:`callStep`、`reportProgress`、`sleep`、`waitForEvent`。
52
+
53
+ ## durable/activity
54
+
55
+ 值:`cancellationSignal`、`getAttempt`、`invokeFunction`。
56
+
57
+ 类型与值完整清单:`cancellationSignal`、`getAttempt`、`invokeFunction`。
@@ -0,0 +1,3 @@
1
+ export async function double(input: { value: number }): Promise<number> {
2
+ return input.value * 2;
3
+ }
@@ -0,0 +1,8 @@
1
+ import { callStep, reportProgress, sleep } from '@lark-base-open/base-database-app-sdk/durable/workflow';
2
+
3
+ export default async function task(input: { value: number }) {
4
+ const result = await callStep<number>('double', input, { key: 'double-input' });
5
+ reportProgress({ processed: 1, total: 1 });
6
+ await sleep(1000);
7
+ return result;
8
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lark-base-open/base-database-app-sdk",
3
- "version": "0.4.0",
3
+ "version": "0.5.0-durable.0",
4
4
  "description": "Base Database 页面与服务端 SDK、平台组件及测试工具",
5
5
  "type": "module",
6
6
  "sideEffects": [
@@ -54,6 +54,26 @@
54
54
  "types": "./dist/codegen.d.cts",
55
55
  "default": "./dist/codegen.cjs"
56
56
  }
57
+ },
58
+ "./durable/workflow": {
59
+ "import": {
60
+ "types": "./dist/durable/workflow.d.cts",
61
+ "default": "./dist/durable/workflow.cjs"
62
+ },
63
+ "require": {
64
+ "types": "./dist/durable/workflow.d.cts",
65
+ "default": "./dist/durable/workflow.cjs"
66
+ }
67
+ },
68
+ "./durable/activity": {
69
+ "import": {
70
+ "types": "./dist/durable/activity.d.cts",
71
+ "default": "./dist/durable/activity.cjs"
72
+ },
73
+ "require": {
74
+ "types": "./dist/durable/activity.d.cts",
75
+ "default": "./dist/durable/activity.cjs"
76
+ }
57
77
  }
58
78
  },
59
79
  "files": [
@@ -67,7 +87,7 @@
67
87
  "development-pairing.json"
68
88
  ],
69
89
  "scripts": {
70
- "build": "node scripts/sync-contract-schema.mjs --check && node scripts/sync-capabilities.mjs && tsup && tsup --config tsup.cjs.config.ts && cp src/page/theme.css dist/theme.css",
90
+ "build": "node scripts/sync-contract-schema.mjs --check && node scripts/sync-capabilities.mjs && tsup && tsup --config tsup.cjs.config.ts && cp src/page/theme.css dist/theme.css && node scripts/build-durable.cjs",
71
91
  "test": "npm run check:migrations && node scripts/sync-capabilities.mjs --check && npm run build && node --import tsx --test test/*.test.ts && npm run test:types && npm run test:components",
72
92
  "check:migrations": "node scripts/check-migrations.mjs",
73
93
  "prepublishOnly": "npm run check:migrations",
@@ -87,11 +107,19 @@
87
107
  "@tanstack/react-query": "5.104.1",
88
108
  "msw": "2.11.6",
89
109
  "react": "^19.2.0",
90
- "react-dom": "^19.2.0"
110
+ "react-dom": "^19.2.0",
111
+ "@temporalio/activity": "1.24.0",
112
+ "@temporalio/workflow": "1.24.0"
91
113
  },
92
114
  "peerDependenciesMeta": {
93
115
  "msw": {
94
116
  "optional": true
117
+ },
118
+ "@temporalio/activity": {
119
+ "optional": true
120
+ },
121
+ "@temporalio/workflow": {
122
+ "optional": true
95
123
  }
96
124
  },
97
125
  "devDependencies": {
@@ -106,7 +134,9 @@
106
134
  "tsx": "4.20.3",
107
135
  "typescript": "5.8.3",
108
136
  "vitest": "3.2.4",
109
- "zod": "4.2.1"
137
+ "zod": "4.2.1",
138
+ "@temporalio/activity": "1.24.0",
139
+ "@temporalio/workflow": "1.24.0"
110
140
  },
111
141
  "typesVersions": {
112
142
  "*": {
@@ -118,6 +148,12 @@
118
148
  ],
119
149
  "codegen": [
120
150
  "dist/codegen.d.cts"
151
+ ],
152
+ "durable/workflow": [
153
+ "dist/durable/workflow.d.ts"
154
+ ],
155
+ "durable/activity": [
156
+ "dist/durable/activity.d.ts"
121
157
  ]
122
158
  }
123
159
  }