Mishig commited on
Commit
e9f817c
1 Parent(s): 4ade85f

[Assistnats] improve reporting (#797)

Browse files

* [Assistnats] improve reporting

* typo

* make it more general

* typo

* update prodEnv script

* Update scripts/updateProdEnv.ts

.env CHANGED
@@ -133,4 +133,6 @@ EXPOSE_API=true
133
 
134
  ENABLE_ASSISTANTS=false #set to true to enable assistants feature
135
 
136
- ALTERNATIVE_REDIRECT_URLS=`[]` #valide alternative redirect URL for OAuth
 
 
 
133
 
134
  ENABLE_ASSISTANTS=false #set to true to enable assistants feature
135
 
136
+ ALTERNATIVE_REDIRECT_URLS=`[]` #valide alternative redirect URL for OAuth
137
+
138
+ WEBHOOK_URL_REPORT_ASSISTANT=#provide webhook url to get notified when an assistant gets reported
scripts/updateProdEnv.ts CHANGED
@@ -6,6 +6,7 @@ const SERPER_API_KEY = process.env.SERPER_API_KEY;
6
  const OPENID_CONFIG = process.env.OPENID_CONFIG;
7
  const MONGODB_URL = process.env.MONGODB_URL;
8
  const HF_TOKEN = process.env.HF_TOKEN ?? process.env.HF_ACCESS_TOKEN; // token used for API requests in prod
 
9
 
10
  // Read the content of the file .env.template
11
  const PUBLIC_CONFIG = fs.readFileSync(".env.template", "utf8");
@@ -16,6 +17,7 @@ MONGODB_URL=${MONGODB_URL}
16
  OPENID_CONFIG=${OPENID_CONFIG}
17
  SERPER_API_KEY=${SERPER_API_KEY}
18
  HF_TOKEN=${HF_TOKEN}
 
19
  `;
20
 
21
  // Make an HTTP POST request to add the space secrets
 
6
  const OPENID_CONFIG = process.env.OPENID_CONFIG;
7
  const MONGODB_URL = process.env.MONGODB_URL;
8
  const HF_TOKEN = process.env.HF_TOKEN ?? process.env.HF_ACCESS_TOKEN; // token used for API requests in prod
9
+ const WEBHOOK_URL_REPORT_ASSISTANT = process.env.WEBHOOK_URL_REPORT_ASSISTANT; // slack webhook url used to get "report assistant" events
10
 
11
  // Read the content of the file .env.template
12
  const PUBLIC_CONFIG = fs.readFileSync(".env.template", "utf8");
 
17
  OPENID_CONFIG=${OPENID_CONFIG}
18
  SERPER_API_KEY=${SERPER_API_KEY}
19
  HF_TOKEN=${HF_TOKEN}
20
+ WEBHOOK_URL_REPORT_ASSISTANT=${WEBHOOK_URL_REPORT_ASSISTANT}
21
  `;
22
 
23
  // Make an HTTP POST request to add the space secrets
src/routes/settings/assistants/[assistantId]/+page.server.ts CHANGED
@@ -3,6 +3,8 @@ import { type Actions, fail, redirect } from "@sveltejs/kit";
3
  import { ObjectId } from "mongodb";
4
  import { authCondition } from "$lib/server/auth";
5
  import { base } from "$app/paths";
 
 
6
 
7
  async function assistantOnlyIfAuthor(locals: App.Locals, assistantId?: string) {
8
  const assistant = await collections.assistants.findOne({ _id: new ObjectId(assistantId) });
@@ -49,7 +51,7 @@ export const actions: Actions = {
49
 
50
  throw redirect(302, `${base}/settings`);
51
  },
52
- report: async ({ params, locals }) => {
53
  // is there already a report from this user for this model ?
54
  const report = await collections.reports.findOne({
55
  assistantId: new ObjectId(params.assistantId),
@@ -71,6 +73,31 @@ export const actions: Actions = {
71
  if (!acknowledged) {
72
  return fail(500, { error: true, message: "Failed to report assistant" });
73
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  return { from: "report", ok: true, message: "Assistant reported" };
75
  },
76
 
 
3
  import { ObjectId } from "mongodb";
4
  import { authCondition } from "$lib/server/auth";
5
  import { base } from "$app/paths";
6
+ import { PUBLIC_ORIGIN, PUBLIC_SHARE_PREFIX } from "$env/static/public";
7
+ import { WEBHOOK_URL_REPORT_ASSISTANT } from "$env/static/private";
8
 
9
  async function assistantOnlyIfAuthor(locals: App.Locals, assistantId?: string) {
10
  const assistant = await collections.assistants.findOne({ _id: new ObjectId(assistantId) });
 
51
 
52
  throw redirect(302, `${base}/settings`);
53
  },
54
+ report: async ({ params, locals, url }) => {
55
  // is there already a report from this user for this model ?
56
  const report = await collections.reports.findOne({
57
  assistantId: new ObjectId(params.assistantId),
 
73
  if (!acknowledged) {
74
  return fail(500, { error: true, message: "Failed to report assistant" });
75
  }
76
+
77
+ if (WEBHOOK_URL_REPORT_ASSISTANT) {
78
+ const prefixUrl = PUBLIC_SHARE_PREFIX || `${PUBLIC_ORIGIN || url.origin}${base}`;
79
+ const assistantUrl = `${prefixUrl}/assistant/${params.assistantId}`;
80
+
81
+ const assistant = await collections.assistants.findOne(
82
+ { _id: new ObjectId(params.assistantId) },
83
+ { projection: { name: 1 } }
84
+ );
85
+
86
+ const res = await fetch(WEBHOOK_URL_REPORT_ASSISTANT, {
87
+ method: "POST",
88
+ headers: {
89
+ "Content-type": "application/json",
90
+ },
91
+ body: JSON.stringify({
92
+ text: `Assistant <${assistantUrl}|${assistant?.name}> reported by <http://hf.co/${locals.user?.username}|${locals.user?.username}>`,
93
+ }),
94
+ });
95
+
96
+ if (!res.ok) {
97
+ console.error(`Webhook assistant report failed. ${res.statusText} ${res.text}`);
98
+ }
99
+ }
100
+
101
  return { from: "report", ok: true, message: "Assistant reported" };
102
  },
103