@triggerlink/sdk 0.4.1 → 0.4.3
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 +16 -8
- package/dist/agent.d.ts +20 -1
- package/dist/agent.js +17 -1
- package/dist/serve.d.ts +1 -1
- package/dist/serve.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -78,7 +78,7 @@ npm install @triggerlink/sdk zod # zod is for tool schemas; ai + providers are
|
|
|
78
78
|
|
|
79
79
|
```ts
|
|
80
80
|
import { createFunction } from "@triggerlink/sdk";
|
|
81
|
-
import { createAgent, anthropic } from "@triggerlink/sdk/agent"; // subpath import, not the main entry
|
|
81
|
+
import { createAgent, createTool, anthropic } from "@triggerlink/sdk/agent"; // subpath import, not the main entry
|
|
82
82
|
import { z } from "zod";
|
|
83
83
|
|
|
84
84
|
// Built-in providers, zero extra installs: anthropic / openai / deepseek
|
|
@@ -86,17 +86,20 @@ import { z } from "zod";
|
|
|
86
86
|
// Default instances read ANTHROPIC_API_KEY / OPENAI_API_KEY / DEEPSEEK_API_KEY from the env.
|
|
87
87
|
// Any other AI SDK LanguageModel can still be passed as `model` directly.
|
|
88
88
|
|
|
89
|
+
// createTool is a generic factory: the zod schema's type flows into the handler's
|
|
90
|
+
// params — annotate nothing. Plain object literals also work; use createTool when
|
|
91
|
+
// sharing a tool across agents.
|
|
92
|
+
const searchKb = createTool({
|
|
93
|
+
description: "Search the knowledge base",
|
|
94
|
+
parameters: z.object({ query: z.string() }),
|
|
95
|
+
handler: async ({ query }) => kb.search(query), // query: string, inferred
|
|
96
|
+
});
|
|
97
|
+
|
|
89
98
|
const researcher = createAgent({
|
|
90
99
|
name: "researcher", // stable ID, used in memo keys — do not rename casually
|
|
91
100
|
model: anthropic("claude-sonnet-4-5"), // any AI SDK LanguageModel
|
|
92
101
|
system: "You are a research assistant. Answer concisely.",
|
|
93
|
-
tools: {
|
|
94
|
-
search: {
|
|
95
|
-
description: "Search the knowledge base",
|
|
96
|
-
parameters: z.object({ query: z.string() }),
|
|
97
|
-
handler: async ({ query }) => kb.search(query),
|
|
98
|
-
},
|
|
99
|
-
},
|
|
102
|
+
tools: { search: searchKb },
|
|
100
103
|
maxIterations: 10, // safety cap; the run fails when exceeded
|
|
101
104
|
});
|
|
102
105
|
|
|
@@ -119,6 +122,10 @@ Notes:
|
|
|
119
122
|
*L + T* platform callbacks (one per step). Each step appears in the dashboard run detail
|
|
120
123
|
with its output — per-call tracing and token usage for free.
|
|
121
124
|
- **Multi-tool responses** are executed sequentially in array order, one step each.
|
|
125
|
+
- **Structured tool outputs**: `AgentResult.toolCalls` lists every tool execution of the run
|
|
126
|
+
(`{ toolCallId, toolName, input, output }`, in order; rebuilt from memos on recovery).
|
|
127
|
+
This covers the "done tool writes to shared state" pattern from agent frameworks like
|
|
128
|
+
AgentKit — read the tool's output here instead of parsing the final text.
|
|
122
129
|
- **`redact` hook** (optional): transforms each step's output inside `step.run` before
|
|
123
130
|
persistence, e.g. to strip secrets or PII from memos. It must be deterministic and
|
|
124
131
|
replay-safe — the memo is what the model sees of its own prior turns after a crash-resume:
|
|
@@ -128,6 +135,7 @@ Notes:
|
|
|
128
135
|
different `name`s; changing the tool set or loop structure between retries of the same run
|
|
129
136
|
can misalign memo keys (changing prompt text is safe).
|
|
130
137
|
- `ai` and the three built-in providers are regular dependencies of the SDK (bundled, no extra install); `zod` is an optional peer dependency — install it if you define tool schemas.
|
|
138
|
+
- **HTTP proxies**: if your environment routes external traffic through `http_proxy`/`https_proxy`, note that Node's global `fetch` ignores them by default — LLM calls will fail with `AI_APICallError: Cannot connect to API`. On Node 24+, start your app with `node --use-env-proxy`; on older Node, install `undici` and set `setGlobalDispatcher(new EnvHttpProxyAgent())` before serving.
|
|
131
139
|
|
|
132
140
|
## Sending events (any TS code, modeled after Inngest's `inngest.send`)
|
|
133
141
|
|
package/dist/agent.d.ts
CHANGED
|
@@ -10,6 +10,11 @@ export interface AgentTool<P = unknown, R = unknown> {
|
|
|
10
10
|
parameters: ZodType<P>;
|
|
11
11
|
handler: (params: P) => Promise<R> | R;
|
|
12
12
|
}
|
|
13
|
+
/**
|
|
14
|
+
* 定义一个 Agent 工具(泛型工厂):让 zod schema 的类型流到 handler 入参。
|
|
15
|
+
* 与直接写字面量等价,但获得完整的类型推断;跨 Agent 复用工具时也应使用它。
|
|
16
|
+
*/
|
|
17
|
+
export declare function createTool<P, R>(def: AgentTool<P, R>): AgentTool<P, R>;
|
|
13
18
|
/** redact 钩子的上下文(§5.7)。 */
|
|
14
19
|
export interface RedactCtx {
|
|
15
20
|
/** 产生输出的 step 类型 */
|
|
@@ -32,7 +37,7 @@ export interface AgentOpts {
|
|
|
32
37
|
/** AI SDK 的 LanguageModel(用户自带 provider 包,如 @ai-sdk/anthropic) */
|
|
33
38
|
model: LanguageModel;
|
|
34
39
|
system?: string;
|
|
35
|
-
tools?: Record<string, AgentTool
|
|
40
|
+
tools?: Record<string, AgentTool<any, any>>;
|
|
36
41
|
/** 迭代上限(一次迭代 = 一次 LLM 调用 + 其全部工具执行),默认 10;超限抛错使 run 失败 */
|
|
37
42
|
maxIterations?: number;
|
|
38
43
|
redact?: RedactHook;
|
|
@@ -47,6 +52,20 @@ export interface AgentResult {
|
|
|
47
52
|
inputTokens: number;
|
|
48
53
|
outputTokens: number;
|
|
49
54
|
};
|
|
55
|
+
/**
|
|
56
|
+
* 本次运行的全部工具执行记录(按执行顺序,含结构化输出)。
|
|
57
|
+
* 覆盖 AgentKit 的 "done 工具写 state.kv" 模式:函数代码从这里的 output 读取
|
|
58
|
+
* 工具产出,无需解析最终文本。恢复重放时 memo 命中路径同样重建该列表。
|
|
59
|
+
*/
|
|
60
|
+
toolCalls: AgentToolCallRecord[];
|
|
61
|
+
}
|
|
62
|
+
/** 一次工具执行的记录。 */
|
|
63
|
+
export interface AgentToolCallRecord {
|
|
64
|
+
toolCallId: string;
|
|
65
|
+
toolName: string;
|
|
66
|
+
input: unknown;
|
|
67
|
+
/** 工具的返回值(redact 钩子启用时为脱敏后的值——与模型所见一致) */
|
|
68
|
+
output: unknown;
|
|
50
69
|
}
|
|
51
70
|
export interface Agent {
|
|
52
71
|
readonly name: string;
|
package/dist/agent.js
CHANGED
|
@@ -10,6 +10,19 @@ import { generateText, tool, } from "ai";
|
|
|
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
|
+
/**
|
|
14
|
+
* 定义一个 Agent 工具(泛型工厂):让 zod schema 的类型流到 handler 入参。
|
|
15
|
+
* 与直接写字面量等价,但获得完整的类型推断;跨 Agent 复用工具时也应使用它。
|
|
16
|
+
*/
|
|
17
|
+
export function createTool(def) {
|
|
18
|
+
if (!def.description)
|
|
19
|
+
throw new Error("createTool: description is required");
|
|
20
|
+
if (!def.parameters)
|
|
21
|
+
throw new Error("createTool: parameters is required");
|
|
22
|
+
if (typeof def.handler !== "function")
|
|
23
|
+
throw new Error("createTool: handler is required");
|
|
24
|
+
return def;
|
|
25
|
+
}
|
|
13
26
|
/** redact 钩子可能破坏 llm memo 结构;此处 fail loud,不让坏 memo 落库或参与历史重建(§5.7)。 */
|
|
14
27
|
function assertLlmMemoShape(m, name) {
|
|
15
28
|
const o = m;
|
|
@@ -62,6 +75,7 @@ export function createAgent(opts) {
|
|
|
62
75
|
const toolStepId = `agent/${agent.name}/tool`;
|
|
63
76
|
const messages = [{ role: "user", content: input }];
|
|
64
77
|
const usage = { inputTokens: 0, outputTokens: 0 };
|
|
78
|
+
const toolCalls = [];
|
|
65
79
|
for (let i = 0;; i++) {
|
|
66
80
|
if (i >= maxIterations) {
|
|
67
81
|
throw new Error(`agent "${agent.name}": maxIterations (${maxIterations}) exceeded`);
|
|
@@ -92,7 +106,7 @@ export function createAgent(opts) {
|
|
|
92
106
|
usage.outputTokens += llmMemo.usage.outputTokens ?? 0;
|
|
93
107
|
messages.push(...llmMemo.responseMessages);
|
|
94
108
|
if (llmMemo.toolCalls.length === 0) {
|
|
95
|
-
return { text: llmMemo.text, iterations: i + 1, usage };
|
|
109
|
+
return { text: llmMemo.text, iterations: i + 1, usage, toolCalls };
|
|
96
110
|
}
|
|
97
111
|
// 并行 tool call 顺序执行(数组序),每个一个 durable step(§5.3)
|
|
98
112
|
for (const call of llmMemo.toolCalls) {
|
|
@@ -106,6 +120,8 @@ export function createAgent(opts) {
|
|
|
106
120
|
? redact(out, { kind: "tool", iteration: i, toolName: call.toolName })
|
|
107
121
|
: out;
|
|
108
122
|
});
|
|
123
|
+
// memo 命中时 step.run 直接返回缓存值,重放路径同样补全记录
|
|
124
|
+
toolCalls.push({ toolCallId: call.toolCallId, toolName: call.toolName, input: call.input, output });
|
|
109
125
|
const toolMsg = {
|
|
110
126
|
role: "tool",
|
|
111
127
|
content: [
|
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.
|
|
3
|
+
export declare const sdkVersion = "triggerlink-ts/0.4.3";
|
|
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.
|
|
4
|
+
export const sdkVersion = "triggerlink-ts/0.4.3";
|
|
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) {
|