Skip to content

About

A super simple agent with necessary tools/prompts and settings to code any language

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

JiaBroAgent 是可以跨端进行大模型编程/编译/测试用的编码智能体,提供websocket cli等多种对接方式

全景图

TODO 待办

  • mode2 实现
  • agent workflow 细化和Node节点设计
  • 针对新工作流重新设计Dag
  • skill模块的设计实现
  • windows sandbox / linux seatbelt
  • 数据库sqlite引入, 对接memory等功能
  • rag和矢量库的实现
  • message模块自进化功能实现

Agent Websocket 初始化参数

#[derive(clap::Parser, Deserialize)]
pub struct WorkArgs {
    #[arg(short, long, help = "会话ID, 也就是SessionId, 可选, 在ws模式下较重要,如不传会自动生成会话ID")]
    pub id: Option<String>,
    #[arg(short, long, help = "工作区地址, 必填")]
    pub workspace: String,
    #[arg(short, long, help = "偏好开发语言, 可选")]
    pub lang: Option<String>,
    #[arg(short, long, help = "记忆池文件, 可选")]
    pub memory: Option<String>,

    #[arg(short, long, help = "特殊大模型调用时, JWT功能时的token, 可选")]
    pub token: Option<String>,
    #[arg(
        long,
        help = "特殊大模型调用时, 自定义请求头,必须以格式 k:v,k:v,k:v...组成, kv不能包含[,][:], 可选"
    )]
    pub custom_header: Option<String>,

    #[arg(short, long, help = "特殊大模型调用时, 添加的功能性配置, 可选")]
    pub client_meta: Option<String>,
    #[arg(long, help = "MCP配置文件地址, 可选")]
    pub mcp_config: Option<String>,
}

Agent Websocket 消息接口

  • start_task 新建会话任务,id是请求id,也就是rid,rid是用于异步请求下区分其他请求的设计, 如果不传agent会自行代入ws会话id也就是sessionId,tid一般放在content内部;
upstream:
{
    "id": <Uuid, Optional>
    "type": <String>, ("start_task")

    "content":{
        "tid": <Uuid, Optional>,
        "request": <String>,
        "text_files": <List<UserFilePart>, Optional>,
    }
}
  • UserFilePart结构体示意如下
pub struct UserFilePart {
    // path表示上传文件的地址,禁止上传agent工作区中的文件,建议使用用户工作区中的文件,如果不是则为上传行为,上传会导致有新文件进入到agent工作区,调用者需要按uploaded事件获取并比较文件
    pub path: String,
    // 镜像区真实文件地址,上传者可忽略
    pub mirror_path: Option<String>,
    // 文件一部分内容,如果该字段不为空,则不是一个upload过程
    pub content: Option<String>,
}
  • Uploaded事件,用于用户拖拽一个非工作区外的文件时在工作区中被记忆的过程
downstream:
{
    "id": <Uuid, Optional>
    "type": <String, Optional>, ("uploaded")
    
    "content": UserFiles
}
  • UserFiles结构体示意如下
pub struct UserFiles {
    // 所有映射后的文件, UserFilePart结构见上面介绍
    pub files: Vec<UserFilePart>,
    // 用户工作区的commit_id
    pub commit: Option<String>,
    // commit消息,包括所有文件的上传介绍
    pub messages: Option<String>,
}
  • 同意/不同意执行某工具,该指令为兼容性指令
// 如果反馈feedback有值,则视为补充内容,agent会自动将该内容插入当前执行的工具调用链之后,作为新消息供大模型处理
upstream:
{
    "id": <Uuid, Optional>
    "type": <String, Optional>, ("toggle_tool_approval")
    
    "content":{
        "tools": List<String>,
        "approved": <bool>, 
        "remember": <bool>, 
        "feedbck": <String, Optional>, 
    }
}
  • 启用/禁止执行某工具,该指令为兼容性指令
upstream:
{
    "id": <Uuid, Optional>
    "type": <String, Optional>, ("toggle_tool_enable")
    
    "content":{
        "tools": List<String>,
        "enabled": <bool>, 
    }
}
  • end_task,结束会话,结束会话必须传入 session_id, 结束会话只会在当前流式请求结束之后中断下一个链式操作
upstream:
{
    "id": <Uuid, Optional>
    "type": <String>, ("end_task")

    "content":{
        "session_id": <Uuid>,
        "reason": <String, Optional>,
    }
}
  • followup,补充用户请求,followup=end_task+start_task;对于followup有插言和接续2种可能,如果当前工具进行链式操作时监听到了followup即为插言,插言会在当前执行的工具得出结果后插入新的用户提示词,然后交给大模型继续处理,因为上一部没有执行完所以也就没有上一次的记忆;接续分同步和异步,因为记忆是异步的,同步接续可继承上一次的对话内容,异步接续因为记忆库没有更新结束所以不会有上一次的对话内容
upstream:
{
    "id": <Uuid, Optional>
    "type": <String>, ("followup")
    "content":{
        "followup": <String>,
    }
}
  • reconfig 更改openai配置或者工作目录,可以选择是否重值会话,默认否,如果不传值则不更改
