@zhin.js/adapter-juejin 0.0.0-stage → 0.1.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 (65) hide show
  1. package/README.md +145 -2
  2. package/adapters/juejin/index.js +19 -0
  3. package/adapters/juejin/index.ts +22 -0
  4. package/commands/juejin-draft/drop/[id]/index.js +13 -0
  5. package/commands/juejin-draft/drop/[id]/index.ts +13 -0
  6. package/commands/juejin-draft/edit/[id]/index.js +16 -0
  7. package/commands/juejin-draft/edit/[id]/index.ts +15 -0
  8. package/commands/juejin-draft/list/index.js +12 -0
  9. package/commands/juejin-draft/list/index.ts +12 -0
  10. package/commands/juejin-draft/send/[id]/index.js +13 -0
  11. package/commands/juejin-draft/send/[id]/index.ts +13 -0
  12. package/commands/juejin-draft/show/[id]/index.js +13 -0
  13. package/commands/juejin-draft/show/[id]/index.ts +13 -0
  14. package/commands/juejin-login/status/index.js +12 -0
  15. package/commands/juejin-login/status/index.ts +12 -0
  16. package/lib/adapter.d.ts +22 -0
  17. package/lib/adapter.js +43 -0
  18. package/lib/client.d.ts +83 -0
  19. package/lib/client.js +192 -0
  20. package/lib/console.d.ts +16 -0
  21. package/lib/console.js +172 -0
  22. package/lib/drafts.d.ts +50 -0
  23. package/lib/drafts.js +137 -0
  24. package/lib/endpoint-directory.d.ts +16 -0
  25. package/lib/endpoint-directory.js +63 -0
  26. package/lib/endpoint.d.ts +86 -0
  27. package/lib/endpoint.js +440 -0
  28. package/lib/index.d.ts +11 -0
  29. package/lib/index.js +11 -0
  30. package/lib/juejin-runtime-state.d.ts +4 -0
  31. package/lib/juejin-runtime-state.js +3 -0
  32. package/lib/login-text.d.ts +5 -0
  33. package/lib/login-text.js +25 -0
  34. package/lib/protocol.d.ts +192 -0
  35. package/lib/protocol.js +214 -0
  36. package/lib/review.d.ts +29 -0
  37. package/lib/review.js +156 -0
  38. package/lib/self-profile.d.ts +9 -0
  39. package/lib/self-profile.js +14 -0
  40. package/lib/storage.d.ts +11 -0
  41. package/lib/storage.js +54 -0
  42. package/package.json +113 -3
  43. package/pages/index/CommentReview.js +140 -0
  44. package/pages/index/CommentReview.tsx +264 -0
  45. package/pages/index/index.js +18 -0
  46. package/pages/index/index.tsx +18 -0
  47. package/pages/index/messages.js +57 -0
  48. package/pages/index/messages.ts +59 -0
  49. package/pages/index/rpc.js +31 -0
  50. package/pages/index/rpc.ts +72 -0
  51. package/plugin.js +33 -0
  52. package/schema.json +485 -0
  53. package/src/adapter.ts +45 -0
  54. package/src/client.ts +247 -0
  55. package/src/console.ts +161 -0
  56. package/src/drafts.ts +180 -0
  57. package/src/endpoint-directory.ts +64 -0
  58. package/src/endpoint.ts +504 -0
  59. package/src/index.ts +50 -0
  60. package/src/juejin-runtime-state.ts +11 -0
  61. package/src/login-text.ts +28 -0
  62. package/src/protocol.ts +377 -0
  63. package/src/review.ts +149 -0
  64. package/src/self-profile.ts +20 -0
  65. package/src/storage.ts +52 -0
