@turingflow/agent-kit 0.1.1 → 0.1.2

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
@@ -42,15 +42,13 @@ kit 侧提供(无需改动):
42
42
  ## Step 0 — 前置条件
43
43
 
44
44
  - Node.js ≥ 18,目标工程为 **Next.js App Router**(`next.config.mjs` 需支持 `basePath`)
45
- - 一次性完成 CodeBuddy 登录(浏览器登录,凭据存 `~/.codebuddy`,SDK 持久化、之后自动复用,无需 API Key)。
46
- **login/logout 是宿主应用侧职责**,agent-kit 不内置认证命令;参考实现见 `apps/web`:
45
+ - 一次性完成 CodeBuddy 登录(浏览器登录,凭据存 `~/.codebuddy`,SDK 持久化、之后自动复用,无需 API Key)。agent-kit 提供安装即用的命令,宿主无需实现认证脚本:
47
46
  ```bash
48
- # 在 apps/web 目录下
49
- npm run login # 自动打开浏览器完成 CodeBuddy 登录(scripts/codebuddy-auth.mjs)
50
- npm run logout # 登出(切外部 API 通道前执行,清订阅凭据)
47
+ # 在宿主应用目录下,以运行服务的同一个系统账号执行
48
+ npx @turingflow/agent-kit login
49
+ npx @turingflow/agent-kit logout
51
50
  ```
52
- 宿主应用只需按同样方式实现(直接调用 `@tencent-ai/agent-sdk` 的
53
- `unstable_v2_authenticate` / `unstable_v2_logout`),无需持有 kit 源码。
51
+ 已安装依赖时也可用 `npm exec -- agent-kit login`。无桌面的服务器会打印登录链接供手动打开;`--no-open` 可跳过尝试启动浏览器。只有服务账号需要执行,应用最终用户无需逐人登录。
54
52
  - 国内网络默认 `CODEBUDDY_INTERNET_ENVIRONMENT=internal`(**SDK 已内置默认值,可不配**;国际版留空即可)
55
53
  - (推荐,否则无会话持久化)PostgreSQL 8+,账号可建库
56
54
 
@@ -0,0 +1,82 @@
1
+ #!/usr/bin/env node
2
+ // 宿主应用可直接运行:npx @turingflow/agent-kit login | logout。
3
+ import { existsSync, readFileSync } from "node:fs";
4
+ import { spawn } from "node:child_process";
5
+ import path from "node:path";
6
+ import { unstable_v2_authenticate, unstable_v2_logout } from "@tencent-ai/agent-sdk";
7
+
8
+ function loadHostEnv() {
9
+ const candidates = [process.env.AGENT_ENV_FILE, path.join(process.cwd(), ".env")].filter(Boolean);
10
+ for (const file of candidates) {
11
+ const resolved = path.resolve(file);
12
+ if (!existsSync(resolved)) continue;
13
+ for (const line of readFileSync(resolved, "utf8").split(/\r?\n/)) {
14
+ const trimmed = line.trim();
15
+ if (!trimmed || trimmed.startsWith("#")) continue;
16
+ const equal = trimmed.indexOf("=");
17
+ if (equal < 0) continue;
18
+ const key = trimmed.slice(0, equal).trim();
19
+ if (!key || key in process.env) continue;
20
+ let value = trimmed.slice(equal + 1).trim();
21
+ if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
22
+ value = value.slice(1, -1);
23
+ }
24
+ process.env[key] = value;
25
+ }
26
+ }
27
+ }
28
+
29
+ function openBrowser(url) {
30
+ const [command, args] = process.platform === "win32"
31
+ ? ["cmd", ["/c", "start", "", url]]
32
+ : process.platform === "darwin"
33
+ ? ["open", [url]]
34
+ : ["xdg-open", [url]];
35
+ const child = spawn(command, args, { detached: true, stdio: "ignore" });
36
+ child.on("error", () => {}); // 无桌面的服务器可复制终端中的链接。
37
+ child.unref();
38
+ }
39
+
40
+ function help() {
41
+ console.log(`agent-kit CodeBuddy 订阅认证
42
+
43
+ 用法:
44
+ agent-kit login 登录运行应用的服务账号
45
+ agent-kit logout 清除该账号的 CodeBuddy 登录状态
46
+
47
+ 也可运行:npx @turingflow/agent-kit login
48
+ 登录凭据由 CodeBuddy 保存在当前系统账号下;应用最终用户无需逐人登录。`);
49
+ }
50
+
51
+ async function main() {
52
+ const command = process.argv[2];
53
+ if (!command || command === "help" || command === "--help" || command === "-h") return help();
54
+ if (command !== "login" && command !== "logout") {
55
+ help();
56
+ process.exitCode = 2;
57
+ return;
58
+ }
59
+
60
+ loadHostEnv();
61
+ const environment = process.env.CODEBUDDY_INTERNET_ENVIRONMENT?.trim() || "internal";
62
+ if (command === "logout") {
63
+ await unstable_v2_logout({ environment });
64
+ console.log("CodeBuddy 已退出登录。");
65
+ return;
66
+ }
67
+
68
+ console.log("正在登录 CodeBuddy。请在弹出的浏览器完成认证;无图形界面时复制下方链接。\n");
69
+ const result = await unstable_v2_authenticate({
70
+ environment,
71
+ onAuthUrl: ({ authUrl }) => {
72
+ console.log(`登录链接:${authUrl}`);
73
+ if (!process.argv.includes("--no-open")) openBrowser(authUrl);
74
+ },
75
+ });
76
+ console.log(`\n登录成功:${result.userinfo?.userNickname || result.userinfo?.userName || "服务账号"}。`);
77
+ }
78
+
79
+ main().catch((error) => {
80
+ console.error(`CodeBuddy 认证失败:${error instanceof Error ? error.message : String(error)}`);
81
+ process.exitCode = 1;
82
+ });
@@ -1,6 +1,5 @@
1
1
  /**
2
2
  * agent-kit · CLI(订阅对话演示/冒烟)
3
- * CodeBuddy 订阅的 login / logout 已移出 SDK,由宿主应用实现
4
- * (参考 apps/web/scripts/codebuddy-auth.mjs + npm run login / logout)。
3
+ * CodeBuddy 订阅登录由 agent-kit bin 命令提供:npx @turingflow/agent-kit login。
5
4
  */
