mycelium-js 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +87 -0
- package/mycelium.js +60 -0
- package/package.json +21 -0
- package/sdk-js/index.html +43 -0
- package/test.js +44 -0
package/index.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 🍄 US Neural: Mycelium JavaScript SDK
|
|
3
|
+
* Drop-in semantic routing for Node.js and Browser AI apps.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export class MyceliumClient {
|
|
7
|
+
constructor(baseUrl = "http://127.0.0.1:8000/api/v1") {
|
|
8
|
+
this.baseUrl = baseUrl;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Register a tool or agent to the Mycelium Semantic Registry.
|
|
13
|
+
*/
|
|
14
|
+
async registerTool(agentId, name, description, tags = []) {
|
|
15
|
+
try {
|
|
16
|
+
const response = await fetch(`${this.baseUrl}/agents/register`, {
|
|
17
|
+
method: "POST",
|
|
18
|
+
headers: { "Content-Type": "application/json" },
|
|
19
|
+
body: JSON.stringify({
|
|
20
|
+
agent_id: agentId,
|
|
21
|
+
name: name,
|
|
22
|
+
description: description,
|
|
23
|
+
tags: tags
|
|
24
|
+
})
|
|
25
|
+
});
|
|
26
|
+
return await response.json();
|
|
27
|
+
} catch (error) {
|
|
28
|
+
console.error(`🍄 [Mycelium Error] Registration failed: ${error.message}`);
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Discover the best tool based on pure user intent.
|
|
35
|
+
*/
|
|
36
|
+
async discoverTool(userQuery, minScore = 0.15) {
|
|
37
|
+
try {
|
|
38
|
+
const url = new URL(`${this.baseUrl}/agents/discover`);
|
|
39
|
+
url.searchParams.append("q", userQuery);
|
|
40
|
+
url.searchParams.append("semantic", "true");
|
|
41
|
+
url.searchParams.append("limit", "1");
|
|
42
|
+
|
|
43
|
+
const response = await fetch(url);
|
|
44
|
+
const data = await response.json();
|
|
45
|
+
|
|
46
|
+
if (data.agents && data.agents.length > 0) {
|
|
47
|
+
const topAgent = data.agents[0];
|
|
48
|
+
if ((topAgent._similarity_score || 0) >= minScore) {
|
|
49
|
+
return topAgent;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return null;
|
|
53
|
+
} catch (error) {
|
|
54
|
+
console.error(`🍄 [Mycelium Error] Discovery failed: ${error.message}`);
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The Magic Function: Matches semantic intent with actual JS functions/tools.
|
|
61
|
+
*/
|
|
62
|
+
async routeAndExecute(userQuery, availableTools) {
|
|
63
|
+
const bestToolMeta = await this.discoverTool(userQuery);
|
|
64
|
+
|
|
65
|
+
if (!bestToolMeta) {
|
|
66
|
+
return { error: "No suitable tool found for your request." };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const targetToolName = bestToolMeta.name;
|
|
70
|
+
const confidence = bestToolMeta._similarity_score;
|
|
71
|
+
|
|
72
|
+
// Find the matching tool in the JS array
|
|
73
|
+
const tool = availableTools.find(
|
|
74
|
+
(t) => t.name.toLowerCase() === targetToolName.toLowerCase()
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
if (tool && typeof tool.execute === 'function') {
|
|
78
|
+
return {
|
|
79
|
+
toolName: targetToolName,
|
|
80
|
+
confidence: confidence,
|
|
81
|
+
result: await tool.execute(userQuery)
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return { error: `Tool '${targetToolName}' discovered but not loaded in memory.` };
|
|
86
|
+
}
|
|
87
|
+
}
|
package/mycelium.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mycelium JavaScript SDK v0.1.0
|
|
3
|
+
* The networking protocol for AI agents.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
class MyceliumClient {
|
|
7
|
+
constructor(baseUrl = "https://usaai-us-neural-registry.hf.space") {
|
|
8
|
+
this.baseUrl = baseUrl;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Discover agents using semantic search
|
|
13
|
+
*/
|
|
14
|
+
async discover(query, limit = 5) {
|
|
15
|
+
try {
|
|
16
|
+
const url = `${this.baseUrl}/api/v1/discover?q=${encodeURIComponent(query)}&limit=${limit}`;
|
|
17
|
+
const response = await fetch(url);
|
|
18
|
+
const data = await response.json();
|
|
19
|
+
return data.results || data.agents || [];
|
|
20
|
+
} catch (error) {
|
|
21
|
+
console.error("🍄 [Mycelium] Discovery failed:", error);
|
|
22
|
+
return [];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Send a message to another agent
|
|
28
|
+
*/
|
|
29
|
+
async sendMessage(toAgentId, capability, inputs = {}) {
|
|
30
|
+
const payload = {
|
|
31
|
+
envelope: {
|
|
32
|
+
to_agent: toAgentId,
|
|
33
|
+
message_type: "request",
|
|
34
|
+
protocol_version: "0.2.0",
|
|
35
|
+
timestamp: new Date().toISOString()
|
|
36
|
+
},
|
|
37
|
+
payload: {
|
|
38
|
+
capability: capability,
|
|
39
|
+
inputs: inputs
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
const response = await fetch(`${this.baseUrl}/api/v1/messages/send`, {
|
|
45
|
+
method: 'POST',
|
|
46
|
+
headers: { 'Content-Type': 'application/json' },
|
|
47
|
+
body: JSON.stringify(payload)
|
|
48
|
+
});
|
|
49
|
+
return await response.json();
|
|
50
|
+
} catch (error) {
|
|
51
|
+
console.error("🍄 [Mycelium] Message relay failed:", error);
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Export for Node.js or Browser
|
|
58
|
+
if (typeof module !== 'undefined' && module.exports) {
|
|
59
|
+
module.exports = { MyceliumClient };
|
|
60
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mycelium-js",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "US Neural: Semantic Tool Registry SDK for JavaScript",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "node test.js"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [
|
|
11
|
+
"ai",
|
|
12
|
+
"agents",
|
|
13
|
+
"semantic",
|
|
14
|
+
"routing",
|
|
15
|
+
"usneural",
|
|
16
|
+
"llm",
|
|
17
|
+
"orchestration"
|
|
18
|
+
],
|
|
19
|
+
"author": "US Neural",
|
|
20
|
+
"license": "MIT"
|
|
21
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<title>Mycelium JS Test</title>
|
|
5
|
+
<style>
|
|
6
|
+
body { background: #0f172a; color: white; font-family: sans-serif; padding: 40px; }
|
|
7
|
+
#results { background: #1e293b; padding: 20px; border-radius: 8px; margin-top: 20px; }
|
|
8
|
+
.agent { color: #38bdf8; font-weight: bold; }
|
|
9
|
+
</style>
|
|
10
|
+
</head>
|
|
11
|
+
<body>
|
|
12
|
+
<h1>🍄 Mycelium JS Explorer</h1>
|
|
13
|
+
<input type="text" id="query" placeholder="Search capability (e.g. weather)..." style="padding: 10px; width: 300px;">
|
|
14
|
+
<button onclick="search()" style="padding: 10px 20px; cursor: pointer;">Discover</button>
|
|
15
|
+
|
|
16
|
+
<div id="results">Enter a query to find agents on the global network...</div>
|
|
17
|
+
|
|
18
|
+
<script src="mycelium.js"></script>
|
|
19
|
+
<script>
|
|
20
|
+
const client = new MyceliumClient();
|
|
21
|
+
|
|
22
|
+
async function search() {
|
|
23
|
+
const q = document.getElementById('query').value;
|
|
24
|
+
const resDiv = document.getElementById('results');
|
|
25
|
+
resDiv.innerHTML = "Searching global swarm...";
|
|
26
|
+
|
|
27
|
+
const agents = await client.discover(q);
|
|
28
|
+
|
|
29
|
+
if (agents.length === 0) {
|
|
30
|
+
resDiv.innerHTML = "❌ No agents found.";
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
resDiv.innerHTML = agents.map(a => `
|
|
35
|
+
<div style="margin-bottom: 15px;">
|
|
36
|
+
<span class="agent">${a.name}</span> (${a.agent_id})<br>
|
|
37
|
+
<small style="color: #94a3b8;">Caps: ${JSON.stringify(a.capabilities.map(c => c.name || c))}</small>
|
|
38
|
+
</div>
|
|
39
|
+
`).join('');
|
|
40
|
+
}
|
|
41
|
+
</script>
|
|
42
|
+
</body>
|
|
43
|
+
</html>
|
package/test.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { MyceliumClient } from './index.js';
|
|
2
|
+
|
|
3
|
+
async function runTest() {
|
|
4
|
+
console.log("🍄 Starting US Neural JS SDK Test...\n");
|
|
5
|
+
const client = new MyceliumClient();
|
|
6
|
+
|
|
7
|
+
// 1. Define our JS Tools
|
|
8
|
+
const tools = [
|
|
9
|
+
{
|
|
10
|
+
name: "ForexAgent",
|
|
11
|
+
description: "Convert currency and get exchange rates",
|
|
12
|
+
execute: async (q) => "EUR/USD is 1.09 today."
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
name: "WeatherBot",
|
|
16
|
+
description: "Get local climate info and temperature",
|
|
17
|
+
execute: async (q) => "It's 22°C and sunny."
|
|
18
|
+
}
|
|
19
|
+
];
|
|
20
|
+
|
|
21
|
+
// 2. Register them to the local mesh
|
|
22
|
+
console.log("Registering JS tools to Mycelium...");
|
|
23
|
+
for (const tool of tools) {
|
|
24
|
+
await client.registerTool(tool.name.toLowerCase(), tool.name, tool.description);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// 3. Test Semantic Routing via JS
|
|
28
|
+
const query = "how many euros will I get for my dollars?";
|
|
29
|
+
console.log(`\nUser Intent: '${query}'`);
|
|
30
|
+
|
|
31
|
+
const startTime = performance.now();
|
|
32
|
+
const response = await client.routeAndExecute(query, tools);
|
|
33
|
+
const latency = performance.now() - startTime;
|
|
34
|
+
|
|
35
|
+
if (response.toolName) {
|
|
36
|
+
console.log(`✅ Routed to: ${response.toolName} (Confidence: ${(response.confidence * 100).toFixed(1)}%)`);
|
|
37
|
+
console.log(`⚡ Latency: ${latency.toFixed(2)} ms`);
|
|
38
|
+
console.log(`📤 Tool Output: ${response.result}`);
|
|
39
|
+
} else {
|
|
40
|
+
console.log(`❌ Failed: ${response.error}`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
runTest();
|