New loads the configured agent and creates a fresh conversation session.
(ctx context.Context, cfg Config)
| 101 | |
| 102 | // New loads the configured agent and creates a fresh conversation session. |
| 103 | func New(ctx context.Context, cfg Config) (*Session, error) { |
| 104 | if cfg.AgentSource == nil { |
| 105 | return nil, ErrAgentSourceRequired |
| 106 | } |
| 107 | runConfig := cfg.RuntimeConfig |
| 108 | if runConfig == nil { |
| 109 | runConfig = &dagentcfg.RuntimeConfig{} |
| 110 | } |
| 111 | |
| 112 | loadOpts := loaderdefaults.Opts() |
| 113 | if cfg.ToolsetRegistry != nil { |
| 114 | loadOpts = append(loadOpts, teamloader.WithToolsetRegistry(cfg.ToolsetRegistry)) |
| 115 | } |
| 116 | loaded, err := teamloader.LoadWithConfig(ctx, cfg.AgentSource, runConfig, loadOpts...) |
| 117 | if err != nil { |
| 118 | return nil, fmt.Errorf("embeddedchat: load agent: %w", err) |
| 119 | } |
| 120 | |
| 121 | modelSwitcher := &dagentruntime.ModelSwitcherConfig{ |
| 122 | Models: loaded.Models, |
| 123 | Providers: loaded.Providers, |
| 124 | ModelsGateway: runConfig.ModelsGateway, |
| 125 | EnvProvider: runConfig.EnvProvider(), |
| 126 | ProviderRegistry: loaded.ProviderRegistry, |
| 127 | AgentDefaultModels: loaded.AgentDefaultModels, |
| 128 | } |
| 129 | // Reuse the models.dev store the team loader already warmed so model- |
| 130 | // metadata lookups don't re-pay the cold catalog parse. |
| 131 | if store, storeErr := runConfig.ModelsDevStore(); storeErr == nil { |
| 132 | modelSwitcher.ModelsStore = store |
| 133 | } |
| 134 | |
| 135 | runtimeOpts := []dagentruntime.Opt{ |
| 136 | dagentruntime.WithModelSwitcherConfig(modelSwitcher), |
| 137 | dagentruntime.WithWorkingDir(runConfig.WorkingDir), |
| 138 | dagentruntime.WithSessionStore(session.NewInMemorySessionStore()), |
| 139 | } |
| 140 | runtimeOpts = append(runtimeOpts, cfg.RuntimeOptions...) |
| 141 | rt, err := dagentruntime.New(ctx, loaded.Team, runtimeOpts...) |
| 142 | if err != nil { |
| 143 | return nil, fmt.Errorf("embeddedchat: create runtime: %w", err) |
| 144 | } |
| 145 | |
| 146 | s := &Session{cfg: cfg, rt: rt} |
| 147 | if root, err := loaded.Team.DefaultAgent(); err == nil { |
| 148 | s.welcome = root.WelcomeMessage() |
| 149 | } |
| 150 | s.resetConversationLocked() |
| 151 | return s, nil |
| 152 | } |
| 153 | |
| 154 | // WelcomeMessage returns the loaded agent's welcome message. |
| 155 | func (s *Session) WelcomeMessage() string { return s.welcome } |