lfqian commited on
Commit
a76972e
·
1 Parent(s): 73e23df

feat: store asset requests in shared JSON file

Browse files

- Create public/data/asset_requests.json for shared storage
- Use Hugging Face API to update file when submitting/voting
- All users can see and vote on asset requests
- Use VITE_HF_TOKEN environment variable for security
- Agent submissions still use localStorage (admin only)

public/data/asset_requests.json ADDED
@@ -0,0 +1 @@
 
 
1
+ []
src/views/AddAssetView.vue CHANGED
@@ -219,7 +219,13 @@ export default {
219
  this.assetForm.message = ''
220
 
221
  try {
222
- // 保存到 localStorage
 
 
 
 
 
 
223
  const assetRequest = {
224
  symbol: this.assetForm.symbol.toUpperCase(),
225
  type: this.assetForm.type,
@@ -228,19 +234,8 @@ export default {
228
  votes: 1
229
  }
230
 
231
- // 获取现有的请求列表
232
- let requests = []
233
- try {
234
- const stored = localStorage.getItem('assetRequests')
235
- if (stored) {
236
- requests = JSON.parse(stored)
237
- }
238
- } catch (e) {
239
- console.error('Error reading localStorage:', e)
240
- }
241
-
242
  // 检查是否已存在相同的 symbol
243
- const existingIndex = requests.findIndex(r => r.symbol.toUpperCase() === assetRequest.symbol.toUpperCase())
244
  if (existingIndex >= 0) {
245
  // 如果已存在,增加投票数
246
  requests[existingIndex].votes += 1
@@ -250,8 +245,37 @@ export default {
250
  requests.push(assetRequest)
251
  }
252
 
253
- // 保存到 localStorage
254
- localStorage.setItem('assetRequests', JSON.stringify(requests))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
255
 
256
  // 延迟跳转,让用户看到动画
257
  await new Promise(resolve => setTimeout(resolve, 500))
 
219
  this.assetForm.message = ''
220
 
221
  try {
222
+ // 读取现有数据
223
+ const response = await fetch('/data/asset_requests.json')
224
+ let requests = []
225
+ if (response.ok) {
226
+ requests = await response.json()
227
+ }
228
+
229
  const assetRequest = {
230
  symbol: this.assetForm.symbol.toUpperCase(),
231
  type: this.assetForm.type,
 
234
  votes: 1
235
  }
236
 
 
 
 
 
 
 
 
 
 
 
 
237
  // 检查是否已存在相同的 symbol
238
+ const existingIndex = requests.findIndex(r => r.symbol === assetRequest.symbol)
239
  if (existingIndex >= 0) {
240
  // 如果已存在,增加投票数
241
  requests[existingIndex].votes += 1
 
245
  requests.push(assetRequest)
246
  }
247
 
248
+ // 使用 Hugging Face API 更新文件
249
+ const token = import.meta.env.VITE_HF_TOKEN || ''
250
+ if (!token) {
251
+ console.warn('HF_TOKEN not configured, asset will not be saved')
252
+ // 仍然跳转到结果页面
253
+ await new Promise(resolve => setTimeout(resolve, 500))
254
+ this.$router.push('/asset-requests')
255
+ return
256
+ }
257
+
258
+ const uploadResponse = await fetch(
259
+ 'https://huggingface.co/api/spaces/TheFinAI/Agent-Market-Arena/upload/main',
260
+ {
261
+ method: 'POST',
262
+ headers: {
263
+ 'Authorization': `Bearer ${token}`,
264
+ 'Content-Type': 'application/json',
265
+ },
266
+ body: JSON.stringify({
267
+ files: [{
268
+ path: 'public/data/asset_requests.json',
269
+ content: JSON.stringify(requests, null, 2)
270
+ }],
271
+ summary: `Add asset request: ${assetRequest.symbol}`
272
+ })
273
+ }
274
+ )
275
+
276
+ if (!uploadResponse.ok) {
277
+ throw new Error('Failed to update file')
278
+ }
279
 
280
  // 延迟跳转,让用户看到动画
281
  await new Promise(resolve => setTimeout(resolve, 500))
src/views/AssetRequestsView.vue CHANGED
@@ -100,12 +100,12 @@ export default {
100
  this.loadVotedAssets()
101
  },
102
  methods: {
103
- loadRequests() {
104
  this.loading = true
105
  try {
106
- const stored = localStorage.getItem('assetRequests')
107
- if (stored) {
108
- const requests = JSON.parse(stored)
109
  // 按投票数排序,取前10个
110
  this.topRequests = requests
111
  .sort((a, b) => b.votes - a.votes)
@@ -129,28 +129,52 @@ export default {
129
  }
130
  },
131
 
132
- voteForAsset(symbol) {
133
  try {
134
- // 获取所有请求
135
- const stored = localStorage.getItem('assetRequests')
136
- if (!stored) return
137
 
138
- const requests = JSON.parse(stored)
139
- const index = requests.findIndex(r => r.symbol.toUpperCase() === symbol.toUpperCase())
140
 
141
  if (index >= 0) {
142
  requests[index].votes += 1
143
  requests[index].timestamp = new Date().toISOString()
144
 
145
- // 保存更新
146
- localStorage.setItem('assetRequests', JSON.stringify(requests))
 
 
 
 
147
 
148
- // 记录投票
149
- this.votedAssets.add(symbol.toUpperCase())
150
- localStorage.setItem('votedAssets', JSON.stringify([...this.votedAssets]))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
 
152
- // 重新加载
153
- this.loadRequests()
 
 
 
 
 
 
154
  }
155
  } catch (e) {
156
  console.error('Error voting:', e)
 
100
  this.loadVotedAssets()
101
  },
102
  methods: {
103
+ async loadRequests() {
104
  this.loading = true
105
  try {
106
+ const response = await fetch('/data/asset_requests.json')
107
+ if (response.ok) {
108
+ const requests = await response.json()
109
  // 按投票数排序,取前10个
110
  this.topRequests = requests
111
  .sort((a, b) => b.votes - a.votes)
 
129
  }
130
  },
131
 
132
+ async voteForAsset(symbol) {
133
  try {
134
+ // 读取现有数据
135
+ const response = await fetch('/data/asset_requests.json')
136
+ if (!response.ok) return
137
 
138
+ const requests = await response.json()
139
+ const index = requests.findIndex(r => r.symbol === symbol)
140
 
141
  if (index >= 0) {
142
  requests[index].votes += 1
143
  requests[index].timestamp = new Date().toISOString()
144
 
145
+ // 使用 Hugging Face API 更新文件
146
+ const token = import.meta.env.VITE_HF_TOKEN || ''
147
+ if (!token) {
148
+ console.warn('HF_TOKEN not configured')
149
+ return
150
+ }
151
 
152
+ const uploadResponse = await fetch(
153
+ 'https://huggingface.co/api/spaces/TheFinAI/Agent-Market-Arena/upload/main',
154
+ {
155
+ method: 'POST',
156
+ headers: {
157
+ 'Authorization': `Bearer ${token}`,
158
+ 'Content-Type': 'application/json',
159
+ },
160
+ body: JSON.stringify({
161
+ files: [{
162
+ path: 'public/data/asset_requests.json',
163
+ content: JSON.stringify(requests, null, 2)
164
+ }],
165
+ summary: `Vote for asset: ${symbol}`
166
+ })
167
+ }
168
+ )
169
 
170
+ if (uploadResponse.ok) {
171
+ // 记录投票
172
+ this.votedAssets.add(symbol)
173
+ localStorage.setItem('votedAssets', JSON.stringify([...this.votedAssets]))
174
+
175
+ // 重新加载
176
+ await this.loadRequests()
177
+ }
178
  }
179
  } catch (e) {
180
  console.error('Error voting:', e)