larkmate 0.1.28 → 0.1.29
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/INSTALL.md
CHANGED
|
@@ -51,6 +51,8 @@ larkmate status
|
|
|
51
51
|
|
|
52
52
|
如果已有可用的凭据文件,直接执行 `larkmate status`,不必覆盖它。Cookie 失效时更新文件后重试。也可以通过 `LARKMATE_HOME` 选择配置目录、`LARKMATE_COOKIE` 提供 Cookie,或使用全局选项 `--credentials-file` 指定同格式文件;读取优先级依次为:指定文件、环境变量、配置目录。
|
|
53
53
|
|
|
54
|
+
macOS Chrome 已登录飞书时,也可以让安装了 LarkMate Skill 的 AI 按 Skill 的登录恢复说明验证账户并更新本机凭据。
|
|
55
|
+
|
|
54
56
|
### 3. 安装 Agent Skill
|
|
55
57
|
|
|
56
58
|
使用 [skills](https://github.com/vercel-labs/skills) 从项目仓库安装 Skill,按提示选择 Agent:
|
package/docs/ai-install.md
CHANGED
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
|
|
29
29
|
未使用系统用户委托或受限代理时,LarkMate 默认从 `~/.larkmate/credentials.json` 读取 Cookie,也支持 `LARKMATE_HOME`、`LARKMATE_COOKIE` 和 `--credentials-file`。先保留并检查已有登录态,可用 `larkmate status` 验证;不要读取或打印 Cookie 的值到对话、日志或命令参数中。
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
如果没有可用登录态,在直连模式下读取已安装 Skill 的 `references/auth.md`;macOS Chrome 已登录飞书时,可按其中的脚本验证账号并恢复本机凭据。其他环境可引导用户按 [快速安装](https://github.com/NWYLZW/larkmate/blob/main/INSTALL.md) 在本机安全配置。不要要求用户把 Cookie、密码或验证码发到对话中。不能自动完成登录时,如实说明,不声称已经登录。
|
|
32
32
|
|
|
33
33
|
安装后按需读取已安装 Skill 的 `SKILL.md` 和相关参考文件,依照用户的具体要求使用 CLI。当前能力包括会话与消息、会话管理和标签、群组管理、录制妙记及文字记录;以实际 CLI 帮助和 Skill 说明为准。发送消息、文件、权限申请及其他写操作应有用户指示;对于结果不确定的写入,先核查状态,避免重复提交。
|
|
34
34
|
|
package/package.json
CHANGED
package/skills/larkmate/SKILL.md
CHANGED
|
@@ -5,7 +5,7 @@ description: 使用 LarkMate 以当前飞书网页版用户身份管理会话、
|
|
|
5
5
|
|
|
6
6
|
# LarkMate
|
|
7
7
|
|
|
8
|
-
使用 `larkmate` 操作当前用户可访问的飞书数据。它使用自己的网页 Cookie,不依赖 Lark CLI 或应用身份。命令和选项以 `larkmate --help`、`larkmate <命令> --help`
|
|
8
|
+
使用 `larkmate` 操作当前用户可访问的飞书数据。它使用自己的网页 Cookie,不依赖 Lark CLI 或应用身份。命令和选项以 `larkmate --help`、`larkmate <命令> --help` 为准;尚未安装时参照 LarkMate 项目的 `INSTALL.md`。不要输出、索取或粘贴 Cookie。
|
|
9
9
|
|
|
10
10
|
若运行环境提供 `LARKMATE_BROKER` 和 `LARKMATE_BROKER_TOKEN_FILE`,使用受限代理并先执行 `larkmate broker-info`。错误码或导出错误的 `cause` 为 `POLICY_DENIED` 时,不尝试绕过或读取 Cookie;由代理所有者调整授权。账号隔离、策略和可用命令见 [Agent 权限委托指南](https://github.com/NWYLZW/larkmate/blob/main/docs/agent-broker.md)。
|
|
11
11
|
|
|
@@ -15,6 +15,7 @@ description: 使用 LarkMate 以当前飞书网页版用户身份管理会话、
|
|
|
15
15
|
|
|
16
16
|
根据任务读取所需参考:
|
|
17
17
|
|
|
18
|
+
- 登录配置或恢复过期登录态: [登录态与恢复](references/auth.md)。
|
|
18
19
|
- 查找会话、搜索与导出消息、获取链接、下载资源,以及管理会话状态和标签: [会话与消息](references/chats.md)。
|
|
19
20
|
- 全局或按会话监听、本地事件服务及脚本触发规则: [消息监听](references/listen.md)。
|
|
20
21
|
- 配置 API 服务、API key 及受限订阅权限: [API 服务与授权](https://github.com/NWYLZW/larkmate/blob/main/docs/api-service.md)。
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# 登录态与恢复
|
|
2
|
+
|
|
3
|
+
先运行 `larkmate status`。受限代理和系统用户委托应先检查各自的授权,不读取浏览器 Cookie、不切换账号,也不把 `POLICY_DENIED` 当成登录过期。仅在当前用户的直连模式下,根据用户的登录或修复请求处理凭据;`authenticate` 返回 401 或 `AUTH_FAILED` 时可尝试恢复。单个资源无权限、网络故障或写入结果不确定不等同于登录过期。
|
|
4
|
+
|
|
5
|
+
macOS 上,如果用户已在 Chrome 登录飞书,可以使用本 Skill 的 `scripts/chrome-login-macos.mjs`。它只读取所选 Chrome 配置中飞书域名的 Cookie,通过系统钥匙串解密,在私有临时目录使用 `larkmate status` 验证,并只输出账户 ID 和名称。脚本需要 `node:sqlite` 支持;旧版 Node、其他浏览器或其他系统可按安装文档手动配置。
|
|
6
|
+
|
|
7
|
+
1. 执行 `node <本 Skill 目录>/scripts/chrome-login-macos.mjs` 验证 Chrome 默认配置。其他 Chrome 配置可加 `--profile 'Profile 2'`,仅检查用户选定的配置。
|
|
8
|
+
2. 对照用户指定的账号或已有上下文确认返回的账户;无法确定时先向用户确认。验证通过且账号匹配后,执行 `node <本 Skill 目录>/scripts/chrome-login-macos.mjs --apply --expect-user-id <返回的用户ID>`;使用其他配置时同样传入 `--profile`。脚本会再次验证账号,再原子更新当前用户的 LarkMate 凭据文件。
|
|
9
|
+
3. 运行 `larkmate status` 检查,随后继续用户原先请求的操作。若此前写操作的结果不确定,先查询实际状态,避免重复发送或修改。
|
|
10
|
+
|
|
11
|
+
不要打印 Cookie、密钥或数据库记录,不把它们放进聊天、命令参数或日志。不要绕过钥匙串、系统账户、浏览器或代理的访问限制;脚本无法读取、解密或验证时,保留现有凭据,说明具体阻碍并引导用户按 [安装文档](https://github.com/NWYLZW/larkmate/blob/main/INSTALL.md) 本地配置。验证时使用的临时凭据正常结束会删除;异常终止后,如有该脚本遗留的私有临时目录,应由当前用户检查并清理。
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Imports only the current macOS user's Feishu Chrome cookies. Never prints credentials.
|
|
3
|
+
import { createDecipheriv, createHash, pbkdf2Sync, randomBytes } from 'node:crypto';
|
|
4
|
+
import { execFileSync } from 'node:child_process';
|
|
5
|
+
import { homedir, tmpdir } from 'node:os';
|
|
6
|
+
import { join } from 'node:path';
|
|
7
|
+
import { chmod, lstat, mkdir, mkdtemp, readFile, rename, rm, writeFile } from 'node:fs/promises';
|
|
8
|
+
import { fileURLToPath } from 'node:url';
|
|
9
|
+
|
|
10
|
+
export function decryptChromeCookie(row, key, version) {
|
|
11
|
+
if (row.value) return row.value;
|
|
12
|
+
const encrypted=Buffer.from(row.encrypted_value || []);
|
|
13
|
+
if (!['v10','v11'].includes(encrypted.subarray(0,3).toString('ascii'))) return null;
|
|
14
|
+
try {
|
|
15
|
+
const cipher=createDecipheriv('aes-128-cbc',key,Buffer.alloc(16,32));
|
|
16
|
+
let plain=Buffer.concat([cipher.update(encrypted.subarray(3)),cipher.final()]);
|
|
17
|
+
const digest=createHash('sha256').update(row.host_key).digest();
|
|
18
|
+
if(version>=24) { if(plain.length<32||!plain.subarray(0,32).equals(digest)) return null; plain=plain.subarray(32); }
|
|
19
|
+
return plain.toString('utf8');
|
|
20
|
+
} catch { return null; }
|
|
21
|
+
}
|
|
22
|
+
export function makeCookie(rows,key,version,now=Date.now()) {
|
|
23
|
+
const epoch=BigInt(now)*1000n+11644473600000000n,jar=new Map();
|
|
24
|
+
for(const row of rows) {
|
|
25
|
+
if(!['.feishu.cn','internal-api-lark-api.feishu.cn'].includes(row.host_key))continue;
|
|
26
|
+
let expiry;try{expiry=BigInt(row.expires_utc)}catch{continue}
|
|
27
|
+
if(expiry!==0n && expiry<=epoch)continue;
|
|
28
|
+
const value=decryptChromeCookie(row,key,version);
|
|
29
|
+
if(!/^[!#$%&'*+.^_`|~\w-]+$/.test(row.name)||!value||/[\x00-\x20\x7f-\xff;,]/.test(value))continue;
|
|
30
|
+
jar.set(row.name,value);
|
|
31
|
+
}
|
|
32
|
+
return [...jar].map(([name,value])=>`${name}=${value}`).join('; ');
|
|
33
|
+
}
|
|
34
|
+
function parseArgs(args) {
|
|
35
|
+
let profile='Default', apply=false, expected;
|
|
36
|
+
while(args.length) {const arg=args.shift();if(arg==='--help')return {help:true};
|
|
37
|
+
if(arg==='--apply')apply=true;else if(arg==='--profile')profile=args.shift();else if(arg==='--expect-user-id')expected=args.shift();else throw Error('Invalid option.');}
|
|
38
|
+
if(!/^(Default|Profile [1-9][0-9]*)$/.test(profile||''))throw Error('Use Default or a Chrome profile named Profile N.');
|
|
39
|
+
if(apply&&!/^[1-9][0-9]{0,18}$/.test(expected||''))throw Error('Applying requires --expect-user-id with the intended numeric user ID.');
|
|
40
|
+
if(!apply&&expected)throw Error('--expect-user-id requires --apply.');
|
|
41
|
+
return {profile,apply,expected};
|
|
42
|
+
}
|
|
43
|
+
async function privateDirectory(path) {
|
|
44
|
+
await mkdir(path,{recursive:true,mode:0o700});const st=await lstat(path);
|
|
45
|
+
if(!st.isDirectory()||st.isSymbolicLink()||st.uid!==process.getuid()||(st.mode&0o077))throw Error('The LarkMate directory must be private and owned by the current user.');
|
|
46
|
+
}
|
|
47
|
+
async function main() {
|
|
48
|
+
const options=parseArgs(process.argv.slice(2));
|
|
49
|
+
if(options.help){console.log('Usage: node chrome-login-macos.mjs [--profile "Default|Profile N"] [--apply --expect-user-id ID]');return;}
|
|
50
|
+
if(process.platform!=='darwin')throw Error('This helper supports macOS Chrome only.');
|
|
51
|
+
if(['LARKMATE_BROKER','LARKMATE_BROKER_TOKEN_FILE','LARKMATE_DELEGATE_OWNER','LARKMATE_DELEGATE_LAUNCHER','LARKMATE_COOKIE'].some(key=>process.env[key]))throw Error('Use direct mode without a broker, delegate, or LARKMATE_COOKIE to refresh this account.');
|
|
52
|
+
let sqlite;try{sqlite=await import('node:sqlite')}catch{throw Error('This helper needs Node.js with node:sqlite support.');}
|
|
53
|
+
const root=join(homedir(),'Library/Application Support/Google/Chrome',options.profile);
|
|
54
|
+
let dbPath;for(const path of [join(root,'Cookies'),join(root,'Network','Cookies')]) {try{if((await lstat(path)).isFile()){dbPath=path;break}}catch{}}
|
|
55
|
+
if(!dbPath)throw Error('Chrome cookie database was not found for that profile.');
|
|
56
|
+
let secret;try{secret=execFileSync('security',['find-generic-password','-w','-s','Chrome Safe Storage'],{encoding:'utf8',stdio:['ignore','pipe','ignore'],timeout:10000}).trim()}catch{throw Error('Chrome Keychain access was unavailable or denied.');}
|
|
57
|
+
if(!secret)throw Error('Chrome Keychain access was unavailable or denied.');
|
|
58
|
+
const key=pbkdf2Sync(secret,'saltysalt',1003,16,'sha1');let db,rows,version;
|
|
59
|
+
try {db=new sqlite.DatabaseSync(dbPath,{readOnly:true});version=Number(db.prepare("SELECT value FROM meta WHERE key='version'").get()?.value||0);
|
|
60
|
+
rows=db.prepare("SELECT host_key,name,value,encrypted_value,CAST(expires_utc AS TEXT) AS expires_utc FROM cookies WHERE host_key IN ('.feishu.cn','internal-api-lark-api.feishu.cn') ORDER BY length(host_key)").all();
|
|
61
|
+
}catch{throw Error('Cannot read the selected Chrome cookie database.');}finally{db?.close()}
|
|
62
|
+
const cookie=makeCookie(rows,key,version);if(!cookie)throw Error('No usable Feishu cookies were found in that Chrome profile.');
|
|
63
|
+
const dir=await mkdtemp(join(tmpdir(),'larkmate-auth-check-'));let result;
|
|
64
|
+
try {
|
|
65
|
+
const candidate=join(dir,'credentials.json');await writeFile(candidate,JSON.stringify({cookie}),{mode:0o600,flag:'wx'});
|
|
66
|
+
try {const out=execFileSync('larkmate',['--credentials-file',candidate,'status'],{encoding:'utf8',stdio:['ignore','pipe','pipe'],timeout:30000});result=JSON.parse(out)}
|
|
67
|
+
catch{throw Error('Chrome login could not be validated by LarkMate. Sign in on the selected profile and try again.');}
|
|
68
|
+
const user=result?.user;if(!result?.ok||result?.status!=='ready'||!/^\d+$/.test(user?.id||''))throw Error('LarkMate returned an invalid account identity.');
|
|
69
|
+
if(options.apply){
|
|
70
|
+
if(user.id!==options.expected)throw Error('The Chrome account does not match the expected user ID; no credentials were changed.');
|
|
71
|
+
const base=process.env.LARKMATE_HOME||join(homedir(),'.larkmate');await privateDirectory(base);const path=join(base,'credentials.json');let original={};
|
|
72
|
+
try{const st=await lstat(path);if(!st.isFile()||st.isSymbolicLink()||st.uid!==process.getuid()||(st.mode&0o077))throw Error('unsafe');original=JSON.parse(await readFile(path,'utf8'));if(!original||typeof original!=='object'||Array.isArray(original))throw Error('unsafe')}
|
|
73
|
+
catch(e){if(e.code!=='ENOENT')throw Error('Existing credentials are not a private, owned JSON file; no changes were made.');}
|
|
74
|
+
const next=join(base,`.credentials-${randomBytes(12).toString('hex')}`);
|
|
75
|
+
try{await writeFile(next,JSON.stringify({...original,cookie})+'\n',{mode:0o600,flag:'wx'});await chmod(next,0o600);await rename(next,path)}finally{await rm(next,{force:true})}
|
|
76
|
+
}
|
|
77
|
+
console.log(JSON.stringify({ok:true,status:options.apply?'updated':'verified',user:{id:user.id,name:user.name||null}}));
|
|
78
|
+
} finally {await rm(dir,{recursive:true,force:true})}
|
|
79
|
+
}
|
|
80
|
+
if(process.argv[1]&&fileURLToPath(import.meta.url)===process.argv[1])main().catch(error=>{console.error(JSON.stringify({ok:false,error:error.message}));process.exitCode=1});
|