feat(miniprogram): add WeChat mini program plugin
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
# WeKnora Mini Program
|
||||
|
||||
This directory contains a WeChat Mini Program plugin for WeKnora. It gives mobile users a lightweight entry point to:
|
||||
|
||||
- configure a WeKnora API endpoint and tenant API key;
|
||||
- list available knowledge bases;
|
||||
- import a URL into a selected knowledge base;
|
||||
- ask a selected knowledge base through WeKnora 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://weknora.example.com`;
|
||||
- API Key from the WeKnora tenant settings page.
|
||||
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.
|
||||
|
||||
## 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 WeKnora through a HTTPS development domain.
|
||||
- In production Mini Programs, add the WeKnora 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,12 @@
|
||||
App({
|
||||
onLaunch() {
|
||||
const settings = wx.getStorageSync("weknora_settings");
|
||||
if (!settings) {
|
||||
wx.setStorageSync("weknora_settings", {
|
||||
baseUrl: "http://localhost:8080",
|
||||
apiKey: "",
|
||||
selectedKnowledgeBaseId: ""
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,33 @@
|
||||
{
|
||||
"pages": [
|
||||
"pages/index/index",
|
||||
"pages/chat/chat",
|
||||
"pages/settings/settings"
|
||||
],
|
||||
"window": {
|
||||
"navigationBarTitleText": "WeKnora",
|
||||
"navigationBarBackgroundColor": "#102a43",
|
||||
"navigationBarTextStyle": "white",
|
||||
"backgroundColor": "#f4f7fb"
|
||||
},
|
||||
"tabBar": {
|
||||
"color": "#667085",
|
||||
"selectedColor": "#1264a3",
|
||||
"backgroundColor": "#ffffff",
|
||||
"borderStyle": "white",
|
||||
"list": [
|
||||
{
|
||||
"pagePath": "pages/index/index",
|
||||
"text": "Knowledge"
|
||||
},
|
||||
{
|
||||
"pagePath": "pages/chat/chat",
|
||||
"text": "Chat"
|
||||
},
|
||||
{
|
||||
"pagePath": "pages/settings/settings",
|
||||
"text": "Settings"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
page {
|
||||
min-height: 100%;
|
||||
background: #f4f7fb;
|
||||
color: #102a43;
|
||||
font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", sans-serif;
|
||||
}
|
||||
|
||||
.page {
|
||||
min-height: 100vh;
|
||||
box-sizing: border-box;
|
||||
padding: 32rpx;
|
||||
}
|
||||
|
||||
.card {
|
||||
padding: 28rpx;
|
||||
border-radius: 24rpx;
|
||||
background: #ffffff;
|
||||
box-shadow: 0 12rpx 36rpx rgba(16, 42, 67, 0.08);
|
||||
}
|
||||
|
||||
.title {
|
||||
font-size: 40rpx;
|
||||
font-weight: 700;
|
||||
margin-bottom: 12rpx;
|
||||
}
|
||||
|
||||
.muted {
|
||||
color: #667085;
|
||||
font-size: 26rpx;
|
||||
}
|
||||
|
||||
.field {
|
||||
margin-top: 24rpx;
|
||||
}
|
||||
|
||||
.label {
|
||||
display: block;
|
||||
margin-bottom: 12rpx;
|
||||
color: #344054;
|
||||
font-size: 26rpx;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
input,
|
||||
textarea,
|
||||
picker {
|
||||
box-sizing: border-box;
|
||||
width: 100%;
|
||||
min-height: 88rpx;
|
||||
padding: 20rpx 24rpx;
|
||||
border: 1rpx solid #d0d5dd;
|
||||
border-radius: 18rpx;
|
||||
background: #ffffff;
|
||||
font-size: 28rpx;
|
||||
}
|
||||
|
||||
textarea {
|
||||
min-height: 180rpx;
|
||||
}
|
||||
|
||||
button {
|
||||
margin-top: 24rpx;
|
||||
border-radius: 18rpx;
|
||||
background: #1264a3;
|
||||
color: #ffffff;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
button[disabled] {
|
||||
background: #98a2b3;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"name": "weknora-miniprogram",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"description": "WeChat Mini Program plugin for WeKnora",
|
||||
"scripts": {
|
||||
"test": "node --test ../tests/miniprogram/*.test.js"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
const { getSettings } = require("../../utils/config");
|
||||
const { createSession, knowledgeChat } = require("../../utils/request");
|
||||
const { collectAnswerFromSSE } = require("../../utils/sse");
|
||||
|
||||
Page({
|
||||
data: {
|
||||
answer: "",
|
||||
loading: false,
|
||||
query: "",
|
||||
rawResponse: "",
|
||||
sessionId: ""
|
||||
},
|
||||
|
||||
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.data?.id;
|
||||
if (!sessionId) {
|
||||
throw new Error("The session API did not return a session id.");
|
||||
}
|
||||
this.setData({ sessionId });
|
||||
return sessionId;
|
||||
},
|
||||
|
||||
async ask() {
|
||||
this.setData({ answer: "", rawResponse: "", loading: true });
|
||||
try {
|
||||
const sessionId = await this.ensureSession();
|
||||
const response = await knowledgeChat(sessionId, this.data.query.trim());
|
||||
const rawResponse = typeof response === "string" ? response : JSON.stringify(response);
|
||||
const answer = collectAnswerFromSSE(rawResponse);
|
||||
this.setData({
|
||||
answer,
|
||||
rawResponse: answer ? "" : rawResponse
|
||||
});
|
||||
} catch (error) {
|
||||
wx.showModal({
|
||||
title: "Chat failed",
|
||||
content: error.message,
|
||||
showCancel: false
|
||||
});
|
||||
} finally {
|
||||
this.setData({ loading: false });
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"navigationBarTitleText": "Chat"
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<view class="page">
|
||||
<view class="card">
|
||||
<view class="title">Knowledge Chat</view>
|
||||
<view class="muted">Ask the selected WeKnora knowledge base.</view>
|
||||
|
||||
<view class="field">
|
||||
<text class="label">Question</text>
|
||||
<textarea value="{{query}}" placeholder="Ask something..." bindinput="onQueryInput" />
|
||||
</view>
|
||||
|
||||
<button bindtap="ask" loading="{{loading}}" disabled="{{!query}}">Ask WeKnora</button>
|
||||
</view>
|
||||
|
||||
<view class="card answer-card" wx:if="{{answer || rawResponse}}">
|
||||
<view class="label">Answer</view>
|
||||
<view class="answer">{{answer || rawResponse}}</view>
|
||||
</view>
|
||||
</view>
|
||||
@@ -0,0 +1,10 @@
|
||||
.answer-card {
|
||||
margin-top: 28rpx;
|
||||
}
|
||||
|
||||
.answer {
|
||||
white-space: pre-wrap;
|
||||
line-height: 1.7;
|
||||
color: #102a43;
|
||||
font-size: 28rpx;
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
const { saveSettings, getSettings } = require("../../utils/config");
|
||||
const { createKnowledgeFromURL, listKnowledgeBases } = require("../../utils/request");
|
||||
|
||||
Page({
|
||||
data: {
|
||||
importing: false,
|
||||
knowledgeBases: [],
|
||||
loading: false,
|
||||
selectedIndex: 0,
|
||||
selectedKnowledgeBaseId: "",
|
||||
selectedKnowledgeBaseName: "",
|
||||
url: ""
|
||||
},
|
||||
|
||||
onShow() {
|
||||
const settings = getSettings();
|
||||
if (settings.selectedKnowledgeBaseId) {
|
||||
this.setData({ selectedKnowledgeBaseId: settings.selectedKnowledgeBaseId });
|
||||
}
|
||||
this.loadKnowledgeBases();
|
||||
},
|
||||
|
||||
onUrlInput(event) {
|
||||
this.setData({ url: event.detail.value });
|
||||
},
|
||||
|
||||
onKnowledgeBaseChange(event) {
|
||||
const selectedIndex = Number(event.detail.value);
|
||||
const selected = this.data.knowledgeBases[selectedIndex];
|
||||
if (!selected) return;
|
||||
|
||||
saveSettings({ selectedKnowledgeBaseId: selected.id });
|
||||
this.setData({
|
||||
selectedIndex,
|
||||
selectedKnowledgeBaseId: selected.id,
|
||||
selectedKnowledgeBaseName: selected.name
|
||||
});
|
||||
},
|
||||
|
||||
async loadKnowledgeBases() {
|
||||
this.setData({ loading: true });
|
||||
try {
|
||||
const response = await listKnowledgeBases();
|
||||
const knowledgeBases = response.data || [];
|
||||
const settings = getSettings();
|
||||
const selectedIndex = Math.max(
|
||||
0,
|
||||
knowledgeBases.findIndex((item) => item.id === settings.selectedKnowledgeBaseId)
|
||||
);
|
||||
const selected = knowledgeBases[selectedIndex];
|
||||
this.setData({
|
||||
knowledgeBases,
|
||||
selectedIndex,
|
||||
selectedKnowledgeBaseId: selected?.id || "",
|
||||
selectedKnowledgeBaseName: selected?.name || ""
|
||||
});
|
||||
if (selected?.id) {
|
||||
saveSettings({ selectedKnowledgeBaseId: selected.id });
|
||||
}
|
||||
} catch (error) {
|
||||
wx.showModal({
|
||||
title: "Load failed",
|
||||
content: 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: "Imported", icon: "success" });
|
||||
} catch (error) {
|
||||
wx.showModal({
|
||||
title: "Import failed",
|
||||
content: error.message,
|
||||
showCancel: false
|
||||
});
|
||||
} finally {
|
||||
this.setData({ importing: false });
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"navigationBarTitleText": "Knowledge"
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<view class="page">
|
||||
<view class="card hero">
|
||||
<view class="title">WeKnora Knowledge</view>
|
||||
<view class="muted">Import a web page into a selected knowledge base from WeChat.</view>
|
||||
</view>
|
||||
|
||||
<view class="card section">
|
||||
<view class="field">
|
||||
<text class="label">Knowledge Base</text>
|
||||
<picker range="{{knowledgeBases}}" range-key="name" value="{{selectedIndex}}" bindchange="onKnowledgeBaseChange">
|
||||
<view class="picker-value">{{selectedKnowledgeBaseName || "Tap to select"}}</view>
|
||||
</picker>
|
||||
</view>
|
||||
|
||||
<button bindtap="loadKnowledgeBases" loading="{{loading}}">Refresh knowledge bases</button>
|
||||
</view>
|
||||
|
||||
<view class="card section">
|
||||
<view class="field">
|
||||
<text class="label">URL</text>
|
||||
<input value="{{url}}" placeholder="https://github.com/Tencent/WeKnora" bindinput="onUrlInput" />
|
||||
</view>
|
||||
|
||||
<button bindtap="importURL" loading="{{importing}}" disabled="{{!selectedKnowledgeBaseId || !url}}">Import URL</button>
|
||||
</view>
|
||||
</view>
|
||||
@@ -0,0 +1,16 @@
|
||||
.hero {
|
||||
background: linear-gradient(135deg, #102a43 0%, #1264a3 100%);
|
||||
color: #ffffff;
|
||||
}
|
||||
|
||||
.hero .muted {
|
||||
color: rgba(255, 255, 255, 0.78);
|
||||
}
|
||||
|
||||
.section {
|
||||
margin-top: 28rpx;
|
||||
}
|
||||
|
||||
.picker-value {
|
||||
color: #102a43;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
const { getSettings, saveSettings } = require("../../utils/config");
|
||||
|
||||
Page({
|
||||
data: {
|
||||
baseUrl: "",
|
||||
apiKey: ""
|
||||
},
|
||||
|
||||
onShow() {
|
||||
const settings = getSettings();
|
||||
this.setData({
|
||||
baseUrl: settings.baseUrl,
|
||||
apiKey: settings.apiKey
|
||||
});
|
||||
},
|
||||
|
||||
onBaseUrlInput(event) {
|
||||
this.setData({ baseUrl: event.detail.value });
|
||||
},
|
||||
|
||||
onApiKeyInput(event) {
|
||||
this.setData({ apiKey: event.detail.value });
|
||||
},
|
||||
|
||||
save() {
|
||||
saveSettings({
|
||||
baseUrl: this.data.baseUrl,
|
||||
apiKey: this.data.apiKey
|
||||
});
|
||||
wx.showToast({ title: "Saved", icon: "success" });
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"navigationBarTitleText": "Settings"
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<view class="page">
|
||||
<view class="card">
|
||||
<view class="title">Connect WeKnora</view>
|
||||
<view class="muted">Use your WeKnora API endpoint and tenant API key.</view>
|
||||
|
||||
<view class="field">
|
||||
<text class="label">API Base URL</text>
|
||||
<input value="{{baseUrl}}" placeholder="https://your-weknora.example.com" bindinput="onBaseUrlInput" />
|
||||
</view>
|
||||
|
||||
<view class="field">
|
||||
<text class="label">API Key</text>
|
||||
<input value="{{apiKey}}" password placeholder="sk-..." bindinput="onApiKeyInput" />
|
||||
</view>
|
||||
|
||||
<button bindtap="save">Save settings</button>
|
||||
</view>
|
||||
</view>
|
||||
@@ -0,0 +1,3 @@
|
||||
.card {
|
||||
margin-top: 40rpx;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"description": "WeKnora Mini Program plugin",
|
||||
"setting": {
|
||||
"urlCheck": true,
|
||||
"es6": true,
|
||||
"enhance": true,
|
||||
"postcss": true,
|
||||
"minified": true
|
||||
},
|
||||
"compileType": "miniprogram",
|
||||
"libVersion": "latest",
|
||||
"projectname": "WeKnora Mini Program",
|
||||
"condition": {}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"appid": "your-wechat-mini-program-appid"
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"rules": [
|
||||
{
|
||||
"action": "allow",
|
||||
"page": "*"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
const STORAGE_KEY = "weknora_settings";
|
||||
|
||||
function normalizeBaseUrl(baseUrl) {
|
||||
if (!baseUrl || typeof baseUrl !== "string") {
|
||||
return "";
|
||||
}
|
||||
|
||||
return baseUrl.trim().replace(/\/+$/, "");
|
||||
}
|
||||
|
||||
function getSettings() {
|
||||
const stored = wx.getStorageSync(STORAGE_KEY) || {};
|
||||
return {
|
||||
baseUrl: normalizeBaseUrl(stored.baseUrl || ""),
|
||||
apiKey: stored.apiKey || "",
|
||||
selectedKnowledgeBaseId: stored.selectedKnowledgeBaseId || ""
|
||||
};
|
||||
}
|
||||
|
||||
function saveSettings(settings) {
|
||||
const current = getSettings();
|
||||
const next = {
|
||||
...current,
|
||||
...settings,
|
||||
baseUrl: normalizeBaseUrl(settings.baseUrl ?? current.baseUrl)
|
||||
};
|
||||
wx.setStorageSync(STORAGE_KEY, next);
|
||||
return next;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
STORAGE_KEY,
|
||||
getSettings,
|
||||
normalizeBaseUrl,
|
||||
saveSettings
|
||||
};
|
||||
@@ -0,0 +1,71 @@
|
||||
const { getSettings } = require("./config");
|
||||
|
||||
function request(path, options = {}) {
|
||||
const settings = getSettings();
|
||||
if (!settings.baseUrl) {
|
||||
return Promise.reject(new Error("Please configure the WeKnora API base URL first."));
|
||||
}
|
||||
if (!settings.apiKey) {
|
||||
return Promise.reject(new Error("Please configure the WeKnora API key first."));
|
||||
}
|
||||
|
||||
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 message = response.data?.error?.message || response.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) {
|
||||
return request(`/api/v1/knowledge-chat/${sessionId}`, {
|
||||
method: "POST",
|
||||
data: { query }
|
||||
});
|
||||
}
|
||||
|
||||
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