@jackwener/opencli 1.5.5 → 1.5.6

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 (231) hide show
  1. package/README.md +27 -2
  2. package/README.zh-CN.md +36 -4
  3. package/dist/browser/daemon-client.d.ts +5 -1
  4. package/dist/browser/page.d.ts +6 -0
  5. package/dist/browser/page.js +15 -0
  6. package/dist/cli-manifest.json +1229 -67
  7. package/dist/clis/band/bands.d.ts +1 -0
  8. package/dist/clis/band/bands.js +72 -0
  9. package/dist/clis/band/mentions.d.ts +1 -0
  10. package/dist/clis/band/mentions.js +127 -0
  11. package/dist/clis/band/post.d.ts +1 -0
  12. package/dist/clis/band/post.js +175 -0
  13. package/dist/clis/band/posts.d.ts +1 -0
  14. package/dist/clis/band/posts.js +94 -0
  15. package/dist/clis/doubao/detail.d.ts +1 -0
  16. package/dist/clis/doubao/detail.js +33 -0
  17. package/dist/clis/doubao/detail.test.d.ts +1 -0
  18. package/dist/clis/doubao/detail.test.js +42 -0
  19. package/dist/clis/doubao/history.d.ts +1 -0
  20. package/dist/clis/doubao/history.js +28 -0
  21. package/dist/clis/doubao/history.test.d.ts +1 -0
  22. package/dist/clis/doubao/history.test.js +37 -0
  23. package/dist/clis/doubao/meeting-summary.d.ts +1 -0
  24. package/dist/clis/doubao/meeting-summary.js +39 -0
  25. package/dist/clis/doubao/meeting-transcript.d.ts +1 -0
  26. package/dist/clis/doubao/meeting-transcript.js +36 -0
  27. package/dist/clis/doubao/utils.d.ts +27 -0
  28. package/dist/clis/doubao/utils.js +317 -0
  29. package/dist/clis/doubao/utils.test.d.ts +1 -0
  30. package/dist/clis/doubao/utils.test.js +24 -0
  31. package/dist/clis/douyin/_shared/public-api.d.ts +33 -0
  32. package/dist/clis/douyin/_shared/public-api.js +29 -0
  33. package/dist/clis/douyin/user-videos.d.ts +5 -0
  34. package/dist/clis/douyin/user-videos.js +74 -0
  35. package/dist/clis/douyin/user-videos.test.d.ts +1 -0
  36. package/dist/clis/douyin/user-videos.test.js +108 -0
  37. package/dist/clis/ones/common.d.ts +32 -0
  38. package/dist/clis/ones/common.js +144 -0
  39. package/dist/clis/ones/enrich-tasks.d.ts +5 -0
  40. package/dist/clis/ones/enrich-tasks.js +37 -0
  41. package/dist/clis/ones/login.d.ts +1 -0
  42. package/dist/clis/ones/login.js +80 -0
  43. package/dist/clis/ones/logout.d.ts +1 -0
  44. package/dist/clis/ones/logout.js +17 -0
  45. package/dist/clis/ones/me.d.ts +1 -0
  46. package/dist/clis/ones/me.js +30 -0
  47. package/dist/clis/ones/my-tasks.d.ts +1 -0
  48. package/dist/clis/ones/my-tasks.js +120 -0
  49. package/dist/clis/ones/resolve-labels.d.ts +10 -0
  50. package/dist/clis/ones/resolve-labels.js +64 -0
  51. package/dist/clis/ones/task-helpers.d.ts +29 -0
  52. package/dist/clis/ones/task-helpers.js +212 -0
  53. package/dist/clis/ones/task-helpers.test.d.ts +1 -0
  54. package/dist/clis/ones/task-helpers.test.js +12 -0
  55. package/dist/clis/ones/task.d.ts +1 -0
  56. package/dist/clis/ones/task.js +66 -0
  57. package/dist/clis/ones/tasks.d.ts +1 -0
  58. package/dist/clis/ones/tasks.js +79 -0
  59. package/dist/clis/ones/token-info.d.ts +1 -0
  60. package/dist/clis/ones/token-info.js +42 -0
  61. package/dist/clis/ones/worklog.d.ts +11 -0
  62. package/dist/clis/ones/worklog.js +267 -0
  63. package/dist/clis/ones/worklog.test.d.ts +1 -0
  64. package/dist/clis/ones/worklog.test.js +20 -0
  65. package/dist/clis/spotify/spotify.d.ts +1 -0
  66. package/dist/clis/spotify/spotify.js +316 -0
  67. package/dist/clis/spotify/utils.d.ts +21 -0
  68. package/dist/clis/spotify/utils.js +66 -0
  69. package/dist/clis/spotify/utils.test.d.ts +1 -0
  70. package/dist/clis/spotify/utils.test.js +67 -0
  71. package/dist/clis/tieba/commands.test.d.ts +4 -0
  72. package/dist/clis/tieba/commands.test.js +79 -0
  73. package/dist/clis/tieba/hot.d.ts +1 -0
  74. package/dist/clis/tieba/hot.js +48 -0
  75. package/dist/clis/tieba/posts.d.ts +1 -0
  76. package/dist/clis/tieba/posts.js +85 -0
  77. package/dist/clis/tieba/read.d.ts +1 -0
  78. package/dist/clis/tieba/read.js +140 -0
  79. package/dist/clis/tieba/search.d.ts +1 -0
  80. package/dist/clis/tieba/search.js +108 -0
  81. package/dist/clis/tieba/utils.d.ts +101 -0
  82. package/dist/clis/tieba/utils.js +240 -0
  83. package/dist/clis/tieba/utils.test.d.ts +1 -0
  84. package/dist/clis/tieba/utils.test.js +290 -0
  85. package/dist/clis/weread/book.js +100 -13
  86. package/dist/clis/weread/commands.test.js +221 -0
  87. package/dist/clis/weread/private-api-regression.test.d.ts +1 -0
  88. package/dist/{weread-private-api-regression.test.js → clis/weread/private-api-regression.test.js} +92 -30
  89. package/dist/clis/weread/search-regression.test.d.ts +1 -0
  90. package/dist/clis/weread/search-regression.test.js +407 -0
  91. package/dist/clis/weread/search.js +143 -7
  92. package/dist/clis/weread/shelf.js +13 -95
  93. package/dist/clis/weread/utils.d.ts +46 -0
  94. package/dist/clis/weread/utils.js +214 -7
  95. package/dist/clis/weread/utils.test.js +71 -1
  96. package/dist/clis/xiaohongshu/publish.d.ts +1 -1
  97. package/dist/clis/xiaohongshu/publish.js +78 -31
  98. package/dist/clis/xiaohongshu/publish.test.js +66 -1
  99. package/dist/clis/xiaohongshu/user-helpers.d.ts +1 -0
  100. package/dist/clis/xiaohongshu/user-helpers.js +2 -0
  101. package/dist/clis/xiaohongshu/user-helpers.test.js +18 -0
  102. package/dist/clis/xueqiu/comments.d.ts +118 -0
  103. package/dist/clis/xueqiu/comments.js +354 -0
  104. package/dist/clis/xueqiu/comments.test.d.ts +1 -0
  105. package/dist/clis/xueqiu/comments.test.js +696 -0
  106. package/dist/clis/youtube/transcript.js +2 -4
  107. package/dist/clis/youtube/utils.d.ts +9 -0
  108. package/dist/clis/youtube/utils.js +67 -3
  109. package/dist/clis/youtube/utils.test.d.ts +1 -0
  110. package/dist/clis/youtube/utils.test.js +37 -0
  111. package/dist/clis/youtube/video.js +16 -15
  112. package/dist/clis/zsxq/dynamics.d.ts +1 -0
  113. package/dist/clis/zsxq/dynamics.js +47 -0
  114. package/dist/clis/zsxq/groups.d.ts +1 -0
  115. package/dist/clis/zsxq/groups.js +32 -0
  116. package/dist/clis/zsxq/search.d.ts +1 -0
  117. package/dist/clis/zsxq/search.js +43 -0
  118. package/dist/clis/zsxq/search.test.d.ts +1 -0
  119. package/dist/clis/zsxq/search.test.js +24 -0
  120. package/dist/clis/zsxq/topic.d.ts +1 -0
  121. package/dist/clis/zsxq/topic.js +47 -0
  122. package/dist/clis/zsxq/topic.test.d.ts +1 -0
  123. package/dist/clis/zsxq/topic.test.js +29 -0
  124. package/dist/clis/zsxq/topics.d.ts +1 -0
  125. package/dist/clis/zsxq/topics.js +25 -0
  126. package/dist/clis/zsxq/topics.test.d.ts +1 -0
  127. package/dist/clis/zsxq/topics.test.js +24 -0
  128. package/dist/clis/zsxq/utils.d.ts +97 -0
  129. package/dist/clis/zsxq/utils.js +230 -0
  130. package/dist/commanderAdapter.js +1 -1
  131. package/dist/commanderAdapter.test.js +39 -0
  132. package/dist/external-clis.yaml +17 -0
  133. package/dist/types.d.ts +5 -0
  134. package/docs/.vitepress/config.mts +3 -0
  135. package/docs/adapters/browser/band.md +63 -0
  136. package/docs/adapters/browser/ones.md +59 -0
  137. package/docs/adapters/browser/spotify.md +62 -0
  138. package/docs/adapters/browser/tieba.md +45 -0
  139. package/docs/adapters/browser/xueqiu.md +5 -0
  140. package/docs/adapters/browser/zsxq.md +49 -0
  141. package/docs/adapters/index.md +5 -2
  142. package/docs/adapters-doc/ones.md +32 -0
  143. package/extension/src/background.ts +15 -0
  144. package/extension/src/cdp.ts +42 -0
  145. package/extension/src/protocol.ts +5 -1
  146. package/package.json +1 -1
  147. package/scripts/postinstall.js +16 -0
  148. package/src/browser/daemon-client.ts +5 -1
  149. package/src/browser/page.ts +16 -0
  150. package/src/clis/band/bands.ts +76 -0
  151. package/src/clis/band/mentions.ts +134 -0
  152. package/src/clis/band/post.ts +187 -0
  153. package/src/clis/band/posts.ts +106 -0
  154. package/src/clis/doubao/detail.test.ts +53 -0
  155. package/src/clis/doubao/detail.ts +41 -0
  156. package/src/clis/doubao/history.test.ts +45 -0
  157. package/src/clis/doubao/history.ts +32 -0
  158. package/src/clis/doubao/meeting-summary.ts +53 -0
  159. package/src/clis/doubao/meeting-transcript.ts +48 -0
  160. package/src/clis/doubao/utils.test.ts +45 -0
  161. package/src/clis/doubao/utils.ts +371 -0
  162. package/src/clis/douyin/_shared/public-api.ts +84 -0
  163. package/src/clis/douyin/user-videos.test.ts +122 -0
  164. package/src/clis/douyin/user-videos.ts +101 -0
  165. package/src/clis/ones/common.ts +187 -0
  166. package/src/clis/ones/enrich-tasks.ts +47 -0
  167. package/src/clis/ones/login.ts +103 -0
  168. package/src/clis/ones/logout.ts +19 -0
  169. package/src/clis/ones/me.ts +34 -0
  170. package/src/clis/ones/my-tasks.ts +148 -0
  171. package/src/clis/ones/resolve-labels.ts +80 -0
  172. package/src/clis/ones/task-helpers.test.ts +14 -0
  173. package/src/clis/ones/task-helpers.ts +214 -0
  174. package/src/clis/ones/task.ts +79 -0
  175. package/src/clis/ones/tasks.ts +92 -0
  176. package/src/clis/ones/token-info.ts +46 -0
  177. package/src/clis/ones/worklog.test.ts +24 -0
  178. package/src/clis/ones/worklog.ts +306 -0
  179. package/src/clis/spotify/spotify.ts +328 -0
  180. package/src/clis/spotify/utils.test.ts +87 -0
  181. package/src/clis/spotify/utils.ts +92 -0
  182. package/src/clis/tieba/commands.test.ts +86 -0
  183. package/src/clis/tieba/hot.ts +52 -0
  184. package/src/clis/tieba/posts.ts +108 -0
  185. package/src/clis/tieba/read.ts +158 -0
  186. package/src/clis/tieba/search.ts +119 -0
  187. package/src/clis/tieba/utils.test.ts +322 -0
  188. package/src/clis/tieba/utils.ts +348 -0
  189. package/src/clis/weread/book.ts +116 -13
  190. package/src/clis/weread/commands.test.ts +249 -0
  191. package/src/{weread-private-api-regression.test.ts → clis/weread/private-api-regression.test.ts} +108 -30
  192. package/src/clis/weread/search-regression.test.ts +440 -0
  193. package/src/clis/weread/search.ts +189 -9
  194. package/src/clis/weread/shelf.ts +20 -122
  195. package/src/clis/weread/utils.test.ts +81 -1
  196. package/src/clis/weread/utils.ts +264 -7
  197. package/src/clis/xiaohongshu/publish.test.ts +79 -1
  198. package/src/clis/xiaohongshu/publish.ts +84 -30
  199. package/src/clis/xiaohongshu/user-helpers.test.ts +23 -0
  200. package/src/clis/xiaohongshu/user-helpers.ts +4 -0
  201. package/src/clis/xueqiu/comments.test.ts +823 -0
  202. package/src/clis/xueqiu/comments.ts +461 -0
  203. package/src/clis/youtube/transcript.ts +2 -4
  204. package/src/clis/youtube/utils.test.ts +43 -0
  205. package/src/clis/youtube/utils.ts +69 -0
  206. package/src/clis/youtube/video.ts +16 -15
  207. package/src/clis/zsxq/dynamics.ts +60 -0
  208. package/src/clis/zsxq/groups.ts +41 -0
  209. package/src/clis/zsxq/search.test.ts +29 -0
  210. package/src/clis/zsxq/search.ts +54 -0
  211. package/src/clis/zsxq/topic.test.ts +34 -0
  212. package/src/clis/zsxq/topic.ts +68 -0
  213. package/src/clis/zsxq/topics.test.ts +29 -0
  214. package/src/clis/zsxq/topics.ts +36 -0
  215. package/src/clis/zsxq/utils.ts +351 -0
  216. package/src/commanderAdapter.test.ts +47 -0
  217. package/src/commanderAdapter.ts +1 -1
  218. package/src/external-clis.yaml +17 -0
  219. package/src/types.ts +5 -0
  220. package/tests/e2e/band-auth.test.ts +20 -0
  221. package/tests/e2e/browser-auth-helpers.ts +18 -0
  222. package/tests/e2e/browser-auth.test.ts +35 -47
  223. package/tests/e2e/browser-public.test.ts +288 -0
  224. package/tests/e2e/management.test.ts +1 -1
  225. package/tests/e2e/plugin-management.test.ts +1 -1
  226. package/vitest.config.ts +1 -0
  227. package/SKILL.md +0 -879
  228. package/dist/weread-private-api-regression.test.d.ts +0 -1
  229. package/dist/weread-search-regression.test.d.ts +0 -1
  230. package/dist/weread-search-regression.test.js +0 -39
  231. package/src/weread-search-regression.test.ts +0 -44
