WeKnora deployment source with the reviewed wiki identity, summary and recovery fixes
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-weknora) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (latest, advisory) (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Build & Publish (dsh-weknora) / Publish to npm (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-weknora) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (latest, advisory) (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Build & Publish (dsh-weknora) / Publish to npm (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
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": "#0d3b2a",
|
||||
"navigationBarTextStyle": "white",
|
||||
"backgroundColor": "#f3f3f3"
|
||||
},
|
||||
"tabBar": {
|
||||
"color": "#8b8b8b",
|
||||
"selectedColor": "#07c05f",
|
||||
"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,74 @@
|
||||
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;
|
||||
}
|
||||
@@ -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,55 @@
|
||||
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 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);
|
||||
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: #1a1a1a;
|
||||
font-size: 28rpx;
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
const { saveSettings, getSettings } = require("../../utils/config");
|
||||
const { createKnowledgeFromURL, listKnowledgeBases } = require("../../utils/request");
|
||||
|
||||
function normalizeKnowledgeBases(response) {
|
||||
if (Array.isArray(response?.data)) {
|
||||
return response.data;
|
||||
}
|
||||
if (Array.isArray(response?.data?.list)) {
|
||||
return response.data.list;
|
||||
}
|
||||
if (Array.isArray(response?.knowledge_bases)) {
|
||||
return response.knowledge_bases;
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
Page({
|
||||
data: {
|
||||
importing: false,
|
||||
knowledgeBases: [],
|
||||
knowledgeBaseNames: [],
|
||||
loading: false,
|
||||
needsSettings: false,
|
||||
selectedIndex: 0,
|
||||
selectedKnowledgeBaseId: "",
|
||||
selectedKnowledgeBaseName: "",
|
||||
statusMessage: "",
|
||||
url: ""
|
||||
},
|
||||
|
||||
onShow() {
|
||||
const settings = getSettings();
|
||||
const needsSettings = !settings.baseUrl || !settings.apiKey;
|
||||
if (settings.selectedKnowledgeBaseId) {
|
||||
this.setData({ selectedKnowledgeBaseId: settings.selectedKnowledgeBaseId });
|
||||
}
|
||||
this.setData({ needsSettings });
|
||||
if (needsSettings) {
|
||||
return;
|
||||
}
|
||||
this.loadKnowledgeBases();
|
||||
},
|
||||
|
||||
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
|
||||
});
|
||||
},
|
||||
|
||||
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 settings = getSettings();
|
||||
const selectedIndex = Math.max(
|
||||
0,
|
||||
knowledgeBases.findIndex((item) => item.id === settings.selectedKnowledgeBaseId)
|
||||
);
|
||||
const selected = knowledgeBases[selectedIndex];
|
||||
this.setData({
|
||||
knowledgeBases,
|
||||
knowledgeBaseNames,
|
||||
selectedIndex,
|
||||
selectedKnowledgeBaseId: selected?.id || "",
|
||||
selectedKnowledgeBaseName: selected?.name || "",
|
||||
statusMessage: knowledgeBases.length
|
||||
? `Loaded ${knowledgeBases.length} knowledge bases.`
|
||||
: "No knowledge bases found."
|
||||
});
|
||||
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,43 @@
|
||||
<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 wx:if="{{needsSettings}}" class="notice">
|
||||
Configure the WeKnora API base URL and API key before loading knowledge bases.
|
||||
<button bindtap="openSettings" class="secondary">Open Settings</button>
|
||||
</view>
|
||||
|
||||
<view class="field">
|
||||
<text class="label">Knowledge Base</text>
|
||||
<picker range="{{knowledgeBaseNames}}" value="{{selectedIndex}}" bindchange="onKnowledgeBaseChange">
|
||||
<view class="picker-value">{{selectedKnowledgeBaseName || "Tap to select"}}</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}}">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,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,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,76 @@
|
||||
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, 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