@@ -0,0 +1,57 @@
1
+ // Generated by build-plugin-runtime-entries.mjs. Do not edit.
2
+ const english = {
3
+ '掘金评论审批': 'Juejin comment review',
4
+ 'AI 写好的掘金评论回复先停在这里,你点「发送」才会真的发出去。': 'AI-written Juejin replies wait here. Nothing is posted until you press Send.',
5
+ '加载中…': 'Loading…',
6
+ '掘金适配器还没有启动。请先在配置里添加 juejin 插件和至少一个账号。': 'The Juejin adapter has not started. Add the juejin plugin and at least one account to the config first.',
7
+ '账号 {0}': 'Account {0}',
8
+ '已登录': 'Signed in',
9
+ '未登录': 'Not signed in',
10
+ '登录已失效': 'Login expired',
11
+ 'Cookie 来自配置 / 环境变量': 'Cookie from config or environment',
12
+ '未登录时只能读取公开评论,不能发送回复。请用 JUEJIN_COOKIE 环境变量注入 Cookie。': 'Without signing in, only public comments are read and replies cannot be posted. Inject the cookie with the JUEJIN_COOKIE environment variable.',
13
+ 'Cookie 已失效,草稿都保留着。更新 JUEJIN_COOKIE 后重启就能继续发送。': 'The cookie has expired. Drafts are kept; update JUEJIN_COOKIE and restart to keep sending.',
14
+ '重新检查登录': 'Check login again',
15
+ 'Cookie 只来自配置 / 环境变量,不会经过这个页面,也不会写进数据目录。': 'The cookie only comes from config or environment; it never passes through this page or the data directory.',
16
+ '触发了掘金风控或账号异常(错误码 {0}),已暂停轮询和发送。请到网页端确认账号状态,处理后重启。': 'Juejin risk control or an account problem was detected (code {0}). Polling and sending are paused. Check the account on the website, then restart.',
17
+ '出站已关闭(sendMode: off),草稿无法发送。': 'Outbound is off (sendMode: off); drafts cannot be sent.',
18
+ '管理的文章': 'Watched articles',
19
+ '还没有配置文章。要管理哪些文章请在配置的 articles 里填写文章 id 或链接。': 'No articles configured. Add article IDs or links under articles in the config.',
20
+ '文章列表来自配置,要增删请改配置文件。': 'This list comes from the config; edit the config file to change it.',
21
+ '待审回复': 'Replies awaiting review',
22
+ '没有待审的回复。新的 AI 回复会自动出现在这里。': 'No replies awaiting review. New AI replies show up here automatically.',
23
+ '草稿只存在内存里,重启会丢失。': 'Drafts are kept in memory only and are lost on restart.',
24
+ '原评论': 'Original comment',
25
+ '拟回复': 'Proposed reply',
26
+ '上次发送的结果未知,这条可能已经发出。请先到掘金文章评论区确认,避免重复回复。': 'The last send outcome is unknown, so this may already be posted. Check the Juejin article comments first to avoid a duplicate.',
27
+ '发送中…': 'Sending…',
28
+ '发送': 'Send',
29
+ '修改': 'Edit',
30
+ '保存': 'Save',
31
+ '取消': 'Cancel',
32
+ '丢弃': 'Discard',
33
+ '确定丢弃这条回复?不会发送。': 'Discard this reply? It will not be posted.',
34
+ '打开文章': 'Open article',
35
+ '已发出': 'Posted',
36
+ '查看': 'View',
37
+ '已丢弃': 'Discarded',
38
+ '已保存修改': 'Changes saved',
39
+ '操作失败': 'Action failed',
40
+ '{0} 字': '{0} chars',
41
+ '刚刚': 'just now',
42
+ '{0} 分钟前': '{0} min ago',
43
+ '{0} 小时前': '{0} h ago',
44
+ '{0} 天前': '{0} d ago',
45
+ '刷新': 'Refresh',
46
+ '文章 {0}': 'Article {0}',
47
+ };
48
+ export function detectLocale() {
49
+ if (typeof document === 'undefined')
50
+ return 'zh-CN';
51
+ const lang = document.documentElement.lang || (typeof navigator !== 'undefined' ? navigator.language : '');
52
+ return /^en\b/i.test(lang) ? 'en' : 'zh-CN';
53
+ }
54
+ export function text(locale, key, ...values) {
55
+ const template = locale === 'en' ? english[key] ?? key : key;
56
+ return template.replace(/\{(\d+)\}/g, (_match, index) => String(values[Number(index)] ?? ''));
57
+ }
@@ -0,0 +1,59 @@
1
+ export type ReviewLocale = 'zh-CN' | 'en';
2
+
3
+ const english: Readonly<Record<string, string>> = {
4
+ '掘金评论审批': 'Juejin comment review',
5
+ 'AI 写好的掘金评论回复先停在这里,你点「发送」才会真的发出去。': 'AI-written Juejin replies wait here. Nothing is posted until you press Send.',
6
+ '加载中…': 'Loading…',
7
+ '掘金适配器还没有启动。请先在配置里添加 juejin 插件和至少一个账号。': 'The Juejin adapter has not started. Add the juejin plugin and at least one account to the config first.',
8
+ '账号 {0}': 'Account {0}',
9
+ '已登录': 'Signed in',
10
+ '未登录': 'Not signed in',
11
+ '登录已失效': 'Login expired',
12
+ 'Cookie 来自配置 / 环境变量': 'Cookie from config or environment',
13
+ '未登录时只能读取公开评论,不能发送回复。请用 JUEJIN_COOKIE 环境变量注入 Cookie。': 'Without signing in, only public comments are read and replies cannot be posted. Inject the cookie with the JUEJIN_COOKIE environment variable.',
14
+ 'Cookie 已失效,草稿都保留着。更新 JUEJIN_COOKIE 后重启就能继续发送。': 'The cookie has expired. Drafts are kept; update JUEJIN_COOKIE and restart to keep sending.',
15
+ '重新检查登录': 'Check login again',
16
+ 'Cookie 只来自配置 / 环境变量,不会经过这个页面,也不会写进数据目录。': 'The cookie only comes from config or environment; it never passes through this page or the data directory.',
17
+ '触发了掘金风控或账号异常(错误码 {0}),已暂停轮询和发送。请到网页端确认账号状态,处理后重启。': 'Juejin risk control or an account problem was detected (code {0}). Polling and sending are paused. Check the account on the website, then restart.',
18
+ '出站已关闭(sendMode: off),草稿无法发送。': 'Outbound is off (sendMode: off); drafts cannot be sent.',
19
+ '管理的文章': 'Watched articles',
20
+ '还没有配置文章。要管理哪些文章请在配置的 articles 里填写文章 id 或链接。': 'No articles configured. Add article IDs or links under articles in the config.',
21
+ '文章列表来自配置,要增删请改配置文件。': 'This list comes from the config; edit the config file to change it.',
22
+ '待审回复': 'Replies awaiting review',
23
+ '没有待审的回复。新的 AI 回复会自动出现在这里。': 'No replies awaiting review. New AI replies show up here automatically.',
24
+ '草稿只存在内存里,重启会丢失。': 'Drafts are kept in memory only and are lost on restart.',
25
+ '原评论': 'Original comment',
26
+ '拟回复': 'Proposed reply',
27
+ '上次发送的结果未知,这条可能已经发出。请先到掘金文章评论区确认,避免重复回复。': 'The last send outcome is unknown, so this may already be posted. Check the Juejin article comments first to avoid a duplicate.',
28
+ '发送中…': 'Sending…',
29
+ '发送': 'Send',
30
+ '修改': 'Edit',
31
+ '保存': 'Save',
32
+ '取消': 'Cancel',
33
+ '丢弃': 'Discard',
34
+ '确定丢弃这条回复?不会发送。': 'Discard this reply? It will not be posted.',
35
+ '打开文章': 'Open article',
36
+ '已发出': 'Posted',
37
+ '查看': 'View',
38
+ '已丢弃': 'Discarded',
39
+ '已保存修改': 'Changes saved',
40
+ '操作失败': 'Action failed',
41
+ '{0} 字': '{0} chars',
42
+ '刚刚': 'just now',
43
+ '{0} 分钟前': '{0} min ago',
44
+ '{0} 小时前': '{0} h ago',
45
+ '{0} 天前': '{0} d ago',
46
+ '刷新': 'Refresh',
47
+ '文章 {0}': 'Article {0}',
48
+ };
49
+
50
+ export function detectLocale(): ReviewLocale {
51
+ if (typeof document === 'undefined') return 'zh-CN';
52
+ const lang = document.documentElement.lang || (typeof navigator !== 'undefined' ? navigator.language : '');
53
+ return /^en\b/i.test(lang) ? 'en' : 'zh-CN';
54
+ }
55
+
56
+ export function text(locale: ReviewLocale, key: string, ...values: Array<string | number>): string {
57
+ const template = locale === 'en' ? english[key] ?? key : key;
58
+ return template.replace(/\{(\d+)\}/g, (_match, index: string) => String(values[Number(index)] ?? ''));
59
+ }
@@ -0,0 +1,31 @@
1
+ // Generated by build-plugin-runtime-entries.mjs. Do not edit.
2
+ import { apiFetch, pluginRpc } from '@zhin.js/client';
3
+ const FALLBACK_PLUGIN_ID = 'root/juejin';
4
+ /** 进程内缓存:用 const 容器持有,避免模块级可变绑定(adapter endpoint 边界检查)。 */
5
+ const pluginIdCache = {};
6
+ /**
7
+ * 找到本插件的 PluginId(插件实例名可以不是 juejin):从页面清单里挑出本包的 index 页,
8
+ * 优先匹配当前地址;找不到时退回 `root/juejin`。
9
+ */
10
+ export function resolvePluginId() {
11
+ pluginIdCache.promise ??= (async () => {
12
+ try {
13
+ const response = await apiFetch('/console/api/pages');
14
+ if (!response.ok)
15
+ return FALLBACK_PLUGIN_ID;
16
+ const body = await response.json();
17
+ const candidates = (body.pages ?? []).filter(page => page.localName === 'index' && typeof page.owner === 'string'
18
+ && typeof page.source === 'string' && /adapter-juejin|adapters[\\/]juejin[\\/]pages/.test(page.source));
19
+ const here = typeof location === 'undefined' ? '' : location.pathname.replace(/\/+$/, '') || '/';
20
+ const match = candidates.find(page => page.route === here) ?? candidates[0];
21
+ return match?.owner ?? FALLBACK_PLUGIN_ID;
22
+ }
23
+ catch {
24
+ return FALLBACK_PLUGIN_ID;
25
+ }
26
+ })();
27
+ return pluginIdCache.promise;
28
+ }
29
+ export async function call(method, data) {
30
+ return pluginRpc(await resolvePluginId(), method, data);
31
+ }
@@ -0,0 +1,72 @@
1
+ import { apiFetch, pluginRpc } from '@zhin.js/client';
2
+
3
+ /** 与插件 `src/console.ts` 返回的结构保持一致(只读视图)。 */
4
+ export interface ReviewDraft {
5
+ readonly id: string;
6
+ readonly url: string;
7
+ readonly articleId: string;
8
+ readonly message: string;
9
+ readonly status: 'pending' | 'sending';
10
+ readonly interrupted: boolean;
11
+ readonly createdAt: number;
12
+ readonly updatedAt?: number;
13
+ readonly author?: string;
14
+ readonly original?: string;
15
+ }
16
+
17
+ export interface ReviewEndpoint {
18
+ readonly id: string;
19
+ readonly sendMode: 'review' | 'off';
20
+ readonly persistent: boolean;
21
+ readonly circuit: { readonly code: number } | null;
22
+ readonly login: {
23
+ readonly state: 'none' | 'ok' | 'expired';
24
+ readonly userId?: string;
25
+ readonly userName?: string;
26
+ };
27
+ readonly articles: readonly { readonly id: string; readonly url: string }[];
28
+ readonly pendingCount: number;
29
+ readonly drafts: readonly ReviewDraft[];
30
+ }
31
+
32
+ export interface ReviewStatus {
33
+ readonly endpoints: readonly ReviewEndpoint[];
34
+ readonly updatedAt: number;
35
+ }
36
+
37
+ interface PageEntry {
38
+ readonly owner?: string;
39
+ readonly localName?: string;
40
+ readonly source?: string;
41
+ readonly route?: string;
42
+ }
43
+
44
+ const FALLBACK_PLUGIN_ID = 'root/juejin';
45
+ /** 进程内缓存:用 const 容器持有,避免模块级可变绑定(adapter endpoint 边界检查)。 */
46
+ const pluginIdCache: { promise?: Promise<string> } = {};
47
+
48
+ /**
49
+ * 找到本插件的 PluginId(插件实例名可以不是 juejin):从页面清单里挑出本包的 index 页,
50
+ * 优先匹配当前地址;找不到时退回 `root/juejin`。
51
+ */
52
+ export function resolvePluginId(): Promise<string> {
53
+ pluginIdCache.promise ??= (async () => {
54
+ try {
55
+ const response = await apiFetch('/console/api/pages');
56
+ if (!response.ok) return FALLBACK_PLUGIN_ID;
57
+ const body = await response.json() as { pages?: readonly PageEntry[] };
58
+ const candidates = (body.pages ?? []).filter(page => page.localName === 'index' && typeof page.owner === 'string'
59
+ && typeof page.source === 'string' && /adapter-juejin|adapters[\\/]juejin[\\/]pages/.test(page.source));
60
+ const here = typeof location === 'undefined' ? '' : location.pathname.replace(/\/+$/, '') || '/';
61
+ const match = candidates.find(page => page.route === here) ?? candidates[0];
62
+ return match?.owner ?? FALLBACK_PLUGIN_ID;
63
+ } catch {
64
+ return FALLBACK_PLUGIN_ID;
65
+ }
66
+ })();
67
+ return pluginIdCache.promise;
68
+ }
69
+
70
+ export async function call<T>(method: string, data?: Record<string, unknown>): Promise<T> {
71
+ return pluginRpc<T>(await resolvePluginId(), method, data);
72
+ }
package/plugin.js ADDED
@@ -0,0 +1,33 @@
1
+ // Generated by build-plugin-runtime-entries.mjs. Do not edit.
2
+ import { definePlugin, outboundHostToken, pluginConsoleRpcToken } from 'zhin.js';
3
+ import { juejinNotifierToken } from "./lib/juejin-runtime-state.js";
4
+ import { createJuejinConsoleRpc } from "./lib/console.js";
5
+ /**
6
+ * Plugin Runtime:
7
+ * - Adapter: `adapters/juejin` 轮询白名单文章评论,出站只进草稿队列(落盘,重启不丢)。
8
+ * - Commands: `juejin-draft list|show|edit|send|drop`、`juejin-login status`(仅 master)。
9
+ * - Page: `pages/index` →「掘金评论审批」:登录态、白名单文章、待审草稿的发送 / 改写 / 丢弃。
10
+ */
11
+ export default definePlugin({
12
+ name: 'juejin',
13
+ configUpdate: { featurePaths: [['nickname'], ['profile'], ['endpoints']] },
14
+ metadata: {
15
+ displayName: 'Juejin Comment Adapter (Experimental)',
16
+ },
17
+ setup(context) {
18
+ const outbound = context.resources.has(outboundHostToken) ? context.resources.use(outboundHostToken) : undefined;
19
+ const notifier = async (target, text) => {
20
+ if (!outbound)
21
+ return false;
22
+ await outbound.send({
23
+ adapter: target.adapter,
24
+ endpointKey: target.endpoint,
25
+ conversation: { kind: target.kind, id: target.id },
26
+ content: text,
27
+ });
28
+ return true;
29
+ };
30
+ context.resources.provide(juejinNotifierToken, notifier);
31
+ context.resources.provide(pluginConsoleRpcToken, createJuejinConsoleRpc());
32
+ },
33
+ });