Skip to content

Commit

Permalink
合并分支
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaowFISH committed Jan 24, 2025
1 parent fc69616 commit dfc3678
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 7 deletions.
3 changes: 1 addition & 2 deletions packages/core/src/adapters/gemini.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import axios from "axios";
import { Config } from "../config";
import { foldText, getMimeTypeFromBase64, sendRequest } from "../utils";
import { getMimeTypeFromBase64, sendRequest } from "../utils";
import { BaseAdapter, Response } from "./base";
import { LLM } from "./config";
import { Message } from "./creators/component";
Expand Down
19 changes: 19 additions & 0 deletions packages/core/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,25 @@ export class Bot {
} catch (e) {
return "";
}
try {
replyTo = replyTo.toString().trim();
if (isNotEmpty(replyTo) && !replyTo.startsWith("private:")) {
const numericMatch = replyTo.match(/\d+/);
if (numericMatch) {
replyTo = numericMatch[0].replace(/\s/g, "");
}
// 不合法的 channelId
if (replyTo.match(/\{.+\}/)) {
replyTo = "";
}
if (replyTo.indexOf("sandbox") > -1) {
replyTo = "";
}
}
return replyTo;
} catch (e) {
return "";
}
}

// TODO: 规范化params
Expand Down
10 changes: 8 additions & 2 deletions packages/core/src/extensions/ext_command.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
// ==Extension==
// @name Command Run
// @version 1.0.0
// @description 允许大模型调用自身指令
// @author MiaowFISH
// ==/Extension==

import { h } from "koishi";

