Files
wangdefa f782b612ec build: 内置 norma 模块,构建不再依赖上游仓库
把 github.com/Autumn-27/norma v0.4.3(commit 320ae32)的源码复制到
third_party/norma,并用 go.mod 的 replace 指向本地副本。上游删除仓库或
改写 tag 都不再影响本仓库构建。

- 已验证 GOPROXY=off 下可完整编译并打出发行包
- go.sum 中该模块的两条校验记录随之删除(本地 replace 不需要哈希)
- 升级方法与授权注意事项见 third_party/README.md
2026-10-20 21:10:41 -07:00

73 lines
2.6 KiB
Go

package tool
import (
"context"
"encoding/json"
"fmt"
"time"
)
// NewTaskOutput builds the TaskOutput tool: read the captured output of a
// background task (from Bash run_in_background or Monitor), optionally blocking
// until it finishes.
func NewTaskOutput() CoreTool {
return Build(Spec{
Name: "TaskOutput",
Description: "Reads the output of a background task started by Bash (run_in_background) or Monitor. You can also Read the task's output-file path directly. Set block=true to wait for the task to finish first.",
Prompt: "Provide the task_id. With block=true the call waits until the task reaches a terminal state (up to timeout_ms) before returning its output.",
Schema: map[string]any{
"type": "object",
"properties": map[string]any{
"task_id": map[string]any{"type": "string", "description": "The task id (e.g. task_1)."},
"max_bytes": map[string]any{"type": "integer", "description": "Max bytes of output to return from the tail (default 30000)."},
"block": map[string]any{"type": "boolean", "description": "Wait until the task finishes before returning (default false)."},
"timeout_ms": map[string]any{"type": "integer", "description": "Max wait when block=true (default 30000)."},
},
"required": []any{"task_id"},
},
ReadOnly: always,
Concurrent: always,
Permissions: allowReadOnly,
Run: runTaskOutput,
})
}
func runTaskOutput(ctx context.Context, input json.RawMessage, tc *ToolContext) (Result, error) {
var in struct {
TaskID string `json:"task_id"`
MaxBytes int `json:"max_bytes"`
Block bool `json:"block"`
TimeoutMS int `json:"timeout_ms"`
}
if err := json.Unmarshal(input, &in); err != nil {
return Result{}, err
}
if tc == nil || tc.Tasks == nil {
return Errorf("Error: background tasks are not enabled"), nil
}
if in.TaskID == "" {
return Errorf("Error: task_id is required"), nil
}
info, ok := tc.Tasks.Get(in.TaskID)
if !ok {
return Errorf(fmt.Sprintf("Error: unknown task %q", in.TaskID)), nil
}
if in.Block && !info.Status.Terminal() {
timeout := 30 * time.Second
if in.TimeoutMS > 0 {
timeout = time.Duration(in.TimeoutMS) * time.Millisecond
}
info, _ = tc.Tasks.Wait(ctx, in.TaskID, timeout)
}
out, err := tc.Tasks.Output(in.TaskID, in.MaxBytes)
if err != nil {
return Errorf("Error: " + err.Error()), nil
}
header := fmt.Sprintf("[task %s | %s", info.ID, info.Status)
if info.Status == TaskCompleted || info.Status == TaskFailed {
header += fmt.Sprintf(" | exit %d", info.ExitCode)
}
header += "]\n"
return Text(Capture(tc, header+out)), nil
}