@openai-lite/codex-feishu 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.
@@ -0,0 +1,163 @@
1
+ import { spawn } from "node:child_process";
2
+ import { getBridgeRpcEndpoint } from "../lib/paths.js";
3
+ import { callJsonRpc } from "../lib/uds_rpc.js";
4
+
5
+ function isTruthy(value) {
6
+ if (typeof value !== "string") {
7
+ return Boolean(value);
8
+ }
9
+ const lowered = value.trim().toLowerCase();
10
+ return lowered === "" || lowered === "1" || lowered === "true" || lowered === "yes" || lowered === "on";
11
+ }
12
+
13
+ function formatInShanghai(value) {
14
+ const date = new Date(value);
15
+ if (!Number.isFinite(date.getTime())) {
16
+ return "";
17
+ }
18
+ const parts = new Intl.DateTimeFormat("zh-CN", {
19
+ timeZone: "Asia/Shanghai",
20
+ year: "numeric",
21
+ month: "2-digit",
22
+ day: "2-digit",
23
+ hour: "2-digit",
24
+ minute: "2-digit",
25
+ second: "2-digit",
26
+ hour12: false,
27
+ }).formatToParts(date);
28
+ const read = (type) => parts.find((item) => item.type === type)?.value ?? "";
29
+ const y = read("year");
30
+ const mon = read("month");
31
+ const d = read("day");
32
+ const h = read("hour");
33
+ const min = read("minute");
34
+ const s = read("second");
35
+ if (!y || !mon || !d || !h || !min || !s) {
36
+ return "";
37
+ }
38
+ return `${y}-${mon}-${d} ${h}:${min}:${s}`;
39
+ }
40
+
41
+ async function maybeAutostartDaemon() {
42
+ const auto = process.env.CODEX_FEISHU_AUTOSTART ?? "true";
43
+ if (!isTruthy(auto)) {
44
+ return false;
45
+ }
46
+
47
+ const trySpawn = (cmd, args) =>
48
+ new Promise((resolve) => {
49
+ const child = spawn(cmd, args, {
50
+ detached: true,
51
+ stdio: "ignore",
52
+ });
53
+ child.once("error", () => resolve(false));
54
+ child.once("spawn", () => {
55
+ child.unref();
56
+ resolve(true);
57
+ });
58
+ });
59
+
60
+ const fromPath = await trySpawn("codex-feishu", ["daemon"]);
61
+ if (!fromPath) {
62
+ const currentEntry = process.argv[1];
63
+ if (!currentEntry) {
64
+ return false;
65
+ }
66
+ const fromCurrentNode = await trySpawn(process.execPath, [currentEntry, "daemon"]);
67
+ if (!fromCurrentNode) {
68
+ return false;
69
+ }
70
+ }
71
+
72
+ await new Promise((resolve) => {
73
+ setTimeout(resolve, 700);
74
+ });
75
+ return true;
76
+ }
77
+
78
+ export async function fetchQrcode(options = {}) {
79
+ const endpoint = getBridgeRpcEndpoint();
80
+ const payload = {
81
+ purpose: options.purpose ?? null,
82
+ cwd_hint: options.cwdHint ?? process.cwd(),
83
+ };
84
+ const timeoutMs = Number.isFinite(options.timeoutMs) ? options.timeoutMs : 6000;
85
+ const autostart = options.autostart !== false;
86
+
87
+ try {
88
+ return await callJsonRpc(endpoint, "feishu/qrcode", payload, { timeoutMs });
89
+ } catch (firstErr) {
90
+ if (!autostart) {
91
+ throw firstErr;
92
+ }
93
+ await maybeAutostartDaemon();
94
+ return await callJsonRpc(endpoint, "feishu/qrcode", payload, { timeoutMs: timeoutMs + 2000 });
95
+ }
96
+ }
97
+
98
+ export async function renderAsciiQr(value) {
99
+ try {
100
+ const mod = await import("qrcode");
101
+ const QRCode = mod?.default ?? mod;
102
+ if (!QRCode || typeof QRCode.toString !== "function") {
103
+ throw new Error("qrcode.toString unavailable");
104
+ }
105
+ return await QRCode.toString(value, { type: "terminal", small: true, margin: 1 });
106
+ } catch {
107
+ return "";
108
+ }
109
+ }
110
+
111
+ function deriveBotOpenId(result) {
112
+ if (result?.bot_open_id) {
113
+ return String(result.bot_open_id);
114
+ }
115
+ const link = result?.open_chat_link;
116
+ if (typeof link !== "string" || !link.trim()) {
117
+ return "";
118
+ }
119
+ try {
120
+ const url = new URL(link);
121
+ return url.searchParams.get("openId") || "";
122
+ } catch {
123
+ return "";
124
+ }
125
+ }
126
+
127
+ export function formatQrcodeSummary(result, options = {}) {
128
+ const expireAtShanghai = formatInShanghai(result?.expires_at);
129
+ const botOpenId = deriveBotOpenId(result);
130
+ const lines = [
131
+ "飞书绑定码已生成。",
132
+ result?.reused ? "- note: 本次复用了最近一次未过期绑定码" : "",
133
+ `- bot_open_id: ${botOpenId || "(not set)"}`,
134
+ `- code: ${result?.code ?? "unknown"}`,
135
+ `- expire_at(上海): ${expireAtShanghai || "unknown"}`,
136
+ `- qrcode_mode: ${result?.qrcode_mode ?? "unknown"}`,
137
+ `- qr_payload: ${result?.qr_text ?? ""}`,
138
+ result?.open_chat_link ? `- open_chat_link: ${result.open_chat_link}` : "",
139
+ `- 在飞书里发送: ${result?.bind_command_hint ?? "unknown"}`,
140
+ ].filter(Boolean);
141
+
142
+ if (options.asciiQr) {
143
+ lines.push("", "可扫码二维码:", "```", options.asciiQr.trimEnd(), "```");
144
+ }
145
+ return lines.join("\n");
146
+ }
147
+
148
+ export async function runQrcode(flags = {}) {
149
+ const purpose = flags.purpose || null;
150
+ const asJson = isTruthy(flags.json);
151
+ const wantAscii = isTruthy(flags.ascii);
152
+ const result = await fetchQrcode({ purpose, autostart: true, cwdHint: process.cwd() });
153
+
154
+ if (asJson) {
155
+ // eslint-disable-next-line no-console
156
+ console.log(JSON.stringify(result, null, 2));
157
+ return;
158
+ }
159
+
160
+ const asciiQr = wantAscii ? await renderAsciiQr(result?.qr_text) : "";
161
+ // eslint-disable-next-line no-console
162
+ console.log(formatQrcodeSummary(result, { asciiQr }));
163
+ }