upstream:
{
    "id": <Uuid, Optional>
    "type": <String>, ("reconfig")

    "content":{
        "workspace": <String, Optional>,
        "lang": <String, Optional>,
        "model": <String, Optional>,
        "memory_file": <String, Optional>,
        "reset": <bool>,
    }
}
  • git_merge 工作区分用户区和agent区,当工作区尝试和agent区合并时调用,将用户改动反馈到agent区的长期记忆库中,保证长期记忆库连续有效
upstream:
{
    "id": <Uuid, Optional>
    "type": <String>, ("git_merge")

    "content":{
        "tid": String,
        "commit": String,
        "comments": <String, Optional>
    }
}
  • toggle_tool_approval 是否同意某些工具的执行,可以选择是否记忆避免下次再次确认
upstream:
{
    "id": <Uuid, Optional>
    "type": <String>, ("toggle_tool_approval")

    "content":{
        "tools": List<String>,
        "approved": bool,
        "remember": bool,
        "feedback": <String, Optional>,
    }
}
  • toggle_tool_enable 是否启用某些工具的执行
upstream:
{
    "id": <Uuid, Optional>
    "type": <String>, ("toggle_tool_enable")

    "content":{
        "tools": List<String>,
        "enabled": bool,
    }
}
  • setup_predefined_prompts 自定义提示词,该指令为排他指令,每次设置一定会代替上一次的配置
upstream:
{
    "id": <Uuid, Optional>
    "type": <String>, ("setup_predefined_prompts")

    "content":{
        "prompts": List<PredefinePrompt>,
    }
}
/// 其中PredefinePrompt结构为
struct PredefinePrompt {
    pub role: String,
    pub prompt: String,
}
  • list_inner_tools 返回内置所有工具
upstream:
{
    "id": <Uuid, Optional>
    "type": <String>, ("list_inner_tools")
}

downstream:
{
    "id": <Uuid, Optional>
    "type": <String>, ("msg_list_inner_tools")

    "content": List<AgentTool>,  
}
/// 其中 AgentTool结构为
AgentTool {
    pub name: String,
    pub description: Option<String>,
    pub input_schema: json::Value,
    pub output_schema: Option<json::Value>,
}
  • mcp_list_tools 返回所有mcp工具,以哈希表分组,每个mcp server一个工具树组
upstream:
{
    "id": <Uuid, Optional>
    "type": <String>, ("mcp_list_tools")
}

downstream:
{
    "id": <Uuid, Optional>
    "type": <String>, ("msg_mcp_list_tools")

    "content": HashMap<String, List<AgentTool>>,  
}
  • mcp_list_prompts 返回所有mcp提示词,以哈希表分组,每个mcp server一个提示词树组
upstream:
{
    "id": <Uuid, Optional>
    "type": <String>, ("mcp_list_prompts")
}

downstream:
{
    "id": <Uuid, Optional>
    "type": <String>, ("msg_mcp_list_prompts")

    "content": HashMap<String, List<Prompt>>,  
}
/// 其中Prompt结构为
Prompt {
    pub name: String,
    pub title: Option<String>,
    pub description: Option<String>,
    pub arguments: Option<Vec<PromptArgument>>,
    pub icons: Option<Vec<Icon>>, 
}
/// 其中PromptArgument结构为
pub struct PromptArgument {
    pub name: String,
    pub title: Option<String>,
    pub description: Option<String>,
    pub required: Option<bool>,
}
/// Icon不常用可无视
  • mcp_get_prompts 返回所有mcp提示词,以哈希表分组,每个mcp server一个提示词树组
upstream:
{
    "id": <Uuid, Optional>
    "type": <String>, ("mcp_get_prompts")

    "content":{
        "mcp_args": <String>,
    }
}

downstream:
{
    "id": <Uuid, Optional>
    "type": <String>, ("msg_mcp_get_prompts")

    "content": <GetPromptResult>,  
}
/// 其中GetPromptResult结构为
struct GetPromptResult {
    pub description: Option<String>,
    pub messages: Vec<PromptMessage>,
}
/// 其中PromptMessage结构为
pub struct PromptMessage {
    pub role: PromptMessageRole,
    pub content: PromptMessageContent,
}
/// 其中PromptMessageRole为枚举,只能为 user / assistant
/// 其中PromptMessageContent为枚举,可以为 Text->String, Image->base64图片结构体, Resource-> 矢量数据
  • list_tool_states 返回所有工具状态,状态包括自动确认和启用状态
upstream:
{
    "id": <Uuid, Optional>
    "type": <String>, ("list_tool_states")
}

downstream:
{
    "id": <Uuid, Optional>
    "type": <String>, ("msg_list_tool_states")

    "content": <List<ToolState>>,  
}
/// 其中ToolState结构为
struct ToolState {
     name: String,
    auto_approve: Option<bool>,
    disabled: Option<bool>,
}
  • list_predefined_prompts 返回所有工具状态,状态包括自动确认和启用状态
upstream:
{
    "id": <Uuid, Optional>
    "type": <String>, ("list_predefined_prompts")
}

