Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions design.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,11 @@

- AI 定时任务页通过「本项目任务 / 共享任务市场」SlidingTabs 切换,市场沿用 MCP 市场卡片与搜索,展示来源、版本、推荐调度及本项目订阅数量;允许继续添加不同参数或调度的订阅。独立任务可发布不可变共享版本。项目订阅显示来源与固定版本,在原任务弹窗填写业务参数,保留独立调度、模型与 Bot 通知。有新版的订阅入口使用淡紫强调色;版本选择、提示词与参数对比直接展开在同一个任务编辑器内,参数随目标版本更新,点击「保存并升级」生效。市场详情保持独立只读窗口。编辑保存成功自动关闭弹窗并刷新列表,失败保留输入;迟到的保存响应不得关闭新打开的编辑器。弹窗沿用 Harness 令牌与关闭交互,窄屏改为单列。
- AI 定时任务页的最后一个标签是「执行记录」(`#aiScheduleExecPanel`,`web/app/schedule-executions.js`),按当前项目列出提示词任务和 SOP 触发的每次执行,数据来自 `GET /api/schedule-job-executions/dashboard`:筛选、计数、分页和概览都在服务端完成,前端每次只取当前一页,数据量再大结果也不会因截断而失真。标签上的数字是该项目保留的记录总数,最近 7 天有失败时改为红色,没有记录时隐藏。面板由三块组成,块间距 16px:
- **执行概览卡**:标题「执行概览(最近 7 天)」,按任务筛选时标题带上任务名。4 张指标卡依次为执行次数(紫 `layers`)、成功率(绿 `check`)、失败次数(红 `close`,数值红字)、平均耗时(蓝 `priority-bolt`)。图标放在 36px 圆角方块里,底色是对应色 12%。每张卡下方写「较上周 ±N%」(成功率写 pp)并带 `arrow-up` / `arrow-down`,变好绿色、变差红色,失败次数和耗时下降算变好;上周没有数据时写「上周无数据」。「最近 7 天」指包含当前小时在内的最近 7×24 小时,「上周」指再往前 7×24 小时,按执行开始的小时归入。已结束的执行来自服务端按小时累计的统计(保留 30 天),不受每个任务只保留 200 条记录的影响;进行中的执行从记录里数。概览不画趋势图,概览只随任务筛选变化。
- **执行概览卡**:标题「执行概览(最近 7 天)」,按任务筛选时标题带上任务名。4 张指标卡依次为执行次数(紫 `layers`)、成功率(绿 `check`)、平均耗时(蓝 `priority-bolt`)、Token 总消耗(紫 `layers`)。图标放在 36px 圆角方块里,底色是对应色 12%。成功率下方显示「成功 N 次 · 失败 N 次」,仅失败大于 0 时标红,不再单独占卡。执行次数和平均耗时下方写「较上周 ±N%」并带 `arrow-up` / `arrow-down`,变好绿色、变差红色,耗时下降算变好;上周没有数据时写「上周无数据」。Token 卡下方显示已上报执行数,仅统计最近 7 天本次 AI 调用的消耗(含重试、不含下游 SOP),未上报不当作零消耗。「最近 7 天」指包含当前小时在内的最近 7×24 小时,「上周」指再往前 7×24 小时,按执行开始的小时归入。已结束的执行来自服务端按小时累计的统计(保留 30 天),不受每个任务只保留 200 条记录的影响;进行中的执行从记录里数。概览不画趋势图,概览只随任务筛选变化。
- **筛选栏**:依次为搜索框(匹配任务名、任务 ID、Trace ID、错误、AI 回复、派发需求)、时间(全部时间 / 今天 / 最近 7 天 / 最近 30 天)、任务、状态,每项左侧有 Harness 图标(`search` / `calendar` / `layers` / `filter`),最右是紫字「重置」(`refresh`,没有筛选时禁用)。改任何筛选都回到第 1 页并重新向服务端请求;搜索框在停止输入 250ms 后才请求。
- **执行记录卡**:标题「执行记录(共 N 条)」,下面是表格,列为展开箭头、状态、任务名称、触发时间、耗时(表头 `info` 提示口径)、Trace ID、操作。状态用 Status Badge(进行中蓝色呼吸点、成功绿色 `check`、失败红色 `alert`、待对齐基线琥珀色 `clock`)。任务名称格第一行是类型标签加任务名,第二行单行省略显示摘要:错误(红)、AI 回复,或 SOP 的「候选 N · 启动 N · 需求名」。触发时间格式为 `YYYY/MM/DD HH:mm:ss`。Trace ID 列是带描边的小按钮,显示末 8 位加 `copy`,点击复制排障信息,失败行用红字红描边。操作列每行固定两个位置,右对齐,「⋯」在整列对齐:左边是主操作位(最小宽 88px),AI 执行显示「打开会话」(`preview-expand`,新标签页打开),SOP 触发显示「查看运行」(打开第一个运行);没有可打开的内容时这个位置不消失,改为灰字「无会话」/「准备中」/「未派发」,悬停说明原因。失败的执行只要已经创建了会话(例如运行中途服务重启),同样可以打开会话。执行记录比会话留得久,会话已被清理(超出保留会话数或保留期限,或被手动删除)时,这个位置显示灰字「会话已清理」,不再给出打不开的链接,AI 回复仍可在详情里查看;是否已清理以列表接口返回的 `sessionGone` 为准。右边是「⋯」菜单(复制排障信息、只看该任务、编辑任务,复用任务行菜单的组件和键盘行为)。点击箭头或行主体展开详情:触发方式、开始 / 结束时间、耗时、完整 Trace ID、SOP 的候选 / 启动,以及完整错误或 AI 回复(最高 220px,超出滚动)和全部派发运行。表格底部是分页:左侧「每页 10 / 20 / 50 条」,右侧上一页、页码(首页、末页和当前页前后各一页,中间用「…」)、「共 N 页」、下一页,当前页紫底白字。翻页和改每页条数也向服务端请求;记录变少导致当前页超出末页时,改为显示末页。最底部脚注说明列表保留每个任务最近 200 次执行,概览按全部执行统计。
- **执行记录卡**:标题「执行记录(共 N 条)」,下面是表格,列为展开箭头、状态、任务名称、触发时间、耗时(表头 `info` 提示口径)、Token、Trace ID、操作。Token 显示本次总量,未上报显示「未上报」、进行中显示「待统计」,展开详情提供模型与输入、输出、缓存读写明细。状态用 Status Badge(进行中蓝色呼吸点、成功绿色 `check`、失败红色 `alert`、待对齐基线琥珀色 `clock`)。任务名称格第一行是类型标签加任务名,第二行单行省略显示摘要:错误(红)、AI 回复,或 SOP 的「候选 N · 启动 N · 需求名」。触发时间格式为 `YYYY/MM/DD HH:mm:ss`。Trace ID 列是带描边的小按钮,显示末 8 位加 `copy`,点击复制排障信息,失败行用红字红描边。操作列每行固定两个位置,右对齐,「⋯」在整列对齐:左边是主操作位(最小宽 88px),AI 执行显示「打开会话」(`preview-expand`,新标签页打开),SOP 触发显示「查看运行」(打开第一个运行);没有可打开的内容时这个位置不消失,改为灰字「无会话」/「准备中」/「未派发」,悬停说明原因。失败的执行只要已经创建了会话(例如运行中途服务重启),同样可以打开会话。执行记录比会话留得久,会话已被清理(超出保留会话数或保留期限,或被手动删除)时,这个位置显示灰字「会话已清理」,不再给出打不开的链接,AI 回复仍可在详情里查看;是否已清理以列表接口返回的 `sessionGone` 为准。右边是「⋯」菜单(复制排障信息、只看该任务、编辑任务,复用任务行菜单的组件和键盘行为)。点击箭头或行主体展开详情:触发方式、开始 / 结束时间、耗时、完整 Trace ID、SOP 的候选 / 启动,以及完整错误或 AI 回复(最高 220px,超出滚动)和全部派发运行。表格底部是分页:左侧「每页 10 / 20 / 50 条」,右侧上一页、页码(首页、末页和当前页前后各一页,中间用「…」)、「共 N 页」、下一页,当前页紫底白字。翻页和改每页条数也向服务端请求;记录变少导致当前页超出末页时,改为显示末页。最底部脚注说明列表保留每个任务最近 200 次执行,概览按全部执行统计。
- 任务列表每行的「⋯」菜单里有「执行记录」,点击后切到这个标签,按该任务筛选并清空其他筛选。AI 任务行上不放「复制 Trace」,排障信息在执行记录里复制。复制内容是一段排障信息:首行「定时任务执行排障信息」,第二行 `Trace ID: <32 位 ID>`(与服务端调度日志里的 `trace=` 字段一致),之后是任务、类型、项目、触发方式、状态、开始时间、耗时、错误或 AI 回复摘要、派发运行、页面和复制时间,空字段省略;可直接贴给 AI 助手,助手用 `schedule_job_executions_list` 按 traceId 查询。
- 窄屏(≤860px)时指标改为 2×2,表格隐藏触发时间、耗时和 Trace ID 三列,这些信息在展开详情里仍能看到。
- 窄屏(≤860px)时指标改为 2×2,表格隐藏触发时间、耗时、Token 和 Trace ID 四列,这些信息在展开详情里仍能看到。
- AI 定时任务列表行常驻启停开关(`.hui-switch`,`role="switch"`,旁边文字为「已启用 / 已停用」)、「立即运行」和一枚「⋯」按钮(Harness `more-horizontal`,`aria-haspopup="menu"`)。开关调用 `POST /api/schedule-jobs/{id}/enabled`,只改启停,不动提示词、执行 Key 和会话;请求期间禁用,失败时回到原状态并提示;停用的任务名改为次要色。编辑弹窗不再显示「启用」,保存时沿用行上的状态,新任务默认启用;触发配置弹窗仍保留「启用」。另外只有订阅有新版时,行上才会多一枚淡紫「有新版 vN」。「⋯」菜单依次为编辑、执行记录、发布共享(订阅为「查看版本」)、分隔线、删除(红字)。菜单 `position: fixed`,在触发器下方 6px 右对齐,放不下时翻到上方;打开时聚焦第一项,方向键 / Home / End 移动,Esc 关闭并把焦点还给触发器,点击外部、Tab、滚动或缩放窗口时关闭;列表轮询重绘时保持菜单打开。点击行主体仍打开编辑。
- AI 定时任务弹窗不让选执行身份:任务用保存人的 Key 执行(新建时就是创建人,其他成员编辑保存后改用他的 Key,不会借用别人的 Key),选的模型决定用哪个 CLI 的 Key。保存人没有个人 CodeBuddy Key 时改用平台共享 Key,模型只能选管理员允许的那几个(选 CLI 默认时用共享 Key 的默认模型);两者都没有,或选了 Codex / Claude Code 但没配对应 Key,保存会被拒绝,并提示去个人配置填 Key 或联系管理员配置共享 Key。弹窗说明和模型提示写明这个规则。模型下方是「会话」(每次新开会话 / 始终在同一个会话里继续),固定会话已有会话时,提示文字后带「打开当前会话 ↗」。选「每次新开会话」时下面多一项「保留会话数」(最近 10 / 50 / 100 个、不限,默认 50),超过后每次执行结束时删除最早的会话,执行记录仍保留;删除会话后,它的消息引用的截图、上传文件和回复文件(存放在发送人的附件目录里)由后台清理:删除请求只记一个待清理标记,后台在删除后约 10 秒(之后每 30 分钟兜底一次)批量检查一遍引用再删,仍被其他会话(例如分支会话)、进行中的回复或空间消息引用的文件保留;这对所有会话的删除都生效,不只是定时任务;选「始终在同一个会话里继续」时这一项隐藏。列表行不写用谁的 Key,固定会话的任务带「同一会话」。

