feat(miniprogram): add WeChat mini program plugin

This commit is contained in:
wolfkill
2026-04-27 16:08:22 +08:00
committed by lyingbug
parent d55b52652c
commit d06111e5f7
27 changed files with 705 additions and 6 deletions
+54
View File
@@ -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 });
}
}
});
+3
View File
@@ -0,0 +1,3 @@
{
"navigationBarTitleText": "Chat"
}
+18
View File
@@ -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>
+10
View File
@@ -0,0 +1,10 @@
.answer-card {
margin-top: 28rpx;
}
.answer {
white-space: pre-wrap;
line-height: 1.7;
color: #102a43;
font-size: 28rpx;
}