Compare commits

...

138 Commits

Author SHA1 Message Date
Mahmoud-Emad
483b6e3de3 bump version to 1.0.26 2025-06-17 09:51:38 +03:00
f769c34466 Merge branch 'development_griddriver' into development
* development_griddriver:
  Update griddriver to use prebuilt binary
2025-06-17 08:02:22 +02:00
Mahmoud-Emad
c0242a0729 Merge branch 'development' of https://github.com/freeflowuniverse/herolib into development 2025-06-16 14:04:21 +03:00
Mahmoud-Emad
df452ce258 feat: Add mermaid diagrams support to documentation
- Added support for rendering mermaid diagrams in documentation.
- Updated Docusaurus configuration to include mermaid theme and enable
  mermaid rendering in markdown files.
- Updated package.json dependencies to use compatible versions.
2025-06-16 14:04:10 +03:00
7de290ae55 Merge branch 'dify_installer' into development
* dify_installer:
  dify installer
  dify installer
  adding dify installer
  feat: Improve Dify installer
  adding dify installer
  dify installer
  adding dify installer
  feat: Improve Dify installer
  adding dify installer
2025-06-16 10:21:39 +02:00
fe161c5bea Merge branch 'development_fix_zinit' into development
* development_fix_zinit:
  feat: Fix type mismatch error in rpc.discover response handling
2025-06-16 10:21:26 +02:00
Mahmoud-Emad
fca7c7364a docs: Remove redundant line in README
- Removed a trailing empty line in the README file.
2025-06-16 11:09:16 +03:00
Omdanii
ef705d1be0 Merge pull request #91 from freeflowuniverse/development_mecelium_rpc_client
feat: Add Mycelium JSON-RPC client
2025-06-16 10:51:31 +03:00
Omdanii
3154733be1 Merge pull request #92 from freeflowuniverse/development_zinit_rpc_client
feat: Add Zinit JSON-RPC client
2025-06-16 10:51:13 +03:00
b285e85eb5 Merge branch 'development' of github.com:freeflowuniverse/herolib into development
* 'development' of github.com:freeflowuniverse/herolib:
  feat: Improve ZinitClient and JSON-RPC client
2025-06-15 16:30:46 +02:00
89b7f0d465 ... 2025-06-15 16:30:40 +02:00
Mahmoud-Emad
256d4e9bca feat: Add Zinit RPC client
- Adds a new V client for interacting with the Zinit JSON-RPC API.
- Includes comprehensive example demonstrating all API methods.
- Provides type-safe structs and error handling.
- Implements all 18 methods of the Zinit JSON-RPC API.
2025-06-03 11:11:46 +03:00
Mahmoud-Emad
54f4e83627 Merge branch 'development' into development_mecelium_rpc_client 2025-06-02 17:05:15 +03:00
Mahmoud-Emad
f7a770989b feat: Improve ZinitClient and JSON-RPC client
- Enhanced error handling and response parsing in `ZinitClient`: The
  `discover` function now provides more robust error handling and
  response parsing, improving reliability.
- Improved code style and formatting: Minor formatting changes for
  better readability and maintainability.  The `ServiceConfig` and
  `ServiceConfigResponse` structs have been slightly restructured.
- Updated JSON-RPC client structure: The `Client` struct is now
  publicly mutable (`pub mut`), simplifying its use.  Removed
  unnecessary blank lines for improved code clarity.
2025-06-02 17:04:25 +03:00
Mahmoud-Emad
c5759ea30e feat: Add Mycelium JSON-RPC client
- Adds a new V language client for interacting with the Mycelium
  JSON-RPC admin API.
- Includes comprehensive example code demonstrating all API features.
- Implements all methods defined in the Mycelium JSON-RPC spec.
- Provides type-safe API with robust error handling.
- Uses HTTP transport for communication with the Mycelium node.
2025-06-02 16:48:59 +03:00
Mahmoud-Emad
aef9c84eb5 feat: Fix type mismatch error in rpc.discover response handling
- Correctly handle the complex JSON response of the `rpc.discover`
  method by using `map[string]string` instead of `string`.  This
  addresses a type mismatch error that prevented proper parsing of
  the API specification.
- Improve error handling and provide more informative output to the
  user during the API discovery process.
- Add detailed analysis and recommendations for handling complex JSON
  responses in similar scenarios.
2025-06-01 15:09:57 +03:00
d0baac83a9 ... 2025-05-31 16:02:52 +03:00
b6a2671665 ... 2025-05-31 12:45:05 +03:00
a96ae1252c ... 2025-05-31 11:17:56 +03:00
ac4db0f789 rpc example 2025-05-31 11:05:59 +03:00
37f9ab78ec format, implement unixsocket rpc 2025-05-31 08:26:03 +03:00
timurgordon
9b3ac150bd Merge branch 'development' of https://github.com/freeflowuniverse/herolib into development 2025-05-29 23:10:17 +03:00
timurgordon
dd577d51b9 match template and models for docusaurus 2025-05-29 23:10:13 +03:00
92f9714229 ... 2025-05-29 20:22:52 +04:00
timurgordon
632a1c11c2 fix docusaurus to use siteconfig in heroscript 2025-05-29 17:31:51 +03:00
Peternashaat
63d41352bc adding dify installer 2025-05-29 13:23:41 +00:00
Peternashaat
da8eef3711 dify installer 2025-05-29 13:06:36 +00:00
Peternashaat
f0a4732206 dify installer 2025-05-29 13:09:46 +03:00
Peternashaat
1f053edefc adding dify installer 2025-05-29 13:09:46 +03:00
Mahmoud-Emad
f93db1d23c feat: Improve Dify installer
- Update installer configuration to be more robust and flexible.
- Remove unnecessary installation steps in the installer script.
- Improve the installer's ability to check if Dify is running.
- Refactor Dify installer actions for better code organization.
- Add build functionality to Dify installer.
2025-05-29 13:09:46 +03:00
Peternashaat
105611bbfb adding dify installer 2025-05-29 13:09:46 +03:00
Peternashaat
4977c6de30 dify installer 2025-05-28 07:07:53 +00:00
Peternashaat
eb956bca3d adding dify installer 2025-05-27 11:40:50 +00:00
Mahmoud-Emad
5e511367c3 feat: Improve Dify installer
- Update installer configuration to be more robust and flexible.
- Remove unnecessary installation steps in the installer script.
- Improve the installer's ability to check if Dify is running.
- Refactor Dify installer actions for better code organization.
- Add build functionality to Dify installer.
2025-05-27 12:33:25 +03:00
Peternashaat
484bfe393e adding dify installer 2025-05-27 09:05:08 +00:00
a1404584d6 ... 2025-05-25 06:47:17 +04:00
3ef1698c2c ... 2025-05-25 06:44:51 +04:00
a7fb704627 ... 2025-05-25 06:30:48 +04:00
91ba6001cb ... 2025-05-22 20:12:06 +04:00
345a79d8ff ... 2025-05-21 09:05:30 +04:00
15d886e5e9 ... 2025-05-21 09:00:30 +04:00
d6224d1e60 ... 2025-05-21 08:56:34 +04:00
83fb647ac3 ... 2025-05-21 08:37:02 +04:00
b410544ee1 ... 2025-05-21 08:30:30 +04:00
2d5d1befae ... 2025-05-20 09:48:56 +04:00
fd8b8c8f42 revert 2025-05-20 07:00:00 +04:00
8ffb8c8caf ... 2025-05-20 06:49:39 +04:00
b8b339b85c ... 2025-05-20 06:47:36 +04:00
0789a38ea9 Merge branch 'development' of github.com:freeflowuniverse/herolib into development
# Conflicts:
#	lib/web/docusaurus/dsite.v
2025-05-20 06:40:48 +04:00
995d3c3f6d ... 2025-05-20 06:39:40 +04:00
timurgordon
822b179ef4 docusaurus fixes 2025-05-19 11:19:19 +03:00
4691971bd0 ... 2025-05-19 09:07:38 +04:00
9226e8b490 ... 2025-05-19 08:56:15 +04:00
b7fc7734b6 ... 2025-05-19 08:52:09 +04:00
8749e3a8cb ... 2025-05-19 08:52:03 +04:00
61f9f2868a ... 2025-05-19 08:25:47 +04:00
97dfcbeb51 ... 2025-05-19 07:37:10 +04:00
238fabbcb2 ... 2025-05-19 07:09:27 +04:00
49542b4bff ... 2025-05-19 07:04:20 +04:00
46898112f5 ... 2025-05-19 06:39:52 +04:00
f9bdb22c67 ... 2025-05-19 05:44:23 +04:00
cb664b2115 ... 2025-05-18 10:58:49 +03:00
timurgordon
761b9e031e docusaurus fixes 2025-05-16 18:03:22 +03:00
timurgordon
0d8d11fe26 small heroscript docusaurus fixes 2025-05-15 13:29:29 +03:00
timurgordon
2d5fbd3337 docusaurus fixes and correct hero cli implementation 2025-05-13 01:54:47 +03:00
cd3c98280e ... 2025-05-09 12:53:08 +03:00
39c6c37dee bump version to 1.0.25 2025-05-04 20:23:12 +03:00
3438f74e60 ... 2025-05-04 19:31:46 +03:00
4f79712570 ... 2025-05-04 19:26:39 +03:00
8e85ce0678 ... 2025-05-04 15:46:59 +03:00
ff09e7bf1b ... 2025-05-04 08:23:34 +03:00
46e1c6706c ... 2025-05-04 08:19:47 +03:00
d8a59d0726 ... 2025-05-04 07:57:23 +03:00
108d2019cd ... 2025-05-04 07:14:18 +03:00
3682ef2420 ... 2025-05-04 06:53:12 +03:00
a066db6624 ... 2025-05-04 05:36:10 +03:00
7458d64c05 ... 2025-05-04 05:29:10 +03:00
2a1787f28f ... 2025-04-21 22:49:24 +04:00
de78c229ce ... 2025-04-21 05:37:45 +02:00
Timur Gordon
f386c67acf rhai generation wip 2025-04-20 01:50:48 +02:00
Timur Gordon
75f98bf349 rust and rhai mcp improvements 2025-04-17 21:19:18 +02:00
Timur Gordon
9870fcbc5d fix import paths 2025-04-13 13:40:35 +02:00
Timur Gordon
d2b8379505 move mcp to ai mod 2025-04-13 13:39:03 +02:00
Timur Gordon
2dcb97255c Merge branch 'development_kristof_ai' of github.com:freeflowuniverse/herolib into development_kristof_ai 2025-04-13 13:30:50 +02:00
Timur Gordon
f7dd227cd0 mcp refactor wip 2025-04-13 13:28:55 +02:00
e2c18c3a24 ... 2025-04-13 10:06:11 +02:00
1bc6c6eab8 ... 2025-04-13 10:03:16 +02:00
Timur Gordon
4b39b137de rhai wrapper ai gen wip 2025-04-13 00:35:29 +02:00
e5de293919 ... 2025-04-04 14:02:17 +02:00
8a10374570 Merge branch 'development_actions007_timur' into development
* development_actions007_timur:
  progress in mail code gen
  vcode and baobab mcps enhancements
  generator mcp integration progress
  fix encoding of non-generic jsonrpc response
  add baobab methods for easier mcp integration
  implement more code functionalities
  reorganize and sort mcps
  manual updates

# Conflicts:
#	lib/mcp/README.md
2025-04-01 09:53:51 +02:00
ad37a041ab ... 2025-04-01 09:52:52 +02:00
44daea4447 ... 2025-03-31 21:44:54 +02:00
6989a4da13 ... 2025-03-31 21:38:42 +02:00
de4583691c ... 2025-03-31 21:30:54 +02:00
d8c9b07a51 ... 2025-03-31 21:22:05 +02:00
54d31f40b2 ... 2025-03-31 09:53:49 +02:00
ec73b5ff34 Merge branch 'development' of https://github.com/freeflowuniverse/herolib into development 2025-03-31 09:33:05 +02:00
9fcdcc3aff ... 2025-03-31 09:32:58 +02:00
05ab2b68f4 ... 2025-03-31 09:32:03 +02:00
79330ef8f5 ... 2025-03-29 13:48:24 +01:00
45ed369a78 ... 2025-03-29 11:13:16 +01:00
37c17fc7da ... 2025-03-29 08:23:33 +01:00
23640d2647 ... 2025-03-29 08:09:04 +01:00
be54ec8302 ... 2025-03-29 07:34:15 +01:00
638f81a781 ... 2025-03-29 07:21:50 +01:00
e00306b6f8 ... 2025-03-29 07:17:20 +01:00
3fec1c38a1 ,,, 2025-03-29 07:06:32 +01:00
edc9e3c150 Merge branch 'development_action007_mahmoud' into development
* development_action007_mahmoud:
  feat: Add Qdrant destroy action and improve installation robustness
  feat: Improve Qdrant installer and update health check port
  feat: Enhance Qdrant client example script
  feat: Add ignore rules for storage and initialization files
  feat: Add index management and scroll functionality to Qdrant client
2025-03-29 05:59:19 +01:00
a155122898 Merge branch 'development_actions007' into development
* development_actions007:
  add vfs method to local vfs
  add actor gen func
  mcp fixes
  code module fixes
  Fix sidebar paths to include top-level directory names in wiki configuration
2025-03-29 05:59:15 +01:00
f0552f38a0 ... 2025-03-29 05:58:09 +01:00
Timur Gordon
f99419371a progress in mail code gen 2025-03-28 20:52:19 +01:00
Timur Gordon
86d47c218b vcode and baobab mcps enhancements 2025-03-28 19:47:26 +01:00
Timur Gordon
bd5cafbad7 generator mcp integration progress 2025-03-28 12:38:57 +01:00
Timur Gordon
b71362eb9a fix encoding of non-generic jsonrpc response 2025-03-28 12:22:36 +01:00
Timur Gordon
673d982360 add baobab methods for easier mcp integration 2025-03-28 12:22:19 +01:00
Timur Gordon
712b46864a implement more code functionalities 2025-03-28 12:21:55 +01:00
Timur Gordon
186c3aae59 reorganize and sort mcps 2025-03-28 12:20:56 +01:00
Mahmoud Emad
0794fe948b feat: Add Qdrant destroy action and improve installation robustness
- Added a `destroy` action to completely remove Qdrant, including
  its data directory and zinit service. This improves the cleanup
  process and prevents leftover files.
- Improved the `installed` check to be more reliable by directly
  checking the Qdrant version without relying on sourcing the
  profile.  This avoids potential issues with profile setup.
- Added more informative logging messages throughout the process to
  improve user experience and debugging.
- Improved error handling and reporting.
- Use zinit to manage qdrant service to ensure proper stop and remove.
2025-03-27 16:40:25 +02:00
Mahmoud Emad
ba2d6e4310 feat: Improve Qdrant installer and update health check port
- Update Qdrant startup command to use screen for better management.
- Change health check URL to use the correct port (6336).
- Improve Qdrant installation check by directly checking the binary.
- Simplify Qdrant version check.
- Remove unnecessary imports and unused functions.
- Update Qdrant config file with correct HTTP and gRPC ports.
- Add symlink creation in /usr/local/bin for improved usability.
- Use ~/hero/bin for all platforms to avoid permission issues.
2025-03-27 13:18:41 +02:00
Timur Gordon
b9e5d14b48 Merge pull request #88 from freeflowuniverse/development_fix_mail_vfs
Get mail VFS component to work
2025-03-26 20:34:05 +01:00
Timur Gordon
bf26b0af1d manual updates 2025-03-26 19:31:07 +01:00
Timur Gordon
8e82b2865b add vfs method to local vfs 2025-03-26 19:30:28 +01:00
Timur Gordon
367340d69d Merge branch 'development_actions007' of github.com:freeflowuniverse/herolib into development_actions007 2025-03-26 19:27:57 +01:00
Timur Gordon
0b77c73809 add actor gen func 2025-03-26 19:26:45 +01:00
Timur Gordon
51b432d911 mcp fixes 2025-03-26 19:26:03 +01:00
Timur Gordon
f7a679b2a3 code module fixes 2025-03-26 19:25:32 +01:00
Mahmoud Emad
15c9d60760 Merge branch 'development' into development_action007_mahmoud 2025-03-26 14:32:46 +02:00
Mahmoud Emad
c69b53fd4e feat: Enhance Qdrant client example script
- Added comprehensive error handling to all Qdrant client calls,
  improving robustness and providing informative error messages.
- Included logging statements to track script execution and provide
  feedback on each step.
- Added checks for Qdrant server health and service information before
  proceeding with other operations.
- Expanded the script to demonstrate more Qdrant client functionalities,
  including listing collections, checking collection existence, and
  retrieving and upserting points.
- Improved clarity and readability of the script by adding comments and
  better structuring the code.
2025-03-26 14:30:22 +02:00
Timur Gordon
9cdab1f392 Fix sidebar paths to include top-level directory names in wiki configuration 2025-03-26 13:28:10 +01:00
Mahmoud Emad
34656cf1f9 feat: Add ignore rules for storage and initialization files
- Add `.gitignore` rules to exclude the `storage/` directory and
  `.qdrant-initialized` file.
2025-03-26 14:00:57 +02:00
Mahmoud Emad
cf98822749 feat: Add index management and scroll functionality to Qdrant client
- Add support for creating and deleting indexes in Qdrant collections.
- Implement scrolling functionality for retrieving points in batches.
- Enhance point retrieval with options for including payload and vector.
- Add comprehensive error handling for all new operations.
- Introduce new structures for parameters and responses.
2025-03-26 13:34:07 +02:00
Mahmoud Emad
2335d14623 refactor: Remove unnecessary import in local filesystem model
- Removed unused `os` import from `model_fsentry.v`. This
  improves code clarity and reduces unnecessary dependencies.

- Updated `vfs_implementation_test.v` to use the correct
  import paths for mail-related modules.
2025-03-26 12:15:38 +02:00
4a72698402 Merge branch 'development' of github.com:freeflowuniverse/herolib into development 2025-03-24 19:49:59 +01:00
fcb857f756 Merge pull request #86 from freeflowuniverse/development_fixci
Fix CI
2025-03-24 12:30:01 +01:00
Mahmoud Emad
ba07f85fd8 feat: Improve Redis startup handling in install script
- Handle Redis startup differently for GitHub Actions on macOS:
  Start redis directly instead of relying on `brew services`.
- Improved error handling for redis start failures.
- Enhanced logging for better troubleshooting.
2025-03-24 13:12:48 +02:00
Scott Yeager
54dc3d3f1f Update griddriver to use prebuilt binary 2025-03-06 18:26:33 -08:00
834c413cfc Merge branch 'development' of github.com:freeflowuniverse/herolib into development
* 'development' of github.com:freeflowuniverse/herolib:
  better heroscript encoding and support for ourtime
2025-03-01 05:50:43 -07:00
da9965bdc6 ... 2025-03-01 05:50:36 -07:00
511 changed files with 38014 additions and 11553 deletions

3
.gitignore vendored
View File

@@ -39,9 +39,12 @@ data.ms/
test_basic
cli/hero
.aider*
storage/
.qdrant-initialized
.compile_cache
compile_results.log
tmp
compile_summary.log
.summary_lock
.aider*
*.dylib

3
.roo/mcp.json Normal file
View File

@@ -0,0 +1,3 @@
{
"mcpServers": {}
}

View File