@@ -0,0 +1,306 @@
1
+ /**
2
+ * Log/backfill work hours. Project API paths vary by deployment,
3
+ * so we try common endpoints in sequence.
4
+ */
5
+
6
+ import { cli, Strategy } from '../../registry.js';
7
+ import { CliError } from '../../errors.js';
8
+ import {
9
+ gotoOnesHome,
10
+ onesFetchInPageWithMeta,
11
+ resolveOnesUserUuid,
12
+ summarizeOnesError,
13
+ } from './common.js';
14
+ import { hoursToOnesManhourRaw } from './task-helpers.js';
15
+
16
+ function summarizeOnesMutationBody(parsed: unknown, status: number): string | null {
17
+ if (!parsed || typeof parsed !== 'object') {
18
+ return status >= 400 ? `HTTP ${status}` : null;
19
+ }
20
+ const o = parsed as Record<string, unknown>;
21
+ if (Array.isArray(o.errors) && o.errors.length > 0) {
22
+ const e0 = o.errors[0];
23
+ if (e0 && typeof e0 === 'object') {
24
+ const msg = String((e0 as Record<string, unknown>).message ?? '').trim();
25
+ if (msg) return msg;
26
+ }
27
+ return 'graphql errors';
28
+ }
29
+ if (o.data && typeof o.data === 'object') {
30
+ const data = o.data as Record<string, unknown>;
31
+ if (data.addManhour && typeof data.addManhour === 'object') {
32
+ const key = String((data.addManhour as Record<string, unknown>).key ?? '').trim();
33
+ if (!key) return 'addManhour returned empty key';
34
+ }
35
+ }
36
+ if (Array.isArray(o.bad_tasks) && o.bad_tasks.length > 0) {
37
+ const b = o.bad_tasks[0] as Record<string, unknown>;
38
+ return String(b.desc ?? b.code ?? JSON.stringify(b));
39
+ }
40
+ if (typeof o.reason === 'string' && o.reason.trim()) return o.reason.trim();
41
+ const c = o.code;
42
+ if (c !== undefined && c !== null) {
43
+ const n = Number(c);
44
+ if (Number.isFinite(n) && n !== 200 && n !== 0) return `code=${String(c)}`;
45
+ }
46
+ const ec = o.errcode;
47
+ if (typeof ec === 'string' && ec && ec !== 'OK') return ec;
48
+ return null;
49
+ }
50
+
51
+ function describeAttemptFailure(r: { ok: boolean; status: number; parsed: unknown }): string | null {
52
+ if (!r.ok) return summarizeOnesError(r.status, r.parsed);
53
+ return summarizeOnesMutationBody(r.parsed, r.status);
54
+ }
55
+
56
+ function todayLocalYmd(): string {
57
+ const d = new Date();
58
+ const y = d.getFullYear();
59
+ const m = String(d.getMonth() + 1).padStart(2, '0');
60
+ const day = String(d.getDate()).padStart(2, '0');
61
+ return `${y}-${m}-${day}`;
62
+ }
63
+
64
+ function validateYmd(s: string): boolean {
65
+ return /^\d{4}-\d{2}-\d{2}$/.test(s);
66
+ }
67
+
68
+ function toLocalMidnightUnixSeconds(ymd: string): number {
69
+ const d = new Date(`${ymd}T00:00:00`);
70
+ const ms = d.getTime();
71
+ if (!Number.isFinite(ms)) return 0;
72
+ return Math.floor(ms / 1000);
73
+ }
74
+
75
+ function pickTaskTotalManhourRaw(parsed: unknown): number | null {
76
+ if (!parsed || typeof parsed !== 'object') return null;
77
+ const o = parsed as Record<string, unknown>;
78
+ const n = Number(o.total_manhour);
79
+ return Number.isFinite(n) ? n : null;
80
+ }
81
+
82
+ export function buildAddManhourGraphqlBody(input: {
83
+ ownerId: string;
84
+ taskId: string;
85
+ startTime: number;
86
+ rawManhour: number;
87
+ note: string;
88
+ }): string {
89
+ const { ownerId, taskId, startTime, rawManhour, note } = input;
90
+ const description = JSON.stringify(note);
91
+ const owner = JSON.stringify(ownerId);
92
+ const task = JSON.stringify(taskId);
93
+
94
+ return JSON.stringify({
95
+ query: `mutation AddManhour {
96
+ addManhour(
97
+ mode: "simple"
98
+ owner: ${owner}
99
+ task: ${task}
100
+ type: "recorded"
101
+ start_time: ${startTime}
102
+ hours: ${rawManhour}
103
+ description: ${description}
104
+ customData: {}
105
+ ) {
106
+ key
107
+ }
108
+ }`,
109
+ });
110
+ }
111
+
112
+ cli({
113
+ site: 'ones',
114
+ name: 'worklog',
115
+ description:
116
+ 'ONES — log work hours on a task (defaults to today; use --date to backfill; endpoint falls back by deployment).',
117
+ domain: 'ones.cn',
118
+ strategy: Strategy.COOKIE,
119
+ browser: true,
120
+ navigateBefore: false,
121
+ args: [
122
+ {
123
+ name: 'task',
124
+ type: 'str',
125
+ required: true,
126
+ positional: true,
127
+ help: 'Work item UUID (usually 16 chars), from my-tasks or browser URL …/task/<id>',
128
+ },
129
+ {
130
+ name: 'hours',
131
+ type: 'str',
132
+ required: true,
133
+ positional: true,
134
+ help: 'Hours to log for this entry (e.g. 2 or 1.5), converted with ONES_MANHOUR_SCALE',
135
+ },
136
+ {
137
+ name: 'team',
138
+ type: 'str',
139
+ required: false,
140
+ help: 'Team UUID from URL …/team/<uuid>/…, or set ONES_TEAM_UUID',
141
+ },
142
+ {
143
+ name: 'date',
144
+ type: 'str',
145
+ required: false,
146
+ help: 'Entry date YYYY-MM-DD, defaults to today (local timezone); use for backfill',
147
+ },
148
+ {
149
+ name: 'note',
150
+ type: 'str',
151
+ required: false,
152
+ help: 'Optional note (written to description/desc)',
153
+ },
154
+ {
155
+ name: 'owner',
156
+ type: 'str',
157
+ required: false,
158
+ help: 'Owner user UUID (defaults to current logged-in user)',
159
+ },
160
+ ],
161
+ columns: ['task', 'date', 'hours', 'owner', 'endpoint'],
162
+
163
+ func: async (page, kwargs) => {
164
+ const taskId = String(kwargs.task ?? '').trim();
165
+ if (!taskId) {
166
+ throw new CliError('CONFIG', 'task uuid required', 'Pass the work item uuid from opencli ones my-tasks or the URL.');
167
+ }
168
+
169
+ const team =
170
+ (kwargs.team as string | undefined)?.trim() ||
171
+ process.env.ONES_TEAM_UUID?.trim() ||
172
+ process.env.ONES_TEAM_ID?.trim();
173
+ if (!team) {
174
+ throw new CliError(
175
+ 'CONFIG',
176
+ 'team UUID required',
177
+ 'Pass --team or set ONES_TEAM_UUID (from …/team/<team>/…).',
178
+ );
179
+ }
180
+
181
+ const hoursHuman = Number(String(kwargs.hours ?? '').replace(/,/g, ''));
182
+ if (!Number.isFinite(hoursHuman) || hoursHuman <= 0 || hoursHuman > 1000) {
183
+ throw new CliError(
184
+ 'CONFIG',
185
+ 'hours must be a positive number (hours)',
186
+ 'Example: opencli ones worklog <taskUUID> 2 --team <teamUUID>',
187
+ );
188
+ }
189
+
190
+ const dateArg = (kwargs.date as string | undefined)?.trim();
191
+ const dateStr = dateArg || todayLocalYmd();
192
+ if (!validateYmd(dateStr)) {
193
+ throw new CliError('CONFIG', 'invalid --date', 'Use YYYY-MM-DD, e.g. 2026-03-24.');
194
+ }
195
+
196
+ const note = String(kwargs.note ?? '').trim();
197
+ const rawManhour = hoursToOnesManhourRaw(hoursHuman);
198
+ const startTime = toLocalMidnightUnixSeconds(dateStr);
199
+ if (!startTime) {
200
+ throw new CliError('CONFIG', 'invalid date for start_time', `Could not parse date ${dateStr}.`);
201
+ }
202
+
203
+ await gotoOnesHome(page);
204
+
205
+ const ownerFromKw = (kwargs.owner as string | undefined)?.trim();
206
+ const ownerId = ownerFromKw || (await resolveOnesUserUuid(page, { skipGoto: true }));
207
+
208
+ const entry: Record<string, unknown> = {
209
+ owner: ownerId,
210
+ manhour: rawManhour,
211
+ start_date: dateStr,
212
+ end_date: dateStr,
213
+ desc: note,
214
+ };
215
+ const entryAlt: Record<string, unknown> = {
216
+ owner: ownerId,
217
+ allManhour: rawManhour,
218
+ startDate: dateStr,
219
+ endDate: dateStr,
220
+ desc: note,
221
+ };
222
+
223
+ const enc = encodeURIComponent(taskId);
224
+ const gqlBody = buildAddManhourGraphqlBody({
225
+ ownerId,
226
+ taskId,
227
+ startTime,
228
+ rawManhour,
229
+ note,
230
+ });
231
+ const attempts: { path: string; body: string }[] = [
232
+ { path: `team/${team}/items/graphql`, body: gqlBody },
233
+ { path: `team/${team}/task/${enc}/manhours/add`, body: JSON.stringify(entry) },
234
+ { path: `team/${team}/task/${enc}/manhours/add`, body: JSON.stringify(entryAlt) },
235
+ { path: `team/${team}/task/${enc}/manhours/add`, body: JSON.stringify({ manhours: [entry] }) },
236
+ { path: `team/${team}/task/${enc}/manhours/add`, body: JSON.stringify({ manhours: [entryAlt] }) },
237
+ { path: `team/${team}/task/${enc}/manhour/add`, body: JSON.stringify(entry) },
238
+ { path: `team/${team}/task/${enc}/manhour/add`, body: JSON.stringify(entryAlt) },
239
+ {
240
+ path: `team/${team}/tasks/update3`,
241
+ body: JSON.stringify({
242
+ tasks: [{ uuid: taskId, manhours: [entry] }],
243
+ }),
244
+ },
245
+ ];
246
+
247
+ const beforeInfo = await onesFetchInPageWithMeta(page, `team/${team}/task/${enc}/info`, {
248
+ method: 'GET',
249
+ skipGoto: true,
250
+ });
251
+ const beforeTotal = beforeInfo.ok ? pickTaskTotalManhourRaw(beforeInfo.parsed) : null;
252
+
253
+ let lastDetail = '';
254
+ for (const { path, body } of attempts) {
255
+ const r = await onesFetchInPageWithMeta(page, path, {
256
+ method: 'POST',
257
+ body,
258
+ skipGoto: true,
259
+ });
260
+ const fail = describeAttemptFailure(r);
261
+ if (!fail) {
262
+ // Guard against false success: HTTP 200 but no actual manhour change.
263
+ const afterInfo = await onesFetchInPageWithMeta(page, `team/${team}/task/${enc}/info`, {
264
+ method: 'GET',
265
+ skipGoto: true,
266
+ });
267
+ if (afterInfo.ok) {
268
+ const afterTotal = pickTaskTotalManhourRaw(afterInfo.parsed);
269
+ const changed =
270
+ beforeTotal === null
271
+ ? afterTotal !== null
272
+ : afterTotal !== null && Math.abs(afterTotal - beforeTotal) >= 1;
273
+ if (changed) {
274
+ return [
275
+ {
276
+ task: taskId,
277
+ date: dateStr,
278
+ hours: String(hoursHuman),
279
+ owner: ownerId,
280
+ endpoint: path,
281
+ },
282
+ ];
283
+ }
284
+ lastDetail = `no effect (total_manhour ${String(beforeTotal)} -> ${String(afterTotal)})`;
285
+ continue;
286
+ }
287
+ // If verification read fails, return success conservatively.
288
+ return [
289
+ {
290
+ task: taskId,
291
+ date: dateStr,
292
+ hours: String(hoursHuman),
293
+ owner: ownerId,
294
+ endpoint: path,
295
+ },
296
+ ];
297
+ }
298
+ lastDetail = fail;
299
+ }
300
+
301
+ throw new CliError(
302
+ 'FETCH_ERROR',
303
+ `ONES worklog: all endpoints failed (last: ${lastDetail})`,
304
+ );
305
+ },
306
+ });
@@ -0,0 +1,328 @@
1
+ import { cli, Strategy } from '../../registry.js';
2
+ import { CliError } from '../../errors.js';
3
+ import { readFileSync, writeFileSync, mkdirSync, existsSync } from 'fs';
4
+ import { createServer } from 'http';
5
+ import { homedir } from 'os';
6
+ import { join } from 'path';
7
+ import { exec } from 'child_process';
8
+ import {
9
+ assertSpotifyCredentialsConfigured,
10
+ getFirstSpotifyTrack,
11
+ mapSpotifyTrackResults,
12
+ parseDotEnv,
13
+ resolveSpotifyCredentials,
14
+ } from './utils.js';
15
+
16
+ // ── Credentials ───────────────────────────────────────────────────────────────
17
+ // Set SPOTIFY_CLIENT_ID and SPOTIFY_CLIENT_SECRET as environment variables,
18
+ // or place them in ~/.opencli/spotify.env:
19
+ // SPOTIFY_CLIENT_ID=your_id
20
+ // SPOTIFY_CLIENT_SECRET=your_secret
21
+
22
+ const ENV_FILE = join(homedir(), '.opencli', 'spotify.env');
23
+
24
+ function loadEnv(): Record<string, string> {
25
+ if (!existsSync(ENV_FILE)) return {};
26
+ return parseDotEnv(readFileSync(ENV_FILE, 'utf-8'));
27
+ }
28
+
29
+ const env = loadEnv();
30
+ const credentials = resolveSpotifyCredentials(env);
31
+ const CLIENT_ID = credentials.clientId;
32
+ const CLIENT_SECRET = credentials.clientSecret;
33
+ const REDIRECT_URI = 'http://127.0.0.1:8888/callback';
34
+ const SCOPES = [
35
+ 'user-read-playback-state',
36
+ 'user-modify-playback-state',
37
+ 'user-read-currently-playing',
38
+ 'playlist-read-private',
39
+ ].join(' ');
40
+
41
+ // ── Token storage ─────────────────────────────────────────────────────────────
42
+
43
+ const TOKEN_FILE = join(homedir(), '.opencli', 'spotify-tokens.json');
44
+
45
+ interface Tokens {
46
+ access_token: string;
47
+ refresh_token: string;
48
+ expires_at: number;
49
+ }
50
+
51
+ function loadTokens(): Tokens | null {
52
+ try { return JSON.parse(readFileSync(TOKEN_FILE, 'utf-8')); } catch { return null; }
53
+ }
54
+
55
+ function saveTokens(tokens: Tokens): void {
56
+ mkdirSync(join(homedir(), '.opencli'), { recursive: true });
57
+ writeFileSync(TOKEN_FILE, JSON.stringify(tokens, null, 2));
58
+ }
59
+
60
+ async function refreshAccessToken(refreshToken: string): Promise<string> {
61
+ const res = await fetch('https://accounts.spotify.com/api/token', {
62
+ method: 'POST',
63
+ headers: {
64
+ 'Content-Type': 'application/x-www-form-urlencoded',
65
+ Authorization: 'Basic ' + Buffer.from(`${CLIENT_ID}:${CLIENT_SECRET}`).toString('base64'),
66
+ },
67
+ body: new URLSearchParams({ grant_type: 'refresh_token', refresh_token: refreshToken }),
68
+ });
69
+ if (!res.ok) {
70
+ const err = await res.json().catch(() => ({})) as any;
71
+ throw new CliError('REFRESH_FAILED', err?.error_description || `Token refresh failed (${res.status})`);
72
+ }
73
+ const data = await res.json() as any;
74
+ const tokens: Tokens = {
75
+ access_token: data.access_token,
76
+ refresh_token: data.refresh_token || refreshToken,
77
+ expires_at: Date.now() + data.expires_in * 1000,
78
+ };
79
+ saveTokens(tokens);
80
+ return tokens.access_token;
81
+ }
82
+
83
+ async function getToken(): Promise<string> {
84
+ const tokens = loadTokens();
85
+ if (!tokens) throw new CliError('AUTH_REQUIRED', 'Not authenticated. Run: opencli spotify auth');
86
+ if (!tokens.access_token || !tokens.refresh_token || !(tokens.expires_at > 0)) {
87
+ throw new CliError('AUTH_CORRUPTED', 'Token file is corrupted. Run: opencli spotify auth');
88
+ }
89
+ if (Date.now() > tokens.expires_at - 60_000) return refreshAccessToken(tokens.refresh_token);
90
+ return tokens.access_token;
91
+ }
92
+
93
+ // ── Spotify API helper ────────────────────────────────────────────────────────
94
+
95
+ async function api(method: string, path: string, body?: unknown): Promise<any> {
96
+ const token = await getToken();
97
+ const res = await fetch(`https://api.spotify.com/v1${path}`, {
98
+ method,
99
+ headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' },
100
+ body: body ? JSON.stringify(body) : undefined,
101
+ });
102
+ if (res.status === 204 || res.status === 202) return null;
103
+ if (!res.ok) {
104
+ const err = await res.json().catch(() => ({})) as any;
105
+ throw new CliError('API_ERROR', err?.error?.message || `Spotify API error ${res.status}`);
106
+ }
107
+ return res.json();
108
+ }
109
+
110
+ async function findTrackUri(query: string): Promise<{ uri: string; name: string; artist: string }> {
111
+ const data = await api('GET', `/search?q=${encodeURIComponent(query)}&type=track&limit=1`);
112
+ const track = getFirstSpotifyTrack(data);
113
+ if (!track) throw new CliError('EMPTY_RESULT', `No track found for: ${query}`);
114
+ return track;
115
+ }
116
+
117
+ function openBrowser(url: string): void {
118
+ const cmd = process.platform === 'win32' ? `start "" "${url}"` : process.platform === 'darwin' ? `open "${url}"` : `xdg-open "${url}"`;
119
+ exec(cmd);
120
+ }
121
+
122
+ // ── Commands ──────────────────────────────────────────────────────────────────
123
+
124
+ cli({
125
+ site: 'spotify',
126
+ name: 'auth',
127
+ description: 'Authenticate with Spotify (OAuth — run once)',
128
+ strategy: Strategy.PUBLIC,
129
+ browser: false,
130
+ args: [],
131
+ columns: ['status'],
132
+ func: async () => {
133
+ assertSpotifyCredentialsConfigured(credentials, ENV_FILE);
134
+ return new Promise((resolve, reject) => {
135
+ const server = createServer(async (req, res) => {
136
+ try {
137
+ const url = new URL(req.url!, 'http://localhost:8888');
138
+ if (url.pathname !== '/callback') { res.end(); return; }
139
+ const code = url.searchParams.get('code');
140
+ if (!code) { res.end('Missing code'); return; }
141
+ const tokenRes = await fetch('https://accounts.spotify.com/api/token', {
142
+ method: 'POST',
143
+ headers: {
144
+ 'Content-Type': 'application/x-www-form-urlencoded',
145
+ Authorization: 'Basic ' + Buffer.from(`${CLIENT_ID}:${CLIENT_SECRET}`).toString('base64'),
146
+ },
147
+ body: new URLSearchParams({ grant_type: 'authorization_code', code, redirect_uri: REDIRECT_URI }),
148
+ });
149
+ if (!tokenRes.ok) {
150
+ const err = await tokenRes.json().catch(() => ({})) as any;
151
+ server.close();
152
+ reject(new CliError('AUTH_FAILED', err?.error_description || `Token exchange failed (${tokenRes.status})`));
153
+ return;
154
+ }
155
+ const data = await tokenRes.json() as any;
156
+ saveTokens({ access_token: data.access_token, refresh_token: data.refresh_token, expires_at: Date.now() + data.expires_in * 1000 });
157
+ res.writeHead(200, { 'Content-Type': 'text/html' });
158
+ res.end('<h2>Spotify authenticated! You can close this tab.</h2>');
159
+ server.close();
160
+ resolve([{ status: 'Authenticated successfully' }]);
161
+ } catch (e) { server.close(); reject(e); }
162
+ });
163
+ server.on('error', (e: NodeJS.ErrnoException) => {
164
+ if (e.code === 'EADDRINUSE') reject(new CliError('PORT_IN_USE', 'Port 8888 is already in use. Stop the other process and retry.'));
165
+ else reject(e);
166
+ });
167
+ const timeout = setTimeout(() => { server.close(); reject(new CliError('AUTH_TIMEOUT', 'Authentication timed out after 5 minutes')); }, 5 * 60 * 1000);
168
+ server.listen(8888, () => {
169
+ const authUrl = `https://accounts.spotify.com/authorize?${new URLSearchParams({ client_id: CLIENT_ID, response_type: 'code', redirect_uri: REDIRECT_URI, scope: SCOPES })}`;
170
+ console.log('Opening browser for Spotify login...');
171
+ console.log('If it does not open, visit:', authUrl);
172
+ openBrowser(authUrl);
173
+ });
174
+ server.on('close', () => clearTimeout(timeout));
175
+ });
176
+ },
177
+ });
178
+
179
+ cli({
180
+ site: 'spotify',
181
+ name: 'status',
182
+ description: 'Show current playback status',
183
+ strategy: Strategy.PUBLIC,
184
+ browser: false,
185
+ args: [],
186
+ columns: ['track', 'artist', 'album', 'status', 'progress'],
187
+ func: async () => {
188
+ const data = await api('GET', '/me/player');
189
+ if (!data || !data.item) return [{ track: 'Nothing playing', artist: '', album: '', status: '', progress: '' }];
190
+ const t = data.item;
191
+ if (t.type !== 'track') return [{ track: t.name, artist: '', album: t.show?.name ?? '', status: data.is_playing ? 'playing' : 'paused', progress: '' }];
192
+ const prog = (data.progress_ms ?? 0) / 1000 | 0;
193
+ const dur = t.duration_ms / 1000 | 0;
194
+ const fmt = (s: number) => `${s / 60 | 0}:${String(s % 60).padStart(2, '0')}`;
195
+ return [{ track: t.name, artist: t.artists.map((a: any) => a.name).join(', '), album: t.album.name, status: data.is_playing ? 'playing' : 'paused', progress: `${fmt(prog)} / ${fmt(dur)}` }];
196
+ },
197
+ });
198
+
199
+ cli({
200
+ site: 'spotify',
201
+ name: 'play',
202
+ description: 'Resume playback or search and play a track/artist',
203
+ strategy: Strategy.PUBLIC,
204
+ browser: false,
205
+ args: [{ name: 'query', type: 'str', default: '', positional: true, help: 'Track or artist to play (optional)' }],
206
+ columns: ['track', 'artist', 'status'],
207
+ func: async (_page, kwargs) => {
208
+ if (kwargs.query) {
209
+ const { uri, name, artist } = await findTrackUri(kwargs.query);
210
+ await api('PUT', '/me/player/play', { uris: [uri] });
211
+ return [{ track: name, artist, status: 'playing' }];
212
+ }
213
+ await api('PUT', '/me/player/play');
214
+ return [{ track: '', artist: '', status: 'resumed' }];
215
+ },
216
+ });
217
+
218
+ cli({
219
+ site: 'spotify',
220
+ name: 'pause',
221
+ description: 'Pause playback',
222
+ strategy: Strategy.PUBLIC,
223
+ browser: false,
224
+ args: [],
225
+ columns: ['status'],
226
+ func: async () => { await api('PUT', '/me/player/pause'); return [{ status: 'paused' }]; },
227
+ });
228
+
229
+ cli({
230
+ site: 'spotify',
231
+ name: 'next',
232
+ description: 'Skip to next track',
233
+ strategy: Strategy.PUBLIC,
234
+ browser: false,
235
+ args: [],
236
+ columns: ['status'],
237
+ func: async () => { await api('POST', '/me/player/next'); return [{ status: 'skipped to next' }]; },
238
+ });
239
+
240
+ cli({
241
+ site: 'spotify',
242
+ name: 'prev',
243
+ description: 'Skip to previous track',
244
+ strategy: Strategy.PUBLIC,
245
+ browser: false,
246
+ args: [],
247
+ columns: ['status'],
248
+ func: async () => { await api('POST', '/me/player/previous'); return [{ status: 'skipped to previous' }]; },
249
+ });
250
+
251
+ cli({
252
+ site: 'spotify',
253
+ name: 'volume',
254
+ description: 'Set playback volume (0-100)',
255
+ strategy: Strategy.PUBLIC,
256
+ browser: false,
257
+ args: [{ name: 'level', type: 'int', default: 50, positional: true, required: true, help: 'Volume 0–100' }],
258
+ columns: ['volume'],
259
+ func: async (_page, kwargs) => {
260
+ const level = Math.round(kwargs.level);
261
+ if (level < 0 || level > 100) throw new CliError('INVALID_ARGS', 'Volume must be between 0 and 100');
262
+ await api('PUT', `/me/player/volume?volume_percent=${level}`);
263
+ return [{ volume: `${level}%` }];
264
+ },
265
+ });
266
+
267
+ cli({
268
+ site: 'spotify',
269
+ name: 'search',
270
+ description: 'Search for tracks',
271
+ strategy: Strategy.PUBLIC,
272
+ browser: false,
273
+ args: [
274
+ { name: 'query', type: 'str', required: true, positional: true, help: 'Search query' },
275
+ { name: 'limit', type: 'int', default: 10, help: 'Number of results (default: 10)' },
276
+ ],
277
+ columns: ['track', 'artist', 'album', 'uri'],
278
+ func: async (_page, kwargs) => {
279
+ const limit = Math.min(50, Math.max(1, Math.round(kwargs.limit)));
280
+ const data = await api('GET', `/search?q=${encodeURIComponent(kwargs.query)}&type=track&limit=${limit}`);
281
+ const results = mapSpotifyTrackResults(data);
282
+ if (!results.length) throw new CliError('EMPTY_RESULT', `No results found for: ${kwargs.query}`);
283
+ return results;
284
+ },
285
+ });
286
+
287
+ cli({
288
+ site: 'spotify',
289
+ name: 'queue',
290
+ description: 'Add a track to the playback queue',
291
+ strategy: Strategy.PUBLIC,
292
+ browser: false,
293
+ args: [{ name: 'query', type: 'str', required: true, positional: true, help: 'Track to add to queue' }],
294
+ columns: ['track', 'artist', 'status'],
295
+ func: async (_page, kwargs) => {
296
+ const { uri, name, artist } = await findTrackUri(kwargs.query);
297
+ await api('POST', `/me/player/queue?uri=${encodeURIComponent(uri)}`);
298
+ return [{ track: name, artist, status: 'added to queue' }];
299
+ },
300
+ });
301
+
302
+ cli({
303
+ site: 'spotify',
304
+ name: 'shuffle',
305
+ description: 'Toggle shuffle on/off',
306
+ strategy: Strategy.PUBLIC,
307
+ browser: false,
308
+ args: [{ name: 'state', type: 'str', default: 'on', positional: true, choices: ['on', 'off'], help: 'on or off' }],
309
+ columns: ['shuffle'],
310
+ func: async (_page, kwargs) => {
311
+ await api('PUT', `/me/player/shuffle?state=${kwargs.state === 'on'}`);
312
+ return [{ shuffle: kwargs.state }];
313
+ },
314
+ });
315
+
316
+ cli({
317
+ site: 'spotify',
318
+ name: 'repeat',
319
+ description: 'Set repeat mode (off / track / context)',
320
+ strategy: Strategy.PUBLIC,
321
+ browser: false,
322
+ args: [{ name: 'mode', type: 'str', default: 'context', positional: true, choices: ['off', 'track', 'context'], help: 'off / track / context' }],
323
+ columns: ['repeat'],
324
+ func: async (_page, kwargs) => {
325
+ await api('PUT', `/me/player/repeat?state=${kwargs.mode}`);
326
+ return [{ repeat: kwargs.mode }];
327
+ },
328
+ });