Skip to content

回调函数

Agent中使用钩子函数

在钩子函数中可以使用以下的函数或是对象来实现回调函数:

在回调函数中,有以下的全局对象:

  • assistant: 助手对象
  • context: 上下文对象
  • Plan: 计划对象。
  • Send: 给前端Stream发送消息的函数
  • Call: 调用其它脚本的函数

对象说明

ts

/**
 * 创建并返回一个计划对象
 * @param planId 计划ID
 * @returns 计划对象
 */
export declare function Plan(planId:string): Plan;


type Method:string;//同一个脚本中的方法名称
type Process:string;//yao处理器
type Callback {//yao处理器回调,同时配置回调函数名和回调函数参数
    name:string;
    args:any[];
}
/**
 * 回调助手,并传入回调函数
 * @param assistantId 助手ID
 * @param input 输入,常用的是用户的提问。
 * @param callback 回调函数,接收4种类型的参数
 * 1. Function: 普通函数
 * 2. Callback: 对象,对象的name属性为回调函数名,args属性为回调函数参数
 * 3. Method: 方法,当前脚本中的方法名称
 * 4. Process: 处理器,调用其它处理器,处理器名称需要包含"."
 * @options 选项,调用聊天模型时的选项,比如temperature,top_p等参数
 */
export declare function Call(assistantId:string,input:string,callback:null|Function|Callback|Method|Process,options:{[key:string]:any}): void;

Message类型定义

ts
/**
 * Message 消息对象
 */
interface Message {
  /** 消息ID */
  id?: string;
  /** 文本内容 */
  text?: string;
  /** 消息类型: error, text, plan, table, form, page, file, video, audio, image, markdown, json等 */
  type?: string;
  /** 类型相关的属性 */
  props?: Record<string, any>;
  /** 标记消息是否完成 */
  done?: boolean;
  /** 标记是否为新消息 */
  new?: boolean;
  /** 标记是否为增量消息 */
  delta?: boolean;
  /** 会话动作列表 */
  actions?: Action[];
  /** 文件附件列表 */
  attachments?: Attachment[];
  /** 消息角色: user, assistant, system等 */
  role?: string;
  /** 消息名称 */
  name?: string;
  /** 助手ID (当role为assistant时) */
  assistant_id?: string;
  /** 助手名称 (当role为assistant时) */
  assistant_name?: string;
  /** 助手头像 (当role为assistant时) */
  assistant_avatar?: string;
  /** 消息提及列表 (当role为user时) */
  mentions?: Mention[];
  /** 消息数据 */
  data?: Record<string, any>;
  /** 消息是否待处理 */
  pending?: boolean;
  /** 消息是否隐藏 (不在UI和历史记录中显示) */
  hidden?: boolean;
  /** 消息是否需要重试 */
  retry?: boolean;
  /** 消息是否静默 (不在UI和历史记录中显示) */
  silent?: boolean;
  /** 工具调用ID */
  tool_call_id?: string;
  /** 函数调用列表 */
  tool_calls?: FunctionCall[];
}

/**
 * 提及对象
 */
interface Mention {
  /** 助手ID */
  assistant_id: string;
  /** 名称 */
  name: string;
  /** 头像 */
  avatar?: string;
}

/**
 * 附件对象
 */
interface Attachment {
  /** 文件名 */
  name?: string;
  /** 文件URL */
  url?: string;
  /** 文件描述 */
  description?: string;
  /** 文件类型 */
  type?: string;
  /** 内容类型 */
  content_type?: string;
  /** 文件大小(字节) */
  bytes?: number;
  /** 创建时间戳 */
  created_at?: number;
  /** 文件ID */
  file_id?: string;
  /** 会话ID */
  chat_id?: string;
  /** 助手ID */
  assistant_id?: string;
}

/**
 * 动作对象
 */
interface Action {
  /** 动作名称 */
  name?: string;
  /** 动作类型 */
  type: string;
  /** 动作载荷 */
  payload?: any;
}

回调函数示例:

ts

/**
 * 示例回调函数
 * @param msg 消息对象
 * @param args 其他参数,如果使用处理器定义,则args为处理器定义的参数
 */
function exampleCallback(msg: Message,..args:any[]) {
    console.log('收到消息:', msg.Text);
}

/**
 * 使用回调函数的示例
 */
function useCallbackExample() {
    // 使用普通函数作为回调
    Call('neo', '你好', exampleCallback);

    // 使用对象形式的回调
    Call('neo', '你好', {
        name: 'exampleCallback',
        args: [] // 如果回调函数不需要参数,可以留空数组
    });

    // 使用当前脚本中的方法作为回调
    Call('neo', '你好', 'exampleCallback');

    // 使用处理器作为回调
    Call('neo', '你好', '处理器名称.方法名称');
}

测试

js
function callback(msg:) {}

function test() {
  Call('neo', '你好', 'callback');
  Call('neo', '你好', function () {
    console.log('回调函数');
  });
}