@actiondock/core 2.0.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.
Files changed (43) hide show
  1. package/README.md +50 -0
  2. package/package.json +51 -0
  3. package/src/build/builder.ts +205 -0
  4. package/src/build/index.ts +2 -0
  5. package/src/build/templates.ts +59 -0
  6. package/src/doctor/doctor.ts +332 -0
  7. package/src/doctor/index.ts +2 -0
  8. package/src/doctor/types.ts +25 -0
  9. package/src/export/index.ts +2 -0
  10. package/src/export/skill.ts +349 -0
  11. package/src/export/templates.ts +258 -0
  12. package/src/filter/index.ts +1 -0
  13. package/src/filter/intent.ts +154 -0
  14. package/src/index.ts +13 -0
  15. package/src/profile/client.ts +302 -0
  16. package/src/profile/index.ts +3 -0
  17. package/src/profile/manager.ts +341 -0
  18. package/src/profile/types.ts +71 -0
  19. package/src/project/index.ts +3 -0
  20. package/src/project/init.ts +194 -0
  21. package/src/project/loader.ts +382 -0
  22. package/src/project/types.ts +62 -0
  23. package/src/registry/index.ts +2 -0
  24. package/src/registry/registry.ts +703 -0
  25. package/src/registry/types.ts +127 -0
  26. package/src/runtime/context.ts +232 -0
  27. package/src/runtime/env.ts +172 -0
  28. package/src/runtime/execution-manager.ts +74 -0
  29. package/src/runtime/index.ts +5 -0
  30. package/src/runtime/runner.ts +368 -0
  31. package/src/runtime/standalone.ts +429 -0
  32. package/src/schema/validator.ts +61 -0
  33. package/src/server/body.ts +112 -0
  34. package/src/server/index.ts +6 -0
  35. package/src/server/runtime-registry.ts +80 -0
  36. package/src/server/security.ts +115 -0
  37. package/src/server/server.ts +572 -0
  38. package/src/server/types.ts +42 -0
  39. package/src/storage/index.ts +64 -0
  40. package/src/storage/mask.ts +34 -0
  41. package/src/storage/sqlite.ts +578 -0
  42. package/src/storage/types.ts +111 -0
  43. package/src/utils/index.ts +60 -0