import { SchemaNode } from "../adapters/creators/schema";
Expand All @@ -12,10 +19,9 @@ import { Description, Extension, Name, Param } from "./base";
你可以运行一些只有在IM平台才能运行的指令,下面是你可以运行的指令列表。
- ban <用户ID> <时长>: 将用户禁言。单位为秒。
- delmsg <消息ID>: 撤回用户的消息。当你认为别人刷屏或发表不当内容时,运行这条指令
- reaction-create <消息ID> <表态编号>: 对一个或多个消息进行表态。表态编号是数字,这里是一个简略的参考:惊讶(0),不适(1),无语(27),震惊(110),滑稽(178), 点赞(76)。
请将指令字符串添加到 cmd 参数上来执行指令。
将channel设置为你要执行指令的频道,不填默认为当前频道。
比如 \`execute("ban 1234567 600")\` 是将用户 1234567 禁言10分钟
比如 \`execute("ban 1234567 600")\` 是将用户 1234567 禁言 10 分钟
这个函数**不能**在 "status" 为 function 的时候使用。DO NOT USE THIS FUNCTION WHEN "status" IS "function".
你只能在 "status" 为 "success" 或 "skip" 的时候使用这个函数。YOU CAN ONLY USE THIS FUNCTION WHEN "status" IS "success" OR "skip".
这个函数没有返回值。
Expand Down
43 changes: 43 additions & 0 deletions packages/core/src/extensions/ext_interactions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// ==Extension==
// @name Interactions
// @version 1.0.0
// @description 允许大模型在聊群内进行交互
// @author HydroGest
// ==/Extension==

import { Description, Extension, Name, Param } from "./base";

@Name("reaction-create")
@Description(`
在当前频道对一个或多个消息进行表态。表态编号是数字,这里是一个简略的参考:惊讶(0),不适(1),无语(27),震惊(110),滑稽(178), 点赞(76)
`)
@Param("message", "消息 ID")
@Param("emoji_id", "表态编号")
export class Reaction extends Extension {
async apply(message: number, emoji_id: number) {
try {
// @ts-ignore
await this.session.onebot._request("set_msg_emoji_like", { message_id: message, emoji_id: emoji_id});
this.ctx.logger.info(`Bot[${this.session.selfId}]对消息 ${message} 进行了表态: ${emoji_id}`);
} catch (e) {
this.ctx.logger.error(`Bot[${this.session.selfId}]执行表态失败: ${message}, ${emoji_id} - `, e.message);
}
}
}

@Name("essence-create")
@Description(`
在当前频道将一个消息设置为精华消息。常在你认为某个消息十分重要或过于典型时使用。
`)
@Param("message", "消息 ID")
export class Essence extends Extension {
async apply(message: number) {
try {
// @ts-ignore
await this.session.onebot._request("set_essence_msg", { message_id: message})
this.ctx.logger.info(`Bot[${this.session.selfId}]将消息 ${message} 设置为精华`);
} catch (e) {
this.ctx.logger.error(`Bot[${this.session.selfId}]设置精华消息失败: ${message} - `, e.message);
}
}
}
5 changes: 3 additions & 2 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Config } from "./config";
import { containsFilter, getBotName, isChannelAllowed, getFileUnique, getFormatDateTime } from "./utils/toolkit";
import { ensurePromptFileExists, genSysPrompt } from "./utils/prompt";
import { MarkType, SendQueue } from "./services/sendQueue";
import { outputSchema } from "./adapters/creators/schema";
import { getOutputSchema } from "./adapters/creators/schema";
import { initDatabase } from "./database";
import { processContent, processText } from "./utils/content";
import { foldText, isEmpty, isNotEmpty } from "./utils/string";
Expand Down Expand Up @@ -254,7 +254,7 @@ export function apply(ctx: Context, config: Config) {
curGroupId: channelId,
BotName: botName,
BotSelfId: session.bot.selfId,
outputSchema,
outputSchema: getOutputSchema(config.Settings.LLMResponseFormat),
functionPrompt: "{{functionPrompt}}",
// 记忆模块还未完成,等完成后取消注释
// coreMemory: await bot.getCoreMemory(session.selfId),
Expand Down Expand Up @@ -414,6 +414,7 @@ ${botName}想要跳过此次回复,来自 API ${current}
catch (error) {
ctx.logger.error(`处理消息时出错: ${error.message}`);
if (config.Debug.DebugAsInfo) ctx.logger.error(error.stack);
if (config.Debug.DebugAsInfo) ctx.logger.error(error.stack);
return false;
}

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from "./prompt";
export * from "./string";
export * from "./toolkit";
export * from "./verifier";
export * from "./metaReader"
43 changes: 43 additions & 0 deletions packages/core/src/utils/metaReader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import fs from 'fs';
import path from 'path';

/**
* 从指定的 TypeScript 文件中读取元数据。
*
* 该函数读取文件内容,查找以 `// ==Extension==` 开始和 `// ==/Extension==` 结束的部分,并提取其中以 `// @` 开头的元数据信息。
* 元数据信息存储在一个对象中,键是 `@` 后面的标识符,值是标识符后面的描述信息。
*
* @param filePath - 要读取元数据的 TypeScript 文件的路径。
* @returns 一个包含元数据的对象,键是元数据标识符(如 `name`、`version`、`description` 等),值是相应的元数据信息。如果不是预期格式则返回空对象。
*
*/
export function readMetadata(filePath: string): { [key: string]: string } {
try {
const content = fs.readFileSync(path.resolve(filePath), 'utf-8');
const metadata: { [key: string]: string } = {};
const lines = content.split('\n');
let capturing = false;
for (const line of lines) {
if (line.trim() === '// ==Extension==') {
capturing = true;
continue;
}
if (line.trim() === '// ==/Extension==') {
capturing = false;
continue;
}
if (capturing) {
if (line.trim().startsWith('// @')) {
const parts = line.trim().substring(3).split(' ');
const key = parts[0];
const value = parts.slice(1).join(' ');
metadata[key] = value;
}
}
}
return metadata;
} catch (error) {
console.error(`Error reading file: ${error}`);
return {};
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

YesImBot / Athena 是一个 [Koishi](https://koishi.chat/zh-CN/) 插件,旨在让人工智能大模型也能参与到群聊的讨论中。

*新的文档站已上线:[https://yesimbot.ccalliance.tech](https://yesimbot.ccalliance.tech/)*
*新的文档站已上线:[https://athena.mkc.icu/](https://athena.mkc.icu/)*

## 🎹 特性

Expand Down

0 comments on commit dfc3678

Please sign in to comment.