6
5
  export {};
@@ -1,7 +1,6 @@
1
1
  /**
2
2
  * agent-kit · CLI(订阅对话演示/冒烟)
3
- * CodeBuddy 订阅的 login / logout 已移出 SDK,由宿主应用实现
4
- * (参考 apps/web/scripts/codebuddy-auth.mjs + npm run login / logout)。
3
+ * CodeBuddy 订阅登录由 agent-kit bin 命令提供:npx @turingflow/agent-kit login。
5
4
  */
6
5
  import { readFileSync, existsSync } from "node:fs";
7
6
  import { createInterface } from "node:readline/promises";
@@ -52,7 +51,7 @@ async function runDemo() {
52
51
  }
53
52
  catch (err) {
54
53
  console.error(`对话失败:${err instanceof Error ? err.message : err}`);
55
- console.error("提示:请先在宿主应用中执行登录命令(如 web 的 npm run login)。");
54
+ console.error("提示:请先在宿主应用目录执行 npx @turingflow/agent-kit login。");
56
55
  break;
57
56
  }
58
57
  }
@@ -91,7 +90,7 @@ function printUsage() {
91
90
  console.log(`agent-kit CLI — 用法:
92
91
  npm run demo 预设订阅对话冒烟
93
92
  无参数 交互式订阅对话
94
- (CodeBuddy 订阅登录/登出见宿主应用:web 的 npm run login / npm run logout)`);
93
+ (CodeBuddy 订阅登录/登出:npx @turingflow/agent-kit login / logout)`);
95
94
  }
96
95
  async function main() {
97
96
  loadDotEnv();
package/package.json CHANGED
@@ -1,11 +1,14 @@
1
1
  {
2
2
  "name": "@turingflow/agent-kit",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "Agent 前后端一体 SDK:服务端订阅对话(CodeBuddy / hy3)+ 前端 workbuddy 风格聊天 UI。Next.js 工程后台引主入口、前台引 /ui",
7
7
  "main": "./dist/server/index.js",
8
8
  "types": "./dist/server/index.d.ts",
9
+ "bin": {
10
+ "agent-kit": "./bin/agent-kit.mjs"
11
+ },
9
12
  "exports": {
10
13
  ".": {
11
14
  "types": "./dist/server/index.d.ts",
@@ -19,6 +22,7 @@
19
22
  "./styles.css": "./dist/styles.css"
20
23
  },
21
24
  "files": [
25
+ "bin",
22
26
  "dist",
23
27
  "README.md",
24
28
  "LICENSE",