@triggerlink/sdk 0.4.5 → 0.4.7

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/README.md CHANGED
@@ -81,9 +81,9 @@ import { createFunction } from "@triggerlink/sdk";
81
81
  import { createAgent, createTool, anthropic } from "@triggerlink/sdk/agent"; // subpath import, not the main entry
82
82
  import { z } from "zod";
83
83
 
84
- // Built-in providers, zero extra installs: anthropic / openai / deepseek
85
- // (plus createAnthropic / createOpenAI / createDeepSeek for custom baseURL/apiKey).
86
- // Default instances read ANTHROPIC_API_KEY / OPENAI_API_KEY / DEEPSEEK_API_KEY from the env.
84
+ // Built-in providers, zero extra installs: anthropic / openai / deepseek / zai
85
+ // (plus createAnthropic / createOpenAI / createDeepSeek / createZhipu for custom baseURL/apiKey).
86
+ // Default instances read ANTHROPIC_API_KEY / OPENAI_API_KEY / DEEPSEEK_API_KEY / ZHIPU_API_KEY from the env.
87
87
  // Any other AI SDK LanguageModel can still be passed as `model` directly.
88
88
 
89
89
  // createTool is a generic factory: the zod schema's type flows into the handler's
@@ -130,6 +130,29 @@ Notes:
130
130
  turns, tool results), each element carrying `role`/`content` — same field name and shape as
131
131
  AgentKit's `result.output`, so helpers like `findLastIndex((m) => m.role === "assistant")`
132
132
  port directly. `lastAssistantTextMessageContent(result)` is the built-in shortcut.
