File size: 1,913 Bytes
e92be04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { db } from "../config/gun.js";
import { gunSafe } from "../utils/gunUtils.js";
import { broadcastHiveEvent } from "./hiveService.js";

/**
 * Task Bidding Service — Implements an auction-based task allocation system.
 */
export const taskBiddingService = {
    /**
     * Publishes a new task to the network.
     */
    async publishTask(data) {
        const taskId = `task-${Date.now()}`;
        const taskData = gunSafe({
            id: taskId,
            creator: data.agentId,
            description: data.description,
            reward: data.reward || 0,
            requirements: data.requirements || [],
            status: "OPEN",
            timestamp: Date.now()
        });

        db.get("tasks").get(taskId).put(taskData);
        broadcastHiveEvent('task_published', { id: taskId, reward: data.reward });
        return taskId;
    },

    /**
     * Submits a bid for a specific task.
     */
    async submitBid(taskId, agentId, data) {
        const bidId = `bid-${Date.now()}-${agentId}`;
        const bidData = gunSafe({
            agentId,
            offer: data.offer || 0,
            specialty: data.specialty || "General",
            status: "PENDING",
            timestamp: Date.now()
        });

        db.get("tasks").get(taskId).get("bids").get(agentId).put(bidData);
        broadcastHiveEvent('bid_submitted', { taskId, agentId });
        return bidId;
    },

    /**
     * Awards a task to a specific bidder.
     */
    async awardTask(taskId, targetAgentId) {
        db.get("tasks").get(taskId).put(gunSafe({
            status: "AWARDED",
            awardedTo: targetAgentId,
            awardedAt: Date.now()
        }));

        db.get("tasks").get(taskId).get("bids").get(targetAgentId).put(gunSafe({
            status: "ACCEPTED"
        }));

        broadcastHiveEvent('task_awarded', { taskId, awardedTo: targetAgentId });
    }
};