mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-09-26 12:23:23 +00:00
feat: Lua 插件 stage 写回支持(与 C ABI v2 对齐)
makeStageHandler 现在把 ctx 以 Lua table 引用传给 handler, handler 修改的字段(raw_message/llm_text/final_text/response/tool_calls/ tool_results/user_id/group_id/no_memory)同步写回内核 StageContext。 新增 TestLuaStageWriteback 验证 on_input/post_action 的写回。
This commit is contained in:
@ -741,15 +741,67 @@ func makeStageHandler(plg *luaPlugin, stage sdk.Stage, fn *lua.LFunction) sdk.St
|
||||
if len(sc.ToolResults) > 0 {
|
||||
ctx["tool_results"] = jsonToIface(sc.ToolResults)
|
||||
}
|
||||
ctxTbl := goValueToLua(L, ctx).(*lua.LTable)
|
||||
L.Push(fn)
|
||||
L.Push(goValueToLua(L, ctx))
|
||||
L.Push(ctxTbl)
|
||||
if err := L.PCall(1, 0, nil); err != nil {
|
||||
return fmt.Errorf("lua stage %s: %w", stage, err)
|
||||
}
|
||||
// 写回:Lua handler 对 ctx table 的字段修改同步回内核 StageContext
|
||||
applyLuaStageResult(sc, luaValueToGo(ctxTbl))
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// applyLuaStageResult 将 Lua stage handler 修改后的 ctx 字段写回内核 StageContext。
|
||||
// Lua 侧修改的字段以 Lua table(引用)形式读回,仅回写插件有权改写的键。
|
||||
func applyLuaStageResult(sc *sdk.StageContext, modified interface{}) {
|
||||
m, ok := modified.(map[string]interface{})
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
sc.Lock()
|
||||
defer sc.Unlock()
|
||||
if v, ok := m["raw_message"].(string); ok {
|
||||
sc.RawMessage = v
|
||||
}
|
||||
if v, ok := m["llm_text"].(string); ok {
|
||||
sc.LLMText = v
|
||||
}
|
||||
if v, ok := m["final_text"].(string); ok {
|
||||
sc.FinalText = v
|
||||
}
|
||||
if v, ok := m["user_id"].(string); ok {
|
||||
sc.UserID = v
|
||||
}
|
||||
if v, ok := m["group_id"].(string); ok {
|
||||
sc.GroupID = v
|
||||
}
|
||||
if v, ok := m["no_memory"].(bool); ok {
|
||||
sc.NoMemory = v
|
||||
}
|
||||
if v, ok := m["response"].(string); ok {
|
||||
vv := v
|
||||
sc.Response = &vv
|
||||
}
|
||||
if v, ok := m["tool_calls"].([]interface{}); ok && len(v) > 0 {
|
||||
if b, err := json.Marshal(v); err == nil {
|
||||
var tcs []sdk.ToolCall
|
||||
if json.Unmarshal(b, &tcs) == nil {
|
||||
sc.ToolCalls = tcs
|
||||
}
|
||||
}
|
||||
}
|
||||
if v, ok := m["tool_results"].([]interface{}); ok && len(v) > 0 {
|
||||
if b, err := json.Marshal(v); err == nil {
|
||||
var trs []sdk.ToolResult
|
||||
if json.Unmarshal(b, &trs) == nil {
|
||||
sc.ToolResults = trs
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func parseStageScope(L *lua.LState) sdk.StageScope {
|
||||
if L.GetTop() >= 3 && L.ToString(3) == "own_tools" {
|
||||
return sdk.StageScopeOwnTools
|
||||
|
||||
Reference in New Issue
Block a user