-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
203 lines (185 loc) · 7.29 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import express, { Express, Request, Response , Application } from 'express';
import cors from 'cors';
import dotenv from 'dotenv';
import { AgentActionPublicType, DataSourceViewType, DustAPI, isRetrievalActionType } from '@dust-tt/client';
import { DustWorkSpaceID, DustApiKey, DustAssistantID } from './config/dust_secrets';
const CONVERSATION_CONTEXT = {
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone || "UTC",
username: "xxxxxxxx", // FIXME
email: "[email protected]", // FIXME
fullName: "xxxxxxxx", // FIXME
profilePictureUrl: null,
origin: null, //"web",
//type: "super admin"
};
// see https://github.com/raycast/extensions/blob/main/extensions/dust-tt/src/utils.tsx
const DUST_AGENT = { // AgentType
sId: "dust",
name: "Dust",
description: "An assistant with context on your company data.",
}
//For env File
dotenv.config();
const app: Application = express();
const port = process.env.PORT || 5000;
interface RequestParams {}
interface ResponseBody {}
interface RequestBody {}
interface RequestQuery {
q: string;
}
const ORIGIN = 'http://bellerophon.localhost.hvbrt.com'
const corsOptions = {
credentials: true,
//origin: true,
origin: ORIGIN,
optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
}
app.use(cors(corsOptions))
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', ORIGIN);
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', "true");
next();
});
app.get('/', async (req: Request, res: Response) => {
console.log("request", req);
let msg = "Hello\n"
msg += `you ask "${req.query['q']}"`
res.send(msg)
})
app.get('/test', async (
req: Request<RequestParams, ResponseBody, RequestBody, RequestQuery>,
res: Response
) => {
let question = req.query.q || "" as string;
// hard coded user name for demo purpose, should be passed in the call
question = "You are replaying to 'John Doe' who is a client of Hivebrite." + question
question = "Please tell me," + question + ". Also please format your response in markdown style, but don't mention the word markdown"
const api = new DustAPI(
{
url: 'https://dust.tt',
nodeEnv: 'production',
},
{
apiKey: DustApiKey,
workspaceId: DustWorkSpaceID,
},
console,
)
let answer = "";
try {
const r = await api.createConversation({
title: null,
visibility: "unlisted",
message: {
content: question,
mentions: [
{
configurationId: DustAssistantID, //DUST_AGENT.sId,
},
],
context: CONVERSATION_CONTEXT,
},
})
if (r.isErr()) {
throw(r);
}
const { conversation, message } = r.value;
if (!conversation || !message) {
throw("Dust API error: conversation or message is missing")
}
try {
const r = await api.streamAgentAnswerEvents({
conversation,
userMessageId: message.sId
//AbortSignal
})
if (r.isErr()) {
throw new Error(r.error.message);
}
const { eventStream } = r.value;
let action: AgentActionPublicType | undefined = undefined;
const chainOfThought: {
tokens: string;
timestamp: number;
}[] = [];
for await (const event of eventStream) {
if (!event) {
continue;
}
switch (event.type) {
case "user_message_error": {
console.error(`User message error: code: ${event.error.code} message: ${event.error.message}`);
throw(`**User message error** ${event.error.message}`);
}
case "agent_error": {
console.error(`Agent message error: code: ${event.error.code} message: ${event.error.message}`);
throw(`**Dust API error** ${event.error.message}`);
}
case "agent_action_success": {
console.log("agent_action_success");
action = event.action;
break;
}
// case "generation_tokens": {
// console.log("generation_tokens");
// if (event.classification === "tokens") {
// console.log("-> tokens", event.text);
// answer = (answer + event.text).trim();
// //const dustAnswer = processAction({ content: answer, action, setDustDocuments });
// //setDustAnswer(dustAnswer + "...");
// } else if (event.classification === "chain_of_thought") {
// console.log("-> chain_of_thought");
// // nothing to do here
// // chainOfThought.push({ tokens: event.text, timestamp: event.created });
// // const thinking = chainOfThought.map((c) => c.tokens).join("");
// // const recent = thinking.slice(-60);
// // showToast({
// // style: Toast.Style.Animated,
// // title: `@${agent.name} is thinking...`,
// // message: recent,
// // });
// }
// break;
// }
case "agent_message_success": {
console.log("agent_message_success", event.message.content);
answer = event.message.content || "";
// answer = processAction({ content: event.message.content ?? "", action, setDustDocuments });
// showToast({
// style: Toast.Style.Success,
// title: `@${agent.name} answered your question`,
// message: question,
// });
// setDustAnswer(answer);
// await addDustHistory({
// conversationId: conversation.sId,
// question: question,
// answer: answer,
// date: new Date(),
// agent: agent.name,
// });
break;
}
default:
// Nothing to do on unsupported events
}
}
} catch(error) {
console.log("streamAgentAnswerEvents error", error);
throw(error)
}
} catch(error) {
console.log("main error:", error)
res.send('Err');
}
console.log("response sent");
res.setHeader('Access-Control-Allow-Credentials', 'true');
res.setHeader('Content-Type', 'application/json');
res.json({ answer: answer});
});
app.listen(port, () => {
console.log(`Server is Fire at http://localhost:${port}`);
});