forked from EduCraft/curriculum-project-hub
fix: label Feishu trigger senders in agent prompts
This commit is contained in:
+20
-14
@@ -187,6 +187,7 @@ export function makeTriggerHandler(deps: TriggerDeps): TriggerHandler {
|
||||
projectId,
|
||||
workspaceDir: project.workspaceDir,
|
||||
rt,
|
||||
senderOpenId,
|
||||
});
|
||||
const promptForAgent = withFeishuTriggerContext(agentPrompt, feishuTriggerContext);
|
||||
|
||||
@@ -241,7 +242,7 @@ export function makeTriggerHandler(deps: TriggerDeps): TriggerHandler {
|
||||
model,
|
||||
prompt: agentPrompt.slice(0, 200),
|
||||
sender: await senderAuditMetadata(rt, senderOpenId),
|
||||
...(feishuTriggerContext === null ? {} : { feishuTriggerContext }),
|
||||
feishuTriggerContext,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -836,18 +837,18 @@ interface BuildFeishuTriggerContextArgs {
|
||||
readonly projectId: string;
|
||||
readonly workspaceDir: string;
|
||||
readonly rt: FeishuRuntime;
|
||||
readonly senderOpenId: string;
|
||||
}
|
||||
|
||||
async function buildFeishuTriggerContext(args: BuildFeishuTriggerContextArgs): Promise<Prisma.InputJsonObject | null> {
|
||||
async function buildFeishuTriggerContext(args: BuildFeishuTriggerContextArgs): Promise<Prisma.InputJsonObject> {
|
||||
const replyToMessageId = nonEmpty(args.msg.parent_id);
|
||||
const rootId = nonEmpty(args.msg.root_id);
|
||||
const threadId = nonEmpty(args.msg.thread_id);
|
||||
if (replyToMessageId === undefined && rootId === undefined && threadId === undefined) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const context: Record<string, Prisma.InputJsonValue> = {
|
||||
trigger_message_id: args.msg.message_id,
|
||||
chat_id: args.chatId,
|
||||
sender: await senderAuditMetadata(args.rt, args.senderOpenId),
|
||||
};
|
||||
if (replyToMessageId !== undefined) context["reply_to_message_id"] = replyToMessageId;
|
||||
if (rootId !== undefined) context["root_id"] = rootId;
|
||||
@@ -878,24 +879,29 @@ function parseJsonForMetadata(text: string): Prisma.InputJsonValue {
|
||||
}
|
||||
}
|
||||
|
||||
function withFeishuTriggerContext(prompt: string, context: Prisma.InputJsonObject | null): string {
|
||||
if (context === null) return prompt;
|
||||
return `Feishu trigger context:\n${JSON.stringify(context, null, 2)}\n\nUser request:\n${prompt}`;
|
||||
function withFeishuTriggerContext(prompt: string, context: Prisma.InputJsonObject): string {
|
||||
return `Feishu trigger context:\n${JSON.stringify(context, null, 2)}\n\nUser request from ${senderLabelFromContext(context)}:\n${prompt}`;
|
||||
}
|
||||
|
||||
function agentRunMetadata(
|
||||
roleId: string,
|
||||
rawPrompt: string,
|
||||
feishuTriggerContext: Prisma.InputJsonObject | null,
|
||||
feishuTriggerContext: Prisma.InputJsonObject,
|
||||
): Prisma.InputJsonObject {
|
||||
const metadata: Record<string, Prisma.InputJsonValue> = { roleId };
|
||||
if (feishuTriggerContext !== null) {
|
||||
metadata["rawPrompt"] = rawPrompt;
|
||||
metadata["feishuTriggerContext"] = feishuTriggerContext;
|
||||
}
|
||||
const metadata: Record<string, Prisma.InputJsonValue> = { roleId, rawPrompt, feishuTriggerContext };
|
||||
return metadata as Prisma.InputJsonObject;
|
||||
}
|
||||
|
||||
function senderLabelFromContext(context: Prisma.InputJsonObject): string {
|
||||
const sender = context["sender"];
|
||||
if (typeof sender !== "object" || sender === null || Array.isArray(sender)) return "unknown sender";
|
||||
const senderObject = sender as Record<string, unknown>;
|
||||
const name = senderObject["name"];
|
||||
if (typeof name === "string" && name.trim() !== "") return name.trim();
|
||||
const openId = senderObject["open_id"];
|
||||
return typeof openId === "string" && openId.trim() !== "" ? openId.trim() : "unknown sender";
|
||||
}
|
||||
|
||||
function auditDecision(decision: AuthorizationDecision): Record<string, unknown> {
|
||||
return {
|
||||
allowed: decision.allowed,
|
||||
|
||||
Reference in New Issue
Block a user