-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.js
More file actions
54 lines (51 loc) · 1.79 KB
/
Copy pathprotocol.js
File metadata and controls
54 lines (51 loc) · 1.79 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
// ==========================================
// Agent Protocol — Structured AI-to-AI messages
// ==========================================
// When two AIs talk, they wrap messages in this envelope
// so any AI (regardless of provider) can understand the intent.
/**
* Create an agent-protocol message envelope.
*/
export function createAgentMessage({ from, to, intent, payload, conversationId, replyTo, group }) {
const msg = {
protocol: 'whatsapp-ai-network',
version: '1.0',
timestamp: new Date().toISOString(),
conversationId: conversationId || crypto.randomUUID(),
from, // { agentId, agentName, phone }
to, // { agentId, agentName, phone } OR { groupId, groupName }
intent, // string: "chat" | "task-request" | "task-response" | "negotiate" | "info" | "group-invite" | "group-chat" | ...
payload, // any — the actual content
replyTo, // optional conversationId this is replying to
};
if (group) msg.group = group; // { groupId, groupName, memberCount }
return msg;
}
/**
* Detect whether an incoming WhatsApp message is an AI-protocol message
* (JSON with our protocol header) or a plain human message.
*/
export function parseIncoming(text) {
try {
const parsed = JSON.parse(text);
if (parsed.protocol === 'whatsapp-ai-network') {
return { type: 'agent', message: parsed };
}
} catch {
// not JSON — treat as human message
}
return { type: 'human', message: text };
}
/**
* Wrap a simple text reply in protocol format.
*/
export function createReply(originalMessage, fromAgent, payload) {
return createAgentMessage({
from: fromAgent,
to: originalMessage.from,
intent: 'task-response',
payload,
conversationId: originalMessage.conversationId,
replyTo: originalMessage.conversationId,
});
}