HerzaJ commited on
Commit
4e93246
·
verified ·
1 Parent(s): e49ea3c

Create copilot.js

Browse files
Files changed (1) hide show
  1. plugins/copilot.js +110 -0
plugins/copilot.js ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const WebSocket = require('ws')
2
+ const axios = require('axios')
3
+
4
+ class Copilot {
5
+ constructor() {
6
+ this.conversationId = null
7
+ this.headers = {
8
+ origin: 'https://copilot.microsoft.com',
9
+ 'user-agent': 'Mozilla/5.0 (Linux; Android 15; SM-F958 Build/AP3A.240905.015) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.86 Mobile Safari/537.36'
10
+ }
11
+ }
12
+
13
+ async createConversation() {
14
+ let { data } = await axios.post('https://copilot.microsoft.com/c/api/conversations', null, { headers: this.headers })
15
+ this.conversationId = data.id
16
+ return this.conversationId
17
+ }
18
+
19
+ async chat(message) {
20
+ if (!this.conversationId) await this.createConversation()
21
+ return new Promise((resolve, reject) => {
22
+ const ws = new WebSocket(`wss://copilot.microsoft.com/c/api/chat?api-version=2&features=-,ncedge,edgepagecontext&setflight=-,ncedge,edgepagecontext&ncedge=1`, { headers: this.headers })
23
+ const response = { text: '', citations: [] }
24
+ ws.on('open', () => {
25
+ ws.send(JSON.stringify({
26
+ event: 'setOptions',
27
+ supportedFeatures: ['partial-generated-images'],
28
+ supportedCards: ['weather', 'local', 'image', 'sports', 'video', 'ads', 'safetyHelpline', 'quiz', 'finance', 'recipe'],
29
+ ads: { supportedTypes: ['text', 'product', 'multimedia', 'tourActivity', 'propertyPromotion'] }
30
+ }))
31
+ ws.send(JSON.stringify({
32
+ event: 'send',
33
+ mode: 'chat',
34
+ conversationId: this.conversationId,
35
+ content: [{ type: 'text', text: message }],
36
+ context: {}
37
+ }))
38
+ })
39
+ ws.on('message', (chunk) => {
40
+ try {
41
+ const parsed = JSON.parse(chunk.toString())
42
+ switch (parsed.event) {
43
+ case 'appendText':
44
+ response.text += parsed.text || ''
45
+ break
46
+ case 'citation':
47
+ response.citations.push({ title: parsed.title, icon: parsed.iconUrl, url: parsed.url })
48
+ break
49
+ case 'done':
50
+ resolve(response)
51
+ ws.close()
52
+ break
53
+ case 'error':
54
+ reject(new Error(parsed.message))
55
+ ws.close()
56
+ break
57
+ }
58
+ } catch (error) {
59
+ reject(error.message)
60
+ }
61
+ })
62
+ ws.on('error', reject)
63
+ })
64
+ }
65
+ }
66
+
67
+ const handler = async (req, res) => {
68
+ try {
69
+ const { text } = req.query
70
+
71
+ if (!text) {
72
+ return res.status(400).json({
73
+ author: 'Herza',
74
+ success: false,
75
+ msg: 'Missing required parameter: text'
76
+ })
77
+ }
78
+
79
+ const copilot = new Copilot()
80
+ const result = await copilot.chat(text)
81
+
82
+ res.json({
83
+ author: 'Herza',
84
+ success: true,
85
+ model: 'copilot',
86
+ msg: result.text.trim(),
87
+ citations: result.citations
88
+ })
89
+
90
+ } catch (error) {
91
+ console.error('Error fetching from Copilot:', error)
92
+ res.status(500).json({
93
+ author: 'Herza',
94
+ success: false,
95
+ msg: 'Terjadi kesalahan saat menghubungi AI.'
96
+ })
97
+ }
98
+ }
99
+
100
+ module.exports = {
101
+ name: 'Copilot AI',
102
+ description: 'Generate responses using Microsoft Copilot',
103
+ type: 'GET',
104
+ routes: ['api/AI/copilot'],
105
+ tags: ['ai', 'copilot'],
106
+ parameters: ['text'],
107
+ enabled: true,
108
+ main: ['AI'],
109
+ handler
110
+ }