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
|
// message is recorded. Nil disables the capability (no role configured, or
|
||||||
// the provider credential did not resolve), leaving the frontend's note.
|
// the provider credential did not resolve), leaving the frontend's note.
|
||||||
Transcriber messaging.Transcriber
|
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
|
// componentInstallTypeEnricher builds Service.Enrich: it knows how to
|
||||||
@@ -110,12 +113,15 @@ func makeChatLLMResponder(
|
|||||||
s chatSetup,
|
s chatSetup,
|
||||||
sessionStore gateway.SessionStore,
|
sessionStore gateway.SessionStore,
|
||||||
router *gateway.Router,
|
router *gateway.Router,
|
||||||
) (gateway.LLMResponder, gateway.LLMStreamResponder) {
|
) (gateway.LLMResponder, gateway.LLMStreamResponder, error) {
|
||||||
if s.LLM == nil || s.LLM() == nil {
|
if s.LLM == nil || s.LLM() == nil {
|
||||||
return nil, nil
|
return nil, nil, nil
|
||||||
}
|
}
|
||||||
runner := newChatTurnRunner(ctx, channel, s, sessionStore, router)
|
runner, err := newChatTurnRunner(ctx, channel, s, sessionStore, router)
|
||||||
return runner.Respond, runner.RespondStream
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
return runner.Respond, runner.RespondStream, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func newChatTurnRunner(
|
func newChatTurnRunner(
|
||||||
@@ -124,9 +130,9 @@ func newChatTurnRunner(
|
|||||||
s chatSetup,
|
s chatSetup,
|
||||||
sessionStore gateway.SessionStore,
|
sessionStore gateway.SessionStore,
|
||||||
router *gateway.Router,
|
router *gateway.Router,
|
||||||
) *gateway.TurnRunner {
|
) (*gateway.TurnRunner, error) {
|
||||||
cfg := s.Cfg.Get()
|
cfg := s.Cfg.Get()
|
||||||
runner := gateway.NewTurnRunner(gateway.TurnRunnerConfig{
|
runner, err := gateway.NewTurnRunner(gateway.TurnRunnerConfig{
|
||||||
Router: router,
|
Router: router,
|
||||||
Sessions: sessionStore,
|
Sessions: sessionStore,
|
||||||
Models: s.ChatModels,
|
Models: s.ChatModels,
|
||||||
@@ -147,12 +153,16 @@ func newChatTurnRunner(
|
|||||||
MemoryWriter: s.MemoryWriter,
|
MemoryWriter: s.MemoryWriter,
|
||||||
UserIdentity: userIdentityResolver(),
|
UserIdentity: userIdentityResolver(),
|
||||||
Transcriber: s.Transcriber,
|
Transcriber: s.Transcriber,
|
||||||
|
OwnerID: s.OwnerID,
|
||||||
Log: s.Log,
|
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 {
|
if err := runner.Recover(ctx); err != nil && s.Log != nil {
|
||||||
s.Log.ErrorContext(ctx, "recover chat turns", "channel", channel, "err", err)
|
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
|
// 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.
|
// that must record their outcomes for /status.
|
||||||
ProviderOutcomes: b.providerOutcomes,
|
ProviderOutcomes: b.providerOutcomes,
|
||||||
Usage: b.usage,
|
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.Titles = newChatTitleGenerator(setup)
|
||||||
router.Log = b.log
|
router.Log = b.log
|
||||||
return &gateway.LocalChatAdapter{
|
return &gateway.LocalChatAdapter{
|
||||||
|
|||||||
@@ -76,8 +76,6 @@ type TurnReplayStore interface {
|
|||||||
FindPriorReply(ctx context.Context, sessionID, sourceID, identity string) (string, error)
|
FindPriorReply(ctx context.Context, sessionID, sourceID, identity string) (string, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
var defaultTurnOwnerID = NewTurnID()
|
|
||||||
|
|
||||||
const defaultTurnPersistenceTimeout = 5 * time.Second
|
const defaultTurnPersistenceTimeout = 5 * time.Second
|
||||||
|
|
||||||
// TurnRunnerConfig contains the dependencies for one channel's chat turn
|
// TurnRunnerConfig contains the dependencies for one channel's chat turn
|
||||||
@@ -136,18 +134,20 @@ type TurnRunner struct {
|
|||||||
recovered bool
|
recovered bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTurnRunner constructs a chat turn runner.
|
// NewTurnRunner constructs a chat turn runner. Every runner in a process must
|
||||||
func NewTurnRunner(cfg TurnRunnerConfig) *TurnRunner {
|
// 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 {
|
if cfg.Ledger == nil {
|
||||||
cfg.Ledger, _ = cfg.Sessions.(TurnLedger)
|
cfg.Ledger, _ = cfg.Sessions.(TurnLedger)
|
||||||
}
|
}
|
||||||
if cfg.OwnerID == "" {
|
|
||||||
cfg.OwnerID = defaultTurnOwnerID
|
|
||||||
}
|
|
||||||
if cfg.PersistenceTimeout <= 0 {
|
if cfg.PersistenceTimeout <= 0 {
|
||||||
cfg.PersistenceTimeout = defaultTurnPersistenceTimeout
|
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
|
// Recover marks turns from a previous process as failed and retryable. It is
|
||||||
|
|||||||
Reference in new issue
Block a user