chore: 初始化仓库,导入项目基线代码
Academy v0.8.3 完整代码基线:Go 后端(internal/cmd/cli/mcp-server/docreader)、 Vue 前端(frontend)、数据库迁移(migrations)、部署编排(docker/helm/deploy)与文档。 今日开发的培训推送管理改动随后单独提交。
This commit is contained in:
commit
7ec21f35b0
5767 files changed
+1436788
No files matched your search
@@ -0,0 +1,39 @@
|
||||
# Academy Mini Program
|
||||
|
||||
This directory contains a WeChat Mini Program plugin for Academy. It gives mobile users a lightweight entry point to:
|
||||
|
||||
- configure a Academy API endpoint and tenant API key;
|
||||
- switch UI language between Chinese and English;
|
||||
- list available knowledge bases;
|
||||
- import a URL into a selected knowledge base;
|
||||
- ask a selected knowledge base through Academy knowledge chat.
|
||||
|
||||
## Getting started
|
||||
|
||||
1. Open `miniprogram/` in WeChat DevTools.
|
||||
2. Copy `project.private.config.json.example` to `project.private.config.json` and set your real Mini Program AppID. The shared `project.config.json` intentionally does not include an AppID to avoid forcing maintainers into a placeholder project.
|
||||
3. Open the **Settings** tab and fill in:
|
||||
- API Base URL, for example `https://academy.example.com`;
|
||||
- API Key from the Academy tenant settings page;
|
||||
- Language (`中文` / `English`, default `中文`).
|
||||
4. Open the **Knowledge** tab, refresh knowledge bases, and select the target knowledge base.
|
||||
5. Import a URL or switch to **Chat** to ask questions.
|
||||
|
||||
## Language
|
||||
|
||||
- Locale strings live in `utils/i18n.js`.
|
||||
- The selected language is stored in local settings as `locale` (`zh` | `en`).
|
||||
- Changing language in Settings updates page copy, navigation titles, and tab bar labels immediately.
|
||||
|
||||
## Local development notes
|
||||
|
||||
- WeChat DevTools may block `localhost` requests when URL validation is enabled. For local testing, either disable domain validation in DevTools or expose Academy through a HTTPS development domain.
|
||||
- In production Mini Programs, add the Academy API domain to the Mini Program request domain allowlist.
|
||||
- The chat endpoint returns Server-Sent Events. The Mini Program client parses completed SSE text responses and displays accumulated `answer` chunks.
|
||||
|
||||
## Test
|
||||
|
||||
```bash
|
||||
cd miniprogram
|
||||
npm test
|
||||
```
|
||||
@@ -0,0 +1,13 @@
|
||||
App({
|
||||
onLaunch() {
|
||||
const settings = wx.getStorageSync("academy_settings");
|
||||
if (!settings) {
|
||||
wx.setStorageSync("academy_settings", {
|
||||
baseUrl: "http://localhost:8080",
|
||||
apiKey: "",
|
||||
selectedKnowledgeBaseId: "",
|
||||
locale: "zh"
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,39 @@
|
||||
{
|
||||
"pages": [
|
||||
"pages/index/index",
|
||||
"pages/chat/chat",
|
||||
"pages/settings/settings"
|
||||
],
|
||||
"window": {
|
||||
"navigationBarTitleText": "Academy",
|
||||
"navigationBarBackgroundColor": "#0d3b2a",
|
||||
"navigationBarTextStyle": "white",
|
||||
"backgroundColor": "#f3f3f3"
|
||||
},
|
||||
"tabBar": {
|
||||
"color": "#8b8b8b",
|
||||
"selectedColor": "#07c05f",
|
||||
"backgroundColor": "#ffffff",
|
||||
"borderStyle": "white",
|
||||
"list": [
|
||||
{
|
||||
"pagePath": "pages/index/index",
|
||||
"text": "知识库",
|
||||
"iconPath": "assets/tab/knowledge.png",
|
||||
"selectedIconPath": "assets/tab/knowledge-active.png"
|
||||
},
|
||||
{
|
||||
"pagePath": "pages/chat/chat",
|
||||
"text": "问答",
|
||||
"iconPath": "assets/tab/chat.png",
|
||||
"selectedIconPath": "assets/tab/chat-active.png"
|
||||
},
|
||||
{
|
||||
"pagePath": "pages/settings/settings",
|
||||
"text": "设置",
|
||||
"iconPath": "assets/tab/settings.png",
|
||||
"selectedIconPath": "assets/tab/settings-active.png"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
page {
|
||||
min-height: 100%;
|
||||
background: #f3f3f3;
|
||||
color: #1a1a1a;
|
||||
font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", sans-serif;
|
||||
}
|
||||
|
||||
.page {
|
||||
min-height: 100vh;
|
||||
box-sizing: border-box;
|
||||
background: linear-gradient(180deg, #f8faf9 0%, #f3f3f3 100%);
|
||||
padding: 32rpx;
|
||||
}
|
||||
|
||||
.card {
|
||||
padding: 28rpx;
|
||||
border-radius: 24rpx;
|
||||
background: #ffffff;
|
||||
border: 1rpx solid #e7ebf0;
|
||||
box-shadow: 0 12rpx 36rpx rgba(7, 192, 95, 0.08);
|
||||
}
|
||||
|
||||
.title {
|
||||
font-size: 40rpx;
|
||||
font-weight: 700;
|
||||
margin-bottom: 12rpx;
|
||||
}
|
||||
|
||||
.muted {
|
||||
color: rgba(0, 0, 0, 0.6);
|
||||
font-size: 26rpx;
|
||||
}
|
||||
|
||||
.field {
|
||||
margin-top: 24rpx;
|
||||
}
|
||||
|
||||
.label {
|
||||
display: block;
|
||||
margin-bottom: 12rpx;
|
||||
color: rgba(0, 0, 0, 0.9);
|
||||
font-size: 26rpx;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
input,
|
||||
textarea,
|
||||
picker {
|
||||
box-sizing: border-box;
|
||||
width: 100%;
|
||||
min-height: 88rpx;
|
||||
padding: 20rpx 24rpx;
|
||||
border: 1rpx solid #dcdcdc;
|
||||
border-radius: 18rpx;
|
||||
background: #ffffff;
|
||||
color: #1a1a1a;
|
||||
font-size: 28rpx;
|
||||
}
|
||||
|
||||
textarea {
|
||||
min-height: 180rpx;
|
||||
}
|
||||
|
||||
button {
|
||||
margin-top: 24rpx;
|
||||
border-radius: 18rpx;
|
||||
background: #07c05f;
|
||||
color: #ffffff;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
button[disabled] {
|
||||
background: #c5c5c5;
|
||||
}
|
||||
|
||||
.picker-value {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
color: #1a1a1a;
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 450 B |
Binary file not shown.
|
After Width: | Height: | Size: 442 B |
Binary file not shown.
|
After Width: | Height: | Size: 315 B |
Binary file not shown.
|
After Width: | Height: | Size: 308 B |
Binary file not shown.
|
After Width: | Height: | Size: 600 B |
Binary file not shown.
|
After Width: | Height: | Size: 591 B |
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"name": "academy-miniprogram",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"description": "WeChat Mini Program plugin for Academy",
|
||||
"scripts": {
|
||||
"test": "node --test ../tests/miniprogram/*.test.js"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
const { getSettings } = require("../../utils/config");
|
||||
const { createSession, knowledgeChat } = require("../../utils/request");
|
||||
const { collectAnswerFromSSE } = require("../../utils/sse");
|
||||
const { applyNavTitle, applyTabBar, getMessages, t } = require("../../utils/i18n");
|
||||
|
||||
function buildViewModel() {
|
||||
const messages = getMessages();
|
||||
return {
|
||||
chatTitle: messages.chatTitle,
|
||||
chatSubtitle: messages.chatSubtitle,
|
||||
questionLabel: messages.question,
|
||||
questionPlaceholder: messages.questionPlaceholder,
|
||||
askText: messages.askAcademy,
|
||||
answerLabel: messages.answer
|
||||
};
|
||||
}
|
||||
|
||||
Page({
|
||||
data: {
|
||||
chatTitle: "知识问答",
|
||||
chatSubtitle: "向当前选中的 Academy 知识库提问。",
|
||||
questionLabel: "问题",
|
||||
questionPlaceholder: "输入你的问题…",
|
||||
askText: "向 Academy 提问",
|
||||
answerLabel: "回答",
|
||||
answer: "",
|
||||
displayAnswer: "",
|
||||
loading: false,
|
||||
query: "",
|
||||
rawResponse: "",
|
||||
sessionId: ""
|
||||
},
|
||||
|
||||
applyI18n() {
|
||||
this.setData(buildViewModel());
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
this.applyI18n();
|
||||
},
|
||||
|
||||
onShow() {
|
||||
try {
|
||||
applyTabBar();
|
||||
applyNavTitle("navChat");
|
||||
this.applyI18n();
|
||||
} catch (error) {
|
||||
// keep fallback Chinese copy
|
||||
}
|
||||
},
|
||||
|
||||
onQueryInput(event) {
|
||||
this.setData({ query: event.detail.value });
|
||||
},
|
||||
|
||||
async ensureSession() {
|
||||
if (this.data.sessionId) {
|
||||
return this.data.sessionId;
|
||||
}
|
||||
|
||||
const settings = getSettings();
|
||||
const response = await createSession(settings.selectedKnowledgeBaseId);
|
||||
const sessionId = response && response.data ? response.data.id : "";
|
||||
if (!sessionId) {
|
||||
throw new Error(t("sessionIdMissing"));
|
||||
}
|
||||
this.setData({ sessionId: sessionId });
|
||||
return sessionId;
|
||||
},
|
||||
|
||||
async ask() {
|
||||
this.setData({ answer: "", rawResponse: "", displayAnswer: "", loading: true });
|
||||
try {
|
||||
const sessionId = await this.ensureSession();
|
||||
const settings = getSettings();
|
||||
const response = await knowledgeChat(
|
||||
sessionId,
|
||||
this.data.query.trim(),
|
||||
settings.selectedKnowledgeBaseId
|
||||
);
|
||||
const rawResponse = typeof response === "string" ? response : JSON.stringify(response);
|
||||
const answer = collectAnswerFromSSE(rawResponse);
|
||||
const displayAnswer = answer || rawResponse;
|
||||
this.setData({
|
||||
answer: answer,
|
||||
rawResponse: answer ? "" : rawResponse,
|
||||
displayAnswer: displayAnswer
|
||||
});
|
||||
} catch (error) {
|
||||
wx.showModal({
|
||||
title: t("chatFailed"),
|
||||
content: (error && error.message) || "",
|
||||
showCancel: false
|
||||
});
|
||||
} finally {
|
||||
this.setData({ loading: false });
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"navigationBarTitleText": "问答"
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<view class="page">
|
||||
<view class="card">
|
||||
<view class="title">{{chatTitle}}</view>
|
||||
<view class="muted">{{chatSubtitle}}</view>
|
||||
|
||||
<view class="field">
|
||||
<text class="label">{{questionLabel}}</text>
|
||||
<textarea value="{{query}}" placeholder="{{questionPlaceholder}}" bindinput="onQueryInput" />
|
||||
</view>
|
||||
|
||||
<button bindtap="ask" loading="{{loading}}" disabled="{{!query}}">{{askText}}</button>
|
||||
</view>
|
||||
|
||||
<view class="card answer-card" wx:if="{{displayAnswer}}">
|
||||
<view class="label">{{answerLabel}}</view>
|
||||
<view class="answer">{{displayAnswer}}</view>
|
||||
</view>
|
||||
</view>
|
||||
@@ -0,0 +1,10 @@
|
||||
.answer-card {
|
||||
margin-top: 28rpx;
|
||||
}
|
||||
|
||||
.answer {
|
||||
white-space: pre-wrap;
|
||||
line-height: 1.7;
|
||||
color: #1a1a1a;
|
||||
font-size: 28rpx;
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
const { getSettings, saveSettings } = require("../../utils/config");
|
||||
const { createKnowledgeFromURL, listKnowledgeBases } = require("../../utils/request");
|
||||
const { applyNavTitle, applyTabBar, getMessages, t } = require("../../utils/i18n");
|
||||
|
||||
function normalizeKnowledgeBases(response) {
|
||||
if (!response) {
|
||||
return [];
|
||||
}
|
||||
if (Array.isArray(response.data)) {
|
||||
return response.data;
|
||||
}
|
||||
if (response.data && Array.isArray(response.data.list)) {
|
||||
return response.data.list;
|
||||
}
|
||||
if (Array.isArray(response.knowledge_bases)) {
|
||||
return response.knowledge_bases;
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
function buildViewModel() {
|
||||
const messages = getMessages();
|
||||
return {
|
||||
texts: messages,
|
||||
knowledgeTitle: messages.knowledgeTitle,
|
||||
knowledgeSubtitle: messages.knowledgeSubtitle,
|
||||
needsSettingsText: messages.needsSettings,
|
||||
openSettingsText: messages.openSettings,
|
||||
knowledgeBaseLabel: messages.knowledgeBase,
|
||||
tapToSelectText: messages.tapToSelect,
|
||||
refreshText: messages.refreshKnowledgeBases,
|
||||
urlLabel: messages.urlLabel,
|
||||
urlPlaceholder: messages.urlPlaceholder,
|
||||
importUrlText: messages.importUrl
|
||||
};
|
||||
}
|
||||
|
||||
Page({
|
||||
data: {
|
||||
texts: {},
|
||||
knowledgeTitle: "Academy 知识库",
|
||||
knowledgeSubtitle: "在微信中选择知识库,导入网页或提问。",
|
||||
needsSettingsText: "请先配置 Academy API 地址和 API Key,再加载知识库。",
|
||||
openSettingsText: "打开设置",
|
||||
knowledgeBaseLabel: "知识库",
|
||||
tapToSelectText: "点击选择",
|
||||
refreshText: "刷新知识库列表",
|
||||
urlLabel: "网页链接",
|
||||
urlPlaceholder: "https://github.com/Tencent/WeKnora",
|
||||
importUrlText: "导入链接",
|
||||
displayKnowledgeBaseName: "点击选择",
|
||||
importing: false,
|
||||
knowledgeBases: [],
|
||||
knowledgeBaseNames: [],
|
||||
loading: false,
|
||||
needsSettings: false,
|
||||
selectedIndex: 0,
|
||||
selectedKnowledgeBaseId: "",
|
||||
selectedKnowledgeBaseName: "",
|
||||
statusMessage: "",
|
||||
url: ""
|
||||
},
|
||||
|
||||
applyI18n() {
|
||||
const vm = buildViewModel();
|
||||
const displayKnowledgeBaseName =
|
||||
this.data.selectedKnowledgeBaseName || vm.tapToSelectText;
|
||||
this.setData({
|
||||
texts: vm.texts,
|
||||
knowledgeTitle: vm.knowledgeTitle,
|
||||
knowledgeSubtitle: vm.knowledgeSubtitle,
|
||||
needsSettingsText: vm.needsSettingsText,
|
||||
openSettingsText: vm.openSettingsText,
|
||||
knowledgeBaseLabel: vm.knowledgeBaseLabel,
|
||||
tapToSelectText: vm.tapToSelectText,
|
||||
refreshText: vm.refreshText,
|
||||
urlLabel: vm.urlLabel,
|
||||
urlPlaceholder: vm.urlPlaceholder,
|
||||
importUrlText: vm.importUrlText,
|
||||
displayKnowledgeBaseName: displayKnowledgeBaseName
|
||||
});
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
this.applyI18n();
|
||||
},
|
||||
|
||||
onShow() {
|
||||
try {
|
||||
applyTabBar();
|
||||
applyNavTitle("navKnowledge");
|
||||
this.applyI18n();
|
||||
|
||||
const settings = getSettings();
|
||||
const needsSettings = !settings.baseUrl || !settings.apiKey;
|
||||
const patch = { needsSettings };
|
||||
if (settings.selectedKnowledgeBaseId) {
|
||||
patch.selectedKnowledgeBaseId = settings.selectedKnowledgeBaseId;
|
||||
}
|
||||
this.setData(patch);
|
||||
|
||||
if (needsSettings) {
|
||||
return;
|
||||
}
|
||||
this.loadKnowledgeBases();
|
||||
} catch (error) {
|
||||
this.setData({
|
||||
needsSettings: true,
|
||||
statusMessage: (error && error.message) || "page init failed"
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
onUrlInput(event) {
|
||||
this.setData({ url: event.detail.value });
|
||||
},
|
||||
|
||||
onKnowledgeBaseChange(event) {
|
||||
const selectedIndex = Number(event.detail.value);
|
||||
this.selectKnowledgeBase(selectedIndex);
|
||||
},
|
||||
|
||||
onKnowledgeBaseTap(event) {
|
||||
this.selectKnowledgeBase(Number(event.currentTarget.dataset.index));
|
||||
},
|
||||
|
||||
selectKnowledgeBase(selectedIndex) {
|
||||
const selected = this.data.knowledgeBases[selectedIndex];
|
||||
if (!selected) return;
|
||||
|
||||
saveSettings({ selectedKnowledgeBaseId: selected.id });
|
||||
this.setData({
|
||||
selectedIndex,
|
||||
selectedKnowledgeBaseId: selected.id,
|
||||
selectedKnowledgeBaseName: selected.name,
|
||||
displayKnowledgeBaseName: selected.name || this.data.tapToSelectText
|
||||
});
|
||||
},
|
||||
|
||||
openSettings() {
|
||||
wx.switchTab({ url: "/pages/settings/settings" });
|
||||
},
|
||||
|
||||
async loadKnowledgeBases() {
|
||||
const settings = getSettings();
|
||||
if (!settings.baseUrl || !settings.apiKey) {
|
||||
this.setData({ needsSettings: true });
|
||||
return;
|
||||
}
|
||||
|
||||
this.setData({ loading: true, statusMessage: "" });
|
||||
try {
|
||||
const response = await listKnowledgeBases();
|
||||
const knowledgeBases = normalizeKnowledgeBases(response);
|
||||
const knowledgeBaseNames = knowledgeBases.map((item) => item.name || item.id);
|
||||
const current = getSettings();
|
||||
let selectedIndex = knowledgeBases.findIndex(
|
||||
(item) => item.id === current.selectedKnowledgeBaseId
|
||||
);
|
||||
if (selectedIndex < 0) {
|
||||
selectedIndex = 0;
|
||||
}
|
||||
const selected = knowledgeBases[selectedIndex];
|
||||
const selectedName = (selected && selected.name) || "";
|
||||
this.setData({
|
||||
knowledgeBases,
|
||||
knowledgeBaseNames,
|
||||
selectedIndex,
|
||||
selectedKnowledgeBaseId: (selected && selected.id) || "",
|
||||
selectedKnowledgeBaseName: selectedName,
|
||||
displayKnowledgeBaseName: selectedName || this.data.tapToSelectText,
|
||||
statusMessage: knowledgeBases.length
|
||||
? t("loadedKnowledgeBases", { count: knowledgeBases.length })
|
||||
: t("noKnowledgeBases")
|
||||
});
|
||||
if (selected && selected.id) {
|
||||
saveSettings({ selectedKnowledgeBaseId: selected.id });
|
||||
}
|
||||
} catch (error) {
|
||||
wx.showModal({
|
||||
title: t("loadFailed"),
|
||||
content: (error && error.message) || "",
|
||||
showCancel: false
|
||||
});
|
||||
} finally {
|
||||
this.setData({ loading: false });
|
||||
}
|
||||
},
|
||||
|
||||
async importURL() {
|
||||
this.setData({ importing: true });
|
||||
try {
|
||||
await createKnowledgeFromURL(this.data.selectedKnowledgeBaseId, this.data.url.trim(), false);
|
||||
this.setData({ url: "" });
|
||||
wx.showToast({ title: t("imported"), icon: "success" });
|
||||
} catch (error) {
|
||||
wx.showModal({
|
||||
title: t("importFailed"),
|
||||
content: (error && error.message) || "",
|
||||
showCancel: false
|
||||
});
|
||||
} finally {
|
||||
this.setData({ importing: false });
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"navigationBarTitleText": "知识库"
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<view class="page">
|
||||
<view class="card hero">
|
||||
<view class="title">{{knowledgeTitle}}</view>
|
||||
<view class="muted">{{knowledgeSubtitle}}</view>
|
||||
</view>
|
||||
|
||||
<view class="card section">
|
||||
<view wx:if="{{needsSettings}}" class="notice">
|
||||
<view>{{needsSettingsText}}</view>
|
||||
<button bindtap="openSettings" class="secondary">{{openSettingsText}}</button>
|
||||
</view>
|
||||
|
||||
<view class="field">
|
||||
<text class="label">{{knowledgeBaseLabel}}</text>
|
||||
<picker range="{{knowledgeBaseNames}}" value="{{selectedIndex}}" bindchange="onKnowledgeBaseChange">
|
||||
<view class="picker-value">{{displayKnowledgeBaseName}}</view>
|
||||
</picker>
|
||||
<view wx:if="{{statusMessage}}" class="status-message">{{statusMessage}}</view>
|
||||
<view wx:if="{{knowledgeBaseNames.length}}" class="knowledge-list">
|
||||
<view
|
||||
wx:for="{{knowledgeBaseNames}}"
|
||||
wx:key="*this"
|
||||
data-index="{{index}}"
|
||||
bindtap="onKnowledgeBaseTap"
|
||||
class="knowledge-item {{index == selectedIndex ? 'active' : ''}}"
|
||||
>
|
||||
{{item}}
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<button bindtap="loadKnowledgeBases" loading="{{loading}}">{{refreshText}}</button>
|
||||
</view>
|
||||
|
||||
<view class="card section">
|
||||
<view class="field">
|
||||
<text class="label">{{urlLabel}}</text>
|
||||
<input value="{{url}}" placeholder="{{urlPlaceholder}}" bindinput="onUrlInput" />
|
||||
</view>
|
||||
|
||||
<button bindtap="importURL" loading="{{importing}}" disabled="{{!selectedKnowledgeBaseId || !url}}">{{importUrlText}}</button>
|
||||
</view>
|
||||
</view>
|
||||
@@ -0,0 +1,62 @@
|
||||
.hero {
|
||||
background: linear-gradient(135deg, #0d3b2a 0%, #05a04f 54%, #07c05f 100%);
|
||||
color: #ffffff;
|
||||
}
|
||||
|
||||
.hero .muted {
|
||||
color: rgba(255, 255, 255, 0.78);
|
||||
}
|
||||
|
||||
.section {
|
||||
margin-top: 28rpx;
|
||||
}
|
||||
|
||||
.notice {
|
||||
box-sizing: border-box;
|
||||
padding: 24rpx;
|
||||
border: 1rpx solid #d0f2de;
|
||||
border-radius: 18rpx;
|
||||
background: #e9f8ec;
|
||||
color: #1f5a3f;
|
||||
font-size: 26rpx;
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.picker-value {
|
||||
color: #1a1a1a;
|
||||
}
|
||||
|
||||
.status-message {
|
||||
margin-top: 12rpx;
|
||||
color: #6b7280;
|
||||
font-size: 24rpx;
|
||||
}
|
||||
|
||||
.knowledge-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 12rpx;
|
||||
margin-top: 16rpx;
|
||||
}
|
||||
|
||||
.knowledge-item {
|
||||
padding: 18rpx 20rpx;
|
||||
border: 1rpx solid #dcdcdc;
|
||||
border-radius: 14rpx;
|
||||
background: #ffffff;
|
||||
color: #1a1a1a;
|
||||
font-size: 26rpx;
|
||||
}
|
||||
|
||||
.knowledge-item.active {
|
||||
border-color: #07c05f;
|
||||
background: #e9f8ec;
|
||||
color: #05a04f;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.secondary {
|
||||
background: #ffffff;
|
||||
color: #07c05f;
|
||||
border: 1rpx solid #b8ebcc;
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
const { getSettings, saveSettings } = require("../../utils/config");
|
||||
const { applyNavTitle, applyTabBar, getLocale, getMessages, setLocale, t } = require("../../utils/i18n");
|
||||
|
||||
const LANGUAGE_VALUES = ["zh", "en"];
|
||||
|
||||
function buildViewModel(locale, languageIndex) {
|
||||
const messages = getMessages(locale);
|
||||
const languageLabels = [messages.languageZh, messages.languageEn];
|
||||
return {
|
||||
settingsTitle: messages.settingsTitle,
|
||||
settingsSubtitle: messages.settingsSubtitle,
|
||||
apiBaseUrlLabel: messages.apiBaseUrl,
|
||||
apiBaseUrlPlaceholder: messages.apiBaseUrlPlaceholder,
|
||||
apiKeyLabel: messages.apiKey,
|
||||
apiKeyPlaceholder: messages.apiKeyPlaceholder,
|
||||
languageLabel: messages.language,
|
||||
saveSettingsText: messages.saveSettings,
|
||||
languageLabels,
|
||||
displayLanguageLabel: languageLabels[languageIndex] || languageLabels[0]
|
||||
};
|
||||
}
|
||||
|
||||
Page({
|
||||
data: {
|
||||
settingsTitle: "连接 Academy",
|
||||
settingsSubtitle: "填写 Academy API 地址与租户 API Key。",
|
||||
apiBaseUrlLabel: "API 地址",
|
||||
apiBaseUrlPlaceholder: "https://your-academy.example.com",
|
||||
apiKeyLabel: "API Key",
|
||||
apiKeyPlaceholder: "sk-...",
|
||||
languageLabel: "界面语言",
|
||||
saveSettingsText: "保存设置",
|
||||
displayLanguageLabel: "中文",
|
||||
baseUrl: "",
|
||||
apiKey: "",
|
||||
languageIndex: 0,
|
||||
languageLabels: ["中文", "English"]
|
||||
},
|
||||
|
||||
refreshI18n() {
|
||||
const locale = getLocale();
|
||||
let languageIndex = LANGUAGE_VALUES.indexOf(locale);
|
||||
if (languageIndex < 0) {
|
||||
languageIndex = 0;
|
||||
}
|
||||
const vm = buildViewModel(locale, languageIndex);
|
||||
this.setData({
|
||||
settingsTitle: vm.settingsTitle,
|
||||
settingsSubtitle: vm.settingsSubtitle,
|
||||
apiBaseUrlLabel: vm.apiBaseUrlLabel,
|
||||
apiBaseUrlPlaceholder: vm.apiBaseUrlPlaceholder,
|
||||
apiKeyLabel: vm.apiKeyLabel,
|
||||
apiKeyPlaceholder: vm.apiKeyPlaceholder,
|
||||
languageLabel: vm.languageLabel,
|
||||
saveSettingsText: vm.saveSettingsText,
|
||||
languageLabels: vm.languageLabels,
|
||||
displayLanguageLabel: vm.displayLanguageLabel,
|
||||
languageIndex: languageIndex
|
||||
});
|
||||
applyTabBar(locale);
|
||||
applyNavTitle("navSettings", locale);
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
this.refreshI18n();
|
||||
},
|
||||
|
||||
onShow() {
|
||||
try {
|
||||
const settings = getSettings();
|
||||
this.setData({
|
||||
baseUrl: settings.baseUrl,
|
||||
apiKey: settings.apiKey
|
||||
});
|
||||
this.refreshI18n();
|
||||
} catch (error) {
|
||||
// keep fallback Chinese copy
|
||||
}
|
||||
},
|
||||
|
||||
onBaseUrlInput(event) {
|
||||
this.setData({ baseUrl: event.detail.value });
|
||||
},
|
||||
|
||||
onApiKeyInput(event) {
|
||||
this.setData({ apiKey: event.detail.value });
|
||||
},
|
||||
|
||||
onLanguageChange(event) {
|
||||
const languageIndex = Number(event.detail.value);
|
||||
const locale = LANGUAGE_VALUES[languageIndex] || "zh";
|
||||
setLocale(locale);
|
||||
this.setData({ languageIndex });
|
||||
this.refreshI18n();
|
||||
},
|
||||
|
||||
save() {
|
||||
saveSettings({
|
||||
baseUrl: this.data.baseUrl,
|
||||
apiKey: this.data.apiKey,
|
||||
locale: LANGUAGE_VALUES[this.data.languageIndex] || getLocale()
|
||||
});
|
||||
this.refreshI18n();
|
||||
wx.showToast({ title: t("saved"), icon: "success" });
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"navigationBarTitleText": "设置"
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<view class="page">
|
||||
<view class="card">
|
||||
<view class="title">{{settingsTitle}}</view>
|
||||
<view class="muted">{{settingsSubtitle}}</view>
|
||||
|
||||
<view class="field">
|
||||
<text class="label">{{apiBaseUrlLabel}}</text>
|
||||
<input value="{{baseUrl}}" placeholder="{{apiBaseUrlPlaceholder}}" bindinput="onBaseUrlInput" />
|
||||
</view>
|
||||
|
||||
<view class="field">
|
||||
<text class="label">{{apiKeyLabel}}</text>
|
||||
<input value="{{apiKey}}" password placeholder="{{apiKeyPlaceholder}}" bindinput="onApiKeyInput" />
|
||||
</view>
|
||||
|
||||
<view class="field">
|
||||
<text class="label">{{languageLabel}}</text>
|
||||
<picker range="{{languageLabels}}" value="{{languageIndex}}" bindchange="onLanguageChange">
|
||||
<view class="picker-value">{{displayLanguageLabel}}</view>
|
||||
</picker>
|
||||
</view>
|
||||
|
||||
<button bindtap="save">{{saveSettingsText}}</button>
|
||||
</view>
|
||||
</view>
|
||||
@@ -0,0 +1,3 @@
|
||||
.card {
|
||||
margin-top: 40rpx;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
{
|
||||
"description": "Academy Mini Program plugin",
|
||||
"setting": {
|
||||
"urlCheck": true,
|
||||
"es6": true,
|
||||
"enhance": true,
|
||||
"postcss": true,
|
||||
"minified": true,
|
||||
"compileWorklet": false,
|
||||
"uglifyFileName": false,
|
||||
"uploadWithSourceMap": true,
|
||||
"packNpmManually": false,
|
||||
"packNpmRelationList": [],
|
||||
"minifyWXSS": true,
|
||||
"minifyWXML": true,
|
||||
"localPlugins": false,
|
||||
"disableUseStrict": false,
|
||||
"useCompilerPlugins": false,
|
||||
"condition": false,
|
||||
"swc": false,
|
||||
"disableSWC": true,
|
||||
"babelSetting": {
|
||||
"ignore": [],
|
||||
"disablePlugins": [],
|
||||
"outputPath": ""
|
||||
}
|
||||
},
|
||||
"compileType": "miniprogram",
|
||||
"libVersion": "latest",
|
||||
"projectname": "Academy Mini Program",
|
||||
"condition": {},
|
||||
"appid": "wxbaf7dc10724effab",
|
||||
"simulatorPluginLibVersion": {},
|
||||
"packOptions": {
|
||||
"ignore": [],
|
||||
"include": []
|
||||
},
|
||||
"editorSetting": {}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"appid": "your-wechat-mini-program-appid"
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"rules": [
|
||||
{
|
||||
"action": "allow",
|
||||
"page": "*"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
const STORAGE_KEY = "academy_settings";
|
||||
|
||||
function normalizeBaseUrl(baseUrl) {
|
||||
if (!baseUrl || typeof baseUrl !== "string") {
|
||||
return "";
|
||||
}
|
||||
|
||||
return baseUrl.trim().replace(/\/+$/, "");
|
||||
}
|
||||
|
||||
function normalizeLocale(locale) {
|
||||
return locale === "en" ? "en" : "zh";
|
||||
}
|
||||
|
||||
function getSettings() {
|
||||
const stored = wx.getStorageSync(STORAGE_KEY) || {};
|
||||
return {
|
||||
baseUrl: normalizeBaseUrl(stored.baseUrl || ""),
|
||||
apiKey: stored.apiKey || "",
|
||||
selectedKnowledgeBaseId: stored.selectedKnowledgeBaseId || "",
|
||||
locale: normalizeLocale(stored.locale || "zh")
|
||||
};
|
||||
}
|
||||
|
||||
function saveSettings(settings) {
|
||||
const current = getSettings();
|
||||
const nextBaseUrl =
|
||||
settings.baseUrl !== undefined && settings.baseUrl !== null
|
||||
? settings.baseUrl
|
||||
: current.baseUrl;
|
||||
const nextLocale =
|
||||
settings.locale !== undefined && settings.locale !== null
|
||||
? settings.locale
|
||||
: current.locale;
|
||||
const next = {
|
||||
baseUrl: normalizeBaseUrl(nextBaseUrl),
|
||||
apiKey: settings.apiKey !== undefined ? settings.apiKey : current.apiKey,
|
||||
selectedKnowledgeBaseId:
|
||||
settings.selectedKnowledgeBaseId !== undefined
|
||||
? settings.selectedKnowledgeBaseId
|
||||
: current.selectedKnowledgeBaseId,
|
||||
locale: normalizeLocale(nextLocale)
|
||||
};
|
||||
wx.setStorageSync(STORAGE_KEY, next);
|
||||
return next;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
STORAGE_KEY,
|
||||
getSettings,
|
||||
normalizeBaseUrl,
|
||||
normalizeLocale,
|
||||
saveSettings
|
||||
};
|
||||
@@ -0,0 +1,169 @@
|
||||
const { getSettings, saveSettings } = require("./config");
|
||||
|
||||
const MESSAGES = {
|
||||
zh: {
|
||||
tabKnowledge: "知识库",
|
||||
tabChat: "问答",
|
||||
tabSettings: "设置",
|
||||
navKnowledge: "知识库",
|
||||
navChat: "问答",
|
||||
navSettings: "设置",
|
||||
knowledgeTitle: "Academy 知识库",
|
||||
knowledgeSubtitle: "在微信中选择知识库,导入网页或提问。",
|
||||
needsSettings: "请先配置 Academy API 地址和 API Key,再加载知识库。",
|
||||
openSettings: "打开设置",
|
||||
knowledgeBase: "知识库",
|
||||
tapToSelect: "点击选择",
|
||||
refreshKnowledgeBases: "刷新知识库列表",
|
||||
urlLabel: "网页链接",
|
||||
urlPlaceholder: "https://github.com/Tencent/WeKnora",
|
||||
importUrl: "导入链接",
|
||||
loadedKnowledgeBases: "已加载 {count} 个知识库。",
|
||||
noKnowledgeBases: "未找到知识库。",
|
||||
loadFailed: "加载失败",
|
||||
imported: "导入成功",
|
||||
importFailed: "导入失败",
|
||||
chatTitle: "知识问答",
|
||||
chatSubtitle: "向当前选中的 Academy 知识库提问。",
|
||||
question: "问题",
|
||||
questionPlaceholder: "输入你的问题…",
|
||||
askAcademy: "向 Academy 提问",
|
||||
answer: "回答",
|
||||
chatFailed: "问答失败",
|
||||
sessionIdMissing: "会话接口未返回 session id。",
|
||||
settingsTitle: "连接 Academy",
|
||||
settingsSubtitle: "填写 Academy API 地址与租户 API Key。",
|
||||
apiBaseUrl: "API 地址",
|
||||
apiBaseUrlPlaceholder: "https://your-academy.example.com",
|
||||
apiKey: "API Key",
|
||||
apiKeyPlaceholder: "sk-...",
|
||||
language: "界面语言",
|
||||
languageZh: "中文",
|
||||
languageEn: "English",
|
||||
saveSettings: "保存设置",
|
||||
saved: "已保存",
|
||||
missingBaseUrl: "请先配置 Academy API 地址。",
|
||||
missingApiKey: "请先配置 Academy API Key。"
|
||||
},
|
||||
en: {
|
||||
tabKnowledge: "Knowledge",
|
||||
tabChat: "Chat",
|
||||
tabSettings: "Settings",
|
||||
navKnowledge: "Knowledge",
|
||||
navChat: "Chat",
|
||||
navSettings: "Settings",
|
||||
knowledgeTitle: "Academy Knowledge",
|
||||
knowledgeSubtitle: "Import a web page into a selected knowledge base from WeChat.",
|
||||
needsSettings: "Configure the Academy API base URL and API key before loading knowledge bases.",
|
||||
openSettings: "Open Settings",
|
||||
knowledgeBase: "Knowledge Base",
|
||||
tapToSelect: "Tap to select",
|
||||
refreshKnowledgeBases: "Refresh knowledge bases",
|
||||
urlLabel: "URL",
|
||||
urlPlaceholder: "https://github.com/Tencent/WeKnora",
|
||||
importUrl: "Import URL",
|
||||
loadedKnowledgeBases: "Loaded {count} knowledge bases.",
|
||||
noKnowledgeBases: "No knowledge bases found.",
|
||||
loadFailed: "Load failed",
|
||||
imported: "Imported",
|
||||
importFailed: "Import failed",
|
||||
chatTitle: "Knowledge Chat",
|
||||
chatSubtitle: "Ask the selected Academy knowledge base.",
|
||||
question: "Question",
|
||||
questionPlaceholder: "Ask something...",
|
||||
askAcademy: "Ask Academy",
|
||||
answer: "Answer",
|
||||
chatFailed: "Chat failed",
|
||||
sessionIdMissing: "The session API did not return a session id.",
|
||||
settingsTitle: "Connect Academy",
|
||||
settingsSubtitle: "Use your Academy API endpoint and tenant API key.",
|
||||
apiBaseUrl: "API Base URL",
|
||||
apiBaseUrlPlaceholder: "https://your-academy.example.com",
|
||||
apiKey: "API Key",
|
||||
apiKeyPlaceholder: "sk-...",
|
||||
language: "Language",
|
||||
languageZh: "中文",
|
||||
languageEn: "English",
|
||||
saveSettings: "Save settings",
|
||||
saved: "Saved",
|
||||
missingBaseUrl: "Please configure the Academy API base URL first.",
|
||||
missingApiKey: "Please configure the Academy API key first."
|
||||
}
|
||||
};
|
||||
|
||||
function normalizeLocale(locale) {
|
||||
return locale === "en" ? "en" : "zh";
|
||||
}
|
||||
|
||||
function getLocale() {
|
||||
return normalizeLocale(getSettings().locale);
|
||||
}
|
||||
|
||||
function setLocale(locale) {
|
||||
const next = normalizeLocale(locale);
|
||||
saveSettings({ locale: next });
|
||||
return next;
|
||||
}
|
||||
|
||||
function t(key, vars = {}, locale = getLocale()) {
|
||||
const table = MESSAGES[normalizeLocale(locale)] || MESSAGES.zh;
|
||||
let text = table[key] || MESSAGES.en[key] || key;
|
||||
Object.keys(vars).forEach((name) => {
|
||||
text = text.replace(new RegExp(`\\{${name}\\}`, "g"), String(vars[name]));
|
||||
});
|
||||
return text;
|
||||
}
|
||||
|
||||
function getMessages(locale = getLocale()) {
|
||||
return MESSAGES[normalizeLocale(locale)] || MESSAGES.zh;
|
||||
}
|
||||
|
||||
function applyTabBar(locale = getLocale()) {
|
||||
const lang = normalizeLocale(locale);
|
||||
const items = [
|
||||
{
|
||||
index: 0,
|
||||
text: t("tabKnowledge", {}, lang),
|
||||
iconPath: "assets/tab/knowledge.png",
|
||||
selectedIconPath: "assets/tab/knowledge-active.png"
|
||||
},
|
||||
{
|
||||
index: 1,
|
||||
text: t("tabChat", {}, lang),
|
||||
iconPath: "assets/tab/chat.png",
|
||||
selectedIconPath: "assets/tab/chat-active.png"
|
||||
},
|
||||
{
|
||||
index: 2,
|
||||
text: t("tabSettings", {}, lang),
|
||||
iconPath: "assets/tab/settings.png",
|
||||
selectedIconPath: "assets/tab/settings-active.png"
|
||||
}
|
||||
];
|
||||
items.forEach((item) => {
|
||||
try {
|
||||
wx.setTabBarItem(item);
|
||||
} catch (error) {
|
||||
// ignore when called outside tab page context
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function applyNavTitle(key, locale = getLocale()) {
|
||||
try {
|
||||
wx.setNavigationBarTitle({ title: t(key, {}, locale) });
|
||||
} catch (error) {
|
||||
// ignore in non-page contexts
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
MESSAGES,
|
||||
applyNavTitle,
|
||||
applyTabBar,
|
||||
getLocale,
|
||||
getMessages,
|
||||
normalizeLocale,
|
||||
setLocale,
|
||||
t
|
||||
};
|
||||
@@ -0,0 +1,79 @@
|
||||
const { getSettings } = require("./config");
|
||||
const { t } = require("./i18n");
|
||||
|
||||
function request(path, options = {}) {
|
||||
const settings = getSettings();
|
||||
if (!settings.baseUrl) {
|
||||
return Promise.reject(new Error(t("missingBaseUrl")));
|
||||
}
|
||||
if (!settings.apiKey) {
|
||||
return Promise.reject(new Error(t("missingApiKey")));
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
wx.request({
|
||||
url: `${settings.baseUrl}${path}`,
|
||||
method: options.method || "GET",
|
||||
data: options.data,
|
||||
header: {
|
||||
"Content-Type": "application/json",
|
||||
"X-API-Key": settings.apiKey,
|
||||
"X-Request-ID": `mp-${Date.now()}-${Math.random().toString(16).slice(2)}`
|
||||
},
|
||||
success(response) {
|
||||
if (response.statusCode >= 200 && response.statusCode < 300) {
|
||||
resolve(response.data);
|
||||
return;
|
||||
}
|
||||
const data = response.data || {};
|
||||
const errorObj = data.error || {};
|
||||
const message = errorObj.message || data.message || `HTTP ${response.statusCode}`;
|
||||
reject(new Error(message));
|
||||
},
|
||||
fail(error) {
|
||||
reject(new Error(error.errMsg || "Network request failed."));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function listKnowledgeBases() {
|
||||
return request("/api/v1/knowledge-bases");
|
||||
}
|
||||
|
||||
function createKnowledgeFromURL(knowledgeBaseId, url, enableMultimodel = false) {
|
||||
return request(`/api/v1/knowledge-bases/${knowledgeBaseId}/knowledge/url`, {
|
||||
method: "POST",
|
||||
data: {
|
||||
url,
|
||||
enable_multimodel: enableMultimodel
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function createSession(knowledgeBaseId) {
|
||||
return request("/api/v1/sessions", {
|
||||
method: "POST",
|
||||
data: knowledgeBaseId ? { knowledge_base_id: knowledgeBaseId } : {}
|
||||
});
|
||||
}
|
||||
|
||||
function knowledgeChat(sessionId, query, knowledgeBaseId) {
|
||||
const data = { query };
|
||||
if (knowledgeBaseId) {
|
||||
data.knowledge_base_ids = [knowledgeBaseId];
|
||||
}
|
||||
|
||||
return request(`/api/v1/knowledge-chat/${sessionId}`, {
|
||||
method: "POST",
|
||||
data
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createKnowledgeFromURL,
|
||||
createSession,
|
||||
knowledgeChat,
|
||||
listKnowledgeBases,
|
||||
request
|
||||
};
|
||||
@@ -0,0 +1,42 @@
|
||||
function parseSSE(raw) {
|
||||
if (!raw || typeof raw !== "string") {
|
||||
return [];
|
||||
}
|
||||
|
||||
return raw
|
||||
.split(/\n\n+/)
|
||||
.map((block) => block.trim())
|
||||
.filter(Boolean)
|
||||
.map((block) => {
|
||||
const event = { event: "message", data: "" };
|
||||
block.split(/\n/).forEach((line) => {
|
||||
if (line.startsWith("event:")) {
|
||||
event.event = line.slice(6).trim();
|
||||
}
|
||||
if (line.startsWith("data:")) {
|
||||
event.data += line.slice(5).trim();
|
||||
}
|
||||
});
|
||||
return event;
|
||||
})
|
||||
.filter((event) => event.data);
|
||||
}
|
||||
|
||||
function collectAnswerFromSSE(raw) {
|
||||
return parseSSE(raw).reduce((answer, event) => {
|
||||
try {
|
||||
const payload = JSON.parse(event.data);
|
||||
if (payload.response_type === "answer" && payload.content) {
|
||||
return answer + payload.content;
|
||||
}
|
||||
} catch (error) {
|
||||
return answer;
|
||||
}
|
||||
return answer;
|
||||
}, "");
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
collectAnswerFromSSE,
|
||||
parseSSE
|
||||
};
|
||||
Reference in new issue
Block a user