2026-04-12 23:37:52 +08:00
|
|
|
package runtime
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2026-08-21 00:41:07 +08:00
|
|
|
applicationruntime "code.tczkiot.com/wlw/ai-agent/internal/ai/application/runtime"
|
|
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/ai/runtime/graphs"
|
|
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/models"
|
2026-04-12 23:37:52 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type runtimeReplyExecutor struct{}
|
|
|
|
|
|
2026-04-19 11:19:51 +08:00
|
|
|
type runtimeReplyRunInput struct {
|
|
|
|
|
Conversation models.Conversation
|
|
|
|
|
Message models.Message
|
|
|
|
|
AIAgent models.AIAgent
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type runtimeReplyResumeInput struct {
|
|
|
|
|
Conversation models.Conversation
|
|
|
|
|
Message models.Message
|
|
|
|
|
AIAgent models.AIAgent
|
|
|
|
|
PendingInterrupt *models.ConversationInterrupt
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-12 23:37:52 +08:00
|
|
|
func newRuntimeReplyExecutor() *runtimeReplyExecutor {
|
|
|
|
|
return &runtimeReplyExecutor{}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-27 23:29:02 +08:00
|
|
|
func (e *runtimeReplyExecutor) Run(ctx context.Context, input runtimeReplyRunInput) (*applicationruntime.RunResult, error) {
|
2026-08-28 22:23:13 +08:00
|
|
|
config, err := applicationruntime.ResolveRuntimeAIConfig(ctx, input.AIAgent.AIConfigID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
// The trigger layer may enrich an anonymous channel conversation with a
|
|
|
|
|
// business subject resolved from the current message or recent history.
|
|
|
|
|
// Run the already validated objects so that card/device identity is not lost
|
|
|
|
|
// by reloading the original guest ownership record from the database.
|
|
|
|
|
summary, err := applicationruntime.DefaultAgentApplicationService.RunPrepared(ctx, applicationruntime.RunInput{
|
|
|
|
|
Conversation: input.Conversation,
|
|
|
|
|
UserMessage: input.Message,
|
|
|
|
|
AIAgent: input.AIAgent,
|
|
|
|
|
AIConfig: *config,
|
2026-04-12 23:37:52 +08:00
|
|
|
})
|
|
|
|
|
return summary, err
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-27 23:29:02 +08:00
|
|
|
func (e *runtimeReplyExecutor) ResumePendingInterrupt(ctx context.Context, input runtimeReplyResumeInput) (*applicationruntime.RunResult, error) {
|
2026-04-19 11:19:51 +08:00
|
|
|
if input.PendingInterrupt == nil {
|
2026-04-19 11:40:17 +08:00
|
|
|
return nil, fmt.Errorf("pending interrupt is required")
|
2026-04-12 23:37:52 +08:00
|
|
|
}
|
2026-08-28 22:23:13 +08:00
|
|
|
config, err := applicationruntime.ResolveRuntimeAIConfig(ctx, input.AIAgent.AIConfigID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
summary, err := applicationruntime.DefaultAgentApplicationService.ResumePrepared(ctx, applicationruntime.ResumeInput{
|
|
|
|
|
Conversation: input.Conversation,
|
|
|
|
|
UserMessage: input.Message,
|
|
|
|
|
AIAgent: input.AIAgent,
|
|
|
|
|
AIConfig: *config,
|
2026-04-19 11:19:51 +08:00
|
|
|
CheckPointID: strings.TrimSpace(input.PendingInterrupt.CheckPointID),
|
2026-04-14 11:47:13 +08:00
|
|
|
ResumeData: map[string]string{
|
2026-04-19 11:19:51 +08:00
|
|
|
strings.TrimSpace(input.PendingInterrupt.InterruptID): strings.TrimSpace(input.Message.Content),
|
2026-04-12 23:37:52 +08:00
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
return summary, err
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-27 23:29:02 +08:00
|
|
|
func expiredInterruptSummary() *applicationruntime.RunResult {
|
|
|
|
|
return &applicationruntime.RunResult{
|
2026-04-12 23:37:52 +08:00
|
|
|
Status: "expired",
|
|
|
|
|
ReplyText: graphs.ConfirmationExpiredReply,
|
|
|
|
|
}
|
|
|
|
|
}
|