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 });
}
}
});