Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File System Access API #3

Merged
merged 5 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 70 additions & 28 deletions liubai-backends/liubai-laf/cloud-functions/ai-entrance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,32 +507,54 @@ class AiDirective {
// 1. get the user's ai room
const room = await AiHelper.getMyAiRoom(entry)
if(!room) return
const roomId = room._id

// 2. find the bot in the room
const theBot = room.characters.find(v => v === bot.character)
const characterKicked = bot.character
const theBot = room.characters.find(v => v === characterKicked)
if(!theBot) {
const msg2 = t("already_left", { botName: bot.name })
TellUser.text(entry, msg2)
return
}

// 3. remove the bot from the room
const newBots = room.characters.filter(v => v !== bot.character)
const oldCharacters = room.characters
const newCharacters = oldCharacters.filter(v => v !== characterKicked)
const u3: Partial<Table_AiRoom> = {
characters: newBots,
characters: newCharacters,
updatedStamp: getNowStamp(),
}
const rCol = db.collection("AiRoom")
const res3 = await rCol.doc(room._id).update(u3)
const res3 = await rCol.doc(roomId).update(u3)

// 4. send a message to user
const msg4 = t("bot_left", { botName: bot.name })
TellUser.text(entry, msg4)
// 4. get non-used characters
let addedList = await AiHelper.getNonUsedCharacters(roomId)
addedList = addedList.filter(v => !Boolean(oldCharacters.includes(v)))
const reservedNum = newCharacters.length < 1 ? 4 : 3
if(addedList.length > reservedNum) {
addedList.splice(reservedNum, addedList.length - reservedNum)
}

// 5. send a message to user
const msg5 = t("bot_left", { botName: bot.name })
const gzhType = AiHelper.getGzhType()
if(gzhType === "service_account" && addedList.length > 0) {
// 5.1 send menu
const menuList: AiMenuItem[] = []
const prefixMsg = msg5 + "\n\n" + t("operation_title")
addedList.forEach(v => menuList.push({ operation: "add", character: v }))
TellUser.menu(entry, prefixMsg, menuList, "")
}
else {
// 5.2 send text
TellUser.text(entry, msg5)
}

// 5. log
LogHelper.kick([bot.character], user)
// 6. log
LogHelper.kick([characterKicked], user)

return res3
return res3
}

