ai-worklog 1.0.1 → 1.0.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/bin/index.js CHANGED
@@ -132,8 +132,9 @@ async function setupGitlabConfig() {
132
132
  } catch {}
133
133
  }
134
134
 
135
- // 尝试从 glab 配置自动读取
136
135
  let token = '', username = '';
136
+
137
+ // 来源1:glab 配置文件
137
138
  const glabPaths = [
138
139
  path.join(os.homedir(), 'Library', 'Application Support', 'glab-cli', 'config.yml'),
139
140
  path.join(os.homedir(), '.config', 'glab-cli', 'config.yml'),
@@ -150,11 +151,51 @@ async function setupGitlabConfig() {
150
151
  if (token) { console.log(' ✓ 从 glab 配置自动读取了 Token'); break; }
151
152
  }
152
153
 
153
- // 未找到则手动输入
154
+ // 来源2:git credential helper(适合配置了 HTTPS 凭据的用户)
154
155
  if (!token) {
155
- console.log(`\n 需要 GitLab Personal Access Token(api + write_repository 权限)`);
156
- console.log(` 获取地址: https://${GITLAB_HOST}/-/user_settings/personal_access_tokens\n`);
157
- token = await prompt(' GitLab Token: ');
156
+ try {
157
+ const cr = spawnSync('git', ['credential', 'fill'], {
158
+ input: `protocol=https\nhost=${GITLAB_HOST}\n\n`,
159
+ encoding: 'utf8', timeout: 3000,
160
+ });
161
+ const passMatch = cr.stdout.match(/password=(.+)/);
162
+ const userMatch = cr.stdout.match(/username=(.+)/);
163
+ if (passMatch) { token = passMatch[1].trim(); console.log(' ✓ 从 git credential 自动读取了 Token'); }
164
+ if (userMatch && !username) username = userMatch[1].trim();
165
+ } catch {}
166
+ }
167
+
168
+ // 来源3:环境变量
169
+ if (!token && process.env.GITLAB_TOKEN) {
170
+ token = process.env.GITLAB_TOKEN;
171
+ console.log(' ✓ 从环境变量 GITLAB_TOKEN 读取了 Token');
172
+ }
173
+
174
+ // 都没找到:自动打开浏览器到预填好的 Token 创建页,提示粘贴
175
+ if (!token) {
176
+ const tokenUrl = `https://${GITLAB_HOST}/-/user_settings/personal_access_tokens?name=ai-worklog&scopes=api,write_repository`;
177
+ console.log('\n 未检测到 GitLab Token,正在打开浏览器...');
178
+ console.log(` 请在页面中点击「Create personal access token」,复制后粘贴到下方\n`);
179
+ spawnSync('open', [tokenUrl]); // macOS
180
+ spawnSync('xdg-open', [tokenUrl]); // Linux(忽略失败)
181
+ token = await prompt(' 粘贴 Token: ');
182
+ }
183
+
184
+ // 用户名:通过 API 自动获取,避免手动输入
185
+ if (!username && token) {
186
+ try {
187
+ const res = await apiRequest({
188
+ hostname: GITLAB_HOST,
189
+ path: '/api/v4/user',
190
+ method: 'GET',
191
+ headers: { 'PRIVATE-TOKEN': token },
192
+ rejectUnauthorized: false,
193
+ });
194
+ if (res.body && res.body.username) {
195
+ username = res.body.username;
196
+ console.log(` ✓ 自动获取用户名: ${username}`);
197
+ }
198
+ } catch {}
158
199
  }
159
200
  if (!username) {
160
201
  username = await prompt(' GitLab 用户名: ');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai-worklog",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "AI 对话工作日志自动收集工具 —— 从 Claude Code / Codex 对话记录生成每日工作日志并推送到 GitLab",
5
5
  "bin": {
6
6
  "ai-worklog": "./bin/index.js"
@@ -278,21 +278,31 @@ def generate_summary(
278
278
  if total_sessions == 0:
279
279
  return _generate_empty_log(target_date)
280
280
 
281
- # 构建给 AI 的原始数据
281
+ # 构建给 AI 的原始数据(去重 + 截断,控制 token 消耗)
282
+ def dedup_and_trim(msgs: list[str], max_chars: int = 200) -> list[str]:
283
+ seen: set[str] = set()
284
+ result = []
285
+ for m in msgs:
286
+ key = m[:80] # 用前 80 字符去重
287
+ if key in seen:
288
+ continue
289
+ seen.add(key)
290
+ result.append(m if len(m) <= max_chars else m[:max_chars] + "…")
291
+ return result
292
+
282
293
  project_sections = []
283
294
  for proj_name, data in all_projects.items():
284
295
  msgs_parts = []
285
296
  if data["claude"]:
286
- msgs_parts.append(f"[Claude Code 对话 {len(data['claude'])} 条]")
287
- for i, m in enumerate(data["claude"], 1):
288
- # 截断过长消息
289
- msg = m if len(m) <= 500 else m[:500] + "..."
290
- msgs_parts.append(f" {i}. {msg}")
297
+ trimmed = dedup_and_trim(data["claude"])
298
+ msgs_parts.append(f"[Claude Code {len(data['claude'])} 条,去重后 {len(trimmed)} 条]")
299
+ for i, m in enumerate(trimmed, 1):
300
+ msgs_parts.append(f" {i}. {m}")
291
301
  if data["codex"]:
292
- msgs_parts.append(f"[Codex 对话 {len(data['codex'])} 条]")
293
- for i, m in enumerate(data["codex"], 1):
294
- msg = m if len(m) <= 500 else m[:500] + "..."
295
- msgs_parts.append(f" {i}. {msg}")
302
+ trimmed = dedup_and_trim(data["codex"])
303
+ msgs_parts.append(f"[Codex {len(data['codex'])} 条,去重后 {len(trimmed)} 条]")
304
+ for i, m in enumerate(trimmed, 1):
305
+ msgs_parts.append(f" {i}. {m}")
296
306
  project_sections.append(f"项目: {proj_name}\n" + "\n".join(msgs_parts))
297
307
 
298
308
  raw_data = "\n\n".join(project_sections)
@@ -462,7 +472,9 @@ def git_commit_and_push(log_file: Path, target_date: str, push: bool = True) ->
462
472
  """全自动 git init → GitLab 项目创建 → commit → push"""
463
473
 
464
474
  def run(cmd: list[str], check_err: bool = True) -> subprocess.CompletedProcess:
465
- r = subprocess.run(cmd, cwd=REPO_DIR, capture_output=True, text=True)
475
+ env = os.environ.copy()
476
+ env["LC_ALL"] = "C" # 强制英文输出,保证字符串匹配不受本地化影响
477
+ r = subprocess.run(cmd, cwd=REPO_DIR, capture_output=True, text=True, env=env)
466
478
  if check_err and r.returncode != 0:
467
479
  print(f"命令失败: {' '.join(cmd)}\n{r.stderr.strip()}", file=sys.stderr)
468
480
  return r
@@ -496,7 +508,7 @@ def git_commit_and_push(log_file: Path, target_date: str, push: bool = True) ->
496
508
  run(["git", "add", "-A"])
497
509
  run(["git", "commit", "-m", "init: 初始化工作日志仓库"])
498
510
 
499
- # ── 4. 提交日志 ────────────────────────────────────────────────────────────
511
+ # ── 4. 提交日志 ───────────────────────────────────────────────────────────
500
512
  rel_path = log_file.relative_to(REPO_DIR)
501
513
  print(f"Git: 添加 {rel_path}")
502
514
  if run(["git", "add", str(rel_path)]).returncode != 0: