feat(ai-gen): 自定义提供商(KEY只存本地) + 生成历史缩略图列

- 左侧新增「我的提供商」配置块:OpenAI 兼容 base_url + KEY,连接拉取模型列表,手动添加/移除/清除
- 自定义模型与站内模型合并展示(optgroup 分组 + 紫色虚线胶囊)
- KEY 仅存浏览器 localStorage,生成时经 relay 转发,服务端零落盘(已验证)
- 每次生成成功服务端落盘(按机器隔离,100张上限),右侧 50x50 缩略图竖排历史列
- 点击历史缩略图回看大图,下载链接带 ?dl=1 附件下载
- 去掉缩略图 loading=lazy(该场景不触发加载导致空白)
This commit is contained in:
3970260761 committed 2026-11-12 22:54:12 +08:00
1 parent 66c3fede6b
commit 003f98e388
2 files changed
+458 -17

No files matched your search

+8
View File
@@ -55,6 +55,14 @@
- **调用记录**:Admin 左侧「MCP 调用」页(近 7 天总览 / 工具排行 / 调用流水),relay 侧 `mcp_usage` 表逐条落库
- **客户端接入**:Hermes(`config.yaml` 的 `mcp_servers`)、任意支持 HTTP 传输的 MCP 客户端、curl;配置片段在功能页 ③ 区一键复制
### AI 生图:自定义提供商 + 生成历史(2026.11 新增)
- **模型来源与免费说明**:站内 10 个生图模型全部经自建 NewAPI 网关聚合——5 个 `pollinations-*` 走 Pollinations.ai(完全免费、无需 KEY),4 个 `flux*` + `dall-e-3` 走 Cloudflare Workers AI(站主账号承担免费额度),**对使用者全部免费**;模型列表由后端从网关实时拉取。
- **我的提供商**:左侧「+ 使用我的提供商 / API KEY」展开,填写 OpenAI 兼容接口地址 + KEY,点「连接并拉取模型」自动拉取上游模型列表;与站内模型合并展示(下拉分组「站内免费模型 / 我的模型」,自定义模型胶囊为紫色虚线框)。支持手动输入模型名回车添加、单个移除、一键清除。
- **KEY 只存本地**:接口地址与 KEY 仅保存在浏览器 localStorage(`it-tools-ai-provider`),生成时临时发给后端转发,**不入库、不落盘**(已 grep 宿主与容器数据目录验证零残留)。
- **生成历史**:每次生成成功自动落盘服务端(按机器隔离,每机上限 100 张、超限自动清理最旧),页面右侧新增 50×50 缩略图竖排历史列;点击任意缩略图回看大图,「下载图片」带 `?dl=1` 触发附件下载,「新窗口打开」直连原图,刷新后历史仍在。
- **新增端点**:`POST /relay/ai/custom-models`(测试连接并拉取模型)、`POST /relay/ai/gen-image` 扩展可选 `provider` 字段(自定义提供商转发)、`GET /relay/ai/img-history?machine=`(历史列表)、`GET /relay/ai/img/<machine>/<id>[?dl=1]`(图片文件/下载)。
## 优势
- **一次激活,全站通用**:输入一次卡密,全部授权功能解锁,无需逐个购买
+450 -17
View File
@@ -1,13 +1,114 @@
<script setup lang="ts">
import { getMachineId, licenseHeaders } from '@/tools/license/license';
const models = ref<{ id: string; i2i: boolean }[]>([]);
const models = ref<{ id: string; i2i: boolean; custom?: boolean }[]>([]);
const model = ref('pollinations-flux');
const prompt = ref('');
const loading = ref(false);
const error = ref('');
const resultUrl = ref('');
// ---------- 自定义提供商(KEY 只存本机浏览器 localStorage) ----------
const PROVIDER_KEY = 'it-tools-ai-provider';
const provider = ref<{ base_url: string; api_key: string; models: string[] }>({ base_url: '', api_key: '', models: [] });
const provOpen = ref(false);
const provBusy = ref(false);
const provErr = ref('');
const manualModel = ref('');
function loadProvider() {
try {
const raw = localStorage.getItem(PROVIDER_KEY);
if (raw) {
const p = JSON.parse(raw);
provider.value = {
base_url: typeof p.base_url === 'string' ? p.base_url : '',
api_key: typeof p.api_key === 'string' ? p.api_key : '',
models: Array.isArray(p.models) ? p.models.map((x: any) => String(x)) : [],
};
}
} catch { /* ignore */ }
}
function saveProvider() {
try { localStorage.setItem(PROVIDER_KEY, JSON.stringify(provider.value)); } catch { /* ignore */ }
}
watch(provider, saveProvider, { deep: true });
const hasCreds = computed(() => !!(provider.value.base_url.trim() && provider.value.api_key.trim()));
async function connectProvider() {
provErr.value = '';
if (!hasCreds.value) { provErr.value = '请填写接口地址和 API KEY'; return; }
provBusy.value = true;
try {
const res = await fetch('/relay/ai/custom-models', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
machine: getMachineId(),
base_url: provider.value.base_url.trim(),
api_key: provider.value.api_key.trim(),
}),
});
const j = await res.json().catch(() => ({}));
if (j.code === 200 && Array.isArray(j.data) && j.data.length) {
provider.value.models = j.data.map((x: any) => String(x));
saveProvider();
mergeModels();
if (!models.value.some((m) => m.id === model.value) && provider.value.models.length) {
model.value = provider.value.models[0];
}
} else {
provErr.value = j.message || '连接失败';
}
} catch (e: any) {
provErr.value = String(e?.message || e);
} finally {
provBusy.value = false;
}
}
function addManualModel() {
const v = manualModel.value.trim();
if (!v) return;
if (!provider.value.models.includes(v)) provider.value.models.push(v);
manualModel.value = '';
saveProvider();
mergeModels();
}
function removeProviderModel(m: string) {
provider.value.models = provider.value.models.filter((x) => x !== m);
saveProvider();
mergeModels();
}
function clearProvider() {
provider.value = { base_url: '', api_key: '', models: [] };
provErr.value = '';
saveProvider();
mergeModels();
}
// 站内模型 + 用户自填模型合并(自定义款 custom=true 排后面)
let inhouseModels: { id: string; i2i: boolean }[] = [];
function mergeModels() {
const inhouse = inhouseModels.map((m) => ({ ...m, custom: false }));
const mine = provider.value.models.map((id) => ({ id, i2i: false, custom: true }));
models.value = [...inhouse, ...mine];
}
// ---------- 生成历史(服务端落盘,点缩略图回看/下载) ----------
const history = ref<{ id: string; at: number; model: string; prompt: string; img_url: string }[]>([]);
async function loadHistory() {
try {
const res = await fetch(`/relay/ai/img-history?machine=${encodeURIComponent(getMachineId())}`);
const j = await res.json();
if (j.code === 200 && Array.isArray(j.data)) history.value = j.data;
} catch { /* ignore */ }
}
function openHist(it: any) {
resultUrl.value = it.img_url || '';
error.value = '';
}
// 宽高比
type Ratio = { label: string; w: number; h: number; sw: number; sh: number };
const RATIOS: Ratio[] = [
@@ -38,19 +139,24 @@ const EXAMPLES = [
const canGen = computed(() => !!prompt.value.trim());
onMounted(async () => {
loadProvider();
loadHistory();
try {
const res = await fetch('/relay/ai/image-models');
const j = await res.json();
if (j.code === 200 && Array.isArray(j.data) && j.data.length) {
models.value = j.data;
if (!j.data.some((m: any) => m.id === model.value)) {
inhouseModels = j.data;
mergeModels();
if (!models.value.some((m: any) => m.id === model.value)) {
// 默认选一个 pollinations(稳定可用)
const p = j.data.find((m: any) => m.id.startsWith('pollinations'));
model.value = (p || j.data[0]).id;
}
} else {
mergeModels();
}
}
catch { /* 模型列表加载失败则用默认 */ }
catch { mergeModels(); /* 模型列表加载失败则用默认 */ }
});
function b64DataUrl(b64: string) {
@@ -60,6 +166,13 @@ function b64DataUrl(b64: string) {
return `data:${mime};base64,${b64}`;
}
// 站内历史文件下载带 ?dl=1(触发 attachment);dataUrl/外链原样
const dlHref = computed(() => {
const u = resultUrl.value;
if (!u) return '#';
return u.startsWith('/relay/ai/img/') ? u + '?dl=1' : u;
});
async function generate() {
if (loading.value || !canGen.value) return;
loading.value = true;
@@ -67,20 +180,28 @@ async function generate() {
resultUrl.value = '';
try {
const r = ratioObj.value;
const body: any = {
machine: getMachineId(),
prompt: prompt.value.trim(),
model: model.value,
width: r.w,
height: r.h,
};
const isMine = provider.value.models.includes(model.value);
if (isMine) {
if (!hasCreds.value) throw new Error('已选自定义模型:请先在「我的提供商」填写接口地址和 API KEY');
body.provider = { base_url: provider.value.base_url.trim(), api_key: provider.value.api_key.trim() };
}
const res = await fetch('/relay/ai/gen-image', {
method: 'POST',
headers: { 'Content-Type': 'application/json', ...licenseHeaders('ai-gen') },
body: JSON.stringify({
machine: getMachineId(),
prompt: prompt.value.trim(),
model: model.value,
width: r.w,
height: r.h,
}),
body: JSON.stringify(body),
});
const j = await res.json().catch(() => ({}));
if (!res.ok || j.code !== 200) throw new Error(j.message || `HTTP ${res.status}`);
resultUrl.value = j.data.b64 ? b64DataUrl(j.data.b64) : j.data.url;
const dataUrl = j.data.b64 ? b64DataUrl(j.data.b64) : '';
resultUrl.value = j.data.img_url || dataUrl || j.data.url;
loadHistory();
}
catch (e: any) {
error.value = String(e?.message || e);
@@ -100,7 +221,12 @@ async function generate() {
<div class="gen-sec">
<div class="gen-h">模型</div>
<select v-model="model" class="gen-model-sel">
<option v-for="m in models" :key="m.id" :value="m.id">{{ m.id }}</option>
<optgroup label="站内免费模型">
<option v-for="m in models.filter(x => !x.custom)" :key="m.id" :value="m.id">{{ m.id }}</option>
</optgroup>
<optgroup v-if="models.some(x => x.custom)" label="我的模型">
<option v-for="m in models.filter(x => x.custom)" :key="m.id" :value="m.id">{{ m.id }}(我的)</option>
</optgroup>
<option v-if="!models.length" value="pollinations-flux">pollinations-flux</option>
</select>
<div class="gen-pills">
@@ -108,12 +234,43 @@ async function generate() {
v-for="m in models"
:key="m.id"
class="gen-pill"
:class="{ on: model === m.id }"
:class="{ on: model === m.id, mine: m.custom }"
@click="model = m.id"
>
{{ m.id }}
</button>
</div>
<!-- 自定义提供商:KEY 只存本机 -->
<div class="prov">
<button type="button" class="prov-toggle" @click="provOpen = !provOpen">
<span>{{ hasCreds ? '✓ 我的提供商已配置' : '+ 使用我的提供商 / API KEY' }}</span>
<span class="prov-caret">{{ provOpen ? '▾' : '▸' }}</span>
</button>
<div v-show="provOpen" class="prov-form">
<input v-model="provider.base_url" class="prov-input" placeholder="接口地址(OpenAI 兼容,如 https://api.openai.com/v1)" spellcheck="false" />
<input v-model="provider.api_key" class="prov-input" type="password" placeholder="API KEY(仅保存在本机浏览器)" spellcheck="false" autocomplete="off" />
<div class="prov-actions">
<button type="button" class="prov-btn prov-primary" :disabled="provBusy" @click="connectProvider">
{{ provBusy ? '⏳ 连接中…' : '🔌 连接并拉取模型' }}
</button>
<button type="button" class="prov-btn" @click="saveProvider">💾 保存到本地</button>
<button v-if="provider.base_url || provider.api_key || provider.models.length" type="button" class="prov-btn prov-danger" @click="clearProvider">清除</button>
</div>
<div v-if="provErr" class="prov-err">⚠️ {{ provErr }}</div>
<div class="prov-hint">接口地址与 KEY 只保存在你的浏览器(localStorage),生成时临时发给服务器转发,不入库、不落盘。</div>
<div v-if="provider.models.length" class="prov-models">
<span v-for="m in provider.models" :key="m" class="prov-tag">
{{ m }}
<i title="移除" @click="removeProviderModel(m)">×</i>
</span>
</div>
<div class="prov-manual">
<input v-model="manualModel" class="prov-input" placeholder="手动输入模型名,回车添加" @keyup.enter="addManualModel" />
<button type="button" class="prov-btn" @click="addManualModel">添加</button>
</div>
</div>
</div>
</div>
<!-- 提示词 -->
@@ -159,12 +316,12 @@ async function generate() {
<div v-if="error" class="gen-err">⚠️ {{ error }}</div>
</div>
<!-- 右侧结果(占满剩余空间) -->
<!-- 中间结果(占满剩余空间) -->
<div class="gen-result">
<div v-if="resultUrl" class="gen-img-box">
<img :src="resultUrl" alt="生成结果">
<div class="gen-img-acts">
<a class="gen-dl" :href="resultUrl" target="_blank" rel="noopener" download>⬇ 下载图片</a>
<a class="gen-dl" :href="dlHref" target="_blank" rel="noopener" download>⬇ 下载图片</a>
<a class="gen-dl" :href="resultUrl" target="_blank" rel="noopener">🔗 新窗口打开</a>
</div>
</div>
@@ -174,6 +331,22 @@ async function generate() {
</div>
<div v-else class="gen-empty">生成的图片会显示在这里</div>
</div>
<!-- 右侧历史:50×50 缩略图竖排 -->
<div class="gen-hist">
<div class="hist-t">历史<em v-if="history.length">{{ history.length }}</em></div>
<div v-if="history.length" class="hist-list">
<img
v-for="it in history"
:key="it.id"
:src="it.img_url"
:title="it.prompt"
:class="{ on: resultUrl === it.img_url }"
@click="openHist(it)"
/>
</div>
<div v-else class="hist-empty">还没有<br>生成记录</div>
</div>
</div>
</div>
</template>
@@ -184,7 +357,7 @@ async function generate() {
flex-direction: column;
}
// 占满屏幕:控制面板 + 结果区填满可视高度
// 占满屏幕:控制面板 + 结果区填满可视高度 + 右侧历史列
.gen-body {
display: flex;
gap: 16px;
@@ -258,6 +431,175 @@ async function generate() {
color: #0e8b87;
font-weight: 700;
}
// 用户自填的模型
&.mine {
border-style: dashed;
border-color: #9b6cff;
color: #7c4dff;
}
&.mine.on {
background: #9b6cff;
border-style: solid;
border-color: #9b6cff;
color: #fff;
}
}
}
/* ---- 自定义提供商 ---- */
.prov {
display: flex;
flex-direction: column;
gap: 7px;
margin-top: 4px;
padding-top: 10px;
border-top: 1px dashed rgba(128, 128, 128, 0.3);
}
.prov-toggle {
display: flex;
justify-content: space-between;
align-items: center;
width: 100%;
padding: 8px 11px;
border: 1px solid rgba(128, 128, 128, 0.3);
border-radius: 8px;
background: rgba(128, 128, 128, 0.05);
font-size: 13px;
font-weight: 600;
color: inherit;
cursor: pointer;
transition: all 0.15s;
&:hover {
border-color: #0ea5a0;
color: #0e8b87;
}
}
.prov-caret {
font-size: 11px;
opacity: 0.6;
}
.prov-form {
display: flex;
flex-direction: column;
gap: 8px;
}
.prov-input {
width: 100%;
box-sizing: border-box;
padding: 9px 11px;
border: 1px solid rgba(128, 128, 128, 0.3);
border-radius: 8px;
font-size: 13px;
background: #fff;
outline: none;
font-family: inherit;
&:focus {
border-color: #0ea5a0;
}
}
.prov-actions {
display: flex;
gap: 7px;
flex-wrap: wrap;
}
.prov-btn {
padding: 7px 12px;
border: 1px solid rgba(128, 128, 128, 0.3);
border-radius: 8px;
background: #fff;
font-size: 12.5px;
font-weight: 600;
color: inherit;
cursor: pointer;
transition: all 0.15s;
&:hover:not(:disabled) {
border-color: #0ea5a0;
color: #0e8b87;
}
&:disabled {
opacity: 0.55;
cursor: not-allowed;
}
}
.prov-primary {
background: linear-gradient(48deg, #0e9384, #14b8a6);
border: none;
color: #fff;
&:hover:not(:disabled) {
opacity: 0.88;
color: #fff;
}
}
.prov-danger:hover:not(:disabled) {
border-color: #d03050;
color: #d03050;
}
.prov-hint {
font-size: 11.5px;
line-height: 1.6;
opacity: 0.6;
}
.prov-err {
font-size: 12.5px;
color: #d03050;
word-break: break-all;
}
.prov-models {
display: flex;
flex-wrap: wrap;
gap: 6px;
}
.prov-tag {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 3px 9px;
border-radius: 999px;
border: 1px dashed #9b6cff;
background: rgba(155, 108, 255, 0.08);
color: #7c4dff;
font-size: 12px;
i {
font-style: normal;
cursor: pointer;
font-size: 14px;
line-height: 1;
opacity: 0.6;
&:hover {
opacity: 1;
color: #d03050;
}
}
}
.prov-manual {
display: flex;
gap: 7px;
.prov-input {
flex: 1;
min-width: 0;
}
}
@@ -418,4 +760,95 @@ async function generate() {
margin-top: 14px;
}
}
// 右侧历史:50×50 缩略图竖排
.gen-hist {
flex: 0 0 76px;
width: 76px;
border: 1px solid rgba(128, 128, 128, 0.25);
border-radius: 12px;
background: rgba(128, 128, 128, 0.04);
display: flex;
flex-direction: column;
overflow: hidden;
.hist-t {
font-size: 11.5px;
font-weight: 700;
text-align: center;
padding: 9px 4px 7px;
border-bottom: 1px solid rgba(128, 128, 128, 0.18);
opacity: 0.75;
flex-shrink: 0;
em {
font-style: normal;
color: #0ea5a0;
margin-left: 4px;
}
}
.hist-list {
flex: 1;
overflow-y: auto;
overflow-x: hidden;
padding: 9px 13px 12px;
display: flex;
flex-direction: column;
align-items: center;
gap: 8px;
img {
width: 50px;
height: 50px;
object-fit: cover;
border-radius: 7px;
border: 2px solid transparent;
cursor: pointer;
background: #fff;
box-shadow: 0 1px 5px rgba(0, 0, 0, 0.1);
transition: all 0.15s;
display: block;
&:hover {
border-color: #0ea5a0;
transform: translateY(-1px);
}
&.on {
border-color: #0ea5a0;
box-shadow: 0 2px 8px rgba(14, 165, 160, 0.35);
}
}
}
.hist-empty {
font-size: 11px;
text-align: center;
opacity: 0.45;
padding: 16px 4px;
line-height: 1.6;
}
@media (max-width: 900px) {
flex: none;
width: 100%;
flex-direction: row;
align-items: center;
.hist-t {
border-bottom: none;
border-right: 1px solid rgba(128, 128, 128, 0.18);
padding: 8px 10px;
flex-shrink: 0;
}
.hist-list {
flex-direction: row;
overflow-x: auto;
overflow-y: hidden;
padding: 8px;
}
}
}
</style>