downstream:
{
    "id": <Uuid, Optional>
    "type": <String>, ("msg_list_predefined_prompts")

    "content": <List<PredefinePrompt>>,  
}

  • 主动推送事件
downstream:

Ready 系统就绪:
{
    "id": <Uuid, Optional>
    "type": <String>, ("ready")
    "content": <Uuid>, // session_id
}


Error 系统异常:
{
    "id": <Uuid, Optional>
    "type": <String>, ("error")
    "session_id": <unsigned int>,
    "content": {
        "reason": <String>,
        "retrying": <bool, Optional>,
    },    
}

Stop 任务结束:
{
    "id": <Uuid, Optional>
    "type": <String>, ("task_stop")
    "content": <Uuid>, // task_id
}

Content 大模型返回描述性流式内容:
{
    "id": <Uuid, Optional>
    "type": <String, Optional>, ("task_stream_content")
    "content": {
        "text": <String>
        "id": <String>,
        "tid": <Uuid>,
    },  
}

Toolcall 大模型返回工具类流式内容, eof=true表示结束
{
    "id": <Uuid, Optional>
    "type": <String, Optional>, ("task_stream_toolcall")
    "content": {
        "tid": <Uuid>,
        "id": <String>,
        "toolcall_id": <String>,
        "name": <String>,
        "args": <String>,
        "eof": <bool, Optional>,
    },  
}

task_toolcall_confirm 大模型返回工具调用请求,目前所有工具都需要确认操作,发出toolcall_confirm后该会话进入阻塞态,如果超时则继续工具链:
{
    "id": <Uuid, Optional>
    "type": <String, Optional>, ("task_toolcall_confirm")
    "content": {
        "tid": <Uuid>,
        "id": <String>,
        "toolcall_id": <String>,
        "name": <String>,
    }, 
}

stream_toolcall_output 流式工具调用结果:
{
    "id": <Uuid, Optional>
    "type": <String, Optional>, ("stream_toolcall_output")
    "content": {
        "tid": <Uuid>,
        "id": <String>,
        "toolcall_id": <String>,
        "text": <String>,
    }, 
}

toolcall_end 工具调用最后结果,如果有落盘行为,则必定触发agent工作区的commit,并返回commit_id:
{
    "id": <Uuid, Optional>
    "type": <String, Optional>, ("toolcall_end")
    "content": {
        "tid": <Uuid>,
        "id": <String>,
        "toolcall_id": <String>,
        "text": <String>,
        "commit": <String, Optional>,
    }, 
}

stream_toolcall_error 工具调用异常:
{
    "id": <Uuid, Optional>
    "type": <String, Optional>, ("stream_toolcall_error")
    "content": {
        "tid": <Uuid>,
        "id": <String>,
        "toolcall_id": <String>,
        "error": <String>,
    }, 
}

toolcall_process 工具调用系统进程记录,可用于非虚拟化下可能的漏掉的进程检查
{
    "id": <Uuid, Optional>
    "type": <String, Optional>, ("toolcall_process")
    "content": {
        "tid": <Uuid>,
        "id": <String>,
        "toolcall_id": <String>,
        "process": <String>,
    }, 
}


Usage token 消耗量总结, 一般在一轮对话结束后触发:
{
    "id": <Uuid, Optional>
    "type": <String, Optional>, ("usage")
    "content": {
        "tid": <Uuid>,
        "usage": <CompletionUsage>,
    }
}

/// 其中CompletionUsage结构数据为
pub struct CompletionUsage {
    pub prompt_tokens: u32,
    pub completion_tokens: u32,
    pub total_tokens: u32,
    pub prompt_tokens_details: Option<PromptTokensDetails>,
    pub completion_tokens_details: Option<CompletionTokensDetails>,
}
/// 其中PromptTokensDetails结构数据为
pub struct PromptTokensDetails {
    pub audio_tokens: Option<u32>,
    pub cached_tokens: Option<u32>,
}
/// 其中CompletionTokensDetails结构数据为
pub struct CompletionTokensDetails {
    pub accepted_prediction_tokens: Option<u32>,
    pub audio_tokens: Option<u32>,
    pub reasoning_tokens: Option<u32>,
    pub rejected_prediction_tokens: Option<u32>,
}

关于kotlin库的使用

  • 只有dll时需要生成def
LIBRARY mylibrary

EXPORTS
    Library_Initialize
    Library_Finalize
    say_hello
    say_hello_with_two
    mylibrary_symbols
  • def之后还有lib文件
lib /def:mylibrary.def /out:mylibrary.lib /machine:x64
  • build.rs中配置编译阶段的链接库
fn main() {
    println!("cargo:rustc-link-search=./lib");
    println!("cargo:rustc-link-lib=dylib=mylibrary");
}
  • kotlin库只需要代码里声明即可
#[link(name = "mylibrary")]
unsafe extern "C" {
    pub fn Library_Finalize();
    pub fn Library_Initialize();
    pub fn say_hello() -> *const std::os::raw::c_char;
    pub fn say_hello_with_two(id: i32, name: *mut std::ffi::c_void) -> *const std::os::raw::c_char;
}

About

A super simple agent with necessary tools/prompts and settings to code any language

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages