refactor(gateway): the turn owner ID is made once by the composition root (#1928)
This commit is contained in:
3 files changed
+31
-16
No files matched your search
@@ -72,6 +72,9 @@ type chatSetup struct {
|
||||
// message is recorded. Nil disables the capability (no role configured, or
|
||||
// the provider credential did not resolve), leaving the frontend's note.
|
||||
Transcriber messaging.Transcriber
|
||||
// OwnerID is the turn owner shared by every runner in this process, so
|
||||
// recovery and persistence agree on which turns are this process's.
|
||||
OwnerID string
|
||||
}
|
||||
|
||||
// componentInstallTypeEnricher builds Service.Enrich: it knows how to
|
||||
@@ -110,12 +113,15 @@ func makeChatLLMResponder(
|
||||
s chatSetup,
|
||||
sessionStore gateway.SessionStore,
|
||||
router *gateway.Router,
|
||||
) (gateway.LLMResponder, gateway.LLMStreamResponder) {
|
||||
) (gateway.LLMResponder, gateway.LLMStreamResponder, error) {
|
||||
if s.LLM == nil || s.LLM() == nil {
|
||||
return nil, nil
|
||||
return nil, nil, nil
|
||||
}
|
||||
runner := newChatTurnRunner(ctx, channel, s, sessionStore, router)
|
||||
return runner.Respond, runner.RespondStream
|
||||
runner, err := newChatTurnRunner(ctx, channel, s, sessionStore, router)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return runner.Respond, runner.RespondStream, nil
|
||||
}
|
||||
|
||||
func newChatTurnRunner(
|
||||
@@ -124,9 +130,9 @@ func newChatTurnRunner(
|
||||
s chatSetup,
|
||||
sessionStore gateway.SessionStore,
|
||||
router *gateway.Router,
|
||||
) *gateway.TurnRunner {
|
||||
) (*gateway.TurnRunner, error) {
|
||||
cfg := s.Cfg.Get()
|
||||
runner := gateway.NewTurnRunner(gateway.TurnRunnerConfig{
|
||||
runner, err := gateway.NewTurnRunner(gateway.TurnRunnerConfig{
|
||||
Router: router,
|
||||
Sessions: sessionStore,
|
||||
Models: s.ChatModels,
|
||||
@@ -147,12 +153,16 @@ func newChatTurnRunner(
|
||||
MemoryWriter: s.MemoryWriter,
|
||||
UserIdentity: userIdentityResolver(),
|
||||
Transcriber: s.Transcriber,
|
||||
OwnerID: s.OwnerID,
|
||||
Log: s.Log,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("chat turn runner for %s: %w", channel, err)
|
||||
}
|
||||
if err := runner.Recover(ctx); err != nil && s.Log != nil {
|
||||
s.Log.ErrorContext(ctx, "recover chat turns", "channel", channel, "err", err)
|
||||
}
|
||||
return runner
|
||||
return runner, nil
|
||||
}
|
||||
|
||||
// chatRepoEnv renders the repositories the chat agent manages, with their
|
||||
|
||||
@@ -214,8 +214,13 @@ func (b *server) setupGatewayChat(ctx context.Context, actor gateway.ChatTaskAct
|
||||
// that must record their outcomes for /status.
|
||||
ProviderOutcomes: b.providerOutcomes,
|
||||
Usage: b.usage,
|
||||
OwnerID: gateway.NewTurnID(),
|
||||
}
|
||||
var err error
|
||||
router.LLM, router.LLMStream, err = makeChatLLMResponder(ctx, "web", setup, b.chatSessionStore, router)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
router.LLM, router.LLMStream = makeChatLLMResponder(ctx, "web", setup, b.chatSessionStore, router)
|
||||
router.Titles = newChatTitleGenerator(setup)
|
||||
router.Log = b.log
|
||||
return &gateway.LocalChatAdapter{
|
||||
|
||||
@@ -76,8 +76,6 @@ type TurnReplayStore interface {
|
||||
FindPriorReply(ctx context.Context, sessionID, sourceID, identity string) (string, error)
|
||||
}
|
||||
|
||||
var defaultTurnOwnerID = NewTurnID()
|
||||
|
||||
const defaultTurnPersistenceTimeout = 5 * time.Second
|
||||
|
||||
// TurnRunnerConfig contains the dependencies for one channel's chat turn
|
||||
@@ -136,18 +134,20 @@ type TurnRunner struct {
|
||||
recovered bool
|
||||
}
|
||||
|
||||
// NewTurnRunner constructs a chat turn runner.
|
||||
func NewTurnRunner(cfg TurnRunnerConfig) *TurnRunner {
|
||||
// NewTurnRunner constructs a chat turn runner. Every runner in a process must
|
||||
// share one OwnerID: recovery and turn persistence match turns by owner, so a
|
||||
// second ID would make this runner fail the other runners' turns.
|
||||
func NewTurnRunner(cfg TurnRunnerConfig) (*TurnRunner, error) {
|
||||
if cfg.OwnerID == "" {
|
||||
return nil, errors.New("turn runner: owner ID is required")
|
||||
}
|
||||
if cfg.Ledger == nil {
|
||||
cfg.Ledger, _ = cfg.Sessions.(TurnLedger)
|
||||
}
|
||||
if cfg.OwnerID == "" {
|
||||
cfg.OwnerID = defaultTurnOwnerID
|
||||
}
|
||||
if cfg.PersistenceTimeout <= 0 {
|
||||
cfg.PersistenceTimeout = defaultTurnPersistenceTimeout
|
||||
}
|
||||
return &TurnRunner{TurnRunnerConfig: cfg}
|
||||
return &TurnRunner{TurnRunnerConfig: cfg}, nil
|
||||
}
|
||||
|
||||
// Recover marks turns from a previous process as failed and retryable. It is
|
||||
|
||||
Reference in new issue
Block a user