private static async _showThereAre3(
Expand Down Expand Up @@ -1324,9 +1346,9 @@ class BaseBot {
if(!txt6) return

// 2. reply to user
const _env = process.env
const finishReason = AiHelper.getFinishReason(chatCompletion)
if(finishReason === "length" && _env.LIU_WX_GZ_TYPE === "service_account") {
const gzhType = AiHelper.getGzhType()
if(finishReason === "length" && gzhType === "service_account") {
TellUser.menu(
aiParam.entry,
txt6,
Expand Down Expand Up @@ -4216,16 +4238,9 @@ class AiHelper {
return res
}

static async checkIfNobodyHere(
entry: AiEntry,
room: Table_AiRoom,
) {
// 1. return false if there is at least one character
const len = room.characters.length
if(len >= 1) return false

// 2. get history for ever existed characters
const chats = await this.getLatestChat(room._id, 10)
static async getNonUsedCharacters(roomId: string) {
// 1. get history
const chats = await this.getLatestChat(roomId, 10)
const everExistedCharacters: AiCharacter[] = []
chats.forEach(v => {
const c = v.character
Expand All @@ -4235,7 +4250,7 @@ class AiHelper {
}
})

// 3. get availableCharacters
// 2. get availableCharacters
const availableCharacters = this.getAvailableCharacters()
for(let i=0; i<availableCharacters.length; i++) {
const v = availableCharacters[i]
Expand All @@ -4244,18 +4259,39 @@ class AiHelper {
i--
}
}
if(availableCharacters.length < 1) return true
if(availableCharacters.length < 1) {
return []
}

// 4. randomly sort
// 3. randomly sort
const addedList: AiCharacter[] = []
while(addedList.length < 3 && availableCharacters.length > 0) {
while(addedList.length < 5 && availableCharacters.length > 0) {
const r = Math.floor(Math.random() * availableCharacters.length)
const c = availableCharacters[r]
addedList.push(c)
availableCharacters.splice(r, 1)
}

// 5. menu
return addedList
}

static async checkIfNobodyHere(
entry: AiEntry,
room: Table_AiRoom,
) {
// 1. return false if there is at least one character
const len = room.characters.length
if(len >= 1) return false

// 2. get history for ever existed characters
const addedList = await this.getNonUsedCharacters(room._id)
const len2 = addedList.length
if(len2 < 1) return false
if(len2 > 3) {
addedList.splice(3, len2 - 3)
}

// 3. menu
const menuList: AiMenuItem[] = []
addedList.forEach(v => menuList.push({ operation: "add", character: v }))
const { t } = useI18n(aiLang, { user: entry.user })
Expand All @@ -4264,6 +4300,11 @@ class AiHelper {
return true
}

static getGzhType() {
const _env = process.env
return _env.LIU_WX_GZ_TYPE ?? "subscription_account"
}

}

class UserHelper {
Expand Down Expand Up @@ -4490,6 +4531,7 @@ class TellUser {
fromCharacter?: AiCharacter
) {
const _env = process.env
const gzhType = AiHelper.getGzhType()
const { wx_gzh_openid, user } = entry
const { t } = useI18n(aiLang, { user })

Expand Down Expand Up @@ -4534,7 +4576,7 @@ class TellUser {

// 2. send to wx gzh
if(wx_gzh_openid) {
if(_env.LIU_WX_GZ_TYPE === "subscription_account") {
if(gzhType === "subscription_account") {
console.warn("we cannot send the menu to the user due to subscription_account")
return
}
Expand Down
2 changes: 1 addition & 1 deletion liubai-backends/liubai-laf/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "liubai-laf",
"version": "0.5.1",
"version": "0.5.2",
"description": "Liubai's backend for Laf",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion liubai-docs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "liubai-docs",
"version": "0.5.1",
"version": "0.5.2",
"description": "It's the docs center for Liubai",
"author": {
"name": "yenche123",
Expand Down
52 changes: 26 additions & 26 deletions liubai-frontends/liubai-web/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "liubai-web",
"private": true,
"version": "0.5.1",
"version": "0.5.2",
"description": "It's the frontend project for Liubai",
"author": {
"name": "yenche123",
Expand All @@ -24,26 +24,26 @@
"@bugfender/sdk": "^2.3.0",
"@he-tree/vue": "^2.9.3",
"@intlify/core-base": "^10.0.5",
"@intlify/unplugin-vue-i18n": "^6.0.1",
"@intlify/unplugin-vue-i18n": "^6.0.2",
"@openpanel/web": "^1.0.1",
"@openreplay/tracker": "^15.0.3",
"@sentry/vue": "^8.42.0",
"@tiptap/core": "2.10.3",
"@tiptap/extension-blockquote": "2.10.3",
"@tiptap/extension-code": "2.10.3",
"@tiptap/extension-code-block-lowlight": "2.10.3",
"@tiptap/extension-hard-break": "2.10.3",
"@tiptap/extension-heading": "2.10.3",
"@tiptap/extension-horizontal-rule": "2.10.3",
"@tiptap/extension-link": "2.10.3",
"@tiptap/extension-mention": "2.10.3",
"@tiptap/extension-placeholder": "2.10.3",
"@tiptap/extension-task-item": "2.10.3",
"@tiptap/extension-task-list": "2.10.3",
"@tiptap/pm": "2.10.3",
"@tiptap/starter-kit": "2.10.3",
"@tiptap/suggestion": "2.10.3",
"@tiptap/vue-3": "2.10.3",
"@sentry/vue": "^8.47.0",
"@tiptap/core": "2.10.4",
"@tiptap/extension-blockquote": "2.10.4",
"@tiptap/extension-code": "2.10.4",
"@tiptap/extension-code-block-lowlight": "2.10.4",
"@tiptap/extension-hard-break": "2.10.4",
"@tiptap/extension-heading": "2.10.4",
"@tiptap/extension-horizontal-rule": "2.10.4",
"@tiptap/extension-link": "2.10.4",
"@tiptap/extension-mention": "2.10.4",
"@tiptap/extension-placeholder": "2.10.4",
"@tiptap/extension-task-item": "2.10.4",
"@tiptap/extension-task-list": "2.10.4",
"@tiptap/pm": "2.10.4",
"@tiptap/starter-kit": "2.10.4",
"@tiptap/suggestion": "2.10.4",
"@tiptap/vue-3": "2.10.4",
"@vuepic/vue-datepicker": "^10.0.0",
"@vueuse/core": "^12.0.0",
"@vueuse/integrations": "^12.0.0",
Expand All @@ -56,13 +56,13 @@
"exifreader": "^4.25.0",
"file-saver": "^2.0.5",
"floating-vue": "^5.2.2",
"highlight.js": "^11.10.0",
"highlight.js": "^11.11.0",
"ics": "^3.8.1",
"jszip": "^3.10.1",
"lowlight": "^3.2.0",
"lowlight": "^3.3.0",
"nanoid": "^5.0.9",
"pinia": "^2.3.0",
"posthog-js": "^1.194.6",
"posthog-js": "^1.203.1",
"prosemirror-virtual-cursor": "^0.4.2",
"qiniu-js": "^3.4.2",
"qrcode": "^1.5.4",
Expand All @@ -78,18 +78,18 @@
},
"devDependencies": {
"@sentry/vite-plugin": "^2.22.7",
"@types/canvas-confetti": "^1.6.4",
"@types/canvas-confetti": "^1.9.0",
"@types/file-saver": "^2.0.7",
"@types/google.accounts": "^0.0.15",
"@types/node": "^20.16.5",
"@vitejs/plugin-vue": "^5.2.1",
"rollup-plugin-visualizer": "^5.12.0",
"sass": "^1.82.0",
"sass": "^1.83.0",
"typescript": "~5.6.3",
"user-agent-data-types": "^0.4.2",
"vite": "^6.0.3",
"vite": "^6.0.5",
"vite-plugin-compression2": "^1.3.3",
"vite-plugin-inspect": "^0.10.3",
"vite-plugin-inspect": "^0.10.4",
"vite-plugin-mkcert": "^1.17.6",
"vite-plugin-pwa": "^0.21.1",
"vite-plugin-qrcode": "^0.2.3",
Expand Down
Loading