Expand Down
25 changes: 25 additions & 0 deletions docs/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5408,6 +5408,21 @@ components:
failed: { type: integer }
running: { type: integer }
avgMs: { type: integer, format: int64 }
metered:
type: integer
description: Finished executions that reported usage; missing usage is not counted as zero.
tokens:
type: object
description: Sum of each execution's reported total (or input plus output); cache details are not added again.
properties:
total: { type: integer, format: int64 }
input: { type: integer, format: int64 }
output: { type: integer, format: int64 }
cacheRead: { type: integer, format: int64 }
cacheWrite: { type: integer, format: int64 }
avgTokens: { type: integer, format: int64, description: Average tokens over metered executions only. }
priced: { type: integer, description: Executions whose cost could be estimated. }
costCny: { type: number, format: double, description: Sum of estimated costs in CNY. }
ScheduleJobExecution:
type: object
description: One execution of a schedule job. Each job keeps its latest 200 executions.
Expand Down Expand Up @@ -5440,6 +5455,16 @@ components:
type: integer
assistantMessage:
type: string
model:
type: string
description: Model used by this execution's AI generation.
usage:
$ref: "#/components/schemas/TokenUsage"
description: Optional usage for this execution including retries and usage before failure, excluding prior turns and downstream SOPs. Absence means unreported.
costCny:
type: number
format: double
description: Optional estimated cost in CNY, only present when pricing is known.
sessionId:
type: string
description: Studio conversation an AI execution ran in.
Expand Down
4 changes: 3 additions & 1 deletion docs/api/orchestration.md
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,9 @@ curl -u "$AGENTFLOW_USER:$AGENTFLOW_PASS" \
- `overview.current` / `overview.previous`:最近 7×24 小时(含当前小时)与再往前 7×24 小时的 `total`、`finished`、`success`、`failed`、`running`、`avgMs`,只随 `jobId` 变化。已结束的执行来自按小时累计的统计表 `schedule_job_execution_stats`(保留 30 天,删除任务时一并删除),不受每个任务只保留 200 条记录的影响;进行中的执行从记录里数;
- `records`:该项目保留的记录总数;`recentFailed`:该项目最近 7 天的失败次数(标签角标用)。

