(block: AgentBlock, context: AgentBlockContext)
| 146 | } |
| 147 | |
| 148 | export async function executeAgentBlock(block: AgentBlock, context: AgentBlockContext): Promise<AgentBlockResult> { |
| 149 | const openai = createOpenAI({ |
| 150 | apiKey: context.openAiToken, |
| 151 | baseURL: process.env.OPENAI_BASE_URL, |
| 152 | }) |
| 153 | |
| 154 | const modelName = |
| 155 | block.metadata.deepnote_agent_model !== 'auto' |
| 156 | ? block.metadata.deepnote_agent_model |
| 157 | : (process.env.OPENAI_MODEL ?? 'gpt-5') |
| 158 | const maxTurns = 10 |
| 159 | |
| 160 | // Use the Responses API for direct OpenAI access (supports reasoning |
| 161 | // summaries), but fall back to Chat Completions for custom base URLs |
| 162 | // since most OpenAI-compatible providers don't implement the Responses API. |
| 163 | const baseURL = process.env.OPENAI_BASE_URL |
| 164 | const model = baseURL ? openai.chat(modelName) : openai(modelName) |
| 165 | |
| 166 | const blockMcpServers = block.metadata.deepnote_mcp_servers ?? [] |
| 167 | const mergedMcpConfig = mergeMcpConfigs(context.mcpServers, blockMcpServers) |
| 168 | |
| 169 | const mcpClients = await Promise.all( |
| 170 | mergedMcpConfig.map(s => |
| 171 | createMCPClient({ |
| 172 | transport: new Experimental_StdioMCPTransport({ |
| 173 | command: s.command, |
| 174 | args: s.args, |
| 175 | env: resolveEnvVars(s.env), |
| 176 | stderr: 'pipe', |
| 177 | }), |
| 178 | }) |
| 179 | ) |
| 180 | ) |
| 181 | |
| 182 | const addCodeBlockTool = tool({ |
| 183 | description: |
| 184 | 'Add a Python code block to the notebook and execute it. Returns the combined output text or an error message.', |
| 185 | inputSchema: z.object({ |
| 186 | code: z.string().describe('Python code to execute'), |
| 187 | }), |
| 188 | execute: context.addAndExecuteCodeBlock, |
| 189 | }) |
| 190 | |
| 191 | const addMarkdownBlockTool = tool({ |
| 192 | description: 'Add a markdown block to the notebook for explanations, section headers, or documentation.', |
| 193 | inputSchema: z.object({ |
| 194 | content: z.string().describe('Markdown content'), |
| 195 | }), |
| 196 | execute: context.addMarkdownBlock, |
| 197 | }) |
| 198 | |
| 199 | const mcpToolSets = await Promise.all(mcpClients.map(client => client.tools())) |
| 200 | const mcpTools: Record<string, unknown> = Object.assign({}, ...mcpToolSets) |
| 201 | |
| 202 | const agent = new ToolLoopAgent({ |
| 203 | model, |
| 204 | instructions: buildSystemPrompt(context.notebookContext, context.integrations), |
| 205 | tools: { |
no test coverage detected