Spaces:
Running
Running
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 +1 -0
- src/views/AddAssetView.vue +39 -15
- src/views/AssetRequestsView.vue +41 -17
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 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 244 |
if (existingIndex >= 0) {
|
| 245 |
// 如果已存在,增加投票数
|
| 246 |
requests[existingIndex].votes += 1
|
|
@@ -250,8 +245,37 @@ export default {
|
|
| 250 |
requests.push(assetRequest)
|
| 251 |
}
|
| 252 |
|
| 253 |
-
//
|
| 254 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 107 |
-
if (
|
| 108 |
-
const requests =
|
| 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
|
| 136 |
-
if (!
|
| 137 |
|
| 138 |
-
const requests =
|
| 139 |
-
const index = requests.findIndex(r => r.symbol
|
| 140 |
|
| 141 |
if (index >= 0) {
|
| 142 |
requests[index].votes += 1
|
| 143 |
requests[index].timestamp = new Date().toISOString()
|
| 144 |
|
| 145 |
-
//
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
-
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|