缺 `projectId` 或 `since` 格式不对返回 400;`jobId` 属于无权访问的项目返回 403。统计表从部署后开始累计,部署前的执行不计入概览。
缺 `projectId` 或 `since` 格式不对返回 400;`jobId` 属于无权访问的项目返回 403。统计表从部署后开始累计。升级时可备份并停止平台后,手动执行 `migrations/039_schedule_execution_legacy_stats.sql`,将仍保留的旧执行补入缺失的小时桶;脚本不覆盖已有桶,发现部分重叠时拒绝回补,不重建已清理的记录,也不推算旧 Token。

AI 执行记录的可选 `model`、`usage`、`costCny` 保存本次 generation 的消耗与费用估计,包含重试及失败前已上报的调用,不含同一固定会话的前次执行或下游 SOP。`usage` 缺失表示未上报。`overview.current` / `overview.previous` 另外返回 `metered`、`tokens`(`total`、`input`、`output`、`cacheRead`、`cacheWrite`)、`avgTokens`、`priced`、`costCny`。`tokens.total` 优先使用模型上报总量,否则为输入加输出;缓存明细不再相加。`avgTokens` 仅对已上报执行取平均,`priced` 仅计入可估价执行。

```bash
curl -u "$AGENTFLOW_USER:$AGENTFLOW_PASS" \
Expand Down
64 changes: 64 additions & 0 deletions internal/domain/schedule_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,51 @@ type ScheduleJobExecution struct {
StartedAt time.Time `json:"startedAt"`
FinishedAt *time.Time `json:"finishedAt,omitempty"`
DurationMs int64 `json:"durationMs,omitempty"`
// Model and Usage cover this execution's generation, including retries
// and usage reported before failure, but not prior turns in a fixed session.
// Usage.CostUSD/CostStatus are the analytics estimate; CostCNY is set only
// when that estimate is known. Unreported executions and SOP triggers have
// no usage.
Model string `json:"model,omitempty"`
Usage *TokenUsage `json:"usage,omitempty"`
CostCNY *float64 `json:"costCny,omitempty"`
}

// RecordUsage stores the prompt's model and usage on the execution, priced
// like conversation turns in analytics. Empty usage is not recorded.
func (e *ScheduleJobExecution) RecordUsage(model string, usage *TokenUsage) {
if usage == nil || usageTotalTokens(*usage) == 0 && usage.CostUSD == 0 {
return
}
priced := PriceScheduleExecutionUsage(model, *usage)
e.Model, e.Usage, e.CostCNY = strings.TrimSpace(model), &priced, nil
if priced.CostStatus == "estimated" {
cny := priced.CostUSD * AnalyticsUSDToCNY
e.CostCNY = &cny
}
}

// PriceScheduleExecutionUsage prices an AI prompt's usage the way analytics
// prices conversation turns: GPT models report input including caches.
func PriceScheduleExecutionUsage(model string, usage TokenUsage) TokenUsage {
inclusive := strings.HasPrefix(strings.ToLower(strings.TrimSpace(model)), "gpt-")
return AnalyticsPricedUsage(model, usage, inclusive)
}

// usageTotalTokens is the billed total, falling back to input + output.
func usageTotalTokens(u TokenUsage) int64 {
if u.TotalTokens > 0 {
return u.TotalTokens
}
return u.InputTokens + u.OutputTokens
}

// ExecutionTotalTokens is the billed total of an execution's usage.
func (e ScheduleJobExecution) ExecutionTotalTokens() int64 {
if e.Usage == nil {
return 0
}
return usageTotalTokens(*e.Usage)
}

// ScheduleJobExecutionRun links an execution to the SOP run it started.
Expand Down Expand Up @@ -80,6 +125,25 @@ type ScheduleExecutionWindow struct {
Failed int `json:"failed"`
Running int `json:"running"`
AvgMs int64 `json:"avgMs"`
// Metered counts the finished executions that recorded token usage;
// AvgTokens averages Tokens.Total over them, so runs that failed before
// reaching a model do not pull the average down. Priced counts those
// whose cost could be estimated; CostCNY sums their estimates.
Metered int `json:"metered"`
Tokens ScheduleExecutionTokens `json:"tokens"`
AvgTokens int64 `json:"avgTokens"`
Priced int `json:"priced"`
CostCNY float64 `json:"costCny"`
}

// ScheduleExecutionTokens sums token usage. Total is the billed total; the
// four parts follow the providers' own split and need not add up to it.
type ScheduleExecutionTokens struct {
Total int64 `json:"total"`
Input int64 `json:"input"`
Output int64 `json:"output"`
CacheRead int64 `json:"cacheRead"`
CacheWrite int64 `json:"cacheWrite"`
}

// ScheduleSessionFixed is ScheduleJob.SessionMode for an AI job that keeps
Expand Down
3 changes: 3 additions & 0 deletions internal/httpapi/assistant.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,9 @@ func (h *assistantHandlers) chat(c *gin.Context) {
if replyModel == "" {
replyModel = assistantDefaultModel
}
if report, ok := modelCtx.Value(scheduledUsageReportKey{}).(scheduledUsageReport); ok && report.GenerationID == assistantControlScopeFrom(modelCtx).GenerationID && report.Report != nil {
defer func() { report.Report(replyModel, turnUsage.snapshot()) }()
}
generate := h.generate
if generate == nil {
resume := false
Expand Down
Loading
Loading