@ton-agent-kit/plugin-identity 1.0.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/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "@ton-agent-kit/plugin-identity",
3
+ "version": "1.0.0",
4
+ "description": "Identity plugin for TON Agent Kit — agent registry, discovery, and reputation",
5
+ "main": "src/index.ts",
6
+ "types": "src/index.ts",
7
+ "scripts": { "build": "tsc" },
8
+ "dependencies": {
9
+ "@ton-agent-kit/core": "^1.0.0",
10
+ "@ton/ton": "^16.2.0",
11
+ "@ton/core": "^0.63.1"
12
+ },
13
+ "keywords": ["ton", "blockchain", "ai", "agent", "sdk"],
14
+ "license": "MIT",
15
+ "publishConfig": { "access": "public" },
16
+ "repository": { "type": "git", "url": "https://github.com/Andy00L/ton-agent-kit.git" }
17
+ }
@@ -0,0 +1,51 @@
1
+ import { z } from "zod";
2
+ import { Address } from "@ton/core";
3
+ import { defineAction, toFriendlyAddress } from "@ton-agent-kit/core";
4
+ import { loadAgentRegistry } from "../utils";
5
+
6
+ export const discoverAgentAction = defineAction({
7
+ name: "discover_agent",
8
+ description:
9
+ "Find registered agents by capability or name. Search the local agent registry to find agents that can perform specific tasks.",
10
+ schema: z.object({
11
+ capability: z
12
+ .string()
13
+ .optional()
14
+ .describe("Capability to search for (e.g., 'price_feed', 'trading')"),
15
+ name: z.string().optional().describe("Agent name to search for"),
16
+ }),
17
+ handler: async (agent, params) => {
18
+ const registry = loadAgentRegistry();
19
+ let results = Object.values(registry);
20
+
21
+ if (params.capability) {
22
+ const cap = params.capability.toLowerCase();
23
+ results = results.filter((a: any) =>
24
+ a.capabilities.some((c: string) => c.toLowerCase().includes(cap)),
25
+ );
26
+ }
27
+
28
+ if (params.name) {
29
+ const name = params.name.toLowerCase();
30
+ results = results.filter((a: any) =>
31
+ a.name.toLowerCase().includes(name),
32
+ );
33
+ }
34
+
35
+ return {
36
+ query: { capability: params.capability, name: params.name },
37
+ count: results.length,
38
+ agents: results.map((a: any) => ({
39
+ id: a.id,
40
+ name: a.name,
41
+ address: a.address,
42
+ friendlyAddress: toFriendlyAddress(Address.parse(a.address), agent.network),
43
+ capabilities: a.capabilities,
44
+ description: a.description,
45
+ endpoint: a.endpoint,
46
+ reputation: a.reputation,
47
+ registeredAt: a.registeredAt,
48
+ })),
49
+ };
50
+ },
51
+ });
@@ -0,0 +1,53 @@
1
+ import { z } from "zod";
2
+ import { Address } from "@ton/core";
3
+ import { defineAction, toFriendlyAddress } from "@ton-agent-kit/core";
4
+ import { loadAgentRegistry, saveAgentRegistry } from "../utils";
5
+
6
+ export const getAgentReputationAction = defineAction({
7
+ name: "get_agent_reputation",
8
+ description:
9
+ "Get the reputation score of a registered agent. Set addTask=true and success=true to record a successful task. Set addTask=true and success=false to record a failed task.",
10
+ schema: z.object({
11
+ agentId: z.string().describe("Agent ID to check or update"),
12
+ addTask: z
13
+ .union([z.boolean(), z.string()])
14
+ .optional()
15
+ .describe("Set to true to record a completed task"),
16
+ success: z
17
+ .union([z.boolean(), z.string()])
18
+ .optional()
19
+ .describe("If addTask is true, whether the task was successful"),
20
+ }),
21
+ handler: async (agent, params) => {
22
+ const registry = loadAgentRegistry();
23
+ const agentRecord = registry[params.agentId];
24
+ if (!agentRecord) throw new Error(`Agent not found: ${params.agentId}`);
25
+
26
+ // Coerce string booleans
27
+ const addTask = typeof params.addTask === "string" ? params.addTask === "true" : params.addTask;
28
+ const success = typeof params.success === "string" ? params.success === "true" : params.success;
29
+
30
+ // Update reputation if recording a task
31
+ if (addTask) {
32
+ agentRecord.reputation.totalTasks += 1;
33
+ if (success !== false) {
34
+ agentRecord.reputation.successfulTasks += 1;
35
+ }
36
+ agentRecord.reputation.score = Math.round(
37
+ (agentRecord.reputation.successfulTasks /
38
+ agentRecord.reputation.totalTasks) *
39
+ 100,
40
+ );
41
+ saveAgentRegistry(registry);
42
+ }
43
+
44
+ return {
45
+ agentId: params.agentId,
46
+ name: agentRecord.name,
47
+ address: agentRecord.address,
48
+ friendlyAddress: toFriendlyAddress(Address.parse(agentRecord.address), agent.network),
49
+ reputation: agentRecord.reputation,
50
+ registeredAt: agentRecord.registeredAt,
51
+ };
52
+ },
53
+ });
@@ -0,0 +1,69 @@
1
+ import { z } from "zod";
2
+ import { defineAction, toFriendlyAddress } from "@ton-agent-kit/core";
3
+ import { loadAgentRegistry, saveAgentRegistry } from "../utils";
4
+
5
+ export const registerAgentAction = defineAction({
6
+ name: "register_agent",
7
+ description:
8
+ "Register an AI agent in the local agent registry with its capabilities, name, and description. Other agents can discover it via discover_agent.",
9
+ schema: z.object({
10
+ name: z
11
+ .string()
12
+ .describe("Agent name (e.g., 'market-data', 'trading-bot')"),
13
+ capabilities: z
14
+ .union([z.array(z.string()), z.string()])
15
+ .describe(
16
+ "List of capabilities (e.g., ['price_feed', 'analytics', 'trading'])",
17
+ ),
18
+ description: z
19
+ .string()
20
+ .optional()
21
+ .describe("Human-readable description of the agent"),
22
+ endpoint: z
23
+ .string()
24
+ .optional()
25
+ .describe("API endpoint where the agent can be reached"),
26
+ }),
27
+ handler: async (agent, params) => {
28
+ const agentId = `agent_${params.name.toLowerCase().replace(/[^a-z0-9]/g, "-")}`;
29
+
30
+ // Coerce string capabilities to array
31
+ let capabilities: string[];
32
+ if (typeof params.capabilities === "string") {
33
+ try {
34
+ capabilities = JSON.parse(params.capabilities);
35
+ } catch {
36
+ capabilities = params.capabilities.split(",").map((c: string) => c.trim());
37
+ }
38
+ } else {
39
+ capabilities = params.capabilities;
40
+ }
41
+
42
+ const agentRecord = {
43
+ id: agentId,
44
+ name: params.name,
45
+ address: agent.wallet.address.toRawString(),
46
+ capabilities,
47
+ description: params.description || "",
48
+ endpoint: params.endpoint || null,
49
+ network: agent.network,
50
+ registeredAt: new Date().toISOString(),
51
+ reputation: { score: 0, totalTasks: 0, successfulTasks: 0 },
52
+ };
53
+
54
+ const registry = loadAgentRegistry();
55
+ registry[agentId] = agentRecord;
56
+ saveAgentRegistry(registry);
57
+
58
+ return {
59
+ agentId,
60
+ name: params.name,
61
+ address: agent.wallet.address.toRawString(),
62
+ friendlyAddress: toFriendlyAddress(agent.wallet.address, agent.network),
63
+ capabilities,
64
+ description: params.description || "",
65
+ status: "registered",
66
+ dnsHint: `${params.name}.agents.ton (requires TON DNS domain)`,
67
+ };
68
+ },
69
+ });
package/src/index.ts ADDED
@@ -0,0 +1,21 @@
1
+ import { definePlugin } from "@ton-agent-kit/core";
2
+ import { registerAgentAction } from "./actions/register-agent";
3
+ import { discoverAgentAction } from "./actions/discover-agent";
4
+ import { getAgentReputationAction } from "./actions/get-agent-reputation";
5
+
6
+ /**
7
+ * Identity Plugin — Agent registry, discovery, and reputation
8
+ *
9
+ * Actions:
10
+ * - register_agent: Register an AI agent with capabilities
11
+ * - discover_agent: Find agents by capability or name
12
+ * - get_agent_reputation: Get/update agent reputation scores
13
+ */
14
+ const IdentityPlugin = definePlugin({
15
+ name: "identity",
16
+ actions: [registerAgentAction, discoverAgentAction, getAgentReputationAction],
17
+ });
18
+
19
+ export default IdentityPlugin;
20
+
21
+ export { registerAgentAction, discoverAgentAction, getAgentReputationAction };
package/src/utils.ts ADDED
@@ -0,0 +1,22 @@
1
+ import { existsSync, readFileSync, writeFileSync } from "fs";
2
+
3
+ const REGISTRY_FILE = ".agent-registry.json";
4
+
5
+ export function loadAgentRegistry(): Record<string, any> {
6
+ try {
7
+ if (existsSync(REGISTRY_FILE)) {
8
+ return JSON.parse(readFileSync(REGISTRY_FILE, "utf-8"));
9
+ }
10
+ } catch (err: any) {
11
+ console.error(`Failed to load agent registry: ${err.message}`);
12
+ }
13
+ return {};
14
+ }
15
+
16
+ export function saveAgentRegistry(registry: Record<string, any>): void {
17
+ try {
18
+ writeFileSync(REGISTRY_FILE, JSON.stringify(registry, null, 2), "utf-8");
19
+ } catch (err: any) {
20
+ console.error(`Failed to save agent registry: ${err.message}`);
21
+ }
22
+ }