-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommand_interface.zig
More file actions
279 lines (235 loc) · 9.08 KB
/
Copy pathcommand_interface.zig
File metadata and controls
279 lines (235 loc) · 9.08 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// ═══════════════════════════════════════════════════════════════════════════════
// TRINITY UNIFIED COMMAND INTERFACE
// ═══════════════════════════════════════════════════════════════════════════════
//
// Purpose: Single source of truth for all TRI commands
//
// Architecture:
// Command Registry (this file) → CLI Dispatch → API Routes → MCP Tools
//
// φ² + 1/φ² = 3 = TRINITY
//
// ═══════════════════════════════════════════════════════════════════════════════
const std = @import("std");
const Allocator = std.mem.Allocator;
// ═══════════════════════════════════════════════════════════════════════════════
// TYPES
// ═══════════════════════════════════════════════════════════════════════════════
/// Unified command handler signature
/// All TRI commands must implement this signature
pub const CommandFn = *const fn (allocator: Allocator, args: []const []const u8) Error!void;
/// Command execution error
pub const Error = error{
InvalidArguments,
NotImplemented,
MissingDependency,
NetworkError,
FilesystemError,
// Re-export common errors
OutOfMemory,
};
/// Command metadata for registry, API, and MCP
pub const CommandMetadata = struct {
/// Primary command name (e.g., "mesh", "wallet", "dashboard")
name: []const u8,
/// Short description (1 line)
description: []const u8,
/// Category for grouping (e.g., "Core", "Omega", "Mesh", "Wallet")
category: []const u8,
/// Alternative names (e.g., {"dash", "ui"} for "dashboard")
aliases: []const []const u8,
/// Handler function
handler: CommandFn,
/// Expose via MCP?
mcp_enabled: bool = true,
/// Expose via API route? (null = no direct route)
api_route: ?[]const u8 = null,
/// Long help text (null = use description)
help_long: ?[]const u8 = null,
};
/// Command category for grouping
pub const CommandCategory = enum {
Core,
SWE_Agent,
Git,
Golden_Chain,
TVC,
Multi_Agent,
Long_Context,
RAG,
Voice_IO,
Code_Sandbox,
Streaming,
Vision,
Fine_Tuning,
Batched,
Priority_Queue,
Deadline,
Multi_Modal,
Unified_Agent,
Autonomous,
Orchestration,
MM_Orchestration,
Memory,
Persistent,
Spawn,
Cluster,
Work_Stealing,
Plugin,
Comms,
Observe,
Consensus,
Spec_Exec,
Governor,
Fed_Learn,
Event_Src,
Cap_Sec,
DTXN,
Cache,
Contract,
Workflow,
Chemistry,
Sacred_Math,
Sacred_Science,
Temporal,
Quantum,
Omega,
Trinity_OS,
Dev_Util,
Needle,
Dashboard,
Wallet,
Mesh,
Reputation,
Hardware,
};
// ═══════════════════════════════════════════════════════════════════════════════
// COMMAND REGISTRY
// ═══════════════════════════════════════════════════════════════════════════════
/// Global command registry
var registry: std.ArrayList(CommandMetadata) = undefined;
var registry_initialized = false;
/// Initialize the command registry
pub fn init(allocator: Allocator) !void {
if (registry_initialized) return;
registry = std.ArrayList(CommandMetadata).init(allocator);
registry_initialized = true;
// NOTE: Auto-population from Command enum deferred - manual registration required
// Integration with existing code planned for v12
}
/// Register a command
pub fn register(meta: CommandMetadata) !void {
if (!registry_initialized) return error.NotInitialized;
// Check for duplicates
for (registry.items) |cmd| {
if (std.mem.eql(u8, cmd.name, meta.name)) {
std.debug.print("Warning: Duplicate command name: {s}\n", .{meta.name});
return error.DuplicateCommand;
}
}
try registry.append(meta);
}
/// Get command by name (checks aliases too)
pub fn get(name: []const u8) ?CommandMetadata {
if (!registry_initialized) return null;
for (registry.items) |cmd| {
if (std.mem.eql(u8, cmd.name, name)) return cmd;
// Check aliases
for (cmd.aliases) |alias| {
if (std.mem.eql(u8, alias, name)) return cmd;
}
}
return null;
}
/// List all commands in a category
pub fn listCategory(category: CommandCategory) []const CommandMetadata {
if (!registry_initialized) return &[_]CommandMetadata{};
const cat_str = @tagName(category);
var result = std.ArrayList(CommandMetadata).init(registry.allocator);
for (registry.items) |cmd| {
if (std.mem.eql(u8, cmd.category, cat_str)) {
result.append(cmd) catch |err| {
std.log.warn("command append failed: {s}", .{@errorName(err)});
};
}
}
return result.toOwnedSlice() catch &[_]CommandMetadata{};
}
/// List all commands
pub fn listAll() []const CommandMetadata {
if (!registry_initialized) return &[_]CommandMetadata{};
return registry.items;
}
/// Get command count
pub fn count() usize {
if (!registry_initialized) return 0;
return registry.items.len;
}
// ═══════════════════════════════════════════════════════════════════════════════
// MCP AUTO-DISCOVERY
// ═══════════════════════════════════════════════════════════════════════════════
/// Generate MCP tool schema for a command
pub fn toMcpTool(cmd: CommandMetadata) McpToolSchema {
return McpToolSchema{
.name = cmd.name,
.description = cmd.description,
.input_schema = .{
.type = "object",
.properties = &[_]Property{
.{
.name = "args",
.type = "array",
.description = "Command arguments",
.required = false,
},
},
},
};
}
pub const McpToolSchema = struct {
name: []const u8,
description: []const u8,
input_schema: InputSchema,
};
pub const InputSchema = struct {
type: []const u8,
properties: []const Property,
};
pub const Property = struct {
name: []const u8,
type: []const u8,
description: []const u8,
required: bool,
};
/// Get all MCP-enabled tools
pub fn listMcpTools() []const McpToolSchema {
if (!registry_initialized) return &[_]McpToolSchema{};
var result = std.ArrayList(McpToolSchema).init(registry.allocator);
for (registry.items) |cmd| {
if (cmd.mcp_enabled) {
result.append(toMcpTool(cmd)) catch |err| {
std.log.warn("MCP tool append failed: {s}", .{@errorName(err)});
};
}
}
return result.toOwnedSlice() catch &[_]McpToolSchema{};
}
// ═══════════════════════════════════════════════════════════════════════════════
// CLEANUP
// ═══════════════════════════════════════════════════════════════════════════════
/// Deinitialize the registry
pub fn deinit() void {
if (registry_initialized) {
registry.deinit();
registry_initialized = false;
}
}
// ═══════════════════════════════════════════════════════════════════════════════
// INITIALIZATION HOOK
// ═══════════════════════════════════════════════════════════════════════════════
// This will be called from main.zig during startup
comptime {
// Ensure this module is linked
_ = Error;
_ = CommandFn;
}