d204ce3187
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
112 lines
4.5 KiB
Go
112 lines
4.5 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/chattools"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// schedule_followup puts a prompt into the calling chat's own durable queue
|
|
// with a send_at, under the sender label chatFollowupClient. The operator sees
|
|
// it as a scheduled message from the agent and can remove it like any queued
|
|
// prompt; until it is due it does not hold the operator's prompts back
|
|
// (chatQueueHead).
|
|
|
|
// chatFollowupClient is the sender label of an agent's follow-up prompt.
|
|
const chatFollowupClient = "Agent follow-up"
|
|
|
|
func (s *chatToolService) runScheduleFollowup(ctx context.Context, sessionID, message, at string, afterMinutes *int64) chattools.Result {
|
|
fail := func(format string, args ...any) chattools.Result {
|
|
return chattools.Result{Text: "Nothing was scheduled: " + fmt.Sprintf(format, args...), IsError: true}
|
|
}
|
|
message = strings.TrimSpace(message)
|
|
if message == "" {
|
|
return fail("message is empty.")
|
|
}
|
|
if len(message) > chattools.MaxFollowupTextBytes {
|
|
return fail("message is %s; the limit is 8 KiB.", humanBytes(int64(len(message))))
|
|
}
|
|
at = strings.TrimSpace(at)
|
|
now := s.now()
|
|
var sendAt time.Time
|
|
switch {
|
|
case at != "" && afterMinutes != nil:
|
|
return fail("give either at or after_minutes, not both.")
|
|
case afterMinutes != nil:
|
|
if *afterMinutes < chattools.MinFollowupMinutes || *afterMinutes > chattools.MaxFollowupMinutes {
|
|
return fail("after_minutes must be between %d and %d (7 days).", chattools.MinFollowupMinutes, chattools.MaxFollowupMinutes)
|
|
}
|
|
sendAt = now.Add(time.Duration(*afterMinutes) * time.Minute)
|
|
case at != "":
|
|
parsed, err := time.Parse(time.RFC3339, at)
|
|
if err != nil {
|
|
return fail("at must be an RFC 3339 time with a zone, such as %s.", now.Add(time.Hour).UTC().Format(time.RFC3339))
|
|
}
|
|
// A few seconds of slack for a time computed as "one minute from now".
|
|
if parsed.Before(now.Add(time.Duration(chattools.MinFollowupMinutes)*time.Minute - 5*time.Second)) {
|
|
return fail("at must be at least %d minute from now (it is %s).", chattools.MinFollowupMinutes, now.UTC().Format(time.RFC3339))
|
|
}
|
|
if parsed.After(now.Add(time.Duration(chattools.MaxFollowupMinutes) * time.Minute)) {
|
|
return fail("at must be at most 7 days from now.")
|
|
}
|
|
sendAt = parsed
|
|
default:
|
|
return fail("give at (an RFC 3339 time) or after_minutes.")
|
|
}
|
|
if s.hub == nil || s.broker == nil {
|
|
return fail("follow-ups are not available on this server.")
|
|
}
|
|
pending := 0
|
|
for _, prompt := range s.hub.queueFrame(sessionID).Prompts {
|
|
if strings.HasPrefix(prompt.OperationID, chatFollowupPrefix) {
|
|
pending++
|
|
}
|
|
}
|
|
if pending >= chattools.MaxPendingFollowups {
|
|
return fail("this chat already has %d follow-ups pending (the limit is %d); the operator can remove one, or wait for one to be delivered.", pending, chattools.MaxPendingFollowups)
|
|
}
|
|
var token [8]byte
|
|
_, _ = rand.Read(token[:])
|
|
operationID := chatFollowupPrefix + hex.EncodeToString(token[:])
|
|
text := fmt.Sprintf("[Follow-up you scheduled at %s with schedule_followup] %s", now.UTC().Format(time.RFC3339), message)
|
|
if err := s.hub.queueFollowup(ctx, s.broker, sessionID, operationID, text, sendAt); err != nil {
|
|
return fail("%v", err)
|
|
}
|
|
return chattools.Result{Text: fmt.Sprintf("Scheduled: Pipelines will send this chat your follow-up at %s (in %s), when the chat is free; the operator sees it queued and can remove it. Do not wait for it now.",
|
|
sendAt.UTC().Format(time.RFC3339), sendAt.Sub(now).Round(time.Minute))}
|
|
}
|
|
|
|
// queueFollowup queues an agent's follow-up prompt for sendAt: durably, under
|
|
// the follow-up sender label, shown to every attached client as scheduled.
|
|
func (h *chatHub) queueFollowup(ctx context.Context, broker chatBroker, sessionID, operationID, text string, sendAt time.Time) error {
|
|
p := &chatQueuedPrompt{
|
|
text: text,
|
|
operationID: operationID,
|
|
messageID: h.nextUserMessageID(),
|
|
client: chatFollowupClient,
|
|
noSteer: true,
|
|
sendAt: sendAt,
|
|
}
|
|
h.rememberOp(sessionID, operationID)
|
|
admission, err := h.admitPrompt(ctx, sessionID, broker, p)
|
|
if admission == chatAdmitRefused {
|
|
h.forgetOp(sessionID, operationID)
|
|
return err
|
|
}
|
|
h.broadcastUser(protocol.ChatEventFrame{
|
|
SessionID: sessionID,
|
|
MessageID: p.messageID,
|
|
Kind: protocol.ChatEventUser,
|
|
Part: &protocol.ChatPart{Type: protocol.ChatPartText, ID: "usr", Text: text},
|
|
Client: chatFollowupClient,
|
|
OperationID: operationID,
|
|
}, "")
|
|
return nil
|
|
}
|