-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathopenresearch.js
More file actions
199 lines (179 loc) · 6.26 KB
/
openresearch.js
File metadata and controls
199 lines (179 loc) · 6.26 KB
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
import { finalizeEvent, generateSecretKey, getPublicKey } from '../lib/esm/pure.js'
import { Relay, useWebSocketImplementation } from '../lib/esm/relay.js'
import {
NewSubspaceCreateEvent,
ValidateSubspaceCreateEvent,
NewSubspaceJoinEvent,
ValidateSubspaceJoinEvent,
toNostrEvent,
setParents,
} from '../lib/esm/cip/subspace.js'
import {
KindSubspaceCreate,
DefaultCommonPrjOps,
DefaultSubspaceOps,
OpenResearchSubspaceOps,
} from '../lib/esm/cip/constants.js'
import {
newPaperEvent,
newAnnotationEvent,
newReviewEvent,
newAiAnalysisEvent,
newDiscussionEvent,
toNostrEvent as toNostrEventOpenResearch,
} from '../lib/esm/cip/cip05/openresearch.js'
import WebSocket from 'ws'
useWebSocketImplementation(WebSocket)
// 0. Connect to the relay for test
const relayURL = 'ws://161.97.129.166:10547'
const relay = await Relay.connect(relayURL)
console.log(`connected to ${relay.url}`)
// Generate keys
let sk = generateSecretKey()
let pk = getPublicKey(sk)
// Subscribe to events
relay.subscribe(
[
{
kinds: [KindSubspaceCreate],
limit: 1,
},
],
{
onevent(event) {
console.log('====================================')
console.log('Subscribe got event:', event)
},
},
)
// 1. Create a new subspace
const subspaceName = 'TestOpenResearch'
const rules = 'Open research collaboration space'
const description = 'A test subspace for open research operations'
const imageURL = 'http://example.com/image.png'
const subspaceEvent = NewSubspaceCreateEvent(
subspaceName,
DefaultCommonPrjOps + ',' + DefaultSubspaceOps + ',' + OpenResearchSubspaceOps,
rules,
description,
imageURL,
)
ValidateSubspaceCreateEvent(subspaceEvent)
// Sign and publish the subspace creation event
const signedSubspaceEvent = finalizeEvent(toNostrEvent(subspaceEvent), sk)
await relay.publish(signedSubspaceEvent)
console.log('====================================')
console.log('Subspace creation event published:', signedSubspaceEvent)
// 2. Join the subspace
const joinEvent = NewSubspaceJoinEvent(subspaceEvent.subspaceID, 'Join')
ValidateSubspaceJoinEvent(joinEvent)
// Sign and publish the subspace join event
const signedJoinEvent = finalizeEvent(toNostrEvent(joinEvent), sk)
await relay.publish(signedJoinEvent)
console.log('====================================')
console.log('Subspace join event published:', signedJoinEvent)
// 3. Create a paper event
const paperEvent = await newPaperEvent(subspaceEvent.subspaceID, '')
if (!paperEvent) {
throw new Error('Failed to create paper event')
}
paperEvent.setPaperInfo(
'10.1234/example.2023',
'research',
['John Doe', 'Jane Smith'],
['AI', 'Machine Learning', 'Research'],
'2023',
'Journal of Example Research',
'Example Research Paper',
'This is an example research paper abstract.',
'https://example.com/paper',
'abc123def456',
)
// Sign and publish the paper event
const signedPaperEvent = finalizeEvent(toNostrEventOpenResearch(paperEvent), sk)
await relay.publish(signedPaperEvent)
console.log('====================================')
console.log('Paper event published:', signedPaperEvent)
// 4. Create an annotation event
const annotationEvent = await newAnnotationEvent(subspaceEvent.subspaceID, '')
if (!annotationEvent) {
throw new Error('Failed to create annotation event')
}
annotationEvent.setAnnotationInfo(
signedPaperEvent.id,
'section1',
'comment',
'',
'This is an interesting point in the paper.',
)
// Sign and publish the annotation event
const signedAnnotationEvent = finalizeEvent(toNostrEventOpenResearch(annotationEvent), sk)
await relay.publish(signedAnnotationEvent)
console.log('====================================')
console.log('Annotation event published:', signedAnnotationEvent)
// 5. Create a review event
const reviewEvent = await newReviewEvent(subspaceEvent.subspaceID, '')
if (!reviewEvent) {
throw new Error('Failed to create review event')
}
reviewEvent.setReviewInfo(
signedPaperEvent.id,
4.5,
{
methodology: 4.5,
results: 4.0,
discussion: 5.0,
},
'Overall, this is a well-written paper with strong methodology.',
'Clear methodology, comprehensive literature review.',
'Could use more discussion on limitations.',
'Consider expanding the discussion section.',
)
// Sign and publish the review event
const signedReviewEvent = finalizeEvent(toNostrEventOpenResearch(reviewEvent), sk)
await relay.publish(signedReviewEvent)
console.log('====================================')
console.log('Review event published:', signedReviewEvent)
// 6. Create an AI analysis event
const aiAnalysisEvent = await newAiAnalysisEvent(subspaceEvent.subspaceID, '')
if (!aiAnalysisEvent) {
throw new Error('Failed to create AI analysis event')
}
aiAnalysisEvent.setAiAnalysisInfo(
'literature_review',
[signedPaperEvent.id],
'Analyze the key contributions and potential impact of this research.',
'The paper presents significant contributions to the field...',
[
'Novel methodology for data analysis',
'Strong experimental validation',
'Clear practical applications',
],
[
'Extend the methodology to other domains',
'Investigate scalability aspects',
'Explore real-world deployment scenarios',
],
)
// Sign and publish the AI analysis event
const signedAiAnalysisEvent = finalizeEvent(toNostrEventOpenResearch(aiAnalysisEvent), sk)
await relay.publish(signedAiAnalysisEvent)
console.log('====================================')
console.log('AI analysis event published:', signedAiAnalysisEvent)
// 7. Create a discussion event
const discussionEvent = await newDiscussionEvent(subspaceEvent.subspaceID, '')
if (!discussionEvent) {
throw new Error('Failed to create discussion event')
}
discussionEvent.setDiscussionInfo(
'Research Impact',
'',
[signedPaperEvent.id],
'Let\'s discuss the potential impact of this research on the field.',
)
// Sign and publish the discussion event
const signedDiscussionEvent = finalizeEvent(toNostrEventOpenResearch(discussionEvent), sk)
await relay.publish(signedDiscussionEvent)
console.log('====================================')
console.log('Discussion event published:', signedDiscussionEvent)
relay.close()