agentosity 0.2.0 → 0.2.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.
Files changed (2) hide show
  1. package/package.json +3 -3
  2. package/src/index.js +80 -29
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "agentosity",
3
- "version": "0.2.0",
4
- "description": "Agentosity \u2014 AI-native is a number now. Automatic attendance for your AI agents (stdio MCP lifecycle + activity probes) + human clock-out CLI.",
3
+ "version": "0.2.2",
4
+ "description": "Agentosity AI-native is a number now. Automatic attendance for your AI agents (stdio MCP lifecycle + activity probes) + human clock-out CLI.",
5
5
  "type": "module",
6
6
  "bin": {
7
7
  "agentosity": "./bin/agentosity.js"
@@ -28,4 +28,4 @@
28
28
  "directory": "packages/cli"
29
29
  },
30
30
  "license": "MIT"
31
- }
31
+ }
package/src/index.js CHANGED
@@ -25,10 +25,9 @@ export async function main(argv) {
25
25
  default:
26
26
  console.log(`agentosity — AI-native is a number now.
27
27
 
28
- 三步开始(需登录):
29
- npx agentosity login <邮箱> # 1. 发验证码
30
- npx agentosity login <邮箱> <验证码> # 2. 登录
31
- npx agentosity init "<公司名>" # 3. 绑定公司 + 自动接入所有 harness
28
+ 一步开始:
29
+ npx agentosity init 登录 网页选公司 → 自动接入所有 harness,全程引导
30
+ (无浏览器环境:login <邮箱> 收验证码登录后,init "<公司名>")
32
31
 
33
32
  其他:
34
33
  npx agentosity radar 进程雷达:补录本机未接入 MCP 的 Agent 会话(常驻)
@@ -39,48 +38,60 @@ export async function main(argv) {
39
38
  }
40
39
  }
41
40
 
42
- async function init(company) {
43
- const pre = loadConfig();
44
- if (!pre.accessToken) {
45
- console.error(`Agentosity 需要登录使用:
46
- npx agentosity login <邮箱> # 收验证码
47
- npx agentosity login <邮箱> <验证码> # 登录
48
- 然后再跑 init。`);
41
+ async function init(companyArg) {
42
+ // 防占位符事故:范例文本被原样粘贴执行
43
+ if (companyArg && /你的公司名|公司名|your ?company|company ?name/i.test(companyArg)) {
44
+ console.error("「" + companyArg + "」看起来是示例占位符,不是真实公司名。直接跑 npx agentosity init(不带参数),公司在网页上选。");
49
45
  process.exit(1);
50
46
  }
51
- if (!company) {
52
- console.error('用法:npx agentosity init "你的公司名"');
53
- process.exit(1);
54
- }
55
- saveConfig({ company });
56
47
 
57
- // 服务端绑定公司(唯一真相;改绑每周一次)
58
- const created = await post("/api/companies", { name: company });
59
- if (created?.id) {
60
- const bind = await post("/api/profile", { companyId: created.id }, { method: "PUT" });
61
- if (bind?.error) console.log(`⚠️ ${bind.error}(本地归属仍按「${company}」记)`);
62
- else console.log(`✅ 公司已绑定:${created.name ?? company}`);
48
+ // 1. 登录(没有就当场走浏览器授权)
49
+ let cfg = loadConfig();
50
+ if (!cfg.accessToken) {
51
+ await browserLogin(); // 失败会自行退出
52
+ cfg = loadConfig();
63
53
  } else {
64
- console.log(`⚠️ 公司绑定暂未同步(网络?),本地归属按「${company}」记`);
54
+ console.log(`✓ 已登录 ${cfg.email ?? ""}`);
55
+ }
56
+
57
+ // 2. 公司绑定(统一收口网页;--company 仅供无浏览器环境)
58
+ let prof = await get(`/api/profile?device=${cfg.deviceId ?? ""}`);
59
+ if (!prof?.company && companyArg) {
60
+ const created = await post("/api/companies", { name: companyArg });
61
+ if (created?.id) await post("/api/profile", { companyId: created.id }, { method: "PUT" });
62
+ prof = await get(`/api/profile?device=${cfg.deviceId ?? ""}`);
63
+ }
64
+ if (!prof?.company) {
65
+ const url = `${apiBase()}/checkin`;
66
+ console.log(`在网页上选择你的公司…\n打不开就手动访问:${url}`);
67
+ await openBrowser(url);
68
+ for (let i = 0; i < 150 && !prof?.company; i++) {
69
+ await new Promise((r) => setTimeout(r, 2000));
70
+ prof = await get(`/api/profile?device=${cfg.deviceId ?? ""}`);
71
+ }
72
+ if (!prof?.company) {
73
+ console.error("等待超时。在网页上绑定公司后,重新跑 npx agentosity init");
74
+ process.exit(1);
75
+ }
65
76
  }
77
+ console.log(`✓ 公司:${prof.company.name}`);
78
+ saveConfig({ company: prof.company.name }); // 本地缓存,考勤/雷达归属用
66
79
 
67
- // 全家 harness 自动接入
80
+ // 3. 全家 harness 自动接入
68
81
  console.log("\n接入 Agent 考勤:");
69
82
  const results = installAllHarnesses();
70
83
  console.log(formatInstallResults(results) || " (未发现已安装的 harness)");
71
84
 
72
85
  console.log(`
73
- 从现在起,新开的 Agent 会话会自动考勤——模型零参与,只上报时长,不读任何内容。
86
+ 完成。从现在起,新开的 Agent 会话会自动考勤——模型零参与,只上报时长,不读任何内容。
74
87
  已开着的老会话不会被追踪(配置只对新会话生效),要收编它们:npx agentosity radar
75
88
 
76
89
  看榜:${apiBase()}/agents`);
77
90
  }
78
91
 
79
92
  async function login(email, code) {
80
- if (!email) {
81
- console.error("用法:npx agentosity login <邮箱>,收到验证码后再跑 login <邮箱> <验证码>");
82
- process.exit(1);
83
- }
93
+ // 不带参数:浏览器登录(像 npm login / gh auth login 一样)
94
+ if (!email) return browserLogin();
84
95
  if (!code) {
85
96
  const r = await post("/api/auth/send", { email });
86
97
  if (r?.ok) console.log(`✅ 验证码已发到 ${email},收到后跑:npx agentosity login ${email} <验证码>`);
@@ -101,6 +112,46 @@ async function login(email, code) {
101
112
  }
102
113
  }
103
114
 
115
+ async function openBrowser(url) {
116
+ try {
117
+ const { spawn } = await import("node:child_process");
118
+ const opener = process.platform === "darwin" ? "open" : process.platform === "win32" ? "start" : "xdg-open";
119
+ const child = spawn(opener, [url], { stdio: "ignore", detached: true, shell: process.platform === "win32" });
120
+ child.on("error", () => {});
121
+ child.unref();
122
+ } catch {
123
+ /* 打不开浏览器就靠打印的 URL */
124
+ }
125
+ }
126
+
127
+ async function browserLogin() {
128
+ const cfg = saveConfig({}); // 确保 deviceId 存在
129
+ const start = await post("/api/device/start", {});
130
+ if (!start?.code) {
131
+ console.error("无法发起登录(网络不可达)");
132
+ process.exit(1);
133
+ }
134
+ const url = `${apiBase()}/login?device=${start.code}`;
135
+ console.log(`在浏览器里完成登录…\n打不开就手动访问:${url}`);
136
+ await openBrowser(url);
137
+ for (let i = 0; i < 150; i++) {
138
+ await new Promise((r) => setTimeout(r, 2000));
139
+ const poll = await post(`/api/device/poll?code=${start.code}`, undefined, { method: "GET" });
140
+ if (poll?.ok && poll.access_token) {
141
+ saveConfig({ email: poll.email, accessToken: poll.access_token, refreshToken: poll.refresh_token });
142
+ await post("/api/auth/merge", { deviceId: cfg.deviceId }); // 设备历史并入账号
143
+ console.log(`✅ 已登录 ${poll.email}`);
144
+ return;
145
+ }
146
+ if (poll?.expired) {
147
+ console.error("登录超时,再跑一次 npx agentosity login");
148
+ process.exit(1);
149
+ }
150
+ }
151
+ console.error("登录超时,再跑一次 npx agentosity login");
152
+ process.exit(1);
153
+ }
154
+
104
155
  async function clockout() {
105
156
  const cfg = loadConfig();
106
157
  if (!cfg.accessToken) {