( params: QueryParams, )
| 274 | } |
| 275 | |
| 276 | export async function* query( |
| 277 | params: QueryParams, |
| 278 | ): AsyncGenerator< |
| 279 | | StreamEvent |
| 280 | | RequestStartEvent |
| 281 | | Message |
| 282 | | TombstoneMessage |
| 283 | | ToolUseSummaryMessage, |
| 284 | Terminal |
| 285 | > { |
| 286 | const consumedCommandUuids: string[] = [] |
| 287 | const consumedAutonomyCommands: QueuedCommand[] = [] |
| 288 | |
| 289 | // Create Langfuse trace for this query turn (no-op if not configured). |
| 290 | // When called as a sub-agent, langfuseTrace is already set by runAgent() |
| 291 | // — reuse it instead of creating an independent trace. |
| 292 | const ownsTrace = !params.toolUseContext.langfuseTrace |
| 293 | logForDebugging( |
| 294 | `[query] ownsTrace=${ownsTrace} incoming langfuseTrace=${params.toolUseContext.langfuseTrace ? 'present' : 'null/undefined'} isLangfuseEnabled=${isLangfuseEnabled()}`, |
| 295 | ) |
| 296 | const langfuseTrace = |
| 297 | params.toolUseContext.langfuseTrace ?? |
| 298 | (isLangfuseEnabled() |
| 299 | ? createTrace({ |
| 300 | sessionId: getSessionId(), |
| 301 | model: params.toolUseContext.options.mainLoopModel, |
| 302 | provider: getAPIProvider(), |
| 303 | input: params.messages, |
| 304 | querySource: params.querySource, |
| 305 | }) |
| 306 | : null) |
| 307 | |
| 308 | // Attach trace to toolUseContext so tool execution can record observations |
| 309 | const paramsWithTrace: QueryParams = langfuseTrace |
| 310 | ? { |
| 311 | ...params, |
| 312 | toolUseContext: { ...params.toolUseContext, langfuseTrace }, |
| 313 | } |
| 314 | : params |
| 315 | |
| 316 | let terminal: Terminal | undefined |
| 317 | let didThrow = false |
| 318 | let thrownError: unknown |
| 319 | try { |
| 320 | terminal = yield* queryLoop( |
| 321 | paramsWithTrace, |
| 322 | consumedCommandUuids, |
| 323 | consumedAutonomyCommands, |
| 324 | ) |
| 325 | } catch (error) { |
| 326 | didThrow = true |
| 327 | thrownError = error |
| 328 | throw error |
| 329 | } finally { |
| 330 | await finalizeAutonomyCommandsForTurn({ |
| 331 | commands: consumedAutonomyCommands, |
| 332 | outcome: getAutonomyTurnOutcome({ |
| 333 | terminal, |
no test coverage detected