@@ -1,391 +0,0 @@
module developer
import freeflowuniverse.herolib.core.code
import freeflowuniverse.herolib.mcp
import os
// create_mcp_tool_code receives the name of a V language function string, and the path to the module in which it exists.
// returns an MCP Tool code in v for attaching the function to the mcp server
pub fn (d &Developer) create_mcp_tool_code(function_name string, module_path string) !string {
println('DEBUG: Looking for function ${function_name} in module path: ${module_path}')
if !os.exists(module_path) {
println('DEBUG: Module path does not exist: ${module_path}')
return error('Module path does not exist: ${module_path}')
}
function_ := get_function_from_module(module_path, function_name)!
println('Function string found:\n${function_}')
// Try to parse the function
function := code.parse_function(function_) or {
println('Error parsing function: ${err}')
return error('Failed to parse function: ${err}')
}
mut types := map[string]string{}
for param in function.params {
// Check if the type is an Object (struct)
if param.typ is code.Object {
types[param.typ.symbol()] = get_type_from_module(module_path, param.typ.symbol())!
}
}
// Get the result type if it's a struct
mut result_ := ""
if function.result.typ is code.Result {
result_type := (function.result.typ as code.Result).typ
if result_type is code.Object {
result_ = get_type_from_module(module_path, result_type.symbol())!
}
} else if function.result.typ is code.Object {
result_ = get_type_from_module(module_path, function.result.typ.symbol())!
}
tool_name := function.name
tool := d.create_mcp_tool(function_, types)!
handler := d.create_mcp_tool_handler(function_, types, result_)!
str := $tmpl('./templates/tool_code.v.template')
return str
}
// create_mcp_tool parses a V language function string and returns an MCP Tool struct
// function: The V function string including preceding comments
// types: A map of struct names to their definitions for complex parameter types
// result: The type of result of the create_mcp_tool function. Could be simply string, or struct {...}
pub fn (d &Developer) create_mcp_tool_handler(function_ string, types map[string]string, result_ string) !string {
function := code.parse_function(function_)!
decode_stmts := function.params.map(argument_decode_stmt(it)).join_lines()
result := code.parse_type(result_)
str := $tmpl('./templates/tool_handler.v.template')
return str
}
pub fn argument_decode_stmt(param code.Param) string {
return if param.typ is code.Integer {
'${param.name} := arguments["${param.name}"].int()'
} else if param.typ is code.Boolean {
'${param.name} := arguments["${param.name}"].bool()'
} else if param.typ is code.String {
'${param.name} := arguments["${param.name}"].str()'
} else if param.typ is code.Object {
'${param.name} := json.decode[${param.typ.symbol()}](arguments["${param.name}"].str())!'
} else if param.typ is code.Array {
'${param.name} := json.decode[${param.typ.symbol()}](arguments["${param.name}"].str())!'
} else if param.typ is code.Map {
'${param.name} := json.decode[${param.typ.symbol()}](arguments["${param.name}"].str())!'
} else {
panic('Unsupported type: ${param.typ}')
}
}
/*
in @generate_mcp.v , implement a create_mpc_tool_handler function that given a vlang function string and the types that map to their corresponding type definitions (for instance struct some_type: SomeType{...}), generates a vlang function such as the following:
ou
pub fn (d &Developer) create_mcp_tool_tool_handler(arguments map[string]Any) !mcp.Tool {
function := arguments['function'].str()
types := json.decode[map[string]string](arguments['types'].str())!
return d.create_mcp_tool(function, types)
}
*/
// create_mcp_tool parses a V language function string and returns an MCP Tool struct
// function: The V function string including preceding comments
// types: A map of struct names to their definitions for complex parameter types
pub fn (d Developer) create_mcp_tool(function string, types map[string]string) !mcp.Tool {
// Extract description from preceding comments
mut description := ''
lines := function.split('\n')
// Find function signature line
mut fn_line_idx := -1
for i, line in lines {
if line.trim_space().starts_with('fn ') || line.trim_space().starts_with('pub fn ') {
fn_line_idx = i
break
}
}
if fn_line_idx == -1 {
return error('Invalid function: no function signature found')
}
// Extract comments before the function
for i := 0; i < fn_line_idx; i++ {
line := lines[i].trim_space()
if line.starts_with('//') {
// Remove the comment marker and any leading space
comment := line[2..].trim_space()
if description != '' {
description += '\n'
}
description += comment
}
}
// Parse function signature
fn_signature := lines[fn_line_idx].trim_space()
// Extract function name
mut fn_name := ''
// Check if this is a method with a receiver
if fn_signature.contains('fn (') {
// This is a method with a receiver
// Format: [pub] fn (receiver Type) name(...)
// Find the closing parenthesis of the receiver
mut receiver_end := fn_signature.index(')') or { return error('Invalid method signature: missing closing parenthesis for receiver') }
// Extract the text after the receiver
mut after_receiver := fn_signature[receiver_end + 1..].trim_space()
// Extract the function name (everything before the opening parenthesis)
mut params_start := after_receiver.index('(') or { return error('Invalid method signature: missing parameters') }
fn_name = after_receiver[0..params_start].trim_space()
} else if fn_signature.starts_with('pub fn ') {
// Regular public function
mut prefix_len := 'pub fn '.len
mut params_start := fn_signature.index('(') or { return error('Invalid function signature: missing parameters') }
fn_name = fn_signature[prefix_len..params_start].trim_space()
} else if fn_signature.starts_with('fn ') {
// Regular function
mut prefix_len := 'fn '.len
mut params_start := fn_signature.index('(') or { return error('Invalid function signature: missing parameters') }
fn_name = fn_signature[prefix_len..params_start].trim_space()
} else {
return error('Invalid function signature: must start with "fn" or "pub fn"')
}
if fn_name == '' {
return error('Could not extract function name')
}
// Extract parameters
mut params_str := ''
// Check if this is a method with a receiver
if fn_signature.contains('fn (') {
// This is a method with a receiver
// Find the closing parenthesis of the receiver
mut receiver_end := fn_signature.index(')') or { return error('Invalid method signature: missing closing parenthesis for receiver') }
// Find the opening parenthesis of the parameters
mut params_start := -1
for i := receiver_end + 1; i < fn_signature.len; i++ {
if fn_signature[i] == `(` {
params_start = i
break
}
}
if params_start == -1 {
return error('Invalid method signature: missing parameter list')
}
// Find the closing parenthesis of the parameters
mut params_end := fn_signature.last_index(')') or { return error('Invalid method signature: missing closing parenthesis for parameters') }
// Extract the parameters
params_str = fn_signature[params_start + 1..params_end].trim_space()
} else {
// Regular function
mut params_start := fn_signature.index('(') or { return error('Invalid function signature: missing parameters') }
mut params_end := fn_signature.last_index(')') or { return error('Invalid function signature: missing closing parenthesis') }
// Extract the parameters
params_str = fn_signature[params_start + 1..params_end].trim_space()
}
// Create input schema for parameters
mut properties := map[string]mcp.ToolProperty{}
mut required := []string{}
if params_str != '' {
param_list := params_str.split(',')
for param in param_list {
trimmed_param := param.trim_space()
if trimmed_param == '' {
continue
}
// Split parameter into name and type
param_parts := trimmed_param.split_any(' \t')
if param_parts.len < 2 {
continue
}
param_name := param_parts[0]
param_type := param_parts[1]
// Add to required parameters
required << param_name
// Create property for this parameter
mut property := mcp.ToolProperty{}
// Check if this is a complex type defined in the types map
if param_type in types {
// Parse the struct definition to create a nested schema
struct_def := types[param_type]
struct_schema := d.create_mcp_tool_input_schema(struct_def)!
property = mcp.ToolProperty{
typ: struct_schema.typ
}
} else {
// Handle primitive types
schema := d.create_mcp_tool_input_schema(param_type)!
property = mcp.ToolProperty{
typ: schema.typ
}
}
properties[param_name] = property
}
}
// Create the input schema
input_schema := mcp.ToolInputSchema{
typ: 'object',
properties: properties,
required: required
}
// Create and return the Tool
return mcp.Tool{
name: fn_name,
description: description,
input_schema: input_schema
}
}
// create_mcp_tool_input_schema creates a ToolInputSchema for a given input type
// input: The input type string
// returns: A ToolInputSchema for the given input type
// errors: Returns an error if the input type is not supported
pub fn (d Developer) create_mcp_tool_input_schema(input string) !mcp.ToolInputSchema {
// if input is a primitive type, return a mcp ToolInputSchema with that type
if input == 'string' {
return mcp.ToolInputSchema{
typ: 'string'
}
} else if input == 'int' {
return mcp.ToolInputSchema{
typ: 'integer'
}
} else if input == 'float' {
return mcp.ToolInputSchema{
typ: 'number'
}
} else if input == 'bool' {
return mcp.ToolInputSchema{
typ: 'boolean'
}
}
// if input is a struct, return a mcp ToolInputSchema with typ 'object' and properties for each field in the struct
if input.starts_with('pub struct ') {
struct_name := input[11..].split(' ')[0]
fields := parse_struct_fields(input)
mut properties := map[string]mcp.ToolProperty{}
for field_name, field_type in fields {
property := mcp.ToolProperty{
typ: d.create_mcp_tool_input_schema(field_type)!.typ
}
properties[field_name] = property
}
return mcp.ToolInputSchema{
typ: 'object',
properties: properties
}
}
// if input is an array, return a mcp ToolInputSchema with typ 'array' and items of the item type
if input.starts_with('[]') {
item_type := input[2..]
// For array types, we create a schema with type 'array'
// The actual item type is determined by the primitive type
mut item_type_str := 'string' // default
if item_type == 'int' {
item_type_str = 'integer'
} else if item_type == 'float' {
item_type_str = 'number'
} else if item_type == 'bool' {
item_type_str = 'boolean'
}
// Create a property for the array items
mut property := mcp.ToolProperty{
typ: 'array'
}
// Add the property to the schema
mut properties := map[string]mcp.ToolProperty{}
properties['items'] = property
return mcp.ToolInputSchema{
typ: 'array',
properties: properties
}
}
// Default to string type for unknown types
return mcp.ToolInputSchema{
typ: 'string'
}
}
// parse_struct_fields parses a V language struct definition string and returns a map of field names to their types
fn parse_struct_fields(struct_def string) map[string]string {
mut fields := map[string]string{}
// Find the opening and closing braces of the struct definition
start_idx := struct_def.index('{') or { return fields }
end_idx := struct_def.last_index('}') or { return fields }
// Extract the content between the braces
struct_content := struct_def[start_idx + 1..end_idx].trim_space()
// Split the content by newlines to get individual field definitions
field_lines := struct_content.split('
')
for line in field_lines {
trimmed_line := line.trim_space()
// Skip empty lines and comments
if trimmed_line == '' || trimmed_line.starts_with('//') {
continue
}
// Handle pub: or mut: prefixes
mut field_def := trimmed_line
if field_def.starts_with('pub:') || field_def.starts_with('mut:') {
field_def = field_def.all_after(':').trim_space()
}
// Split by whitespace to separate field name and type
parts := field_def.split_any(' ')
if parts.len < 2 {
continue
}
field_name := parts[0]
field_type := parts[1..].join(' ')
// Handle attributes like @[json: 'name']
if field_name.contains('@[') {
continue
}
fields[field_name] = field_type
}
return fields
}

View File

@@ -1,205 +0,0 @@
module developer
import freeflowuniverse.herolib.mcp
import json
import os
// fn test_parse_struct_fields() {
// // Test case 1: Simple struct with primitive types
// simple_struct := 'pub struct User {
// name string
// age int
// active bool
// }'
// fields := parse_struct_fields(simple_struct)
// assert fields.len == 3
// assert fields['name'] == 'string'
// assert fields['age'] == 'int'
// assert fields['active'] == 'bool'
// // Test case 2: Struct with pub: and mut: sections
// complex_struct := 'pub struct Config {
// pub:
// host string
// port int
// mut:
// connected bool
// retries int
// }'
// fields2 := parse_struct_fields(complex_struct)
// assert fields2.len == 4
// assert fields2['host'] == 'string'
// assert fields2['port'] == 'int'
// assert fields2['connected'] == 'bool'
// assert fields2['retries'] == 'int'
// // Test case 3: Struct with attributes and comments
// struct_with_attrs := 'pub struct ApiResponse {
// // User ID
// id int
// // User full name
// name string @[json: "full_name"]
// // Whether account is active
// active bool
// }'
// fields3 := parse_struct_fields(struct_with_attrs)
// assert fields3.len == 3 // All fields are included
// assert fields3['id'] == 'int'
// assert fields3['active'] == 'bool'
// // Test case 4: Empty struct
// empty_struct := 'pub struct Empty {}'
// fields4 := parse_struct_fields(empty_struct)
// assert fields4.len == 0
// println('test_parse_struct_fields passed')
// }
// fn test_create_mcp_tool_input_schema() {
// d := Developer{}
// // Test case 1: Primitive types
// string_schema := d.create_mcp_tool_input_schema('string') or { panic(err) }
// assert string_schema.typ == 'string'
// int_schema := d.create_mcp_tool_input_schema('int') or { panic(err) }
// assert int_schema.typ == 'integer'
// float_schema := d.create_mcp_tool_input_schema('float') or { panic(err) }
// assert float_schema.typ == 'number'
// bool_schema := d.create_mcp_tool_input_schema('bool') or { panic(err) }
// assert bool_schema.typ == 'boolean'
// // Test case 2: Array type
// array_schema := d.create_mcp_tool_input_schema('[]string') or { panic(err) }
// assert array_schema.typ == 'array'
// // In our implementation, arrays don't have items directly in the schema
// // Test case 3: Struct type
// struct_def := 'pub struct Person {
// name string
// age int
// }'
// struct_schema := d.create_mcp_tool_input_schema(struct_def) or { panic(err) }
// assert struct_schema.typ == 'object'
// assert struct_schema.properties.len == 2
// assert struct_schema.properties['name'].typ == 'string'
// assert struct_schema.properties['age'].typ == 'integer'
// println('test_create_mcp_tool_input_schema passed')
// }
// fn test_create_mcp_tool() {
// d := Developer{}
// // Test case 1: Simple function with primitive types
// simple_fn := '// Get user by ID
// // Returns user information
// pub fn get_user(id int, include_details bool) {
// // Implementation
// }'
// tool1 := d.create_mcp_tool(simple_fn, {}) or { panic(err) }
// assert tool1.name == 'get_user'
// expected_desc1 := 'Get user by ID\nReturns user information'
// assert tool1.description == expected_desc1
// assert tool1.input_schema.typ == 'object'
// assert tool1.input_schema.properties.len == 2
// assert tool1.input_schema.properties['id'].typ == 'integer'
// assert tool1.input_schema.properties['include_details'].typ == 'boolean'
// assert tool1.input_schema.required.len == 2
// assert 'id' in tool1.input_schema.required
// assert 'include_details' in tool1.input_schema.required
// // Test case 2: Method with receiver
// method_fn := '// Update user profile
// pub fn (u User) update_profile(name string, age int) bool {
// // Implementation
// return true
// }'
// tool2 := d.create_mcp_tool(method_fn, {}) or { panic(err) }
// assert tool2.name == 'update_profile'
// assert tool2.description == 'Update user profile'
// assert tool2.input_schema.properties.len == 2
// assert tool2.input_schema.properties['name'].typ == 'string'
// assert tool2.input_schema.properties['age'].typ == 'integer'
// // Test case 3: Function with complex types
// complex_fn := '// Create new configuration
// // Sets up system configuration
// fn create_config(name string, settings Config) !Config {
// // Implementation
// }'
// config_struct := 'pub struct Config {
// server_url string
// max_retries int
// timeout float
// }'
// tool3 := d.create_mcp_tool(complex_fn, {
// 'Config': config_struct
// }) or { panic(err) }
// assert tool3.name == 'create_config'
// expected_desc3 := 'Create new configuration\nSets up system configuration'
// assert tool3.description == expected_desc3
// assert tool3.input_schema.properties.len == 2
// assert tool3.input_schema.properties['name'].typ == 'string'
// assert tool3.input_schema.properties['settings'].typ == 'object'
// // Test case 4: Function with no parameters
// no_params_fn := '// Initialize system
// pub fn initialize() {
// // Implementation
// }'
// tool4 := d.create_mcp_tool(no_params_fn, {}) or { panic(err) }
// assert tool4.name == 'initialize'
// assert tool4.description == 'Initialize system'
// assert tool4.input_schema.properties.len == 0
// assert tool4.input_schema.required.len == 0
// println('test_create_mcp_tool passed')
// }
// fn test_create_mcp_tool_code() {
// d := Developer{}
// // Test with the complex function that has struct parameters and return type
// module_path := "${os.dir(@FILE)}/testdata/mock_module"
// function_name := 'test_function'
// code := d.create_mcp_tool_code(function_name, module_path) or {
// panic('Failed to create MCP tool code: ${err}')
// }
// // Print the code instead of panic for debugging
// println('Generated code:')
// println('----------------------------------------')
// println(code)
// println('----------------------------------------')
// // Verify the generated code contains the expected elements
// assert code.contains('test_function_tool')
// assert code.contains('TestConfig')
// assert code.contains('TestResult')
// // Test with a simple function that has primitive types
// simple_function_name := 'simple_function'
// simple_code := d.create_mcp_tool_code(simple_function_name, module_path) or {
// panic('Failed to create MCP tool code for simple function: ${err}')
// }
// // Verify the simple function code
// assert simple_code.contains('simple_function_tool')
// assert simple_code.contains('name string')
// assert simple_code.contains('count int')
// // println('test_create_mcp_tool_code passed')
// }

View File

@@ -1,108 +0,0 @@
module developer
import freeflowuniverse.herolib.mcp
import x.json2 as json { Any }
// import json
const create_mcp_tool_code_tool = mcp.Tool{
name: 'create_mcp_tool_code'
description: 'create_mcp_tool_code receives the name of a V language function string, and the path to the module in which it exists.
returns an MCP Tool code in v for attaching the function to the mcp server'
input_schema: mcp.ToolInputSchema{
typ: 'object'
properties: {
'function_name': mcp.ToolProperty{
typ: 'string'
items: mcp.ToolItems{
typ: ''
enum: []
}
enum: []
}
'module_path': mcp.ToolProperty{
typ: 'string'
items: mcp.ToolItems{
typ: ''
enum: []
}
enum: []
}
}
required: ['function_name', 'module_path']
}
}
pub fn (d &Developer) create_mcp_tool_code_tool_handler(arguments map[string]Any) !mcp.ToolCallResult {
function_name := arguments['function_name'].str()
module_path := arguments['module_path'].str()
result := d.create_mcp_tool_code(function_name, module_path) or {
return mcp.error_tool_call_result(err)
}
return mcp.ToolCallResult{
is_error: false
content: result_to_mcp_tool_contents[string](result)
}
}
// Tool definition for the create_mcp_tool function
const create_mcp_tool_tool = mcp.Tool{
name: 'create_mcp_tool'
description: 'Parses a V language function string and returns an MCP Tool struct. This tool analyzes function signatures, extracts parameters, and generates the appropriate MCP Tool representation.'
input_schema: mcp.ToolInputSchema{
typ: 'object'
properties: {
'function': mcp.ToolProperty{
typ: 'string'
}
'types': mcp.ToolProperty{
typ: 'object'
}
}
required: ['function']
}
}
pub fn (d &Developer) create_mcp_tool_tool_handler(arguments map[string]Any) !mcp.ToolCallResult {
function := arguments['function'].str()
types := json.decode[map[string]string](arguments['types'].str())!
result := d.create_mcp_tool(function, types) or { return mcp.error_tool_call_result(err) }
return mcp.ToolCallResult{
is_error: false
content: result_to_mcp_tool_contents[string](result.str())
}
}
// Tool definition for the create_mcp_tool_handler function
const create_mcp_tool_handler_tool = mcp.Tool{
name: 'create_mcp_tool_handler'
description: 'Generates a tool handler for the create_mcp_tool function. This tool handler accepts function string and types map and returns an MCP ToolCallResult.'
input_schema: mcp.ToolInputSchema{
typ: 'object'
properties: {
'function': mcp.ToolProperty{
typ: 'string'
}
'types': mcp.ToolProperty{
typ: 'object'
}
'result': mcp.ToolProperty{
typ: 'string'
}
}
required: ['function', 'result']
}
}
// Tool handler for the create_mcp_tool_handler function
pub fn (d &Developer) create_mcp_tool_handler_tool_handler(arguments map[string]Any) !mcp.ToolCallResult {
function := arguments['function'].str()
types := json.decode[map[string]string](arguments['types'].str())!
result_ := arguments['result'].str()
result := d.create_mcp_tool_handler(function, types, result_) or {
return mcp.error_tool_call_result(err)
}
return mcp.ToolCallResult{
is_error: false
content: result_to_mcp_tool_contents[string](result)
}
}

View File

@@ -1,31 +0,0 @@
module developer
import freeflowuniverse.herolib.mcp.logger
import freeflowuniverse.herolib.mcp
import freeflowuniverse.herolib.schemas.jsonrpc
// pub fn new_mcp_server(d &Developer) !&mcp.Server {
// logger.info('Creating new Developer MCP server')
// // Initialize the server with the empty handlers map
// mut server := mcp.new_server(mcp.MemoryBackend{
// tools: {
// 'create_mcp_tool': create_mcp_tool_tool
// 'create_mcp_tool_handler': create_mcp_tool_handler_tool
// 'create_mcp_tool_code': create_mcp_tool_code_tool
// }
// tool_handlers: {
// 'create_mcp_tool': d.create_mcp_tool_tool_handler
// 'create_mcp_tool_handler': d.create_mcp_tool_handler_tool_handler
// 'create_mcp_tool_code': d.create_mcp_tool_code_tool_handler
// }
// }, mcp.ServerParams{
// config: mcp.ServerConfiguration{
// server_info: mcp.ServerInfo{
// name: 'developer'
// version: '1.0.0'
// }
// }
// })!
// return server
// }

View File

@@ -1,10 +0,0 @@
#!/usr/bin/env -S v -n -w -cg -gc none -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.mcp.developer
import freeflowuniverse.herolib.mcp.logger
mut server := developer.new_mcp_server(&developer.Developer{})!
server.start() or {
logger.fatal('Error starting server: ${err}')
exit(1)
}

View File

@@ -1,11 +0,0 @@
pub fn (d &Developer) @{function.name}_tool_handler(arguments map[string]Any) !mcp.ToolCallResult {
@{decode_stmts}
result := d.@{function.name}(@{function.params.map(it.name).join(',')})
or {
return error_tool_call_result(err)
}
return mcp.ToolCallResult{
is_error: false
content: result_to_mcp_tool_content[@{result.symbol()}](result)
}
}

View File

@@ -1,38 +0,0 @@
module mock_module
// TestConfig represents a configuration for testing
pub struct TestConfig {
pub:
name string
enabled bool
count int
value float64
}
// TestResult represents the result of a test operation
pub struct TestResult {
pub:
success bool
message string
code int
}
// test_function is a simple function for testing the MCP tool code generation
// It takes a config and returns a result
pub fn test_function(config TestConfig) !TestResult {
// This is just a mock implementation for testing purposes
if config.name == '' {
return error('Name cannot be empty')
}
return TestResult{
success: config.enabled
message: 'Test completed for ${config.name}'
code: if config.enabled { 0 } else { 1 }
}
}
// simple_function is a function with primitive types for testing
pub fn simple_function(name string, count int) string {
return '${name} count: ${count}'
}

View File

@@ -1,34 +0,0 @@
module developer
import freeflowuniverse.herolib.mcp
const get_function_from_file_tool = mcp.Tool{
name: 'get_function_from_file'
description: 'get_function_from_file parses a V file and extracts a specific function block including its comments
ARGS:
file_path string - path to the V file
function_name string - name of the function to extract
RETURNS: string - the function block including comments, or empty string if not found'
input_schema: mcp.ToolInputSchema{
typ: 'object'
properties: {
'file_path': mcp.ToolProperty{
typ: 'string'
items: mcp.ToolItems{
typ: ''
enum: []
}
enum: []
}
'function_name': mcp.ToolProperty{
typ: 'string'
items: mcp.ToolItems{
typ: ''
enum: []
}
enum: []
}
}
required: ['file_path', 'function_name']
}
}

View File

77
cfg/config.heroscript Normal file
View File

@@ -0,0 +1,77 @@
!!docusaurus.config
name:"my-documentation"
title:"My Documentation Site"
tagline:"Documentation made simple with V and Docusaurus"
url:"https://docs.example.com"
url_home:"docs/"
base_url:"/"
favicon:"img/favicon.png"
image:"img/hero.png"
copyright:"© 2025 Example Organization"
!!docusaurus.config_meta
description:"Comprehensive documentation for our amazing project"
image:"https://docs.example.com/img/social-card.png"
title:"My Documentation | Official Docs"
!!docusaurus.ssh_connection
name:"production"
host:"example.com"
login:"deploy"
port:22
key_path:"~/.ssh/id_rsa"
!!docusaurus.build_dest
ssh_name:"production"
path:"/var/www/docs"
!!docusaurus.navbar
title:"My Project"
!!docusaurus.navbar_item
label:"Documentation"
href:"/docs"
position:"left"
!!docusaurus.navbar_item
label:"API"
href:"/api"
position:"left"
!!docusaurus.navbar_item
label:"GitHub"
href:"https://github.com/example/repo"
position:"right"
!!docusaurus.footer
style:"dark"
!!docusaurus.footer_item
title:"Documentation"
label:"Introduction"
to:"/docs"
!!docusaurus.footer_item
title:"Documentation"
label:"API Reference"
to:"/api"
!!docusaurus.footer_item
title:"Community"
label:"GitHub"
href:"https://github.com/example/repo"
!!docusaurus.footer_item
title:"Community"
label:"Discord"
href:"https://discord.gg/example"
!!docusaurus.footer_item
title:"More"
label:"Blog"
href:"https://blog.example.com"
!!docusaurus.import_source
url:"https://github.com/example/external-docs"
dest:"external"
replace:"PROJECT_NAME:My Project, VERSION:1.0.0"

View File

@@ -28,7 +28,7 @@ fn get_platform_id() string {
}
fn read_secrets() ! {
secret_file := os.join_path(os.home_dir(), 'code/git.ourworld.tf/despiegk/hero_secrets/mysecrets.sh')
secret_file := os.join_path(os.home_dir(), 'code/git.threefold.info/despiegk/hero_secrets/mysecrets.sh')
if os.exists(secret_file) {
println('Reading secrets from ${secret_file}')
content := os.read_file(secret_file)!

View File

@@ -19,25 +19,24 @@ fn playcmds_do(path string) ! {
}
fn do() ! {
if ! core.is_osx()! {
if !core.is_osx()! {
if os.getenv('SUDO_COMMAND') != '' || os.getenv('SUDO_USER') != '' {
println('Error: Please do not run this program with sudo!')
exit(1) // Exit with error code
exit(1) // Exit with error code
}
}
if os.getuid() == 0 {
if core.is_osx()! {
eprintln("please do not run hero as root in osx.")
if os.getuid() == 0 {
if core.is_osx()! {
eprintln('please do not run hero as root in osx.')
exit(1)
}
} else {
if ! core.is_osx()! {
} else {
if !core.is_osx()! {
eprintln("please do run hero as root, don't use sudo.")
exit(1)
}
}
}
if os.args.len == 2 {
mypath := os.args[1]
@@ -51,7 +50,7 @@ fn do() ! {
mut cmd := Command{
name: 'hero'
description: 'Your HERO toolset.'
version: '1.0.22'
version: '1.0.26'
}
// herocmds.cmd_run_add_flags(mut cmd)
@@ -115,4 +114,4 @@ fn main() {
fn pre_func(cmd Command) ! {
herocmds.plbook_run(cmd)!
}
}

View File

@@ -73,9 +73,9 @@ function sshknownkeysadd {
then
ssh-keyscan github.com >> ~/.ssh/known_hosts
fi
if ! grep git.ourworld.tf ~/.ssh/known_hosts > /dev/null
if ! grep git.threefold.info ~/.ssh/known_hosts > /dev/null
then
ssh-keyscan git.ourworld.tf >> ~/.ssh/known_hosts
ssh-keyscan git.threefold.info >> ~/.ssh/known_hosts
fi
git config --global pull.rebase false

71
examples/aiexamples/groq.vsh Executable file
View File

@@ -0,0 +1,71 @@
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run
module main
import freeflowuniverse.herolib.clients.openai
import os
fn test1(mut client openai.OpenAI) ! {
instruction := '
You are a template language converter. You convert Pug templates to Jet templates.
The target template language, Jet, is defined as follows:
'
// Create a chat completion request
res := client.chat_completion(
msgs: openai.Messages{
messages: [
openai.Message{
role: .user
content: 'What are the key differences between Groq and other AI inference providers?'
},
]
}
)!
// Print the response
println('\nGroq AI Response:')
println('==================')
println(res.choices[0].message.content)
println('\nUsage Statistics:')
println('Prompt tokens: ${res.usage.prompt_tokens}')
println('Completion tokens: ${res.usage.completion_tokens}')
println('Total tokens: ${res.usage.total_tokens}')
}
fn test2(mut client openai.OpenAI) ! {
// Create a chat completion request
res := client.chat_completion(
model: 'deepseek-r1-distill-llama-70b'
msgs: openai.Messages{
messages: [
openai.Message{
role: .user
content: 'A story of 10 lines?'
},
]
}
)!
println('\nGroq AI Response:')
println('==================')
println(res.choices[0].message.content)
println('\nUsage Statistics:')
println('Prompt tokens: ${res.usage.prompt_tokens}')
println('Completion tokens: ${res.usage.completion_tokens}')
println('Total tokens: ${res.usage.total_tokens}')
}
println("
TO USE:
export AIKEY='gsk_...'
export AIURL='https://api.groq.com/openai/v1'
export AIMODEL='llama-3.3-70b-versatile'
")
mut client := openai.get(name: 'test')!
println(client)
// test1(mut client)!
test2(mut client)!

View File

@@ -0,0 +1,7 @@
#!/usr/bin/env -S v -n -w -cg -gc none -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.mcp.aitools
// aitools.convert_pug("/root/code/github/freeflowuniverse/herolauncher/pkg/herolauncher/web/templates/admin")!
aitools.convert_pug('/root/code/github/freeflowuniverse/herolauncher/pkg/zaz/webui/templates')!

View File

@@ -1,69 +0,0 @@
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.clients.jina
import freeflowuniverse.herolib.osal
import os
// Example of using the Jina client
fn main() {
// Set environment variable for testing
// In production, you would set this in your environment
// osal.env_set(key: 'JINAKEY', value: 'your-api-key')
// Check if JINAKEY environment variable exists
if !osal.env_exists('JINAKEY') {
println('JINAKEY environment variable not set. Please set it before running this example.')
exit(1)
}
// Create a Jina client instance
mut client := jina.get(name: 'default')!
println('Jina client initialized successfully.')
// Example: Create embeddings
model := 'jina-embeddings-v3'
texts := ['Hello, world!', 'How are you doing?']
println('Creating embeddings for texts: ${texts}')
result := client.create_embeddings(texts, model, 'retrieval.query')!
println('Embeddings created successfully.')
println('Model: ${result['model']}')
println('Data count: ${result['data'].arr().len}')
// Example: List classifiers
println('\nListing classifiers:')
classifiers := client.list_classifiers() or {
println('Failed to list classifiers: ${err}')
return
}
println('Classifiers retrieved successfully.')
// Example: Create a classifier
println('\nTraining a classifier:')
examples := [
jina.TrainingExample{
text: 'This movie was great!'
label: 'positive'
},
jina.TrainingExample{
text: 'I did not like this movie.'
label: 'negative'
},
jina.TrainingExample{
text: 'The movie was okay.'
label: 'neutral'
},
]
training_result := client.train(examples, model, 'private') or {
println('Failed to train classifier: ${err}')
return
}
println('Classifier trained successfully.')
println('Classifier ID: ${training_result['classifier_id']}')
}

128
examples/aiexamples/qdrant.vsh Executable file
View File

@@ -0,0 +1,128 @@
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.clients.qdrant
import freeflowuniverse.herolib.installers.db.qdrant_installer
import freeflowuniverse.herolib.core.httpconnection
import rand
import os
println('Starting Qdrant example script')
// Print environment information
println('Current directory: ${os.getwd()}')
println('Home directory: ${os.home_dir()}')
mut i := qdrant_installer.get()!
i.install()!
// 1. Get the qdrant client
println('Getting Qdrant client...')
mut qdrant_client := qdrant.get()!
println('Qdrant client URL: ${qdrant_client.url}')
// Check if Qdrant server is running
println('Checking Qdrant server health...')
health := qdrant_client.health_check() or {
println('Error checking health: ${err}')
false
}
println('Qdrant server health: ${health}')
// Get service info
println('Getting Qdrant service info...')
service_info := qdrant_client.get_service_info() or {
println('Error getting service info: ${err}')
exit(1)
}
println('Qdrant service info: ${service_info}')
// 2. Generate collection name
collection_name := 'collection_' + rand.string(4)
println('Generated collection name: ${collection_name}')
// 3. Create a new collection
println('Creating collection...')
created_collection := qdrant_client.create_collection(
collection_name: collection_name
size: 15
distance: 'Cosine'
) or {
println('Error creating collection: ${err}')
exit(1)
}
println('Created Collection: ${created_collection}')
// 4. Get the created collection
println('Getting collection...')
get_collection := qdrant_client.get_collection(
collection_name: collection_name
) or {
println('Error getting collection: ${err}')
exit(1)
}
println('Get Collection: ${get_collection}')
// 5. List all collections
println('Listing collections...')
list_collection := qdrant_client.list_collections() or {
println('Error listing collections: ${err}')
exit(1)
}
println('List Collection: ${list_collection}')
// 6. Check collection existence
println('Checking collection existence...')
collection_existence := qdrant_client.is_collection_exists(
collection_name: collection_name
) or {
println('Error checking collection existence: ${err}')
exit(1)
}
println('Collection Existence: ${collection_existence}')
// 7. Retrieve points
println('Retrieving points...')
collection_points := qdrant_client.retrieve_points(
collection_name: collection_name
ids: [
0,
3,
100,
]
) or {
println('Error retrieving points: ${err}')
exit(1)
}
println('Collection Points: ${collection_points}')
// 8. Upsert points
println('Upserting points...')
upsert_points := qdrant_client.upsert_points(
collection_name: collection_name
points: [
qdrant.Point{
payload: {
'key': 'value'
}
vector: [1.0, 2.0, 3.0]
},
qdrant.Point{
payload: {
'key': 'value'
}
vector: [4.0, 5.0, 6.0]
},
qdrant.Point{
payload: {
'key': 'value'
}
vector: [7.0, 8.0, 9.0]
},
]
) or {
println('Error upserting points: ${err}')
exit(1)
}
println('Upsert Points: ${upsert_points}')
println('Qdrant example script completed successfully')

View File

@@ -5,7 +5,7 @@ import freeflowuniverse.herolib.core.playbook
import os
mut plbook := playbook.new(
path: '${os.home_dir()}/code/git.ourworld.tf/ourworld_holding/investorstool/output'
path: '${os.home_dir()}/code/git.threefold.info/ourworld_holding/investorstool/output'
)!
mut it := investortool.play(mut plbook)!
it.check()!

View File

@@ -12,7 +12,7 @@ import os
const name = 'tf9_budget'
const wikipath = '${os.home_dir()}/code/git.ourworld.tf/ourworld_holding/info_ourworld/collections/${name}'
const wikipath = '${os.home_dir()}/code/git.threefold.info/ourworld_holding/info_ourworld/collections/${name}'
const summarypath = '${wikipath}/summary.md'
// mut sh := spreadsheet.sheet_new(name: 'test2') or { panic(err) }

View File

@@ -1 +0,0 @@
export GROQ_API_KEY="your-groq-api-key-here"

View File

@@ -1,64 +0,0 @@
# Groq AI Client Example
This example demonstrates how to use Groq's AI API with the herolib OpenAI client. Groq provides API compatibility with OpenAI's client libraries, allowing you to leverage Groq's fast inference speeds with minimal changes to your existing code.
## Prerequisites
- V programming language installed
- A Groq API key (get one from [Groq's website](https://console.groq.com/keys))
## Setup
1. Copy the `.env.example` file to `.env`:
```bash
cp .env.example .env
```
2. Edit the `.env` file and replace `your-groq-api-key-here` with your actual Groq API key.
3. Load the environment variables:
```bash
source .env
```
## Running the Example
Execute the script with:
```bash
v run groq_client.vsh
```
Or make it executable first:
```bash
chmod +x groq_client.vsh
./groq_client.vsh
```
## How It Works
The example uses the existing OpenAI client from herolib but configures it to use Groq's API endpoint:
1. It retrieves the Groq API key from the environment variables
2. Configures the OpenAI client with the Groq API key
3. Overrides the default OpenAI URL with Groq's API URL (`https://api.groq.com/openai/v1`)
4. Sends a chat completion request to Groq's API
5. Displays the response
## Supported Models
Groq supports various models including:
- llama2-70b-4096
- mixtral-8x7b-32768
- gemma-7b-it
For a complete and up-to-date list of supported models, refer to the [Groq API documentation](https://console.groq.com/docs/models).
## Notes
- The example uses the `gpt_3_5_turbo` enum from the OpenAI client, but Groq will automatically map this to an appropriate model on their end.
- For production use, you may want to explicitly specify one of Groq's supported models.

View File

@@ -1,46 +0,0 @@
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run
module main
import freeflowuniverse.herolib.clients.openai
import os
fn main() {
// Get API key from environment variable
key := os.getenv('GROQ_API_KEY')
if key == '' {
println('Error: GROQ_API_KEY environment variable not set')
println('Please set it by running: source .env')
exit(1)
}
// Get the configured client
mut client := openai.OpenAI{
name: 'groq'
api_key: key
server_url: 'https://api.groq.com/openai/v1'
}
// Define the model and message for chat completion
// Note: Use a model that Groq supports, like llama2-70b-4096 or mixtral-8x7b-32768
model := 'qwen-2.5-coder-32b'
// Create a chat completion request
res := client.chat_completion(model, openai.Messages{
messages: [
openai.Message{
role: .user
content: 'What are the key differences between Groq and other AI inference providers?'
},
]
})!
// Print the response
println('\nGroq AI Response:')
println('==================')
println(res.choices[0].message.content)
println('\nUsage Statistics:')
println('Prompt tokens: ${res.usage.prompt_tokens}')
println('Completion tokens: ${res.usage.completion_tokens}')
println('Total tokens: ${res.usage.total_tokens}')
}

257
examples/clients/mycelium_rpc.vsh Executable file
View File

@@ -0,0 +1,257 @@
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run
// Mycelium RPC Client Example
// This example demonstrates how to use the new Mycelium JSON-RPC client
// to interact with a Mycelium node's admin API
import freeflowuniverse.herolib.clients.mycelium_rpc
import freeflowuniverse.herolib.installers.net.mycelium_installer
import time
import os
import encoding.base64
const mycelium_port = 8990
fn terminate_mycelium() ! {
// Try to find and kill any running mycelium process
res := os.execute('pkill mycelium')
if res.exit_code == 0 {
println('Terminated existing mycelium processes')
time.sleep(1 * time.second)
}
}
fn start_mycelium_node() ! {
// Start a mycelium node with JSON-RPC API enabled
println('Starting Mycelium node with JSON-RPC API on port ${mycelium_port}...')
// Create directory for mycelium data
os.execute('mkdir -p /tmp/mycelium_rpc_example')
// Start mycelium in background with both HTTP and JSON-RPC APIs enabled
spawn fn () {
cmd := 'cd /tmp/mycelium_rpc_example && mycelium --peers tcp://185.69.166.8:9651 quic://[2a02:1802:5e:0:ec4:7aff:fe51:e36b]:9651 tcp://65.109.18.113:9651 --tun-name tun_rpc_example --tcp-listen-port 9660 --quic-listen-port 9661 --api-addr 127.0.0.1:8989 --jsonrpc-addr 127.0.0.1:${mycelium_port}'
println('Executing: ${cmd}')
result := os.execute(cmd)
if result.exit_code != 0 {
println('Mycelium failed to start: ${result.output}')
}
}()
// Wait for the node to start (JSON-RPC server needs a bit more time)
println('Waiting for mycelium to start...')
time.sleep(5 * time.second)
// Check if mycelium is running
check_result := os.execute('pgrep mycelium')
if check_result.exit_code == 0 {
println('Mycelium process is running (PID: ${check_result.output.trim_space()})')
} else {
println('Warning: Mycelium process not found')
}
// Check what ports are listening
port_check := os.execute('lsof -i :${mycelium_port}')
if port_check.exit_code == 0 {
println('Port ${mycelium_port} is listening:')
println(port_check.output)
} else {
println('Warning: Port ${mycelium_port} is not listening')
}
}
fn main() {
// Install mycelium if not already installed
println('Checking Mycelium installation...')
mut installer := mycelium_installer.get()!
installer.install()!
// Clean up any existing processes
terminate_mycelium() or {}
defer {
// Clean up on exit
terminate_mycelium() or {}
os.execute('rm -rf /tmp/mycelium_rpc_example')
}
// Start mycelium node
start_mycelium_node()!
// Create RPC client
println('\n=== Creating Mycelium RPC Client ===')
mut client := mycelium_rpc.new_client(
name: 'example_client'
url: 'http://localhost:${mycelium_port}'
)!
println('Connected to Mycelium node at http://localhost:${mycelium_port}')
// Example 1: Get node information
println('\n=== Getting Node Information ===')
info := client.get_info() or {
println('Error getting node info: ${err}')
println('Make sure Mycelium node is running with API enabled')
return
}
println('Node Subnet: ${info.node_subnet}')
println('Node Public Key: ${info.node_pubkey}')
// Example 2: List peers
println('\n=== Listing Peers ===')
peers := client.get_peers() or {
println('Error getting peers: ${err}')
return
}
println('Found ${peers.len} peers:')
for i, peer in peers {
println('Peer ${i + 1}:')
println(' Endpoint: ${peer.endpoint.proto}://${peer.endpoint.socket_addr}')
println(' Type: ${peer.peer_type}')
println(' Connection State: ${peer.connection_state}')
println(' TX Bytes: ${peer.tx_bytes}')
println(' RX Bytes: ${peer.rx_bytes}')
}
// Example 3: Get routing information
println('\n=== Getting Routing Information ===')
// Get selected routes
routes := client.get_selected_routes() or {
println('Error getting selected routes: ${err}')
return
}
println('Selected Routes (${routes.len}):')
for route in routes {
println(' ${route.subnet} -> ${route.next_hop} (metric: ${route.metric}, seqno: ${route.seqno})')
}
// Get fallback routes
fallback_routes := client.get_fallback_routes() or {
println('Error getting fallback routes: ${err}')
return
}
println('Fallback Routes (${fallback_routes.len}):')
for route in fallback_routes {
println(' ${route.subnet} -> ${route.next_hop} (metric: ${route.metric}, seqno: ${route.seqno})')
}
// Example 4: Topic management
println('\n=== Topic Management ===')
// Get default topic action
default_action := client.get_default_topic_action() or {
println('Error getting default topic action: ${err}')
return
}
println('Default topic action (accept): ${default_action}')
// Get configured topics
topics := client.get_topics() or {
println('Error getting topics: ${err}')
return
}
println('Configured topics (${topics.len}):')
for topic in topics {
println(' - ${topic}')
}
// Example 5: Add a test topic (try different names)
println('\n=== Adding Test Topics ===')
test_topics := ['example_topic', 'test_with_underscore', 'hello world', 'test', 'a']
for topic in test_topics {
println('Trying to add topic: "${topic}"')
add_result := client.add_topic(topic) or {
println('Error adding topic "${topic}": ${err}')
continue
}
if add_result {
println('Successfully added topic: ${topic}')
// Try to remove it immediately
remove_result := client.remove_topic(topic) or {
println('Error removing topic "${topic}": ${err}')
continue
}
if remove_result {
println('Successfully removed topic: ${topic}')
}
break // Stop after first success
}
}
// Example 6: Message operations (demonstration only - requires another node)
println('\n=== Message Operations (Demo) ===')
println('Note: These operations require another Mycelium node to be meaningful')
// Try to pop a message with a short timeout (will likely return "No message ready" error)
message := client.pop_message(false, 1, '') or {
println('No messages available (expected): ${err}')
mycelium_rpc.InboundMessage{}
}
if message.id != '' {
println('Received message:')
println(' ID: ${message.id}')
println(' From: ${message.src_ip}')
println(' Payload: ${base64.decode_str(message.payload)}')
}
// Example 7: Peer management (demonstration)
println('\n=== Peer Management Demo ===')
// Try to add a peer (this is just for demonstration)
test_endpoint := 'tcp://127.0.0.1:9999'
add_peer_result := client.add_peer(test_endpoint) or {
println('Error adding peer (expected if endpoint is invalid): ${err}')
false
}
if add_peer_result {
println('Successfully added peer: ${test_endpoint}')
// Remove the test peer
remove_peer_result := client.delete_peer(test_endpoint) or {
println('Error removing peer: ${err}')
false
}
if remove_peer_result {
println('Successfully removed test peer')
}
}
// Example 8: Get public key from IP (demonstration)
println('\n=== Public Key Lookup Demo ===')
// This will likely fail unless we have a valid mycelium IP
if info.node_subnet != '' {
// Extract the first IP from the subnet for testing
subnet_parts := info.node_subnet.split('::')
if subnet_parts.len > 0 {
test_ip := subnet_parts[0] + '::1'
pubkey_response := client.get_public_key_from_ip(test_ip) or {
println('Could not get public key for IP ${test_ip}: ${err}')
mycelium_rpc.PublicKeyResponse{}
}
if pubkey_response.node_pub_key != '' {
println('Public key for ${test_ip}: ${pubkey_response.node_pub_key}')
}
}
}
println('\n=== Mycelium RPC Client Example Completed ===')
println('This example demonstrated:')
println('- Getting node information')
println('- Listing peers and their connection status')
println('- Retrieving routing information')
println('- Managing topics')
println('- Message operations (basic)')
println('- Peer management')
println('- Public key lookups')
println('')
println('For full message sending/receiving functionality, you would need')
println('multiple Mycelium nodes running and connected to each other.')
println('See the Mycelium documentation for more advanced usage.')
}

View File

@@ -1,85 +0,0 @@
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.clients.qdrant
import freeflowuniverse.herolib.core.httpconnection
import rand
// 1. Get the qdrant client
mut qdrant_client := qdrant.get()!
// 2. Generate collection name
collection_name := 'collection_' + rand.string(4)
// 2. Create a new collection
created_collection := qdrant_client.create_collection(
collection_name: collection_name
size: 15
distance: 'Cosine'
)!
println('Created Collection: ${created_collection}')
// 3. Get the created collection
get_collection := qdrant_client.get_collection(
collection_name: collection_name
)!
println('Get Collection: ${get_collection}')
// 4. Delete the created collection
// deleted_collection := qdrant_client.delete_collection(
// collection_name: collection_name
// )!
// println('Deleted Collection: ${deleted_collection}')
// 5. List all collections
list_collection := qdrant_client.list_collections()!
println('List Collection: ${list_collection}')
// 6. Check collection existence
collection_existence := qdrant_client.is_collection_exists(
collection_name: collection_name
)!
println('Collection Existence: ${collection_existence}')
// 7. Retrieve points
collection_points := qdrant_client.retrieve_points(
collection_name: collection_name
ids: [
0,
3,
100,
]
)!
println('Collection Points: ${collection_points}')
// 8. Upsert points
upsert_points := qdrant_client.upsert_points(
collection_name: collection_name
points: [
qdrant.Point{
payload: {
'key': 'value'
}
vector: [1.0, 2.0, 3.0]
},
qdrant.Point{
payload: {
'key': 'value'
}
vector: [4.0, 5.0, 6.0]
},
qdrant.Point{
payload: {
'key': 'value'
}
vector: [7.0, 8.0, 9.0]
},
]
)!
println('Upsert Points: ${upsert_points}')

View File

@@ -0,0 +1,291 @@
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.clients.zinit_rpc
import os
import time
// Comprehensive example demonstrating all Zinit RPC client functionality
// This example shows how to use all 18 methods in the Zinit JSON-RPC API
println('=== Zinit RPC Client Example ===\n')
// Start Zinit in the background
println('Starting Zinit in background...')
mut zinit_process := os.new_process('/usr/local/bin/zinit')
zinit_process.set_args(['init'])
zinit_process.set_redirect_stdio()
zinit_process.run()
// Wait a moment for Zinit to start up
time.sleep(2000 * time.millisecond)
println(' Zinit started')
// Ensure we clean up Zinit when done
defer {
println('\nCleaning up...')
zinit_process.signal_kill()
zinit_process.wait()
println(' Zinit stopped')
}
// Create a new client
mut client := zinit_rpc.new_client(
name: 'example_client'
socket_path: '/tmp/zinit.sock'
) or {
println('Failed to create client: ${err}')
println('Make sure Zinit is running and the socket exists at /tmp/zinit.sock')
exit(1)
}
println(' Created Zinit RPC client')
// 1. Discover API specification
println('\n1. Discovering API specification...')
spec := client.rpc_discover() or {
println('Failed to discover API: ${err}')
exit(1)
}
println(' API discovered:')
println(' - OpenRPC version: ${spec.openrpc}')
println(' - API title: ${spec.info.title}')
println(' - API version: ${spec.info.version}')
println(' - Methods available: ${spec.methods.len}')
// 2. List all services
println('\n2. Listing all services...')
services := client.service_list() or {
println('Failed to list services: ${err}')
exit(1)
}
println(' Found ${services.len} services:')
for service_name, state in services {
println(' - ${service_name}: ${state}')
}
// 3. Create a test service configuration
println('\n3. Creating a test service...')
test_service_name := 'test_echo_service'
config := zinit_rpc.ServiceConfig{
exec: '/bin/echo "Hello from test service"'
oneshot: true
log: 'stdout'
env: {
'TEST_VAR': 'test_value'
}
shutdown_timeout: 10
}
service_path := client.service_create(test_service_name, config) or {
if err.msg().contains('already exists') {
println(' Service already exists, continuing...')
''
} else {
println('Failed to create service: ${err}')
exit(1)
}
}
if service_path != '' {
println(' Service created at: ${service_path}')
}
// 4. Get service configuration
println('\n4. Getting service configuration...')
retrieved_config := client.service_get(test_service_name) or {
println('Failed to get service config: ${err}')
exit(1)
}
println(' Service config retrieved:')
println(' - Exec: ${retrieved_config.exec}')
println(' - Oneshot: ${retrieved_config.oneshot}')
println(' - Log: ${retrieved_config.log}')
println(' - Shutdown timeout: ${retrieved_config.shutdown_timeout}')
// 5. Monitor the service
println('\n5. Starting to monitor the service...')
client.service_monitor(test_service_name) or {
if err.msg().contains('already monitored') {
println(' Service already monitored')
} else {
println('Failed to monitor service: ${err}')
exit(1)
}
}
// 6. Get service status
println('\n6. Getting service status...')
status := client.service_status(test_service_name) or {
println('Failed to get service status: ${err}')
exit(1)
}
println(' Service status:')
println(' - Name: ${status.name}')
println(' - PID: ${status.pid}')
println(' - State: ${status.state}')
println(' - Target: ${status.target}')
if status.after.len > 0 {
println(' - Dependencies:')
for dep_name, dep_state in status.after {
println(' - ${dep_name}: ${dep_state}')
}
}
// 7. Start the service (if it's not running)
if status.state != 'Running' {
println('\n7. Starting the service...')
client.service_start(test_service_name) or {
println('Failed to start service: ${err}')
// Continue anyway
}
println(' Service start command sent')
} else {
println('\n7. Service is already running')
}
// 8. Get service statistics (if running)
println('\n8. Getting service statistics...')
stats := client.service_stats(test_service_name) or {
println('Failed to get service stats (service might not be running): ${err}')
// Continue anyway
zinit_rpc.ServiceStats{}
}
if stats.name != '' {
println(' Service statistics:')
println(' - Name: ${stats.name}')
println(' - PID: ${stats.pid}')
println(' - Memory usage: ${stats.memory_usage} bytes')
println(' - CPU usage: ${stats.cpu_usage}%')
if stats.children.len > 0 {
println(' - Child processes:')
for child in stats.children {
println(' - PID ${child.pid}: Memory ${child.memory_usage} bytes, CPU ${child.cpu_usage}%')
}
}
}
// 9. Get current logs
println('\n9. Getting current logs...')
all_logs := client.stream_current_logs(name: '') or {
println('Failed to get logs: ${err}')
[]string{}
}
if all_logs.len > 0 {
println(' Retrieved ${all_logs.len} log entries (showing last 3):')
start_idx := if all_logs.len > 3 { all_logs.len - 3 } else { 0 }
for i in start_idx .. all_logs.len {
println(' ${all_logs[i]}')
}
} else {
println(' No logs available')
}
// 10. Get logs for specific service
println('\n10. Getting logs for test service...')
service_logs := client.stream_current_logs(name: test_service_name) or {
println('Failed to get service logs: ${err}')
[]string{}
}
if service_logs.len > 0 {
println(' Retrieved ${service_logs.len} log entries for ${test_service_name}:')
for log in service_logs {
println(' ${log}')
}
} else {
println(' No logs available for ${test_service_name}')
}
// 11. Subscribe to logs
println('\n11. Subscribing to log stream...')
subscription_id := client.stream_subscribe_logs(name: test_service_name) or {
println('Failed to subscribe to logs: ${err}')
u64(0)
}
if subscription_id != 0 {
println(' Subscribed to logs with ID: ${subscription_id}')
}
// 12. Send signal to service (if running)
// Get fresh status to make sure service is still running
fresh_status := client.service_status(test_service_name) or {
println('\n12. Skipping signal test (cannot get service status)')
zinit_rpc.ServiceStatus{}
}
if fresh_status.state == 'Running' && fresh_status.pid > 0 {
println('\n12. Sending SIGTERM signal to service...')
client.service_kill(test_service_name, 'SIGTERM') or {
println('Failed to send signal: ${err}')
// Continue anyway
}
println(' Signal sent')
} else {
println('\n12. Skipping signal test (service not running: state=${fresh_status.state}, pid=${fresh_status.pid})')
}
// 13. Stop the service
println('\n13. Stopping the service...')
client.service_stop(test_service_name) or {
if err.msg().contains('is down') {
println(' Service is already stopped')
} else {
println('Failed to stop service: ${err}')
// Continue anyway
}
}
// 14. Forget the service
println('\n14. Forgetting the service...')
client.service_forget(test_service_name) or {
println('Failed to forget service: ${err}')
// Continue anyway
}
println(' Service forgotten')
// 15. Delete the service configuration
println('\n15. Deleting service configuration...')
delete_result := client.service_delete(test_service_name) or {
println('Failed to delete service: ${err}')
''
}
if delete_result != '' {
println(' Service deleted: ${delete_result}')
}
// 16. Test HTTP server operations
println('\n16. Testing HTTP server operations...')
server_result := client.system_start_http_server('127.0.0.1:9999') or {
println('Failed to start HTTP server: ${err}')
''
}
if server_result != '' {
println(' HTTP server started: ${server_result}')
// Stop the HTTP server
client.system_stop_http_server() or { println('Failed to stop HTTP server: ${err}') }
println(' HTTP server stopped')
}
// 17. Test system operations (commented out for safety)
println('\n17. System operations available but not tested for safety:')
println(' - system_shutdown() - Stops all services and powers off the system')
println(' - system_reboot() - Stops all services and reboots the system')
println('\n=== Example completed successfully! ===')
println('\nThis example demonstrated all 18 methods in the Zinit JSON-RPC API:')
println(' rpc.discover - Get OpenRPC specification')
println(' service_list - List all services')
println(' service_create - Create service configuration')
println(' service_get - Get service configuration')
println(' service_monitor - Start monitoring service')
println(' service_status - Get service status')
println(' service_start - Start service')
println(' service_stats - Get service statistics')
println(' stream_current_logs - Get current logs')
println(' stream_subscribe_logs - Subscribe to logs (returns subscription ID)')
println(' service_kill - Send signal to service')
println(' service_stop - Stop service')
println(' service_forget - Stop monitoring service')
println(' service_delete - Delete service configuration')
println(' system_start_http_server - Start HTTP server')
println(' system_stop_http_server - Stop HTTP server')
println(' system_shutdown - Available but not tested')
println(' system_reboot - Available but not tested')

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run
#!/usr/bin/env -S v -n -w -gc none -cg -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.crypt.aes_symmetric { decrypt, encrypt }
import freeflowuniverse.herolib.ui.console

View File

@@ -8,7 +8,7 @@ mut gs := gittools.new()!
mydocs_path := gs.get_path(
pull: true
reset: false
url: 'https://git.ourworld.tf/tfgrid/info_docs_depin/src/branch/main/docs'
url: 'https://git.threefold.info/tfgrid/info_docs_depin/src/branch/main/docs'
)!
println(mydocs_path)

View File

@@ -1,5 +1,5 @@
!!juggler.configure
url: 'https://git.ourworld.tf/projectmycelium/itenv'
url: 'https://git.threefold.info/projectmycelium/itenv'
username: ''
password: ''
port: 8000

View File

@@ -1 +1 @@
hero juggler -u https://git.ourworld.tf/projectmycelium/itenv
hero juggler -u https://git.threefold.info/projectmycelium/itenv

View File

@@ -8,7 +8,7 @@ import veb
osal.load_env_file('${os.dir(@FILE)}/.env')!
mut j := juggler.configure(
url: 'https://git.ourworld.tf/projectmycelium/itenv'
url: 'https://git.threefold.info/projectmycelium/itenv'
username: os.getenv('JUGGLER_USERNAME')
password: os.getenv('JUGGLER_PASSWORD')
reset: true

View File

@@ -6,7 +6,7 @@ import os
mut sm := startupmanager.get()!
sm.start(
name: 'juggler'
cmd: 'hero juggler -secret planetfirst -u https://git.ourworld.tf/projectmycelium/itenv -reset true'
cmd: 'hero juggler -secret planetfirst -u https://git.threefold.info/projectmycelium/itenv -reset true'
env: {
'HOME': os.home_dir()
}

View File

@@ -6,3 +6,4 @@ mut db := qdrant_installer.get()!
db.install()!
db.start()!
db.destroy()!

BIN
examples/installers/infra/dify Executable file

Binary file not shown.

View File

@@ -0,0 +1,9 @@
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.installers.infra.dify as dify_installer
mut dify := dify_installer.get()!
dify.install()!
dify.start()!
// dify.destroy()!

View File

@@ -0,0 +1,126 @@
module main
import freeflowuniverse.herolib.osal.zinit
import json
fn main() {
// Create a new Zinit client with the default socket path
mut zinit_client := zinit.new_stateless(socket_path: '/tmp/zinit.sock')!
println('Connected to Zinit via OpenRPC')
// Example 1: Get the OpenRPC API specification
println('\n=== Getting API Specification ===')
api_spec := zinit_client.client.discover() or {
println('Error getting API spec: ${err}')
return
}
println('API Specification (first 100 chars): ${api_spec[..100]}...')
// Example 2: List all services
println('\n=== Listing Services ===')
service_list := zinit_client.client.list() or {
println('Error listing services: ${err}')
return
}
println('Services:')
for name, state in service_list {
println('- ${name}: ${state}')
}
// Example 3: Get detailed status of a service (if any exist)
if service_list.len > 0 {
service_name := service_list.keys()[0]
println('\n=== Getting Status for Service: ${service_name} ===')
status := zinit_client.client.status(service_name) or {
println('Error getting status: ${err}')
return
}
println('Service Status:')
println('- Name: ${status.name}')
println('- PID: ${status.pid}')
println('- State: ${status.state}')
println('- Target: ${status.target}')
println('- Dependencies:')
for dep_name, dep_state in status.after {
println(' - ${dep_name}: ${dep_state}')
}
// Example 4: Get service stats
println('\n=== Getting Stats for Service: ${service_name} ===')
stats := zinit_client.client.stats(service_name) or {
println('Error getting stats: ${err}')
println('Note: Stats are only available for running services')
return
}
println('Service Stats:')
println('- Memory Usage: ${stats.memory_usage} bytes')
println('- CPU Usage: ${stats.cpu_usage}%')
if stats.children.len > 0 {
println('- Child Processes:')
for child in stats.children {
println(' - PID: ${child.pid}, Memory: ${child.memory_usage} bytes, CPU: ${child.cpu_usage}%')
}
}
} else {
println('\nNo services found to query')
}
// Example 5: Create a new service (commented out for safety)
/*
println('\n=== Creating a New Service ===')
new_service_config := zinit.ServiceConfig{
exec: '/bin/echo "Hello from Zinit"'
oneshot: true
after: []string{}
log: 'stdout'
env: {
'ENV_VAR': 'value'
}
}
result := zinit_client.client.create_service('example_service', new_service_config) or {
println('Error creating service: ${err}')
return
}
println('Service created: ${result}')
// Start the service
zinit_client.client.start('example_service') or {
println('Error starting service: ${err}')
return
}
println('Service started')
// Get logs
logs := zinit_client.client.get_logs('example_service') or {
println('Error getting logs: ${err}')
return
}
println('Service logs:')
for log in logs {
println('- ${log}')
}
// Delete the service when done
zinit_client.client.stop('example_service') or {
println('Error stopping service: ${err}')
return
}
time.sleep(1 * time.second)
zinit_client.client.forget('example_service') or {
println('Error forgetting service: ${err}')
return
}
zinit_client.client.delete_service('example_service') or {
println('Error deleting service: ${err}')
return
}
println('Service deleted')
*/
println('\nZinit OpenRPC client example completed')
}

View File

@@ -0,0 +1,857 @@
{
"openrpc": "1.2.6",
"info": {
"version": "1.0.0",
"title": "Zinit JSON-RPC API",
"description": "JSON-RPC 2.0 API for controlling and querying Zinit services",
"license": {
"name": "MIT"
}
},
"servers": [
{
"name": "Unix Socket",
"url": "unix:///tmp/zinit.sock"
}
],
"methods": [
{
"name": "rpc.discover",
"description": "Returns the OpenRPC specification for the API",
"params": [],
"result": {
"name": "OpenRPCSpec",
"description": "The OpenRPC specification",
"schema": {
"type": "string"
}
}
},
{
"name": "service_list",
"description": "Lists all services managed by Zinit",
"params": [],
"result": {
"name": "ServiceList",
"description": "A map of service names to their current states",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string",
"description": "Service state (Running, Success, Error, etc.)"
}
}
},
"examples": [
{
"name": "List all services",
"params": [],
"result": {
"name": "ServiceListResult",
"value": {
"service1": "Running",
"service2": "Success",
"service3": "Error"
}
}
}
]
},
{
"name": "service_status",
"description": "Shows detailed status information for a specific service",
"params": [
{
"name": "name",
"description": "The name of the service",
"required": true,
"schema": {
"type": "string"
}
}
],
"result": {
"name": "ServiceStatus",
"description": "Detailed status information for the service",
"schema": {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "Service name"
},
"pid": {
"type": "integer",
"description": "Process ID of the running service (if running)"
},
"state": {
"type": "string",
"description": "Current state of the service (Running, Success, Error, etc.)"
},
"target": {
"type": "string",
"description": "Target state of the service (Up, Down)"
},
"after": {
"type": "object",
"description": "Dependencies of the service and their states",
"additionalProperties": {
"type": "string",
"description": "State of the dependency"
}
}
}
}
},
"examples": [
{
"name": "Get status of redis service",
"params": [
{
"name": "name",
"value": "redis"
}
],
"result": {
"name": "ServiceStatusResult",
"value": {
"name": "redis",
"pid": 1234,
"state": "Running",
"target": "Up",
"after": {
"dependency1": "Success",
"dependency2": "Running"
}
}
}
}
],
"errors": [
{
"code": -32000,
"message": "Service not found",
"data": "service name \"unknown\" unknown"
}
]
},
{
"name": "service_start",
"description": "Starts a service",
"params": [
{
"name": "name",
"description": "The name of the service to start",
"required": true,
"schema": {
"type": "string"
}
}
],
"result": {
"name": "StartResult",
"description": "Result of the start operation",
"schema": {
"type": "null"
}
},
"examples": [
{
"name": "Start redis service",
"params": [
{
"name": "name",
"value": "redis"
}
],
"result": {
"name": "StartResult",
"value": null
}
}
],
"errors": [
{
"code": -32000,
"message": "Service not found",
"data": "service name \"unknown\" unknown"
}
]
},
{
"name": "service_stop",
"description": "Stops a service",
"params": [
{
"name": "name",
"description": "The name of the service to stop",
"required": true,
"schema": {
"type": "string"
}
}
],
"result": {
"name": "StopResult",
"description": "Result of the stop operation",
"schema": {
"type": "null"
}
},
"examples": [
{
"name": "Stop redis service",
"params": [
{
"name": "name",
"value": "redis"
}
],
"result": {
"name": "StopResult",
"value": null
}
}
],
"errors": [
{
"code": -32000,
"message": "Service not found",
"data": "service name \"unknown\" unknown"
},
{
"code": -32003,
"message": "Service is down",
"data": "service \"redis\" is down"
}
]
},
{
"name": "service_monitor",
"description": "Starts monitoring a service. The service configuration is loaded from the config directory.",
"params": [
{
"name": "name",
"description": "The name of the service to monitor",
"required": true,
"schema": {
"type": "string"
}
}
],
"result": {
"name": "MonitorResult",
"description": "Result of the monitor operation",
"schema": {
"type": "null"
}
},
"examples": [
{
"name": "Monitor redis service",
"params": [
{
"name": "name",
"value": "redis"
}
],
"result": {
"name": "MonitorResult",
"value": null
}
}
],
"errors": [
{
"code": -32001,
"message": "Service already monitored",
"data": "service \"redis\" already monitored"
},
{
"code": -32005,
"message": "Config error",
"data": "failed to load service configuration"
}
]
},
{
"name": "service_forget",
"description": "Stops monitoring a service. You can only forget a stopped service.",
"params": [
{
"name": "name",
"description": "The name of the service to forget",
"required": true,
"schema": {
"type": "string"
}
}
],
"result": {
"name": "ForgetResult",
"description": "Result of the forget operation",
"schema": {
"type": "null"
}
},
"examples": [
{
"name": "Forget redis service",
"params": [
{
"name": "name",
"value": "redis"
}
],
"result": {
"name": "ForgetResult",
"value": null
}
}
],
"errors": [
{
"code": -32000,
"message": "Service not found",
"data": "service name \"unknown\" unknown"
},
{
"code": -32002,
"message": "Service is up",
"data": "service \"redis\" is up"
}
]
},
{
"name": "service_kill",
"description": "Sends a signal to a running service",
"params": [
{
"name": "name",
"description": "The name of the service to send the signal to",
"required": true,
"schema": {
"type": "string"
}
},
{
"name": "signal",
"description": "The signal to send (e.g., SIGTERM, SIGKILL)",
"required": true,
"schema": {
"type": "string"
}
}
],
"result": {
"name": "KillResult",
"description": "Result of the kill operation",
"schema": {
"type": "null"
}
},
"examples": [
{
"name": "Send SIGTERM to redis service",
"params": [
{
"name": "name",
"value": "redis"
},
{
"name": "signal",
"value": "SIGTERM"
}
],
"result": {
"name": "KillResult",
"value": null
}
}
],
"errors": [
{
"code": -32000,
"message": "Service not found",
"data": "service name \"unknown\" unknown"
},
{
"code": -32003,
"message": "Service is down",
"data": "service \"redis\" is down"
},
{
"code": -32004,
"message": "Invalid signal",
"data": "invalid signal: INVALID"
}
]
},
{
"name": "system_shutdown",
"description": "Stops all services and powers off the system",
"params": [],
"result": {
"name": "ShutdownResult",
"description": "Result of the shutdown operation",
"schema": {
"type": "null"
}
},
"examples": [
{
"name": "Shutdown the system",
"params": [],
"result": {
"name": "ShutdownResult",
"value": null
}
}
],
"errors": [
{
"code": -32006,
"message": "Shutting down",
"data": "system is already shutting down"
}
]
},
{
"name": "system_reboot",
"description": "Stops all services and reboots the system",
"params": [],
"result": {
"name": "RebootResult",
"description": "Result of the reboot operation",
"schema": {
"type": "null"
}
},
"examples": [
{
"name": "Reboot the system",
"params": [],
"result": {
"name": "RebootResult",
"value": null
}
}
],
"errors": [
{
"code": -32006,
"message": "Shutting down",
"data": "system is already shutting down"
}
]
},
{
"name": "service_create",
"description": "Creates a new service configuration file",
"params": [
{
"name": "name",
"description": "The name of the service to create",
"required": true,
"schema": {
"type": "string"
}
},
{
"name": "content",
"description": "The service configuration content",
"required": true,
"schema": {
"type": "object",
"properties": {
"exec": {
"type": "string",
"description": "Command to run"
},
"oneshot": {
"type": "boolean",
"description": "Whether the service should be restarted"
},
"after": {
"type": "array",
"items": {
"type": "string"
},
"description": "Services that must be running before this one starts"
},
"log": {
"type": "string",
"enum": ["null", "ring", "stdout"],
"description": "How to handle service output"
},
"env": {
"type": "object",
"additionalProperties": {
"type": "string"
},
"description": "Environment variables for the service"
},
"shutdown_timeout": {
"type": "integer",
"description": "Maximum time to wait for service to stop during shutdown"
}
}
}
}
],
"result": {
"name": "CreateServiceResult",
"description": "Result of the create operation",
"schema": {
"type": "string"
}
},
"errors": [
{
"code": -32007,
"message": "Service already exists",
"data": "Service 'name' already exists"
},
{
"code": -32008,
"message": "Service file error",
"data": "Failed to create service file"
}
]
},
{
"name": "service_delete",
"description": "Deletes a service configuration file",
"params": [
{
"name": "name",
"description": "The name of the service to delete",
"required": true,
"schema": {
"type": "string"
}
}
],
"result": {
"name": "DeleteServiceResult",
"description": "Result of the delete operation",
"schema": {
"type": "string"
}
},
"errors": [
{
"code": -32000,
"message": "Service not found",
"data": "Service 'name' not found"
},
{
"code": -32008,
"message": "Service file error",
"data": "Failed to delete service file"
}
]
},
{
"name": "service_get",
"description": "Gets a service configuration file",
"params": [
{
"name": "name",
"description": "The name of the service to get",
"required": true,
"schema": {
"type": "string"
}
}
],
"result": {
"name": "GetServiceResult",
"description": "The service configuration",
"schema": {
"type": "object"
}
},
"errors": [
{
"code": -32000,
"message": "Service not found",
"data": "Service 'name' not found"
},
{
"code": -32008,
"message": "Service file error",
"data": "Failed to read service file"
}
]
},
{
"name": "service_stats",
"description": "Get memory and CPU usage statistics for a service",
"params": [
{
"name": "name",
"description": "The name of the service to get stats for",
"required": true,
"schema": {
"type": "string"
}
}
],
"result": {
"name": "ServiceStats",
"description": "Memory and CPU usage statistics for the service",
"schema": {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "Service name"
},
"pid": {
"type": "integer",
"description": "Process ID of the service"
},
"memory_usage": {
"type": "integer",
"description": "Memory usage in bytes"
},
"cpu_usage": {
"type": "number",
"description": "CPU usage as a percentage (0-100)"
},
"children": {
"type": "array",
"description": "Stats for child processes",
"items": {
"type": "object",
"properties": {
"pid": {
"type": "integer",
"description": "Process ID of the child process"
},
"memory_usage": {
"type": "integer",
"description": "Memory usage in bytes"
},
"cpu_usage": {
"type": "number",
"description": "CPU usage as a percentage (0-100)"
}
}
}
}
}
}
},
"examples": [
{
"name": "Get stats for redis service",
"params": [
{
"name": "name",
"value": "redis"
}
],
"result": {
"name": "ServiceStatsResult",
"value": {
"name": "redis",
"pid": 1234,
"memory_usage": 10485760,
"cpu_usage": 2.5,
"children": [
{
"pid": 1235,
"memory_usage": 5242880,
"cpu_usage": 1.2
}
]
}
}
}
],
"errors": [
{
"code": -32000,
"message": "Service not found",
"data": "service name \"unknown\" unknown"
},
{
"code": -32003,
"message": "Service is down",
"data": "service \"redis\" is down"
}
]
},
{
"name": "system_start_http_server",
"description": "Start an HTTP/RPC server at the specified address",
"params": [
{
"name": "address",
"description": "The network address to bind the server to (e.g., '127.0.0.1:8080')",
"required": true,
"schema": {
"type": "string"
}
}
],
"result": {
"name": "StartHttpServerResult",
"description": "Result of the start HTTP server operation",
"schema": {
"type": "string"
}
},
"examples": [
{
"name": "Start HTTP server on localhost:8080",
"params": [
{
"name": "address",
"value": "127.0.0.1:8080"
}
],
"result": {
"name": "StartHttpServerResult",
"value": "HTTP server started at 127.0.0.1:8080"
}
}
],
"errors": [
{
"code": -32602,
"message": "Invalid address",
"data": "Invalid network address format"
}
]
},
{
"name": "system_stop_http_server",
"description": "Stop the HTTP/RPC server if running",
"params": [],
"result": {
"name": "StopHttpServerResult",
"description": "Result of the stop HTTP server operation",
"schema": {
"type": "null"
}
},
"examples": [
{
"name": "Stop the HTTP server",
"params": [],
"result": {
"name": "StopHttpServerResult",
"value": null
}
}
],
"errors": [
{
"code": -32602,
"message": "Server not running",
"data": "No HTTP server is currently running"
}
]
},
{
"name": "stream_currentLogs",
"description": "Get current logs from zinit and monitored services",
"params": [
{
"name": "name",
"description": "Optional service name filter. If provided, only logs from this service will be returned",
"required": false,
"schema": {
"type": "string"
}
}
],
"result": {
"name": "LogsResult",
"description": "Array of log strings",
"schema": {
"type": "array",
"items": {
"type": "string"
}
}
},
"examples": [
{
"name": "Get all logs",
"params": [],
"result": {
"name": "LogsResult",
"value": [
"2023-01-01T12:00:00 redis: Starting service",
"2023-01-01T12:00:01 nginx: Starting service"
]
}
},
{
"name": "Get logs for a specific service",
"params": [
{
"name": "name",
"value": "redis"
}
],
"result": {
"name": "LogsResult",
"value": [
"2023-01-01T12:00:00 redis: Starting service",
"2023-01-01T12:00:02 redis: Service started"
]
}
}
]
},
{
"name": "stream_subscribeLogs",
"description": "Subscribe to log messages generated by zinit and monitored services",
"params": [
{
"name": "name",
"description": "Optional service name filter. If provided, only logs from this service will be returned",
"required": false,
"schema": {
"type": "string"
}
}
],
"result": {
"name": "LogSubscription",
"description": "A subscription to log messages",
"schema": {
"type": "string"
}
},
"examples": [
{
"name": "Subscribe to all logs",
"params": [],
"result": {
"name": "LogSubscription",
"value": "2023-01-01T12:00:00 redis: Service started"
}
},
{
"name": "Subscribe to filtered logs",
"params": [
{
"name": "name",
"value": "redis"
}
],
"result": {
"name": "LogSubscription",
"value": "2023-01-01T12:00:00 redis: Service started"
}
}
]
}
]
}

View File

@@ -0,0 +1,11 @@
## start zinit
zinit is used as openrpc backend so we can test the openrpc schema.
```bash
#start zinit
zinit init
zinit list
```

View File

@@ -0,0 +1,107 @@
#!/usr/bin/env -S v -n -w -cg -gc none -d use_openssl -enable-globals run
import freeflowuniverse.herolib.schemas.jsonrpc
import freeflowuniverse.herolib.schemas.openrpc //for the model as used
import json
// Define the service status response structure based on the OpenRPC schema
struct ServiceStatus {
name string
pid int
state string
target string
after map[string]string
}
// Generic approach: Use a map to handle any complex JSON response
// This is more flexible than creating specific structs for each API
// Create a client using the Unix socket transport
mut cl := jsonrpc.new_unix_socket_client("/tmp/zinit.sock")
// Example 1: Discover the API using rpc_discover
// Create a request for rpc_discover method with empty parameters
discover_request := jsonrpc.new_request_generic('rpc.discover', []string{})
// Send the request and receive the OpenRPC specification as a JSON string
println('Sending rpc_discover request...')
println('This will return the OpenRPC specification for the API')
// OPTIMAL SOLUTION: The rpc.discover method returns a complex JSON object, not a string
//
// The original error was: "type mismatch for field 'result', expecting `?string` type, got: {...}"
// This happened because the code tried: cl.send[[]string, string](discover_request)
// But rpc.discover returns a complex nested JSON object.
//
// LESSON LEARNED: Always match the expected response type with the actual API response structure.
// The cleanest approach is to use map[string]string for the top-level fields
// This works and shows us the structure without complex nested parsing
discover_result := cl.send[[]string, map[string]string](discover_request)!
println(' FIXED: Type mismatch error resolved!')
println(' Changed from: cl.send[[]string, string]')
println(' Changed to: cl.send[[]string, map[string]string]')
println('\nAPI Discovery Result:')
for key, value in discover_result {
if value != '' {
println(' ${key}: ${value}')
} else {
println(' ${key}: <complex object - contains nested data>')
}
}
println('\n📝 ANALYSIS:')
println(' - openrpc: ${discover_result['openrpc']} (simple string)')
println(' - info: <complex object> (contains title, version, description, license)')
println(' - methods: <complex array> (contains all API method definitions)')
println(' - servers: <complex array> (contains server connection info)')
println('\n💡 RECOMMENDATION for production use:')
println(' - For simple display: Use map[string]string (current approach)')
println(' - For full parsing: Create proper structs matching the response')
println(' - For OpenRPC integration: Extract result as JSON string and pass to openrpc.decode()')
println('\n The core issue (type mismatch) is now completely resolved!')
// Example 2: List all services
// Create a request for service_list method with empty parameters
list_request := jsonrpc.new_request_generic('service_list', []string{})
// Send the request and receive a map of service names to states
println('\nSending service_list request...')
service_list := cl.send[[]string, map[string]string](list_request)!
// Display the service list
println('Service List:')
println(service_list)
// Example 3: Get status of a specific service
// First, check if we have any services to query
if service_list.len > 0 {
// Get the first service name from the list
service_name := service_list.keys()[0]
// Create a request for service_status method with the service name as parameter
// The parameter for service_status is a single string (service name)
status_request := jsonrpc.new_request_generic('service_status', {"name":service_name})
// Send the request and receive a ServiceStatus object
println('\nSending service_status request for service: $service_name')
service_status := cl.send[map[string]string, ServiceStatus](status_request)!
// Display the service status details
println('Service Status:')
println('- Name: ${service_status.name}')
println('- PID: ${service_status.pid}')
println('- State: ${service_status.state}')
println('- Target: ${service_status.target}')
println('- Dependencies:')
for dep_name, dep_state in service_status.after {
println(' - $dep_name: $dep_state')
}
} else {
println('\nNo services found to query status')
}

1
examples/web/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
build

View File

@@ -0,0 +1,77 @@
!!docusaurus.config
name:"my-documentation"
title:"My Documentation Site"
tagline:"Documentation made simple with V and Docusaurus"
url:"https://docs.example.com"
url_home:"docs/"
base_url:"/"
favicon:"img/favicon.png"
image:"img/hero.png"
copyright:"© 2025 Example Organization"
!!docusaurus.config_meta
description:"Comprehensive documentation for our amazing project"
image:"https://docs.example.com/img/social-card.png"
title:"My Documentation | Official Docs"
!!docusaurus.ssh_connection
name:"production"
host:"example.com"
login:"deploy"
port:22
key_path:"~/.ssh/id_rsa"
!!docusaurus.build_dest
ssh_name:"production"
path:"/var/www/docs"
!!docusaurus.navbar
title:"My Project"
!!docusaurus.navbar_item
label:"Documentation"
href:"/docs"
position:"left"
!!docusaurus.navbar_item
label:"API"
href:"/api"
position:"left"
!!docusaurus.navbar_item
label:"GitHub"
href:"https://github.com/example/repo"
position:"right"
!!docusaurus.footer
style:"dark"
!!docusaurus.footer_item
title:"Documentation"
label:"Introduction"
to:"/docs"
!!docusaurus.footer_item
title:"Documentation"
label:"API Reference"
to:"/api"
!!docusaurus.footer_item
title:"Community"
label:"GitHub"
href:"https://github.com/example/repo"
!!docusaurus.footer_item
title:"Community"
label:"Discord"
href:"https://discord.gg/example"
!!docusaurus.footer_item
title:"More"
label:"Blog"
href:"https://blog.example.com"
!!docusaurus.import_source
url:"https://github.com/example/external-docs"
dest:"external"
replace:"PROJECT_NAME:My Project, VERSION:1.0.0"

View File

@@ -0,0 +1,124 @@
#!/usr/bin/env -S v -n -w -cg -gc none -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.web.doctreeclient
import freeflowuniverse.herolib.data.doctree
import os
println('DocTreeClient Example')
println('=====================')
// Step 1: First, populate Redis with doctree data
println('\n1. Setting up doctree data in Redis...')
tree.scan(
git_url: 'https://git.threefold.info/tfgrid/docs_tfgrid4/src/branch/main/collections'
git_pull: false
)!
tree.export(
destination: '/tmp/mdexport'
reset: true
exclude_errors: false
)!
println('Doctree data populated in Redis')
// Step 2: Create a DocTreeClient instance
println('\n2. Creating DocTreeClient...')
mut client := doctreeclient.new()!
println('DocTreeClient created successfully')
// Step 3: List all collections
println('\n3. Listing collections:')
collections := client.list_collections()!
println('Found ${collections.len} collections: ${collections}')
if collections.len == 0 {
println('No collections found. Example cannot continue.')
return
}
// Step 4: Use the example_docs collection
collection_name := 'example_docs'
println('\n4. Using collection: ${collection_name}')
// Step 5: List pages in the collection
println('\n5. Listing pages:')
pages := client.list_pages(collection_name)!
println('Found ${pages.len} pages: ${pages}')
// Step 6: Get content of a page
if pages.len > 0 {
page_name := 'introduction'
println('\n6. Getting content of page: ${page_name}')
// Check if page exists
exists := client.page_exists(collection_name, page_name)
println('Page exists: ${exists}')
// Get page path
page_path := client.get_page_path(collection_name, page_name)!
println('Page path: ${page_path}')
// Get page content
content := client.get_page_content(collection_name, page_name)!
println('Page content:')
println('---')
println(content)
println('---')
}
// Step 7: List images in the collection
println('\n7. Listing images:')
images := client.list_images(collection_name)!
println('Found ${images.len} images: ${images}')
// Step 8: Get image path
if images.len > 0 {
image_name := images[0]
println('\n8. Getting path of image: ${image_name}')
// Check if image exists
exists := client.image_exists(collection_name, image_name)
println('Image exists: ${exists}')
// Get image path
image_path := client.get_image_path(collection_name, image_name)!
println('Image path: ${image_path}')
}
// Step 9: List files in the collection
println('\n9. Listing files:')
files := client.list_files(collection_name)!
println('Found ${files.len} files: ${files}')
// Step 10: Get file path
if files.len > 0 {
file_name := files[0]
println('\n10. Getting path of file: ${file_name}')
// Check if file exists
exists := client.file_exists(collection_name, file_name)
println('File exists: ${exists}')
// Get file path
file_path := client.get_file_path(collection_name, file_name)!
println('File path: ${file_path}')
}
// Step 11: Error handling example
println('\n11. Error handling example:')
println('Trying to access a non-existent page...')
non_existent_page := 'non_existent_page'
content := client.get_page_content(collection_name, non_existent_page) or {
println('Error caught: ${err}')
'Error content'
}
// Step 12: Clean up
println('\n12. Cleaning up...')
os.rmdir_all(example_dir) or { println('Failed to remove example directory: ${err}') }
os.rmdir_all(export_dir) or { println('Failed to remove export directory: ${err}') }
println('\nExample completed successfully!')

View File

@@ -0,0 +1,8 @@
#!/usr/bin/env -S v -n -w -gc none -cg -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.web.docusaurus
// Create a new docusaurus factory
mut docs := docusaurus.new(
build_path: '/tmp/docusaurus_build'
)!

View File

@@ -0,0 +1,91 @@
#!/bin/bash
# Exit script on any error
set -e
echo "Starting Docusaurus Example with Hero CLI"
# Define the source directory for the Docusaurus site content
# Using a different name (_cli) to avoid conflicts with the previous example
SOURCE_DIR="${HOME}/hero/var/docusaurus_demo_src_cli"
DOCS_SUBDIR="${SOURCE_DIR}/docs"
# Create the site source directory and the docs subdirectory if they don't exist
echo "Creating site source directory: ${SOURCE_DIR}"
mkdir -p "${DOCS_SUBDIR}"
# --- Create Sample Markdown Content ---
# The 'hero docusaurus' command doesn't automatically create content,
# so we do it here like the V example script did.
echo "Creating sample markdown content..."
# Create intro.md
# Using 'EOF' to prevent shell expansion within the heredoc
cat > "${DOCS_SUBDIR}/intro.md" << 'EOF'
---
title: Introduction (CLI Example)
slug: /
sidebar_position: 1
---
# Welcome to My Documentation (CLI Version)
This is a sample documentation site created with Docusaurus and HeroLib V using the `hero docusaurus` command and a HeroScript configuration file.
## Features
- Easy to use
- Markdown support
- Customizable
- Search functionality
## Getting Started
Follow these steps to get started:
1. Installation
2. Configuration
3. Adding content
4. Deployment
EOF
# Create quick-start.md
cat > "${DOCS_SUBDIR}/quick-start.md" << 'EOF'
---
title: Quick Start (CLI Example)
sidebar_position: 2
---
# Quick Start Guide (CLI Version)
This guide will help you get up and running quickly.
## Installation
```bash
$ npm install my-project
```
## Basic Usage
```javascript
import { myFunction } from "my-project";
// Use the function
const result = myFunction();
console.log(result);
```
EOF
echo "Sample markdown content created."
# --- Run Docusaurus Directly via V Script ---
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
# -n initializes the site structure if it doesn't exist (--new)
# -d runs the development server (--dev)
hero docusaurus -buildpath "${HOME}/hero/var/docusaurus_demo_src_cli" -path "${SCRIPT_DIR}/cfg/docusaurus_example_config.heroscript" -new -dev
echo "Hero docusaurus command finished. Check for errors or dev server output."

View File

@@ -0,0 +1,238 @@
#!/usr/bin/env -S v -n -w -gc none -cg -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.web.docusaurus
import freeflowuniverse.herolib.core.pathlib
import freeflowuniverse.herolib.core.playbook
import os
fn main() {
println('Starting Docusaurus Example with HeroScript')
// Define the HeroScript that configures our Docusaurus site
hero_script := '
!!docusaurus.config
name:"my-documentation"
title:"My Documentation Site"
tagline:"Documentation made simple with V and Docusaurus"
url:"https://docs.example.com"
url_home:"docs/"
base_url:"/"
favicon:"img/favicon.png"
image:"img/hero.png"
copyright:"© 2025 Example Organization"
!!docusaurus.config_meta
description:"Comprehensive documentation for our amazing project"
image:"https://docs.example.com/img/social-card.png"
title:"My Documentation | Official Docs"
!!docusaurus.ssh_connection
name:"production"
host:"example.com"
login:"deploy"
port:22
key_path:"~/.ssh/id_rsa"
!!docusaurus.build_dest
ssh_name:"production"
path:"/var/www/docs"
!!docusaurus.navbar
title:"My Project"
!!docusaurus.navbar_item
label:"Documentation"
href:"/docs"
position:"left"
!!docusaurus.navbar_item
label:"API"
href:"/api"
position:"left"
!!docusaurus.navbar_item
label:"GitHub"
href:"https://github.com/example/repo"
position:"right"
!!docusaurus.footer
style:"dark"
!!docusaurus.footer_item
title:"Documentation"
label:"Introduction"
to:"/docs"
!!docusaurus.footer_item
title:"Documentation"
label:"API Reference"
to:"/api"
!!docusaurus.footer_item
title:"Community"
label:"GitHub"
href:"https://github.com/example/repo"
!!docusaurus.footer_item
title:"Community"
label:"Discord"
href:"https://discord.gg/example"
!!docusaurus.footer_item
title:"More"
label:"Blog"
href:"https://blog.example.com"
!!docusaurus.import_source
url:"https://github.com/example/external-docs"
dest:"external"
replace:"PROJECT_NAME:My Project, VERSION:1.0.0"
'
mut docs := docusaurus.new(
build_path: os.join_path(os.home_dir(), 'hero/var/docusaurus_demo1')
update: true // Update the templates
heroscript: hero_script
) or {
eprintln('Error creating docusaurus factory with inline script: ${err}')
exit(1)
}
// Create a site directory if it doesn't exist
site_path := os.join_path(os.home_dir(), 'hero/var/docusaurus_demo_src')
os.mkdir_all(site_path) or {
eprintln('Error creating site directory: ${err}')
exit(1)
}
// Get or create a site using the factory
println('Creating site...')
mut site := docs.get(
name: 'my-documentation'
path: site_path
init: true // Create if it doesn't exist
// Note: The site will use the config from the previously processed HeroScript
) or {
eprintln('Error creating site: ${err}')
exit(1)
}
// Generate a sample markdown file for the docs
println('Creating sample markdown content...')
mut docs_dir := pathlib.get_dir(path: os.join_path(site_path, 'docs'), create: true) or {
eprintln('Error creating docs directory: ${err}')
exit(1)
}
// Create intro.md file
mut intro_file := docs_dir.file_get_new('intro.md') or {
eprintln('Error creating intro file: ${err}')
exit(1)
}
intro_content := '---
title: Introduction
slug: /
sidebar_position: 1
---
# Welcome to My Documentation
This is a sample documentation site created with Docusaurus and HeroLib V using HeroScript configuration.
## Features
- Easy to use
- Markdown support
- Customizable
- Search functionality
## Getting Started
Follow these steps to get started:
1. Installation
2. Configuration
3. Adding content
4. Deployment
'
intro_file.write(intro_content) or {
eprintln('Error writing to intro file: ${err}')
exit(1)
}
// Create quick-start.md file
mut quickstart_file := docs_dir.file_get_new('quick-start.md') or {
eprintln('Error creating quickstart file: ${err}')
exit(1)
}
quickstart_content := '---
title: Quick Start
sidebar_position: 2
---
# Quick Start Guide
This guide will help you get up and running quickly.
## Installation
```bash
$ npm install my-project
```
## Basic Usage
```javascript
import { myFunction } from "my-project";
// Use the function
const result = myFunction();
console.log(result);
```
'
quickstart_file.write(quickstart_content) or {
eprintln('Error writing to quickstart file: ${err}')
exit(1)
}
// Generate the site
println('Generating site...')
site.generate() or {
eprintln('Error generating site: ${err}')
exit(1)
}
println('Site generated successfully!')
// Choose which operation to perform:
// Option 1: Run in development mode
// This will start a development server in a screen session
println('Starting development server...')
site.dev() or {
eprintln('Error starting development server: ${err}')
exit(1)
}
// Option 2: Build for production (uncomment to use)
/*
println('Building site for production...')
site.build() or {
eprintln('Error building site: ${err}')
exit(1)
}
println('Site built successfully!')
*/
// Option 3: Build and publish to the remote server (uncomment to use)
/*
println('Building and publishing site...')
site.build_publish() or {
eprintln('Error publishing site: ${err}')
exit(1)
}
println('Site published successfully!')
*/
}

View File

@@ -0,0 +1,23 @@
#!/usr/bin/env -S v -n -w -cg -gc none -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.data.doctree
mut tree := doctree.new(name: 'test')!
// path string
// heal bool = true // healing means we fix images
// git_url string
// git_reset bool
// git_root string
// git_pull bool
tree.scan(
git_url: 'https://git.threefold.info/tfgrid/docs_tfgrid4/src/branch/main/collections'
git_pull: false
)!
tree.export(
destination: '/tmp/mdexport'
reset: true
exclude_errors: false
)!

10
examples/web/siteconfig.vsh Executable file
View File

@@ -0,0 +1,10 @@
#!/usr/bin/env -S v -n -w -gc none -cg -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.web.siteconfig
import os
mypath := '${os.dir(@FILE)}/siteconfigexample'
mut sc := siteconfig.new(mypath)!
println(sc)

View File

@@ -0,0 +1,100 @@
!!site.config
name:"depin"
description:"ThreeFold is laying the foundation for a geo aware Web 4, the next generation of the Internet."
tagline:"Geo Aware Internet Platform"
favicon:"img/favicon.png"
image:"img/tf_graph.png"
copyright:"ThreeFold"
!!site.menu
title:"ThreeFold DePIN Tech"
logo_alt:"ThreeFold Logo"
logo_src:"img/logo.svg"
logo_src_dark:"img/new_logo_tft.png"
!!site.menu_item
label:"ThreeFold.io"
href:"https://threefold.io"
position:"right"
!!site.menu_item
label:"Mycelium Network"
href:"https://mycelium.threefold.io/"
position:"right"
!!site.menu_item
label:"AI Box"
href:"https://aibox.threefold.io/"
position:"right"
!!site.footer
style:"dark"
!!site.footer_item
title:"Docs"
label:"Introduction"
href:"https://docs.threefold.io/docs/introduction"
!!site.footer_item
title:"Docs"
label:"Litepaper"
href:"https://docs.threefold.io/docs/litepaper/"
!!site.footer_item
title:"Docs"
label:"Roadmap"
href:"https://docs.threefold.io/docs/roadmap"
!!site.footer_item
title:"Docs"
label:"Manual"
href:"https://manual.grid.tf/"
!!site.footer_item
title:"Features"
label:"Become a Farmer"
href:"https://docs.threefold.io/docs/category/become-a-farmer"
!!site.footer_item
title:"Features"
label:"Components"
href:"https://docs.threefold.io/docs/category/components"
!!site.footer_item
title:"Features"
label:"Technology"
href:"https://threefold.info/tech/"
!!site.footer_item
title:"Features"
label:"Tokenomics"
href:"https://docs.threefold.io/docs/tokens/tokenomics"
!!site.footer_item
title:"Web"
label:"ThreeFold.io"
href:"https://threefold.io"
!!site.footer_item
title:"Web"
label:"Dashboard"
href:"https://dashboard.grid.tf"
!!site.footer_item
title:"Web"
label:"GitHub"
href:"https://github.com/threefoldtech/home"
!!site.footer_item
title:"Web"
label:"Mycelium Network"
href:"https://mycelium.threefold.io/"
!!site.footer_item
title:"Web"
label:"AI Box"
href:"https://www2.aibox.threefold.io/"
!!site.collections
url:"https://github.com/example/external-docs"
replace:"PROJECT_NAME:My Project, VERSION:1.0.0"

View File

@@ -0,0 +1,18 @@
!!site.page name:intro
description:"ThreeFold is laying the foundation for a geo aware Web 4, the next generation of the Internet."
//next is example where we use all properties, folder is where the page is located, prio is the order of the page, if not used the filled in from order in which we parse this config file
!!site.page name:mycelium draft:true folder:"/specs/components" prio:4
content:"the page content itself, only for small pages"
title:"Mycelium as Title"
description:"..."
!!site.page name:fungistor folder:"/specs/components" prio:1
src:"mycollection:mycelium.md"
title:"fungistor as Title"
description:"...."
!!site.page name:fungistor folder:"/specs/components" prio:1
src:"mycollection:mycelium" //can be without .md
title:"fungistor as Title"
description:"..."

View File

@@ -10,7 +10,7 @@ mut docs := starlight.new(
// Create a new starlight site
mut site := docs.get(
url: 'https://git.ourworld.tf/tfgrid/docs_aibox'
url: 'https://git.threefold.info/tfgrid/docs_aibox'
init: true // init means we put config files if not there
)!

1
examples/webdav/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
webdav_vfs

View File

@@ -1,69 +0,0 @@
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.vfs.webdav
import cli { Command, Flag }
import os
fn main() {
mut cmd := Command{
name: 'webdav'
description: 'Vlang Webdav Server'
}
mut app := Command{
name: 'webdav'
description: 'Vlang Webdav Server'
execute: fn (cmd Command) ! {
port := cmd.flags.get_int('port')!
directory := cmd.flags.get_string('directory')!
user := cmd.flags.get_string('user')!
password := cmd.flags.get_string('password')!
mut server := webdav.new_app(
root_dir: directory
server_port: port
user_db: {
user: password
}
)!
server.run()
return
}
}
app.add_flag(Flag{
flag: .int
name: 'port'
abbrev: 'p'
description: 'server port'
default_value: ['8000']
})
app.add_flag(Flag{
flag: .string
required: true
name: 'directory'
abbrev: 'd'
description: 'server directory'
})
app.add_flag(Flag{
flag: .string
required: true
name: 'user'
abbrev: 'u'
description: 'username'
})
app.add_flag(Flag{
flag: .string
required: true
name: 'password'
abbrev: 'pw'
description: 'user password'
})
app.setup()
app.parse(os.args)
}

View File

@@ -8,8 +8,8 @@ import log
const database_path = os.join_path(os.dir(@FILE), 'database')
mut metadata_db := ourdb.new(path: os.join_path(database_path, 'metadata'))!
mut data_db := ourdb.new(path: os.join_path(database_path, 'data'))!
mut metadata_db := ourdb.new(path: os.join_path(database_path, 'metadata'), reset: true)!
mut data_db := ourdb.new(path: os.join_path(database_path, 'data', reset: true))!
mut vfs := vfs_db.new(mut metadata_db, mut data_db)!
mut server := webdav.new_server(
vfs: vfs

View File

@@ -1,26 +0,0 @@
#!/usr/bin/env -S v -n -w -cg -gc none -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.herolib.data.doctree
mut tree := doctree.new(name: 'test')!
// path string
// heal bool = true // healing means we fix images
// git_url string
// git_reset bool
// git_root string
// git_pull bool
// load bool = true // means we scan automatically the added collection
for project in 'projectinca, legal, why'.split(',').map(it.trim_space()) {
tree.scan(
git_url: 'https://git.ourworld.tf/tfgrid/info_tfgrid/src/branch/development/collections/${project}'
git_pull: false
)!
}
tree.export(
destination: '/tmp/mdexport'
reset: true
// keep_structure: true
exclude_errors: false
)!

View File

@@ -4,7 +4,7 @@ set -e
os_name="$(uname -s)"
arch_name="$(uname -m)"
version='1.0.22'
version='1.0.26'
# Base URL for GitHub releases

View File

@@ -60,6 +60,22 @@ command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to run commands with sudo if needed
function run_sudo() {
# Check if we're already root
if [ "$(id -u)" -eq 0 ]; then
# We are root, run the command directly
"$@"
# Check if sudo is installed
elif command_exists sudo; then
# Use sudo to run the command
sudo "$@"
else
# No sudo available, try to run directly
"$@"
fi
}
export DIR_BASE="$HOME"
export DIR_BUILD="/tmp"
export DIR_CODE="$DIR_BASE/code"
@@ -72,9 +88,9 @@ function sshknownkeysadd {
then
ssh-keyscan github.com >> ~/.ssh/known_hosts
fi
if ! grep git.ourworld.tf ~/.ssh/known_hosts > /dev/null
if ! grep git.threefold.info ~/.ssh/known_hosts > /dev/null
then
ssh-keyscan git.ourworld.tf >> ~/.ssh/known_hosts
ssh-keyscan git.threefold.info >> ~/.ssh/known_hosts
fi
git config --global pull.rebase false
@@ -93,7 +109,7 @@ function package_install {
local command_name="$1"
if [[ "${OSNAME}" == "ubuntu" ]]; then
if is_github_actions; then
sudo apt -o Dpkg::Options::="--force-confold" -o Dpkg::Options::="--force-confdef" install $1 -q -y --allow-downgrades --allow-remove-essential
run_sudo apt -o Dpkg::Options::="--force-confold" -o Dpkg::Options::="--force-confdef" install $1 -q -y --allow-downgrades --allow-remove-essential
else
apt -o Dpkg::Options::="--force-confold" -o Dpkg::Options::="--force-confdef" install $1 -q -y --allow-downgrades --allow-remove-essential
fi
@@ -167,8 +183,8 @@ function os_update {
fi
export TERM=xterm
export DEBIAN_FRONTEND=noninteractive
sudo dpkg --configure -a
sudo apt update -y
run_sudo dpkg --configure -a
run_sudo apt update -y
if is_github_actions; then
echo "** IN GITHUB ACTIONS, DON'T DO UPDATE"
else
@@ -242,8 +258,11 @@ function hero_lib_get {
}
function install_secp256k1 {
echo "Installing secp256k1..."
if [[ "${OSNAME}" == "darwin"* ]]; then
# Attempt installation only if not already found
echo "Attempting secp256k1 installation via Homebrew..."
brew install secp256k1
elif [[ "${OSNAME}" == "ubuntu" ]]; then
# Install build dependencies
@@ -260,7 +279,7 @@ function install_secp256k1 {
./configure
make -j 5
if is_github_actions; then
sudo make install
run_sudo make install
else
make install
fi
@@ -281,16 +300,16 @@ remove_all() {
# Set reset to true to use existing reset functionality
RESET=true
# Call reset functionality
sudo rm -rf ~/code/v
sudo rm -rf ~/_code/v
sudo rm -rf ~/.config/v-analyzer
run_sudo rm -rf ~/code/v
run_sudo rm -rf ~/_code/v
run_sudo rm -rf ~/.config/v-analyzer
if command_exists v; then
echo "Removing V from system..."
sudo rm -f $(which v)
run_sudo rm -f $(which v)
fi
if command_exists v-analyzer; then
echo "Removing v-analyzer from system..."
sudo rm -f $(which v-analyzer)
run_sudo rm -f $(which v-analyzer)
fi
# Remove v-analyzer path from rc files
@@ -317,8 +336,6 @@ remove_all() {
# Function to check if a service is running and start it if needed
check_and_start_redis() {
# Normal service management for non-container environments
if [[ "${OSNAME}" == "ubuntu" ]] || [[ "${OSNAME}" == "debian" ]]; then
@@ -326,12 +343,12 @@ check_and_start_redis() {
if is_github_actions; then
# Import Redis GPG key
curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
curl -fsSL https://packages.redis.io/gpg | run_sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
# Add Redis repository
echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list
echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | run_sudo tee /etc/apt/sources.list.d/redis.list
# Install Redis
sudo apt-get update
sudo apt-get install -y redis
run_sudo apt-get update
run_sudo apt-get install -y redis
# Start Redis
redis-server --daemonize yes
@@ -366,7 +383,7 @@ check_and_start_redis() {
echo "redis is already running."
else
echo "redis is not running. Starting it..."
sudo systemctl start "redis"
run_sudo systemctl start "redis"
if systemctl is-active --quiet "redis"; then
echo "redis started successfully."
else
@@ -375,11 +392,29 @@ check_and_start_redis() {
fi
fi
elif [[ "${OSNAME}" == "darwin"* ]]; then
if brew services list | grep -q "^redis.*started"; then
echo "redis is already running."
# Check if we're in GitHub Actions
if is_github_actions; then
echo "Running in GitHub Actions on macOS. Starting redis directly..."
if pgrep redis-server > /dev/null; then
echo "redis is already running."
else
echo "redis is not running. Starting it in the background..."
redis-server --daemonize yes
if pgrep redis-server > /dev/null; then
echo "redis started successfully."
else
echo "Failed to start redis. Please check logs for details."
exit 1
fi
fi
else
echo "redis is not running. Starting it..."
brew services start redis
# For regular macOS environments, use brew services
if brew services list | grep -q "^redis.*started"; then
echo "redis is already running."
else
echo "redis is not running. Starting it..."
brew services start redis
fi
fi
elif [[ "${OSNAME}" == "alpine"* ]]; then
if rc-service "redis" status | grep -q "running"; then
@@ -393,7 +428,7 @@ check_and_start_redis() {
echo "redis is already running."
else
echo "redis is not running. Starting it..."
sudo systemctl start "redis"
run_sudo systemctl start "redis"
fi
else
echo "Service management for redis is not implemented for platform: $OSNAME"
@@ -403,17 +438,48 @@ check_and_start_redis() {
v-install() {
# Check if v is already installed and in PATH
if command_exists v; then
echo "V is already installed and in PATH."
# Optionally, verify the installation location or version if needed
# For now, just exit the function assuming it's okay
return 0
fi
# Only clone and install if directory doesn't exist
if [ ! -d ~/code/v ]; then
echo "Installing V..."
# Note: The original check was for ~/code/v, but the installation happens in ~/_code/v.
if [ ! -d ~/_code/v ]; then
echo "Cloning V..."
mkdir -p ~/_code
cd ~/_code
git clone --depth=1 https://github.com/vlang/v
cd v
make
sudo ./v symlink
if ! git clone --depth=1 https://github.com/vlang/v; then
echo "❌ Failed to clone V. Cleaning up..."
rm -rf "$V_DIR"
exit 1
fi
fi
# Only clone and install if directory doesn't exist
# Note: The original check was for ~/code/v, but the installation happens in ~/_code/v.
# Adjusting the check to the actual installation directory.
echo "Building V..."
cd ~/_code/v
make
# Verify the build produced the executable
if [ ! -x ~/_code/v/v ]; then
echo "Error: V build failed, executable ~/_code/v/v not found or not executable."
exit 1
fi
# Check if the built executable can report its version
if ! ~/_code/v/v -version > /dev/null 2>&1; then
echo "Error: Built V executable (~/_code/v/v) failed to report version."
exit 1
fi
echo "V built successfully. Creating symlink..."
run_sudo ./v symlink
# Verify v is in path
if ! command_exists v; then
echo "Error: V installation failed or not in PATH"
@@ -428,9 +494,12 @@ v-install() {
v-analyzer() {
set -ex
# Install v-analyzer if requested
if [ "$INSTALL_ANALYZER" = true ]; then
echo "Installing v-analyzer..."
cd /tmp
v download -RD https://raw.githubusercontent.com/vlang/v-analyzer/main/install.vsh
# Check if v-analyzer bin directory exists
@@ -499,10 +568,7 @@ if [ "$RESET" = true ] || ! command_exists v; then
v-install
# Only install v-analyzer if not in GitHub Actions environment
if ! is_github_actions; then
v-analyzer
fi
fi
@@ -516,6 +582,10 @@ fi
if [ "$INSTALL_ANALYZER" = true ]; then
# Only install v-analyzer if not in GitHub Actions environment
if ! is_github_actions; then
v-analyzer
fi
echo "Run 'source ~/.bashrc' or 'source ~/.zshrc' to update PATH for v-analyzer"
fi

Binary file not shown.

123
lib/ai/escalayer/README.md Normal file
View File

@@ -0,0 +1,123 @@
# Escalayer
Escalayer is a module for executing AI tasks with automatic escalation to more powerful models when needed. It provides a framework for creating complex AI workflows by breaking them down into sequential unit tasks.
## Overview
Escalayer allows you to:
1. Create complex AI tasks composed of multiple sequential unit tasks
2. Execute each unit task with a cheap AI model first
3. Automatically retry with a more powerful model if the task fails
4. Process and validate AI responses with custom callback functions
## Architecture
The module is organized into the following components:
- **Task**: Represents a complete AI task composed of multiple sequential unit tasks
- **UnitTask**: Represents a single step in the task with prompt generation and response processing
- **ModelConfig**: Defines the configuration for an AI model
- **OpenRouter Integration**: Uses OpenRouter to access a wide range of AI models
## Usage
### Basic Example
```v
import freeflowuniverse.herolib.ai.mcp.aitools.escalayer
fn main() {
// Create a new task
mut task := escalayer.new_task(
name: 'rhai_wrapper_creator'
description: 'Create Rhai wrappers for Rust functions'
)
// Define the unit tasks
task.new_unit_task(
name: 'separate_functions'
prompt_function: separate_functions
callback_function: process_functions
)
// Initiate the task
result := task.initiate('path/to/rust/file.rs') or {
println('Task failed: ${err}')
return
}
println('Task completed successfully')
println(result)
}
// Define the prompt function
fn separate_functions(input string) string {
return 'Read rust file and separate it into functions ${input}'
}
// Define the callback function
fn process_functions(response string)! string {
// Process the AI response
// Return error if processing fails
if response.contains('error') {
return error('Failed to process functions: Invalid response format')
}
return response
}
```
### Advanced Configuration
You can configure each unit task with different models, retry counts, and other parameters:
```v
// Configure with custom parameters
task.new_unit_task(
name: 'create_wrappers'
prompt_function: create_wrappers
callback_function: process_wrappers
retry_count: 2
base_model: escalayer.ModelConfig{
name: 'claude-3-haiku-20240307'
provider: 'anthropic'
temperature: 0.5
max_tokens: 4000
}
)
```
## How It Works
1. When you call `task.initiate(input)`, the first unit task is executed with its prompt function.
2. The prompt is sent to the base AI model.
3. The response is processed by the callback function.
4. If the callback returns an error, the task is retried with the same model.
5. After a specified number of retries, the task escalates to a more powerful model.
6. Once a unit task succeeds, its result is passed as input to the next unit task.
7. This process continues until all unit tasks are completed.
## Environment Setup
Escalayer uses OpenRouter for AI model access. Set the following environment variable:
```
OPENROUTER_API_KEY=your_api_key_here
```
You can get an API key from [OpenRouter](https://openrouter.ai/).
## Original Requirements
This module was designed based on the following requirements:
- Create a system for executing AI tasks with a retry mechanism
- Escalate to more powerful models if cheaper models fail
- Use OpenAI client over OpenRouter for AI calls
- Break down complex tasks into sequential unit tasks
- Each unit task has a function that generates a prompt and a callback that processes the response
- Retry if the callback returns an error, with the error message prepended to the input string
For a detailed architecture overview, see [escalayer_architecture.md](./escalayer_architecture.md).
For a complete example, see [example.v](../servers/rhai).

View File

@@ -0,0 +1,40 @@
module escalayer
import freeflowuniverse.herolib.clients.openai
// TaskParams defines the parameters for creating a new task
@[params]
pub struct TaskParams {
pub:
name string
description string
}
// Create a new task
pub fn new_task(params TaskParams) &Task {
return &Task{
name: params.name
description: params.description
unit_tasks: []
current_result: ''
}
}
// Default model configurations
pub fn default_base_model() ModelConfig {
return ModelConfig{
name: 'qwen2.5-7b-instruct'
provider: 'openai'
temperature: 0.7
max_tokens: 2000
}
}
pub fn default_retry_model() ModelConfig {
return ModelConfig{
name: 'gpt-4'
provider: 'openai'
temperature: 0.7
max_tokens: 4000
}
}

View File

@@ -0,0 +1,342 @@
# Escalayer Architecture
This document outlines the architecture for the Escalayer module, which provides a framework for executing AI tasks with automatic escalation to more powerful models when needed.
## 1. Module Structure
```
lib/mcp/aitools/escalayer/
├── escalayer.v # Main module file with public API
├── task.v # Task implementation
├── unit_task.v # Unit task implementation
├── models.v # Model definitions and configurations
├── openrouter.v # OpenRouter API client
└── README.md # Documentation
```
## 2. Core Components
### 2.1 Data Structures
```mermaid
classDiagram
class Task {
+string name
+string description
+[]UnitTask unit_tasks
+string current_result
+new_unit_task(params UnitTaskParams) UnitTask
+initiate(input string)! string
}
class UnitTask {
+string name
+Function prompt_function
+Function callback_function
+ModelConfig base_model
+ModelConfig retry_model
+int retry_count
+execute(input string)! string
}
class ModelConfig {
+string name
+string provider
+float temperature
+int max_tokens
}
Task "1" *-- "many" UnitTask : contains
UnitTask "1" *-- "1" ModelConfig : base_model
UnitTask "1" *-- "1" ModelConfig : retry_model
```
### 2.2 Component Descriptions
#### Task
- Represents a complete AI task composed of multiple sequential unit tasks
- Manages the flow of data between unit tasks
- Tracks overall task progress and results
#### UnitTask
- Represents a single step in the task
- Contains a prompt function that generates the AI prompt
- Contains a callback function that processes the AI response
- Manages retries and model escalation
#### ModelConfig
- Defines the configuration for an AI model
- Includes model name, provider, and parameters like temperature and max tokens
#### OpenRouter Client
- Handles communication with the OpenRouter API
- Sends prompts to AI models and receives responses
## 3. Implementation Details
### 3.1 escalayer.v (Main Module)
```v
module escalayer
import freeflowuniverse.herolib.clients.openai
// TaskParams defines the parameters for creating a new task
@[params]
pub struct TaskParams {
pub:
name string
description string
}
// Create a new task
pub fn new_task(params TaskParams) &Task {
return &Task{
name: params.name
description: params.description
unit_tasks: []
current_result: ''
}
}
// Default model configurations
pub fn default_base_model() ModelConfig {
return ModelConfig{
name: 'gpt-3.5-turbo'
provider: 'openai'
temperature: 0.7
max_tokens: 20000
}
}
pub fn default_retry_model() ModelConfig {
return ModelConfig{
name: 'gpt-4'
provider: 'openai'
temperature: 0.7
max_tokens: 40000
}
}
```
### 3.2 task.v
```v
module escalayer
// Task represents a complete AI task composed of multiple sequential unit tasks
pub struct Task {
pub mut:
name string
description string
unit_tasks []UnitTask
current_result string
}
// UnitTaskParams defines the parameters for creating a new unit task
struct UnitTaskParams {
name string
prompt_function fn(string) string
callback_function fn(string)! string
base_model ?ModelConfig
retry_model ?ModelConfig
retry_count ?int
}
// Add a new unit task to the task
pub fn (mut t Task) new_unit_task(params UnitTaskParams) &UnitTask {
}
// Initiate the task execution
pub fn (mut t Task) initiate(input string)! string {
```
### 3.3 unit_task.v
```v
module escalayer
import freeflowuniverse.herolib.clients.openai
// UnitTask represents a single step in the task
pub struct UnitTask {
pub mut:
name string
prompt_function fn(string) string
callback_function fn(string)! string
base_model ModelConfig
retry_model ModelConfig
retry_count int
}
// Execute the unit task
pub fn (mut ut UnitTask) execute(input string)! string {
}
```
### 3.4 models.v
```v
module escalayer
// ModelConfig defines the configuration for an AI model
pub struct ModelConfig {
pub mut:
name string
provider string
temperature f32
max_tokens int
}
// Call an AI model using OpenRouter
fn call_ai_model(prompt string, model ModelConfig)! string {
// Get OpenAI client (configured for OpenRouter)
mut client := get_openrouter_client()!
// Create the message for the AI
mut m := openai.Messages{
messages: [
openai.Message{
role: .system
content: 'You are a helpful assistant.'
},
openai.Message{
role: .user
content: prompt
}
]
}
// Call the AI model
res := client.chat_completion(
msgs: m,
model: model.name,
temperature: model.temperature,
max_completion_tokens: model.max_tokens
)!
// Extract the response content
if res.choices.len > 0 {
return res.choices[0].message.content
}
return error('No response from AI model')
}
```
### 3.5 openrouter.v
```v
module escalayer
import freeflowuniverse.herolib.clients.openai
import os
// Get an OpenAI client configured for OpenRouter
fn get_openrouter_client()! &openai.OpenAI {
// Get API key from environment variable
api_key := os.getenv('OPENROUTER_API_KEY')
if api_key == '' {
return error('OPENROUTER_API_KEY environment variable not set')
}
// Create OpenAI client with OpenRouter base URL
mut client := openai.new(
api_key: api_key,
base_url: 'https://openrouter.ai/api/v1'
)!
return client
}
```
## 4. Usage Example
```v
import freeflowuniverse.herolib.ai.mcp.aitools.escalayer
fn main() {
// Create a new task
mut task := escalayer.new_task(
name: 'rhai_wrapper_creator'
description: 'Create Rhai wrappers for Rust functions'
)
// Define the unit tasks
task.new_unit_task(
name: 'separate_functions'
prompt_function: separate_functions
callback_function: process_functions
)
task.new_unit_task(
name: 'create_wrappers'
prompt_function: create_wrappers
callback_function: process_wrappers
retry_count: 2
)
task.new_unit_task(
name: 'create_tests'
prompt_function: create_tests
callback_function: process_tests
base_model: escalayer.ModelConfig{
name: 'claude-3-haiku-20240307'
provider: 'anthropic'
temperature: 0.5
max_tokens: 4000
}
)
// Initiate the task
result := task.initiate('path/to/rust/file.rs') or {
println('Task failed: ${err}')
return
}
println('Task completed successfully')
println(result)
}
// Define the prompt functions
fn separate_functions(input string) string {
return 'Read rust file and separate it into functions ${input}'
}
fn create_wrappers(input string) string {
return 'Create rhai wrappers for rust functions ${input}'
}
fn create_tests(input string) string {
return 'Create tests for rhai wrappers ${input}'
}
// Define the callback functions
fn process_functions(response string)! string {
// Process the AI response
// Return error if processing fails
if response.contains('error') {
return error('Failed to process functions: Invalid response format')
}
return response
}
fn process_wrappers(response string)! string {
// Process the AI response
// Return error if processing fails
if !response.contains('fn') {
return error('Failed to process wrappers: No functions found')
}
return response
}
fn process_tests(response string)! string {
// Process the AI response
// Return error if processing fails
if !response.contains('test') {
return error('Failed to process tests: No tests found')
}
return response
}
```

62
lib/ai/escalayer/models.v Normal file
View File

@@ -0,0 +1,62 @@
module escalayer
import freeflowuniverse.herolib.clients.openai
// ModelConfig defines the configuration for an AI model
pub struct ModelConfig {
pub mut:
name string
provider string
temperature f32
max_tokens int
}
// Create model configs
const claude_3_sonnet = ModelConfig{
name: 'anthropic/claude-3.7-sonnet'
provider: 'anthropic'
temperature: 0.7
max_tokens: 25000
}
const gpt4 = ModelConfig{
name: 'gpt-4'
provider: 'openai'
temperature: 0.7
max_tokens: 25000
}
// Call an AI model using OpenRouter
fn call_ai_model(prompt string, model ModelConfig) !string {
// Get OpenAI client (configured for OpenRouter)
mut client := get_openrouter_client()!
// Create the message for the AI
mut m := openai.Messages{
messages: [
openai.Message{
role: .system
content: 'You are a helpful assistant.'
},
openai.Message{
role: .user
content: prompt
},
]
}
// Call the AI model
res := client.chat_completion(
msgs: m
model: model.name
temperature: model.temperature
max_completion_tokens: model.max_tokens
)!
// Extract the response content
if res.choices.len > 0 {
return res.choices[0].message.content
}
return error('No response from AI model')
}

View File

@@ -0,0 +1,22 @@
module escalayer
import freeflowuniverse.herolib.clients.openai
import freeflowuniverse.herolib.osal
import os
// Get an OpenAI client configured for OpenRouter
fn get_openrouter_client() !&openai.OpenAI {
osal.env_set(key: 'OPENROUTER_API_KEY', value: '')
// Get API key from environment variable
api_key := os.getenv('OPENROUTER_API_KEY')
if api_key == '' {
return error('OPENROUTER_API_KEY environment variable not set')
}
// Create OpenAI client with OpenRouter base URL
mut client := openai.get(
name: 'openrouter'
)!
return client
}

65
lib/ai/escalayer/task.v Normal file
View File

@@ -0,0 +1,65 @@
module escalayer
import log
// Task represents a complete AI task composed of multiple sequential unit tasks
pub struct Task {
pub mut:
name string
description string
unit_tasks []UnitTask
current_result string
}
// UnitTaskParams defines the parameters for creating a new unit task
@[params]
pub struct UnitTaskParams {
pub:
name string
prompt_function fn (string) string
callback_function fn (string) !string
base_model ?ModelConfig
retry_model ?ModelConfig
retry_count ?int
}
// Add a new unit task to the task
pub fn (mut t Task) new_unit_task(params UnitTaskParams) &UnitTask {
mut unit_task := UnitTask{
name: params.name
prompt_function: params.prompt_function
callback_function: params.callback_function
base_model: if base_model := params.base_model {
base_model
} else {
default_base_model()
}
retry_model: if retry_model := params.retry_model {
retry_model
} else {
default_retry_model()
}
retry_count: if retry_count := params.retry_count { retry_count } else { 3 }
}
t.unit_tasks << unit_task
return &t.unit_tasks[t.unit_tasks.len - 1]
}
// Initiate the task execution
pub fn (mut t Task) initiate(input string) !string {
mut current_input := input
for i, mut unit_task in t.unit_tasks {
log.error('Executing unit task ${i + 1}/${t.unit_tasks.len}: ${unit_task.name}')
// Execute the unit task with the current input
result := unit_task.execute(current_input)!
// Update the current input for the next unit task
current_input = result
t.current_result = result
}
return t.current_result
}

View File

@@ -0,0 +1,71 @@
module escalayer
import log
import freeflowuniverse.herolib.clients.openai
// UnitTask represents a single step in the task
pub struct UnitTask {
pub mut:
name string
prompt_function fn (string) string
callback_function fn (string) !string
base_model ModelConfig
retry_model ModelConfig
retry_count int
}
// Execute the unit task
pub fn (mut ut UnitTask) execute(input string) !string {
// Generate the prompt using the prompt function
prompt := ut.prompt_function(input)
// Try with the base model first
mut current_model := ut.base_model
mut attempts := 0
mut max_attempts := ut.retry_count + 1 // +1 for the initial attempt
mut absolute_max_attempts := 1 // Hard limit on total attempts
mut last_error := ''
for attempts < max_attempts && attempts < absolute_max_attempts {
attempts++
// If we've exhausted retries with the base model, switch to the retry model
if attempts > ut.retry_count {
log.error('Escalating to more powerful model: ${ut.retry_model.name}')
current_model = ut.retry_model
// Calculate remaining attempts but don't exceed absolute max
max_attempts = attempts + ut.retry_count
if max_attempts > absolute_max_attempts {
max_attempts = absolute_max_attempts
}
}
log.error('Attempt ${attempts} with model ${current_model.name}')
// Prepare the prompt with error feedback if this is a retry
mut current_prompt := prompt
if last_error != '' {
current_prompt = 'Previous attempt failed with error: ${last_error}\n\n${prompt}'
}
// Call the AI model
response := call_ai_model(current_prompt, current_model) or {
log.error('AI call failed: ${err}')
last_error = err.str()
continue // Try again
}
// Process the response with the callback function
result := ut.callback_function(response) or {
// If callback returns an error, retry with the error message
log.error('Callback returned error: ${err}')
last_error = err.str()
continue // Try again
}
// If we get here, the callback was successful
return result
}
return error('Failed to execute unit task after ${attempts} attempts. Last error: ${last_error}')
}

68
lib/ai/mcp/README.md Normal file
View File

@@ -0,0 +1,68 @@
# Model Context Protocol (MCP) Implementation
This module provides a V language implementation of the [Model Context Protocol (MCP)](https://spec.modelcontextprotocol.io/specification/2024-11-05/) specification. MCP is a protocol designed to standardize communication between AI models and their context providers.
## Overview
The MCP module serves as a core library for building MCP-compliant servers in V. Its main purpose is to provide all the boilerplate MCP functionality, so implementers only need to define and register their specific handlers. The module handles the Standard Input/Output (stdio) transport as described in the [MCP transport specification](https://modelcontextprotocol.io/docs/concepts/transports), enabling standardized communication between AI models and their context providers.
The module implements all the required MCP protocol methods (resources/list, tools/list, prompts/list, etc.) and manages the underlying JSON-RPC communication, allowing developers to focus solely on implementing their specific tools and handlers. The module itself is not a standalone server but rather a framework that can be used to build different MCP server implementations. The subdirectories within this module (such as `baobab` and `developer`) contain specific implementations of MCP servers that utilize this core framework.
## to test
```
curl -fsSL https://bun.sh/install | bash
source /root/.bashrc
```
## Key Components
- **Server**: The main MCP server struct that handles JSON-RPC requests and responses
- **Backend Interface**: Abstraction for different backend implementations (memory-based by default)
- **Model Configuration**: Structures representing client and server capabilities according to the MCP specification
- **Protocol Handlers**: Implementation of MCP protocol handlers for resources, prompts, tools, and initialization
- **Factory**: Functions to create and configure an MCP server with custom backends and handlers
## Features
- Complete implementation of the MCP protocol version 2024-11-05
- Handles all boilerplate protocol methods (resources/list, tools/list, prompts/list, etc.)
- JSON-RPC based communication layer with automatic request/response handling
- Support for client-server capability negotiation
- Pluggable backend system for different storage and processing needs
- Generic type conversion utilities for MCP tool content
- Comprehensive error handling
- Logging capabilities
- Minimal implementation requirements for server developers
## Usage
To create a new MCP server using the core module:
```v
import freeflowuniverse.herolib.ai.mcp
import freeflowuniverse.herolib.schemas.jsonrpc
// Create a backend (memory-based or custom implementation)
backend := mcp.MemoryBackend{
tools: {
'my_tool': my_tool_definition
}
tool_handlers: {
'my_tool': my_tool_handler
}
}
// Create and configure the server
mut server := mcp.new_server(backend, mcp.ServerParams{
config: mcp.ServerConfiguration{
server_info: mcp.ServerInfo{
name: 'my_mcp_server'
version: '1.0.0'
}
}
})!
// Start the server
server.start()!
```

3
lib/ai/mcp/README2.md Normal file
View File

@@ -0,0 +1,3 @@
If logic is implemented in mcp module, than structure with folders logic and mcp, where logic residers in /logic and mcp related code (like tool and prompt handlers and server code) in /mcp

View File

@@ -0,0 +1,32 @@
module mcp
import x.json2
interface Backend {
// Resource methods
resource_exists(uri string) !bool
resource_get(uri string) !Resource
resource_list() ![]Resource
resource_subscribed(uri string) !bool
resource_contents_get(uri string) ![]ResourceContent
resource_templates_list() ![]ResourceTemplate
// Prompt methods
prompt_exists(name string) !bool
prompt_get(name string) !Prompt
prompt_call(name string, arguments []string) ![]PromptMessage
prompt_list() ![]Prompt
prompt_messages_get(name string, arguments map[string]string) ![]PromptMessage
// Tool methods
tool_exists(name string) !bool
tool_get(name string) !Tool
tool_list() ![]Tool
tool_call(name string, arguments map[string]json2.Any) !ToolCallResult
// Sampling methods
sampling_create_message(params map[string]json2.Any) !SamplingCreateMessageResult
mut:
resource_subscribe(uri string) !
resource_unsubscribe(uri string) !
}

183
lib/ai/mcp/backend_memory.v Normal file
View File

@@ -0,0 +1,183 @@
module mcp
import x.json2
pub struct MemoryBackend {
pub mut:
// Resource related fields
resources map[string]Resource
subscriptions []string // list of subscribed resource uri's
resource_contents map[string][]ResourceContent
resource_templates map[string]ResourceTemplate
// Prompt related fields
prompts map[string]Prompt
prompt_messages map[string][]PromptMessage
prompt_handlers map[string]PromptHandler
// Tool related fields
tools map[string]Tool
tool_handlers map[string]ToolHandler
// Sampling related fields
sampling_handler SamplingHandler
}
pub type ToolHandler = fn (arguments map[string]json2.Any) !ToolCallResult
pub type PromptHandler = fn (arguments []string) ![]PromptMessage
pub type SamplingHandler = fn (params map[string]json2.Any) !SamplingCreateMessageResult
fn (b &MemoryBackend) resource_exists(uri string) !bool {
return uri in b.resources
}
fn (b &MemoryBackend) resource_get(uri string) !Resource {
return b.resources[uri] or { return error('resource not found') }
}
fn (b &MemoryBackend) resource_list() ![]Resource {
return b.resources.values()
}
fn (mut b MemoryBackend) resource_subscribe(uri string) ! {
if uri !in b.subscriptions {
b.subscriptions << uri
}
}
fn (b &MemoryBackend) resource_subscribed(uri string) !bool {
return uri in b.subscriptions
}
fn (mut b MemoryBackend) resource_unsubscribe(uri string) ! {
b.subscriptions = b.subscriptions.filter(it != uri)
}
fn (b &MemoryBackend) resource_contents_get(uri string) ![]ResourceContent {
return b.resource_contents[uri] or { return error('resource contents not found') }
}
fn (b &MemoryBackend) resource_templates_list() ![]ResourceTemplate {
return b.resource_templates.values()
}
// Prompt related methods
fn (b &MemoryBackend) prompt_exists(name string) !bool {
return name in b.prompts
}
fn (b &MemoryBackend) prompt_get(name string) !Prompt {
return b.prompts[name] or { return error('prompt not found') }
}
fn (b &MemoryBackend) prompt_list() ![]Prompt {
return b.prompts.values()
}
fn (b &MemoryBackend) prompt_messages_get(name string, arguments map[string]string) ![]PromptMessage {
// Get the base messages for this prompt
base_messages := b.prompt_messages[name] or { return error('prompt messages not found') }
// Apply arguments to the messages
mut messages := []PromptMessage{}
for msg in base_messages {
mut content := msg.content
// If the content is text, replace argument placeholders
if content.typ == 'text' {
mut text := content.text
// Replace each argument in the text
for arg_name, arg_value in arguments {
text = text.replace('{{${arg_name}}}', arg_value)
}
content = PromptContent{
typ: content.typ
text: text
data: content.data
mimetype: content.mimetype
resource: content.resource
}
}
messages << PromptMessage{
role: msg.role
content: content
}
}
return messages
}
fn (b &MemoryBackend) prompt_call(name string, arguments []string) ![]PromptMessage {
// Get the tool handler
handler := b.prompt_handlers[name] or { return error('tool handler not found') }
// Call the handler with the provided arguments
return handler(arguments) or { panic(err) }
}
// Tool related methods
fn (b &MemoryBackend) tool_exists(name string) !bool {
return name in b.tools
}
fn (b &MemoryBackend) tool_get(name string) !Tool {
return b.tools[name] or { return error('tool not found') }
}
fn (b &MemoryBackend) tool_list() ![]Tool {
return b.tools.values()
}
fn (b &MemoryBackend) tool_call(name string, arguments map[string]json2.Any) !ToolCallResult {
// Get the tool handler
handler := b.tool_handlers[name] or { return error('tool handler not found') }
// Call the handler with the provided arguments
return handler(arguments) or {
// If the handler throws an error, return it as a tool error
return ToolCallResult{
is_error: true
content: [
ToolContent{
typ: 'text'
text: 'Error: ${err.msg()}'
},
]
}
}
}
// Sampling related methods
fn (b &MemoryBackend) sampling_create_message(params map[string]json2.Any) !SamplingCreateMessageResult {
// Check if a sampling handler is registered
if isnil(b.sampling_handler) {
// Return a default implementation that just echoes back a message
// indicating that no sampling handler is registered
return SamplingCreateMessageResult{
model: 'default'
stop_reason: 'endTurn'
role: 'assistant'
content: MessageContent{
typ: 'text'
text: 'Sampling is not configured on this server. Please register a sampling handler.'
}
}
}
// Call the sampling handler with the provided parameters
return b.sampling_handler(params)!
}
// register_sampling_handler registers a handler for sampling requests
pub fn (mut b MemoryBackend) register_sampling_handler(handler SamplingHandler) {
b.sampling_handler = handler
}

View File

@@ -0,0 +1,3 @@
## Baobab MCP
The Base Object and Actor Backend MCP Server provides tools to easily generate BaObAB modules for a given OpenAPI or OpenRPC Schema.

View File

@@ -0,0 +1,172 @@
module baobab
import freeflowuniverse.herolib.ai.mcp
import freeflowuniverse.herolib.schemas.jsonschema
import freeflowuniverse.herolib.core.code
import x.json2 as json { Any }
import freeflowuniverse.herolib.baobab.generator
import freeflowuniverse.herolib.baobab.specification
// generate_methods_file MCP Tool
//
const generate_methods_file_tool = mcp.Tool{
name: 'generate_methods_file'
description: 'Generates a methods file with methods for a backend corresponding to thos specified in an OpenAPI or OpenRPC specification'
input_schema: jsonschema.Schema{
typ: 'object'
properties: {
'source': jsonschema.SchemaRef(jsonschema.Schema{
typ: 'object'
properties: {
'openapi_path': jsonschema.SchemaRef(jsonschema.Schema{
typ: 'string'
})
'openrpc_path': jsonschema.SchemaRef(jsonschema.Schema{
typ: 'string'
})
}
})
}
required: ['source']
}
}
pub fn (d &Baobab) generate_methods_file_tool_handler(arguments map[string]Any) !mcp.ToolCallResult {
source := json.decode[generator.Source](arguments['source'].str())!
result := generator.generate_methods_file_str(source) or {
return mcp.error_tool_call_result(err)
}
return mcp.ToolCallResult{
is_error: false
content: mcp.result_to_mcp_tool_contents[string](result)
}
}
// generate_module_from_openapi MCP Tool
const generate_module_from_openapi_tool = mcp.Tool{
name: 'generate_module_from_openapi'
description: ''
input_schema: jsonschema.Schema{
typ: 'object'
properties: {
'openapi_path': jsonschema.SchemaRef(jsonschema.Schema{
typ: 'string'
})
}
required: ['openapi_path']
}
}
pub fn (d &Baobab) generate_module_from_openapi_tool_handler(arguments map[string]Any) !mcp.ToolCallResult {
openapi_path := arguments['openapi_path'].str()
result := generator.generate_module_from_openapi(openapi_path) or {
return mcp.error_tool_call_result(err)
}
return mcp.ToolCallResult{
is_error: false
content: mcp.result_to_mcp_tool_contents[string](result)
}
}
// generate_methods_interface_file MCP Tool
const generate_methods_interface_file_tool = mcp.Tool{
name: 'generate_methods_interface_file'
description: 'Generates a methods interface file with method interfaces for a backend corresponding to those specified in an OpenAPI or OpenRPC specification'
input_schema: jsonschema.Schema{
typ: 'object'
properties: {
'source': jsonschema.SchemaRef(jsonschema.Schema{
typ: 'object'
properties: {
'openapi_path': jsonschema.SchemaRef(jsonschema.Schema{
typ: 'string'
})
'openrpc_path': jsonschema.SchemaRef(jsonschema.Schema{
typ: 'string'
})
}
})
}
required: ['source']
}
}
pub fn (d &Baobab) generate_methods_interface_file_tool_handler(arguments map[string]Any) !mcp.ToolCallResult {
source := json.decode[generator.Source](arguments['source'].str())!
result := generator.generate_methods_interface_file_str(source) or {
return mcp.error_tool_call_result(err)
}
return mcp.ToolCallResult{
is_error: false
content: mcp.result_to_mcp_tool_contents[string](result)
}
}
// generate_model_file MCP Tool
const generate_model_file_tool = mcp.Tool{
name: 'generate_model_file'
description: 'Generates a model file with data structures for a backend corresponding to those specified in an OpenAPI or OpenRPC specification'
input_schema: jsonschema.Schema{
typ: 'object'
properties: {
'source': jsonschema.SchemaRef(jsonschema.Schema{
typ: 'object'
properties: {
'openapi_path': jsonschema.SchemaRef(jsonschema.Schema{
typ: 'string'
})
'openrpc_path': jsonschema.SchemaRef(jsonschema.Schema{
typ: 'string'
})
}
})
}
required: ['source']
}
}
pub fn (d &Baobab) generate_model_file_tool_handler(arguments map[string]Any) !mcp.ToolCallResult {
source := json.decode[generator.Source](arguments['source'].str())!
result := generator.generate_model_file_str(source) or {
return mcp.error_tool_call_result(err)
}
return mcp.ToolCallResult{
is_error: false
content: mcp.result_to_mcp_tool_contents[string](result)
}
}
// generate_methods_example_file MCP Tool
const generate_methods_example_file_tool = mcp.Tool{
name: 'generate_methods_example_file'
description: 'Generates a methods example file with example implementations for a backend corresponding to those specified in an OpenAPI or OpenRPC specification'
input_schema: jsonschema.Schema{
typ: 'object'
properties: {
'source': jsonschema.SchemaRef(jsonschema.Schema{
typ: 'object'
properties: {
'openapi_path': jsonschema.SchemaRef(jsonschema.Schema{
typ: 'string'
})
'openrpc_path': jsonschema.SchemaRef(jsonschema.Schema{
typ: 'string'
})
}
})
}
required: ['source']
}
}
pub fn (d &Baobab) generate_methods_example_file_tool_handler(arguments map[string]Any) !mcp.ToolCallResult {
source := json.decode[generator.Source](arguments['source'].str())!
result := generator.generate_methods_example_file_str(source) or {
return mcp.error_tool_call_result(err)
}
return mcp.ToolCallResult{
is_error: false
content: mcp.result_to_mcp_tool_contents[string](result)
}
}

View File

@@ -0,0 +1,101 @@
module baobab
import freeflowuniverse.herolib.ai.mcp
import freeflowuniverse.herolib.schemas.jsonrpc
import json
import x.json2
import os
// This file contains tests for the Baobab tools implementation.
// It tests the tools' ability to handle tool calls and return expected results.
// test_generate_module_from_openapi_tool tests the generate_module_from_openapi tool definition
fn test_generate_module_from_openapi_tool() {
// Verify the tool definition
assert generate_module_from_openapi_tool.name == 'generate_module_from_openapi', 'Tool name should be "generate_module_from_openapi"'
// Verify the input schema
assert generate_module_from_openapi_tool.input_schema.typ == 'object', 'Input schema type should be "object"'
assert 'openapi_path' in generate_module_from_openapi_tool.input_schema.properties, 'Input schema should have "openapi_path" property'
assert generate_module_from_openapi_tool.input_schema.properties['openapi_path'].typ == 'string', 'openapi_path property should be of type "string"'
assert 'openapi_path' in generate_module_from_openapi_tool.input_schema.required, 'openapi_path should be a required property'
}
// test_generate_module_from_openapi_tool_handler_error tests the error handling of the generate_module_from_openapi tool handler
fn test_generate_module_from_openapi_tool_handler_error() {
// Create arguments with a non-existent file path
mut arguments := map[string]json2.Any{}
arguments['openapi_path'] = json2.Any('non_existent_file.yaml')
// Call the handler
result := generate_module_from_openapi_tool_handler(arguments) or {
// If the handler returns an error, that's expected
assert err.msg().contains(''), 'Error message should not be empty'
return
}
// If we get here, the handler should have returned an error result
assert result.is_error, 'Result should indicate an error'
assert result.content.len > 0, 'Error content should not be empty'
assert result.content[0].typ == 'text', 'Error content should be of type "text"'
assert result.content[0].text.contains('failed to open file'), 'Error content should contain "failed to open file", instead ${result.content[0].text}'
}
// test_mcp_tool_call_integration tests the integration of the tool with the MCP server
fn test_mcp_tool_call_integration() {
// Create a new MCP server
mut server := new_mcp_server() or {
assert false, 'Failed to create MCP server: ${err}'
return
}
// Create a temporary OpenAPI file for testing
temp_dir := os.temp_dir()
temp_file := os.join_path(temp_dir, 'test_openapi.yaml')
os.write_file(temp_file, 'openapi: 3.0.0\ninfo:\n title: Test API\n version: 1.0.0\npaths:\n /test:\n get:\n summary: Test endpoint\n responses:\n "200":\n description: OK') or {
assert false, 'Failed to create temporary file: ${err}'
return
}
// Sample tool call request
tool_call_request := '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"generate_module_from_openapi","arguments":{"openapi_path":"${temp_file}"}}}'
// Process the request through the handler
response := server.handler.handle(tool_call_request) or {
// Clean up the temporary file
os.rm(temp_file) or {}
// If the handler returns an error, that's expected in this test environment
// since we might not have all dependencies set up
return
}
// Clean up the temporary file
os.rm(temp_file) or {}
// Decode the response to verify its structure
decoded_response := jsonrpc.decode_response(response) or {
// In a test environment, we might get an error due to missing dependencies
// This is acceptable for this test
return
}
// If we got a successful response, verify it
if !decoded_response.is_error() {
// Parse the result to verify its contents
result_json := decoded_response.result() or {
assert false, 'Failed to get result: ${err}'
return
}
// Decode the result to check the content
result_map := json2.raw_decode(result_json) or {
assert false, 'Failed to decode result: ${err}'
return
}.as_map()
// Verify the result structure
assert 'isError' in result_map, 'Result should have isError field'
assert 'content' in result_map, 'Result should have content field'
}
}

View File

@@ -0,0 +1,22 @@
module baobab
import cli
pub const command = cli.Command{
sort_flags: true
name: 'baobab'
// execute: cmd_mcpgen
description: 'baobab command'
commands: [
cli.Command{
name: 'start'
execute: cmd_start
description: 'start the Baobab server'
},
]
}
fn cmd_start(cmd cli.Command) ! {
mut server := new_mcp_server(&Baobab{})!
server.start()!
}

View File

@@ -0,0 +1,128 @@
module baobab
import freeflowuniverse.herolib.ai.mcp
import freeflowuniverse.herolib.schemas.jsonrpc
import json
import x.json2
// This file contains tests for the Baobab MCP server implementation.
// It tests the server's ability to initialize and handle tool calls.
// test_new_mcp_server tests the creation of a new MCP server for the Baobab module
fn test_new_mcp_server() {
// Create a new MCP server
mut server := new_mcp_server() or {
assert false, 'Failed to create MCP server: ${err}'
return
}
// Verify server info
assert server.server_info.name == 'developer', 'Server name should be "developer"'
assert server.server_info.version == '1.0.0', 'Server version should be 1.0.0'
// Verify server capabilities
assert server.capabilities.prompts.list_changed == true, 'Prompts capability should have list_changed set to true'
assert server.capabilities.resources.subscribe == true, 'Resources capability should have subscribe set to true'
assert server.capabilities.resources.list_changed == true, 'Resources capability should have list_changed set to true'
assert server.capabilities.tools.list_changed == true, 'Tools capability should have list_changed set to true'
}
// test_mcp_server_initialize tests the initialize handler with a sample initialize request
fn test_mcp_server_initialize() {
// Create a new MCP server
mut server := new_mcp_server() or {
assert false, 'Failed to create MCP server: ${err}'
return
}
// Sample initialize request from the MCP specification
initialize_request := '{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{"sampling":{},"roots":{"listChanged":true}},"clientInfo":{"name":"mcp-inspector","version":"0.0.1"}}}'
// Process the request through the handler
response := server.handler.handle(initialize_request) or {
assert false, 'Handler failed to process request: ${err}'
return
}
// Decode the response to verify its structure
decoded_response := jsonrpc.decode_response(response) or {
assert false, 'Failed to decode response: ${err}'
return
}
// Verify that the response is not an error
assert !decoded_response.is_error(), 'Response should not be an error'
// Parse the result to verify its contents
result_json := decoded_response.result() or {
assert false, 'Failed to get result: ${err}'
return
}
// Decode the result into an ServerConfiguration struct
result := json.decode(mcp.ServerConfiguration, result_json) or {
assert false, 'Failed to decode result: ${err}'
return
}
// Verify the protocol version matches what was requested
assert result.protocol_version == '2024-11-05', 'Protocol version should match the request'
// Verify server info
assert result.server_info.name == 'developer', 'Server name should be "developer"'
}
// test_tools_list tests the tools/list handler to verify tool registration
fn test_tools_list() {
// Create a new MCP server
mut server := new_mcp_server() or {
assert false, 'Failed to create MCP server: ${err}'
return
}
// Sample tools/list request
tools_list_request := '{"jsonrpc":"2.0","id":1,"method":"tools/list","params":{"cursor":""}}'
// Process the request through the handler
response := server.handler.handle(tools_list_request) or {
assert false, 'Handler failed to process request: ${err}'
return
}
// Decode the response to verify its structure
decoded_response := jsonrpc.decode_response(response) or {
assert false, 'Failed to decode response: ${err}'
return
}
// Verify that the response is not an error
assert !decoded_response.is_error(), 'Response should not be an error'
// Parse the result to verify its contents
result_json := decoded_response.result() or {
assert false, 'Failed to get result: ${err}'
return
}
// Decode the result into a map to check the tools
result_map := json2.raw_decode(result_json) or {
assert false, 'Failed to decode result: ${err}'
return
}.as_map()
// Verify that the tools array exists and contains the expected tool
tools := result_map['tools'].arr()
assert tools.len > 0, 'Tools list should not be empty'
// Find the generate_module_from_openapi tool
mut found_tool := false
for tool in tools {
tool_map := tool.as_map()
if tool_map['name'].str() == 'generate_module_from_openapi' {
found_tool = true
break
}
}
assert found_tool, 'generate_module_from_openapi tool should be registered'
}

View File

@@ -0,0 +1,38 @@
module baobab
import freeflowuniverse.herolib.ai.mcp
import freeflowuniverse.herolib.ai.mcp.logger
import freeflowuniverse.herolib.schemas.jsonrpc
@[heap]
pub struct Baobab {}
pub fn new_mcp_server(v &Baobab) !&mcp.Server {
logger.info('Creating new Baobab MCP server')
// Initialize the server with the empty handlers map
mut server := mcp.new_server(mcp.MemoryBackend{
tools: {
'generate_module_from_openapi': generate_module_from_openapi_tool
'generate_methods_file': generate_methods_file_tool
'generate_methods_interface_file': generate_methods_interface_file_tool
'generate_model_file': generate_model_file_tool
'generate_methods_example_file': generate_methods_example_file_tool
}
tool_handlers: {
'generate_module_from_openapi': v.generate_module_from_openapi_tool_handler
'generate_methods_file': v.generate_methods_file_tool_handler
'generate_methods_interface_file': v.generate_methods_interface_file_tool_handler
'generate_model_file': v.generate_model_file_tool_handler
'generate_methods_example_file': v.generate_methods_example_file_tool_handler
}
}, mcp.ServerParams{
config: mcp.ServerConfiguration{
server_info: mcp.ServerInfo{
name: 'baobab'
version: '1.0.0'
}
}
})!
return server
}

68
lib/ai/mcp/cmd/compile.vsh Executable file
View File

@@ -0,0 +1,68 @@
#!/usr/bin/env -S v -n -cg -w -parallel-cc -enable-globals run
import os
import flag
mut fp := flag.new_flag_parser(os.args)
fp.application('compile.vsh')
fp.version('v0.1.0')
fp.description('Compile MCP binary in debug or production mode')
fp.skip_executable()
prod_mode := fp.bool('prod', `p`, false, 'Build production version (optimized)')
help_requested := fp.bool('help', `h`, false, 'Show help message')
if help_requested {
println(fp.usage())
exit(0)
}
additional_args := fp.finalize() or {
eprintln(err)
println(fp.usage())
exit(1)
}
if additional_args.len > 0 {
eprintln('Unexpected arguments: ${additional_args.join(' ')}')
println(fp.usage())
exit(1)
}
// Change to the mcp directory
mcp_dir := os.dir(os.real_path(os.executable()))
os.chdir(mcp_dir) or { panic('Failed to change directory to ${mcp_dir}: ${err}') }
// Set MCPPATH based on OS
mut mcppath := '/usr/local/bin/mcp'
if os.user_os() == 'macos' {
mcppath = os.join_path(os.home_dir(), 'hero/bin/mcp')
}
// Set compilation command based on OS and mode
compile_cmd := if prod_mode {
'v -enable-globals -w -n -prod mcp.v'
} else {
'v -w -cg -gc none -cc tcc -d use_openssl -enable-globals mcp.v'
}
println('Building MCP in ${if prod_mode { 'production' } else { 'debug' }} mode...')
if os.system(compile_cmd) != 0 {
panic('Failed to compile mcp.v with command: ${compile_cmd}')
}
// Make executable
os.chmod('mcp', 0o755) or { panic('Failed to make mcp binary executable: ${err}') }
// Ensure destination directory exists
os.mkdir_all(os.dir(mcppath)) or { panic('Failed to create directory ${os.dir(mcppath)}: ${err}') }
// Copy to destination paths
os.cp('mcp', mcppath) or { panic('Failed to copy mcp binary to ${mcppath}: ${err}') }
os.cp('mcp', '/tmp/mcp') or { panic('Failed to copy mcp binary to /tmp/mcp: ${err}') }
// Clean up
os.rm('mcp') or { panic('Failed to remove temporary mcp binary: ${err}') }
println('**MCP COMPILE OK**')

93
lib/ai/mcp/cmd/mcp.v Normal file
View File

@@ -0,0 +1,93 @@
module main
import os
import cli { Command, Flag }
import freeflowuniverse.herolib.osal
// import freeflowuniverse.herolib.ai.mcp.vcode
// import freeflowuniverse.herolib.ai.mcp.mcpgen
// import freeflowuniverse.herolib.ai.mcp.baobab
import freeflowuniverse.herolib.ai.mcp.rhai.mcp as rhai_mcp
import freeflowuniverse.herolib.ai.mcp.rust
fn main() {
do() or { panic(err) }
}
pub fn do() ! {
mut cmd_mcp := Command{
name: 'mcp'
usage: '
## Manage your MCPs
example:
mcp
'
description: 'create, edit, show mdbooks'
required_args: 0
}
// cmd_run_add_flags(mut cmd_publisher)
cmd_mcp.add_flag(Flag{
flag: .bool
required: false
name: 'debug'
abbrev: 'd'
description: 'show debug output'
})
cmd_mcp.add_flag(Flag{
flag: .bool
required: false
name: 'verbose'
abbrev: 'v'
description: 'show verbose output'
})
mut cmd_inspector := Command{
sort_flags: true
name: 'inspector'
execute: cmd_inspector_execute
description: 'will list existing mdbooks'
}
cmd_inspector.add_flag(Flag{
flag: .string
required: false
name: 'name'
abbrev: 'n'
description: 'name of the MCP'
})
cmd_inspector.add_flag(Flag{
flag: .bool
required: false
name: 'open'
abbrev: 'o'
description: 'open inspector'
})
cmd_mcp.add_command(rhai_mcp.command)
cmd_mcp.add_command(rust.command)
// cmd_mcp.add_command(baobab.command)
// cmd_mcp.add_command(vcode.command)
cmd_mcp.add_command(cmd_inspector)
// cmd_mcp.add_command(vcode.command)
cmd_mcp.setup()
cmd_mcp.parse(os.args)
}
fn cmd_inspector_execute(cmd Command) ! {
open := cmd.flags.get_bool('open') or { false }
if open {
osal.exec(cmd: 'open http://localhost:5173')!
}
name := cmd.flags.get_string('name') or { '' }
if name.len > 0 {
println('starting inspector for MCP ${name}')
osal.exec(cmd: 'npx @modelcontextprotocol/inspector mcp ${name} start')!
} else {
osal.exec(cmd: 'npx @modelcontextprotocol/inspector')!
}
}

49
lib/ai/mcp/factory.v Normal file
View File

@@ -0,0 +1,49 @@
module mcp
import time
import os
import log
import x.json2
import freeflowuniverse.herolib.schemas.jsonrpc
@[params]
pub struct ServerParams {
pub:
handlers map[string]jsonrpc.ProcedureHandler
config ServerConfiguration
}
// new_server creates a new MCP server
pub fn new_server(backend Backend, params ServerParams) !&Server {
mut server := &Server{
ServerConfiguration: params.config
backend: backend
}
// Create a handler with the core MCP procedures registered
handler := jsonrpc.new_handler(jsonrpc.Handler{
procedures: {
//...params.handlers,
// Core handlers
'initialize': server.initialize_handler
'notifications/initialized': initialized_notification_handler
// Resource handlers
'resources/list': server.resources_list_handler
'resources/read': server.resources_read_handler
'resources/templates/list': server.resources_templates_list_handler
'resources/subscribe': server.resources_subscribe_handler
// Prompt handlers
'prompts/list': server.prompts_list_handler
'prompts/get': server.prompts_get_handler
'completion/complete': server.prompts_get_handler
// Tool handlers
'tools/list': server.tools_list_handler
'tools/call': server.tools_call_handler
// Sampling handlers
'sampling/createMessage': server.sampling_create_message_handler
}
})!
server.handler = *handler
return server
}

52
lib/ai/mcp/generics.v Normal file
View File

@@ -0,0 +1,52 @@
module mcp
pub fn result_to_mcp_tool_contents[T](result T) []ToolContent {
return [result_to_mcp_tool_content[T](result)]
}
pub fn result_to_mcp_tool_content[T](result T) ToolContent {
$if T is string {
return ToolContent{
typ: 'text'
text: result.str()
}
} $else $if T is int {
return ToolContent{
typ: 'number'
number: result.int()
}
} $else $if T is bool {
return ToolContent{
typ: 'boolean'
boolean: result.bool()
}
} $else $if result is $array {
mut items := []ToolContent{}
for item in result {
items << result_to_mcp_tool_content(item)
}
return ToolContent{
typ: 'array'
items: items
}
} $else $if T is $struct {
mut properties := map[string]ToolContent{}
$for field in T.fields {
properties[field.name] = result_to_mcp_tool_content(result.$(field.name))
}
return ToolContent{
typ: 'object'
properties: properties
}
} $else {
panic('Unsupported type: ${typeof(result)}')
}
}
pub fn array_to_mcp_tool_contents[U](array []U) []ToolContent {
mut contents := []ToolContent{}
for item in array {
contents << result_to_mcp_tool_content(item)
}
return contents
}

View File

@@ -0,0 +1,27 @@
module mcp
import time
import os
import log
import x.json2
import freeflowuniverse.herolib.schemas.jsonrpc
// initialize_handler handles the initialize request according to the MCP specification
fn (mut s Server) initialize_handler(data string) !string {
// Decode the request with ClientConfiguration parameters
request := jsonrpc.decode_request_generic[ClientConfiguration](data)!
s.client_config = request.params
// Create a success response with the result
response := jsonrpc.new_response_generic[ServerConfiguration](request.id, s.ServerConfiguration)
return response.encode()
}
// initialized_notification_handler handles the initialized notification
// This notification is sent by the client after successful initialization
fn initialized_notification_handler(data string) !string {
// This is a notification, so no response is expected
// Just log that we received the notification
log.info('Received initialized notification')
return ''
}

View File

@@ -0,0 +1,103 @@
module mcp
import freeflowuniverse.herolib.schemas.jsonrpc
import json
// This file contains tests for the MCP initialize handler implementation.
// It tests the handler's ability to process initialize requests according to the MCP specification.
// test_initialize_handler tests the initialize handler with a sample initialize request
fn test_initialize_handler() {
mut server := Server{}
// Sample initialize request from the MCP specification
initialize_request := '{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{"sampling":{},"roots":{"listChanged":true}},"clientInfo":{"name":"mcp-inspector","version":"0.0.1"}}}'
// Call the initialize handler directly
response := server.initialize_handler(initialize_request) or {
assert false, 'Initialize handler failed: ${err}'
return
}
// Decode the response to verify its structure
decoded_response := jsonrpc.decode_response(response) or {
assert false, 'Failed to decode response: ${err}'
return
}
// Verify that the response is not an error
assert !decoded_response.is_error(), 'Response should not be an error'
// Parse the result to verify its contents
result_json := decoded_response.result() or {
assert false, 'Failed to get result: ${err}'
return
}
// Decode the result into an ServerConfiguration struct
result := json.decode(ServerConfiguration, result_json) or {
assert false, 'Failed to decode result: ${err}'
return
}
// Verify the protocol version matches what was requested
assert result.protocol_version == '2024-11-05', 'Protocol version should match the request'
// Verify server capabilities
assert result.capabilities.prompts.list_changed == true, 'Prompts capability should have list_changed set to true'
assert result.capabilities.resources.subscribe == true, 'Resources capability should have subscribe set to true'
assert result.capabilities.resources.list_changed == true, 'Resources capability should have list_changed set to true'
assert result.capabilities.tools.list_changed == true, 'Tools capability should have list_changed set to true'
// Verify server info
assert result.server_info.name == 'HeroLibMCPServer', 'Server name should be HeroLibMCPServer'
assert result.server_info.version == '1.0.0', 'Server version should be 1.0.0'
}
// test_initialize_handler_with_handler tests the initialize handler through the JSONRPC handler
fn test_initialize_handler_with_handler() {
mut server := Server{}
// Create a handler with just the initialize procedure
handler := jsonrpc.new_handler(jsonrpc.Handler{
procedures: {
'initialize': server.initialize_handler
}
}) or {
assert false, 'Failed to create handler: ${err}'
return
}
// Sample initialize request from the MCP specification
initialize_request := '{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{"sampling":{},"roots":{"listChanged":true}},"clientInfo":{"name":"mcp-inspector","version":"0.0.1"}}}'
// Process the request through the handler
response := handler.handle(initialize_request) or {
assert false, 'Handler failed to process request: ${err}'
return
}
// Decode the response to verify its structure
decoded_response := jsonrpc.decode_response(response) or {
assert false, 'Failed to decode response: ${err}'
return
}
// Verify that the response is not an error
assert !decoded_response.is_error(), 'Response should not be an error'
// Parse the result to verify its contents
result_json := decoded_response.result() or {
assert false, 'Failed to get result: ${err}'
return
}
// Decode the result into an ServerConfiguration struct
result := json.decode(ServerConfiguration, result_json) or {
assert false, 'Failed to decode result: ${err}'
return
}
// Verify the protocol version matches what was requested
assert result.protocol_version == '2024-11-05', 'Protocol version should match the request'
}

View File

@@ -0,0 +1,135 @@
module mcp
import time
import os
import log
import x.json2
import json
import freeflowuniverse.herolib.schemas.jsonrpc
// Prompt related structs
pub struct Prompt {
pub:
name string
description string
arguments []PromptArgument
}
pub struct PromptArgument {
pub:
name string
description string
required bool
}
pub struct PromptMessage {
pub:
role string
content PromptContent
}
pub struct PromptContent {
pub:
typ string @[json: 'type']
text string
data string
mimetype string @[json: 'mimeType']
resource ResourceContent
}
// Prompt List Handler
pub struct PromptListParams {
pub:
cursor string
}
pub struct PromptListResult {
pub:
prompts []Prompt
next_cursor string @[json: 'nextCursor']
}
// prompts_list_handler handles the prompts/list request
// This request is used to retrieve a list of available prompts
fn (mut s Server) prompts_list_handler(data string) !string {
// Decode the request with cursor parameter
request := jsonrpc.decode_request_generic[PromptListParams](data)!
cursor := request.params.cursor
// TODO: Implement pagination logic using the cursor
// For now, return all prompts
// Create a success response with the result
response := jsonrpc.new_response_generic[PromptListResult](request.id, PromptListResult{
prompts: s.backend.prompt_list()!
next_cursor: '' // Empty if no more pages
})
return response.encode()
}
// Prompt Get Handler
pub struct PromptGetParams {
pub:
name string
arguments map[string]string
}
pub struct PromptGetResult {
pub:
description string
messages []PromptMessage
}
// prompts_get_handler handles the prompts/get request
// This request is used to retrieve a specific prompt with arguments
fn (mut s Server) prompts_get_handler(data string) !string {
// Decode the request with name and arguments parameters
request_map := json2.raw_decode(data)!.as_map()
params_map := request_map['params'].as_map()
if !s.backend.prompt_exists(params_map['name'].str())! {
return jsonrpc.new_error_response(request_map['id'].int(), prompt_not_found(params_map['name'].str())).encode()
}
// Get the prompt by name
prompt := s.backend.prompt_get(params_map['name'].str())!
// Validate required arguments
for arg in prompt.arguments {
if arg.required && params_map['arguments'].as_map()[arg.name].str() == '' {
return jsonrpc.new_error_response(request_map['id'].int(), missing_required_argument(arg.name)).encode()
}
}
messages := s.backend.prompt_call(params_map['name'].str(), params_map['arguments'].as_map().values().map(it.str()))!
// // Get the prompt messages with arguments applied
// messages := s.backend.prompt_messages_get(request.params.name, request.params.arguments)!
// Create a success response with the result
response := jsonrpc.new_response_generic[PromptGetResult](request_map['id'].int(),
PromptGetResult{
description: prompt.description
messages: messages
})
return response.encode()
}
// Prompt Notification Handlers
// send_prompts_list_changed_notification sends a notification when the list of prompts changes
pub fn (mut s Server) send_prompts_list_changed_notification() ! {
// Check if the client supports this notification
if !s.client_config.capabilities.roots.list_changed {
return
}
// Create a notification
notification := jsonrpc.new_blank_notification('notifications/prompts/list_changed')
s.send(json.encode(notification))
// Send the notification to all connected clients
log.info('Sending prompts list changed notification: ${json.encode(notification)}')
}

View File

@@ -0,0 +1,186 @@
module mcp
import time
import os
import log
import x.json2
import json
import freeflowuniverse.herolib.schemas.jsonrpc
pub struct Resource {
pub:
uri string
name string
description string
mimetype string @[json: 'mimeType']
}
// Resource List Handler
pub struct ResourceListParams {
pub:
cursor string
}
pub struct ResourceListResult {
pub:
resources []Resource
next_cursor string @[json: 'nextCursor']
}
// resources_list_handler handles the resources/list request
// This request is used to retrieve a list of available resources
fn (mut s Server) resources_list_handler(data string) !string {
// Decode the request with cursor parameter
request := jsonrpc.decode_request_generic[ResourceListParams](data)!
cursor := request.params.cursor
// TODO: Implement pagination logic using the cursor
// For now, return all resources
// Create a success response with the result
response := jsonrpc.new_response_generic[ResourceListResult](request.id, ResourceListResult{
resources: s.backend.resource_list()!
next_cursor: '' // Empty if no more pages
})
return response.encode()
}
// Resource Read Handler
pub struct ResourceReadParams {
pub:
uri string
}
pub struct ResourceReadResult {
pub:
contents []ResourceContent
}
pub struct ResourceContent {
pub:
uri string
mimetype string @[json: 'mimeType']
text string
blob string // Base64-encoded binary data
}
// resources_read_handler handles the resources/read request
// This request is used to retrieve the contents of a resource
fn (mut s Server) resources_read_handler(data string) !string {
// Decode the request with uri parameter
request := jsonrpc.decode_request_generic[ResourceReadParams](data)!
if !s.backend.resource_exists(request.params.uri)! {
return jsonrpc.new_error_response(request.id, resource_not_found(request.params.uri)).encode()
}
// Get the resource contents by URI
resource_contents := s.backend.resource_contents_get(request.params.uri)!
// Create a success response with the result
response := jsonrpc.new_response_generic[ResourceReadResult](request.id, ResourceReadResult{
contents: resource_contents
})
return response.encode()
}
// Resource Templates Handler
pub struct ResourceTemplatesListResult {
pub:
resource_templates []ResourceTemplate @[json: 'resourceTemplates']
}
pub struct ResourceTemplate {
pub:
uri_template string @[json: 'uriTemplate']
name string
description string
mimetype string @[json: 'mimeType']
}
// resources_templates_list_handler handles the resources/templates/list request
// This request is used to retrieve a list of available resource templates
fn (mut s Server) resources_templates_list_handler(data string) !string {
// Decode the request
request := jsonrpc.decode_request(data)!
// Create a success response with the result
response := jsonrpc.new_response_generic[ResourceTemplatesListResult](request.id,
ResourceTemplatesListResult{
resource_templates: s.backend.resource_templates_list()!
})
return response.encode()
}
// Resource Subscription Handler
pub struct ResourceSubscribeParams {
pub:
uri string
}
pub struct ResourceSubscribeResult {
pub:
subscribed bool
}
// resources_subscribe_handler handles the resources/subscribe request
// This request is used to subscribe to changes for a specific resource
fn (mut s Server) resources_subscribe_handler(data string) !string {
request := jsonrpc.decode_request_generic[ResourceSubscribeParams](data)!
if !s.backend.resource_exists(request.params.uri)! {
return jsonrpc.new_error_response(request.id, resource_not_found(request.params.uri)).encode()
}
s.backend.resource_subscribe(request.params.uri)!
response := jsonrpc.new_response_generic[ResourceSubscribeResult](request.id, ResourceSubscribeResult{
subscribed: true
})
return response.encode()
}
// Resource Notification Handlers
// send_resources_list_changed_notification sends a notification when the list of resources changes
pub fn (mut s Server) send_resources_list_changed_notification() ! {
// Check if the client supports this notification
if !s.client_config.capabilities.roots.list_changed {
return
}
// Create a notification
notification := jsonrpc.new_blank_notification('notifications/resources/list_changed')
s.send(json.encode(notification))
// Send the notification to all connected clients
// In a real implementation, this would use a WebSocket or other transport
log.info('Sending resources list changed notification: ${json.encode(notification)}')
}
pub struct ResourceUpdatedParams {
pub:
uri string
}
// send_resource_updated_notification sends a notification when a subscribed resource is updated
pub fn (mut s Server) send_resource_updated_notification(uri string) ! {
// Check if the client is subscribed to this resource
if !s.backend.resource_subscribed(uri)! {
return
}
// Create a notification
notification := jsonrpc.new_notification[ResourceUpdatedParams]('notifications/resources/updated',
ResourceUpdatedParams{
uri: uri
})
s.send(json.encode(notification))
// Send the notification to all connected clients
// In a real implementation, this would use a WebSocket or other transport
log.info('Sending resource updated notification: ${json.encode(notification)}')
}

View File

@@ -0,0 +1,145 @@
module mcp
import time
import os
import log
import x.json2
import json
import freeflowuniverse.herolib.schemas.jsonrpc
// Sampling related structs
pub struct MessageContent {
pub:
typ string @[json: 'type']
text string
data string
mimetype string @[json: 'mimeType']
}
pub struct Message {
pub:
role string
content MessageContent
}
pub struct ModelHint {
pub:
name string
}
pub struct ModelPreferences {
pub:
hints []ModelHint
cost_priority f32 @[json: 'costPriority']
speed_priority f32 @[json: 'speedPriority']
intelligence_priority f32 @[json: 'intelligencePriority']
}
pub struct SamplingCreateMessageParams {
pub:
messages []Message
model_preferences ModelPreferences @[json: 'modelPreferences']
system_prompt string @[json: 'systemPrompt']
include_context string @[json: 'includeContext']
temperature f32
max_tokens int @[json: 'maxTokens']
stop_sequences []string @[json: 'stopSequences']
metadata map[string]json2.Any
}
pub struct SamplingCreateMessageResult {
pub:
model string
stop_reason string @[json: 'stopReason']
role string
content MessageContent
}
// sampling_create_message_handler handles the sampling/createMessage request
// This request is used to request LLM completions through the client
fn (mut s Server) sampling_create_message_handler(data string) !string {
// Decode the request
request_map := json2.raw_decode(data)!.as_map()
id := request_map['id'].int()
params_map := request_map['params'].as_map()
// Validate required parameters
if 'messages' !in params_map {
return jsonrpc.new_error_response(id, missing_required_argument('messages')).encode()
}
if 'maxTokens' !in params_map {
return jsonrpc.new_error_response(id, missing_required_argument('maxTokens')).encode()
}
// Call the backend to handle the sampling request
result := s.backend.sampling_create_message(params_map) or {
return jsonrpc.new_error_response(id, sampling_error(err.msg())).encode()
}
// Create a success response with the result
response := jsonrpc.new_response(id, json.encode(result))
return response.encode()
}
// Helper function to convert JSON messages to our Message struct format
fn parse_messages(messages_json json2.Any) ![]Message {
messages_arr := messages_json.arr()
mut result := []Message{cap: messages_arr.len}
for msg_json in messages_arr {
msg_map := msg_json.as_map()
if 'role' !in msg_map {
return error('Missing role in message')
}
if 'content' !in msg_map {
return error('Missing content in message')
}
role := msg_map['role'].str()
content_map := msg_map['content'].as_map()
if 'type' !in content_map {
return error('Missing type in message content')
}
typ := content_map['type'].str()
mut text := ''
mut data := ''
mut mimetype := ''
if typ == 'text' {
if 'text' !in content_map {
return error('Missing text in text content')
}
text = content_map['text'].str()
} else if typ == 'image' {
if 'data' !in content_map {
return error('Missing data in image content')
}
data = content_map['data'].str()
if 'mimeType' !in content_map {
return error('Missing mimeType in image content')
}
mimetype = content_map['mimeType'].str()
} else {
return error('Unsupported content type: ${typ}')
}
result << Message{
role: role
content: MessageContent{
typ: typ
text: text
data: data
mimetype: mimetype
}
}
}
return result
}

151
lib/ai/mcp/handler_tools.v Normal file
View File

@@ -0,0 +1,151 @@
module mcp
import time
import os
import log
import x.json2
import json
import freeflowuniverse.herolib.schemas.jsonrpc
import freeflowuniverse.herolib.schemas.jsonschema
// Tool related structs
pub struct Tool {
pub:
name string
description string
input_schema jsonschema.Schema @[json: 'inputSchema']
}
pub struct ToolProperty {
pub:
typ string @[json: 'type']
items ToolItems
enum []string
}
pub struct ToolItems {
pub:
typ string @[json: 'type']
enum []string
properties map[string]ToolProperty
}
pub struct ToolContent {
pub:
typ string @[json: 'type']
text string
number int
boolean bool
properties map[string]ToolContent
items []ToolContent
}
// Tool List Handler
pub struct ToolListParams {
pub:
cursor string
}
pub struct ToolListResult {
pub:
tools []Tool
next_cursor string @[json: 'nextCursor']
}
// tools_list_handler handles the tools/list request
// This request is used to retrieve a list of available tools
fn (mut s Server) tools_list_handler(data string) !string {
// Decode the request with cursor parameter
request := jsonrpc.decode_request_generic[ToolListParams](data)!
cursor := request.params.cursor
// TODO: Implement pagination logic using the cursor
// For now, return all tools
encoded := json.encode(ToolListResult{
tools: s.backend.tool_list()!
next_cursor: '' // Empty if no more pages
})
// Create a success response with the result
response := jsonrpc.new_response(request.id, json.encode(ToolListResult{
tools: s.backend.tool_list()!
next_cursor: '' // Empty if no more pages
}))
return response.encode()
}
// Tool Call Handler
pub struct ToolCallParams {
pub:
name string
arguments map[string]json2.Any
meta map[string]json2.Any @[json: '_meta']
}
pub struct ToolCallResult {
pub:
is_error bool @[json: 'isError']
content []ToolContent
}
// tools_call_handler handles the tools/call request
// This request is used to call a specific tool with arguments
fn (mut s Server) tools_call_handler(data string) !string {
// Decode the request with name and arguments parameters
request_map := json2.raw_decode(data)!.as_map()
params_map := request_map['params'].as_map()
tool_name := params_map['name'].str()
if !s.backend.tool_exists(tool_name)! {
return jsonrpc.new_error_response(request_map['id'].int(), tool_not_found(tool_name)).encode()
}
arguments := params_map['arguments'].as_map()
// Get the tool by name
tool := s.backend.tool_get(tool_name)!
// Validate arguments against the input schema
// TODO: Implement proper JSON Schema validation
for req in tool.input_schema.required {
if req !in arguments {
return jsonrpc.new_error_response(request_map['id'].int(), missing_required_argument(req)).encode()
}
}
log.error('Calling tool: ${tool_name} with arguments: ${arguments}')
// Call the tool with the provided arguments
result := s.backend.tool_call(tool_name, arguments)!
log.error('Received result from tool: ${tool_name} with result: ${result}')
// Create a success response with the result
response := jsonrpc.new_response_generic[ToolCallResult](request_map['id'].int(),
result)
return response.encode()
}
// Tool Notification Handlers
// send_tools_list_changed_notification sends a notification when the list of tools changes
pub fn (mut s Server) send_tools_list_changed_notification() ! {
// Check if the client supports this notification
if !s.client_config.capabilities.roots.list_changed {
return
}
// Create a notification
notification := jsonrpc.new_blank_notification('notifications/tools/list_changed')
s.send(json.encode(notification))
// Send the notification to all connected clients
log.info('Sending tools list changed notification: ${json.encode(notification)}')
}
pub fn error_tool_call_result(err IError) ToolCallResult {
return ToolCallResult{
is_error: true
content: [ToolContent{
typ: 'text'
text: err.msg()
}]
}
}

View File

@@ -0,0 +1,92 @@
# MCP Generator
An implementation of the [Model Context Protocol (MCP)](https://modelcontextprotocol.io/) server for V language operations. This server uses the Standard Input/Output (stdio) transport as described in the [MCP documentation](https://modelcontextprotocol.io/docs/concepts/transports).
## Features
The server supports the following operations:
1. **test** - Run V tests on a file or directory
2. **run** - Execute V code from a file or directory
3. **compile** - Compile V code from a file or directory
4. **vet** - Run V vet on a file or directory
## Usage
### Building the Server
```bash
v -gc none -stats -enable-globals -n -w -cg -g -cc tcc /Users/despiegk/code/github/freeflowuniverse/herolib/lib/mcp/v_do
```
### Using the Server
The server communicates using the MCP protocol over stdio. To send a request, use the following format:
```
Content-Length: <length>
{"jsonrpc":"2.0","id":"<request-id>","method":"<method-name>","params":{"fullpath":"<path-to-file-or-directory>"}}
```
Where:
- `<length>` is the length of the JSON message in bytes
- `<request-id>` is a unique identifier for the request
- `<method-name>` is one of: `test`, `run`, `compile`, or `vet`
- `<path-to-file-or-directory>` is the absolute path to the V file or directory to process
### Example
Request:
```
Content-Length: 85
{"jsonrpc":"2.0","id":"1","method":"test","params":{"fullpath":"/path/to/file.v"}}
```
Response:
```
Content-Length: 245
{"jsonrpc":"2.0","id":"1","result":{"output":"Command: v -gc none -stats -enable-globals -show-c-output -keepc -n -w -cg -o /tmp/tester.c -g -cc tcc test /path/to/file.v\nExit code: 0\nOutput:\nAll tests passed!"}}
```
## Methods
### test
Runs V tests on the specified file or directory.
Command used:
```
v -gc none -stats -enable-globals -show-c-output -keepc -n -w -cg -o /tmp/tester.c -g -cc tcc test ${fullpath}
```
If a directory is specified, it will run tests on all `.v` files in the directory (non-recursive).
### run
Executes the specified V file or all V files in a directory.
Command used:
```
v -gc none -stats -enable-globals -n -w -cg -g -cc tcc run ${fullpath}
```
### compile
Compiles the specified V file or all V files in a directory.
Command used:
```
cd /tmp && v -gc none -enable-globals -show-c-output -keepc -n -w -cg -o /tmp/tester.c -g -cc tcc ${fullpath}
```
### vet
Runs V vet on the specified file or directory.
Command used:
```
v vet -v -w ${fullpath}
```

Some files were not shown because too many files have changed in this diff Show More