@@ -0,0 +1,341 @@
1
+ import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
2
+ import { dirname, join } from "node:path";
3
+ import { toSnakeUpperCase } from "../runtime/env";
4
+ import { getActionDockHome } from "../utils";
5
+ import type {
6
+ ProfileEntry,
7
+ ProfilesConfig,
8
+ ResolvedTarget,
9
+ TokenResolutionSource,
10
+ } from "./types";
11
+
12
+ const PROFILE_NAME_REGEX = /^[a-zA-Z0-9_\-\.]+$/;
13
+
14
+ /**
15
+ * 默认初始 Profiles 配置(预设 local 本地环境)。
16
+ */
17
+ export const DEFAULT_PROFILES_CONFIG: ProfilesConfig = {
18
+ currentProfile: "local",
19
+ profiles: {
20
+ local: {
21
+ serverUrl: "local",
22
+ description: "Local execution environment",
23
+ },
24
+ },
25
+ };
26
+
27
+ /**
28
+ * 格式化并规范化 Server URL 地址(自动补齐 http:// 协议头并移除末尾斜杠)。
29
+ */
30
+ export function normalizeServerUrl(url: string): string {
31
+ let cleaned = url.trim().replace(/\/+$/, "");
32
+ if (!/^https?:\/\//i.test(cleaned) && cleaned !== "local") {
33
+ cleaned = `http://${cleaned}`;
34
+ }
35
+ return cleaned;
36
+ }
37
+
38
+ /**
39
+ * 获取 profiles.json 配置文件的物理绝对路径(~/.actiondock/profiles.json)。
40
+ */
41
+ export function getProfilesFilePath(customHome?: string): string {
42
+ const baseDir = getActionDockHome(customHome);
43
+ return join(baseDir, ".actiondock", "profiles.json");
44
+ }
45
+
46
+ /**
47
+ * 加载并读取 profiles.json 配置文件。
48
+ */
49
+ export function loadProfiles(customHome?: string): ProfilesConfig {
50
+ const filePath = getProfilesFilePath(customHome);
51
+ if (!existsSync(filePath)) {
52
+ return structuredClone(DEFAULT_PROFILES_CONFIG);
53
+ }
54
+
55
+ try {
56
+ const raw = readFileSync(filePath, "utf-8");
57
+ const parsed = JSON.parse(raw);
58
+ if (!parsed || typeof parsed !== "object" || !parsed.profiles) {
59
+ return structuredClone(DEFAULT_PROFILES_CONFIG);
60
+ }
61
+ return parsed as ProfilesConfig;
62
+ } catch {
63
+ return structuredClone(DEFAULT_PROFILES_CONFIG);
64
+ }
65
+ }
66
+
67
+ /**
68
+ * 保存并写入 profiles.json 配置文件,并严格限制文件权限为 0600。
69
+ */
70
+ export function saveProfiles(data: ProfilesConfig, customHome?: string): void {
71
+ const filePath = getProfilesFilePath(customHome);
72
+ const dir = dirname(filePath);
73
+ try {
74
+ mkdirSync(dir, { recursive: true, mode: 0o700 });
75
+ try {
76
+ chmodSync(dir, 0o700);
77
+ } catch {
78
+ // Ignore on systems where chmod is not supported
79
+ }
80
+ } catch {
81
+ // Ignore
82
+ }
83
+
84
+ writeFileSync(filePath, JSON.stringify(data, null, 2) + "\n", {
85
+ encoding: "utf-8",
86
+ mode: 0o600,
87
+ });
88
+
89
+ try {
90
+ chmodSync(filePath, 0o600);
91
+ } catch {
92
+ // Ignore on systems where chmod is not supported
93
+ }
94
+ }
95
+
96
+ export function addProfile(
97
+ name: string,
98
+ entry: ProfileEntry,
99
+ customHome?: string
100
+ ): void {
101
+ const trimmedName = name.trim();
102
+ if (!trimmedName) {
103
+ throw new Error("Profile name cannot be empty");
104
+ }
105
+ if (!PROFILE_NAME_REGEX.test(trimmedName)) {
106
+ throw new Error(
107
+ `Invalid profile name '${trimmedName}'. Profile names may only contain letters, numbers, hyphens, underscores, and dots.`
108
+ );
109
+ }
110
+
111
+ const profilesConfig = loadProfiles(customHome);
112
+ const normalizedServer = normalizeServerUrl(entry.serverUrl);
113
+
114
+ profilesConfig.profiles[trimmedName] = {
115
+ serverUrl: normalizedServer,
116
+ token: entry.token?.trim() || undefined,
117
+ tokenEnv: entry.tokenEnv?.trim() || undefined,
118
+ description: entry.description?.trim() || undefined,
119
+ };
120
+
121
+ saveProfiles(profilesConfig, customHome);
122
+ }
123
+
124
+ export function removeProfile(name: string, customHome?: string): boolean {
125
+ const trimmedName = name.trim();
126
+ const profilesConfig = loadProfiles(customHome);
127
+
128
+ if (!profilesConfig.profiles[trimmedName]) {
129
+ return false;
130
+ }
131
+
132
+ delete profilesConfig.profiles[trimmedName];
133
+ if (profilesConfig.currentProfile === trimmedName) {
134
+ profilesConfig.currentProfile = "local";
135
+ }
136
+
137
+ saveProfiles(profilesConfig, customHome);
138
+ return true;
139
+ }
140
+
141
+ export function useProfile(name: string, customHome?: string): void {
142
+ const trimmedName = name.trim();
143
+ const profilesConfig = loadProfiles(customHome);
144
+
145
+ if (trimmedName !== "local" && !profilesConfig.profiles[trimmedName]) {
146
+ throw new Error(
147
+ `Profile '${trimmedName}' not found. Use 'ac profile list' to see available profiles or 'ac profile add' to register one.`
148
+ );
149
+ }
150
+
151
+ profilesConfig.currentProfile = trimmedName;
152
+ saveProfiles(profilesConfig, customHome);
153
+ }
154
+
155
+ export function getProfile(
156
+ name: string,
157
+ customHome?: string
158
+ ): ProfileEntry | undefined {
159
+ const profilesConfig = loadProfiles(customHome);
160
+ return profilesConfig.profiles[name.trim()];
161
+ }
162
+
163
+ export function listProfiles(
164
+ customHome?: string
165
+ ): Array<{ name: string; isCurrent: boolean; entry: ProfileEntry }> {
166
+ const profilesConfig = loadProfiles(customHome);
167
+ const current = profilesConfig.currentProfile || "local";
168
+
169
+ const entries: Array<{
170
+ name: string;
171
+ isCurrent: boolean;
172
+ entry: ProfileEntry;
173
+ }> = [];
174
+
175
+ // Ensure 'local' is listed
176
+ if (!profilesConfig.profiles["local"]) {
177
+ entries.push({
178
+ name: "local",
179
+ isCurrent: current === "local",
180
+ entry: {
181
+ serverUrl: "local",
182
+ description: "Local execution environment",
183
+ },
184
+ });
185
+ }
186
+
187
+ for (const [name, entry] of Object.entries(profilesConfig.profiles)) {
188
+ entries.push({
189
+ name,
190
+ isCurrent: name === current,
191
+ entry,
192
+ });
193
+ }
194
+
195
+ return entries;
196
+ }
197
+
198
+ /**
199
+ * Resolves token following multi-tier precedence:
200
+ * 1. CLI explicit --token
201
+ * 2. profile.tokenEnv specified environment variable
202
+ * 3. Derived profile environment variable: ACTIONDOCK_<PROFILE>_TOKEN or <PROFILE>_TOKEN
203
+ * 4. Stored token in profiles.json (deprecated)
204
+ * 5. Global ACTIONDOCK_TOKEN
205
+ */
206
+ export function resolveProfileToken(
207
+ profileName?: string,
208
+ entry?: ProfileEntry,
209
+ explicitToken?: string
210
+ ): { token?: string; source: TokenResolutionSource } {
211
+ // 1. Explicit CLI --token
212
+ if (explicitToken && explicitToken.trim()) {
213
+ return { token: explicitToken.trim(), source: "cli" };
214
+ }
215
+
216
+ // 2. Explicit tokenEnv in profile
217
+ if (entry?.tokenEnv && entry.tokenEnv.trim()) {
218
+ const envKey = entry.tokenEnv.trim();
219
+ const envVal = process.env[envKey];
220
+ if (envVal !== undefined && envVal.trim()) {
221
+ return { token: envVal.trim(), source: "tokenEnv" };
222
+ }
223
+ }
224
+
225
+ // 3. Derived profile environment variable: ACTIONDOCK_<PROFILE>_TOKEN / <PROFILE>_TOKEN
226
+ if (profileName && profileName !== "local") {
227
+ const snakeName = toSnakeUpperCase(profileName);
228
+ const candidate1 = `ACTIONDOCK_${snakeName}_TOKEN`;
229
+ const candidate2 = `${snakeName}_TOKEN`;
230
+ if (process.env[candidate1] && process.env[candidate1]!.trim()) {
231
+ return { token: process.env[candidate1]!.trim(), source: "profileEnv" };
232
+ }
233
+ if (process.env[candidate2] && process.env[candidate2]!.trim()) {
234
+ return { token: process.env[candidate2]!.trim(), source: "profileEnv" };
235
+ }
236
+ }
237
+
238
+ // 4. Stored token in profiles.json (deprecated fallback)
239
+ if (entry?.token && entry.token.trim()) {
240
+ return { token: entry.token.trim(), source: "profile" };
241
+ }
242
+
243
+ // 5. Global ACTIONDOCK_TOKEN
244
+ if (process.env.ACTIONDOCK_TOKEN && process.env.ACTIONDOCK_TOKEN.trim()) {
245
+ return { token: process.env.ACTIONDOCK_TOKEN.trim(), source: "globalEnv" };
246
+ }
247
+
248
+ return { token: undefined, source: "none" };
249
+ }
250
+
251
+ export function resolveTarget(
252
+ options?: { profile?: string; server?: string; token?: string },
253
+ customHome?: string
254
+ ): ResolvedTarget {
255
+ // 1. Explicit CLI --server flag
256
+ if (options?.server && options.server.trim()) {
257
+ const resolvedToken = resolveProfileToken(undefined, undefined, options.token);
258
+ return {
259
+ type: "remote",
260
+ serverUrl: normalizeServerUrl(options.server),
261
+ token: resolvedToken.token,
262
+ tokenSource: resolvedToken.source,
263
+ };
264
+ }
265
+
266
+ const profilesConfig = loadProfiles(customHome);
267
+
268
+ // 2. Explicit CLI --profile flag
269
+ if (options?.profile && options.profile.trim()) {
270
+ const pName = options.profile.trim();
271
+ if (pName === "local") {
272
+ return { type: "local", profileName: "local" };
273
+ }
274
+ const found = profilesConfig.profiles[pName];
275
+ if (!found) {
276
+ throw new Error(
277
+ `Profile '${pName}' not found. Configure it with 'ac profile add ${pName} --server <url>'`
278
+ );
279
+ }
280
+ const resolvedToken = resolveProfileToken(pName, found, options.token);
281
+ return {
282
+ type: "remote",
283
+ profileName: pName,
284
+ serverUrl: found.serverUrl,
285
+ token: resolvedToken.token,
286
+ tokenSource: resolvedToken.source,
287
+ };
288
+ }
289
+
290
+ // 3. Environment variable ACTIONDOCK_SERVER_URL
291
+ if (process.env.ACTIONDOCK_SERVER_URL && process.env.ACTIONDOCK_SERVER_URL.trim()) {
292
+ const resolvedToken = resolveProfileToken(undefined, undefined, options?.token);
293
+ return {
294
+ type: "remote",
295
+ serverUrl: normalizeServerUrl(process.env.ACTIONDOCK_SERVER_URL),
296
+ token: resolvedToken.token,
297
+ tokenSource: resolvedToken.source,
298
+ };
299
+ }
300
+
301
+ // 4. Environment variable ACTIONDOCK_PROFILE
302
+ if (process.env.ACTIONDOCK_PROFILE && process.env.ACTIONDOCK_PROFILE.trim()) {
303
+ const pName = process.env.ACTIONDOCK_PROFILE.trim();
304
+ if (pName === "local") {
305
+ return { type: "local", profileName: "local" };
306
+ }
307
+ const found = profilesConfig.profiles[pName];
308
+ if (!found) {
309
+ throw new Error(
310
+ `Profile '${pName}' (from ACTIONDOCK_PROFILE) not found. Configure it with 'ac profile add ${pName} --server <url>'`
311
+ );
312
+ }
313
+ const resolvedToken = resolveProfileToken(pName, found, options?.token);
314
+ return {
315
+ type: "remote",
316
+ profileName: pName,
317
+ serverUrl: found.serverUrl,
318
+ token: resolvedToken.token,
319
+ tokenSource: resolvedToken.source,
320
+ };
321
+ }
322
+
323
+ // 5. Current Profile in config
324
+ const current = profilesConfig.currentProfile;
325
+ if (current && current !== "local") {
326
+ const found = profilesConfig.profiles[current];
327
+ if (found && found.serverUrl && found.serverUrl !== "local") {
328
+ const resolvedToken = resolveProfileToken(current, found, options?.token);
329
+ return {
330
+ type: "remote",
331
+ profileName: current,
332
+ serverUrl: found.serverUrl,
333
+ token: resolvedToken.token,
334
+ tokenSource: resolvedToken.source,
335
+ };
336
+ }
337
+ }
338
+
339
+ // 6. Default to local
340
+ return { type: "local", profileName: "local" };
341
+ }
@@ -0,0 +1,71 @@
1
+ /**
2
+ * 单个远端配置环境(Profile)实体定义。
3
+ */
4
+ export interface ProfileEntry {
5
+ /** 远端 ActionDock 服务端地址(如 "http://192.168.1.100:5177" 或 "local") */
6
+ serverUrl: string;
7
+ /**
8
+ * 存储在配置文件中的明文鉴权 Token。
9
+ * @deprecated 强烈建议使用 tokenEnv 或标准环境变量(如 ACTIONDOCK_<PROFILE>_TOKEN),避免明文持久化
10
+ */
11
+ token?: string;
12
+ /** 指定从哪一个操作系统环境变量名动态读取 Token */
13
+ tokenEnv?: string;
14
+ /** 该机器/环境的描述信息 */
15
+ description?: string;
16
+ }
17
+
18
+ /**
19
+ * 全局 profiles.json 配置文件结构契约。
20
+ */
21
+ export interface ProfilesConfig {
22
+ /** 当前激活的默认 Profile 名称(默认为 "local") */
23
+ currentProfile?: string;
24
+ /** 已配置的 Profile 字典映射表 */
25
+ profiles: Record<string, ProfileEntry>;
26
+ }
27
+
28
+ /**
29
+ * Token 解析命中来源枚举。
30
+ */
31
+ export type TokenResolutionSource =
32
+ | "cli" // 来源于显式 CLI 参数 (--token)
33
+ | "tokenEnv" // 来源于 Profile 显式绑定的 tokenEnv 环境变量
34
+ | "profileEnv" // 来源于自动推导的 ACTIONDOCK_<PROFILE>_TOKEN
35
+ | "profile" // 来源于 profiles.json 中存储的遗留明文 Token
36
+ | "globalEnv" // 来源于全局 ACTIONDOCK_TOKEN
37
+ | "none"; // 未配置任何 Token
38
+
39
+ /**
40
+ * 经过解析后确定的最终执行目标环境。
41
+ */
42
+ export interface ResolvedTarget {
43
+ /** 执行环境类型:local (本地 Bun 进程) 或 remote (远端 HTTP Server) */
44
+ type: "local" | "remote";
45
+ /** 命中的 Profile 名称 */
46
+ profileName?: string;
47
+ /** 远端 Server URL */
48
+ serverUrl?: string;
49
+ /** 解析出的有效鉴权 Token */
50
+ token?: string;
51
+ /** Token 数据来源 */
52
+ tokenSource?: TokenResolutionSource;
53
+ }
54
+
55
+ /**
56
+ * 远端服务器健康探测与时延检测结果。
57
+ */
58
+ export interface RemoteHealthResult {
59
+ /** 服务端是否连通且鉴权成功 */
60
+ ok: boolean;
61
+ /** 服务端状态标识(如 "ok") */
62
+ status?: string;
63
+ /** 远端 ActionDock 版本号 */
64
+ version?: string;
65
+ /** 远端服务运行时间(秒) */
66
+ uptime?: number;
67
+ /** 网络往返延迟(毫秒) */
68
+ latencyMs: number;
69
+ /** 探测失败时的错误信息 */
70
+ error?: string;
71
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./init";
2
+ export * from "./loader";
3
+ export * from "./types";
@@ -0,0 +1,194 @@
1
+ import { existsSync, mkdirSync, writeFileSync } from "node:fs";
2
+ import { basename, join, resolve } from "node:path";
3
+
4
+ /**
5
+ * 项目初始化脚手架选项。
6
+ */
7
+ export interface InitOptions {
8
+ /** 自定义项目 ID(默认使用目录名) */
9
+ id?: string;
10
+ /** 自定义项目展示名称(默认由目录名美化生成) */
11
+ name?: string;
12
+ /** 自定义项目描述 */
13
+ description?: string;
14
+ }
15
+
16
+ /**
17
+ * 在目标目录初始化一个完整的 ActionDock 2.0 Action Package 脚手架。
18
+ * 生成内容包括:
19
+ * 1. actiondock.json(项目元数据与配置声明)
20
+ * 2. package.json(模块依赖与 bun test 脚本)
21
+ * 3. tsconfig.json(现代 ESNext / Bundler 编译配置)
22
+ * 4. .gitignore(排除持久化 db、node_modules、dist)
23
+ * 5. actions/greet.ts(标准示例 Action,演示 config、state、log 使用)
24
+ * 6. playbooks/greet-user.md(标准 SOP Playbook 演示)
25
+ * 7. tests/greet.test.ts(基于 createTestRuntime 的零依赖单元测试)
26
+ *
27
+ * @param targetDir 目标项目目录
28
+ * @param options 初始化选项
29
+ */
30
+ export function initProject(targetDir: string, options: InitOptions = {}): void {
31
+ const root = resolve(targetDir);
32
+ if (!existsSync(root)) {
33
+ mkdirSync(root, { recursive: true });
34
+ }
35
+
36
+ const dirName = basename(root);
37
+ const id = options.id || dirName;
38
+ const name = options.name || dirName.replace(/[-_]/g, " ").replace(/\b\w/g, (c) => c.toUpperCase());
39
+ const description = options.description || "ActionDock AI Agent Actions";
40
+
41
+ // 1. actiondock.json
42
+ const actiondockJson = {
43
+ id,
44
+ name,
45
+ version: "0.1.0",
46
+ description,
47
+ actionsDir: "actions",
48
+ playbooksDir: "playbooks",
49
+ config: {
50
+ SAMPLE_GREETING: {
51
+ description: "Default greeting message",
52
+ default: "Hello",
53
+ },
54
+ },
55
+ };
56
+ writeFileSync(
57
+ join(root, "actiondock.json"),
58
+ JSON.stringify(actiondockJson, null, 2) + "\n"
59
+ );
60
+
61
+ // 2. package.json
62
+ const packageJson = {
63
+ name: id,
64
+ version: "0.1.0",
65
+ description,
66
+ type: "module",
67
+ scripts: {
68
+ test: "bun test",
69
+ },
70
+ dependencies: {
71
+ "@actiondock/sdk": "^2.0.0",
72
+ },
73
+ devDependencies: {
74
+ "@types/bun": "latest",
75
+ "typescript": "^5.7.0",
76
+ },
77
+ };
78
+ writeFileSync(
79
+ join(root, "package.json"),
80
+ JSON.stringify(packageJson, null, 2) + "\n"
81
+ );
82
+
83
+ // 3. tsconfig.json
84
+ const tsconfigJson = {
85
+ compilerOptions: {
86
+ target: "ESNext",
87
+ module: "ESNext",
88
+ moduleResolution: "bundler",
89
+ strict: true,
90
+ skipLibCheck: true,
91
+ types: ["bun-types"],
92
+ },
93
+ };
94
+ writeFileSync(
95
+ join(root, "tsconfig.json"),
96
+ JSON.stringify(tsconfigJson, null, 2) + "\n"
97
+ );
98
+
99
+ // 4. .gitignore
100
+ const gitignore = `.actiondock/
101
+ node_modules/
102
+ dist/
103
+ bun.lock
104
+ *.db
105
+ `;
106
+ writeFileSync(join(root, ".gitignore"), gitignore);
107
+
108
+ // 5. actions/
109
+ const actionsDir = join(root, "actions");
110
+ mkdirSync(actionsDir, { recursive: true });
111
+
112
+ const sampleAction = `import { defineAction } from "@actiondock/sdk";
113
+
114
+ export default defineAction({
115
+ id: "sample.greet",
116
+ description: "Greet a user with configurable greeting",
117
+
118
+ inputSchema: {
119
+ type: "object",
120
+ properties: {
121
+ name: { type: "string", description: "Name of person to greet" },
122
+ },
123
+ required: ["name"],
124
+ },
125
+
126
+ outputSchema: {
127
+ type: "object",
128
+ properties: {
129
+ message: { type: "string" },
130
+ timestamp: { type: "string" },
131
+ },
132
+ required: ["message", "timestamp"],
133
+ },
134
+
135
+ async run(input: { name: string }, ctx) {
136
+ const greeting = ctx.config.get("SAMPLE_GREETING", "Hello");
137
+ const count = ((await ctx.state.get<number>("greet_count")) || 0) + 1;
138
+ await ctx.state.set("greet_count", count);
139
+
140
+ ctx.log.info(\`Greeting \${input.name} (times greeted: \${count})\`);
141
+
142
+ return {
143
+ message: \`\${greeting}, \${input.name}!\`,
144
+ timestamp: new Date().toISOString(),
145
+ };
146
+ },
147
+ });
148
+ `;
149
+ writeFileSync(join(actionsDir, "greet.ts"), sampleAction);
150
+
151
+ // 6. playbooks/
152
+ const playbooksDir = join(root, "playbooks");
153
+ mkdirSync(playbooksDir, { recursive: true });
154
+
155
+ const samplePlaybook = `---
156
+ id: greet-user
157
+ description: SOP for greeting a new user and verifying system health
158
+ actions:
159
+ - sample.greet
160
+ ---
161
+
162
+ # Greeting SOP
163
+
164
+ 1. Call \`sample.greet\` with the user's name.
165
+ 2. Confirm the returned greeting message.
166
+ `;
167
+ writeFileSync(join(playbooksDir, "greet-user.md"), samplePlaybook);
168
+
169
+ // 7. tests/
170
+ const testsDir = join(root, "tests");
171
+ mkdirSync(testsDir, { recursive: true });
172
+
173
+ const sampleTest = `import { describe, expect, it } from "bun:test";
174
+ import { createTestRuntime } from "@actiondock/sdk";
175
+ import greetAction from "../actions/greet";
176
+
177
+ describe("greet action", () => {
178
+ it("should greet user with greeting and increment count", async () => {
179
+ const runtime = createTestRuntime({
180
+ config: { SAMPLE_GREETING: "Hi" },
181
+ });
182
+
183
+ const res1 = await runtime.run(greetAction, { name: "Alice" });
184
+ expect(res1.message).toBe("Hi, Alice!");
185
+ expect(await runtime.state.get("greet_count")).toBe(1);
186
+
187
+ const res2 = await runtime.run(greetAction, { name: "Bob" });
188
+ expect(res2.message).toBe("Hi, Bob!");
189
+ expect(await runtime.state.get("greet_count")).toBe(2);
190
+ });
191
+ });
192
+ `;
193
+ writeFileSync(join(testsDir, "greet.test.ts"), sampleTest);
194
+ }