Files
ai_academy/internal/application/service/suspended_run.go
T
runny 9058149576
Build & Publish (dsh-academy) / End-to-end inside dsh (latest, advisory) (push) Skipped
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Frontend / Docker multi-stage image (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-academy) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-academy) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-academy) / Publish to npm (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Helm / chart (push) Canceled after 0s
refactor: 项目更名 WeKnora → Academy
- 全量替换代码/配置/文档中的项目名(保留上游 GitHub 链接、weixin 文档域名、Docker Hub 镜像名)
- 重命名 32 个含项目名的文件/目录;Go module 路径改为 academy(含 client/cli 子模块)
- docreader proto 描述符采用等长替换,保持序列化字节布局有效
- Dockerfile.app 构建网络修复: browserskill 离线产物通道、PyPI/apt 国内镜像
- 运行验证: go build ✓ docker compose up ✓ 健康检查 ✓
2026-11-13 20:52:51 +08:00

102 lines
3.5 KiB
Go

package service
import (
"context"
"time"
"academy/internal/agent"
"academy/internal/agent/hitl"
agenttools "academy/internal/agent/tools"
"academy/internal/types"
"academy/internal/types/interfaces"
)
// SuspendedRunService keeps the assistant message of a suspended run in step
// with its prompt when the system, not the run itself, moves the prompt on:
// the reaper taking over, or the user moving on without answering.
type SuspendedRunService struct {
RecoverAnswered func(context.Context, *types.AgentPrompt) (bool, error)
StopDelegation func(context.Context, *types.AgentPrompt) error
broker *hitl.Broker
messages interfaces.MessageRepository
}
// NewSuspendedRunService builds the service.
func NewSuspendedRunService(broker *hitl.Broker, messages interfaces.MessageRepository) *SuspendedRunService {
s := &SuspendedRunService{broker: broker, messages: messages}
broker.SetRunHooks(s.Hooks())
return s
}
// Hooks returns the message updates the broker runs as it takes prompts over.
func (s *SuspendedRunService) Hooks() hitl.RunHooks {
return hitl.RunHooks{RecoverAnswered: s.RecoverAnswered, Suspended: s.MarkAwaitingInput, Closed: s.Close}
}
// Reap runs one reaper sweep.
func (s *SuspendedRunService) Reap(ctx context.Context, now time.Time) (int, error) {
return s.broker.Reap(ctx, now, s.Hooks())
}
// SupersedeSession closes the session's suspended runs because the user moved
// on. Call it before the session's history is read or cut.
func (s *SuspendedRunService) SupersedeSession(ctx context.Context, sessionID string) (int, error) {
return s.broker.SupersedeSession(ctx, sessionID, s.Hooks())
}
// MarkAwaitingInput shows a run suspended on its owner's behalf the way a
// self-suspended run looks: the saved steps, waiting on the prompt.
func (s *SuspendedRunService) MarkAwaitingInput(
ctx context.Context, prompt *types.AgentPrompt, state *types.AgentRunSuspension,
) error {
return s.update(ctx, prompt, state, func(msg *types.Message, snap *agent.RunSnapshot) {
msg.AgentSteps = agenttools.SanitizeAgentStepsForStorage(snap.DisplaySteps())
msg.RunState = types.MessageRunStateAwaitingInput
})
}
// Close completes a run that will never resume. The blocked call and the
// calls after it are recorded as failed with reason.
func (s *SuspendedRunService) Close(
ctx context.Context, prompt *types.AgentPrompt, state *types.AgentRunSuspension, reason string,
) error {
if s.StopDelegation != nil {
if err := s.StopDelegation(ctx, prompt); err != nil {
return err
}
}
return s.update(ctx, prompt, state, func(msg *types.Message, snap *agent.RunSnapshot) {
msg.AgentSteps = agenttools.SanitizeAgentStepsForStorage(snap.ClosedSteps(reason))
msg.KnowledgeReferences = mergeSnapshotReferences(msg.KnowledgeReferences, snap.KnowledgeRefs)
msg.RunState = ""
msg.IsCompleted = true
})
}
func (s *SuspendedRunService) update(
ctx context.Context, prompt *types.AgentPrompt, state *types.AgentRunSuspension,
apply func(*types.Message, *agent.RunSnapshot),
) error {
snap, err := agent.DecodeRunSnapshot(state.Snapshot)
if err != nil {
return err
}
msg, err := s.messages.GetMessage(ctx, prompt.SessionID, prompt.AssistantMessageID)
if err != nil {
return err
}
if msg == nil || msg.IsCompleted {
return nil
}
apply(msg, snap)
msg.UpdatedAt = time.Now()
return s.messages.UpdateMessage(ctx, msg)
}
func mergeSnapshotReferences(existing types.References, refs []*types.SearchResult) types.References {
if len(existing) > 0 || len(refs) == 0 {
return existing
}
return types.References(refs)
}