@kadi.build/core 0.0.1-alpha.3 → 0.0.1-alpha.5
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/README.md +754 -606
- package/dist/KadiClient.d.ts +440 -0
- package/dist/KadiClient.d.ts.map +1 -0
- package/dist/KadiClient.js +1518 -0
- package/dist/KadiClient.js.map +1 -0
- package/dist/errors/error-codes.d.ts +215 -0
- package/dist/errors/error-codes.d.ts.map +1 -0
- package/dist/errors/error-codes.js +295 -0
- package/dist/errors/error-codes.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +24 -0
- package/dist/index.js.map +1 -0
- package/dist/loadAbility.d.ts +106 -0
- package/dist/loadAbility.d.ts.map +1 -0
- package/dist/loadAbility.js +376 -0
- package/dist/loadAbility.js.map +1 -0
- package/dist/messages/BrokerMessages.d.ts +84 -0
- package/dist/messages/BrokerMessages.d.ts.map +1 -0
- package/dist/messages/BrokerMessages.js +125 -0
- package/dist/messages/BrokerMessages.js.map +1 -0
- package/dist/messages/MessageBuilder.d.ts +83 -0
- package/dist/messages/MessageBuilder.d.ts.map +1 -0
- package/dist/messages/MessageBuilder.js +144 -0
- package/dist/messages/MessageBuilder.js.map +1 -0
- package/dist/schemas/events.schemas.d.ts +177 -0
- package/dist/schemas/events.schemas.d.ts.map +1 -0
- package/dist/schemas/events.schemas.js +265 -0
- package/dist/schemas/events.schemas.js.map +1 -0
- package/dist/schemas/index.d.ts +3 -0
- package/dist/schemas/index.d.ts.map +1 -0
- package/dist/schemas/index.js +4 -0
- package/dist/schemas/index.js.map +1 -0
- package/dist/schemas/kadi.schemas.d.ts +70 -0
- package/dist/schemas/kadi.schemas.d.ts.map +1 -0
- package/dist/schemas/kadi.schemas.js +120 -0
- package/dist/schemas/kadi.schemas.js.map +1 -0
- package/dist/transports/BrokerTransport.d.ts +106 -0
- package/dist/transports/BrokerTransport.d.ts.map +1 -0
- package/dist/transports/BrokerTransport.js +177 -0
- package/dist/transports/BrokerTransport.js.map +1 -0
- package/dist/transports/NativeTransport.d.ts +82 -0
- package/dist/transports/NativeTransport.d.ts.map +1 -0
- package/dist/transports/NativeTransport.js +263 -0
- package/dist/transports/NativeTransport.js.map +1 -0
- package/dist/transports/StdioTransport.d.ts +112 -0
- package/dist/transports/StdioTransport.d.ts.map +1 -0
- package/dist/transports/StdioTransport.js +445 -0
- package/dist/transports/StdioTransport.js.map +1 -0
- package/dist/transports/Transport.d.ts +93 -0
- package/dist/transports/Transport.d.ts.map +1 -0
- package/dist/transports/Transport.js +13 -0
- package/dist/transports/Transport.js.map +1 -0
- package/dist/types/broker.d.ts +31 -0
- package/dist/types/broker.d.ts.map +1 -0
- package/dist/types/broker.js +6 -0
- package/dist/types/broker.js.map +1 -0
- package/dist/types/core.d.ts +139 -0
- package/dist/types/core.d.ts.map +1 -0
- package/dist/types/core.js +26 -0
- package/dist/types/core.js.map +1 -0
- package/dist/types/events.d.ts +186 -0
- package/dist/types/events.d.ts.map +1 -0
- package/dist/types/events.js +16 -0
- package/dist/types/events.js.map +1 -0
- package/dist/types/index.d.ts +9 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +13 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/protocol.d.ts +160 -0
- package/dist/types/protocol.d.ts.map +1 -0
- package/dist/types/protocol.js +5 -0
- package/dist/types/protocol.js.map +1 -0
- package/dist/utils/agentUtils.d.ts +187 -0
- package/dist/utils/agentUtils.d.ts.map +1 -0
- package/dist/utils/agentUtils.js +185 -0
- package/dist/utils/agentUtils.js.map +1 -0
- package/dist/utils/commandUtils.d.ts +45 -0
- package/dist/utils/commandUtils.d.ts.map +1 -0
- package/dist/utils/commandUtils.js +145 -0
- package/dist/utils/commandUtils.js.map +1 -0
- package/dist/utils/configUtils.d.ts +55 -0
- package/dist/utils/configUtils.d.ts.map +1 -0
- package/dist/utils/configUtils.js +100 -0
- package/dist/utils/configUtils.js.map +1 -0
- package/dist/utils/logger.d.ts +59 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/logger.js +122 -0
- package/dist/utils/logger.js.map +1 -0
- package/dist/utils/pathUtils.d.ts +48 -0
- package/dist/utils/pathUtils.d.ts.map +1 -0
- package/dist/utils/pathUtils.js +128 -0
- package/dist/utils/pathUtils.js.map +1 -0
- package/package.json +56 -5
- package/agent.json +0 -18
- package/examples/example-abilities/echo-js/README.md +0 -131
- package/examples/example-abilities/echo-js/agent.json +0 -63
- package/examples/example-abilities/echo-js/package.json +0 -24
- package/examples/example-abilities/echo-js/service.js +0 -43
- package/examples/example-abilities/hash-go/agent.json +0 -53
- package/examples/example-abilities/hash-go/cmd/hash_ability/main.go +0 -340
- package/examples/example-abilities/hash-go/go.mod +0 -3
- package/examples/example-agent/abilities/echo-js/0.0.1/README.md +0 -131
- package/examples/example-agent/abilities/echo-js/0.0.1/agent.json +0 -63
- package/examples/example-agent/abilities/echo-js/0.0.1/package-lock.json +0 -93
- package/examples/example-agent/abilities/echo-js/0.0.1/package.json +0 -24
- package/examples/example-agent/abilities/echo-js/0.0.1/service.js +0 -41
- package/examples/example-agent/abilities/hash-go/0.0.1/agent.json +0 -53
- package/examples/example-agent/abilities/hash-go/0.0.1/bin/hash_ability +0 -0
- package/examples/example-agent/abilities/hash-go/0.0.1/cmd/hash_ability/main.go +0 -340
- package/examples/example-agent/abilities/hash-go/0.0.1/go.mod +0 -3
- package/examples/example-agent/agent.json +0 -39
- package/examples/example-agent/index.js +0 -102
- package/examples/example-agent/package-lock.json +0 -93
- package/examples/example-agent/package.json +0 -17
- package/src/KadiAbility.js +0 -478
- package/src/index.js +0 -65
- package/src/loadAbility.js +0 -1086
- package/src/servers/BaseRpcServer.js +0 -404
- package/src/servers/BrokerRpcServer.js +0 -776
- package/src/servers/StdioRpcServer.js +0 -360
- package/src/transport/BrokerMessageBuilder.js +0 -377
- package/src/transport/IpcMessageBuilder.js +0 -1229
- package/src/utils/agentUtils.js +0 -137
- package/src/utils/commandUtils.js +0 -64
- package/src/utils/configUtils.js +0 -72
- package/src/utils/logger.js +0 -161
- package/src/utils/pathUtils.js +0 -86
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
# Echo-JS Ability
|
|
2
|
-
|
|
3
|
-
A simple echo ability that demonstrates the new **improved KadiAbility architecture**. This ability showcases how easy it is to create powerful abilities with minimal code.
|
|
4
|
-
|
|
5
|
-
## What This Demonstrates
|
|
6
|
-
|
|
7
|
-
### 🏗️ **New Architecture Benefits**
|
|
8
|
-
|
|
9
|
-
1. **Cleaner Code**: The entire ability is ~100 lines vs ~600+ lines with the old architecture
|
|
10
|
-
2. **Transport Abstraction**: Automatically works with stdio and broker modes
|
|
11
|
-
3. **Built-in Discovery**: `__kadi_init` and `__kadi_discover` methods are automatic
|
|
12
|
-
4. **Better Error Handling**: Protocol errors are handled gracefully
|
|
13
|
-
5. **Metadata Support**: Easy to add schemas and descriptions to methods
|
|
14
|
-
|
|
15
|
-
### 🚀 **Simple API**
|
|
16
|
-
|
|
17
|
-
```javascript
|
|
18
|
-
const ability = new KadiAbility({ name: 'echo-js' });
|
|
19
|
-
|
|
20
|
-
ability
|
|
21
|
-
.method('echo', async ({ message }) => {
|
|
22
|
-
return { echo: message, timestamp: new Date().toISOString() };
|
|
23
|
-
})
|
|
24
|
-
.serve();
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
That's it! The new architecture handles:
|
|
28
|
-
|
|
29
|
-
- LSP framing for stdio communication
|
|
30
|
-
- Request/response correlation
|
|
31
|
-
- Error handling
|
|
32
|
-
- Discovery methods
|
|
33
|
-
- Transport selection
|
|
34
|
-
|
|
35
|
-
## Available Methods
|
|
36
|
-
|
|
37
|
-
### `echo`
|
|
38
|
-
|
|
39
|
-
Returns your message with metadata:
|
|
40
|
-
|
|
41
|
-
```json
|
|
42
|
-
{
|
|
43
|
-
"method": "echo",
|
|
44
|
-
"params": { "message": "Hello!" }
|
|
45
|
-
}
|
|
46
|
-
// Returns: { "echo": "Hello!", "timestamp": "2024-...", "length": 6 }
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
### `mirror`
|
|
50
|
-
|
|
51
|
-
Returns your entire input object:
|
|
52
|
-
|
|
53
|
-
```json
|
|
54
|
-
{
|
|
55
|
-
"method": "mirror",
|
|
56
|
-
"params": { "foo": "bar", "numbers": [1, 2, 3] }
|
|
57
|
-
}
|
|
58
|
-
// Returns: { "mirrored": {...}, "metadata": {...} }
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
### `health`
|
|
62
|
-
|
|
63
|
-
Returns service health status:
|
|
64
|
-
|
|
65
|
-
```json
|
|
66
|
-
{
|
|
67
|
-
"method": "health",
|
|
68
|
-
"params": {}
|
|
69
|
-
}
|
|
70
|
-
// Returns: { "status": "healthy", "uptime": 12345, "version": "0.0.1" }
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
## Running
|
|
74
|
-
|
|
75
|
-
### Stdio Mode (default)
|
|
76
|
-
|
|
77
|
-
```bash
|
|
78
|
-
node service.js
|
|
79
|
-
# Then send JSON-RPC messages via stdin
|
|
80
|
-
```
|
|
81
|
-
|
|
82
|
-
### Broker Mode
|
|
83
|
-
|
|
84
|
-
```bash
|
|
85
|
-
KADI_MODE=broker KADI_BROKER_URL=ws://localhost:8080 node service.js
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
### Testing
|
|
89
|
-
|
|
90
|
-
```bash
|
|
91
|
-
node test-manual.js
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
## Architecture Comparison
|
|
95
|
-
|
|
96
|
-
### Before (Old KadiAbility)
|
|
97
|
-
|
|
98
|
-
```javascript
|
|
99
|
-
// Had to implement:
|
|
100
|
-
// - LSP frame reading/writing (~200 lines)
|
|
101
|
-
// - Request/response loop (~100 lines)
|
|
102
|
-
// - Error handling (~100 lines)
|
|
103
|
-
// - Discovery methods (~50 lines)
|
|
104
|
-
// - Transport management (~50 lines)
|
|
105
|
-
// Total: ~500+ lines before any business logic!
|
|
106
|
-
```
|
|
107
|
-
|
|
108
|
-
### After (New Architecture)
|
|
109
|
-
|
|
110
|
-
```javascript
|
|
111
|
-
import { KadiAbility } from '@kadi/core';
|
|
112
|
-
|
|
113
|
-
const ability = new KadiAbility({ name: 'my-ability' });
|
|
114
|
-
ability.method('myMethod', handler);
|
|
115
|
-
ability.serve();
|
|
116
|
-
|
|
117
|
-
// Everything else is handled automatically!
|
|
118
|
-
// Focus on business logic, not plumbing.
|
|
119
|
-
```
|
|
120
|
-
|
|
121
|
-
## Key Features Demonstrated
|
|
122
|
-
|
|
123
|
-
- ✅ **Multiple Methods**: echo, mirror, health
|
|
124
|
-
- ✅ **Method Metadata**: Descriptions and schemas
|
|
125
|
-
- ✅ **Error Handling**: Graceful error responses
|
|
126
|
-
- ✅ **Event Monitoring**: Request/response logging
|
|
127
|
-
- ✅ **Discovery**: Built-in `__kadi_init` and `__kadi_discover`
|
|
128
|
-
- ✅ **Transport Agnostic**: Works with stdio and broker modes
|
|
129
|
-
- ✅ **Clean Shutdown**: Handles SIGINT/SIGTERM
|
|
130
|
-
|
|
131
|
-
This is what ability development should feel like! 🎉
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "echo-js",
|
|
3
|
-
"kind": "ability",
|
|
4
|
-
"version": "0.0.1",
|
|
5
|
-
"license": "MIT",
|
|
6
|
-
"description": "Simple echo ability that returns whatever you send to it - demonstrates the new KadiAbility architecture.",
|
|
7
|
-
"repo": "",
|
|
8
|
-
"brokers": {
|
|
9
|
-
"local": "ws://127.0.0.1:8080",
|
|
10
|
-
"remote": "ws://kadi.build:8080"
|
|
11
|
-
},
|
|
12
|
-
"abilities": {},
|
|
13
|
-
"scripts": {
|
|
14
|
-
"preflight": "npm uninstall @kadi.build/core && npm cache clean --force && npm pack ../../../../../ && npm install kadi.build-core-0.0.1-alpha.2.tgz && rm kadi.build-core-0.0.1-alpha.2.tgz",
|
|
15
|
-
"setup": "npm install",
|
|
16
|
-
"start": "node service.js",
|
|
17
|
-
"stop": ""
|
|
18
|
-
},
|
|
19
|
-
"interfaces": {
|
|
20
|
-
"native": {
|
|
21
|
-
"entry": "service.js"
|
|
22
|
-
},
|
|
23
|
-
"stdio": {
|
|
24
|
-
"discover": true,
|
|
25
|
-
"timeoutMs": 10000
|
|
26
|
-
},
|
|
27
|
-
"broker": {
|
|
28
|
-
"discover": true,
|
|
29
|
-
"timeoutMs": 15000
|
|
30
|
-
}
|
|
31
|
-
},
|
|
32
|
-
"exports": [
|
|
33
|
-
{
|
|
34
|
-
"name": "echo",
|
|
35
|
-
"title": "Echo",
|
|
36
|
-
"description": "Returns the exact input you provide - useful for testing.",
|
|
37
|
-
"inputSchema": {
|
|
38
|
-
"type": "object",
|
|
39
|
-
"properties": {
|
|
40
|
-
"message": {
|
|
41
|
-
"type": "string",
|
|
42
|
-
"description": "Message to echo back"
|
|
43
|
-
}
|
|
44
|
-
},
|
|
45
|
-
"required": ["message"]
|
|
46
|
-
},
|
|
47
|
-
"outputSchema": {
|
|
48
|
-
"type": "object",
|
|
49
|
-
"properties": {
|
|
50
|
-
"echo": {
|
|
51
|
-
"type": "string"
|
|
52
|
-
},
|
|
53
|
-
"timestamp": {
|
|
54
|
-
"type": "string"
|
|
55
|
-
},
|
|
56
|
-
"length": {
|
|
57
|
-
"type": "number"
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
]
|
|
63
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "echo-js",
|
|
3
|
-
"version": "0.0.1",
|
|
4
|
-
"description": "Simple echo ability demonstrating the new KadiAbility architecture",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"main": "service.js",
|
|
7
|
-
"scripts": {
|
|
8
|
-
"preflight": "",
|
|
9
|
-
"setup": "npm install",
|
|
10
|
-
"start": "node service.js",
|
|
11
|
-
"test": "echo 'No tests yet'"
|
|
12
|
-
},
|
|
13
|
-
"keywords": [
|
|
14
|
-
"kadi",
|
|
15
|
-
"ability",
|
|
16
|
-
"echo",
|
|
17
|
-
"demo"
|
|
18
|
-
],
|
|
19
|
-
"author": "",
|
|
20
|
-
"license": "MIT",
|
|
21
|
-
"dependencies": {
|
|
22
|
-
"@kadi.build/core": "file:../../../../../Repositories/KADI/kadi-core/kadi.build-core-0.0.1-alpha.2.tgz"
|
|
23
|
-
}
|
|
24
|
-
}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
// echo-js/index.ts (or .js)
|
|
2
|
-
import { KadiAbility } from '@kadi.build/core';
|
|
3
|
-
|
|
4
|
-
const echoAbility = new KadiAbility({
|
|
5
|
-
name: 'echo-js',
|
|
6
|
-
version: '0.0.1',
|
|
7
|
-
description:
|
|
8
|
-
'Simple echo ability demonstrating the new KadiAbility architecture',
|
|
9
|
-
scope: process.env.KADI_AGENT_SCOPE
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
async function echo(message) {
|
|
13
|
-
const timestamp = new Date().toISOString();
|
|
14
|
-
const messageText = message?.message ?? message ?? '';
|
|
15
|
-
const length = messageText.length;
|
|
16
|
-
|
|
17
|
-
echoAbility.publishEvent('echo:test-event', { from: 'message echo' });
|
|
18
|
-
echoAbility.publishEvent('echo:test-event', { from: 'message echo 2' });
|
|
19
|
-
|
|
20
|
-
return { echo: messageText, timestamp, length };
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
async function say_message(message) {
|
|
24
|
-
return { message };
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// Register 'echo' and 'say_message' methods
|
|
28
|
-
echoAbility.method('echo', echo);
|
|
29
|
-
|
|
30
|
-
echoAbility.method('say_message', say_message, {
|
|
31
|
-
description: 'Say a message',
|
|
32
|
-
inputSchema: { type: 'object', properties: { message: { type: 'string' } } },
|
|
33
|
-
outputSchema: { type: 'object', properties: { message: { type: 'string' } } }
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
// For native/in-process you usually do NOT need serve().
|
|
37
|
-
// If you keep it for other protocols, it won’t hurt, but isn’t required here.
|
|
38
|
-
echoAbility.serve().catch((error) => {
|
|
39
|
-
console.error(`[echo-js] Fatal error: ${error.message}`);
|
|
40
|
-
process.exit(1);
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
export default echoAbility;
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "hash-go",
|
|
3
|
-
"kind": "ability",
|
|
4
|
-
"version": "0.0.1",
|
|
5
|
-
"license": "MIT",
|
|
6
|
-
"description": "Compute digests (sha256 default).",
|
|
7
|
-
"repo": "",
|
|
8
|
-
"brokers": {
|
|
9
|
-
"local": "ws://127.0.0.1:8080",
|
|
10
|
-
"remote": "ws://146.190.121.213:8080"
|
|
11
|
-
},
|
|
12
|
-
"abilities": {},
|
|
13
|
-
"scripts": {
|
|
14
|
-
"preflight": "",
|
|
15
|
-
"setup": "go mod tidy && mkdir -p bin && go build -o bin/hash_ability ./cmd/hash_ability",
|
|
16
|
-
"start": "./bin/hash_ability",
|
|
17
|
-
"stop": ""
|
|
18
|
-
},
|
|
19
|
-
"interfaces": {
|
|
20
|
-
"stdio": {
|
|
21
|
-
"discover": true,
|
|
22
|
-
"timeoutMs": 10000
|
|
23
|
-
}
|
|
24
|
-
},
|
|
25
|
-
"exports": [
|
|
26
|
-
{
|
|
27
|
-
"name": "digest",
|
|
28
|
-
"title": "Compute Digest",
|
|
29
|
-
"description": "Return a hex digest of the input text.",
|
|
30
|
-
"inputSchema": {
|
|
31
|
-
"type": "object",
|
|
32
|
-
"properties": {
|
|
33
|
-
"text": { "type": "string", "description": "Input text" },
|
|
34
|
-
"algo": {
|
|
35
|
-
"type": "string",
|
|
36
|
-
"enum": ["sha256", "md5"],
|
|
37
|
-
"default": "sha256",
|
|
38
|
-
"description": "Hash algorithm"
|
|
39
|
-
}
|
|
40
|
-
},
|
|
41
|
-
"required": ["text"]
|
|
42
|
-
},
|
|
43
|
-
"outputSchema": {
|
|
44
|
-
"type": "object",
|
|
45
|
-
"properties": {
|
|
46
|
-
"algo": { "type": "string" },
|
|
47
|
-
"hex": { "type": "string" }
|
|
48
|
-
},
|
|
49
|
-
"required": ["algo", "hex"]
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
]
|
|
53
|
-
}
|
|
@@ -1,340 +0,0 @@
|
|
|
1
|
-
// A minimal JSON-RPC 2.0 server over stdin/stdout for the "hash-go" ability.
|
|
2
|
-
// - Uses LSP-style framing: Kadi-Content-Length header + JSON body
|
|
3
|
-
// - Implements:
|
|
4
|
-
// __kadi_init : handshake/version negotiation
|
|
5
|
-
// __kadi_discover : returns functions/tools metadata
|
|
6
|
-
// digest : computes sha256/md5 hex digest of input text
|
|
7
|
-
//
|
|
8
|
-
// This follows the LSP-style protocol used by spawnJSONRPCProcess() in kadi-core.
|
|
9
|
-
|
|
10
|
-
package main
|
|
11
|
-
|
|
12
|
-
import (
|
|
13
|
-
"crypto/md5"
|
|
14
|
-
"crypto/sha256"
|
|
15
|
-
"encoding/hex"
|
|
16
|
-
"encoding/json"
|
|
17
|
-
"fmt"
|
|
18
|
-
"io"
|
|
19
|
-
"os"
|
|
20
|
-
"strconv"
|
|
21
|
-
"strings"
|
|
22
|
-
)
|
|
23
|
-
|
|
24
|
-
// RPCRequest models a JSON-RPC request coming from the agent.
|
|
25
|
-
// ID is left as interface{} so we can echo back either number or string.
|
|
26
|
-
type RPCRequest struct {
|
|
27
|
-
JSONRPC string `json:"jsonrpc"`
|
|
28
|
-
ID interface{} `json:"id"`
|
|
29
|
-
Method string `json:"method"`
|
|
30
|
-
Params json.RawMessage `json:"params"`
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// RPCError is the error object defined by JSON-RPC 2.0.
|
|
34
|
-
type RPCError struct {
|
|
35
|
-
Code int `json:"code"`
|
|
36
|
-
Message string `json:"message"`
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// RPCResponse models a JSON-RPC response. Either Result or Error is present.
|
|
40
|
-
type RPCResponse struct {
|
|
41
|
-
JSONRPC string `json:"jsonrpc"`
|
|
42
|
-
ID interface{} `json:"id"`
|
|
43
|
-
Result interface{} `json:"result,omitempty"`
|
|
44
|
-
Error *RPCError `json:"error,omitempty"`
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// digestParams describes the input we expect for the "digest" method/tool.
|
|
48
|
-
type digestParams struct {
|
|
49
|
-
Text string `json:"text"`
|
|
50
|
-
Algo string `json:"algo"` // "sha256" (default) or "md5"
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// toolSpec models an MCP-style tool descriptor.
|
|
54
|
-
type toolSpec struct {
|
|
55
|
-
Name string `json:"name"`
|
|
56
|
-
Title string `json:"title,omitempty"`
|
|
57
|
-
Description string `json:"description,omitempty"`
|
|
58
|
-
InputSchema map[string]interface{} `json:"inputSchema"`
|
|
59
|
-
OutputSchema map[string]interface{} `json:"outputSchema,omitempty"`
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
func main() {
|
|
63
|
-
// Loop forever: read LSP-style frames → handle → write response.
|
|
64
|
-
for {
|
|
65
|
-
// Read headers byte by byte until we find \r\n\r\n
|
|
66
|
-
headers := make(map[string]string)
|
|
67
|
-
headerData := make([]byte, 0)
|
|
68
|
-
|
|
69
|
-
// Read until we find the header delimiter \r\n\r\n
|
|
70
|
-
delimiter := []byte{'\r', '\n', '\r', '\n'}
|
|
71
|
-
for {
|
|
72
|
-
b := make([]byte, 1)
|
|
73
|
-
_, err := os.Stdin.Read(b)
|
|
74
|
-
if err != nil {
|
|
75
|
-
if err == io.EOF {
|
|
76
|
-
return // Clean exit
|
|
77
|
-
}
|
|
78
|
-
fmt.Fprintf(os.Stderr, "Error reading header byte: %v\n", err)
|
|
79
|
-
return
|
|
80
|
-
}
|
|
81
|
-
headerData = append(headerData, b[0])
|
|
82
|
-
|
|
83
|
-
// Check if we have the delimiter
|
|
84
|
-
if len(headerData) >= 4 {
|
|
85
|
-
if string(headerData[len(headerData)-4:]) == string(delimiter) {
|
|
86
|
-
break
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// Parse headers (exclude the delimiter)
|
|
92
|
-
headerSection := string(headerData[:len(headerData)-4])
|
|
93
|
-
lines := strings.Split(headerSection, "\r\n")
|
|
94
|
-
|
|
95
|
-
for _, line := range lines {
|
|
96
|
-
if strings.Contains(line, ":") {
|
|
97
|
-
parts := strings.SplitN(line, ":", 2)
|
|
98
|
-
if len(parts) == 2 {
|
|
99
|
-
name := strings.TrimSpace(parts[0])
|
|
100
|
-
value := strings.TrimSpace(parts[1])
|
|
101
|
-
headers[name] = value
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
// Extract Content-Length
|
|
107
|
-
contentLengthStr, ok := headers["Kadi-Content-Length"]
|
|
108
|
-
if !ok {
|
|
109
|
-
fmt.Fprintf(os.Stderr, "Missing Content-Length header\n")
|
|
110
|
-
continue
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
contentLength, err := strconv.Atoi(contentLengthStr)
|
|
114
|
-
if err != nil {
|
|
115
|
-
fmt.Fprintf(os.Stderr, "Invalid Content-Length: %v\n", err)
|
|
116
|
-
continue
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// Read the JSON body
|
|
120
|
-
body := make([]byte, contentLength)
|
|
121
|
-
_, err = io.ReadFull(os.Stdin, body)
|
|
122
|
-
if err != nil {
|
|
123
|
-
fmt.Fprintf(os.Stderr, "Error reading body: %v\n", err)
|
|
124
|
-
continue
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
var req RPCRequest
|
|
128
|
-
if err := json.Unmarshal(body, &req); err != nil {
|
|
129
|
-
// If the request can't be parsed, return a JSON-RPC parse error (-32700).
|
|
130
|
-
writeResponse(RPCResponse{
|
|
131
|
-
JSONRPC: "2.0",
|
|
132
|
-
ID: nil,
|
|
133
|
-
Error: &RPCError{Code: -32700, Message: "parse error: " + err.Error()},
|
|
134
|
-
})
|
|
135
|
-
continue
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
// Dispatch by method. Each handler returns (result, *RPCError).
|
|
139
|
-
var (
|
|
140
|
-
result interface{}
|
|
141
|
-
rerr *RPCError
|
|
142
|
-
)
|
|
143
|
-
|
|
144
|
-
switch req.Method {
|
|
145
|
-
case "__kadi_init":
|
|
146
|
-
// Handshake/version negotiation. You can return metadata here.
|
|
147
|
-
result, rerr = handleInit(req.Params)
|
|
148
|
-
|
|
149
|
-
case "__kadi_discover":
|
|
150
|
-
// Return functions map + optional tools array (mirrors MCP).
|
|
151
|
-
result, rerr = handleDiscover()
|
|
152
|
-
|
|
153
|
-
case "digest":
|
|
154
|
-
// Direct method call (what loadAbility().call("digest", ...) would use).
|
|
155
|
-
result, rerr = handleDigest(req.Params)
|
|
156
|
-
|
|
157
|
-
default:
|
|
158
|
-
// Method not found (-32601) per JSON-RPC.
|
|
159
|
-
rerr = &RPCError{Code: -32601, Message: "method not found: " + req.Method}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
// Send the response using LSP-style framing.
|
|
163
|
-
writeResponse(RPCResponse{
|
|
164
|
-
JSONRPC: "2.0",
|
|
165
|
-
ID: req.ID,
|
|
166
|
-
Result: result,
|
|
167
|
-
Error: rerr,
|
|
168
|
-
})
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
// writeResponse serializes and prints a JSON-RPC response using LSP-style framing.
|
|
173
|
-
// Format: Content-Length header + JSON body
|
|
174
|
-
func writeResponse(resp RPCResponse) {
|
|
175
|
-
b, err := json.Marshal(resp)
|
|
176
|
-
if err != nil {
|
|
177
|
-
// As a last resort, emit a generic error with no ID.
|
|
178
|
-
fallback := RPCResponse{
|
|
179
|
-
JSONRPC: "2.0",
|
|
180
|
-
ID: nil,
|
|
181
|
-
Error: &RPCError{Code: -32603, Message: "internal error: " + err.Error()},
|
|
182
|
-
}
|
|
183
|
-
b, _ = json.Marshal(fallback)
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
// Write LSP-style frame: headers + body
|
|
187
|
-
header := fmt.Sprintf("Kadi-Content-Length: %d\r\nContent-Type: application/kadi-jsonrpc; charset=utf-8\r\n\r\n", len(b))
|
|
188
|
-
fmt.Print(header)
|
|
189
|
-
fmt.Print(string(b))
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
/* ----------------------------- Handlers ---------------------------------- */
|
|
193
|
-
|
|
194
|
-
// handleInit replies to __kadi_init. You can extend this with capabilities.
|
|
195
|
-
func handleInit(params json.RawMessage) (interface{}, *RPCError) {
|
|
196
|
-
// Optionally parse params to inspect the API version.
|
|
197
|
-
var payload map[string]interface{}
|
|
198
|
-
_ = json.Unmarshal(params, &payload)
|
|
199
|
-
|
|
200
|
-
// Return metadata; you could also include "functions" here to skip discover.
|
|
201
|
-
return map[string]interface{}{
|
|
202
|
-
"api": "1.0",
|
|
203
|
-
"ability": map[string]interface{}{
|
|
204
|
-
"name": "hash-go",
|
|
205
|
-
"version": "0.0.1",
|
|
206
|
-
},
|
|
207
|
-
}, nil
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
// handleDiscover advertises callable functions (your loader can auto-stub from this).
|
|
211
|
-
// We also include an MCP-compatible "tools" array for symmetry.
|
|
212
|
-
func handleDiscover() (interface{}, *RPCError) {
|
|
213
|
-
input, output := digestSchemas()
|
|
214
|
-
|
|
215
|
-
return map[string]interface{}{
|
|
216
|
-
"functions": map[string]interface{}{
|
|
217
|
-
"digest": map[string]interface{}{
|
|
218
|
-
"inputSchema": input,
|
|
219
|
-
"outputSchema": output,
|
|
220
|
-
},
|
|
221
|
-
},
|
|
222
|
-
"tools": []toolSpec{
|
|
223
|
-
{
|
|
224
|
-
Name: "digest",
|
|
225
|
-
Title: "Compute Digest",
|
|
226
|
-
Description: "Return a hex digest (sha256 default) of the input text.",
|
|
227
|
-
InputSchema: input,
|
|
228
|
-
OutputSchema: map[string]interface{}{
|
|
229
|
-
"type": "object",
|
|
230
|
-
"properties": map[string]interface{}{
|
|
231
|
-
"algo": map[string]interface{}{"type": "string"},
|
|
232
|
-
"hex": map[string]interface{}{"type": "string"},
|
|
233
|
-
},
|
|
234
|
-
"required": []string{"algo", "hex"},
|
|
235
|
-
},
|
|
236
|
-
},
|
|
237
|
-
},
|
|
238
|
-
}, nil
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
// handleToolsList matches MCP "tools/list": return the list of tools.
|
|
242
|
-
func handleToolsList() (interface{}, *RPCError) {
|
|
243
|
-
input, _ := digestSchemas()
|
|
244
|
-
tools := []toolSpec{
|
|
245
|
-
{
|
|
246
|
-
Name: "digest",
|
|
247
|
-
Title: "Compute Digest",
|
|
248
|
-
Description: "Return a hex digest (sha256 default) of the input text.",
|
|
249
|
-
InputSchema: input,
|
|
250
|
-
OutputSchema: map[string]interface{}{
|
|
251
|
-
"type": "object",
|
|
252
|
-
"properties": map[string]interface{}{
|
|
253
|
-
"algo": map[string]interface{}{"type": "string"},
|
|
254
|
-
"hex": map[string]interface{}{"type": "string"},
|
|
255
|
-
},
|
|
256
|
-
"required": []string{"algo", "hex"},
|
|
257
|
-
},
|
|
258
|
-
},
|
|
259
|
-
}
|
|
260
|
-
return tools, nil
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
// handleToolsCall matches MCP "tools/call": expects {"name": "...", "arguments": {...}}
|
|
264
|
-
func handleToolsCall(params json.RawMessage) (interface{}, *RPCError) {
|
|
265
|
-
var p struct {
|
|
266
|
-
Name string `json:"name"`
|
|
267
|
-
Arguments json.RawMessage `json:"arguments"`
|
|
268
|
-
}
|
|
269
|
-
if err := json.Unmarshal(params, &p); err != nil {
|
|
270
|
-
return nil, &RPCError{Code: -32602, Message: "invalid params: " + err.Error()}
|
|
271
|
-
}
|
|
272
|
-
switch p.Name {
|
|
273
|
-
case "digest":
|
|
274
|
-
// Reuse our regular digest handler for the arguments payload.
|
|
275
|
-
return handleDigest(p.Arguments)
|
|
276
|
-
default:
|
|
277
|
-
return nil, &RPCError{Code: -32601, Message: "tool not found: " + p.Name}
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
// handleDigest is the actual business logic: compute a digest and return algo+hex.
|
|
282
|
-
func handleDigest(params json.RawMessage) (interface{}, *RPCError) {
|
|
283
|
-
var p digestParams
|
|
284
|
-
if err := json.Unmarshal(params, &p); err != nil {
|
|
285
|
-
return nil, &RPCError{Code: -32602, Message: "invalid params: " + err.Error()}
|
|
286
|
-
}
|
|
287
|
-
if p.Algo == "" {
|
|
288
|
-
p.Algo = "sha256"
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
textBytes := []byte(p.Text)
|
|
292
|
-
switch strings.ToLower(p.Algo) {
|
|
293
|
-
case "sha256":
|
|
294
|
-
sum := sha256.Sum256(textBytes)
|
|
295
|
-
return map[string]interface{}{
|
|
296
|
-
"algo": "sha256",
|
|
297
|
-
"hex": hex.EncodeToString(sum[:]),
|
|
298
|
-
}, nil
|
|
299
|
-
case "md5":
|
|
300
|
-
sum := md5.Sum(textBytes)
|
|
301
|
-
return map[string]interface{}{
|
|
302
|
-
"algo": "md5",
|
|
303
|
-
"hex": hex.EncodeToString(sum[:]),
|
|
304
|
-
}, nil
|
|
305
|
-
default:
|
|
306
|
-
return nil, &RPCError{Code: -32602, Message: "unsupported algo: " + p.Algo}
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
/* --------------------------- JSON Schemas -------------------------------- */
|
|
311
|
-
|
|
312
|
-
// digestSchemas returns the JSON Schemas for input/output (as plain Go maps).
|
|
313
|
-
func digestSchemas() (input map[string]interface{}, output map[string]interface{}) {
|
|
314
|
-
input = map[string]interface{}{
|
|
315
|
-
"type": "object",
|
|
316
|
-
"properties": map[string]interface{}{
|
|
317
|
-
"text": map[string]interface{}{
|
|
318
|
-
"type": "string",
|
|
319
|
-
"description": "Input text to hash.",
|
|
320
|
-
},
|
|
321
|
-
"algo": map[string]interface{}{
|
|
322
|
-
"type": "string",
|
|
323
|
-
"enum": []interface{}{"sha256", "md5"},
|
|
324
|
-
"default": "sha256",
|
|
325
|
-
"description": "Hash algorithm.",
|
|
326
|
-
},
|
|
327
|
-
},
|
|
328
|
-
"required": []string{"text"},
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
output = map[string]interface{}{
|
|
332
|
-
"type": "object",
|
|
333
|
-
"properties": map[string]interface{}{
|
|
334
|
-
"algo": map[string]interface{}{"type": "string"},
|
|
335
|
-
"hex": map[string]interface{}{"type": "string"},
|
|
336
|
-
},
|
|
337
|
-
"required": []string{"algo", "hex"},
|
|
338
|
-
}
|
|
339
|
-
return
|
|
340
|
-
}
|