@triggerlink/sdk 0.4.7 → 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/agent.js CHANGED
@@ -88,8 +88,7 @@ export function createAgent(opts) {
88
88
  }
89
89
  registry.set(agent.name, agent);
90
90
  const redact = opts.redact;
91
- const llmStepId = `agent/${agent.name}/llm`;
92
- const toolStepId = `agent/${agent.name}/tool`;
91
+ const llmStepId = `agent/${agent.name}`;
93
92
  const messages = [{ role: "user", content: input }];
94
93
  const usage = { inputTokens: 0, outputTokens: 0 };
95
94
  const toolCalls = [];
@@ -142,7 +141,7 @@ export function createAgent(opts) {
142
141
  if (!def) {
143
142
  throw new Error(`agent "${agent.name}": model called unknown tool "${call.toolName}"`);
144
143
  }
145
- const output = await step.run(toolStepId, async () => {
144
+ const output = await step.run(`agent/${agent.name}/${call.toolName}`, async () => {
146
145
  const out = await def.handler(def.parameters.parse(call.input));
147
146
  return redact
148
147
  ? redact(out, { kind: "tool", iteration: i, toolName: call.toolName })
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 {
@@ -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: Required<FunctionOpts>;
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
@@ -22,6 +22,9 @@ export function serve(opts) {
22
22
  id: f.opts.id,
23
23
  event: f.opts.event,
24
24
  retries: f.opts.retries,
25
+ ...(f.opts.cancelOn?.length
26
+ ? { cancel_on: f.opts.cancelOn.map((c) => ({ event: c.event, match: c.match })) }
27
+ : {}),
25
28
  })),
26
29
  });
27
30
  }
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@triggerlink/sdk",
3
- "version": "0.4.7",
3
+ "version": "0.4.9",
4
4
  "description": "TriggerLink TypeScript SDK: durable, crash-recoverable functions for Next.js / Node.js",
5
5
  "keywords": [
6
6
  "triggerlink",