@luckylillia-dev/plugin-aiimgdetector 0.0.0-stage → 1.0.4
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/README.md +2 -2
- package/assets/demo.png +0 -0
- package/assets/icon.svg +15 -0
- package/assets/synthid-login-check.jpg +0 -0
- package/docs/details.md +45 -0
- package/docs/publishing.md +66 -0
- package/docs/synthid.md +80 -0
- package/lib/image.js +90 -0
- package/lib/index.js +207 -0
- package/lib/provenance.js +21 -0
- package/lib/synthid-auth.js +100 -0
- package/lib/synthid-browser.js +451 -0
- package/lib/synthid-http-runtime/chromium-css-property-catalog.mjs +7 -0
- package/lib/synthid-http-runtime/complete-protocol.mjs +88 -0
- package/lib/synthid-http-runtime/entry.d.mts +41 -0
- package/lib/synthid-http-runtime/entry.mjs +6 -0
- package/lib/synthid-http-runtime/event-listener-diagnostics.mjs +141 -0
- package/lib/synthid-http-runtime/independent-request-reference.mjs +49 -0
- package/lib/synthid-http-runtime/jsdom-event-clock-compat.mjs +76 -0
- package/lib/synthid-http-runtime/lifecycle-client.mjs +165 -0
- package/lib/synthid-http-runtime/lifecycle-web-session.mjs +114 -0
- package/lib/synthid-http-runtime/lifecycle-worker.mjs +363 -0
- package/lib/synthid-http-runtime/node-platform-failure-diagnostics.mjs +108 -0
- package/lib/synthid-http-runtime/realm-activation-diagnostic.js +31 -0
- package/lib/synthid-http-runtime/realm-focus-diagnostic.js +9 -0
- package/lib/synthid-http-runtime/realm-init.js +34 -0
- package/lib/synthid-http-runtime/realm-pointer-diagnostic.js +49 -0
- package/lib/synthid-http-runtime/runtime-anchor-href-translate.mjs +242 -0
- package/lib/synthid-http-runtime/runtime-child-web-compat.mjs +103 -0
- package/lib/synthid-http-runtime/runtime-chrome-csi.mjs +216 -0
- package/lib/synthid-http-runtime/runtime-clock-precision-control.mjs +51 -0
- package/lib/synthid-http-runtime/runtime-css-properties-compat.mjs +165 -0
- package/lib/synthid-http-runtime/runtime-detached-frame-compat.mjs +214 -0
- package/lib/synthid-http-runtime/runtime-javascript-bindings.mjs +69 -0
- package/lib/synthid-http-runtime/runtime-navigator-compat.mjs +99 -0
- package/lib/synthid-http-runtime/runtime-permissions-binding.mjs +64 -0
- package/lib/synthid-http-runtime/runtime-permissions-control.mjs +143 -0
- package/lib/synthid-http-runtime/runtime-web-compat.mjs +639 -0
- package/lib/synthid-http-runtime/runtime-webidl-callable-control.mjs +151 -0
- package/lib/synthid-http-runtime/runtime-webidl-realm-compat.mjs +132 -0
- package/lib/synthid-http-runtime/runtime-window-state-compat.mjs +128 -0
- package/lib/synthid-http-runtime/scripting-parser.mjs +13 -0
- package/lib/synthid-http-runtime/web-session.mjs +64 -0
- package/lib/synthid-http-runtime/webidl-brand-errors-preload.mjs +49 -0
- package/lib/synthid-http-runtime/webidl-callable-source-catalog.mjs +4491 -0
- package/lib/synthid-http-runtime/webidl-realm-catalog.mjs +124 -0
- package/lib/synthid-http.js +314 -0
- package/lib/synthid-manual.js +365 -0
- package/lib/synthid-page.js +101 -0
- package/lib/synthid-result.js +189 -0
- package/lib/synthid.js +540 -0
- package/lib/vision.js +41 -0
- package/package.json +84 -4
- package/src/image.ts +74 -0
- package/src/index.ts +203 -0
- package/src/provenance.ts +40 -0
- package/src/synthid-auth.ts +83 -0
- package/src/synthid-browser.ts +426 -0
- package/src/synthid-http-runtime/chromium-css-property-catalog.mjs +7 -0
- package/src/synthid-http-runtime/complete-protocol.mjs +88 -0
- package/src/synthid-http-runtime/entry.d.mts +41 -0
- package/src/synthid-http-runtime/entry.mjs +6 -0
- package/src/synthid-http-runtime/event-listener-diagnostics.mjs +141 -0
- package/src/synthid-http-runtime/independent-request-reference.mjs +49 -0
- package/src/synthid-http-runtime/jsdom-event-clock-compat.mjs +76 -0
- package/src/synthid-http-runtime/lifecycle-client.mjs +165 -0
- package/src/synthid-http-runtime/lifecycle-web-session.mjs +114 -0
- package/src/synthid-http-runtime/lifecycle-worker.mjs +363 -0
- package/src/synthid-http-runtime/node-platform-failure-diagnostics.mjs +108 -0
- package/src/synthid-http-runtime/realm-activation-diagnostic.js +31 -0
- package/src/synthid-http-runtime/realm-focus-diagnostic.js +9 -0
- package/src/synthid-http-runtime/realm-init.js +34 -0
- package/src/synthid-http-runtime/realm-pointer-diagnostic.js +49 -0
- package/src/synthid-http-runtime/runtime-anchor-href-translate.mjs +242 -0
- package/src/synthid-http-runtime/runtime-child-web-compat.mjs +103 -0
- package/src/synthid-http-runtime/runtime-chrome-csi.mjs +216 -0
- package/src/synthid-http-runtime/runtime-clock-precision-control.mjs +51 -0
- package/src/synthid-http-runtime/runtime-css-properties-compat.mjs +165 -0
- package/src/synthid-http-runtime/runtime-detached-frame-compat.mjs +214 -0
- package/src/synthid-http-runtime/runtime-javascript-bindings.mjs +69 -0
- package/src/synthid-http-runtime/runtime-navigator-compat.mjs +99 -0
- package/src/synthid-http-runtime/runtime-permissions-binding.mjs +64 -0
- package/src/synthid-http-runtime/runtime-permissions-control.mjs +143 -0
- package/src/synthid-http-runtime/runtime-web-compat.mjs +639 -0
- package/src/synthid-http-runtime/runtime-webidl-callable-control.mjs +151 -0
- package/src/synthid-http-runtime/runtime-webidl-realm-compat.mjs +132 -0
- package/src/synthid-http-runtime/runtime-window-state-compat.mjs +128 -0
- package/src/synthid-http-runtime/scripting-parser.mjs +13 -0
- package/src/synthid-http-runtime/web-session.mjs +64 -0
- package/src/synthid-http-runtime/webidl-brand-errors-preload.mjs +49 -0
- package/src/synthid-http-runtime/webidl-callable-source-catalog.mjs +4491 -0
- package/src/synthid-http-runtime/webidl-realm-catalog.mjs +124 -0
- package/src/synthid-http.ts +290 -0
- package/src/synthid-manual.ts +388 -0
- package/src/synthid-page.ts +103 -0
- package/src/synthid-result.ts +177 -0
- package/src/synthid.ts +514 -0
- package/src/vision.ts +57 -0
- package/webui/index.js +279 -0
package/README.md
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
#
|
|
1
|
+
# 幸运莉莉娅插件 - 检测图片是否为 AI 生成
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
对着图片回复 `/ai吗`,或发送 `/ai吗` 并附图,使用 OpenAI 水印检测、可选的 SynthID 检测和看图模型判断图片是否由 AI 生成。
|
package/assets/demo.png
ADDED
|
Binary file
|
package/assets/icon.svg
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 96 96">
|
|
2
|
+
<defs>
|
|
3
|
+
<linearGradient id="bg" gradientUnits="userSpaceOnUse" x1="0" y1="0" x2="96" y2="96">
|
|
4
|
+
<stop offset="0" stop-color="#34d399"/>
|
|
5
|
+
<stop offset="1" stop-color="#0ea5e9"/>
|
|
6
|
+
</linearGradient>
|
|
7
|
+
</defs>
|
|
8
|
+
<rect width="96" height="96" rx="22" fill="url(#bg)"/>
|
|
9
|
+
<rect x="16" y="20" width="50" height="40" rx="8" fill="none" stroke="#fff" stroke-width="5"/>
|
|
10
|
+
<circle cx="30" cy="33" r="4.5" fill="#fff"/>
|
|
11
|
+
<path d="M21 55l12-12 9 9 6-6 13 13" fill="none" stroke="#fff" stroke-width="5" stroke-linecap="round" stroke-linejoin="round"/>
|
|
12
|
+
<circle cx="62" cy="60" r="15" fill="url(#bg)" stroke="#fff" stroke-width="5"/>
|
|
13
|
+
<path d="M73 71l9 9" stroke="#fff" stroke-width="6.5" stroke-linecap="round"/>
|
|
14
|
+
<text x="62" y="65.5" text-anchor="middle" font-family="Arial, Helvetica, sans-serif" font-size="14" font-weight="700" fill="#fff">AI</text>
|
|
15
|
+
</svg>
|
|
Binary file
|
package/docs/details.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
## 怎么用
|
|
2
|
+
|
|
3
|
+
在群里或私聊里**对着一张图片回复「/ai吗」**,或者发「/ai吗」时**直接带上图片**,Bot 会引用那张图,回复检测结果。默认也支持不带斜杠的「ai吗」,文字和图片之间不用加空格。
|
|
4
|
+
|
|
5
|
+
- 不分大小写,支持前面 @ 机器人
|
|
6
|
+
- 前缀、是否允许省略前缀统一在「指令」页设置
|
|
7
|
+
- 同一个群(或私聊)两次检测之间有冷却,默认 10 秒
|
|
8
|
+
- 触发词、冷却时间都能在设置里改;触发词填写指令名,不带前缀,名称内部不能有空格
|
|
9
|
+
|
|
10
|
+
## 三种检测
|
|
11
|
+
|
|
12
|
+
| 检测 | 怎么查 | 结论 | 默认 |
|
|
13
|
+
|:--|:--|:--|:--:|
|
|
14
|
+
| OpenAI 水印检测 | 使用 OpenAI 官方服务,查图片中的 C2PA 凭据和 SynthID 水印 | 识别 ChatGPT、OpenAI API 和 Codex 等工具留下的 OpenAI 来源标记 | 开 |
|
|
15
|
+
| SynthID 检测 | 浏览器登录 Google 或手动填写凭证后,通过 synthid.com 上传和检测 | 报告 Google 或合作方的 AI 生成标记 | 关 |
|
|
16
|
+
| 看图判断 | 让能看图的 AI 找手指、文字、边缘、光影这些破绽 | 推测,会看错 | 开 |
|
|
17
|
+
|
|
18
|
+
按 **OpenAI 水印检测 → SynthID 检测 → 看图判断** 的顺序执行,只运行已启用的检测。查出标记就直接回答,未检出、无法判断或检测出错时继续尝试下一项。没有配置 OpenAI 官方服务时自动跳过 OpenAI 水印检测。
|
|
19
|
+
|
|
20
|
+
## 回复长什么样
|
|
21
|
+
|
|
22
|
+
- OpenAI 水印检出:`图片由 OpenAI 制作。`
|
|
23
|
+
- SynthID 检出:`图片是 OpenAI 生成`,来源以服务返回值为准
|
|
24
|
+
- 看图:`qwen-vl-max 判断:疑似 AI 生成(置信度 82%)`,下面跟最多 3 条理由
|
|
25
|
+
- 水印未检出且没有其他结果:`没有找到 AI 特征,但不能完全确认不是 AI。`
|
|
26
|
+
- SynthID 无法判断且没有后续结果:说明分辨率过低、细节不足等原因
|
|
27
|
+
- 出错时说原因:检测太频繁、额度用完、Key 无效、连不上检测服务等
|
|
28
|
+
|
|
29
|
+
## 准备
|
|
30
|
+
|
|
31
|
+
1. 从插件市场安装 aiimgdetector 时,会先复用已安装的兼容 **AI 服务**插件;没有时,从同一插件市场列表自动安装。市场列表里找不到该插件就会提示缺少依赖
|
|
32
|
+
2. 启用 **AI 服务**插件,在服务列表中点击「添加服务」。在弹窗里填写服务名称、接口地址和 Key,点击「自动获取」或手动添加模型;以后点列表里的「编辑」即可修改配置、添加或删除模型
|
|
33
|
+
3. 在同一个弹窗里直接测试当前输入,再点「确定」返回服务列表。需要时在服务卡片上点击「设为默认」,卡片会显示默认标记;最后点列表外层的「保存配置」,服务、模型和默认项才会供其他插件使用。列表中的删除也需要这一步保存
|
|
34
|
+
4. 在 aiimgdetector 的「看图判断」里先选服务,再选该服务的模型。模型必须支持**图片输入**,以服务商说明为准;各插件可以选用不同服务和模型
|
|
35
|
+
5. 水印检测默认打开,服务下拉框只列 OpenAI 官方服务的实际名称。未指定时,优先使用全局默认服务(须为 OpenAI 官方服务),否则使用列表中的第一个官方服务,下拉框直接显示该名称。打开设置不会改写原配置,已指定的服务或关闭状态都会保留;也可以手动选择其他官方服务。没有可用服务时,点击提示中的「设置」进入 AI 服务页添加。能否调用取决于 Key 的权限;国内连接官方接口通常需要启用「代理」插件
|
|
36
|
+
|
|
37
|
+
新安装的插件默认停用,需要分别启用。批准 AI 服务插件的界面后,aiimgdetector 配置中会显示服务和模型下拉框。
|
|
38
|
+
|
|
39
|
+
要使用 SynthID,批准本插件界面,在设置里打开「SynthID 检测,支持 Google、OpenAI、NVIDIA、Kakao 生成的图片」,点击组内的「登录 SynthID」,在 Bot 所在电脑弹出的浏览器窗口中完成 Google 登录和首次使用提示。插件会在当前已登录页面自动选择内置测试图片取得凭证,无需自行选图。取得凭证后自动填入下面的三项输入框,再点击弹窗底部「保存」。之后使用保存的浏览器会话,由网站为每张图片生成新的网页校验。登录和检测自动沿用 Bot「代理」插件的网络设置。
|
|
40
|
+
|
|
41
|
+
三项凭证始终显示,也可直接从自己浏览器中获取登录凭据、网站上传标识和网页校验凭据并填写,无需切换模式。设置中有可展开的获取步骤,完整操作路径见 [SynthID 接入说明](synthid.md)。手动获取不要求 Bot 安装浏览器;网页校验被拒绝不代表登录失效,可改用插件浏览器登录,或重新从同一次网页检测获取上传标识和网页校验凭据。上传标识是网站请求里的值,不是本机文件夹。
|
|
42
|
+
|
|
43
|
+
只使用 SynthID 时,可以关闭 OpenAI 水印检测和看图判断,无需配置模型或 OpenAI API Key,但仍需启用 AI 服务插件。
|
|
44
|
+
|
|
45
|
+
> 看图判断按图片计费,结论只是推测,别拿它当证据。图片会发给启用的检测服务;启用 SynthID 时会上传到 Google。
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# 自动发布到 npm
|
|
2
|
+
|
|
3
|
+
[Publish to npm](https://github.com/linyuchen/lucky-lillia-plugin-aiimgdetector/actions/workflows/publish.yml) 在推送版本标签时运行全部单元与集成测试、构建并检查包内容,通过后发布已验证的同一份 tarball。浏览器端到端测试仍通过本地的 `npm run test:browser` 单独运行。
|
|
4
|
+
|
|
5
|
+
- 标签必须与 `package.json` 一致,例如版本 `1.0.0` 对应 `v1.0.0`。
|
|
6
|
+
- 正式版本发布到 `latest`;带预发布后缀的版本(例如 `1.1.0-beta.1`)发布到 `next`。
|
|
7
|
+
- Actions 页面上的 **Run workflow** 只验证、保存安装包,不发布 npm。安装包保留 14 天。
|
|
8
|
+
|
|
9
|
+
## 构建依赖
|
|
10
|
+
|
|
11
|
+
SDK 和 AI 服务直接从 npm 安装,包名分别为 `@luckylillia-dev/plugin-sdk`、`@luckylillia-dev/plugin-aiprovider`。工作流通过 `npm ci` 按 `package-lock.json` 锁定的版本安装,无需额外检出 Bot 或 AI 服务源码,也无需配置仓库变量。
|
|
12
|
+
|
|
13
|
+
本地开发同样可以在本仓库直接执行:
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
npm ci --registry=https://registry.npmjs.org/
|
|
17
|
+
npm test
|
|
18
|
+
npm run build
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
更新依赖时一并提交 `package.json` 和 `package-lock.json`,先手动运行验证,再发布版本。运行插件的 Bot 仍需提供兼容的 SDK,并安装、启用 AI 服务插件。
|
|
22
|
+
|
|
23
|
+
## 首次 npm 授权
|
|
24
|
+
|
|
25
|
+
推荐使用 npm **Trusted Publisher(OIDC)**,后续自动发布无需长期保存 npm token。工作流使用 GitHub 托管的 runner、Node 24,并检查 npm 至少为 11.5.1;发布任务具有 `id-token: write` 权限。
|
|
26
|
+
|
|
27
|
+
如果 npm 上还没有这个包,需要先由包所有者完成首次发布,才能在包的 Settings 页面配置 Trusted Publisher。可以在本地完成开发依赖安装、测试和构建后,用自己的 npm 账号登录并发布:
|
|
28
|
+
|
|
29
|
+
```sh
|
|
30
|
+
npm test
|
|
31
|
+
npm pack
|
|
32
|
+
npm login --registry=https://registry.npmjs.org/
|
|
33
|
+
npm publish ./lucky-lillia-plugin-aiimgdetector-1.0.0.tgz --access public --registry=https://registry.npmjs.org/
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
文件名以当前版本为准。此命令会实际发布版本;已经发布的版本不能重复发布。
|
|
37
|
+
|
|
38
|
+
包创建后,在 npm 包的 **Settings → Trusted Publisher** 中填写:
|
|
39
|
+
|
|
40
|
+
| 字段 | 值 |
|
|
41
|
+
| --- | --- |
|
|
42
|
+
| Provider | GitHub Actions |
|
|
43
|
+
| Organization or user | `linyuchen` |
|
|
44
|
+
| Repository | `lucky-lillia-plugin-aiimgdetector` |
|
|
45
|
+
| Workflow filename | `publish.yml` |
|
|
46
|
+
| Environment | 留空 |
|
|
47
|
+
| Allowed actions | 勾选 **Allow npm publish** |
|
|
48
|
+
|
|
49
|
+
字段区分大小写,Workflow filename 只填文件名。新建配置默认只允许暂存发布,需要明确允许 `npm publish`。新配置必须在 **2 天内**成功发布一次,否则需要删除并重新创建;因此请在准备下一次正式发布时配置。
|
|
50
|
+
|
|
51
|
+
如果选择让 Action 完成首次发布,可临时把 granular token 保存到 GitHub [Actions secret](https://github.com/linyuchen/lucky-lillia-plugin-aiimgdetector/settings/secrets/actions) `NPM_TOKEN`。它需要 **Read and write (publish and stage)** 和 **Bypass two-factor authentication**;未创建的无作用域包无法按已有包名单单独选择,通常需要更宽的 All Packages 权限,因此优先建议首次交互式发布。请在 GitHub 设置中填写 token,不要写入代码或聊天。
|
|
52
|
+
|
|
53
|
+
首次发布后配置 OIDC,并删除、撤销临时 token。npm 已宣布在 **2027 年 1 月**移除 granular token 直接发布新版本的能力,token 仅作为当前的首次发布过渡方案。
|
|
54
|
+
|
|
55
|
+
## 发布新版本
|
|
56
|
+
|
|
57
|
+
首次配置完成后,先在 Actions 页面手动运行验证。通过后在干净工作区执行:
|
|
58
|
+
|
|
59
|
+
```sh
|
|
60
|
+
npm version patch
|
|
61
|
+
git push origin main --follow-tags
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
`npm version patch` 会更新 `package.json` 和 lockfile、创建提交和版本标签。需要其他版本号时可替换为 `minor`、`major` 或具体版本。推送标签后自动发布;单独推送普通提交不会发布。
|
|
65
|
+
|
|
66
|
+
官方说明:[Trusted publishers](https://docs.npmjs.com/trusted-publishers/)、[访问令牌](https://docs.npmjs.com/about-access-tokens/)、[发布来源证明](https://docs.npmjs.com/generating-provenance-statements/)。
|
package/docs/synthid.md
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# SynthID 使用说明
|
|
2
|
+
|
|
3
|
+
SynthID 默认关闭。启用后,只需配置当前账号的 `refresh_token`。检测通过宿主的 HTTP 服务完成:每张图片都会获取新的挑战、在独立 Node.js 子进程中执行官网下发的校验程序、生成新的上传路径和绑定该路径的校验凭据,然后上传原图并读取结果。检测不会启动 Chrome、控制浏览器或重放旧图片的 `bg_token`。
|
|
4
|
+
|
|
5
|
+
检测运行时使用纯 JavaScript,要求 **Node.js 24 或以上版本**,不再依赖 `.node` 原生模块或固定 Node ABI。运行时依赖已随插件声明,不需要安装浏览器来检测图片。
|
|
6
|
+
|
|
7
|
+
## 执行与授权边界
|
|
8
|
+
|
|
9
|
+
纯 HTTP 检测仍会下载并执行 Google 当次下发的 JavaScript 校验程序。这属于远程代码执行能力,并不是改为纯 JavaScript 后就消失的行为。浏览器兼容层会模拟部分浏览器状态,包括 `userActivation` 和 `webdriver`;它不是实际浏览器或可信用户输入。
|
|
10
|
+
|
|
11
|
+
校验程序在单独的 Node 子进程中执行,设置了超时、内存上限、文件读取范围,并禁止原生扩展。`node:vm` 和 Node 权限模型都不是针对恶意代码的完整安全沙箱;目前没有操作系统级沙箱保证。启用此功能意味着信任 Google 下发的校验代码。若部署或市场政策禁止执行远程代码,需要保持 SynthID 关闭;仅增加授权说明不能消除这项执行能力。
|
|
12
|
+
|
|
13
|
+
插件的 `/api/aiimgdetector/synthid/*` 路由经过宿主 WebUI 管理密码鉴权,不是面向 QQ 群成员的公开接口。宿主当前没有向此路由传递 QQ owner/admin 身份,因此不能声称插件做了独立的 QQ 管理角色校验。`credentials` 接口仍可向已通过 WebUI 鉴权的管理端返回长期登录凭据;管理密码及浏览器端的保护属于这一信任边界的一部分。
|
|
14
|
+
|
|
15
|
+
可选登录只打开专属浏览器、由用户完成登录,再读取该浏览器中的账号凭据。插件已移除旧的浏览器上传检测路径及 PowerShell / UI Automation / SendInput 脚本,不会自动操作文件对话框。
|
|
16
|
+
|
|
17
|
+
## 配置登录凭据
|
|
18
|
+
|
|
19
|
+
在插件设置里打开「SynthID 检测,支持 Google、OpenAI、NVIDIA、Kakao 生成的图片」,可用以下任一方式填写登录凭据,完成后点击弹窗底部「保存」。
|
|
20
|
+
|
|
21
|
+
### 自动登录
|
|
22
|
+
|
|
23
|
+
1. Bot 所在电脑安装 Chrome 或 Edge,并具有可交互桌面。
|
|
24
|
+
2. 点击 SynthID 设置内的「登录 SynthID」。窗口在 **Bot 所在电脑** 打开;远程访问 WebUI 时,也需在该电脑操作窗口。
|
|
25
|
+
3. 完成 Google 登录与 SynthID 首次使用提示。插件直接读取该账号的登录凭据,保存并填入表单,然后关闭登录窗口。
|
|
26
|
+
|
|
27
|
+
这一步不会上传测试图片或发起水印检测。检测也无需保留登录窗口。登录过程中可点击取消;登录错误只显示经过清理的信息,不会将失败请求当作有效凭据。
|
|
28
|
+
|
|
29
|
+
「清除浏览器会话」清除本插件的登录记录和独立浏览器资料,不影响平时使用的浏览器,也不删除配置中明确保存的登录凭据。配置为空时可恢复本插件以前保存的账号;明确填写的凭据优先。更换自动登录账号后,保存新表单即可切换检测账号。
|
|
30
|
+
|
|
31
|
+
### 手动填写
|
|
32
|
+
|
|
33
|
+
1. 在自己的浏览器打开 [synthid.com/image-detection](https://synthid.com/image-detection),完成 Google 登录与首次使用提示。
|
|
34
|
+
2. 按 F12,在 **Application(应用)→ IndexedDB → firebaseLocalStorageDb → firebaseLocalStorage** 中展开当前账号对应的 `firebase:authUser:…` 记录。
|
|
35
|
+
3. 在 `value.stsTokenManager.refreshToken` 中复制完整值,不带外围引号,填写「登录凭据」。看不到记录时,确认登录完成,再刷新开发者工具的数据。
|
|
36
|
+
|
|
37
|
+
无需上传测试图片,也无需复制 `Push-Filename` 或 `X-Goog-Ext-525006476-Jspb`。这里需要的是 Firebase `refreshToken`,不是 Google 密码或短期的 `accessToken`。Bot 使用手动凭据时不需要桌面或浏览器。
|
|
38
|
+
|
|
39
|
+
旧配置中的 `pushFilename`、`bgToken` 仍按敏感字段遮罩,但不再用于检测。已保存字段的空白与遮罩处理遵循宿主设置流程;需要更换时填写新的完整值。
|
|
40
|
+
|
|
41
|
+
## 网络、队列与结果
|
|
42
|
+
|
|
43
|
+
所有检测请求均通过 `ctx.http.fetch()`,自动沿用 Bot「代理」插件的规则。可选登录窗口使用 `ctx.http.proxy()` 取得代理;旧宿主保留 HTTP 转发兼容。网络需能够访问 SynthID、Google 登录令牌服务、WAA 挑战服务和 Google 上传服务。
|
|
44
|
+
|
|
45
|
+
检测仅支持图片,最大 20 MB。原图会上传给 Google。队列按服务端刷新确认的账号 ID 串行处理,每账号最多 4 个正在处理或等待的任务;认证前也限制待处理图片数量。配置更新只影响后续请求。取消、超时和插件卸载会中止相应 HTTP 请求并清理解释器子进程。
|
|
46
|
+
|
|
47
|
+
消息图片下载只接受 `gchat.qpic.cn`、`c2cpicdw.qpic.cn`、`multimedia.nt.qq.com.cn` 三个 QQ 图片域名;旧的 HTTP 地址会升级为 HTTPS。其他域名、IP 地址、额外端口、URL 内用户名/密码以及跳转均被拒绝。响应在流式读取过程中限制到 20 MB,并按文件签名确认图片类型,不会把普通文本响应当作 PNG 交给检测服务。
|
|
48
|
+
|
|
49
|
+
每次检测使用一个截止时间;排队等待不消耗检测时间。服务明确拒绝登录时,只刷新并重试一次,重试会重新生成挑战、上传路径和校验凭据。普通 `RPC 7` 不自动重试或解释为登录失效。额度错误按服务提供的等待时间冷却。
|
|
50
|
+
|
|
51
|
+
| 状态 | 含义与处理 |
|
|
52
|
+
| --- | --- |
|
|
53
|
+
| 检出水印 | 显示「图片是 OpenAI 生成」等提示,来源以服务返回值为准 |
|
|
54
|
+
| 未检出 | 没有找到 AI 特征,但不能完全确认不是 AI |
|
|
55
|
+
| 无法判断 | 服务没有足够的信息,例如图片质量或细节不足 |
|
|
56
|
+
| 尚未登录或登录失效 | 自动登录,或更新当前账号的 `refresh_token` |
|
|
57
|
+
| 服务拒绝(RPC 7 / HTTP 403) | 当前请求未被接受,稍后重试;不视为未检出 |
|
|
58
|
+
| 额度用完 | 按提示等待,不重复上传 |
|
|
59
|
+
| 运行环境不可用 | 使用 Node.js 24 或以上版本,并确认插件运行时文件完整 |
|
|
60
|
+
| 无法识别结果 | 官网接口可能变化,不视为未检出 |
|
|
61
|
+
|
|
62
|
+
服务端接受情况可能随时间变化。2026-11-17,编译后的纯 JavaScript HTTP 客户端在 Windows x64、Node.js 24 下通过宿主 HTTP 服务使用已保存账号完成真实检测,返回 `detected`、来源 `openai`;子进程通过 `--no-addons` 禁止加载原生模块。该结果证明当次链路可用,不保证服务始终接受所有请求。
|
|
63
|
+
|
|
64
|
+
## 数据保存
|
|
65
|
+
|
|
66
|
+
登录会话保存在 `ctx.storage.dir()/synthid-session/`。可选浏览器资料位于其中的 `browser/`,只属于本插件。自动获取凭据的接口受宿主 WebUI 鉴权保护,以当前凭据版本号校验并禁止缓存;普通状态接口不返回凭据。
|
|
67
|
+
|
|
68
|
+
轮换后的刷新令牌保存在 `ctx.storage.dir()/synthid-http/`,以原始登录凭据的 SHA-256 作为文件名,原子写入。重启后会再次向登录服务确认账号,不信任磁盘上的 JWT。旧版 `synthid-manual/` 缓存不会被自动作为新的账号身份来源。
|
|
69
|
+
|
|
70
|
+
凭据可代表你的账号,请勿分享、截图或提交到仓库。设置页遮罩不等于磁盘加密,Bot 配置和插件数据目录仍需妥善保管。挑战程序、页面 Cookie、上传路径和每图校验凭据只用于当前请求,不写入登录缓存或日志。
|
|
71
|
+
|
|
72
|
+
## 开发验证
|
|
73
|
+
|
|
74
|
+
运行 `npm run build` 和 `npm test`。测试使用本地 HTTP 服务和合成账号,覆盖参数绑定、原图上传、账号验证、队列、取消、限流、令牌轮换、配置更新和 WebUI 接口。
|
|
75
|
+
|
|
76
|
+
执行 `npm run test:runtime`,使用本地自编程序验证完整解释器运行时、跨窗口调用、快照、资源恢复与进程退出,不连接外网。安装 Chrome 或 Edge 后执行 `npm run test:browser`,验证可选登录、凭据读取、无测试图上传和代理;源码加载还可执行 `node ../../LuckyLillia.Bot/node_modules/tsx/dist/cli.mjs test/synthid-browser.mjs --source`。
|
|
77
|
+
|
|
78
|
+
`scripts/check-pack.mjs` 检查运行时资源、UTF-8 文本可读性,并拒绝打包原生二进制文件。`scripts/smoke-synthid-runtime.mjs` 可接收安装后的 `entry.mjs` 路径,以验证干净 npm 安装及依赖提升后的实际布局。
|
|
79
|
+
|
|
80
|
+
结果协议参考 [sisi0318/synthid 的 PROTOCOL.md](https://github.com/sisi0318/synthid/blob/a4baf87fbe049950b9120a11a239348fd608c55a/PROTOCOL.md)。上游未提供标准 LICENSE,其 README 写有研究用途和商业集成限制;本插件的协议实现不代表上游发布授权已经明确。
|
package/lib/image.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
const MAX_SIZE = 20 * 1024 * 1024;
|
|
2
|
+
// Exact QQ media hosts only. Message senders cannot introduce a domain they
|
|
3
|
+
// control, a local address, a different port, or a redirect destination.
|
|
4
|
+
const IMAGE_HOSTS = new Set(['gchat.qpic.cn', 'c2cpicdw.qpic.cn', 'multimedia.nt.qq.com.cn']);
|
|
5
|
+
function imageUrl(input) {
|
|
6
|
+
if (typeof input !== 'string' || input.length > 16 * 1024 || /[\x00-\x20\x7f\\]/.test(input))
|
|
7
|
+
throw new Error('unsupported image URL');
|
|
8
|
+
let url;
|
|
9
|
+
try {
|
|
10
|
+
url = new URL(input);
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
throw new Error('unsupported image URL');
|
|
14
|
+
}
|
|
15
|
+
if (!['http:', 'https:'].includes(url.protocol) || !IMAGE_HOSTS.has(url.hostname)
|
|
16
|
+
|| url.port || url.username || url.password)
|
|
17
|
+
throw new Error('unsupported image URL');
|
|
18
|
+
// Older QQ adapters return http: URLs for the same CDN. Always use TLS.
|
|
19
|
+
url.protocol = 'https:';
|
|
20
|
+
url.hash = '';
|
|
21
|
+
return url;
|
|
22
|
+
}
|
|
23
|
+
function imageType(bytes) {
|
|
24
|
+
const starts = (...signature) => signature.every((byte, index) => bytes[index] === byte);
|
|
25
|
+
const text = (start, end) => String.fromCharCode(...bytes.subarray(start, end));
|
|
26
|
+
if (starts(0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a))
|
|
27
|
+
return 'image/png';
|
|
28
|
+
if (starts(0xff, 0xd8, 0xff))
|
|
29
|
+
return 'image/jpeg';
|
|
30
|
+
if (['GIF87a', 'GIF89a'].includes(text(0, 6)))
|
|
31
|
+
return 'image/gif';
|
|
32
|
+
if (text(0, 4) === 'RIFF' && text(8, 12) === 'WEBP')
|
|
33
|
+
return 'image/webp';
|
|
34
|
+
if (starts(0x42, 0x4d))
|
|
35
|
+
return 'image/bmp';
|
|
36
|
+
if (starts(0x49, 0x49, 0x2a, 0) || starts(0x4d, 0x4d, 0, 0x2a))
|
|
37
|
+
return 'image/tiff';
|
|
38
|
+
if (text(4, 8) === 'ftyp' && bytes.length >= 16) {
|
|
39
|
+
const brands = [text(8, 12)];
|
|
40
|
+
for (let offset = 16; offset + 4 <= Math.min(bytes.length, 64); offset += 4)
|
|
41
|
+
brands.push(text(offset, offset + 4));
|
|
42
|
+
if (brands.some(brand => ['avif', 'avis'].includes(brand)))
|
|
43
|
+
return 'image/avif';
|
|
44
|
+
if (brands.some(brand => ['heic', 'heix', 'hevc', 'hevx'].includes(brand)))
|
|
45
|
+
return 'image/heic';
|
|
46
|
+
if (brands.some(brand => ['mif1', 'msf1'].includes(brand)))
|
|
47
|
+
return 'image/heif';
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
export async function download(ctx, url) {
|
|
51
|
+
const target = imageUrl(url);
|
|
52
|
+
const response = await ctx.http.fetch(target.href, {
|
|
53
|
+
redirect: 'error', credentials: 'omit', referrerPolicy: 'no-referrer', signal: AbortSignal.timeout(30_000),
|
|
54
|
+
}, { timeout: 30_000, proxyAgent: '' });
|
|
55
|
+
const length = response.headers.get('content-length');
|
|
56
|
+
if (!response.ok || response.redirected || length !== null && Number(length) > MAX_SIZE || !response.body) {
|
|
57
|
+
await response.body?.cancel();
|
|
58
|
+
throw new Error('image download was refused or exceeds 20 MB');
|
|
59
|
+
}
|
|
60
|
+
const reader = response.body.getReader(), chunks = [];
|
|
61
|
+
let size = 0;
|
|
62
|
+
try {
|
|
63
|
+
while (true) {
|
|
64
|
+
const { done, value } = await reader.read();
|
|
65
|
+
if (done)
|
|
66
|
+
break;
|
|
67
|
+
size += value.byteLength;
|
|
68
|
+
if (size > MAX_SIZE)
|
|
69
|
+
throw new Error('the image is over 20 MB');
|
|
70
|
+
chunks.push(value);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
await reader.cancel().catch(() => { });
|
|
75
|
+
throw error;
|
|
76
|
+
}
|
|
77
|
+
finally {
|
|
78
|
+
reader.releaseLock();
|
|
79
|
+
}
|
|
80
|
+
const data = new Uint8Array(size);
|
|
81
|
+
let offset = 0;
|
|
82
|
+
for (const chunk of chunks) {
|
|
83
|
+
data.set(chunk, offset);
|
|
84
|
+
offset += chunk.byteLength;
|
|
85
|
+
}
|
|
86
|
+
const type = imageType(data);
|
|
87
|
+
if (!type)
|
|
88
|
+
throw new Error('the response is not a supported image');
|
|
89
|
+
return { data: data.buffer, type };
|
|
90
|
+
}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { h, Schema } from '@luckylillia-dev/plugin-sdk';
|
|
2
|
+
import { download } from './image.js';
|
|
3
|
+
import { checkProvenance, provenanceService } from './provenance.js';
|
|
4
|
+
import { SynthIdClient, handleSynthId } from './synthid.js';
|
|
5
|
+
import { describeSynthIdFailure, formatSynthId, SynthIdError } from './synthid-result.js';
|
|
6
|
+
import { describeManualSynthIdFailure, ManualSynthIdError } from './synthid-manual.js';
|
|
7
|
+
import { judgeByVision } from './vision.js';
|
|
8
|
+
export const name = 'aiimgdetector';
|
|
9
|
+
export const inject = ['bots', 'ai', 'http', 'commands'];
|
|
10
|
+
export const Config = Schema.object({
|
|
11
|
+
trigger: Schema.string().pattern(/^\s*\S*\s*$/).default('ai吗').description('触发词,不分大小写:对着图片回复它,或者它后面直接带图'),
|
|
12
|
+
cooldown: Schema.natural().role('ms').default(10_000).description('同一个群(或私聊)两次检测至少隔多久'),
|
|
13
|
+
provenance: Schema.union([
|
|
14
|
+
Schema.const(false),
|
|
15
|
+
Schema.object({
|
|
16
|
+
ai: Schema.string().role('ai').extra('extra', {
|
|
17
|
+
provider: 'openai',
|
|
18
|
+
emptyHint: 'OpenAI 水印检测免费,但有调用频率限制;请求过于频繁时请稍后再试。',
|
|
19
|
+
}).default('').description('服务'),
|
|
20
|
+
}),
|
|
21
|
+
]).default({ ai: '' }).description('OpenAI 官方 API,检测 ChatGPT 等 OpenAI 工具生成的图片'),
|
|
22
|
+
synthid: Schema.union([
|
|
23
|
+
Schema.const(false).description('不用'),
|
|
24
|
+
Schema.object({
|
|
25
|
+
// Mask credentials from the previous version if an old saved config still contains them.
|
|
26
|
+
// They are ignored by the browser login and are never displayed as form fields.
|
|
27
|
+
refreshToken: Schema.string().role('secret').hidden(),
|
|
28
|
+
bgToken: Schema.string().role('secret').hidden(),
|
|
29
|
+
pushFilename: Schema.string().role('secret').hidden(),
|
|
30
|
+
timeout: Schema.natural().min(1000).max(120_000).role('ms').default(30_000).description('检测超时'),
|
|
31
|
+
credentials: Schema.union([
|
|
32
|
+
Schema.object({
|
|
33
|
+
refreshToken: Schema.string().role('secret').default('').description('登录凭据(refresh_token)'),
|
|
34
|
+
pushFilename: Schema.string().role('secret').default('').hidden(),
|
|
35
|
+
bgToken: Schema.string().role('secret').default('').hidden(),
|
|
36
|
+
}),
|
|
37
|
+
// Migrate the previous browser/manual switch without losing saved secrets.
|
|
38
|
+
Schema.transform(Schema.const(false).hidden(), () => ({ refreshToken: '', pushFilename: '', bgToken: '' })),
|
|
39
|
+
]).default({ refreshToken: '', pushFilename: '', bgToken: '' }).description('登录凭证'),
|
|
40
|
+
}).description('用'),
|
|
41
|
+
]).default(false).description('SynthID 检测,支持 Google、OpenAI、NVIDIA、Kakao 生成的图片'),
|
|
42
|
+
vision: Schema.union([
|
|
43
|
+
Schema.const(false).description('不用'),
|
|
44
|
+
Schema.object({
|
|
45
|
+
ai: Schema.string().default('').description('服务'),
|
|
46
|
+
model: Schema.string().default('').description('模型:请选择支持图片输入的模型'),
|
|
47
|
+
}).role('ai-model').description('用'),
|
|
48
|
+
]).default({ ai: '', model: '' }).description('多模态模型判断:让能看图的 AI 从画面里找 AI 生成的破绽。只是推测,按图片计费'),
|
|
49
|
+
});
|
|
50
|
+
const VERDICT_TEXT = {
|
|
51
|
+
ai: '疑似 AI 生成',
|
|
52
|
+
real: '更像人工创作',
|
|
53
|
+
unsure: '看不出来',
|
|
54
|
+
};
|
|
55
|
+
export function apply(ctx, config) {
|
|
56
|
+
const logger = ctx.logger('aiimgdetector');
|
|
57
|
+
const trigger = config.trigger.trim();
|
|
58
|
+
const lastCheck = new Map();
|
|
59
|
+
// Login is available before enabling detection; saving a config must not lose the browser session.
|
|
60
|
+
const synthid = new SynthIdClient(ctx, config.synthid || { timeout: 30_000 });
|
|
61
|
+
ctx.inject(['webuiServer'], (ctx) => {
|
|
62
|
+
// SDK contract: private /api routes run after the host's WebUI password
|
|
63
|
+
// authentication. This is management access, not a QQ message role check.
|
|
64
|
+
ctx.webuiServer.route('/api/aiimgdetector/synthid', { fetch: request => handleSynthId(synthid, request) }, { public: false });
|
|
65
|
+
});
|
|
66
|
+
let active = true;
|
|
67
|
+
ctx.effect(() => () => { active = false; });
|
|
68
|
+
ctx.on('internal/update', (next, restart) => {
|
|
69
|
+
if (next.trigger.trim() !== trigger)
|
|
70
|
+
return restart();
|
|
71
|
+
synthid.update(next.synthid || { timeout: 30_000 });
|
|
72
|
+
config = next;
|
|
73
|
+
});
|
|
74
|
+
function cooling(key) {
|
|
75
|
+
if (!config.cooldown)
|
|
76
|
+
return false;
|
|
77
|
+
const now = Date.now();
|
|
78
|
+
const last = lastCheck.get(key);
|
|
79
|
+
if (last !== undefined && now - last < config.cooldown)
|
|
80
|
+
return true;
|
|
81
|
+
lastCheck.set(key, now);
|
|
82
|
+
for (const [other, at] of lastCheck)
|
|
83
|
+
if (now - at >= config.cooldown)
|
|
84
|
+
lastCheck.delete(other);
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
async function verdict(image) {
|
|
88
|
+
// Whether a check ran to the end without finding anything, and what failed on the way
|
|
89
|
+
let checked = false;
|
|
90
|
+
let failure;
|
|
91
|
+
let inconclusive;
|
|
92
|
+
const watermarkAi = config.provenance
|
|
93
|
+
? provenanceService(ctx.ai, config.provenance.ai) : undefined;
|
|
94
|
+
if (watermarkAi) {
|
|
95
|
+
try {
|
|
96
|
+
const answer = await checkProvenance(ctx.ai, watermarkAi, image);
|
|
97
|
+
if (answer.results?.some(result => result.outcome === 'detected'))
|
|
98
|
+
return '图片由 OpenAI 制作。';
|
|
99
|
+
checked = true;
|
|
100
|
+
}
|
|
101
|
+
catch (e) {
|
|
102
|
+
failure = e;
|
|
103
|
+
logger.warn('watermark check failed:', e.message);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (config.synthid && active) {
|
|
107
|
+
try {
|
|
108
|
+
const result = await synthid.detect(image);
|
|
109
|
+
if (result.status === 'detected')
|
|
110
|
+
return formatSynthId(result);
|
|
111
|
+
if (result.status === 'not_detected')
|
|
112
|
+
checked = true;
|
|
113
|
+
else if (result.status === 'uncertain')
|
|
114
|
+
inconclusive = formatSynthId(result);
|
|
115
|
+
else if (result.status === 'quota_exceeded') {
|
|
116
|
+
failure ??= new SynthIdError('rate-limit', 'SynthID account quota exceeded', undefined, result.retryAfterSeconds);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
catch (e) {
|
|
120
|
+
failure ??= e;
|
|
121
|
+
if (active)
|
|
122
|
+
logger.warn('SynthID check failed:', e.message);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (config.vision && active) {
|
|
126
|
+
try {
|
|
127
|
+
const result = await judgeByVision(ctx.ai, config.vision, image);
|
|
128
|
+
if (result)
|
|
129
|
+
return formatVision(result);
|
|
130
|
+
inconclusive ??= '看图模型没有返回检测结果,暂时无法判断。';
|
|
131
|
+
}
|
|
132
|
+
catch (e) {
|
|
133
|
+
failure ??= e;
|
|
134
|
+
logger.warn('vision check failed:', e.message);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
if (failure)
|
|
138
|
+
return describeFailure(failure);
|
|
139
|
+
if (inconclusive)
|
|
140
|
+
return inconclusive;
|
|
141
|
+
if (checked)
|
|
142
|
+
return formatSynthId({ status: 'not_detected' });
|
|
143
|
+
if (config.provenance)
|
|
144
|
+
return config.provenance.ai
|
|
145
|
+
? '所选服务无法用于水印检测,请重新选择 OpenAI 官方服务,或打开看图判断。'
|
|
146
|
+
: '没有可用于水印检测的 OpenAI 官方服务,请在 AI 服务中添加,或打开看图判断。';
|
|
147
|
+
return '还没有打开任何检测,请在 aiimgdetector 插件的设置里打开。';
|
|
148
|
+
}
|
|
149
|
+
if (!trigger)
|
|
150
|
+
return;
|
|
151
|
+
ctx.command(trigger, '检测图片是否由 AI 生成').action(async ({ session, next }) => {
|
|
152
|
+
const picked = pickImage(session);
|
|
153
|
+
// A message starting with the word is not always meant for this plugin
|
|
154
|
+
if (!picked)
|
|
155
|
+
return next();
|
|
156
|
+
if (cooling(`${session.platform}:${session.channelId}`))
|
|
157
|
+
return next();
|
|
158
|
+
// Quote the message the image is in: with several images in a busy group, that says which one it was about
|
|
159
|
+
const reply = (text) => session.send(picked.messageId ? [h('quote', { id: picked.messageId }), h.text(text)] : h.text(text));
|
|
160
|
+
let image;
|
|
161
|
+
try {
|
|
162
|
+
image = await download(ctx, picked.url);
|
|
163
|
+
}
|
|
164
|
+
catch (e) {
|
|
165
|
+
logger.warn('cannot download the image:', e.message);
|
|
166
|
+
await reply('图片下载失败,请稍后再试。');
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
if (!active)
|
|
170
|
+
return;
|
|
171
|
+
const answer = await verdict(image);
|
|
172
|
+
if (active)
|
|
173
|
+
await reply(answer);
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
/** An image of the message itself first, then one of the message it quotes */
|
|
177
|
+
function pickImage(session) {
|
|
178
|
+
const own = h.select(session.elements.filter(element => element.type !== 'quote'), 'img')[0];
|
|
179
|
+
if (own)
|
|
180
|
+
return { url: String(own.attrs.src ?? own.attrs.url), messageId: session.messageId };
|
|
181
|
+
const quote = session.elements.find(element => element.type === 'quote');
|
|
182
|
+
const quoted = quote && h.select(quote.children, 'img')[0];
|
|
183
|
+
if (quoted)
|
|
184
|
+
return { url: String(quoted.attrs.src ?? quoted.attrs.url), messageId: quote.attrs.id || session.messageId };
|
|
185
|
+
}
|
|
186
|
+
function formatVision(result) {
|
|
187
|
+
if (result.raw)
|
|
188
|
+
return `${result.model} 判断:${result.raw}`;
|
|
189
|
+
const head = result.confidence ? `${VERDICT_TEXT[result.verdict]}(置信度 ${result.confidence}%)` : VERDICT_TEXT[result.verdict];
|
|
190
|
+
return [`${result.model} 判断:${head}`, ...result.reasons.map(reason => `- ${reason}`)].join('\n');
|
|
191
|
+
}
|
|
192
|
+
/** What group members can act on; the details go to the log */
|
|
193
|
+
function describeFailure(error) {
|
|
194
|
+
if (error instanceof ManualSynthIdError)
|
|
195
|
+
return describeManualSynthIdFailure(error);
|
|
196
|
+
if (error instanceof SynthIdError)
|
|
197
|
+
return describeSynthIdFailure(error);
|
|
198
|
+
switch (error?.kind) {
|
|
199
|
+
case 'rate-limit': return '检测请求过于频繁,请稍后再试。';
|
|
200
|
+
case 'quota': return 'AI 服务的余额或额度用完了。';
|
|
201
|
+
case 'auth': return 'API Key 无效或没有权限。';
|
|
202
|
+
case 'network':
|
|
203
|
+
case 'timeout': return '连不上检测服务,请检查网络或代理设置。';
|
|
204
|
+
case 'config': return 'AI 服务或模型尚未配置好。';
|
|
205
|
+
default: return '检测失败,请稍后再试。';
|
|
206
|
+
}
|
|
207
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** Keep implicit selection in sync with the filtered service selector: official default, then list order. */
|
|
2
|
+
export function provenanceService(ai, name) {
|
|
3
|
+
if (name) {
|
|
4
|
+
const explicit = ai.get(name);
|
|
5
|
+
return explicit?.provider === 'openai' ? explicit.name : undefined;
|
|
6
|
+
}
|
|
7
|
+
const fallback = ai.get();
|
|
8
|
+
if (fallback?.provider === 'openai')
|
|
9
|
+
return fallback.name;
|
|
10
|
+
return ai.list().find(service => service.provider === 'openai')?.name;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Whether OpenAI's own marks are in the image: its C2PA credentials and SynthID watermark. It knows nothing of other
|
|
14
|
+
* makers' images, and only OpenAI's own API has the endpoint (relays answer 404).
|
|
15
|
+
*/
|
|
16
|
+
export async function checkProvenance(ai, name, image) {
|
|
17
|
+
const form = new FormData();
|
|
18
|
+
form.append('file', new Blob([image.data], { type: image.type }), 'image');
|
|
19
|
+
const response = await ai.request(name, '/content_provenance_checks', { method: 'POST', body: form });
|
|
20
|
+
return await response.json();
|
|
21
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
2
|
+
import { mkdir, readFile, rename, unlink, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { SynthIdError } from './synthid-result.js';
|
|
5
|
+
const locks = new Map();
|
|
6
|
+
export function tokenClaims(token) {
|
|
7
|
+
try {
|
|
8
|
+
const value = JSON.parse(Buffer.from(token.split('.')[1], 'base64url').toString('utf8'));
|
|
9
|
+
return value && typeof value === 'object' && !Array.isArray(value) ? value : {};
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
return {};
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
const validSecret = (value) => typeof value === 'string' && /^[\x21-\x7e]+$/.test(value);
|
|
16
|
+
function waitForLock(lock, signal) {
|
|
17
|
+
if (!signal)
|
|
18
|
+
return lock;
|
|
19
|
+
signal.throwIfAborted();
|
|
20
|
+
return new Promise((resolve, reject) => {
|
|
21
|
+
const abort = () => reject(signal.reason);
|
|
22
|
+
signal.addEventListener('abort', abort, { once: true });
|
|
23
|
+
void lock.then(() => { signal.removeEventListener('abort', abort); resolve(); }, reject);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
/** Trust account identity only after the HTTPS refresh endpoint responds.
|
|
27
|
+
* Persist rotated refresh tokens atomically; a disk JWT is never authority. */
|
|
28
|
+
export class SynthIdTokenStore {
|
|
29
|
+
ctx;
|
|
30
|
+
initial;
|
|
31
|
+
tokens;
|
|
32
|
+
file;
|
|
33
|
+
updatedAt = 0;
|
|
34
|
+
constructor(ctx, initial) {
|
|
35
|
+
this.ctx = ctx;
|
|
36
|
+
this.initial = initial;
|
|
37
|
+
}
|
|
38
|
+
async get(refresh, force = false, signal) {
|
|
39
|
+
signal?.throwIfAborted();
|
|
40
|
+
if (!force && this.tokens && Number(tokenClaims(this.tokens.idToken).exp) > Date.now() / 1000 + 180)
|
|
41
|
+
return { ...this.tokens };
|
|
42
|
+
const directory = join(await this.ctx.storage.dir(), 'synthid-http');
|
|
43
|
+
const file = this.file ??= join(directory, createHash('sha256').update(this.initial).digest('hex') + '.json');
|
|
44
|
+
const previous = locks.get(file) ?? Promise.resolve();
|
|
45
|
+
let release;
|
|
46
|
+
const gate = new Promise(resolve => { release = resolve; });
|
|
47
|
+
const tail = previous.then(() => gate);
|
|
48
|
+
locks.set(file, tail);
|
|
49
|
+
try {
|
|
50
|
+
await waitForLock(previous, signal);
|
|
51
|
+
signal?.throwIfAborted();
|
|
52
|
+
if (!force && this.tokens && Number(tokenClaims(this.tokens.idToken).exp) > Date.now() / 1000 + 180)
|
|
53
|
+
return { ...this.tokens };
|
|
54
|
+
let refreshToken = this.tokens?.refreshToken ?? this.initial;
|
|
55
|
+
try {
|
|
56
|
+
const saved = JSON.parse(await readFile(file, 'utf8'));
|
|
57
|
+
if (validSecret(saved?.refreshToken) && (!this.tokens || saved.updatedAt >= this.updatedAt))
|
|
58
|
+
refreshToken = saved.refreshToken;
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
if (error?.code !== 'ENOENT')
|
|
62
|
+
this.ctx.logger('aiimgdetector').warn('Could not load SynthID login cache');
|
|
63
|
+
}
|
|
64
|
+
const value = await refresh(refreshToken);
|
|
65
|
+
if (!validSecret(value.id_token))
|
|
66
|
+
throw new SynthIdError('response', 'SynthID token refresh returned an invalid token');
|
|
67
|
+
const claims = tokenClaims(value.id_token);
|
|
68
|
+
const userId = claims.user_id ?? claims.sub;
|
|
69
|
+
if (typeof userId !== 'string' || !/^[A-Za-z0-9_-]{1,128}$/.test(userId)
|
|
70
|
+
|| claims.sub !== undefined && claims.sub !== userId || claims.user_id !== undefined && claims.user_id !== userId
|
|
71
|
+
|| value.user_id !== undefined && value.user_id !== userId || this.tokens && this.tokens.userId !== userId) {
|
|
72
|
+
throw new SynthIdError('auth', 'SynthID token refresh returned inconsistent account information');
|
|
73
|
+
}
|
|
74
|
+
if (typeof claims.exp !== 'number' || !Number.isFinite(claims.exp) || claims.exp <= Date.now() / 1000) {
|
|
75
|
+
throw new SynthIdError('response', 'SynthID token refresh returned an invalid expiry');
|
|
76
|
+
}
|
|
77
|
+
this.tokens = { userId, idToken: value.id_token, refreshToken: validSecret(value.refresh_token) ? value.refresh_token : refreshToken };
|
|
78
|
+
this.updatedAt = Math.max(Date.now(), this.updatedAt + 1);
|
|
79
|
+
const temporary = file + '.' + randomUUID() + '.tmp';
|
|
80
|
+
try {
|
|
81
|
+
await mkdir(directory, { recursive: true, mode: 0o700 });
|
|
82
|
+
// Only the rotating secret is needed after a restart.
|
|
83
|
+
await writeFile(temporary, JSON.stringify({ refreshToken: this.tokens.refreshToken, updatedAt: this.updatedAt }), { mode: 0o600, flag: 'wx' });
|
|
84
|
+
await rename(temporary, file);
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
this.ctx.logger('aiimgdetector').warn('Could not save SynthID login cache; login may need renewing after restart');
|
|
88
|
+
}
|
|
89
|
+
finally {
|
|
90
|
+
await unlink(temporary).catch(() => { });
|
|
91
|
+
}
|
|
92
|
+
return { ...this.tokens };
|
|
93
|
+
}
|
|
94
|
+
finally {
|
|
95
|
+
release();
|
|
96
|
+
void tail.then(() => { if (locks.get(file) === tail)
|
|
97
|
+
locks.delete(file); });
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|