--------------------------------------------------------------------------- chat ---------------------------------------------------------------------------
(_ js.Value, args []js.Value)
| 169 | // --------------------------------------------------------------------------- |
| 170 | |
| 171 | func chatJS(_ js.Value, args []js.Value) any { |
| 172 | if len(args) < 1 { |
| 173 | return rejectedPromise("chat: expected at least one argument (options object)") |
| 174 | } |
| 175 | opts := args[0] |
| 176 | var onEvent js.Value |
| 177 | if len(args) >= 2 && args[1].Type() == js.TypeFunction { |
| 178 | onEvent = args[1] |
| 179 | } |
| 180 | |
| 181 | yamlStr := opts.Get("yaml").String() |
| 182 | agentName := "" |
| 183 | if v := opts.Get("agentName"); v.Type() == js.TypeString { |
| 184 | agentName = v.String() |
| 185 | } |
| 186 | envMap := jsObjectToStringMap(opts.Get("env")) |
| 187 | messages, err := jsToMessages(opts.Get("messages")) |
| 188 | if err != nil { |
| 189 | return rejectedPromise(fmt.Sprintf("chat: parsing messages: %v", err)) |
| 190 | } |
| 191 | |
| 192 | // Set up cancellable context. |
| 193 | ctx, cancel := context.WithCancel(context.Background()) |
| 194 | abortState.mu.Lock() |
| 195 | if abortState.cancel != nil { |
| 196 | abortState.cancel() |
| 197 | } |
| 198 | abortState.cancel = cancel |
| 199 | abortState.mu.Unlock() |
| 200 | |
| 201 | return newPromise(func(resolve, reject func(any)) { |
| 202 | go func() { |
| 203 | defer func() { |
| 204 | abortState.mu.Lock() |
| 205 | defer abortState.mu.Unlock() |
| 206 | cancel() |
| 207 | }() |
| 208 | |
| 209 | result, err := runChat(ctx, yamlStr, agentName, envMap, messages, onEvent) |
| 210 | if err != nil { |
| 211 | if ctx.Err() != nil { |
| 212 | reject(jsError(fmt.Errorf("chat aborted"))) |
| 213 | } else { |
| 214 | reject(jsError(err)) |
| 215 | } |
| 216 | return |
| 217 | } |
| 218 | resolve(result) |
| 219 | }() |
| 220 | }) |
| 221 | } |
| 222 | |
| 223 | // runChat loads the config, builds the runtime, and runs the agentic loop. |
| 224 | func runChat(ctx context.Context, yamlStr, agentName string, envMap map[string]string, messages []chat.Message, onEvent js.Value) (any, error) { |
nothing calls this directly
no test coverage detected