@triggerlink/sdk 0.4.8 → 0.4.9
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/dist/execx.d.ts +4 -1
- package/dist/function.d.ts +14 -1
- package/dist/index.d.ts +2 -2
- package/dist/serve.js +3 -0
- package/dist/step.d.ts +17 -0
- package/dist/step.js +26 -0
- package/package.json +1 -1
package/dist/execx.d.ts
CHANGED
|
@@ -11,13 +11,16 @@ export interface OpError {
|
|
|
11
11
|
}
|
|
12
12
|
/** 应用 → 平台的执行指令(serve 序列化为响应)。 */
|
|
13
13
|
export interface Opcode {
|
|
14
|
-
op: "StepComplete" | "StepError" | "RunComplete" | "RunError" | "Sleep" | "SendEvent";
|
|
14
|
+
op: "StepComplete" | "StepError" | "RunComplete" | "RunError" | "Sleep" | "SendEvent" | "WaitForEvent";
|
|
15
15
|
id?: string;
|
|
16
16
|
step_id?: string;
|
|
17
17
|
output?: unknown;
|
|
18
18
|
error?: OpError;
|
|
19
19
|
until?: string;
|
|
20
20
|
events?: OutgoingEvent[];
|
|
21
|
+
event?: string;
|
|
22
|
+
match?: string;
|
|
23
|
+
timeout?: string;
|
|
21
24
|
}
|
|
22
25
|
/** step.sendEvent 待扇出的事件(id/ts 缺省由平台补全,id 缺省为确定性派生)。 */
|
|
23
26
|
export interface OutgoingEvent {
|
package/dist/function.d.ts
CHANGED
|
@@ -6,6 +6,15 @@ export interface EventPayload<T = unknown> {
|
|
|
6
6
|
data: T;
|
|
7
7
|
ts: string;
|
|
8
8
|
}
|
|
9
|
+
/** step.waitForEvent 命中后返回的事件(与 EventPayload 同构)。 */
|
|
10
|
+
export type TriggerLinkEvent<T = unknown> = EventPayload<T>;
|
|
11
|
+
/** cancelOn 取消规则(FR-4.9):event 到达且 match 命中时,平台取消该函数的在途 run。 */
|
|
12
|
+
export interface CancelOnRule {
|
|
13
|
+
/** 触发取消的事件名 */
|
|
14
|
+
event: string;
|
|
15
|
+
/** 可选 expr 表达式,留空 = 该事件到达即取消;环境:data=到达事件 data,event=本 run 触发事件 {name, data} */
|
|
16
|
+
match?: string;
|
|
17
|
+
}
|
|
9
18
|
/** 传给用户 handler 的上下文。 */
|
|
10
19
|
export interface HandlerContext<T = unknown> {
|
|
11
20
|
event: EventPayload<T>;
|
|
@@ -20,9 +29,13 @@ export interface FunctionOpts {
|
|
|
20
29
|
event: string;
|
|
21
30
|
/** 重试上限;0/缺省 = 平台默认(4) */
|
|
22
31
|
retries?: number;
|
|
32
|
+
/** 取消规则,随 manifest 上报为 cancel_on */
|
|
33
|
+
cancelOn?: CancelOnRule[];
|
|
23
34
|
}
|
|
24
35
|
export interface TriggerFunction<T = unknown> {
|
|
25
|
-
readonly opts:
|
|
36
|
+
readonly opts: FunctionOpts & {
|
|
37
|
+
retries: number;
|
|
38
|
+
};
|
|
26
39
|
readonly handler: (ctx: HandlerContext<T>) => Promise<unknown>;
|
|
27
40
|
}
|
|
28
41
|
/** 定义一个 durable 函数。副作用必须放进 step.run(协议第 6 节约束)。 */
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { createClient, type Client, type ClientOptions, type SendEventInput, type SendResult, type RegisterOptions, } from "./client.js";
|
|
2
|
-
export { createFunction, type TriggerFunction, type FunctionOpts, type HandlerContext, type EventPayload, } from "./function.js";
|
|
2
|
+
export { createFunction, type TriggerFunction, type FunctionOpts, type HandlerContext, type EventPayload, type TriggerLinkEvent, type CancelOnRule, } from "./function.js";
|
|
3
3
|
export { serve, sdkVersion, type ServeOptions } from "./serve.js";
|
|
4
4
|
export { StepInterrupt, type Opcode, type StepState, type OutgoingEvent } from "./execx.js";
|
|
5
|
-
export type { StepTool } from "./step.js";
|
|
5
|
+
export type { StepTool, WaitForEventOpts } from "./step.js";
|
package/dist/serve.js
CHANGED
package/dist/step.d.ts
CHANGED
|
@@ -1,4 +1,14 @@
|
|
|
1
1
|
import { ExecCtx, type OutgoingEvent } from "./execx.js";
|
|
2
|
+
import type { TriggerLinkEvent } from "./function.js";
|
|
3
|
+
/** step.waitForEvent 的配置。 */
|
|
4
|
+
export interface WaitForEventOpts {
|
|
5
|
+
/** 等待的事件名(必填) */
|
|
6
|
+
event: string;
|
|
7
|
+
/** 可选 expr 表达式;求值环境:data=到达事件 data,event=本 run 触发事件 {name, data} */
|
|
8
|
+
match?: string;
|
|
9
|
+
/** 可选超时:Go duration 字符串(如 "168h")或毫秒数(1500 → "1.5s");超时后返回 null */
|
|
10
|
+
timeout?: string | number;
|
|
11
|
+
}
|
|
2
12
|
/** handler 内可用的 step 工具。 */
|
|
3
13
|
export interface StepTool {
|
|
4
14
|
/**
|
|
@@ -19,6 +29,13 @@ export interface StepTool {
|
|
|
19
29
|
* memo 语义同 run——恢复重入时直接返回已发事件 ID 列表,不会重复发送。
|
|
20
30
|
*/
|
|
21
31
|
sendEvent(id: string, events: OutgoingEvent | OutgoingEvent[]): Promise<string[]>;
|
|
32
|
+
/**
|
|
33
|
+
* 挂起当前 run 直至匹配事件到达或超时(协议 5.7 / FR-2.6):
|
|
34
|
+
* 事件命中 → 返回该事件;超时 → 返回 null(超时分支)。
|
|
35
|
+
* 与 sleep 同为挂起原语(恢复重入时 memo 命中直接返回),但唤醒由平台在
|
|
36
|
+
* 事件路由命中/超时扫描时驱动,挂起期间零占用。
|
|
37
|
+
*/
|
|
38
|
+
waitForEvent(id: string, opts: WaitForEventOpts): Promise<TriggerLinkEvent | null>;
|
|
22
39
|
}
|
|
23
40
|
/** 为一次函数调用构造 step 工具。 */
|
|
24
41
|
export declare function createStepTool(ec: ExecCtx): StepTool;
|
package/dist/step.js
CHANGED
|
@@ -45,8 +45,34 @@ export function createStepTool(ec) {
|
|
|
45
45
|
throw new Error(`step.sendEvent "${id}": no events`);
|
|
46
46
|
throw new StepInterrupt({ op: "SendEvent", id: h, step_id: id, events: list });
|
|
47
47
|
},
|
|
48
|
+
async waitForEvent(id, opts) {
|
|
49
|
+
const h = ec.nextHash(id);
|
|
50
|
+
const memo = ec.steps[h];
|
|
51
|
+
if (memo && memo.status === "completed") {
|
|
52
|
+
if (memo.output == null)
|
|
53
|
+
return null; // 超时分支:平台以 output=JSON null 完成
|
|
54
|
+
return memo.output;
|
|
55
|
+
}
|
|
56
|
+
if (!opts.event)
|
|
57
|
+
throw new Error(`step.waitForEvent "${id}": event is required`);
|
|
58
|
+
throw new StepInterrupt({
|
|
59
|
+
op: "WaitForEvent",
|
|
60
|
+
id: h,
|
|
61
|
+
step_id: id,
|
|
62
|
+
event: opts.event,
|
|
63
|
+
match: opts.match,
|
|
64
|
+
timeout: typeof opts.timeout === "number" ? msToGoDuration(opts.timeout) : opts.timeout,
|
|
65
|
+
});
|
|
66
|
+
},
|
|
48
67
|
};
|
|
49
68
|
}
|
|
69
|
+
/** 毫秒 → Go duration 字符串(协议要求 timeout 为 Go duration,如 1500 → "1.5s")。 */
|
|
70
|
+
function msToGoDuration(ms) {
|
|
71
|
+
if (!Number.isFinite(ms) || ms <= 0) {
|
|
72
|
+
throw new Error(`step.waitForEvent: invalid timeout ${ms}`);
|
|
73
|
+
}
|
|
74
|
+
return `${ms / 1000}s`;
|
|
75
|
+
}
|
|
50
76
|
export function errMessage(err) {
|
|
51
77
|
return err instanceof Error ? err.message : String(err);
|
|
52
78
|
}
|