( config: ApiQueryHookConfig<TResult>, )
| 54 | } |
| 55 | |
| 56 | export function createApiQueryHook<TResult>( |
| 57 | config: ApiQueryHookConfig<TResult>, |
| 58 | ) { |
| 59 | return async (context: ApiQueryHookContext): Promise<void> => { |
| 60 | try { |
| 61 | const shouldRun = await config.shouldRun(context) |
| 62 | if (!shouldRun) { |
| 63 | return |
| 64 | } |
| 65 | |
| 66 | const uuid = randomUUID() |
| 67 | |
| 68 | // Build messages using the config's buildMessages function |
| 69 | const messages = config.buildMessages(context) |
| 70 | context.queryMessageCount = messages.length |
| 71 | |
| 72 | // Use config's system prompt if provided, otherwise use context's |
| 73 | const systemPrompt = config.systemPrompt |
| 74 | ? asSystemPrompt([config.systemPrompt]) |
| 75 | : context.systemPrompt |
| 76 | |
| 77 | // Use config's tools preference (defaults to true = use context tools) |
| 78 | const useTools = config.useTools ?? true |
| 79 | const tools = useTools ? context.toolUseContext.options.tools : [] |
| 80 | |
| 81 | // Get model (lazy loaded) |
| 82 | const model = config.getModel(context) |
| 83 | |
| 84 | // Make API call |
| 85 | const response = await queryModelWithoutStreaming({ |
| 86 | messages, |
| 87 | systemPrompt, |
| 88 | thinkingConfig: { type: 'disabled' as const }, |
| 89 | tools, |
| 90 | signal: createAbortController().signal, |
| 91 | options: { |
| 92 | getToolPermissionContext: async () => { |
| 93 | const appState = context.toolUseContext.getAppState() |
| 94 | return appState.toolPermissionContext |
| 95 | }, |
| 96 | model, |
| 97 | toolChoice: undefined, |
| 98 | isNonInteractiveSession: |
| 99 | context.toolUseContext.options.isNonInteractiveSession, |
| 100 | hasAppendSystemPrompt: |
| 101 | !!context.toolUseContext.options.appendSystemPrompt, |
| 102 | temperatureOverride: 0, |
| 103 | agents: context.toolUseContext.options.agentDefinitions.activeAgents, |
| 104 | querySource: config.name, |
| 105 | mcpTools: [], |
| 106 | agentId: context.toolUseContext.agentId, |
| 107 | }, |
| 108 | }) |
| 109 | |
| 110 | // Parse response |
| 111 | const content = extractTextContent(response.message.content).trim() |
| 112 | |
| 113 | try { |
no test coverage detected