133
+ - **`lifecycle.onResponse`**: fired once per *actual* LLM call (inside the durable step, after
134
+ redaction) with `{ result, iteration }`; `result` is an `AgentIterationResult`
135
+ (`{ text, output, toolCalls, usage }` for that iteration) and works directly with
136
+ `lastAssistantTextMessageContent(result)`. Memo-hit replays do not re-fire it, so hook side
137
+ effects run exactly once per LLM call; a throwing hook fails the step (platform retries).
138
+ There is no `network.state` — because function code is the router, extract results after
139
+ `agent.run` returns (via `result.output` / `result.toolCalls`), or write to your own storage
140
+ inside the hook (closure variables don't survive platform re-invocations):
141
+
142
+ ```ts
143
+ const codeAgent = createAgent({
144
+ name: "code-agent",
145
+ model: openai("gpt-4.1"),
146
+ lifecycle: {
147
+ onResponse: async ({ result }) => {
148
+ const lastAssistantText = lastAssistantTextMessageContent(result);
149
+ if (lastAssistantText?.includes("<task_summary>")) {
150
+ await db.runs.update(runId, { summary: lastAssistantText }); // your own storage
151
+ }
152
+ },
153
+ },
154
+ });
155
+ ```
133
156
  - **`redact` hook** (optional): transforms each step's output inside `step.run` before
134
157
  persistence, e.g. to strip secrets or PII from memos. It must be deterministic and
135
158
  replay-safe — the memo is what the model sees of its own prior turns after a crash-resume:
package/dist/agent.d.ts CHANGED
@@ -9,6 +9,7 @@ export type TextMessage = UserModelMessage | AssistantModelMessage | ToolModelMe
9
9
  export { anthropic, createAnthropic } from "@ai-sdk/anthropic";
10
10
  export { openai, createOpenAI } from "@ai-sdk/openai";
11
11
  export { deepseek, createDeepSeek } from "@ai-sdk/deepseek";
12
+ export { zai, createZhipu } from "zhipu-ai-provider";
12
13
  /** Agent 工具定义。parameters 为 zod schema;handler 入参是 schema parse 后的值。 */
13
14
  export interface AgentTool<P = unknown, R = unknown> {
14
15
  description: string;
@@ -36,6 +37,36 @@ export interface RedactCtx {
36
37
  * responseMessages)不得破坏,否则抛错(见 assertLlmMemoShape)。
37
38
  */
38
39
  export type RedactHook = (output: unknown, ctx: RedactCtx) => unknown;
40
+ /** 单轮 LLM 调用的结果(lifecycle.onResponse 的入参)。 */
41
+ export interface AgentIterationResult {
42
+ /** 本轮 assistant 文本 */
43
+ text: string;
44
+ /** 本轮 assistant 消息(与 lastAssistantTextMessageContent 兼容) */
45
+ output: TextMessage[];
46
+ /** 本轮模型发起的工具调用(尚未执行) */
47
+ toolCalls: Array<{
48
+ toolCallId: string;
49
+ toolName: string;
50
+ input: unknown;
51
+ }>;
52
+ usage: {
53
+ inputTokens?: number;
54
+ outputTokens?: number;
55
+ };
56
+ }
57
+ export interface AgentLifecycle {
58
+ /**
59
+ * 每次 LLM 调用真实执行后触发一次(memo 命中/恢复重放不触发),
60
+ * 看到的是 redact 之后的最终内容(与落库 memo、模型历史一致)。
61
+ * 抛错视同 step 失败:StepError → 平台退避重试(LLM 会被重新调用)。
62
+ * 注意:函数会被平台反复重入,闭包变量不可靠;要保留 hook 产出请写自己的存储,
63
+ * 或在 agent.run 返回后从 AgentResult 收尾提取。
64
+ */
65
+ onResponse?: (args: {
66
+ result: AgentIterationResult;
67
+ iteration: number;
68
+ }) => void | Promise<void>;
69
+ }
39
70
  export interface AgentOpts {
40
71
  /** 稳定标识,用作 memo 键前缀(agent/<name>/...);同一函数内不同 Agent 必须不同名(§5.2) */
41
72
  name: string;
@@ -46,6 +77,7 @@ export interface AgentOpts {
46
77
  /** 迭代上限(一次迭代 = 一次 LLM 调用 + 其全部工具执行),默认 10;超限抛错使 run 失败 */
47
78
  maxIterations?: number;
48
79
  redact?: RedactHook;
80
+ lifecycle?: AgentLifecycle;
49
81
  }
50
82
  export interface AgentResult {
51
83
  /** 最终一条 assistant 文本 */
package/dist/agent.js CHANGED
@@ -4,12 +4,13 @@
4
4
  // 主入口 index.ts 不得 import 本模块(§8.1),否则未装 ai 的普通用户会在 import 时崩溃。
5
5
  import { generateText, tool, } from "ai";
6
6
  // 内置 provider,开箱即用:默认实例从环境变量读 API key
7
- // (ANTHROPIC_API_KEY / OPENAI_API_KEY / DEEPSEEK_API_KEY);
7
+ // (ANTHROPIC_API_KEY / OPENAI_API_KEY / DEEPSEEK_API_KEY / ZHIPU_API_KEY);
8
8
  // 需要自定义 baseURL/apiKey/代理时用 createXxx 构造专属实例。
9
9
  // 其余 provider 不受影响:createAgent 的 model 接受任意 AI SDK LanguageModel。
10
10
  export { anthropic, createAnthropic } from "@ai-sdk/anthropic";
11
11
  export { openai, createOpenAI } from "@ai-sdk/openai";
12
12
  export { deepseek, createDeepSeek } from "@ai-sdk/deepseek";
13
+ export { zai, createZhipu } from "zhipu-ai-provider";
13
14
  /**
14
15
  * 定义一个 Agent 工具(泛型工厂):让 zod schema 的类型流到 handler 入参。
15
16
  * 与直接写字面量等价,但获得完整的类型推断;跨 Agent 复用工具时也应使用它。
@@ -115,6 +116,17 @@ export function createAgent(opts) {
115
116
  };
116
117
  const out = redact ? redact(memo, { kind: "llm", iteration: i }) : memo;
117
118
  assertLlmMemoShape(out, agent.name); // 落库前拦截坏结构
119
+ // onResponse 在 step 回调内触发:只在 LLM 真实执行时跑一次,
120
+ // memo 命中/恢复重放不重复触发;抛错视同 step 失败走重试。
121
+ await opts.lifecycle?.onResponse?.({
122
+ result: {
123
+ text: out.text,
124
+ output: out.responseMessages,
125
+ toolCalls: out.toolCalls,
126
+ usage: out.usage,
127
+ },
128
+ iteration: i,
129
+ });
118
130
  return out;
119
131
  });
120
132
  assertLlmMemoShape(llmMemo, agent.name); // memo 命中路径同样校验(防御脏数据)
package/dist/serve.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { Client } from "./client.js";
2
2
  import type { TriggerFunction } from "./function.js";
3
- export declare const sdkVersion = "triggerlink-ts/0.4.5";
3
+ export declare const sdkVersion = "triggerlink-ts/0.4.6";
4
4
  export declare const SIGNATURE_HEADER = "x-triggerlink-signature";
5
5
  export interface ServeOptions {
6
6
  client: Client;
package/dist/serve.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { ExecCtx, StepInterrupt } from "./execx.js";
2
2
  import { createStepTool, errMessage } from "./step.js";
3
3
  import { verifySignature } from "./sign.js";
4
- export const sdkVersion = "triggerlink-ts/0.4.5";
4
+ export const sdkVersion = "triggerlink-ts/0.4.6";
5
5
  export const SIGNATURE_HEADER = "x-triggerlink-signature";
6
6
  const MAX_BODY = 10 << 20; // 10 MB,与平台一致
7
7
  function json(data, status = 200) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@triggerlink/sdk",
3
- "version": "0.4.5",
3
+ "version": "0.4.7",
4
4
  "description": "TriggerLink TypeScript SDK: durable, crash-recoverable functions for Next.js / Node.js",
5
5
  "keywords": [
6
6
  "triggerlink",
@@ -59,8 +59,9 @@
59
59
  },
60
60
  "dependencies": {
61
61
  "@ai-sdk/anthropic": "^4.0.39",
62
- "@ai-sdk/deepseek": "^3.0.28",
62
+ "@ai-sdk/deepseek": "^3.0.32",
63
63
  "@ai-sdk/openai": "^4.0.42",
64
- "ai": "^7.0.66"
64
+ "ai": "^7.0.66",
65
+ "zhipu-ai-provider": "^0.4.0"
65
66
  }
66
67
  }